decompiled_code:
int __fastcall draw_square(__int64 a1, unsigned int a2, unsigned int a3)
{
char v4; // al
unsigned int v5; // eax
_BYTE *v6; // rax
unsigned int v7; // eax
int v8; // eax
int v9; // eax
unsigned int v10; // eax
int v11; // eax
unsigned int v12; // eax
int v13; // eax
unsigned int v14; // eax
int v15; // eax
unsigned int v16; // eax
unsigned int v17; // eax
unsigned int v18; // eax

srand(map + (*(_DWORD *)(a1 + 8) ^ (*(_DWORD *)(a1 + 16) << 6)));
set_cursor(a2, a3);
v4 = *(_BYTE *)a1 & 3;
switch ( v4 )
{
case 2:
v12 = grey(0);
set_background(v12);
if ( (rand() & 3) != 0 )
{
LABEL_10:
LODWORD(v6) = putchar(32);
break;
}
v13 = rand();
v14 = grey(v13 % 10 + 5);
set_color(v14);
v15 = rand();
LODWORD(v6) = putchar(byte_707[v15 % 2]);
break;
case 3:
v16 = grey(15);
set_background(v16);
LODWORD(v6) = putchar(32);
break;
case 1:
v7 = grey(0);
set_background(v7);
v8 = rand();
if ( v8 == 15 * (v8 / 15) )
{
v9 = rand();
v10 = rgb(0, v9 % 5 + 1, 0);
set_color(v10);
v11 = rand();
LODWORD(v6) = putchar(byte_707[v11 % 2]);
break;
}
goto LABEL_10;
default:
v5 = rgb(0, 2, 5);
set_background(v5);
LODWORD(v6) = putchar(32);
break;
}
if ( *(_QWORD *)(a1 + 32) )
{
set_cursor(a2, a3);
LODWORD(v6) = **(_BYTE **)(a1 + 32) & 3;
if ( (_BYTE)v6 != 3 )
{
v17 = rgb(5, 2, 0);
set_color(v17);
LODWORD(v6) = putchar(117);
}
}
else if ( *(_QWORD *)(a1 + 24) )
{
set_cursor(a2, a3);
v6 = **(_BYTE ***)(a1 + 24);
if ( (*v6 & 1) == 0 )
{
v18 = grey(20);
set_color(v18);
LODWORD(v6) = putchar(41);
}
}
return (int)v6;
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1, unsigned int a2, unsigned int a3)
{
char v4; // al
unsigned int v5; // eax
_BYTE *v6; // rax
unsigned int v7; // eax
int v8; // eax
int v9; // eax
unsigned int v10; // eax
int v11; // eax
unsigned int v12; // eax
int v13; // eax
unsigned int v14; // eax
int v15; // eax
unsigned int v16; // eax
unsigned int v17; // eax
unsigned int v18; // eax

srand(map + (*(_DWORD *)(a1 + 8) ^ (*(_DWORD *)(a1 + 16) << 6)));
func5(a2, a3);
v4 = *(_BYTE *)a1 & 3;
switch ( v4 )
{
case 2:
v12 = func1(0);
func11(v12);
if ( (rand() & 3) != 0 )
{
LABEL_10:
LODWORD(v6) = putchar(32);
break;
}
v13 = rand();
v14 = func1(v13 % 10 + 5);
func10(v14);
v15 = rand();
LODWORD(v6) = putchar(byte_707[v15 % 2]);
break;
case 3:
v16 = func1(15);
func11(v16);
LODWORD(v6) = putchar(32);
break;
case 1:
v7 = func1(0);
func11(v7);
v8 = rand();
if ( v8 == 15 * (v8 / 15) )
{
v9 = rand();
v10 = func0(0, v9 % 5 + 1, 0);
func10(v10);
v11 = rand();
LODWORD(v6) = putchar(byte_707[v11 % 2]);
break;
}
goto LABEL_10;
default:
v5 = func0(0, 2, 5);
func11(v5);
LODWORD(v6) = putchar(32);
break;
}
if ( *(_QWORD *)(a1 + 32) )
{
func5(a2, a3);
LODWORD(v6) = **(_BYTE **)(a1 + 32) & 3;
if ( (_BYTE)v6 != 3 )
{
v17 = func0(5, 2, 0);
func10(v17);
LODWORD(v6) = putchar(117);
}
}
else if ( *(_QWORD *)(a1 + 24) )
{
func5(a2, a3);
v6 = **(_BYTE ***)(a1 + 24);
if ( (*v6 & 1) == 0 )
{
v18 = func1(20);
func10(v18);
LODWORD(v6) = putchar(41);
}
}
return (int)v6;
}

original_code:
void draw_square(struct square* sq, const int x, const int y){
    srand((unsigned int) (map.seed + (sq->x ^ (sq->y * 64))));
    set_cursor(x, y);
    switch(sq->terrain){
        case 0:
            set_background(rgb(0,2,5));
            putchar(' ');
            break;
        case 1:
            set_background(grey(0));
            if(rand() % 15 == 0){
                set_color(rgb(0,rand()%5+1,0));
                putchar(",."[rand()%2]);
            }
            else
                putchar(' ');
            break;
        case 2:
            set_background(grey(0));
            if(rand() % 4 == 0){
                set_color(grey(rand()%10 + 5));
                putchar(",."[rand()%2]);
            }
            else
                putchar(' ');
            break;
        case 3:
            set_background(grey(15));
            putchar(' ');
            break;
    }
    if(sq->c != 0){
        set_cursor(x, y);
        switch(sq->c->type){
            case 0:
            case 1:
            case 2:
                set_color(rgb(5, 2, 0));
                putchar('u');
                break;
        }
    }
    else if(sq->i){
        set_cursor(x, y);
        switch(sq->i->item->type){
            case 0:
                set_color(grey(20));
                putchar(')');
        }
    }
}

canonical_original_code:
void draw_square(struct square *sq, int x, int y){
    srand((unsigned int) (map.seed + (sq->x ^ (sq->y * 64))));
    set_cursor(x, y);
    switch(sq->terrain){
        case 0:
            set_background(rgb(0,2,5));
            putchar(' ');
            break;
        case 1:
            set_background(grey(0));
            if(rand() % 15 == 0){
                set_color(rgb(0,rand()%5+1,0));
                putchar(",."[rand()%2]);
            }
            else
                putchar(' ');
            break;
        case 2:
            set_background(grey(0));
            if(rand() % 4 == 0){
                set_color(grey(rand()%10 + 5));
                putchar(",."[rand()%2]);
            }
            else
                putchar(' ');
            break;
        case 3:
            set_background(grey(15));
            putchar(' ');
            break;
    }
    if(sq->c != 0){
        set_cursor(x, y);
        switch(sq->c->type){
            case 0:
            case 1:
            case 2:
                set_color(rgb(5, 2, 0));
                putchar('u');
                break;
        }
    }
    else if(sq->i){
        set_cursor(x, y);
        switch(sq->i->item->type){
            case 0:
                set_color(grey(20));
                putchar(')');
        }
    }
}

variable_types: struct square { unsigned int terrain; unsigned int elevation; long x; struct itemlist { struct item { unsigned int type; } *item; struct itemlist *next; } *i; struct creature { unsigned int type; int health; unsigned int str; int x; struct itemlist *inv; struct item *wielded; struct skill_list { struct skill { unsigned int type; unsigned int level; } *skill; struct skill_list *next; } *skills; } *c; } *

user_defined_types: struct square { unsigned int terrain; unsigned int elevation; long x; struct itemlist *i; struct creature *c; }


repo: codl/ponyrl

decompiled_code:
int __fastcall draw_tile(__int64 a1, unsigned int a2, signed int a3)
{
__int64 i; // r13
signed int v5; // ebx
__int64 v6; // rbp
int result; // eax

if ( *(_DWORD *)a1 != 1 )
{
get_screen_size(0LL, 0LL);
for ( i = 2560LL; i != 166400; i += 2560LL )
{
v5 = a2;
result = set_cursor(a2, (unsigned int)a3);
v6 = i - 2560;
do
{
if ( v5 > 0 && v5 <= screen_width && a3 > 0 && a3 <= screen_height )
result = draw_square(*(_QWORD *)(a1 + 16) + v6, v5, a3);
++v5;
v6 += 40LL;
}
while ( v6 != i );
++a3;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, unsigned int a2, signed int a3)
{
__int64 i; // r13
signed int v5; // ebx
__int64 v6; // rbp
int result; // eax

if ( *(_DWORD *)a1 != 1 )
{
func2(0LL, 0LL);
for ( i = 2560LL; i != 166400; i += 2560LL )
{
v5 = a2;
result = func5(a2, (unsigned int)a3);
v6 = i - 2560;
do
{
if ( v5 > 0 && v5 <= screen_width && a3 > 0 && a3 <= screen_height )
result = func12(*(_QWORD *)(a1 + 16) + v6, v5, a3);
++v5;
v6 += 40LL;
}
while ( v6 != i );
++a3;
}
}
return result;
}

original_code:
void draw_tile(struct tile* t, const int x, const int y){
    if(t->type == Fill)
        return;
    int xx, yy;
    get_screen_size(0, 0);
    for(yy = 0; yy < 64; yy++){
        set_cursor(x, y+yy);
        for(xx = 0; xx < 64; xx++){
            if(x + xx < 1 || x + xx > screen_width ||
                    y + yy < 1 || y + yy > screen_height){
                continue;
            }
            draw_square(((t->tile.sq) + (yy) * 64 + (xx)), x + xx, y + yy);
        }
    }
}

canonical_original_code:
void draw_tile(struct tile *t, int x, int y){
    if(t->type == Fill)
        return;
    int xx; int yy;
    get_screen_size(0, 0);
    for(yy = 0; yy < 64; yy++){
        set_cursor(x, y+yy);
        for(xx = 0; xx < 64; xx++){
            if(x + xx < 1 || x + xx > screen_width ||
                    y + yy < 1 || y + yy > screen_height){
                continue;
            }
            draw_square(((t->tile.sq) + (yy) * 64 + (xx)), x + xx, y + yy);
        }
    }
}

variable_types: struct tile { enum { Array=0, Fill=1 } type; int x; union { struct square { unsigned int terrain; unsigned int elevation; long x; struct itemlist { struct item { unsigned int type; } *item; struct itemlist *next; } *i; struct creature { unsigned int type; int health; unsigned int str; int x; struct itemlist *inv; struct item *wielded; struct skill_list { struct skill { unsigned int type; unsigned int level; } *skill; struct skill_list *next; } *skills; } *c; } *fill; struct square *sq; } tile; } *

variable_types: x int

variable_types: y int

variable_types: xx int

variable_types: yy int

return_type: void

user_defined_types: struct tile { enum { Array=0, Fill=1 } type; int x; union { struct square *fill; struct square *sq; } tile; }

user_defined_types: struct itemlist { struct item *item; struct itemlist *next; }

user_defined_types: struct creature { unsigned int type; int health; unsigned int str; int x; struct itemlist *inv; struct item *wielded; struct skill_list *skills; }

user_defined_types: struct skill_list { struct skill *skill; struct skill_list *next; }


decompiled_code:
__int64 __fastcall draw_map(int a1, int a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax
int v6; // ebx
signed int v7; // ebx
int i; // er12
unsigned int v9; // ebp
__int64 v10; // rax
int v11; // [rsp+8h] [rbp-40h]

v4 = a1 - screen_width / 2 + 63;
if ( a1 - screen_width / 2 >= 0 )
v4 = a1 - screen_width / 2;
result = v4 & 0xFFFFFFC0;
if ( (int)result <= 0 )
result = (unsigned int)(result - 64);
if ( (int)result <= a1 + screen_width / 2 + 63 )
{
v11 = result - a1;
do
{
v6 = a2 - screen_height / 2 + 63;
if ( a2 - screen_height / 2 >= 0 )
v6 = a2 - screen_height / 2;
v7 = v6 & 0xFFFFFFC0;
if ( v7 <= 0 )
v7 -= 64;
for ( i = screen_height / 2; a2 + screen_height / 2 + 63 >= v7; i = screen_height / 2 )
{
v9 = v11 + screen_width / 2;
v10 = get_tile((unsigned int)((a1 + v11) / 64), (unsigned int)(v7 / 64), a3);
draw_tile(v10, v9, v7 - a2 + i);
v7 += 64;
}
v11 += 64;
result = (unsigned int)(a1 + v11);
}
while ( a1 + screen_width / 2 + 63 >= (int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, int a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax
int v6; // ebx
signed int v7; // ebx
int i; // er12
unsigned int v9; // ebp
__int64 v10; // rax
int v11; // [rsp+8h] [rbp-40h]

v4 = a1 - screen_width / 2 + 63;
if ( a1 - screen_width / 2 >= 0 )
v4 = a1 - screen_width / 2;
result = v4 & 0xFFFFFFC0;
if ( (int)result <= 0 )
result = (unsigned int)(result - 64);
if ( (int)result <= a1 + screen_width / 2 + 63 )
{
v11 = result - a1;
do
{
v6 = a2 - screen_height / 2 + 63;
if ( a2 - screen_height / 2 >= 0 )
v6 = a2 - screen_height / 2;
v7 = v6 & 0xFFFFFFC0;
if ( v7 <= 0 )
v7 -= 64;
for ( i = screen_height / 2; a2 + screen_height / 2 + 63 >= v7; i = screen_height / 2 )
{
v9 = v11 + screen_width / 2;
v10 = get_tile((unsigned int)((a1 + v11) / 64), (unsigned int)(v7 / 64), a3);
func13(v10, v9, v7 - a2 + i);
v7 += 64;
}
v11 += 64;
result = (unsigned int)(a1 + v11);
}
while ( a1 + screen_width / 2 + 63 >= (int)result );
}
return result;
}

original_code:
void draw_map(const int centerx, const int centery, const int ele){
    int x, y;
    x = (centerx - screen_width/2)/64*64;
    if(x <= 0) x -= 64;
    for(; x < centerx + screen_width/2 + 64; x+=64){
        y = (centery - screen_height/2)/64*64;
        if(y <= 0) y -= 64;
        for(; y < centery + screen_height/2 + 64; y+=64){
            draw_tile(get_tile(x/64, y/64, ele), x - centerx + screen_width/2, y - centery + screen_height/2);
        }
    }
}

canonical_original_code:
void draw_map(int centerx, int centery, int ele){
    int x; int y;
    x = (centerx - screen_width/2)/64*64;
    if(x <= 0) x -= 64;
    for(; x < centerx + screen_width/2 + 64; x+=64){
        y = (centery - screen_height/2)/64*64;
        if(y <= 0) y -= 64;
        for(; y < centery + screen_height/2 + 64; y+=64){
            draw_tile(get_tile(x/64, y/64, ele), x - centerx + screen_width/2, y - centery + screen_height/2);
        }
    }
}

variable_types: centerx int

variable_types: centery int

variable_types: ele int


putmsg

decompiled_code:
int __fastcall putmsg(char *s)
{
unsigned int v1; // eax
unsigned int v2; // eax

set_cursor(1u, 1LL);
v1 = grey(0);
set_background(v1);
v2 = grey(25);
set_color(v2);
return puts(s);
}

canonical_decompiled_code:
int __fastcall func15(char *s)
{
unsigned int v1; // eax
unsigned int v2; // eax

func5(1u, 1LL);
v1 = func1(0);
func11(v1);
v2 = func1(25);
func10(v2);
return puts(s);
}

original_code:
void putmsg(const char* msg){
    set_cursor(1,1);
    set_background(grey(0));
    set_color(grey(25));
    puts(msg);
}

canonical_original_code:
void putmsg(char *msg){
    set_cursor(1,1);
    set_background(grey(0));
    set_color(grey(25));
    puts(msg);
}

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall move_player(int a1)
{
unsigned int v1; // er12
unsigned int v2; // er14
unsigned int v3; // er13
unsigned int v4; // er15
unsigned int v5; // ebx
unsigned int v6; // eax
__int64 v7; // rbx
int v8; // ebp
unsigned int v9; // ebp
unsigned int v10; // eax
__int64 v11; // rbp
__int64 v12; // rbx
__int64 v13; // rax
__int64 v14; // rdi
__int64 v16; // rbx
int v17; // er14
__int64 v18; // rbp
int v19; // er12
unsigned int v20; // [rsp+8h] [rbp-40h]
int v21; // [rsp+Ch] [rbp-3Ch]

v1 = player[8];
v2 = player[9];
v20 = player[10];
switch ( a1 )
{
case 0:
v3 = player[8];
goto LABEL_4;
case 1:
v4 = v2 - 1;
goto LABEL_21;
case 2:
v4 = player[9];
LABEL_21:
v3 = v1 + 1;
break;
case 3:
v3 = v1 + 1;
goto LABEL_18;
case 4:
v3 = player[8];
LABEL_18:
v4 = v2 + 1;
break;
case 5:
v4 = v2 + 1;
goto LABEL_15;
case 6:
v4 = player[9];
LABEL_15:
v3 = v1 - 1;
break;
case 7:
v3 = v1 - 1;
LABEL_4:
v4 = v2 - 1;
break;
default:
v4 = player[9];
v3 = player[8];
break;
}
v5 = pos_div(v2, 64LL);
v6 = pos_div(v1, 64LL);
v7 = get_tile(v6, v5, 0LL);
v8 = pos_div(v1, 64LL);
if ( v8 != (unsigned int)pos_div(v3, 64LL)
|| (v21 = pos_div(v2, 64LL), v11 = v7, v21 != (unsigned int)pos_div(v4, 64LL)) )
{
v9 = pos_div(v4, 64LL);
v10 = pos_div(v3, 64LL);
v11 = get_tile(v10, v9, 0LL);
}
if ( *(_DWORD *)v7 )
{
v12 = *(_QWORD *)(v7 + 16);
}
else
{
v16 = *(_QWORD *)(v7 + 16);
v17 = pos_mod(v2, 64LL);
v12 = v16 + 40 * ((int)pos_mod(v1, 64LL) + (__int64)(v17 << 6));
}
if ( *(_DWORD *)v11 )
{
v13 = *(_QWORD *)(v11 + 16);
}
else
{
v18 = *(_QWORD *)(v11 + 16);
v19 = pos_mod(v4, 64LL);
v13 = v18 + 40 * ((int)pos_mod(v3, 64LL) + (__int64)(v19 << 6));
}
v14 = *(_QWORD *)(v13 + 32);
if ( v14 )
return hit(v14, player);
if ( (*(_BYTE *)v13 & 3) == 3 )
return putmsg("You bump into a wall.");
*(_QWORD *)(v12 + 32) = 0LL;
player[8] = v3;
player[9] = v4;
*(_QWORD *)(v13 + 32) = player;
return draw_map(v3, v4, v20);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
unsigned int v1; // er12
unsigned int v2; // er14
unsigned int v3; // er13
unsigned int v4; // er15
unsigned int v5; // ebx
unsigned int v6; // eax
__int64 v7; // rbx
int v8; // ebp
unsigned int v9; // ebp
unsigned int v10; // eax
__int64 v11; // rbp
__int64 v12; // rbx
__int64 v13; // rax
__int64 v14; // rdi
__int64 v16; // rbx
int v17; // er14
__int64 v18; // rbp
int v19; // er12
unsigned int v20; // [rsp+8h] [rbp-40h]
int v21; // [rsp+Ch] [rbp-3Ch]

v1 = player[8];
v2 = player[9];
v20 = player[10];
switch ( a1 )
{
case 0:
v3 = player[8];
goto LABEL_4;
case 1:
v4 = v2 - 1;
goto LABEL_21;
case 2:
v4 = player[9];
LABEL_21:
v3 = v1 + 1;
break;
case 3:
v3 = v1 + 1;
goto LABEL_18;
case 4:
v3 = player[8];
LABEL_18:
v4 = v2 + 1;
break;
case 5:
v4 = v2 + 1;
goto LABEL_15;
case 6:
v4 = player[9];
LABEL_15:
v3 = v1 - 1;
break;
case 7:
v3 = v1 - 1;
LABEL_4:
v4 = v2 - 1;
break;
default:
v4 = player[9];
v3 = player[8];
break;
}
v5 = pos_div(v2, 64LL);
v6 = pos_div(v1, 64LL);
v7 = get_tile(v6, v5, 0LL);
v8 = pos_div(v1, 64LL);
if ( v8 != (unsigned int)pos_div(v3, 64LL)
|| (v21 = pos_div(v2, 64LL), v11 = v7, v21 != (unsigned int)pos_div(v4, 64LL)) )
{
v9 = pos_div(v4, 64LL);
v10 = pos_div(v3, 64LL);
v11 = get_tile(v10, v9, 0LL);
}
if ( *(_DWORD *)v7 )
{
v12 = *(_QWORD *)(v7 + 16);
}
else
{
v16 = *(_QWORD *)(v7 + 16);
v17 = pos_mod(v2, 64LL);
v12 = v16 + 40 * ((int)pos_mod(v1, 64LL) + (__int64)(v17 << 6));
}
if ( *(_DWORD *)v11 )
{
v13 = *(_QWORD *)(v11 + 16);
}
else
{
v18 = *(_QWORD *)(v11 + 16);
v19 = pos_mod(v4, 64LL);
v13 = v18 + 40 * ((int)pos_mod(v3, 64LL) + (__int64)(v19 << 6));
}
v14 = *(_QWORD *)(v13 + 32);
if ( v14 )
return hit(v14, player);
if ( (*(_BYTE *)v13 & 3) == 3 )
return putmsg("You bump into a wall.");
*(_QWORD *)(v12 + 32) = 0LL;
player[8] = v3;
player[9] = v4;
*(_QWORD *)(v13 + 32) = player;
return draw_map(v3, v4, v20);
}

original_code:
void move_player(enum direction direction){
    int x, y, ele;
    int xx, yy;
    struct square *from, *to;
    struct tile *tilefrom, *tileto;
    x = xx = player.x;
    y = yy = player.y;
    ele = player.ele;
    switch(direction){
        case(NW):
            xx = x - 1;
        case(N):
            yy = y - 1;
            break;
        case(SW):
            yy = y + 1;
        case(W):
            xx = x - 1;
            break;
        case(SE):
            xx = x + 1;
        case(S):
            yy = y + 1;
            break;
        case(NE):
            yy = y - 1;
        case(E):
            xx = x + 1;
            break;
    }
    tilefrom = get_tile(pos_div(x, 64), pos_div(y, 64), 0);
    if(pos_div(x, 64) == pos_div(xx, 64) && pos_div(y, 64) == pos_div(yy, 64))
        tileto = tilefrom;
    else
        tileto = get_tile(pos_div(xx, 64), pos_div(yy, 64), 0);
    from = tilefrom->type == Array? ((tilefrom->tile.sq) + (pos_mod(y, 64)) * 64 + (pos_mod(x, 64))) : tilefrom->tile.fill;
    to = tileto->type == Array? ((tileto->tile.sq) + (pos_mod(yy, 64)) * 64 + (pos_mod(xx, 64))) : tileto->tile.fill;
    if(to->c){
        hit(to->c,&player);
    }
    else {
        switch(to->terrain){
            case(3):
                putmsg("You bump into a wall.");
                break;
            default:
                from->c = 0;
                player.x = xx;
                player.y = yy;
                to->c = &player;
                draw_map(xx, yy, ele);
        }
    }
}

canonical_original_code:
void move_player(enum direction direction){
    int x; int y; int ele;
    int xx; int yy;
    struct square *from; struct square *to;
    struct tile *tilefrom; struct tile *tileto;
    x = xx = player.x;
    y = yy = player.y;
    ele = player.ele;
    switch(direction){
        case(NW):
            xx = x - 1;
        case(N):
            yy = y - 1;
            break;
        case(SW):
            yy = y + 1;
        case(W):
            xx = x - 1;
            break;
        case(SE):
            xx = x + 1;
        case(S):
            yy = y + 1;
            break;
        case(NE):
            yy = y - 1;
        case(E):
            xx = x + 1;
            break;
    }
    tilefrom = get_tile(pos_div(x, 64), pos_div(y, 64), 0);
    if(pos_div(x, 64) == pos_div(xx, 64) && pos_div(y, 64) == pos_div(yy, 64))
        tileto = tilefrom;
    else
        tileto = get_tile(pos_div(xx, 64), pos_div(yy, 64), 0);
    from = tilefrom->type == Array? ((tilefrom->tile.sq) + (pos_mod(y, 64)) * 64 + (pos_mod(x, 64))) : tilefrom->tile.fill;
    to = tileto->type == Array? ((tileto->tile.sq) + (pos_mod(yy, 64)) * 64 + (pos_mod(xx, 64))) : tileto->tile.fill;
    if(to->c){
        hit(to->c,&player);
    }
    else {
        switch(to->terrain){
            case(3):
                putmsg("You bump into a wall.");
                break;
            default:
                from->c = 0;
                player.x = xx;
                player.y = yy;
                to->c = &player;
                draw_map(xx, yy, ele);
        }
    }
}

variable_types: enum direction { N=0, NE=1, E=2, SE=3, S=4, SW=5, W=6, NW=7 }

user_defined_types: struct skill { unsigned int type; unsigned int level; }


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

result = get_square((unsigned int)player[8], (unsigned int)player[9], (unsigned int)player[10]);
if ( !*(_QWORD *)(result + 24) )
result = putmsg("There is nothing to pick up here.");
return result;
}

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

result = get_square((unsigned int)player[8], (unsigned int)player[9], (unsigned int)player[10]);
if ( !*(_QWORD *)(result + 24) )
result = putmsg("There is nothing to pick up here.");
return result;
}

original_code:
void pick_up(void){
    struct itemlist* i = get_square(player.x, player.y, player.ele)->i;
    if(!i){
        putmsg("There is nothing to pick up here.");
    }
    else {
    }
}

canonical_original_code:
void pick_up(){
    struct itemlist *i = get_square(player.x, player.y, player.ele)->i;
    if(!i){
        putmsg("There is nothing to pick up here.");
    }
    else {
    }
}

variable_types: struct itemlist { struct item { unsigned int type; } *item; struct itemlist *next; } *


decompiled_code:
int init_map()
{
int result; // eax

result = rand();
map = result;
*((_QWORD *)&map + 1) = 0LL;
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

result = rand();
map = result;
*((_QWORD *)&map + 1) = 0LL;
return result;
}

original_code:
void init_map(void){
    map.seed = rand();
    map.tiles = ((void *)0);
}

canonical_original_code:
void init_map(){
    map.seed = rand();
    map.tiles = ((void *)0);
}


decompiled_code:
void __fastcall free_tile(void *ptr)
{
if ( !*(_DWORD *)ptr )
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( !*(_DWORD *)ptr )
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void free_tile(struct tile* t){
    if(t->type == Array){
        free(t->tile.sq);
    }
    free(t);
}

canonical_original_code:
void free_tile(struct tile *t){
    if(t->type == Array){
        free(t->tile.sq);
    }
    free(t);
}


original_code:
void gc_map(void){
    int counter = 0;
    struct tilelist* list = map.tiles;
    struct tilelist* next;
    while(list){
        counter++;
        next = list->next;
        if(counter == 100){
            list->next = ((void *)0);
        }
        else if (counter > 100) {
            free_tile(list->tile);
            free(list);
        }
        list = next;
    }
}

canonical_original_code:
void gc_map(){
    int counter = 0;
    struct tilelist *list = map.tiles;
    struct tilelist *next;
    while(list){
        counter++;
        next = list->next;
        if(counter == 100){
            list->next = ((void *)0);
        }
        else if (counter > 100) {
            free_tile(list->tile);
            free(list);
        }
        list = next;
    }
}

variable_types: counter int

variable_types: struct tilelist { struct tile { enum { Array=0, Fill=1 } type; int x; union { struct square { unsigned int terrain; unsigned int elevation; long x; struct itemlist { struct item { unsigned int type; } *item; struct itemlist *next; } *i; struct creature { unsigned int type; int health; unsigned int str; int x; struct itemlist *inv; struct item *wielded; struct skill_list { struct skill { unsigned int type; unsigned int level; } *skill; struct skill_list *next; } *skills; } *c; } *fill; struct square *sq; } tile; } *tile; struct tilelist *next; } *

user_defined_types: struct tilelist { struct tile *tile; struct tilelist *next; }


decompiled_code:
__int64 __fastcall get_square(unsigned int a1, unsigned int a2, int a3)
{
int v4; // ebp
int v5; // eax
__int64 v6; // rbp
int v7; // ebx

v4 = pos_div(a2, 64LL);
v5 = pos_div(a1, 64LL);
v6 = *((_QWORD *)get_tile(v5, v4, a3) + 2);
v7 = pos_mod(a2, 64LL);
return v6 + 40 * ((int)pos_mod(a1, 64LL) + (__int64)(v7 << 6));
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, unsigned int a2, int a3)
{
int v4; // ebp
int v5; // eax
__int64 v6; // rbp
int v7; // ebx

v4 = pos_div(a2, 64LL);
v5 = pos_div(a1, 64LL);
v6 = *((_QWORD *)get_tile(v5, v4, a3) + 2);
v7 = pos_mod(a2, 64LL);
return v6 + 40 * ((int)pos_mod(a1, 64LL) + (__int64)(v7 << 6));
}

original_code:
struct square* get_square(int x, int y, int ele){
    return ((get_tile(pos_div(x, 64), pos_div(y, 64), ele)->tile.sq) + (pos_mod(y, 64)) * 64 + (pos_mod(x, 64)));
}

canonical_original_code:
struct square *get_square(int x, int y, int ele){
    return ((get_tile(pos_div(x, 64), pos_div(y, 64), ele)->tile.sq) + (pos_mod(y, 64)) * 64 + (pos_mod(x, 64)));
}

return_type: struct square { unsigned int terrain; unsigned int elevation; long x; struct itemlist { struct item { unsigned int type; } *item; struct itemlist *next; } *i; struct creature { unsigned int type; int health; unsigned int str; int x; struct itemlist *inv; struct item *wielded; struct skill_list { struct skill { unsigned int type; unsigned int level; } *skill; struct skill_list *next; } *skills; } *c; } *


roll

decompiled_code:
__int64 __fastcall roll(int a1, int a2)
{
unsigned int v2; // er12
int i; // ebx

if ( a1 <= 0 )
return 0;
v2 = 0;
for ( i = 0; i != a1; ++i )
v2 += rand() % a2 + 1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
unsigned int v2; // er12
int i; // ebx

if ( a1 <= 0 )
return 0;
v2 = 0;
for ( i = 0; i != a1; ++i )
v2 += rand() % a2 + 1;
return v2;
}

original_code:
int roll(int times, int sides) {
    int i;
    int sum = 0;
    for(i = 0; i < times; i++) {
        sum = sum + (rand() % sides + 1);
    }
    return sum;
}

variable_types: times int

variable_types: sides int

variable_types: i int

variable_types: sum int

return_type: int


repo: codyharrington/ircbot

decompiled_code:
void __fastcall free_context(void *ptr)
{
int v2; // edi

v2 = *((_DWORD *)ptr + 12);
if ( v2 > 0 )
close(v2);
free_msg(*((_QWORD *)ptr + 7));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
int v2; // edi

v2 = *((_DWORD *)ptr + 12);
if ( v2 > 0 )
close(v2);
free_msg(*((_QWORD *)ptr + 7));
free(ptr);
}

original_code:
void free_context(struct IRC_CTX *ctx) {
 if (ctx->sockfd && ctx->sockfd >= 0)
  close(ctx->sockfd);
 free_msg(ctx->msg);
 free(ctx);
}

variable_types: struct IRC_CTX { struct sockaddr { unsigned short sa_family; char sa_data[14]; } servaddr; unsigned int servaddr_len; char *servaddr_str; char *channel; char *nick; int sockfd; int port; struct IRC_MSG { char *_raw; unsigned long _raw_len; struct IRC_SRC { char *server_name; char *nick; char *user; char *host; } *src; char *command; int argc; char **argv; char *text; int valid; } *msg; union sem_t { char __size[32]; long __align; } msg_mutex; } *

user_defined_types: struct IRC_CTX { struct sockaddr servaddr; unsigned int servaddr_len; char *servaddr_str; char *channel; char *nick; int sockfd; int port; struct IRC_MSG *msg; union sem_t msg_mutex; }

user_defined_types: struct IRC_MSG { char *_raw; unsigned long _raw_len; struct IRC_SRC *src; char *command; int argc; char **argv; char *text; int valid; }


decompiled_code:
void *__fastcall init_context(__int64 a1)
{
void *result; // rax

*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 24) = "irc.rizon.net";
*(_QWORD *)(a1 + 32) = "#/g/spam";
*(_DWORD *)(a1 + 52) = 6667;
*(_QWORD *)(a1 + 40) = "chchjesus_bot_two";
result = calloc(1uLL, 0x40uLL);
*(_QWORD *)(a1 + 56) = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
void *result; // rax

*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 24) = "irc.rizon.net";
*(_QWORD *)(a1 + 32) = "#/g/spam";
*(_DWORD *)(a1 + 52) = 6667;
*(_QWORD *)(a1 + 40) = "chchjesus_bot_two";
result = calloc(1uLL, 0x40uLL);
*(_QWORD *)(a1 + 56) = result;
return result;
}

original_code:
void init_context(struct IRC_CTX *ctx) {
 memset(&(ctx->servaddr), 0, sizeof(ctx->servaddr));
 ctx->servaddr_str = "irc.rizon.net";
 ctx->channel = "#/g/spam";
 ctx->port = 6667;
 ctx->nick = "chchjesus_bot_two";
 ctx->msg = (struct IRC_MSG *) calloc(1, sizeof(struct IRC_MSG));
}

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

user_defined_types: union sem_t { char __size[32]; long __align; }

user_defined_types: struct IRC_SRC { char *server_name; char *nick; char *user; char *host; }


decompiled_code:
__int64 __fastcall init_connection(struct sockaddr *addr)
{
const char *v1; // rdx
struct addrinfo *v2; // rbp
int v3; // esi
int v4; // eax
const char *v5; // rdx
const char *v6; // rdx
char s[4]; // [rsp+3h] [rbp-55h] BYREF
char v9; // [rsp+7h] [rbp-51h]
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_DWORD *)s = 0;
v9 = 0;
*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
sprintf(s, "%d", *(unsigned int *)&addr[3].sa_data[2]);
req.ai_family = 2;
req.ai_socktype = 1;
if ( getaddrinfo(*(const char **)&addr[1].sa_data[6], s, &req, &pai) < 0 )
error((int)"getaddrinfo", (int)s, v1);
v2 = pai;
memcpy(addr, pai->ai_addr, pai->ai_addrlen);
*(_DWORD *)&addr[1].sa_family = v2->ai_addrlen;
v3 = v2->ai_socktype;
v4 = socket(v2->ai_family, v3, v2->ai_protocol);
*(_DWORD *)&addr[3].sa_family = v4;
if ( v4 < 0 )
error((int)"socket", v3, v5);
freeaddrinfo(pai);
if ( connect(*(_DWORD *)&addr[3].sa_family, addr, *(_DWORD *)&addr[1].sa_family) < 0 )
error((int)"connect", (int)addr, v6);
write_to_socket(addr, "USER %s 0 0 :%s\r\n", *(const char **)&addr[2].sa_data[6], *(const char **)&addr[2].sa_data[6]);
return write_to_socket(addr, "NICK %s\r\n", *(const char **)&addr[2].sa_data[6]);
}

canonical_decompiled_code:
__int64 __fastcall func2(struct sockaddr *addr)
{
const char *v1; // rdx
struct addrinfo *v2; // rbp
int v3; // esi
int v4; // eax
const char *v5; // rdx
const char *v6; // rdx
char s[4]; // [rsp+3h] [rbp-55h] BYREF
char v9; // [rsp+7h] [rbp-51h]
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_DWORD *)s = 0;
v9 = 0;
*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
sprintf(s, "%d", *(unsigned int *)&addr[3].sa_data[2]);
req.ai_family = 2;
req.ai_socktype = 1;
if ( getaddrinfo(*(const char **)&addr[1].sa_data[6], s, &req, &pai) < 0 )
error((int)"getaddrinfo", (int)s, v1);
v2 = pai;
memcpy(addr, pai->ai_addr, pai->ai_addrlen);
*(_DWORD *)&addr[1].sa_family = v2->ai_addrlen;
v3 = v2->ai_socktype;
v4 = socket(v2->ai_family, v3, v2->ai_protocol);
*(_DWORD *)&addr[3].sa_family = v4;
if ( v4 < 0 )
error((int)"socket", v3, v5);
freeaddrinfo(pai);
if ( connect(*(_DWORD *)&addr[3].sa_family, addr, *(_DWORD *)&addr[1].sa_family) < 0 )
error((int)"connect", (int)addr, v6);
write_to_socket(addr, "USER %s 0 0 :%s\r\n", *(const char **)&addr[2].sa_data[6], *(const char **)&addr[2].sa_data[6]);
return write_to_socket(addr, "NICK %s\r\n", *(const char **)&addr[2].sa_data[6]);
}

