drawfigure

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

v7 = a1;
v8 = HIDWORD(a1);
if ( a5 && rows != a5 )
v7 = (int)(float)((float)((float)rows / (float)a5) * (float)(int)a1);
if ( a6 && cols != a6 )
v8 = (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, v7, v8) != -1 )
waddch(stdscr, (unsigned int)a2);
return (v8 << 32) | v7;
}

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

v7 = a1;
v8 = HIDWORD(a1);
if ( a5 && rows != a5 )
v7 = (int)(float)((float)((float)rows / (float)a5) * (float)(int)a1);
if ( a6 && cols != a6 )
v8 = (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, v7, v8) != -1 )
waddch(stdscr, (unsigned int)a2);
return (v8 << 32) | v7;
}

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


setpos

repo: nissafors/lwrace

decompiled_code:
__int64 __fastcall setpos(int a1, int *a2, double *a3, double *a4, double *a5)
{
double v8; // xmm0_8
double v9; // r15
double v10; // xmm0_8
__int64 result; // rax
double v12; // xmm0_8
double v13; // xmm0_8
double v14; // xmm0_8
double v15; // xmm0_8
double v16; // xmm0_8
double v17; // xmm0_8
double v18; // xmm0_8

v8 = *a4 * *a4 + *a3 * *a3;
if ( v8 < 0.0 )
v9 = sqrt(v8);
else
v9 = sqrt(v8);
switch ( a1 )
{
case 1:
v10 = getnow();
result = 0LL;
if ( *a3 <= v10 - *a5 )
{
--*a2;
goto LABEL_7;
}
break;
case 2:
v12 = getnow();
result = 0LL;
if ( *a3 <= v12 - *a5 )
{
++*a2;
goto LABEL_7;
}
break;
case 3:
v14 = getnow();
result = 0LL;
if ( *a4 <= v14 - *a5 )
{
--a2[1];
goto LABEL_7;
}
break;
case 4:
v13 = getnow();
result = 0LL;
if ( *a4 <= v13 - *a5 )
{
++a2[1];
goto LABEL_7;
}
break;
case 5:
v15 = getnow();
result = 0LL;
if ( v9 <= v15 - *a5 )
{
--*a2;
++a2[1];
goto LABEL_7;
}
break;
case 6:
v16 = getnow();
result = 0LL;
if ( v9 <= v16 - *a5 )
{
--*a2;
--a2[1];
goto LABEL_7;
}
break;
case 7:
v17 = getnow();
result = 0LL;
if ( v9 <= v17 - *a5 )
{
++*a2;
++a2[1];
goto LABEL_7;
}
break;
case 8:
v18 = getnow();
result = 0LL;
if ( v9 <= v18 - *a5 )
{
++*a2;
--a2[1];
LABEL_7:
if ( *a2 < 0 )
*a2 = 0;
if ( *a2 >= rows - 1 )
*a2 = rows - 1;
if ( a2[1] < 0 )
a2[1] = 0;
if ( a2[1] >= cols - 1 )
a2[1] = cols - 1;
*a5 = getnow();
result = 1LL;
}
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int *a2, double *a3, double *a4, double *a5)
{
double v8; // xmm0_8
double v9; // r15
double v10; // xmm0_8
__int64 result; // rax
double v12; // xmm0_8
double v13; // xmm0_8
double v14; // xmm0_8
double v15; // xmm0_8
double v16; // xmm0_8
double v17; // xmm0_8
double v18; // xmm0_8

v8 = *a4 * *a4 + *a3 * *a3;
if ( v8 < 0.0 )
v9 = sqrt(v8);
else
v9 = sqrt(v8);
switch ( a1 )
{
case 1:
v10 = getnow();
result = 0LL;
if ( *a3 <= v10 - *a5 )
{
--*a2;
goto LABEL_7;
}
break;
case 2:
v12 = getnow();
result = 0LL;
if ( *a3 <= v12 - *a5 )
{
++*a2;
goto LABEL_7;
}
break;
case 3:
v14 = getnow();
result = 0LL;
if ( *a4 <= v14 - *a5 )
{
--a2[1];
goto LABEL_7;
}
break;
case 4:
v13 = getnow();
result = 0LL;
if ( *a4 <= v13 - *a5 )
{
++a2[1];
goto LABEL_7;
}
break;
case 5:
v15 = getnow();
result = 0LL;
if ( v9 <= v15 - *a5 )
{
--*a2;
++a2[1];
goto LABEL_7;
}
break;
case 6:
v16 = getnow();
result = 0LL;
if ( v9 <= v16 - *a5 )
{
--*a2;
--a2[1];
goto LABEL_7;
}
break;
case 7:
v17 = getnow();
result = 0LL;
if ( v9 <= v17 - *a5 )
{
++*a2;
++a2[1];
goto LABEL_7;
}
break;
case 8:
v18 = getnow();
result = 0LL;
if ( v9 <= v18 - *a5 )
{
++*a2;
--a2[1];
LABEL_7:
if ( *a2 < 0 )
*a2 = 0;
if ( *a2 >= rows - 1 )
*a2 = rows - 1;
if ( a2[1] < 0 )
a2[1] = 0;
if ( a2[1] >= cols - 1 )
a2[1] = cols - 1;
*a5 = getnow();
result = 1LL;
}
break;
default:
result = 0LL;
break;
}
return result;
}

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

return_type: int


treasures

decompiled_code:
__int64 __fastcall treasures(__int64 a1)
{
__int64 v1; // rax
int v2; // esi
__int64 result; // rax
int v4; // edx

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

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
int v2; // esi
__int64 result; // rax
int v4; // edx

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

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; // edx
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

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

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

if ( *s1 == 39 && s1[2] == 39 && strlen(s1) == 3 )
{
*a2 = s1[1];
result = 0LL;
}
else
{
v2 = func0(s1);
v5[0] = v2;
if ( v2 == -1 )
{
v4 = _isoc99_sscanf(s1, &unk_351, v5);
result = 1LL;
if ( v4 == 1 )
{
*a2 = v5[0];
result = 0LL;
}
}
else
{
*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 = (double)(int)result * 0.5 + 1.5;
player_col_delay = v1;
enemy_to_player_delay_ratio = (double)(int)result * 0.125 + 1.125;
player_row_delay = v1 * row_to_col_delay_ratio;
enemy_col_delay = v1 * enemy_to_player_delay_ratio;
enemy_row_delay = v1 * enemy_to_player_delay_ratio * 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 = (double)(int)result * 0.5 + 1.5;
player_col_delay = v1;
enemy_to_player_delay_ratio = (double)(int)result * 0.125 + 1.125;
player_row_delay = v1 * row_to_col_delay_ratio;
enemy_col_delay = v1 * enemy_to_player_delay_ratio;
enemy_row_delay = v1 * enemy_to_player_delay_ratio * 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)
{
__int64 v1; // r12
_DWORD *v2; // rax
__int64 v3; // r14
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi
time_t timer; // [rsp+8h] [rbp-30h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
__int64 v1; // r12
_DWORD *v2; // rax
__int64 v3; // r14
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi
time_t timer; // [rsp+8h] [rbp-30h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
__int64 v1; // r12
__int64 *v2; // rax
_DWORD *v3; // rdx
__int64 v4; // rbp
__int64 v5; // rbx
__int64 v6; // rsi
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 )
{
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = threads + v4;
if ( *(_DWORD *)(threads + v4 + 16) == 1 )
break;
++v5;
v4 += 24LL;
if ( v5 == NTHREADS )
goto LABEL_3;
}
*(_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);
*(_QWORD *)(*(_QWORD *)(threads + v4 + 8) + 32LL) = timer;
sem_post((sem_t *)(working_threads + 32 * v5));
}
LABEL_3:
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; // rbp
char *v2; // rbx
const char *v3; // r13
int v4; // er12
const char *v5; // rbx
int v6; // eax
char *v7; // r12
const char *v9; // r13
int v10; // er12
const char *v11; // rbx
int v12; // eax
char *v13; // r12
const char *v14; // r12
const char *v15; // r13
int v16; // er12
int v17; // eax
char *v18; // r12
const char *v19; // rbx
int v20; // eax
const char *v21; // r13
int v22; // er12
const char *v23; // rbx
int v24; // eax
char *v25; // r12
const char *v26; // r12
const char *v27; // r13
int v28; // er12
int v29; // eax
char *v30; // r12
struct stat v31; // [rsp+0h] [rbp-B8h] BYREF

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

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

v1 = malloc(0x28uLL);
*((_DWORD *)v1 + 1) = strcmp(strtok(s, " "), "GET") != 0;
v2 = strtok(0LL, " ");
if ( !strchr(v2, 126) )
{
if ( strchr(v2, 46) )
{
v19 = v2 + 1;
v20 = strlen(v19);
*((_QWORD *)v1 + 1) = malloc(v20 + 1);
stat(v19, &v31);
strcpy(*((char **)v1 + 1), v19);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
v21 = dir;
v22 = strlen(dir);
v23 = v2 + 1;
v24 = strlen(v23);
v25 = (char *)malloc(v22 + v24 + 13);
*((_QWORD *)v1 + 1) = v25;
strcpy(v25, v21);
*(_WORD *)&v25[strlen(v25)] = 47;
strcat(v25, "index.html");
v26 = (const char *)*((_QWORD *)v1 + 1);
*(_WORD *)&v26[strlen(v26)] = 47;
strcat(*((char **)v1 + 1), v23);
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v27 = dir;
v28 = strlen(dir);
v29 = strlen(v23);
v30 = (char *)malloc(v28 + v29 + 2);
*((_QWORD *)v1 + 1) = v30;
strcpy(v30, v27);
*(_WORD *)&v30[strlen(v30)] = 47;
strcpy(v30, v23);
*((_DWORD *)v1 + 4) = 0;
return v1;
}
LABEL_6:
stat(*((const char **)v1 + 1), &v31);
*((_DWORD *)v1 + 4) = v31.st_size;
return v1;
}
if ( !strchr(v2, 46) )
{
v9 = dir;
v10 = strlen(dir);
v11 = v2 + 2;
v12 = strlen(v11);
v13 = (char *)malloc(v10 + v12 + 13);
*((_QWORD *)v1 + 1) = v13;
strcpy(v13, v9);
strcat(v13, v11);
*(_WORD *)&v13[strlen(v13)] = 47;
strcat(v13, "index.html");
v14 = (const char *)*((_QWORD *)v1 + 1);
*(_WORD *)&v14[strlen(v14)] = 47;
if ( open(*((const char **)v1 + 1), 0) == -1 )
{
v15 = dir;
v16 = strlen(dir);
v17 = strlen(v11);
v18 = (char *)malloc(v16 + v17 + 2);
*((_QWORD *)v1 + 1) = v18;
strcpy(v18, v15);
strcat(v18, v11);
*(_WORD *)&v18[strlen(v18)] = 47;
*((_DWORD *)v1 + 4) = 0;
return v1;
}
goto LABEL_6;
}
v3 = dir;
v4 = strlen(dir);
v5 = v2 + 2;
v6 = strlen(v5);
v7 = (char *)malloc(v4 + v6 + 2);
*((_QWORD *)v1 + 1) = v7;
strcpy(v7, v3);
strcat(v7, v5);
*(_WORD *)&v7[strlen(v7)] = 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
int v4; // ebp
__int64 *v5; // rbx
__int64 v6; // rbx
__int64 v7; // rbx
char *v8; // rbx
_DWORD *v9; // rax
int v10; // er12
__int64 v11; // rax
int v12; // ebx
char v14[1024]; // [rsp+0h] [rbp-8B8h] BYREF
char s[1032]; // [rsp+400h] [rbp-4B8h] BYREF
time_t timer; // [rsp+808h] [rbp-B0h] BYREF
pthread_t newthread; // [rsp+810h] [rbp-A8h] BYREF
int arg; // [rsp+81Ch] [rbp-9Ch] BYREF
struct sockaddr addr; // [rsp+820h] [rbp-98h] BYREF
socklen_t addr_len; // [rsp+83Ch] [rbp-7Ch] BYREF
struct sockaddr v21; // [rsp+840h] [rbp-78h] BYREF
char buf[104]; // [rsp+850h] [rbp-68h] 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_4;
case 'd':
debug = 1;
break;
case 'h':
puts("The usage summary is ");
LABEL_4:
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.");
if ( dir )
{
printf("The root directory is %s\n", dir);
}
else
{
getcwd(buf, 0x50uLL);
v8 = (char *)malloc(0x50uLL);
dir = v8;
strcpy(v8, buf);
printf("The root directory is %s\n", v8);
}
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);
if ( dir )
{
printf("The root directory is %s\n", dir);
}
else
{
if ( getcwd(v14, 0x400uLL) )
fprintf(stdout, "The current working directory is %s\n", v14);
else
perror("getcwd() error");
getcwd(buf, 0x50uLL);
dir = (char *)malloc(0x50uLL);
strcpy(buf, dir);
printf("The root directory is %s\n", v14);
}
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_23;
if ( fork() >= 0 )
{
setsid();
chdir(dir);
umask(0);
LABEL_23:
v4 = socket(2, 1, 0);
listen(v4, 100);
*(_WORD *)addr.sa_data = _bswap_16(PORT_NUMBER);
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
addr.sa_family = 2;
bind(v4, &addr, 0x10u);
v5 = q;
do
*v5++ = (__int64)malloc(0x28uLL);
while ( v5 != (__int64 *)(unsigned int)((_DWORD)q + 800) );
v6 = (int)NTHREADS;
working_threads = (__int64)malloc(32LL * (int)NTHREADS);
threads = (__int64)malloc(24 * v6);
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 )
{
v7 = 0LL;
do
sem_init((sem_t *)(working_threads + 32 * v7++), 0, 0);
while ( (int)NTHREADS > (int)v7 );
}
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 )
{
v10 = accept(v4, &v21, &addr_len);
addr_len = 16;
if ( !v10 )
{
puts("ERROR - Unable to create socket ");
exit(0);
}
puts("NEW REQUEST ARRIVES.");
recv(v10, s, 0x400uLL, 0);
pthread_mutex_lock(&qLock);
v11 = 0LL;
while ( 1 )
{
v12 = v11;
if ( !*(_DWORD *)q[v11] )
break;
if ( ++v11 == 100 )
goto LABEL_45;
}
time(&timer);
*(_QWORD *)(q[v12] + 24) = timer;
v9 = process(s);
q[v12] = (__int64)v9;
*v9 = v10;
sem_post(&count);
LABEL_45:
pthread_mutex_unlock(&qLock);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
__int64 *v5; // rbx
__int64 v6; // rbx
__int64 v7; // rbx
char *v8; // rbx
_DWORD *v9; // rax
int v10; // er12
__int64 v11; // rax
int v12; // ebx
char v14[1024]; // [rsp+0h] [rbp-8B8h] BYREF
char s[1032]; // [rsp+400h] [rbp-4B8h] BYREF
time_t timer; // [rsp+808h] [rbp-B0h] BYREF
pthread_t newthread; // [rsp+810h] [rbp-A8h] BYREF
int arg; // [rsp+81Ch] [rbp-9Ch] BYREF
struct sockaddr addr; // [rsp+820h] [rbp-98h] BYREF
socklen_t addr_len; // [rsp+83Ch] [rbp-7Ch] BYREF
struct sockaddr v21; // [rsp+840h] [rbp-78h] BYREF
char buf[104]; // [rsp+850h] [rbp-68h] 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_4;
case 'd':
debug = 1;
break;
case 'h':
puts("The usage summary is ");
LABEL_4:
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.");
if ( dir )
{
printf("The root directory is %s\n", dir);
}
else
{
getcwd(buf, 0x50uLL);
v8 = (char *)malloc(0x50uLL);
dir = v8;
strcpy(v8, buf);
printf("The root directory is %s\n", v8);
}
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);
if ( dir )
{
printf("The root directory is %s\n", dir);
}
else
{
if ( getcwd(v14, 0x400uLL) )
fprintf(stdout, "The current working directory is %s\n", v14);
else
perror("getcwd() error");
getcwd(buf, 0x50uLL);
dir = (char *)malloc(0x50uLL);
strcpy(buf, dir);
printf("The root directory is %s\n", v14);
}
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_23;
if ( fork() >= 0 )
{
setsid();
chdir(dir);
umask(0);
LABEL_23:
v4 = socket(2, 1, 0);
listen(v4, 100);
*(_WORD *)addr.sa_data = func0(PORT_NUMBER);
*(_DWORD *)&addr.sa_data[2] = func1(0);
addr.sa_family = 2;
bind(v4, &addr, 0x10u);
v5 = q;
do
*v5++ = (__int64)malloc(0x28uLL);
while ( v5 != (__int64 *)(unsigned int)((_DWORD)q + 800) );
v6 = (int)NTHREADS;
working_threads = (__int64)malloc(32LL * (int)NTHREADS);
threads = (__int64)malloc(24 * v6);
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 )
{
v7 = 0LL;
do
sem_init((sem_t *)(working_threads + 32 * v7++), 0, 0);
while ( (int)NTHREADS > (int)v7 );
}
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 )
{
v10 = accept(v4, &v21, &addr_len);
addr_len = 16;
if ( !v10 )
{
puts("ERROR - Unable to create socket ");
exit(0);
}
puts("NEW REQUEST ARRIVES.");
recv(v10, s, 0x400uLL, 0);
pthread_mutex_lock(&qLock);
v11 = 0LL;
while ( 1 )
{
v12 = v11;
if ( !*(_DWORD *)q[v11] )
break;
if ( ++v11 == 100 )
goto LABEL_45;
}
time(&timer);
*(_QWORD *)(q[v12] + 24) = timer;
v9 = func5(s);
q[v12] = (__int64)v9;
*v9 = v10;
sem_post(&count);
LABEL_45:
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; // rbx

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; // rbx

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; // rbx

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; // rbx

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:
char __fastcall check_checksum(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rax
char v3; // dl

if ( a2 )
{
v2 = a1;
v3 = 0;
do
v3 += *v2++;
while ( v2 != &a1[a2] );
}
else
{
v3 = 0;
}
return a1[a2] + v3;
}

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

if ( a2 )
{
v2 = a1;
v3 = 0;
do
v3 += *v2++;
while ( v2 != &a1[a2] );
}
else
{
v3 = 0;
}
return a1[a2] + v3;
}

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(char *a1)
{
char nptr[11]; // [rsp+Dh] [rbp-Bh] BYREF

nptr[0] = *a1;
nptr[1] = a1[1];
nptr[2] = 0;
return atoi(nptr);
}

canonical_decompiled_code:
int __fastcall func1(char *a1)
{
char nptr[11]; // [rsp+Dh] [rbp-Bh] BYREF

nptr[0] = *a1;
nptr[1] = a1[1];
nptr[2] = 0;
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; // ebx
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; // ebx
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


original_code:
int cid_parse_buffer(callerid_t *cid, const uint8_t* buf, size_t len)
{
    size_t seizure_count = 0;
    int end;
    int i=0;
    while (i<len) {
        if (buf[i] == 0x55) {
            break;
        } else {
            i++;
        }
    }
    while (i<len) {
        if (buf[i] == 0x55 || buf[i] == 0xD5) {
            seizure_count++;
            i++;
        } else {
            break;
        }
    }
    if (seizure_count < 12) {
        printf("Failed to find enough seizure bytes\n");
        return -1;
    }
    if (buf[i] != 0x80) {
        printf("Message type is not 0x80 (CLIP)\n");
        return -2;
    } else {
        i++;
    }
    end = buf[i] + i;
    if (end >= len) {
        printf("Not enough bytes in buffer\n");
        return -3;
    } else {
        i++;
    }
    if (check_checksum(&buf[i-2], buf[i-1]+2)) {
        printf("Error: checksum did not validate\n");
        return -4;
    }
    while (i < end) {
        int param_type = buf[i++];
        int param_len = buf[i++];
        switch(param_type) {
            case CID_PARAM_DATE_TIME:
              cid->month = parse_ascii_num(&buf[i]);
              cid->day = parse_ascii_num(&buf[i+2]);
              cid->hour = parse_ascii_num(&buf[i+4]);
              cid->min = parse_ascii_num(&buf[i+6]);
            break;
            case CID_PARAM_CALLER:
              memcpy(cid->caller, &buf[i], param_len);
              cid->caller[param_len] = '\0';
            break;
            case CID_PARAM_CALLED:
              memcpy(cid->called, &buf[i], param_len);
              cid->called[param_len] = '\0';
            break;
            case CID_PARAM_REASON_DN:
              cid->reason_dn = buf[i];
            break;
            case CID_PARAM_NAME:
              memcpy(cid->name, &buf[i], param_len);
              cid->called[param_len] = '\0';
            break;
            case CID_PARAM_REASON_NAME:
              cid->reason_name = buf[i];
            break;
            case CID_PARAM_CALL_TYPE:
              cid->call_type = buf[i];
            break;
            default:
              fprintf(stderr, "Warning: unknown param type: 0x%2.2x\n", param_type);
            break;
        }
        i += param_len;
    }
    return 0;
}

canonical_original_code:
int cid_parse_buffer(struct callerid_t *cid, unsigned char *buf, unsigned long len)
{
    unsigned long seizure_count = 0;
    int end;
    int i = 0;
    while (i<len) {
        if (buf[i] == 0x55) {
            break;
        } else {
            i++;
        }
    }
    while (i<len) {
        if (buf[i] == 0x55 || buf[i] == 0xD5) {
            seizure_count++;
            i++;
        } else {
            break;
        }
    }
    if (seizure_count < 12) {
        printf("Failed to find enough seizure bytes\n");
        return -1;
    }
    if (buf[i] != 0x80) {
        printf("Message type is not 0x80 (CLIP)\n");
        return -2;
    } else {
        i++;
    }
    end = buf[i] + i;
    if (end >= len) {
        printf("Not enough bytes in buffer\n");
        return -3;
    } else {
        i++;
    }
    if (check_checksum(&buf[i-2], buf[i-1]+2)) {
        printf("Error: checksum did not validate\n");
        return -4;
    }
    while (i < end) {
        int param_type = buf[i++];
        int param_len = buf[i++];
        switch(param_type) {
            case CID_PARAM_DATE_TIME:
              cid->month = parse_ascii_num(&buf[i]);
              cid->day = parse_ascii_num(&buf[i+2]);
              cid->hour = parse_ascii_num(&buf[i+4]);
              cid->min = parse_ascii_num(&buf[i+6]);
            break;
            case CID_PARAM_CALLER:
              memcpy(cid->caller, &buf[i], param_len);
              cid->caller[param_len] = '\0';
            break;
            case CID_PARAM_CALLED:
              memcpy(cid->called, &buf[i], param_len);
              cid->called[param_len] = '\0';
            break;
            case CID_PARAM_REASON_DN:
              cid->reason_dn = buf[i];
            break;
            case CID_PARAM_NAME:
              memcpy(cid->name, &buf[i], param_len);
              cid->called[param_len] = '\0';
            break;
            case CID_PARAM_REASON_NAME:
              cid->reason_name = buf[i];
            break;
            case CID_PARAM_CALL_TYPE:
              cid->call_type = buf[i];
            break;
            default:
              fprintf(stderr, "Warning: unknown param type: 0x%2.2x\n", param_type);
            break;
        }
        i += param_len;
    }
    return 0;
}

variable_types: struct callerid_t { int call_type; int month; int day; int hour; int min; char caller[19]; char called[19]; char reason_dn; char name[21]; char reason_name; } *

variable_types: end int


zasady

repo: nkopa/GSuma

decompiled_code:
int zasady()
{
puts("\n\n\n ZASADY GRY:\n");
return printf(
"Gra polega na podawaniu kolejno przez gracza i komputer jednej liczby: 1, 2 lub 3, ktore sa do siebie dodawan"
"e. Gra konczy sie, gdy suma liczb osiagne %i. Wygrywa ten, kto poda ostatnia liczbe do sumy.\n"
"\n",
20LL);
}

canonical_decompiled_code:
int func7()
{
puts("\n\n\n ZASADY GRY:\n");
return printf(
"Gra polega na podawaniu kolejno przez gracza i komputer jednej liczby: 1, 2 lub 3, ktore sa do siebie dodawan"
"e. Gra konczy sie, gdy suma liczb osiagne %i. Wygrywa ten, kto poda ostatnia liczbe do sumy.\n"
"\n",
20LL);
}

original_code:
void zasady(){
  printf("\n\n\n ZASADY GRY:\n\n");
  printf("Gra polega na podawaniu kolejno przez gracza i komputer jednej liczby: 1, 2 lub 3, ktore sa do siebie dodawane. Gra konczy sie, gdy suma liczb osiagne %i. Wygrywa ten, kto poda ostatnia liczbe do sumy.\n\n",20);
}


wyswietl

decompiled_code:
int __fastcall wyswietl(unsigned int a1, unsigned int a2, unsigned int a3)
{
return printf("\n Ruch gracza: %i\n Ruch komputera: %i\n Suma: %i\n", a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, unsigned int a2, unsigned int a3)
{
return printf("\n Ruch gracza: %i\n Ruch komputera: %i\n Suma: %i\n", a1, a2, a3);
}

original_code:
void wyswietl(int czl, int kom, int suma){
  printf("\n Ruch gracza:   %i\n Ruch komputera:   %i\n Suma:   %i\n",czl,kom,suma);
}

variable_types: czl int

variable_types: kom int

variable_types: suma int


sortuj

decompiled_code:
__int64 __fastcall sortuj(__int64 a1, _DWORD *a2, int a3)
{
int v5; // ebp
int v6; // er12
__int64 result; // rax
int *v8; // rdi
int v9; // esi
int *v10; // rcx

*a2 = 0;
a2[1] = 1;
a2[2] = 2;
v5 = 0;
v6 = 1;
LODWORD(result) = 1;
while ( 1 )
{
do
{
v8 = &a2[v5];
v9 = *v8;
v10 = &a2[(int)result];
if ( *(_DWORD *)(a1 + 40LL * *v8 + 4LL * a3) < *(_DWORD *)(a1 + 40LL * *v10 + 4LL * a3) )
{
*v8 = *v10;
*v10 = v9;
}
LODWORD(result) = result + 1;
}
while ( (int)result <= 2 );
result = (unsigned int)(v6 + 1);
if ( (int)result > 2 )
break;
v5 = v6++;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _DWORD *a2, int a3)
{
int v5; // ebp
int v6; // er12
__int64 result; // rax
int *v8; // rdi
int v9; // esi
int *v10; // rcx

*a2 = 0;
a2[1] = 1;
a2[2] = 2;
v5 = 0;
v6 = 1;
LODWORD(result) = 1;
while ( 1 )
{
do
{
v8 = &a2[v5];
v9 = *v8;
v10 = &a2[(int)result];
if ( *(_DWORD *)(a1 + 40LL * *v8 + 4LL * a3) < *(_DWORD *)(a1 + 40LL * *v10 + 4LL * a3) )
{
*v8 = *v10;
*v10 = v9;
}
LODWORD(result) = result + 1;
}
while ( (int)result <= 2 );
result = (unsigned int)(v6 + 1);
if ( (int)result > 2 )
break;
v5 = v6++;
}
return result;
}

original_code:
void sortuj(int naupom[3][20/2], int kolej[3], int licznik){
  int i,j,x;
  for(i=0;i<3;i++){
    kolej[i]=i;
  }
  for(i=0;i<3;i++){
    for(j=i+1;j<3;j++){
      if(naupom[kolej[i]][licznik]<naupom[kolej[j]][licznik]){
 x=kolej[i];
 kolej[i]=kolej[j];
 kolej[j]=x;
      }
    }
  }
}

canonical_original_code:
void sortuj(int naupom[3][-1], int kolej[3], int licznik){
  int i; int j; int x;
  for(i=0;i<3;i++){
    kolej[i]=i;
  }
  for(i=0;i<3;i++){
    for(j=i+1;j<3;j++){
      if(naupom[kolej[i]][licznik]<naupom[kolej[j]][licznik]){
 x=kolej[i];
 kolej[i]=kolej[j];
 kolej[j]=x;
      }
    }
  }
}

variable_types:
naupom
int [3][-1]

variable_types:
kolej
int [3]

variable_types: licznik int

variable_types: j int

variable_types: x int


naukaCzesc

decompiled_code:
__int64 __fastcall naukaCzesc(__int64 a1, int a2, int a3)
{
__int64 result; // rax

result = a1 + 40LL * a3 - 40;
*(_DWORD *)(result + 4LL * a2) = 1;
return result;
}

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

result = a1 + 40LL * a3 - 40;
*(_DWORD *)(result + 4LL * a2) = 1;
return result;
}

original_code:
void naukaCzesc(int pomnau[3][20/2], int licznik, int kom){
  pomnau[kom-1][licznik]=1;
}

canonical_original_code:
void naukaCzesc(int pomnau[3][-1], int licznik, int kom){
  pomnau[kom-1][licznik]=1;
}

variable_types:
pomnau
int [3][-1]


ruchKom

decompiled_code:
__int64 __fastcall ruchKom(int a1, __int64 a2, __int64 a3, unsigned int a4)
{
__int64 v5; // rbp
unsigned int v6; // ebx
int v7; // eax
bool v8; // dl
int v10; // [rsp+0h] [rbp-38h]
int v11[13]; // [rsp+4h] [rbp-34h] BYREF

sortuj(a3, v11, a1);
v5 = 1LL;
do
{
v6 = *(&v10 + v5) + 1;
v7 = czyLegal(a4, v6);
v8 = (int)v5++ <= 2;
}
while ( v7 != 0 && v8 );
naukaCzesc(a2, a1, v6);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, __int64 a2, __int64 a3, unsigned int a4)
{
__int64 v5; // rbp
unsigned int v6; // ebx
int v7; // eax
bool v8; // dl
int v10; // [rsp+0h] [rbp-38h]
int v11[13]; // [rsp+4h] [rbp-34h] BYREF

func9(a3, v11, a1);
v5 = 1LL;
do
{
v6 = *(&v10 + v5) + 1;
v7 = func14(a4, v6);
v8 = (int)v5++ <= 2;
}
while ( v7 != 0 && v8 );
func10(a2, a1, v6);
return v6;
}

original_code:
int ruchKom(int licznik, int pomnau[3][20/2], int nau[3][20/2], int suma){
  int kolej[3];
  int i,r,kom;
  sortuj(nau,kolej,licznik);
  i=0;
  do{
    kom=kolej[i]+1;
    i++;
  }while(czyLegal(suma,kom) && i<3);
  naukaCzesc(pomnau,licznik,kom);
  return kom;
}

canonical_original_code:
int ruchKom(int licznik, int pomnau[3][-1], int nau[3][-1], int suma){
  int kolej[3];
  int i; int r; int kom;
  sortuj(nau,kolej,licznik);
  i=0;
  do{
    kom=kolej[i]+1;
    i++;
  }while(czyLegal(suma,kom) && i<3);
  naukaCzesc(pomnau,licznik,kom);
  return kom;
}

variable_types: i int

variable_types: r int


naukaCalosc

decompiled_code:
__int64 __fastcall naukaCalosc(__int64 a1, __int64 a2, char a3)
{
__int64 j; // rdx
__int64 i; // rdx
__int64 result; // rax

if ( a3 == 45 )
{
for ( i = 120LL; i != 160; i += 4LL )
{
result = i - 120;
do
{
*(_DWORD *)(a2 + result) -= *(_DWORD *)(a1 + result);
result += 40LL;
}
while ( result != i );
}
}
else
{
for ( j = 120LL; j != 160; j += 4LL )
{
result = j - 120;
do
{
*(_DWORD *)(a2 + result) += *(_DWORD *)(a1 + result);
result += 40LL;
}
while ( result != j );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, char a3)
{
__int64 j; // rdx
__int64 i; // rdx
__int64 result; // rax

if ( a3 == 45 )
{
for ( i = 120LL; i != 160; i += 4LL )
{
result = i - 120;
do
{
*(_DWORD *)(a2 + result) -= *(_DWORD *)(a1 + result);
result += 40LL;
}
while ( result != i );
}
}
else
{
for ( j = 120LL; j != 160; j += 4LL )
{
result = j - 120;
do
{
*(_DWORD *)(a2 + result) += *(_DWORD *)(a1 + result);
result += 40LL;
}
while ( result != j );
}
}
return result;
}

variable_types: znak char


czyKoniec

decompiled_code:
_BOOL8 __fastcall czyKoniec(int a1)
{
return a1 <= 19;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(int a1)
{
return a1 <= 19;
}

original_code:
int czyKoniec(int suma){
  if(suma<20)return 1;
  else return 0;
}


czyLegal

decompiled_code:
_BOOL8 __fastcall czyLegal(int a1, int a2)
{
if ( a1 <= 17 )
return (unsigned int)(a2 - 1) > 2;
if ( a1 == 18 )
return (unsigned int)(a2 - 1) > 1;
if ( a1 == 19 )
return a2 != 1;
puts("\n BLAD! PRZERWIJ GRE");
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(int a1, int a2)
{
if ( a1 <= 17 )
return (unsigned int)(a2 - 1) > 2;
if ( a1 == 18 )
return (unsigned int)(a2 - 1) > 1;
if ( a1 == 19 )
return a2 != 1;
puts("\n BLAD! PRZERWIJ GRE");
return 0LL;
}

original_code:
int czyLegal(int suma, int ruch){
  if(suma<=(20 -3)){
    if(ruch==1 || ruch==2 || ruch==3)return 0;
    else return 1;
  }else if(suma==(20 -2)){
    if(ruch==1 || ruch==2)return 0;
    else return 1;
  }else if(suma==(20 -1)){
    if(ruch==1)return 0;
    else return 1;
  }else{
    printf("\n BLAD! PRZERWIJ GRE\n");
    return 0;
  }
}

variable_types: ruch int


ktoWygr

decompiled_code:
int __fastcall ktoWygr(char a1, __int64 a2, __int64 a3)
{
int result; // eax

if ( a1 == 99 )
{
puts("\nWygrales... Gratuluje...");
result = naukaCalosc(a2, a3, 45);
}
else if ( a1 == 107 )
{
puts("\nHURRA!!! WYGRALEM!!!");
result = naukaCalosc(a2, a3, 43);
}
else
{
result = puts("\nBLAD");
}
return result;
}

canonical_decompiled_code:
int __fastcall func15(char a1, __int64 a2, __int64 a3)
{
int result; // eax

if ( a1 == 99 )
{
puts("\nWygrales... Gratuluje...");
result = func12(a2, a3, 45);
}
else if ( a1 == 107 )
{
puts("\nHURRA!!! WYGRALEM!!!");
result = func12(a2, a3, 43);
}
else
{
result = puts("\nBLAD");
}
return result;
}

original_code:
void ktoWygr(char ruch, int pomnau[3][20/2], int nau[3][20/2]){
  if(ruch=='c'){
    printf("\nWygrales... Gratuluje...\n");
    naukaCalosc(pomnau,nau,'-');
  }
  else if(ruch=='k'){
    printf("\nHURRA!!! WYGRALEM!!!\n");
    naukaCalosc(pomnau,nau,'+');
  }else printf("\nBLAD\n");
}

canonical_original_code:
void ktoWygr(char ruch, int pomnau[3][-1], int nau[3][-1]){
  if(ruch=='c'){
    printf("\nWygrales... Gratuluje...\n");
    naukaCalosc(pomnau,nau,'-');
  }
  else if(ruch=='k'){
    printf("\nHURRA!!! WYGRALEM!!!\n");
    naukaCalosc(pomnau,nau,'+');
  }else printf("\nBLAD\n");
}

variable_types: ruch char


ruchCzl

decompiled_code:
__int64 __fastcall ruchCzl(int a1)
{
int v1; // eax
_BOOL4 v2; // edx
unsigned int v4; // [rsp+Ch] [rbp-Ch]

v1 = 0;
do
{
if ( v1 )
puts("\n\n Nie rozumiem.");
printf("\n Podaj liczbe: ");
__isoc99_scanf();
getchar();
v2 = czyLegal(a1, v4);
v1 = 1;
}
while ( v2 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
int v1; // eax
_BOOL4 v2; // edx
unsigned int v4; // [rsp+Ch] [rbp-Ch]

v1 = 0;
do
{
if ( v1 )
puts("\n\n Nie rozumiem.");
printf("\n Podaj liczbe: ");
__isoc99_scanf();
getchar();
v2 = func14(a1, v4);
v1 = 1;
}
while ( v2 );
return v4;
}

original_code:
int ruchCzl(int suma){
  int czl,i=0;
  do{
    if(i!=0)printf("\n\n Nie rozumiem.\n");
    printf("\n Podaj liczbe: ");
    scanf("%i",&czl);
    getchar();
    if(i==0)i=1;
  }while(czyLegal(suma,czl));
  return czl;
}

canonical_original_code:
int ruchCzl(int suma){
  int czl; int i = 0;
  do{
    if(i!=0)printf("\n\n Nie rozumiem.\n");
    printf("\n Podaj liczbe: ");
    scanf("%i",&czl);
    getchar();
    if(i==0)i=1;
  }while(czyLegal(suma,czl));
  return czl;
}


czyJeszcze

decompiled_code:
__int64 czyJeszcze()
{
int v0; // edx
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v0 = 0;
do
{
if ( v0 )
printf("\n Nie rozumiem.");
puts("\n\n Czy chcesz grac dalej?");
puts(" 0 - nie");
puts(" 1 - tak");
printf(" Podaj cyfre: ");
__isoc99_scanf();
getchar();
result = v2;
v0 = 1;
}
while ( v2 > 1 );
return result;
}

canonical_decompiled_code:
__int64 func17()
{
int v0; // edx
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v0 = 0;
do
{
if ( v0 )
printf("\n Nie rozumiem.");
puts("\n\n Czy chcesz grac dalej?");
puts(" 0 - nie");
puts(" 1 - tak");
printf(" Podaj cyfre: ");
__isoc99_scanf();
getchar();
result = v2;
v0 = 1;
}
while ( v2 > 1 );
return result;
}

original_code:
int czyJeszcze(){
  int w,i=0;
  do{
    if(i!=0)printf("\n Nie rozumiem.");
    printf("\n\n Czy chcesz grac dalej?\n");
    printf(" 0 - nie\n");
    printf(" 1 - tak\n");
    printf(" Podaj cyfre: ");
    scanf("%i",&w);
    getchar();
    if(i==0)i=1;
  }while(w!=1 && w!=0);
  return w;
}

canonical_original_code:
int czyJeszcze(){
  int w; int i = 0;
  do{
    if(i!=0)printf("\n Nie rozumiem.");
    printf("\n\n Czy chcesz grac dalej?\n");
    printf(" 0 - nie\n");
    printf(" 1 - tak\n");
    printf(" Podaj cyfre: ");
    scanf("%i",&w);
    getchar();
    if(i==0)i=1;
  }while(w!=1 && w!=0);
  return w;
}

variable_types: w int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r14
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // ebx
unsigned int v7; // er13
_DWORD v9[32]; // [rsp+0h] [rbp-138h] BYREF
_DWORD v10[46]; // [rsp+80h] [rbp-B8h] BYREF

czyscTab(v10);
v3 = 122;
do
{
czyscTab(v9);
zasady();
wyswietl(0, 0, 0);
v4 = 0;
v6 = 0;
v7 = 0;
while ( czyKoniec(v6) )
{
v5 = ruchCzl(v6);
v6 += v5;
v3 = 99;
if ( czyKoniec(v6) )
{
v7 = ruchKom(v4, (__int64)v9, (__int64)v10, v6);
v6 += v7;
v3 = 107;
}
wyswietl(v5, v7, v6);
++v4;
}
ktoWygr(v3, (__int64)v9, (__int64)v10);
}
while ( (unsigned int)czyJeszcze() );
puts("Dzieki za gre. Do zobaczenia nastepnym razem ;)");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r14
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // ebx
unsigned int v7; // er13
_DWORD v9[32]; // [rsp+0h] [rbp-138h] BYREF
_DWORD v10[46]; // [rsp+80h] [rbp-B8h] BYREF

czyscTab(v10);
v3 = 122;
do
{
czyscTab(v9);
func7();
func8(0, 0, 0);
v4 = 0;
v6 = 0;
v7 = 0;
while ( func13(v6) )
{
v5 = func16(v6);
v6 += v5;
v3 = 99;
if ( func13(v6) )
{
v7 = func11(v4, (__int64)v9, (__int64)v10, v6);
v6 += v7;
v3 = 107;
}
func8(v5, v7, v6);
++v4;
}
func15(v3, (__int64)v9, (__int64)v10);
}
while ( (unsigned int)func17() );
puts("Dzieki za gre. Do zobaczenia nastepnym razem ;)");
return 0;
}

original_code:
int main(){
  int czl;
  int kom;
  int suma;
  int licznik;
  int nau[3][20/2];
  int pomnau[3][20/2];
  char ruch='z';
  czyscTab(nau);
  do{
    czyscTab(pomnau);
    suma=0;
    czl=0;
    kom=0;
    licznik=0;
    zasady();
    wyswietl(czl,kom,suma);
    while(czyKoniec(suma)){
      czl=ruchCzl(suma);
      ruch='c';
      suma+=czl;
      if(czyKoniec(suma)){
 kom=ruchKom(licznik,pomnau,nau,suma);
        ruch='k';
 suma+=kom;
      }
      wyswietl(czl,kom,suma);
      licznik++;
    }
    ktoWygr(ruch,pomnau,nau);
  }while(czyJeszcze());
  printf("Dzieki za gre. Do zobaczenia nastepnym razem ;)\n");
  return 0;
}

canonical_original_code:
int main(){
  int czl;
  int kom;
  int suma;
  int licznik;
  int nau[3][-1];
  int pomnau[3][-1];
  char ruch = 'z';
  czyscTab(nau);
  do{
    czyscTab(pomnau);
    suma=0;
    czl=0;
    kom=0;
    licznik=0;
    zasady();
    wyswietl(czl,kom,suma);
    while(czyKoniec(suma)){
      czl=ruchCzl(suma);
      ruch='c';
      suma+=czl;
      if(czyKoniec(suma)){
 kom=ruchKom(licznik,pomnau,nau,suma);
        ruch='k';
 suma+=kom;
      }
      wyswietl(czl,kom,suma);
      licznik++;
    }
    ktoWygr(ruch,pomnau,nau);
  }while(czyJeszcze());
  printf("Dzieki za gre. Do zobaczenia nastepnym razem ;)\n");
  return 0;
}


tightloop

repo: nkurz/loop-call-weirdness

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

original_code:
void tightloop() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        counter += j;
    }
    ;
}

canonical_original_code:
void tightloop() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        counter += j;
    }
    ;
}

variable_types:
j
unsigned int


decompiled_code:
__int64 __fastcall loop_var(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; result != 400000000; ++result )
*a1 += result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; result != 400000000; ++result )
*a1 += result;
return result;
}

original_code:
void loop_var(volatile unsigned long long *counter_p) {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        *counter_p += j;
    }
    ;
}

canonical_original_code:
void loop_var(unsigned long long *counter_p) {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        *counter_p += j;
    }
    ;
}


decompiled_code:
__int64 loop_with_extra_call()
{
__int64 v0; // rax
__int64 result; // rax

do
{
v0 = foo();
counter += v0;
result = v0 + 1;
}
while ( result != 400000000 );
return result;
}

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

do
{
v0 = func11();
counter += v0;
result = v0 + 1;
}
while ( result != 400000000 );
return result;
}

original_code:
void loop_with_extra_call() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        __asm__("call foo");
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_with_extra_call() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        __asm__("call foo");
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

original_code:
void loop_exchange() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        __asm__("xchg %rdx, %rdx");
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_exchange() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        __asm__("xchg %rdx, %rdx");
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

original_code:
void loop_exchange_other() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        __asm__("xchg %rcx, %rcx");
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_exchange_other() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        __asm__("xchg %rcx, %rcx");
        counter += j;
    }
    ;
}


foo

decompiled_code:
void foo()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void foo() {
}

canonical_original_code:
void foo() {
}


decompiled_code:
void foo_full()
{
;
}

canonical_decompiled_code:
void func12()
{
;
}

original_code:
void foo_full() {
    __asm__("movaps %xmm0, %xmm0");
    __asm__("movaps %xmm0, %xmm0");
    __asm__("movaps %xmm0, %xmm0");
    __asm__("movaps %xmm0, %xmm0");
}


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

for ( result = 0LL; ; ++result )
{
counter += result;
if ( result == 399999999 )
break;
if ( (((_BYTE)result + 1) & 1) != 0 )
foo();
}
return result;
}

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

for ( result = 0LL; ; ++result )
{
counter += result;
if ( result == 399999999 )
break;
if ( (((_BYTE)result + 1) & 1) != 0 )
func11();
}
return result;
}

original_code:
void loop_mod_2() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        if (j % 2) __asm__("call foo");
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_mod_2() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        if (j % 2) __asm__("call foo");
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

original_code:
void loop_dummy_read() {
    ;
    unsigned j;
    unsigned dummy_read;
    for (j = 0; j < N; ++j) {
        dummy_read = dummy;
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_dummy_read() {
    ;
    unsigned int j;
    unsigned int dummy_read;
    for (j = 0; j < N; ++j) {
        dummy_read = dummy;
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
counter += result;
return result;
}

original_code:
void loop_dummy_read_self() {
    ;
    unsigned j;
    unsigned dummy_read;
    for (j = 0; j < N; ++j) {
        dummy_read = counter;
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_dummy_read_self() {
    ;
    unsigned int j;
    unsigned int dummy_read;
    for (j = 0; j < N; ++j) {
        dummy_read = counter;
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
{
__asm { prefetcht0 byte ptr cs:counter }
counter += result;
}
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
{
__asm { prefetcht0 byte ptr cs:counter }
counter += result;
}
return result;
}

original_code:
void loop_dummy_prefetch() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        __builtin_prefetch((void *)&counter);
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_dummy_prefetch() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        __builtin_prefetch((void *)&counter);
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
{
dummy = result;
counter += result;
}
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
{
dummy = result;
counter += result;
}
return result;
}

original_code:
void loop_write() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        dummy = j;
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_write() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        dummy = j;
        counter += j;
    }
    ;
}


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

for ( result = 0LL; result != 400000000; ++result )
{
if ( (result & 1) == 0 )
foo();
counter += result;
}
return result;
}

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

for ( result = 0LL; result != 400000000; ++result )
{
if ( (result & 1) == 0 )
func11();
counter += result;
}
return result;
}

original_code:
void loop_jump() {
    ;
    unsigned j;
    for (j = 0; j < N; ++j) {
        __asm__("test $1, %al");
        __asm__("jnz 1f");
        __asm__("call foo");
        __asm__("1:");
        counter += j;
    }
    ;
}

canonical_original_code:
void loop_jump() {
    ;
    unsigned int j;
    for (j = 0; j < N; ++j) {
        __asm__("test $1, %al");
        __asm__("jnz 1f");
        __asm__("call foo");
        __asm__("1:");
        counter += j;
    }
    ;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
char v4; // al

v3 = 1;
if ( argc > 1 )
{
v4 = *argv[1];
switch ( v4 )
{
case 't':
tightloop(argc, argv, 1LL);
v3 = 0;
break;
case 'c':
loop_with_extra_call(argc, argv, 1LL);
v3 = 0;
break;
case 'e':
loop_exchange(argc, argv, 1LL);
v3 = 0;
break;
case 'o':
loop_exchange_other(argc, argv, 1LL);
v3 = 0;
break;
case 'w':
loop_write(argc, argv, 1LL);
v3 = 0;
break;
case '2':
loop_mod_2(argc, argv, 1LL);
v3 = 0;
break;
case 'd':
loop_dummy_read(argc, argv, 1LL);
v3 = 0;
break;
case 's':
loop_dummy_read_self(argc, argv, 1LL);
v3 = 0;
break;
case 'p':
loop_dummy_prefetch(argc, argv, 1LL);
v3 = 0;
break;
case 'j':
loop_jump(argc, argv, 1LL);
v3 = 0;
break;
default:
v3 = 0;
if ( v4 == 118 )
{
loop_var(&counter);
v3 = 0;
}
break;
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
char v4; // al

v3 = 1;
if ( argc > 1 )
{
v4 = *argv[1];
switch ( v4 )
{
case 't':
func6(argc, argv, 1LL);
v3 = 0;
break;
case 'c':
func8(argc, argv, 1LL);
v3 = 0;
break;
case 'e':
func9(argc, argv, 1LL);
v3 = 0;
break;
case 'o':
func10(argc, argv, 1LL);
v3 = 0;
break;
case 'w':
func17(argc, argv, 1LL);
v3 = 0;
break;
case '2':
func13(argc, argv, 1LL);
v3 = 0;
break;
case 'd':
func14(argc, argv, 1LL);
v3 = 0;
break;
case 's':
func15(argc, argv, 1LL);
v3 = 0;
break;
case 'p':
func16(argc, argv, 1LL);
v3 = 0;
break;
case 'j':
func18(argc, argv, 1LL);
v3 = 0;
break;
default:
v3 = 0;
if ( v4 == 118 )
{
func7(&counter);
v3 = 0;
}
break;
}
}
return v3;
}

original_code:
int main(int argc, char** argv) {
    if (argc <= 1) {
        return 1;
    }
    if (argv[1][0] == 't') {
        tightloop();
    } else if (argv[1][0] == 'c') {
        loop_with_extra_call();
    } else if (argv[1][0] == 'e') {
        loop_exchange();
    } else if (argv[1][0] == 'o') {
        loop_exchange_other();
    } else if (argv[1][0] == 'w') {
        loop_write();
    } else if (argv[1][0] == '2') {
        loop_mod_2();
    } else if (argv[1][0] == 'd') {
        loop_dummy_read();
    } else if (argv[1][0] == 's') {
        loop_dummy_read_self();
    } else if (argv[1][0] == 'p') {
        loop_dummy_prefetch();
    } else if (argv[1][0] == 'j') {
        loop_jump();
    } else if (argv[1][0] == 'v') {
        loop_var(&counter);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    if (argc <= 1) {
        return 1;
    }
    if (argv[1][0] == 't') {
        tightloop();
    } else if (argv[1][0] == 'c') {
        loop_with_extra_call();
    } else if (argv[1][0] == 'e') {
        loop_exchange();
    } else if (argv[1][0] == 'o') {
        loop_exchange_other();
    } else if (argv[1][0] == 'w') {
        loop_write();
    } else if (argv[1][0] == '2') {
        loop_mod_2();
    } else if (argv[1][0] == 'd') {
        loop_dummy_read();
    } else if (argv[1][0] == 's') {
        loop_dummy_read_self();
    } else if (argv[1][0] == 'p') {
        loop_dummy_prefetch();
    } else if (argv[1][0] == 'j') {
        loop_jump();
    } else if (argv[1][0] == 'v') {
        loop_var(&counter);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **