repo: cyfdecyf/mem-order

decompiled_code:
void __fastcall __noreturn tsx_assert_fail(const char *a1, unsigned int a2, const char *a3)
{
bool v3; // zf

v3 = has_rtm == 0;
if ( has_rtm )
{
while ( 1 )
{
__asm { xtest }
v3 = v3;
if ( v3 )
break;
__asm { xend }
}
}
fprintf(stderr, "Assertation failure: %s:%d: %s\n", a1, a2, a3);
abort();
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, unsigned int a2, const char *a3)
{
bool v3; // zf

v3 = has_rtm == 0;
if ( has_rtm )
{
while ( 1 )
{
__asm { xtest }
v3 = v3;
if ( v3 )
break;
__asm { xend }
}
}
fprintf(stderr, "Assertation failure: %s:%d: %s\n", a1, a2, a3);
abort();
}

original_code:
void tsx_assert_fail(char *file, int line, char *expr)
{
 if (has_rtm) {
  while (_xtest())
   _xend();
 }
 fprintf(stderr, "Assertation failure: %s:%d: %s\n", file, line, expr);
 abort();
}

variable_types:
file
char *

variable_types: line int

variable_types:
expr
char *


decompiled_code:
__int64 cpu_has_rtm()
{
unsigned int v0; // er8
__int64 result; // rax

v0 = _get_cpuid_max_constprop_0();
result = 0LL;
if ( v0 > 6 )
{
_RAX = 7LL;
__asm { cpuid }
result = ((unsigned int)_RBX >> 11) & 1;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // er8
__int64 result; // rax

v0 = func1();
result = 0LL;
if ( v0 > 6 )
{
_RAX = 7LL;
__asm { cpuid }
result = ((unsigned int)_RBX >> 11) & 1;
}
return result;
}

canonical_original_code:
static inline int cpu_has_rtm()
{
    if (__get_cpuid_max(0, ((void *)0)) >= 7) {
        unsigned int a; unsigned int b; unsigned int c; unsigned int d;
        __asm__ __volatile__ ("cpuid\n\t" : "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "0" (7), "2" (0));
        return !!(b & (1 << 11));
    }
    return 0;
}

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
c
unsigned int

variable_types:
d
unsigned int

return_type: int


init

decompiled_code:
__int64 init()
{
__int64 result; // rax

result = cpu_has_rtm();
has_rtm = result;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

result = func2();
has_rtm = result;
return result;
}

original_code:
static void __attribute__((constructor)) init(void)
{
 has_rtm = cpu_has_rtm();
}

canonical_original_code:
static void init()
{
 has_rtm = cpu_has_rtm();
}

return_type: void


decompiled_code:
__int64 next_wait_version_log()
{
__int64 result; // rax
__m128i v1; // xmm0

result = qword_8B0;
if ( *(_QWORD *)qword_8B0 == -1LL )
{
*(_QWORD *)&wait_version = -1LL;
}
else
{
v1 = _mm_loadu_si128((const __m128i *)qword_8B0);
result = qword_8B0 + 16;
qword_8B0 += 16LL;
wait_version = (__int128)v1;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__m128i v1; // xmm0

result = qword_8B0;
if ( *(_QWORD *)qword_8B0 == -1LL )
{
*(_QWORD *)&wait_version = -1LL;
}
else
{
v1 = _mm_loadu_si128((const __m128i *)qword_8B0);
result = qword_8B0 + 16;
qword_8B0 += 16LL;
wait_version = (__int128)v1;
}
return result;
}

original_code:
static inline void next_wait_version_log() {
    struct mapped_log *log = &wait_version_log;
    struct wait_version *wv = (struct wait_version *)(log->buf);
    if (wv->memop == -1) {
        wait_version.memop = -1;
        return;
    }
    wait_version = *wv;
    log->buf = (char *)(wv + 1);
}

variable_types: struct mapped_log { char *start; char *buf; char *end; int fd; } *

variable_types: struct wait_version { long memop; long version; } *


decompiled_code:
__int64 load_wait_reader_log()
{
__int64 result; // rax
__int64 v1; // rdx
__int64 v2; // rcx
int v3; // er13
__int64 v4; // rbx
__int64 v5; // rdi
__int64 v6; // rbp
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // rax
_QWORD v12[4]; // [rsp+0h] [rbp-68h] BYREF
char v13[8]; // [rsp+20h] [rbp-48h] BYREF
__int64 v14; // [rsp+28h] [rbp-40h]

wait_reader_log = calloc_check(g_nobj, 56LL, "Can't allocate wait_memop");
result = open_mapped_log_path("replay-log/memop", v12);
if ( (_DWORD)result )
{
if ( g_nobj > 0 )
{
result = wait_reader_log;
v1 = wait_reader_log + 56;
v2 = wait_reader_log + 56 + 56LL * (unsigned int)(g_nobj - 1);
while ( 1 )
{
*(_QWORD *)result = 0LL;
*(_DWORD *)(result + 8) = 0;
*(_DWORD *)(result + 12) = -1;
result = v1;
if ( v1 == v2 )
break;
v1 += 56LL;
}
}
}
else
{
v3 = open_mapped_log_path("replay-log/memop-index", v13);
if ( v3 )
{
puts("Can't open memop-index log");
exit(1);
}
v4 = v12[1];
if ( g_nobj > 0 )
{
v5 = wait_reader_log;
v6 = v14 + 8;
v7 = 0LL;
do
{
v9 = v7 + v5;
*(_DWORD *)(v9 + 8) = 0;
pthread_mutex_init((pthread_mutex_t *)(v9 + 16), 0LL);
v10 = *(int *)(v6 - 8);
v5 = wait_reader_log;
if ( (_DWORD)v10 == -1 )
{
v11 = wait_reader_log + v7;
*(_QWORD *)v11 = 0LL;
*(_DWORD *)(v11 + 12) = -1;
}
else
{
v8 = wait_reader_log + v7;
*(_QWORD *)v8 = v4 + 24 * v10;
*(_DWORD *)(v8 + 12) = *(_DWORD *)(v6 - 4);
}
++v3;
v7 += 56LL;
v6 += 8LL;
}
while ( g_nobj > v3 );
}
result = unmap_log(v13);
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int64 v1; // rdx
__int64 v2; // rcx
int v3; // er13
__int64 v4; // rbx
__int64 v5; // rdi
__int64 v6; // rbp
__int64 v7; // r12
__int64 v8; // rdx
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // rax
_QWORD v12[4]; // [rsp+0h] [rbp-68h] BYREF
char v13[8]; // [rsp+20h] [rbp-48h] BYREF
__int64 v14; // [rsp+28h] [rbp-40h]

wait_reader_log = calloc_check(g_nobj, 56LL, "Can't allocate wait_memop");
result = open_mapped_log_path("replay-log/memop", v12);
if ( (_DWORD)result )
{
if ( g_nobj > 0 )
{
result = wait_reader_log;
v1 = wait_reader_log + 56;
v2 = wait_reader_log + 56 + 56LL * (unsigned int)(g_nobj - 1);
while ( 1 )
{
*(_QWORD *)result = 0LL;
*(_DWORD *)(result + 8) = 0;
*(_DWORD *)(result + 12) = -1;
result = v1;
if ( v1 == v2 )
break;
v1 += 56LL;
}
}
}
else
{
v3 = open_mapped_log_path("replay-log/memop-index", v13);
if ( v3 )
{
puts("Can't open memop-index log");
exit(1);
}
v4 = v12[1];
if ( g_nobj > 0 )
{
v5 = wait_reader_log;
v6 = v14 + 8;
v7 = 0LL;
do
{
v9 = v7 + v5;
*(_DWORD *)(v9 + 8) = 0;
pthread_mutex_init((pthread_mutex_t *)(v9 + 16), 0LL);
v10 = *(int *)(v6 - 8);
v5 = wait_reader_log;
if ( (_DWORD)v10 == -1 )
{
v11 = wait_reader_log + v7;
*(_QWORD *)v11 = 0LL;
*(_DWORD *)(v11 + 12) = -1;
}
else
{
v8 = wait_reader_log + v7;
*(_QWORD *)v8 = v4 + 24 * v10;
*(_DWORD *)(v8 + 12) = *(_DWORD *)(v6 - 4);
}
++v3;
v7 += 56LL;
v6 += 8LL;
}
while ( g_nobj > v3 );
}
result = unmap_log(v13);
}
return result;
}

original_code:
static void load_wait_reader_log() {
    struct mapped_log memop_log, index_log;
    wait_reader_log = calloc_check(g_nobj, sizeof(*wait_reader_log), "Can't allocate wait_memop");
    if (open_mapped_log_path("replay-log/""memop", &memop_log) != 0) {
        ;
        for (int i = 0; i < g_nobj; i++) {
            wait_reader_log[i].log = ((void *)0);
            wait_reader_log[i].n = 0;
            wait_reader_log[i].size = -1;
        }
        return;
    }
    if (open_mapped_log_path("replay-log/""memop-index", &index_log) != 0) {
        printf("Can't open memop-index log\n");
        exit(1);
    }
    int *index = (int *)index_log.buf;
    struct replay_wait_memop *log_start = (struct replay_wait_memop *)memop_log.buf;
    for (int i = 0; i < g_nobj; i++) {
        wait_reader_log[i].n = 0;
        pthread_mutex_init(&wait_reader_log[i].mutex, ((void *)0));
        if (*index == -1) {
            wait_reader_log[i].log = ((void *)0);
            wait_reader_log[i].size = -1;
            index += 2;
            continue;
        }
        ;
        wait_reader_log[i].log = &log_start[*index++];
        wait_reader_log[i].size = *index++;
    }
    unmap_log(&index_log);
}

canonical_original_code:
static void load_wait_reader_log() {
    struct mapped_log memop_log; struct mapped_log index_log;
    wait_reader_log = calloc_check(g_nobj, sizeof(*wait_reader_log), "Can't allocate wait_memop");
    if (open_mapped_log_path("replay-log/""memop", &memop_log) != 0) {
        ;
        for (int i = 0; i < g_nobj; i++) {
            wait_reader_log[i].log = ((void *)0);
            wait_reader_log[i].n = 0;
            wait_reader_log[i].size = -1;
        }
        return;
    }
    if (open_mapped_log_path("replay-log/""memop-index", &index_log) != 0) {
        printf("Can't open memop-index log\n");
        exit(1);
    }
    int *index = (int *)index_log.buf;
    struct replay_wait_memop *log_start = (struct replay_wait_memop *)memop_log.buf;
    for (int i = 0; i < g_nobj; i++) {
        wait_reader_log[i].n = 0;
        pthread_mutex_init(&wait_reader_log[i].mutex, ((void *)0));
        if (*index == -1) {
            wait_reader_log[i].log = ((void *)0);
            wait_reader_log[i].size = -1;
            index += 2;
            continue;
        }
        ;
        wait_reader_log[i].log = &log_start[*index++];
        wait_reader_log[i].size = *index++;
    }
    unmap_log(&index_log);
}

variable_types: struct mapped_log { char *start; char *buf; char *end; int fd; }

variable_types: i int

variable_types:
index
int *

variable_types: struct replay_wait_memop { long version; long memop; char tid; } *


decompiled_code:
__int64 mem_finish()
{
return end_clock();
}

canonical_decompiled_code:
__int64 func7()
{
return end_clock();
}

original_code:
void mem_finish(tid_t nthr, int nobj) {
    end_clock();
}

canonical_original_code:
void mem_finish(char nthr, int nobj) {
    end_clock();
}

variable_types: nthr char

variable_types: nobj int


decompiled_code:
void begin_clock()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void begin_clock() {
}

canonical_original_code:
void begin_clock() {
}


decompiled_code:
void end_clock()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void end_clock() {
}

canonical_original_code:
void end_clock() {
}


decompiled_code:
int batch_process_log()
{
int result; // eax
int v1; // ebp
unsigned int *v2; // rbx
unsigned int *v3; // rdi

result = g_batch_idx;
if ( g_batch_idx <= 0 )
{
g_batch_idx = 0;
}
else
{
v1 = 0;
v2 = (unsigned int *)((char *)&g_batch_q + __readfsqword(0));
do
{
v3 = v2;
++v1;
v2 += 4;
result = process_1log(v3);
}
while ( g_batch_idx > v1 );
g_batch_idx = 0;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax
int v1; // ebp
unsigned int *v2; // rbx
unsigned int *v3; // rdi

result = g_batch_idx;
if ( g_batch_idx <= 0 )
{
g_batch_idx = 0;
}
else
{
v1 = 0;
v2 = (unsigned int *)((char *)&g_batch_q + __readfsqword(0));
do
{
v3 = v2;
++v1;
v2 += 4;
result = func1(v3);
}
while ( g_batch_idx > v1 );
g_batch_idx = 0;
}
return result;
}

original_code:
static void batch_process_log() {
    int i;
    for (i = 0; i < g_batch_idx; i++) {
        process_1log(&g_batch_q[i]);
    }
    g_batch_idx = 0;
}


decompiled_code:
__int64 __fastcall spin_lock(_BYTE *a1)
{
__int64 result; // rax

while ( 1 )
{
result = xchg_8_constprop_0(a1);
if ( !(_WORD)result )
break;
while ( *a1 )
{
do
_mm_pause();
while ( *a1 );
result = xchg_8_constprop_0(a1);
if ( !(_WORD)result )
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_BYTE *a1)
{
__int64 result; // rax

while ( 1 )
{
result = func6(a1);
if ( !(_WORD)result )
break;
while ( *a1 )
{
do
_mm_pause();
while ( *a1 );
result = func6(a1);
if ( !(_WORD)result )
return result;
}
}
return result;
}

original_code:
static inline void spin_lock(spinlock *lock)
{
    while (1) {
        if (!xchg_8(lock, 1)) return;
        while (*lock) asm volatile("pause\n": : :"memory");
    }
}

canonical_original_code:
static inline void spin_lock(unsigned char *lock)
{
    while (1) {
        if (!xchg_8(lock, 1)) return;
        while (*lock) asm volatile("pause\n": : :"memory");
    }
}

variable_types:
lock
unsigned char *


decompiled_code:
__int64 __fastcall calc_objid_addcnt(__int64 a1)
{
return (a1 - g_obj) >> 3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return (a1 - g_obj) >> 3;
}

original_code:
objid_t calc_objid_addcnt(void *addr) {
    return ((long)addr - (long)g_obj) >> 3;
}

canonical_original_code:
int calc_objid_addcnt(void *addr) {
    return ((long)addr - (long)g_obj) >> 3;
}

variable_types:
addr
void *


decompiled_code:
__int64 thr_end()
{
return mem_finish_thr();
}

canonical_decompiled_code:
__int64 func1()
{
return mem_finish_thr();
}

original_code:
static inline void thr_end() {
    mem_finish_thr();
}


handler

decompiled_code:
void __fastcall __noreturn handler(unsigned int a1)
{
int v1; // ebp
void *v2[13]; // [rsp+0h] [rbp-68h] BYREF

v1 = backtrace(v2, 10);
fprintf(stderr, "Error: signal %d:\n", a1);
backtrace_symbols_fd(v2, v1, 2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(unsigned int a1)
{
int v1; // ebp
void *v2[13]; // [rsp+0h] [rbp-68h] BYREF

v1 = backtrace(v2, 10);
fprintf(stderr, "Error: signal %d:\n", a1);
backtrace_symbols_fd(v2, v1, 2);
exit(1);
}

original_code:
static void handler(int sig)
{
    void *array[10];
    size_t size;
    size = backtrace(array, 10);
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, 2);
    exit(1);
}

canonical_original_code:
static void handler(int sig)
{
    void *array[10];
    unsigned long size;
    size = backtrace(array, 10);
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, 2);
    exit(1);
}

variable_types: sig int

variable_types:
array
void *[10]

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall thr_start(char a1)
{
mem_init_thr((unsigned int)a1);
return sync_thread_constprop_0();
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1)
{
mem_init_thr((unsigned int)a1);
return func3();
}

original_code:
static inline void thr_start(void *_tid) {
    mem_init_thr((tid_t)(long)_tid);
    sync_thread(&start_flag);
}

canonical_original_code:
static inline void thr_start(void *_tid) {
    mem_init_thr((char)(long)_tid);
    sync_thread(&start_flag);
}


decompiled_code:
__int64 __fastcall access_thr_fn4(char a1)
{
int v1; // er12
__int64 v2; // rbx
__int64 v3; // r13
__int64 v4; // r14
int v5; // eax
int v6; // eax

v1 = 20000;
thr_start(a1);
do
{
v2 = 72LL;
do
{
v3 = g_obj + v2;
v4 = g_obj + v2 - 8;
v2 -= 16LL;
v5 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4, (unsigned int)(v5 + 1));
v6 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v6 + 2));
}
while ( v2 != -8 );
--v1;
}
while ( v1 );
thr_end();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1)
{
int v1; // er12
__int64 v2; // rbx
__int64 v3; // r13
__int64 v4; // r14
int v5; // eax
int v6; // eax

v1 = 20000;
func4(a1);
do
{
v2 = 72LL;
do
{
v3 = g_obj + v2;
v4 = g_obj + v2 - 8;
v2 -= 16LL;
v5 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4, (unsigned int)(v5 + 1));
v6 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v6 + 2));
}
while ( v2 != -8 );
--v1;
}
while ( v1 );
func1();
return 0LL;
}

original_code:
static void *access_thr_fn4(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 10 - 1; j > 0; j -= 2) {
            uint32_t *addrj = (uint32_t *)&g_obj[j];
            uint32_t *addrj1 = (uint32_t *)&g_obj[j-1];
            uint32_t val = mem_read(g_tid, addrj);
            mem_write(g_tid, addrj1, val + 1);
            val = mem_read(g_tid, addrj1);
            mem_write(g_tid, addrj, val + 2);
        }
    }
    thr_end();
    return ((void *)0);
}