original_code:
void init_connection(struct IRC_CTX *ctx) {
 struct addrinfo hints;
 struct addrinfo *res;
 char port_str[5] = {'\0'};
 memset(&hints, 0, sizeof(hints));
 memset(&res, 0, sizeof(res));
 sprintf(port_str, "%d", ctx->port);
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_STREAM;
 if (getaddrinfo(ctx->servaddr_str, port_str, &hints, &res) < 0) {
  error("getaddrinfo");
 }
 memcpy(&(ctx->servaddr), res->ai_addr, res->ai_addrlen);
 ctx->servaddr_len = res->ai_addrlen;
 ctx->sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 if (ctx->sockfd < 0) {
  error("socket");
 }
 freeaddrinfo(res);
 if (connect(ctx->sockfd, &(ctx->servaddr), ctx->servaddr_len) < 0) {
  error("connect");
 }
 write_to_socket(ctx, "USER %s 0 0 :%s\r\n", ctx->nick, ctx->nick);
 write_to_socket(ctx, "NICK %s\r\n", ctx->nick);
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sockaddr *v3; // rbx

signal(2, (__sighandler_t)&sigint_handler);
signal(13, (__sighandler_t)&sigpipe_handler);
v3 = (struct sockaddr *)malloc(0x60uLL);
init_context((__int64)v3);
init_connection(v3);
return listen_server(v3);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sockaddr *v3; // rbx

signal(2, (__sighandler_t)&sigint_handler);
signal(13, (__sighandler_t)&sigpipe_handler);
v3 = (struct sockaddr *)malloc(0x60uLL);
func1((__int64)v3);
func2(v3);
return listen_server(v3);
}

original_code:
int main() {
 signal(2, sigint_handler);
 signal(13, sigpipe_handler);
 int exit_status = 0;
 struct IRC_CTX *ctx = (struct IRC_CTX *) malloc(sizeof(struct IRC_CTX));
 init_context(ctx);
 init_connection(ctx);
 exit_status = listen_server(ctx);
 return exit_status;
}


decompiled_code:
int __fastcall print_buf(char *a1, __int64 a2)
{
char *v2; // rbx
char *v3; // rbp
int result; // eax
char v5; // di

if ( a2 )
{
v2 = a1;
v3 = &a1[a2];
do
{
v5 = *v2;
if ( *v2 == 10 )
{
result = fwrite(&unk_242, 1uLL, 2uLL, stdout);
}
else if ( v5 > 10 )
{
if ( v5 != 13 )
goto LABEL_13;
result = fwrite(&unk_245, 1uLL, 2uLL, stdout);
}
else if ( v5 )
{
if ( v5 != 9 )
{
LABEL_13:
result = putchar(v5);
goto LABEL_6;
}
result = fwrite(&unk_24B, 1uLL, 2uLL, stdout);
}
else
{
result = fwrite(&unk_248, 1uLL, 2uLL, stdout);
}
LABEL_6:
++v2;
}
while ( v2 != v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *a1, __int64 a2)
{
char *v2; // rbx
char *v3; // rbp
int result; // eax
char v5; // di

if ( a2 )
{
v2 = a1;
v3 = &a1[a2];
do
{
v5 = *v2;
if ( *v2 == 10 )
{
result = fwrite(&unk_242, 1uLL, 2uLL, stdout);
}
else if ( v5 > 10 )
{
if ( v5 != 13 )
goto LABEL_13;
result = fwrite(&unk_245, 1uLL, 2uLL, stdout);
}
else if ( v5 )
{
if ( v5 != 9 )
{
LABEL_13:
result = putchar(v5);
goto LABEL_6;
}
result = fwrite(&unk_24B, 1uLL, 2uLL, stdout);
}
else
{
result = fwrite(&unk_248, 1uLL, 2uLL, stdout);
}
LABEL_6:
++v2;
}
while ( v2 != v3 );
}
return result;
}

original_code:
void print_buf(char *buf, size_t len) {
   for (int i = 0; i < len; i++) {
        switch (buf[i]) {
         case '\n':
                fputs("\\n", stdout);
                break;
         case '\r':
                fputs("\\r", stdout);
                break;
         case '\0':
                fputs("\\0", stdout);
                break;
         case '\t':
                fputs("\\t", stdout);
                break;
         default:
                putchar(buf[i]);
        }
   }
}

canonical_original_code:
void print_buf(char *buf, unsigned long len) {
   for (int i = 0; i < len; i++) {
        switch (buf[i]) {
         case '\n':
                fputs("\\n", stdout);
                break;
         case '\r':
                fputs("\\r", stdout);
                break;
         case '\0':
                fputs("\\0", stdout);
                break;
         case '\t':
                fputs("\\t", stdout);
                break;
         default:
                putchar(buf[i]);
        }
   }
}

variable_types:
buf
char *

variable_types:
len
unsigned long


original_code:
void debug_print_msg(struct IRC_MSG *msg) {
    printf("IRC_MSG: {\n\t %s: ", "_raw");
    print_buf(msg->_raw, msg->_raw_len);
    printf(",\n\t%s: %lu,\n\t",
    "_raw_len", msg->_raw_len);
    printf("IRC_SRC: {\n\t\t%s: %s,\n\t\t%s: %s,\n\t\t%s: %s,\n\t\t%s: %s\n\t},\n\t",
    "server_name", msg->src->server_name,
    "nick", msg->src->nick,
    "user", msg->src->user,
    "host", msg->src->host);
    printf("%s: %s,\n\t%s: %d,\n\t",
    "command", msg->command,
    "argc", msg->argc);
    printf("argv: [");
    for (int i = 0; i < msg->argc; i++) {
      printf("%s", msg->argv[i]);
      if (i != (msg->argc - 1))
 printf(", ");
    }
    printf("],\n\t");
    printf("%s: %s,\n\t%s: %d\n}\n",
    "text", msg->text,
    "valid", msg->valid
 );
}

variable_types: struct IRC_MSG { char *_raw; unsigned long _raw_len; struct IRC_SRC { char *server_name; char *nick; char *user; char *host; } *src; char *command; int argc; char **argv; char *text; int valid; } *


error

decompiled_code:
int __fastcall error(const char *a1)
{
return printf(a1);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
return printf(a1);
}

original_code:
void error(char *msg) {
  printf(msg);
}

canonical_original_code:
void error(char *msg) {
  printf(msg);
}


decompiled_code:
int sigint_handler()
{
return error("sigint");
}

canonical_decompiled_code:
int func3()
{
return func2("sigint");
}

original_code:
void sigint_handler(int signo) {
    error("sigint");
}

variable_types: signo int


decompiled_code:
int sigpipe_handler()
{
return error("sigpipe");
}

canonical_decompiled_code:
int func4()
{
return func2("sigpipe");
}

original_code:
void sigpipe_handler(int signo) {
    error("sigpipe");
}


success

decompiled_code:
int __fastcall success(const char *a1)
{
return printf(a1);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return printf(a1);
}

original_code:
void success(char *msg) {
  printf(msg);
}


original_code:
struct IRC_MSG *init_msg() {
    struct IRC_MSG *msg = (struct IRC_MSG *) calloc(1, sizeof(struct IRC_MSG));
    msg->_raw = ((void *)0);
    msg->_raw_len = 0;
    msg->src = (struct IRC_SRC *) calloc(1, sizeof(struct IRC_SRC));
    msg->src->server_name = ((void *)0);
    msg->src->nick = ((void *)0);
    msg->src->user = ((void *)0);
    msg->src->host = ((void *)0);
    msg->command = ((void *)0);
    msg->argc = 0;
    msg->argv = ((void *)0);
    msg->text = ((void *)0);
    msg->valid = 0;
    return msg;
}

return_type: struct IRC_MSG { char *_raw; unsigned long _raw_len; struct IRC_SRC { char *server_name; char *nick; char *user; char *host; } *src; char *command; int argc; char **argv; char *text; int valid; } *


decompiled_code:
void __fastcall free_msg(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 5));
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 5));
free(*(void **)ptr);
free(ptr);
}

