decompiled_code:
bool __fastcall tdb_free_blk(_DWORD *a1, __int64 a2)
{
char *v2; // rax
__int64 v3; // r8
__int64 v4; // rsi
__int64 v5; // r9
signed __int32 v6; // eax
__int64 v7; // r9
volatile signed __int32 *v8; // r10
bool result; // al
__int64 v10; // r9
__int64 v11; // r11
volatile signed __int32 *v12; // rsi
__int64 v13; // rdi
signed __int32 v14; // eax

v2 = tdb_ext_ptr(a1, a2);
v4 = v3;
tdb_blk_ptr((__int64)v2, v3);
v6 = tdb_blk_ext_off(v5, v4);
result = lfs_push((volatile signed __int32 *)(v7 + 4), v8, v6);
if ( result )
{
v12 = (volatile signed __int32 *)v10;
v13 = v11;
v14 = ext_id_by_ext(v11, v10);
result = ext_free(v13, v12, v14);
}
return result;
}

canonical_decompiled_code:
bool __fastcall func22(_DWORD *a1, __int64 a2)
{
char *v2; // rax
__int64 v3; // r8
__int64 v4; // rsi
__int64 v5; // r9
signed __int32 v6; // eax
__int64 v7; // r9
volatile signed __int32 *v8; // r10
bool result; // al
__int64 v10; // r9
__int64 v11; // r11
volatile signed __int32 *v12; // rsi
__int64 v13; // rdi
signed __int32 v14; // eax

v2 = func13(a1, a2);
v4 = v3;
func9((__int64)v2, v3);
v6 = func11(v5, v4);
result = func4((volatile signed __int32 *)(v7 + 4), v8, v6);
if ( result )
{
v12 = (volatile signed __int32 *)v10;
v13 = v11;
v14 = func5(v11, v10);
result = func6(v13, v12, v14);
}
return result;
}

original_code:
void
tdb_free_blk(TdbAlloc *a, uint64_t addr)
{
 TdbExt *e = tdb_ext_ptr(a, addr);
 SEntry *blk = tdb_blk_ptr(e, addr);
 int o = tdb_blk_ext_off(e, addr);
 int eid = ext_id_by_ext(a, e);
 if (lfs_push(&e->blk_free, blk, o))
  ext_free(a, e, eid);
}

canonical_original_code:
void tdb_free_blk(struct TdbAlloc *a, unsigned long addr)
{
 struct TdbExt *e = tdb_ext_ptr(a, addr);
 struct SEntry *blk = tdb_blk_ptr(e, addr);
 int o = tdb_blk_ext_off(e, addr);
 int eid = ext_id_by_ext(a, e);
 if (lfs_push(&e->blk_free, blk, o))
  ext_free(a, e, eid);
}

variable_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t { int counter; } ext_shr; struct atomic_t ext_cur; struct LfStack { union { struct { struct atomic_t head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } ext_free; } *


repo: krizhanovsky/NatSys-Lab

decompiled_code:
bool __fastcall tdb_alloc_init(_DWORD *a1, int a2, unsigned __int64 a3)
{
_QWORD *v3; // rdi
_DWORD *v4; // r8
__int64 v5; // rdi
char *v6; // rax

*a1 = a2;
v3 = a1 + 4;
*(v3 - 1) = 0x100000000LL;
*((_DWORD *)v3 - 3) = (a3 >> 21) - 1;
lfs_init(v3);
v5 = (__int64)v4;
v6 = ext_by_id(v4, 0);
return ext_init(v5, v6);
}

original_code:
void
tdb_alloc_init(TdbAlloc *a, size_t hdr_sz, size_t db_sz)
{
 ((void) (0));
 a->hdr_reserved = hdr_sz;
 a->ext_max = (db_sz >> 21) - 1;
 ((&a->ext_shr)->counter = (0));
 ((&a->ext_cur)->counter = (1));
 lfs_init(&a->ext_free);
 ext_init(a, ext_by_id(a, 0));
}

canonical_original_code:
void tdb_alloc_init(struct TdbAlloc *a, unsigned long hdr_sz, unsigned long db_sz)
{
 ((void) (0));
 a->hdr_reserved = hdr_sz;
 a->ext_max = (db_sz >> 21) - 1;
 ((&a->ext_shr)->counter = (0));
 ((&a->ext_cur)->counter = (1));
 lfs_init(&a->ext_free);
 ext_init(a, ext_by_id(a, 0));
}

return_type: void

user_defined_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t ext_shr; struct atomic_t ext_cur; struct LfStack ext_free; }

user_defined_types: struct LfStack { union { struct { struct atomic_t head; struct atomic_t gen; }; struct atomic64_t _trx; long _val; }; }


decompiled_code:
__int64 mapfile_size()
{
return 0x200000000LL;
}

original_code:
size_t
mapfile_size(void)
{
 return ((1UL << 21) * 4096);
}

canonical_original_code:
unsigned long mapfile_size()
{
 return ((1UL << 21) * 4096);
}

return_type: unsigned long


decompiled_code:
__int64 mapfile_raw_ptr()
{
__int64 v0; // r12
void *v2; // rax
void *v3; // rbx

v0 = mem;
if ( mem )
return v0;
v2 = mmap((void *)0x600000200000LL, 0x200000000uLL, 3, 33, -1, 0LL);
mem = (__int64)v2;
v3 = v2;
if ( v2 == (void *)-1LL )
{
perror("Dummy allocator: cannot allocate memory");
mem = 0LL;
return v0;
}
if ( mlock(v2, 0x200000000uLL) )
{
fprintf(stderr, "Dummy allocator: cannot lock memory. Please set ulimit -l %lu\n", 0x800000LL);
mem = 0LL;
return v0;
}
return (__int64)v3;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // r12
void *v2; // rax
void *v3; // rbx

v0 = mem;
if ( mem )
return v0;
v2 = mmap((void *)0x600000200000LL, 0x200000000uLL, 3, 33, -1, 0LL);
mem = (__int64)v2;
v3 = v2;
if ( v2 == (void *)-1LL )
{
perror("Dummy allocator: cannot allocate memory");
mem = 0LL;
return v0;
}
if ( mlock(v2, 0x200000000uLL) )
{
fprintf(stderr, "Dummy allocator: cannot lock memory. Please set ulimit -l %lu\n", 0x800000LL);
mem = 0LL;
return v0;
}
return (__int64)v3;
}

original_code:
void *
mapfile_raw_ptr(void)
{
 int r;
 if (mem)
  return mem;
 mem = (char *)mmap(((void *)(0x600000000000UL + (1UL << 21))), ((1UL << 21) * 4096), 0x1 | 0x2,
      0x01 | 0x20, -1, 0);
 if (mem == ((void *) -1)) {
  perror("Dummy allocator: cannot allocate memory");
  mem = ((void *)0);
  return ((void *)0);
 }
 r = mlock(mem, ((1UL << 21) * 4096));
 if (r) {
  fprintf(stderr, "Dummy allocator: cannot lock memory."
    " Please set ulimit -l %lu\n", ((1UL << 21) * 4096) / 1024);
  mem = ((void *)0);
  return ((void *)0);
 }
 return mem;
}

canonical_original_code:
void *mapfile_raw_ptr()
{
 int r;
 if (mem)
  return mem;
 mem = (char *)mmap(((void *)(0x600000000000UL + (1UL << 21))), ((1UL << 21) * 4096), 0x1 | 0x2,
      0x01 | 0x20, -1, 0);
 if (mem == ((void *) -1)) {
  perror("Dummy allocator: cannot allocate memory");
  mem = ((void *)0);
  return ((void *)0);
 }
 r = mlock(mem, ((1UL << 21) * 4096));
 if (r) {
  fprintf(stderr, "Dummy allocator: cannot lock memory."
    " Please set ulimit -l %lu\n", ((1UL << 21) * 4096) / 1024);
  mem = ((void *)0);
  return ((void *)0);
 }
 return mem;
}

variable_types: r int

return_type: void *


repo: krkhan/bsg

decompiled_code:
// attributes: thunk
int print_hash_table(const char *s)
{
return puts(s);
}

canonical_decompiled_code:
int func7(const char *s)
{
return puts(s);
}

original_code:
void print_hash_table(gpointer key, gpointer value, gpointer data)
{
  printf("%s\n", (char *) key);
}

canonical_original_code:
void print_hash_table(void *key, void *value, void *data)
{
  printf("%s\n", (char *) key);
}

variable_types:
key
void *

variable_types:
value
void *

variable_types:
data
void *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax

return result;
}

original_code:
int main(){
}

canonical_original_code:
int main(){
}

return_type: int


repo: kroody/tetris-free

decompiled_code:
__int64 __fastcall write_data_to_buf(int *a1, char *a2, int a3)
{
int v3; // ecx
__int64 v5; // rax
__int64 v6; // rdi
int v7; // er12
int v8; // edx
void *v9; // rdi
size_t v10; // r14
__int64 result; // rax

v3 = *a1;
v5 = a1[1];
v6 = a1[2];
v7 = v3 - v5 - v6;
if ( v7 > a3 )
v7 = a3;
v8 = v5 + v6;
v9 = (void *)(*((_QWORD *)a1 + 2) + v5 + v6);
if ( v3 >= v7 + v8 )
{
memcpy(v9, a2, v7);
a1[2] += v7;
result = (unsigned int)v7;
}
else
{
v10 = v3 - v8;
memcpy(v9, a2, v10);
if ( v7 > (int)v10 )
memcpy(*((void **)a1 + 2), &a2[v10], v7 - (int)v10);
a1[2] += v7;
result = (unsigned int)v7;
}
return result;
}

original_code:
int write_data_to_buf(DataBuffer * netbuf, char * buf, int len)
{
    int size;
    if (len > netbuf->buf_size - netbuf->start - netbuf->len)
        len = netbuf->buf_size - netbuf->start - netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(netbuf->buf + netbuf->start + netbuf->len, buf, size);
    if (len > size)
        memcpy(netbuf->buf, buf + size, len - size);
    netbuf->len += len;
    return len;
}

canonical_original_code:
int write_data_to_buf(struct _DataBuffer *netbuf, char *buf, int len)
{
    int size;
    if (len > netbuf->buf_size - netbuf->start - netbuf->len)
        len = netbuf->buf_size - netbuf->start - netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(netbuf->buf + netbuf->start + netbuf->len, buf, size);
    if (len > size)
        memcpy(netbuf->buf, buf + size, len - size);
    netbuf->len += len;
    return len;
}

variable_types: struct _DataBuffer { int buf_size; int start; int len; char *buf; } *

variable_types:
buf
char *

variable_types: len int

variable_types: size int


