getStackEle

decompiled_code:
__int64 __fastcall getStackEle(int a1)
{
_QWORD *v1; // rdx
int i; // eax

if ( a1 < 0 )
return 0LL;
if ( stack <= a1 )
return 0LL;
v1 = (_QWORD *)*((_QWORD *)&stack + 1);
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
v1 = (_QWORD *)*v1;
}
return v1[2];
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
_QWORD *v1; // rdx
int i; // eax

if ( a1 < 0 )
return 0LL;
if ( stack <= a1 )
return 0LL;
v1 = (_QWORD *)*((_QWORD *)&stack + 1);
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
v1 = (_QWORD *)*v1;
}
return v1[2];
}

original_code:
Number * getStackEle(int which)
{
    int i;
    struct StackElem *s = ((void *)0);
    if (which < 0 || which >= stack.length)
 return ((void *)0);
    s = stack.head;
    for (i = 0; i < which; i++)
 s = s->t;
    return s->data;
}

canonical_original_code:
struct _Number *getStackEle(int which)
{
    int i;
    struct StackElem *s = ((void *)0);
    if (which < 0 || which >= stack.length)
 return ((void *)0);
    s = stack.head;
    for (i = 0; i < which; i++)
 s = s->t;
    return s->data;
}

variable_types: which int

variable_types: i int

return_type: struct _Number { int refCnt; int type; void *data; } *


Push

repo: getreu/grpn

decompiled_code:
__int64 __fastcall Push(__int64 a1)
{
_QWORD *v1; // rbx

v1 = (_QWORD *)newStackEle();
v1[2] = a1;
*v1 = *((_QWORD *)&stack + 1);
v1[1] = 0LL;
incRefcntNumber(a1);
if ( *v1 )
*(_QWORD *)(*v1 + 8LL) = v1;
*((_QWORD *)&stack + 1) = v1;
++stack;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
_QWORD *v1; // rbx

v1 = (_QWORD *)func3();
v1[2] = a1;
*v1 = *((_QWORD *)&stack + 1);
v1[1] = 0LL;
incRefcntNumber(a1);
if ( *v1 )
*(_QWORD *)(*v1 + 8LL) = v1;
*((_QWORD *)&stack + 1) = v1;
++stack;
return 1LL;
}

original_code:
int Push(Number *data)
{
    struct StackElem *s;
    s = newStackEle();
    s->data = data;
    s->t = stack.head;
    s->b = ((void *)0);
    incRefcntNumber(data);
    if(s->t)
 s->t->b = s;
    stack.head = s;
    stack.length++;
    return 1;
}

canonical_original_code:
int Push(struct _Number *data)
{
    struct StackElem *s;
    s = newStackEle();
    s->data = data;
    s->t = stack.head;
    s->b = ((void *)0);
    incRefcntNumber(data);
    if(s->t)
 s->t->b = s;
    stack.head = s;
    stack.length++;
    return 1;
}

variable_types: struct StackElem { struct StackElem *t; struct StackElem *b; struct _Number { int refCnt; int type; void *data; } *data; } *

return_type: int

user_defined_types: struct StackElem { struct StackElem *t; struct StackElem *b; struct _Number *data; }


Pop

decompiled_code:
__int64 Pop()
{
_QWORD *v0; // rbx
__int64 v1; // rbp

v0 = (_QWORD *)*((_QWORD *)&stack + 1);
if ( !v0 )
return 0LL;
if ( *v0 )
{
*(_QWORD *)(*v0 + 8LL) = 0LL;
*((_QWORD *)&stack + 1) = *v0;
}
else
{
*((_QWORD *)&stack + 1) = 0LL;
}
v1 = v0[2];
decRefcntNumber(v1);
stackAddToFreeList((unsigned __int64)v0);
--stack;
return v1;
}

canonical_decompiled_code:
__int64 func12()
{
_QWORD *v0; // rbx
__int64 v1; // rbp

v0 = (_QWORD *)*((_QWORD *)&stack + 1);
if ( !v0 )
return 0LL;
if ( *v0 )
{
*(_QWORD *)(*v0 + 8LL) = 0LL;
*((_QWORD *)&stack + 1) = *v0;
}
else
{
*((_QWORD *)&stack + 1) = 0LL;
}
v1 = v0[2];
decRefcntNumber(v1);
func4((unsigned __int64)v0);
--stack;
return v1;
}

original_code:
Number * Pop()
{
    Number *ptr;
    struct StackElem *s;
    s = stack.head;
    if(s == ((void *)0))
 return ((void *)0);
    if(s->t) {
 s->t->b = ((void *)0);
 stack.head = s->t;
    } else
 stack.head = ((void *)0);
    ptr = s->data;
    decRefcntNumber(ptr);
    stackAddToFreeList(s);
    stack.length--;
    return ptr;
}

canonical_original_code:
struct _Number *Pop()
{
    struct _Number *ptr;
    struct StackElem *s;
    s = stack.head;
    if(s == ((void *)0))
 return ((void *)0);
    if(s->t) {
 s->t->b = ((void *)0);
 stack.head = s->t;
    } else
 stack.head = ((void *)0);
    ptr = s->data;
    decRefcntNumber(ptr);
    stackAddToFreeList(s);
    stack.length--;
    return ptr;
}

variable_types: struct _Number { int refCnt; int type; void *data; } *


resetError

decompiled_code:
void resetError()
{
ErrorString = 0;
}

canonical_decompiled_code:
void func0()
{
ErrorString = 0;
}

original_code:
void resetError() {
    *ErrorString = '\0';
}

return_type: void


isError

decompiled_code:
_BOOL8 isError()
{
return ErrorString != 0;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return ErrorString != 0;
}

original_code:
int isError() {
    return *ErrorString != '\0';
}


getStringError

decompiled_code:
char *getStringError()
{
return &ErrorString;
}

canonical_decompiled_code:
char *func2()
{
return &ErrorString;
}

original_code:
char *getStringError() {
    return ErrorString;
}

return_type: char *


setStringError

decompiled_code:
char *__fastcall setStringError(char *src)
{
char *result; // rax

result = strncpy(ErrorString, src, 0x400uLL);
ErrorString[1023] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func3(char *src)
{
char *result; // rax

result = strncpy(ErrorString, src, 0x400uLL);
ErrorString[1023] = 0;
return result;
}

original_code:
void setStringError(char *error) {
    strncpy(ErrorString, error, 1024);
    ErrorString[1024 -1] = '\0';
}

variable_types:
error
char *


checkArgs

decompiled_code:
__int64 __fastcall checkArgs(const char *a1, int a2)
{
int v2; // edx
__int64 result; // rax

v2 = stackLen();
result = 0LL;
if ( v2 < a2 )
{
sprintf(ErrorString, "%s Error: Too few Arguments.", a1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, int a2)
{
int v2; // edx
__int64 result; // rax

v2 = stackLen();
result = 0LL;
if ( v2 < a2 )
{
sprintf(ErrorString, "%s Error: Too few Arguments.", a1);
result = 1LL;
}
return result;
}

original_code:
int checkArgs(char *name, int args) {
    if (stackLen() < args) {
 sprintf(ErrorString, "%s Error: Too few Arguments.", name);
 return 1;
    }
    return 0;
}

variable_types:
name
char *

variable_types: args int


invalidTyprError

decompiled_code:
int __fastcall invalidTyprError(const char *a1)
{
return sprintf(ErrorString, "%s Error: Invalid Type.", a1);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return sprintf(ErrorString, "%s Error: Invalid Type.", a1);
}

original_code:
void invalidTyprError(char *name) {
    sprintf(ErrorString, "%s Error: Invalid Type.", name);
}


getRadixMode

decompiled_code:
__int64 getRadixMode()
{
return (unsigned int)radixMode;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)radixMode;
}

original_code:
int getRadixMode(){
    return radixMode;
}

canonical_original_code:
int getRadixMode(){
    return radixMode;
}


setRadixMode

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

if ( a1 == 32 )
{
radixMode = 32;
}
else if ( a1 == 16 )
{
radixMode = 16;
}
else
{
result = puts("setRadixMode invalid mode");
}
return result;
}

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

if ( a1 == 32 )
{
radixMode = 32;
}
else if ( a1 == 16 )
{
radixMode = 16;
}
else
{
result = puts("setRadixMode invalid mode");
}
return result;
}

original_code:
void setRadixMode(int mode)
{
    if (mode == 0x20)
 radixMode = 0x20;
    else if (mode == 0x10)
 radixMode = 0x10;
    else
 printf("setRadixMode invalid mode\n");
}

variable_types: mode int


setPolarMode

decompiled_code:
void __fastcall setPolarMode(int a1)
{
polarMode = a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1)
{
polarMode = a1;
}

original_code:
void setPolarMode(int val) {
    polarMode = val;
}

variable_types: val int


getPolarMode

decompiled_code:
__int64 getPolarMode()
{
return (unsigned int)polarMode;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)polarMode;
}

original_code:
int getPolarMode() {
    return polarMode;
}


setBaseMode

decompiled_code:
void __fastcall setBaseMode(int a1)
{
baseMode = a1;
}

canonical_decompiled_code:
void __fastcall func4(int a1)
{
baseMode = a1;
}

original_code:
void setBaseMode(int val) {
    baseMode = val;
}


getBaseMode

decompiled_code:
__int64 getBaseMode()
{
return (unsigned int)baseMode;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)baseMode;
}

original_code:
int getBaseMode() {
    return baseMode;
}

canonical_original_code:
int getBaseMode() {
    return baseMode;
}


getModeDisplayString

original_code:
char *getModeDisplayString()
{
    int rad_mode, pol_mode, base_mode;
    static char buf[256];
    rad_mode = getRadixMode();
    switch (rad_mode) {
    case 0x10:
 strcpy(buf, " Degrees   ");
 break;
    case 0x20:
 strcpy(buf, " Radians   ");
 break;
    default:
 fprintf(stderr, "invalid Radix mode\n");
 break;
    }
    pol_mode = getPolarMode();
    switch (pol_mode) {
    case 0x40:
 strcat(buf, "Rectangular   ");
 break;
    case 0x80:
 strcat(buf, "Polar   ");
 break;
    default:
 fprintf(stderr, "invalid Polar mode\n");
 break;
    }
    base_mode = getBaseMode();
    switch (base_mode) {
    case 0x1:
 strcat(buf, "Binary");
 break;
    case 0x2:
 strcat(buf, "Octal");
 break;
    case 0x4:
 strcat(buf, "Decimal");
 break;
    case 0x400:
 strcat(buf, "Decimal-Eng");
 break;
    case 0x8:
 strcat(buf, "Hexadecimal");
 break;
    default:
 fprintf(stderr, "invalid Base mode\n");
 break;
    }
    return buf;
}

canonical_original_code:
char *getModeDisplayString()
{
    int rad_mode; int pol_mode; int base_mode;
    static char buf[256];
    rad_mode = getRadixMode();
    switch (rad_mode) {
    case 0x10:
 strcpy(buf, " Degrees   ");
 break;
    case 0x20:
 strcpy(buf, " Radians   ");
 break;
    default:
 fprintf(stderr, "invalid Radix mode\n");
 break;
    }
    pol_mode = getPolarMode();
    switch (pol_mode) {
    case 0x40:
 strcat(buf, "Rectangular   ");
 break;
    case 0x80:
 strcat(buf, "Polar   ");
 break;
    default:
 fprintf(stderr, "invalid Polar mode\n");
 break;
    }
    base_mode = getBaseMode();
    switch (base_mode) {
    case 0x1:
 strcat(buf, "Binary");
 break;
    case 0x2:
 strcat(buf, "Octal");
 break;
    case 0x4:
 strcat(buf, "Decimal");
 break;
    case 0x400:
 strcat(buf, "Decimal-Eng");
 break;
    case 0x8:
 strcat(buf, "Hexadecimal");
 break;
    default:
 fprintf(stderr, "invalid Base mode\n");
 break;
    }
    return buf;
}

variable_types:
buf
char [256]


quitCB

decompiled_code:
__int64 quitCB()
{
return gtk_main_quit();
}

canonical_decompiled_code:
__int64 func2()
{
return gtk_main_quit();
}

original_code:
void quitCB(gpointer data) {
    gtk_main_quit();
}

canonical_original_code:
void quitCB(void *data) {
    gtk_main_quit();
}

variable_types:
data
void *


clearCB

decompiled_code:
__int64 clearCB()
{
clearLCD();
return redrawLCD();
}

canonical_decompiled_code:
__int64 func3()
{
clearLCD();
return redrawLCD();
}

original_code:
void clearCB(gpointer data) {
    clearLCD();
    redrawLCD();
}

canonical_original_code:
void clearCB(void *data) {
    clearLCD();
    redrawLCD();
}


aboutCB

decompiled_code:
__int64 aboutCB()
{
return about_popup();
}

canonical_decompiled_code:
__int64 func4()
{
return about_popup();
}

original_code:
void aboutCB(gpointer data) {
    about_popup();
}

canonical_original_code:
void aboutCB(void *data) {
    about_popup();
}


licenseCB

decompiled_code:
__int64 licenseCB()
{
return license_popup();
}

canonical_decompiled_code:
__int64 func5()
{
return license_popup();
}

original_code:
void licenseCB(gpointer data) {
    license_popup();
}

canonical_original_code:
void licenseCB(void *data) {
    license_popup();
}


helpCB

decompiled_code:
__int64 helpCB()
{
return help_popup();
}

canonical_decompiled_code:
__int64 func6()
{
return help_popup();
}

original_code:
void helpCB(gpointer data) {
    help_popup();
}

canonical_original_code:
void helpCB(void *data) {
    help_popup();
}


baseCB

decompiled_code:
__int64 baseCB()
{
setBaseMode();
redrawLCD();
return refreshModeDisplay();
}

canonical_decompiled_code:
__int64 func7()
{
setBaseMode();
redrawLCD();
return refreshModeDisplay();
}

original_code:
void baseCB(gpointer clientData) {
    setBaseMode(((gint) (glong) (clientData)));
    redrawLCD();
    refreshModeDisplay();
}

canonical_original_code:
void baseCB(void *clientData) {
    setBaseMode(((gint) (glong) (clientData)));
    redrawLCD();
    refreshModeDisplay();
}

variable_types:
clientData
void *


radixCB

decompiled_code:
__int64 radixCB()
{
setRadixMode();
redrawLCD();
return refreshModeDisplay();
}

canonical_decompiled_code:
__int64 func10()
{
setRadixMode();
redrawLCD();
return refreshModeDisplay();
}

original_code:
void radixCB(gpointer clientData) {
    setRadixMode(((gint) (glong) (clientData)));
    redrawLCD();
    refreshModeDisplay();
}

canonical_original_code:
void radixCB(void *clientData) {
    setRadixMode(((gint) (glong) (clientData)));
    redrawLCD();
    refreshModeDisplay();
}


cmodeCB

decompiled_code:
__int64 __fastcall cmodeCB(int a1)
{
if ( a1 == 64 )
{
setPolarMode(64LL);
}
else
{
if ( a1 != 128 )
{
fwrite("Error: cmodeCB: i shouldn't get here\n", 1uLL, 0x25uLL, stderr);
exit(0);
}
setPolarMode(128LL);
}
redrawLCD();
return refreshModeDisplay();
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
if ( a1 == 64 )
{
setPolarMode(64LL);
}
else
{
if ( a1 != 128 )
{
fwrite("Error: cmodeCB: i shouldn't get here\n", 1uLL, 0x25uLL, stderr);
exit(0);
}
setPolarMode(128LL);
}
redrawLCD();
return refreshModeDisplay();
}

original_code:
void cmodeCB(gpointer clientData) {
    switch(((gint) (glong) (clientData))) {
    case 0x40:
 setPolarMode(0x40);
 break;
    case 0x80:
 setPolarMode(0x80);
 break;
    default:
 fprintf(stderr, "Error: cmodeCB: i shouldn't get here\n");
 exit(0);
    }
    redrawLCD();
    refreshModeDisplay();
}

canonical_original_code:
void cmodeCB(void *clientData) {
    switch(((gint) (glong) (clientData))) {
    case 0x40:
 setPolarMode(0x40);
 break;
    case 0x80:
 setPolarMode(0x80);
 break;
    default:
 fprintf(stderr, "Error: cmodeCB: i shouldn't get here\n");
 exit(0);
    }
    redrawLCD();
    refreshModeDisplay();
}


dmodeCB

decompiled_code:
__int64 dmodeCB()
{
setLCDDispMode();
return redrawLCD();
}

canonical_decompiled_code:
__int64 func16()
{
setLCDDispMode();
return redrawLCD();
}

original_code:
void dmodeCB(gpointer clientData) {
    setLCDDispMode(((gint) (glong) (clientData)));
    redrawLCD();
}

canonical_original_code:
void dmodeCB(void *clientData) {
    setLCDDispMode(((gint) (glong) (clientData)));
    redrawLCD();
}


decompiled_code:
__int64 setup_constant()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rax
__int64 v12; // rbx
__int64 v13; // rbp
__int64 v14; // rax

v0 = newReal();
realE = setRealDouble(v0, 2.718281828459045);
v1 = newReal();
realPi = setRealDouble(v1, 3.141592653589793);
v2 = newReal();
realPi2 = setRealDouble(v2, 1.570796326794897);
v3 = newReal();
realZero = setRealDouble(v3, 0.0);
v4 = newReal();
realHalf = setRealDouble(v4, 0.5);
v5 = newReal();
realTen = setRealDouble(v5, 10.0);
v6 = newReal();
realOne = setRealDouble(v6, 1.0);
v7 = newReal();
realTwo = setRealDouble(v7, 2.0);
v8 = newReal();
realMOne = setRealDouble(v8, -1.0);
v9 = newReal();
v10 = setRealDouble(v9, 180.0);
real180Pi = divReal(v10, realPi);
freeReal(v10);
v11 = newReal();
v12 = setRealDouble(v11, 1.0);
v13 = realZero;
v14 = newCmplx();
cmplxI = setCmplxReal(v14, v13, v12);
return freeReal(v12);
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rax
__int64 v12; // rbx
__int64 v13; // rbp
__int64 v14; // rax

v0 = newReal();
realE = setRealDouble(v0, 2.718281828459045);
v1 = newReal();
realPi = setRealDouble(v1, 3.141592653589793);
v2 = newReal();
realPi2 = setRealDouble(v2, 1.570796326794897);
v3 = newReal();
realZero = setRealDouble(v3, 0.0);
v4 = newReal();
realHalf = setRealDouble(v4, 0.5);
v5 = newReal();
realTen = setRealDouble(v5, 10.0);
v6 = newReal();
realOne = setRealDouble(v6, 1.0);
v7 = newReal();
realTwo = setRealDouble(v7, 2.0);
v8 = newReal();
realMOne = setRealDouble(v8, -1.0);
v9 = newReal();
v10 = setRealDouble(v9, 180.0);
real180Pi = divReal(v10, realPi);
freeReal(v10);
v11 = newReal();
v12 = setRealDouble(v11, 1.0);
v13 = realZero;
v14 = newCmplx();
cmplxI = setCmplxReal(v14, v13, v12);
return freeReal(v12);
}

original_code:
void setup_constant(){
    Real *tmp;
    realE = setRealDouble(newReal(), 2.7182818284590452354);
    realPi = setRealDouble(newReal(), 3.14159265358979323846);
    realPi2 = setRealDouble(newReal(), 1.57079632679489661923);
    realZero = setRealDouble(newReal(), 0.0);
    realHalf = setRealDouble(newReal(), 0.5);
    realTen = setRealDouble(newReal(), 10.0);
    realOne = setRealDouble(newReal(), 1.0);
    realTwo = setRealDouble(newReal(), 2.0);
    realMOne = setRealDouble(newReal(), -1.0);
    tmp = setRealDouble(newReal(), 180.0);
    real180Pi = divReal(tmp, realPi);
    freeReal(tmp);
    tmp = setRealDouble(newReal(), 1.0);
    cmplxI = setCmplxReal(newCmplx(), realZero, tmp);
    freeReal(tmp);
}

canonical_original_code:
void setup_constant(){
    struct _Real *tmp;
    realE = setRealDouble(newReal(), 2.7182818284590452354);
    realPi = setRealDouble(newReal(), 3.14159265358979323846);
    realPi2 = setRealDouble(newReal(), 1.57079632679489661923);
    realZero = setRealDouble(newReal(), 0.0);
    realHalf = setRealDouble(newReal(), 0.5);
    realTen = setRealDouble(newReal(), 10.0);
    realOne = setRealDouble(newReal(), 1.0);
    realTwo = setRealDouble(newReal(), 2.0);
    realMOne = setRealDouble(newReal(), -1.0);
    tmp = setRealDouble(newReal(), 180.0);
    real180Pi = divReal(tmp, realPi);
    freeReal(tmp);
    tmp = setRealDouble(newReal(), 1.0);
    cmplxI = setCmplxReal(newCmplx(), realZero, tmp);
    freeReal(tmp);
}

variable_types:
tmp
struct _Real { char ok; double num; } *


usage

decompiled_code:
size_t __fastcall usage(const char *a1)
{
if ( a1 )
fprintf(stderr, "Error: %s\n", a1);
fwrite("Usage: grpn [-fn font] [-btn-fn font] [-disp-fn font] [-bm basemode]\n", 1uLL, 0x45uLL, stderr);
fwrite(" [-rows n] [-cols n] [-b]\n", 1uLL, 0x25uLL, stderr);
fwrite(" -b: Don't draw the buttons.\n", 1uLL, 0x26uLL, stderr);
fwrite(" -rows: Display (a minimum of) \"n\" rows.\n", 1uLL, 0x2FuLL, stderr);
fwrite(" -cols: Display (a minimum of) \"n\" columns.\n", 1uLL, 0x32uLL, stderr);
fwrite(" -btn-fn: Use \"font\" for the buttons and menu-bar.\n", 1uLL, 0x37uLL, stderr);
fwrite(" -disp-fn: Use \"font\" for the main display.\n", 1uLL, 0x2FuLL, stderr);
fwrite(" -fn: Specifies both -btn-fn and -disp-fn.\n", 1uLL, 0x33uLL, stderr);
return fwrite(" -bm: basemode: dec, eng, bin, oct or hex.\n", 1uLL, 0x34uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
if ( a1 )
fprintf(stderr, "Error: %s\n", a1);
fwrite("Usage: grpn [-fn font] [-btn-fn font] [-disp-fn font] [-bm basemode]\n", 1uLL, 0x45uLL, stderr);
fwrite(" [-rows n] [-cols n] [-b]\n", 1uLL, 0x25uLL, stderr);
fwrite(" -b: Don't draw the buttons.\n", 1uLL, 0x26uLL, stderr);
fwrite(" -rows: Display (a minimum of) \"n\" rows.\n", 1uLL, 0x2FuLL, stderr);
fwrite(" -cols: Display (a minimum of) \"n\" columns.\n", 1uLL, 0x32uLL, stderr);
fwrite(" -btn-fn: Use \"font\" for the buttons and menu-bar.\n", 1uLL, 0x37uLL, stderr);
fwrite(" -disp-fn: Use \"font\" for the main display.\n", 1uLL, 0x2FuLL, stderr);
fwrite(" -fn: Specifies both -btn-fn and -disp-fn.\n", 1uLL, 0x33uLL, stderr);
return fwrite(" -bm: basemode: dec, eng, bin, oct or hex.\n", 1uLL, 0x34uLL, stderr);
}

original_code:
void usage(char *str){
    if (str)
 fprintf(stderr, "Error: %s\n", str);
    fprintf(stderr, "Usage: grpn [-fn font] [-btn-fn font] [-disp-fn font] [-bm basemode]\n");
    fprintf(stderr, "            [-rows n] [-cols n] [-b]\n");
    fprintf(stderr, "   -b:        Don't draw the buttons.\n");
    fprintf(stderr, "   -rows:     Display (a minimum of) \"n\" rows.\n");
    fprintf(stderr, "   -cols:     Display (a minimum of) \"n\" columns.\n");
    fprintf(stderr, "   -btn-fn:   Use \"font\" for the buttons and menu-bar.\n");
    fprintf(stderr, "   -disp-fn:  Use \"font\" for the main display.\n");
    fprintf(stderr, "   -fn:       Specifies both -btn-fn and -disp-fn.\n");
    fprintf(stderr, "   -bm:        basemode: dec, eng, bin, oct or hex.\n");
}

variable_types:
str
char *


main

canonical_name: main

repo: gfv/byteoscilloscope

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
unsigned __int8 v5; // [rsp+Fh] [rbp-A9h]
char v6[128]; // [rsp+10h] [rbp-A8h] BYREF
char v7; // [rsp+90h] [rbp-28h] BYREF

v3 = v6;
do
*v3++ = 32;
while ( v3 != &v7 );
v7 = 0;
while ( !feof(stdin) )
{
v6[64] = 124;
v6[32] = 91;
v6[96] = 93;
__isoc99_scanf();
putchar(10);
v6[v5 >> 1] = 37;
printf("[%s] ", v6);
v6[v5 >> 1] = 32;
if ( (unsigned int)(v5 >> 1) - 32 <= 0x40 )
printf("%d", v5);
else
printf("\x1B[01;31m%d\x1B[00m RED", v5 >> 1);
}
return 0;
}

