printstatus

decompiled_code:
__int64 __fastcall printstatus(unsigned int a1)
{
int v2; // esi
int v3; // edi
int v4; // ebx
__int64 result; // rax

v2 = oldscrrow_0;
v3 = rows;
if ( oldscrrow_0 != rows && cols > 0 )
{
v4 = 0;
while ( 1 )
{
if ( wmove(stdscr, v2, v4) != -1 )
waddch(stdscr, 0x20uLL);
if ( cols <= ++v4 )
break;
v2 = oldscrrow_0;
}
v3 = rows;
}
mvprintw(v3, 0, "Score: %d", a1);
mvprintw(rows, cols - 9, "Level: %d", level);
result = (unsigned int)rows;
oldscrrow_0 = rows;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
int v2; // esi
int v3; // edi
int v4; // ebx
__int64 result; // rax

v2 = oldscrrow_0;
v3 = rows;
if ( oldscrrow_0 != rows && cols > 0 )
{
v4 = 0;
while ( 1 )
{
if ( wmove(stdscr, v2, v4) != -1 )
waddch(stdscr, 0x20uLL);
if ( cols <= ++v4 )
break;
v2 = oldscrrow_0;
}
v3 = rows;
}
mvprintw(v3, 0, "Score: %d", a1);
mvprintw(rows, cols - 9, "Level: %d", level);
result = (unsigned int)rows;
oldscrrow_0 = rows;
return result;
}

original_code:
void printstatus (int score) {
    extern int rows;
    extern int cols;
    extern int level;
    static int oldscrrow;
    int clrcol;
    char *scoref = "Score: %d";
    char *levelf = "Level: %d";
    if (oldscrrow != rows)
        for (clrcol = 0; clrcol < cols; clrcol++)
            (wmove((stdscr),((oldscrrow)),((clrcol))) == (-1) ? (-1) : waddch((stdscr),((' '))));
    mvprintw(rows, 0, scoref, score);
    mvprintw(rows, cols - strlen(levelf), levelf, level);
    oldscrrow = rows;
}

canonical_original_code:
void printstatus(int score) {
    extern int rows;
    extern int cols;
    extern int level;
    static int oldscrrow;
    int clrcol;
    char *scoref = "Score: %d";
    char *levelf = "Level: %d";
    if (oldscrrow != rows)
        for (clrcol = 0; clrcol < cols; clrcol++)
            (wmove((stdscr),((oldscrrow)),((clrcol))) == (-1) ? (-1) : waddch((stdscr),((' '))));
    mvprintw(rows, 0, scoref, score);
    mvprintw(rows, cols - strlen(levelf), levelf, level);
    oldscrrow = rows;
}

variable_types: score int

variable_types: level int

variable_types: oldscrrow int

variable_types: clrcol int

variable_types:
scoref
char *

variable_types:
levelf
char *


fobjects

repo: nissafors/lwrace

decompiled_code:
__int64 __fastcall fobjects(__int64 a1, int a2)
{
int v3; // eax
__int64 *v4; // rbp
__int64 v5; // rbx
double v7; // xmm0_8
int v8; // eax
__int64 v9; // rax
double v10; // xmm0_8
double v11; // xmm1_8
__int64 v12; // rsi
int v13; // eax
__int64 v14; // rsi
__int64 v15; // rdi
__int64 v16; // rsi
__int64 v17; // rbx
int v18; // eax
__int64 v19; // rsi
__int64 v20; // rdi
int v21; // eax
__int64 v22; // rdx
float v23; // xmm0_4
__int64 v24[11]; // [rsp+0h] [rbp-58h] BYREF

v3 = objcount_5;
if ( (float)((float)a2 / 5.0) == (float)(objcount_5 + 3) && objcount_5 <= 4 )
{
v16 = cols;
v17 = objcount_5;
LODWORD(lastpos_4[objcount_5]) = 0;
v18 = genrand(0LL, v16);
v19 = (unsigned int)(100 * fobj_init_delay_max);
v20 = (unsigned int)(100 * fobj_init_delay_min);
HIDWORD(lastpos_4[v17]) = v18;
v21 = genrand(v20, v19);
v22 = objcount_5;
v23 = (float)v21;
v3 = ++objcount_5;
delay_3[v22] = v23 / 100.0 / (double)rows;
}
v4 = v24;
v5 = 0LL;
if ( v3 <= 0 )
return 0LL;
while ( 1 )
{
*v4 = lastpos_4[v5];
if ( (unsigned int)setpos(2LL, v4, &delay_3[v5], &delay_3[v5], (char *)&lasttime_2 + 8 * v5) )
{
v7 = pow(fobj_base, COERCE_DOUBLE(COERCE_UNSIGNED_INT64((double)*(int *)v4 / (double)rows) ^ LC2) * fobj_acc);
v8 = rows - 1;
delay_3[v5] = v7 * fobj_fall_delay_start + fobj_fall_delay_end;
if ( v8 <= SLODWORD(lastpos_4[v5]) )
{
v12 = cols;
*(_DWORD *)v4 = 0;
v13 = genrand(0LL, v12);
v14 = (unsigned int)(100 * fobj_hang_delay_max);
v15 = (unsigned int)(100 * fobj_hang_delay_min);
*((_DWORD *)v4 + 1) = v13;
delay_3[v5] = (float)(int)genrand(v15, v14) / 100.0;
}
v9 = drawfigure(
v24[(int)v5],
111LL,
lastpos_4[(int)v5],
32LL,
(unsigned int)lastrows_0[v5],
(unsigned int)lastcols_1[v5]);
v10 = delay_3[v5];
lastpos_4[(int)v5] = v9;
v11 = (double)rows;
lastrows_0[v5] = rows;
lastcols_1[v5] = cols;
delay_3[v5] = v10 / v11;
}
if ( *v4 == a1 )
break;
++v5;
++v4;
if ( objcount_5 <= (int)v5 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
int v3; // eax
__int64 *v4; // rbp
__int64 v5; // rbx
double v7; // xmm0_8
int v8; // eax
__int64 v9; // rax
double v10; // xmm0_8
double v11; // xmm1_8
__int64 v12; // rsi
int v13; // eax
__int64 v14; // rsi
__int64 v15; // rdi
__int64 v16; // rsi
__int64 v17; // rbx
int v18; // eax
__int64 v19; // rsi
__int64 v20; // rdi
int v21; // eax
__int64 v22; // rdx
float v23; // xmm0_4
__int64 v24[11]; // [rsp+0h] [rbp-58h] BYREF

v3 = objcount_5;
if ( (float)((float)a2 / 5.0) == (float)(objcount_5 + 3) && objcount_5 <= 4 )
{
v16 = cols;
v17 = objcount_5;
LODWORD(lastpos_4[objcount_5]) = 0;
v18 = genrand(0LL, v16);
v19 = (unsigned int)(100 * fobj_init_delay_max);
v20 = (unsigned int)(100 * fobj_init_delay_min);
HIDWORD(lastpos_4[v17]) = v18;
v21 = genrand(v20, v19);
v22 = objcount_5;
v23 = (float)v21;
v3 = ++objcount_5;
delay_3[v22] = v23 / 100.0 / (double)rows;
}
v4 = v24;
v5 = 0LL;
if ( v3 <= 0 )
return 0LL;
while ( 1 )
{
*v4 = lastpos_4[v5];
if ( (unsigned int)setpos(2LL, v4, &delay_3[v5], &delay_3[v5], (char *)&lasttime_2 + 8 * v5) )
{
v7 = pow(fobj_base, COERCE_DOUBLE(COERCE_UNSIGNED_INT64((double)*(int *)v4 / (double)rows) ^ LC2) * fobj_acc);
v8 = rows - 1;
delay_3[v5] = v7 * fobj_fall_delay_start + fobj_fall_delay_end;
if ( v8 <= SLODWORD(lastpos_4[v5]) )
{
v12 = cols;
*(_DWORD *)v4 = 0;
v13 = genrand(0LL, v12);
v14 = (unsigned int)(100 * fobj_hang_delay_max);
v15 = (unsigned int)(100 * fobj_hang_delay_min);
*((_DWORD *)v4 + 1) = v13;
delay_3[v5] = (float)(int)genrand(v15, v14) / 100.0;
}
v9 = drawfigure(
v24[(int)v5],
111LL,
lastpos_4[(int)v5],
32LL,
(unsigned int)lastrows_0[v5],
(unsigned int)lastcols_1[v5]);
v10 = delay_3[v5];
lastpos_4[(int)v5] = v9;
v11 = (double)rows;
lastrows_0[v5] = rows;
lastcols_1[v5] = cols;
delay_3[v5] = v10 / v11;
}
if ( *v4 == a1 )
break;
++v5;
++v4;
if ( objcount_5 <= (int)v5 )
return 0LL;
}
return 1LL;
}

original_code:
bool_t fobjects(struct pos plpos, int score) {
    extern int rows, cols;
    extern int fobj_init_delay_min, fobj_init_delay_max;
    extern int fobj_hang_delay_min, fobj_hang_delay_max;
    extern double fobj_fall_delay_start, fobj_fall_delay_end;
    extern double fobj_base, fobj_acc;
    static struct pos lastpos[5];
    static double lasttime[5];
    static int lastrows[5], lastcols[5];
    static int objcount;
    struct pos objpos[5];
    static double delay[5];
    int i;
    if ((float)score / (float)5 == objcount +
       3 && objcount < 5) {
        lastpos[objcount].row = 0;
        lastpos[objcount].col = genrand(0,cols);
        delay[objcount] = (float)genrand(fobj_init_delay_min * 100,
                           fobj_init_delay_max * 100) / 100.0;;
        delay[objcount] /= rows;
        objcount++;
    }
    for (i = 0; i < objcount; i++) {
        objpos[i] = lastpos[i];
        if (setpos(2, objpos+i, delay+i, delay+i, lasttime+i)) {
            delay[i] = fobj_fall_delay_end + fobj_fall_delay_start *
                       pow(fobj_base, -((double)objpos[i].row / (double)rows) *
                       fobj_acc);
            if (lastpos[i].row > rows - 2) {
                objpos[i].row = 0;
                objpos[i].col = genrand(0, cols);
                delay[i] = (float)genrand(fobj_hang_delay_min * 100,
                             fobj_hang_delay_max * 100) / 100.0;
            }
            lastpos[i] = drawfigure(objpos[i], 'o', lastpos[i], ' ',
                                    lastrows[i], lastcols[i]);
            delay[i] /= rows;
            lastrows[i] = rows, lastcols[i] = cols;
        }
        if (objpos[i].row == plpos.row && objpos[i].col == plpos.col)
            return 1;
    }
    return 0;
}

canonical_original_code:
int fobjects(struct pos plpos, int score) {
    extern int rows; extern int cols;
    extern int fobj_init_delay_min; extern int fobj_init_delay_max;
    extern int fobj_hang_delay_min; extern int fobj_hang_delay_max;
    extern double fobj_fall_delay_start; extern double fobj_fall_delay_end;
    extern double fobj_base; extern double fobj_acc;
    static struct pos lastpos[5];
    static double lasttime[5];
    static int lastrows[5]; static int lastcols[5];
    static int objcount;
    struct pos objpos[5];
    static double delay[5];
    int i;
    if ((float)score / (float)5 == objcount +
       3 && objcount < 5) {
        lastpos[objcount].row = 0;
        lastpos[objcount].col = genrand(0,cols);
        delay[objcount] = (float)genrand(fobj_init_delay_min * 100,
                           fobj_init_delay_max * 100) / 100.0;;
        delay[objcount] /= rows;
        objcount++;
    }
    for (i = 0; i < objcount; i++) {
        objpos[i] = lastpos[i];
        if (setpos(2, objpos+i, delay+i, delay+i, lasttime+i)) {
            delay[i] = fobj_fall_delay_end + fobj_fall_delay_start *
                       pow(fobj_base, -((double)objpos[i].row / (double)rows) *
                       fobj_acc);
            if (lastpos[i].row > rows - 2) {
                objpos[i].row = 0;
                objpos[i].col = genrand(0, cols);
                delay[i] = (float)genrand(fobj_hang_delay_min * 100,
                             fobj_hang_delay_max * 100) / 100.0;
            }
            lastpos[i] = drawfigure(objpos[i], 'o', lastpos[i], ' ',
                                    lastrows[i], lastcols[i]);
            delay[i] /= rows;
            lastrows[i] = rows, lastcols[i] = cols;
        }
        if (objpos[i].row == plpos.row && objpos[i].col == plpos.col)
            return 1;
    }
    return 0;
}

variable_types:
plpos
struct pos { int row; int col; }

variable_types: rows int

variable_types: cols int

variable_types:
lastpos
struct pos { int row; int col; } [5]

variable_types:
lasttime
double [5]

variable_types:
lastrows
int [5]

variable_types:
lastcols
int [5]

variable_types: objcount int

variable_types:
objpos
struct pos { int row; int col; } [5]

variable_types:
delay
double [5]

variable_types: i int

return_type: int


hunt

decompiled_code:
__int64 __fastcall hunt(int *a1, int *a2, int a3)
{
int v3; // ecx
int v4; // er8
int v5; // esi
int v6; // edi
double v7; // xmm1_8
__int64 result; // rax

v3 = *a2;
v4 = *a1;
v5 = a2[1];
v6 = a1[1];
v7 = (double)(int)abs32(v3 - v4);
if ( 0.22 * (double)rows > v7 && (double)cols * 0.0625 > (double)(int)abs32(v5 - v6)
|| v7 > (double)rows * 0.44
|| (double)(int)abs32(v5 - v6) > (double)cols * 0.375 )
{
goto LABEL_3;
}
result = (unsigned int)(a3 % 3);
if ( (_DWORD)result != 1 )
{
if ( (_DWORD)result == 2 )
{
if ( v5 < v6 )
return 4LL;
if ( v5 <= v6 )
{
if ( v3 >= v4 )
result = v3 > v4;
return result;
}
return 3LL;
}
if ( (_DWORD)result )
return 0LL;
LABEL_3:
if ( v3 < v4 )
{
result = 7LL;
if ( v5 >= v6 )
{
result = 8LL;
if ( v5 <= v6 )
result = 2LL;
}
return result;
}
if ( v3 > v4 )
{
result = 5LL;
if ( v5 >= v6 )
result = 5 * (unsigned int)(v5 > v6) + 1;
return result;
}
LABEL_20:
result = 4LL;
if ( v5 < v6 )
return result;
result = v5 > v6;
if ( v5 <= v6 )
return result;
return 3LL;
}
if ( v3 < v4 )
return 2LL;
if ( v3 <= v4 )
goto LABEL_20;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, int *a2, int a3)
{
int v3; // ecx
int v4; // er8
int v5; // esi
int v6; // edi
double v7; // xmm1_8
__int64 result; // rax

v3 = *a2;
v4 = *a1;
v5 = a2[1];
v6 = a1[1];
v7 = (double)(int)abs32(v3 - v4);
if ( 0.22 * (double)rows > v7 && (double)cols * 0.0625 > (double)(int)abs32(v5 - v6)
|| v7 > (double)rows * 0.44
|| (double)(int)abs32(v5 - v6) > (double)cols * 0.375 )
{
goto LABEL_3;
}
result = (unsigned int)(a3 % 3);
if ( (_DWORD)result != 1 )
{
if ( (_DWORD)result == 2 )
{
if ( v5 < v6 )
return 4LL;
if ( v5 <= v6 )
{
if ( v3 >= v4 )
result = v3 > v4;
return result;
}
return 3LL;
}
if ( (_DWORD)result )
return 0LL;
LABEL_3:
if ( v3 < v4 )
{
result = 7LL;
if ( v5 >= v6 )
{
result = 8LL;
if ( v5 <= v6 )
result = 2LL;
}
return result;
}
if ( v3 > v4 )
{
result = 5LL;
if ( v5 >= v6 )
result = 5 * (unsigned int)(v5 > v6) + 1;
return result;
}
LABEL_20:
result = 4LL;
if ( v5 < v6 )
return result;
result = v5 > v6;
if ( v5 <= v6 )
return result;
return 3LL;
}
if ( v3 < v4 )
return 2LL;
if ( v3 <= v4 )
goto LABEL_20;
return result;
}

original_code:
dir_t hunt(struct pos *target, struct pos *hunter, int logic) {
    extern int rows, cols;
    int up, down, left, right;
    up = down = left = right = 0;
    logic = logic % 3;
    if (
            (
                (double)abs(hunter->row - target->row)
                < 0.22 * (double)rows
                && (double)abs(hunter->col - target->col)
                < 0.0625 * (double)cols
            )
            || (double)abs(hunter->row - target->row)
                > 0.44 * (double)rows
            || (double)abs(hunter->col - target->col)
                > 0.375 * (double)cols
        )
        logic = 0;
    if (logic == 1) {
        if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
        else if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
    }
    if (logic == 2) {
        if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
        else if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
    }
    if (logic == 0) {
        if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
        if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
    }
    if (up && right)
        return 5;
    else if (up && left)
        return 6;
    else if (down && right)
        return 7;
    else if (down && left)
        return 8;
    else if (up)
        return 1;
    else if (down)
        return 2;
    else if (right)
        return 4;
    else if (left)
        return 3;
    else
        return 0;
}

canonical_original_code:
int hunt(struct pos *target, struct pos *hunter, int logic) {
    extern int rows; extern int cols;
    int up; int down; int left; int right;
    up = down = left = right = 0;
    logic = logic % 3;
    if (
            (
                (double)abs(hunter->row - target->row)
                < 0.22 * (double)rows
                && (double)abs(hunter->col - target->col)
                < 0.0625 * (double)cols
            )
            || (double)abs(hunter->row - target->row)
                > 0.44 * (double)rows
            || (double)abs(hunter->col - target->col)
                > 0.375 * (double)cols
        )
        logic = 0;
    if (logic == 1) {
        if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
        else if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
    }
    if (logic == 2) {
        if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
        else if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
    }
    if (logic == 0) {
        if (hunter->row < target->row)
            down = 1;
        else if (hunter->row > target->row)
            up = 1;
        if (hunter->col < target->col)
            right = 1;
        else if (hunter->col > target->col)
            left = 1;
    }
    if (up && right)
        return 5;
    else if (up && left)
        return 6;
    else if (down && right)
        return 7;
    else if (down && left)
        return 8;
    else if (up)
        return 1;
    else if (down)
        return 2;
    else if (right)
        return 4;
    else if (left)
        return 3;
    else
        return 0;
}

variable_types:
target
struct pos { int row; int col; } *

variable_types:
hunter
struct pos *

variable_types: logic int

variable_types: up int

variable_types: down int

variable_types: left int

variable_types: right int


drawenemies

decompiled_code:
__int64 __fastcall drawenemies(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rbp
unsigned int v5; // eax
double v6; // xmm2_8
double v7; // xmm0_8
__int64 v8; // r10
__int64 v9; // rax
double v10; // xmm0_8
double v11; // r12
double v12; // xmm1_8
int i; // er12
int v15; // eax
__int64 v16; // rsi
int v17; // eax
__int64 v18; // rdx
double v19; // xmm0_8
__int64 v20[16]; // [rsp+0h] [rbp-80h] BYREF

v2 = encount_7;
v20[0] = a1;
if ( (float)((float)a2 / 5.0) == (float)encount_7 && encount_7 <= 2 )
{
v15 = genrand(0LL, (unsigned int)rows);
v16 = cols;
LODWORD(lastpos_6[v2]) = v15;
v2 = encount_7;
v17 = genrand(0LL, v16);
v18 = encount_7;
HIDWORD(lastpos_6[v2]) = v17;
delaydiff_5[v18] = 0LL;
encount_7 = v18 + 1;
v19 = getnow();
LODWORD(v2) = encount_7;
displaytimer_3 = 1;
immortaltimer_4 = *(_QWORD *)&v19;
}
if ( (int)v2 <= 0 )
return 0LL;
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
while ( 1 )
{
v20[v3 + 1] = lastpos_6[v3];
v5 = hunt((int *)v20, (int *)&v20[v3 + 1], v4);
v6 = *(double *)&delaydiff_5[v3];
v7 = enemy_col_delay;
*(double *)&v20[v3 + 5] = enemy_row_delay / (double)rows + v6 / (double)rows;
*(double *)&v20[v3 + 9] = v7 / (double)cols + v6 / (double)cols;
if ( (unsigned int)setpos(v5, v8, &v20[v3 + 5], &v20[v3 + 9], (char *)&lasttime_2 + v3 * 8) )
break;
++v4;
++v3;
if ( encount_7 <= (int)v4 )
return 0LL;
}
v9 = drawfigure(
v20[(int)v4 + 1],
35LL,
lastpos_6[(int)v4],
32LL,
(unsigned int)lastrows_0[v4],
(unsigned int)lastcols_1[v4]);
v10 = *(double *)&immortaltimer_4 + enemy_nokill_time;
lastpos_6[(int)v4] = v9;
v11 = v10;
v12 = getnow();
if ( v11 - v12 < 0.0 )
break;
mvprintw(rows, cols / 2 - 14, "* Untouchable for %d seconds!", (unsigned int)((int)(v11 - v12) + 1));
LABEL_11:
++v3;
lastrows_0[v4] = rows;
lastcols_1[v4++] = cols;
if ( encount_7 <= (int)v4 )
return 0LL;
}
if ( displaytimer_3 == 1 )
{
for ( i = 0; i != 29; ++i )
{
if ( wmove(stdscr, rows, i + cols / 2 - 14) != -1 )
waddch(stdscr, 0x20uLL);
}
displaytimer_3 = 0;
}
if ( __PAIR64__(HIDWORD(v20[v3 + 1]), v20[v3 + 1]) != v20[0] )
goto LABEL_11;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rbp
unsigned int v5; // eax
double v6; // xmm2_8
double v7; // xmm0_8
__int64 v8; // r10
__int64 v9; // rax
double v10; // xmm0_8
double v11; // r12
double v12; // xmm1_8
int i; // er12
int v15; // eax
__int64 v16; // rsi
int v17; // eax
__int64 v18; // rdx
double v19; // xmm0_8
__int64 v20[16]; // [rsp+0h] [rbp-80h] BYREF

v2 = encount_7;
v20[0] = a1;
if ( (float)((float)a2 / 5.0) == (float)encount_7 && encount_7 <= 2 )
{
v15 = genrand(0LL, (unsigned int)rows);
v16 = cols;
LODWORD(lastpos_6[v2]) = v15;
v2 = encount_7;
v17 = genrand(0LL, v16);
v18 = encount_7;
HIDWORD(lastpos_6[v2]) = v17;
delaydiff_5[v18] = 0LL;
encount_7 = v18 + 1;
v19 = getnow();
LODWORD(v2) = encount_7;
displaytimer_3 = 1;
immortaltimer_4 = *(_QWORD *)&v19;
}
if ( (int)v2 <= 0 )
return 0LL;
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
while ( 1 )
{
v20[v3 + 1] = lastpos_6[v3];
v5 = func0((int *)v20, (int *)&v20[v3 + 1], v4);
v6 = *(double *)&delaydiff_5[v3];
v7 = enemy_col_delay;
*(double *)&v20[v3 + 5] = enemy_row_delay / (double)rows + v6 / (double)rows;
*(double *)&v20[v3 + 9] = v7 / (double)cols + v6 / (double)cols;
if ( (unsigned int)setpos(v5, v8, &v20[v3 + 5], &v20[v3 + 9], (char *)&lasttime_2 + v3 * 8) )
break;
++v4;
++v3;
if ( encount_7 <= (int)v4 )
return 0LL;
}
v9 = drawfigure(
v20[(int)v4 + 1],
35LL,
lastpos_6[(int)v4],
32LL,
(unsigned int)lastrows_0[v4],
(unsigned int)lastcols_1[v4]);
v10 = *(double *)&immortaltimer_4 + enemy_nokill_time;
lastpos_6[(int)v4] = v9;
v11 = v10;
v12 = getnow();
if ( v11 - v12 < 0.0 )
break;
mvprintw(rows, cols / 2 - 14, "* Untouchable for %d seconds!", (unsigned int)((int)(v11 - v12) + 1));
LABEL_11:
++v3;
lastrows_0[v4] = rows;
lastcols_1[v4++] = cols;
if ( encount_7 <= (int)v4 )
return 0LL;
}
if ( displaytimer_3 == 1 )
{
for ( i = 0; i != 29; ++i )
{
if ( wmove(stdscr, rows, i + cols / 2 - 14) != -1 )
waddch(stdscr, 0x20uLL);
}
displaytimer_3 = 0;
}
if ( __PAIR64__(HIDWORD(v20[v3 + 1]), v20[v3 + 1]) != v20[0] )
goto LABEL_11;
return 1LL;
}

