decompiled_code:
__int64 __fastcall client_exec_misc_cmd(_QWORD *a1)
{
__int64 v1; // rbx
__int64 v2; // r12
__int64 v3; // r13
__int64 result; // rax
int v5; // [rsp+8h] [rbp-60h] BYREF
int v6; // [rsp+Ch] [rbp-5Ch] BYREF
int v7[22]; // [rsp+10h] [rbp-58h] BYREF

v1 = a1[6];
v2 = a1[4];
while ( 1 )
{
result = get_msg_from_buf(*(_QWORD *)(v1 + 8), &v6, (__int64)&v5, (__int64)v7);
if ( !(_DWORD)result )
return result;
if ( !v5 )
{
v3 = a1[1];
goto LABEL_9;
}
if ( v5 == 1 )
{
v3 = a1[2];
LABEL_9:
if ( v3 )
{
switch ( v6 )
{
case 4:
bits_to_win_map((char *)v7, v3 + 116);
(*(void (__fastcall **)(__int64, __int64))(v2 + 24))(v3, v3 + 116);
break;
case 2:
*(_DWORD *)(v3 + 20) = v7[0];
(*(void (__fastcall **)(__int64))(v2 + 8))(v3);
break;
case 3:
bits_to_matrix(v7, v3 + 100);
(*(void (__fastcall **)(__int64))(v2 + 16))(v3);
break;
case 6:
*(_DWORD *)(v1 + 4) &= 0xFFFFFFFC;
break;
case 7:
*(_DWORD *)(v1 + 4) &= 0xFFFFFFFD;
break;
case 5:
*(_DWORD *)(v1 + 4) |= 1u;
break;
}
}
}
}
}

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


repo: kroody/tetris-free

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]

variable_types: len int

return_type: void

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 _DataBuffer { int buf_size; int start; int len; char *buf; }

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


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

v1 = *(_QWORD *)(a1 + 48);
recv_server_data(a1);
if ( (*(_BYTE *)(v1 + 4) & 1) != 0 )
recv_key_event(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;
}