decompiled_code:
__int64 __fastcall read_data_from_buf(int *a1, void *a2, int a3)
{
__int64 result; // rax
__int64 v6; // rsi
int v7; // er12
int v8; // eax
const void *v9; // rsi
unsigned int v10; // ebp
int v11; // er12
bool v12; // zf
char *v13; // rax
char *v14; // rdi

result = (unsigned int)a1[2];
if ( (_DWORD)result )
{
v6 = a1[1];
v7 = *a1;
if ( (int)result <= a3 )
a3 = result;
v8 = v6 + result;
v9 = (const void *)(*((_QWORD *)a1 + 2) + v6);
v10 = a3;
if ( v8 + a3 > v7 )
{
v11 = v7 - v8;
v13 = (char *)memcpy(a2, v9, v11);
if ( (int)v10 > v11 )
{
a1[1] = 0;
v14 = &v13[v11];
v11 = v10 - v11;
memcpy(v14, *((const void **)a1 + 2), v11);
v12 = a1[2] == v10;
a1[2] -= v10;
if ( !v12 )
goto LABEL_7;
goto LABEL_11;
}
}
else
{
v11 = a3;
memcpy(a2, v9, a3);
}
v12 = a1[2] == v10;
a1[2] -= v10;
if ( !v12 )
{
LABEL_7:
a1[1] += v11;
return v10;
}
LABEL_11:
a1[1] = 0;
result = v10;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, void *a2, int a3)
{
__int64 result; // rax
__int64 v6; // rsi
int v7; // er12
int v8; // eax
const void *v9; // rsi
unsigned int v10; // ebp
int v11; // er12
bool v12; // zf
char *v13; // rax
char *v14; // rdi

result = (unsigned int)a1[2];
if ( (_DWORD)result )
{
v6 = a1[1];
v7 = *a1;
if ( (int)result <= a3 )
a3 = result;
v8 = v6 + result;
v9 = (const void *)(*((_QWORD *)a1 + 2) + v6);
v10 = a3;
if ( v8 + a3 > v7 )
{
v11 = v7 - v8;
v13 = (char *)memcpy(a2, v9, v11);
if ( (int)v10 > v11 )
{
a1[1] = 0;
v14 = &v13[v11];
v11 = v10 - v11;
memcpy(v14, *((const void **)a1 + 2), v11);
v12 = a1[2] == v10;
a1[2] -= v10;
if ( !v12 )
goto LABEL_7;
goto LABEL_11;
}
}
else
{
v11 = a3;
memcpy(a2, v9, a3);
}
v12 = a1[2] == v10;
a1[2] -= v10;
if ( !v12 )
{
LABEL_7:
a1[1] += v11;
return v10;
}
LABEL_11:
a1[1] = 0;
result = v10;
}
return result;
}

original_code:
int read_data_from_buf(DataBuffer * netbuf, char * buf, int len)
{
    int size;
    if (netbuf->len == 0)
        return 0;
    if (len > netbuf->len)
        len = netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(buf, netbuf->buf + netbuf->start, size);
    if (len > size) {
        netbuf->start = 0;
        memcpy(buf + size, netbuf->buf, len - size);
        size = len - size;
    }
    netbuf->len -= len;
    netbuf->start += size;
    if (netbuf->len == 0)
        netbuf->start = 0;
    return len;
}

canonical_original_code:
int read_data_from_buf(struct _DataBuffer *netbuf, char *buf, int len)
{
    int size;
    if (netbuf->len == 0)
        return 0;
    if (len > netbuf->len)
        len = netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(buf, netbuf->buf + netbuf->start, size);
    if (len > size) {
        netbuf->start = 0;
        memcpy(buf + size, netbuf->buf, len - size);
        size = len - size;
    }
    netbuf->len -= len;
    netbuf->start += size;
    if (netbuf->len == 0)
        netbuf->start = 0;
    return len;
}

user_defined_types: struct _DataBuffer { int buf_size; int start; int len; char *buf; }


decompiled_code:
// attributes: thunk
void release_data_buf(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func2(void *ptr)
{
free(ptr);
}

original_code:
void release_data_buf(DataBuffer * buf)
{
    free(buf);
}

canonical_original_code:
void release_data_buf(struct _DataBuffer *buf)
{
    free(buf);
}


decompiled_code:
__int64 __fastcall msg_sizeof(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = (unsigned int)(a1 - 2);
result = 0xFFFFFFFFLL;
if ( (unsigned int)v1 <= 5 )
result = (unsigned int)CSWTCH_16[v1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = (unsigned int)(a1 - 2);
result = 0xFFFFFFFFLL;
if ( (unsigned int)v1 <= 5 )
result = (unsigned int)CSWTCH_16[v1];
return result;
}

original_code:
static int msg_sizeof(int msg)
{
    int size = -1;
    switch (msg) {
    case 2:
        size = (sizeof(int));
    break;
    case 3:
        size = 2;
    break;
    case 4:
        size = (((12 + 7) / 8) * 20);
    break;
    case 5:
        size = 0;
    break;
    case 6:
        size = 0;
    break;
    case 7:
        size = 0;
    break;
    }
    return size;
}

variable_types: msg int


decompiled_code:
char __fastcall bits_to_win_map(char *a1, __int64 a2)
{
__int64 v3; // r9
__int64 v4; // rdi
char result; // al
char *v6; // rcx
__int64 v7; // rdx
char *v8; // rsi

v3 = a2 + 240;
v4 = a2;
do
{
result = *a1;
v6 = a1;
v7 = 1LL;
v8 = a1 + 1;
while ( 1 )
{
a1 = v8;
*(_BYTE *)(v4 + v7 - 1) = result & 1;
result = *v6 >> 1;
*v6 = result;
if ( v7 == 12 )
break;
if ( (v7 & 7) == 0 )
{
result = v6[1];
v6 = v8++;
}
++v7;
}
v4 += 12LL;
}
while ( v3 != v4 );
return result;
}

canonical_original_code:
static void bits_to_win_map(char *buf, unsigned char map[20][12])
{
    char *t = buf;
    int x; int y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
            if (x % 8 == 0 && x != 0)
                t++;
            if (*t & 0x1)
                map[y][x] = 1;
            else
                map[y][x] = 0;
            *t >>= 1;
        }
        t++;
    }
}

variable_types:
map
unsigned char [20][12]

variable_types:
t
char *

variable_types: x int

variable_types: y int


decompiled_code:
_BOOL8 __fastcall get_msg_from_buf(__int64 a1, int *a2, __int64 a3, __int64 a4)
{
_BOOL8 result; // rax
int v7; // ebx

if ( !*(_DWORD *)(a1 + 8) )
return 0LL;
if ( (unsigned int)read_data_from_buf(a1, a2, 4LL) <= 3 )
return 0LL;
v7 = msg_sizeof(*a2);
if ( v7 < 0 )
{
*(_QWORD *)(a1 + 4) = 0LL;
return 0LL;
}
if ( (unsigned int)read_data_from_buf(a1, a3, 4LL) <= 3 )
return 0LL;
result = 1LL;
if ( v7 )
result = (int)read_data_from_buf(a1, a4, (unsigned int)v7) >= v7;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, int *a2, __int64 a3, __int64 a4)
{
_BOOL8 result; // rax
int v7; // ebx

if ( !*(_DWORD *)(a1 + 8) )
return 0LL;
if ( (unsigned int)read_data_from_buf(a1, a2, 4LL) <= 3 )
return 0LL;
v7 = func0(*a2);
if ( v7 < 0 )
{
*(_QWORD *)(a1 + 4) = 0LL;
return 0LL;
}
if ( (unsigned int)read_data_from_buf(a1, a3, 4LL) <= 3 )
return 0LL;
result = 1LL;
if ( v7 )
result = (int)read_data_from_buf(a1, a4, (unsigned int)v7) >= v7;
return result;
}

original_code:
static int get_msg_from_buf(DataBuffer * buf, int * msg, int * id, char * arg)
{
    int len;
    if ((buf->len == 0))
        return 0;
    if (read_data_from_buf(buf, (char *)msg, sizeof(int)) < sizeof(int))
        return 0;
    if ((len = msg_sizeof(*msg)) < 0) {
        { (buf)->len = 0; (buf)->start = 0; };
        return 0;
    }
    if (read_data_from_buf(buf, (char *)id, sizeof(int)) < sizeof(int))
        return 0;
    if (len > 0 && read_data_from_buf(buf, arg, len) < len)
  return 0;
    return 1;
}

canonical_original_code:
static int get_msg_from_buf(struct _DataBuffer *buf, int *msg, int *id, char *arg)
{
    int len;
    if ((buf->len == 0))
        return 0;
    if (read_data_from_buf(buf, (char *)msg, sizeof(int)) < sizeof(int))
        return 0;
    if ((len = msg_sizeof(*msg)) < 0) {
        { (buf)->len = 0; (buf)->start = 0; };
        return 0;
    }
    if (read_data_from_buf(buf, (char *)id, sizeof(int)) < sizeof(int))
        return 0;
    if (len > 0 && read_data_from_buf(buf, arg, len) < len)
  return 0;
    return 1;
}

variable_types:
msg
int *

variable_types:
id
int *

variable_types:
arg
char *


original_code:
static int client_exec_misc_cmd(RussiaBlock * rb)
{
    CliRB * rb_c = (CliRB *)rb->type_data;
    IOOPS * io_ops = rb->io_ops;
    char buf[((((12 + 7) / 8) * 20) + sizeof(int) * 2)];
    int msg, id;
    while (get_msg_from_buf(rb_c->cmd_buf, &msg, &id, buf)) {
        Player * player = 0;
        if (id == 0)
            player = rb->match[0];
        else if (id == 1)
            player = rb->match[1];
        if (!player)
            continue;
        if (msg == 4) {
            bits_to_win_map(buf, player->block_map);
            io_ops->out_win_map(player, player->block_map);
        }
        else if (msg == 2) {
            player->grade = *(int *)buf;
            io_ops->out_grade(player);
        }
        else if (msg == 3) {
            bits_to_matrix(buf, player->next_matrix);
            io_ops->out_next_block(player);
        }
        else if (msg == 6) {
            rb_c->status &= ~(0x1 << 0);
            rb_c->status &= ~(0x1 << 1);
        }
        else if (msg == 7) {
            rb_c->status &= ~(0x1 << 1);
        }
        else if (msg == 5) {
            rb_c->status |= (0x1 << 0);
        }
    }
    return 0;
}

canonical_original_code:
static int client_exec_misc_cmd(struct _RussiaBlock *rb)
{
    struct CliRB *rb_c = (CliRB *)rb->type_data;
    struct _InOut *io_ops = rb->io_ops;
    char buf[-1];
    int msg; int id;
    while (get_msg_from_buf(rb_c->cmd_buf, &msg, &id, buf)) {
        struct _Player *player = 0;
        if (id == 0)
            player = rb->match[0];
        else if (id == 1)
            player = rb->match[1];
        if (!player)
            continue;
        if (msg == 4) {
            bits_to_win_map(buf, player->block_map);
            io_ops->out_win_map(player, player->block_map);
        }
        else if (msg == 2) {
            player->grade = *(int *)buf;
            io_ops->out_grade(player);
        }
        else if (msg == 3) {
            bits_to_matrix(buf, player->next_matrix);
            io_ops->out_next_block(player);
        }
        else if (msg == 6) {
            rb_c->status &= ~(0x1 << 0);
            rb_c->status &= ~(0x1 << 1);
        }
        else if (msg == 7) {
            rb_c->status &= ~(0x1 << 1);
        }
        else if (msg == 5) {
            rb_c->status |= (0x1 << 0);
        }
    }
    return 0;
}

variable_types: struct _RussiaBlock { int type; int status; struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } *match[2]; struct _DataBuffer *cmd_buf; struct _InOut { int (*init)(struct _Player *player); void (*out_grade)(struct _Player *player); void (*out_next_block)(struct _Player *player); void (*out_win_map)(struct _Player *player, unsigned char map[20][12]); void (*refresh_screen)(int time); } *io_ops; struct _RBOPS { int (*init)(struct _RussiaBlock *rb, void *data); int (*native_init)(struct _RussiaBlock *rb); struct _Player *(*add_player)(struct _RussiaBlock *rb); void (*add_match)(struct _RussiaBlock *rb); void (*del_match)(struct _RussiaBlock *rb, struct _Player *player); int (*recv_player_data)(struct _RussiaBlock *rb); void (*send_player_data)(struct _RussiaBlock *rb); int (*exec_misc_cmd)(struct _RussiaBlock *rb); } *rb_ops; void *type_data; } *

variable_types: struct CliRB { int connfd; int status; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *cmd_buf; struct _DataBuffer *send_buf; int *event; } *

variable_types: struct _InOut { int (*init)(struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } *player); void (*out_grade)(struct _Player *player); void (*out_next_block)(struct _Player *player); void (*out_win_map)(struct _Player *player, unsigned char map[20][12]); void (*refresh_screen)(int time); } *

variable_types:
buf
char [-1]

variable_types: id int

user_defined_types: struct _RussiaBlock { int type; int status; struct _Player *match[2]; struct _DataBuffer *cmd_buf; struct _InOut *io_ops; struct _RBOPS *rb_ops; void *type_data; }

user_defined_types: struct CliRB { int connfd; int status; struct _DataBuffer *cmd_buf; struct _DataBuffer *send_buf; int *event; }

user_defined_types: struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer *event_buf; }


decompiled_code:
__int64 __fastcall client_send_player_data(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
__int64 v3; // rbx
_BYTE v4[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = *(_QWORD *)(a1 + 48);
result = read_data_from_buf(*(_QWORD *)(v1 + 16), v4, 1024LL);
if ( (int)result > 0 )
{
v3 = (int)result;
result = write(*(_DWORD *)v1, v4, (int)result);
if ( result != v3 )
result = fwrite("send data fail.\n", 1uLL, 0x10uLL, stderr);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
__int64 v3; // rbx
_BYTE v4[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = *(_QWORD *)(a1 + 48);
result = read_data_from_buf(*(_QWORD *)(v1 + 16), v4, 1024LL);
if ( (int)result > 0 )
{
v3 = (int)result;
result = write(*(_DWORD *)v1, v4, (int)result);
if ( result != v3 )
result = fwrite("send data fail.\n", 1uLL, 0x10uLL, stderr);
}
return result;
}

original_code:
static void client_send_player_data(RussiaBlock * rb)
{
    CliRB * rb_c = (CliRB *)rb->type_data;
    char buf[1024];
    int len;
    if ((len = read_data_from_buf(rb_c->send_buf, buf, 1024)) > 0)
        if (write(rb_c->connfd, buf, len) != len)
            fprintf(stderr, "send data fail.\n");
}

canonical_original_code:
static void client_send_player_data(struct _RussiaBlock *rb)
{
    struct CliRB *rb_c = (CliRB *)rb->type_data;
    char buf[1024];
    int len;
    if ((len = read_data_from_buf(rb_c->send_buf, buf, 1024)) > 0)
        if (write(rb_c->connfd, buf, len) != len)
            fprintf(stderr, "send data fail.\n");
}

variable_types:
buf
char [1024]

user_defined_types: struct _InOut { int (*init)(struct _Player *player); void (*out_grade)(struct _Player *player); void (*out_next_block)(struct _Player *player); void (*out_win_map)(struct _Player *player, unsigned char map[20][12]); void (*refresh_screen)(int time); }

user_defined_types: struct _RBOPS { int (*init)(struct _RussiaBlock *rb, void *data); int (*native_init)(struct _RussiaBlock *rb); struct _Player *(*add_player)(struct _RussiaBlock *rb); void (*add_match)(struct _RussiaBlock *rb); void (*del_match)(struct _RussiaBlock *rb, struct _Player *player); int (*recv_player_data)(struct _RussiaBlock *rb); void (*send_player_data)(struct _RussiaBlock *rb); int (*exec_misc_cmd)(struct _RussiaBlock *rb); }

user_defined_types: struct Shape { unsigned char matrix[4][4]; int w; }


decompiled_code:
__int64 __fastcall client_init(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebp
_QWORD *v4; // rax
_QWORD *v5; // rbx
__int64 v6; // rax
int v7; // er8
__int64 result; // rax
struct sockaddr v9; // [rsp+0h] [rbp-38h] BYREF

v9 = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("client");
result = 0LL;
}
else
{
v3 = v2;
v9.sa_family = 2;
*(_WORD *)v9.sa_data = _bswap_16_constprop_0(a2);
*(_DWORD *)&v9.sa_data[2] = inet_addr(a2);
if ( connect(v3, &v9, 0x10u) < 0 )
{
perror("client");
close(v3);
result = 0LL;
}
else
{
fcntl(v3, 4, 2048LL);
v4 = malloc(0x20uLL);
*(_DWORD *)v4 = v3;
v5 = v4;
v4[1] = malloc_data_buf(512LL);
v6 = malloc_data_buf(100LL);
*(_QWORD *)(a1 + 48) = v5;
v5[2] = v6;
v5[3] = &key_code;
if ( (int)x_init_display(720LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v7 = init_digit();
result = 1LL;
if ( v7 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebp
_QWORD *v4; // rax
_QWORD *v5; // rbx
__int64 v6; // rax
int v7; // er8
__int64 result; // rax
struct sockaddr v9; // [rsp+0h] [rbp-38h] BYREF

v9 = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("client");
result = 0LL;
}
else
{
v3 = v2;
v9.sa_family = 2;
*(_WORD *)v9.sa_data = func7(a2);
*(_DWORD *)&v9.sa_data[2] = inet_addr(a2);
if ( connect(v3, &v9, 0x10u) < 0 )
{
perror("client");
close(v3);
result = 0LL;
}
else
{
fcntl(v3, 4, 2048LL);
v4 = malloc(0x20uLL);
*(_DWORD *)v4 = v3;
v5 = v4;
v4[1] = malloc_data_buf(512LL);
v6 = malloc_data_buf(100LL);
*(_QWORD *)(a1 + 48) = v5;
v5[2] = v6;
v5[3] = &key_code;
if ( (int)x_init_display(720LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v7 = init_digit();
result = 1LL;
if ( v7 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
}
}
return result;
}

original_code:
static int client_init(RussiaBlock * rb, void * data)
{
    char * ip_addr = (char *)data;
    struct sockaddr_in servaddr = {0};
    int sockfd;
    CliRB * rb_c;
    if ((sockfd = socket(2, SOCK_STREAM, 0)) < 0) {
        perror("client");
        return 0;
    }
    servaddr.sin_family = 2;
    servaddr.sin_port = htons(9877);
    servaddr.sin_addr.s_addr = inet_addr(ip_addr);
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
        perror("client");
        goto close_fd;
    }
    fcntl(sockfd, 4, 04000);
    rb_c = (CliRB *)malloc(sizeof(CliRB));
    rb_c->connfd = sockfd;
    rb_c->cmd_buf = (DataBuffer *)malloc_data_buf(512);
    rb_c->send_buf = (DataBuffer *)malloc_data_buf(100);
    rb_c->event = key_code;
    rb->type_data = rb_c;
    if (x_init_display(((12 * 20 + 6 * 20) * 2), (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
close_fd:
    close(sockfd);
    return 0;
}

canonical_original_code:
static int client_init(struct _RussiaBlock *rb, void *data)
{
    char *ip_addr = (char *)data;
    struct sockaddr_in servaddr = {0};
    int sockfd;
    struct CliRB *rb_c;
    if ((sockfd = socket(2, SOCK_STREAM, 0)) < 0) {
        perror("client");
        return 0;
    }
    servaddr.sin_family = 2;
    servaddr.sin_port = htons(9877);
    servaddr.sin_addr.s_addr = inet_addr(ip_addr);
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
        perror("client");
        goto close_fd;
    }
    fcntl(sockfd, 4, 04000);
    rb_c = (struct CliRB *)malloc(sizeof(CliRB));
    rb_c->connfd = sockfd;
    rb_c->cmd_buf = (struct _DataBuffer *)malloc_data_buf(512);
    rb_c->send_buf = (struct _DataBuffer *)malloc_data_buf(100);
    rb_c->event = key_code;
    rb->type_data = rb_c;
    if (x_init_display(((12 * 20 + 6 * 20) * 2), (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
close_fd:
    close(sockfd);
    return 0;
}

variable_types:
data
void *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: sockfd int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall client_recv_player_data(__int64 a1)
{
__int64 v1; // rbp

v1 = *(_QWORD *)(a1 + 48);
recv_server_data_isra_0(a1);
if ( (*(_BYTE *)(v1 + 4) & 1) != 0 )
recv_key_event_isra_0(v1);
return 1LL;
}

original_code:
static int client_recv_player_data(RussiaBlock * rb)
{
    CliRB * rb_c = (CliRB *)rb->type_data;
    recv_server_data(rb);
    if (rb_c->status & (0x1 << 0))
        recv_key_event(rb_c);
    return 1;
}

canonical_original_code:
static int client_recv_player_data(struct _RussiaBlock *rb)
{
    struct CliRB *rb_c = (CliRB *)rb->type_data;
    recv_server_data(rb);
    if (rb_c->status & (0x1 << 0))
        recv_key_event(rb_c);
    return 1;
}


decompiled_code:
Window __fastcall create_x_window(__int64 a1, Window a2)
{
Colormap v2; // rax
unsigned int v3; // er9
Colormap v4; // rbp
unsigned int v5; // er8
Window v6; // r12
XSetWindowAttributes v8; // [rsp+0h] [rbp-88h] BYREF

v2 = XCreateColormap(dpy, a2, *(Visual **)a1, 0);
v3 = *((_DWORD *)&screen_image + 5);
v8.colormap = v2;
v4 = v2;
LODWORD(v2) = *(_DWORD *)(a1 + 20);
v8.event_mask = 1LL;
v5 = *((_DWORD *)&screen_image + 4);
v8.border_pixel = 0LL;
v6 = XCreateWindow(dpy, a2, 0, 0, v5, v3, 0, v2, 1u, *(Visual **)a1, 0x2808uLL, &v8);
XStoreName(dpy, v6, "RussiaBlock");
if ( *(_DWORD *)(a1 + 24) != 4 )
XFreeColormap(dpy, v4);
return v6;
}

canonical_decompiled_code:
Window __fastcall func0(__int64 a1, Window a2)
{
Colormap v2; // rax
unsigned int v3; // er9
Colormap v4; // rbp
unsigned int v5; // er8
Window v6; // r12
XSetWindowAttributes v8; // [rsp+0h] [rbp-88h] BYREF

v2 = XCreateColormap(dpy, a2, *(Visual **)a1, 0);
v3 = *((_DWORD *)&screen_image + 5);
v8.colormap = v2;
v4 = v2;
LODWORD(v2) = *(_DWORD *)(a1 + 20);
v8.event_mask = 1LL;
v5 = *((_DWORD *)&screen_image + 4);
v8.border_pixel = 0LL;
v6 = XCreateWindow(dpy, a2, 0, 0, v5, v3, 0, v2, 1u, *(Visual **)a1, 0x2808uLL, &v8);
XStoreName(dpy, v6, "RussiaBlock");
if ( *(_DWORD *)(a1 + 24) != 4 )
XFreeColormap(dpy, v4);
return v6;
}

original_code:
static Window create_x_window(XVisualInfo * vinfo, Window root)
{
    XSetWindowAttributes attribs;
    Colormap tmpcmap;
    Window win;
    tmpcmap = XCreateColormap(dpy, root, vinfo->visual, 0);
    attribs.event_mask = (1L<<0);
    attribs.border_pixel = 0;
    attribs.colormap = tmpcmap;
    win = XCreateWindow(dpy, root,
            0, 0,
            screen_image.w, screen_image.h,
            0,
            vinfo->depth,
            1,
            vinfo->visual,
            (1L<<11) | (1L<<13) | (1L<<3),
            &attribs);
    XStoreName(dpy, win, "RussiaBlock");
    if (vinfo->class != 4)
        XFreeColormap(dpy, tmpcmap);
    return win;
}

canonical_original_code:
static unsigned long create_x_window(struct XVisualInfo *vinfo, unsigned long root)
{
    struct XSetWindowAttributes attribs;
    unsigned long tmpcmap;
    unsigned long win;
    tmpcmap = XCreateColormap(dpy, root, vinfo->visual, 0);
    attribs.event_mask = (1L<<0);
    attribs.border_pixel = 0;
    attribs.colormap = tmpcmap;
    win = XCreateWindow(dpy, root,
            0, 0,
            screen_image.w, screen_image.h,
            0,
            vinfo->depth,
            1,
            vinfo->visual,
            (1L<<11) | (1L<<13) | (1L<<3),
            &attribs);
    XStoreName(dpy, win, "RussiaBlock");
    if (vinfo->class != 4)
        XFreeColormap(dpy, tmpcmap);
    return win;
}

variable_types: struct XVisualInfo { struct Visual { struct _XExtData { int number; struct _XExtData *next; int (*free_private)(struct _XExtData *extension); char *private_data; } *ext_data; unsigned long visualid; int class; unsigned long red_mask; int bits_per_rgb; int map_entries; } *visual; unsigned long visualid; int screen; int depth; int class; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int colormap_size; int bits_per_rgb; } *

variable_types:
root
unsigned long

variable_types: struct XSetWindowAttributes { unsigned long background_pixmap; unsigned long background_pixel; unsigned long border_pixmap; unsigned long border_pixel; int bit_gravity; int win_gravity; int backing_store; unsigned long backing_planes; unsigned long backing_pixel; int save_under; long event_mask; long do_not_propagate_mask; int override_redirect; unsigned long colormap; unsigned long cursor; }

variable_types:
tmpcmap
unsigned long

variable_types:
win
unsigned long

user_defined_types: struct XVisualInfo { struct Visual *visual; unsigned long visualid; int screen; int depth; int class; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int colormap_size; int bits_per_rgb; }

user_defined_types: struct Visual { struct _XExtData *ext_data; unsigned long visualid; int class; unsigned long red_mask; int bits_per_rgb; int map_entries; }


decompiled_code:
__int64 __fastcall x_init_display(int a1, int a2)
{
Display *v3; // rax
int v4; // eax
Visual *v5; // rax
XVisualInfo *v6; // rax
Window v7; // rax
__int64 v8; // rdi
__int64 result; // rax
char *v10; // rax
int v11; // [rsp+Ch] [rbp-DCh] BYREF
XVisualInfo v12; // [rsp+10h] [rbp-D8h] BYREF
XGCValues v13; // [rsp+50h] [rbp-98h] BYREF

v3 = XOpenDisplay(0LL);
dpy = v3;
if ( v3 )
{
XSynchronize(v3, 1);
v4 = XDefaultScreen(dpy);
v5 = XDefaultVisual(dpy, v4);
v12.visualid = XVisualIDFromVisual(v5);
v6 = XGetVisualInfo(dpy, 1LL, &v12, &v11);
visual_info = (__int64)v6;
if ( v11 )
{
v7 = XRootWindow(dpy, v6->screen);
v8 = visual_info;
*((_DWORD *)&screen_image + 4) = a1;
*((_DWORD *)&screen_image + 5) = a2;
x_win = create_x_window(v8, v7);
v13.graphics_exposures = 0;
x_gc = XCreateGC(dpy, x_win, 0x10000uLL, &v13);
XMapWindow(dpy, x_win);
XMoveWindow(dpy, x_win, 0, 0);
x_fb = reset_frame_buf_isra_0(*(Visual **)visual_info, *(_DWORD *)(visual_info + 20));
result = 0LL;
}
else
{
printf("Bad visual id %d\n", LODWORD(v12.visualid));
result = 0xFFFFFFFFLL;
}
}
else
{
v10 = getenv("DISPLAY");
printf("Could not open display [%s]\n", v10);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
Display *v3; // rax
int v4; // eax
Visual *v5; // rax
XVisualInfo *v6; // rax
Window v7; // rax
__int64 v8; // rdi
__int64 result; // rax
char *v10; // rax
int v11; // [rsp+Ch] [rbp-DCh] BYREF
XVisualInfo v12; // [rsp+10h] [rbp-D8h] BYREF
XGCValues v13; // [rsp+50h] [rbp-98h] BYREF

v3 = XOpenDisplay(0LL);
dpy = v3;
if ( v3 )
{
XSynchronize(v3, 1);
v4 = XDefaultScreen(dpy);
v5 = XDefaultVisual(dpy, v4);
v12.visualid = XVisualIDFromVisual(v5);
v6 = XGetVisualInfo(dpy, 1LL, &v12, &v11);
visual_info = (__int64)v6;
if ( v11 )
{
v7 = XRootWindow(dpy, v6->screen);
v8 = visual_info;
*((_DWORD *)&screen_image + 4) = a1;
*((_DWORD *)&screen_image + 5) = a2;
x_win = func0(v8, v7);
v13.graphics_exposures = 0;
x_gc = XCreateGC(dpy, x_win, 0x10000uLL, &v13);
XMapWindow(dpy, x_win);
XMoveWindow(dpy, x_win, 0, 0);
x_fb = reset_frame_buf_isra_0(*(Visual **)visual_info, *(_DWORD *)(visual_info + 20));
result = 0LL;
}
else
{
printf("Bad visual id %d\n", LODWORD(v12.visualid));
result = 0xFFFFFFFFLL;
}
}
else
{
v10 = getenv("DISPLAY");
printf("Could not open display [%s]\n", v10);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int x_init_display(int w, int h)
{
 int vnum;
 XVisualInfo vinfo;
 Window root;
 if (!(dpy = XOpenDisplay(0))) {
        printf("Could not open display [%s]\n", getenv("DISPLAY"));
        return -1;
 }
 XSynchronize(dpy, 1);
    vinfo.visualid = XVisualIDFromVisual(XDefaultVisual(dpy, XDefaultScreen(dpy)));
 visual_info = XGetVisualInfo(dpy, 0x1, &vinfo, &vnum);
 if (vnum == 0) {
        printf("Bad visual id %d\n", (int)vinfo.visualid);
        return -1;
 }
 root = XRootWindow(dpy, visual_info->screen);
 screen_image.w = w;
 screen_image.h = h;
    x_win = create_x_window(visual_info, root);
 {
  XGCValues xgcvalues;
  xgcvalues.graphics_exposures = 0;
  x_gc = XCreateGC(dpy, x_win, (1L<<16), &xgcvalues);
 }
 XMapWindow(dpy, x_win);
 XMoveWindow(dpy, x_win, 0, 0);
    x_fb = reset_frame_buf(visual_info);
 return 0;
}

canonical_original_code:
int x_init_display(int w, int h)
{
 int vnum;
 struct XVisualInfo vinfo;
 unsigned long root;
 if (!(dpy = XOpenDisplay(0))) {
        printf("Could not open display [%s]\n", getenv("DISPLAY"));
        return -1;
 }
 XSynchronize(dpy, 1);
    vinfo.visualid = XVisualIDFromVisual(XDefaultVisual(dpy, XDefaultScreen(dpy)));
 visual_info = XGetVisualInfo(dpy, 0x1, &vinfo, &vnum);
 if (vnum == 0) {
        printf("Bad visual id %d\n", (int)vinfo.visualid);
        return -1;
 }
 root = XRootWindow(dpy, visual_info->screen);
 screen_image.w = w;
 screen_image.h = h;
    x_win = create_x_window(visual_info, root);
 {
  struct XGCValues xgcvalues;
  xgcvalues.graphics_exposures = 0;
  x_gc = XCreateGC(dpy, x_win, (1L<<16), &xgcvalues);
 }
 XMapWindow(dpy, x_win);
 XMoveWindow(dpy, x_win, 0, 0);
    x_fb = reset_frame_buf(visual_info);
 return 0;
}

variable_types: w int

variable_types: h int

variable_types: vnum int

variable_types: struct XVisualInfo { struct Visual { struct _XExtData { int number; struct _XExtData *next; int (*free_private)(struct _XExtData *extension); char *private_data; } *ext_data; unsigned long visualid; int class; unsigned long red_mask; int bits_per_rgb; int map_entries; } *visual; unsigned long visualid; int screen; int depth; int class; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int colormap_size; int bits_per_rgb; }

variable_types: struct XGCValues { int function; unsigned long plane_mask; unsigned long foreground; unsigned long background; int line_width; int line_style; int cap_style; int join_style; int fill_style; int fill_rule; int arc_mode; unsigned long tile; unsigned long stipple; int ts_x_origin; int ts_y_origin; unsigned long font; int subwindow_mode; int graphics_exposures; int clip_x_origin; int clip_y_origin; unsigned long clip_mask; int dash_offset; char dashes; }


decompiled_code:
int x_paint()
{
XPutImage(dpy, x_win, x_gc, x_fb, 0, 0, 0, 0, *((_DWORD *)&screen_image + 4), *((_DWORD *)&screen_image + 5));
return XSync(dpy, 0);
}

canonical_decompiled_code:
int func4()
{
XPutImage(dpy, x_win, x_gc, x_fb, 0, 0, 0, 0, *((_DWORD *)&screen_image + 4), *((_DWORD *)&screen_image + 5));
return XSync(dpy, 0);
}

original_code:
void x_paint(void)
{
    XPutImage(dpy, x_win, x_gc, x_fb, 0, 0, 0, 0, screen_image.w, screen_image.h);
    XSync(dpy, 0);
}

canonical_original_code:
void x_paint()
{
    XPutImage(dpy, x_win, x_gc, x_fb, 0, 0, 0, 0, screen_image.w, screen_image.h);
    XSync(dpy, 0);
}


decompiled_code:
__int64 __fastcall image_copy(unsigned int *a1, int a2, int a3, __int64 a4, signed int a5, signed int a6, int a7, unsigned int a8)
{
int v10; // edx
__int64 result; // rax
signed int v12; // esi
signed int v13; // ebx
int v14; // esi
int v15; // ebx
int v16; // er9

v10 = a7;
result = a8;
if ( !a7 )
v10 = *(_DWORD *)(a4 + 16);
if ( !a8 )
result = *(unsigned int *)(a4 + 20);
v12 = a1[4];
if ( v12 > a5 )
{
v13 = a1[5];
if ( v13 > a6 )
{
if ( v10 + a5 > *(_DWORD *)(a4 + 16) )
v10 = *(_DWORD *)(a4 + 16) - a5;
v14 = v12 - a2;
if ( v14 <= v10 )
v10 = v14;
if ( (int)result + a6 > *(_DWORD *)(a4 + 20) )
LODWORD(result) = *(_DWORD *)(a4 + 20) - a6;
v15 = v13 - a3;
if ( v15 <= (int)result )
LODWORD(result) = v15;
v16 = result;
result = a1[2];
if ( (_DWORD)result == 2 )
{
result = image_copy16_constprop_0((__int64)a1, a2, a3, a4, v10, v16);
}
else if ( (_DWORD)result == 4 )
{
result = image_copy32_constprop_0((__int64)a1, a2, a3, a4, v10, v16);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, int a2, int a3, __int64 a4, signed int a5, signed int a6, int a7, unsigned int a8)
{
int v10; // edx
__int64 result; // rax
signed int v12; // esi
signed int v13; // ebx
int v14; // esi
int v15; // ebx
int v16; // er9

v10 = a7;
result = a8;
if ( !a7 )
v10 = *(_DWORD *)(a4 + 16);
if ( !a8 )
result = *(unsigned int *)(a4 + 20);
v12 = a1[4];
if ( v12 > a5 )
{
v13 = a1[5];
if ( v13 > a6 )
{
if ( v10 + a5 > *(_DWORD *)(a4 + 16) )
v10 = *(_DWORD *)(a4 + 16) - a5;
v14 = v12 - a2;
if ( v14 <= v10 )
v10 = v14;
if ( (int)result + a6 > *(_DWORD *)(a4 + 20) )
LODWORD(result) = *(_DWORD *)(a4 + 20) - a6;
v15 = v13 - a3;
if ( v15 <= (int)result )
LODWORD(result) = v15;
v16 = result;
result = a1[2];
if ( (_DWORD)result == 2 )
{
result = func3((__int64)a1, a2, a3, a4, v10, v16);
}
else if ( (_DWORD)result == 4 )
{
result = func1((__int64)a1, a2, a3, a4, v10, v16);
}
}
}
return result;
}

original_code:
void image_copy(PixImage * dst_bitmap, int dx, int dy, PixImage * src_bitmap, int sx, int sy, int w, int h)
{
    if (w == 0)
        w = src_bitmap->w;
    if (h == 0)
        h = src_bitmap->h;
    if (sx >= dst_bitmap->w)
        return;
    if (sy >= dst_bitmap->h)
        return;
    if (sx + w > src_bitmap->w)
        w = src_bitmap->w - sx;
    if (w > dst_bitmap->w - dx)
        w = dst_bitmap->w - dx;
    if (sy + h > src_bitmap->h)
        h = src_bitmap->h - sy;
    if (h > dst_bitmap->h - dy)
        h = dst_bitmap->h - dy;
    if (dst_bitmap->pixelbytes == 2)
        image_copy16(dst_bitmap, dx, dy, src_bitmap, sx, sy, w, h);
    else if (dst_bitmap->pixelbytes == 4)
        image_copy32(dst_bitmap, dx, dy, src_bitmap, sx, sy, w, h);
}

canonical_original_code:
void image_copy(struct PixImage *dst_bitmap, int dx, int dy, struct PixImage *src_bitmap, int sx, int sy, int w, int h)
{
    if (w == 0)
        w = src_bitmap->w;
    if (h == 0)
        h = src_bitmap->h;
    if (sx >= dst_bitmap->w)
        return;
    if (sy >= dst_bitmap->h)
        return;
    if (sx + w > src_bitmap->w)
        w = src_bitmap->w - sx;
    if (w > dst_bitmap->w - dx)
        w = dst_bitmap->w - dx;
    if (sy + h > src_bitmap->h)
        h = src_bitmap->h - sy;
    if (h > dst_bitmap->h - dy)
        h = dst_bitmap->h - dy;
    if (dst_bitmap->pixelbytes == 2)
        image_copy16(dst_bitmap, dx, dy, src_bitmap, sx, sy, w, h);
    else if (dst_bitmap->pixelbytes == 4)
        image_copy32(dst_bitmap, dx, dy, src_bitmap, sx, sy, w, h);
}

variable_types: struct PixImage { unsigned char *pixels; int pixelbytes; int rowbytes; int w; } *

variable_types: dx int

variable_types: dy int

variable_types: sx int

variable_types: sy int


original_code:
void clear_image(PixImage * bitmap)
{
 int i, n;
 if (!bitmap->pixels)
  return;
 n = bitmap->w * bitmap->h;
 if (bitmap->pixelbytes == 2) {
  Color16 * pixels16 = (Color16 *)bitmap->pixels;
  for (i = 0; i < n; i++)
   pixels16[i] = 0;
 }
 else if (bitmap->pixelbytes == 4) {
  Color32 * pixels32 = (Color32 *)bitmap->pixels;
  for (i = 0; i < n; i++)
   pixels32[i] = 0;
 }
}

canonical_original_code:
void clear_image(struct PixImage *bitmap)
{
 int i; int n;
 if (!bitmap->pixels)
  return;
 n = bitmap->w * bitmap->h;
 if (bitmap->pixelbytes == 2) {
  unsigned short *pixels16 = (Color16 *)bitmap->pixels;
  for (i = 0; i < n; i++)
   pixels16[i] = 0;
 }
 else if (bitmap->pixelbytes == 4) {
  unsigned long *pixels32 = (Color32 *)bitmap->pixels;
  for (i = 0; i < n; i++)
   pixels32[i] = 0;
 }
}

variable_types: i int

variable_types: n int

user_defined_types: struct PixImage { unsigned char *pixels; int pixelbytes; int rowbytes; int w; }


decompiled_code:
void __fastcall release_bitmap(void *ptr)
{
void *v2; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
void *v2; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

original_code:
void release_bitmap(PixImage * bitmap)
{
 if (bitmap->pixels) {
  free(bitmap->pixels);
  bitmap->pixels = ((void *)0);
 }
 free(bitmap);
}

canonical_original_code:
void release_bitmap(struct PixImage *bitmap)
{
 if (bitmap->pixels) {
  free(bitmap->pixels);
  bitmap->pixels = ((void *)0);
 }
 free(bitmap);
}


original_code:
static inline int match_ready(RussiaBlock * rb)
{
    return ((rb->match[0]->status & (0x1 << 1))
                && (rb->match[1]->status & (0x1 << 1)));
}

canonical_original_code:
static inline int match_ready(struct _RussiaBlock *rb)
{
    return ((rb->match[0]->status & (0x1 << 1))
                && (rb->match[1]->status & (0x1 << 1)));
}

variable_types: struct _RussiaBlock { int type; int status; struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } *match[2]; struct _DataBuffer *cmd_buf; struct _InOut { int (*init)(struct _Player *player); void (*out_grade)(struct _Player *player); void (*out_next_block)(struct _Player *player); void (*out_win_map)(struct _Player *player, unsigned char map[20][12]); void (*refresh_screen)(int time); } *io_ops; struct _RBOPS { int (*init)(struct _RussiaBlock *rb, void *data); int (*native_init)(struct _RussiaBlock *rb); struct _Player *(*add_player)(struct _RussiaBlock *rb); void (*add_match)(struct _RussiaBlock *rb); void (*del_match)(struct _RussiaBlock *rb, struct _Player *player); int (*recv_player_data)(struct _RussiaBlock *rb); void (*send_player_data)(struct _RussiaBlock *rb); int (*exec_misc_cmd)(struct _RussiaBlock *rb); } *rb_ops; void *type_data; } *


decompiled_code:
__int64 __fastcall init_match_player(__int64 a1)
{
init_player(a1, *(_QWORD *)(a1 + 8));
return init_player(a1, *(_QWORD *)(a1 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
init_player(a1, *(_QWORD *)(a1 + 8));
return init_player(a1, *(_QWORD *)(a1 + 16));
}

original_code:
static void init_match_player(RussiaBlock * rb)
{
    int i;
    for (i = 0; i < 2; i++)
        init_player(rb, rb->match[i]);
}

canonical_original_code:
static void init_match_player(struct _RussiaBlock *rb)
{
    int i;
    for (i = 0; i < 2; i++)
        init_player(rb, rb->match[i]);
}


decompiled_code:
__int64 __fastcall game_start(unsigned int *a1)
{
__int64 result; // rax
int v2; // er8
unsigned int v3; // eax

result = *a1;
switch ( (_DWORD)result )
{
case 2:
result = ready_event(*((_QWORD *)a1 + 1));
if ( !(_DWORD)result )
{
LABEL_5:
a1[1] &= 0xFFFFFFFE;
return result;
}
result = init_player(a1, *((_QWORD *)a1 + 1));
a1[1] |= 1u;
break;
case 4:
if ( *((_QWORD *)a1 + 1) && *((_QWORD *)a1 + 2) )
{
v2 = match_ready((__int64)a1);
v3 = a1[1];
if ( v2 )
{
a1[1] = v3 | 1;
result = init_match_player((__int64)a1);
}
else
{
result = v3 & 0xFFFFFFFE;
a1[1] = result;
}
}
else
{
result = (__int64)add_match((__int64)a1);
}
break;
case 1:
result = ready_event(*((_QWORD *)a1 + 1));
if ( !(_DWORD)result )
goto LABEL_5;
init_player(a1, *((_QWORD *)a1 + 1));
result = init_player(a1, *((_QWORD *)a1 + 2));
a1[1] |= 1u;
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1)
{
__int64 result; // rax
int v2; // er8
unsigned int v3; // eax

result = *a1;
switch ( (_DWORD)result )
{
case 2:
result = ready_event(*((_QWORD *)a1 + 1));
if ( !(_DWORD)result )
{
LABEL_5:
a1[1] &= 0xFFFFFFFE;
return result;
}
result = init_player(a1, *((_QWORD *)a1 + 1));
a1[1] |= 1u;
break;
case 4:
if ( *((_QWORD *)a1 + 1) && *((_QWORD *)a1 + 2) )
{
v2 = func0((__int64)a1);
v3 = a1[1];
if ( v2 )
{
a1[1] = v3 | 1;
result = func1((__int64)a1);
}
else
{
result = v3 & 0xFFFFFFFE;
a1[1] = result;
}
}
else
{
result = (__int64)add_match((__int64)a1);
}
break;
case 1:
result = ready_event(*((_QWORD *)a1 + 1));
if ( !(_DWORD)result )
goto LABEL_5;
init_player(a1, *((_QWORD *)a1 + 1));
result = init_player(a1, *((_QWORD *)a1 + 2));
a1[1] |= 1u;
break;
default:
return result;
}
return result;
}

original_code:
static void game_start(RussiaBlock * rb)
{
    switch (rb->type) {
    case 2:
        if (ready_event(rb->match[0])) {
   init_player(rb, rb->match[0]);
            rb->status |= (0x1 << 0);
  }
        else
            rb->status &= ~(0x1 << 0);
    break;
    case 1:
    {
  if (ready_event(rb->match[0])) {
   init_player(rb, rb->match[0]);
   init_player(rb, rb->match[1]);
            rb->status |= (0x1 << 0);
  }
        else
            rb->status &= ~(0x1 << 0);
    }
    break;
    case 3:
    break;
    case 4:
        if (!(rb->match[0] && rb->match[1])) {
            add_match(rb);
  }
        else if (match_ready(rb)) {
            rb->status |= (0x1 << 0);
            init_match_player(rb);
        }
        else
            rb->status &= ~(0x1 << 0);
    break;
    case 5:
    break;
    }
}

canonical_original_code:
static void game_start(struct _RussiaBlock *rb)
{
    switch (rb->type) {
    case 2:
        if (ready_event(rb->match[0])) {
   init_player(rb, rb->match[0]);
            rb->status |= (0x1 << 0);
  }
        else
            rb->status &= ~(0x1 << 0);
    break;
    case 1:
    {
  if (ready_event(rb->match[0])) {
   init_player(rb, rb->match[0]);
   init_player(rb, rb->match[1]);
            rb->status |= (0x1 << 0);
  }
        else
            rb->status &= ~(0x1 << 0);
    }
    break;
    case 3:
    break;
    case 4:
        if (!(rb->match[0] && rb->match[1])) {
            add_match(rb);
  }
        else if (match_ready(rb)) {
            rb->status |= (0x1 << 0);
            init_match_player(rb);
        }
        else
            rb->status &= ~(0x1 << 0);
    break;
    case 5:
    break;
    }
}


decompiled_code:
__int64 __fastcall native_refresh_screen(int a1)
{
int v1; // edi
__int64 result; // rax

v1 = slice_0 + a1;
result = (unsigned int)(v1 + 19);
if ( (unsigned int)result > 0x26 )
{
slice_0 = v1 % 20;
result = x_paint();
}
else
{
slice_0 = v1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // edi
__int64 result; // rax

v1 = slice_0 + a1;
result = (unsigned int)(v1 + 19);
if ( (unsigned int)result > 0x26 )
{
slice_0 = v1 % 20;
result = x_paint();
}
else
{
slice_0 = v1;
}
return result;
}

original_code:
static void native_refresh_screen(Mtime time)
{
    static Mtime slice = 0;
    slice += time;
    if ((slice / 20) == 0)
        return;
    slice %= 20;
    x_paint();
    return;
}

canonical_original_code:
static void native_refresh_screen(int time)
{
    static int slice = 0;
    slice += time;
    if ((slice / 20) == 0)
        return;
    slice %= 20;
    x_paint();
    return;
}

variable_types: time int

variable_types: slice int


decompiled_code:
__int64 __fastcall set_block_image(int a1, int a2, int a3, int a4, char a5)
{
return fill_image(
&screen_image,
a5 == 0 ? 0 : 0xFF00FF,
(unsigned int)(a3 + 20 * a1 + 5),
(unsigned int)(a4 + 20 * a2),
20LL,
20LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3, int a4, char a5)
{
return fill_image(
&screen_image,
a5 == 0 ? 0 : 0xFF00FF,
(unsigned int)(a3 + 20 * a1 + 5),
(unsigned int)(a4 + 20 * a2),
20LL,
20LL);
}

original_code:
static void set_block_image(int x, int y, int left, int top, char value)
{
    Color32 color = value ? 0x00ff00ff : 0;
    int pixel_x = x * 20 + left + 5;
    int pixel_y = y * 20 + top;
    fill_image(&screen_image, color, pixel_x, pixel_y, 20, 20);
}

canonical_original_code:
static void set_block_image(int x, int y, int left, int top, char value)
{
    unsigned long color = value ? 0x00ff00ff : 0;
    int pixel_x = x * 20 + left + 5;
    int pixel_y = y * 20 + top;
    fill_image(&screen_image, color, pixel_x, pixel_y, 20, 20);
}

variable_types: x int

variable_types: y int

variable_types: left int

variable_types: top int

variable_types: value char

variable_types:
color
unsigned long


original_code:
static void native_io_out_win_map(Player * player, Uchar map[20][12])
{
    NatPlayer * np = (NatPlayer *)player;
    HandleIO * handle = np->io_handle;
    int x, y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
            if (np->old_map[y][x] != map[y][x]) {
                np->old_map[y][x] = map[y][x];
                set_block_image(x, y, handle->left, handle->top, map[y][x]);
            }
        }
    }
}

canonical_original_code:
static void native_io_out_win_map(struct _Player *player, unsigned char map[20][12])
{
    struct NatPlayer *np = (NatPlayer *)player;
    struct _HandleIO *handle = np->io_handle;
    int x; int y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
            if (np->old_map[y][x] != map[y][x]) {
                np->old_map[y][x] = map[y][x];
                set_block_image(x, y, handle->left, handle->top, map[y][x]);
            }
        }
    }
}

variable_types: struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } *

variable_types:
map
unsigned char [20][12]

variable_types: struct NatPlayer { struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } player; unsigned char old_map[20][12]; struct _HandleIO { int left; int wid; int event[6]; } *io_handle; } *

user_defined_types: struct NatPlayer { struct _Player player; unsigned char old_map[20][12]; struct _HandleIO *io_handle; }


decompiled_code:
__int64 __fastcall matrix_to_screen(__int64 a1, int a2, int a3)
{
int i; // ebp
__int64 j; // rbx
char v7; // r8
int v8; // edi
__int64 result; // rax

for ( i = 0; i != 4; ++i )
{
for ( j = 0LL; j != 4; ++j )
{
v7 = *(_BYTE *)(a1 + j);
v8 = j;
result = set_block_image(v8, i, a2, a3, v7);
}
a1 += 4LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, int a3)
{
int i; // ebp
__int64 j; // rbx
char v7; // r8
int v8; // edi
__int64 result; // rax

for ( i = 0; i != 4; ++i )
{
for ( j = 0LL; j != 4; ++j )
{
v7 = *(_BYTE *)(a1 + j);
v8 = j;
result = func1(v8, i, a2, a3, v7);
}
a1 += 4LL;
}
return result;
}

original_code:
static void matrix_to_screen(Uchar matrix[4][4], int left, int top)
{
    int y, x;
    for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
            set_block_image(x, y, left, top, matrix[y][x]);
}

canonical_original_code:
static void matrix_to_screen(unsigned char matrix[4][4], int left, int top)
{
    int y; int x;
    for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
            set_block_image(x, y, left, top, matrix[y][x]);
}

variable_types:
matrix
unsigned char [4][4]


decompiled_code:
__int64 __fastcall native_io_out_next_block(__int64 a1)
{
return matrix_to_screen(a1 + 100, **(_DWORD **)(a1 + 608) + 260, *(_DWORD *)(*(_QWORD *)(a1 + 608) + 4LL));
}

original_code:
static void native_io_out_next_block(Player * player)
{
    NatPlayer * np = (NatPlayer *)player;
    HandleIO * handle = np->io_handle;
    matrix_to_screen(player->next_matrix,
            handle->left + (12 + 1) * 20,
            handle->top);
}

canonical_original_code:
static void native_io_out_next_block(struct _Player *player)
{
    struct NatPlayer *np = (NatPlayer *)player;
    struct _HandleIO *handle = np->io_handle;
    matrix_to_screen(player->next_matrix,
            handle->left + (12 + 1) * 20,
            handle->top);
}

variable_types: struct _HandleIO { int left; int wid; int event[6]; } *


original_code:
static int native_io_init(Player * player)
{
    NatPlayer * np = (NatPlayer *)player;
    HandleIO * handle = np->io_handle;
    int x, y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
   player->block_map[y][x] = 0;
            np->old_map[y][x] = 0;
  }
    }
    fill_image(&screen_image,
            0xff,
            handle->left,
            handle->top,
            5,
            20 * 20);
    fill_image(&screen_image,
            0xff,
            handle->left + 12 * 20 + 5,
            handle->top,
            5,
            20 * 20);
    fill_image(&screen_image,
            0x0,
            handle->left + 5,
            handle->top,
            12 * 20,
            20 * 20);
    return 0;
}

canonical_original_code:
static int native_io_init(struct _Player *player)
{
    struct NatPlayer *np = (NatPlayer *)player;
    struct _HandleIO *handle = np->io_handle;
    int x; int y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
   player->block_map[y][x] = 0;
            np->old_map[y][x] = 0;
  }
    }
    fill_image(&screen_image,
            0xff,
            handle->left,
            handle->top,
            5,
            20 * 20);
    fill_image(&screen_image,
            0xff,
            handle->left + 12 * 20 + 5,
            handle->top,
            5,
            20 * 20);
    fill_image(&screen_image,
            0x0,
            handle->left + 5,
            handle->top,
            12 * 20,
            20 * 20);
    return 0;
}

user_defined_types: struct _HandleIO { int left; int wid; int event[6]; }


decompiled_code:
__int64 __fastcall native_io_out_grade(__int64 a1)
{
int *v1; // r12
int v2; // ebp
__int64 v3; // rax
unsigned int v4; // ebp
int v5; // edx
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // [rsp-10h] [rbp-28h]

v1 = *(int **)(a1 + 608);
v2 = *v1;
v3 = digit_to_image(*(unsigned int *)(a1 + 20));
v4 = v2 + 299;
image_copy(&screen_image, v4, 150LL, v3, 0LL, 0LL, 0LL);
v5 = *(_DWORD *)(a1 + 20);
result = (unsigned int)(-858993459 * v5 + 429496729);
if ( (unsigned int)result <= 0x33333332 )
{
v7 = digit_to_image((unsigned int)(v5 / 5 + 1));
image_copy(&screen_image, v4, (unsigned int)(v1[1] + 200), v7, 0LL, 0LL, 0LL);
result = v8;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int *v1; // r12
int v2; // ebp
__int64 v3; // rax
unsigned int v4; // ebp
int v5; // edx
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // [rsp-10h] [rbp-28h]

v1 = *(int **)(a1 + 608);
v2 = *v1;
v3 = digit_to_image(*(unsigned int *)(a1 + 20));
v4 = v2 + 299;
image_copy(&screen_image, v4, 150LL, v3, 0LL, 0LL, 0LL);
v5 = *(_DWORD *)(a1 + 20);
result = (unsigned int)(-858993459 * v5 + 429496729);
if ( (unsigned int)result <= 0x33333332 )
{
v7 = digit_to_image((unsigned int)(v5 / 5 + 1));
image_copy(&screen_image, v4, (unsigned int)(v1[1] + 200), v7, 0LL, 0LL, 0LL);
result = v8;
}
return result;
}

original_code:
static void native_io_out_grade(Player * player)
{
    PixImage * image;
    NatPlayer * np = (NatPlayer *)player;
    HandleIO * handle = np->io_handle;
    int left = handle->left;
    image = digit_to_image(player->grade);
    image_copy(&screen_image, left + 12 * 20 + (13*3 + 5*2) + 10, 150, image, 0, 0, 0, 0);
    if (!(player->grade % 5)) {
        image = digit_to_image(player->grade / 5 + 1);
        image_copy(&screen_image,
                left + 12 * 20 + (13*3 + 5*2) + 10,
                handle->top + 200, image, 0, 0, 0, 0);
    }
}

canonical_original_code:
static void native_io_out_grade(struct _Player *player)
{
    struct PixImage *image;
    struct NatPlayer *np = (NatPlayer *)player;
    struct _HandleIO *handle = np->io_handle;
    int left = handle->left;
    image = digit_to_image(player->grade);
    image_copy(&screen_image, left + 12 * 20 + (13*3 + 5*2) + 10, 150, image, 0, 0, 0, 0);
    if (!(player->grade % 5)) {
        image = digit_to_image(player->grade / 5 + 1);
        image_copy(&screen_image,
                left + 12 * 20 + (13*3 + 5*2) + 10,
                handle->top + 200, image, 0, 0, 0, 0);
    }
}

variable_types: struct PixImage { unsigned char *pixels; int pixelbytes; int rowbytes; int w; } *


original_code:
static int native_recv_player_data(RussiaBlock * rb)
{
    int key;
    while ((key = x_key_event()) >= 0) {
        int p;
        for (p = 0; p < 2; p++) {
            int i;
            HandleIO * handle;
            if (!rb->match[p])
                continue;
            handle = ((NatPlayer *)rb->match[p])->io_handle;
            for (i = 0; i < 6; i++)
                if (rb->match[p] && key == handle->event[i])
                    write_data_to_buf(rb->match[p]->event_buf, (char *)&i, sizeof(i));
        }
    }
    return 0;
}

canonical_original_code:
static int native_recv_player_data(struct _RussiaBlock *rb)
{
    int key;
    while ((key = x_key_event()) >= 0) {
        int p;
        for (p = 0; p < 2; p++) {
            int i;
            struct _HandleIO *handle;
            if (!rb->match[p])
                continue;
            handle = ((struct NatPlayer *)rb->match[p])->io_handle;
            for (i = 0; i < 6; i++)
                if (rb->match[p] && key == handle->event[i])
                    write_data_to_buf(rb->match[p]->event_buf, (char *)&i, sizeof(i));
        }
    }
    return 0;
}

variable_types: key int

variable_types: p int

variable_types: i int


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

if ( (int)x_init_display(720LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v0 = init_digit();
result = 1LL;
if ( v0 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
return result;
}

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

if ( (int)x_init_display(720LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v0 = init_digit();
result = 1LL;
if ( v0 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
return result;
}

original_code:
static int native_init(RussiaBlock * rb, void * data)
{
    if (x_init_display(((12 * 20 + 6 * 20) * 2), (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
}

canonical_original_code:
static int native_init(struct _RussiaBlock *rb, void *data)
{
    if (x_init_display(((12 * 20 + 6 * 20) * 2), (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
}

variable_types: struct _RussiaBlock { int type; int status; struct _Player { int type; int status; int x; int direct; int grade; int level; int solidify; struct Shape { unsigned char matrix[4][4]; int w; } *cur_shape; struct Shape *next_shape; int time; int speed; int slice; struct Block { int x; } blocks[4]; unsigned char next_matrix[4][4]; unsigned char block_map[20][12]; struct _DataBuffer { int buf_size; int start; int len; char *buf; } *event_buf; } *match[2]; struct _DataBuffer *cmd_buf; struct _InOut { int (*init)(struct _Player *player); void (*out_grade)(struct _Player *player); void (*out_next_block)(struct _Player *player); void (*out_win_map)(struct _Player *player, unsigned char map[20][12]); void (*refresh_screen)(int time); } *io_ops; struct _RBOPS { int (*init)(struct _RussiaBlock *rb, void *data); int (*native_init)(struct _RussiaBlock *rb); struct _Player *(*add_player)(struct _RussiaBlock *rb); void (*add_match)(struct _RussiaBlock *rb); void (*del_match)(struct _RussiaBlock *rb, struct _Player *player); int (*recv_player_data)(struct _RussiaBlock *rb); void (*send_player_data)(struct _RussiaBlock *rb); int (*exec_misc_cmd)(struct _RussiaBlock *rb); } *rb_ops; void *type_data; } *

variable_types:
data
void *


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

if ( (int)x_init_display(360LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v0 = init_digit();
result = 1LL;
if ( v0 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
return result;
}

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

if ( (int)x_init_display(360LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v0 = init_digit();
result = 1LL;
if ( v0 < 0 )
{
fwrite("Cannt init digit.\n", 1uLL, 0x12uLL, stderr);
result = 0LL;
}
}
return result;
}

original_code:
static int native_single_init(RussiaBlock * rb, void * data)
{
    if (x_init_display(((12 * 20 + 6 * 20) * 2) / 2, (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
}

canonical_original_code:
static int native_single_init(struct _RussiaBlock *rb, void *data)
{
    if (x_init_display(((12 * 20 + 6 * 20) * 2) / 2, (20 * 20)) < 0) {
        fprintf(stderr, "init display fail.\n");
        return 0;
    }
    if (init_digit() < 0) {
        fprintf(stderr, "Cannt init digit.\n");
        return 0;
    }
    return 1;
}


decompiled_code:
__int64 get_msec()
{
int v0; // ebx
int v1; // edx
__int64 result; // rax
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
v0 = secbase_2;
if ( secbase_2 )
{
v1 = LODWORD(tv.tv_usec) - oldusec_0;
if ( oldsec_1 < SLODWORD(tv.tv_sec) )
v1 += 1000000;
if ( v1 <= 999 )
{
usleep(1000 - v1);
v0 = secbase_2;
}
gettimeofday(&tv, &tz);
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = 1000 * (LODWORD(tv.tv_sec) - v0) + (unsigned int)(tv.tv_usec / 1000);
}
else
{
secbase_2 = tv.tv_sec;
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = (unsigned int)(SLODWORD(tv.tv_usec) / 1000);
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // ebx
int v1; // edx
__int64 result; // rax
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
v0 = secbase_2;
if ( secbase_2 )
{
v1 = LODWORD(tv.tv_usec) - oldusec_0;
if ( oldsec_1 < SLODWORD(tv.tv_sec) )
v1 += 1000000;
if ( v1 <= 999 )
{
usleep(1000 - v1);
v0 = secbase_2;
}
gettimeofday(&tv, &tz);
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = 1000 * (LODWORD(tv.tv_sec) - v0) + (unsigned int)(tv.tv_usec / 1000);
}
else
{
secbase_2 = tv.tv_sec;
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = (unsigned int)(SLODWORD(tv.tv_usec) / 1000);
}
return result;
}

original_code:
int get_msec(void)
{
    struct timeval tp;
    struct timezone tzp;
    static Mtime secbase = 0, oldsec, oldusec;
    Mtime newsec, newusec, udiff;
    gettimeofday(&tp, &tzp);
    newsec = tp.tv_sec;
    newusec = tp.tv_usec;
    if (!secbase) {
        oldsec = newsec = secbase = tp.tv_sec;
        oldusec = newusec = tp.tv_usec;
        return tp.tv_usec / 1000;
    }
    if (newsec > oldsec)
        udiff = 1000000 - oldusec + newusec;
    else
        udiff = newusec - oldusec;
    if (udiff < 1000) {
        usleep(1000 - udiff);
    }
    gettimeofday(&tp, &tzp);
    oldsec = tp.tv_sec;
    oldusec = tp.tv_usec;
    return ((tp.tv_sec - secbase) * 1000 + tp.tv_usec / 1000);
}

canonical_original_code:
int get_msec()
{
    struct timeval tp;
    struct timezone tzp;
    static int secbase = 0; static int oldsec; static int oldusec;
    int newsec; int newusec; int udiff;
    gettimeofday(&tp, &tzp);
    newsec = tp.tv_sec;
    newusec = tp.tv_usec;
    if (!secbase) {
        oldsec = newsec = secbase = tp.tv_sec;
        oldusec = newusec = tp.tv_usec;
        return tp.tv_usec / 1000;
    }
    if (newsec > oldsec)
        udiff = 1000000 - oldusec + newusec;
    else
        udiff = newusec - oldusec;
    if (udiff < 1000) {
        usleep(1000 - udiff);
    }
    gettimeofday(&tp, &tzp);
    oldsec = tp.tv_sec;
    oldusec = tp.tv_usec;
    return ((tp.tv_sec - secbase) * 1000 + tp.tv_usec / 1000);
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

variable_types: secbase int

variable_types: oldsec int

variable_types: oldusec int

variable_types: newsec int

variable_types: newusec int

variable_types: udiff int


decompiled_code:
__int64 rand_id()
{
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
srand(tv.tv_usec);
return (unsigned int)(rand() % 7);
}

canonical_decompiled_code:
__int64 func1()
{
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
srand(tv.tv_usec);
return (unsigned int)(rand() % 7);
}

original_code:
int rand_id(void)
{
    struct timeval tp;
    struct timezone tzp;
    gettimeofday(&tp, &tzp);
    srand(tp.tv_usec);
    return rand() % 7;
}

canonical_original_code:
int rand_id()
{
    struct timeval tp;
    struct timezone tzp;
    gettimeofday(&tp, &tzp);
    srand(tp.tv_usec);
    return rand() % 7;
}

user_defined_types: struct timeval { long tv_sec; long tv_usec; }

user_defined_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
__int64 __fastcall paint_pos_digit(int a1, int a2)
{
__int64 v2; // rdx
char *v3; // rcx
char *v4; // rsi
char v5; // dl
__int64 i; // rax
__int128 v8; // [rsp+0h] [rbp-148h] BYREF
char v9[280]; // [rsp+10h] [rbp-138h] BYREF
__int16 v10; // [rsp+128h] [rbp-20h]
char v11; // [rsp+12Ah] [rbp-1Eh]

memset(v9, 0, sizeof(v9));
v10 = 0;
v11 = 0;
fill_image(&digit_image, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
v2 = 7LL * a2;
v3 = (char *)&digit_mask + v2;
v4 = (char *)&off_2A7 + v2;
do
{
v5 = *v3;
if ( *v3 <= 0 )
break;
for ( i = 0LL; i != 299; ++i )
{
if ( digit_map[i] == v5 )
*((_BYTE *)&v8 + i) = 1;
}
++v3;
}
while ( v3 != v4 );
digit_map_to_image_constprop_0((__int64)&v8);
return image_copy(&whole_digit_image, (unsigned int)(-18 * a1 + 36), 0LL, &digit_image);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // rdx
char *v3; // rcx
char *v4; // rsi
char v5; // dl
__int64 i; // rax
__int128 v8; // [rsp+0h] [rbp-148h] BYREF
char v9[280]; // [rsp+10h] [rbp-138h] BYREF
__int16 v10; // [rsp+128h] [rbp-20h]
char v11; // [rsp+12Ah] [rbp-1Eh]

memset(v9, 0, sizeof(v9));
v10 = 0;
v11 = 0;
fill_image(&digit_image, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
v2 = 7LL * a2;
v3 = (char *)&digit_mask + v2;
v4 = (char *)&off_2A7 + v2;
do
{
v5 = *v3;
if ( *v3 <= 0 )
break;
for ( i = 0LL; i != 299; ++i )
{
if ( digit_map[i] == v5 )
*((_BYTE *)&v8 + i) = 1;
}
++v3;
}
while ( v3 != v4 );
func0((__int64)&v8);
return image_copy(&whole_digit_image, (unsigned int)(-18 * a1 + 36), 0LL, &digit_image);
}

original_code:
static void paint_pos_digit(int pos, int digit)
{
    int x, i, j;
    char map[(13 * 23)] = {0};
    char * mask = digit_mask[digit];
    fill_image(&digit_image, 0, 0, 0, 0, 0);
    for (i = 0; i < 7 && mask[i] > 0; i++) {
        for (j = 0; j < (13 * 23); j++) {
            if (digit_map[j] == mask[i])
                map[j] = 1;
        }
    }
    digit_map_to_image(&digit_image, map, 0xff00ff);
    x = (13*3 + 5*2) - 13 - (5 +13) * pos;
    image_copy(&whole_digit_image, x, 0, &digit_image, 0, 0, 0, 0);
}

canonical_original_code:
static void paint_pos_digit(int pos, int digit)
{
    int x; int i; int j;
    char map[-1] = {0};
    char *mask = digit_mask[digit];
    fill_image(&digit_image, 0, 0, 0, 0, 0);
    for (i = 0; i < 7 && mask[i] > 0; i++) {
        for (j = 0; j < (13 * 23); j++) {
            if (digit_map[j] == mask[i])
                map[j] = 1;
        }
    }
    digit_map_to_image(&digit_image, map, 0xff00ff);
    x = (13*3 + 5*2) - 13 - (5 +13) * pos;
    image_copy(&whole_digit_image, x, 0, &digit_image, 0, 0, 0, 0);
}

variable_types: pos int

variable_types: digit int

variable_types: x int

variable_types: j int

variable_types:
map
char [-1]

variable_types:
mask
char *


original_code:
int init_digit(void)
{
    int i;
    for (i = 0; i < (13 * 23); i++) {
        if (digit_str[i] == ' ')
            digit_map[i] = 0;
        else
            digit_map[i] = digit_str[i] - '0';
    }
    digit_image.w = 13;
    digit_image.h = 23;
    digit_image.pixelbytes = screen_image.pixelbytes;
    digit_image.rowbytes = 13 * screen_image.pixelbytes;
    digit_image.pixels = (pixel_t *)malloc(23 * digit_image.rowbytes);
    whole_digit_image.w = (13*3 + 5*2);
    whole_digit_image.h = 23;
    whole_digit_image.pixelbytes = screen_image.pixelbytes;
    whole_digit_image.rowbytes = (13*3 + 5*2) * screen_image.pixelbytes;
    whole_digit_image.pixels = (pixel_t *)malloc(23 * whole_digit_image.rowbytes);
    return 0;
}

canonical_original_code:
int init_digit()
{
    int i;
    for (i = 0; i < (13 * 23); i++) {
        if (digit_str[i] == ' ')
            digit_map[i] = 0;
        else
            digit_map[i] = digit_str[i] - '0';
    }
    digit_image.w = 13;
    digit_image.h = 23;
    digit_image.pixelbytes = screen_image.pixelbytes;
    digit_image.rowbytes = 13 * screen_image.pixelbytes;
    digit_image.pixels = (unsigned char *)malloc(23 * digit_image.rowbytes);
    whole_digit_image.w = (13*3 + 5*2);
    whole_digit_image.h = 23;
    whole_digit_image.pixelbytes = screen_image.pixelbytes;
    whole_digit_image.rowbytes = (13*3 + 5*2) * screen_image.pixelbytes;
    whole_digit_image.pixels = (unsigned char *)malloc(23 * whole_digit_image.rowbytes);
    return 0;
}

variable_types: i int


repo: krosswindz/tmst

decompiled_code:
__int64 set_defaults()
{
unsigned int v0; // er12
_QWORD *v2; // rax
void *v3; // rax
_QWORD *v4; // rax
void *v5; // rax

if ( !ptr )
{
v2 = calloc(8uLL, 1uLL);
ptr = v2;
if ( !v2 )
goto LABEL_14;
*v2 = 0x302E302E302E30LL;
}
if ( !port )
{
v3 = calloc(6uLL, 1uLL);
port = v3;
if ( !v3 )
goto LABEL_14;
strcpy((char *)v3, "30404");
}
if ( !levels )
{
v4 = calloc(8uLL, 1uLL);
levels = (__int64)v4;
if ( !v4 )
{
v0 = -1;
fwrite("ERROR: out-of-memroy.\n", 1uLL, 0x16uLL, log_fp);
return v0;
}
*v4 = 0x5252455F474F4CLL;
}
v0 = 0;
if ( !host )
{
v5 = calloc(0xAuLL, 1uLL);
host = v5;
if ( v5 )
{
strcpy((char *)v5, "localhost");
return v0;
}
LABEL_14:
v0 = -1;
fwrite("ERROR: out-of-memory.\n", 1uLL, 0x16uLL, log_fp);
}
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
_QWORD *v2; // rax
void *v3; // rax
_QWORD *v4; // rax
void *v5; // rax

if ( !ptr )
{
v2 = calloc(8uLL, 1uLL);
ptr = v2;
if ( !v2 )
goto LABEL_14;
*v2 = 0x302E302E302E30LL;
}
if ( !port )
{
v3 = calloc(6uLL, 1uLL);
port = v3;
if ( !v3 )
goto LABEL_14;
strcpy((char *)v3, "30404");
}
if ( !levels )
{
v4 = calloc(8uLL, 1uLL);
levels = (__int64)v4;
if ( !v4 )
{
v0 = -1;
fwrite("ERROR: out-of-memroy.\n", 1uLL, 0x16uLL, log_fp);
return v0;
}
*v4 = 0x5252455F474F4CLL;
}
v0 = 0;
if ( !host )
{
v5 = calloc(0xAuLL, 1uLL);
host = v5;
if ( v5 )
{
strcpy((char *)v5, "localhost");
return v0;
}
LABEL_14:
v0 = -1;
fwrite("ERROR: out-of-memory.\n", 1uLL, 0x16uLL, log_fp);
}
return v0;
}

original_code:
static inline int
set_defaults (void)
{
 if (ip == ((void *)0)) {
  ip = (char *) malloc (sizeof (char) * 8);
  if (ip == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (ip, 0, sizeof (char) * 8);
  sprintf (ip, "0.0.0.0");
 }
 if (port == ((void *)0)) {
  port = (char *) malloc (sizeof (char) * 6);
  if (port == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (port, 0, sizeof (char) * 6);
  sprintf (port, "30404");
 }
 if (levels == ((void *)0)) {
  levels = (char *) malloc (sizeof (char) * 8);
  if (levels == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memroy.\n");
   return -1;
  }
  memset (levels, 0, sizeof (char) * 8);
  sprintf (levels, "LOG_ERR");
 }
 if (host == ((void *)0)) {
  host = (char *) malloc (sizeof (char) * 10);
  if (host == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (host, 0, sizeof (char) * 10);
  sprintf (host, "localhost");
 }
 return 0;
}

canonical_original_code:
static inline int set_defaults()
{
 if (ip == ((void *)0)) {
  ip = (char *) malloc (sizeof (char) * 8);
  if (ip == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (ip, 0, sizeof (char) * 8);
  sprintf (ip, "0.0.0.0");
 }
 if (port == ((void *)0)) {
  port = (char *) malloc (sizeof (char) * 6);
  if (port == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (port, 0, sizeof (char) * 6);
  sprintf (port, "30404");
 }
 if (levels == ((void *)0)) {
  levels = (char *) malloc (sizeof (char) * 8);
  if (levels == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memroy.\n");
   return -1;
  }
  memset (levels, 0, sizeof (char) * 8);
  sprintf (levels, "LOG_ERR");
 }
 if (host == ((void *)0)) {
  host = (char *) malloc (sizeof (char) * 10);
  if (host == ((void *)0)) {
   fprintf (log_fp, "ERROR: out-of-memory.\n");
   return -1;
  }
  memset (host, 0, sizeof (char) * 10);
  sprintf (host, "localhost");
 }
 return 0;
}


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

if ( name )
{
if ( user )
{
if ( passwd )
{
result = 0LL;
}
else
{
fwrite("ERROR: missing database password in config file.\n", 1uLL, 0x31uLL, log_fp);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: missing database user name in config file.\n", 1uLL, 0x32uLL, log_fp);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: missing database name in config file.\n", 1uLL, 0x2DuLL, log_fp);
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( name )
{
if ( user )
{
if ( passwd )
{
result = 0LL;
}
else
{
fwrite("ERROR: missing database password in config file.\n", 1uLL, 0x31uLL, log_fp);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: missing database user name in config file.\n", 1uLL, 0x32uLL, log_fp);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: missing database name in config file.\n", 1uLL, 0x2DuLL, log_fp);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static inline int
check_valid_config (void)
{
 if (name == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database name in config "
    "file.\n");
  return -1;
 }
 if (user == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database user name in config "
    "file.\n");
  return -1;
 }
 if (passwd == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database password in config "
    "file.\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
static inline int check_valid_config()
{
 if (name == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database name in config "
    "file.\n");
  return -1;
 }
 if (user == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database user name in config "
    "file.\n");
  return -1;
 }
 if (passwd == ((void *)0)) {
  fprintf (log_fp, "ERROR: missing database password in config "
    "file.\n");
  return -1;
 }
 return 0;
}


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
printf("usage: %s [options]\n", a1);
puts("optins:\n");
puts("\t-c, --conffile=<config file>\n\t\tUse <config file> as the configuration file.");
puts("\t-f, --foreground\n\t\tRun in foreground, all output is sent to stdout.");
puts("\t-h, --help\n\t\tPrint this help message and then exit.");
puts("\t-l, --logfile=<log file>\n\t\tUse <log file> for logging.");
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const char *a1)
{
printf("usage: %s [options]\n", a1);
puts("optins:\n");
puts("\t-c, --conffile=<config file>\n\t\tUse <config file> as the configuration file.");
puts("\t-f, --foreground\n\t\tRun in foreground, all output is sent to stdout.");
puts("\t-h, --help\n\t\tPrint this help message and then exit.");
puts("\t-l, --logfile=<log file>\n\t\tUse <log file> for logging.");
exit(0);
}

original_code:
static void
usage (char *name)
{
 printf ("usage: %s [options]\n", name);
 printf ("optins:\n\n");
 printf ("\t-c, --conffile=<config file>\n\t\tUse <config file> as the "
   "configuration file.\n");
 printf ("\t-f, --foreground\n\t\tRun in foreground, all output is sent"
   " to stdout.\n");
 printf ("\t-h, --help\n\t\tPrint this help message and then exit.\n");
 printf ("\t-l, --logfile=<log file>\n\t\tUse <log file> for logging."
   "\n");
 exit (0);
}

canonical_original_code:
static void usage(char *name)
{
 printf ("usage: %s [options]\n", name);
 printf ("optins:\n\n");
 printf ("\t-c, --conffile=<config file>\n\t\tUse <config file> as the "
   "configuration file.\n");
 printf ("\t-f, --foreground\n\t\tRun in foreground, all output is sent"
   " to stdout.\n");
 printf ("\t-h, --help\n\t\tPrint this help message and then exit.\n");
 printf ("\t-l, --logfile=<log file>\n\t\tUse <log file> for logging."
   "\n");
 exit (0);
}

variable_types:
name
char *


decompiled_code:
char *__fastcall strip_whitespace(char *s)
{
int v2; // eax
__int64 v3; // rbx
int v4; // eax
int v5; // edi
char *v6; // rbp
char *v7; // rbx
int v8; // er8
char *v9; // rax

while ( isspace(*s) )
++s;
v2 = strlen(s) - 1;
v3 = v2;
if ( v2 >= 0 )
{
while ( isspace(s[v3]) )
{
v4 = --v3;
if ( (_DWORD)v3 == -1 )
{
v3 = v4;
break;
}
}
}
s[v3 + 1] = 0;
v5 = *s;
v6 = s;
v7 = s;
if ( *s )
{
do
{
v8 = isspace(v5);
v9 = v6 - 1;
if ( !v8 )
v9 = v6++;
v5 = *++v7;
v9[1] = v5;
}
while ( (_BYTE)v5 );
}
return s;
}

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
int v2; // eax
__int64 v3; // rbx
int v4; // eax
int v5; // edi
char *v6; // rbp
char *v7; // rbx
int v8; // er8
char *v9; // rax

while ( isspace(*s) )
++s;
v2 = strlen(s) - 1;
v3 = v2;
if ( v2 >= 0 )
{
while ( isspace(s[v3]) )
{
v4 = --v3;
if ( (_DWORD)v3 == -1 )
{
v3 = v4;
break;
}
}
}
s[v3 + 1] = 0;
v5 = *s;
v6 = s;
v7 = s;
if ( *s )
{
do
{
v8 = isspace(v5);
v9 = v6 - 1;
if ( !v8 )
v9 = v6++;
v5 = *++v7;
v9[1] = v5;
}
while ( (_BYTE)v5 );
}
return s;
}

original_code:
static inline char *
strip_whitespace (char *str)
{
 char *c1, *c2;
 int i;
 while (isspace (*str))
  str++;
 for (i = strlen (str) - 1; i >= 0; i--)
  if (!isspace (str[i]))
   break;
 str[i + 1] = '\0';
 c1 = str;
 c2 = str;
 while ((*c2 = *c1) != '\0') {
  if (isspace (*c1))
   c2--;
  c1++;
  c2++;
 }
 return str;
}

canonical_original_code:
static inline char *strip_whitespace(char *str)
{
 char *c1; char *c2;
 int i;
 while (isspace (*str))
  str++;
 for (i = strlen (str) - 1; i >= 0; i--)
  if (!isspace (str[i]))
   break;
 str[i + 1] = '\0';
 c1 = str;
 c2 = str;
 while ((*c2 = *c1) != '\0') {
  if (isspace (*c1))
   c2--;
  c1++;
  c2++;
 }
 return str;
}

variable_types:
str
char *

return_type: char *


decompiled_code:
__int64 __fastcall logger_init(const char *a1)
{
char *v1; // rax
char *v2; // rbp
char *v3; // rax
char *stringp; // [rsp+8h] [rbp-10h] BYREF

v1 = strdup(a1);
log_level = 0;
stringp = v1;
v2 = v1;
while ( 1 )
{
v3 = strsep(&stringp, "|");
if ( !v3 )
break;
if ( !strcmp("LOG_ERR", v3) )
log_level |= 1u;
if ( !strcmp("LOG_DBG", v3) )
log_level |= 2u;
if ( !strcmp("LOG_INFO", v3) )
log_level |= 4u;
if ( !strcmp("LOG_WARN", v3) )
log_level |= 8u;
}
if ( v2 )
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
char *v1; // rax
char *v2; // rbp
char *v3; // rax
char *stringp; // [rsp+8h] [rbp-10h] BYREF

v1 = strdup(a1);
log_level = 0;
stringp = v1;
v2 = v1;
while ( 1 )
{
v3 = strsep(&stringp, "|");
if ( !v3 )
break;
if ( !strcmp("LOG_ERR", v3) )
log_level |= 1u;
if ( !strcmp("LOG_DBG", v3) )
log_level |= 2u;
if ( !strcmp("LOG_INFO", v3) )
log_level |= 4u;
if ( !strcmp("LOG_WARN", v3) )
log_level |= 8u;
}
if ( v2 )
free(v2);
return 0LL;
}

original_code:
static inline int
logger_init (char *levels)
{
 char *level, *ptr, *token;
 ptr = strdup (levels);
 token = ptr;
 log_level = 0;
 while (1) {
  level = strsep (&token, "|");
  if (level == ((void *)0))
   break;
  if (strcmp ("LOG_ERR", level) == 0)
   log_level |= LOG_ERR;
  if (strcmp ("LOG_DBG", level) == 0)
   log_level |= LOG_DBG;
  if (strcmp ("LOG_INFO", level) == 0)
   log_level |= LOG_INFO;
  if (strcmp ("LOG_WARN", level) == 0)
   log_level |= LOG_WARN;
 }
 if (ptr != ((void *)0))
  free (ptr);
 return 0;
}

canonical_original_code:
static inline int logger_init(char *levels)
{
 char *level; char *ptr; char *token;
 ptr = strdup (levels);
 token = ptr;
 log_level = 0;
 while (1) {
  level = strsep (&token, "|");
  if (level == ((void *)0))
   break;
  if (strcmp ("LOG_ERR", level) == 0)
   log_level |= LOG_ERR;
  if (strcmp ("LOG_DBG", level) == 0)
   log_level |= LOG_DBG;
  if (strcmp ("LOG_INFO", level) == 0)
   log_level |= LOG_INFO;
  if (strcmp ("LOG_WARN", level) == 0)
   log_level |= LOG_WARN;
 }
 if (ptr != ((void *)0))
  free (ptr);
 return 0;
}

variable_types:
levels
char *

variable_types:
level
char *

variable_types:
ptr
char *

variable_types:
token
char *


logger

decompiled_code:
int logger(unsigned __int8 a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( log_fp )
{
if ( (a1 & (unsigned __int8)log_level) != 0 )
{
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
result = fflush(log_fp);
}
}
return result;
}

canonical_decompiled_code:
int func6(unsigned __int8 a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( log_fp )
{
if ( (a1 & (unsigned __int8)log_level) != 0 )
{
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
result = fflush(log_fp);
}
}
return result;
}

original_code:
static inline void
logger (uint8_t level, char *args, ...)
{
 va_list ap;
 if (log_fp == ((void *)0))
  return;
 if ((level & log_level) == 0)
  return;
 __builtin_va_start(ap,args);
 vfprintf (log_fp, args, ap);
 __builtin_va_end(ap);
 fflush (log_fp);
 return;
}

canonical_original_code:
static inline void logger(unsigned char level, char *args, ...)
{
 __builtin_va_list ap;
 if (log_fp == ((void *)0))
  return;
 if ((level & log_level) == 0)
  return;
 __builtin_va_start(ap,args);
 vfprintf (log_fp, args, ap);
 __builtin_va_end(ap);
 fflush (log_fp);
 return;
}

variable_types:
level
unsigned char

variable_types:
args
char *


decompiled_code:
void __fastcall signal_handler(int a1)
{
char *v1; // rax
char *v2; // rax
char *v3; // rax

if ( a1 != 3 )
{
if ( a1 > 3 )
{
if ( a1 == 15 )
goto LABEL_5;
LABEL_7:
v2 = strsignal(a1);
logger(1u, "ERROR: received %s, unhandled.\n", v2);
return;
}
if ( a1 != 1 )
{
if ( a1 == 2 )
{
LABEL_5:
v1 = strsignal(a1);
logger(2u, "INFO: received %s, terminating.\n", v1);
terminate = 1;
return;
}
goto LABEL_7;
}
}
v3 = strsignal(a1);
logger(2u, "INFO: received %s, ignoring.\n", v3);
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
char *v1; // rax
char *v2; // rax
char *v3; // rax

if ( a1 != 3 )
{
if ( a1 > 3 )
{
if ( a1 == 15 )
goto LABEL_5;
LABEL_7:
v2 = strsignal(a1);
func6(1u, "ERROR: received %s, unhandled.\n", v2);
return;
}
if ( a1 != 1 )
{
if ( a1 == 2 )
{
LABEL_5:
v1 = strsignal(a1);
func6(2u, "INFO: received %s, terminating.\n", v1);
terminate = 1;
return;
}
goto LABEL_7;
}
}
v3 = strsignal(a1);
func6(2u, "INFO: received %s, ignoring.\n", v3);
}

original_code:
static void
signal_handler (int signo)
{
 switch (signo) {
  case 1:
  case 3:
   logger (LOG_DBG, "INFO: received %s, ignoring.\n",
     strsignal (signo));
   break;
  case 2:
  case 15:
   logger (LOG_DBG, "INFO: received %s, terminating.\n",
     strsignal (signo));
   terminate = 1;
   break;
  default:
   logger (LOG_ERR, "ERROR: received %s, unhandled.\n",
     strsignal (signo));
 }
 return;
}

canonical_original_code:
static void signal_handler(int signo)
{
 switch (signo) {
  case 1:
  case 3:
   logger (LOG_DBG, "INFO: received %s, ignoring.\n",
     strsignal (signo));
   break;
  case 2:
  case 15:
   logger (LOG_DBG, "INFO: received %s, terminating.\n",
     strsignal (signo));
   terminate = 1;
   break;
  default:
   logger (LOG_ERR, "ERROR: received %s, unhandled.\n",
     strsignal (signo));
 }
 return;
}

variable_types: signo int