original_code:
bool_t drawenemies(struct pos plpos, int score) {
    extern int rows, cols;
    extern double enemy_row_delay, enemy_col_delay, enemy_nokill_time;
    static struct pos lastpos[3];
    static double lasttime[3];
    static int lastrows[3], lastcols[3];
    static double delaydiff[3];
    static int encount;
    static double immortaltimer;
    static bool_t displaytimer;
    struct pos enpos[3];
    double rdelay[3], cdelay[3];
    double timeleft;
    int i, j;
    dir_t dir = 0;
    char *immortaltext = "* Untouchable for %d seconds!";
    if ((float)score / (float)5 == encount
      && encount < 3) {
        lastpos[encount].row = genrand(0,rows);
        lastpos[encount].col = genrand(0,cols);
        delaydiff[encount] = 0 * encount;
        encount++;
        immortaltimer = getnow();
        displaytimer = 1;
    }
    for (i = 0; i < encount; i++) {
        enpos[i] = lastpos[i];
        dir = hunt(&plpos, enpos+i, i);
        rdelay[i] = enemy_row_delay / rows + delaydiff[i] / rows;
        cdelay[i] = enemy_col_delay / cols + delaydiff[i] / cols;
        if (setpos(dir, enpos+i, rdelay+i, cdelay+i, lasttime+i)) {
            lastpos[i] = drawfigure(enpos[i], '#', lastpos[i], ' ',
                                    lastrows[i], lastcols[i]);
            timeleft = immortaltimer + enemy_nokill_time - getnow();
            if (timeleft < 0) {
                if (displaytimer == 1) {
                    for (j = 0; j < strlen(immortaltext); j++) {
                        (wmove((stdscr),((rows)),((cols / 2 - strlen(immortaltext) / 2 + j))) == (-1) ? (-1) : waddch((stdscr),((' '))));
                    }
                    displaytimer = 0;
                }
                if (enpos[i].row == plpos.row && enpos[i].col == plpos.col)
                    return 1;
            } else {
                mvprintw(rows, cols/2-strlen(immortaltext)/2, immortaltext,
                    (int)timeleft + 1);
            }
            lastrows[i] = rows, lastcols[i] = cols;
        }
    }
    return 0;
}

canonical_original_code:
int drawenemies(struct pos plpos, int score) {
    extern int rows; extern int cols;
    extern double enemy_row_delay; extern double enemy_col_delay; extern double enemy_nokill_time;
    static struct pos lastpos[3];
    static double lasttime[3];
    static int lastrows[3]; static int lastcols[3];
    static double delaydiff[3];
    static int encount;
    static double immortaltimer;
    static int displaytimer;
    struct pos enpos[3];
    double rdelay[3]; double cdelay[3];
    double timeleft;
    int i; int j;
    int dir = 0;
    char *immortaltext = "* Untouchable for %d seconds!";
    if ((float)score / (float)5 == encount
      && encount < 3) {
        lastpos[encount].row = genrand(0,rows);
        lastpos[encount].col = genrand(0,cols);
        delaydiff[encount] = 0 * encount;
        encount++;
        immortaltimer = getnow();
        displaytimer = 1;
    }
    for (i = 0; i < encount; i++) {
        enpos[i] = lastpos[i];
        dir = hunt(&plpos, enpos+i, i);
        rdelay[i] = enemy_row_delay / rows + delaydiff[i] / rows;
        cdelay[i] = enemy_col_delay / cols + delaydiff[i] / cols;
        if (setpos(dir, enpos+i, rdelay+i, cdelay+i, lasttime+i)) {
            lastpos[i] = drawfigure(enpos[i], '#', lastpos[i], ' ',
                                    lastrows[i], lastcols[i]);
            timeleft = immortaltimer + enemy_nokill_time - getnow();
            if (timeleft < 0) {
                if (displaytimer == 1) {
                    for (j = 0; j < strlen(immortaltext); j++) {
                        (wmove((stdscr),((rows)),((cols / 2 - strlen(immortaltext) / 2 + j))) == (-1) ? (-1) : waddch((stdscr),((' '))));
                    }
                    displaytimer = 0;
                }
                if (enpos[i].row == plpos.row && enpos[i].col == plpos.col)
                    return 1;
            } else {
                mvprintw(rows, cols/2-strlen(immortaltext)/2, immortaltext,
                    (int)timeleft + 1);
            }
            lastrows[i] = rows, lastcols[i] = cols;
        }
    }
    return 0;
}

variable_types:
plpos
struct pos { int row; int col; }

variable_types: score int

variable_types: rows int

variable_types:
lastpos
struct pos { int row; int col; } [3]

variable_types:
lasttime
double [3]

variable_types:
lastrows
int [3]

variable_types:
lastcols
int [3]

variable_types:
delaydiff
double [3]

variable_types: encount int

variable_types: immortaltimer double

variable_types: displaytimer int

variable_types:
enpos
struct pos { int row; int col; } [3]

variable_types:
rdelay
double [3]

variable_types:
cdelay
double [3]

variable_types: timeleft double

variable_types: j int

variable_types: dir int

variable_types:
immortaltext
char *


getudir

decompiled_code:
struct passwd *getudir()
{
__uid_t v0; // ebx
struct passwd *result; // rax
struct passwd *v2; // [rsp+8h] [rbp-10h]

v0 = getuid();
result = getpwent();
if ( result )
{
while ( result->pw_uid != v0 )
{
result = getpwent();
if ( !result )
return result;
}
v2 = result;
endpwent();
result = (struct passwd *)v2->pw_dir;
}
else
{
endpwent();
result = 0LL;
}
return result;
}

canonical_decompiled_code:
struct passwd *func0()
{
__uid_t v0; // ebx
struct passwd *result; // rax
struct passwd *v2; // [rsp+8h] [rbp-10h]

v0 = getuid();
result = getpwent();
if ( result )
{
while ( result->pw_uid != v0 )
{
result = getpwent();
if ( !result )
return result;
}
v2 = result;
endpwent();
result = (struct passwd *)v2->pw_dir;
}
else
{
endpwent();
result = 0LL;
}
return result;
}

original_code:
static char *getudir() {
    struct passwd *upwent;
    uid_t uid;
    uid = getuid();
    if ( (upwent = getpwent()) ) {
        while (upwent->pw_uid != uid) {
            if ( !(upwent = getpwent()) ) {
                return ((void *)0);
            }
        }
        endpwent();
        return upwent->pw_dir;
    }
    endpwent();
    return ((void *)0);
}

canonical_original_code:
static char *getudir() {
    struct passwd *upwent;
    unsigned int uid;
    uid = getuid();
    if ( (upwent = getpwent()) ) {
        while (upwent->pw_uid != uid) {
            if ( !(upwent = getpwent()) ) {
                return ((void *)0);
            }
        }
        endpwent();
        return upwent->pw_dir;
    }
    endpwent();
    return ((void *)0);
}

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
uid
unsigned int

return_type: char *


expandpath

decompiled_code:
char *__fastcall expandpath(void *src, int a2)
{
char v2; // bl
char v3; // r13
const char *v4; // r13
size_t v5; // rbx
int v6; // eax
char *v7; // rax
char *v8; // r12
size_t v9; // rax
size_t v10; // r12
unsigned __int64 v11; // rbx
void *v12; // rax
int v14; // er12
int v15; // eax
char *v16; // rax
char v17[4136]; // [rsp+0h] [rbp-1028h] BYREF

v2 = *(_BYTE *)src;
v3 = *((_BYTE *)src + 1);
if ( *((char *)src + strlen((const char *)src) - 1) == 47 && a2 )
return 0LL;
if ( v2 == 126 )
{
if ( v3 == 47 )
{
v4 = (const char *)getudir();
goto LABEL_8;
}
}
else if ( v2 != 47 )
{
if ( v2 == 46 && v3 == 47 )
goto LABEL_7;
if ( !strchr((const char *)src, 47) )
{
if ( getcwd(v17, 0x1000uLL) )
{
v14 = strlen(v17);
v15 = strlen((const char *)src);
v16 = (char *)malloc(v14 + v15 + 2);
v8 = v16;
if ( v16 )
{
sprintf(v16, "%s/%s", v17, (const char *)src);
return v8;
}
}
}
if ( v2 == 46 && v3 == 47 )
{
LABEL_7:
v4 = getcwd(v17, 0x1000uLL);
LABEL_8:
if ( v4 )
{
v5 = strlen(v4);
if ( v5 <= 0x1000 )
{
v6 = strlen((const char *)src);
v7 = (char *)malloc((int)v5 + v6);
v8 = v7;
if ( v7 )
sprintf(v7, "%s%s", v4, (const char *)src + 1);
return v8;
}
}
}
return 0LL;
}
if ( v2 != 47 )
return 0LL;
v9 = strlen((const char *)src);
v10 = v9 + 1;
v11 = v9;
v12 = malloc(v9 + 1);
if ( !v12 || v11 > 0x1000 )
return 0LL;
return (char *)memcpy(v12, src, v10);
}

canonical_decompiled_code:
char *__fastcall func1(void *src, int a2)
{
char v2; // bl
char v3; // r13
const char *v4; // r13
size_t v5; // rbx
int v6; // eax
char *v7; // rax
char *v8; // r12
size_t v9; // rax
size_t v10; // r12
unsigned __int64 v11; // rbx
void *v12; // rax
int v14; // er12
int v15; // eax
char *v16; // rax
char v17[4136]; // [rsp+0h] [rbp-1028h] BYREF

v2 = *(_BYTE *)src;
v3 = *((_BYTE *)src + 1);
if ( *((char *)src + strlen((const char *)src) - 1) == 47 && a2 )
return 0LL;
if ( v2 == 126 )
{
if ( v3 == 47 )
{
v4 = (const char *)func0();
goto LABEL_8;
}
}
else if ( v2 != 47 )
{
if ( v2 == 46 && v3 == 47 )
goto LABEL_7;
if ( !strchr((const char *)src, 47) )
{
if ( getcwd(v17, 0x1000uLL) )
{
v14 = strlen(v17);
v15 = strlen((const char *)src);
v16 = (char *)malloc(v14 + v15 + 2);
v8 = v16;
if ( v16 )
{
sprintf(v16, "%s/%s", v17, (const char *)src);
return v8;
}
}
}
if ( v2 == 46 && v3 == 47 )
{
LABEL_7:
v4 = getcwd(v17, 0x1000uLL);
LABEL_8:
if ( v4 )
{
v5 = strlen(v4);
if ( v5 <= 0x1000 )
{
v6 = strlen((const char *)src);
v7 = (char *)malloc((int)v5 + v6);
v8 = v7;
if ( v7 )
sprintf(v7, "%s%s", v4, (const char *)src + 1);
return v8;
}
}
}
return 0LL;
}
if ( v2 != 47 )
return 0LL;
v9 = strlen((const char *)src);
v10 = v9 + 1;
v11 = v9;
v12 = malloc(v9 + 1);
if ( !v12 || v11 > 0x1000 )
return 0LL;
return (char *)memcpy(v12, src, v10);
}

original_code:
char *expandpath(char *path, int isfile) {
    char firstchar, secondchar, lastchar;
    char cwd[4096];
    char *dir = ((void *)0);
    char *heap = ((void *)0);
    int len = 0;
    firstchar = *path;
    secondchar = *(path + 1);
    lastchar = *(path + strlen(path) - 1);
    if (lastchar == '/' && isfile)
        return ((void *)0);
    if (firstchar != '~' && firstchar != '/') {
        if ( !(firstchar == '.' && secondchar == '/') ) {
            if (!strchr(path, '/')) {
                if (getcwd(cwd, 4096)) {
                    len = strlen(cwd) + strlen(path) + 2;
                    if ( (heap = malloc(len * sizeof(char))) ) {
                        sprintf(heap, "%s/%s", cwd, path);
                        return heap;
                    }
                }
            }
        }
    }
    if ( (firstchar == '~' || firstchar == '.') && secondchar == '/') {
        if (firstchar == '~') {
            dir = getudir();
        } else {
            dir = getcwd(cwd, 4096);
        }
        if (dir) {
            if (strlen(dir) > 4096)
                return ((void *)0);
            len = strlen(dir) + strlen(path);
            if ( (heap = malloc(len * sizeof(char))) ) {
                sprintf(heap, "%s%s", dir, path + 1);
                return heap;
            }
        }
    } else if (firstchar == '/') {
        heap = malloc((strlen(path) + 1) * sizeof(char));
        if (heap)
            if (strlen(path) <= 4096)
                return strcpy(heap, path);
    }
    return ((void *)0);
}

canonical_original_code:
char *expandpath(char *path, int isfile) {
    char firstchar; char secondchar; char lastchar;
    char cwd[4096];
    char *dir = ((void *)0);
    char *heap = ((void *)0);
    int len = 0;
    firstchar = *path;
    secondchar = *(path + 1);
    lastchar = *(path + strlen(path) - 1);
    if (lastchar == '/' && isfile)
        return ((void *)0);
    if (firstchar != '~' && firstchar != '/') {
        if ( !(firstchar == '.' && secondchar == '/') ) {
            if (!strchr(path, '/')) {
                if (getcwd(cwd, 4096)) {
                    len = strlen(cwd) + strlen(path) + 2;
                    if ( (heap = malloc(len * sizeof(char))) ) {
                        sprintf(heap, "%s/%s", cwd, path);
                        return heap;
                    }
                }
            }
        }
    }
    if ( (firstchar == '~' || firstchar == '.') && secondchar == '/') {
        if (firstchar == '~') {
            dir = getudir();
        } else {
            dir = getcwd(cwd, 4096);
        }
        if (dir) {
            if (strlen(dir) > 4096)
                return ((void *)0);
            len = strlen(dir) + strlen(path);
            if ( (heap = malloc(len * sizeof(char))) ) {
                sprintf(heap, "%s%s", dir, path + 1);
                return heap;
            }
        }
    } else if (firstchar == '/') {
        heap = malloc((strlen(path) + 1) * sizeof(char));
        if (heap)
            if (strlen(path) <= 4096)
                return strcpy(heap, path);
    }
    return ((void *)0);
}

variable_types:
path
char *

variable_types: isfile int

variable_types: firstchar char

variable_types: secondchar char

variable_types: lastchar char

variable_types:
cwd
char [4096]

variable_types:
dir
char *

variable_types:
heap
char *

variable_types: len int


drawfigure

decompiled_code:
__int64 __fastcall drawfigure(__int64 a1, char a2, __int64 a3, char a4, int a5, int a6)
{
__int64 v7; // rbp
unsigned int v8; // ebx

v7 = HIDWORD(a1);
v8 = a1;
if ( rows != a5 && a5 )
v8 = (int)(float)((float)((float)rows / (float)a5) * (float)(int)a1);
if ( cols != a6 && a6 )
v7 = (unsigned int)(int)(float)((float)((float)cols / (float)a6) * (float)SHIDWORD(a1));
if ( wmove(stdscr, a3, SHIDWORD(a3)) != -1 )
waddch(stdscr, (unsigned int)a4);
if ( wmove(stdscr, v8, v7) != -1 )
waddch(stdscr, (unsigned int)a2);
return (v7 << 32) | v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2, __int64 a3, char a4, int a5, int a6)
{
__int64 v7; // rbp
unsigned int v8; // ebx

v7 = HIDWORD(a1);
v8 = a1;
if ( rows != a5 && a5 )
v8 = (int)(float)((float)((float)rows / (float)a5) * (float)(int)a1);
if ( cols != a6 && a6 )
v7 = (unsigned int)(int)(float)((float)((float)cols / (float)a6) * (float)SHIDWORD(a1));
if ( wmove(stdscr, a3, SHIDWORD(a3)) != -1 )
waddch(stdscr, (unsigned int)a4);
if ( wmove(stdscr, v8, v7) != -1 )
waddch(stdscr, (unsigned int)a2);
return (v7 << 32) | v8;
}

original_code:
struct pos drawfigure (struct pos drawpos, char drawc,
                       struct pos erasepos, char erasec,
                       int oldrows, int oldcols) {
    extern int rows, cols;
    if (rows != oldrows && oldrows != 0)
        drawpos.row *= (float)rows / (float)oldrows;
    if (cols != oldcols && oldcols != 0)
        drawpos.col *= (float)cols / (float)oldcols;
    (wmove((stdscr),((erasepos.row)),((erasepos.col))) == (-1) ? (-1) : waddch((stdscr),((erasec))));
    (wmove((stdscr),((drawpos.row)),((drawpos.col))) == (-1) ? (-1) : waddch((stdscr),((drawc))));
    return drawpos;
}

canonical_original_code:
struct pos drawfigure(struct pos drawpos, char drawc, struct pos erasepos, char erasec, int oldrows, int oldcols) {
    extern int rows; extern int cols;
    if (rows != oldrows && oldrows != 0)
        drawpos.row *= (float)rows / (float)oldrows;
    if (cols != oldcols && oldcols != 0)
        drawpos.col *= (float)cols / (float)oldcols;
    (wmove((stdscr),((erasepos.row)),((erasepos.col))) == (-1) ? (-1) : waddch((stdscr),((erasec))));
    (wmove((stdscr),((drawpos.row)),((drawpos.col))) == (-1) ? (-1) : waddch((stdscr),((drawc))));
    return drawpos;
}

variable_types:
drawpos
struct pos

variable_types: drawc char

variable_types:
erasepos
struct pos

variable_types: erasec char

variable_types: oldrows int

variable_types: oldcols int

variable_types: rows int

variable_types: cols int

return_type: struct pos { int row; int col; }


setpos

original_code:
bool_t setpos(dir_t dir, struct pos *rc, double *rd, double *cd, double *trec) {
    extern int rows, cols;
    double dd = sqrt(pow(*rd, 2) + pow(*cd, 2));
    switch (dir) {
        case 1:
            if (getnow() - *trec < *rd)
                return 0;
            rc->row--;
            break;
        case 2:
            if (getnow() - *trec < *rd)
                return 0;
            rc->row++;
            break;
        case 4:
            if (getnow() - *trec < *cd)
                return 0;
            rc->col++;
            break;
        case 3:
            if (getnow() - *trec < *cd)
                return 0;
            rc->col--;
            break;
        case 5:
            if (getnow() - *trec < dd)
                return 0;
            rc->row--;
            rc->col++;
            break;
        case 6:
            if (getnow() - *trec < dd)
                return 0;
            rc->row--;
            rc->col--;
            break;
        case 7:
            if (getnow() - *trec < dd)
                return 0;
            rc->row++;
            rc->col++;
            break;
        case 8:
            if (getnow() - *trec < dd)
                return 0;
            rc->row++;
            rc->col--;
            break;
        default:
            return 0;
    }
    if (rc->row < 0)
        rc->row = 0;
    if (rc->row >= rows - 1)
        rc->row = rows - 1;
    if (rc->col < 0)
        rc->col = 0;
    if (rc->col >= cols - 1)
        rc->col = cols - 1;
    *trec = getnow();
    return 1;
}

canonical_original_code:
int setpos(int dir, struct pos *rc, double *rd, double *cd, double *trec) {
    extern int rows; extern int cols;
    double dd = sqrt(pow(*rd, 2) + pow(*cd, 2));
    switch (dir) {
        case 1:
            if (getnow() - *trec < *rd)
                return 0;
            rc->row--;
            break;
        case 2:
            if (getnow() - *trec < *rd)
                return 0;
            rc->row++;
            break;
        case 4:
            if (getnow() - *trec < *cd)
                return 0;
            rc->col++;
            break;
        case 3:
            if (getnow() - *trec < *cd)
                return 0;
            rc->col--;
            break;
        case 5:
            if (getnow() - *trec < dd)
                return 0;
            rc->row--;
            rc->col++;
            break;
        case 6:
            if (getnow() - *trec < dd)
                return 0;
            rc->row--;
            rc->col--;
            break;
        case 7:
            if (getnow() - *trec < dd)
                return 0;
            rc->row++;
            rc->col++;
            break;
        case 8:
            if (getnow() - *trec < dd)
                return 0;
            rc->row++;
            rc->col--;
            break;
        default:
            return 0;
    }
    if (rc->row < 0)
        rc->row = 0;
    if (rc->row >= rows - 1)
        rc->row = rows - 1;
    if (rc->col < 0)
        rc->col = 0;
    if (rc->col >= cols - 1)
        rc->col = cols - 1;
    *trec = getnow();
    return 1;
}

variable_types: dir int

variable_types:
rc
struct pos { int row; int col; } *

variable_types:
rd
double *

variable_types:
cd
double *

variable_types:
trec
double *

variable_types: dd double


treasures

decompiled_code:
__int64 __fastcall treasures(__int64 a1)
{
unsigned int v1; // er12
int v3; // eax

if ( !trcount_3 )
{
LODWORD(trpos_2) = genrand(0LL, rows);
v3 = genrand(0LL, cols);
++trcount_3;
HIDWORD(trpos_2) = v3;
}
v1 = 0;
trpos_2 = drawfigure(trpos_2, 36LL, trpos_2, 32LL, (unsigned int)oldrows_0, (unsigned int)oldcols_1);
oldrows_0 = rows;
oldcols_1 = cols;
if ( trpos_2 == a1 )
{
--trcount_3;
v1 = 1;
if ( wmove(stdscr, trpos_2, SHIDWORD(trpos_2)) != -1 )
waddch(stdscr, 0x4FuLL);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // er12
int v3; // eax

if ( !trcount_3 )
{
LODWORD(trpos_2) = genrand(0LL, rows);
v3 = genrand(0LL, cols);
++trcount_3;
HIDWORD(trpos_2) = v3;
}
v1 = 0;
trpos_2 = drawfigure(trpos_2, 36LL, trpos_2, 32LL, (unsigned int)oldrows_0, (unsigned int)oldcols_1);
oldrows_0 = rows;
oldcols_1 = cols;
if ( trpos_2 == a1 )
{
--trcount_3;
v1 = 1;
if ( wmove(stdscr, trpos_2, SHIDWORD(trpos_2)) != -1 )
waddch(stdscr, 0x4FuLL);
}
return v1;
}

original_code:
bool_t treasures(struct pos plpos) {
    extern int rows, cols;
    static struct pos trpos;
    static int trcount;
    static int oldrows, oldcols;
    if (!trcount) {
        trpos.row = genrand(0, rows);
        trpos.col = genrand(0, cols);
        trcount++;
    }
    trpos = drawfigure(trpos, '$', trpos, ' ', oldrows, oldcols);
    oldrows = rows, oldcols = cols;
    if (plpos.row == trpos.row && plpos.col == trpos.col) {
        trcount--;
        (wmove((stdscr),((trpos.row)),((trpos.col))) == (-1) ? (-1) : waddch((stdscr),(('O'))));
        return 1;
    }
    return 0;
}

canonical_original_code:
int treasures(struct pos plpos) {
    extern int rows; extern int cols;
    static struct pos trpos;
    static int trcount;
    static int oldrows; static int oldcols;
    if (!trcount) {
        trpos.row = genrand(0, rows);
        trpos.col = genrand(0, cols);
        trcount++;
    }
    trpos = drawfigure(trpos, '$', trpos, ' ', oldrows, oldcols);
    oldrows = rows, oldcols = cols;
    if (plpos.row == trpos.row && plpos.col == trpos.col) {
        trcount--;
        (wmove((stdscr),((trpos.row)),((trpos.col))) == (-1) ? (-1) : waddch((stdscr),(('O'))));
        return 1;
    }
    return 0;
}

variable_types:
plpos
struct pos { int row; int col; }

variable_types:
trpos
struct pos { int row; int col; }

variable_types: trcount int


decompiled_code:
__int64 __fastcall lookup_key(char *s1)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp(s1, (&keys)[2 * v1]) )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
return LODWORD((&keys)[2 * (int)v1 + 1]);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp(s1, (&keys)[2 * v1]) )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
return LODWORD((&keys)[2 * (int)v1 + 1]);
}

original_code:
int lookup_key(char* keystr) {
    int i;
    for (i = 0; i < 16; i++) {
        if (strcmp(keystr, keys[i].keyspec) == 0) {
            return keys[i].keynum;
        }
    }
    return -1;
}

canonical_original_code:
int lookup_key(char *keystr) {
    int i;
    for (i = 0; i < 16; i++) {
        if (strcmp(keystr, keys[i].keyspec) == 0) {
            return keys[i].keynum;
        }
    }
    return -1;
}

variable_types:
keystr
char *

variable_types: i int


decompiled_code:
__int64 __fastcall parse_key(char *s1, int *a2)
{
int v2; // eax
__int64 result; // rax
int v4; // er8
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *s1 == 39 && s1[2] == 39 && strlen(s1) == 3 )
{
*a2 = s1[1];
return 0LL;
}
v2 = lookup_key(s1);
v5[0] = v2;
if ( v2 != -1 )
goto LABEL_3;
v4 = _isoc99_sscanf(s1, &unk_371, v5);
result = 1LL;
if ( v4 == 1 )
{
v2 = v5[0];
LABEL_3:
*a2 = v2;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1, int *a2)
{
int v2; // eax
__int64 result; // rax
int v4; // er8
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *s1 == 39 && s1[2] == 39 && strlen(s1) == 3 )
{
*a2 = s1[1];
return 0LL;
}
v2 = func0(s1);
v5[0] = v2;
if ( v2 != -1 )
goto LABEL_3;
v4 = _isoc99_sscanf(s1, &unk_371, v5);
result = 1LL;
if ( v4 == 1 )
{
v2 = v5[0];
LABEL_3:
*a2 = v2;
result = 0LL;
}
return result;
}

original_code:
int parse_key(char *value, int *key) {
    int temp;
    if (value[0] == '\'' && value[2] == '\'' && strlen(value) == 3) {
        *key = (int)value[1];
        return 0;
    }
    if ( (temp = lookup_key(value)) != -1 ) {
        *key = temp;
        return 0;
    }
    if (sscanf(value, "%d", &temp) == 1) {
        *key = temp;
        return 0;
    }
    return 1;
}

variable_types:
value
char *

variable_types:
key
int *

variable_types: temp int


setspeed

decompiled_code:
__int64 setspeed()
{
__int64 result; // rax
double v1; // xmm0_8

result = (unsigned int)(10 - level);
v1 = 0.5 * (double)(int)result + 1.5;
player_col_delay = v1;
enemy_to_player_delay_ratio = (double)(int)result * 0.125 + 1.125;
enemy_col_delay = v1 * ((double)(int)result * 0.125 + 1.125);
player_row_delay = v1 * row_to_col_delay_ratio;
enemy_row_delay = enemy_col_delay * row_to_col_delay_ratio;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
double v1; // xmm0_8

result = (unsigned int)(10 - level);
v1 = 0.5 * (double)(int)result + 1.5;
player_col_delay = v1;
enemy_to_player_delay_ratio = (double)(int)result * 0.125 + 1.125;
enemy_col_delay = v1 * ((double)(int)result * 0.125 + 1.125);
player_row_delay = v1 * row_to_col_delay_ratio;
enemy_row_delay = enemy_col_delay * row_to_col_delay_ratio;
return result;
}

original_code:
void setspeed() {
    extern int level;
    extern double enemy_row_delay, enemy_col_delay;
    extern double player_row_delay, player_col_delay;
    extern double row_to_col_delay_ratio, enemy_to_player_delay_ratio;
    const double PCD_K = 0.5, PCD_M = 1.5;
    const double ETPD_K = 0.125, ETPD_M = 1.125;
    player_col_delay = (9 + 1 - level) * PCD_K + PCD_M;
    enemy_to_player_delay_ratio = (9 + 1 - level) * ETPD_K + ETPD_M;
    player_row_delay = player_col_delay * row_to_col_delay_ratio;
    enemy_col_delay = player_col_delay * enemy_to_player_delay_ratio;
    enemy_row_delay = enemy_col_delay * row_to_col_delay_ratio;
}

canonical_original_code:
void setspeed() {
    extern int level;
    extern double enemy_row_delay; extern double enemy_col_delay;
    extern double player_row_delay; extern double player_col_delay;
    extern double row_to_col_delay_ratio; extern double enemy_to_player_delay_ratio;
    double PCD_K = 0.5; double PCD_M = 1.5;
    double ETPD_K = 0.125; double ETPD_M = 1.125;
    player_col_delay = (9 + 1 - level) * PCD_K + PCD_M;
    enemy_to_player_delay_ratio = (9 + 1 - level) * ETPD_K + ETPD_M;
    player_row_delay = player_col_delay * row_to_col_delay_ratio;
    enemy_col_delay = player_col_delay * enemy_to_player_delay_ratio;
    enemy_row_delay = enemy_col_delay * row_to_col_delay_ratio;
}

variable_types: level int

return_type: void


initSJF

repo: nivruthn/multithreadwebserverinc

decompiled_code:
void __fastcall __noreturn initSJF(void *a1)
{
int v1; // er12
__int64 v2; // r13
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // rcx
const char *v6; // rsi
sem_t *v7; // rdi
__int64 v8; // rax
_DWORD *v9; // rdx
bool v10; // cl
time_t timer; // [rsp+8h] [rbp-30h] BYREF

sleep(t);
while ( 1 )
{
v1 = 0;
sem_wait(&count);
pthread_mutex_lock(&qLock);
v2 = q[0];
if ( *(_DWORD *)(q[1] + 16) < *(_DWORD *)(q[0] + 16) )
{
v2 = q[1];
v1 = 1;
}
pthread_mutex_unlock(&qLock);
sem_wait(&idle_threads);
pthread_mutex_lock(&wLock);
if ( (int)NTHREADS > 0 )
{
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = threads + v3;
if ( *(_DWORD *)(threads + v3 + 16) == 1 )
break;
++v4;
v3 += 24LL;
if ( v4 == NTHREADS )
goto LABEL_9;
}
*(_DWORD *)(v5 + 16) = 0;
v6 = *(const char **)(v2 + 8);
*(_QWORD *)(v5 + 8) = v2;
printf("The file %s is assigned to thread %d\n", v6, (unsigned int)(v4 + 1));
time(&timer);
v7 = (sem_t *)(working_threads + 32 * v4);
*(_QWORD *)(*(_QWORD *)(threads + v3 + 8) + 32LL) = timer;
sem_post(v7);
}
LABEL_9:
pthread_mutex_unlock(&wLock);
pthread_mutex_lock(&qLock);
v8 = v1 + 1;
do
{
v9 = (_DWORD *)q[v8];
q[v8 - 1] = (__int64)v9;
v10 = *v9 != 0;
LOBYTE(v9) = (int)v8++ <= 99;
}
while ( ((unsigned __int8)v9 & v10) != 0 );
pthread_mutex_unlock(&qLock);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // er12
__int64 v2; // r13
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // rcx
const char *v6; // rsi
sem_t *v7; // rdi
__int64 v8; // rax
_DWORD *v9; // rdx
bool v10; // cl
time_t timer; // [rsp+8h] [rbp-30h] BYREF

sleep(t);
while ( 1 )
{
v1 = 0;
sem_wait(&count);
pthread_mutex_lock(&qLock);
v2 = q[0];
if ( *(_DWORD *)(q[1] + 16) < *(_DWORD *)(q[0] + 16) )
{
v2 = q[1];
v1 = 1;
}
pthread_mutex_unlock(&qLock);
sem_wait(&idle_threads);
pthread_mutex_lock(&wLock);
if ( (int)NTHREADS > 0 )
{
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = threads + v3;
if ( *(_DWORD *)(threads + v3 + 16) == 1 )
break;
++v4;
v3 += 24LL;
if ( v4 == NTHREADS )
goto LABEL_9;
}
*(_DWORD *)(v5 + 16) = 0;
v6 = *(const char **)(v2 + 8);
*(_QWORD *)(v5 + 8) = v2;
printf("The file %s is assigned to thread %d\n", v6, (unsigned int)(v4 + 1));
time(&timer);
v7 = (sem_t *)(working_threads + 32 * v4);
*(_QWORD *)(*(_QWORD *)(threads + v3 + 8) + 32LL) = timer;
sem_post(v7);
}
LABEL_9:
pthread_mutex_unlock(&wLock);
pthread_mutex_lock(&qLock);
v8 = v1 + 1;
do
{
v9 = (_DWORD *)q[v8];
q[v8 - 1] = (__int64)v9;
v10 = *v9 != 0;
LOBYTE(v9) = (int)v8++ <= 99;
}
while ( ((unsigned __int8)v9 & v10) != 0 );
pthread_mutex_unlock(&qLock);
}
}