return_type: int


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

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

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebx
_QWORD *v4; // rbp
int v5; // edx
__int64 result; // rax
struct sockaddr v7; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&v7.sa_family = 0LL;
*(_QWORD *)&v7.sa_data[6] = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("client");
result = 0LL;
}
else
{
v3 = v2;
v7.sa_family = 2;
*(_WORD *)v7.sa_data = func10(9877);
*(_DWORD *)&v7.sa_data[2] = inet_addr(a2);
if ( connect(v3, &v7, 0x10u) < 0 )
{
perror("client");
close(v3);
result = 0LL;
}
else
{
fcntl(v3, 4, 2048LL);
v4 = malloc(0x20uLL);
*(_DWORD *)v4 = v3;
v4[1] = malloc_data_buf(512LL);
v4[2] = malloc_data_buf(100LL);
v4[3] = &key_code;
*(_QWORD *)(a1 + 48) = v4;
if ( (int)x_init_display(720LL, 400LL) < 0 )
{
fwrite("init display fail.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
v5 = init_digit(720LL, 400LL);
result = 1LL;
if ( v5 < 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:
Window __fastcall create_x_window(__int64 a1, Window a2)
{
Colormap v2; // r12
Window v3; // rbp
XSetWindowAttributes v5; // [rsp+0h] [rbp-88h] BYREF

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

canonical_decompiled_code:
Window __fastcall func0(__int64 a1, Window a2)
{
Colormap v2; // r12
Window v3; // rbp
XSetWindowAttributes v5; // [rsp+0h] [rbp-88h] BYREF

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

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

return_type: 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 *v2; // rax
int v3; // eax
Visual *v4; // rax
XVisualInfo *v5; // rax
Window v6; // rax
__int64 result; // rax
char *v8; // rax
XGCValues v9; // [rsp+0h] [rbp-E8h] BYREF
XVisualInfo v10; // [rsp+80h] [rbp-68h] BYREF
int v11[7]; // [rsp+CCh] [rbp-1Ch] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
Display *v2; // rax
int v3; // eax
Visual *v4; // rax
XVisualInfo *v5; // rax
Window v6; // rax
__int64 result; // rax
char *v8; // rax
XGCValues v9; // [rsp+0h] [rbp-E8h] BYREF
XVisualInfo v10; // [rsp+80h] [rbp-68h] BYREF
int v11[7]; // [rsp+CCh] [rbp-1Ch] BYREF

v2 = XOpenDisplay(0LL);
dpy = v2;
if ( v2 )
{
XSynchronize(v2, 1);
v3 = XDefaultScreen(dpy);
v4 = XDefaultVisual(dpy, v3);
v10.visualid = XVisualIDFromVisual(v4);
v5 = XGetVisualInfo(dpy, 1LL, &v10, v11);
visual_info = (__int64)v5;
if ( v11[0] )
{
v6 = XRootWindow(dpy, v5->screen);
*((_DWORD *)&screen_image + 4) = a1;
*((_DWORD *)&screen_image + 5) = a2;
x_win = func0(visual_info, v6);
v9.graphics_exposures = 0;
x_gc = XCreateGC(dpy, x_win, 0x10000uLL, &v9);
XMapWindow(dpy, x_win);
XMoveWindow(dpy, x_win, 0, 0);
x_fb = reset_frame_buf(visual_info);
result = 0LL;
}
else
{
printf("Bad visual id %d\n", LODWORD(v10.visualid));
result = 0xFFFFFFFFLL;
}
}
else
{
v8 = getenv("DISPLAY");
printf("Could not open display [%s]\n", v8);
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, __int64 a5, __int64 a6, int a7, unsigned int a8)
{
int v10; // edx
__int64 result; // rax
int v12; // esi
int v13; // er10
int v14; // esi
int v15; // er10

v10 = a7;
result = a8;
if ( !a7 )
v10 = *(_DWORD *)(a4 + 16);
if ( !a8 )
result = *(unsigned int *)(a4 + 20);
v12 = a1[4];
if ( v12 > (int)a5 )
{
v13 = a1[5];
if ( v13 > (int)a6 )
{
if ( v10 + (int)a5 > *(_DWORD *)(a4 + 16) )
v10 = *(_DWORD *)(a4 + 16) - a5;
v14 = v12 - a2;
if ( v14 > v10 )
v14 = v10;
if ( (int)result + (int)a6 > *(_DWORD *)(a4 + 20) )
LODWORD(result) = *(_DWORD *)(a4 + 20) - a6;
v15 = v13 - a3;
if ( v15 > (int)result )
v15 = result;
result = a1[2];
if ( (_DWORD)result == 2 )
{
result = image_copy16((__int64)a1, a2, a3, a4, a5, a6, v14, v15);
}
else if ( (_DWORD)result == 4 )
{
result = image_copy32((__int64)a1, a2, a3, a4, a5, a6, v14, v15);
}
}
}
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 func3(__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

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

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
__int64 result; // rax

result = *a1;
switch ( (_DWORD)result )
{
case 2:
result = ready_event(*((_QWORD *)a1 + 1));
if ( (_DWORD)result )
{
result = init_player(a1, *((_QWORD *)a1 + 1));
a1[1] |= 1u;
}
else
{
a1[1] &= 0xFFFFFFFE;
}
break;
case 4:
if ( *((_QWORD *)a1 + 1) && *((_QWORD *)a1 + 2) )
{
result = func0((__int64)a1);
if ( (_DWORD)result )
{
a1[1] |= 1u;
result = func3((__int64)a1);
}
else
{
a1[1] &= 0xFFFFFFFE;
}
}
else
{
result = (__int64)add_match((__int64)a1);
}
break;
case 1:
result = ready_event(*((_QWORD *)a1 + 1));
if ( (_DWORD)result )
{
init_player(a1, *((_QWORD *)a1 + 1));
result = init_player(a1, *((_QWORD *)a1 + 2));
a1[1] |= 1u;
}
else
{
a1[1] &= 0xFFFFFFFE;
}
break;
}
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 ? 0xFF00FF : 0,
(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 ? 0xFF00FF : 0,
(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
__int64 result; // rax

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

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

for ( i = 0; i != 4; ++i )
{
for ( j = 0LL; j != 4; ++j )
result = func1(j, i, a2, a3, *(_BYTE *)(a1 + j));
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; // rbp
int v2; // ebx
__int64 v3; // rax
unsigned int v4; // ebx
int v5; // edx
__int64 result; // rax
__int64 v7; // rax

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, 0LL);
v5 = *(_DWORD *)(a1 + 20);
result = (unsigned int)(5 * (v5 / 5));
if ( v5 == (_DWORD)result )
{
v7 = digit_to_image((unsigned int)(*(_DWORD *)(a1 + 20) / 5 + 1));
result = image_copy(&screen_image, v4, (unsigned int)(v1[1] + 200), v7, 0LL, 0LL, 0LL, 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int *v1; // rbp
int v2; // ebx
__int64 v3; // rax
unsigned int v4; // ebx
int v5; // edx
__int64 result; // rax
__int64 v7; // rax

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, 0LL);
v5 = *(_DWORD *)(a1 + 20);
result = (unsigned int)(5 * (v5 / 5));
if ( v5 == (_DWORD)result )
{
v7 = digit_to_image((unsigned int)(*(_DWORD *)(a1 + 20) / 5 + 1));
result = image_copy(&screen_image, v4, (unsigned int)(v1[1] + 200), v7, 0LL, 0LL, 0LL, 0LL);
}
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; // edx
__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; // edx
__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; // edx
__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; // edx
__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; // ecx
__int64 result; // rax
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
if ( secbase_2 )
{
if ( oldsec_1 >= SLODWORD(tv.tv_sec) )
v0 = LODWORD(tv.tv_usec) - oldusec_0;
else
v0 = LODWORD(tv.tv_usec) - oldusec_0 + 1000000;
if ( v0 <= 999 )
usleep(1000 - v0);
gettimeofday(&tv, &tz);
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = 1000 * (LODWORD(tv.tv_sec) - secbase_2) + (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; // ecx
__int64 result; // rax
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, &tz);
if ( secbase_2 )
{
if ( oldsec_1 >= SLODWORD(tv.tv_sec) )
v0 = LODWORD(tv.tv_usec) - oldusec_0;
else
v0 = LODWORD(tv.tv_usec) - oldusec_0 + 1000000;
if ( v0 <= 999 )
usleep(1000 - v0);
gettimeofday(&tv, &tz);
oldsec_1 = tv.tv_sec;
oldusec_0 = tv.tv_usec;
result = 1000 * (LODWORD(tv.tv_sec) - secbase_2) + (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
__int64 i; // rax
char v6; // dl
__int64 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
{
v6 = *v3;
if ( *v3 <= 0 )
break;
for ( i = 0LL; i != 299; ++i )
{
if ( digit_map[i] == v6 )
*((_BYTE *)&v8 + i) = 1;
}
++v3;
}
while ( v3 != v4 );
digit_map_to_image(&digit_image, (__int64)&v8, 16711935LL);
return image_copy(&whole_digit_image, (unsigned int)(-18 * a1 + 36), 0LL, &digit_image, 0LL, 0LL, 0LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // rdx
char *v3; // rcx
char *v4; // rsi
__int64 i; // rax
char v6; // dl
__int64 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
{
v6 = *v3;
if ( *v3 <= 0 )
break;
for ( i = 0LL; i != 299; ++i )
{
if ( digit_map[i] == v6 )
*((_BYTE *)&v8 + i) = 1;
}
++v3;
}
while ( v3 != v4 );
func0(&digit_image, (__int64)&v8, 16711935LL);
return image_copy(&whole_digit_image, (unsigned int)(-18 * a1 + 36), 0LL, &digit_image, 0LL, 0LL, 0LL, 0LL);
}

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()
{
__int64 result; // rax
_QWORD *v1; // rax
void *v2; // rax
_QWORD *v3; // rax
void *v4; // rax

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

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
_QWORD *v1; // rax
void *v2; // rax
_QWORD *v3; // rax
void *v4; // rax

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

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; // ebp
char *v3; // r12
char v4; // di
char *v5; // r12
char *v6; // rbp

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

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
int v2; // ebp
char *v3; // r12
char v4; // di
char *v5; // r12
char *v6; // rbp

while ( isspace(*s) )
++s;
v2 = strlen(s) - 1;
if ( v2 >= 0 )
{
v3 = &s[v2];
do
{
if ( !isspace(*v3) )
break;
--v3;
--v2;
}
while ( v2 >= 0 );
}
s[v2 + 1] = 0;
v4 = *s;
if ( *s )
{
v5 = s;
v6 = s;
do
{
++v6;
v5 += isspace(v4) == 0;
v4 = *v6;
*v5 = *v6;
}
while ( v4 );
}
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; // rbx
char *v2; // rax
char *stringp; // [rsp+8h] [rbp-10h] BYREF

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

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

v1 = strdup(a1);
stringp = v1;
log_level = 0;
while ( 1 )
{
v2 = strsep(&stringp, "|");
if ( !v2 )
break;
if ( !strcmp("LOG_ERR", v2) )
log_level |= 1u;
if ( !strcmp("LOG_DBG", v2) )
log_level |= 2u;
if ( !strcmp("LOG_INFO", v2) )
log_level |= 4u;
if ( !strcmp("LOG_WARN", v2) )
log_level |= 8u;
}
if ( v1 )
free(v1);
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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v4; // r13
int v5; // er14
void *i; // r12
const char *v7; // rsi
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
__int64 v11; // r8
__int64 v12; // r9
__pid_t v13; // eax
const char *v14; // rbp
size_t v15; // rax
size_t v16; // rax
const char *v17; // rbx
size_t v18; // rax
size_t v19; // rax
int v20; // eax
struct option v22; // [rsp+0h] [rbp-C8h] BYREF
const char *v23; // [rsp+20h] [rbp-A8h]
int v24; // [rsp+28h] [rbp-A0h]
__int64 v25; // [rsp+30h] [rbp-98h]
int v26; // [rsp+38h] [rbp-90h]
const char *v27; // [rsp+40h] [rbp-88h]
int v28; // [rsp+48h] [rbp-80h]
__int64 v29; // [rsp+50h] [rbp-78h]
int v30; // [rsp+58h] [rbp-70h]
const char *v31; // [rsp+60h] [rbp-68h]
int v32; // [rsp+68h] [rbp-60h]
__int64 v33; // [rsp+70h] [rbp-58h]
int v34; // [rsp+78h] [rbp-50h]
__int64 v35; // [rsp+80h] [rbp-48h]
int v36; // [rsp+88h] [rbp-40h]
__int64 v37; // [rsp+90h] [rbp-38h]
int v38; // [rsp+98h] [rbp-30h]

v22.name = "conffile";
v22.has_arg = 1;
v22.flag = 0LL;
v22.val = 99;
v23 = "foreground";
v24 = 0;
v25 = 0LL;
v26 = 102;
v27 = "help";
v28 = 0;
v29 = 0LL;
v30 = 104;
v31 = "logfile";
v32 = 1;
v33 = 0LL;
v34 = 108;
v35 = 0LL;
v36 = 0;
v37 = 0LL;
v38 = 0;
v4 = 0LL;
v5 = 0;
for ( i = 0LL; ; i = strdup(optarg) )
{
while ( 1 )
{
while ( 1 )
{
v7 = (const char *)argv;
v8 = getopt_long(argc, (char *const *)argv, "c:fhl:", &v22, 0LL);
if ( v8 != 102 )
break;
v5 = 1;
}
if ( v8 <= 102 )
break;
if ( v8 != 108 )
goto LABEL_9;
v4 = strdup(optarg);
}
if ( v8 == -1 )
break;
if ( v8 != 99 )
LABEL_9:
usage(*argv);
}
if ( v5 )
{
log_fp = stdout;
if ( !v4 )
{
LABEL_13:
if ( !i )
{
v17 = *argv;
v18 = strlen(v17);
i = malloc(v18 + 6);
v19 = strlen(v17);
memset(i, 0, v19 + 6);
v7 = "%s.conf";
sprintf((char *)i, "%s.conf", v17);
}
if ( (unsigned int)parse_config_file((const char *)i) )
{
fwrite("ERROR: parsing config file failed.\n", 1uLL, 0x23uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)set_defaults(
i,
v7,
v9,
v10,
v11,
v12,
v22.name,
v22.has_arg,
v22.flag,
v22.val,
v23,
v24,
v25,
v26,
v27,
v28,
v29,
v30,
v31,
v32) )
{
fwrite("ERROR: unable to set defaults.\n", 1uLL, 0x1FuLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)check_valid_config() )
{
fwrite("ERROR: bad config file.\n", 1uLL, 0x18uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)logger_init((const char *)levels) )
{
fwrite("ERROR: unable initialize logger.\n", 1uLL, 0x21uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)http_init() )
{
logger(1u, "ERROR: http_init failed.\n");
v5 = 1;
}
else
{
v5 = sql_init();
if ( v5 )
{
logger(1u, "ERROR: sql_init failed.\n");
v5 = 1;
}
else
{
while ( !terminate )
;
}
}
sql_fin();
http_fin();
fflush(log_fp);
if ( log_fp != stdout )
{
v20 = fileno(log_fp);
fsync(v20);
fclose(log_fp);
}
if ( v4 )
free(v4);
if ( i )
free(i);
if ( host )
free(host);
if ( name )
free(name);
if ( passwd )
free(passwd);
if ( user )
free(user);
if ( ptr )
free(ptr);
if ( port )
free(port);
return v5;
}
LABEL_12:
v7 = "a";
log_fp = fopen((const char *)v4, "a");
if ( !log_fp )
{
printf("ERROR: Unable to open log file: %s\n", (const char *)v4);
return 1;
}
goto LABEL_13;
}
v13 = fork();
if ( v13 < 0 )
{
puts("Unable to background as daemon process.");
return 1;
}
if ( v13 <= 0 )
{
umask(0);
if ( setsid() == -1 )
{
puts("Unable to create new session for child process.");
return 1;
}
fclose(stdin);
fclose(stdout);
fclose(stderr);
signal(17, (__sighandler_t)((char *)strip_comments + 1));
signal(13, (__sighandler_t)((char *)strip_comments + 1));
signal(1, signal_handler);
signal(2, signal_handler);
signal(3, signal_handler);
signal(15, signal_handler);
if ( !v4 )
{
v14 = *argv;
v15 = strlen(*argv);
v4 = malloc(v15 + 5);
v16 = strlen(v14);
memset(v4, 0, v16 + 5);
sprintf((char *)v4, "%s.log", v14);
}
log_fp = stdout;
goto LABEL_12;
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v4; // r13
int v5; // er14
void *i; // r12
const char *v7; // rsi
int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
__int64 v11; // r8
__int64 v12; // r9
__pid_t v13; // eax
const char *v14; // rbp
size_t v15; // rax
size_t v16; // rax
const char *v17; // rbx
size_t v18; // rax
size_t v19; // rax
int v20; // eax
struct option v22; // [rsp+0h] [rbp-C8h] BYREF
const char *v23; // [rsp+20h] [rbp-A8h]
int v24; // [rsp+28h] [rbp-A0h]
__int64 v25; // [rsp+30h] [rbp-98h]
int v26; // [rsp+38h] [rbp-90h]
const char *v27; // [rsp+40h] [rbp-88h]
int v28; // [rsp+48h] [rbp-80h]
__int64 v29; // [rsp+50h] [rbp-78h]
int v30; // [rsp+58h] [rbp-70h]
const char *v31; // [rsp+60h] [rbp-68h]
int v32; // [rsp+68h] [rbp-60h]
__int64 v33; // [rsp+70h] [rbp-58h]
int v34; // [rsp+78h] [rbp-50h]
__int64 v35; // [rsp+80h] [rbp-48h]
int v36; // [rsp+88h] [rbp-40h]
__int64 v37; // [rsp+90h] [rbp-38h]
int v38; // [rsp+98h] [rbp-30h]

v22.name = "conffile";
v22.has_arg = 1;
v22.flag = 0LL;
v22.val = 99;
v23 = "foreground";
v24 = 0;
v25 = 0LL;
v26 = 102;
v27 = "help";
v28 = 0;
v29 = 0LL;
v30 = 104;
v31 = "logfile";
v32 = 1;
v33 = 0LL;
v34 = 108;
v35 = 0LL;
v36 = 0;
v37 = 0LL;
v38 = 0;
v4 = 0LL;
v5 = 0;
for ( i = 0LL; ; i = strdup(optarg) )
{
while ( 1 )
{
while ( 1 )
{
v7 = (const char *)argv;
v8 = getopt_long(argc, (char *const *)argv, "c:fhl:", &v22, 0LL);
if ( v8 != 102 )
break;
v5 = 1;
}
if ( v8 <= 102 )
break;
if ( v8 != 108 )
goto LABEL_9;
v4 = strdup(optarg);
}
if ( v8 == -1 )
break;
if ( v8 != 99 )
LABEL_9:
func2(*argv);
}
if ( v5 )
{
log_fp = stdout;
if ( !v4 )
{
LABEL_13:
if ( !i )
{
v17 = *argv;
v18 = strlen(v17);
i = malloc(v18 + 6);
v19 = strlen(v17);
memset(i, 0, v19 + 6);
v7 = "%s.conf";
sprintf((char *)i, "%s.conf", v17);
}
if ( (unsigned int)func4((const char *)i) )
{
fwrite("ERROR: parsing config file failed.\n", 1uLL, 0x23uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)func0(
i,
v7,
v9,
v10,
v11,
v12,
v22.name,
v22.has_arg,
v22.flag,
v22.val,
v23,
v24,
v25,
v26,
v27,
v28,
v29,
v30,
v31,
v32) )
{
fwrite("ERROR: unable to set defaults.\n", 1uLL, 0x1FuLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)func1() )
{
fwrite("ERROR: bad config file.\n", 1uLL, 0x18uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)func5((const char *)levels) )
{
fwrite("ERROR: unable initialize logger.\n", 1uLL, 0x21uLL, log_fp);
v5 = 1;
}
else if ( (unsigned int)http_init() )
{
func6(1u, "ERROR: http_init failed.\n");
v5 = 1;
}
else
{
v5 = sql_init();
if ( v5 )
{
func6(1u, "ERROR: sql_init failed.\n");
v5 = 1;
}
else
{
while ( !terminate )
;
}
}
sql_fin();
http_fin();
fflush(log_fp);
if ( log_fp != stdout )
{
v20 = fileno(log_fp);
fsync(v20);
fclose(log_fp);
}
if ( v4 )
free(v4);
if ( i )
free(i);
if ( host )
free(host);
if ( name )
free(name);
if ( passwd )
free(passwd);
if ( user )
free(user);
if ( ptr )
free(ptr);
if ( port )
free(port);
return v5;
}
LABEL_12:
v7 = "a";
log_fp = fopen((const char *)v4, "a");
if ( !log_fp )
{
printf("ERROR: Unable to open log file: %s\n", (const char *)v4);
return 1;
}
goto LABEL_13;
}
v13 = fork();
if ( v13 < 0 )
{
puts("Unable to background as daemon process.");
return 1;
}
if ( v13 <= 0 )
{
umask(0);
if ( setsid() == -1 )
{
puts("Unable to create new session for child process.");
return 1;
}
fclose(stdin);
fclose(stdout);
fclose(stderr);
signal(17, (__sighandler_t)((char *)strip_comments + 1));
signal(13, (__sighandler_t)((char *)strip_comments + 1));
signal(1, signal_handler);
signal(2, signal_handler);
signal(3, signal_handler);
signal(15, signal_handler);
if ( !v4 )
{
v14 = *argv;
v15 = strlen(*argv);
v4 = malloc(v15 + 5);
v16 = strlen(v14);
memset(v4, 0, v16 + 5);
sprintf((char *)v4, "%s.log", v14);
}
log_fp = stdout;
goto LABEL_12;
}
return v5;
}

original_code:
int
main (int argc, char *argv[])
{
 int c = 0;
 char *conffile = ((void *)0);
 int fg = 0;
 char *logfile = ((void *)0);
 struct option opts[] = {
  {.name = "conffile", .has_arg = 1, .flag = ((void *)0), .val = 'c'},
  {.name = "foreground", .has_arg = 0, .flag = ((void *)0), .val = 'f'},
  {.name = "help", .has_arg = 0, .flag = ((void *)0), .val = 'h'},
  {.name = "logfile", .has_arg = 1, .flag = ((void *)0), .val = 'l'},
  {.name = ((void *)0), .has_arg = 0, .flag = ((void *)0), .val = 0}
 };
 pid_t pid;
 int retval = 0;
 while (c != (-1)) {
  c = getopt_long (argc, argv, "c:fhl:", opts, ((void *)0));
  switch (c) {
   case (-1):
    break;
   case 'c':
    conffile = strdup (optarg);
    break;
   case 'f':
    fg = 1;
    break;
   case 'l':
    logfile = strdup (optarg);
    break;
   case '?':
   case 'h':
   default:
    usage (argv[0]);
  }
 }
 if (fg == 0) {
  pid = fork ();
  if (pid < 0) {
   printf ("Unable to background as daemon process.\n");
   return 1;
  }
  if (pid > 0)
   return 0;
  umask (0);
  if (setsid () == (pid_t) -1) {
   printf ("Unable to create new session for child "
     "process.\n");
   return 1;
  }
  fclose (stdin);
  fclose (stdout);
  fclose (stderr);
  signal (17, ((__sighandler_t) 1));
  signal (13, ((__sighandler_t) 1));
  signal (1, signal_handler);
  signal (2, signal_handler);
  signal (3, signal_handler);
  signal (15, signal_handler);
  if (logfile == ((void *)0)) {
   logfile = (char *) malloc (sizeof (char)
     * (strlen (argv[0]) + 5));
   memset (logfile, 0, sizeof (char)
     * (strlen (argv[0]) + 5));
   sprintf (logfile, "%s.log", argv[0]);
  }
 }
 log_fp = stdout;
 if (logfile != ((void *)0)) {
  log_fp = fopen (logfile, "a");
  if (log_fp == ((void *)0)) {
   printf ("ERROR: Unable to open log file: %s\n",
     logfile);
   return 1;
  }
 }
 if (conffile == ((void *)0)) {
  conffile = (char *) malloc (sizeof (char)
    * (strlen (argv[0]) + 6));
  memset (conffile, 0, sizeof (char) * (strlen (argv[0]) + 6));
  sprintf (conffile, "%s.conf", argv[0]);
 }
 if (parse_config_file (conffile) != 0) {
  fprintf (log_fp, "ERROR: parsing config file failed.\n");
  retval = 1;
  goto cleanup;
 }
 if (set_defaults () != 0) {
  fprintf (log_fp, "ERROR: unable to set defaults.\n");
  retval = 1;
  goto cleanup;
 }
 if (check_valid_config () != 0) {
  fprintf (log_fp, "ERROR: bad config file.\n");
  retval = 1;
  goto cleanup;
 }
 if (logger_init (levels) != 0) {
  fprintf (log_fp, "ERROR: unable initialize logger.\n");
  retval = 1;
  goto cleanup;
 }
 if (http_init () != 0) {
  logger (LOG_ERR, "ERROR: http_init failed.\n");
  retval = 1;
  goto cleanup;
 }
 if (sql_init () != 0) {
  logger (LOG_ERR, "ERROR: sql_init failed.\n");
  retval = 1;
  goto cleanup;
 }
 while (terminate == 0);
cleanup:
 sql_fin ();
 http_fin ();
 fflush (log_fp);
 if (log_fp != stdout) {
  fsync (fileno (log_fp));
  fclose (log_fp);
 }
 if (logfile != ((void *)0))
  free (logfile);
 if (conffile != ((void *)0))
  free (conffile);
 if (host != ((void *)0))
  free (host);
 if (name != ((void *)0))
  free (name);
 if (passwd != ((void *)0))
  free (passwd);
 if (user != ((void *)0))
  free (user);
 if (ip != ((void *)0))
  free (ip);
 if (port != ((void *)0))
  free (port);
 return retval;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int c = 0;
 char *conffile = ((void *)0);
 int fg = 0;
 char *logfile = ((void *)0);
 struct option opts[] = {
  {.name = "conffile", .has_arg = 1, .flag = ((void *)0), .val = 'c'},
  {.name = "foreground", .has_arg = 0, .flag = ((void *)0), .val = 'f'},
  {.name = "help", .has_arg = 0, .flag = ((void *)0), .val = 'h'},
  {.name = "logfile", .has_arg = 1, .flag = ((void *)0), .val = 'l'},
  {.name = ((void *)0), .has_arg = 0, .flag = ((void *)0), .val = 0}
 };
 int pid;
 int retval = 0;
 while (c != (-1)) {
  c = getopt_long (argc, argv, "c:fhl:", opts, ((void *)0));
  switch (c) {
   case (-1):
    break;
   case 'c':
    conffile = strdup (optarg);
    break;
   case 'f':
    fg = 1;
    break;
   case 'l':
    logfile = strdup (optarg);
    break;
   case '?':
   case 'h':
   default:
    usage (argv[0]);
  }
 }
 if (fg == 0) {
  pid = fork ();
  if (pid < 0) {
   printf ("Unable to background as daemon process.\n");
   return 1;
  }
  if (pid > 0)
   return 0;
  umask (0);
  if (setsid () == (pid_t) -1) {
   printf ("Unable to create new session for child "
     "process.\n");
   return 1;
  }
  fclose (stdin);
  fclose (stdout);
  fclose (stderr);
  signal (17, ((void (*)(int)) 1));
  signal (13, ((void (*)(int)) 1));
  signal (1, signal_handler);
  signal (2, signal_handler);
  signal (3, signal_handler);
  signal (15, signal_handler);
  if (logfile == ((void *)0)) {
   logfile = (char *) malloc (sizeof (char)
     * (strlen (argv[0]) + 5));
   memset (logfile, 0, sizeof (char)
     * (strlen (argv[0]) + 5));
   sprintf (logfile, "%s.log", argv[0]);
  }
 }
 log_fp = stdout;
 if (logfile != ((void *)0)) {
  log_fp = fopen (logfile, "a");
  if (log_fp == ((void *)0)) {
   printf ("ERROR: Unable to open log file: %s\n",
     logfile);
   return 1;
  }
 }
 if (conffile == ((void *)0)) {
  conffile = (char *) malloc (sizeof (char)
    * (strlen (argv[0]) + 6));
  memset (conffile, 0, sizeof (char) * (strlen (argv[0]) + 6));
  sprintf (conffile, "%s.conf", argv[0]);
 }
 if (parse_config_file (conffile) != 0) {
  fprintf (log_fp, "ERROR: parsing config file failed.\n");
  retval = 1;
  goto cleanup;
 }
 if (set_defaults () != 0) {
  fprintf (log_fp, "ERROR: unable to set defaults.\n");
  retval = 1;
  goto cleanup;
 }
 if (check_valid_config () != 0) {
  fprintf (log_fp, "ERROR: bad config file.\n");
  retval = 1;
  goto cleanup;
 }
 if (logger_init (levels) != 0) {
  fprintf (log_fp, "ERROR: unable initialize logger.\n");
  retval = 1;
  goto cleanup;
 }
 if (http_init () != 0) {
  logger (LOG_ERR, "ERROR: http_init failed.\n");
  retval = 1;
  goto cleanup;
 }
 if (sql_init () != 0) {
  logger (LOG_ERR, "ERROR: sql_init failed.\n");
  retval = 1;
  goto cleanup;
 }
 while (terminate == 0);
cleanup:
 sql_fin ();
 http_fin ();
 fflush (log_fp);
 if (log_fp != stdout) {
  fsync (fileno (log_fp));
  fclose (log_fp);
 }
 if (logfile != ((void *)0))
  free (logfile);
 if (conffile != ((void *)0))
  free (conffile);
 if (host != ((void *)0))
  free (host);
 if (name != ((void *)0))
  free (name);
 if (passwd != ((void *)0))
  free (passwd);
 if (user != ((void *)0))
  free (user);
 if (ip != ((void *)0))
  free (ip);
 if (port != ((void *)0))
  free (port);
 return retval;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types:
conffile
char *

variable_types: fg int

variable_types:
logfile
char *

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: pid int

variable_types: retval int


decompiled_code:
void __fastcall free_benc_data(_QWORD *a1)
{
unsigned int v2; // ebp
char *v3; // rax
char *v4; // rdi
__int64 v5; // r12
_QWORD *v6; // rax
int v7; // ebp

if ( a1 )
{
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 2 )
{
v6 = (_QWORD *)a1[1];
if ( v6 )
{
if ( *v6 )
{
v7 = 0;
do
{
((void (*)(void))free_benc_data)();
++v7;
v6 = (_QWORD *)a1[1];
}
while ( v6[v7] );
}
free(v6);
}
goto LABEL_5;
}
if ( v2 == 3 )
{
v4 = (char *)a1[1];
if ( !v4 )
{
LABEL_5:
free(a1);
return;
}
}
else
{
if ( v2 )
goto LABEL_5;
v3 = (char *)a1[1];
if ( !v3 )
goto LABEL_5;
v4 = (char *)a1[1];
if ( *((_QWORD *)v3 + 1) )
{
v5 = 0LL;
do
{
free(*(void **)v3);
free_benc_data(*(_QWORD *)(a1[1] + v5 + 8));
++v2;
v4 = (char *)a1[1];
v5 = 16LL * v2;
v3 = &v4[v5];
}
while ( *(_QWORD *)&v4[v5 + 8] );
}
}
free(v4);
goto LABEL_5;
}
}

original_code:
static inline void
free_benc_data (benc_data_t *bd)
{
 uint32_t i;
 if (bd == ((void *)0))
  return;
 switch (bd->type) {
  case BENC_TYPE_DCT:
   if (bd->data.d == ((void *)0))
    break;
   for (i = 0; bd->data.d[i].val; i++) {
    free (bd->data.d[i].key);
    free_benc_data (bd->data.d[i].val);
   }
   free (bd->data.d);
   break;
  case BENC_TYPE_INT:
   break;
  case BENC_TYPE_LST:
   if (bd->data.l == ((void *)0))
    break;
   for (i = 0; bd->data.l[i]; i++)
    free_benc_data (bd->data.l[i]);
   free (bd->data.l);
   break;
  case BENC_TYPE_STR:
   if (bd->data.s)
    free (bd->data.s);
   break;
  default:
   break;
 }
 free (bd);
 return;
}

canonical_original_code:
static inline void free_benc_data(struct __benc_data_type *bd)
{
 unsigned int i;
 if (bd == ((void *)0))
  return;
 switch (bd->type) {
  case BENC_TYPE_DCT:
   if (bd->data.d == ((void *)0))
    break;
   for (i = 0; bd->data.d[i].val; i++) {
    free (bd->data.d[i].key);
    free_benc_data (bd->data.d[i].val);
   }
   free (bd->data.d);
   break;
  case BENC_TYPE_INT:
   break;
  case BENC_TYPE_LST:
   if (bd->data.l == ((void *)0))
    break;
   for (i = 0; bd->data.l[i]; i++)
    free_benc_data (bd->data.l[i]);
   free (bd->data.l);
   break;
  case BENC_TYPE_STR:
   if (bd->data.s)
    free (bd->data.s);
   break;
  default:
   break;
 }
 free (bd);
 return;
}

variable_types: struct __benc_data_type { enum BENC_TYPE { BENC_TYPE_DCT=0, BENC_TYPE_INT=1, BENC_TYPE_LST=2, BENC_TYPE_STR=3 } type; union { struct __benc_dict_type { char *key; struct __benc_data_type *val; } *d; long i; struct __benc_data_type **l; char *s; } data; } *

variable_types:
i
unsigned int

user_defined_types: struct __benc_data_type { enum BENC_TYPE type; union { struct __benc_dict_type *d; long i; struct __benc_data_type **l; char *s; } data; }


decompiled_code:
char *__fastcall bencode_int(__int64 a1)
{
char *v1; // rax
char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp

v1 = lltostr(a1, 10);
v2 = v1;
if ( !v1 )
return 0LL;
v3 = strlen(v1);
v4 = (char *)malloc(v3 + 3);
v5 = v4;
if ( v4 )
sprintf(v4, "i%se", v2);
else
free(v2);
return v5;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
char *v1; // rax
char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp

v1 = func1(a1, 10);
v2 = v1;
if ( !v1 )
return 0LL;
v3 = strlen(v1);
v4 = (char *)malloc(v3 + 3);
v5 = v4;
if ( v4 )
sprintf(v4, "i%se", v2);
else
free(v2);
return v5;
}

original_code:
static inline char *
bencode_int (int64_t i)
{
 char *str = ((void *)0), *tmp = ((void *)0);
 tmp = lltostr (i, BASE_DEC);
 if (tmp == ((void *)0))
  return ((void *)0);
 str = (char *) malloc (sizeof (char) * (strlen (tmp) + 3));
 if (str == ((void *)0)) {
  free (tmp);
  return ((void *)0);
 }
 sprintf (str, "i%se", tmp);
 return str;
}

canonical_original_code:
static inline char *bencode_int(long i)
{
 char *str = ((void *)0); char *tmp = ((void *)0);
 tmp = lltostr (i, BASE_DEC);
 if (tmp == ((void *)0))
  return ((void *)0);
 str = (char *) malloc (sizeof (char) * (strlen (tmp) + 3));
 if (str == ((void *)0)) {
  free (tmp);
  return ((void *)0);
 }
 sprintf (str, "i%se", tmp);
 return str;
}

variable_types: i long

variable_types:
str
char *

variable_types:
tmp
char *


decompiled_code:
char *__fastcall bencode_str(char *s)
{
__int64 v1; // rax
char *v2; // rax
char *v3; // rbx
size_t v4; // r12
size_t v5; // rax
char *v6; // rax
char *v7; // r12

v1 = strlen(s);
v2 = lltostr(v1, 10);
v3 = v2;
if ( !v2 )
return 0LL;
v4 = strlen(v2);
v5 = strlen(s);
v6 = (char *)malloc(v4 + v5 + 2);
v7 = v6;
if ( v6 )
sprintf(v6, "%s:%s", v3, s);
else
free(v3);
return v7;
}

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
__int64 v1; // rax
char *v2; // rax
char *v3; // rbx
size_t v4; // r12
size_t v5; // rax
char *v6; // rax
char *v7; // r12

v1 = strlen(s);
v2 = func1(v1, 10);
v3 = v2;
if ( !v2 )
return 0LL;
v4 = strlen(v2);
v5 = strlen(s);
v6 = (char *)malloc(v4 + v5 + 2);
v7 = v6;
if ( v6 )
sprintf(v6, "%s:%s", v3, s);
else
free(v3);
return v7;
}

original_code:
static inline char *
bencode_str (char *s)
{
 char *len = ((void *)0), *str = ((void *)0);
 len = lltostr ((int64_t) strlen (s), BASE_DEC);
 if (len == ((void *)0))
  return ((void *)0);
 str = (char *) malloc (sizeof (char) * (strlen (len) + strlen (s) + 2));
 if (str == ((void *)0)) {
  free (len);
  return ((void *)0);
 }
 sprintf (str, "%s:%s", len, s);
 return str;
}

canonical_original_code:
static inline char *bencode_str(char *s)
{
 char *len = ((void *)0); char *str = ((void *)0);
 len = lltostr ((long) strlen (s), BASE_DEC);
 if (len == ((void *)0))
  return ((void *)0);
 str = (char *) malloc (sizeof (char) * (strlen (len) + strlen (s) + 2));
 if (str == ((void *)0)) {
  free (len);
  return ((void *)0);
 }
 sprintf (str, "%s:%s", len, s);
 return str;
}

variable_types:
s
char *

variable_types:
len
char *


decompiled_code:
char *__fastcall bencode_lst(char **a1)
{
char *v1; // rbx
const char *v3; // rax
void *v4; // rbp
size_t v5; // r12
char *v6; // rdi
int v7; // er13
const char *v8; // rax
char *v9; // rax
size_t v10; // rax
char *v11; // rax

v1 = *a1;
if ( *a1 )
{
v3 = (const char *)benc_encode(*a1);
v4 = (void *)v3;
if ( v3 )
{
v5 = strlen(v3) + 1;
v6 = a1[1];
if ( v6 )
{
v7 = 1;
while ( 1 )
{
v8 = (const char *)benc_encode(v6);
v1 = (char *)v8;
if ( !v8 )
break;
v5 += strlen(v8);
v9 = (char *)realloc(v4, v5);
v4 = v9;
if ( v9 )
{
strcat(v9, v1);
free(v1);
v6 = a1[++v7];
if ( v6 )
continue;
}
goto LABEL_8;
}
}
else
{
LABEL_8:
v10 = strlen((const char *)v4);
v11 = (char *)malloc(v10 + 3);
v1 = v11;
if ( v11 )
sprintf(v11, "l%se", (const char *)v4);
}
free(v4);
}
else
{
v1 = 0LL;
}
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func4(char **a1)
{
char *v1; // rbx
const char *v3; // rax
void *v4; // rbp
size_t v5; // r12
char *v6; // rdi
int v7; // er13
const char *v8; // rax
char *v9; // rax
size_t v10; // rax
char *v11; // rax

v1 = *a1;
if ( *a1 )
{
v3 = (const char *)func5(*a1);
v4 = (void *)v3;
if ( v3 )
{
v5 = strlen(v3) + 1;
v6 = a1[1];
if ( v6 )
{
v7 = 1;
while ( 1 )
{
v8 = (const char *)func5(v6);
v1 = (char *)v8;
if ( !v8 )
break;
v5 += strlen(v8);
v9 = (char *)realloc(v4, v5);
v4 = v9;
if ( v9 )
{
strcat(v9, v1);
free(v1);
v6 = a1[++v7];
if ( v6 )
continue;
}
goto LABEL_8;
}
}
else
{
LABEL_8:
v10 = strlen((const char *)v4);
v11 = (char *)malloc(v10 + 3);
v1 = v11;
if ( v11 )
sprintf(v11, "l%se", (const char *)v4);
}
free(v4);
}
else
{
v1 = 0LL;
}
}
return v1;
}

original_code:
static inline char *
bencode_lst (benc_data_t **list)
{
 uint32_t i = 0;
 size_t len = 0;
 char *str = ((void *)0), *tmp = ((void *)0);
 if (list[0] == ((void *)0))
  return ((void *)0);
 tmp = benc_encode (list[0]);
 if (tmp == ((void *)0))
  return ((void *)0);
 str = tmp;
 tmp = ((void *)0);
 len = strlen (str) + 1;
 for (i = 1; list[i]; i++) {
  tmp = benc_encode (list[i]);
  if (tmp == ((void *)0)) {
   free (str);
   return ((void *)0);
  }
  len += strlen (tmp);
  str = (char *) realloc (str, sizeof (char) * len);
  if (str == ((void *)0))
   break;
  strcat (str, tmp);
  free (tmp);
  tmp = ((void *)0);
 }
 tmp = str;
 len = strlen (str) + 3;
 str = (char *) malloc (sizeof (char) * len);
 if (str == ((void *)0)) {
  free (tmp);
  return ((void *)0);
 }
 sprintf (str, "l%se", tmp);
 free (tmp);
 return str;
}

canonical_original_code:
static inline char *bencode_lst(struct __benc_data_type **list)
{
 unsigned int i = 0;
 unsigned long len = 0;
 char *str = ((void *)0); char *tmp = ((void *)0);
 if (list[0] == ((void *)0))
  return ((void *)0);
 tmp = benc_encode (list[0]);
 if (tmp == ((void *)0))
  return ((void *)0);
 str = tmp;
 tmp = ((void *)0);
 len = strlen (str) + 1;
 for (i = 1; list[i]; i++) {
  tmp = benc_encode (list[i]);
  if (tmp == ((void *)0)) {
   free (str);
   return ((void *)0);
  }
  len += strlen (tmp);
  str = (char *) realloc (str, sizeof (char) * len);
  if (str == ((void *)0))
   break;
  strcat (str, tmp);
  free (tmp);
  tmp = ((void *)0);
 }
 tmp = str;
 len = strlen (str) + 3;
 str = (char *) malloc (sizeof (char) * len);
 if (str == ((void *)0)) {
  free (tmp);
  return ((void *)0);
 }
 sprintf (str, "l%se", tmp);
 free (tmp);
 return str;
}

variable_types: struct __benc_data_type { enum BENC_TYPE { BENC_TYPE_DCT=0, BENC_TYPE_INT=1, BENC_TYPE_LST=2, BENC_TYPE_STR=3 } type; union { struct __benc_dict_type { char *key; struct __benc_data_type *val; } *d; long i; struct __benc_data_type **l; char *s; } data; } **

variable_types:
len
unsigned long


decompiled_code:
char *__fastcall benc_encode(__int64 a1)
{
unsigned int v1; // eax
char *result; // rax

if ( !a1 )
return 0LL;
v1 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 2 )
return bencode_lst(*(char ***)(a1 + 8));
if ( v1 > 2 )
{
if ( v1 == 3 )
result = bencode_str(*(char **)(a1 + 8));
else
result = 0LL;
}
else if ( v1 )
{
result = bencode_int(*(_QWORD *)(a1 + 8));
}
else
{
result = (char *)bencode_dct(*(_QWORD *)(a1 + 8));
}
return result;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1)
{
unsigned int v1; // eax
char *result; // rax

if ( !a1 )
return 0LL;
v1 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 2 )
return func4(*(char ***)(a1 + 8));
if ( v1 > 2 )
{
if ( v1 == 3 )
result = func3(*(char **)(a1 + 8));
else
result = 0LL;
}
else if ( v1 )
{
result = func2(*(_QWORD *)(a1 + 8));
}
else
{
result = (char *)func6(*(_QWORD *)(a1 + 8));
}
return result;
}

original_code:
static inline char *
benc_encode (benc_data_t *bd)
{
 char *benc_str = ((void *)0);
 if (bd == ((void *)0))
  return ((void *)0);
 switch (bd->type) {
  case BENC_TYPE_DCT:
   benc_str = bencode_dct (bd->data.d);
   break;
  case BENC_TYPE_INT:
   benc_str = bencode_int (bd->data.i);
   break;
  case BENC_TYPE_LST:
   benc_str = bencode_lst (bd->data.l);
   break;
  case BENC_TYPE_STR:
   benc_str = bencode_str (bd->data.s);
   break;
  default:
   return ((void *)0);
 }
 return benc_str;
}

canonical_original_code:
static inline char *benc_encode(struct __benc_data_type *bd)
{
 char *benc_str = ((void *)0);
 if (bd == ((void *)0))
  return ((void *)0);
 switch (bd->type) {
  case BENC_TYPE_DCT:
   benc_str = bencode_dct (bd->data.d);
   break;
  case BENC_TYPE_INT:
   benc_str = bencode_int (bd->data.i);
   break;
  case BENC_TYPE_LST:
   benc_str = bencode_lst (bd->data.l);
   break;
  case BENC_TYPE_STR:
   benc_str = bencode_str (bd->data.s);
   break;
  default:
   return ((void *)0);
 }
 return benc_str;
}

user_defined_types: enum BENC_TYPE { BENC_TYPE_DCT=0, BENC_TYPE_INT=1, BENC_TYPE_LST=2, BENC_TYPE_STR=3 }

user_defined_types: struct __benc_dict_type { char *key; struct __benc_data_type *val; }