decompiled_code:
int __fastcall str_2_sig(char *nptr, _DWORD *a2)
{
const char **v2; // r13
const char *v4; // rsi
int v5; // ebx
int result; // eax
__int64 v7; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v2 = (const char **)off_440;
v4 = "EXIT";
v5 = 0;
do
{
result = strcmp(nptr, v4);
if ( !result )
{
*a2 = sig_strs[6 * v5];
return result;
}
v4 = *v2;
v2 += 3;
++v5;
}
while ( v4 );
v7 = strtol(nptr, &endptr, 10);
if ( *endptr || v7 < 0 || v5 <= v7 || ((*nptr - 43) & 0xFD) == 0 )
return -1;
*a2 = v7;
return 0;
}

canonical_decompiled_code:
int __fastcall func0(char *nptr, _DWORD *a2)
{
const char **v2; // r13
const char *v4; // rsi
int v5; // ebx
int result; // eax
__int64 v7; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v2 = (const char **)off_440;
v4 = "EXIT";
v5 = 0;
do
{
result = strcmp(nptr, v4);
if ( !result )
{
*a2 = sig_strs[6 * v5];
return result;
}
v4 = *v2;
v2 += 3;
++v5;
}
while ( v4 );
v7 = strtol(nptr, &endptr, 10);
if ( *endptr || v7 < 0 || v5 <= v7 || ((*nptr - 43) & 0xFD) == 0 )
return -1;
*a2 = v7;
return 0;
}

original_code:
int
str_2_sig(const char *str, int *signum)
{
 register int i;
 long n;
 char *x;
 for (i = 0; sig_strs[i].sig_str; i++)
  if (strcmp(str, sig_strs[i].sig_str) == 0)
   break;
 if (sig_strs[i].sig_str == ((void *)0)) {
  n = strtol(str, &x, 10);
  if (*x != '\0' || n < 0 || n >= i || *str == '+' || *str == '-')
   return -1;
  *signum = n;
 } else
  *signum = sig_strs[i].sig_num;
 return 0;
}

canonical_original_code:
int str_2_sig(char *str, int *signum)
{
 register int i;
 long n;
 char *x;
 for (i = 0; sig_strs[i].sig_str; i++)
  if (strcmp(str, sig_strs[i].sig_str) == 0)
   break;
 if (sig_strs[i].sig_str == ((void *)0)) {
  n = strtol(str, &x, 10);
  if (*x != '\0' || n < 0 || n >= i || *str == '+' || *str == '-')
   return -1;
  *signum = n;
 } else
  *signum = sig_strs[i].sig_num;
 return 0;
}

variable_types:
str
char *

variable_types:
signum
int *

variable_types: i int

variable_types: n long

variable_types:
x
char *


repo: mutoso-mirrors/heirloom-sh

