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


repo: cyfdecyf/mem-order

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; } *

return_type: int


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);
v4 = v3;
*(_QWORD *)a1 = 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);
v4 = v3;
*(_QWORD *)a1 = 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; // ebp
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; // ebp
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)
{
unsigned int v1; // ebx

if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) != -1 )
return 0;
v1 = -1;
perror("munmap in unmap_log");
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned int v1; // ebx

if ( munmap(*(void **)a1, *(_QWORD *)(a1 + 16) - *(_QWORD *)a1) != -1 )
return 0;
v1 = -1;
perror("munmap in unmap_log");
return v1;
}

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 *

return_type: 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]; }

return_type: void


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; // er12
__int64 v2; // rdi
int v3; // ebx
int v4; // eax
int v5; // er13
int *v6; // rbx
int v7; // eax
__int64 v8; // r13

mem_init_thr((unsigned int)(char)a1);
bind_core((char)__readfsbyte((unsigned int)&g_tid));
sync_thread();
v1 = 1000000;
do
{
v2 = (unsigned int)(char)__readfsbyte((unsigned int)&g_tid);
v3 = g_sig[16 * (__int64)(int)v2];
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);
v8 = (unsigned int)mix(v5, v7);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6, v8);
g_sig[16 * (__int64)(char)__readfsbyte((unsigned int)&g_tid)] = v8;
--v1;
}
while ( v1 );
mem_finish_thr();
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
int v1; // er12
__int64 v2; // rdi
int v3; // ebx
int v4; // eax
int v5; // er13
int *v6; // rbx
int v7; // eax
__int64 v8; // r13

mem_init_thr((unsigned int)(char)a1);
func1((char)__readfsbyte((unsigned int)&g_tid));
func2();
v1 = 1000000;
do
{
v2 = (unsigned int)(char)__readfsbyte((unsigned int)&g_tid);
v3 = g_sig[16 * (__int64)(int)v2];
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);
v8 = (unsigned int)func3(v5, v7);
mem_write((unsigned int)(char)__readfsbyte((unsigned int)&g_tid), v6, v8);
g_sig[16 * (__int64)(char)__readfsbyte((unsigned int)&g_tid)] = v8;
--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

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 i; // rbx
pthread_t *v5; // r13
int v6; // ebx
pthread_t *v7; // r12
char *v8; // rbp
__int64 v9; // rbp
unsigned int v10; // ebp
int *v11; // r12
pthread_attr_t v13; // [rsp+0h] [rbp-68h] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 i; // rbx
pthread_t *v5; // r13
int v6; // ebx
pthread_t *v7; // r12
char *v8; // rbp
__int64 v9; // rbp
unsigned int v10; // ebp
int *v11; // r12
pthread_attr_t v13; // [rsp+0h] [rbp-68h] BYREF

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

if ( *(_QWORD *)(a1 + 16) < (unsigned __int64)(*(_QWORD *)(a1 + 8) + a2) )
enlarge_mapped_log();
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = result + a2;
return result;
}

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

if ( *(_QWORD *)(a1 + 16) < (unsigned __int64)(*(_QWORD *)(a1 + 8) + a2) )
enlarge_mapped_log();
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = result + a2;
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, _QWORD *a2)
{
__int64 result; // rax

result = next_memop_log((__int64)&unk_470 + __readfsqword(0));
*(_DWORD *)result = a1;
*(_QWORD *)(result + 8) = *a2;
*(_QWORD *)(result + 16) = a2[1];
return result;
}

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

result = func2((__int64)&unk_470 + __readfsqword(0));
*(_DWORD *)result = a1;
*(_QWORD *)(result + 8) = *a2;
*(_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()
{
char *v0; // rbx
_QWORD *v1; // rax
__int64 result; // rax

v0 = (char *)&logs + __readfsqword(0);
v1 = (_QWORD *)next_version_log((__int64)v0);
*v1 = -1LL;
v1[1] = -1LL;
result = next_memop_log((__int64)(v0 + 32));
*(_DWORD *)result = -1;
*(_QWORD *)(result + 8) = -1LL;
*(_QWORD *)(result + 16) = -1LL;
return result;
}

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

v0 = (char *)&logs + __readfsqword(0);
v1 = (_QWORD *)func1((__int64)v0);
*v1 = -1LL;
v1[1] = -1LL;
result = func2((__int64)(v0 + 32));
*(_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, _QWORD *a3)
{
__int64 result; // rax

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

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

result = (__int64)log_wait_version(a2);
if ( (__int64)a3[1] >= 0 )
result = func3(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((__int64)&g_commit_log, 1);
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((__int64)&g_commit_log, 1);
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:
__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

wait_flag((volatile signed __int32 *)&wflag, 8);
v1 = 1000000;
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

func7((volatile signed __int32 *)&wflag, 8);
v1 = 1000000;
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 *

variable_types: i int


reader

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

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

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

func7((volatile signed __int32 *)&wflag, 8);
v1 = 1000000;
do
{
func12(&rwlock);
if ( dword_404064 != shared || dword_404068 != shared )
{
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

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t *v3; // rbx
pthread_t *v4; // rbp
_BYTE v6[48]; // [rsp+0h] [rbp-58h] BYREF
pthread_t newthread; // [rsp+30h] [rbp-28h] BYREF
pthread_t th[4]; // [rsp+38h] [rbp-20h] BYREF

v3 = (pthread_t *)v6;
v4 = (pthread_t *)v6;
do
{
if ( pthread_create(v4, 0LL, reader, 0LL) )
perror("thread creating failed");
++v4;
}
while ( v4 != &newthread );
if ( pthread_create(&newthread, 0LL, writer, 0LL) )
perror("thread creating failed");
if ( pthread_create(th, 0LL, writer, 0LL) )
perror("thread creating failed");
do
pthread_join(*v3++, 0LL);
while ( v3 != &newthread );
pthread_join(newthread, 0LL);
pthread_join(th[0], 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: argc int

variable_types:
argv
char *[]


repo: cyfdecyf/spinlock

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 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;
}

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");
    }
}


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

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

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

if ( argc != 2 )
{
printf("Usage: %s <num of threads>\n", *argv);
exit(1);
}
v3 = atoi(argv[1]);
nthr = v3;
v4 = (pthread_t *)calloc(8uLL, v3);
v5 = v4;
if ( v3 > 0 )
{
v6 = v4;
v7 = 0LL;
do
{
if ( pthread_create(v6, 0LL, inc_thread, v7) )
perror("thread creating failed");
++v7;
++v6;
}
while ( nthr > (__int64)v7 );
if ( nthr > 0 )
{
v8 = 0LL;
do
pthread_join(v5[v8++], 0LL);
while ( nthr > v8 );
}
}
func9(&start_time.tv_sec, &end_time.tv_sec);
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:
thr
unsigned long *

variable_types: ret int

variable_types: i long


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;
}


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

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


repo: cyfdecyf/traceopen

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

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

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

v0 = dlopen("libc.so.6", 1);
v1 = dlerror();
if ( v1 )
{
puts(v1);
exit(1);
}
realopen = dlsym(v0, "open");
result = dlerror();
if ( result )
{
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; // eax
int v2; // ebx
char *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

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

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

v0 = getenv("TRACEDIR");
ptr[0] = 0LL;
fileopend = 1;
if ( v0 )
v1 = asprintf(ptr, "%s/traceopenXXXXXX", v0);
else
v1 = asprintf(ptr, "/tmp/traceopenXXXXXX");
if ( v1 == -1 )
{
perror("asprintf failed");
}
else
{
v2 = mkstemp(ptr[0]);
free(ptr[0]);
ptr[0] = 0LL;
if ( v2 == -1 )
{
perror("mkstemp failed failed");
}
else
{
outputfile = fdopen(v2, "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, unsigned int a3)
{
char *v3; // rax
char *v4; // r12
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func8(char *name, unsigned int a2, unsigned int a3)
{
char *v3; // rax
char *v4; // r12
__int64 result; // rax

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

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


getch

repo: cyphar/ascii-snake

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

tcgetattr(0, &old_1);
*(__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);
*(__m128i *)&new_0.c_cc[15] = _mm_load_si128((const __m128i *)&old_1.c_cc[15]);
*(_QWORD *)&new_0.c_cc[31] = *(_QWORD *)&old_1.c_cc[31];
new_0.c_ospeed = old_1.c_ospeed;
new_0.c_lflag &= 0xFFFFFFF5;
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);
*(__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);
*(__m128i *)&new_0.c_cc[15] = _mm_load_si128((const __m128i *)&old_1.c_cc[15]);
*(_QWORD *)&new_0.c_cc[31] = *(_QWORD *)&old_1.c_cc[31];
new_0.c_ospeed = old_1.c_ospeed;
new_0.c_lflag &= 0xFFFFFFF5;
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
__pid_t v1; // eax
__int64 v2; // rbx
void **v3; // rbp
__int64 i; // rbx
void **v5; // rbp
__int64 j; // rax
char *result; // rax

v0 = time(0LL);
v1 = getpid();
srand(v0 ^ v1);
score = 0;
snake_len = 5;
snake_head = 15;
dword_40410C = 10;
snake_body = malloc(0x28uLL);
v2 = 0LL;
do
{
v3 = (void **)((char *)snake_body + 8 * v2);
*v3 = malloc(8uLL);
**((_DWORD **)snake_body + v2) = snake_head - v2;
*(_DWORD *)(*((_QWORD *)snake_body + v2++) + 4LL) = dword_40410C;
}
while ( snake_len > (int)v2 );
food = snake_rand(0, 29);
dword_4040F4 = snake_rand(0, 19);
bonus = -1;
dword_4040EC = -1;
timer = -1;
game_state = calloc(0x258uLL, 8uLL);
for ( i = 0LL; i != 240; i += 8LL )
{
v5 = (void **)((char *)game_state + i);
*v5 = calloc(0x1FuLL, 1uLL);
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + 30LL) = 0;
}
border = (char *)calloc(0x21uLL, 1uLL);
for ( j = 1LL; j != 31; ++j )
border[j] = 45;
result = border;
border[31] = 43;
*result = 43;
return result;
}

canonical_decompiled_code:
char *func9()
{
int v0; // ebx
__pid_t v1; // eax
__int64 v2; // rbx
void **v3; // rbp
__int64 i; // rbx
void **v5; // rbp
__int64 j; // rax
char *result; // rax

v0 = time(0LL);
v1 = getpid();
srand(v0 ^ v1);
score = 0;
snake_len = 5;
snake_head = 15;
dword_40410C = 10;
snake_body = malloc(0x28uLL);
v2 = 0LL;
do
{
v3 = (void **)((char *)snake_body + 8 * v2);
*v3 = malloc(8uLL);
**((_DWORD **)snake_body + v2) = snake_head - v2;
*(_DWORD *)(*((_QWORD *)snake_body + v2++) + 4LL) = dword_40410C;
}
while ( snake_len > (int)v2 );
food = func8(0, 29);
dword_4040F4 = func8(0, 19);
bonus = -1;
dword_4040EC = -1;
timer = -1;
game_state = calloc(0x258uLL, 8uLL);
for ( i = 0LL; i != 240; i += 8LL )
{
v5 = (void **)((char *)game_state + i);
*v5 = calloc(0x1FuLL, 1uLL);
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + 30LL) = 0;
}
border = (char *)calloc(0x21uLL, 1uLL);
for ( j = 1LL; j != 31; ++j )
border[j] = 45;
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] = '+';
}

variable_types: i int


decompiled_code:
__int64 snake_input()
{
unsigned int v0; // ebp
unsigned int v1; // eax
int v2; // ebx
__int64 result; // rax

v0 = fcntl(0, 3);
v1 = fcntl(0, 3);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v2 = getch();
fcntl(0, 4, v0);
if ( (_BYTE)v2 != 27 )
return (unsigned int)(v2 - 65);
result = getch();
if ( (_BYTE)result == 91 )
{
result = getch();
if ( (_BYTE)result == 67 )
{
if ( snake_direction != 2 )
snake_direction = 3;
}
else if ( (char)result > 67 )
{
if ( (_BYTE)result == 68 && snake_direction != 3 )
snake_direction = 2;
}
else if ( (_BYTE)result == 65 )
{
if ( snake_direction != 1 )
snake_direction = 0;
}
else if ( (_BYTE)result == 66 )
{
snake_direction = snake_direction != 0;
}
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // ebp
unsigned int v1; // eax
int v2; // ebx
__int64 result; // rax

v0 = fcntl(0, 3);
v1 = fcntl(0, 3);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v2 = func7();
fcntl(0, 4, v0);
if ( (_BYTE)v2 != 27 )
return (unsigned int)(v2 - 65);
result = func7();
if ( (_BYTE)result == 91 )
{
result = func7();
if ( (_BYTE)result == 67 )
{
if ( snake_direction != 2 )
snake_direction = 3;
}
else if ( (char)result > 67 )
{
if ( (_BYTE)result == 68 && snake_direction != 3 )
snake_direction = 2;
}
else if ( (_BYTE)result == 65 )
{
if ( snake_direction != 1 )
snake_direction = 0;
}
else if ( (_BYTE)result == 66 )
{
snake_direction = snake_direction != 0;
}
}
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:
__int64 shift_snake()
{
int v0; // edx
__int64 v1; // rax
__int64 result; // rax

v0 = snake_len;
if ( snake_len - 1 > 0 )
{
v1 = 8LL * (snake_len - 1);
do
{
**(_DWORD **)((char *)snake_body + v1) = **(_DWORD **)((char *)snake_body + v1 - 8);
*(_DWORD *)(*(_QWORD *)((char *)snake_body + v1) + 4LL) = *(_DWORD *)(*(_QWORD *)((char *)snake_body + v1 - 8)
+ 4LL);
v1 -= 8LL;
}
while ( v1 != 8 * (v0 - (unsigned __int64)(unsigned int)(v0 - 2)) - 16 );
}
**(_DWORD **)snake_body = snake_head;
result = *(_QWORD *)snake_body;
*(_DWORD *)(*(_QWORD *)snake_body + 4LL) = dword_40410C;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // edx
__int64 v1; // rax
__int64 result; // rax

v0 = snake_len;
if ( snake_len - 1 > 0 )
{
v1 = 8LL * (snake_len - 1);
do
{
**(_DWORD **)((char *)snake_body + v1) = **(_DWORD **)((char *)snake_body + v1 - 8);
*(_DWORD *)(*(_QWORD *)((char *)snake_body + v1) + 4LL) = *(_DWORD *)(*(_QWORD *)((char *)snake_body + v1 - 8)
+ 4LL);
v1 -= 8LL;
}
while ( v1 != 8 * (v0 - (unsigned __int64)(unsigned int)(v0 - 2)) - 16 );
}
**(_DWORD **)snake_body = snake_head;
result = *(_QWORD *)snake_body;
*(_DWORD *)(*(_QWORD *)snake_body + 4LL) = dword_40410C;
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 ( snake_head != a1 || (result = 1LL, dword_40410C != a2) )
{
if ( snake_len <= 0 )
{
result = 0LL;
}
else
{
v2 = (char *)snake_body;
do
{
if ( **(_DWORD **)v2 == a1 && *(_DWORD *)(*(_QWORD *)v2 + 4LL) == a2 )
return 1LL;
v2 += 8;
}
while ( v2 != (char *)snake_body + 8 * (unsigned int)(snake_len - 1) + 8 );
result = 0LL;
}
}
return result;
}

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

if ( snake_head != a1 || (result = 1LL, dword_40410C != a2) )
{
if ( snake_len <= 0 )
{
result = 0LL;
}
else
{
v2 = (char *)snake_body;
do
{
if ( **(_DWORD **)v2 == a1 && *(_DWORD *)(*(_QWORD *)v2 + 4LL) == a2 )
return 1LL;
v2 += 8;
}
while ( v2 != (char *)snake_body + 8 * (unsigned int)(snake_len - 1) + 8 );
result = 0LL;
}
}
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


decompiled_code:
__int64 snake_update()
{
__int64 result; // rax
int v1; // ecx
int v2; // eax
void **v3; // rbx
int v4; // eax
int v5; // ebx
int v6; // ebp
__int64 i; // rcx
__int64 j; // rax
__int64 v9; // rsi
int v10; // ecx
int v11; // ebx
int v12; // ebp
int *v13; // rdx
int v14; // eax
int v15; // edx

if ( snake_direction == 2 )
{
--snake_head;
}
else if ( snake_direction > 2 )
{
if ( snake_direction == 3 )
++snake_head;
}
else if ( snake_direction )
{
if ( snake_direction == 1 )
++dword_40410C;
}
else
{
--dword_40410C;
}
if ( snake_len <= 1 )
{
LABEL_16:
v1 = dword_40410C;
if ( (unsigned int)dword_40410C > 0x13 )
{
v2 = snake_head;
if ( snake_head < 0 )
v2 = 29;
if ( dword_40410C < 0 )
v1 = 19;
}
else
{
v2 = snake_head;
if ( (unsigned int)snake_head <= 0x1D )
{
LABEL_26:
if ( snake_head == food && dword_40410C == dword_4040F4 )
{
++score;
snake_body = realloc(snake_body, 8LL * (snake_len + 1));
v3 = (void **)((char *)snake_body + 8 * snake_len);
*v3 = malloc(8uLL);
**((_DWORD **)snake_body + snake_len) = -1;
v4 = snake_len;
*(_DWORD *)(*((_QWORD *)snake_body + snake_len) + 4LL) = -1;
snake_len = v4 + 1;
do
{
do
{
food = snake_rand(0, 29);
v5 = snake_rand(0, 19);
dword_4040F4 = v5;
v6 = food;
}
while ( (unsigned int)in_snake(food, v5) );
}
while ( v6 == bonus && v5 == dword_4040EC );
}
if ( snake_head == bonus && dword_40410C == dword_4040EC )
{
score += 10;
bonus = -1;
dword_4040EC = -1;
timer = -1;
}
else if ( bonus >= 0 && dword_4040EC >= 0 )
{
if ( !--timer )
{
bonus = -1;
dword_4040EC = -1;
timer = -1;
}
goto LABEL_41;
}
if ( (unsigned int)snake_rand(0, 400) == 200 )
{
do
{
do
{
bonus = snake_rand(0, 29);
v11 = snake_rand(0, 19);
dword_4040EC = v11;
v12 = bonus;
}
while ( (unsigned int)in_snake(bonus, v11) );
}
while ( v12 == food && v11 == dword_4040F4 );
timer = snake_rand(30, 120);
}
LABEL_41:
shift_snake();
for ( i = 0LL; i != 160; i += 8LL )
{
for ( j = 0LL; j != 30; ++j )
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + j) = 32;
}
if ( snake_len > 1 )
{
v9 = 8LL;
v10 = 1;
do
{
v13 = *(int **)((char *)snake_body + v9);
v14 = *v13;
v15 = v13[1];
if ( v14 >= 0 && v15 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + v15) + v14) = 42;
++v10;
v9 += 8LL;
}
while ( snake_len > v10 );
}
if ( food >= 0 && dword_4040F4 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + dword_4040F4) + food) = 64;
result = (unsigned int)bonus;
if ( bonus >= 0 && dword_4040EC >= 0 )
{
result = bonus;
*(_BYTE *)(*((_QWORD *)game_state + dword_4040EC) + bonus) = 36;
}
if ( snake_head >= 0 && dword_40410C >= 0 )
{
if ( snake_direction == 2 )
{
result = 62LL;
}
else if ( snake_direction > 2 )
{
result = 88LL;
if ( snake_direction == 3 )
result = 60LL;
}
else
{
result = 118LL;
if ( snake_direction )
{
result = 88LL;
if ( snake_direction == 1 )
result = 94LL;
}
}
*(_BYTE *)(*((_QWORD *)game_state + dword_40410C) + snake_head) = result;
}
return result;
}
if ( snake_head < 0 )
v2 = 29;
}
snake_head = v2 % 30;
dword_40410C = v1 % 20;
goto LABEL_26;
}
result = (__int64)snake_body + 8;
while ( snake_head != **(_DWORD **)result || dword_40410C != *(_DWORD *)(*(_QWORD *)result + 4LL) )
{
result += 8LL;
if ( (void *)result == (char *)snake_body + 8 * (unsigned int)(snake_len - 2) + 16 )
goto LABEL_16;
}
quit = 1;
return result;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 result; // rax
int v1; // ecx
int v2; // eax
void **v3; // rbx
int v4; // eax
int v5; // ebx
int v6; // ebp
__int64 i; // rcx
__int64 j; // rax
__int64 v9; // rsi
int v10; // ecx
int v11; // ebx
int v12; // ebp
int *v13; // rdx
int v14; // eax
int v15; // edx

if ( snake_direction == 2 )
{
--snake_head;
}
else if ( snake_direction > 2 )
{
if ( snake_direction == 3 )
++snake_head;
}
else if ( snake_direction )
{
if ( snake_direction == 1 )
++dword_40410C;
}
else
{
--dword_40410C;
}
if ( snake_len <= 1 )
{
LABEL_16:
v1 = dword_40410C;
if ( (unsigned int)dword_40410C > 0x13 )
{
v2 = snake_head;
if ( snake_head < 0 )
v2 = 29;
if ( dword_40410C < 0 )
v1 = 19;
}
else
{
v2 = snake_head;
if ( (unsigned int)snake_head <= 0x1D )
{
LABEL_26:
if ( snake_head == food && dword_40410C == dword_4040F4 )
{
++score;
snake_body = realloc(snake_body, 8LL * (snake_len + 1));
v3 = (void **)((char *)snake_body + 8 * snake_len);
*v3 = malloc(8uLL);
**((_DWORD **)snake_body + snake_len) = -1;
v4 = snake_len;
*(_DWORD *)(*((_QWORD *)snake_body + snake_len) + 4LL) = -1;
snake_len = v4 + 1;
do
{
do
{
food = func8(0, 29);
v5 = func8(0, 19);
dword_4040F4 = v5;
v6 = food;
}
while ( (unsigned int)func12(food, v5) );
}
while ( v6 == bonus && v5 == dword_4040EC );
}
if ( snake_head == bonus && dword_40410C == dword_4040EC )
{
score += 10;
bonus = -1;
dword_4040EC = -1;
timer = -1;
}
else if ( bonus >= 0 && dword_4040EC >= 0 )
{
if ( !--timer )
{
bonus = -1;
dword_4040EC = -1;
timer = -1;
}
goto LABEL_41;
}
if ( (unsigned int)func8(0, 400) == 200 )
{
do
{
do
{
bonus = func8(0, 29);
v11 = func8(0, 19);
dword_4040EC = v11;
v12 = bonus;
}
while ( (unsigned int)func12(bonus, v11) );
}
while ( v12 == food && v11 == dword_4040F4 );
timer = func8(30, 120);
}
LABEL_41:
func11();
for ( i = 0LL; i != 160; i += 8LL )
{
for ( j = 0LL; j != 30; ++j )
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + j) = 32;
}
if ( snake_len > 1 )
{
v9 = 8LL;
v10 = 1;
do
{
v13 = *(int **)((char *)snake_body + v9);
v14 = *v13;
v15 = v13[1];
if ( v14 >= 0 && v15 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + v15) + v14) = 42;
++v10;
v9 += 8LL;
}
while ( snake_len > v10 );
}
if ( food >= 0 && dword_4040F4 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + dword_4040F4) + food) = 64;
result = (unsigned int)bonus;
if ( bonus >= 0 && dword_4040EC >= 0 )
{
result = bonus;
*(_BYTE *)(*((_QWORD *)game_state + dword_4040EC) + bonus) = 36;
}
if ( snake_head >= 0 && dword_40410C >= 0 )
{
if ( snake_direction == 2 )
{
result = 62LL;
}
else if ( snake_direction > 2 )
{
result = 88LL;
if ( snake_direction == 3 )
result = 60LL;
}
else
{
result = 118LL;
if ( snake_direction )
{
result = 88LL;
if ( snake_direction == 1 )
result = 94LL;
}
}
*(_BYTE *)(*((_QWORD *)game_state + dword_40410C) + snake_head) = result;
}
return result;
}
if ( snake_head < 0 )
v2 = 29;
}
snake_head = v2 % 30;
dword_40410C = v1 % 20;
goto LABEL_26;
}
result = (__int64)snake_body + 8;
while ( snake_head != **(_DWORD **)result || dword_40410C != *(_DWORD *)(*(_QWORD *)result + 4LL) )
{
result += 8LL;
if ( (void *)result == (char *)snake_body + 8 * (unsigned int)(snake_len - 2) + 16 )
goto LABEL_16;
}
quit = 1;
return result;
}

original_code:
void snake_update(void) {
 switch(snake_direction) {
  case up:
   snake_head[1]--;
   break;
  case down:
   snake_head[1]++;
   break;
  case left:
   snake_head[0]--;
   break;
  case right:
   snake_head[0]++;
   break;
 }
 int i;
 for(i = 1; i < snake_len; i++) {
  if(snake_head[0] == snake_body[i][0] && snake_head[1] == snake_body[i][1]) {
   quit = 1;
   return;
  }
 }
 if(snake_head[1] < 0 || snake_head[1] >= 20 ||
    snake_head[0] < 0 || snake_head[0] >= 30) {
  if(1) {
   snake_head[0] = (snake_head[0] < 0) ? 30 - 1 : snake_head[0];
   snake_head[1] = (snake_head[1] < 0) ? 20 - 1 : snake_head[1];
   snake_head[0] %= 30;
   snake_head[1] %= 20;
  } else {
   quit = 1;
   return;
  }
 }
 if(snake_head[0] == food[0] && snake_head[1] == food[1]) {
  score += 1;
  snake_body = realloc(snake_body, (snake_len + 1) * sizeof(int *));
  snake_body[snake_len] = malloc(2 * sizeof(int));
  snake_body[snake_len][0] = -1;
  snake_body[snake_len][1] = -1;
  snake_len++;
  do {
   food[0] = snake_rand(0, 30 - 1);
   food[1] = snake_rand(0, 20 - 1);
  } while(in_snake(food[0], food[1]) || (food[0] == bonus[0] && food[1] == bonus[1]));
 }
 if(snake_head[0] == bonus[0] && snake_head[1] == bonus[1]) {
  score += 10;
  bonus[0] = -1;
  bonus[1] = -1;
  timer = -1;
 }
 if(bonus[0] >= 0 && bonus[1] >= 0) {
  timer--;
  if(!timer) {
   bonus[0] = -1;
   bonus[1] = -1;
   timer = -1;
  }
 } else {
  if(snake_rand(0, 400) == 400 / 2) {
   do {
    bonus[0] = snake_rand(0, 30 - 1);
    bonus[1] = snake_rand(0, 20 - 1);
   } while(in_snake(bonus[0], bonus[1]) || (bonus[0] == food[0] && bonus[1] == food[1]));
   timer = snake_rand(30, 120);
  }
 }
 shift_snake();
 int x, y;
 for(y = 0; y < 20; y++) {
  for(x = 0; x < 30; x++) {
   game_state[y][x] = ' ';
  }
 }
 for(i = 1; i < snake_len; i++) {
  x = snake_body[i][0];
  y = snake_body[i][1];
  if(x < 0 || y < 0)
   continue;
  game_state[y][x] = '*';
 }
 x = food[0];
 y = food[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '@';
 x = bonus[0];
 y = bonus[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '$';
 x = snake_head[0];
 y = snake_head[1];
 if(x >= 0 && y >= 0) {
  char head = 'X';
  switch(snake_direction) {
   case up:
    head = 'v';
    break;
   case down:
    head = '^';
    break;
   case left:
    head = '>';
    break;
   case right:
    head = '<';
    break;
  }
  game_state[y][x] = head;
 }
}

canonical_original_code:
void snake_update() {
 switch(snake_direction) {
  case up:
   snake_head[1]--;
   break;
  case down:
   snake_head[1]++;
   break;
  case left:
   snake_head[0]--;
   break;
  case right:
   snake_head[0]++;
   break;
 }
 int i;
 for(i = 1; i < snake_len; i++) {
  if(snake_head[0] == snake_body[i][0] && snake_head[1] == snake_body[i][1]) {
   quit = 1;
   return;
  }
 }
 if(snake_head[1] < 0 || snake_head[1] >= 20 ||
    snake_head[0] < 0 || snake_head[0] >= 30) {
  if(1) {
   snake_head[0] = (snake_head[0] < 0) ? 30 - 1 : snake_head[0];
   snake_head[1] = (snake_head[1] < 0) ? 20 - 1 : snake_head[1];
   snake_head[0] %= 30;
   snake_head[1] %= 20;
  } else {
   quit = 1;
   return;
  }
 }
 if(snake_head[0] == food[0] && snake_head[1] == food[1]) {
  score += 1;
  snake_body = realloc(snake_body, (snake_len + 1) * sizeof(int *));
  snake_body[snake_len] = malloc(2 * sizeof(int));
  snake_body[snake_len][0] = -1;
  snake_body[snake_len][1] = -1;
  snake_len++;
  do {
   food[0] = snake_rand(0, 30 - 1);
   food[1] = snake_rand(0, 20 - 1);
  } while(in_snake(food[0], food[1]) || (food[0] == bonus[0] && food[1] == bonus[1]));
 }
 if(snake_head[0] == bonus[0] && snake_head[1] == bonus[1]) {
  score += 10;
  bonus[0] = -1;
  bonus[1] = -1;
  timer = -1;
 }
 if(bonus[0] >= 0 && bonus[1] >= 0) {
  timer--;
  if(!timer) {
   bonus[0] = -1;
   bonus[1] = -1;
   timer = -1;
  }
 } else {
  if(snake_rand(0, 400) == 400 / 2) {
   do {
    bonus[0] = snake_rand(0, 30 - 1);
    bonus[1] = snake_rand(0, 20 - 1);
   } while(in_snake(bonus[0], bonus[1]) || (bonus[0] == food[0] && bonus[1] == food[1]));
   timer = snake_rand(30, 120);
  }
 }
 shift_snake();
 int x; int y;
 for(y = 0; y < 20; y++) {
  for(x = 0; x < 30; x++) {
   game_state[y][x] = ' ';
  }
 }
 for(i = 1; i < snake_len; i++) {
  x = snake_body[i][0];
  y = snake_body[i][1];
  if(x < 0 || y < 0)
   continue;
  game_state[y][x] = '*';
 }
 x = food[0];
 y = food[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '@';
 x = bonus[0];
 y = bonus[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '$';
 x = snake_head[0];
 y = snake_head[1];
 if(x >= 0 && y >= 0) {
  char head = 'X';
  switch(snake_direction) {
   case up:
    head = 'v';
    break;
   case down:
    head = '^';
    break;
   case left:
    head = '>';
    break;
   case right:
    head = '<';
    break;
  }
  game_state[y][x] = head;
 }
}

variable_types: head char


decompiled_code:
int clr_line()
{
printf("\x1B[s");
printf("%*s", 30, "");
printf("\x1B[u");
return fflush(stdout);
}

canonical_decompiled_code:
int func14()
{
printf("\x1B[s");
printf("%*s", 30, "");
printf("\x1B[u");
return fflush(stdout);
}

original_code:
void clr_line(void) {
 printf("\x1b[s");
 printf("%*s", 30, "");
 printf("\x1b[u");
 fflush(stdout);
}

canonical_original_code:
void clr_line() {
 printf("\x1b[s");
 printf("%*s", 30, "");
 printf("\x1b[u");
 fflush(stdout);
}


decompiled_code:
int snake_redraw()
{
__int64 i; // rbx

printf("\x1B[H");
fflush(stdout);
puts(border);
for ( i = 0LL; i != 160; i += 8LL )
printf("%c%s%c\n", 124LL, *(const char **)((char *)game_state + i), 124LL);
puts(border);
clr_line();
printf("\nScore: \x1B[1;34m%d\x1B[0m\n", (unsigned int)score);
if ( timer >= 0 )
{
clr_line();
printf("Timer: \x1B[1;33m%d\x1B[0m\n", (unsigned int)timer);
}
return clr_line();
}

canonical_decompiled_code:
int func15()
{
__int64 i; // rbx

printf("\x1B[H");
fflush(stdout);
puts(border);
for ( i = 0LL; i != 160; i += 8LL )
printf("%c%s%c\n", 124LL, *(const char **)((char *)game_state + i), 124LL);
puts(border);
func14();
printf("\nScore: \x1B[1;34m%d\x1B[0m\n", (unsigned int)score);
if ( timer >= 0 )
{
func14();
printf("Timer: \x1B[1;33m%d\x1B[0m\n", (unsigned int)timer);
}
return func14();
}

original_code:
void snake_redraw(void) {
 printf("\x1b[H");
 fflush(stdout);
 printf("%s\n", border);
 int i;
 for(i = 0; i < 20; i++)
  printf("%c%s%c\n", '|', game_state[i], '|');
 printf("%s\n", border);
 clr_line();
 printf("\n" "Score: " "\x1b[1;34m" "%d" "\x1b[0m" "\n", score);
 if(timer >= 0) {
  clr_line();
  printf("Timer: " "\x1b[1;33m" "%d" "\x1b[0m" "\n", timer);
 }
 clr_line();
}

canonical_original_code:
void snake_redraw() {
 printf("\x1b[H");
 fflush(stdout);
 printf("%s\n", border);
 int i;
 for(i = 0; i < 20; i++)
  printf("%c%s%c\n", '|', game_state[i], '|');
 printf("%s\n", border);
 clr_line();
 printf("\n" "Score: " "\x1b[1;34m" "%d" "\x1b[0m" "\n", score);
 if(timer >= 0) {
  clr_line();
  printf("Timer: " "\x1b[1;33m" "%d" "\x1b[0m" "\n", timer);
 }
 clr_line();
}


original_code:
int snake_end_state(void) {
 int x, y;
 for(y = 0; y < 20; y++)
  for(x = 0; x < 30; x++)
   if(!(game_state[y][x] == '*' || game_state[y][x] == 'v' || game_state[y][x] == '^' || game_state[y][x] == '>' || game_state[y][x] == '<'))
    return loss;
 return win;
}

canonical_original_code:
int snake_end_state() {
 int x; int y;
 for(y = 0; y < 20; y++)
  for(x = 0; x < 30; x++)
   if(!(game_state[y][x] == '*' || game_state[y][x] == 'v' || game_state[y][x] == '^' || game_state[y][x] == '>' || game_state[y][x] == '<'))
    return loss;
 return win;
}


decompiled_code:
int __fastcall move_cursor(unsigned int a1, unsigned int a2)
{
printf("\x1B[%d;%df", a1, a2);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func17(unsigned int a1, unsigned int a2)
{
printf("\x1B[%d;%df", a1, a2);
return fflush(stdout);
}

original_code:
void move_cursor(int row, int column) {
 printf("\x1b[%d;%df", row, column);
 fflush(stdout);
}

variable_types: row int

variable_types: column int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
const char *v4; // rbx
size_t v5; // rax
__int64 i; // rbx
__int64 v7; // rbx

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
const char *v4; // rbx
size_t v5; // rax
__int64 i; // rbx
__int64 v7; // rbx

printf("\x1B[H\x1B[2J");
v3 = (__int64)stdout;
fflush(stdout);
func9();
func15();
while ( !quit )
{
func10(v3, argv);
func13();
func15();
v3 = 80000LL;
usleep(0x13880u);
}
v4 = "You Won!";
if ( (unsigned int)func16(v3, argv) )
v4 = "Game Over!";
v5 = strlen(v4);
func17(0xCu, 17 - (v5 >> 1));
printf("%s%s%s", "\x1B[1;31m", v4, "\x1B[0m");
fflush(stdout);
for ( i = 0LL; i != 4800; i += 8LL )
free(*(void **)((char *)game_state + i));
free(game_state);
if ( snake_len > 0 )
{
v7 = 0LL;
do
free(*((void **)snake_body + v7++));
while ( snake_len > (int)v7 );
}
free(snake_body);
free(border);
func17((timer >= 0) + 25, 0);
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 *


decompiled_code:
__int64 __fastcall con_charAt(__int64 a1, __int16 a2, int a3, unsigned int a4)
{
__int64 v5; // rsi
char v7; // [rsp+0h] [rbp-18h]

v5 = a4;
con_gotoXY(a3);
con_setColor((unsigned int)a2, v5);
return con_outTxt("%c", v7);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int16 a2, int a3, unsigned int a4)
{
__int64 v5; // rsi
char v7; // [rsp+0h] [rbp-18h]

v5 = a4;
func15(a3);
func23((unsigned int)a2, v5);
return func19("%c", v7);
}

original_code:
static void con_charAt(int ch, int color, int x, int y) {
    con_gotoXY(x, y);
    con_setColor(color);
    con_outTxt("%c", ch);
}

variable_types: color int


decompiled_code:
__int64 initial_draw()
{
int v0; // eax
int i; // ebp
int v3; // eax
__int16 v4; // si
__int64 v5; // rdi
int j; // ebx
int v7; // eax
char v8; // [rsp+0h] [rbp-18h]

con_clearScr();
con_gotoXY(3);
con_outTxt("Use arrows to move point, use Esc to exit.", v8);
v0 = field_width;
for ( i = 0; field_width > i; v0 = field_width )
{
v7 = field_height;
for ( j = 0; field_height > j; v7 = field_height )
{
if ( i )
{
if ( field_width - 1 != i && j )
{
v3 = v7 - 1;
v4 = (v3 != j) + 1;
v5 = 3 * (unsigned int)(v3 == j) + 32;
}
else
{
v4 = 1;
v5 = 35LL;
}
}
else
{
v4 = 1;
v5 = 35LL;
}
con_charAt(v5, v4, field_x + i, field_y + j++);
}
++i;
}
point_x = field_x + v0 / 2;
point_y = field_y + field_height / 2;
return con_charAt(64LL, 3, field_x + v0 / 2, field_y + field_height / 2);
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // eax
int i; // ebp
int v3; // eax
__int16 v4; // si
__int64 v5; // rdi
int j; // ebx
int v7; // eax
char v8; // [rsp+0h] [rbp-18h]

func18();
func15(3);
func19("Use arrows to move point, use Esc to exit.", v8);
v0 = field_width;
for ( i = 0; field_width > i; v0 = field_width )
{
v7 = field_height;
for ( j = 0; field_height > j; v7 = field_height )
{
if ( i )
{
if ( field_width - 1 != i && j )
{
v3 = v7 - 1;
v4 = (v3 != j) + 1;
v5 = 3 * (unsigned int)(v3 == j) + 32;
}
else
{
v4 = 1;
v5 = 35LL;
}
}
else
{
v4 = 1;
v5 = 35LL;
}
func7(v5, v4, field_x + i, field_y + j++);
}
++i;
}
point_x = field_x + v0 / 2;
point_y = field_y + field_height / 2;
return func7(64LL, 3, field_x + v0 / 2, field_y + field_height / 2);
}

original_code:
static void initial_draw() {
    con_clearScr();
    con_gotoXY(3, 1);
    con_outTxt("Use arrows to move point, use Esc to exit.");
    {
        int i, j;
        for (i = 0; i < field_width; ++i) {
            for (j = 0; j < field_height; ++j) {
                int ch;
                int color;
                if (i == 0 || i == field_width-1 || j == 0 || j == field_height-1) {
                    ch = '#';
                    color = 1;
                } else {
                    ch = ' ';
                    color = 2;
                }
                con_charAt(ch, color, field_x + i, field_y + j);
            }
        }
    }
    point_x = field_x + field_width/2;
    point_y = field_y + field_height/2;
    con_charAt('@', 3, point_x, point_y);
}

canonical_original_code:
static void initial_draw() {
    con_clearScr();
    con_gotoXY(3, 1);
    con_outTxt("Use arrows to move point, use Esc to exit.");
    {
        int i; int j;
        for (i = 0; i < field_width; ++i) {
            for (j = 0; j < field_height; ++j) {
                int ch;
                int color;
                if (i == 0 || i == field_width-1 || j == 0 || j == field_height-1) {
                    ch = '#';
                    color = 1;
                } else {
                    ch = ' ';
                    color = 2;
                }
                con_charAt(ch, color, field_x + i, field_y + j);
            }
        }
    }
    point_x = field_x + field_width/2;
    point_y = field_y + field_height/2;
    con_charAt('@', 3, point_x, point_y);
}

variable_types: j int


decompiled_code:
__int64 init_colors()
{
con_initPair(1LL, 0LL, 4LL);
con_initPair(2LL, 2LL, 2LL);
return con_initPair(3LL, 1LL, 2LL);
}

canonical_decompiled_code:
__int64 func9()
{
func22(1LL, 0LL, 4LL);
func22(2LL, 2LL, 2LL);
return func22(3LL, 1LL, 2LL);
}

original_code:
static void init_colors() {
    con_initPair(1, 0, 4);
    con_initPair(2, 2, 2);
    con_initPair(3, 1, 2);
}


decompiled_code:
__int64 __fastcall process_key(int a1)
{
int v1; // ebx
int v2; // ebp
__int64 result; // rax

if ( a1 == 259 )
{
v2 = -(point_y - 1 > field_y);
v1 = 0;
}
else if ( a1 <= 259 )
{
result = 1LL;
if ( a1 == 27 )
return result;
if ( a1 != 258 )
return 0LL;
v2 = point_y + 1 < field_y + field_height - 1;
v1 = 0;
}
else if ( a1 == 260 )
{
v1 = -(point_x - 1 > field_x);
v2 = 0;
}
else
{
if ( a1 != 261 )
return 0LL;
v1 = point_x + 1 < field_x + field_width - 1;
v2 = 0;
}
result = v2 | (unsigned int)v1;
if ( v2 | v1 )
{
con_charAt(32LL, 2, point_x, point_y);
point_x += v1;
point_y += v2;
con_charAt(64LL, 3, point_x, point_y);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
int v1; // ebx
int v2; // ebp
__int64 result; // rax

if ( a1 == 259 )
{
v2 = -(point_y - 1 > field_y);
v1 = 0;
}
else if ( a1 <= 259 )
{
result = 1LL;
if ( a1 == 27 )
return result;
if ( a1 != 258 )
return 0LL;
v2 = point_y + 1 < field_y + field_height - 1;
v1 = 0;
}
else if ( a1 == 260 )
{
v1 = -(point_x - 1 > field_x);
v2 = 0;
}
else
{
if ( a1 != 261 )
return 0LL;
v1 = point_x + 1 < field_x + field_width - 1;
v2 = 0;
}
result = v2 | (unsigned int)v1;
if ( v2 | v1 )
{
func7(32LL, 2, point_x, point_y);
point_x += v1;
point_y += v2;
func7(64LL, 3, point_x, point_y);
result = 0LL;
}
return result;
}

original_code:
int process_key(int key) {
    int dx = 0;
    int dy = 0;
    switch (key) {
        case 27:
            return 1;
        case 0403:
            if (point_y - 1 > field_y) {
                dy = -1;
            }
            break;
        case 0402:
            if (point_y + 1 < field_y + field_height - 1) {
                dy = 1;
            }
            break;
        case 0404:
            if (point_x - 1 > field_x) {
                dx = -1;
            }
            break;
        case 0405:
            if (point_x + 1 < field_x + field_width - 1) {
                dx = 1;
            }
            break;
    }
    if (dx != 0 || dy != 0) {
        con_charAt(' ', 2, point_x, point_y);
        point_x += dx;
        point_y += dy;
        con_charAt('@', 3, point_x, point_y);
    }
    return 0;
}

variable_types: key int

variable_types: dx int

variable_types: dy int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

con_init(argc, argv, envp);
con_hideCursor();
init_colors();
con_getMaxXY(v6, &v5);
field_x = 5;
field_y = 3;
field_width = v6[0] - 8;
field_height = v5 - 6;
if ( v6[0] - 8 <= 2 )
__assert_fail("field_width > 2", "example.c", 0x79u, "main");
if ( v5 - 6 <= 2 )
__assert_fail("field_height > 2", "example.c", 0x7Au, "main");
initial_draw();
do
{
while ( !(unsigned int)con_keyPressed() )
;
v3 = con_getKey();
}
while ( !(unsigned int)process_key(v3) );
con_clearScr();
con_deinit();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

func13(argc, argv, envp);
func14();
func9();
func17(v6, &v5);
field_x = 5;
field_y = 3;
field_width = v6[0] - 8;
field_height = v5 - 6;
if ( v6[0] - 8 <= 2 )
__assert_fail("field_width > 2", "example.c", 0x79u, "main");
if ( v5 - 6 <= 2 )
__assert_fail("field_height > 2", "example.c", 0x7Au, "main");
func8();
do
{
while ( !(unsigned int)func20() )
;
v3 = func21();
}
while ( !(unsigned int)func10(v3) );
func18();
func11();
return 0;
}

original_code:
int main() {
    int quit = 0;
    int max_x, max_y;
    con_init();
    con_hideCursor();
    init_colors();
    con_getMaxXY(&max_x, &max_y);
    field_x = 3 + 1 + 1;
    field_y = 3;
    field_width = max_x - field_x - 3;
    field_height = max_y - field_y - 3;
    ((void) sizeof ((field_width > 2) ? 1 : 0), __extension__ ({ if (field_width > 2) ; else __assert_fail ("field_width > 2", "example.c", 121, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((field_height > 2) ? 1 : 0), __extension__ ({ if (field_height > 2) ; else __assert_fail ("field_height > 2", "example.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    initial_draw();
    while (!quit) {
        if (con_keyPressed()) {
            if (process_key(con_getKey())) {
                quit = 1;
            }
        }
    }
    con_clearScr();
    con_deinit();
    return 0;
}

canonical_original_code:
int main() {
    int quit = 0;
    int max_x; int max_y;
    con_init();
    con_hideCursor();
    init_colors();
    con_getMaxXY(&max_x, &max_y);
    field_x = 3 + 1 + 1;
    field_y = 3;
    field_width = max_x - field_x - 3;
    field_height = max_y - field_y - 3;
    ((void) sizeof ((field_width > 2) ? 1 : 0), __extension__ ({ if (field_width > 2) ; else __assert_fail ("field_width > 2", "example.c", 121, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((field_height > 2) ? 1 : 0), __extension__ ({ if (field_height > 2) ; else __assert_fail ("field_height > 2", "example.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    initial_draw();
    while (!quit) {
        if (con_keyPressed()) {
            if (process_key(con_getKey())) {
                quit = 1;
            }
        }
    }
    con_clearScr();
    con_deinit();
    return 0;
}

variable_types: quit int


decompiled_code:
int con_deinit()
{
return endwin();
}

canonical_decompiled_code:
int func11()
{
return endwin();
}

original_code:
void con_deinit()
{
    endwin();
}

canonical_original_code:
void con_deinit()
{
    endwin();
}


decompiled_code:
int con_showCursor()
{
return curs_set(1);
}

canonical_decompiled_code:
int func12()
{
return curs_set(1);
}

original_code:
void con_showCursor()
{
    curs_set(1);
}

canonical_original_code:
void con_showCursor()
{
    curs_set(1);
}


decompiled_code:
int con_init()
{
initscr();
noecho();
cbreak();
nodelay(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
start_color();
wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, 0x200000uLL, 0LL);
return con_showCursor();
}

canonical_decompiled_code:
int func13()
{
initscr();
noecho();
cbreak();
nodelay(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
start_color();
wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, 0x200000uLL, 0LL);
return func12();
}

original_code:
void con_init()
{
    initscr();
    noecho();
    cbreak();
    nodelay(stdscr, 1);
    keypad(stdscr, 1);
    start_color();
    wattr_on(stdscr, (attr_t)((((chtype)((1U)) << ((13) + 8)))), ((void *)0));
    con_showCursor();
}


decompiled_code:
int con_hideCursor()
{
return curs_set(0);
}

canonical_decompiled_code:
int func14()
{
return curs_set(0);
}

original_code:
void con_hideCursor()
{
    curs_set(0);
}

canonical_original_code:
void con_hideCursor()
{
    curs_set(0);
}


decompiled_code:
int __fastcall con_gotoXY(int a1, int a2)
{
return wmove(stdscr__NCURSES6_TINFO_5_0_19991023, a2, a1);
}

canonical_decompiled_code:
int __fastcall func15(int a1, int a2)
{
return wmove(stdscr__NCURSES6_TINFO_5_0_19991023, a2, a1);
}

original_code:
int con_gotoXY(int x, int y)
{
    return wmove(stdscr,(y),(x));
}


decompiled_code:
__int64 __fastcall con_getXY(_DWORD *a1, int *a2)
{
int v2; // eax
__int64 result; // rax

v2 = -1;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
v2 = stdscr__NCURSES6_TINFO_5_0_19991023->_cury;
*a2 = v2;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
result = (unsigned int)stdscr__NCURSES6_TINFO_5_0_19991023->_curx;
else
result = 0xFFFFFFFFLL;
*a1 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(_DWORD *a1, int *a2)
{
int v2; // eax
__int64 result; // rax

v2 = -1;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
v2 = stdscr__NCURSES6_TINFO_5_0_19991023->_cury;
*a2 = v2;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
result = (unsigned int)stdscr__NCURSES6_TINFO_5_0_19991023->_curx;
else
result = 0xFFFFFFFFLL;
*a1 = result;
return result;
}

original_code:
int con_getXY(int *px, int *py)
{
    return (*py = ((0 != (const void *)((stdscr))) ? (stdscr)->_cury : (-1)), *px = ((0 != (const void *)((stdscr))) ? (stdscr)->_curx : (-1)));
}

canonical_original_code:
int con_getXY(int *px, int *py)
{
    return (*py = ((0 != (void *)((stdscr))) ? (stdscr)->_cury : (-1)), *px = ((0 != (void *)((stdscr))) ? (stdscr)->_curx : (-1)));
}

variable_types:
px
int *

variable_types:
py
int *


decompiled_code:
__int64 __fastcall con_getMaxXY(_DWORD *a1, int *a2)
{
int v2; // eax
__int64 result; // rax

v2 = -1;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
v2 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
*a2 = v2;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
result = (unsigned int)(stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1);
else
result = 0xFFFFFFFFLL;
*a1 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(_DWORD *a1, int *a2)
{
int v2; // eax
__int64 result; // rax

v2 = -1;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
v2 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
*a2 = v2;
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
result = (unsigned int)(stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1);
else
result = 0xFFFFFFFFLL;
*a1 = result;
return result;
}

original_code:
int con_getMaxXY(int *px, int *py)
{
    return (*py = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), *px = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
}

canonical_original_code:
int con_getMaxXY(int *px, int *py)
{
    return (*py = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), *px = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
}