canonical_original_code:
static void *access_thr_fn4(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 10 - 1; j > 0; j -= 2) {
            unsigned int *addrj = (uint32_t *)&g_obj[j];
            unsigned int *addrj1 = (uint32_t *)&g_obj[j-1];
            unsigned int val = mem_read(g_tid, addrj);
            mem_write(g_tid, addrj1, val + 1);
            val = mem_read(g_tid, addrj1);
            mem_write(g_tid, addrj, val + 2);
        }
    }
    thr_end();
    return ((void *)0);
}

variable_types:
dummyid
void *

variable_types: j int

variable_types:
addrj
unsigned int *

variable_types:
val
unsigned int

return_type: void *


decompiled_code:
__int64 __fastcall access_thr_fn3(char a1)
{
int v1; // er12
__int64 v2; // rbx
__int64 v3; // r13
__int64 v4; // r14
int v5; // eax
int v6; // eax

v1 = 20000;
thr_start(a1);
do
{
v2 = 0LL;
do
{
v3 = g_obj + v2;
v4 = g_obj + v2 + 8;
v2 += 16LL;
v5 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4, (unsigned int)(v5 + 1));
v6 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v6 + 2));
}
while ( v2 != 80 );
--v1;
}
while ( v1 );
thr_end();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1)
{
int v1; // er12
__int64 v2; // rbx
__int64 v3; // r13
__int64 v4; // r14
int v5; // eax
int v6; // eax

v1 = 20000;
func4(a1);
do
{
v2 = 0LL;
do
{
v3 = g_obj + v2;
v4 = g_obj + v2 + 8;
v2 += 16LL;
v5 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4, (unsigned int)(v5 + 1));
v6 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v4);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v6 + 2));
}
while ( v2 != 80 );
--v1;
}
while ( v1 );
func1();
return 0LL;
}

original_code:
static void *access_thr_fn3(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 0; j < 10 - 1; j += 2) {
            uint32_t *addrj = (uint32_t *)&g_obj[j];
            uint32_t *addrj1 = (uint32_t *)&g_obj[j+1];
            uint32_t val = mem_read(g_tid, addrj);
            mem_write(g_tid, addrj1, val + 1);
            val = mem_read(g_tid, addrj1);
            mem_write(g_tid, addrj, val + 2);
        }
    }
    thr_end();
    return ((void *)0);
}

canonical_original_code:
static void *access_thr_fn3(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 0; j < 10 - 1; j += 2) {
            unsigned int *addrj = (uint32_t *)&g_obj[j];
            unsigned int *addrj1 = (uint32_t *)&g_obj[j+1];
            unsigned int val = mem_read(g_tid, addrj);
            mem_write(g_tid, addrj1, val + 1);
            val = mem_read(g_tid, addrj1);
            mem_write(g_tid, addrj, val + 2);
        }
    }
    thr_end();
    return ((void *)0);
}


decompiled_code:
__int64 __fastcall access_thr_fn2(char a1)
{
int v1; // er13
__int64 i; // rbx
__int64 v3; // rbp
int v4; // eax

v1 = 20000;
thr_start(a1);
do
{
for ( i = 72LL; i != -8; i -= 8LL )
{
v3 = i + g_obj;
v4 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v4 + 1));
}
--v1;
}
while ( v1 );
thr_end();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
int v1; // er13
__int64 i; // rbx
__int64 v3; // rbp
int v4; // eax

v1 = 20000;
func4(a1);
do
{
for ( i = 72LL; i != -8; i -= 8LL )
{
v3 = i + g_obj;
v4 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v4 + 1));
}
--v1;
}
while ( v1 );
func1();
return 0LL;
}

original_code:
static void *access_thr_fn2(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 10 - 1; j > -1; j--) {
            uint32_t *addr = (uint32_t *)&g_obj[j];
            uint32_t val = mem_read(g_tid, addr);
            mem_write(g_tid, addr, val + 1);
        }
    }
    thr_end();
    return ((void *)0);
}

canonical_original_code:
static void *access_thr_fn2(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 10 - 1; j > -1; j--) {
            unsigned int *addr = (uint32_t *)&g_obj[j];
            unsigned int val = mem_read(g_tid, addr);
            mem_write(g_tid, addr, val + 1);
        }
    }
    thr_end();
    return ((void *)0);
}

variable_types:
addr
unsigned int *


decompiled_code:
__int64 __fastcall access_thr_fn1(char a1)
{
int v1; // er13
__int64 i; // rbx
__int64 v3; // rbp
int v4; // eax

v1 = 20000;
thr_start(a1);
do
{
for ( i = 0LL; i != 80; i += 8LL )
{
v3 = i + g_obj;
v4 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v4 + 1));
}
--v1;
}
while ( v1 );
thr_end();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1)
{
int v1; // er13
__int64 i; // rbx
__int64 v3; // rbp
int v4; // eax

v1 = 20000;
func4(a1);
do
{
for ( i = 0LL; i != 80; i += 8LL )
{
v3 = i + g_obj;
v4 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v3, (unsigned int)(v4 + 1));
}
--v1;
}
while ( v1 );
func1();
return 0LL;
}

original_code:
static void *access_thr_fn1(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 0; j < 10; j++) {
            uint32_t *addr = (uint32_t *)&g_obj[j];
            uint32_t val = mem_read(g_tid, addr);
            mem_write(g_tid, addr, val + 1);
        }
    }
    thr_end();
    return ((void *)0);
}

canonical_original_code:
static void *access_thr_fn1(void *dummyid) {
    thr_start(dummyid);
    for (int i = 0; i < 20000; i++) {
        for (int j = 0; j < 10; j++) {
            unsigned int *addr = (uint32_t *)&g_obj[j];
            unsigned int val = mem_read(g_tid, addr);
            mem_write(g_tid, addr, val + 1);
        }
    }
    thr_end();
    return ((void *)0);
}


decompiled_code:
int print_g_obj()
{
__int64 i; // rbx
__int64 v1; // rsi
int result; // eax

for ( i = 0LL; i != 80; i += 8LL )
{
v1 = *(_QWORD *)(g_obj + i);
result = printf("%lx\n", v1);
}
return result;
}

canonical_decompiled_code:
int func9()
{
__int64 i; // rbx
__int64 v1; // rsi
int result; // eax

for ( i = 0LL; i != 80; i += 8LL )
{
v1 = *(_QWORD *)(g_obj + i);
result = printf("%lx\n", v1);
}
return result;
}

original_code:
void print_g_obj(void) {
    for (int i = 0; i < 10; i++) {
        printf("%lx\n", (long)g_obj[i]);
    }
}

canonical_original_code:
void print_g_obj() {
    for (int i = 0; i < 10; i++) {
        printf("%lx\n", (long)g_obj[i]);
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r12
pthread_t *v4; // rbp
void *(*v5)(void *); // rdx
char *i; // rbx
__int64 v7; // rbx
pthread_t v8; // rdi
void *memptr; // [rsp+8h] [rbp-20h] BYREF

if ( argc != 2 )
{
printf("Usage: %s <no of threads>\n", *argv);
exit(1);
}
if ( posix_memalign(&memptr, 8uLL, 0x50uLL) )
{
puts("memory allocation for objs failed");
exit(1);
}
calc_objid = calc_objid_addcnt;
g_obj = (__int64)memptr;
signal(11, (__sighandler_t)handler);
nthr = atoi(argv[1]);
v3 = calloc_check(nthr, 8LL, "pthread_t array thr");
mem_init((unsigned int)(char)nthr, 10LL);
if ( nthr > 0 )
{
v4 = (pthread_t *)v3;
v5 = (void *(*)(void *))access_thr_fn1;
for ( i = 0LL; ; v5 = *(void *(**)(void *))((char *)thr_fn + ((unsigned __int8)i & 0x18)) )
{
if ( pthread_create(v4, 0LL, v5, i) )
{
puts("thread creation failed");
exit(1);
}
++i;
++v4;
if ( nthr <= (__int64)i )
break;
}
if ( nthr > 0 )
{
v7 = 0LL;
do
{
v8 = *(_QWORD *)(v3 + 8 * v7++);
pthread_join(v8, 0LL);
}
while ( nthr > v7 );
}
}
print_g_obj();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r12
pthread_t *v4; // rbp
void *(*v5)(void *); // rdx
char *i; // rbx
__int64 v7; // rbx
pthread_t v8; // rdi
void *memptr; // [rsp+8h] [rbp-20h] BYREF

if ( argc != 2 )
{
printf("Usage: %s <no of threads>\n", *argv);
exit(1);
}
if ( posix_memalign(&memptr, 8uLL, 0x50uLL) )
{
puts("memory allocation for objs failed");
exit(1);
}
calc_objid = calc_objid_addcnt;
g_obj = (__int64)memptr;
signal(11, (__sighandler_t)handler);
nthr = atoi(argv[1]);
v3 = calloc_check(nthr, 8LL, "pthread_t array thr");
mem_init((unsigned int)(char)nthr, 10LL);
if ( nthr > 0 )
{
v4 = (pthread_t *)v3;
v5 = (void *(*)(void *))access_thr_fn1;
for ( i = 0LL; ; v5 = *(void *(**)(void *))((char *)thr_fn + ((unsigned __int8)i & 0x18)) )
{
if ( pthread_create(v4, 0LL, v5, i) )
{
puts("thread creation failed");
exit(1);
}
++i;
++v4;
if ( nthr <= (__int64)i )
break;
}
if ( nthr > 0 )
{
v7 = 0LL;
do
{
v8 = *(_QWORD *)(v3 + 8 * v7++);
pthread_join(v8, 0LL);
}
while ( nthr > v7 );
}
}
func9();
return 0;
}

original_code:
int main(int argc, const char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <no of threads>\n", argv[0]);
        exit(1);
    }
    if (posix_memalign((void **)&g_obj, sizeof(*g_obj), 10 * sizeof(*g_obj)) != 0) {
        printf("memory allocation for objs failed\n");
        exit(1);
    }
    calc_objid = calc_objid_addcnt;
    signal(11, handler);
    nthr = atoi(argv[1]);
    pthread_t *thr;
    thr = calloc_check(nthr, sizeof(pthread_t), "pthread_t array thr");
    mem_init((tid_t)nthr, 10);
    for (long i = 0; i < nthr; i++) {
        if (pthread_create(&thr[i], ((void *)0), thr_fn[i % sizeof(thr_fn)/sizeof(thr_fn[0])], (void *)i) != 0) {
            printf("thread creation failed\n");
            exit(1);
        }
    }
    for (long i = 0; i < nthr; i++) {
        pthread_join(thr[i], ((void *)0));
    }
    print_g_obj();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <no of threads>\n", argv[0]);
        exit(1);
    }
    if (posix_memalign((void **)&g_obj, sizeof(*g_obj), 10 * sizeof(*g_obj)) != 0) {
        printf("memory allocation for objs failed\n");
        exit(1);
    }
    calc_objid = calc_objid_addcnt;
    signal(11, handler);
    nthr = atoi(argv[1]);
    unsigned long *thr;
    thr = calloc_check(nthr, sizeof(pthread_t), "pthread_t array thr");
    mem_init((char)nthr, 10);
    for (long i = 0; i < nthr; i++) {
        if (pthread_create(&thr[i], ((void *)0), thr_fn[i % sizeof(thr_fn)/sizeof(thr_fn[0])], (void *)i) != 0) {
            printf("thread creation failed\n");
            exit(1);
        }
    }
    for (long i = 0; i < nthr; i++) {
        pthread_join(thr[i], ((void *)0));
    }
    print_g_obj();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