original_code:
void* initSJF(void* ptr)
{
 sleep(t);
 while (1)
       {
  sem_wait(&count);
  pthread_mutex_lock(&qLock);
  struct requestQueue* request = q[0];
  int i=1, k = 0;
  while ( i < 100)
               {
        if ((q[i]->len) < (request->len))
                      {
    request = q[i];
    k = i;
   }
                        break;
                        if (q[i]->client_id == 0)
   i++;
  }
  pthread_mutex_unlock(&qLock);
  sem_wait(&idle_threads);
  pthread_mutex_lock(&wLock);
                time_t aTime;
  int s=0;
  while ( s < NTHREADS)
               {
   if ((threads + s)->flag == 1)
                       {
    (threads + s)->flag = 0;
                                (threads + s)->pointer = request;
    printf("The file %s is assigned to thread %d\n",
      (threads + s)->pointer->reqPath, s + 1);
    time(&aTime);
    (threads + s)->pointer->assignTime = aTime;
    sem_post(&working_threads[s]);
    break;
   }
    s++;
  }
  pthread_mutex_unlock(&wLock);
  pthread_mutex_lock(&qLock);
  int j;
  j = k;
  while ( j < 100)
               {
   q[j] = q[j + 1];
   if (q[j + 1]->client_id == 0)
                       {
    break;
   }
    j++;
  }
  pthread_mutex_unlock(&qLock);
 }
}

canonical_original_code:
void *initSJF(void *ptr)
{
 sleep(t);
 while (1)
       {
  sem_wait(&count);
  pthread_mutex_lock(&qLock);
  struct requestQueue *request = q[0];
  int i = 1; int k = 0;
  while ( i < 100)
               {
        if ((q[i]->len) < (request->len))
                      {
    request = q[i];
    k = i;
   }
                        break;
                        if (q[i]->client_id == 0)
   i++;
  }
  pthread_mutex_unlock(&qLock);
  sem_wait(&idle_threads);
  pthread_mutex_lock(&wLock);
                long aTime;
  int s = 0;
  while ( s < NTHREADS)
               {
   if ((threads + s)->flag == 1)
                       {
    (threads + s)->flag = 0;
                                (threads + s)->pointer = request;
    printf("The file %s is assigned to thread %d\n",
      (threads + s)->pointer->reqPath, s + 1);
    time(&aTime);
    (threads + s)->pointer->assignTime = aTime;
    sem_post(&working_threads[s]);
    break;
   }
    s++;
  }
  pthread_mutex_unlock(&wLock);
  pthread_mutex_lock(&qLock);
  int j;
  j = k;
  while ( j < 100)
               {
   q[j] = q[j + 1];
   if (q[j + 1]->client_id == 0)
                       {
    break;
   }
    j++;
  }
  pthread_mutex_unlock(&qLock);
 }
}

variable_types:
ptr
void *

variable_types: struct requestQueue { int client_id; int reqType; char *reqPath; int len; long receiveTime; long assignTime; } *

variable_types: k int

variable_types: aTime long

variable_types: s int

variable_types: j int

return_type: void *


initFCFS

decompiled_code:
void __fastcall __noreturn initFCFS(void *a1)
{
__int64 v1; // r12
__int64 *v2; // rax
_DWORD *v3; // rdx
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rcx
sem_t *v7; // rdi
time_t timer; // [rsp+8h] [rbp-20h] BYREF

sleep(t);
while ( 1 )
{
sem_wait(&count);
v1 = q[0];
pthread_mutex_lock(&qLock);
v2 = q;
do
{
v3 = (_DWORD *)v2[1];
*v2++ = (__int64)v3;
}
while ( *v3 );
pthread_mutex_unlock(&qLock);
sem_wait(&idle_threads);
pthread_mutex_lock(&wLock);
if ( (int)NTHREADS <= 0 )
{
LABEL_8:
pthread_mutex_unlock(&wLock);
}
else
{
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = threads + v4;
if ( *(_DWORD *)(threads + v4 + 16) == 1 )
break;
++v5;
v4 += 24LL;
if ( v5 == NTHREADS )
goto LABEL_8;
}
*(_DWORD *)(v6 + 16) = 0;
*(_QWORD *)(v6 + 8) = v1;
printf("The file %s is assigned to thread %d\n", *(const char **)(v1 + 8), (unsigned int)(v5 + 1));
time(&timer);
v7 = (sem_t *)(32 * v5 + working_threads);
*(_QWORD *)(*(_QWORD *)(threads + v4 + 8) + 32LL) = timer;
sem_post(v7);
pthread_mutex_unlock(&wLock);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
__int64 v1; // r12
__int64 *v2; // rax
_DWORD *v3; // rdx
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rcx
sem_t *v7; // rdi
time_t timer; // [rsp+8h] [rbp-20h] BYREF

sleep(t);
while ( 1 )
{
sem_wait(&count);
v1 = q[0];
pthread_mutex_lock(&qLock);
v2 = q;
do
{
v3 = (_DWORD *)v2[1];
*v2++ = (__int64)v3;
}
while ( *v3 );
pthread_mutex_unlock(&qLock);
sem_wait(&idle_threads);
pthread_mutex_lock(&wLock);
if ( (int)NTHREADS <= 0 )
{
LABEL_8:
pthread_mutex_unlock(&wLock);
}
else
{
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = threads + v4;
if ( *(_DWORD *)(threads + v4 + 16) == 1 )
break;
++v5;
v4 += 24LL;
if ( v5 == NTHREADS )
goto LABEL_8;
}
*(_DWORD *)(v6 + 16) = 0;
*(_QWORD *)(v6 + 8) = v1;
printf("The file %s is assigned to thread %d\n", *(const char **)(v1 + 8), (unsigned int)(v5 + 1));
time(&timer);
v7 = (sem_t *)(32 * v5 + working_threads);
*(_QWORD *)(*(_QWORD *)(threads + v4 + 8) + 32LL) = timer;
sem_post(v7);
pthread_mutex_unlock(&wLock);
}
}
}

original_code:
void* initFCFS(void* ptr)
{
       sleep(t);
       while (1)
 {
  sem_wait(&count);
  int i=0;
               struct requestQueue* request = q[0];
               pthread_mutex_lock(&qLock);
  while ( i < 100)
               {
   q[i] = q[i + 1];
   if (q[i + 1]->client_id == 0)
                      {
    break;
   }
    i++;
  }
  pthread_mutex_unlock(&qLock);
  sem_wait(&idle_threads);
  pthread_mutex_lock(&wLock);
  int s=0;
  time_t aTime;
  while (s < NTHREADS)
               {
   if ((threads + s)->flag == 1)
                       {
    (threads + s)->flag = 0;
                              (threads + s)->pointer = request;
    printf("The file %s is assigned to thread %d\n",
      (threads + s)->pointer->reqPath, s + 1);
    time(&aTime);
    (threads + s)->pointer->assignTime = aTime;
    sem_post(&working_threads[s]);
    break;
   }
   s++;
  }
  pthread_mutex_unlock(&wLock);
 }
}

canonical_original_code:
void *initFCFS(void *ptr)
{
       sleep(t);
       while (1)
 {
  sem_wait(&count);
  int i = 0;
               struct requestQueue *request = q[0];
               pthread_mutex_lock(&qLock);
  while ( i < 100)
               {
   q[i] = q[i + 1];
   if (q[i + 1]->client_id == 0)
                      {
    break;
   }
    i++;
  }
  pthread_mutex_unlock(&qLock);
  sem_wait(&idle_threads);
  pthread_mutex_lock(&wLock);
  int s = 0;
  long aTime;
  while (s < NTHREADS)
               {
   if ((threads + s)->flag == 1)
                       {
    (threads + s)->flag = 0;
                              (threads + s)->pointer = request;
    printf("The file %s is assigned to thread %d\n",
      (threads + s)->pointer->reqPath, s + 1);
    time(&aTime);
    (threads + s)->pointer->assignTime = aTime;
    sem_post(&working_threads[s]);
    break;
   }
   s++;
  }
  pthread_mutex_unlock(&wLock);
 }
}

user_defined_types: struct requestQueue { int client_id; int reqType; char *reqPath; int len; long receiveTime; long assignTime; }


process

decompiled_code:
void *__fastcall process(char *s)
{
void *v1; // r12
char *v2; // rbp
const char *v3; // rdi
const char *v4; // rbp
char *v5; // r13
int v6; // eax
const char *v7; // r14
int v8; // ebx
int v9; // eax
char *v10; // rax
const char *v11; // rsi
char *v12; // rdi
char *v13; // rax
char *v15; // rax
char *v16; // rax
const char *v17; // rbp
char *v18; // r13
int v19; // eax
const char *v20; // r13
int v21; // edi
char *v22; // rax
const char *v23; // r14
int v24; // ebx
int v25; // eax
char *v26; // r13
char *v27; // r13
int v28; // ebx
int v29; // eax
char *v30; // rax
struct stat v31; // [rsp+0h] [rbp-B8h] BYREF

v1 = malloc(0x28uLL);
*((_DWORD *)v1 + 1) = strcmp(strtok(s, " "), "GET") != 0;
v2 = strtok(0LL, " ");
v3 = v2;
if ( !strchr(v2, 126) )
{
v17 = v2 + 1;
v18 = strchr(v3, 46);
v19 = strlen(v17);
if ( v18 )
{
*((_QWORD *)v1 + 1) = malloc(v19 + 1);
stat(v17, &v31);
strcpy(*((char **)v1 + 1), v17);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
v20 = dir;
v21 = strlen(dir) + v19 + 13;
v22 = (char *)malloc(v21);
*((_QWORD *)v1 + 1) = v22;
strcpy(stpcpy(v22, v20), "/index.html/");
strcat(*((char **)v1 + 1), v17);
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v23 = dir;
v24 = strlen(dir);
v25 = strlen(v17);
v26 = (char *)malloc(v24 + v25 + 2);
*((_QWORD *)v1 + 1) = v26;
*(_WORD *)stpcpy(v26, v23) = 47;
strcpy(v26, v17);
*((_DWORD *)v1 + 4) = 0;
return v1;
}
goto LABEL_7;
}
v4 = v2 + 2;
v5 = strchr(v3, 46);
v6 = strlen(v4);
v7 = dir;
v8 = v6;
v9 = strlen(dir);
if ( !v5 )
{
v15 = (char *)malloc(v9 + v8 + 13);
*((_QWORD *)v1 + 1) = v15;
v16 = stpcpy(v15, v7);
strcpy(stpcpy(v16, v4), "/index.html/");
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v27 = dir;
v28 = strlen(dir);
v29 = strlen(v4);
v30 = (char *)malloc(v28 + v29 + 2);
v11 = v27;
*((_QWORD *)v1 + 1) = v30;
v12 = v30;
goto LABEL_4;
}
LABEL_7:
stat(*((const char **)v1 + 1), &v31);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
v10 = (char *)malloc(v8 + v9 + 2);
v11 = v7;
*((_QWORD *)v1 + 1) = v10;
v12 = v10;
LABEL_4:
v13 = stpcpy(v12, v11);
*(_WORD *)stpcpy(v13, v4) = 47;
*((_DWORD *)v1 + 4) = 0;
return v1;
}