original_code:
void free_msg(struct IRC_MSG *msg) {
 free(msg->src);
 free(msg->argv);
 free(msg->_raw);
 free(msg);
}


decompiled_code:
_BOOL8 __fastcall msg_is_valid(__int64 a1)
{
return *(_QWORD *)(a1 + 24) != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1)
{
return *(_QWORD *)(a1 + 24) != 0LL;
}

original_code:
int msg_is_valid(struct IRC_MSG *msg) {
    if (msg->command == ((void *)0))
      return 0;
    return 1;
}


decompiled_code:
__int64 __fastcall handle_irc_command(__int64 a1)
{
__int64 result; // rax
__int64 v2; // r8

result = a1;
v2 = *(_QWORD *)(a1 + 56);
if ( !strcmp(*(const char **)(v2 + 24), "PING") )
result = write_to_socket(a1, "PONG :%s\r\n", *(const char **)(*(_QWORD *)(v2 + 16) + 24LL));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
__int64 v2; // r8

result = a1;
v2 = *(_QWORD *)(a1 + 56);
if ( !strcmp(*(const char **)(v2 + 24), "PING") )
result = write_to_socket(a1, "PONG :%s\r\n", *(const char **)(*(_QWORD *)(v2 + 16) + 24LL));
return result;
}

original_code:
void handle_irc_command(struct IRC_CTX *ctx) {
 if (!strcmp(ctx->msg->command, "PING"))
  write_to_socket(ctx, "PONG :%s\r\n", ctx->msg->src->host);
}


decompiled_code:
char *__fastcall parse_src(__int64 a1, char *a2)
{
char **v2; // rbx
char *result; // rax
__int64 v4; // rbp
char v5; // al
__int64 v6; // rbp
__int64 v7; // rbx
__int64 v8; // rbx
char *stringp[4]; // [rsp+8h] [rbp-20h] BYREF

stringp[0] = a2;
if ( *strpbrk(a2, "! ") == 33 )
{
v4 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v4 + 8) = strsep(stringp, "!");
v5 = *strpbrk(stringp[0], "@ ");
if ( v5 == 32 )
{
v8 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v8 + 16) = strsep(stringp, " ");
}
else if ( v5 == 64 )
{
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v6 + 16) = strsep(stringp, "@");
v7 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v7 + 24) = strsep(stringp, " ");
}
result = stringp[0];
}
else
{
v2 = *(char ***)(a1 + 16);
*v2 = strsep(stringp, " ");
result = stringp[0];
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(__int64 a1, char *a2)
{
char **v2; // rbx
char *result; // rax
__int64 v4; // rbp
char v5; // al
__int64 v6; // rbp
__int64 v7; // rbx
__int64 v8; // rbx
char *stringp[4]; // [rsp+8h] [rbp-20h] BYREF

stringp[0] = a2;
if ( *strpbrk(a2, "! ") == 33 )
{
v4 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v4 + 8) = strsep(stringp, "!");
v5 = *strpbrk(stringp[0], "@ ");
if ( v5 == 32 )
{
v8 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v8 + 16) = strsep(stringp, " ");
}
else if ( v5 == 64 )
{
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v6 + 16) = strsep(stringp, "@");
v7 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(v7 + 24) = strsep(stringp, " ");
}
result = stringp[0];
}
else
{
v2 = *(char ***)(a1 + 16);
*v2 = strsep(stringp, " ");
result = stringp[0];
}
return result;
}

original_code:
char *parse_src(struct IRC_MSG *msg, char *raw_msg_ptr) {
    if (*strpbrk(raw_msg_ptr, "! ") != '!') {
 msg->src->server_name = strsep(&raw_msg_ptr, " ");
 return raw_msg_ptr;
    } else {
 msg->src->nick = strsep(&raw_msg_ptr, "!");
    }
    switch (*strpbrk(raw_msg_ptr, "@ ")) {
 case '@':
     msg->src->user = strsep(&raw_msg_ptr, "@");
     msg->src->host = strsep(&raw_msg_ptr, " ");
     break;
 case ' ':
     msg->src->user = strsep(&raw_msg_ptr, " ");
     break;
    }
    return raw_msg_ptr;
}

return_type: char *


decompiled_code:
char *__fastcall parse_command(__int64 a1, char *a2)
{
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_QWORD *)(a1 + 24) = strsep(&stringp, " ");
return stringp;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1, char *a2)
{
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_QWORD *)(a1 + 24) = strsep(&stringp, " ");
return stringp;
}

original_code:
char *parse_command(struct IRC_MSG *msg, char *raw_msg_ptr) {
    msg->command = strsep(&raw_msg_ptr, " ");
    return raw_msg_ptr;
}


decompiled_code:
char *__fastcall parse_params(__int64 a1, char *a2)
{
_QWORD *v2; // r12
char *i; // rdx
int v4; // eax
char *stringp; // [rsp+8h] [rbp-20h] BYREF

stringp = a2;
v2 = calloc(8uLL, 8uLL);
for ( i = a2; *stringp != 58; i = stringp )
{
v4 = *(_DWORD *)(a1 + 32);
if ( v4 > 7 )
break;
v2[v4] = strsep(&stringp, " ");
++*(_DWORD *)(a1 + 32);
}
*(_QWORD *)(a1 + 40) = v2;
return i;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1, char *a2)
{
_QWORD *v2; // r12
char *i; // rdx
int v4; // eax
char *stringp; // [rsp+8h] [rbp-20h] BYREF

stringp = a2;
v2 = calloc(8uLL, 8uLL);
for ( i = a2; *stringp != 58; i = stringp )
{
v4 = *(_DWORD *)(a1 + 32);
if ( v4 > 7 )
break;
v2[v4] = strsep(&stringp, " ");
++*(_DWORD *)(a1 + 32);
}
*(_QWORD *)(a1 + 40) = v2;
return i;
}

original_code:
char *parse_params(struct IRC_MSG *msg, char *raw_msg_ptr) {
    char **argv = (char **) calloc (8, sizeof(char *));
    while (*raw_msg_ptr != ':' && msg->argc < 8) {
 argv[msg->argc] = strsep(&raw_msg_ptr, " ");
 msg->argc += 1;
    }
    msg->argv = argv;
    return raw_msg_ptr;
}

variable_types:
argv
char **


decompiled_code:
char *__fastcall parse_text(__int64 a1, char *a2)
{
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_QWORD *)(a1 + 48) = strsep(&stringp, "\r\n");
return stringp;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 a1, char *a2)
{
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a2;
*(_QWORD *)(a1 + 48) = strsep(&stringp, "\r\n");
return stringp;
}

original_code:
char *parse_text(struct IRC_MSG *msg, char *raw_msg_ptr) {
    msg->text = strsep(&raw_msg_ptr, "\r\n");
    return raw_msg_ptr;
}


decompiled_code:
char **__fastcall create_message(void *src, size_t n)
{
char **v2; // rbx
char *v3; // rbp
char *v4; // rax
char *v5; // rax
char *v6; // rbp
char *v7; // rax
char *v9; // rbp
char *v10; // rax
char *s; // [rsp+8h] [rbp-20h] BYREF

v2 = (char **)init_msg();
*v2 = (char *)calloc(n + 1, 1uLL);
s = 0LL;
memcpy(*v2, src, n);
v2[1] = (char *)n;
v3 = *v2;
s = v3;
v4 = strpbrk(v3, ": ");
if ( v4 && *v4 == 58 )
s = parse_src((__int64)v2, v3 + 1);
v5 = strpbrk(s, " :");
if ( v5 )
{
if ( *v5 == 32 )
{
v2[3] = strsep(&s, " ");
v9 = s;
v10 = strpbrk(s, " :");
if ( v10 )
{
if ( *v10 == 32 )
s = parse_params((__int64)v2, v9);
}
}
}
v6 = s;
v7 = strpbrk(s, ":\r\n");
if ( v7 && *v7 == 58 )
{
s = v6 + 1;
s = parse_text((__int64)v2, v6 + 1);
}
*((_DWORD *)v2 + 14) = msg_is_valid((__int64)v2);
return v2;
}

original_code:
struct IRC_MSG *create_message(char *raw, size_t raw_len) {
    struct IRC_MSG *msg = init_msg();
    msg->_raw = calloc(raw_len + 1, sizeof(char));
    char *raw_msg_ptr = ((void *)0);
    char *delim_match = ((void *)0);
    memcpy(msg->_raw, raw, raw_len);
    msg->_raw_len = raw_len;
    raw_msg_ptr = msg->_raw;
    delim_match = strpbrk(raw_msg_ptr, ": ");
    if (delim_match != ((void *)0) && *delim_match == ':') {
 raw_msg_ptr = parse_src(msg, ++raw_msg_ptr);
    }
    delim_match = strpbrk(raw_msg_ptr, " :");
    if (delim_match != ((void *)0) && *delim_match == ' ') {
        msg->command = strsep(&raw_msg_ptr, " ");
    }
    delim_match = strpbrk(raw_msg_ptr, " :");
    if (delim_match != ((void *)0) && *delim_match == ' ') {
 raw_msg_ptr = parse_params(msg, raw_msg_ptr);
    }
    delim_match = strpbrk(raw_msg_ptr, ":\r\n");
    if (delim_match != ((void *)0) && *delim_match == ':') {
       raw_msg_ptr = parse_text(msg, ++raw_msg_ptr);
    }
    msg->valid = msg_is_valid(msg);
    return msg;
}

canonical_original_code:
struct IRC_MSG *create_message(char *raw, unsigned long raw_len) {
    struct IRC_MSG *msg = init_msg();
    msg->_raw = calloc(raw_len + 1, sizeof(char));
    char *raw_msg_ptr = ((void *)0);
    char *delim_match = ((void *)0);
    memcpy(msg->_raw, raw, raw_len);
    msg->_raw_len = raw_len;
    raw_msg_ptr = msg->_raw;
    delim_match = strpbrk(raw_msg_ptr, ": ");
    if (delim_match != ((void *)0) && *delim_match == ':') {
 raw_msg_ptr = parse_src(msg, ++raw_msg_ptr);
    }
    delim_match = strpbrk(raw_msg_ptr, " :");
    if (delim_match != ((void *)0) && *delim_match == ' ') {
        msg->command = strsep(&raw_msg_ptr, " ");
    }
    delim_match = strpbrk(raw_msg_ptr, " :");
    if (delim_match != ((void *)0) && *delim_match == ' ') {
 raw_msg_ptr = parse_params(msg, raw_msg_ptr);
    }
    delim_match = strpbrk(raw_msg_ptr, ":\r\n");
    if (delim_match != ((void *)0) && *delim_match == ':') {
       raw_msg_ptr = parse_text(msg, ++raw_msg_ptr);
    }
    msg->valid = msg_is_valid(msg);
    return msg;
}

variable_types:
raw
char *


decompiled_code:
void *__fastcall parse_read_buffer(__int64 a1, char *a2, size_t *a3)
{
char *i; // rbp
size_t v5; // rbx
char *v6; // rax
size_t v7; // rax

for ( i = a2; ; i = &i[v5 - 1] )
{
v6 = strstr(i, "\r\n");
if ( !v6 )
break;
v5 = v6 + 3 - i;
*(_QWORD *)(a1 + 56) = create_message(i, v5);
}
v7 = strlen(i);
*a3 = v7;
return memmove(a2, i, v7);
}

canonical_decompiled_code:
void *__fastcall func9(__int64 a1, char *a2, size_t *a3)
{
char *i; // rbp
size_t v5; // rbx
char *v6; // rax
size_t v7; // rax

for ( i = a2; ; i = &i[v5 - 1] )
{
v6 = strstr(i, "\r\n");
if ( !v6 )
break;
v5 = v6 + 3 - i;
*(_QWORD *)(a1 + 56) = func8(i, v5);
}
v7 = strlen(i);
*a3 = v7;
return memmove(a2, i, v7);
}

original_code:
void parse_read_buffer(struct IRC_CTX *ctx, char *read_buf, size_t *remainder_size) {
 char *msg_end_ptr = ((void *)0);
 char *read_buf_ptr = read_buf;
 size_t msg_len = 0;
 while ((msg_end_ptr = strstr(read_buf, "\r\n")) != ((void *)0)) {
  msg_len = (msg_end_ptr + sizeof("\r\n")) - read_buf;
  ctx->msg = create_message(read_buf, msg_len);
  read_buf += (msg_len - 1);
 }
 *remainder_size = strlen(read_buf);
 memmove(read_buf_ptr, read_buf, *remainder_size);
}

canonical_original_code:
void parse_read_buffer(struct IRC_CTX *ctx, char *read_buf, unsigned long *remainder_size) {
 char *msg_end_ptr = ((void *)0);
 char *read_buf_ptr = read_buf;
 unsigned long msg_len = 0;
 while ((msg_end_ptr = strstr(read_buf, "\r\n")) != ((void *)0)) {
  msg_len = (msg_end_ptr + sizeof("\r\n")) - read_buf;
  ctx->msg = create_message(read_buf, msg_len);
  read_buf += (msg_len - 1);
 }
 *remainder_size = strlen(read_buf);
 memmove(read_buf_ptr, read_buf, *remainder_size);
}


decompiled_code:
__int64 __fastcall display_message(__int64 a1)
{
return debug_print_msg(*(_QWORD *)(a1 + 56));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return debug_print_msg(*(_QWORD *)(a1 + 56));
}

original_code:
void display_message(struct IRC_CTX *ctx) {
    debug_print_msg(ctx->msg);
}


decompiled_code:
int write_to_socket(__int64 a1, const char *a2, ...)
{
size_t v2; // rax
ssize_t v3; // rax
const char *v4; // rdx
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[8]; // [rsp+20h] [rbp-4B8h] BYREF
__int64 v8; // [rsp+28h] [rbp-4B0h]
char v9[1008]; // [rsp+30h] [rbp-4A8h] BYREF
__int64 v10; // [rsp+430h] [rbp-A8h]

va_start(arg, a2);
v10 = va_arg(arg, _QWORD);
va_end(arg);
va_start(arg, a2);
*(_QWORD *)s = 0LL;
v8 = 0LL;
memset(v9, 0, sizeof(v9));
vsnprintf(s, 0x400uLL, a2, arg);
printf("<< %s", s);
v2 = strlen(s);
v3 = write(*(_DWORD *)(a1 + 48), s, v2);
if ( v3 == -1 )
{
error((int)"write", (int)s, v4);
goto LABEL_3;
}
if ( !v3 )
LABEL_3:
LODWORD(v3) = printf("wrote nothing");
return v3;
}

canonical_decompiled_code:
int func1(__int64 a1, const char *a2, ...)
{
size_t v2; // rax
ssize_t v3; // rax
const char *v4; // rdx
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[8]; // [rsp+20h] [rbp-4B8h] BYREF
__int64 v8; // [rsp+28h] [rbp-4B0h]
char v9[1008]; // [rsp+30h] [rbp-4A8h] BYREF
__int64 v10; // [rsp+430h] [rbp-A8h]

va_start(arg, a2);
v10 = va_arg(arg, _QWORD);
va_end(arg);
va_start(arg, a2);
*(_QWORD *)s = 0LL;
v8 = 0LL;
memset(v9, 0, sizeof(v9));
vsnprintf(s, 0x400uLL, a2, arg);
printf("<< %s", s);
v2 = strlen(s);
v3 = write(*(_DWORD *)(a1 + 48), s, v2);
if ( v3 == -1 )
{
error((int)"write", (int)s, v4);
goto LABEL_3;
}
if ( !v3 )
LABEL_3:
LODWORD(v3) = printf("wrote nothing");
return v3;
}

original_code:
void write_to_socket(struct IRC_CTX *ctx, char *fmt, ...) {
 ssize_t ret = 0;
 char sbuf[1024] = {'\0'};
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(sbuf, 1024, fmt, ap);
 __builtin_va_end(ap);
 printf("<< %s", sbuf);
 ret = write(ctx->sockfd, sbuf, strlen(sbuf));
 switch (ret) {
  case -1:
   error("write");
  case 0:
   printf("wrote nothing");
  default: ;
 }
}

canonical_original_code:
void write_to_socket(struct IRC_CTX *ctx, char *fmt, ...) {
 long ret = 0;
 char sbuf[1024] = {'\0'};
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(sbuf, 1024, fmt, ap);
 __builtin_va_end(ap);
 printf("<< %s", sbuf);
 ret = write(ctx->sockfd, sbuf, strlen(sbuf));
 switch (ret) {
  case -1:
   error("write");
  case 0:
   printf("wrote nothing");
  default: ;
 }
}

variable_types:
fmt
char *

variable_types: ret long

variable_types:
sbuf
char [1024]


decompiled_code:
__int64 __fastcall listen_server(__int64 a1)
{
__int64 v1; // rbp
char *v2; // r12
ssize_t v3; // rax
char s[1032]; // [rsp+0h] [rbp-428h] BYREF
__int64 v6[4]; // [rsp+408h] [rbp-20h] BYREF

v6[0] = 0LL;
while ( 1 )
{
v1 = v6[0];
v2 = &s[v6[0]];
memset(&s[v6[0]], 0, 1024 - v6[0]);
v3 = read(*(_DWORD *)(a1 + 48), v2, 1023 - v1);
if ( v3 == -1 )
break;
if ( !v3 )
{
printf("Finished reading.");
free_context(a1);
return 0LL;
}
while ( 1 )
{
parse_read_buffer(a1, s, v6);
if ( *(_DWORD *)(*(_QWORD *)(a1 + 56) + 56LL) )
break;
free_msg();
}
handle_irc_command(a1);
display_message(a1);
}
close(*(_DWORD *)(a1 + 48));
perror("Read error");
free_context(a1);
return (unsigned int)*_errno_location();
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbp
char *v2; // r12
ssize_t v3; // rax
char s[1032]; // [rsp+0h] [rbp-428h] BYREF
__int64 v6[4]; // [rsp+408h] [rbp-20h] BYREF

v6[0] = 0LL;
while ( 1 )
{
v1 = v6[0];
v2 = &s[v6[0]];
memset(&s[v6[0]], 0, 1024 - v6[0]);
v3 = read(*(_DWORD *)(a1 + 48), v2, 1023 - v1);
if ( v3 == -1 )
break;
if ( !v3 )
{
printf("Finished reading.");
free_context(a1);
return 0LL;
}
while ( 1 )
{
parse_read_buffer(a1, s, v6);
if ( *(_DWORD *)(*(_QWORD *)(a1 + 56) + 56LL) )
break;
free_msg();
}
handle_irc_command(a1);
func0(a1);
}
close(*(_DWORD *)(a1 + 48));
perror("Read error");
free_context(a1);
return (unsigned int)*_errno_location();
}

original_code:
int listen_server(struct IRC_CTX *ctx) {
 ssize_t ret = 0;
 size_t offset = 0;
 char read_buf[1024];
 int return_value = 0;
 int checked_valid = 0;
 for (;;) {
  memset(read_buf + offset, 0, 1024 - offset);
  ret = read(ctx->sockfd, read_buf + offset,
    (1024 - 1) - offset);
  checked_valid = 0;
  switch (ret) {
   case -1:
    close(ctx->sockfd);
    perror("Read error");
    goto cleanup;
    break;
   case 0:
    printf("Finished reading.");
    goto cleanup;
    break;
   default:
    while (!checked_valid) {
        parse_read_buffer(ctx, read_buf, &offset);
        if (!ctx->msg->valid)
     free_msg(ctx->msg);
        else
     checked_valid = 1;
    }
    handle_irc_command(ctx);
    display_message(ctx);
    break;
  }
 }
cleanup:
 free_context(ctx);
 return_value = ret ? (*__errno_location ()) : ret;
 return return_value;
}

canonical_original_code:
int listen_server(struct IRC_CTX *ctx) {
 long ret = 0;
 unsigned long offset = 0;
 char read_buf[1024];
 int return_value = 0;
 int checked_valid = 0;
 for (;;) {
  memset(read_buf + offset, 0, 1024 - offset);
  ret = read(ctx->sockfd, read_buf + offset,
    (1024 - 1) - offset);
  checked_valid = 0;
  switch (ret) {
   case -1:
    close(ctx->sockfd);
    perror("Read error");
    goto cleanup;
    break;
   case 0:
    printf("Finished reading.");
    goto cleanup;
    break;
   default:
    while (!checked_valid) {
        parse_read_buffer(ctx, read_buf, &offset);
        if (!ctx->msg->valid)
     free_msg(ctx->msg);
        else
     checked_valid = 1;
    }
    handle_irc_command(ctx);
    display_message(ctx);
    break;
  }
 }
cleanup:
 free_context(ctx);
 return_value = ret ? (*__errno_location ()) : ret;
 return return_value;
}

variable_types:
offset
unsigned long


repo: cohenadair/assignments

decompiled_code:
__pid_t sigchld_handler()
{
__pid_t result; // eax

do
result = waitpid(-1, 0LL, 1);
while ( result > 0 );
return result;
}

canonical_decompiled_code:
__pid_t func0()
{
__pid_t result; // eax

do
result = waitpid(-1, 0LL, 1);
while ( result > 0 );
return result;
}

original_code:
void sigchld_handler(int s)
{
 while(waitpid(-1, ((void *)0), 1) > 0);
}

variable_types: s int


handleOptions

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

if ( a1 != 2 )
{
fprintf(stderr, "usage: %s <port number>\n", *a2);
exit(-1);
}
result = atoi(a2[1]);
*a3 = result;
return result;
}

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

if ( a1 != 2 )
{
fprintf(stderr, "usage: %s <port number>\n", *a2);
exit(-1);
}
result = atoi(a2[1]);
*a3 = result;
return result;
}

original_code:
void handleOptions( int argc, char *argv[], int *portnoPtr )
{
     if (argc != 2){
     fprintf(stderr, "usage: %s <port number>\n", argv[0]);
     exit(-1);
 }
 *portnoPtr = atoi(argv[1]);
}

canonical_original_code:
void handleOptions(int argc, char *argv[], int *portnoPtr)
{
     if (argc != 2){
     fprintf(stderr, "usage: %s <port number>\n", argv[0]);
     exit(-1);
 }
 *portnoPtr = atoi(argv[1]);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
portnoPtr
int *


handleFortuneRequest

decompiled_code:
__int64 __fastcall handleFortuneRequest(__int64 a1)
{
int v2; // eax
unsigned __int8 v4; // [rsp+Eh] [rbp-Ah] BYREF
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v5[0] = 2;
v4 = strlen(fortunes[fortuneIndex % 3]);
(*(void (__fastcall **)(__int64, char *, __int64))(a1 + 16))(a1, v5, 1LL);
v4 = strlen(fortunes[fortuneIndex % 3]);
(*(void (__fastcall **)(__int64, unsigned __int8 *, __int64))(a1 + 16))(a1, &v4, 1LL);
v2 = fortuneIndex++;
(*(void (__fastcall **)(__int64, char *, _QWORD))(a1 + 16))(a1, fortunes[v2 % 3], v4);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v2; // eax
unsigned __int8 v4; // [rsp+Eh] [rbp-Ah] BYREF
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v5[0] = 2;
v4 = strlen(fortunes[fortuneIndex % 3]);
(*(void (__fastcall **)(__int64, char *, __int64))(a1 + 16))(a1, v5, 1LL);
v4 = strlen(fortunes[fortuneIndex % 3]);
(*(void (__fastcall **)(__int64, unsigned __int8 *, __int64))(a1 + 16))(a1, &v4, 1LL);
v2 = fortuneIndex++;
(*(void (__fastcall **)(__int64, char *, _QWORD))(a1 + 16))(a1, fortunes[v2 % 3], v4);
return 1LL;
}

original_code:
int handleFortuneRequest(nmu_socket_t serviceSocket)
{
 uint8_t fortuneReply = 2;
 uint8_t count = (uint8_t)strlen(fortunes[fortuneIndex % 3]);
 serviceSocket->write(serviceSocket, &fortuneReply, sizeof(uint8_t));
 count = strlen(fortunes[fortuneIndex % 3]);
 serviceSocket->write(serviceSocket, &count, sizeof(uint8_t));
 serviceSocket->write(serviceSocket, fortunes[fortuneIndex++ % 3], count);
 return 1;
}

canonical_original_code:
int handleFortuneRequest(struct NMUSocket *serviceSocket)
{
 unsigned char fortuneReply = 2;
 unsigned char count = (uint8_t)strlen(fortunes[fortuneIndex % 3]);
 serviceSocket->write(serviceSocket, &fortuneReply, sizeof(unsigned char));
 count = strlen(fortunes[fortuneIndex % 3]);
 serviceSocket->write(serviceSocket, &count, sizeof(unsigned char));
 serviceSocket->write(serviceSocket, fortunes[fortuneIndex++ % 3], count);
 return 1;
}

variable_types: struct NMUSocket { struct NMUSocket *(*accept)(struct NMUSocket *); int (*read)(struct NMUSocket *, void *, int); int (*write)(struct NMUSocket *, void *, int); void (*close)(struct NMUSocket *); void (*error)(struct NMUSocket *, char *); 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]; } sockAddr; int sockfd; } *

variable_types:
fortuneReply
unsigned char

variable_types:
count
unsigned char

user_defined_types: struct NMUSocket { struct NMUSocket *(*accept)(struct NMUSocket *); int (*read)(struct NMUSocket *, void *, int); int (*write)(struct NMUSocket *, void *, int); void (*close)(struct NMUSocket *); void (*error)(struct NMUSocket *, char *); struct sockaddr_in sockAddr; int sockfd; }

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


handleFortuneQuit

decompiled_code:
__int64 __fastcall handleFortuneQuit(__int64 a1)
{
(*(void (**)(void))(a1 + 24))();
return 2LL;
}

original_code:
int handleFortuneQuit(nmu_socket_t serviceSocket)
{
 serviceSocket->close(serviceSocket);
 return 2;
}

canonical_original_code:
int handleFortuneQuit(struct NMUSocket *serviceSocket)
{
 serviceSocket->close(serviceSocket);
 return 2;
}


handleFortuneServerProtocol

decompiled_code:
void __fastcall handleFortuneServerProtocol(__int64 a1)
{
char v1[9]; // [rsp+Fh] [rbp-9h] BYREF

state = 1;
while ( state != 2 )
{
(*(void (__fastcall **)(__int64, char *, __int64))(a1 + 8))(a1, v1, 1LL);
if ( v1[0] == 1 )
{
state = handleFortuneRequest(a1);
}
else if ( v1[0] == 3 )
{
state = handleFortuneQuit(a1);
}
else if ( v1[0] )
{
(*(void (__fastcall **)(__int64, const char *))(a1 + 32))(a1, "ERROR, protocol error");
}
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
char v1[9]; // [rsp+Fh] [rbp-9h] BYREF

state = 1;
while ( state != 2 )
{
(*(void (__fastcall **)(__int64, char *, __int64))(a1 + 8))(a1, v1, 1LL);
if ( v1[0] == 1 )
{
state = func2(a1);
}
else if ( v1[0] == 3 )
{
state = func3(a1);
}
else if ( v1[0] )
{
(*(void (__fastcall **)(__int64, const char *))(a1 + 32))(a1, "ERROR, protocol error");
}
}
}

original_code:
void handleFortuneServerProtocol(nmu_socket_t serviceSocket)
{
 uint8_t pduId;
 state = 1;
 while( state != 2 ){
  serviceSocket->read(serviceSocket, &pduId, sizeof(uint8_t));
  switch( pduId ){
  case 1:
   state = handleFortuneRequest(serviceSocket);
   break;
  case 3:
   state = handleFortuneQuit(serviceSocket);
   break;
  case 0:
   break;
  default:
   serviceSocket->error(serviceSocket, "ERROR, protocol error" );
  }
 }
}

canonical_original_code:
void handleFortuneServerProtocol(struct NMUSocket *serviceSocket)
{
 unsigned char pduId;
 state = 1;
 while( state != 2 ){
  serviceSocket->read(serviceSocket, &pduId, sizeof(unsigned char));
  switch( pduId ){
  case 1:
   state = handleFortuneRequest(serviceSocket);
   break;
  case 3:
   state = handleFortuneQuit(serviceSocket);
   break;
  case 0:
   break;
  default:
   serviceSocket->error(serviceSocket, "ERROR, protocol error" );
  }
 }
}

variable_types:
pduId
unsigned char


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
__int64 v4; // rbx
__pid_t v5; // eax
struct sigaction v6; // [rsp+0h] [rbp-B8h] BYREF
int v7[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

handleOptions(argc, argv, v7);
((void (*)(void))socketFactory[2])();
v6.sa_handler = (__sighandler_t)sigchld_handler;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0x10000000;
if ( sigaction(17, &v6, 0LL) != -1 )
{
v3 = ((__int64 (__fastcall **)(const char *, _QWORD))socketFactory)[0]("localhost", (unsigned int)SLOWORD(v7[0]));
while ( 1 )
{
v4 = (*(__int64 (__fastcall **)(__int64))v3)(v3);
v5 = fork();
if ( v5 < 0 )
{
fwrite("Could not fork protocol handler.\n", 1uLL, 0x21uLL, stderr);
(*(void (__fastcall **)(__int64))(v4 + 24))(v4);
}
else if ( !v5 )
{
(*(void (__fastcall **)(__int64))(v3 + 24))(v3);
handleFortuneServerProtocol(v4);
exit(0);
}
(*(void (__fastcall **)(__int64))(v4 + 24))(v4);
}
}
fwrite("Could not set SIGCHLD handler.\n", 1uLL, 0x1FuLL, stderr);
exit(-1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
__int64 v4; // rbx
__pid_t v5; // eax
struct sigaction v6; // [rsp+0h] [rbp-B8h] BYREF
int v7[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

func1(argc, argv, v7);
((void (*)(void))socketFactory[2])();
v6.sa_handler = (__sighandler_t)sigchld_handler;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0x10000000;
if ( sigaction(17, &v6, 0LL) != -1 )
{
v3 = ((__int64 (__fastcall **)(const char *, _QWORD))socketFactory)[0]("localhost", (unsigned int)SLOWORD(v7[0]));
while ( 1 )
{
v4 = (*(__int64 (__fastcall **)(__int64))v3)(v3);
v5 = fork();
if ( v5 < 0 )
{
fwrite("Could not fork protocol handler.\n", 1uLL, 0x21uLL, stderr);
(*(void (__fastcall **)(__int64))(v4 + 24))(v4);
}
else if ( !v5 )
{
(*(void (__fastcall **)(__int64))(v3 + 24))(v3);
func4(v4);
exit(0);
}
(*(void (__fastcall **)(__int64))(v4 + 24))(v4);
}
}
fwrite("Could not set SIGCHLD handler.\n", 1uLL, 0x1FuLL, stderr);
exit(-1);
}

original_code:
int main(int argc, char *argv[])
{
 int port;
 _Bool notDone = 1;
 nmu_socket_t serverSocket;
 struct sigaction sigact;
 handleOptions( argc, argv, &port );
 socketFactory.initialize();
 sigact.__sigaction_handler.sa_handler = sigchld_handler;
 sigemptyset(&sigact.sa_mask);
 sigact.sa_flags = 0x10000000;
 if( sigaction(17, &sigact, ((void *)0)) == -1){
  fprintf(stderr, "Could not set SIGCHLD handler.\n");
  exit(-1);
 }
 serverSocket = socketFactory.serveTCPForHostnameAndPort( "localhost", port );
 while( notDone ) {
  pid_t childPid;
  nmu_socket_t serviceSocket;
  serviceSocket = serverSocket->accept(serverSocket);
  childPid = fork();
  if(childPid < 0){
   fprintf( stderr, "Could not fork protocol handler.\n" );
   serviceSocket->close(serviceSocket);
  }
  if(childPid){
   serviceSocket->close(serviceSocket);
  }
  else {
   serverSocket->close(serverSocket);
   handleFortuneServerProtocol(serviceSocket);
   exit(0);
  }
 }
 serverSocket->close(serverSocket);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int port;
 _Bool notDone = 1;
 struct NMUSocket *serverSocket;
 struct sigaction sigact;
 handleOptions( argc, argv, &port );
 socketFactory.initialize();
 sigact.__sigaction_handler.sa_handler = sigchld_handler;
 sigemptyset(&sigact.sa_mask);
 sigact.sa_flags = 0x10000000;
 if( sigaction(17, &sigact, ((void *)0)) == -1){
  fprintf(stderr, "Could not set SIGCHLD handler.\n");
  exit(-1);
 }
 serverSocket = socketFactory.serveTCPForHostnameAndPort( "localhost", port );
 while( notDone ) {
  int childPid;
  struct NMUSocket *serviceSocket;
  serviceSocket = serverSocket->accept(serverSocket);
  childPid = fork();
  if(childPid < 0){
   fprintf( stderr, "Could not fork protocol handler.\n" );
   serviceSocket->close(serviceSocket);
  }
  if(childPid){
   serviceSocket->close(serviceSocket);
  }
  else {
   serverSocket->close(serverSocket);
   handleFortuneServerProtocol(serviceSocket);
   exit(0);
  }
 }
 serverSocket->close(serverSocket);
 return 0;
}

variable_types: port int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: childPid int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


handleError

decompiled_code:
void __fastcall __noreturn handleError(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(-1);
}

original_code:
void handleError(char *msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(-1);
}

variable_types:
msg
char *


connectToServer

decompiled_code:
__int64 __fastcall connectToServer(const char *a1, const char *a2)
{
int v2; // ebp
struct addrinfo *v3; // rbx
int v4; // eax
struct addrinfo v6; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&v6.ai_flags = 0LL;
*(_QWORD *)&v6.ai_addrlen = 0LL;
v6.ai_addr = 0LL;
v6.ai_canonname = 0LL;
v6.ai_next = 0LL;
v6.ai_socktype = 1;
v6.ai_protocol = 6;
if ( getaddrinfo(a1, a2, &v6, &pai) )
handleError("ERROR getaddrinfo failed");
v3 = pai;
if ( !pai )
{
LABEL_10:
freeaddrinfo(pai);
if ( v2 >= 0 )
handleError("ERROR connecting to server");
LABEL_11:
handleError("ERROR creating socket");
}
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v2 = v4;
if ( v4 != -1 )
break;
LABEL_9:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_10;
}
if ( connect(v4, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v2);
goto LABEL_9;
}
freeaddrinfo(pai);
if ( v2 < 0 )
goto LABEL_11;
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1, const char *a2)
{
int v2; // ebp
struct addrinfo *v3; // rbx
int v4; // eax
struct addrinfo v6; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&v6.ai_flags = 0LL;
*(_QWORD *)&v6.ai_addrlen = 0LL;
v6.ai_addr = 0LL;
v6.ai_canonname = 0LL;
v6.ai_next = 0LL;
v6.ai_socktype = 1;
v6.ai_protocol = 6;
if ( getaddrinfo(a1, a2, &v6, &pai) )
func7("ERROR getaddrinfo failed");
v3 = pai;
if ( !pai )
{
LABEL_10:
freeaddrinfo(pai);
if ( v2 >= 0 )
func7("ERROR connecting to server");
LABEL_11:
func7("ERROR creating socket");
}
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v2 = v4;
if ( v4 != -1 )
break;
LABEL_9:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_10;
}
if ( connect(v4, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v2);
goto LABEL_9;
}
freeaddrinfo(pai);
if ( v2 < 0 )
goto LABEL_11;
return (unsigned int)v2;
}

original_code:
int connectToServer(char *hostname, char *port)
{
 struct addrinfo *result = ((void *)0);
 struct addrinfo *next = ((void *)0);
 struct addrinfo hints;
 int sockfd;
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 if (getaddrinfo(hostname, port, &hints, &result) != 0)
  handleError("ERROR getaddrinfo failed");
 for(next = result; next != ((void *)0); next = next->ai_next) {
  sockfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
  if( sockfd == -1 )
   continue;
  if(connect(sockfd, next->ai_addr, next->ai_addrlen) == -1){
   close(sockfd);
   continue;
  }
  break;
 }
 freeaddrinfo(result);
 if (sockfd < 0)
  handleError("ERROR creating socket");
 if (next == ((void *)0))
  handleError("ERROR connecting to server");
 return sockfd;
}

variable_types:
hostname
char *

variable_types:
port
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: sockfd int


handleReply

decompiled_code:
__int64 __fastcall handleReply(int fd)
{
__int64 result; // rax
char v2[271]; // [rsp+0h] [rbp-118h] BYREF
char buf[9]; // [rsp+10Fh] [rbp-9h] BYREF

memset(v2, 0, 0x100uLL);
if ( read(fd, buf, 1uLL) == -1 )
{
perror("Error reading count");
result = 0LL;
}
else if ( read(fd, v2, (unsigned __int8)buf[0]) == -1 )
{
perror("Error reading fortune");
result = 0LL;
}
else
{
printf("%s", v2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
__int64 result; // rax
char v2[271]; // [rsp+0h] [rbp-118h] BYREF
char buf[9]; // [rsp+10Fh] [rbp-9h] BYREF

memset(v2, 0, 0x100uLL);
if ( read(fd, buf, 1uLL) == -1 )
{
perror("Error reading count");
result = 0LL;
}
else if ( read(fd, v2, (unsigned __int8)buf[0]) == -1 )
{
perror("Error reading fortune");
result = 0LL;
}
else
{
printf("%s", v2);
result = 1LL;
}
return result;
}

original_code:
_Bool handleReply(int sockfd)
{
 uint8_t count;
 char buffer[256];
 memset(buffer, 0, 256);
 if (read(sockfd, &count, sizeof(uint8_t)) == -1) {
  perror("Error reading count");
  return 0;
 }
 if (read(sockfd, buffer, count) == -1) {
  perror("Error reading fortune");
  return 0;
 }
 printf("%s", buffer);
 return 1;
}

canonical_original_code:
_Bool handleReply(int sockfd)
{
 unsigned char count;
 char buffer[256];
 memset(buffer, 0, 256);
 if (read(sockfd, &count, sizeof(unsigned char)) == -1) {
  perror("Error reading count");
  return 0;
 }
 if (read(sockfd, buffer, count) == -1) {
  perror("Error reading fortune");
  return 0;
 }
 printf("%s", buffer);
 return 1;
}

variable_types:
buffer
char [256]


handleFortuneProtocol

decompiled_code:
int __fastcall handleFortuneProtocol(int fd)
{
int v1; // ebp
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( killServer )
{
buf[0] = 3;
if ( write(fd, buf, 1uLL) == -1 )
perror("Error writing FORTUNE_KILL");
close(fd);
handleError("Killed server.");
}
v1 = 9;
while ( 1 )
{
buf[0] = 0;
if ( write(fd, buf, 1uLL) == -1 )
{
perror("Error writing pduId");
goto LABEL_8;
}
if ( read(fd, buf, 1uLL) == -1 )
break;
if ( buf[0] != 1 )
{
close(fd);
handleError("Protocol violation");
}
if ( (unsigned __int8)handleReply(fd) )
{
if ( --v1 )
continue;
}
goto LABEL_8;
}
perror("Error reading pduId");
LABEL_8:
buf[0] = 2;
write(fd, buf, 1uLL);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func11(int fd)
{
int v1; // ebp
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( killServer )
{
buf[0] = 3;
if ( write(fd, buf, 1uLL) == -1 )
perror("Error writing FORTUNE_KILL");
close(fd);
func7("Killed server.");
}
v1 = 9;
while ( 1 )
{
buf[0] = 0;
if ( write(fd, buf, 1uLL) == -1 )
{
perror("Error writing pduId");
goto LABEL_8;
}
if ( read(fd, buf, 1uLL) == -1 )
break;
if ( buf[0] != 1 )
{
close(fd);
func7("Protocol violation");
}
if ( (unsigned __int8)func10(fd) )
{
if ( --v1 )
continue;
}
goto LABEL_8;
}
perror("Error reading pduId");
LABEL_8:
buf[0] = 2;
write(fd, buf, 1uLL);
return close(fd);
}

original_code:
void handleFortuneProtocol(int sockfd)
{
 uint8_t pduId;
 int fortuneCount;
 if (killServer) {
  pduId = 3;
  if (write(sockfd, &pduId, sizeof(uint8_t)) == -1)
   perror("Error writing FORTUNE_KILL");
  close(sockfd);
  handleError("Killed server.");
 }
 for (fortuneCount = 1; fortuneCount <= 9; fortuneCount++) {
  pduId = 0;
  if (write(sockfd, &pduId, sizeof(uint8_t)) == -1) {
   perror("Error writing pduId");
   break;
  }
  if (read(sockfd, &pduId, sizeof(uint8_t)) == -1) {
   perror("Error reading pduId");
   break;
  }
  if (pduId == 1) {
   if (!handleReply(sockfd))
    break;
  } else {
   close(sockfd);
   handleError("Protocol violation");
  }
 }
 pduId = 2;
 write(sockfd, &pduId, sizeof(uint8_t));
 close(sockfd);
}

canonical_original_code:
void handleFortuneProtocol(int sockfd)
{
 unsigned char pduId;
 int fortuneCount;
 if (killServer) {
  pduId = 3;
  if (write(sockfd, &pduId, sizeof(unsigned char)) == -1)
   perror("Error writing FORTUNE_KILL");
  close(sockfd);
  handleError("Killed server.");
 }
 for (fortuneCount = 1; fortuneCount <= 9; fortuneCount++) {
  pduId = 0;
  if (write(sockfd, &pduId, sizeof(unsigned char)) == -1) {
   perror("Error writing pduId");
   break;
  }
  if (read(sockfd, &pduId, sizeof(unsigned char)) == -1) {
   perror("Error reading pduId");
   break;
  }
  if (pduId == 1) {
   if (!handleReply(sockfd))
    break;
  } else {
   close(sockfd);
   handleError("Protocol violation");
  }
 }
 pduId = 2;
 write(sockfd, &pduId, sizeof(unsigned char));
 close(sockfd);
}

variable_types: fortuneCount int


handleFortuneError

decompiled_code:
__int64 __fastcall handleFortuneError(__int64 a1)
{
(*(void (__fastcall **)(__int64, const char *))(a1 + 40))(a1, "fortuneServer: ERROR protocolError\n");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
(*(void (__fastcall **)(__int64, const char *))(a1 + 40))(a1, "fortuneServer: ERROR protocolError\n");
return 1LL;
}

original_code:
int handleFortuneError(nmu_socket_t serviceSocket)
{
 serviceSocket->error(serviceSocket, "fortuneServer: ERROR protocolError\n");
 return 1;
}

canonical_original_code:
int handleFortuneError(struct NMUSocket *serviceSocket)
{
 serviceSocket->error(serviceSocket, "fortuneServer: ERROR protocolError\n");
 return 1;
}

variable_types: struct NMUSocket { struct NMUSocket *(*accept)(struct NMUSocket *); void (*acceptAndServe)(struct NMUSocket *, void *(*)(struct NMUSocket *)); int (*read)(struct NMUSocket *, void *, int); int (*write)(struct NMUSocket *, void *, int); void (*close)(struct NMUSocket *); void (*error)(struct NMUSocket *, char *); 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]; } sockAddr; int sockfd; } *

user_defined_types: struct NMUSocket { struct NMUSocket *(*accept)(struct NMUSocket *); void (*acceptAndServe)(struct NMUSocket *, void *(*)(struct NMUSocket *)); int (*read)(struct NMUSocket *, void *, int); int (*write)(struct NMUSocket *, void *, int); void (*close)(struct NMUSocket *); void (*error)(struct NMUSocket *, char *); struct sockaddr_in sockAddr; int sockfd; }


handleProtocolError

decompiled_code:
void __fastcall __noreturn handleProtocolError(__int64 a1)
{
(*(void (**)(void))(a1 + 32))();
fwrite("Protocol violation.\n", 1uLL, 0x14uLL, stderr);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1)
{
(*(void (**)(void))(a1 + 32))();
fwrite("Protocol violation.\n", 1uLL, 0x14uLL, stderr);
exit(-1);
}

original_code:
int handleProtocolError(nmu_socket_t pSocket)
{
 pSocket->close(pSocket);
 fprintf(stderr, "Protocol violation.\n");
 exit(-1);
 return 1;
}

canonical_original_code:
int handleProtocolError(struct NMUSocket *pSocket)
{
 pSocket->close(pSocket);
 fprintf(stderr, "Protocol violation.\n");
 exit(-1);
 return 1;
}


shouldQuit

decompiled_code:
bool shouldQuit()
{
int v0; // eax

v0 = fortuneCount_0++;
return v0 > 8;
}

canonical_decompiled_code:
bool func2()
{
int v0; // eax

v0 = fortuneCount_0++;
return v0 > 8;
}

original_code:
_Bool shouldQuit(void)
{
 static int fortuneCount = 0;
 return (fortuneCount++ >= 9);
}

canonical_original_code:
_Bool shouldQuit()
{
 static int fortuneCount = 0;
 return (fortuneCount++ >= 9);
}


wstrlen

decompiled_code:
__int64 __fastcall wstrlen(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

original_code:
static inline int wstrlen(const wstr s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

canonical_original_code:
static inline int wstrlen(char *s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

variable_types:
s
char *

variable_types: struct wstrhd { int len; int free; char buf[]; } *


extraValidator

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

result = Server[271];
if ( (int)Server[275] >= (int)result )
result = wheat_assert("Server.stat_refresh_seconds < Server.worker_timeout", "config.c", 211LL, 1LL);
if ( !Server[2] || !Server[3] || !Server[274] )
result = wheat_assert(
"Server.port_range_start && Server.port_range_end && Server.stat_port",
"config.c",
212LL,
1LL);
return result;
}

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

result = Server[271];
if ( (int)Server[275] >= (int)result )
result = wheat_assert("Server.stat_refresh_seconds < Server.worker_timeout", "config.c", 211LL, 1LL);
if ( !Server[2] || !Server[3] || !Server[274] )
result = wheat_assert(
"Server.port_range_start && Server.port_range_end && Server.stat_port",
"config.c",
212LL,
1LL);
return result;
}

original_code:
static void extraValidator()
{
    do { if (!(Server.stat_refresh_seconds < Server.worker_timeout)) { wheat_assert("Server.stat_refresh_seconds < Server.worker_timeout", "config.c", 211, 1); } } while (0);
    do { if (!(Server.port_range_start && Server.port_range_end && Server.stat_port)) { wheat_assert("Server.port_range_start && Server.port_range_end && Server.stat_port", "config.c", 212, 1); } } while (0);
}


stringValidator

decompiled_code:
__int64 __fastcall stringValidator(__int64 a1, __int64 a2, const char *a3)
{
if ( !a3 )
wheat_assert("val", "config.c", 125LL, 1LL);
if ( *(_QWORD *)(a1 + 24) && !*(_QWORD *)(a1 + 32) )
{
wfree();
*(_QWORD *)(a1 + 32) = 0LL;
}
if ( !strncasecmp(a3, "NULL", 5uLL) )
*(_QWORD *)(a1 + 24) = 0LL;
else
*(_QWORD *)(a1 + 24) = strdup(a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, const char *a3)
{
if ( !a3 )
wheat_assert("val", "config.c", 125LL, 1LL);
if ( *(_QWORD *)(a1 + 24) && !*(_QWORD *)(a1 + 32) )
{
wfree();
*(_QWORD *)(a1 + 32) = 0LL;
}
if ( !strncasecmp(a3, "NULL", 5uLL) )
*(_QWORD *)(a1 + 24) = 0LL;
else
*(_QWORD *)(a1 + 24) = strdup(a3);
return 0LL;
}

original_code:
int stringValidator(struct configuration *conf, const char *key, const char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 125, 1); } } while (0);
    if (conf->target.ptr && !conf->helper) {
        wfree(conf->target.ptr);
        conf->helper = ((void *)0);
    }
    if (!strncasecmp(val, "NULL", sizeof("NULL")))
        conf->target.ptr = ((void *)0);
    else
        conf->target.ptr = strdup(val);
    return 0;
}

canonical_original_code:
int stringValidator(struct configuration *conf, char *key, char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 125, 1); } } while (0);
    if (conf->target.ptr && !conf->helper) {
        wfree(conf->target.ptr);
        conf->helper = ((void *)0);
    }
    if (!strncasecmp(val, "NULL", sizeof("NULL")))
        conf->target.ptr = ((void *)0);
    else
        conf->target.ptr = strdup(val);
    return 0;
}

variable_types: struct configuration { char *name; int args; int (*validator)(struct configuration *conf, char *key, char *value); union { int val; void *ptr; struct enumIdName { int id; char *name; } *enum_ptr; } target; void *helper; enum printFormat { INT_FORMAT=0, STRING_FORMAT=1, ENUM_FORMAT=2, BOOL_FORMAT=3, LIST_FORMAT=4 } format; } *

variable_types:
key
char *

variable_types:
val
char *

user_defined_types: struct configuration { char *name; int args; int (*validator)(struct configuration *conf, char *key, char *value); union { int val; void *ptr; struct enumIdName *enum_ptr; } target; void *helper; enum printFormat format; }


unsignedIntValidator

decompiled_code:
__int64 __fastcall unsignedIntValidator(__int64 a1, __int64 a2, const char *a3)
{
__int64 v4; // r12
char v5; // al
__int64 v6; // rdx
char v7; // al
bool v8; // cl
bool v9; // al
__int64 result; // rax
int v11; // edx

if ( !a3 )
wheat_assert("val", "config.c", 154LL, 1LL);
v4 = *(_QWORD *)(a1 + 32);
if ( v4 )
{
v5 = *a3;
if ( !*a3 )
{
v11 = atoi(a3);
goto LABEL_14;
}
}
else
{
v5 = *a3;
v4 = 0LL;
if ( !*a3 )
{
v11 = atoi(a3);
goto LABEL_11;
}
}
if ( (unsigned int)(v5 - 48) > 9 )
return 1LL;
v6 = 1LL;
while ( 1 )
{
v7 = a3[v6];
if ( !v7 )
break;
v8 = (unsigned int)(v7 - 48) > 9;
v9 = (int)v6++ > 9;
if ( v9 || v8 )
return 1LL;
}
v11 = atoi(a3);
if ( !v4 )
goto LABEL_11;
LABEL_14:
result = 1LL;
if ( v11 <= v4 )
{
LABEL_11:
*(_DWORD *)(a1 + 24) = v11;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, const char *a3)
{
__int64 v4; // r12
char v5; // al
__int64 v6; // rdx
char v7; // al
bool v8; // cl
bool v9; // al
__int64 result; // rax
int v11; // edx

if ( !a3 )
wheat_assert("val", "config.c", 154LL, 1LL);
v4 = *(_QWORD *)(a1 + 32);
if ( v4 )
{
v5 = *a3;
if ( !*a3 )
{
v11 = atoi(a3);
goto LABEL_14;
}
}
else
{
v5 = *a3;
v4 = 0LL;
if ( !*a3 )
{
v11 = atoi(a3);
goto LABEL_11;
}
}
if ( (unsigned int)(v5 - 48) > 9 )
return 1LL;
v6 = 1LL;
while ( 1 )
{
v7 = a3[v6];
if ( !v7 )
break;
v8 = (unsigned int)(v7 - 48) > 9;
v9 = (int)v6++ > 9;
if ( v9 || v8 )
return 1LL;
}
v11 = atoi(a3);
if ( !v4 )
goto LABEL_11;
LABEL_14:
result = 1LL;
if ( v11 <= v4 )
{
LABEL_11:
*(_DWORD *)(a1 + 24) = v11;
result = 0LL;
}
return result;
}

original_code:
int unsignedIntValidator(struct configuration *conf, const char *key, const char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 154, 1); } } while (0);
    int i, digit, value;
    intptr_t max_limit = 0;
    if (conf->helper)
        max_limit = (intptr_t)conf->helper;
    for (i = 0; val[i] != '\0'; i++) {
        digit = val[i] - 48;
        if (digit < 0 || digit > 9)
            return 1;
        if (i >= 10)
            return 1;
    }
    value = atoi(val);
    if (max_limit != 0 && value > max_limit)
        return 1;
    conf->target.val = value;
    return 0;
}

canonical_original_code:
int unsignedIntValidator(struct configuration *conf, char *key, char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 154, 1); } } while (0);
    int i; int digit; int value;
    long max_limit = 0;
    if (conf->helper)
        max_limit = (long)conf->helper;
    for (i = 0; val[i] != '\0'; i++) {
        digit = val[i] - 48;
        if (digit < 0 || digit > 9)
            return 1;
        if (i >= 10)
            return 1;
    }
    value = atoi(val);
    if (max_limit != 0 && value > max_limit)
        return 1;
    conf->target.val = value;
    return 0;
}

variable_types: i int

variable_types: digit int

variable_types: value int

user_defined_types: enum printFormat { INT_FORMAT=0, STRING_FORMAT=1, ENUM_FORMAT=2, BOOL_FORMAT=3, LIST_FORMAT=4 }

user_defined_types: struct enumIdName { int id; char *name; }


enumValidator

decompiled_code:
int __fastcall enumValidator(__int64 a1, __int64 a2, const char *a3)
{
__int64 v4; // rbp
const char *v5; // rbx
size_t v6; // rax
int result; // eax

if ( !a3 )
wheat_assert("val", "config.c", 179LL, 1LL);
if ( !*(_QWORD *)(a1 + 32) )
wheat_assert("conf->helper", "config.c", 180LL, 1LL);
v4 = *(_QWORD *)(a1 + 32);
v5 = *(const char **)(v4 + 8);
if ( !v5 )
return 1;
while ( 1 )
{
v6 = strlen(v5);
result = strncasecmp(a3, v5, v6);
if ( !result )
{
*(_QWORD *)(a1 + 24) = v4;
return result;
}
if ( !v4 )
break;
v4 += 16LL;
v5 = *(const char **)(v4 + 8);
if ( !v5 )
return 1;
}
return 1;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2, const char *a3)
{
__int64 v4; // rbp
const char *v5; // rbx
size_t v6; // rax
int result; // eax

if ( !a3 )
wheat_assert("val", "config.c", 179LL, 1LL);
if ( !*(_QWORD *)(a1 + 32) )
wheat_assert("conf->helper", "config.c", 180LL, 1LL);
v4 = *(_QWORD *)(a1 + 32);
v5 = *(const char **)(v4 + 8);
if ( !v5 )
return 1;
while ( 1 )
{
v6 = strlen(v5);
result = strncasecmp(a3, v5, v6);
if ( !result )
{
*(_QWORD *)(a1 + 24) = v4;
return result;
}
if ( !v4 )
break;
v4 += 16LL;
v5 = *(const char **)(v4 + 8);
if ( !v5 )
return 1;
}
return 1;
}

original_code:
int enumValidator(struct configuration *conf, const char *key, const char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 179, 1); } } while (0);
    do { if (!(conf->helper)) { wheat_assert("conf->helper", "config.c", 180, 1); } } while (0);
    struct enumIdName *sentinel = (struct enumIdName *)conf->helper;
    while (sentinel->name) {
        if (strncasecmp(val, sentinel->name, strlen(sentinel->name)) == 0) {
            conf->target.enum_ptr = sentinel;
            return 0;
        }
        else if (sentinel == ((void *)0))
            break ;
        sentinel++;
    }
    return 1;
}

canonical_original_code:
int enumValidator(struct configuration *conf, char *key, char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 179, 1); } } while (0);
    do { if (!(conf->helper)) { wheat_assert("conf->helper", "config.c", 180, 1); } } while (0);
    struct enumIdName *sentinel = (struct enumIdName *)conf->helper;
    while (sentinel->name) {
        if (strncasecmp(val, sentinel->name, strlen(sentinel->name)) == 0) {
            conf->target.enum_ptr = sentinel;
            return 0;
        }
        else if (sentinel == ((void *)0))
            break ;
        sentinel++;
    }
    return 1;
}

variable_types: struct enumIdName { int id; char *name; } *


boolValidator

decompiled_code:
__int64 __fastcall boolValidator(__int64 a1, __int64 a2, const char *a3)
{
size_t v4; // rbp
unsigned int v5; // edx

v4 = strlen(a3) + 1;
if ( !memcmp("on", a3, v4) )
{
v5 = 0;
*(_DWORD *)(a1 + 24) = 1;
}
else
{
v5 = memcmp("off", a3, v4);
if ( v5 )
v5 = 1;
else
*(_DWORD *)(a1 + 24) = 0;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, const char *a3)
{
size_t v4; // rbp
unsigned int v5; // edx

v4 = strlen(a3) + 1;
if ( !memcmp("on", a3, v4) )
{
v5 = 0;
*(_DWORD *)(a1 + 24) = 1;
}
else
{
v5 = memcmp("off", a3, v4);
if ( v5 )
v5 = 1;
else
*(_DWORD *)(a1 + 24) = 0;
}
return v5;
}

original_code:
int boolValidator(struct configuration *conf, const char *key, const char *val)
{
    size_t len = strlen(val) + 1;
    if (memcmp("on", val, len) == 0) {
        conf->target.val = 1;
    } else if (memcmp("off", val, len) == 0) {
        conf->target.val = 0;
    } else
        return 1;
    return 0;
}

canonical_original_code:
int boolValidator(struct configuration *conf, char *key, char *val)
{
    unsigned long len = strlen(val) + 1;
    if (memcmp("on", val, len) == 0) {
        conf->target.val = 1;
    } else if (memcmp("off", val, len) == 0) {
        conf->target.val = 0;
    } else
        return 1;
    return 0;
}

variable_types:
len
unsigned long


handleList

decompiled_code:
__int64 __fastcall handleList(__int64 a1, _DWORD *a2, int a3)
{
int v4; // er12
_QWORD *v5; // r13
int v6; // ebx
int v7; // er14
__int64 v8; // rbp
char v9; // al
__int64 v10; // rax
__int64 v12; // [rsp+0h] [rbp-48h]

v4 = *a2 + 1;
v12 = createList(a1);
*(_QWORD *)(v12 + 32) = &wstrFree;
if ( v4 < a3 )
{
v5 = (_QWORD *)(a1 + 8LL * v4);
do
{
v8 = wstrStrip(*v5, "\t\n\r ");
v7 = 0;
v6 = 0;
while ( (int)wstrlen(v8) > v6 )
{
v9 = *(_BYTE *)(v8 + v6);
if ( v9 == 32 )
{
++v6;
}
else if ( v9 == 45 )
{
if ( v7 )
goto LABEL_13;
++v6;
v7 = 1;
}
else
{
if ( !v7 )
goto LABEL_13;
v10 = wstrNew();
appendToListTail(v12, v10);
v6 = wstrlen(v8);
}
}
++v4;
++v5;
}
while ( a3 != v4 );
}
LABEL_13:
*a2 = v4 - 1;
return v12;
}

original_code:
static struct list *handleList(wstr *lines, int *i, int count)
{
    int pos = *i + 1;
    wstr line = ((void *)0);
    struct list *l = createList();
    ((l)->free = ((void(*)(void *))wstrFree));
    while (pos < count) {
        line = wstrStrip(lines[pos], "\t\n\r ");
        int len = 0;
        int line_valid = 0;
        while (len < wstrlen(line)) {
            char ch = line[len];
            switch(ch) {
                case ' ':
                    len++;
                    break;
                case '-':
                    if (!line_valid)
                        line_valid = 1;
                    else
                        goto handle_error;
                    len++;
                    break;
                default:
                    if (!line_valid)
                        goto handle_error;
                    appendToListTail(l, wstrNew(&line[len]));
                    len = wstrlen(line);
                    break;
            }
        }
        pos++;
    }
handle_error:
    pos--;
    *i = pos;
    return l;
}

canonical_original_code:
static struct list *handleList(char **lines, int *i, int count)
{
    int pos = *i + 1;
    char *line = ((void *)0);
    struct list *l = createList();
    ((l)->free = ((void (*)(void *))wstrFree));
    while (pos < count) {
        line = wstrStrip(lines[pos], "\t\n\r ");
        int len = 0;
        int line_valid = 0;
        while (len < wstrlen(line)) {
            char ch = line[len];
            switch(ch) {
                case ' ':
                    len++;
                    break;
                case '-':
                    if (!line_valid)
                        line_valid = 1;
                    else
                        goto handle_error;
                    len++;
                    break;
                default:
                    if (!line_valid)
                        goto handle_error;
                    appendToListTail(l, wstrNew(&line[len]));
                    len = wstrlen(line);
                    break;
            }
        }
        pos++;
    }
handle_error:
    pos--;
    *i = pos;
    return l;
}

variable_types:
lines
char **

variable_types:
i
int *

variable_types: count int

variable_types: pos int

variable_types:
line
char *

variable_types: struct list { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; } *

variable_types: len int

variable_types: ch char

user_defined_types: struct list { struct listNode *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; }


fillServerConfig

decompiled_code:
__int64 fillServerConfig()
{
__int64 result; // rax
unsigned int v1; // edx
unsigned int v2; // edx

*(_QWORD *)Server = *(&configTable + 9);
result = *((unsigned int *)&configTable + 30);
Server[3] = result;
Server[2] = result;
Server[4] = *((_DWORD *)&configTable + 42);
*(_QWORD *)&Server[6] = *((_QWORD *)*(&configTable + 27) + 1);
*(_QWORD *)&Server[21044] = *(&configTable + 33);
Server[21046] = *(_DWORD *)*(&configTable + 39);
Server[267] = *((_DWORD *)&configTable + 90);
*(_QWORD *)&Server[268] = *(&configTable + 51);
Server[270] = *((_DWORD *)&configTable + 114);
*(_QWORD *)&Server[272] = *(&configTable + 63);
Server[274] = *((_DWORD *)&configTable + 138);
Server[275] = *((_DWORD *)&configTable + 150);
*(_QWORD *)&Server[276] = *(&configTable + 81);
Server[271] = *((_DWORD *)&configTable + 174);
*(_QWORD *)&Server[21028] = *((int *)&configTable + 186);
v1 = *((_DWORD *)&configTable + 198);
if ( !v1 )
v1 = result;
Server[2] = v1;
v2 = *((_DWORD *)&configTable + 210);
if ( v2 )
result = v2;
Server[3] = result;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
unsigned int v1; // edx
unsigned int v2; // edx

*(_QWORD *)Server = *(&configTable + 9);
result = *((unsigned int *)&configTable + 30);
Server[3] = result;
Server[2] = result;
Server[4] = *((_DWORD *)&configTable + 42);
*(_QWORD *)&Server[6] = *((_QWORD *)*(&configTable + 27) + 1);
*(_QWORD *)&Server[21044] = *(&configTable + 33);
Server[21046] = *(_DWORD *)*(&configTable + 39);
Server[267] = *((_DWORD *)&configTable + 90);
*(_QWORD *)&Server[268] = *(&configTable + 51);
Server[270] = *((_DWORD *)&configTable + 114);
*(_QWORD *)&Server[272] = *(&configTable + 63);
Server[274] = *((_DWORD *)&configTable + 138);
Server[275] = *((_DWORD *)&configTable + 150);
*(_QWORD *)&Server[276] = *(&configTable + 81);
Server[271] = *((_DWORD *)&configTable + 174);
*(_QWORD *)&Server[21028] = *((int *)&configTable + 186);
v1 = *((_DWORD *)&configTable + 198);
if ( !v1 )
v1 = result;
Server[2] = v1;
v2 = *((_DWORD *)&configTable + 210);
if ( v2 )
result = v2;
Server[3] = result;
return result;
}

original_code:
void fillServerConfig()
{
    struct configuration *conf = &configTable[1];
    Server.bind_addr = conf->target.ptr;
    conf++;
    Server.port_range_start = Server.port_range_end = conf->target.val;
    conf++;
    Server.worker_number = conf->target.val;
    conf++;
    Server.worker_type = conf->target.enum_ptr->name;
    conf++;
    Server.logfile = conf->target.ptr;
    conf++;
    Server.verbose = conf->target.enum_ptr->id;
    conf++;
    Server.daemon = conf->target.val;
    conf++;
    Server.pidfile = conf->target.ptr;
    conf++;
    Server.max_buffer_size = conf->target.val;
    conf++;
    Server.stat_addr = conf->target.ptr;
    conf++;
    Server.stat_port = conf->target.val;
    conf++;
    Server.stat_refresh_seconds = conf->target.val;
    conf++;
    Server.stat_file = conf->target.ptr;
    conf++;
    Server.worker_timeout = conf->target.val;
    conf++;
    Server.mbuf_size = conf->target.val;
    conf++;
    if (conf->target.val)
        Server.port_range_start = conf->target.val;
    conf++;
    if (conf->target.val)
        Server.port_range_end = conf->target.val;
}


listValidator

decompiled_code:
__int64 __fastcall listValidator(__int64 a1, __int64 a2, __int64 a3)
{
if ( !a3 )
wheat_assert("val", "config.c", 141LL, 1LL);
if ( *(_QWORD *)(a1 + 24) )
freeList();
*(_QWORD *)(a1 + 24) = a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3)
{
if ( !a3 )
wheat_assert("val", "config.c", 141LL, 1LL);
if ( *(_QWORD *)(a1 + 24) )
freeList();
*(_QWORD *)(a1 + 24) = a3;
return 0LL;
}

original_code:
int listValidator(struct configuration *conf, const char *key, const char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 141, 1); } } while (0);
    if (conf->target.ptr) {
        freeList(conf->target.ptr);
    }
    conf->target.ptr = (struct list *)val;
    return 0;
}

canonical_original_code:
int listValidator(struct configuration *conf, char *key, char *val)
{
    do { if (!(val)) { wheat_assert("val", "config.c", 141, 1); } } while (0);
    if (conf->target.ptr) {
        freeList(conf->target.ptr);
    }
    conf->target.ptr = (struct list *)val;
    return 0;
}

user_defined_types: struct listNode { struct listNode *next; struct listNode *prev; void *value; }


initServerConfs

decompiled_code:
__int64 __fastcall initServerConfs(__int64 a1)
{
char **v1; // rbx
__int64 result; // rax

v1 = &configTable;
do
{
result = appendToListTail(a1, v1);
v1 += 6;
}
while ( v1 != (char **)(unsigned int)((_DWORD)&configTable + 912) );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
char **v1; // rbx
__int64 result; // rax

v1 = &configTable;
do
{
result = appendToListTail(a1, v1);
v1 += 6;
}
while ( v1 != (char **)(unsigned int)((_DWORD)&configTable + 912) );
return result;
}

original_code:
void initServerConfs(struct list *confs)
{
    struct configuration *conf;
    int len, i;
    len = sizeof(configTable) / sizeof(struct configuration);
    i = 0;
    while (i != len) {
        conf = &configTable[i];
        appendToListTail(confs, conf);
        i++;
    }
}

canonical_original_code:
void initServerConfs(struct list *confs)
{
    struct configuration *conf;
    int len; int i;
    len = sizeof(configTable) / sizeof(struct configuration);
    i = 0;
    while (i != len) {
        conf = &configTable[i];
        appendToListTail(confs, conf);
        i++;
    }
}