decompiled_code:
__int64 __fastcall sig_2_str(int a1, char *a2)
{
char *v3; // rax
int v4; // edx
const char *v5; // rsi

v3 = (char *)&unk_438;
v4 = 0;
v5 = "EXIT";
while ( 1 )
{
if ( a1 == v4 )
{
strcpy(a2, v5);
return 0LL;
}
v5 = (const char *)*((_QWORD *)v3 + 1);
if ( !v5 )
break;
v4 = *(_DWORD *)v3;
v3 += 24;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, char *a2)
{
char *v3; // rax
int v4; // edx
const char *v5; // rsi

v3 = (char *)&unk_438;
v4 = 0;
v5 = "EXIT";
while ( 1 )
{
if ( a1 == v4 )
{
strcpy(a2, v5);
return 0LL;
}
v5 = (const char *)*((_QWORD *)v3 + 1);
if ( !v5 )
break;
v4 = *(_DWORD *)v3;
v3 += 24;
}
return 0xFFFFFFFFLL;
}

original_code:
int
sig_2_str(int signum, char *str)
{
 register int i;
 for (i = 0; sig_strs[i].sig_str; i++)
  if (sig_strs[i].sig_num == signum)
   break;
 if (sig_strs[i].sig_str == ((void *)0))
  return -1;
 strcpy(str, sig_strs[i].sig_str);
 return 0;
}

variable_types: signum int

return_type: int


decompiled_code:
const char *__fastcall str_signal(int a1)
{
char *v1; // rax
int v2; // edx
const char *v3; // r8

v1 = (char *)&unk_438;
v2 = 0;
v3 = "UNKNOWN SIGNAL";
while ( a1 != v2 )
{
v3 = (const char *)*((_QWORD *)v1 + 2);
if ( !v3 )
break;
v2 = *(_DWORD *)v1;
v1 += 24;
}
return v3;
}

canonical_decompiled_code:
const char *__fastcall func2(int a1)
{
char *v1; // rax
int v2; // edx
const char *v3; // r8

v1 = (char *)&unk_438;
v2 = 0;
v3 = "UNKNOWN SIGNAL";
while ( a1 != v2 )
{
v3 = (const char *)*((_QWORD *)v1 + 2);
if ( !v3 )
break;
v2 = *(_DWORD *)v1;
v1 += 24;
}
return v3;
}

original_code:
char *
str_signal(int signum)
{
 register int i;
 for (i = 0; sig_strs[i].sig_name; i++)
  if (sig_strs[i].sig_num == signum)
   break;
 return (char *)sig_strs[i].sig_name;
}

canonical_original_code:
char *str_signal(int signum)
{
 register int i;
 for (i = 0; sig_strs[i].sig_name; i++)
  if (sig_strs[i].sig_num == signum)
   break;
 return (char *)sig_strs[i].sig_name;
}

return_type: char *


mbtowi

decompiled_code:
int __fastcall mbtowi(wchar_t *a1, const char *a2, size_t a3)
{
int result; // eax
wchar_t pwc[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = mbtowc(pwc, a2, a3);
*a1 = pwc[0];
return result;
}

canonical_decompiled_code:
int __fastcall func0(wchar_t *a1, const char *a2, size_t a3)
{
int result; // eax
wchar_t pwc[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = mbtowc(pwc, a2, a3);
*a1 = pwc[0];
return result;
}

original_code:
static
 inline
 int
mbtowi(wint_t *pwi, const char *s, size_t n)
{
 wchar_t wc;
 int i;
 i = mbtowc(&wc, s, n);
 *pwi = wc;
 return i;
}

canonical_original_code:
static inline int mbtowi(unsigned int *pwi, char *s, unsigned long n)
{
 int wc;
 int i;
 i = mbtowc(&wc, s, n);
 *pwi = wc;
 return i;
}

variable_types:
pwi
unsigned int *

variable_types:
s
char *

variable_types:
n
unsigned long

variable_types: wc int


gmatch

decompiled_code:
__int64 __fastcall gmatch(const char *a1, const char *a2)
{
const char *v3; // rbx
int v4; // ebp
int v5; // er14
unsigned int v6; // eax
const char *v7; // r12
__int64 v8; // rsi
const char *v9; // r15
unsigned int v10; // er8
int v12; // eax
__int64 v13; // rdx
unsigned __int8 *v14; // r15
char *v15; // rbp
unsigned __int64 v16; // rbx
int v17; // er13
char v18; // dl
unsigned int v19; // eax
__int64 v20; // rdx
__int64 v21; // rdx
__int64 v22; // rax
int v23; // eax
__int64 v24; // r12
int v25; // eax
unsigned int v26; // eax
__int64 v27; // rdx
int v28; // eax
__int64 v29; // rdx
int v30; // eax
int v31; // eax
int v32; // eax
bool v33; // zf
int v34; // eax
int v35; // [rsp+Ch] [rbp-4Ch]
unsigned int v36; // [rsp+18h] [rbp-40h] BYREF
wchar_t v37[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v3 = a1;
v4 = _ctype_get_mb_cur_max();
v5 = v4;
if ( v4 <= 1 )
{
v7 = a1 + 1;
v37[0] = *(unsigned __int8 *)a1;
v6 = *(unsigned __int8 *)a2;
goto LABEL_4;
}
if ( *a1 < 0 )
{
v23 = mbtowi(v37, a1, v4);
v24 = v23;
if ( v23 <= 0 )
{
if ( v23 )
return 0;
v24 = 1LL;
}
v7 = &a1[v24];
if ( v37[0] == -1 )
return 0;
v6 = *(unsigned __int8 *)a2;
if ( (v6 & 0x80u) == 0 )
goto LABEL_4;
}
else
{
v37[0] = *a1;
v6 = *(unsigned __int8 *)a2;
v7 = a1 + 1;
if ( (v6 & 0x80u) == 0 )
{
LABEL_4:
v36 = v6;
v8 = 1LL;
goto LABEL_5;
}
}
v25 = mbtowi((wchar_t *)&v36, a2, v4);
if ( v25 <= 0 )
{
if ( v25 )
return 0;
v6 = v36;
v8 = 1LL;
}
else
{
v8 = v25;
v6 = v36;
}
LABEL_5:
v9 = &a2[v8];
if ( v6 == 63 )
{
if ( !v37[0] )
return 0;
return (unsigned int)gmatch(v7, v9);
}
if ( v6 <= 0x3F )
{
if ( !v6 )
return v37[0] == 0;
if ( v6 == 42 )
{
if ( *v9 )
{
if ( !*a1 )
return 0;
while ( !(unsigned int)gmatch(v3, v9) )
{
if ( v4 > 1 && *v3 < 0 )
{
LODWORD(v22) = mbtowi(v37, v3, v4);
v33 = (_DWORD)v22 == 0;
if ( (int)v22 <= 0 )
{
v22 = 1LL;
if ( !v33 )
v37[0] = -1;
}
else
{
v22 = (int)v22;
}
}
else
{
v37[0] = *(unsigned __int8 *)v3;
v22 = 1LL;
}
v3 += v22;
if ( !*v3 )
return 0;
}
}
return 1;
}
goto LABEL_71;
}
if ( v6 == 92 )
{
v6 = *(unsigned __int8 *)v9;
if ( v4 > 1 && (v6 & 0x80u) != 0 )
{
v32 = mbtowi((wchar_t *)&v36, v9, v4);
if ( v32 <= 0 )
{
if ( v32 )
{
++v9;
v36 = -1;
if ( v37[0] != -1 )
return 0;
return (unsigned int)gmatch(v7, v9);
}
v6 = v36;
v29 = 1LL;
}
else
{
v29 = v32;
v6 = v36;
}
}
else
{
v36 = *(unsigned __int8 *)v9;
v29 = 1LL;
}
if ( !v6 )
return 0;
v9 += v29;
LABEL_71:
if ( v37[0] != v6 )
return 0;
return (unsigned int)gmatch(v7, v9);
}
v10 = 0;
if ( v6 == -1 )
return v10;
if ( v6 != 91 )
goto LABEL_71;
v12 = *(unsigned __int8 *)v9;
v35 = 0;
if ( (_BYTE)v12 == 33 )
{
v12 = *((unsigned __int8 *)v9 + 1);
v35 = 1;
++v9;
}
if ( v4 <= 1 || (v12 & 0x80u) == 0 )
{
v36 = v12;
v13 = 1LL;
goto LABEL_16;
}
v31 = mbtowi((wchar_t *)&v36, v9, v4);
if ( v31 > 0 )
{
v13 = v31;
v12 = v36;
goto LABEL_16;
}
if ( !v31 )
{
v12 = v36;
v13 = 1LL;
LABEL_16:
v14 = (unsigned __int8 *)&v9[v13];
if ( !v12 )
return 0;
goto LABEL_17;
}
v36 = -1;
v14 = (unsigned __int8 *)(v9 + 1);
v12 = -1;
LABEL_17:
v15 = (char *)v14;
v16 = -1LL;
v17 = 0;
while ( 1 )
{
if ( v12 == 93 )
{
if ( v14 >= (unsigned __int8 *)v15 )
{
v16 = v36;
v18 = *v15;
v19 = v36;
goto LABEL_26;
}
if ( v35 == v17 )
return 0;
return (unsigned int)gmatch(v7, v15);
}
v18 = *v15;
if ( v14 >= (unsigned __int8 *)v15 || v12 != 45 )
break;
if ( v18 != 93 )
{
if ( v18 == 92 )
v18 = *++v15;
if ( v5 <= 1 || v18 >= 0 )
{
v26 = (unsigned __int8)v18;
v27 = 1LL;
v36 = v26;
goto LABEL_60;
}
v34 = mbtowi((wchar_t *)&v36, v15, v5);
if ( v34 > 0 )
{
v27 = v34;
v26 = v36;
goto LABEL_60;
}
if ( v34 )
{
v18 = v15[1];
v36 = -1;
++v15;
if ( v16 > (unsigned int)v37[0] )
goto LABEL_29;
}
else
{
v26 = v36;
v27 = 1LL;
LABEL_60:
v15 += v27;
if ( !v26 )
return 0;
v18 = *v15;
if ( (unsigned int)v37[0] < v16 || v37[0] > v26 )
goto LABEL_28;
}
v17 = 1;
goto LABEL_28;
}
while ( 1 )
{
LABEL_53:
v16 = v36;
v19 = v36;
LABEL_26:
if ( v37[0] == v19 )
v17 = 1;
LABEL_28:
if ( v5 <= 1 )
{
LABEL_30:
v12 = (unsigned __int8)v18;
v21 = 1LL;
v36 = v12;
goto LABEL_31;
}
LABEL_29:
if ( v18 >= 0 )
goto LABEL_30;
v28 = mbtowi((wchar_t *)&v36, v15, v5);
if ( v28 > 0 )
{
v21 = v28;
v12 = v36;
goto LABEL_31;
}
if ( !v28 )
break;
v36 = -1;
v18 = *++v15;
}
v12 = v36;
v21 = 1LL;
LABEL_31:
v15 += v21;
if ( !v12 )
return 0;
}
if ( v12 != 92 )
goto LABEL_53;
if ( v5 > 1 && v18 < 0 )
{
v30 = mbtowi((wchar_t *)&v36, v15, v5);
if ( v30 <= 0 )
{
if ( v30 )
{
v18 = v15[1];
v36 = -1;
++v15;
v16 = 0xFFFFFFFFLL;
if ( v37[0] == -1 )
{
v17 = 1;
goto LABEL_29;
}
goto LABEL_28;
}
v19 = v36;
v20 = 1LL;
}
else
{
v20 = v30;
v19 = v36;
}
}
else
{
v19 = (unsigned __int8)v18;
v20 = 1LL;
v36 = v19;
}
v15 += v20;
if ( v19 )
{
v18 = *v15;
v16 = v19;
goto LABEL_26;
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
const char *v3; // rbx
int v4; // ebp
int v5; // er14
unsigned int v6; // eax
const char *v7; // r12
__int64 v8; // rsi
const char *v9; // r15
unsigned int v10; // er8
int v12; // eax
__int64 v13; // rdx
unsigned __int8 *v14; // r15
char *v15; // rbp
unsigned __int64 v16; // rbx
int v17; // er13
char v18; // dl
unsigned int v19; // eax
__int64 v20; // rdx
__int64 v21; // rdx
__int64 v22; // rax
int v23; // eax
__int64 v24; // r12
int v25; // eax
unsigned int v26; // eax
__int64 v27; // rdx
int v28; // eax
__int64 v29; // rdx
int v30; // eax
int v31; // eax
int v32; // eax
bool v33; // zf
int v34; // eax
int v35; // [rsp+Ch] [rbp-4Ch]
unsigned int v36; // [rsp+18h] [rbp-40h] BYREF
wchar_t v37[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v3 = a1;
v4 = _ctype_get_mb_cur_max();
v5 = v4;
if ( v4 <= 1 )
{
v7 = a1 + 1;
v37[0] = *(unsigned __int8 *)a1;
v6 = *(unsigned __int8 *)a2;
goto LABEL_4;
}
if ( *a1 < 0 )
{
v23 = func0(v37, a1, v4);
v24 = v23;
if ( v23 <= 0 )
{
if ( v23 )
return 0;
v24 = 1LL;
}
v7 = &a1[v24];
if ( v37[0] == -1 )
return 0;
v6 = *(unsigned __int8 *)a2;
if ( (v6 & 0x80u) == 0 )
goto LABEL_4;
}
else
{
v37[0] = *a1;
v6 = *(unsigned __int8 *)a2;
v7 = a1 + 1;
if ( (v6 & 0x80u) == 0 )
{
LABEL_4:
v36 = v6;
v8 = 1LL;
goto LABEL_5;
}
}
v25 = func0((wchar_t *)&v36, a2, v4);
if ( v25 <= 0 )
{
if ( v25 )
return 0;
v6 = v36;
v8 = 1LL;
}
else
{
v8 = v25;
v6 = v36;
}
LABEL_5:
v9 = &a2[v8];
if ( v6 == 63 )
{
if ( !v37[0] )
return 0;
return (unsigned int)func1(v7, v9);
}
if ( v6 <= 0x3F )
{
if ( !v6 )
return v37[0] == 0;
if ( v6 == 42 )
{
if ( *v9 )
{
if ( !*a1 )
return 0;
while ( !(unsigned int)func1(v3, v9) )
{
if ( v4 > 1 && *v3 < 0 )
{
LODWORD(v22) = func0(v37, v3, v4);
v33 = (_DWORD)v22 == 0;
if ( (int)v22 <= 0 )
{
v22 = 1LL;
if ( !v33 )
v37[0] = -1;
}
else
{
v22 = (int)v22;
}
}
else
{
v37[0] = *(unsigned __int8 *)v3;
v22 = 1LL;
}
v3 += v22;
if ( !*v3 )
return 0;
}
}
return 1;
}
goto LABEL_71;
}
if ( v6 == 92 )
{
v6 = *(unsigned __int8 *)v9;
if ( v4 > 1 && (v6 & 0x80u) != 0 )
{
v32 = func0((wchar_t *)&v36, v9, v4);
if ( v32 <= 0 )
{
if ( v32 )
{
++v9;
v36 = -1;
if ( v37[0] != -1 )
return 0;
return (unsigned int)func1(v7, v9);
}
v6 = v36;
v29 = 1LL;
}
else
{
v29 = v32;
v6 = v36;
}
}
else
{
v36 = *(unsigned __int8 *)v9;
v29 = 1LL;
}
if ( !v6 )
return 0;
v9 += v29;
LABEL_71:
if ( v37[0] != v6 )
return 0;
return (unsigned int)func1(v7, v9);
}
v10 = 0;
if ( v6 == -1 )
return v10;
if ( v6 != 91 )
goto LABEL_71;
v12 = *(unsigned __int8 *)v9;
v35 = 0;
if ( (_BYTE)v12 == 33 )
{
v12 = *((unsigned __int8 *)v9 + 1);
v35 = 1;
++v9;
}
if ( v4 <= 1 || (v12 & 0x80u) == 0 )
{
v36 = v12;
v13 = 1LL;
goto LABEL_16;
}
v31 = func0((wchar_t *)&v36, v9, v4);
if ( v31 > 0 )
{
v13 = v31;
v12 = v36;
goto LABEL_16;
}
if ( !v31 )
{
v12 = v36;
v13 = 1LL;
LABEL_16:
v14 = (unsigned __int8 *)&v9[v13];
if ( !v12 )
return 0;
goto LABEL_17;
}
v36 = -1;
v14 = (unsigned __int8 *)(v9 + 1);
v12 = -1;
LABEL_17:
v15 = (char *)v14;
v16 = -1LL;
v17 = 0;
while ( 1 )
{
if ( v12 == 93 )
{
if ( v14 >= (unsigned __int8 *)v15 )
{
v16 = v36;
v18 = *v15;
v19 = v36;
goto LABEL_26;
}
if ( v35 == v17 )
return 0;
return (unsigned int)func1(v7, v15);
}
v18 = *v15;
if ( v14 >= (unsigned __int8 *)v15 || v12 != 45 )
break;
if ( v18 != 93 )
{
if ( v18 == 92 )
v18 = *++v15;
if ( v5 <= 1 || v18 >= 0 )
{
v26 = (unsigned __int8)v18;
v27 = 1LL;
v36 = v26;
goto LABEL_60;
}
v34 = func0((wchar_t *)&v36, v15, v5);
if ( v34 > 0 )
{
v27 = v34;
v26 = v36;
goto LABEL_60;
}
if ( v34 )
{
v18 = v15[1];
v36 = -1;
++v15;
if ( v16 > (unsigned int)v37[0] )
goto LABEL_29;
}
else
{
v26 = v36;
v27 = 1LL;
LABEL_60:
v15 += v27;
if ( !v26 )
return 0;
v18 = *v15;
if ( (unsigned int)v37[0] < v16 || v37[0] > v26 )
goto LABEL_28;
}
v17 = 1;
goto LABEL_28;
}
while ( 1 )
{
LABEL_53:
v16 = v36;
v19 = v36;
LABEL_26:
if ( v37[0] == v19 )
v17 = 1;
LABEL_28:
if ( v5 <= 1 )
{
LABEL_30:
v12 = (unsigned __int8)v18;
v21 = 1LL;
v36 = v12;
goto LABEL_31;
}
LABEL_29:
if ( v18 >= 0 )
goto LABEL_30;
v28 = func0((wchar_t *)&v36, v15, v5);
if ( v28 > 0 )
{
v21 = v28;
v12 = v36;
goto LABEL_31;
}
if ( !v28 )
break;
v36 = -1;
v18 = *++v15;
}
v12 = v36;
v21 = 1LL;
LABEL_31:
v15 += v21;
if ( !v12 )
return 0;
}
if ( v12 != 92 )
goto LABEL_53;
if ( v5 > 1 && v18 < 0 )
{
v30 = func0((wchar_t *)&v36, v15, v5);
if ( v30 <= 0 )
{
if ( v30 )
{
v18 = v15[1];
v36 = -1;
++v15;
v16 = 0xFFFFFFFFLL;
if ( v37[0] == -1 )
{
v17 = 1;
goto LABEL_29;
}
goto LABEL_28;
}
v19 = v36;
v20 = 1LL;
}
else
{
v20 = v30;
v19 = v36;
}
}
else
{
v19 = (unsigned __int8)v18;
v20 = 1LL;
v36 = v19;
}
v15 += v20;
if ( v19 )
{
v18 = *v15;
v16 = v19;
goto LABEL_26;
}
return 0;
}

variable_types:
p
char *

variable_types:
bs
char *

variable_types:
c
unsigned int

variable_types:
scc
unsigned int

variable_types: n int

variable_types: ok int

variable_types: excl int

variable_types:
lc
unsigned long

variable_types:
bp
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
void *v4; // rsp
pthread_t *v5; // r14
__int64 v6; // r12
__int64 v7; // rax
pthread_t *v8; // rdi
__int64 v9; // rbx
pthread_t v10; // rdi
_QWORD v12[4]; // [rsp+0h] [rbp-20h] BYREF

if ( argc <= 1 || atoi(argv[1]) == 1 )
numberPhilosophers = 5;
else
numberPhilosophers = atoi(argv[1]);
v3 = 0LL;
puts("Initiating Dining Philosophers");
bodyCount = numberPhilosophers;
v4 = alloca(8LL * numberPhilosophers);
v5 = v12;
if ( numberPhilosophers > 0 )
{
do
{
v6 = v3;
philosopherLocation[v3] = v3;
v7 = 5 * v3++;
pthread_mutex_init((pthread_mutex_t *)((char *)&chopsticks + 8 * v7), 0LL);
v8 = v5++;
pthread_create(v8, 0LL, (void *(*)(void *))&transitions, &philosopherLocation[v6]);
}
while ( numberPhilosophers > (int)v3 );
if ( numberPhilosophers > 0 )
{
v9 = 0LL;
do
{
v10 = v12[v9++];
pthread_join(v10, 0LL);
}
while ( numberPhilosophers > (int)v9 );
}
}
return 0;
}

original_code:
int main(int argc, char** argv) {
   if(argc < 2){
      numberPhilosophers = 5;
   }
   else if(atoi(argv[1]) == 1){
      numberPhilosophers = 5;
   }
   else{
      numberPhilosophers = atoi(argv[1]);
   }
   printf("Initiating Dining Philosophers\n");
   int i;
   bodyCount = numberPhilosophers;
   pthread_t philosophers[numberPhilosophers];
   for(i = 0 ; i < numberPhilosophers ; i++){
      philosopherLocation[i] = i;
      pthread_mutex_init(&chopsticks[i], ((void *)0));
      pthread_create(&philosophers[i], ((void *)0),transitions,&philosopherLocation[i]);
   }
   for (i = 0; i < numberPhilosophers; i++) {
      pthread_join(philosophers[i], ((void *)0));
   }
   return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
   if(argc < 2){
      numberPhilosophers = 5;
   }
   else if(atoi(argv[1]) == 1){
      numberPhilosophers = 5;
   }
   else{
      numberPhilosophers = atoi(argv[1]);
   }
   printf("Initiating Dining Philosophers\n");
   int i;
   bodyCount = numberPhilosophers;
   unsigned long philosophers[-1];
   for(i = 0 ; i < numberPhilosophers ; i++){
      philosopherLocation[i] = i;
      pthread_mutex_init(&chopsticks[i], ((void *)0));
      pthread_create(&philosophers[i], ((void *)0),transitions,&philosopherLocation[i]);
   }
   for (i = 0; i < numberPhilosophers; i++) {
      pthread_join(philosophers[i], ((void *)0));
   }
   return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types:
philosophers
unsigned long [-1]


ponder

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

randomwait((unsigned int)bodyCount);
v1 = rand();
result = (unsigned int)(50 * (v1 / 50));
if ( v1 % 50 > 20 )
philosopherStatus[a1] = 1;
return result;
}

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

randomwait((unsigned int)bodyCount);
v1 = rand();
result = (unsigned int)(50 * (v1 / 50));
if ( v1 % 50 > 20 )
philosopherStatus[a1] = 1;
return result;
}

original_code:
void ponder (int philosopher) {
   randomwait(bodyCount);
   if( rand() % 50 > 20){
      philosopherStatus[philosopher] = 1;
   }
}

canonical_original_code:
void ponder(int philosopher) {
   randomwait(bodyCount);
   if( rand() % 50 > 20){
      philosopherStatus[philosopher] = 1;
   }
}

variable_types: philosopher int

return_type: void


startEating

decompiled_code:
__int64 __fastcall startEating(unsigned int a1)
{
__int64 v1; // rbx
__int64 v3; // rdi

v1 = (int)a1;
if ( rand() % 50 <= 20 )
return randomwait((unsigned int)bodyCount);
chopstickGrab(a1);
chopstickGrab((unsigned int)((int)(a1 + 1) % bodyCount));
v3 = (unsigned int)bodyCount;
philosopherStatus[v1] = 2;
return randomwait(v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
__int64 v1; // rbx
__int64 v3; // rdi

v1 = (int)a1;
if ( rand() % 50 <= 20 )
return randomwait((unsigned int)bodyCount);
chopstickGrab(a1);
chopstickGrab((unsigned int)((int)(a1 + 1) % bodyCount));
v3 = (unsigned int)bodyCount;
philosopherStatus[v1] = 2;
return randomwait(v3);
}

original_code:
void startEating (int philosopher) {
   if( rand() % 50 > 20){
      chopstickGrab(philosopher);
      chopstickGrab((philosopher + 1) % bodyCount);
      philosopherStatus[philosopher] = 2;
   }
   randomwait(bodyCount);
}

canonical_original_code:
void startEating(int philosopher) {
   if( rand() % 50 > 20){
      chopstickGrab(philosopher);
      chopstickGrab((philosopher + 1) % bodyCount);
      philosopherStatus[philosopher] = 2;
   }
   randomwait(bodyCount);
}


finishEating

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

v1 = rand();
result = (unsigned int)(50 * (v1 / 50));
if ( v1 % 50 > 20 )
{
chopstickRelease(a1);
result = chopstickRelease((unsigned int)((int)(a1 + 1) % bodyCount));
philosopherStatus[a1] = 0;
}
return result;
}

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

v1 = rand();
result = (unsigned int)(50 * (v1 / 50));
if ( v1 % 50 > 20 )
{
chopstickRelease(a1);
result = chopstickRelease((unsigned int)((int)(a1 + 1) % bodyCount));
philosopherStatus[a1] = 0;
}
return result;
}

original_code:
void finishEating (int philosopher) {
   if( rand() % 50 > 20){
      chopstickRelease(philosopher);
      chopstickRelease((philosopher + 1) % bodyCount);
      philosopherStatus[philosopher] = 0;
   }
}

canonical_original_code:
void finishEating(int philosopher) {
   if( rand() % 50 > 20){
      chopstickRelease(philosopher);
      chopstickRelease((philosopher + 1) % bodyCount);
      philosopherStatus[philosopher] = 0;
   }
}


transitions

decompiled_code:
void __fastcall __noreturn transitions(unsigned int *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int v3; // eax

v1 = (int)*a1;
v2 = *a1;
printPhilosophers();
while ( 1 )
{
printPhilosophers();
v3 = philosopherStatus[v1];
if ( v3 )
{
if ( v3 == 1 )
{
startEating(v2);
}
else if ( v3 == 2 )
{
finishEating(v2);
}
}
else
{
ponder(v2);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(unsigned int *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int v3; // eax

v1 = (int)*a1;
v2 = *a1;
printPhilosophers();
while ( 1 )
{
printPhilosophers();
v3 = philosopherStatus[v1];
if ( v3 )
{
if ( v3 == 1 )
{
func1(v2);
}
else if ( v3 == 2 )
{
func2(v2);
}
}
else
{
func0(v2);
}
}
}

original_code:
void* transitions(void* philosopher){
   int philosopherId = *(int*)philosopher;
   printPhilosophers();
   while(1){
      printPhilosophers();
      if(philosopherStatus[philosopherId] == 0){
         ponder(philosopherId);
      }
      else if(philosopherStatus[philosopherId] == 1){
         startEating(philosopherId);
      }
      else if(philosopherStatus[philosopherId] == 2){
         finishEating(philosopherId);
      }
   }
}

canonical_original_code:
void *transitions(void *philosopher){
   int philosopherId = *(int*)philosopher;
   printPhilosophers();
   while(1){
      printPhilosophers();
      if(philosopherStatus[philosopherId] == 0){
         ponder(philosopherId);
      }
      else if(philosopherStatus[philosopherId] == 1){
         startEating(philosopherId);
      }
      else if(philosopherStatus[philosopherId] == 2){
         finishEating(philosopherId);
      }
   }
}

variable_types:
philosopher
void *

variable_types: philosopherId int

return_type: void *


randomwait

decompiled_code:
__int64 __fastcall randomwait(int a1)
{
unsigned int v1; // er12

v1 = rand() % a1;
sleep(v1);
return v1;
}

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

v1 = rand() % a1;
sleep(v1);
return v1;
}

original_code:
int randomwait(int bound) {
    int wait = rand() % bound;
    sleep(wait);
    return wait;
}

variable_types: bound int

variable_types: wait int


printPhilosophers

decompiled_code:
int printPhilosophers()
{
__int64 v0; // rbx
int v1; // eax

if ( bodyCount > 0 )
{
v0 = 0LL;
do
{
while ( 1 )
{
v1 = philosopherStatus[v0];
if ( v1 )
break;
++v0;
printf(" ? ");
if ( bodyCount <= (int)v0 )
return putchar(10);
}
if ( v1 == 1 )
{
printf(" ! ");
}
else if ( v1 == 2 )
{
printf(" \\/ ");
}
++v0;
}
while ( bodyCount > (int)v0 );
}
return putchar(10);
}

canonical_decompiled_code:
int func1()
{
__int64 v0; // rbx
int v1; // eax

if ( bodyCount > 0 )
{
v0 = 0LL;
do
{
while ( 1 )
{
v1 = philosopherStatus[v0];
if ( v1 )
break;
++v0;
printf(" ? ");
if ( bodyCount <= (int)v0 )
return putchar(10);
}
if ( v1 == 1 )
{
printf(" ! ");
}
else if ( v1 == 2 )
{
printf(" \\/ ");
}
++v0;
}
while ( bodyCount > (int)v0 );
}
return putchar(10);
}

original_code:
void printPhilosophers () {
   int i;
   for (i = 0; i < bodyCount; i++) {
      if (philosopherStatus[i] == 0) {
         printf(" ? ");
      } else if (philosopherStatus[i] == 1) {
         printf(" ! ");
      } else if (philosopherStatus[i] == 2) {
         printf(" \\/ ");
      }
   }
   printf("\n");
}

canonical_original_code:
void printPhilosophers() {
   int i;
   for (i = 0; i < bodyCount; i++) {
      if (philosopherStatus[i] == 0) {
         printf(" ? ");
      } else if (philosopherStatus[i] == 1) {
         printf(" ! ");
      } else if (philosopherStatus[i] == 2) {
         printf(" \\/ ");
      }
   }
   printf("\n");
}


chopstickGrab

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

result = pthread_mutex_lock(&chopsticks[a1]);
++chopstickStatus[a1];
return result;
}

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

result = pthread_mutex_lock(&chopsticks[a1]);
++chopstickStatus[a1];
return result;
}

original_code:
void chopstickGrab (int chopstick){
   pthread_mutex_lock(&chopsticks[chopstick]);
   chopstickStatus[chopstick] += 1;
}

canonical_original_code:
void chopstickGrab(int chopstick){
   pthread_mutex_lock(&chopsticks[chopstick]);
   chopstickStatus[chopstick] += 1;
}

variable_types: chopstick int


chopstickRelease

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

result = pthread_mutex_unlock(&chopsticks[a1]);
--chopstickStatus[a1];
return result;
}

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

result = pthread_mutex_unlock(&chopsticks[a1]);
--chopstickStatus[a1];
return result;
}

original_code:
void chopstickRelease(int chopstick){
   pthread_mutex_unlock(&chopsticks[chopstick]);
   chopstickStatus[chopstick] -= 1;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
test_file_content_read_as_string(argc, argv, envp);
test_str_rstrip();
puts("\n\nAll test are OK\n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
func8();
puts("\n\nAll test are OK\n");
return 0;
}

original_code:
int main(int argc, char **argv)
{
   test_file_content_read_as_string();
   test_str_rstrip();
   printf("\n\nAll test are OK\n\n");
   return 0;
}


decompiled_code:
__int64 test_file_content_read_as_string()
{
__int64 result; // rax
int v1; // [rsp+0h] [rbp-88h] BYREF
__int16 v2; // [rsp+4h] [rbp-84h]

if ( (unsigned int)file_content_read_as_string("not-there", &v1, 128LL) )
__assert_fail(
"file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE",
"test_meep.c",
0xAu,
"test_file_content_read_as_string");
result = file_content_read_as_string("test_meep.data", &v1, 128LL);
if ( (_DWORD)result != 1 )
__assert_fail(
"file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE",
"test_meep.c",
0xCu,
"test_file_content_read_as_string");
if ( v1 != 875770417 || v2 != 53 )
__assert_fail("strcmp(buf, \"12345\") == 0", "test_meep.c", 0xDu, "test_file_content_read_as_string");
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
int v1; // [rsp+0h] [rbp-88h] BYREF
__int16 v2; // [rsp+4h] [rbp-84h]

if ( (unsigned int)func9("not-there", &v1, 128LL) )
__assert_fail(
"file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE",
"test_meep.c",
0xAu,
"test_file_content_read_as_string");
result = func9("test_meep.data", &v1, 128LL);
if ( (_DWORD)result != 1 )
__assert_fail(
"file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE",
"test_meep.c",
0xCu,
"test_file_content_read_as_string");
if ( v1 != 875770417 || v2 != 53 )
__assert_fail("strcmp(buf, \"12345\") == 0", "test_meep.c", 0xDu, "test_file_content_read_as_string");
return result;
}

original_code:
void test_file_content_read_as_string()
{
   char buf[128];
   ((void) sizeof ((file_content_read_as_string("not-there", buf, sizeof(buf)) == 0) ? 1 : 0), __extension__ ({ if (file_content_read_as_string("not-there", buf, sizeof(buf)) == 0) ; else __assert_fail ("file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE", "test_meep.c", 10, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof ((file_content_read_as_string("test_meep.data", buf, sizeof(buf)) == 1) ? 1 : 0), __extension__ ({ if (file_content_read_as_string("test_meep.data", buf, sizeof(buf)) == 1) ; else __assert_fail ("file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE", "test_meep.c", 12, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof ((strcmp(buf, "12345") == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, "12345") == 0) ; else __assert_fail ("strcmp(buf, \"12345\") == 0", "test_meep.c", 13, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types:
buf
char [128]


decompiled_code:
__int64 test_str_rstrip()
{
__int64 result; // rax
char v1; // [rsp+9h] [rbp-1Fh] BYREF
__int16 v2; // [rsp+Ah] [rbp-1Eh] BYREF
char v3; // [rsp+Ch] [rbp-1Ch]
char v4[9]; // [rsp+Dh] [rbp-1Bh] BYREF
char s[16]; // [rsp+18h] [rbp-10h] BYREF

strcpy(s, "meep\n \n");
str_rstrip(s);
if ( strlen(s) != 4 )
__assert_fail("strlen(buf1) == 4", "test_meep.c", 0x14u, "test_str_rstrip");
strcpy(v4, "mep\n");
str_rstrip(v4);
if ( strlen(v4) != 3 )
__assert_fail("strlen(buf2) == 3", "test_meep.c", 0x18u, "test_str_rstrip");
v1 = 0;
str_rstrip(&v1);
if ( v1 )
__assert_fail("strlen(buf3) == 0", "test_meep.c", 0x1Cu, "test_str_rstrip");
v3 = 0;
v2 = 256;
str_rstrip(&v2);
if ( v2 != 256 )
__assert_fail("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 0x20u, "test_str_rstrip");
strcpy(&v4[5], " \n \t");
result = str_rstrip(&v4[5]);
if ( v4[5] )
__assert_fail("strlen(buf5) == 0", "test_meep.c", 0x25u, "test_str_rstrip");
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
char v1; // [rsp+9h] [rbp-1Fh] BYREF
__int16 v2; // [rsp+Ah] [rbp-1Eh] BYREF
char v3; // [rsp+Ch] [rbp-1Ch]
char v4[9]; // [rsp+Dh] [rbp-1Bh] BYREF
char s[16]; // [rsp+18h] [rbp-10h] BYREF

strcpy(s, "meep\n \n");
func10(s);
if ( strlen(s) != 4 )
__assert_fail("strlen(buf1) == 4", "test_meep.c", 0x14u, "test_str_rstrip");
strcpy(v4, "mep\n");
func10(v4);
if ( strlen(v4) != 3 )
__assert_fail("strlen(buf2) == 3", "test_meep.c", 0x18u, "test_str_rstrip");
v1 = 0;
func10(&v1);
if ( v1 )
__assert_fail("strlen(buf3) == 0", "test_meep.c", 0x1Cu, "test_str_rstrip");
v3 = 0;
v2 = 256;
func10(&v2);
if ( v2 != 256 )
__assert_fail("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 0x20u, "test_str_rstrip");
strcpy(&v4[5], " \n \t");
result = func10(&v4[5]);
if ( v4[5] )
__assert_fail("strlen(buf5) == 0", "test_meep.c", 0x25u, "test_str_rstrip");
return result;
}

original_code:
void test_str_rstrip()
{
   char buf1[] = "meep\n \n";
   str_rstrip(buf1);
   ((void) sizeof ((strlen(buf1) == 4) ? 1 : 0), __extension__ ({ if (strlen(buf1) == 4) ; else __assert_fail ("strlen(buf1) == 4", "test_meep.c", 20, __extension__ __PRETTY_FUNCTION__); }));
   char buf2[] = "mep\n";
   str_rstrip(buf2);
   ((void) sizeof ((strlen(buf2) == 3) ? 1 : 0), __extension__ ({ if (strlen(buf2) == 3) ; else __assert_fail ("strlen(buf2) == 3", "test_meep.c", 24, __extension__ __PRETTY_FUNCTION__); }));
   char buf3[] = "";
   str_rstrip(buf3);
   ((void) sizeof ((strlen(buf3) == 0) ? 1 : 0), __extension__ ({ if (strlen(buf3) == 0) ; else __assert_fail ("strlen(buf3) == 0", "test_meep.c", 28, __extension__ __PRETTY_FUNCTION__); }));
   char buf4[] = "\0\1";
   str_rstrip(buf4);
   ((void) sizeof ((buf4[0] == '\0' && buf4[1] == '\1') ? 1 : 0), __extension__ ({ if (buf4[0] == '\0' && buf4[1] == '\1') ; else __assert_fail ("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 32, __extension__ __PRETTY_FUNCTION__); }));
   char buf5[] = "  \n \t";
   str_rstrip(buf5);
   ((void) sizeof ((strlen(buf5) == 0) ? 1 : 0), __extension__ ({ if (strlen(buf5) == 0) ; else __assert_fail ("strlen(buf5) == 0", "test_meep.c", 37, __extension__ __PRETTY_FUNCTION__); }));
}


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

result = (int)strlen(a1);
if ( (int)result >= 0 )
{
while ( (unsigned __int8)(a1[result] - 9) <= 1u || (a1[result] & 0xDF) == 0 )
{
v2 = --result;
if ( (_DWORD)result == -1 )
{
result = v2;
break;
}
}
}
a1[result + 1] = 0;
return result;
}

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

result = (int)strlen(a1);
if ( (int)result >= 0 )
{
while ( (unsigned __int8)(a1[result] - 9) <= 1u || (a1[result] & 0xDF) == 0 )
{
v2 = --result;
if ( (_DWORD)result == -1 )
{
result = v2;
break;
}
}
}
a1[result + 1] = 0;
return result;
}

original_code:
void str_rstrip(char *input)
{
   int i = strlen(input);
   while(i >= 0 && (input[i] == '\0' || input[i] == '\n' ||
                    input[i] == ' ' || input[i] == '\t')) {
      i--;
   }
   input[i+1] = 0;
}

variable_types:
input
char *


repo: mvystrcil/digger-mate-panel-monitor

decompiled_code:
__int64 __fastcall Generator__DumpData(const char *a1)
{
const char *v2; // [rsp+0h] [rbp-18h]
__int64 v3; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n\n", "generator.c", 57LL, "Generator__DumpData", a1, v2);
fprintf(stdout, "[%s:%d]:%s: Scripts: %p, %s\n\n", "generator.c", 58LL, "Generator__DumpData", a1, a1 + 256);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
const char *v2; // [rsp+0h] [rbp-18h]
__int64 v3; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n\n", "generator.c", 57LL, "Generator__DumpData", a1, v2);
fprintf(stdout, "[%s:%d]:%s: Scripts: %p, %s\n\n", "generator.c", 58LL, "Generator__DumpData", a1, a1 + 256);
return v3;
}

original_code:
void Generator__DumpData(GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s\n" "\n", "generator.c",57,__FUNCTION__, data, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Scripts: %p, %s\n" "\n", "generator.c",58,__FUNCTION__, data, data->path_to_scripts); } while(0);
}

canonical_original_code:
void Generator__DumpData(struct GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s\n" "\n", "generator.c",57,__FUNCTION__, data, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Scripts: %p, %s\n" "\n", "generator.c",58,__FUNCTION__, data, data->path_to_scripts); } while(0);
}

variable_types: struct GeneratorData { char xml_file[256]; char path_to_scripts[256]; int *stop; } *


decompiled_code:
int __fastcall Generator__RunFile(char *arg, __int64 a2)
{
int result; // eax
int v3; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = *(_DWORD *)(a2 + 24);
if ( (result & 0x40) != 0 )
{
result = *(_DWORD *)(a2 + 24) & 0xF000;
if ( result != 40960 )
{
fprintf(
stdout,
"[%s:%d]:%s: Execute script with arguments %s\n\n",
"generator.c",
117LL,
"Generator__RunFile",
arg);
v3 = fork();
if ( v3 )
{
if ( v3 < 0 )
result = fprintf(stdout, "[%s:%d]:%s: Cannont fork process\n\n", "generator.c", 123LL, "Generator__RunFile");
else
result = waitpid(v3, stat_loc, 0);
}
else
{
execl(arg, arg, generator_data, 0LL);
result = fprintf(stdout, "[%s:%d]:%s: Done\n\n", "generator.c", 121LL, "Generator__RunFile");
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *arg, __int64 a2)
{
int result; // eax
int v3; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = *(_DWORD *)(a2 + 24);
if ( (result & 0x40) != 0 )
{
result = *(_DWORD *)(a2 + 24) & 0xF000;
if ( result != 40960 )
{
fprintf(
stdout,
"[%s:%d]:%s: Execute script with arguments %s\n\n",
"generator.c",
117LL,
"Generator__RunFile",
arg);
v3 = fork();
if ( v3 )
{
if ( v3 < 0 )
result = fprintf(stdout, "[%s:%d]:%s: Cannont fork process\n\n", "generator.c", 123LL, "Generator__RunFile");
else
result = waitpid(v3, stat_loc, 0);
}
else
{
execl(arg, arg, generator_data, 0LL);
result = fprintf(stdout, "[%s:%d]:%s: Done\n\n", "generator.c", 121LL, "Generator__RunFile");
}
}
}
return result;
}

original_code:
void Generator__RunFile(char *file, struct stat *st){
 pid_t pid;
 int ret;
 if((st->st_mode) & 0100 && !((((st->st_mode)) & 0170000) == (0120000))){
  do { fprintf(stdout, "[%s:%d]:%s: " "Execute script with arguments %s\n" "\n", "generator.c",117,__FUNCTION__, file); } while(0);
  pid = fork();
  if(pid == 0){
   execl(file, file, generator_data.xml_file, (char*) ((void *)0));
   do { fprintf(stdout, "[%s:%d]:%s: " "Done\n" "\n", "generator.c",121,__FUNCTION__); } while(0);
  } else if (pid < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannont fork process\n" "\n", "generator.c",123,__FUNCTION__); } while(0);
  } else {
   waitpid(pid, &ret, 0);
  }
 }
}

canonical_original_code:
void Generator__RunFile(char *file, struct stat *st){
 int pid;
 int ret;
 if((st->st_mode) & 0100 && !((((st->st_mode)) & 0170000) == (0120000))){
  do { fprintf(stdout, "[%s:%d]:%s: " "Execute script with arguments %s\n" "\n", "generator.c",117,__FUNCTION__, file); } while(0);
  pid = fork();
  if(pid == 0){
   execl(file, file, generator_data.xml_file, (char *) ((void *)0));
   do { fprintf(stdout, "[%s:%d]:%s: " "Done\n" "\n", "generator.c",121,__FUNCTION__); } while(0);
  } else if (pid < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannont fork process\n" "\n", "generator.c",123,__FUNCTION__); } while(0);
  } else {
   waitpid(pid, &ret, 0);
  }
 }
}

variable_types:
file
char *

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

variable_types: pid int

variable_types: ret int

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


decompiled_code:
void __fastcall __noreturn Generator__Init(const void *a1)
{
fprintf(stdout, "[%s:%d]:%s: Initializing logs\n\n", "generator.c", 41LL, "Generator__Init");
qmemcpy(generator_data, a1, sizeof(generator_data));
Generator__DumpData(generator_data);
while ( 1 )
{
Generator__RunScripts((const char *)(unsigned int)((_DWORD)generator_data + 256));
fprintf(stdout, "[%s:%d]:%s: Wait - next cycle\n\n", "generator.c", 50LL, "Generator__Init");
sleep(0xAu);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const void *a1)
{
fprintf(stdout, "[%s:%d]:%s: Initializing logs\n\n", "generator.c", 41LL, "Generator__Init");
qmemcpy(generator_data, a1, sizeof(generator_data));
func0(generator_data);
while ( 1 )
{
func2((const char *)(unsigned int)((_DWORD)generator_data + 256));
fprintf(stdout, "[%s:%d]:%s: Wait - next cycle\n\n", "generator.c", 50LL, "Generator__Init");
sleep(0xAu);
}
}

original_code:
void* Generator__Init(void *gen_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "Initializing logs\n" "\n", "generator.c",41,__FUNCTION__); } while(0);
 generator_data = *((GeneratorData*) gen_data);
 const char *path = generator_data.path_to_scripts;
 Generator__DumpData(&generator_data);
 while(1){
  Generator__RunScripts(path);
  do { fprintf(stdout, "[%s:%d]:%s: " "Wait - next cycle\n" "\n", "generator.c",50,__FUNCTION__); } while(0);
  sleep(10);
 }
 return (void *)1;
}

canonical_original_code:
void *Generator__Init(void *gen_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "Initializing logs\n" "\n", "generator.c",41,__FUNCTION__); } while(0);
 generator_data = *((struct GeneratorData *) gen_data);
 char *path = generator_data.path_to_scripts;
 Generator__DumpData(&generator_data);
 while(1){
  Generator__RunScripts(path);
  do { fprintf(stdout, "[%s:%d]:%s: " "Wait - next cycle\n" "\n", "generator.c",50,__FUNCTION__); } while(0);
  sleep(10);
 }
 return (void *)1;
}

variable_types:
path
char *

user_defined_types: struct GeneratorData { char xml_file[256]; char path_to_scripts[256]; int *stop; }


decompiled_code:
int __fastcall Main__DumpGeneratorData(__int64 a1)
{
fprintf(stdout, "[%s:%d]:%s: ##### Generator data ##################\n", "main.c", 85LL, "Main__DumpGeneratorData");
fprintf(stdout, "[%s:%d]:%s: Generator struct addr %p\n", "main.c", 86LL, "Main__DumpGeneratorData", (const void *)a1);
fprintf(
stdout,
"[%s:%d]:%s: XML file: %p, %s\n",
"main.c",
87LL,
"Main__DumpGeneratorData",
(const void *)a1,
(const char *)a1);
fprintf(
stdout,
"[%s:%d]:%s: Path scr: %p, %s\n",
"main.c",
88LL,
"Main__DumpGeneratorData",
(const void *)(a1 + 256),
(const char *)(a1 + 256));
return fprintf(
stdout,
"[%s:%d]:%s: #######################################\n\n",
"main.c",
90LL,
"Main__DumpGeneratorData");
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
fprintf(stdout, "[%s:%d]:%s: ##### Generator data ##################\n", "main.c", 85LL, "Main__DumpGeneratorData");
fprintf(stdout, "[%s:%d]:%s: Generator struct addr %p\n", "main.c", 86LL, "Main__DumpGeneratorData", (const void *)a1);
fprintf(
stdout,
"[%s:%d]:%s: XML file: %p, %s\n",
"main.c",
87LL,
"Main__DumpGeneratorData",
(const void *)a1,
(const char *)a1);
fprintf(
stdout,
"[%s:%d]:%s: Path scr: %p, %s\n",
"main.c",
88LL,
"Main__DumpGeneratorData",
(const void *)(a1 + 256),
(const char *)(a1 + 256));
return fprintf(
stdout,
"[%s:%d]:%s: #######################################\n\n",
"main.c",
90LL,
"Main__DumpGeneratorData");
}

original_code:
void Main__DumpGeneratorData(GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Generator data ##################" "\n", "main.c",85,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Generator struct addr %p" "\n", "main.c",86,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s" "\n", "main.c",87,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Path scr: %p, %s" "\n", "main.c",88,__FUNCTION__, data->path_to_scripts, data->path_to_scripts); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#######################################\n" "\n", "main.c",90,__FUNCTION__); } while(0);
}

canonical_original_code:
void Main__DumpGeneratorData(struct GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Generator data ##################" "\n", "main.c",85,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Generator struct addr %p" "\n", "main.c",86,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s" "\n", "main.c",87,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Path scr: %p, %s" "\n", "main.c",88,__FUNCTION__, data->path_to_scripts, data->path_to_scripts); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#######################################\n" "\n", "main.c",90,__FUNCTION__); } while(0);
}


decompiled_code:
int __fastcall Main__DumpConnectionData(const char *a1)
{
__int64 v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: ##### Connection data ##################\n", "main.c", 94LL, "Main__DumpConnectionData");
fprintf(stdout, "[%s:%d]:%s: Connection struct addr %p\n", "main.c", 95LL, "Main__DumpConnectionData", a1);
fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n", "main.c", 96LL, "Main__DumpConnectionData", a1 + 4, a1 + 4);
LODWORD(v2) = *(_DWORD *)a1;
fprintf(stdout, "[%s:%d]:%s: Port : %p, %d\n", "main.c", 97LL, "Main__DumpConnectionData", a1, v2);
return fprintf(
stdout,
"[%s:%d]:%s: ########################################\n\n",
"main.c",
99LL,
"Main__DumpConnectionData");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
__int64 v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: ##### Connection data ##################\n", "main.c", 94LL, "Main__DumpConnectionData");
fprintf(stdout, "[%s:%d]:%s: Connection struct addr %p\n", "main.c", 95LL, "Main__DumpConnectionData", a1);
fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n", "main.c", 96LL, "Main__DumpConnectionData", a1 + 4, a1 + 4);
LODWORD(v2) = *(_DWORD *)a1;
fprintf(stdout, "[%s:%d]:%s: Port : %p, %d\n", "main.c", 97LL, "Main__DumpConnectionData", a1, v2);
return fprintf(
stdout,
"[%s:%d]:%s: ########################################\n\n",
"main.c",
99LL,
"Main__DumpConnectionData");
}

original_code:
void Main__DumpConnectionData(ConnectionData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Connection data ##################" "\n", "main.c",94,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Connection struct addr %p" "\n", "main.c",95,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file:  %p, %s" "\n", "main.c",96,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port    : %p, %d" "\n", "main.c",97,__FUNCTION__, &data->port, data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "########################################\n" "\n", "main.c",99,__FUNCTION__); } while(0);
}

canonical_original_code:
void Main__DumpConnectionData(struct ConnectionData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Connection data ##################" "\n", "main.c",94,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Connection struct addr %p" "\n", "main.c",95,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file:  %p, %s" "\n", "main.c",96,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port    : %p, %d" "\n", "main.c",97,__FUNCTION__, &data->port, data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "########################################\n" "\n", "main.c",99,__FUNCTION__); } while(0);
}

variable_types: struct ConnectionData { int port; char xml_file[256]; int *stop; } *


decompiled_code:
__int64 __fastcall Main__FillStructuresFromArgs(__int64 a1, __int64 a2, __int64 a3, const char **a4)
{
const char *v6; // rsi
__int64 v8; // [rsp-8h] [rbp-28h]

fprintf(
stdout,
"[%s:%d]:%s: Fill in structure data %s, %s\n\n",
"main.c",
106LL,
"Main__FillStructuresFromArgs",
a4[1],
a4[2]);
strcpy((char *)a2, a4[2]);
fprintf(stdout, "[%s:%d]:%s: Fill path to scripts\n\n", "main.c", 108LL, "Main__FillStructuresFromArgs");
strcpy((char *)(a2 + 256), a4[1]);
v6 = a4[2];
*(_QWORD *)(a2 + 512) = &stop;
strcpy((char *)(a1 + 4), v6);
*(_DWORD *)a1 = atoi(a4[3]);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, const char **a4)
{
const char *v6; // rsi
__int64 v8; // [rsp-8h] [rbp-28h]

fprintf(
stdout,
"[%s:%d]:%s: Fill in structure data %s, %s\n\n",
"main.c",
106LL,
"Main__FillStructuresFromArgs",
a4[1],
a4[2]);
strcpy((char *)a2, a4[2]);
fprintf(stdout, "[%s:%d]:%s: Fill path to scripts\n\n", "main.c", 108LL, "Main__FillStructuresFromArgs");
strcpy((char *)(a2 + 256), a4[1]);
v6 = a4[2];
*(_QWORD *)(a2 + 512) = &stop;
strcpy((char *)(a1 + 4), v6);
*(_DWORD *)a1 = atoi(a4[3]);
return v8;
}

original_code:
void Main__FillStructuresFromArgs(ConnectionData *connection,
                                  GeneratorData *generator,
                                  int argc,
                                  char *argv[]){
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill in structure data %s, %s\n" "\n", "main.c",106,__FUNCTION__, argv[1], argv[2]); } while(0);
 strcpy(generator->xml_file, argv[2]);
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill path to scripts\n" "\n", "main.c",108,__FUNCTION__); } while(0);
 strcpy(generator->path_to_scripts, argv[1]);
 generator->stop = &stop;
 strcpy(connection->xml_file, argv[2]);
 connection->port = atoi(argv[3]);
}

canonical_original_code:
void Main__FillStructuresFromArgs(struct ConnectionData *connection, struct GeneratorData *generator, int argc, char *argv[]){
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill in structure data %s, %s\n" "\n", "main.c",106,__FUNCTION__, argv[1], argv[2]); } while(0);
 strcpy(generator->xml_file, argv[2]);
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill path to scripts\n" "\n", "main.c",108,__FUNCTION__); } while(0);
 strcpy(generator->path_to_scripts, argv[1]);
 generator->stop = &stop;
 strcpy(connection->xml_file, argv[2]);
 connection->port = atoi(argv[3]);
}

variable_types: argc int

variable_types:
argv
char *[]

user_defined_types: struct ConnectionData { int port; char xml_file[256]; int *stop; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

stop = 0;
memset(connection_data, 0, sizeof(connection_data));
memset(&generator_data, 0, 0x208uLL);
if ( argc <= 1 )
{
fprintf(
stdout,
"[%s:%d]:%s: Usage: %s path to scripts path where to store xml port\n\n",
"main.c",
58LL,
"main",
*argv);
result = -1;
}
else
{
Main__FillStructuresFromArgs((__int64)connection_data, (__int64)&generator_data, (unsigned int)argc, argv);
Main__DumpConnectionData(connection_data);
Main__DumpGeneratorData((__int64)&generator_data);
Mutex__Init();
if ( pthread_create(newthread, 0LL, (void *(*)(void *))&Generator__Init, &generator_data) < 0 )
fprintf(stdout, "[%s:%d]:%s: Cannot create Generator Hypervisor thread\n\n", "main.c", 70LL, "main");
if ( pthread_create(&th, 0LL, (void *(*)(void *))&Connection__Connect, connection_data) < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot create Connection Hypervisor thread\n\n", "main.c", 74LL, "main");
result = -1;
}
else
{
pthread_join(th, 0LL);
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

stop = 0;
memset(connection_data, 0, sizeof(connection_data));
memset(&generator_data, 0, 0x208uLL);
if ( argc <= 1 )
{
fprintf(
stdout,
"[%s:%d]:%s: Usage: %s path to scripts path where to store xml port\n\n",
"main.c",
58LL,
"main",
*argv);
result = -1;
}
else
{
func2((__int64)connection_data, (__int64)&generator_data, (unsigned int)argc, argv);
func1(connection_data);
func0((__int64)&generator_data);
Mutex__Init();
if ( pthread_create(newthread, 0LL, (void *(*)(void *))&Generator__Init, &generator_data) < 0 )
fprintf(stdout, "[%s:%d]:%s: Cannot create Generator Hypervisor thread\n\n", "main.c", 70LL, "main");
if ( pthread_create(&th, 0LL, (void *(*)(void *))&Connection__Connect, connection_data) < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot create Connection Hypervisor thread\n\n", "main.c", 74LL, "main");
result = -1;
}
else
{
pthread_join(th, 0LL);
result = 0;
}
}
return result;
}

original_code:
int main(int argc, char* argv[]){
 pthread_t connection, generate;
 memset(&connection_data, 0, sizeof(connection_data));
 memset(&generator_data, 0, sizeof(generator_data));
 stop = 0;
 if(argc < 2){
  do { fprintf(stdout, "[%s:%d]:%s: " "Usage: %s path to scripts path where to store xml port\n" "\n", "main.c",58,__FUNCTION__, argv[0]); } while(0);
  return -1;
 } else {
  Main__FillStructuresFromArgs(&connection_data, &generator_data, argc, argv);
 }
 Main__DumpConnectionData(&connection_data);
 Main__DumpGeneratorData(&generator_data);
 Mutex__Init();
 if(pthread_create(&generate, ((void *)0), Generator__Init, (void *)&generator_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Generator Hypervisor thread\n" "\n", "main.c",70,__FUNCTION__); } while(0);
 }
 if(pthread_create(&connection, ((void *)0), Connection__Connect, (void *)&connection_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Connection Hypervisor thread\n" "\n", "main.c",74,__FUNCTION__); } while(0);
  return -1;
 }
 pthread_join(connection, ((void *)0));
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
 unsigned long connection; unsigned long generate;
 memset(&connection_data, 0, sizeof(connection_data));
 memset(&generator_data, 0, sizeof(generator_data));
 stop = 0;
 if(argc < 2){
  do { fprintf(stdout, "[%s:%d]:%s: " "Usage: %s path to scripts path where to store xml port\n" "\n", "main.c",58,__FUNCTION__, argv[0]); } while(0);
  return -1;
 } else {
  Main__FillStructuresFromArgs(&connection_data, &generator_data, argc, argv);
 }
 Main__DumpConnectionData(&connection_data);
 Main__DumpGeneratorData(&generator_data);
 Mutex__Init();
 if(pthread_create(&generate, ((void *)0), Generator__Init, (void *)&generator_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Generator Hypervisor thread\n" "\n", "main.c",70,__FUNCTION__); } while(0);
 }
 if(pthread_create(&connection, ((void *)0), Connection__Connect, (void *)&connection_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Connection Hypervisor thread\n" "\n", "main.c",74,__FUNCTION__); } while(0);
  return -1;
 }
 pthread_join(connection, ((void *)0));
 return 0;
}

variable_types:
connection
unsigned long

variable_types:
generate
unsigned long


decompiled_code:
__int64 Mutex__Init()
{
int v0; // er8
__int64 result; // rax

v0 = pthread_mutex_init(&xml_file_mutex, 0LL);
result = 1LL;
if ( v0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot initialize connection mutex\n\n", "mutex.c", 29LL, "Mutex__Init");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // er8
__int64 result; // rax

v0 = pthread_mutex_init(&xml_file_mutex, 0LL);
result = 1LL;
if ( v0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot initialize connection mutex\n\n", "mutex.c", 29LL, "Mutex__Init");
result = 0LL;
}
return result;
}

original_code:
int Mutex__Init(){
 if(pthread_mutex_init(&xml_file_mutex, ((void *)0)) != 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot initialize connection mutex\n" "\n", "mutex.c",29,__FUNCTION__); } while(0);
  return 0;
 }
 return 1;
}


decompiled_code:
__int64 Mutex__LockFileMutex()
{
pthread_mutex_lock(&xml_file_mutex);
return 1LL;
}

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

original_code:
int Mutex__LockFileMutex(){
 pthread_mutex_lock(&xml_file_mutex);
 return 1;
}


decompiled_code:
__int64 Mutex__UnlockFileMutex()
{
pthread_mutex_unlock(&xml_file_mutex);
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
pthread_mutex_unlock(&xml_file_mutex);
return 1LL;
}

original_code:
int Mutex__UnlockFileMutex(){
 pthread_mutex_unlock(&xml_file_mutex);
 return 1;
}


decompiled_code:
int Mutex__DestroyAll()
{
return pthread_mutex_destroy(&xml_file_mutex);
}

canonical_decompiled_code:
int func3()
{
return pthread_mutex_destroy(&xml_file_mutex);
}

original_code:
void Mutex__DestroyAll(){
 pthread_mutex_destroy(&xml_file_mutex);
}


decompiled_code:
int __fastcall Connection__HandleConnections(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
int v6; // [rsp+8h] [rbp-28h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-24h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+18h] [rbp-18h] BYREF

addr_len = 16;
fprintf(
stdout,
"[%s:%d]:%s: Dump data %p, %s\n\n",
"connection.c",
84LL,
"Connection__HandleConnections",
connection_data,
&connection_data[4]);
while ( 1 )
{
v4 = accept(a3, &addr, &addr_len);
v6 = v4;
if ( v4 < 0 )
break;
fprintf(
stdout,
"[%s:%d]:%s: Client accepted - handle it\n\n",
"connection.c",
92LL,
"Connection__HandleConnections");
if ( pthread_create(&newthread, 0LL, Connection__ConnectionHandler, &v6) < 0 )
fprintf(
stdout,
"[%s:%d]:%s: Cannot create thread for accepting new client\n\n",
"connection.c",
99LL,
"Connection__HandleConnections");
}
return fprintf(
stdout,
"[%s:%d]:%s: Cannot get socket fd for accept connection %d\n\n",
"connection.c",
89LL,
"Connection__HandleConnections",
(unsigned int)v4);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
int v6; // [rsp+8h] [rbp-28h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-24h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+18h] [rbp-18h] BYREF

addr_len = 16;
fprintf(
stdout,
"[%s:%d]:%s: Dump data %p, %s\n\n",
"connection.c",
84LL,
"Connection__HandleConnections",
connection_data,
&connection_data[4]);
while ( 1 )
{
v4 = accept(a3, &addr, &addr_len);
v6 = v4;
if ( v4 < 0 )
break;
fprintf(
stdout,
"[%s:%d]:%s: Client accepted - handle it\n\n",
"connection.c",
92LL,
"Connection__HandleConnections");
if ( pthread_create(&newthread, 0LL, Connection__ConnectionHandler, &v6) < 0 )
fprintf(
stdout,
"[%s:%d]:%s: Cannot create thread for accepting new client\n\n",
"connection.c",
99LL,
"Connection__HandleConnections");
}
return fprintf(
stdout,
"[%s:%d]:%s: Cannot get socket fd for accept connection %d\n\n",
"connection.c",
89LL,
"Connection__HandleConnections",
(unsigned int)v4);
}

original_code:
void Connection__HandleConnections(struct sockaddr_in server_data, int server_fd){
 int client_fd;
 int *new_sock;
 struct sockaddr_in client_data;
 socklen_t tmp = sizeof(server_data);
 do { fprintf(stdout, "[%s:%d]:%s: " "Dump data %p, %s\n" "\n", "connection.c",84,__FUNCTION__, &connection_data, connection_data.xml_file); } while(0);
 while(1){
  client_fd = accept(server_fd, (struct sockaddr *)&client_data, &tmp);
  if(client_fd < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd for accept connection %d\n" "\n", "connection.c",89,__FUNCTION__, client_fd); } while(0);
   return;
  } else {
   do { fprintf(stdout, "[%s:%d]:%s: " "Client accepted - handle it\n" "\n", "connection.c",92,__FUNCTION__); } while(0);
  }
  new_sock = malloc(sizeof(char));
  new_sock = &client_fd;
  pthread_t thread;
  if(pthread_create(&thread, ((void *)0), Connection__ConnectionHandler, (void *) new_sock) < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create thread for accepting new client\n" "\n", "connection.c",99,__FUNCTION__); } while(0);
  }
 }
}

canonical_original_code:
void Connection__HandleConnections(struct sockaddr_in server_data, int server_fd){
 int client_fd;
 int *new_sock;
 struct sockaddr_in client_data;
 unsigned int tmp = sizeof(server_data);
 do { fprintf(stdout, "[%s:%d]:%s: " "Dump data %p, %s\n" "\n", "connection.c",84,__FUNCTION__, &connection_data, connection_data.xml_file); } while(0);
 while(1){
  client_fd = accept(server_fd, (struct sockaddr *)&client_data, &tmp);
  if(client_fd < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd for accept connection %d\n" "\n", "connection.c",89,__FUNCTION__, client_fd); } while(0);
   return;
  } else {
   do { fprintf(stdout, "[%s:%d]:%s: " "Client accepted - handle it\n" "\n", "connection.c",92,__FUNCTION__); } while(0);
  }
  new_sock = malloc(sizeof(char));
  new_sock = &client_fd;
  unsigned long thread;
  if(pthread_create(&thread, ((void *)0), Connection__ConnectionHandler, (void *) new_sock) < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create thread for accepting new client\n" "\n", "connection.c",99,__FUNCTION__); } while(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:
tmp
unsigned int

variable_types:
thread
unsigned long

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

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


decompiled_code:
int __fastcall Connection__SendString(int fd, void *buf)
{
size_t v2; // rax
int v3; // ebx
size_t v4; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
v4 = strlen((const char *)buf);
if ( v3 != v4 )
LODWORD(v4) = fprintf(
stdout,
"[%s:%d]:%s: Cannot sent all data\n\n",
"connection.c",
136LL,
"Connection__SendString");
return v4;
}

canonical_decompiled_code:
int __fastcall func3(int fd, void *buf)
{
size_t v2; // rax
int v3; // ebx
size_t v4; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
v4 = strlen((const char *)buf);
if ( v3 != v4 )
LODWORD(v4) = fprintf(
stdout,
"[%s:%d]:%s: Cannot sent all data\n\n",
"connection.c",
136LL,
"Connection__SendString");
return v4;
}

original_code:
void Connection__SendString(int socket, char *string){
 int len;
 len = write(socket, string, strlen(string));
 if(len != strlen(string)){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot sent all data\n" "\n", "connection.c",136,__FUNCTION__); } while(0);
 }
}

variable_types: socket int

variable_types:
string
char *

variable_types: len int


decompiled_code:
__int64 __fastcall Connection__Connect(const void *a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int v5; // ebp
__int64 result; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v8; // [rsp+10h] [rbp-28h] BYREF

qmemcpy(connection_data, a1, sizeof(connection_data));
v1 = *(unsigned int *)connection_data;
fprintf(
stdout,
"[%s:%d]:%s: Port: %d\n\n",
"connection.c",
42LL,
"Connection__Connect",
*(unsigned int *)connection_data);
fprintf(
stdout,
"[%s:%d]:%s: PORT: %p, %d\n\n",
"connection.c",
43LL,
"Connection__Connect",
connection_data,
*(unsigned int *)connection_data);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot get file descriptor for Internet Socket\n\n",
"connection.c",
48LL,
"Connection__Connect");
result = 1LL;
}
else
{
v3 = v2;
fprintf(stdout, "[%s:%d]:%s: Got sockect fd: %d\n\n", "connection.c", 51LL, "Connection__Connect", (unsigned int)v2);
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
v8 = 0LL;
v8.sa_family = 2;
*(_WORD *)v8.sa_data = _bswap_16(v1);
v5 = bind(v3, &v8, v4);
listen(v3, 5);
if ( v5 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot bind to socket fd: %d, port: %d\n\n",
"connection.c",
67LL,
"Connection__Connect",
v3,
v1);
result = 1LL;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Socket binded to port: %d\n\n",
"connection.c",
70LL,
"Connection__Connect",
(unsigned int)v1);
Connection__HandleConnections(*(__int64 *)&v8.sa_family, *(__int64 *)&v8.sa_data[6], v3);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const void *a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int v5; // ebp
__int64 result; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v8; // [rsp+10h] [rbp-28h] BYREF

qmemcpy(connection_data, a1, sizeof(connection_data));
v1 = *(unsigned int *)connection_data;
fprintf(
stdout,
"[%s:%d]:%s: Port: %d\n\n",
"connection.c",
42LL,
"Connection__Connect",
*(unsigned int *)connection_data);
fprintf(
stdout,
"[%s:%d]:%s: PORT: %p, %d\n\n",
"connection.c",
43LL,
"Connection__Connect",
connection_data,
*(unsigned int *)connection_data);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot get file descriptor for Internet Socket\n\n",
"connection.c",
48LL,
"Connection__Connect");
result = 1LL;
}
else
{
v3 = v2;
fprintf(stdout, "[%s:%d]:%s: Got sockect fd: %d\n\n", "connection.c", 51LL, "Connection__Connect", (unsigned int)v2);
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
v8 = 0LL;
v8.sa_family = 2;
*(_WORD *)v8.sa_data = func0(v1);
v5 = bind(v3, &v8, v4);
listen(v3, 5);
if ( v5 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot bind to socket fd: %d, port: %d\n\n",
"connection.c",
67LL,
"Connection__Connect",
v3,
v1);
result = 1LL;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Socket binded to port: %d\n\n",
"connection.c",
70LL,
"Connection__Connect",
(unsigned int)v1);
func1(*(__int64 *)&v8.sa_family, *(__int64 *)&v8.sa_data[6], v3);
result = 0LL;
}
}
return result;
}

original_code:
void* Connection__Connect(void *conn_data){
 connection_data = *((ConnectionData *) conn_data);
 int port = connection_data.port;
 int server_fd, res;
 do { fprintf(stdout, "[%s:%d]:%s: " "Port: %d\n" "\n", "connection.c",42,__FUNCTION__, port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "PORT: %p, %d\n" "\n", "connection.c",43,__FUNCTION__, &connection_data.port, connection_data.port); } while(0);
 struct sockaddr_in server_data;
 server_fd = socket(2, SOCK_STREAM, 0);
 if(server_fd < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get file descriptor for Internet Socket\n" "\n", "connection.c",48,__FUNCTION__); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Got sockect fd: %d\n" "\n", "connection.c",51,__FUNCTION__, server_fd); } while(0);
 }
 int optval = 1;
 setsockopt(server_fd, 1, 2, &optval, sizeof(int));
 bzero(&server_data, sizeof(server_data));
 server_data.sin_family = 2;
 server_data.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 server_data.sin_port = htons(port);
 res = bind(server_fd, (struct sockaddr *) &server_data, sizeof(server_data));
 listen(server_fd, 5);
 if(res < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot bind to socket fd: %d, port: %d\n" "\n", "connection.c",67,__FUNCTION__, server_fd, port); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Socket binded to port: %d\n" "\n", "connection.c",70,__FUNCTION__, port); } while(0);
 }
 Connection__HandleConnections (server_data, server_fd);
 return (void *)0;
}

canonical_original_code:
void *Connection__Connect(void *conn_data){
 connection_data = *((struct ConnectionData *) conn_data);
 int port = connection_data.port;
 int server_fd; int res;
 do { fprintf(stdout, "[%s:%d]:%s: " "Port: %d\n" "\n", "connection.c",42,__FUNCTION__, port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "PORT: %p, %d\n" "\n", "connection.c",43,__FUNCTION__, &connection_data.port, connection_data.port); } while(0);
 struct sockaddr_in server_data;
 server_fd = socket(2, SOCK_STREAM, 0);
 if(server_fd < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get file descriptor for Internet Socket\n" "\n", "connection.c",48,__FUNCTION__); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Got sockect fd: %d\n" "\n", "connection.c",51,__FUNCTION__, server_fd); } while(0);
 }
 int optval = 1;
 setsockopt(server_fd, 1, 2, &optval, sizeof(int));
 bzero(&server_data, sizeof(server_data));
 server_data.sin_family = 2;
 server_data.sin_addr.s_addr = ((unsigned int) 0x00000000);
 server_data.sin_port = htons(port);
 res = bind(server_fd, (struct sockaddr *) &server_data, sizeof(server_data));
 listen(server_fd, 5);
 if(res < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot bind to socket fd: %d, port: %d\n" "\n", "connection.c",67,__FUNCTION__, server_fd, port); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Socket binded to port: %d\n" "\n", "connection.c",70,__FUNCTION__, port); } while(0);
 }
 Connection__HandleConnections (server_data, server_fd);
 return (void *)0;
}

variable_types: port int

variable_types: res int

variable_types: optval int


decompiled_code:
int __fastcall Connection__DumpData(const char *a1)
{
fprintf(stdout, "[%s:%d]:%s: ####### Connect to #######\n", "connection.c", 79LL, "Connection__DumpData");
fprintf(stdout, "[%s:%d]:%s: Port %d\n", "connection.c", 80LL, "Connection__DumpData", *(unsigned int *)a1);
fprintf(stdout, "[%s:%d]:%s: Addr: %s\n", "connection.c", 81LL, "Connection__DumpData", a1 + 12);
fprintf(stdout, "[%s:%d]:%s: Refresh: %d\n", "connection.c", 82LL, "Connection__DumpData", *((unsigned int *)a1 + 1));
fprintf(stdout, "[%s:%d]:%s: Retry: %d\n", "connection.c", 83LL, "Connection__DumpData", *((unsigned int *)a1 + 2));
fprintf(stdout, "[%s:%d]:%s: File: %s\n", "connection.c", 84LL, "Connection__DumpData", a1 + 268);
fprintf(stdout, "[%s:%d]:%s: Error: %d\n", "connection.c", 85LL, "Connection__DumpData", *((unsigned int *)a1 + 131));
return fprintf(stdout, "[%s:%d]:%s: #########################\n\n", "connection.c", 86LL, "Connection__DumpData");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
fprintf(stdout, "[%s:%d]:%s: ####### Connect to #######\n", "connection.c", 79LL, "Connection__DumpData");
fprintf(stdout, "[%s:%d]:%s: Port %d\n", "connection.c", 80LL, "Connection__DumpData", *(unsigned int *)a1);
fprintf(stdout, "[%s:%d]:%s: Addr: %s\n", "connection.c", 81LL, "Connection__DumpData", a1 + 12);
fprintf(stdout, "[%s:%d]:%s: Refresh: %d\n", "connection.c", 82LL, "Connection__DumpData", *((unsigned int *)a1 + 1));
fprintf(stdout, "[%s:%d]:%s: Retry: %d\n", "connection.c", 83LL, "Connection__DumpData", *((unsigned int *)a1 + 2));
fprintf(stdout, "[%s:%d]:%s: File: %s\n", "connection.c", 84LL, "Connection__DumpData", a1 + 268);
fprintf(stdout, "[%s:%d]:%s: Error: %d\n", "connection.c", 85LL, "Connection__DumpData", *((unsigned int *)a1 + 131));
return fprintf(stdout, "[%s:%d]:%s: #########################\n\n", "connection.c", 86LL, "Connection__DumpData");
}

original_code:
void Connection__DumpData(ConnectionData *connection_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "####### Connect to #######" "\n", "connection.c",79,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port %d" "\n", "connection.c",80,__FUNCTION__, connection_data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Addr: %s" "\n", "connection.c",81,__FUNCTION__, connection_data->address); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Refresh: %d" "\n", "connection.c",82,__FUNCTION__, connection_data->refresh); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Retry: %d" "\n", "connection.c",83,__FUNCTION__, connection_data->retry); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "File: %s" "\n", "connection.c",84,__FUNCTION__, connection_data->file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Error: %d" "\n", "connection.c",85,__FUNCTION__, connection_data->error); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#########################\n" "\n", "connection.c",86,__FUNCTION__); } while(0);
}

canonical_original_code:
void Connection__DumpData(struct ConnectionData *connection_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "####### Connect to #######" "\n", "connection.c",79,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port %d" "\n", "connection.c",80,__FUNCTION__, connection_data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Addr: %s" "\n", "connection.c",81,__FUNCTION__, connection_data->address); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Refresh: %d" "\n", "connection.c",82,__FUNCTION__, connection_data->refresh); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Retry: %d" "\n", "connection.c",83,__FUNCTION__, connection_data->retry); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "File: %s" "\n", "connection.c",84,__FUNCTION__, connection_data->file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Error: %d" "\n", "connection.c",85,__FUNCTION__, connection_data->error); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#########################\n" "\n", "connection.c",86,__FUNCTION__); } while(0);
}

variable_types: struct ConnectionData { int port; int refresh; int retry; char address[256]; char file[256]; int error; } *


decompiled_code:
__int64 __fastcall Connection__Connect(_DWORD *a1)
{
int v1; // ebx
int v2; // ebp
void *v3; // rdx
int *v4; // rbx
struct sockaddr v6; // [rsp+0h] [rbp-238h] BYREF
unsigned int v7[138]; // [rsp+10h] [rbp-228h] BYREF

v1 = 5;
qmemcpy(v7, a1, 0x210uLL);
Connection__DumpData((const char *)v7);
LABEL_2:
v6 = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot get socket fd\n\n", "connection.c", 49LL, "Connection__Connect");
}
else
{
v6.sa_family = 2;
*(_WORD *)v6.sa_data = _bswap_16(v7[0]);
if ( inet_pton(2, (const char *)&v7[3], v3) > 0 )
{
do
{
if ( connect(v2, &v6, 0x10u) >= 0 )
{
Connection__SaveServerData(v2, (char *)&v7[67]);
sleep(v7[1]);
goto LABEL_2;
}
--v1;
sleep(v7[2]);
}
while ( v1 > 0 );
v4 = _errno_location();
fprintf(
stdout,
"[%s:%d]:%s: Cannot connect to server: %s, errno %d\n\n",
"connection.c",
65LL,
"Connection__Connect",
(const char *)&v7[3],
(unsigned int)*v4);
a1[131] = *v4;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot set server address: %s\n\n",
"connection.c",
57LL,
"Connection__Connect",
(const char *)&v7[3]);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1)
{
int v1; // ebx
int v2; // ebp
void *v3; // rdx
int *v4; // rbx
struct sockaddr v6; // [rsp+0h] [rbp-238h] BYREF
unsigned int v7[138]; // [rsp+10h] [rbp-228h] BYREF

v1 = 5;
qmemcpy(v7, a1, 0x210uLL);
func1((const char *)v7);
LABEL_2:
v6 = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot get socket fd\n\n", "connection.c", 49LL, "Connection__Connect");
}
else
{
v6.sa_family = 2;
*(_WORD *)v6.sa_data = func0(v7[0]);
if ( inet_pton(2, (const char *)&v7[3], v3) > 0 )
{
do
{
if ( connect(v2, &v6, 0x10u) >= 0 )
{
func2(v2, (char *)&v7[67]);
sleep(v7[1]);
goto LABEL_2;
}
--v1;
sleep(v7[2]);
}
while ( v1 > 0 );
v4 = _errno_location();
fprintf(
stdout,
"[%s:%d]:%s: Cannot connect to server: %s, errno %d\n\n",
"connection.c",
65LL,
"Connection__Connect",
(const char *)&v7[3],
(unsigned int)*v4);
a1[131] = *v4;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot set server address: %s\n\n",
"connection.c",
57LL,
"Connection__Connect",
(const char *)&v7[3]);
}
}
return 0LL;
}