canonical_decompiled_code:
void *__fastcall func5(char *s)
{
void *v1; // r12
char *v2; // rbp
const char *v3; // rdi
const char *v4; // rbp
char *v5; // r13
int v6; // eax
const char *v7; // r14
int v8; // ebx
int v9; // eax
char *v10; // rax
const char *v11; // rsi
char *v12; // rdi
char *v13; // rax
char *v15; // rax
char *v16; // rax
const char *v17; // rbp
char *v18; // r13
int v19; // eax
const char *v20; // r13
int v21; // edi
char *v22; // rax
const char *v23; // r14
int v24; // ebx
int v25; // eax
char *v26; // r13
char *v27; // r13
int v28; // ebx
int v29; // eax
char *v30; // rax
struct stat v31; // [rsp+0h] [rbp-B8h] BYREF

v1 = malloc(0x28uLL);
*((_DWORD *)v1 + 1) = strcmp(strtok(s, " "), "GET") != 0;
v2 = strtok(0LL, " ");
v3 = v2;
if ( !strchr(v2, 126) )
{
v17 = v2 + 1;
v18 = strchr(v3, 46);
v19 = strlen(v17);
if ( v18 )
{
*((_QWORD *)v1 + 1) = malloc(v19 + 1);
stat(v17, &v31);
strcpy(*((char **)v1 + 1), v17);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
v20 = dir;
v21 = strlen(dir) + v19 + 13;
v22 = (char *)malloc(v21);
*((_QWORD *)v1 + 1) = v22;
strcpy(stpcpy(v22, v20), "/index.html/");
strcat(*((char **)v1 + 1), v17);
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v23 = dir;
v24 = strlen(dir);
v25 = strlen(v17);
v26 = (char *)malloc(v24 + v25 + 2);
*((_QWORD *)v1 + 1) = v26;
*(_WORD *)stpcpy(v26, v23) = 47;
strcpy(v26, v17);
*((_DWORD *)v1 + 4) = 0;
return v1;
}
goto LABEL_7;
}
v4 = v2 + 2;
v5 = strchr(v3, 46);
v6 = strlen(v4);
v7 = dir;
v8 = v6;
v9 = strlen(dir);
if ( !v5 )
{
v15 = (char *)malloc(v9 + v8 + 13);
*((_QWORD *)v1 + 1) = v15;
v16 = stpcpy(v15, v7);
strcpy(stpcpy(v16, v4), "/index.html/");
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v27 = dir;
v28 = strlen(dir);
v29 = strlen(v4);
v30 = (char *)malloc(v28 + v29 + 2);
v11 = v27;
*((_QWORD *)v1 + 1) = v30;
v12 = v30;
goto LABEL_4;
}
LABEL_7:
stat(*((const char **)v1 + 1), &v31);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
v10 = (char *)malloc(v8 + v9 + 2);
v11 = v7;
*((_QWORD *)v1 + 1) = v10;
v12 = v10;
LABEL_4:
v13 = stpcpy(v12, v11);
*(_WORD *)stpcpy(v13, v4) = 47;
*((_DWORD *)v1 + 4) = 0;
return v1;
}

original_code:
struct requestQueue* process(char* p)
{
 char *file_name, *request_type;
 int len, len1, len2, len3, fh;
        struct stat buf;
        struct requestQueue* req = (struct requestQueue*) malloc(sizeof(struct requestQueue));
 request_type = strtok(p, " ");
 if (strcmp(request_type, "GET") == 0)
       {
  req->reqType = 0;
 }
       else
       {
  req->reqType = 1;
 }
 file_name = strtok(((void *)0), " ");
 if (strstr(file_name, "~") != ((void *)0))
       {
  if (strstr(file_name, ".") != ((void *)0))
               {
   len2 = strlen(dir);
                       len1 = strlen(&file_name[2]);
   req->reqPath = malloc(len1 + len2 + 2);
   strcpy(req->reqPath, dir);
   strcat(req->reqPath, &file_name[2]);
                        strcat(req->reqPath, "/");
   req->len = 0;
   return req;
  }
               else
               {
   len2 = strlen(dir);
   len3 = strlen(&file_name[2]);
                        len1 = strlen("index.html");
   req->reqPath = malloc(len1 + len2 + len3 + 3);
   strcpy(req->reqPath, dir);
                        strcat(req->reqPath, &file_name[2]);
   strcat(req->reqPath, "/");
   strcat(req->reqPath, "index.html");
   strcat(req->reqPath, "/");
          fh = open(req->reqPath, 00);
   if (fh != -1)
                       {
    stat(req->reqPath, &buf);
    req->len = buf.st_size;
    return req;
   }
                      else
                      {
    len2 = strlen(dir);
                              len1 = strlen(&file_name[2]);
    req->reqPath = malloc(len1 + len2 + 2);
    strcpy(req->reqPath, dir);
                              strcat(req->reqPath, &file_name[2]);
    strcat(req->reqPath, "/");
    req->len = 0;
    return req;
   }
  }
 }
       else
       {
  if (strstr(file_name, ".") != ((void *)0))
               {
   len = strlen(&file_name[1]);
   req->reqPath = malloc(len + 1);
                       stat(&file_name[1], &buf);
   strcpy(req->reqPath, &file_name[1]);
   req->len = buf.st_size;
   return req;
  }
              else
              {
                       len2 = strlen(dir);
   len3 = strlen(&file_name[1]);
   len1 = strlen("index.html");
   req->reqPath = malloc(len1 + len2 + len3 + 3);
   strcpy(req->reqPath, dir);
                        strcat(req->reqPath, "/");
   strcat(req->reqPath, "index.html");
   strcat(req->reqPath, "/");
   strcat(req->reqPath, &file_name[1]);
   fh = open(req->reqPath, 00);
   if (fh != -1)
   {
    stat(req->reqPath, &buf);
    req->len = buf.st_size;
    return req;
   }
   else
   {
    len2 = strlen(dir);
                                len = strlen(&file_name[1]);
    req->reqPath = malloc(len + len2 + 2);
    strcpy(req->reqPath, dir);
    strcat(req->reqPath, "/");
    strcpy(req->reqPath, &file_name[1]);
    req->len = 0;
    return req;
   }
  }
 }
}

canonical_original_code:
struct requestQueue *process(char *p)
{
 char *file_name; char *request_type;
 int len; int len1; int len2; int len3; int fh;
        struct stat buf;
        struct requestQueue *req = (struct requestQueue*) malloc(sizeof(struct requestQueue));
 request_type = strtok(p, " ");
 if (strcmp(request_type, "GET") == 0)
       {
  req->reqType = 0;
 }
       else
       {
  req->reqType = 1;
 }
 file_name = strtok(((void *)0), " ");
 if (strstr(file_name, "~") != ((void *)0))
       {
  if (strstr(file_name, ".") != ((void *)0))
               {
   len2 = strlen(dir);
                       len1 = strlen(&file_name[2]);
   req->reqPath = malloc(len1 + len2 + 2);
   strcpy(req->reqPath, dir);
   strcat(req->reqPath, &file_name[2]);
                        strcat(req->reqPath, "/");
   req->len = 0;
   return req;
  }
               else
               {
   len2 = strlen(dir);
   len3 = strlen(&file_name[2]);
                        len1 = strlen("index.html");
   req->reqPath = malloc(len1 + len2 + len3 + 3);
   strcpy(req->reqPath, dir);
                        strcat(req->reqPath, &file_name[2]);
   strcat(req->reqPath, "/");
   strcat(req->reqPath, "index.html");
   strcat(req->reqPath, "/");
          fh = open(req->reqPath, 00);
   if (fh != -1)
                       {
    stat(req->reqPath, &buf);
    req->len = buf.st_size;
    return req;
   }
                      else
                      {
    len2 = strlen(dir);
                              len1 = strlen(&file_name[2]);
    req->reqPath = malloc(len1 + len2 + 2);
    strcpy(req->reqPath, dir);
                              strcat(req->reqPath, &file_name[2]);
    strcat(req->reqPath, "/");
    req->len = 0;
    return req;
   }
  }
 }
       else
       {
  if (strstr(file_name, ".") != ((void *)0))
               {
   len = strlen(&file_name[1]);
   req->reqPath = malloc(len + 1);
                       stat(&file_name[1], &buf);
   strcpy(req->reqPath, &file_name[1]);
   req->len = buf.st_size;
   return req;
  }
              else
              {
                       len2 = strlen(dir);
   len3 = strlen(&file_name[1]);
   len1 = strlen("index.html");
   req->reqPath = malloc(len1 + len2 + len3 + 3);
   strcpy(req->reqPath, dir);
                        strcat(req->reqPath, "/");
   strcat(req->reqPath, "index.html");
   strcat(req->reqPath, "/");
   strcat(req->reqPath, &file_name[1]);
   fh = open(req->reqPath, 00);
   if (fh != -1)
   {
    stat(req->reqPath, &buf);
    req->len = buf.st_size;
    return req;
   }
   else
   {
    len2 = strlen(dir);
                                len = strlen(&file_name[1]);
    req->reqPath = malloc(len + len2 + 2);
    strcpy(req->reqPath, dir);
    strcat(req->reqPath, "/");
    strcpy(req->reqPath, &file_name[1]);
    req->len = 0;
    return req;
   }
  }
 }
}

variable_types:
p
char *

variable_types: len int

variable_types: fh int

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

return_type: struct requestQueue { int client_id; int reqType; char *reqPath; int len; long receiveTime; long assignTime; } *

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v4; // rsi
__int64 *v5; // rbx
unsigned int v6; // ebp
int v7; // eax
socklen_t v8; // edx
__int64 v9; // rbx
__int64 v10; // rbx
__int64 v11; // rdi
int v12; // eax
int v13; // er12
__int64 v14; // rax
__int64 v15; // rbx
_DWORD *v16; // rax
char *v17; // rsi
socklen_t addr_len; // [rsp+8h] [rbp-4A0h] BYREF
int arg; // [rsp+Ch] [rbp-49Ch] BYREF
pthread_t newthread; // [rsp+10h] [rbp-498h] BYREF
time_t timer; // [rsp+18h] [rbp-490h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-488h] BYREF
struct sockaddr v24; // [rsp+30h] [rbp-478h] BYREF
char dest[80]; // [rsp+40h] [rbp-468h] BYREF
char buf[1048]; // [rsp+90h] [rbp-418h] BYREF

puts("WEB SERVER INITIALIZATION");
opterr = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "d:h:I:p:r:t:n:s");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'I':
goto LABEL_6;
case 'd':
debug = 1;
break;
case 'h':
puts("The usage summary is ");
LABEL_6:
logfile = (__int64)malloc(8uLL);
strcpy((char *)logfile, optarg);
f = 1;
break;
case 'n':
NTHREADS = atoi(optarg);
break;
case 'p':
PORT_NUMBER = atoi(optarg);
break;
case 'r':
dir = (char *)malloc(8uLL);
strcpy(dir, optarg);
break;
case 's':
sched = atoi(optarg);
break;
case 't':
t = atoi(optarg);
break;
default:
puts("Other Options");
break;
}
}
if ( debug == 1 )
{
printf("The port number is %d\n", (unsigned int)PORT_NUMBER);
puts("Enter the debugging mode.");
v17 = dir;
if ( !dir )
{
getcwd(dest, 0x50uLL);
dir = (char *)malloc(0x50uLL);
v17 = strcpy(dir, dest);
}
printf("The root directory is %s\n", v17);
printf("The queueing time is %ds\n", t);
NTHREADS = 1;
}
else
{
if ( f == 1 )
printf("The log file name is %s\n", (const char *)logfile);
printf("The port number is %d\n", (unsigned int)PORT_NUMBER);
v4 = dir;
if ( !dir )
{
if ( getcwd(buf, 0x400uLL) )
fprintf(stdout, "The current working directory is %s\n", buf);
else
perror("getcwd() error");
getcwd(dest, 0x50uLL);
dir = (char *)malloc(0x50uLL);
strcpy(dest, dir);
v4 = buf;
}
printf("The root directory is %s\n", v4);
printf("The number of executable threads are %d\n", NTHREADS);
printf("The queueing time is %ds\n", t);
if ( sched )
puts("The scheduling policy is SJF");
else
puts("The scheduling policy is FCFS");
}
if ( debug )
goto LABEL_22;
if ( fork() >= 0 )
{
setsid();
chdir(dir);
umask(0);
LABEL_22:
v5 = q;
v6 = socket(2, 1, 0);
listen(v6, 100);
*(_WORD *)v24.sa_data = _bswap_16(PORT_NUMBER);
v7 = _bswap_32_constprop_0(v6, &v24);
v24.sa_family = 2;
*(_DWORD *)&v24.sa_data[2] = v7;
bind(v6, &v24, v8);
do
*v5++ = (__int64)malloc(0x28uLL);
while ( v5 != &q[100] );
v9 = (int)NTHREADS;
working_threads = (__int64)malloc(32LL * (int)NTHREADS);
threads = (__int64)malloc(24 * v9);
pthread_mutex_init(&qLock, 0LL);
pthread_cond_init(&wContd, 0LL);
pthread_mutex_init(&wLock, 0LL);
sem_init(&count, 0, 0);
sem_init(&idle_threads, 0, NTHREADS);
if ( (int)NTHREADS > 0 )
{
v10 = 0LL;
do
{
v11 = v10++;
sem_init((sem_t *)(working_threads + 32 * v11), 0, 0);
}
while ( (int)NTHREADS > (int)v10 );
}
for ( arg = 0; arg < (int)NTHREADS; ++arg )
{
pthread_cond_wait(&wContd, &wLock);
pthread_create((pthread_t *)(threads + 24LL * arg), 0LL, (void *(*)(void *))HTTPresponse, &arg);
*(_DWORD *)(threads + 24LL * arg + 16) = 1;
pthread_mutex_lock(&wLock);
pthread_mutex_unlock(&wLock);
}
if ( sched )
pthread_create(&newthread, 0LL, (void *(*)(void *))initSJF, 0LL);
else
pthread_create(&newthread, 0LL, (void *(*)(void *))initFCFS, 0LL);
puts("MY HTTPD WEBSERVER");
while ( 1 )
{
v12 = accept(v6, &addr, &addr_len);
addr_len = 16;
v13 = v12;
if ( !v12 )
{
puts("ERROR - Unable to create socket ");
exit(0);
}
puts("NEW REQUEST ARRIVES.");
recv(v13, buf, 0x400uLL, 0);
pthread_mutex_lock(&qLock);
v14 = 0LL;
while ( 1 )
{
v15 = (int)v14;
if ( !*(_DWORD *)q[v14] )
break;
if ( ++v14 == 100 )
goto LABEL_37;
}
time(&timer);
*(_QWORD *)(q[v15] + 24) = timer;
v16 = process(buf);
q[v15] = (__int64)v16;
*v16 = v13;
sem_post(&count);
LABEL_37:
pthread_mutex_unlock(&qLock);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v4; // rsi
__int64 *v5; // rbx
unsigned int v6; // ebp
int v7; // eax
socklen_t v8; // edx
__int64 v9; // rbx
__int64 v10; // rbx
__int64 v11; // rdi
int v12; // eax
int v13; // er12
__int64 v14; // rax
__int64 v15; // rbx
_DWORD *v16; // rax
char *v17; // rsi
socklen_t addr_len; // [rsp+8h] [rbp-4A0h] BYREF
int arg; // [rsp+Ch] [rbp-49Ch] BYREF
pthread_t newthread; // [rsp+10h] [rbp-498h] BYREF
time_t timer; // [rsp+18h] [rbp-490h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-488h] BYREF
struct sockaddr v24; // [rsp+30h] [rbp-478h] BYREF
char dest[80]; // [rsp+40h] [rbp-468h] BYREF
char buf[1048]; // [rsp+90h] [rbp-418h] BYREF

puts("WEB SERVER INITIALIZATION");
opterr = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "d:h:I:p:r:t:n:s");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'I':
goto LABEL_6;
case 'd':
debug = 1;
break;
case 'h':
puts("The usage summary is ");
LABEL_6:
logfile = (__int64)malloc(8uLL);
strcpy((char *)logfile, optarg);
f = 1;
break;
case 'n':
NTHREADS = atoi(optarg);
break;
case 'p':
PORT_NUMBER = atoi(optarg);
break;
case 'r':
dir = (char *)malloc(8uLL);
strcpy(dir, optarg);
break;
case 's':
sched = atoi(optarg);
break;
case 't':
t = atoi(optarg);
break;
default:
puts("Other Options");
break;
}
}
if ( debug == 1 )
{
printf("The port number is %d\n", (unsigned int)PORT_NUMBER);
puts("Enter the debugging mode.");
v17 = dir;
if ( !dir )
{
getcwd(dest, 0x50uLL);
dir = (char *)malloc(0x50uLL);
v17 = strcpy(dir, dest);
}
printf("The root directory is %s\n", v17);
printf("The queueing time is %ds\n", t);
NTHREADS = 1;
}
else
{
if ( f == 1 )
printf("The log file name is %s\n", (const char *)logfile);
printf("The port number is %d\n", (unsigned int)PORT_NUMBER);
v4 = dir;
if ( !dir )
{
if ( getcwd(buf, 0x400uLL) )
fprintf(stdout, "The current working directory is %s\n", buf);
else
perror("getcwd() error");
getcwd(dest, 0x50uLL);
dir = (char *)malloc(0x50uLL);
strcpy(dest, dir);
v4 = buf;
}
printf("The root directory is %s\n", v4);
printf("The number of executable threads are %d\n", NTHREADS);
printf("The queueing time is %ds\n", t);
if ( sched )
puts("The scheduling policy is SJF");
else
puts("The scheduling policy is FCFS");
}
if ( debug )
goto LABEL_22;
if ( fork() >= 0 )
{
setsid();
chdir(dir);
umask(0);
LABEL_22:
v5 = q;
v6 = socket(2, 1, 0);
listen(v6, 100);
*(_WORD *)v24.sa_data = func0(PORT_NUMBER);
v7 = func4(v6, &v24);
v24.sa_family = 2;
*(_DWORD *)&v24.sa_data[2] = v7;
bind(v6, &v24, v8);
do
*v5++ = (__int64)malloc(0x28uLL);
while ( v5 != &q[100] );
v9 = (int)NTHREADS;
working_threads = (__int64)malloc(32LL * (int)NTHREADS);
threads = (__int64)malloc(24 * v9);
pthread_mutex_init(&qLock, 0LL);
pthread_cond_init(&wContd, 0LL);
pthread_mutex_init(&wLock, 0LL);
sem_init(&count, 0, 0);
sem_init(&idle_threads, 0, NTHREADS);
if ( (int)NTHREADS > 0 )
{
v10 = 0LL;
do
{
v11 = v10++;
sem_init((sem_t *)(working_threads + 32 * v11), 0, 0);
}
while ( (int)NTHREADS > (int)v10 );
}
for ( arg = 0; arg < (int)NTHREADS; ++arg )
{
pthread_cond_wait(&wContd, &wLock);
pthread_create((pthread_t *)(threads + 24LL * arg), 0LL, (void *(*)(void *))HTTPresponse, &arg);
*(_DWORD *)(threads + 24LL * arg + 16) = 1;
pthread_mutex_lock(&wLock);
pthread_mutex_unlock(&wLock);
}
if ( sched )
pthread_create(&newthread, 0LL, (void *(*)(void *))initSJF, 0LL);
else
pthread_create(&newthread, 0LL, (void *(*)(void *))initFCFS, 0LL);
puts("MY HTTPD WEBSERVER");
while ( 1 )
{
v12 = accept(v6, &addr, &addr_len);
addr_len = 16;
v13 = v12;
if ( !v12 )
{
puts("ERROR - Unable to create socket ");
exit(0);
}
puts("NEW REQUEST ARRIVES.");
recv(v13, buf, 0x400uLL, 0);
pthread_mutex_lock(&qLock);
v14 = 0LL;
while ( 1 )
{
v15 = (int)v14;
if ( !*(_DWORD *)q[v14] )
break;
if ( ++v14 == 100 )
goto LABEL_37;
}
time(&timer);
*(_QWORD *)(q[v15] + 24) = timer;
v16 = func5(buf);
q[v15] = (__int64)v16;
*v16 = v13;
sem_post(&count);
LABEL_37:
pthread_mutex_unlock(&qLock);
}
}
return -1;
}

original_code:
int main(int argc, char** argv)
{
 printf("WEB SERVER INITIALIZATION\n");
 opterr = 0;
       int option;
 while ((option = getopt(argc, argv, "d:h:I:p:r:t:n:s")) != -1)
       {
  switch (option)
               {
               case 'd':
                        debug = 1;
                        break;
  case 'h':
                        printf("The usage summary is \n");
               case 'I':
   logfile = (char *) malloc(sizeof optarg);
   strcpy(logfile, optarg);
   f = 1;
   break;
  case 'p':
   PORT_NUMBER = atoi(optarg);
   break;
  case 'r':
   dir = (char *) malloc(sizeof optarg);
   strcpy(dir, optarg);
   break;
  case 't':
   t = atoi(optarg);
   break;
  case 'n':
   NTHREADS = atoi(optarg);
   break;
  case 's':
   sched = atoi(optarg);
   break;
   printf(
     "-d          : Enter debugging mode. That is, do not daemonize, only accept one connection at a time and enable logging to stdout. Without this option, the web server should run as a daemon process in the background.\n");
   printf(
     "-h          : Print a usage summary with all options and exit.\n");
   printf(
                                      "-I file     : Log all requests to the given file.\n");
   printf(
     "-p port     : Listen on the given port. If not provided, myhttpd will listen on port 8080.\n");
   printf(
     "-r dir      : Set the root directory for the http server to dir\n");
   printf(
     "-t time     : Set the queuing time to time seconds. The default should be 60 seconds\n");
   printf(
     "-n threadnum: Set number of threads waiting ready in the execution thread pool to threadnum. The default should be 4 execution threads.\n");
   printf(
     "-s sched    : Set the scheduling policy. It can be either FCFS or SJF. The default will be FCFS.\n");
   exit(1);
   break;
  default:
   printf("Other Options\n");
  }
 }
 char path[80];
 if (debug == 1)
       {
  printf("The port number is %d\n", PORT_NUMBER);
               printf("Enter the debugging mode.\n");
  if (dir != ((void *)0))
               {
   printf("The root directory is %s\n", dir);
  }
                        else
               {
   getcwd(path, sizeof(path));
   dir = (char *) malloc(sizeof path);
   strcpy(dir, path);
   printf("The root directory is %s\n", dir);
  }
  printf("The queueing time is %ds\n", t);
  NTHREADS = 1;
 }
                else
               {
  if (f == 1)
               {
   printf("The log file name is %s\n", logfile);
  }
  printf("The port number is %d\n", PORT_NUMBER);
  if (dir != ((void *)0))
               {
   printf("The root directory is %s\n", dir);
  }
                        else
               {
   char cwd[1024];
       if (getcwd(cwd, sizeof(cwd)) != ((void *)0))
           fprintf(stdout, "The current working directory is %s\n", cwd);
       else
           perror("getcwd() error");
   getcwd(path, sizeof(path));
   dir = (char *) malloc(sizeof path);
   strcpy(path, dir);
   printf("The root directory is %s\n", cwd);
  }
  printf("The number of executable threads are %d\n", NTHREADS);
               printf("The queueing time is %ds\n", t);
  if (sched == 0)
               {
   printf("The scheduling policy is FCFS\n");
  }
                        else
               {
   printf("The scheduling policy is SJF\n");
  }
 }
 if (debug == 0)
       {
       pid_t pid;
             if (pid != 0)
             exit(0);
             else if ((pid = fork()) < 0)
             return (-1);
       setsid();
       chdir(dir);
       umask(0);
 }
      unsigned int client_socket;
      struct sockaddr_in client_address;
      socklen_t address_len;
      unsigned int server_socket;
      struct sockaddr_in server_address;
 server_socket = socket(2, SOCK_STREAM, 0);
 listen(server_socket, 100);
 server_address.sin_port = htons(PORT_NUMBER);
       server_address.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
       server_address.sin_family = 2;
 bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
 int a=0;
 while (a < 100)
 {
  q[a] = (struct requestQueue*) malloc(sizeof(struct requestQueue));
  a++;
 }
        working_threads = (sem_t *) malloc(NTHREADS * sizeof(sem_t));
 threads = (struct currentThread*) malloc(NTHREADS * sizeof(struct currentThread));
        pthread_mutex_init(&qLock, ((void *)0));
        pthread_cond_init(&wContd, ((void *)0));
        pthread_mutex_init(&wLock, ((void *)0));
 sem_init(&count, 0, 0);
        sem_init(&idle_threads, 0, NTHREADS);
 int k=0;
 while(k < NTHREADS)
 {
  sem_init(&working_threads[k], 0, 0);
   k++;
 }
 int index = 0;
 while ( index < NTHREADS )
 {
  pthread_cond_wait(&wContd, &wLock);
               pthread_create(&((threads + index)->thread), ((void *)0), HTTPresponse, &index);
  (threads + index)->flag = 1;
  pthread_mutex_lock(&wLock);
               pthread_mutex_unlock(&wLock);
  index++;
 }
 pthread_t scheduler;
 if (sched == 0)
 {
  pthread_create(&scheduler, ((void *)0), initFCFS, ((void *)0));
 }
 else
 {
  pthread_create(&scheduler, ((void *)0), initSJF, ((void *)0));
 }
 printf(
   "MY HTTPD WEBSERVER\n");
 time_t rTime;
       char in_buf[1024];
 while (1)
 {
  client_socket = accept(server_socket, (struct sockaddr *) &client_address,
    &address_len);
               address_len = sizeof(client_address);
  if (client_socket == 0)
  {
   printf("ERROR - Unable to create socket \n");
   exit(0);
  }
              else
              {
   printf("NEW REQUEST ARRIVES.\n");
   recv(client_socket, in_buf, 1024, 0);
   pthread_mutex_lock(&qLock);
   int j=0;
   while ( j < 100)
   {
    if (q[j]->client_id == 0)
    {
     time(&rTime);
          q[j]->receiveTime = rTime;
                                      q[j] = process(in_buf);
     q[j]->client_id = client_socket;
     sem_post(&count);
     break;
    }
    j++;
   }
   pthread_mutex_unlock(&qLock);
  }
 }
}

canonical_original_code:
int main(int argc, char **argv)
{
 printf("WEB SERVER INITIALIZATION\n");
 opterr = 0;
       int option;
 while ((option = getopt(argc, argv, "d:h:I:p:r:t:n:s")) != -1)
       {
  switch (option)
               {
               case 'd':
                        debug = 1;
                        break;
  case 'h':
                        printf("The usage summary is \n");
               case 'I':
   logfile = (char *) malloc(sizeof optarg);
   strcpy(logfile, optarg);
   f = 1;
   break;
  case 'p':
   PORT_NUMBER = atoi(optarg);
   break;
  case 'r':
   dir = (char *) malloc(sizeof optarg);
   strcpy(dir, optarg);
   break;
  case 't':
   t = atoi(optarg);
   break;
  case 'n':
   NTHREADS = atoi(optarg);
   break;
  case 's':
   sched = atoi(optarg);
   break;
   printf(
     "-d          : Enter debugging mode. That is, do not daemonize, only accept one connection at a time and enable logging to stdout. Without this option, the web server should run as a daemon process in the background.\n");
   printf(
     "-h          : Print a usage summary with all options and exit.\n");
   printf(
                                      "-I file     : Log all requests to the given file.\n");
   printf(
     "-p port     : Listen on the given port. If not provided, myhttpd will listen on port 8080.\n");
   printf(
     "-r dir      : Set the root directory for the http server to dir\n");
   printf(
     "-t time     : Set the queuing time to time seconds. The default should be 60 seconds\n");
   printf(
     "-n threadnum: Set number of threads waiting ready in the execution thread pool to threadnum. The default should be 4 execution threads.\n");
   printf(
     "-s sched    : Set the scheduling policy. It can be either FCFS or SJF. The default will be FCFS.\n");
   exit(1);
   break;
  default:
   printf("Other Options\n");
  }
 }
 char path[80];
 if (debug == 1)
       {
  printf("The port number is %d\n", PORT_NUMBER);
               printf("Enter the debugging mode.\n");
  if (dir != ((void *)0))
               {
   printf("The root directory is %s\n", dir);
  }
                        else
               {
   getcwd(path, sizeof(path));
   dir = (char *) malloc(sizeof path);
   strcpy(dir, path);
   printf("The root directory is %s\n", dir);
  }
  printf("The queueing time is %ds\n", t);
  NTHREADS = 1;
 }
                else
               {
  if (f == 1)
               {
   printf("The log file name is %s\n", logfile);
  }
  printf("The port number is %d\n", PORT_NUMBER);
  if (dir != ((void *)0))
               {
   printf("The root directory is %s\n", dir);
  }
                        else
               {
   char cwd[1024];
       if (getcwd(cwd, sizeof(cwd)) != ((void *)0))
           fprintf(stdout, "The current working directory is %s\n", cwd);
       else
           perror("getcwd() error");
   getcwd(path, sizeof(path));
   dir = (char *) malloc(sizeof path);
   strcpy(path, dir);
   printf("The root directory is %s\n", cwd);
  }
  printf("The number of executable threads are %d\n", NTHREADS);
               printf("The queueing time is %ds\n", t);
  if (sched == 0)
               {
   printf("The scheduling policy is FCFS\n");
  }
                        else
               {
   printf("The scheduling policy is SJF\n");
  }
 }
 if (debug == 0)
       {
       int pid;
             if (pid != 0)
             exit(0);
             else if ((pid = fork()) < 0)
             return (-1);
       setsid();
       chdir(dir);
       umask(0);
 }
      unsigned int client_socket;
      struct sockaddr_in client_address;
      unsigned int address_len;
      unsigned int server_socket;
      struct sockaddr_in server_address;
 server_socket = socket(2, SOCK_STREAM, 0);
 listen(server_socket, 100);
 server_address.sin_port = htons(PORT_NUMBER);
       server_address.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
       server_address.sin_family = 2;
 bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
 int a = 0;
 while (a < 100)
 {
  q[a] = (struct requestQueue *) malloc(sizeof(struct requestQueue));
  a++;
 }
        working_threads = (union sem_t *) malloc(NTHREADS * sizeof(sem_t));
 threads = (struct currentThread *) malloc(NTHREADS * sizeof(struct currentThread));
        pthread_mutex_init(&qLock, ((void *)0));
        pthread_cond_init(&wContd, ((void *)0));
        pthread_mutex_init(&wLock, ((void *)0));
 sem_init(&count, 0, 0);
        sem_init(&idle_threads, 0, NTHREADS);
 int k = 0;
 while(k < NTHREADS)
 {
  sem_init(&working_threads[k], 0, 0);
   k++;
 }
 int index = 0;
 while ( index < NTHREADS )
 {
  pthread_cond_wait(&wContd, &wLock);
               pthread_create(&((threads + index)->thread), ((void *)0), HTTPresponse, &index);
  (threads + index)->flag = 1;
  pthread_mutex_lock(&wLock);
               pthread_mutex_unlock(&wLock);
  index++;
 }
 unsigned long scheduler;
 if (sched == 0)
 {
  pthread_create(&scheduler, ((void *)0), initFCFS, ((void *)0));
 }
 else
 {
  pthread_create(&scheduler, ((void *)0), initSJF, ((void *)0));
 }
 printf(
   "MY HTTPD WEBSERVER\n");
 long rTime;
       char in_buf[1024];
 while (1)
 {
  client_socket = accept(server_socket, (struct sockaddr *) &client_address,
    &address_len);
               address_len = sizeof(client_address);
  if (client_socket == 0)
  {
   printf("ERROR - Unable to create socket \n");
   exit(0);
  }
              else
              {
   printf("NEW REQUEST ARRIVES.\n");
   recv(client_socket, in_buf, 1024, 0);
   pthread_mutex_lock(&qLock);
   int j = 0;
   while ( j < 100)
   {
    if (q[j]->client_id == 0)
    {
     time(&rTime);
          q[j]->receiveTime = rTime;
                                      q[j] = process(in_buf);
     q[j]->client_id = client_socket;
     sem_post(&count);
     break;
    }
    j++;
   }
   pthread_mutex_unlock(&qLock);
  }
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: option int

variable_types:
path
char [80]

variable_types:
cwd
char [1024]

variable_types: pid int

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

variable_types: a int

variable_types: index int

variable_types:
scheduler
unsigned long

variable_types: rTime long

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

user_defined_types: struct currentThread { unsigned long thread; struct requestQueue *pointer; int flag; }

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

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


repo: nixzhu/nixhttpd

decompiled_code:
void __fastcall __noreturn print_error_and_exit(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void print_error_and_exit(const char *s)
{
 perror(s);
 exit(1);
}

canonical_original_code:
void print_error_and_exit(char *s)
{
 perror(s);
 exit(1);
}

variable_types:
s
char *


Accept

decompiled_code:
int __fastcall Accept(int fd, struct sockaddr *addr, socklen_t *addr_len)
{
int result; // eax
int v5; // eax

while ( 1 )
{
result = accept(fd, addr, addr_len);
if ( result >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 103 && v5 != 4 )
print_error_and_exit("accept error");
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int fd, struct sockaddr *addr, socklen_t *addr_len)
{
int result; // eax
int v5; // eax

while ( 1 )
{
result = accept(fd, addr, addr_len);
if ( result >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 103 && v5 != 4 )
func0("accept error");
}
return result;
}

original_code:
int Accept(int fd, struct sockaddr *sa, socklen_t *salen_p)
{
 int n;
again:
 n = accept(fd, sa, salen_p);
 if (n < 0) {
  if (((*__errno_location ()) == 103) || ((*__errno_location ()) == 4))
   goto again;
  else
   print_error_and_exit("accept error");
 }
 return n;
}

canonical_original_code:
int Accept(int fd, struct sockaddr *sa, unsigned int *salen_p)
{
 int n;
again:
 n = accept(fd, sa, salen_p);
 if (n < 0) {
  if (((*__errno_location ()) == 103) || ((*__errno_location ()) == 4))
   goto again;
  else
   print_error_and_exit("accept error");
 }
 return n;
}

variable_types: fd int

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

variable_types: n int


Bind

decompiled_code:
int __fastcall Bind(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = bind(a1, a2, a3);
if ( result < 0 )
print_error_and_exit("bind error");
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = bind(a1, a2, a3);
if ( result < 0 )
func0("bind error");
return result;
}

original_code:
void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
 if (bind(fd, sa, salen) < 0)
  print_error_and_exit("bind error");
}

canonical_original_code:
void Bind(int fd, struct sockaddr *sa, unsigned int salen)
{
 if (bind(fd, sa, salen) < 0)
  print_error_and_exit("bind error");
}

variable_types:
salen
unsigned int


Connect

decompiled_code:
int __fastcall Connect(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = connect(a1, a2, a3);
if ( result < 0 )
print_error_and_exit("connect error");
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = connect(a1, a2, a3);
if ( result < 0 )
func0("connect error");
return result;
}

original_code:
void Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
 if (connect(fd, sa, salen) < 0)
  print_error_and_exit("connect error");
}

canonical_original_code:
void Connect(int fd, struct sockaddr *sa, unsigned int salen)
{
 if (connect(fd, sa, salen) < 0)
  print_error_and_exit("connect error");
}


Listen

decompiled_code:
int __fastcall Listen(int a1, int a2)
{
int result; // eax

result = listen(a1, a2);
if ( result < 0 )
print_error_and_exit("listen error");
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, int a2)
{
int result; // eax

result = listen(a1, a2);
if ( result < 0 )
func0("listen error");
return result;
}

original_code:
void Listen(int fd, int backlog)
{
 if (listen(fd, backlog) < 0)
  print_error_and_exit("listen error");
}

variable_types: backlog int


Socket

decompiled_code:
int __fastcall Socket(int a1, int a2, int a3)
{
int result; // eax

result = socket(a1, a2, a3);
if ( result < 0 )
print_error_and_exit("socket error");
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1, int a2, int a3)
{
int result; // eax

result = socket(a1, a2, a3);
if ( result < 0 )
func0("socket error");
return result;
}

original_code:
int Socket(int family, int type, int protocol)
{
 int n;
 if ((n = socket(family, type, protocol)) < 0)
  print_error_and_exit("socket error");
 return n;
}

variable_types: family int

variable_types: type int

variable_types: protocol int


Read

decompiled_code:
ssize_t __fastcall Read(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // r12

do
v4 = read(fd, buf, nbytes);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func6(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // r12

do
v4 = read(fd, buf, nbytes);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

original_code:
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
 ssize_t n;
again:
 if((n = read(fd, ptr, nbytes)) == -1) {
  if ((*__errno_location ()) == 4)
   goto again;
  else
   return -1;
 }
 return n;
}

canonical_original_code:
long Read(int fd, void *ptr, unsigned long nbytes)
{
 long n;
again:
 if((n = read(fd, ptr, nbytes)) == -1) {
  if ((*__errno_location ()) == 4)
   goto again;
  else
   return -1;
 }
 return n;
}

variable_types:
ptr
void *

variable_types:
nbytes
unsigned long

variable_types: n long

return_type: long


Write

decompiled_code:
ssize_t __fastcall Write(int fd, void *buf, size_t n)
{
ssize_t v4; // r12

do
v4 = write(fd, buf, n);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func7(int fd, void *buf, size_t n)
{
ssize_t v4; // r12

do
v4 = write(fd, buf, n);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

original_code:
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
 ssize_t n;
again:
 if((n = write(fd, ptr, nbytes)) == -1) {
  if ((*__errno_location ()) == 4)
   goto again;
  else
   return -1;
 }
 return n;
}

canonical_original_code:
long Write(int fd, void *ptr, unsigned long nbytes)
{
 long n;
again:
 if((n = write(fd, ptr, nbytes)) == -1) {
  if ((*__errno_location ()) == 4)
   goto again;
  else
   return -1;
 }
 return n;
}


Close

decompiled_code:
int __fastcall Close(int a1)
{
int result; // eax

result = close(a1);
if ( result == -1 )
print_error_and_exit("close error");
return result;
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
int result; // eax

result = close(a1);
if ( result == -1 )
func0("close error");
return result;
}

original_code:
void Close(int fd)
{
 if (close(fd) == -1)
  print_error_and_exit("close error");
}


repo: njh/mqtt-callerid

decompiled_code:
__int64 __fastcall check_checksum(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rax
int v3; // edx

v2 = a1;
v3 = 0;
do
LOBYTE(v3) = *v2++ + v3;
while ( &a1[a2] != v2 );
return v3 + (unsigned int)(unsigned __int8)a1[a2];
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rax
int v3; // edx

v2 = a1;
v3 = 0;
do
LOBYTE(v3) = *v2++ + v3;
while ( &a1[a2] != v2 );
return v3 + (unsigned int)(unsigned __int8)a1[a2];
}

original_code:
static int8_t check_checksum(const uint8_t* buf, size_t len)
{
    int8_t checksum = 0;
    int i;
    for(i=0; i<len; i++) {
        checksum += (int8_t)buf[i];
    }
    return buf[len]+checksum;
}

canonical_original_code:
static char check_checksum(unsigned char *buf, unsigned long len)
{
    char checksum = 0;
    int i;
    for(i=0; i<len; i++) {
        checksum += (char)buf[i];
    }
    return buf[len]+checksum;
}

variable_types:
buf
unsigned char *

variable_types:
len
unsigned long

variable_types: checksum char

variable_types: i int

return_type: char


decompiled_code:
int __fastcall parse_ascii_num(__int16 *a1)
{
__int16 v1; // ax
char nptr[2]; // [rsp+Dh] [rbp-Bh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

v1 = *a1;
v4 = 0;
*(_WORD *)nptr = v1;
return atoi(nptr);
}

canonical_decompiled_code:
int __fastcall func1(__int16 *a1)
{
__int16 v1; // ax
char nptr[2]; // [rsp+Dh] [rbp-Bh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

v1 = *a1;
v4 = 0;
*(_WORD *)nptr = v1;
return atoi(nptr);
}

original_code:
static int parse_ascii_num(const uint8_t* str)
{
    char num[3];
    num[0] = str[0];
    num[1] = str[1];
    num[2] = '\0';
    return atoi(num);
}

canonical_original_code:
static int parse_ascii_num(unsigned char *str)
{
    char num[3];
    num[0] = str[0];
    num[1] = str[1];
    num[2] = '\0';
    return atoi(num);
}

variable_types:
str
unsigned char *

variable_types:
num
char [3]


decompiled_code:
__int64 __fastcall cid_open_serial(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

v1 = open(a1, 2304);
v2 = v1;
if ( v1 < 0 )
{
perror("Unable to open serial port");
}
else
{
fcntl(v1, 4, 0LL);
tcgetattr(v2, &v4);
cfsetispeed(&v4, 9u);
cfsetospeed(&v4, 9u);
v4.c_cflag = v4.c_cflag & 0xFFFFFE8F | 0x30;
tcsetattr(v2, 0, &v4);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

v1 = open(a1, 2304);
v2 = v1;
if ( v1 < 0 )
{
perror("Unable to open serial port");
}
else
{
fcntl(v1, 4, 0LL);
tcgetattr(v2, &v4);
cfsetispeed(&v4, 9u);
cfsetospeed(&v4, 9u);
v4.c_cflag = v4.c_cflag & 0xFFFFFE8F | 0x30;
tcsetattr(v2, 0, &v4);
}
return v2;
}

original_code:
int cid_open_serial(const char* dev)
{
    struct termios options;
    int fd;
    fd = open(dev, 00 | 0400 | 04000);
    if (fd < 0) {
        perror("Unable to open serial port");
        return fd;
    }
    fcntl(fd, 4, 0);
    tcgetattr(fd, &options);
    cfsetispeed(&options, 0000011);
    cfsetospeed(&options, 0000011);
    options.c_cflag &= ~0000400;
    options.c_cflag &= ~0000100;
    options.c_cflag &= ~0000060;
    options.c_cflag |= 0000060;
    tcsetattr(fd, 0, &options);
    return fd;
}

canonical_original_code:
int cid_open_serial(char *dev)
{
    struct termios options;
    int fd;
    fd = open(dev, 00 | 0400 | 04000);
    if (fd < 0) {
        perror("Unable to open serial port");
        return fd;
    }
    fcntl(fd, 4, 0);
    tcgetattr(fd, &options);
    cfsetispeed(&options, 0000011);
    cfsetospeed(&options, 0000011);
    options.c_cflag &= ~0000400;
    options.c_cflag &= ~0000100;
    options.c_cflag &= ~0000060;
    options.c_cflag |= 0000060;
    tcsetattr(fd, 0, &options);
    return fd;
}

variable_types:
dev
char *

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

variable_types: fd int