thr
unsigned long *

variable_types: i long


logpath

decompiled_code:
int __fastcall logpath(char *a1, const char *a2, __int64 a3)
{
int result; // eax

result = snprintf(a1, 0x100uLL, "replay-log/%s-%ld", a2, a3);
if ( result > 255 )
{
puts("Path name too long");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *a1, const char *a2, __int64 a3)
{
int result; // eax

result = snprintf(a1, 0x100uLL, "replay-log/%s-%ld", a2, a3);
if ( result > 255 )
{
puts("Path name too long");
exit(1);
}
return result;
}

original_code:
static inline void logpath(char *buf, const char *name, long id) {
    if (snprintf(buf, 256, "replay-log/""%s-%ld", name, id) >= 256) {
        printf("Path name too long\n");
        exit(1);
    }
}

canonical_original_code:
static inline void logpath(char *buf, char *name, long id) {
    if (snprintf(buf, 256, "replay-log/""%s-%ld", name, id) >= 256) {
        printf("Path name too long\n");
        exit(1);
    }
}

variable_types:
buf
char *

variable_types:
name
char *

variable_types: id long


decompiled_code:
__int64 __fastcall new_mapped_log_path(const char *a1, __int64 a2)
{
int v2; // eax
char *v3; // rax

v2 = open(a1, 578, 420LL);
*(_DWORD *)(a2 + 24) = v2;
if ( v2 == -1 )
{
perror("creat in new_mapped_log");
exit(1);
}
if ( ftruncate(v2, 4096LL) == -1 )
{
perror("ftruncate in new_mapped_log");
exit(1);
}
v3 = (char *)mmap(0LL, 0x1000uLL, 3, 1, *(_DWORD *)(a2 + 24), 0LL);
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)a2 = v3;
if ( v3 == (char *)-1LL )
{
perror("mmap in new_mapped_log");
exit(1);
}
*(_QWORD *)(a2 + 16) = v3 + 4096;
if ( madvise(v3, 0x1000uLL, 2) == -1 )
{
perror("madvise in new_mapped_log");
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, __int64 a2)
{
int v2; // eax
char *v3; // rax

v2 = open(a1, 578, 420LL);
*(_DWORD *)(a2 + 24) = v2;
if ( v2 == -1 )
{
perror("creat in new_mapped_log");
exit(1);
}
if ( ftruncate(v2, 4096LL) == -1 )
{
perror("ftruncate in new_mapped_log");
exit(1);
}
v3 = (char *)mmap(0LL, 0x1000uLL, 3, 1, *(_DWORD *)(a2 + 24), 0LL);
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)a2 = v3;
if ( v3 == (char *)-1LL )
{
perror("mmap in new_mapped_log");
exit(1);
}
*(_QWORD *)(a2 + 16) = v3 + 4096;
if ( madvise(v3, 0x1000uLL, 2) == -1 )
{
perror("madvise in new_mapped_log");
exit(1);
}
return 0LL;
}

original_code:
int new_mapped_log_path(const char *path, struct mapped_log *log) {
    log->fd = open(path, 02|0100|01000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (log->fd == -1) {
        perror("creat in new_mapped_log");
        exit(1);
    }
    if (ftruncate(log->fd, LOG_BUFFER_SIZE) == -1) {
        perror("ftruncate in new_mapped_log");
        exit(1);
    }
    log->start = log->buf = mmap(0, LOG_BUFFER_SIZE, 0x2|0x1, 0x01, log->fd, 0);
    if (log->buf == ((void *) -1)) {
        perror("mmap in new_mapped_log");
        exit(1);
    }
    log->end = log->start + LOG_BUFFER_SIZE;
    if (madvise(log->start, LOG_BUFFER_SIZE, 2) == -1) {
        perror("madvise in new_mapped_log");
        exit(1);
    }
    return 0;
}

canonical_original_code:
int new_mapped_log_path(char *path, struct mapped_log *log) {
    log->fd = open(path, 02|0100|01000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (log->fd == -1) {
        perror("creat in new_mapped_log");
        exit(1);
    }
    if (ftruncate(log->fd, LOG_BUFFER_SIZE) == -1) {
        perror("ftruncate in new_mapped_log");
        exit(1);
    }
    log->start = log->buf = mmap(0, LOG_BUFFER_SIZE, 0x2|0x1, 0x01, log->fd, 0);
    if (log->buf == ((void *) -1)) {
        perror("mmap in new_mapped_log");
        exit(1);
    }
    log->end = log->start + LOG_BUFFER_SIZE;
    if (madvise(log->start, LOG_BUFFER_SIZE, 2) == -1) {
        perror("madvise in new_mapped_log");
        exit(1);
    }
    return 0;
}

variable_types:
path
char *

variable_types: struct mapped_log { char *start; char *buf; char *end; int fd; } *


decompiled_code:
__int64 __fastcall new_mapped_log(const char *a1, int a2, __int64 a3)
{
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

logpath(v5, a1, a2);
return new_mapped_log_path(v5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2, __int64 a3)
{
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

func0(v5, a1, a2);
return func1(v5, a3);
}

original_code:
int new_mapped_log(const char *name, int id, struct mapped_log *log) {
    char path[256];
    logpath(path, name, id);
    return new_mapped_log_path(path, log);
}

canonical_original_code:
int new_mapped_log(char *name, int id, struct mapped_log *log) {
    char path[256];
    logpath(path, name, id);
    return new_mapped_log_path(path, log);
}

variable_types: id int

variable_types:
path
char [256]

user_defined_types: struct mapped_log { char *start; char *buf; char *end; int fd; }


decompiled_code:
__int64 __fastcall enlarge_mapped_log(__int64 a1)
{
__off_t v2; // rbp
char *v3; // rax
char *v4; // rdi
__int64 v5; // rax
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

if ( fstat(*(_DWORD *)(a1 + 24), &v7) == -1 )
{
perror("fstat in enlarge_mapped_log");
exit(1);
}
v2 = v7.st_size;
if ( (v7.st_size & 0xFFF) != 0 )
_assert_fail("original_size % LOG_BUFFER_SIZE == 0", "log.c", 0x4Au, "enlarge_mapped_log");
if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) == -1 )
{
perror("munmap in enlarge_mapped_log");
exit(1);
}
if ( ftruncate(*(_DWORD *)(a1 + 24), v2 + 4096) == -1 )
{
perror("ftruncate in enlarge_mapped_log");
exit(1);
}
v3 = (char *)mmap(0LL, 0x2000uLL, 3, 1, *(_DWORD *)(a1 + 24), v2 - 4096);
*(_QWORD *)a1 = v3;
v4 = v3;
if ( v3 == (char *)-1LL )
{
perror("mmap in enlarge_mapped_log");
exit(1);
}
*(_QWORD *)(a1 + 16) = v3 + 0x2000;
v5 = *(_QWORD *)(a1 + 8) & 0xFFFLL;
if ( (*(_QWORD *)(a1 + 8) & 0xFFF) == 0 )
v5 = 4096LL;
*(_QWORD *)(a1 + 8) = &v4[v5];
if ( madvise(v4, 0x2000uLL, 2) == -1 )
{
perror("madvise in enlarge_mapped_log");
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__off_t v2; // rbp
char *v3; // rax
char *v4; // rdi
__int64 v5; // rax
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

if ( fstat(*(_DWORD *)(a1 + 24), &v7) == -1 )
{
perror("fstat in enlarge_mapped_log");
exit(1);
}
v2 = v7.st_size;
if ( (v7.st_size & 0xFFF) != 0 )
_assert_fail("original_size % LOG_BUFFER_SIZE == 0", "log.c", 0x4Au, "enlarge_mapped_log");
if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) == -1 )
{
perror("munmap in enlarge_mapped_log");
exit(1);
}
if ( ftruncate(*(_DWORD *)(a1 + 24), v2 + 4096) == -1 )
{
perror("ftruncate in enlarge_mapped_log");
exit(1);
}
v3 = (char *)mmap(0LL, 0x2000uLL, 3, 1, *(_DWORD *)(a1 + 24), v2 - 4096);
*(_QWORD *)a1 = v3;
v4 = v3;
if ( v3 == (char *)-1LL )
{
perror("mmap in enlarge_mapped_log");
exit(1);
}
*(_QWORD *)(a1 + 16) = v3 + 0x2000;
v5 = *(_QWORD *)(a1 + 8) & 0xFFFLL;
if ( (*(_QWORD *)(a1 + 8) & 0xFFF) == 0 )
v5 = 4096LL;
*(_QWORD *)(a1 + 8) = &v4[v5];
if ( madvise(v4, 0x2000uLL, 2) == -1 )
{
perror("madvise in enlarge_mapped_log");
exit(1);
}
return 0LL;
}

original_code:
int enlarge_mapped_log(struct mapped_log *log) {
    struct stat sb;
    if (fstat(log->fd, &sb) == -1) {
        perror("fstat in enlarge_mapped_log");
        exit(1);
    }
    off_t original_size = sb.st_size;
    ((void) sizeof ((original_size % LOG_BUFFER_SIZE == 0) ? 1 : 0), __extension__ ({ if (original_size % LOG_BUFFER_SIZE == 0) ; else __assert_fail ("original_size % LOG_BUFFER_SIZE == 0", "log.c", 74, __extension__ __PRETTY_FUNCTION__); }));
    if (munmap(log->start, log->end - log->start) == -1) {
        perror("munmap in enlarge_mapped_log");
        exit(1);
    }
    if (ftruncate(log->fd, original_size + LOG_BUFFER_SIZE) == -1) {
        perror("ftruncate in enlarge_mapped_log");
        exit(1);
    }
    int map_size = LOG_BUFFER_SIZE + 4096;
    log->start = mmap(0, map_size, 0x2|0x1, 0x01,
        log->fd, original_size - 4096);
    if (log->start == ((void *) -1)) {
        perror("mmap in enlarge_mapped_log");
        exit(1);
    }
    log->end = log->start + map_size;
    long page_offset = (long)log->buf & 0xFFF;
    log->buf = log->start + (page_offset ? page_offset : 4096);
    if (madvise(log->start, map_size, 2) == -1) {
        perror("madvise in enlarge_mapped_log");
        exit(1);
    }
    return 0;
}

canonical_original_code:
int enlarge_mapped_log(struct mapped_log *log) {
    struct stat sb;
    if (fstat(log->fd, &sb) == -1) {
        perror("fstat in enlarge_mapped_log");
        exit(1);
    }
    long original_size = sb.st_size;
    ((void) sizeof ((original_size % LOG_BUFFER_SIZE == 0) ? 1 : 0), __extension__ ({ if (original_size % LOG_BUFFER_SIZE == 0) ; else __assert_fail ("original_size % LOG_BUFFER_SIZE == 0", "log.c", 74, __extension__ __PRETTY_FUNCTION__); }));
    if (munmap(log->start, log->end - log->start) == -1) {
        perror("munmap in enlarge_mapped_log");
        exit(1);
    }
    if (ftruncate(log->fd, original_size + LOG_BUFFER_SIZE) == -1) {
        perror("ftruncate in enlarge_mapped_log");
        exit(1);
    }
    int map_size = LOG_BUFFER_SIZE + 4096;
    log->start = mmap(0, map_size, 0x2|0x1, 0x01,
        log->fd, original_size - 4096);
    if (log->start == ((void *) -1)) {
        perror("mmap in enlarge_mapped_log");
        exit(1);
    }
    log->end = log->start + map_size;
    long page_offset = (long)log->buf & 0xFFF;
    log->buf = log->start + (page_offset ? page_offset : 4096);
    if (madvise(log->start, map_size, 2) == -1) {
        perror("madvise in enlarge_mapped_log");
        exit(1);
    }
    return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
int __fastcall open_mapped_log_path(const char *a1, __int64 a2)
{
int result; // eax
char *v4; // rax
__off_t v5; // rsi
struct stat v6; // [rsp+0h] [rbp-98h] BYREF

result = open(a1, 0);
*(_DWORD *)(a2 + 24) = result;
if ( result == -1 )
{
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)a2 = 0LL;
}
else
{
if ( fstat(result, &v6) == -1 )
{
perror("fstat");
exit(1);
}
v4 = (char *)mmap(0LL, v6.st_size, 1, 2, *(_DWORD *)(a2 + 24), 0LL);
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)a2 = v4;
if ( v4 == (char *)-1LL )
{
perror("mmap");
exit(1);
}
v5 = v6.st_size;
*(_QWORD *)(a2 + 16) = &v4[v6.st_size];
if ( madvise(v4, v5, 2) == -1 )
{
perror("madvise");
exit(1);
}
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(const char *a1, __int64 a2)
{
int result; // eax
char *v4; // rax
__off_t v5; // rsi
struct stat v6; // [rsp+0h] [rbp-98h] BYREF

result = open(a1, 0);
*(_DWORD *)(a2 + 24) = result;
if ( result == -1 )
{
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)a2 = 0LL;
}
else
{
if ( fstat(result, &v6) == -1 )
{
perror("fstat");
exit(1);
}
v4 = (char *)mmap(0LL, v6.st_size, 1, 2, *(_DWORD *)(a2 + 24), 0LL);
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)a2 = v4;
if ( v4 == (char *)-1LL )
{
perror("mmap");
exit(1);
}
v5 = v6.st_size;
*(_QWORD *)(a2 + 16) = &v4[v6.st_size];
if ( madvise(v4, v5, 2) == -1 )
{
perror("madvise");
exit(1);
}
result = 0;
}
return result;
}

original_code:
int open_mapped_log_path(const char *path, struct mapped_log *log) {
    log->fd = open(path, 00);
    if (log->fd == -1) {
        log->start = log->end = log->buf = ((void *)0);
        return -1;
    }
    struct stat sb;
    if (fstat(log->fd, &sb) == -1) {
        perror("fstat");
        exit(1);
    }
    log->start = log->buf = mmap(0, sb.st_size, 0x1, 0x02, log->fd, 0);
    if (log->buf == ((void *) -1)) {
        perror("mmap");
        exit(1);
    }
    log->end = log->buf + sb.st_size;
    if (madvise(log->buf, sb.st_size, 2) == -1) {
        perror("madvise");
        exit(1);
    }
    return 0;
}

canonical_original_code:
int open_mapped_log_path(char *path, struct mapped_log *log) {
    log->fd = open(path, 00);
    if (log->fd == -1) {
        log->start = log->end = log->buf = ((void *)0);
        return -1;
    }
    struct stat sb;
    if (fstat(log->fd, &sb) == -1) {
        perror("fstat");
        exit(1);
    }
    log->start = log->buf = mmap(0, sb.st_size, 0x1, 0x02, log->fd, 0);
    if (log->buf == ((void *) -1)) {
        perror("mmap");
        exit(1);
    }
    log->end = log->buf + sb.st_size;
    if (madvise(log->buf, sb.st_size, 2) == -1) {
        perror("madvise");
        exit(1);
    }
    return 0;
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
int __fastcall open_mapped_log(const char *a1, int a2, __int64 a3)
{
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

logpath(v5, a1, a2);
return open_mapped_log_path(v5, a3);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1, int a2, __int64 a3)
{
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

func0(v5, a1, a2);
return func4(v5, a3);
}

original_code:
int open_mapped_log(const char *name, int id, struct mapped_log *log) {
    char path[256];
    logpath(path, name, id);
    return open_mapped_log_path(path, log);
}

canonical_original_code:
int open_mapped_log(char *name, int id, struct mapped_log *log) {
    char path[256];
    logpath(path, name, id);
    return open_mapped_log_path(path, log);
}


decompiled_code:
__int64 __fastcall truncate_log(__int64 a1)
{
unsigned int v1; // er12
struct stat v3; // [rsp+0h] [rbp-A8h] BYREF

if ( fstat(*(_DWORD *)(a1 + 24), &v3) == -1 )
{
perror("fstat in unmap log");
exit(1);
}
v1 = ftruncate(*(_DWORD *)(a1 + 24), v3.st_size - (*(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8)));
if ( v1 == -1 )
{
printf("size: total %ld, garbage: %ld\n", v3.st_size, *(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8));
perror("ftruncate in unmap_log");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
unsigned int v1; // er12
struct stat v3; // [rsp+0h] [rbp-A8h] BYREF

if ( fstat(*(_DWORD *)(a1 + 24), &v3) == -1 )
{
perror("fstat in unmap log");
exit(1);
}
v1 = ftruncate(*(_DWORD *)(a1 + 24), v3.st_size - (*(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8)));
if ( v1 == -1 )
{
printf("size: total %ld, garbage: %ld\n", v3.st_size, *(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8));
perror("ftruncate in unmap_log");
}
return v1;
}

original_code:
int truncate_log(struct mapped_log *log) {
    struct stat sb;
    if (fstat(log->fd, &sb) == -1) {
        perror("fstat in unmap log");
        exit(1);
    }
    int ret = 0;
    if ((ret = ftruncate(log->fd, sb.st_size - (log->end - log->buf))) == -1) {
        printf("size: total %ld, garbage: %ld\n", sb.st_size, log->end - log->buf);
        perror("ftruncate in unmap_log");
    }
    return ret;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall unmap_log(__int64 a1)
{
if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) != -1 )
return 0LL;
perror("munmap in unmap_log");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) != -1 )
return 0LL;
perror("munmap in unmap_log");
return 0xFFFFFFFFLL;
}

original_code:
int unmap_log(struct mapped_log *log) {
    if (munmap(log->start, log->end - log->start) == -1) {
        perror("munmap in unmap_log");
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall unmap_truncate_log(__int64 a1)
{
__int64 result; // rax

result = unmap_log(a1);
if ( !(_DWORD)result )
result = truncate_log(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax

result = func7(a1);
if ( !(_DWORD)result )
result = func6(a1);
return result;
}

original_code:
int unmap_truncate_log(struct mapped_log *log) {
    int ret = unmap_log(log);
    if (ret != 0)
        return ret;
    return truncate_log(log);
}


decompiled_code:
void *__fastcall create_mapped_file(const char *a1, size_t a2)
{
int v2; // eax
int v3; // ebx
void *result; // rax

v2 = open(a1, 578, 420LL);
if ( v2 == -1 )
{
perror("open in create_mapped_file");
exit(1);
}
v3 = v2;
if ( ftruncate(v2, a2) == -1 )
{
perror("ftruncate in create_mapped_file");
exit(1);
}
result = mmap(0LL, a2, 3, 1, v3, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap in create_mapped_file");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func9(const char *a1, size_t a2)
{
int v2; // eax
int v3; // ebx
void *result; // rax

v2 = open(a1, 578, 420LL);
if ( v2 == -1 )
{
perror("open in create_mapped_file");
exit(1);
}
v3 = v2;
if ( ftruncate(v2, a2) == -1 )
{
perror("ftruncate in create_mapped_file");
exit(1);
}
result = mmap(0LL, a2, 3, 1, v3, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap in create_mapped_file");
exit(1);
}
return result;
}

original_code:
void *create_mapped_file(const char *path, unsigned long size) {
    int fd = open(path, 02|0100|01000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (fd == -1) {
        perror("open in create_mapped_file");
        exit(1);
    }
    if (ftruncate(fd, size) == -1) {
        perror("ftruncate in create_mapped_file");
        exit(1);
    }
    void *buf = mmap(0, size, 0x2|0x1, 0x01, fd, 0);
    if (buf == ((void *) -1)) {
        perror("mmap in create_mapped_file");
        exit(1);
    }
    return buf;
}

canonical_original_code:
void *create_mapped_file(char *path, unsigned long size) {
    int fd = open(path, 02|0100|01000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
    if (fd == -1) {
        perror("open in create_mapped_file");
        exit(1);
    }
    if (ftruncate(fd, size) == -1) {
        perror("ftruncate in create_mapped_file");
        exit(1);
    }
    void *buf = mmap(0, size, 0x2|0x1, 0x01, fd, 0);
    if (buf == ((void *) -1)) {
        perror("mmap in create_mapped_file");
        exit(1);
    }
    return buf;
}

variable_types:
size
unsigned long

variable_types: fd int

variable_types:
buf
void *


decompiled_code:
unsigned __int64 __fastcall calc_objid_racey(__int64 a1)
{
unsigned __int64 result; // rax

result = (unsigned __int64)(a1 - (_QWORD)&g_m) >> 6;
if ( (int)result > 63 )
_assert_fail("id < MAX_ELEM", "racey.c", 0x7Cu, "calc_objid_racey");
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
unsigned __int64 result; // rax

result = (unsigned __int64)(a1 - (_QWORD)&g_m) >> 6;
if ( (int)result > 63 )
_assert_fail("id < MAX_ELEM", "racey.c", 0x7Cu, "calc_objid_racey");
return result;
}

original_code:
objid_t calc_objid_racey(void *addr) {
    objid_t id = ((long)addr - (long)g_m)/sizeof(g_m[0]);
    ((void) sizeof ((id < 64) ? 1 : 0), __extension__ ({ if (id < 64) ; else __assert_fail ("id < MAX_ELEM", "racey.c", 124, __extension__ __PRETTY_FUNCTION__); }));
    return id;
}

canonical_original_code:
int calc_objid_racey(void *addr) {
    int id = ((long)addr - (long)g_m)/sizeof(g_m[0]);
    ((void) sizeof ((id < 64) ? 1 : 0), __extension__ ({ if (id < 64) ; else __assert_fail ("id < MAX_ELEM", "racey.c", 124, __extension__ __PRETTY_FUNCTION__); }));
    return id;
}

variable_types:
addr
void *


decompiled_code:
int __fastcall bind_core(unsigned __int64 a1)
{
int result; // eax
cpu_set_t v2; // [rsp+0h] [rbp-88h] BYREF

memset(&v2, 0, sizeof(v2));
if ( a1 <= 0x3FF )
v2.__bits[a1 >> 6] |= 1LL << a1;
result = sched_setaffinity(0, 0x80uLL, &v2);
if ( result )
{
printf("tid: %ld ", a1);
perror("Set affinity failed");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned __int64 a1)
{
int result; // eax
cpu_set_t v2; // [rsp+0h] [rbp-88h] BYREF

memset(&v2, 0, sizeof(v2));
if ( a1 <= 0x3FF )
v2.__bits[a1 >> 6] |= 1LL << a1;
result = sched_setaffinity(0, 0x80uLL, &v2);
if ( result )
{
printf("tid: %ld ", a1);
perror("Set affinity failed");
exit(1);
}
return result;
}

original_code:
static void bind_core(long threadid) {
    cpu_set_t set;
    do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ size_t __cpu = (threadid); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    if (sched_setaffinity(0, sizeof(set), &set) != 0) {
        printf("tid: %ld ", threadid);
        perror("Set affinity failed");
        exit(1);
    }
}

canonical_original_code:
static void bind_core(long threadid) {
    struct cpu_set_t set;
    do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ unsigned long __cpu = (threadid); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    if (sched_setaffinity(0, sizeof(set), &set) != 0) {
        printf("tid: %ld ", threadid);
        perror("Set affinity failed");
        exit(1);
    }
}

variable_types: threadid long

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


mix

canonical_original_code:
unsigned int mix(unsigned int i, unsigned int j) {
  return (i + j * 103995407) % 103072243;
}

variable_types:
i
unsigned int

variable_types:
j
unsigned int

return_type: unsigned int


ThreadBody

decompiled_code:
void *__fastcall ThreadBody(void *a1)
{
int v1; // er13
__int64 v2; // rdi
__int64 v3; // rbp
int v4; // eax
int v5; // ebp
int *v6; // r12
int v7; // eax

v1 = 1000000;
mem_init_thr((unsigned int)(char)a1);
bind_core((char)__readfsbyte((unsigned int)&g_tid));
sync_thread_isra_0();
v2 = (char)__readfsbyte((unsigned int)&g_tid);
LODWORD(v3) = g_sig[16 * v2];
do
{
v4 = mem_read(v2, &g_m[16 * (unsigned __int64)(v3 & 0x3F)]);
v5 = mix(v3, v4);
v6 = &g_m[16 * (unsigned __int64)(v5 & 0x3F)];
v7 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6);
v3 = (unsigned int)mix(v5, v7);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6, v3);
v2 = (char)__readfsbyte((unsigned int)&g_tid);
g_sig[16 * v2] = v3;
--v1;
}
while ( v1 );
mem_finish_thr();
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
int v1; // er13
__int64 v2; // rdi
__int64 v3; // rbp
int v4; // eax
int v5; // ebp
int *v6; // r12
int v7; // eax

v1 = 1000000;
mem_init_thr((unsigned int)(char)a1);
func1((char)__readfsbyte((unsigned int)&g_tid));
func2();
v2 = (char)__readfsbyte((unsigned int)&g_tid);
LODWORD(v3) = g_sig[16 * v2];
do
{
v4 = mem_read(v2, &g_m[16 * (unsigned __int64)(v3 & 0x3F)]);
v5 = func3(v3, v4);
v6 = &g_m[16 * (unsigned __int64)(v5 & 0x3F)];
v7 = mem_read((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6);
v3 = (unsigned int)func3(v5, v7);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6, v3);
v2 = (char)__readfsbyte((unsigned int)&g_tid);
g_sig[16 * v2] = v3;
--v1;
}
while ( v1 );
mem_finish_thr();
return 0LL;
}

original_code:
void* ThreadBody(void* _tid)
{
  int i;
  ;
  mem_init_thr((tid_t)(long)_tid);
  bind_core((long)g_tid);
  int v = sync_thread();
  if (v == NumProcs) ;
  for(i = 0 ; i < 1000000; i++) {
    uint32_t num = g_sig[g_tid].sigval;
    uint32_t index1 = num%64;
    uint32_t index2;
    num = mix(num, mem_read(g_tid, (uint32_t *)&g_m[index1].value));
    index2 = num%64;
    num = mix(num, mem_read(g_tid, (uint32_t *)&g_m[index2].value));
    mem_write(g_tid, (uint32_t *)&g_m[index2].value, num);
    g_sig[g_tid].sigval = num;
  }
  mem_finish_thr();
  ;
  return ((void *)0);
}

canonical_original_code:
void *ThreadBody(void *_tid)
{
  int i;
  ;
  mem_init_thr((char)(long)_tid);
  bind_core((long)g_tid);
  int v = sync_thread();
  if (v == NumProcs) ;
  for(i = 0 ; i < 1000000; i++) {
    unsigned int num = g_sig[g_tid].sigval;
    unsigned int index1 = num%64;
    unsigned int index2;
    num = mix(num, mem_read(g_tid, (unsigned int *)&g_m[index1].value));
    index2 = num%64;
    num = mix(num, mem_read(g_tid, (unsigned int *)&g_m[index2].value));
    mem_write(g_tid, (unsigned int *)&g_m[index2].value, num);
    g_sig[g_tid].sigval = num;
  }
  mem_finish_thr();
  ;
  return ((void *)0);
}

variable_types: i int

variable_types: v int

variable_types:
num
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rcx
int v4; // er8
__int64 v5; // rcx
__int64 v6; // rax
pthread_t *v7; // r12
char v8; // cl
pthread_t *v9; // rbp
char *v10; // rbx
__int64 v11; // rbx
unsigned int v12; // ebp
int *v13; // r9
int *v14; // r10
pthread_attr_t v16; // [rsp+0h] [rbp-58h] BYREF

if ( argc != 2 )
{
fprintf(stderr, "%s <numProcesors>\n", *argv);
exit(1);
}
NumProcs = atoi(argv[1]);
if ( (unsigned int)(NumProcs - 1) > 0xE )
_assert_fail("NumProcs > 0 && NumProcs < 16", "racey.c", 0xE2u, "main");
calc_objid = calc_objid_racey;
mem_init((unsigned int)NumProcs, 64LL);
LODWORD(v3) = 0;
do
{
v4 = mix(v3, v3);
v6 = v5;
v3 = v5 + 1;
g_m[16 * v6] = v4;
}
while ( v3 != 64 );
v7 = (pthread_t *)malloc(8LL * NumProcs);
if ( !v7 )
_assert_fail("threads != NULL", "racey.c", 0xF9u, "main");
pthread_attr_init(&v16);
pthread_attr_setscope(&v16, 0);
v8 = NumProcs;
if ( NumProcs <= 0 )
goto LABEL_18;
v9 = v7;
v10 = 0LL;
do
{
if ( pthread_create(v9, &v16, ThreadBody, v10) )
_assert_fail("ret == 0", "racey.c", 0x109u, "main");
v8 = NumProcs;
++v10;
++v9;
}
while ( NumProcs > (int)v10 );
if ( NumProcs <= 0 )
{
LABEL_18:
v12 = g_sig[0];
}
else
{
v11 = 0LL;
do
{
if ( pthread_join(v7[v11], 0LL) )
_assert_fail("ret == 0", "racey.c", 0x10Fu, "main");
v8 = NumProcs;
++v11;
}
while ( NumProcs > (int)v11 );
v12 = g_sig[0];
if ( NumProcs > 1 )
{
v13 = (int *)(unsigned int)((_DWORD)g_sig + 64);
do
v12 = mix(*v13, v12);
while ( v13 != v14 );
}
}
mem_finish((unsigned int)v8, 64LL);
printf("Short signature: %08x\n", v12);
fflush(stdout);
pthread_attr_destroy(&v16);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rcx
int v4; // er8
__int64 v5; // rcx
__int64 v6; // rax
pthread_t *v7; // r12
char v8; // cl
pthread_t *v9; // rbp
char *v10; // rbx
__int64 v11; // rbx
unsigned int v12; // ebp
int *v13; // r9
int *v14; // r10
pthread_attr_t v16; // [rsp+0h] [rbp-58h] BYREF

if ( argc != 2 )
{
fprintf(stderr, "%s <numProcesors>\n", *argv);
exit(1);
}
NumProcs = atoi(argv[1]);
if ( (unsigned int)(NumProcs - 1) > 0xE )
_assert_fail("NumProcs > 0 && NumProcs < 16", "racey.c", 0xE2u, "main");
calc_objid = calc_objid_racey;
mem_init((unsigned int)NumProcs, 64LL);
LODWORD(v3) = 0;
do
{
v4 = func3(v3, v3);
v6 = v5;
v3 = v5 + 1;
g_m[16 * v6] = v4;
}
while ( v3 != 64 );
v7 = (pthread_t *)malloc(8LL * NumProcs);
if ( !v7 )
_assert_fail("threads != NULL", "racey.c", 0xF9u, "main");
pthread_attr_init(&v16);
pthread_attr_setscope(&v16, 0);
v8 = NumProcs;
if ( NumProcs <= 0 )
goto LABEL_18;
v9 = v7;
v10 = 0LL;
do
{
if ( pthread_create(v9, &v16, ThreadBody, v10) )
_assert_fail("ret == 0", "racey.c", 0x109u, "main");
v8 = NumProcs;
++v10;
++v9;
}
while ( NumProcs > (int)v10 );
if ( NumProcs <= 0 )
{
LABEL_18:
v12 = g_sig[0];
}
else
{
v11 = 0LL;
do
{
if ( pthread_join(v7[v11], 0LL) )
_assert_fail("ret == 0", "racey.c", 0x10Fu, "main");
v8 = NumProcs;
++v11;
}
while ( NumProcs > (int)v11 );
v12 = g_sig[0];
if ( NumProcs > 1 )
{
v13 = (int *)(unsigned int)((_DWORD)g_sig + 64);
do
v12 = func3(*v13, v12);
while ( v13 != v14 );
}
}
mem_finish((unsigned int)v8, 64LL);
printf("Short signature: %08x\n", v12);
fflush(stdout);
pthread_attr_destroy(&v16);
return 0;
}

original_code:
int
main(int argc, char* argv[])
{
  pthread_t* threads;
  pthread_attr_t attr;
  int ret, i;
  uint32_t mix_sig;
  if(argc != 2) {
    fprintf(stderr, "%s <numProcesors>\n", argv[0]);
    exit(1);
  }
  NumProcs = atoi(argv[1]);
  ((void) sizeof ((NumProcs > 0 && NumProcs < 16) ? 1 : 0), __extension__ ({ if (NumProcs > 0 && NumProcs < 16) ; else __assert_fail ("NumProcs > 0 && NumProcs < 16", "racey.c", 226, __extension__ __PRETTY_FUNCTION__); }));
  calc_objid = calc_objid_racey;
  mem_init(NumProcs, 64);
  ;
  for(i = 0; i < 64; i++) {
    g_m[i].value = mix(i,i);
  }
  threads = (pthread_t *) malloc(sizeof(pthread_t) * NumProcs);
  ((void) sizeof ((threads != ((void *)0)) ? 1 : 0), __extension__ ({ if (threads != ((void *)0)) ; else __assert_fail ("threads != NULL", "racey.c", 249, __extension__ __PRETTY_FUNCTION__); }));
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  ;
  for(i=0; i < NumProcs; i++) {
    ret = pthread_create(&threads[i], &attr, ThreadBody, (void *)(long)i);
    ((void) sizeof ((ret == 0) ? 1 : 0), __extension__ ({ if (ret == 0) ; else __assert_fail ("ret == 0", "racey.c", 265, __extension__ __PRETTY_FUNCTION__); }));
  }
  for(i=0; i < NumProcs; i++) {
    ret = pthread_join(threads[i], ((void *)0));
    ((void) sizeof ((ret == 0) ? 1 : 0), __extension__ ({ if (ret == 0) ; else __assert_fail ("ret == 0", "racey.c", 271, __extension__ __PRETTY_FUNCTION__); }));
  }
  mix_sig = g_sig[0].sigval;
  for(i = 1; i < NumProcs ; i++) {
    mix_sig = mix(g_sig[i].sigval, mix_sig);
  }
  mem_finish(NumProcs, 64);
  ;
  ;
  printf("Short signature: %08x\n", mix_sig);
  fflush(stdout);
  pthread_attr_destroy(&attr);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  unsigned long *threads;
  union pthread_attr_t attr;
  int ret; int i;
  unsigned int mix_sig;
  if(argc != 2) {
    fprintf(stderr, "%s <numProcesors>\n", argv[0]);
    exit(1);
  }
  NumProcs = atoi(argv[1]);
  ((void) sizeof ((NumProcs > 0 && NumProcs < 16) ? 1 : 0), __extension__ ({ if (NumProcs > 0 && NumProcs < 16) ; else __assert_fail ("NumProcs > 0 && NumProcs < 16", "racey.c", 226, __extension__ __PRETTY_FUNCTION__); }));
  calc_objid = calc_objid_racey;
  mem_init(NumProcs, 64);
  ;
  for(i = 0; i < 64; i++) {
    g_m[i].value = mix(i,i);
  }
  threads = (unsigned long *) malloc(sizeof(pthread_t) * NumProcs);
  ((void) sizeof ((threads != ((void *)0)) ? 1 : 0), __extension__ ({ if (threads != ((void *)0)) ; else __assert_fail ("threads != NULL", "racey.c", 249, __extension__ __PRETTY_FUNCTION__); }));
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  ;
  for(i=0; i < NumProcs; i++) {
    ret = pthread_create(&threads[i], &attr, ThreadBody, (void *)(long)i);
    ((void) sizeof ((ret == 0) ? 1 : 0), __extension__ ({ if (ret == 0) ; else __assert_fail ("ret == 0", "racey.c", 265, __extension__ __PRETTY_FUNCTION__); }));
  }
  for(i=0; i < NumProcs; i++) {
    ret = pthread_join(threads[i], ((void *)0));
    ((void) sizeof ((ret == 0) ? 1 : 0), __extension__ ({ if (ret == 0) ; else __assert_fail ("ret == 0", "racey.c", 271, __extension__ __PRETTY_FUNCTION__); }));
  }
  mix_sig = g_sig[0].sigval;
  for(i = 1; i < NumProcs ; i++) {
    mix_sig = mix(g_sig[i].sigval, mix_sig);
  }
  mem_finish(NumProcs, 64);
  ;
  ;
  printf("Short signature: %08x\n", mix_sig);
  fflush(stdout);
  pthread_attr_destroy(&attr);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
threads
unsigned long *

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
__int64 __fastcall next_log_entry(__int64 a1, int a2)
{
__int64 result; // rax
unsigned __int64 v3; // rdx

result = *(_QWORD *)(a1 + 8);
v3 = result + a2;
if ( v3 > *(_QWORD *)(a1 + 16) )
{
enlarge_mapped_log();
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = result + a2;
}
else
{
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 result; // rax
unsigned __int64 v3; // rdx

result = *(_QWORD *)(a1 + 8);
v3 = result + a2;
if ( v3 > *(_QWORD *)(a1 + 16) )
{
enlarge_mapped_log();
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = result + a2;
}
else
{
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

original_code:
static inline char *next_log_entry(struct mapped_log *log, int entry_size) {
    if ((log->buf + entry_size) > log->end) {
        enlarge_mapped_log(log);
    }
    char *start = log->buf;
    log->buf += entry_size;
    return start;
}

variable_types: struct mapped_log { char *start; char *buf; char *end; int fd; } *

variable_types:
start
char *

return_type: char *


decompiled_code:
__int64 __fastcall log_other_wait_memop(int a1, __int64 *a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = next_memop_log_constprop_0();
v3 = *a2;
*(_DWORD *)result = a1;
*(_QWORD *)(result + 8) = v3;
*(_QWORD *)(result + 16) = a2[1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 *a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = func1();
v3 = *a2;
*(_DWORD *)result = a1;
*(_QWORD *)(result + 8) = v3;
*(_QWORD *)(result + 16) = a2[1];
return result;
}

original_code:
static inline void log_other_wait_memop(objid_t objid,
        struct last_objinfo *lastobj) {
    struct wait_memop *l = next_memop_log(&logs.wait_memop);
    l->objid = objid;
    l->version = lastobj->version;
    l->memop = lastobj->memop;
}

canonical_original_code:
static inline void log_other_wait_memop(int objid, struct last_objinfo *lastobj) {
    struct wait_memop *l = next_memop_log(&logs.wait_memop);
    l->objid = objid;
    l->version = lastobj->version;
    l->memop = lastobj->memop;
}

variable_types: objid int

variable_types: struct last_objinfo { long version; long memop; } *

variable_types: struct wait_memop { int objid; long version; long memop; } *


decompiled_code:
__int64 mark_log_end()
{
_QWORD *v0; // rax
__int64 result; // rax

v0 = (_QWORD *)next_version_log_constprop_0();
*v0 = -1LL;
v0[1] = -1LL;
result = next_memop_log_constprop_0();
*(_DWORD *)result = -1;
*(_QWORD *)(result + 8) = -1LL;
*(_QWORD *)(result + 16) = -1LL;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
_QWORD *v0; // rax
__int64 result; // rax

v0 = (_QWORD *)func3();
*v0 = -1LL;
v0[1] = -1LL;
result = func1();
*(_DWORD *)result = -1;
*(_QWORD *)(result + 8) = -1LL;
*(_QWORD *)(result + 16) = -1LL;
return result;
}

original_code:
static inline void mark_log_end() {
    struct wait_version *l = next_version_log(&logs.wait_version);
    l->memop = -1;
    l->version = -1;
    struct wait_memop *k = next_memop_log(&logs.wait_memop);
    k->objid = -1;
    k->version = -1;
    k->memop = -1;
}

variable_types: struct wait_version { long memop; long version; } *

user_defined_types: struct wait_memop { int objid; long version; long memop; }


decompiled_code:
__int64 __fastcall log_order(int a1, __int64 a2, __int64 *a3)
{
__int64 result; // rax

result = (__int64)log_wait_version(a2);
if ( a3[1] >= 0 )
result = log_other_wait_memop(a1, a3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2, __int64 *a3)
{
__int64 result; // rax

result = (__int64)log_wait_version(a2);
if ( a3[1] >= 0 )
result = func2(a1, a3);
return result;
}

original_code:
void log_order(objid_t objid, version_t current_version,
        struct last_objinfo *lastobj) {
    log_wait_version(current_version);
    if (lastobj->memop >= 0)
        log_other_wait_memop(objid, lastobj);
}

canonical_original_code:
void log_order(int objid, long current_version, struct last_objinfo *lastobj) {
    log_wait_version(current_version);
    if (lastobj->memop >= 0)
        log_other_wait_memop(objid, lastobj);
}

user_defined_types: struct last_objinfo { long version; long memop; }


decompiled_code:
__int64 mem_finish()
{
return end_clock();
}

canonical_decompiled_code:
__int64 func9()
{
return end_clock();
}

original_code:
void mem_finish(tid_t nthr, int nobj) {
    end_clock();
}

canonical_original_code:
void mem_finish(char nthr, int nobj) {
    end_clock();
}

variable_types: nthr char

variable_types: nobj int


decompiled_code:
void before_finish_thr()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code: void before_finish_thr() {}

canonical_original_code: void before_finish_thr() {}


decompiled_code:
size_t read_next_thr()
{
unsigned __int8 *v0; // rax
size_t result; // rax

v0 = (unsigned __int8 *)read_log_entry_constprop_0();
if ( !v0 )
return fwrite("no more threads in commit order log\n", 1uLL, 0x24uLL, stderr);
result = *v0;
g_next_thr = result;
return result;
}

canonical_decompiled_code:
size_t func1()
{
unsigned __int8 *v0; // rax
size_t result; // rax

v0 = (unsigned __int8 *)func0();
if ( !v0 )
return fwrite("no more threads in commit order log\n", 1uLL, 0x24uLL, stderr);
result = *v0;
g_next_thr = result;
return result;
}

original_code:
void read_next_thr() {
    tid_t *ptid = (tid_t *)read_log_entry(&g_commit_log, sizeof(tid_t));
    if (!ptid) {
        fprintf(stderr, "no more threads in commit order log\n");
        return;
    }
    g_next_thr = *ptid;
}

canonical_original_code:
void read_next_thr() {
    char *ptid = (tid_t *)read_log_entry(&g_commit_log, sizeof(tid_t));
    if (!ptid) {
        fprintf(stderr, "no more threads in commit order log\n");
        return;
    }
    g_next_thr = *ptid;
}

variable_types:
ptid
char *


canonical_decompiled_code:
__int64 func4()
{
return end_clock();
}


decompiled_code:
void *__fastcall calloc_check(size_t a1, size_t a2, const char *a3)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
{
printf("memory allocation failed: %s\n", a3);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1, size_t a2, const char *a3)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
{
printf("memory allocation failed: %s\n", a3);
exit(1);
}
return result;
}

original_code:
void *calloc_check(long nmemb, long size, const char *err_msg) {
    void *p = calloc(nmemb, size);
    if (!p) {
        printf("memory allocation failed: %s\n", err_msg);
        exit(1);
    }
    return p;
}

canonical_original_code:
void *calloc_check(long nmemb, long size, char *err_msg) {
    void *p = calloc(nmemb, size);
    if (!p) {
        printf("memory allocation failed: %s\n", err_msg);
        exit(1);
    }
    return p;
}

variable_types: nmemb long

variable_types: size long

variable_types:
p
void *


canonical_decompiled_code:
__int64 func6()
{
return end_clock();
}


repo: cyfdecyf/rwlock

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
pthread_t v5; // rdi
pthread_t th; // [rsp+0h] [rbp-58h] BYREF
pthread_t v8; // [rsp+8h] [rbp-50h] BYREF
pthread_t newthread[6]; // [rsp+10h] [rbp-48h] BYREF
char v10[24]; // [rsp+40h] [rbp-18h] BYREF

v3 = (char *)newthread;
v4 = (char *)newthread;
do
{
while ( !pthread_create((pthread_t *)v4, 0LL, reader, 0LL) )
{
v4 += 8;
if ( v10 == v4 )
goto LABEL_5;
}
v4 += 8;
perror("thread creating failed");
}
while ( v10 != v4 );
LABEL_5:
if ( pthread_create(&th, 0LL, writer, 0LL) )
perror("thread creating failed");
if ( pthread_create(&v8, 0LL, writer, 0LL) )
perror("thread creating failed");
do
{
v5 = *(_QWORD *)v3;
v3 += 8;
pthread_join(v5, 0LL);
}
while ( v10 != v3 );
pthread_join(th, 0LL);
pthread_join(v8, 0LL);
return 0;
}

original_code:
int main(int argc, const char *argv[])
{
    pthread_t thr_reader[NREADER], thr_writer[NWRITER];
    int i;
    for (i = 0; i < NREADER; i++) {
        if (pthread_create(&thr_reader[i], ((void *)0), reader, ((void *)0)) != 0) {
            perror("thread creating failed");
        }
    }
    for (i = 0; i < NWRITER; i++) {
        if (pthread_create(&thr_writer[i], ((void *)0), writer, ((void *)0)) != 0) {
            perror("thread creating failed");
        }
    }
    for (i = 0; i < NREADER; i++)
        pthread_join(thr_reader[i], ((void *)0));
    for (i = 0; i < NWRITER; i++)
        pthread_join(thr_writer[i], ((void *)0));
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    unsigned long thr_reader[-1]; unsigned long thr_writer[-1];
    int i;
    for (i = 0; i < NREADER; i++) {
        if (pthread_create(&thr_reader[i], ((void *)0), reader, ((void *)0)) != 0) {
            perror("thread creating failed");
        }
    }
    for (i = 0; i < NWRITER; i++) {
        if (pthread_create(&thr_writer[i], ((void *)0), writer, ((void *)0)) != 0) {
            perror("thread creating failed");
        }
    }
    for (i = 0; i < NREADER; i++)
        pthread_join(thr_reader[i], ((void *)0));
    for (i = 0; i < NWRITER; i++)
        pthread_join(thr_writer[i], ((void *)0));
    return 0;
}

variable_types: i int


decompiled_code:
__int64 __fastcall wait_flag(volatile signed __int32 *a1, int a2)
{
__int64 result; // rax

_InterlockedAdd(a1, 1u);
do
result = *(unsigned int *)a1;
while ( (_DWORD)result != a2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(volatile signed __int32 *a1, int a2)
{
__int64 result; // rax

_InterlockedAdd(a1, 1u);
do
result = *(unsigned int *)a1;
while ( (_DWORD)result != a2 );
return result;
}

original_code:
void wait_flag(volatile uint32_t *flag, uint32_t expect) {
    __sync_fetch_and_add((uint32_t *)flag, 1);
    while (*flag != expect);
}

canonical_original_code:
void wait_flag(unsigned int *flag, unsigned int expect) {
    __sync_fetch_and_add((unsigned int *)flag, 1);
    while (*flag != expect);
}

variable_types:
flag
unsigned int *

variable_types:
expect
unsigned int


writer

decompiled_code:
void *__fastcall writer(void *a1)
{
int v1; // ebx

v1 = 1000000;
wait_flag((volatile signed __int32 *)&wflag, 8);
do
{
tbb_start_write(&rwlock);
++shared;
++dword_404064;
++dword_404068;
tbb_end_write(&rwlock);
--v1;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
int v1; // ebx

v1 = 1000000;
func7((volatile signed __int32 *)&wflag, 8);
do
{
func10(&rwlock);
++shared;
++dword_404064;
++dword_404068;
func11(&rwlock);
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
void *writer(void *dummy) {
    int i;
    wait_flag(&wflag, NTHR);
    for (i = 0; i < NITER; i++) {
        tbb_start_write(&rwlock);
        shared[0]++;
        shared[1]++;
        shared[2]++;
        tbb_end_write(&rwlock);
    }
    return ((void *)0);
}

variable_types:
dummy
void *


reader

decompiled_code:
void *__fastcall reader(void *a1)
{
int v1; // ebx

v1 = 1000000;
wait_flag((volatile signed __int32 *)&wflag, 8);
do
{
tbb_start_read(&rwlock);
if ( dword_404064 != shared || dword_404068 != dword_404064 )
{
printf("ERROR");
exit(1);
}
tbb_end_read(&rwlock);
--v1;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func9(void *a1)
{
int v1; // ebx

v1 = 1000000;
func7((volatile signed __int32 *)&wflag, 8);
do
{
func12(&rwlock);
if ( dword_404064 != shared || dword_404068 != dword_404064 )
{
printf("ERROR");
exit(1);
}
func13(&rwlock);
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
void *reader(void *dummy) {
    int i;
    wait_flag(&wflag, NTHR);
    for (i = 0; i < NITER; i++) {
        tbb_start_read(&rwlock);
        uint32_t val = shared[0];
        if ((shared[1] != val) || (shared[2] != val)) {
            printf("ERROR");
            exit(1);
        }
        tbb_end_read(&rwlock);
    }
    return ((void *)0);
}

canonical_original_code:
void *reader(void *dummy) {
    int i;
    wait_flag(&wflag, NTHR);
    for (i = 0; i < NITER; i++) {
        tbb_start_read(&rwlock);
        unsigned int val = shared[0];
        if ((shared[1] != val) || (shared[2] != val)) {
            printf("ERROR");
            exit(1);
        }
        tbb_end_read(&rwlock);
    }
    return ((void *)0);
}

variable_types:
val
unsigned int


main

repo: cyfdecyf/spinlock

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er13
pthread_t *v5; // r12
pthread_t *v6; // rbp
__int64 v7; // rbx
pthread_t v8; // rdi

if ( argc != 2 )
{
printf("Usage: %s <num of threads>\n", *argv);
exit(1);
}
v3 = 0LL;
nthr = atoi(argv[1]);
v4 = nthr;
v5 = (pthread_t *)calloc(8uLL, nthr);
v6 = v5;
if ( v4 > 0 )
{
do
{
if ( pthread_create(v6, 0LL, inc_thread, v3) )
perror("thread creating failed");
++v3;
++v6;
}
while ( nthr > (__int64)v3 );
if ( nthr > 0 )
{
v7 = 0LL;
do
{
v8 = v5[v7++];
pthread_join(v8, 0LL);
}
while ( nthr > v7 );
}
}
calc_time_constprop_0();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er13
pthread_t *v5; // r12
pthread_t *v6; // rbp
__int64 v7; // rbx
pthread_t v8; // rdi

if ( argc != 2 )
{
printf("Usage: %s <num of threads>\n", *argv);
exit(1);
}
v3 = 0LL;
nthr = atoi(argv[1]);
v4 = nthr;
v5 = (pthread_t *)calloc(8uLL, nthr);
v6 = v5;
if ( v4 > 0 )
{
do
{
if ( pthread_create(v6, 0LL, inc_thread, v3) )
perror("thread creating failed");
++v3;
++v6;
}
while ( nthr > (__int64)v3 );
if ( nthr > 0 )
{
v7 = 0LL;
do
{
v8 = v5[v7++];
pthread_join(v8, 0LL);
}
while ( nthr > v7 );
}
}
func8();
return 0;
}

original_code:
int main(int argc, const char *argv[])
{
    pthread_t *thr;
    int ret = 0;
    if (argc != 2) {
        printf("Usage: %s <num of threads>\n", argv[0]);
        exit(1);
    }
    nthr = atoi(argv[1]);
    thr = calloc(sizeof(*thr), nthr);
    for (long i = 0; i < nthr; i++) {
        if (pthread_create(&thr[i], ((void *)0), inc_thread, (void *)i) != 0) {
            perror("thread creating failed");
        }
    }
    for (long i = 0; i < nthr; i++)
        pthread_join(thr[i], ((void *)0));
    calc_time(&start_time, &end_time);
    return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    unsigned long *thr;
    int ret = 0;
    if (argc != 2) {
        printf("Usage: %s <num of threads>\n", argv[0]);
        exit(1);
    }
    nthr = atoi(argv[1]);
    thr = calloc(sizeof(*thr), nthr);
    for (long i = 0; i < nthr; i++) {
        if (pthread_create(&thr[i], ((void *)0), inc_thread, (void *)i) != 0) {
            perror("thread creating failed");
        }
    }
    for (long i = 0; i < nthr; i++)
        pthread_join(thr[i], ((void *)0));
    calc_time(&start_time, &end_time);
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
thr
unsigned long *

variable_types: ret int

variable_types: i long


decompiled_code:
__int64 __fastcall xchg_64(volatile __int64 *a1, __int64 a2)
{
return _InterlockedExchange64(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(volatile __int64 *a1, __int64 a2)
{
return _InterlockedExchange64(a1, a2);
}

original_code:
static inline void *xchg_64(void *ptr, void *x)
{
    __asm__ __volatile__("xchgq %0,%1"
                :"=r" ((unsigned long long) x)
                :"m" (*(volatile long long *)ptr), "0" ((unsigned long long) x)
                :"memory");
    return x;
}

canonical_original_code:
static inline void *xchg_64(void *ptr, void *x)
{
    __asm__ __volatile__("xchgq %0,%1"
                :"=r" ((unsigned long long) x)
                :"m" (*(long long *)ptr), "0" ((unsigned long long) x)
                :"memory");
    return x;
}

variable_types:
ptr
void *

variable_types:
x
void *


decompiled_code:
__int64 __fastcall wait_flag(volatile signed __int32 *a1, int a2)
{
__int64 result; // rax

_InterlockedAdd(a1, 1u);
for ( result = *(unsigned int *)a1; (_DWORD)result != a2; result = *(unsigned int *)a1 )
_mm_pause();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(volatile signed __int32 *a1, int a2)
{
__int64 result; // rax

_InterlockedAdd(a1, 1u);
for ( result = *(unsigned int *)a1; (_DWORD)result != a2; result = *(unsigned int *)a1 )
_mm_pause();
return result;
}

original_code:
void wait_flag(volatile uint32_t *flag, uint32_t expect) {
    __sync_fetch_and_add((uint32_t *)flag, 1);
    while (*flag != expect) {
        asm volatile("pause\n": : :"memory");
    }
}

canonical_original_code:
void wait_flag(unsigned int *flag, unsigned int expect) {
    __sync_fetch_and_add((unsigned int *)flag, 1);
    while (*flag != expect) {
        asm volatile("pause\n": : :"memory");
    }
}


canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er13
pthread_t *v5; // r12
pthread_t *v6; // rbp
__int64 v7; // rbx
pthread_t v8; // rdi

if ( argc != 2 )
{
printf("Usage: %s <num of threads>\n", *argv);
exit(1);
}
v3 = 0LL;
nthr = atoi(argv[1]);
v4 = nthr;
v5 = (pthread_t *)calloc(8uLL, nthr);
v6 = v5;
if ( v4 > 0 )
{
do
{
if ( pthread_create(v6, 0LL, inc_thread, v3) )
perror("thread creating failed");
++v3;
++v6;
}
while ( nthr > (__int64)v3 );
if ( nthr > 0 )
{
v7 = 0LL;
do
{
v8 = v5[v7++];
pthread_join(v8, 0LL);
}
while ( nthr > v7 );
}
}
func7();
return 0;
}


canonical_decompiled_code:
__int64 __fastcall func10(volatile signed __int32 *a1, int a2)
{
__int64 result; // rax

_InterlockedAdd(a1, 1u);
for ( result = *(unsigned int *)a1; (_DWORD)result != a2; result = *(unsigned int *)a1 )
_mm_pause();
return result;
}


repo: cyfdecyf/traceopen

decompiled_code:
char *resolve_realopen()
{
void *v0; // rbp
char *result; // rax

v0 = dlopen("libc.so.6", 1);
result = dlerror();
if ( result || (realopen = dlsym(v0, "open"), (result = dlerror()) != 0LL) )
{
puts(result);
exit(1);
}
return result;
}

canonical_decompiled_code:
char *func6()
{
void *v0; // rbp
char *result; // rax

v0 = dlopen("libc.so.6", 1);
result = dlerror();
if ( result || (realopen = dlsym(v0, "open"), (result = dlerror()) != 0LL) )
{
puts(result);
exit(1);
}
return result;
}

original_code:
static void resolve_realopen() {
    void *handle;
    char *error;
    handle = dlopen("libc.so.6", 0x00001);
    if ((error = dlerror())) {
        puts(error);
        exit(1);
    }
    realopen = dlsym(handle, "open");
    if ((error = dlerror())) {
        puts(error);
        exit(1);
    }
}

variable_types:
handle
void *

variable_types:
error
char *


decompiled_code:
void open_outputfile()
{
char *v0; // rax
int v1; // ebp
char *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

v0 = getenv("TRACEDIR");
ptr[0] = 0LL;
fileopend = 1;
if ( v0 )
{
if ( asprintf(ptr, "%s/traceopenXXXXXX", v0) != -1 )
goto LABEL_3;
LABEL_7:
perror("asprintf failed");
return;
}
if ( asprintf(ptr, "/tmp/traceopenXXXXXX") == -1 )
goto LABEL_7;
LABEL_3:
v1 = mkstemp(ptr[0]);
free(ptr[0]);
ptr[0] = 0LL;
if ( v1 == -1 )
{
perror("mkstemp failed failed");
}
else
{
outputfile = fdopen(v1, "w");
if ( !outputfile )
perror("fdopen failed");
}
}

canonical_decompiled_code:
void func7()
{
char *v0; // rax
int v1; // ebp
char *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

v0 = getenv("TRACEDIR");
ptr[0] = 0LL;
fileopend = 1;
if ( v0 )
{
if ( asprintf(ptr, "%s/traceopenXXXXXX", v0) != -1 )
goto LABEL_3;
LABEL_7:
perror("asprintf failed");
return;
}
if ( asprintf(ptr, "/tmp/traceopenXXXXXX") == -1 )
goto LABEL_7;
LABEL_3:
v1 = mkstemp(ptr[0]);
free(ptr[0]);
ptr[0] = 0LL;
if ( v1 == -1 )
{
perror("mkstemp failed failed");
}
else
{
outputfile = fdopen(v1, "w");
if ( !outputfile )
perror("fdopen failed");
}
}

original_code:
static void open_outputfile() {
    int r;
    const char *envdir = getenv("TRACEDIR");
    char *filepath = ((void *)0);
    fileopend = 1;
    if (envdir) {
        r = asprintf(&filepath, "%s/" "traceopenXXXXXX", envdir);
    } else {
        r = asprintf(&filepath, "/tmp/" "traceopenXXXXXX");
    }
    if (r == -1) {
        perror("asprintf failed");
        return;
    }
    r = mkstemp(filepath);
    free(filepath);
    filepath = ((void *)0);
    if (r == -1) {
        perror("mkstemp failed failed");
        return;
    }
    outputfile = fdopen(r, "w");
    if (outputfile == ((void *)0)) {
        perror("fdopen failed");
        return;
    }
}

canonical_original_code:
static void open_outputfile() {
    int r;
    char *envdir = getenv("TRACEDIR");
    char *filepath = ((void *)0);
    fileopend = 1;
    if (envdir) {
        r = asprintf(&filepath, "%s/" "traceopenXXXXXX", envdir);
    } else {
        r = asprintf(&filepath, "/tmp/" "traceopenXXXXXX");
    }
    if (r == -1) {
        perror("asprintf failed");
        return;
    }
    r = mkstemp(filepath);
    free(filepath);
    filepath = ((void *)0);
    if (r == -1) {
        perror("mkstemp failed failed");
        return;
    }
    outputfile = fdopen(r, "w");
    if (outputfile == ((void *)0)) {
        perror("fdopen failed");
        return;
    }
}

variable_types: r int

variable_types:
envdir
char *

variable_types:
filepath
char *


open

decompiled_code:
__int64 __fastcall open(char *name, unsigned int a2, __int64 a3)
{
char *v4; // rax
char *v5; // r14
unsigned int v6; // [rsp+30h] [rbp-48h]

v6 = a3;
if ( !realopen )
resolve_realopen();
if ( !fileopend )
open_outputfile();
if ( outputfile )
{
if ( *name == 47 )
{
fprintf(outputfile, "%s\n", name);
}
else
{
v4 = realpath(name, 0LL);
v5 = v4;
if ( !v4 || (fprintf(outputfile, "%s\n", v4), name != v5) )
{
free(v5);
if ( (a2 & 0x40) == 0 )
return realopen(name, a2, a3);
return realopen(name, a2, v6);
}
}
}
if ( (a2 & 0x40) == 0 )
return realopen(name, a2, a3);
return realopen(name, a2, v6);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *name, unsigned int a2, __int64 a3)
{
char *v4; // rax
char *v5; // r14
unsigned int v6; // [rsp+30h] [rbp-48h]

v6 = a3;
if ( !realopen )
func6();
if ( !fileopend )
func7();
if ( outputfile )
{
if ( *name == 47 )
{
fprintf(outputfile, "%s\n", name);
}
else
{
v4 = realpath(name, 0LL);
v5 = v4;
if ( !v4 || (fprintf(outputfile, "%s\n", v4), name != v5) )
{
free(v5);
if ( (a2 & 0x40) == 0 )
return realopen(name, a2, a3);
return realopen(name, a2, v6);
}
}
}
if ( (a2 & 0x40) == 0 )
return realopen(name, a2, a3);
return realopen(name, a2, v6);
}

original_code:
int open(const char *pathname, int flags, ...) {
    if (!realopen) {
        resolve_realopen();
    }
    if (!fileopend) {
        open_outputfile();
    }
    if (outputfile && pathname) {
        const char *abspath = (pathname[0] == '/') ? pathname : realpath(pathname, ((void *)0));
        if (abspath) {
            fprintf(outputfile, "%s\n", abspath);
        }
        if (abspath != pathname) {
            free((void *)abspath);
        }
    }
    if (flags & 0100) {
        mode_t mode;
        va_list ap;
        __builtin_va_start(ap,flags);
        mode = __builtin_va_arg(ap,mode_t);
        __builtin_va_end(ap);
        return realopen(pathname, flags, mode);
    } else {
        return realopen(pathname, flags);
    }
}

canonical_original_code:
int open(char *pathname, int flags, ...) {
    if (!realopen) {
        resolve_realopen();
    }
    if (!fileopend) {
        open_outputfile();
    }
    if (outputfile && pathname) {
        char *abspath = (pathname[0] == '/') ? pathname : realpath(pathname, ((void *)0));
        if (abspath) {
            fprintf(outputfile, "%s\n", abspath);
        }
        if (abspath != pathname) {
            free((void *)abspath);
        }
    }
    if (flags & 0100) {
        unsigned int mode;
        __builtin_va_list ap;
        __builtin_va_start(ap,flags);
        mode = __builtin_va_arg(ap,mode_t);
        __builtin_va_end(ap);
        return realopen(pathname, flags, mode);
    } else {
        return realopen(pathname, flags);
    }
}

variable_types:
pathname
char *

variable_types: flags int

variable_types:
abspath
char *

variable_types:
mode
unsigned int


main

repo: cyphar/ascii-snake

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rdi
const char *v4; // r12
__int64 v5; // rbx
size_t v6; // rax
void *v7; // rdi
__int64 v8; // rbx
void *v9; // rdi

printf("\x1B[H\x1B[2J");
v3 = stdout;
fflush(stdout);
snake_init(v3, argv);
snake_redraw();
while ( !quit )
{
snake_input();
snake_update();
snake_redraw();
usleep(0x13880u);
}
v4 = "You Won!";
if ( (unsigned int)snake_end_state() )
v4 = "Game Over!";
v5 = 0LL;
v6 = strlen(v4);
move_cursor(12LL, 17 - (unsigned int)(v6 >> 1));
printf("%s%s%s", "\x1B[1;31m", v4, "\x1B[0m");
fflush(stdout);
do
{
v7 = *(void **)((char *)game_state + v5);
v5 += 8LL;
free(v7);
}
while ( v5 != 4800 );
free(game_state);
if ( snake_len > 0 )
{
v8 = 0LL;
do
{
v9 = (void *)*((_QWORD *)snake_body + v8++);
free(v9);
}
while ( snake_len > (int)v8 );
}
free(snake_body);
free(border);
move_cursor((unsigned int)(timer >= 0) + 25, 0LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rdi
const char *v4; // r12
__int64 v5; // rbx
size_t v6; // rax
void *v7; // rdi
__int64 v8; // rbx
void *v9; // rdi

printf("\x1B[H\x1B[2J");
v3 = stdout;
fflush(stdout);
func9(v3, argv);
func15();
while ( !quit )
{
func10();
func13();
func15();
usleep(0x13880u);
}
v4 = "You Won!";
if ( (unsigned int)func16() )
v4 = "Game Over!";
v5 = 0LL;
v6 = strlen(v4);
func17(12LL, 17 - (unsigned int)(v6 >> 1));
printf("%s%s%s", "\x1B[1;31m", v4, "\x1B[0m");
fflush(stdout);
do
{
v7 = *(void **)((char *)game_state + v5);
v5 += 8LL;
free(v7);
}
while ( v5 != 4800 );
free(game_state);
if ( snake_len > 0 )
{
v8 = 0LL;
do
{
v9 = (void *)*((_QWORD *)snake_body + v8++);
free(v9);
}
while ( snake_len > (int)v8 );
}
free(snake_body);
free(border);
func17((unsigned int)(timer >= 0) + 25, 0LL);
return 0;
}

original_code:
int main(void) {
 printf("\x1b[H\x1b[2J");
 fflush(stdout);
 snake_init();
 snake_redraw();
 while(!quit) {
  snake_input();
  snake_update();
  snake_redraw();
  usleep(0.08 * 1000000L);
 }
 char *msg = ((void *)0);
 int end_state = snake_end_state();
 switch(end_state) {
  case win:
   msg = "You Won!";
   break;
  case loss:
  default:
   msg = "Game Over!";
   break;
 }
 move_cursor((20 / 2) + 2, (30 / 2) - (strlen(msg) / 2) + 2);
 printf("%s%s%s", "\x1b[1;31m", msg, "\x1b[0m");
 fflush(stdout);
 int i;
 for(i = 0; i < 20 * 30; i++)
  free(game_state[i]);
 free(game_state);
 for(i = 0; i < snake_len; i++)
  free(snake_body[i]);
 free(snake_body);
 free(border);
 move_cursor(20 + 5 + (timer >= 0), 0);
 return 0;
}

canonical_original_code:
int main() {
 printf("\x1b[H\x1b[2J");
 fflush(stdout);
 snake_init();
 snake_redraw();
 while(!quit) {
  snake_input();
  snake_update();
  snake_redraw();
  usleep(0.08 * 1000000L);
 }
 char *msg = ((void *)0);
 int end_state = snake_end_state();
 switch(end_state) {
  case win:
   msg = "You Won!";
   break;
  case loss:
  default:
   msg = "Game Over!";
   break;
 }
 move_cursor((20 / 2) + 2, (30 / 2) - (strlen(msg) / 2) + 2);
 printf("%s%s%s", "\x1b[1;31m", msg, "\x1b[0m");
 fflush(stdout);
 int i;
 for(i = 0; i < 20 * 30; i++)
  free(game_state[i]);
 free(game_state);
 for(i = 0; i < snake_len; i++)
  free(snake_body[i]);
 free(snake_body);
 free(border);
 move_cursor(20 + 5 + (timer >= 0), 0);
 return 0;
}

variable_types:
msg
char *

variable_types: i int


getch

decompiled_code:
__int64 getch()
{
unsigned int v0; // ebx

tcgetattr(0, &old_1);
*(_QWORD *)&new_0.c_cc[31] = *(_QWORD *)&old_1.c_cc[31];
*(__m128i *)&new_0.c_iflag = _mm_load_si128((const __m128i *)&old_1);
*(__m128i *)&new_0.c_line = _mm_load_si128((const __m128i *)&old_1.c_line);
new_0.c_lflag &= 0xFFFFFFF5;
*(__m128i *)&new_0.c_cc[15] = _mm_load_si128((const __m128i *)&old_1.c_cc[15]);
new_0.c_ospeed = old_1.c_ospeed;
tcsetattr(0, 0, &new_0);
v0 = getchar();
tcsetattr(0, 0, &old_1);
if ( (_BYTE)v0 == 3 )
exit(2);
return v0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // ebx

tcgetattr(0, &old_1);
*(_QWORD *)&new_0.c_cc[31] = *(_QWORD *)&old_1.c_cc[31];
*(__m128i *)&new_0.c_iflag = _mm_load_si128((const __m128i *)&old_1);
*(__m128i *)&new_0.c_line = _mm_load_si128((const __m128i *)&old_1.c_line);
new_0.c_lflag &= 0xFFFFFFF5;
*(__m128i *)&new_0.c_cc[15] = _mm_load_si128((const __m128i *)&old_1.c_cc[15]);
new_0.c_ospeed = old_1.c_ospeed;
tcsetattr(0, 0, &new_0);
v0 = getchar();
tcsetattr(0, 0, &old_1);
if ( (_BYTE)v0 == 3 )
exit(2);
return v0;
}

original_code:
char getch(void) {
 static struct termios old, new;
 tcgetattr(0, &old);
 new = old;
 new.c_lflag &= ~0000002;
 new.c_lflag &= ~0000010;
 tcsetattr(0, 0, &new);
 char ch = getchar();
 tcsetattr(0, 0, &old);
 if(ch == 3)
  exit(2);
 return ch;
}

canonical_original_code:
char getch() {
 static struct termios old; static struct termios new;
 tcgetattr(0, &old);
 new = old;
 new.c_lflag &= ~0000002;
 new.c_lflag &= ~0000010;
 tcsetattr(0, 0, &new);
 char ch = getchar();
 tcsetattr(0, 0, &old);
 if(ch == 3)
  exit(2);
 return ch;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: ch char

return_type: char


decompiled_code:
__int64 __fastcall snake_rand(int a1, int a2)
{
return (unsigned int)(rand() % (a2 + 1 - a1) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
return (unsigned int)(rand() % (a2 + 1 - a1) + a1);
}

original_code:
int snake_rand(int min, int max) {
 return (rand() % (max + 1 - min)) + min;
}

variable_types: min int

variable_types: max int


decompiled_code:
char *snake_init()
{
int v0; // ebx
int v1; // eax
__int64 v2; // rbx
_QWORD *v3; // rbp
_DWORD *v4; // rax
int v5; // edx
int v6; // eax
char *v7; // rbx
char *v8; // rbp
_BYTE *v9; // rax
char *v10; // rdx
__int64 v11; // rax
char *result; // rax

v0 = time(0LL);
v1 = v0 ^ getpid();
v2 = 0LL;
srand(v1);
score = 0;
snake_len = 5;
snake_head = 0xA0000000FLL;
snake_body = malloc(0x28uLL);
v3 = snake_body;
do
{
v4 = malloc(8uLL);
v5 = 15 - v2;
v3[v2++] = v4;
*v4 = v5;
v4[1] = 10;
}
while ( v2 != 5 );
food = snake_rand(0, 29);
v6 = snake_rand(0, 19);
bonus = -1LL;
dword_4040F4 = v6;
timer = -1;
game_state = calloc(0x258uLL, 8uLL);
v7 = (char *)game_state;
v8 = (char *)game_state + 240;
do
{
v7 += 8;
v9 = calloc(0x1FuLL, 1uLL);
*((_QWORD *)v7 - 1) = v9;
v9[30] = 0;
}
while ( v8 != v7 );
border = (char *)calloc(0x21uLL, 1uLL);
v10 = border;
v11 = 1LL;
while ( 1 )
{
v10[v11++] = 45;
if ( v11 == 31 )
break;
v10 = border;
}
result = border;
border[31] = 43;
*result = 43;
return result;
}

canonical_decompiled_code:
char *func9()
{
int v0; // ebx
int v1; // eax
__int64 v2; // rbx
_QWORD *v3; // rbp
_DWORD *v4; // rax
int v5; // edx
int v6; // eax
char *v7; // rbx
char *v8; // rbp
_BYTE *v9; // rax
char *v10; // rdx
__int64 v11; // rax
char *result; // rax

v0 = time(0LL);
v1 = v0 ^ getpid();
v2 = 0LL;
srand(v1);
score = 0;
snake_len = 5;
snake_head = 0xA0000000FLL;
snake_body = malloc(0x28uLL);
v3 = snake_body;
do
{
v4 = malloc(8uLL);
v5 = 15 - v2;
v3[v2++] = v4;
*v4 = v5;
v4[1] = 10;
}
while ( v2 != 5 );
food = func8(0, 29);
v6 = func8(0, 19);
bonus = -1LL;
dword_4040F4 = v6;
timer = -1;
game_state = calloc(0x258uLL, 8uLL);
v7 = (char *)game_state;
v8 = (char *)game_state + 240;
do
{
v7 += 8;
v9 = calloc(0x1FuLL, 1uLL);
*((_QWORD *)v7 - 1) = v9;
v9[30] = 0;
}
while ( v8 != v7 );
border = (char *)calloc(0x21uLL, 1uLL);
v10 = border;
v11 = 1LL;
while ( 1 )
{
v10[v11++] = 45;
if ( v11 == 31 )
break;
v10 = border;
}
result = border;
border[31] = 43;
*result = 43;
return result;
}

original_code:
void snake_init(void) {
 srand(time(((void *)0)) ^ getpid());
 score = 0;
 snake_len = 5;
 snake_head[0] = 30 / 2;
 snake_head[1] = 20 / 2;
 snake_body = malloc(snake_len * sizeof(int *));
 int i;
 for(i = 0; i < snake_len; i++) {
  snake_body[i] = malloc(2 * sizeof(int));
  snake_body[i][0] = snake_head[0] - i;
  snake_body[i][1] = snake_head[1];
 }
 food[0] = snake_rand(0, 30 - 1);
 food[1] = snake_rand(0, 20 - 1);
 bonus[0] = -1;
 bonus[1] = -1;
 timer = -1;
 game_state = calloc(20 * 30, sizeof(char *));
 for(i = 0; i < 30; i++) {
  game_state[i] = calloc(30 + 1, 1);
  game_state[i][30] = '\0';
 }
 border = calloc(30 + 3, 1);
 for(i = 0; i < 30; i++)
  border[i + 1] = '-';
 border[0] = border[30 + 1] = '+';
}

canonical_original_code:
void snake_init() {
 srand(time(((void *)0)) ^ getpid());
 score = 0;
 snake_len = 5;
 snake_head[0] = 30 / 2;
 snake_head[1] = 20 / 2;
 snake_body = malloc(snake_len * sizeof(int *));
 int i;
 for(i = 0; i < snake_len; i++) {
  snake_body[i] = malloc(2 * sizeof(int));
  snake_body[i][0] = snake_head[0] - i;
  snake_body[i][1] = snake_head[1];
 }
 food[0] = snake_rand(0, 30 - 1);
 food[1] = snake_rand(0, 20 - 1);
 bonus[0] = -1;
 bonus[1] = -1;
 timer = -1;
 game_state = calloc(20 * 30, sizeof(char *));
 for(i = 0; i < 30; i++) {
  game_state[i] = calloc(30 + 1, 1);
  game_state[i][30] = '\0';
 }
 border = calloc(30 + 3, 1);
 for(i = 0; i < 30; i++)
  border[i + 1] = '-';
 border[0] = border[30 + 1] = '+';
}


decompiled_code:
int snake_input()
{
unsigned int v0; // er12
unsigned int v1; // eax
char v2; // bl
int result; // eax

v0 = fcntl(0, 3);
v1 = fcntl(0, 3);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v2 = getch();
result = fcntl(0, 4, v0);
if ( v2 == 27 )
{
result = getch();
if ( (_BYTE)result == 91 )
{
result = getch();
if ( (_BYTE)result == 67 )
{
LABEL_10:
if ( snake_direction != 2 )
snake_direction = 3;
}
else if ( (char)result > 67 )
{
if ( (_BYTE)result == 68 )
{
LABEL_8:
if ( snake_direction != 3 )
snake_direction = 2;
}
}
else if ( (_BYTE)result == 65 )
{
LABEL_5:
if ( snake_direction != 1 )
snake_direction = 0;
}
else if ( (_BYTE)result == 66 )
{
LABEL_17:
result = snake_direction;
snake_direction = snake_direction != 0;
}
}
}
else
{
switch ( v2 )
{
case 'A':
case 'a':
goto LABEL_8;
case 'D':
case 'd':
goto LABEL_10;
case 'Q':
case 'q':
quit = 1;
break;
case 'S':
case 's':
goto LABEL_17;
case 'W':
case 'w':
goto LABEL_5;
default:
return result;
}
}
return result;
}

canonical_decompiled_code:
int func10()
{
unsigned int v0; // er12
unsigned int v1; // eax
char v2; // bl
int result; // eax

v0 = fcntl(0, 3);
v1 = fcntl(0, 3);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v2 = func7();
result = fcntl(0, 4, v0);
if ( v2 == 27 )
{
result = func7();
if ( (_BYTE)result == 91 )
{
result = func7();
if ( (_BYTE)result == 67 )
{
LABEL_10:
if ( snake_direction != 2 )
snake_direction = 3;
}
else if ( (char)result > 67 )
{
if ( (_BYTE)result == 68 )
{
LABEL_8:
if ( snake_direction != 3 )
snake_direction = 2;
}
}
else if ( (_BYTE)result == 65 )
{
LABEL_5:
if ( snake_direction != 1 )
snake_direction = 0;
}
else if ( (_BYTE)result == 66 )
{
LABEL_17:
result = snake_direction;
snake_direction = snake_direction != 0;
}
}
}
else
{
switch ( v2 )
{
case 'A':
case 'a':
goto LABEL_8;
case 'D':
case 'd':
goto LABEL_10;
case 'Q':
case 'q':
quit = 1;
break;
case 'S':
case 's':
goto LABEL_17;
case 'W':
case 'w':
goto LABEL_5;
default:
return result;
}
}
return result;
}

original_code:
void snake_input(void) {
 int old = fcntl(0, 3);
 fcntl(0, 4, fcntl(0, 3) | 04000);
 int ch = getch();
 fcntl(0, 4, old);
 int weird = 0;
 switch(ch) {
  case 'q':
  case 'Q':
   quit = 1;
   break;
  case 'w':
  case 'W':
   if(snake_direction != down)
    snake_direction = up;
   break;
  case 's':
  case 'S':
   if(snake_direction != up)
    snake_direction = down;
   break;
  case 'd':
  case 'D':
   if(snake_direction != left)
    snake_direction = right;
   break;
  case 'a':
  case 'A':
   if(snake_direction != right)
    snake_direction = left;
   break;
  case 27:
   weird = 1;
   break;
 }
 if(!weird)
  return;
 if(ch == 27 && getch() == 91) {
  switch(getch()) {
   case 65:
    if(snake_direction != down)
     snake_direction = up;
    break;
   case 66:
    if(snake_direction != up)
     snake_direction = down;
    break;
   case 67:
    if(snake_direction != left)
     snake_direction = right;
    break;
   case 68:
    if(snake_direction != right)
     snake_direction = left;
    break;
   default:
    break;
  }
 }
}

canonical_original_code:
void snake_input() {
 int old = fcntl(0, 3);
 fcntl(0, 4, fcntl(0, 3) | 04000);
 int ch = getch();
 fcntl(0, 4, old);
 int weird = 0;
 switch(ch) {
  case 'q':
  case 'Q':
   quit = 1;
   break;
  case 'w':
  case 'W':
   if(snake_direction != down)
    snake_direction = up;
   break;
  case 's':
  case 'S':
   if(snake_direction != up)
    snake_direction = down;
   break;
  case 'd':
  case 'D':
   if(snake_direction != left)
    snake_direction = right;
   break;
  case 'a':
  case 'A':
   if(snake_direction != right)
    snake_direction = left;
   break;
  case 27:
   weird = 1;
   break;
 }
 if(!weird)
  return;
 if(ch == 27 && getch() == 91) {
  switch(getch()) {
   case 65:
    if(snake_direction != down)
     snake_direction = up;
    break;
   case 66:
    if(snake_direction != up)
     snake_direction = down;
    break;
   case 67:
    if(snake_direction != left)
     snake_direction = right;
    break;
   case 68:
    if(snake_direction != right)
     snake_direction = left;
    break;
   default:
    break;
  }
 }
}

variable_types: old int

variable_types: ch int

variable_types: weird int


decompiled_code:
_QWORD *shift_snake()
{
_QWORD *v0; // rsi
__int64 v1; // rax
_DWORD *v2; // rcx
_DWORD *v3; // rdx
_QWORD *result; // rax

v0 = snake_body;
v1 = snake_len - 1;
if ( snake_len - 1 > 0 )
{
do
{
v2 = (_DWORD *)v0[v1 - 1];
v3 = (_DWORD *)v0[v1--];
*v3 = *v2;
v3[1] = v2[1];
}
while ( (int)v1 > 0 );
}
result = (_QWORD *)*v0;
*(_QWORD *)*v0 = snake_head;
return result;
}

canonical_decompiled_code:
_QWORD *func11()
{
_QWORD *v0; // rsi
__int64 v1; // rax
_DWORD *v2; // rcx
_DWORD *v3; // rdx
_QWORD *result; // rax

v0 = snake_body;
v1 = snake_len - 1;
if ( snake_len - 1 > 0 )
{
do
{
v2 = (_DWORD *)v0[v1 - 1];
v3 = (_DWORD *)v0[v1--];
*v3 = *v2;
v3[1] = v2[1];
}
while ( (int)v1 > 0 );
}
result = (_QWORD *)*v0;
*(_QWORD *)*v0 = snake_head;
return result;
}

original_code:
void shift_snake(void) {
 int i;
 for(i = snake_len - 1; i > 0; i--) {
  snake_body[i][0] = snake_body[i-1][0];
  snake_body[i][1] = snake_body[i-1][1];
 }
 snake_body[0][0] = snake_head[0];
 snake_body[0][1] = snake_head[1];
}

canonical_original_code:
void shift_snake() {
 int i;
 for(i = snake_len - 1; i > 0; i--) {
  snake_body[i][0] = snake_body[i-1][0];
  snake_body[i][1] = snake_body[i-1][1];
 }
 snake_body[0][0] = snake_head[0];
 snake_body[0][1] = snake_head[1];
}


decompiled_code:
__int64 __fastcall in_snake(int a1, int a2)
{
char *v2; // rax
__int64 result; // rax

if ( (_DWORD)snake_head != a1 || (result = 1LL, HIDWORD(snake_head) != a2) )
{
if ( snake_len <= 0 )
{
LABEL_8:
result = 0LL;
}
else
{
v2 = (char *)snake_body;
while ( **(_DWORD **)v2 != a1 || *(_DWORD *)(*(_QWORD *)v2 + 4LL) != a2 )
{
v2 += 8;
if ( v2 == (char *)snake_body + 8 * (unsigned int)(snake_len - 1) + 8 )
goto LABEL_8;
}
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, int a2)
{
char *v2; // rax
__int64 result; // rax

if ( (_DWORD)snake_head != a1 || (result = 1LL, HIDWORD(snake_head) != a2) )
{
if ( snake_len <= 0 )
{
LABEL_8:
result = 0LL;
}
else
{
v2 = (char *)snake_body;
while ( **(_DWORD **)v2 != a1 || *(_DWORD *)(*(_QWORD *)v2 + 4LL) != a2 )
{
v2 += 8;
if ( v2 == (char *)snake_body + 8 * (unsigned int)(snake_len - 1) + 8 )
goto LABEL_8;
}
result = 1LL;
}
}
return result;
}

original_code:
int in_snake(int x, int y) {
 if(snake_head[0] == x && snake_head[1] == y)
  return 1;
 int i;
 for(i = 0; i < snake_len; i++) {
  if(snake_body[i][0] == x && snake_body[i][1] == y)
   return 1;
 }
 return 0;
}

variable_types: x int

variable_types: y int