original_code:
void *Connection__Connect(void *conn_data){
 int sockfd = 0;
 int attempts = 5;
 ConnectionData connection_data = *((ConnectionData *)conn_data);
 struct sockaddr_in serv_addr;
 Connection__DumpData(&connection_data);
 while(1){
  memset(&serv_addr, 0, sizeof(serv_addr));
  if((sockfd = socket(2, SOCK_STREAM, 0)) < 0){
      do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd\n" "\n", "connection.c",49,__FUNCTION__); } while(0);
      return ((void *)0);
  }
  serv_addr.sin_family = 2;
  serv_addr.sin_port = htons(connection_data.port);
  if(inet_pton(2, connection_data.address, &serv_addr.sin_addr)<=0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot set server address: %s\n" "\n", "connection.c",57,__FUNCTION__, connection_data.address); } while(0);
   return ((void *)0);
  }
  while(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
   sleep(connection_data.retry);
   attempts--;
   if(attempts <= 0){
    do { fprintf(stdout, "[%s:%d]:%s: " "Cannot connect to server: %s, errno %d\n" "\n", "connection.c",65,__FUNCTION__, connection_data.address, (*__errno_location ())); } while(0);
    ((ConnectionData *) conn_data)->error = (*__errno_location ());
    return ((void *)0);
   }
  }
  Connection__SaveServerData(sockfd, connection_data.file);
  sleep(connection_data.refresh);
 }
 return ((void *)0);
}

canonical_original_code:
void *Connection__Connect(void *conn_data){
 int sockfd = 0;
 int attempts = 5;
 struct ConnectionData connection_data = *((ConnectionData *)conn_data);
 struct sockaddr_in serv_addr;
 Connection__DumpData(&connection_data);
 while(1){
  memset(&serv_addr, 0, sizeof(serv_addr));
  if((sockfd = socket(2, SOCK_STREAM, 0)) < 0){
      do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd\n" "\n", "connection.c",49,__FUNCTION__); } while(0);
      return ((void *)0);
  }
  serv_addr.sin_family = 2;
  serv_addr.sin_port = htons(connection_data.port);
  if(inet_pton(2, connection_data.address, &serv_addr.sin_addr)<=0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot set server address: %s\n" "\n", "connection.c",57,__FUNCTION__, connection_data.address); } while(0);
   return ((void *)0);
  }
  while(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
   sleep(connection_data.retry);
   attempts--;
   if(attempts <= 0){
    do { fprintf(stdout, "[%s:%d]:%s: " "Cannot connect to server: %s, errno %d\n" "\n", "connection.c",65,__FUNCTION__, connection_data.address, (*__errno_location ())); } while(0);
    ((struct ConnectionData *) conn_data)->error = (*__errno_location ());
    return ((void *)0);
   }
  }
  Connection__SaveServerData(sockfd, connection_data.file);
  sleep(connection_data.refresh);
 }
 return ((void *)0);
}

variable_types: sockfd int

variable_types: attempts int

variable_types: struct ConnectionData { int port; int refresh; int retry; char address[256]; char file[256]; int error; }


emptyList

repo: mwalton/data-structures

decompiled_code:
void __fastcall emptyList(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

if ( ptr )
{
v1 = ptr;
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

if ( ptr )
{
v1 = ptr;
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
while ( v1 );
}
}

original_code:
void emptyList(node *head) {
  while (head != ((void *)0)) {
    node *old = head;
    head = head->link;
    free (old);
  }
}

canonical_original_code:
void emptyList(struct node *head) {
  while (head != ((void *)0)) {
    struct node *old = head;
    head = head->link;
    free (old);
  }
}

variable_types: struct node { double item; struct node *link; } *


repo: mwarning/KadNode

decompiled_code:
void __fastcall nss_server_handler(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax
socklen_t addr_len; // [rsp+0h] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-78h] BYREF

if ( a1 > 0 )
{
addr_len = 16;
v2 = accept(a2, &addr, &addr_len);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &nss_client_handler);
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax
socklen_t addr_len; // [rsp+0h] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-78h] BYREF

if ( a1 > 0 )
{
addr_len = 16;
v2 = accept(a2, &addr, &addr_len);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &nss_client_handler);
}
}
}

original_code:
static void nss_server_handler(int rc, int serversock)
{
 socklen_t addrlen;
 int clientsock;
 struct sockaddr_un addr;
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(struct sockaddr_in);
 clientsock = accept(serversock, (struct sockaddr *) &addr, &addrlen);
 if (clientsock < 0) {
  log_print(3, "accept(): %s", strerror((*__errno_location ())));;
  return;
 }
 net_add_handler(clientsock, &nss_client_handler);
}

canonical_original_code:
static void nss_server_handler(int rc, int serversock)
{
 unsigned int addrlen;
 int clientsock;
 struct sockaddr_un addr;
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(struct sockaddr_in);
 clientsock = accept(serversock, (struct sockaddr *) &addr, &addrlen);
 if (clientsock < 0) {
  log_print(3, "accept(): %s", strerror((*__errno_location ())));;
  return;
 }
 net_add_handler(clientsock, &nss_client_handler);
}

variable_types: rc int

variable_types: serversock int

variable_types:
addrlen
unsigned int

variable_types: clientsock int

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


original_code:
static void nss_client_handler(int rc, int clientsock)
{
 const struct search_t *search;
 const struct result_t *result;
 struct kadnode_nss_request req = {0};
 struct kadnode_nss_response res = {0};
 int count;
 int af;
 if (rc <= 0) {
  return;
 }
 rc = recv(clientsock, &req, sizeof(req), 0);
 if (rc != sizeof(req)) {
  goto abort;
 }
 req.name[256 - 1] = '\0';
 if (!has_tld(&req.name[0], gconf->query_tld)) {
  goto finish;
 }
 search = kad_lookup(&req.name[0]);
 if (search == ((void *)0)) {
  goto finish;
 }
 af = req.af;
 count = 0;
 for (result = search->results; result; result = result->next) {
  if (is_valid_result(result) && count < 16) {
   if (af == 0) {
    af = result->addr.ss_family;
   }
   if (af != result->addr.ss_family) {
    continue;
   }
   if (af == 10) {
    memcpy(&res.data.ipv6[count], &((IP6 *)&result->addr)->sin6_addr, sizeof(struct in6_addr));
    count += 1;
   }
   if (af == 2) {
    memcpy(&res.data.ipv4[count], &((IP4 *)&result->addr)->sin_addr, sizeof(struct in_addr));
    count += 1;
   }
  }
 }
 res.af = af;
 res.count = count;
finish:
 write(clientsock, &res, sizeof(res));
abort:
 close(clientsock);
 net_remove_handler(clientsock, &nss_client_handler);
}

canonical_original_code:
static void nss_client_handler(int rc, int clientsock)
{
 struct search_t *search;
 struct result_t *result;
 struct kadnode_nss_request req = {0};
 struct kadnode_nss_response res = {0};
 int count;
 int af;
 if (rc <= 0) {
  return;
 }
 rc = recv(clientsock, &req, sizeof(req), 0);
 if (rc != sizeof(req)) {
  goto abort;
 }
 req.name[256 - 1] = '\0';
 if (!has_tld(&req.name[0], gconf->query_tld)) {
  goto finish;
 }
 search = kad_lookup(&req.name[0]);
 if (search == ((void *)0)) {
  goto finish;
 }
 af = req.af;
 count = 0;
 for (result = search->results; result; result = result->next) {
  if (is_valid_result(result) && count < 16) {
   if (af == 0) {
    af = result->addr.ss_family;
   }
   if (af != result->addr.ss_family) {
    continue;
   }
   if (af == 10) {
    memcpy(&res.data.ipv6[count], &((struct sockaddr_in6 *)&result->addr)->sin6_addr, sizeof(struct in6_addr));
    count += 1;
   }
   if (af == 2) {
    memcpy(&res.data.ipv4[count], &((struct sockaddr_in *)&result->addr)->sin_addr, sizeof(struct in_addr));
    count += 1;
   }
  }
 }
 res.af = af;
 res.count = count;
finish:
 write(clientsock, &res, sizeof(res));
abort:
 close(clientsock);
 net_remove_handler(clientsock, &nss_client_handler);
}

variable_types: struct search_t { struct search_t *next; unsigned char id[20]; unsigned short done; char query[256]; long start_time; struct result_t { struct result_t *next; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } addr; enum AUTH_STATE { AUTH_OK=0, AUTH_AGAIN=1, AUTH_FAILED=2, AUTH_ERROR=3, AUTH_SKIP=4, AUTH_PROGRESS=5, AUTH_WAITING=6 } state; } *results; void (*callback)(); } *

variable_types: struct kadnode_nss_request { int af; char name[256]; }

variable_types: struct kadnode_nss_response { int af; int count; union { struct in_addr { unsigned int s_addr; } ipv4[16]; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ipv6[16]; } data; }

variable_types: count int

variable_types: af int

user_defined_types: struct kadnode_nss_response { int af; int count; union { struct in_addr ipv4[16]; struct in6_addr ipv6[16]; } data; }

user_defined_types: struct search_t { struct search_t *next; unsigned char id[20]; unsigned short done; char query[256]; long start_time; struct result_t *results; void (*callback)(); }

user_defined_types: struct result_t { struct result_t *next; struct sockaddr_storage addr; enum AUTH_STATE state; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }


decompiled_code:
__int64 nss_setup()
{
if ( (unsigned int)unix_create_unix_socket(*(_QWORD *)(gconf + 128LL), &g_nss_sock) )
return 1LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "NSS: Bind to %s", *(const char **)(gconf + 128LL));
net_add_handler((unsigned int)g_nss_sock, nss_server_handler);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( (unsigned int)unix_create_unix_socket(*(_QWORD *)(gconf + 128LL), &g_nss_sock) )
return 1LL;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "NSS: Bind to %s", *(const char **)(gconf + 128LL));
net_add_handler((unsigned int)g_nss_sock, nss_server_handler);
return 0LL;
}

original_code:
int nss_setup(void)
{
 if (unix_create_unix_socket(gconf->nss_path, &g_nss_sock) == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "NSS: Bind to %s", gconf->nss_path); } while (0);
  net_add_handler(g_nss_sock, &nss_server_handler);
  return 0;
 }
 return 1;
}

canonical_original_code:
int nss_setup()
{
 if (unix_create_unix_socket(gconf->nss_path, &g_nss_sock) == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "NSS: Bind to %s", gconf->nss_path); } while (0);
  net_add_handler(g_nss_sock, &nss_server_handler);
  return 0;
 }
 return 1;
}


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

if ( g_nss_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 128LL));
return result;
}

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

if ( g_nss_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 128LL));
return result;
}

original_code:
void nss_free(void)
{
 if (g_nss_sock >= 0) {
  unix_remove_unix_socket(gconf->nss_path, g_nss_sock);
 }
}

canonical_original_code:
void nss_free()
{
 if (g_nss_sock >= 0) {
  unix_remove_unix_socket(gconf->nss_path, g_nss_sock);
 }
}


decompiled_code:
__int64 main_run()
{
unsigned int v0; // er12
int v1; // ebx
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx

v0 = conf_load();
if ( v0 == 1 )
return v0;
v1 = kad_setup();
announces_setup();
searches_setup();
peerfile_setup();
v2 = lpd_setup() | v1;
v3 = bob_setup() | v0 | v2;
v4 = dns_setup() | v3;
v5 = nss_setup() | v4;
v6 = tls_client_setup() | v5;
v7 = tls_server_setup() | v6;
v0 = cmd_setup() | v7;
if ( !v0 )
{
net_loop();
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "Shutting down...");
}
peerfile_export();
cmd_free();
nss_free();
dns_free();
bob_free();
lpd_free();
tls_server_free();
tls_client_free();
peerfile_free();
searches_free();
announces_free();
kad_free();
conf_free();
net_free();
if ( !g_pidfile_written )
return v0;
unlink(*(const char **)(gconf + 32LL));
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
int v1; // ebx
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx

v0 = conf_load();
if ( v0 == 1 )
return v0;
v1 = kad_setup();
announces_setup();
searches_setup();
peerfile_setup();
v2 = lpd_setup() | v1;
v3 = bob_setup() | v0 | v2;
v4 = dns_setup() | v3;
v5 = nss_setup() | v4;
v6 = tls_client_setup() | v5;
v7 = tls_server_setup() | v6;
v0 = cmd_setup() | v7;
if ( !v0 )
{
net_loop();
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "Shutting down...");
}
peerfile_export();
cmd_free();
nss_free();
dns_free();
bob_free();
lpd_free();
tls_server_free();
tls_client_free();
peerfile_free();
searches_free();
announces_free();
kad_free();
conf_free();
net_free();
if ( !g_pidfile_written )
return v0;
unlink(*(const char **)(gconf + 32LL));
return v0;
}

original_code:
int main_run(void)
{
 int rc = 0;
 if (0 != 0) {
  fprintf(stderr, "Problematic EXIT_SUCCESS definition\n");
  return 1;
 }
 rc |= conf_load();
 if (rc == 1) {
  return 1;
 }
 rc |= kad_setup();
 announces_setup();
 searches_setup();
 peerfile_setup();
 rc |= lpd_setup();
 rc |= bob_setup();
 rc |= dns_setup();
 rc |= nss_setup();
 rc |= tls_client_setup();
 rc |= tls_server_setup();
 rc |= cmd_setup();
 if (rc == 0) {
  net_loop();
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "Shutting down..."); } while (0);
 }
 peerfile_export();
 cmd_free();
 nss_free();
 dns_free();
 bob_free();
 lpd_free();
 tls_server_free();
 tls_client_free();
 peerfile_free();
 searches_free();
 announces_free();
 kad_free();
 conf_free();
 net_free();
 if (g_pidfile_written) {
  unlink(gconf->pidfile);
 }
 return rc;
}

canonical_original_code:
int main_run()
{
 int rc = 0;
 if (0 != 0) {
  fprintf(stderr, "Problematic EXIT_SUCCESS definition\n");
  return 1;
 }
 rc |= conf_load();
 if (rc == 1) {
  return 1;
 }
 rc |= kad_setup();
 announces_setup();
 searches_setup();
 peerfile_setup();
 rc |= lpd_setup();
 rc |= bob_setup();
 rc |= dns_setup();
 rc |= nss_setup();
 rc |= tls_client_setup();
 rc |= tls_server_setup();
 rc |= cmd_setup();
 if (rc == 0) {
  net_loop();
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "Shutting down..."); } while (0);
 }
 peerfile_export();
 cmd_free();
 nss_free();
 dns_free();
 bob_free();
 lpd_free();
 tls_server_free();
 tls_client_free();
 peerfile_free();
 searches_free();
 announces_free();
 kad_free();
 conf_free();
 net_free();
 if (g_pidfile_written) {
  unlink(gconf->pidfile);
 }
 return rc;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v6; // rbp
unsigned int v7; // eax
int *v8; // rax
char *v9; // rax

if ( strstr(*argv, "kadnode-ctl") )
return ((__int64 (__fastcall *)(_QWORD, const char **, __int64, __int64))cmd_client)(
(unsigned int)argc,
argv,
v3,
v4);
if ( (unsigned int)conf_setup((unsigned int)argc, argv) == 1 )
return 1;
if ( *(_BYTE *)(gconf + 56LL) )
{
*(_DWORD *)(gconf + 64LL) = 1;
fclose(stderr);
fclose(stdout);
fclose(stdin);
unix_fork();
if ( chdir("/") )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(3LL, "Changing working directory to '/' failed: %s", v9);
exit(1);
}
}
else
{
conf_info();
}
unix_signals();
v6 = *(_QWORD *)(gconf + 32LL);
if ( v6 )
{
v7 = getpid();
unix_write_pidfile(v7, v6);
g_pidfile_written = 1;
}
unix_dropuid0();
return main_run();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v6; // rbp
unsigned int v7; // eax
int *v8; // rax
char *v9; // rax

if ( strstr(*argv, "kadnode-ctl") )
return ((__int64 (__fastcall *)(_QWORD, const char **, __int64, __int64))cmd_client)(
(unsigned int)argc,
argv,
v3,
v4);
if ( (unsigned int)conf_setup((unsigned int)argc, argv) == 1 )
return 1;
if ( *(_BYTE *)(gconf + 56LL) )
{
*(_DWORD *)(gconf + 64LL) = 1;
fclose(stderr);
fclose(stdout);
fclose(stdin);
unix_fork();
if ( chdir("/") )
{
v8 = _errno_location();
v9 = strerror(*v8);
log_print(3LL, "Changing working directory to '/' failed: %s", v9);
exit(1);
}
}
else
{
conf_info();
}
unix_signals();
v6 = *(_QWORD *)(gconf + 32LL);
if ( v6 )
{
v7 = getpid();
unix_write_pidfile(v7, v6);
g_pidfile_written = 1;
}
unix_dropuid0();
return func0();
}

original_code:
int main(int argc, char *argv[])
{
 int rc = 0;
 if (strstr(argv[0], "kadnode-ctl")) {
  return cmd_client(argc, argv);
 }
 rc |= conf_setup(argc, argv);
 if (rc == 1) {
  return 1;
 }
 if (gconf->is_daemon) {
  gconf->use_syslog = 1;
  fclose(stderr);
  fclose(stdout);
  fclose(stdin);
  unix_fork();
  if (chdir("/") != 0) {
   log_print(3, "Changing working directory to '/' failed: %s", strerror((*__errno_location ())));;
   exit(1);
  }
 } else {
  conf_info();
 }
 unix_signals();
 if (gconf->pidfile) {
  unix_write_pidfile(getpid(), gconf->pidfile);
  g_pidfile_written = 1;
 }
 unix_dropuid0();
 return main_run();
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void __fastcall tls_server_handler(int a1, int a2)
{
int *v2; // rbx
int *v3; // rdi
int v4; // eax
int v5; // eax
char v6[8]; // [rsp+0h] [rbp-20h] BYREF
__int128 v7; // [rsp+8h] [rbp-18h] BYREF

v7 = 0LL;
if ( a1 > 0 && g_client_fd4 < 0 && g_client_fd6 < 0 )
{
v2 = &g_client_fd6;
v3 = &g_listen_fd6;
if ( g_listen_fd6 != a2 )
v2 = &g_client_fd4;
if ( g_listen_fd6 != a2 )
v3 = &g_listen_fd4;
v4 = mbedtls_net_accept(v3, v2, &v7, 16LL, v6);
if ( v4 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: mbedtls_net_accept returned -0x%x", (unsigned int)-v4);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Got incoming connection");
v5 = mbedtls_net_set_nonblock(v2);
if ( v5 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: net_set_nonblock() returned -0x%x", (unsigned int)-v5);
}
else
{
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_set_bio(&g_ssl, v2, &mbedtls_net_send, &mbedtls_net_recv, 0LL);
net_add_handler((unsigned int)*v2, tls_client_handler);
}
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
int *v2; // rbx
int *v3; // rdi
int v4; // eax
int v5; // eax
char v6[8]; // [rsp+0h] [rbp-20h] BYREF
__int128 v7; // [rsp+8h] [rbp-18h] BYREF

v7 = 0LL;
if ( a1 > 0 && g_client_fd4 < 0 && g_client_fd6 < 0 )
{
v2 = &g_client_fd6;
v3 = &g_listen_fd6;
if ( g_listen_fd6 != a2 )
v2 = &g_client_fd4;
if ( g_listen_fd6 != a2 )
v3 = &g_listen_fd4;
v4 = mbedtls_net_accept(v3, v2, &v7, 16LL, v6);
if ( v4 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: mbedtls_net_accept returned -0x%x", (unsigned int)-v4);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Got incoming connection");
v5 = mbedtls_net_set_nonblock(v2);
if ( v5 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: net_set_nonblock() returned -0x%x", (unsigned int)-v5);
}
else
{
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_set_bio(&g_ssl, v2, &mbedtls_net_send, &mbedtls_net_recv, 0LL);
net_add_handler((unsigned int)*v2, tls_client_handler);
}
}
}
}

original_code:
static void tls_server_handler(int rc, int sock)
{
 unsigned char client_ip[16] = { 0 };
 mbedtls_net_context *listen_fd;
 mbedtls_net_context *client_fd;
 size_t cliip_len;
 int ret;
 if (rc <= 0 || g_client_fd4.fd > -1 || g_client_fd6.fd > -1) {
  return;
 }
 if (sock == g_listen_fd6.fd) {
  listen_fd = &g_listen_fd6;
  client_fd = &g_client_fd6;
 } else {
  listen_fd = &g_listen_fd4;
  client_fd = &g_client_fd4;
 }
 if ((ret = mbedtls_net_accept(listen_fd, client_fd,
    client_ip, sizeof(client_ip), &cliip_len)) != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: mbedtls_net_accept returned -0x%x", -ret); } while (0);
  return;
 }
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Got incoming connection"); } while (0);
 ret = mbedtls_net_set_nonblock(client_fd);
 if (ret != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: net_set_nonblock() returned -0x%x", -ret); } while (0);
  return;
 }
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_set_bio(&g_ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, ((void *)0));
 net_add_handler(client_fd->fd, &tls_client_handler);
}

canonical_original_code:
static void tls_server_handler(int rc, int sock)
{
 unsigned char client_ip[16] = { 0 };
 struct mbedtls_net_context *listen_fd;
 struct mbedtls_net_context *client_fd;
 unsigned long cliip_len;
 int ret;
 if (rc <= 0 || g_client_fd4.fd > -1 || g_client_fd6.fd > -1) {
  return;
 }
 if (sock == g_listen_fd6.fd) {
  listen_fd = &g_listen_fd6;
  client_fd = &g_client_fd6;
 } else {
  listen_fd = &g_listen_fd4;
  client_fd = &g_client_fd4;
 }
 if ((ret = mbedtls_net_accept(listen_fd, client_fd,
    client_ip, sizeof(client_ip), &cliip_len)) != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: mbedtls_net_accept returned -0x%x", -ret); } while (0);
  return;
 }
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Got incoming connection"); } while (0);
 ret = mbedtls_net_set_nonblock(client_fd);
 if (ret != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: net_set_nonblock() returned -0x%x", -ret); } while (0);
  return;
 }
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_set_bio(&g_ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, ((void *)0));
 net_add_handler(client_fd->fd, &tls_client_handler);
}

variable_types: sock int

variable_types: struct mbedtls_net_context { int fd; } *

variable_types: ret int


decompiled_code:
__int64 __fastcall end_client_connection(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

net_remove_handler(*a1, tls_client_handler);
while ( (unsigned int)mbedtls_ssl_close_notify(&g_ssl) == -26752 )
;
mbedtls_net_free(a1);
mbedtls_ssl_session_reset(&g_ssl);
if ( a2 == -27264 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Hello verification requested");
}
else if ( a2 == -26496 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Client initiated reconnection from same port");
}
else if ( a2 )
{
mbedtls_strerror(a2, v3, 100LL);
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Error -0x%x - %s", -a2, v3);
}
else
{
result = *(unsigned int *)(gconf + 60LL);
if ( !(_DWORD)result )
result = log_print(7LL, "TLS-Server: Authentication successful");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

net_remove_handler(*a1, tls_client_handler);
while ( (unsigned int)mbedtls_ssl_close_notify(&g_ssl) == -26752 )
;
mbedtls_net_free(a1);
mbedtls_ssl_session_reset(&g_ssl);
if ( a2 == -27264 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Hello verification requested");
}
else if ( a2 == -26496 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Client initiated reconnection from same port");
}
else if ( a2 )
{
mbedtls_strerror(a2, v3, 100LL);
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Error -0x%x - %s", -a2, v3);
}
else
{
result = *(unsigned int *)(gconf + 60LL);
if ( !(_DWORD)result )
result = log_print(7LL, "TLS-Server: Authentication successful");
}
return result;
}

original_code:
static void end_client_connection(mbedtls_net_context *client_fd, int result)
{
 int ret;
 net_remove_handler(client_fd->fd, &tls_client_handler);
 do ret = mbedtls_ssl_close_notify(&g_ssl);
 while (ret == -0x6880);
 mbedtls_net_free(client_fd);
 mbedtls_ssl_session_reset(&g_ssl);
 if (result == -0x6A80) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Hello verification requested"); } while (0);
 } else if (result == -0x6780) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Client initiated reconnection from same port"); } while (0);
 } else if (result != 0) {
  char error_buf[100];
  mbedtls_strerror(result, error_buf, 100);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Error -0x%x - %s", -result, error_buf); } while (0);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Authentication successful"); } while (0);
 }
}

canonical_original_code:
static void end_client_connection(struct mbedtls_net_context *client_fd, int result)
{
 int ret;
 net_remove_handler(client_fd->fd, &tls_client_handler);
 do ret = mbedtls_ssl_close_notify(&g_ssl);
 while (ret == -0x6880);
 mbedtls_net_free(client_fd);
 mbedtls_ssl_session_reset(&g_ssl);
 if (result == -0x6A80) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Hello verification requested"); } while (0);
 } else if (result == -0x6780) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Client initiated reconnection from same port"); } while (0);
 } else if (result != 0) {
  char error_buf[100];
  mbedtls_strerror(result, error_buf, 100);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Error -0x%x - %s", -result, error_buf); } while (0);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Authentication successful"); } while (0);
 }
}

variable_types: result int