original_code:
int main(int argc, char** argv) {
    int i = 0;
    char wave[129];
    for(int j = 0; j < 128; j++) {
        wave[j] = ' ';
    }
    wave[128] = 0;
    while (!feof(stdin)) {
        wave[64] = '|';
        wave[32] = '[';
        wave[96] = ']';
        unsigned char c;
        i++;
        scanf("%c", &c);
        printf("\n");
        int rv = (c/2);
        int initial = c;
        wave[rv] = '%';
        printf("[%s] ", wave);
        wave[rv] = ' ';
        if (rv > 96 || rv < 32) {
            printf("\033[01;31m%d\033[00m RED", rv);
        } else {
            printf("%d", initial);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    int i = 0;
    char wave[129];
    for(int j = 0; j < 128; j++) {
        wave[j] = ' ';
    }
    wave[128] = 0;
    while (!feof(stdin)) {
        wave[64] = '|';
        wave[32] = '[';
        wave[96] = ']';
        unsigned char c;
        i++;
        scanf("%c", &c);
        printf("\n");
        int rv = (c/2);
        int initial = c;
        wave[rv] = '%';
        printf("[%s] ", wave);
        wave[rv] = ' ';
        if (rv > 96 || rv < 32) {
            printf("\033[01;31m%d\033[00m RED", rv);
        } else {
            printf("%d", initial);
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
wave
char [129]

variable_types: j int

variable_types:
c
unsigned char

variable_types: rv int

variable_types: initial int


decompiled_code:
int __fastcall get_rate_limit(__int64 a1, const char *a2)
{
__int64 v2; // rbp
__int64 v4; // rbx

v2 = a1;
if ( !a1 )
return 5;
while ( 1 )
{
if ( !strcmp("rates", *(const char **)v2) )
{
v4 = *(_QWORD *)(v2 + 8);
if ( v4 )
break;
}
LABEL_4:
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
return 5;
}
while ( !strstr(a2, *(const char **)v4) )
{
v4 = *(_QWORD *)(v4 + 16);
if ( !v4 )
goto LABEL_4;
}
return atoi(*(const char **)(v4 + 8));
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
__int64 v2; // rbp
__int64 v4; // rbx

v2 = a1;
if ( !a1 )
return 5;
while ( 1 )
{
if ( !strcmp("rates", *(const char **)v2) )
{
v4 = *(_QWORD *)(v2 + 8);
if ( v4 )
break;
}
LABEL_4:
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
return 5;
}
while ( !strstr(a2, *(const char **)v4) )
{
v4 = *(_QWORD *)(v4 + 16);
if ( !v4 )
goto LABEL_4;
}
return atoi(*(const char **)(v4 + 8));
}

original_code:
int get_rate_limit(struct config_sect * config_options, char * host_address){
  char *section = "rates";
  while (config_options) {
    if (strcmp (section, config_options->name) == 0) {
      struct config_token *tokens = config_options->tokens;
      while (tokens) {
 if(strstr( host_address, tokens->token) != ((void *)0)){
   return atoi(tokens->value);
 }
 tokens = tokens->next;
      }
    }
    config_options = config_options->next;
  }
  return 5;
}

canonical_original_code:
int get_rate_limit(struct config_sect *config_options, char *host_address){
  char *section = "rates";
  while (config_options) {
    if (strcmp (section, config_options->name) == 0) {
      struct config_token *tokens = config_options->tokens;
      while (tokens) {
 if(strstr( host_address, tokens->token) != ((void *)0)){
   return atoi(tokens->value);
 }
 tokens = tokens->next;
      }
    }
    config_options = config_options->next;
  }
  return 5;
}

variable_types: struct config_sect { char *name; struct config_token { char *token; char *value; struct config_token *next; } *tokens; struct config_sect *next; } *

variable_types:
section
char *

user_defined_types: struct config_sect { char *name; struct config_token *tokens; struct config_sect *next; }


convertToBpInterval

decompiled_code:
__int64 __fastcall convertToBpInterval(int a1)
{
return (unsigned int)(int)(float)((float)a1 * 1024.0);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(int)(float)((float)a1 * 1024.0);
}

original_code:
int convertToBpInterval(int rate_limit){
  float k = 1024;
  return (int) rate_limit*k;
}

variable_types: k float


decompiled_code:
void __fastcall update_bin(int a1, __int64 a2)
{
if ( a1 < 0 )
_assert_fail("amount_sent >= 0", "rate_lib.c", 0x68u, "update_bin");
if ( !a2 )
_assert_fail("rate_limit != NULL", "rate_lib.c", 0x69u, "update_bin");
*(_DWORD *)(a2 + 32) -= a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1, __int64 a2)
{
if ( a1 < 0 )
_assert_fail("amount_sent >= 0", "rate_lib.c", 0x68u, "update_bin");
if ( !a2 )
_assert_fail("rate_limit != NULL", "rate_lib.c", 0x69u, "update_bin");
*(_DWORD *)(a2 + 32) -= a1;
}

original_code:
void update_bin(int amount_sent, struct rate *rate_limit){
  ((void) sizeof ((amount_sent >= 0) ? 1 : 0), __extension__ ({ if (amount_sent >= 0) ; else __assert_fail ("amount_sent >= 0", "rate_lib.c", 104, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((rate_limit != ((void *)0)) ? 1 : 0), __extension__ ({ if (rate_limit != ((void *)0)) ; else __assert_fail ("rate_limit != NULL", "rate_lib.c", 105, __extension__ __PRETTY_FUNCTION__); }));
  rate_limit -> bin_amount = rate_limit -> bin_amount - amount_sent;
}

variable_types: struct rate { struct timeval { long tv_sec; long tv_usec; } timestamp; int bin_amount; } *

user_defined_types: struct rate { struct timeval timestamp; int bin_amount; }


decompiled_code:
__int64 __fastcall time_compare(__int64 *a1, __int64 *a2)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rsi
unsigned int v6; // ecx

v2 = *a1;
if ( *a1 < 0 )
_assert_fail("(time1 -> tv_sec) >= 0", "rate_lib.c", 0x78u, "time_compare");
v3 = a1[1];
if ( v3 < 0 )
_assert_fail("(time1 -> tv_usec) >= 0", "rate_lib.c", 0x79u, "time_compare");
v4 = *a2;
if ( *a2 < 0 )
_assert_fail("(time2 -> tv_sec) >= 0", "rate_lib.c", 0x7Au, "time_compare");
v5 = a2[1];
if ( v5 < 0 )
_assert_fail("(time2 -> tv_usec) >= 0", "rate_lib.c", 0x7Bu, "time_compare");
v6 = 1;
if ( v2 <= v4 )
{
v6 = -1;
if ( v2 >= v4 )
v6 = v3 > v5;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, __int64 *a2)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rsi
unsigned int v6; // ecx

v2 = *a1;
if ( *a1 < 0 )
_assert_fail("(time1 -> tv_sec) >= 0", "rate_lib.c", 0x78u, "time_compare");
v3 = a1[1];
if ( v3 < 0 )
_assert_fail("(time1 -> tv_usec) >= 0", "rate_lib.c", 0x79u, "time_compare");
v4 = *a2;
if ( *a2 < 0 )
_assert_fail("(time2 -> tv_sec) >= 0", "rate_lib.c", 0x7Au, "time_compare");
v5 = a2[1];
if ( v5 < 0 )
_assert_fail("(time2 -> tv_usec) >= 0", "rate_lib.c", 0x7Bu, "time_compare");
v6 = 1;
if ( v2 <= v4 )
{
v6 = -1;
if ( v2 >= v4 )
v6 = v3 > v5;
}
return v6;
}

original_code:
int time_compare(struct timeval *time1, struct timeval *time2){
  ((void) sizeof (((time1 -> tv_sec) >= 0) ? 1 : 0), __extension__ ({ if ((time1 -> tv_sec) >= 0) ; else __assert_fail ("(time1 -> tv_sec) >= 0", "rate_lib.c", 120, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof (((time1 -> tv_usec) >= 0) ? 1 : 0), __extension__ ({ if ((time1 -> tv_usec) >= 0) ; else __assert_fail ("(time1 -> tv_usec) >= 0", "rate_lib.c", 121, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof (((time2 -> tv_sec) >= 0) ? 1 : 0), __extension__ ({ if ((time2 -> tv_sec) >= 0) ; else __assert_fail ("(time2 -> tv_sec) >= 0", "rate_lib.c", 122, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof (((time2 -> tv_usec) >= 0) ? 1 : 0), __extension__ ({ if ((time2 -> tv_usec) >= 0) ; else __assert_fail ("(time2 -> tv_usec) >= 0", "rate_lib.c", 123, __extension__ __PRETTY_FUNCTION__); }));
  if( (time1 -> tv_sec) > (time2 -> tv_sec) ) return 1;
  else if ((time1 -> tv_sec) < (time2 -> tv_sec) ) return -1;
  else{
    if( (time1 -> tv_usec) > (time2 -> tv_usec) ) return 1;
    else if ((time1 -> tv_sec) < (time2 -> tv_sec) ) return -1;
    else return 0;
  }
}

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


suspend

decompiled_code:
__int64 __fastcall suspend(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // rax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
struct timeval v6; // [rsp+0h] [rbp-38h] BYREF
__m128i v7; // [rsp+10h] [rbp-28h] BYREF
struct timeval tv; // [rsp+20h] [rbp-18h] BYREF

if ( !a1 )
_assert_fail("rate_limit != NULL", "rate_lib.c", 0x49u, "suspend");
v1 = *(_QWORD *)a1 + *(_QWORD *)(a1 + 16);
v7.m128i_i64[0] = v1;
v2 = *(_QWORD *)(a1 + 8) + *(_QWORD *)(a1 + 24);
v7.m128i_i64[1] = v2;
if ( v2 > 999999 )
{
v7.m128i_i64[0] = v1 + 1;
v7.m128i_i64[1] = v2 - 1000000;
}
if ( gettimeofday(&tv, 0LL) < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("getting clock information failed: %s", v5);
result = 0xFFFFFFFFLL;
}
else if ( (int)time_compare(&tv.tv_sec, v7.m128i_i64) <= 0 )
{
result = 1LL;
if ( *(int *)(a1 + 32) <= 0 )
{
v6.tv_sec = v7.m128i_i64[0] - tv.tv_sec;
v6.tv_usec = v7.m128i_i64[1] - tv.tv_usec;
if ( v7.m128i_i64[1] - tv.tv_usec < 0 )
{
v6.tv_sec = v7.m128i_i64[0] - tv.tv_sec - 1;
v6.tv_usec = v7.m128i_i64[1] - tv.tv_usec + 1000000;
}
printf("Going to sleep");
select(0, 0LL, 0LL, 0LL, &v6);
*(__m128i *)a1 = _mm_loadu_si128(&v7);
*(_DWORD *)(a1 + 32) = *(_DWORD *)(a1 + 36);
result = 1LL;
}
}
else
{
*(__m128i *)a1 = _mm_loadu_si128((const __m128i *)&tv);
*(_DWORD *)(a1 + 32) = *(_DWORD *)(a1 + 36);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // rax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
struct timeval v6; // [rsp+0h] [rbp-38h] BYREF
__m128i v7; // [rsp+10h] [rbp-28h] BYREF
struct timeval tv; // [rsp+20h] [rbp-18h] BYREF

if ( !a1 )
_assert_fail("rate_limit != NULL", "rate_lib.c", 0x49u, "suspend");
v1 = *(_QWORD *)a1 + *(_QWORD *)(a1 + 16);
v7.m128i_i64[0] = v1;
v2 = *(_QWORD *)(a1 + 8) + *(_QWORD *)(a1 + 24);
v7.m128i_i64[1] = v2;
if ( v2 > 999999 )
{
v7.m128i_i64[0] = v1 + 1;
v7.m128i_i64[1] = v2 - 1000000;
}
if ( gettimeofday(&tv, 0LL) < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("getting clock information failed: %s", v5);
result = 0xFFFFFFFFLL;
}
else if ( (int)func3(&tv.tv_sec, v7.m128i_i64) <= 0 )
{
result = 1LL;
if ( *(int *)(a1 + 32) <= 0 )
{
v6.tv_sec = v7.m128i_i64[0] - tv.tv_sec;
v6.tv_usec = v7.m128i_i64[1] - tv.tv_usec;
if ( v7.m128i_i64[1] - tv.tv_usec < 0 )
{
v6.tv_sec = v7.m128i_i64[0] - tv.tv_sec - 1;
v6.tv_usec = v7.m128i_i64[1] - tv.tv_usec + 1000000;
}
printf("Going to sleep");
select(0, 0LL, 0LL, 0LL, &v6);
*(__m128i *)a1 = _mm_loadu_si128(&v7);
*(_DWORD *)(a1 + 32) = *(_DWORD *)(a1 + 36);
result = 1LL;
}
}
else
{
*(__m128i *)a1 = _mm_loadu_si128((const __m128i *)&tv);
*(_DWORD *)(a1 + 32) = *(_DWORD *)(a1 + 36);
result = 1LL;
}
return result;
}

original_code:
int suspend(struct rate *rate_limit){
  ((void) sizeof ((rate_limit != ((void *)0)) ? 1 : 0), __extension__ ({ if (rate_limit != ((void *)0)) ; else __assert_fail ("rate_limit != NULL", "rate_lib.c", 73, __extension__ __PRETTY_FUNCTION__); }));
  struct timeval current_time, deadline, sleeptime;
  do { (&deadline)->tv_sec = (&(rate_limit -> timestamp))->tv_sec + (&(rate_limit -> period))->tv_sec; (&deadline)->tv_usec = (&(rate_limit -> timestamp))->tv_usec + (&(rate_limit -> period))->tv_usec; if ((&deadline)->tv_usec >= 1000000) { ++(&deadline)->tv_sec; (&deadline)->tv_usec -= 1000000; } } while (0);
  if(gettimeofday(&current_time, ((void *)0)) < 0){
    printf("getting clock information failed: %s", strerror((*__errno_location ())));
    return -1;
  }
  if(time_compare(&current_time, &deadline) <= 0 && rate_limit -> bin_amount <= 0){
    do { (&sleeptime)->tv_sec = (&deadline)->tv_sec - (&current_time)->tv_sec; (&sleeptime)->tv_usec = (&deadline)->tv_usec - (&current_time)->tv_usec; if ((&sleeptime)->tv_usec < 0) { --(&sleeptime)->tv_sec; (&sleeptime)->tv_usec += 1000000; } } while (0);
    printf("Going to sleep");
    select(0,0,0,0,&sleeptime);
    rate_limit -> timestamp = deadline;
    rate_limit -> bin_amount = rate_limit ->bin_max_amount;
  }
  else if(time_compare(&current_time, &deadline) > 0){
    rate_limit -> timestamp = current_time;
    rate_limit -> bin_amount = rate_limit -> bin_max_amount;
  }
  return 1;
}

canonical_original_code:
int suspend(struct rate *rate_limit){
  ((void) sizeof ((rate_limit != ((void *)0)) ? 1 : 0), __extension__ ({ if (rate_limit != ((void *)0)) ; else __assert_fail ("rate_limit != NULL", "rate_lib.c", 73, __extension__ __PRETTY_FUNCTION__); }));
  struct timeval current_time; struct timeval deadline; struct timeval sleeptime;
  do { (&deadline)->tv_sec = (&(rate_limit -> timestamp))->tv_sec + (&(rate_limit -> period))->tv_sec; (&deadline)->tv_usec = (&(rate_limit -> timestamp))->tv_usec + (&(rate_limit -> period))->tv_usec; if ((&deadline)->tv_usec >= 1000000) { ++(&deadline)->tv_sec; (&deadline)->tv_usec -= 1000000; } } while (0);
  if(gettimeofday(&current_time, ((void *)0)) < 0){
    printf("getting clock information failed: %s", strerror((*__errno_location ())));
    return -1;
  }
  if(time_compare(&current_time, &deadline) <= 0 && rate_limit -> bin_amount <= 0){
    do { (&sleeptime)->tv_sec = (&deadline)->tv_sec - (&current_time)->tv_sec; (&sleeptime)->tv_usec = (&deadline)->tv_usec - (&current_time)->tv_usec; if ((&sleeptime)->tv_usec < 0) { --(&sleeptime)->tv_sec; (&sleeptime)->tv_usec += 1000000; } } while (0);
    printf("Going to sleep");
    select(0,0,0,0,&sleeptime);
    rate_limit -> timestamp = deadline;
    rate_limit -> bin_amount = rate_limit ->bin_max_amount;
  }
  else if(time_compare(&current_time, &deadline) > 0){
    rate_limit -> timestamp = current_time;
    rate_limit -> bin_amount = rate_limit -> bin_max_amount;
  }
  return 1;
}

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


decompiled_code:
int __fastcall print_timeval(_QWORD *a1)
{
return printf("%ld sec %ld usec", *a1, a1[1]);
}

canonical_decompiled_code:
int __fastcall func5(_QWORD *a1)
{
return printf("%ld sec %ld usec", *a1, a1[1]);
}

original_code:
void print_timeval(struct timeval *value){
  printf("%ld sec %ld usec", value -> tv_sec, value -> tv_usec);
}

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


decompiled_code:
int test_suspend()
{
__m128i v1; // [rsp+0h] [rbp-58h] BYREF
__int64 v2; // [rsp+10h] [rbp-48h]
__int64 v3; // [rsp+18h] [rbp-40h]
int v4; // [rsp+20h] [rbp-38h]
int v5; // [rsp+24h] [rbp-34h]
struct timeval tv; // [rsp+30h] [rbp-28h] BYREF
struct timeval timeout; // [rsp+40h] [rbp-18h] BYREF

v1 = 0uLL;
v3 = 0LL;
v4 = 0;
v2 = 3LL;
v5 = 3;
gettimeofday((struct timeval *)&v1, 0LL);
tv.tv_sec = v1.m128i_i64[0] + 3;
tv.tv_usec = v1.m128i_i64[1];
if ( v1.m128i_i64[1] > 999999 )
{
tv.tv_sec = v1.m128i_i64[0] + 4;
tv.tv_usec = v1.m128i_i64[1] - 1000000;
}
suspend((__int64)&v1);
printf("\nExpect:");
print_timeval(&tv);
printf("\nGot: ");
print_timeval(&v1);
printf("\nAmount(3):%d\n", 0LL);
gettimeofday((struct timeval *)&v1, 0LL);
tv = (struct timeval)_mm_loadu_si128(&v1);
v4 = 2;
suspend((__int64)&v1);
printf("\n\nExpect:");
print_timeval(&tv);
printf("\nGot: ");
print_timeval(&v1);
printf("\nAmount(2):%d\n", 0LL);
gettimeofday((struct timeval *)&v1, 0LL);
timeout.tv_sec = 4LL;
timeout.tv_usec = 0LL;
select(0, 0LL, 0LL, 0LL, &timeout);
gettimeofday(&tv, 0LL);
suspend((__int64)&v1);
printf("\n\nExpect:");
print_timeval(&tv);
printf("\nGot: ");
print_timeval(&v1);
return printf("\nAmount(3):%d\n", 2LL);
}

canonical_decompiled_code:
int func6()
{
__m128i v1; // [rsp+0h] [rbp-58h] BYREF
__int64 v2; // [rsp+10h] [rbp-48h]
__int64 v3; // [rsp+18h] [rbp-40h]
int v4; // [rsp+20h] [rbp-38h]
int v5; // [rsp+24h] [rbp-34h]
struct timeval tv; // [rsp+30h] [rbp-28h] BYREF
struct timeval timeout; // [rsp+40h] [rbp-18h] BYREF

v1 = 0uLL;
v3 = 0LL;
v4 = 0;
v2 = 3LL;
v5 = 3;
gettimeofday((struct timeval *)&v1, 0LL);
tv.tv_sec = v1.m128i_i64[0] + 3;
tv.tv_usec = v1.m128i_i64[1];
if ( v1.m128i_i64[1] > 999999 )
{
tv.tv_sec = v1.m128i_i64[0] + 4;
tv.tv_usec = v1.m128i_i64[1] - 1000000;
}
func4((__int64)&v1);
printf("\nExpect:");
func5(&tv);
printf("\nGot: ");
func5(&v1);
printf("\nAmount(3):%d\n", 0LL);
gettimeofday((struct timeval *)&v1, 0LL);
tv = (struct timeval)_mm_loadu_si128(&v1);
v4 = 2;
func4((__int64)&v1);
printf("\n\nExpect:");
func5(&tv);
printf("\nGot: ");
func5(&v1);
printf("\nAmount(2):%d\n", 0LL);
gettimeofday((struct timeval *)&v1, 0LL);
timeout.tv_sec = 4LL;
timeout.tv_usec = 0LL;
select(0, 0LL, 0LL, 0LL, &timeout);
gettimeofday(&tv, 0LL);
func4((__int64)&v1);
printf("\n\nExpect:");
func5(&tv);
printf("\nGot: ");
func5(&v1);
return printf("\nAmount(3):%d\n", 2LL);
}

original_code:
void test_suspend(void){
  int amount, max_amount;
  max_amount = 3;
  amount = 0;
  const struct timeval reader_timeout = {.tv_sec = 3, .tv_usec = 0};
  struct timeval exp_time;
  struct rate rate_limit = {.period = reader_timeout, .bin_amount = amount,
      .bin_max_amount = max_amount };
  gettimeofday(&(rate_limit.timestamp), ((void *)0));
  do { (&exp_time)->tv_sec = (&(rate_limit.timestamp))->tv_sec + (&reader_timeout)->tv_sec; (&exp_time)->tv_usec = (&(rate_limit.timestamp))->tv_usec + (&reader_timeout)->tv_usec; if ((&exp_time)->tv_usec >= 1000000) { ++(&exp_time)->tv_sec; (&exp_time)->tv_usec -= 1000000; } } while (0);
  suspend(&rate_limit);
  printf("\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(3):%d\n", amount);
  gettimeofday(&(rate_limit.timestamp), ((void *)0));
  exp_time = rate_limit.timestamp;
  rate_limit.bin_amount = 2;
  suspend(&rate_limit);
  printf("\n\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(2):%d\n", amount);
  gettimeofday(&rate_limit.timestamp, ((void *)0));
  amount = 2;
  select(0,0,0,0,&((struct timeval) {.tv_sec = 4, .tv_usec = 0}));
  gettimeofday(&exp_time, ((void *)0));
  suspend(&rate_limit);
  printf("\n\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(3):%d\n", amount);
}

canonical_original_code:
void test_suspend(){
  int amount; int max_amount;
  max_amount = 3;
  amount = 0;
  struct timeval reader_timeout = {.tv_sec = 3, .tv_usec = 0};
  struct timeval exp_time;
  struct rate rate_limit = {.period = reader_timeout, .bin_amount = amount,
      .bin_max_amount = max_amount };
  gettimeofday(&(rate_limit.timestamp), ((void *)0));
  do { (&exp_time)->tv_sec = (&(rate_limit.timestamp))->tv_sec + (&reader_timeout)->tv_sec; (&exp_time)->tv_usec = (&(rate_limit.timestamp))->tv_usec + (&reader_timeout)->tv_usec; if ((&exp_time)->tv_usec >= 1000000) { ++(&exp_time)->tv_sec; (&exp_time)->tv_usec -= 1000000; } } while (0);
  suspend(&rate_limit);
  printf("\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(3):%d\n", amount);
  gettimeofday(&(rate_limit.timestamp), ((void *)0));
  exp_time = rate_limit.timestamp;
  rate_limit.bin_amount = 2;
  suspend(&rate_limit);
  printf("\n\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(2):%d\n", amount);
  gettimeofday(&rate_limit.timestamp, ((void *)0));
  amount = 2;
  select(0,0,0,0,&((struct timeval) {.tv_sec = 4, .tv_usec = 0}));
  gettimeofday(&exp_time, ((void *)0));
  suspend(&rate_limit);
  printf("\n\nExpect:"); print_timeval(&exp_time);
  printf("\nGot: "); print_timeval(&(rate_limit.timestamp));
  printf("\nAmount(3):%d\n", amount);
}

variable_types: amount int

variable_types: struct rate { struct timeval { long tv_sec; long tv_usec; } timestamp; int bin_amount; }


decompiled_code:
int rate_lib_tests()
{
return test_suspend();
}

canonical_decompiled_code:
int func7()
{
return func6();
}

original_code:
void rate_lib_tests(void){
  test_suspend();
}

canonical_original_code:
void rate_lib_tests(){
  test_suspend();
}


decompiled_code:
int __fastcall no_fork_proc(int a1, __int64 a2, unsigned int a3)
{
int v4; // eax
int v5; // ebx
int result; // eax
int *v7; // rax
char *v8; // rax

v4 = accept(a1, 0LL, 0LL);
if ( v4 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
result = printf("ERROR in creating socket: %s", v8);
}
else
{
v5 = v4;
relay((unsigned int)v4, a2, a3);
result = close(v5);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1, __int64 a2, unsigned int a3)
{
int v4; // eax
int v5; // ebx
int result; // eax
int *v7; // rax
char *v8; // rax

v4 = accept(a1, 0LL, 0LL);
if ( v4 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
result = printf("ERROR in creating socket: %s", v8);
}
else
{
v5 = v4;
relay((unsigned int)v4, a2, a3);
result = close(v5);
}
return result;
}

original_code:
void no_fork_proc(int sock_lis,
  struct config_sect *config_options,
  int rate_limiting){
  int client_sock;
  if ( (client_sock = accept(sock_lis, ((void *)0), ((void *)0))) < 0){
      printf("ERROR in creating socket: %s", strerror((*__errno_location ())));
      return ;
  }
  relay(client_sock, config_options,rate_limiting );
  close(client_sock);
}

user_defined_types: struct config_token { char *token; char *value; struct config_token *next; }


decompiled_code:
void __fastcall __noreturn fork_proc(int fd, __int64 a2, unsigned int a3)
{
int *v4; // rax
char *v5; // rax
int v6; // ebx
__pid_t v7; // eax

while ( 1 )
{
while ( 1 )
{
fwrite("\nWaiting For connection\n", 1uLL, 0x18uLL, stdout);
v6 = accept(fd, 0LL, 0LL);
if ( v6 >= 0 )
break;
v4 = _errno_location();
v5 = strerror(*v4);
printf("ERROR in creating socket: %s", v5);
}
v7 = fork();
if ( v7 )
{
if ( v7 < 0 )
printf("ERROR in creating fork");
}
else
{
relay((unsigned int)v6, a2, a3);
}
close(v6);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(int fd, __int64 a2, unsigned int a3)
{
int *v4; // rax
char *v5; // rax
int v6; // ebx
__pid_t v7; // eax

while ( 1 )
{
while ( 1 )
{
fwrite("\nWaiting For connection\n", 1uLL, 0x18uLL, stdout);
v6 = accept(fd, 0LL, 0LL);
if ( v6 >= 0 )
break;
v4 = _errno_location();
v5 = strerror(*v4);
printf("ERROR in creating socket: %s", v5);
}
v7 = fork();
if ( v7 )
{
if ( v7 < 0 )
printf("ERROR in creating fork");
}
else
{
relay((unsigned int)v6, a2, a3);
}
close(v6);
}
}

original_code:
void fork_proc(int sock_lis,
  struct config_sect *config_options,
  int rate_limiting){
  int client_sock;
  pid_t fork_pid;
  while(1){
      fprintf(stdout, "\nWaiting For connection\n");
      if ( (client_sock = accept(sock_lis, ((void *)0), ((void *)0))) < 0){
          printf("ERROR in creating socket: %s", strerror((*__errno_location ())));
          continue;
      }
      fork_pid = fork();
      if(fork_pid == 0){
          relay(client_sock, config_options, rate_limiting);
      }
      else if(fork_pid < 0){
         printf("ERROR in creating fork");
      }
      close(client_sock);
  }
}

canonical_original_code:
void fork_proc(int sock_lis, struct config_sect *config_options, int rate_limiting){
  int client_sock;
  int fork_pid;
  while(1){
      fprintf(stdout, "\nWaiting For connection\n");
      if ( (client_sock = accept(sock_lis, ((void *)0), ((void *)0))) < 0){
          printf("ERROR in creating socket: %s", strerror((*__errno_location ())));
          continue;
      }
      fork_pid = fork();
      if(fork_pid == 0){
          relay(client_sock, config_options, rate_limiting);
      }
      else if(fork_pid < 0){
         printf("ERROR in creating fork");
      }
      close(client_sock);
  }
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
char *v4; // rax
char *v5; // rbp
int v6; // er12
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = 0;
v3 = parseArgs(argc, argv, v8);
v4 = (char *)malloc(0xAuLL);
if ( v3 )
{
v3 = config_load(v3);
v5 = (char *)extractListPort(v3);
extractDebugLevel(v3);
}
else
{
v5 = v4;
strcpy(v4, "8080");
}
v6 = setup_socket(v5, 0LL);
free(v5);
if ( v6 >= 0 )
fork_proc(v6, v3, v8[0]);
printf("ERROR Could not set up listening socket");
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
char *v4; // rax
char *v5; // rbp
int v6; // er12
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = 0;
v3 = parseArgs(argc, argv, v8);
v4 = (char *)malloc(0xAuLL);
if ( v3 )
{
v3 = config_load(v3);
v5 = (char *)extractListPort(v3);
extractDebugLevel(v3);
}
else
{
v5 = v4;
strcpy(v4, "8080");
}
v6 = setup_socket(v5, 0LL);
free(v5);
if ( v6 >= 0 )
func1(v6, v3, v8[0]);
printf("ERROR Could not set up listening socket");
return -1;
}

original_code:
int main(int argc, char *argv[] )
{
  int sock_lis, debug_mode = 0, rate_limiting = 0;
  struct config_sect * config_options;
  char *configFile = parseArgs(argc, argv, &rate_limiting);
  char * lis_port = malloc(10*sizeof(char));
  if(lis_port == ((void *)0)){
  }
  if(configFile != ((void *)0)){
    config_options = config_load(configFile);
    lis_port = extractListPort(config_options);
    debug_mode = extractDebugLevel(config_options);
  } else {
    strcpy(lis_port, "8080");
    config_options = ((void *)0);
  }
  sock_lis = setup_socket(lis_port, ((void *)0));
  free(lis_port);
  if(sock_lis < 0){
    printf("ERROR Could not set up listening socket");
    return -1;
  }
 fork_proc(sock_lis, config_options, rate_limiting);
  close(sock_lis);
  return 1;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int sock_lis; int debug_mode = 0; int rate_limiting = 0;
  struct config_sect *config_options;
  char *configFile = parseArgs(argc, argv, &rate_limiting);
  char *lis_port = malloc(10*sizeof(char));
  if(lis_port == ((void *)0)){
  }
  if(configFile != ((void *)0)){
    config_options = config_load(configFile);
    lis_port = extractListPort(config_options);
    debug_mode = extractDebugLevel(config_options);
  } else {
    strcpy(lis_port, "8080");
    config_options = ((void *)0);
  }
  sock_lis = setup_socket(lis_port, ((void *)0));
  free(lis_port);
  if(sock_lis < 0){
    printf("ERROR Could not set up listening socket");
    return -1;
  }
 fork_proc(sock_lis, config_options, rate_limiting);
  close(sock_lis);
  return 1;
}

variable_types:
argv
char *[]

variable_types:
configFile
char *


parseArgs

decompiled_code:
__int64 __fastcall parseArgs(int a1, __int64 a2, _DWORD *a3)
{
__int64 v3; // rax
__int64 v4; // r8

if ( a1 <= 0 )
return 0LL;
v3 = a2;
v4 = 0LL;
do
{
if ( !strcmp(*(const char **)v3, "-f") && *(_QWORD *)(v3 + 8) )
v4 = *(_QWORD *)(v3 + 8);
if ( !strcmp(*(const char **)v3, "-rl") )
*a3 = 1;
v3 += 8LL;
}
while ( v3 != a2 + 8LL * (unsigned int)(a1 - 1) + 8 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, _DWORD *a3)
{
__int64 v3; // rax
__int64 v4; // r8

if ( a1 <= 0 )
return 0LL;
v3 = a2;
v4 = 0LL;
do
{
if ( !strcmp(*(const char **)v3, "-f") && *(_QWORD *)(v3 + 8) )
v4 = *(_QWORD *)(v3 + 8);
if ( !strcmp(*(const char **)v3, "-rl") )
*a3 = 1;
v3 += 8LL;
}
while ( v3 != a2 + 8LL * (unsigned int)(a1 - 1) + 8 );
return v4;
}

original_code:
char * parseArgs(int argc, char * argv[], int *rate_limiting){
    int i;
    char * fileName = ((void *)0);
    for(i = 0; i < argc; i++){
        if(strcmp(argv[i], "-f")==0 && argv[i+1] != ((void *)0)){
            fileName = argv[i+1];
        }
        if(strcmp(argv[i], "-rl")==0){
            *rate_limiting = 1;
        }
    }
    return fileName;
}

canonical_original_code:
char *parseArgs(int argc, char *argv[], int *rate_limiting){
    int i;
    char *fileName = ((void *)0);
    for(i = 0; i < argc; i++){
        if(strcmp(argv[i], "-f")==0 && argv[i+1] != ((void *)0)){
            fileName = argv[i+1];
        }
        if(strcmp(argv[i], "-rl")==0){
            *rate_limiting = 1;
        }
    }
    return fileName;
}

variable_types: argc int

variable_types: i int

variable_types:
fileName
char *


extractListPort

decompiled_code:
__int64 __fastcall extractListPort(__int64 a1)
{
__int64 v1; // r8
__int64 v3; // rax

v1 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
if ( !strcmp("default", *(const char **)v1) )
{
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
break;
}
LABEL_4:
v1 = *(_QWORD *)(v1 + 16);
if ( !v1 )
return 0LL;
}
while ( strcmp("proxy_port", *(const char **)v3) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_4;
}
return *(_QWORD *)(v3 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // r8
__int64 v3; // rax

v1 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
if ( !strcmp("default", *(const char **)v1) )
{
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
break;
}
LABEL_4:
v1 = *(_QWORD *)(v1 + 16);
if ( !v1 )
return 0LL;
}
while ( strcmp("proxy_port", *(const char **)v3) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_4;
}
return *(_QWORD *)(v3 + 8);
}

original_code:
char * extractListPort(struct config_sect *config_options){
    char * section = "default";
    char * tokenToFind = "proxy_port";
    while (config_options) {
        if (strcmp (section, config_options->name) == 0) {
            struct config_token *tokens = config_options->tokens;
            while (tokens) {
                if(strcmp(tokenToFind, tokens->token) == 0){
                    return tokens->value;
                }
                tokens = tokens->next;
            }
        }
        config_options = config_options->next;
    }
    return ((void *)0);
}

canonical_original_code:
char *extractListPort(struct config_sect *config_options){
    char *section = "default";
    char *tokenToFind = "proxy_port";
    while (config_options) {
        if (strcmp (section, config_options->name) == 0) {
            struct config_token *tokens = config_options->tokens;
            while (tokens) {
                if(strcmp(tokenToFind, tokens->token) == 0){
                    return tokens->value;
                }
                tokens = tokens->next;
            }
        }
        config_options = config_options->next;
    }
    return ((void *)0);
}

variable_types:
tokenToFind
char *

variable_types: struct config_token { char *token; char *value; struct config_token *next; } *


extractDebugLevel

decompiled_code:
int __fastcall extractDebugLevel(__int64 a1)
{
__int64 v1; // r8
__int64 v3; // rax

v1 = a1;
if ( !a1 )
return -1;
while ( 1 )
{
if ( !strcmp("default", *(const char **)v1) )
{
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
break;
}
LABEL_4:
v1 = *(_QWORD *)(v1 + 16);
if ( !v1 )
return -1;
}
while ( strcmp("debug", *(const char **)v3) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_4;
}
return atoi(*(const char **)(v3 + 8));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
__int64 v1; // r8
__int64 v3; // rax

v1 = a1;
if ( !a1 )
return -1;
while ( 1 )
{
if ( !strcmp("default", *(const char **)v1) )
{
v3 = *(_QWORD *)(v1 + 8);
if ( v3 )
break;
}
LABEL_4:
v1 = *(_QWORD *)(v1 + 16);
if ( !v1 )
return -1;
}
while ( strcmp("debug", *(const char **)v3) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_4;
}
return atoi(*(const char **)(v3 + 8));
}

original_code:
int extractDebugLevel(struct config_sect *config_options){
    char * section = "default";
    char * tokenToFind = "debug";
    while (config_options) {
        if (strcmp (section, config_options->name) == 0) {
            struct config_token *tokens = config_options->tokens;
            while (tokens) {
                if(strcmp(tokenToFind, tokens->token) == 0){
                    return atoi(tokens->value);
                }
                tokens = tokens->next;
            }
        }
        config_options = config_options->next;
    }
    return -1;
}

canonical_original_code:
int extractDebugLevel(struct config_sect *config_options){
    char *section = "default";
    char *tokenToFind = "debug";
    while (config_options) {
        if (strcmp (section, config_options->name) == 0) {
            struct config_token *tokens = config_options->tokens;
            while (tokens) {
                if(strcmp(tokenToFind, tokens->token) == 0){
                    return atoi(tokens->value);
                }
                tokens = tokens->next;
            }
        }
        config_options = config_options->next;
    }
    return -1;
}


decompiled_code:
__int64 __fastcall config_get_value(__int64 a1, const char *a2, const char *a3, int a4)
{
__int64 v4; // rbp
__int64 v9; // rbx

v4 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
if ( a4 ? strcasecmp(a2, *(const char **)v4) == 0 : strcmp(a2, *(const char **)v4) == 0 )
{
v9 = *(_QWORD *)(v4 + 8);
if ( v9 )
break;
}
LABEL_5:
v4 = *(_QWORD *)(v4 + 16);
if ( !v4 )
return 0LL;
}
while ( !(a4 ? strcasecmp(a3, *(const char **)v9) == 0 : strcmp(a3, *(const char **)v9) == 0) )
{
v9 = *(_QWORD *)(v9 + 16);
if ( !v9 )
goto LABEL_5;
}
return *(_QWORD *)(v9 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, const char *a3, int a4)
{
__int64 v4; // rbp
__int64 v9; // rbx

v4 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
if ( a4 ? strcasecmp(a2, *(const char **)v4) == 0 : strcmp(a2, *(const char **)v4) == 0 )
{
v9 = *(_QWORD *)(v4 + 8);
if ( v9 )
break;
}
LABEL_5:
v4 = *(_QWORD *)(v4 + 16);
if ( !v4 )
return 0LL;
}
while ( !(a4 ? strcasecmp(a3, *(const char **)v9) == 0 : strcmp(a3, *(const char **)v9) == 0) )
{
v9 = *(_QWORD *)(v9 + 16);
if ( !v9 )
goto LABEL_5;
}
return *(_QWORD *)(v9 + 8);
}

original_code:
char *config_get_value (struct config_sect *sects, char *section, char *token, int icase)
{
 while (sects) {
  if ((icase ? strcasecmp (section, sects->name) : strcmp (section, sects->name)) == 0) {
   struct config_token *tokens = sects->tokens;
   while (tokens) {
    if ((icase ? strcasecmp (token, tokens->token) : strcmp (token, tokens->token)) == 0) {
     return (tokens->value);
    }
    tokens = tokens->next;
   }
  }
  sects = sects->next;
 }
 return (((void *)0));
}

canonical_original_code:
char *config_get_value(struct config_sect *sects, char *section, char *token, int icase)
{
 while (sects) {
  if ((icase ? strcasecmp (section, sects->name) : strcmp (section, sects->name)) == 0) {
   struct config_token *tokens = sects->tokens;
   while (tokens) {
    if ((icase ? strcasecmp (token, tokens->token) : strcmp (token, tokens->token)) == 0) {
     return (tokens->value);
    }
    tokens = tokens->next;
   }
  }
  sects = sects->next;
 }
 return (((void *)0));
}

variable_types:
token
char *

variable_types: icase int


decompiled_code:
int __fastcall config_dump(__int64 a1)
{
__int64 i; // rbp
int result; // eax
__int64 j; // rbx

for ( i = a1; i; i = *(_QWORD *)(i + 16) )
{
result = printf("[%s]\n", *(const char **)i);
for ( j = *(_QWORD *)(i + 8); j; j = *(_QWORD *)(j + 16) )
result = printf("%s\t%s\n", *(const char **)j, *(const char **)(j + 8));
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
__int64 i; // rbp
int result; // eax
__int64 j; // rbx

for ( i = a1; i; i = *(_QWORD *)(i + 16) )
{
result = printf("[%s]\n", *(const char **)i);
for ( j = *(_QWORD *)(i + 8); j; j = *(_QWORD *)(j + 16) )
result = printf("%s\t%s\n", *(const char **)j, *(const char **)(j + 8));
}
return result;
}

original_code:
void config_dump (struct config_sect *sects)
{
 while (sects) {
  printf ("[%s]\n", sects->name);
  struct config_token *tokens = sects->tokens;
  while (tokens) {
   printf ("%s\t%s\n", tokens->token, tokens->value);
   tokens = tokens->next;
  }
  sects = sects->next;
 }
}

canonical_original_code:
void config_dump(struct config_sect *sects)
{
 while (sects) {
  printf ("[%s]\n", sects->name);
  struct config_token *tokens = sects->tokens;
  while (tokens) {
   printf ("%s\t%s\n", tokens->token, tokens->value);
   tokens = tokens->next;
  }
  sects = sects->next;
 }
}

variable_types: struct config_sect { char *name; struct config_token { char *token; char *value; struct config_token *next; } *tokens; struct config_sect *next; } *


decompiled_code:
void __fastcall config_destroy(void *ptr)
{
char **v1; // r13
char **v2; // r12
char *v3; // rbp
char *v4; // rbx
char *v5; // r14
size_t v6; // rax
char *v7; // r14
size_t v8; // rax
char *v9; // rbx
size_t v10; // rax

v1 = (char **)ptr;
if ( ptr )
{
do
{
v2 = v1;
v1 = (char **)v1[2];
v3 = v2[1];
while ( v3 )
{
v4 = v3;
v3 = (char *)*((_QWORD *)v3 + 2);
v5 = *(char **)v4;
v6 = strlen(*(const char **)v4);
memset(v5, 0, v6);
free(v5);
v7 = (char *)*((_QWORD *)v4 + 1);
v8 = strlen(v7);
memset(v7, 0, v8);
free(v7);
*(_QWORD *)v4 = 0LL;
*((_QWORD *)v4 + 1) = 0LL;
*((_QWORD *)v4 + 2) = 0LL;
free(v4);
}
v9 = *v2;
v10 = strlen(*v2);
memset(v9, 0, v10);
free(v9);
*v2 = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
free(v2);
}
while ( v1 );
}
}

original_code:
void config_destroy (struct config_sect *sects)
{
 while (sects) {
  struct config_sect *next = sects->next;
  struct config_token *token = sects->tokens;
  while (token) {
   struct config_token *next_token = token->next;
   free (memset (token->token, 0, strlen (token->token)));
   free (memset (token->value, 0, strlen (token->value)));
   free (memset (token, 0, sizeof (struct config_token)));
   token = next_token;
  }
  free (memset (sects->name, 0, strlen (sects->name)));
  free (memset (sects, 0, sizeof (struct config_sect)));
  sects = next;
 }
}

canonical_original_code:
void config_destroy(struct config_sect *sects)
{
 while (sects) {
  struct config_sect *next = sects->next;
  struct config_token *token = sects->tokens;
  while (token) {
   struct config_token *next_token = token->next;
   free (memset (token->token, 0, strlen (token->token)));
   free (memset (token->value, 0, strlen (token->value)));
   free (memset (token, 0, sizeof (struct config_token)));
   token = next_token;
  }
  free (memset (sects->name, 0, strlen (sects->name)));
  free (memset (sects, 0, sizeof (struct config_sect)));
  sects = next;
 }
}


repo: ggreg/libsimplenet

decompiled_code:
int socket_unix()
{
return socket(1, 1, 0);
}

canonical_decompiled_code:
int func1()
{
return socket(1, 1, 0);
}

original_code:
inline
int
socket_unix(void)
{
        return socket(1, SOCK_STREAM, 0);
}

canonical_original_code:
inline int socket_unix()
{
        return socket(1, SOCK_STREAM, 0);
}


decompiled_code:
int socket_tcp()
{
return socket(2, 1, 0);
}

canonical_decompiled_code:
int func2()
{
return socket(2, 1, 0);
}

original_code:
inline
int
socket_tcp(void)
{
        return socket(2, SOCK_STREAM, 0);
}

canonical_original_code:
inline int socket_tcp()
{
        return socket(2, SOCK_STREAM, 0);
}


decompiled_code:
__int64 __fastcall socket_init(int a1)
{
int v1; // edx
__int64 result; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = setsockopt(a1, 1, 2, optval, 4u);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v1; // edx
__int64 result; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = setsockopt(a1, 1, 2, optval, 4u);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int
socket_init(int fd)
{
 int optval = 1;
 int err = setsockopt(fd, 1, 2,
   &optval, sizeof(optval));
 if (err == -1) return (*__errno_location ());
 return 0;
}

variable_types: fd int

variable_types: optval int

variable_types: err int


decompiled_code:
__int64 __fastcall socket_close(int a1)
{
int v1; // edx
__int64 result; // rax

v1 = close(a1);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
int v1; // edx
__int64 result; // rax

v1 = close(a1);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int
socket_close(int fd)
{
 int err = close(fd);
 if (err == -1) return (*__errno_location ());
 return 0;
}


decompiled_code:
__int64 __fastcall socket_set_nonblocking(int a1)
{
int v1; // edx
__int64 result; // rax

v1 = fcntl(a1, 4, 2048LL);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
int v1; // edx
__int64 result; // rax

v1 = fcntl(a1, 4, 2048LL);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int
socket_set_nonblocking(int fd)
{
 int err = fcntl(fd, 4, 04000);
 if (err == -1) return (*__errno_location ());
 return 0;
}


decompiled_code:
__int64 __fastcall socket_set_tcpnodelay(int a1)
{
int v1; // edx
__int64 result; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = setsockopt(a1, 1, 1, optval, 4u);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
int v1; // edx
__int64 result; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v1 = setsockopt(a1, 1, 1, optval, 4u);
result = 0LL;
if ( v1 == -1 )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int
socket_set_tcpnodelay(int fd)
{
 int optval = 1;
 socklen_t optlen = sizeof(optval);
 int err = setsockopt(fd, 1, 1, &optval, optlen);
 if (err == -1) return (*__errno_location ());
 return 0;
}

canonical_original_code:
int socket_set_tcpnodelay(int fd)
{
 int optval = 1;
 unsigned int optlen = sizeof(optval);
 int err = setsockopt(fd, 1, 1, &optval, optlen);
 if (err == -1) return (*__errno_location ());
 return 0;
}

variable_types:
optlen
unsigned int


decompiled_code:
__int64 __fastcall socket_listen_unix(int fd, struct sockaddr *addr, char *src, int n)
{
size_t v6; // rax
int v7; // edx
__int64 result; // rax

addr->sa_family = 1;
v6 = strlen(src);
strncpy(addr->sa_data, src, v6);
if ( bind(fd, addr, 0x6Eu) == -1 || (v7 = listen(fd, n), result = 0LL, v7 == -1) )
result = (unsigned int)*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, struct sockaddr *addr, char *src, int n)
{
size_t v6; // rax
int v7; // edx
__int64 result; // rax

addr->sa_family = 1;
v6 = strlen(src);
strncpy(addr->sa_data, src, v6);
if ( bind(fd, addr, 0x6Eu) == -1 || (v7 = listen(fd, n), result = 0LL, v7 == -1) )
result = (unsigned int)*_errno_location();
return result;
}

original_code:
int
socket_listen_unix(int fd, struct sockaddr_un *addr,
  const char *path, int backlog)
{
 const int addr_family = 1;
 addr->sun_family = addr_family;
 strncpy(addr->sun_path, path, strlen(path));
 const size_t addrsize = sizeof(struct sockaddr_un);
 int err = bind(fd, (const struct sockaddr *) addr, addrsize);
 if (err == -1) return (*__errno_location ());
 err = listen(fd, backlog);
 if (err == -1) return (*__errno_location ());
 return 0;
}

canonical_original_code:
int socket_listen_unix(int fd, struct sockaddr_un *addr, char *path, int backlog)
{
 int addr_family = 1;
 addr->sun_family = addr_family;
 strncpy(addr->sun_path, path, strlen(path));
 unsigned long addrsize = sizeof(struct sockaddr_un);
 int err = bind(fd, (const struct sockaddr *) addr, addrsize);
 if (err == -1) return (*__errno_location ());
 err = listen(fd, backlog);
 if (err == -1) return (*__errno_location ());
 return 0;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } *

variable_types:
path
char *

variable_types: backlog int

variable_types:
addrsize
unsigned long


decompiled_code:
__int64 __fastcall socket_listen_tcp(int fd, struct sockaddr *addr, char *cp, __int16 a4, int a5)
{
int v7; // eax
unsigned int v8; // edx
int v9; // eax

addr->sa_family = 2;
v7 = inet_pton(2, cp, &addr->sa_data[2]);
v8 = 22;
if ( v7 )
{
if ( v7 == -1
|| (*(_WORD *)addr->sa_data = _bswap_16(a4), bind(fd, addr, 0x10u) == -1)
|| (v9 = listen(fd, a5), v8 = 0, v9 == -1) )
{
v8 = *_errno_location();
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, struct sockaddr *addr, char *cp, __int16 a4, int a5)
{
int v7; // eax
unsigned int v8; // edx
int v9; // eax

addr->sa_family = 2;
v7 = inet_pton(2, cp, &addr->sa_data[2]);
v8 = 22;
if ( v7 )
{
if ( v7 == -1
|| (*(_WORD *)addr->sa_data = func0(a4), bind(fd, addr, 0x10u) == -1)
|| (v9 = listen(fd, a5), v8 = 0, v9 == -1) )
{
v8 = *_errno_location();
}
}
return v8;
}

original_code:
int
socket_listen_tcp(int fd, struct sockaddr_in *addr,
  const char *host, int port, int backlog)
{
 const int addr_family = 2;
 addr->sin_family = addr_family;
 int ok = inet_pton(addr_family, host, &addr->sin_addr.s_addr);
 if (ok == 0) return 22;
 if (ok == -1) return (*__errno_location ());
 addr->sin_port = htons(port);
 const size_t addrsize = sizeof(struct sockaddr_in);
 int err = bind(fd, (const struct sockaddr *) addr, addrsize);
 if (err == -1) return (*__errno_location ());
 err = listen(fd, backlog);
 if (err == -1) return (*__errno_location ());
 return 0;
}

canonical_original_code:
int socket_listen_tcp(int fd, struct sockaddr_in *addr, char *host, int port, int backlog)
{
 int addr_family = 2;
 addr->sin_family = addr_family;
 int ok = inet_pton(addr_family, host, &addr->sin_addr.s_addr);
 if (ok == 0) return 22;
 if (ok == -1) return (*__errno_location ());
 addr->sin_port = htons(port);
 unsigned long addrsize = sizeof(struct sockaddr_in);
 int err = bind(fd, (const struct sockaddr *) addr, addrsize);
 if (err == -1) return (*__errno_location ());
 err = listen(fd, backlog);
 if (err == -1) return (*__errno_location ());
 return 0;
}

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:
host
char *

variable_types: port int

variable_types: ok int

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


decompiled_code:
int __fastcall socket_connect_unix(int fd, const char *a2)
{
_BYTE v3[104]; // [rsp+0h] [rbp-88h] BYREF
int v4; // [rsp+68h] [rbp-20h]
__int16 v5; // [rsp+6Ch] [rbp-1Ch]

if ( fd == -1 || !a2 )
_assert_fail("fd != -1 && path_unix != NULL", "network_socket.c", 0x8Au, "socket_connect_unix");
memset(v3, 0, sizeof(v3));
v4 = 0;
v5 = 0;
*(_WORD *)v3 = 1;
strcpy(&v3[2], a2);
return connect(fd, (const struct sockaddr *)v3, 0x6Eu);
}

canonical_decompiled_code:
int __fastcall func9(int fd, const char *a2)
{
_BYTE v3[104]; // [rsp+0h] [rbp-88h] BYREF
int v4; // [rsp+68h] [rbp-20h]
__int16 v5; // [rsp+6Ch] [rbp-1Ch]

if ( fd == -1 || !a2 )
_assert_fail("fd != -1 && path_unix != NULL", "network_socket.c", 0x8Au, "socket_connect_unix");
memset(v3, 0, sizeof(v3));
v4 = 0;
v5 = 0;
*(_WORD *)v3 = 1;
strcpy(&v3[2], a2);
return connect(fd, (const struct sockaddr *)v3, 0x6Eu);
}

original_code:
inline
int
socket_connect_unix(const int fd, const char *path_unix)
{
        ((void) sizeof ((fd != -1 && path_unix != ((void *)0)) ? 1 : 0), __extension__ ({ if (fd != -1 && path_unix != ((void *)0)) ; else __assert_fail ("fd != -1 && path_unix != NULL", "network_socket.c", 138, __extension__ __PRETTY_FUNCTION__); }));
        struct sockaddr_un server_addr;
        const socklen_t addr_len = sizeof(server_addr);
        memset(&server_addr, 0, addr_len);
        server_addr.sun_family = 1;
        strcpy(server_addr.sun_path, path_unix);
        return connect(fd, (const struct sockaddr *) &server_addr, addr_len);
}

canonical_original_code:
inline int socket_connect_unix(int fd, char *path_unix)
{
        ((void) sizeof ((fd != -1 && path_unix != ((void *)0)) ? 1 : 0), __extension__ ({ if (fd != -1 && path_unix != ((void *)0)) ; else __assert_fail ("fd != -1 && path_unix != NULL", "network_socket.c", 138, __extension__ __PRETTY_FUNCTION__); }));
        struct sockaddr_un server_addr;
        unsigned int addr_len = sizeof(server_addr);
        memset(&server_addr, 0, addr_len);
        server_addr.sun_family = 1;
        strcpy(server_addr.sun_path, path_unix);
        return connect(fd, (struct sockaddr *) &server_addr, addr_len);
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

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


decompiled_code:
int __fastcall socket_connect_tcp(int fd, char *cp, int a3)
{
struct sockaddr buf[2]; // [rsp+0h] [rbp-28h] BYREF

if ( a3 <= 0 || cp == 0LL || fd == -1 )
_assert_fail("fd != -1 && ip != NULL && port > 0", "network_socket.c", 0x9Bu, "socket_connect_tcp");
*(_QWORD *)&buf[0].sa_family = 2LL;
*(_QWORD *)&buf[0].sa_data[6] = 0LL;
*(_WORD *)buf[0].sa_data = _bswap_16(a3);
inet_pton(2, cp, &buf[0].sa_data[2]);
*(_QWORD *)&buf[0].sa_data[6] = 0LL;
return connect(fd, buf, 0x10u);
}

canonical_decompiled_code:
int __fastcall func10(int fd, char *cp, int a3)
{
struct sockaddr buf[2]; // [rsp+0h] [rbp-28h] BYREF

if ( a3 <= 0 || cp == 0LL || fd == -1 )
_assert_fail("fd != -1 && ip != NULL && port > 0", "network_socket.c", 0x9Bu, "socket_connect_tcp");
*(_QWORD *)&buf[0].sa_family = 2LL;
*(_QWORD *)&buf[0].sa_data[6] = 0LL;
*(_WORD *)buf[0].sa_data = func0(a3);
inet_pton(2, cp, &buf[0].sa_data[2]);
*(_QWORD *)&buf[0].sa_data[6] = 0LL;
return connect(fd, buf, 0x10u);
}

original_code:
inline
int
socket_connect_tcp(const int fd, const char *ip, const int port)
{
        ((void) sizeof ((fd != -1 && ip != ((void *)0) && port > 0) ? 1 : 0), __extension__ ({ if (fd != -1 && ip != ((void *)0) && port > 0) ; else __assert_fail ("fd != -1 && ip != NULL && port > 0", "network_socket.c", 155, __extension__ __PRETTY_FUNCTION__); }));
        struct sockaddr_in server_addr;
        const socklen_t addr_len = sizeof(server_addr);
        memset(&server_addr, 0, addr_len);
        server_addr.sin_family = 2;
        server_addr.sin_port = htons(port);
        inet_pton(2, ip, &server_addr.sin_addr.s_addr);
        memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
        return connect(fd, (const struct sockaddr *) &server_addr, addr_len);
}

canonical_original_code:
inline int socket_connect_tcp(int fd, char *ip, int port)
{
        ((void) sizeof ((fd != -1 && ip != ((void *)0) && port > 0) ? 1 : 0), __extension__ ({ if (fd != -1 && ip != ((void *)0) && port > 0) ; else __assert_fail ("fd != -1 && ip != NULL && port > 0", "network_socket.c", 155, __extension__ __PRETTY_FUNCTION__); }));
        struct sockaddr_in server_addr;
        unsigned int addr_len = sizeof(server_addr);
        memset(&server_addr, 0, addr_len);
        server_addr.sin_family = 2;
        server_addr.sin_port = htons(port);
        inet_pton(2, ip, &server_addr.sin_addr.s_addr);
        memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
        return connect(fd, (struct sockaddr *) &server_addr, addr_len);
}

variable_types:
ip
char *

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


decompiled_code:
__int64 __fastcall network_client_connect_tcp(unsigned int *a1, __int64 a2)
{
return socket_connect_tcp(*a1, *(_QWORD *)a2, *(unsigned int *)(a2 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, __int64 a2)
{
return socket_connect_tcp(*a1, *(_QWORD *)a2, *(unsigned int *)(a2 + 8));
}

original_code:
static
int
network_client_connect_tcp(struct network_client *client, const void *conf_)
{
    const struct socket_config_tcp *conf = conf_;
    return socket_connect_tcp(client->fd, conf->ip, conf->port);
}

canonical_original_code:
static int network_client_connect_tcp(struct network_client *client, void *conf_)
{
    struct socket_config_tcp *conf = conf_;
    return socket_connect_tcp(client->fd, conf->ip, conf->port);
}

variable_types: struct network_client { int fd; enum socket_type_t { SOCKET_UNIX=0, SOCKET_TCP=1, SOCKET_INVALID=2 } type; } *

variable_types: struct socket_config_tcp { char *ip; int port; int backlog; } *

user_defined_types: struct network_client { int fd; enum socket_type_t type; }


decompiled_code:
__int64 __fastcall network_client_connect_unix(unsigned int *a1, _QWORD *a2)
{
return socket_connect_unix(*a1, *a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, _QWORD *a2)
{
return socket_connect_unix(*a1, *a2);
}

original_code:
static
int
network_client_connect_unix(struct network_client *client, const void *conf_)
{
    const struct socket_config_unix *conf = conf_;
    return socket_connect_unix(client->fd, conf->path);
}

canonical_original_code:
static int network_client_connect_unix(struct network_client *client, void *conf_)
{
    struct socket_config_unix *conf = conf_;
    return socket_connect_unix(client->fd, conf->path);
}

variable_types: struct socket_config_unix { char *path; int backlog; } *

user_defined_types: enum socket_type_t { SOCKET_UNIX=0, SOCKET_TCP=1, SOCKET_INVALID=2 }


decompiled_code:
void __fastcall network_client_free(void *a1)
{
if ( !a1 )
_assert_fail("client != NULL", "network_client.c", 0x41u, "network_client_free");
free(a1);
}

canonical_decompiled_code:
void __fastcall func8(void *a1)
{
if ( !a1 )
_assert_fail("client != NULL", "network_client.c", 0x41u, "network_client_free");
free(a1);
}

original_code:
void
network_client_free(struct network_client *client)
{
    ((void) sizeof ((client != ((void *)0)) ? 1 : 0), __extension__ ({ if (client != ((void *)0)) ; else __assert_fail ("client != NULL", "network_client.c", 65, __extension__ __PRETTY_FUNCTION__); }));
    free(client);
}


decompiled_code:
__int64 __fastcall network_client_close(unsigned int *a1)
{
return socket_close(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1)
{
return socket_close(*a1);
}

original_code:
int
network_client_close(struct network_client *client)
{
    return socket_close(client->fd);
}


decompiled_code:
__int64 __fastcall network_client_connect(__int64 a1)
{
return ((__int64 (*)(void))*(&socket_type_ops + 2 * *(unsigned int *)(a1 + 4) + 1))();
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return ((__int64 (*)(void))*(&socket_type_ops + 2 * *(unsigned int *)(a1 + 4) + 1))();
}

original_code:
int
network_client_connect(struct network_client *client, void *conf)
{
    if (client->type < SOCKET_UNIX && client->type >= SOCKET_INVALID)
 return 22;
    networkclient_connect_t socket_connect;
    socket_connect = socket_type_ops[client->type].connect;
    return socket_connect(client, conf);
}

canonical_original_code:
int network_client_connect(struct network_client *client, void *conf)
{
    if (client->type < SOCKET_UNIX && client->type >= SOCKET_INVALID)
 return 22;
    int (*socket_connect)(struct network_client *, void *);
    socket_connect = socket_type_ops[client->type].connect;
    return socket_connect(client, conf);
}

variable_types:
conf
void *

variable_types: int (*)(struct network_client { int fd; enum socket_type_t { SOCKET_UNIX=0, SOCKET_TCP=1, SOCKET_INVALID=2 } type; } *, void *)


decompiled_code:
__int64 __fastcall network_client_send(int *a1, __int64 a2, unsigned int a3)
{
unsigned int v3; // ebp
const void *v4; // rax
ssize_t v5; // rax
int v6; // ebx

v3 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v4 = (const void *)simple_buffer_get_head(a2);
v5 = write(*a1, v4, v3);
v6 = v5;
if ( !v5 )
break;
if ( v5 == -1 )
return (unsigned int)*_errno_location();
simple_buffer_pull(a2, v5);
v3 -= v6;
if ( !v3 )
return 0LL;
}
return 11LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int *a1, __int64 a2, unsigned int a3)
{
unsigned int v3; // ebp
const void *v4; // rax
ssize_t v5; // rax
int v6; // ebx

v3 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v4 = (const void *)func0(a2);
v5 = write(*a1, v4, v3);
v6 = v5;
if ( !v5 )
break;
if ( v5 == -1 )
return (unsigned int)*_errno_location();
func2(a2, v5);
v3 -= v6;
if ( !v3 )
return 0LL;
}
return 11LL;
}

original_code:
int
network_client_send(struct network_client *client,
 struct simple_buffer *data, unsigned int len)
{
    while (len) {
 ssize_t n = write(client->fd, simple_buffer_get_head(data), len);
 if (n == 0) return 11;
 if (n == -1) return (*__errno_location ());
 simple_buffer_pull(data, n);
 len -= n;
    }
    return 0;
}

canonical_original_code:
int network_client_send(struct network_client *client, struct simple_buffer *data, unsigned int len)
{
    while (len) {
 long n = write(client->fd, simple_buffer_get_head(data), len);
 if (n == 0) return 11;
 if (n == -1) return (*__errno_location ());
 simple_buffer_pull(data, n);
 len -= n;
    }
    return 0;
}

variable_types: struct simple_buffer { char *data; char *head; char *tail; char *userptr; unsigned int size; unsigned int max_size; unsigned int chunk_size; } *

variable_types:
len
unsigned int

variable_types: n long


decompiled_code:
__int64 __fastcall network_client_recv(int *a1, _DWORD *a2, unsigned int a3)
{
unsigned int v3; // ebp
void *v4; // rax
ssize_t v5; // rax
int v6; // ebx

v3 = a3;
simple_buffer_resize_tail((__int64)a2, a3);
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = (void *)simple_buffer_get_tail((__int64)a2);
v5 = read(*a1, v4, v3);
v6 = v5;
if ( !v5 )
break;
if ( v5 == -1 )
return (unsigned int)*_errno_location();
simple_buffer_move_tail(a2, v5);
v3 -= v6;
if ( !v3 )
return 0LL;
}
return 11LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int *a1, _DWORD *a2, unsigned int a3)
{
unsigned int v3; // ebp
void *v4; // rax
ssize_t v5; // rax
int v6; // ebx

v3 = a3;
func7((__int64)a2, a3);
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = (void *)func1((__int64)a2);
v5 = read(*a1, v4, v3);
v6 = v5;
if ( !v5 )
break;
if ( v5 == -1 )
return (unsigned int)*_errno_location();
func3(a2, v5);
v3 -= v6;
if ( !v3 )
return 0LL;
}
return 11LL;
}

original_code:
int
network_client_recv(struct network_client *client,
 struct simple_buffer *data, unsigned int len)
{
    simple_buffer_resize_tail(data, len);
    while (len) {
 ssize_t n = read(client->fd, simple_buffer_get_tail(data), len);
 if (n == 0)return 11;
 if (n == -1) return (*__errno_location ());
 simple_buffer_move_tail(data, n);
 len -= n;
    }
    return 0;
}

canonical_original_code:
int network_client_recv(struct network_client *client, struct simple_buffer *data, unsigned int len)
{
    simple_buffer_resize_tail(data, len);
    while (len) {
 long n = read(client->fd, simple_buffer_get_tail(data), len);
 if (n == 0)return 11;
 if (n == -1) return (*__errno_location ());
 simple_buffer_move_tail(data, n);
 len -= n;
    }
    return 0;
}

user_defined_types: struct simple_buffer { char *data; char *head; char *tail; char *userptr; unsigned int size; unsigned int max_size; unsigned int chunk_size; }


original_code:
static inline
int
simple_buffer_resize(struct simple_buffer * const buf, size_t newsize)
{
 if (newsize != buf->max_size) {
  size_t head_offset = buf->head - buf->data;
  size_t tail_offset = buf->tail - buf->data;
  size_t userptr_offset = buf->userptr - buf->data;
  buf->max_size = (newsize/buf->chunk_size+1) * buf->chunk_size;
  char *newbufdata = realloc(buf->data, buf->max_size);
  if (newbufdata == ((void *)0))
   return (*__errno_location ());
  buf->data = newbufdata;
  buf->head = buf->data + head_offset;
  buf->tail = buf->data + tail_offset;
  buf->userptr = buf->data + userptr_offset;
 }
 return 0;
}

canonical_original_code:
static inline int simple_buffer_resize(struct simple_buffer *buf, unsigned long newsize)
{
 if (newsize != buf->max_size) {
  unsigned long head_offset = buf->head - buf->data;
  unsigned long tail_offset = buf->tail - buf->data;
  unsigned long userptr_offset = buf->userptr - buf->data;
  buf->max_size = (newsize/buf->chunk_size+1) * buf->chunk_size;
  char *newbufdata = realloc(buf->data, buf->max_size);
  if (newbufdata == ((void *)0))
   return (*__errno_location ());
  buf->data = newbufdata;
  buf->head = buf->data + head_offset;
  buf->tail = buf->data + tail_offset;
  buf->userptr = buf->data + userptr_offset;
 }
 return 0;
}

variable_types:
newsize
unsigned long

variable_types:
newbufdata
char *


decompiled_code:
__int64 __fastcall client_callback_do_request(const char *a1, __int64 a2, __int64 a3, _DWORD *a4)
{
size_t v6; // rax
unsigned int v7; // ebp
const void *v8; // rax

v6 = strlen(a1);
simple_buffer_append(a2, a1, v6);
v7 = simple_buffer_size(a3);
v8 = (const void *)simple_buffer_get_head(a3);
simple_buffer_append(a2, v8, v7);
simple_buffer_clear(a3);
*a4 = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, __int64 a2, __int64 a3, _DWORD *a4)
{
size_t v6; // rax
unsigned int v7; // ebp
const void *v8; // rax

v6 = strlen(a1);
func4(a2, a1, v6);
v7 = func0(a3);
v8 = (const void *)func1(a3);
func4(a2, v8, v7);
func5(a3);
*a4 = 1;
return 0LL;
}

original_code:
int
client_callback_do_request(void *data,
  struct simple_buffer *bufwrite,
  struct simple_buffer *bufread,
  int *done)
{
 simple_buffer_append(bufwrite, (char *) data, strlen(data));
 simple_buffer_append(bufwrite,
   simple_buffer_get_head(bufread),
   simple_buffer_size(bufread));
 simple_buffer_clear(bufread);
 *done = 1;
 return 0;
}

variable_types:
data
void *

variable_types:
bufread
struct simple_buffer *

variable_types:
done
int *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v5; // r8
const char **v6; // r12
__int64 v7; // rdi
__int64 v8; // rbx
char *v9; // rdi
__int64 i; // rcx
unsigned int v11; // ebp
int *v12; // rax
void (**v13)(int, const char *, ...); // [rsp+0h] [rbp-68h] BYREF
char v14; // [rsp+8h] [rbp-60h] BYREF
__int64 (__fastcall *v15)(const char *, __int64, __int64, _DWORD *); // [rsp+10h] [rbp-58h]
const char *v16; // [rsp+30h] [rbp-38h] BYREF
int v17; // [rsp+38h] [rbp-30h]
int v18; // [rsp+3Ch] [rbp-2Ch]
const char *v19; // [rsp+40h] [rbp-28h] BYREF
int v20; // [rsp+48h] [rbp-20h]

if ( argc > 2 )
{
v5 = argv[1];
if ( !memcmp("unix", v5, 4uLL) )
{
v19 = "/tmp/simpleserver.socket";
v20 = 2;
}
if ( !memcmp("tcp", v5, 3uLL) )
{
if ( argc <= 3 )
{
usage(argv);
exit(22);
}
v16 = "127.0.0.1";
v17 = 12345;
v18 = 2;
v6 = &v16;
v7 = 1LL;
}
else
{
v6 = &v19;
v7 = 0LL;
}
v8 = server_new(v7, 2LL, argv);
if ( v8 )
{
v9 = &v14;
for ( i = 8LL; i; --i )
{
*(_DWORD *)v9 = 0;
v9 += 4;
}
v13 = &syslog;
v15 = client_callback_do_request;
v11 = server_init(v8, &v13, "hello, ", 2048LL);
if ( !v11 )
server_listen(v8, v6);
server_stop(v8, v11);
exit(v11);
}
v12 = _errno_location();
exit(*v12);
}
usage(argv);
exit(22);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v5; // r8
const char **v6; // r12
__int64 v7; // rdi
__int64 v8; // rbx
char *v9; // rdi
__int64 i; // rcx
unsigned int v11; // ebp
int *v12; // rax
void (**v13)(int, const char *, ...); // [rsp+0h] [rbp-68h] BYREF
char v14; // [rsp+8h] [rbp-60h] BYREF
__int64 (__fastcall *v15)(const char *, __int64, __int64, _DWORD *); // [rsp+10h] [rbp-58h]
const char *v16; // [rsp+30h] [rbp-38h] BYREF
int v17; // [rsp+38h] [rbp-30h]
int v18; // [rsp+3Ch] [rbp-2Ch]
const char *v19; // [rsp+40h] [rbp-28h] BYREF
int v20; // [rsp+48h] [rbp-20h]

if ( argc > 2 )
{
v5 = argv[1];
if ( !memcmp("unix", v5, 4uLL) )
{
v19 = "/tmp/simpleserver.socket";
v20 = 2;
}
if ( !memcmp("tcp", v5, 3uLL) )
{
if ( argc <= 3 )
{
func7(argv);
exit(22);
}
v16 = "127.0.0.1";
v17 = 12345;
v18 = 2;
v6 = &v16;
v7 = 1LL;
}
else
{
v6 = &v19;
v7 = 0LL;
}
v8 = server_new(v7, 2LL, argv);
if ( v8 )
{
v9 = &v14;
for ( i = 8LL; i; --i )
{
*(_DWORD *)v9 = 0;
v9 += 4;
}
v13 = &syslog;
v15 = client_callback_do_request;
v11 = server_init(v8, &v13, "hello, ", 2048LL);
if ( !v11 )
server_listen(v8, v6);
server_stop(v8, v11);
exit(v11);
}
v12 = _errno_location();
exit(*v12);
}
func7(argv);
exit(22);
}

original_code:
int
main(int argc, char **argv)
{
    if (argc < 3) {
 usage(argv);
 exit(22);
    }
    int socket_type;
    void *conf;
    struct socket_config_unix unixcf;
    if (strncmp("unix", argv[1], 4) == 0) {
     unixcf.path = argv[2];
     conf = &unixcf;
     socket_type = SOCKET_UNIX;
    }
    struct socket_config_tcp tcpcf;
    if (strncmp("tcp", argv[1], 3) == 0) {
 if (argc < 4) {
  usage(argv);
  exit(22);
 }
 tcpcf.ip = argv[2];
 tcpcf.port = atoi(argv[3]);
 conf = &tcpcf;
 socket_type = SOCKET_TCP;
    };
    struct network_client *client = network_client_new(socket_type);
    if (client == ((void *)0)) {
     fprintf(stderr,
      "error in network_client_new(): %s\n",
      strerror((*__errno_location ())));
     exit(1);
    }
    struct simple_buffer *bufwrite = simple_buffer_new(getpagesize());
    simple_buffer_append(bufwrite, "client", 6);
    struct simple_buffer *bufread = simple_buffer_new(getpagesize());
    int err = network_client_connect(client, conf);
    if (err) {
     fprintf(stderr,
      "error in network_client_connect(): %s\n",
      strerror(err));
     exit(err);
    }
    err = network_client_send(client, bufwrite, 6);
    if (err) {
     fprintf(stderr,
      "error cannot write to socket: %s\n",
      strerror(err));
     goto end;
    }
    err = network_client_recv(client, bufread, 13);
    if (err) {
     fprintf(stderr,
      "error cannot read from socket: %s\n",
      strerror(err));
     goto end;
    }
    write(1, simple_buffer_get_head(bufread), 13);
    write(1, "\n", 1);
end:
    network_client_free(client);
    simple_buffer_free(bufread);
    simple_buffer_free(bufwrite);
    exit(err);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct socket_config_unix { char *path; int backlog; }

variable_types: struct socket_config_tcp { char *ip; int port; int backlog; }

variable_types: err int


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

if ( !ptr )
_assert_fail("buf != NULL", "network_buffer.h", 0x47u, "simple_buffer_free");
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

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

if ( !ptr )
_assert_fail("buf != NULL", "network_buffer.h", 0x47u, "simple_buffer_free");
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

original_code:
static inline
void
simple_buffer_free(struct simple_buffer *buf)
{
 ((void) sizeof ((buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (buf != ((void *)0)) ; else __assert_fail ("buf != NULL", "network_buffer.h", 71, __extension__ __PRETTY_FUNCTION__); }));
 if (buf->data)
  free(buf->data);
 free(buf);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v5; // r8
bool v6; // cl
bool v7; // cf
bool v8; // zf
__int64 v9; // rcx
const char *v10; // rsi
const char *v11; // rdi
const char **v12; // r14
__int64 v13; // rdi
__int64 v14; // rbx
unsigned int v15; // ebp
_QWORD *v16; // r12
_QWORD *v17; // r13
int v18; // eax
int v19; // ebp
char *v20; // rax
int *v21; // rax
char *v22; // rax
int v23; // eax
int v24; // ebp
int v25; // eax
const void *v26; // rax
char *v27; // rax
char *v28; // rax
const char *v29; // [rsp+0h] [rbp-48h] BYREF
int v30; // [rsp+8h] [rbp-40h]
const char *v31; // [rsp+10h] [rbp-38h] BYREF

if ( argc > 2 )
{
v5 = argv[1];
v6 = memcmp("unix", v5, 4uLL) != 0;
v7 = 0;
v8 = !v6;
if ( !v6 )
v31 = argv[2];
v9 = 3LL;
v10 = "tcp";
v11 = v5;
do
{
if ( !v9 )
break;
v7 = *v10 < (unsigned int)*v11;
v8 = *v10++ == *v11++;
--v9;
}
while ( v8 );
if ( (!v7 && !v8) == v7 )
{
if ( argc <= 3 )
{
usage(argv);
exit(22);
}
v29 = argv[2];
v30 = atoi(argv[3]);
v12 = &v29;
v13 = 1LL;
}
else
{
v12 = &v31;
v13 = 0LL;
}
v14 = network_client_new(v13, v10);
if ( v14 )
{
v15 = getpagesize();
v16 = simple_buffer_new(v15);
simple_buffer_append((__int64)v16, "client", 6uLL);
v17 = simple_buffer_new(v15);
v18 = network_client_connect(v14, v12);
v19 = v18;
if ( v18 )
{
v20 = strerror(v18);
fprintf(stderr, "error in network_client_connect(): %s\n", v20);
exit(v19);
}
v23 = network_client_send(v14, v16, 6LL);
v24 = v23;
if ( v23 )
{
v27 = strerror(v23);
fprintf(stderr, "error cannot write to socket: %s\n", v27);
}
else
{
v25 = network_client_recv(v14, v17, 13LL);
v24 = v25;
if ( v25 )
{
v28 = strerror(v25);
fprintf(stderr, "error cannot read from socket: %s\n", v28);
}
else
{
v26 = (const void *)simple_buffer_get_head((__int64)v17);
write(1, v26, 0xDuLL);
write(1, "\n", 1uLL);
}
}
network_client_free(v14);
simple_buffer_free(v17);
simple_buffer_free(v16);
exit(v24);
}
v21 = _errno_location();
v22 = strerror(*v21);
fprintf(stderr, "error in network_client_new(): %s\n", v22);
exit(1);
}
usage(argv);
exit(22);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v5; // r8
bool v6; // cl
bool v7; // cf
bool v8; // zf
__int64 v9; // rcx
const char *v10; // rsi
const char *v11; // rdi
const char **v12; // r14
__int64 v13; // rdi
__int64 v14; // rbx
unsigned int v15; // ebp
_QWORD *v16; // r12
_QWORD *v17; // r13
int v18; // eax
int v19; // ebp
char *v20; // rax
int *v21; // rax
char *v22; // rax
int v23; // eax
int v24; // ebp
int v25; // eax
const void *v26; // rax
char *v27; // rax
char *v28; // rax
const char *v29; // [rsp+0h] [rbp-48h] BYREF
int v30; // [rsp+8h] [rbp-40h]
const char *v31; // [rsp+10h] [rbp-38h] BYREF

if ( argc > 2 )
{
v5 = argv[1];
v6 = memcmp("unix", v5, 4uLL) != 0;
v7 = 0;
v8 = !v6;
if ( !v6 )
v31 = argv[2];
v9 = 3LL;
v10 = "tcp";
v11 = v5;
do
{
if ( !v9 )
break;
v7 = *v10 < (unsigned int)*v11;
v8 = *v10++ == *v11++;
--v9;
}
while ( v8 );
if ( (!v7 && !v8) == v7 )
{
if ( argc <= 3 )
{
func4(argv);
exit(22);
}
v29 = argv[2];
v30 = atoi(argv[3]);
v12 = &v29;
v13 = 1LL;
}
else
{
v12 = &v31;
v13 = 0LL;
}
v14 = network_client_new(v13, v10);
if ( v14 )
{
v15 = getpagesize();
v16 = simple_buffer_new(v15);
func2((__int64)v16, "client", 6uLL);
v17 = simple_buffer_new(v15);
v18 = network_client_connect(v14, v12);
v19 = v18;
if ( v18 )
{
v20 = strerror(v18);
fprintf(stderr, "error in network_client_connect(): %s\n", v20);
exit(v19);
}
v23 = network_client_send(v14, v16, 6LL);
v24 = v23;
if ( v23 )
{
v27 = strerror(v23);
fprintf(stderr, "error cannot write to socket: %s\n", v27);
}
else
{
v25 = network_client_recv(v14, v17, 13LL);
v24 = v25;
if ( v25 )
{
v28 = strerror(v25);
fprintf(stderr, "error cannot read from socket: %s\n", v28);
}
else
{
v26 = (const void *)func0((__int64)v17);
write(1, v26, 0xDuLL);
write(1, "\n", 1uLL);
}
}
network_client_free(v14);
func3(v17);
func3(v16);
exit(v24);
}
v21 = _errno_location();
v22 = strerror(*v21);
fprintf(stderr, "error in network_client_new(): %s\n", v22);
exit(1);
}
func4(argv);
exit(22);
}

canonical_original_code:
int main(int argc, char **argv)
{
    if (argc < 3) {
 usage(argv);
 exit(22);
    }
    int socket_type;
    void *conf;
    struct socket_config_unix unixcf;
    if (strncmp("unix", argv[1], 4) == 0) {
     unixcf.path = argv[2];
     conf = &unixcf;
     socket_type = SOCKET_UNIX;
    }
    struct socket_config_tcp tcpcf;
    if (strncmp("tcp", argv[1], 3) == 0) {
 if (argc < 4) {
  usage(argv);
  exit(22);
 }
 tcpcf.ip = argv[2];
 tcpcf.port = atoi(argv[3]);
 conf = &tcpcf;
 socket_type = SOCKET_TCP;
    };
    struct network_client *client = network_client_new(socket_type);
    if (client == ((void *)0)) {
     fprintf(stderr,
      "error in network_client_new(): %s\n",
      strerror((*__errno_location ())));
     exit(1);
    }
    struct simple_buffer *bufwrite = simple_buffer_new(getpagesize());
    simple_buffer_append(bufwrite, "client", 6);
    struct simple_buffer *bufread = simple_buffer_new(getpagesize());
    int err = network_client_connect(client, conf);
    if (err) {
     fprintf(stderr,
      "error in network_client_connect(): %s\n",
      strerror(err));
     exit(err);
    }
    err = network_client_send(client, bufwrite, 6);
    if (err) {
     fprintf(stderr,
      "error cannot write to socket: %s\n",
      strerror(err));
     goto end;
    }
    err = network_client_recv(client, bufread, 13);
    if (err) {
     fprintf(stderr,
      "error cannot read from socket: %s\n",
      strerror(err));
     goto end;
    }
    write(1, simple_buffer_get_head(bufread), 13);
    write(1, "\n", 1);
end:
    network_client_free(client);
    simple_buffer_free(bufread);
    simple_buffer_free(bufwrite);
    exit(err);
}

variable_types: struct network_client { int fd; enum socket_type_t { SOCKET_UNIX=0, SOCKET_TCP=1, SOCKET_INVALID=2 } type; } *

user_defined_types: struct socket_config_unix { char *path; int backlog; }

user_defined_types: struct socket_config_tcp { char *ip; int port; int backlog; }


decompiled_code:
__int64 __fastcall detect_stack_direction(unsigned __int64 a1)
{
char v2[4]; // [rsp+0h] [rbp-4h] BYREF

if ( !stack_direction_cached )
{
if ( a1 <= (unsigned __int64)v2 )
stack_direction_cached = a1 < (unsigned __int64)v2 ? 3 : 1;
else
stack_direction_cached = 2;
}
return (unsigned int)stack_direction_cached;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
char v2[4]; // [rsp+0h] [rbp-4h] BYREF

if ( !stack_direction_cached )
{
if ( a1 <= (unsigned __int64)v2 )
stack_direction_cached = a1 < (unsigned __int64)v2 ? 3 : 1;
else
stack_direction_cached = 2;
}
return (unsigned int)stack_direction_cached;
}

original_code:
static enum stack_direction detect_stack_direction (void *before)
{
 if (stack_direction_cached == STACK_DIRECTION_UNSET)
 {
  int tmp;
  void *after = &tmp;
  if (after < before)
   stack_direction_cached = STACK_DIRECTION_DOWN;
  else if (after > before)
   stack_direction_cached = STACK_DIRECTION_UP;
  else
   stack_direction_cached = STACK_DIRECTION_UNKNOWN;
 }
 return stack_direction_cached;
}

canonical_original_code:
static enum stack_direction detect_stack_direction(void *before)
{
 if (stack_direction_cached == STACK_DIRECTION_UNSET)
 {
  int tmp;
  void *after = &tmp;
  if (after < before)
   stack_direction_cached = STACK_DIRECTION_DOWN;
  else if (after > before)
   stack_direction_cached = STACK_DIRECTION_UP;
  else
   stack_direction_cached = STACK_DIRECTION_UNKNOWN;
 }
 return stack_direction_cached;
}

variable_types:
before
void *

variable_types: tmp int

variable_types:
after
void *

return_type: enum stack_direction { STACK_DIRECTION_UNSET=0, STACK_DIRECTION_UNKNOWN=1, STACK_DIRECTION_DOWN=2, STACK_DIRECTION_UP=3 }


decompiled_code:
__int64 detect_stack_direction_wrong()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 1LL;
}

original_code:
static enum stack_direction detect_stack_direction_wrong (void *before)
{
 return STACK_DIRECTION_UNKNOWN;
}

canonical_original_code:
static enum stack_direction detect_stack_direction_wrong(void *before)
{
 return STACK_DIRECTION_UNKNOWN;
}

user_defined_types: enum stack_direction { STACK_DIRECTION_UNSET=0, STACK_DIRECTION_UNKNOWN=1, STACK_DIRECTION_DOWN=2, STACK_DIRECTION_UP=3 }


decompiled_code:
void stack_wrapper_wrong()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
static void stack_wrapper_wrong (stack_magic_callback callback,
 void *data, size_t dummysize,
 jmp_buf parent, jmp_buf child)
{
}

canonical_original_code:
static void stack_wrapper_wrong(void (*callback)(void *, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1]), void *data, unsigned long dummysize, struct __jmp_buf_tag parent[1], struct __jmp_buf_tag child[1])
{
}

variable_types: void (*)(void *, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1], struct __jmp_buf_tag [1])

variable_types:
dummysize
unsigned long

variable_types:
parent
struct __jmp_buf_tag [1]

variable_types:
child
struct __jmp_buf_tag [1]

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }