main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rax
int **v4; // rbx
unsigned __int64 v5; // rax
int **v6; // rbx
unsigned __int64 v7; // rax
__int64 ***v8; // rbx
unsigned __int64 v9; // rax
__int64 ***v10; // rbx
unsigned __int64 v11; // rax
__int64 **v12; // rbx
unsigned __int64 v13; // rax
__int64 **v14; // rbx
char s[112]; // [rsp+0h] [rbp-1A0h] BYREF
char v17[32]; // [rsp+70h] [rbp-130h] BYREF
char v18[44]; // [rsp+90h] [rbp-110h] BYREF
int v19; // [rsp+BCh] [rbp-E4h] BYREF
char v20[32]; // [rsp+C0h] [rbp-E0h] BYREF
char v21[32]; // [rsp+E0h] [rbp-C0h] BYREF
char v22[32]; // [rsp+100h] [rbp-A0h] BYREF
__int64 v23; // [rsp+120h] [rbp-80h]
__int64 v24; // [rsp+128h] [rbp-78h]
__int64 v25; // [rsp+130h] [rbp-70h]
char *v26; // [rsp+138h] [rbp-68h]
char *v27; // [rsp+140h] [rbp-60h]
__int64 ***v28; // [rsp+148h] [rbp-58h]
__int64 ***v29; // [rsp+150h] [rbp-50h]
int **v30; // [rsp+158h] [rbp-48h]
int **v31; // [rsp+160h] [rbp-40h]
int *v32; // [rsp+168h] [rbp-38h]
int jj; // [rsp+170h] [rbp-30h]
int ii; // [rsp+174h] [rbp-2Ch]
int n; // [rsp+178h] [rbp-28h]
int m; // [rsp+17Ch] [rbp-24h]
int l; // [rsp+180h] [rbp-20h]
int k; // [rsp+184h] [rbp-1Ch]
int j; // [rsp+188h] [rbp-18h]
int i; // [rsp+18Ch] [rbp-14h]

scanf("%d\n", &v19);
v32 = (int *)malloc(0x1770uLL);
getPrimeMap(v32, 1500);
v31 = (int **)operator new[](0x98uLL);
v30 = (int **)operator new[](0x98uLL);
for ( i = 0; i <= 18; ++i )
{
v3 = 9 * i + 1;
if ( v3 > 0x1FFFFFFFFFFFFFFELL )
_cxa_throw_bad_array_new_length();
v4 = &v31[i];
*v4 = (int *)operator new[](4 * v3);
v5 = 9 * i + 1;
if ( v5 > 0x1FFFFFFFFFFFFFFELL )
_cxa_throw_bad_array_new_length();
v6 = &v30[i];
*v6 = (int *)operator new[](4 * v5);
}
v29 = (__int64 ***)operator new[](0x98uLL);
v28 = (__int64 ***)operator new[](0x98uLL);
for ( j = 0; j <= 18; ++j )
{
v7 = 9 * j + 1;
if ( v7 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v8 = &v29[j];
*v8 = (__int64 **)operator new[](8 * v7);
v9 = 9 * j + 1;
if ( v9 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v10 = &v28[18 - j];
*v10 = (__int64 **)operator new[](8 * v9);
}
for ( k = 0; k <= 18; ++k )
{
for ( l = 0; l <= 9 * k; ++l )
{
v11 = 81 * k + 1;
if ( v11 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v12 = &v29[k][l];
*v12 = (__int64 *)operator new[](8 * v11);
v13 = 81 * k + 1;
if ( v13 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v14 = &v28[18 - k][l];
*v14 = (__int64 *)operator new[](8 * v13);
}
}
computeG(v29, v28, v31, v30);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v18);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v17);
v27 = s;
while ( v19 > 0 )
{
fgets(s, 100, stdin);
v26 = strtok(v27, " ");
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator=(v18, v26);
v26 = strtok(0LL, " \n");
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator=(v17, v26);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v20, v18);
v25 = getNumber((__int64)v20, v29, v28, v31, v30, v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v20);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v21, v17);
v24 = getNumber((__int64)v21, v29, v28, v31, v30, v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v21);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v22, v18);
v23 = isLucky((__int64)v22, (__int64)v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v22);
printf("%Ld\n", v24 - v25 + v23);
--v19;
}
for ( m = 0; m <= 18; ++m )
{
if ( v31[m] )
operator delete[](v31[m]);
if ( v30[m] )
operator delete[](v30[m]);
}
if ( v31 )
operator delete[](v31);
if ( v30 )
operator delete[](v30);
for ( n = 0; n <= 18; ++n )
{
for ( ii = 0; ii <= 9 * n; ++ii )
{
if ( v29[n][ii] )
operator delete[](v29[n][ii]);
if ( v28[18 - n][ii] )
operator delete[](v28[18 - n][ii]);
}
}
for ( jj = 0; jj <= 18; ++jj )
{
if ( v29[jj] )
operator delete[](v29[jj]);
if ( v28[18 - jj] )
operator delete[](v28[18 - jj]);
}
if ( v29 )
operator delete[](v29);
if ( v28 )
operator delete[](v28);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v17);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v18);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rax
int **v4; // rbx
unsigned __int64 v5; // rax
int **v6; // rbx
unsigned __int64 v7; // rax
__int64 ***v8; // rbx
unsigned __int64 v9; // rax
__int64 ***v10; // rbx
unsigned __int64 v11; // rax
__int64 **v12; // rbx
unsigned __int64 v13; // rax
__int64 **v14; // rbx
char s[112]; // [rsp+0h] [rbp-1A0h] BYREF
char v17[32]; // [rsp+70h] [rbp-130h] BYREF
char v18[44]; // [rsp+90h] [rbp-110h] BYREF
int v19; // [rsp+BCh] [rbp-E4h] BYREF
char v20[32]; // [rsp+C0h] [rbp-E0h] BYREF
char v21[32]; // [rsp+E0h] [rbp-C0h] BYREF
char v22[32]; // [rsp+100h] [rbp-A0h] BYREF
__int64 v23; // [rsp+120h] [rbp-80h]
__int64 v24; // [rsp+128h] [rbp-78h]
__int64 v25; // [rsp+130h] [rbp-70h]
char *v26; // [rsp+138h] [rbp-68h]
char *v27; // [rsp+140h] [rbp-60h]
__int64 ***v28; // [rsp+148h] [rbp-58h]
__int64 ***v29; // [rsp+150h] [rbp-50h]
int **v30; // [rsp+158h] [rbp-48h]
int **v31; // [rsp+160h] [rbp-40h]
int *v32; // [rsp+168h] [rbp-38h]
int jj; // [rsp+170h] [rbp-30h]
int ii; // [rsp+174h] [rbp-2Ch]
int n; // [rsp+178h] [rbp-28h]
int m; // [rsp+17Ch] [rbp-24h]
int l; // [rsp+180h] [rbp-20h]
int k; // [rsp+184h] [rbp-1Ch]
int j; // [rsp+188h] [rbp-18h]
int i; // [rsp+18Ch] [rbp-14h]

scanf("%d\n", &v19);
v32 = (int *)malloc(0x1770uLL);
func0(v32, 1500);
v31 = (int **)operator new[](0x98uLL);
v30 = (int **)operator new[](0x98uLL);
for ( i = 0; i <= 18; ++i )
{
v3 = 9 * i + 1;
if ( v3 > 0x1FFFFFFFFFFFFFFELL )
_cxa_throw_bad_array_new_length();
v4 = &v31[i];
*v4 = (int *)operator new[](4 * v3);
v5 = 9 * i + 1;
if ( v5 > 0x1FFFFFFFFFFFFFFELL )
_cxa_throw_bad_array_new_length();
v6 = &v30[i];
*v6 = (int *)operator new[](4 * v5);
}
v29 = (__int64 ***)operator new[](0x98uLL);
v28 = (__int64 ***)operator new[](0x98uLL);
for ( j = 0; j <= 18; ++j )
{
v7 = 9 * j + 1;
if ( v7 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v8 = &v29[j];
*v8 = (__int64 **)operator new[](8 * v7);
v9 = 9 * j + 1;
if ( v9 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v10 = &v28[18 - j];
*v10 = (__int64 **)operator new[](8 * v9);
}
for ( k = 0; k <= 18; ++k )
{
for ( l = 0; l <= 9 * k; ++l )
{
v11 = 81 * k + 1;
if ( v11 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v12 = &v29[k][l];
*v12 = (__int64 *)operator new[](8 * v11);
v13 = 81 * k + 1;
if ( v13 > 0xFFFFFFFFFFFFFFFLL )
_cxa_throw_bad_array_new_length();
v14 = &v28[18 - k][l];
*v14 = (__int64 *)operator new[](8 * v13);
}
}
func1(v29, v28, v31, v30);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v18);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v17);
v27 = s;
while ( v19 > 0 )
{
fgets(s, 100, stdin);
v26 = strtok(v27, " ");
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator=(v18, v26);
v26 = strtok(0LL, " \n");
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator=(v17, v26);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v20, v18);
v25 = func3((__int64)v20, v29, v28, v31, v30, v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v20);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v21, v17);
v24 = func3((__int64)v21, v29, v28, v31, v30, v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v21);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(v22, v18);
v23 = func4((__int64)v22, (__int64)v32);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v22);
printf("%Ld\n", v24 - v25 + v23);
--v19;
}
for ( m = 0; m <= 18; ++m )
{
if ( v31[m] )
operator delete[](v31[m]);
if ( v30[m] )
operator delete[](v30[m]);
}
if ( v31 )
operator delete[](v31);
if ( v30 )
operator delete[](v30);
for ( n = 0; n <= 18; ++n )
{
for ( ii = 0; ii <= 9 * n; ++ii )
{
if ( v29[n][ii] )
operator delete[](v29[n][ii]);
if ( v28[18 - n][ii] )
operator delete[](v28[18 - n][ii]);
}
}
for ( jj = 0; jj <= 18; ++jj )
{
if ( v29[jj] )
operator delete[](v29[jj]);
if ( v28[18 - jj] )
operator delete[](v28[18 - jj]);
}
if ( v29 )
operator delete[](v29);
if ( v28 )
operator delete[](v28);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v17);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(v18);
return 0;
}


repo: sakisbou/gamelib

decompiled_code:
__int64 game_logic_tick()
{
int v0; // xmm0_4
int v1; // xmm0_4
int v2; // xmm0_4
int v3; // xmm0_4
float v4; // xmm0_4
int v5; // xmm0_4
double v6; // xmm0_8
int v7; // xmm0_4
int v8; // xmm0_4
int v9; // xmm0_4
double v10; // xmm0_8
int v11; // xmm0_4
int v12; // xmm0_4
int v13; // xmm0_4
double v14; // xmm0_8
int v15; // xmm0_4
int v16; // xmm0_4
int v17; // xmm0_4
double v18; // xmm0_8
int v19; // xmm0_4
int v20; // xmm0_4
__int64 result; // rax

time_diff = get_rel_time();
if ( (unsigned __int8)getkey(4LL) )
{
*(float *)&v0 = *(float *)&lander_x_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_x_pos_rate = v0;
}
else if ( (unsigned __int8)getkey(7LL) )
{
*(float *)&v5 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_x_pos_rate;
lander_x_pos_rate = v5;
}
else
{
v6 = *(float *)&lander_x_pos_rate;
if ( *(float *)&lander_x_pos_rate <= 0.0001 )
{
if ( v6 < -0.0001 )
{
*(float *)&v8 = v6 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_x_pos_rate = v8;
}
}
else
{
*(float *)&v7 = v6 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_x_pos_rate = v7;
}
}
*(float *)&lander_x_pos = (float)((float)((float)time_diff * *(float *)&lander_x_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_x_pos;
if ( (unsigned __int8)getkey(26LL) )
{
*(float *)&v1 = *(float *)&lander_z_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_z_pos_rate = v1;
}
else if ( (unsigned __int8)getkey(22LL) )
{
*(float *)&v9 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_z_pos_rate;
lander_z_pos_rate = v9;
}
else
{
v10 = *(float *)&lander_z_pos_rate;
if ( *(float *)&lander_z_pos_rate <= 0.0001 )
{
if ( v10 < -0.0001 )
{
*(float *)&v12 = v10 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_z_pos_rate = v12;
}
}
else
{
*(float *)&v11 = v10 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_z_pos_rate = v11;
}
}
*(float *)&lander_z_pos = (float)((float)((float)time_diff * *(float *)&lander_z_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_z_pos;
if ( (unsigned __int8)getkey(82LL) )
{
*(float *)&v2 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_y_pos_rate;
lander_y_pos_rate = v2;
}
else if ( (unsigned __int8)getkey(81LL) )
{
*(float *)&v13 = *(float *)&lander_y_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_pos_rate = v13;
}
else
{
v14 = *(float *)&lander_y_pos_rate;
if ( *(float *)&lander_y_pos_rate <= 0.0001 )
{
if ( v14 < -0.0001 )
{
*(float *)&v16 = v14 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_y_pos_rate = v16;
}
}
else
{
*(float *)&v15 = v14 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_y_pos_rate = v15;
}
}
*(float *)&lander_y_pos = (float)((float)((float)time_diff * *(float *)&lander_y_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_y_pos;
if ( (unsigned __int8)getkey(80LL) )
{
*(float *)&v3 = (double)time_diff * 0.0005 * *(float *)&accel_scale + *(float *)&lander_y_angle_rate;
lander_y_angle_rate = v3;
}
else if ( (unsigned __int8)getkey(79LL) )
{
*(float *)&v17 = *(float *)&lander_y_angle_rate - (double)time_diff * 0.0005 * *(float *)&accel_scale;
lander_y_angle_rate = v17;
}
else
{
v18 = *(float *)&lander_y_angle_rate;
if ( *(float *)&lander_y_angle_rate <= 0.0001 )
{
if ( v18 < -0.0001 )
{
*(float *)&v20 = v18 + (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_angle_rate = v20;
}
}
else
{
*(float *)&v19 = v18 - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_angle_rate = v19;
}
}
*(float *)&lander_y_angle = (float)((float)((float)time_diff * *(float *)&lander_y_angle_rate) * *(float *)&move_scale)
+ *(float *)&lander_y_angle;
v4 = *(float *)&earth_angle + 0.01;
if ( v4 > 360.0 )
earth_angle = 0;
else
*(float *)&earth_angle = *(float *)&earth_angle + 0.01;
result = getkey(41LL);
if ( (_BYTE)result )
result = quit();
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // xmm0_4
int v1; // xmm0_4
int v2; // xmm0_4
int v3; // xmm0_4
float v4; // xmm0_4
int v5; // xmm0_4
double v6; // xmm0_8
int v7; // xmm0_4
int v8; // xmm0_4
int v9; // xmm0_4
double v10; // xmm0_8
int v11; // xmm0_4
int v12; // xmm0_4
int v13; // xmm0_4
double v14; // xmm0_8
int v15; // xmm0_4
int v16; // xmm0_4
int v17; // xmm0_4
double v18; // xmm0_8
int v19; // xmm0_4
int v20; // xmm0_4
__int64 result; // rax

time_diff = get_rel_time();
if ( (unsigned __int8)getkey(4LL) )
{
*(float *)&v0 = *(float *)&lander_x_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_x_pos_rate = v0;
}
else if ( (unsigned __int8)getkey(7LL) )
{
*(float *)&v5 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_x_pos_rate;
lander_x_pos_rate = v5;
}
else
{
v6 = *(float *)&lander_x_pos_rate;
if ( *(float *)&lander_x_pos_rate <= 0.0001 )
{
if ( v6 < -0.0001 )
{
*(float *)&v8 = v6 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_x_pos_rate = v8;
}
}
else
{
*(float *)&v7 = v6 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_x_pos_rate = v7;
}
}
*(float *)&lander_x_pos = (float)((float)((float)time_diff * *(float *)&lander_x_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_x_pos;
if ( (unsigned __int8)getkey(26LL) )
{
*(float *)&v1 = *(float *)&lander_z_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_z_pos_rate = v1;
}
else if ( (unsigned __int8)getkey(22LL) )
{
*(float *)&v9 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_z_pos_rate;
lander_z_pos_rate = v9;
}
else
{
v10 = *(float *)&lander_z_pos_rate;
if ( *(float *)&lander_z_pos_rate <= 0.0001 )
{
if ( v10 < -0.0001 )
{
*(float *)&v12 = v10 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_z_pos_rate = v12;
}
}
else
{
*(float *)&v11 = v10 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_z_pos_rate = v11;
}
}
*(float *)&lander_z_pos = (float)((float)((float)time_diff * *(float *)&lander_z_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_z_pos;
if ( (unsigned __int8)getkey(82LL) )
{
*(float *)&v2 = (double)time_diff * 0.0001 * *(float *)&accel_scale + *(float *)&lander_y_pos_rate;
lander_y_pos_rate = v2;
}
else if ( (unsigned __int8)getkey(81LL) )
{
*(float *)&v13 = *(float *)&lander_y_pos_rate - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_pos_rate = v13;
}
else
{
v14 = *(float *)&lander_y_pos_rate;
if ( *(float *)&lander_y_pos_rate <= 0.0001 )
{
if ( v14 < -0.0001 )
{
*(float *)&v16 = v14 + (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_y_pos_rate = v16;
}
}
else
{
*(float *)&v15 = v14 - (double)time_diff * 0.00003 * *(float *)&accel_scale;
lander_y_pos_rate = v15;
}
}
*(float *)&lander_y_pos = (float)((float)((float)time_diff * *(float *)&lander_y_pos_rate) * *(float *)&move_scale)
+ *(float *)&lander_y_pos;
if ( (unsigned __int8)getkey(80LL) )
{
*(float *)&v3 = (double)time_diff * 0.0005 * *(float *)&accel_scale + *(float *)&lander_y_angle_rate;
lander_y_angle_rate = v3;
}
else if ( (unsigned __int8)getkey(79LL) )
{
*(float *)&v17 = *(float *)&lander_y_angle_rate - (double)time_diff * 0.0005 * *(float *)&accel_scale;
lander_y_angle_rate = v17;
}
else
{
v18 = *(float *)&lander_y_angle_rate;
if ( *(float *)&lander_y_angle_rate <= 0.0001 )
{
if ( v18 < -0.0001 )
{
*(float *)&v20 = v18 + (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_angle_rate = v20;
}
}
else
{
*(float *)&v19 = v18 - (double)time_diff * 0.0001 * *(float *)&accel_scale;
lander_y_angle_rate = v19;
}
}
*(float *)&lander_y_angle = (float)((float)((float)time_diff * *(float *)&lander_y_angle_rate) * *(float *)&move_scale)
+ *(float *)&lander_y_angle;
v4 = *(float *)&earth_angle + 0.01;
if ( v4 > 360.0 )
earth_angle = 0;
else
*(float *)&earth_angle = *(float *)&earth_angle + 0.01;
result = getkey(41LL);
if ( (_BYTE)result )
result = quit();
return result;
}

original_code:
void game_logic_tick(void)
{
  time_diff = get_rel_time();
  if(getkey(SDL_SCANCODE_A)) lander_x_pos_rate -= .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_D)) lander_x_pos_rate += .0001 * time_diff * accel_scale;
  else if(lander_x_pos_rate > 0.0001) lander_x_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_x_pos_rate < -0.0001) lander_x_pos_rate += .00003 * time_diff * accel_scale;
  lander_x_pos += lander_x_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_W)) lander_z_pos_rate -= .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_S)) lander_z_pos_rate += .0001 * time_diff * accel_scale;
  else if(lander_z_pos_rate > 0.0001) lander_z_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_z_pos_rate < -0.0001) lander_z_pos_rate += .00003 * time_diff * accel_scale;
  lander_z_pos += lander_z_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_UP)) lander_y_pos_rate += .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_DOWN)) lander_y_pos_rate -= .0001 * time_diff * accel_scale;
  else if(lander_y_pos_rate > 0.0001) lander_y_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_y_pos_rate < -0.0001) lander_y_pos_rate += .00003 * time_diff * accel_scale;
  lander_y_pos += lander_y_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_LEFT)) lander_y_angle_rate += .0005 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_RIGHT)) lander_y_angle_rate -= .0005 * time_diff * accel_scale;
  else if(lander_y_angle_rate > 0.0001) lander_y_angle_rate -= .0001 * time_diff * accel_scale;
  else if(lander_y_angle_rate < -0.0001) lander_y_angle_rate += .0001 * time_diff * accel_scale;
  lander_y_angle += lander_y_angle_rate * time_diff * move_scale;
  earth_angle += 0.01; if(earth_angle > 360) earth_angle = 0;
  if(getkey(SDL_SCANCODE_ESCAPE)) quit();
}

canonical_original_code:
void game_logic_tick()
{
  time_diff = get_rel_time();
  if(getkey(SDL_SCANCODE_A)) lander_x_pos_rate -= .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_D)) lander_x_pos_rate += .0001 * time_diff * accel_scale;
  else if(lander_x_pos_rate > 0.0001) lander_x_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_x_pos_rate < -0.0001) lander_x_pos_rate += .00003 * time_diff * accel_scale;
  lander_x_pos += lander_x_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_W)) lander_z_pos_rate -= .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_S)) lander_z_pos_rate += .0001 * time_diff * accel_scale;
  else if(lander_z_pos_rate > 0.0001) lander_z_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_z_pos_rate < -0.0001) lander_z_pos_rate += .00003 * time_diff * accel_scale;
  lander_z_pos += lander_z_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_UP)) lander_y_pos_rate += .0001 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_DOWN)) lander_y_pos_rate -= .0001 * time_diff * accel_scale;
  else if(lander_y_pos_rate > 0.0001) lander_y_pos_rate -= .00003 * time_diff * accel_scale;
  else if(lander_y_pos_rate < -0.0001) lander_y_pos_rate += .00003 * time_diff * accel_scale;
  lander_y_pos += lander_y_pos_rate * time_diff * move_scale;
  if(getkey(SDL_SCANCODE_LEFT)) lander_y_angle_rate += .0005 * time_diff * accel_scale;
  else if(getkey(SDL_SCANCODE_RIGHT)) lander_y_angle_rate -= .0005 * time_diff * accel_scale;
  else if(lander_y_angle_rate > 0.0001) lander_y_angle_rate -= .0001 * time_diff * accel_scale;
  else if(lander_y_angle_rate < -0.0001) lander_y_angle_rate += .0001 * time_diff * accel_scale;
  lander_y_angle += lander_y_angle_rate * time_diff * move_scale;
  earth_angle += 0.01; if(earth_angle > 360) earth_angle = 0;
  if(getkey(SDL_SCANCODE_ESCAPE)) quit();
}

return_type: void


init

decompiled_code:
__int64 init()
{
sdl_init();
gl_init();
gl_textures_init();
game_data_init();
return time_reset();
}

canonical_decompiled_code:
__int64 func0()
{
sdl_init();
gl_init();
gl_textures_init();
game_data_init();
return time_reset();
}

original_code:
void init(void)
{
  sdl_init();
  gl_init();
  gl_textures_init();
  game_data_init();
  time_reset();
}

canonical_original_code:
void init()
{
  sdl_init();
  gl_init();
  gl_textures_init();
  game_data_init();
  time_reset();
}


cleanup

decompiled_code:
__int64 cleanup()
{
return sdl_cleanup();
}

canonical_decompiled_code:
__int64 func1()
{
return sdl_cleanup();
}

original_code:
void cleanup(void)
{
  sdl_cleanup();
}

canonical_original_code:
void cleanup()
{
  sdl_cleanup();
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init(argc, argv, envp);
do
{
game_logic_tick();
render_scene();
}
while ( (unsigned int)update_events() != -1 );
cleanup();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func0(argc, argv, envp);
do
{
game_logic_tick();
render_scene();
}
while ( (unsigned int)update_events() != -1 );
func1();
return 0;
}

original_code:
int main(int argc, char **argv)
{
  init();
  for(;;)
  {
    game_logic_tick();
    render_scene();
    if(update_events() == -1) break;
  }
  cleanup();
  return(0);
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
void __fastcall __noreturn syserror_(const char *a1, unsigned int a2)
{
int *v2; // rax

perror("game");
fprintf(stderr, ": %s : line %d\n", a1, a2);
v2 = _errno_location();
exit(*v2);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, unsigned int a2)
{
int *v2; // rax

perror("game");
fprintf(stderr, ": %s : line %d\n", a1, a2);
v2 = _errno_location();
exit(*v2);
}

original_code:
void syserror_(char *file, int line)
{
  perror("game");
  fprintf(stderr, ": %s : line %d\n", file, line);
  exit((*__errno_location ()));
}

variable_types:
file
char *

variable_types: line int


decompiled_code:
void __fastcall __noreturn sdl_error_(const char *a1, unsigned int a2)
{
const char *v2; // rax

v2 = (const char *)SDL_GetError();
fprintf(stderr, "%s: %s: line %d: %s\n", "game", a1, a2, v2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1, unsigned int a2)
{
const char *v2; // rax

v2 = (const char *)SDL_GetError();
fprintf(stderr, "%s: %s: line %d: %s\n", "game", a1, a2, v2);
exit(1);
}

original_code:
void sdl_error_(char *file, int line)
{
  fprintf(stderr, "%s: %s: line %d: %s\n", "game", file, line, SDL_GetError());
  exit(1);
}


decompiled_code:
void __noreturn usererror_(const char *a1, ...)
{
char v1[1000]; // [rsp+0h] [rbp-4B8h] BYREF
gcc_va_list arg; // [rsp+3E8h] [rbp-D0h] BYREF

va_start(arg, a1);
sprintf(v1, "%s%s%s", "game", " : %s : line %i : ", a1);
vfprintf(stderr, v1, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func2(const char *a1, ...)
{
char v1[1000]; // [rsp+0h] [rbp-4B8h] BYREF
gcc_va_list arg; // [rsp+3E8h] [rbp-D0h] BYREF

va_start(arg, a1);
sprintf(v1, "%s%s%s", "game", " : %s : line %i : ", a1);
vfprintf(stderr, v1, arg);
exit(1);
}

original_code:
void usererror_(char *format, ...)
{
  va_list ap;
  char formatstr[1000];
  sprintf(formatstr, "%s%s%s", "game", " : %s : line %i : ", format);
  __builtin_va_start(ap,format);
  vfprintf(stderr, formatstr, ap);
  __builtin_va_end(ap);
  exit(1);
}

canonical_original_code:
void usererror_(char *format, ...)
{
  __builtin_va_list ap;
  char formatstr[1000];
  sprintf(formatstr, "%s%s%s", "game", " : %s : line %i : ", format);
  __builtin_va_start(ap,format);
  vfprintf(stderr, formatstr, ap);
  __builtin_va_end(ap);
  exit(1);
}

variable_types:
format
char *

variable_types:
formatstr
char [1000]


decompiled_code:
__int64 sdl_init()
{
__int64 v0; // rdi
__int64 result; // rax

if ( (unsigned int)SDL_Init(16433LL) )
sdl_error_("sdl.c", 0x56u);
if ( (unsigned int)SDL_GL_SetAttribute(4LL, 32LL) )
sdl_error_("sdl.c", 0x58u);
if ( (unsigned int)SDL_GL_SetAttribute(6LL, 24LL) )
sdl_error_("sdl.c", 0x59u);
if ( (unsigned int)SDL_GL_SetAttribute(5LL, 1LL) )
sdl_error_("sdl.c", 0x5Au);
v0 = SDL_CreateWindow("game", 536805376LL, 536805376LL, 1024LL, 768LL, 3LL);
main_window = v0;
if ( !v0 )
sdl_error_("sdl.c", 0x62u);
result = SDL_GL_CreateContext(v0);
main_gl_context = result;
if ( !result )
sdl_error_("sdl.c", 0x65u);
return result;
}

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

if ( (unsigned int)SDL_Init(16433LL) )
func1("sdl.c", 0x56u);
if ( (unsigned int)SDL_GL_SetAttribute(4LL, 32LL) )
func1("sdl.c", 0x58u);
if ( (unsigned int)SDL_GL_SetAttribute(6LL, 24LL) )
func1("sdl.c", 0x59u);
if ( (unsigned int)SDL_GL_SetAttribute(5LL, 1LL) )
func1("sdl.c", 0x5Au);
v0 = SDL_CreateWindow("game", 536805376LL, 536805376LL, 1024LL, 768LL, 3LL);
main_window = v0;
if ( !v0 )
func1("sdl.c", 0x62u);
result = SDL_GL_CreateContext(v0);
main_gl_context = result;
if ( !result )
func1("sdl.c", 0x65u);
return result;
}

original_code:
void sdl_init(void)
{
  int r;
  uint32_t fs = 0; if(1) fs = SDL_WINDOW_FULLSCREEN;
  r = SDL_Init(
                0x00000020u |
                0x00000010u |
                0x00000001u |
                0x00004000u
              );
  if(r != 0) sdl_error_("sdl.c", 86);
  if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32)!=0) sdl_error_("sdl.c", 88);
  if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)!=0) sdl_error_("sdl.c", 89);
  if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)!=0) sdl_error_("sdl.c", 90);
  main_window = SDL_CreateWindow(
                                  "game",
                                  (0x1FFF0000u|(0)), (0x1FFF0000u|(0)),
                                  1024, 768,
                                  SDL_WINDOW_OPENGL | fs
                                );
  if(main_window == ((void *)0)) sdl_error_("sdl.c", 98);
  main_gl_context = SDL_GL_CreateContext(main_window);
  if(main_gl_context == ((void *)0)) sdl_error_("sdl.c", 101);
}

canonical_original_code:
void sdl_init()
{
  int r;
  unsigned int fs = 0; if(1) fs = SDL_WINDOW_FULLSCREEN;
  r = SDL_Init(
                0x00000020u |
                0x00000010u |
                0x00000001u |
                0x00004000u
              );
  if(r != 0) sdl_error_("sdl.c", 86);
  if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32)!=0) sdl_error_("sdl.c", 88);
  if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)!=0) sdl_error_("sdl.c", 89);
  if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)!=0) sdl_error_("sdl.c", 90);
  main_window = SDL_CreateWindow(
                                  "game",
                                  (0x1FFF0000u|(0)), (0x1FFF0000u|(0)),
                                  1024, 768,
                                  SDL_WINDOW_OPENGL | fs
                                );
  if(main_window == ((void *)0)) sdl_error_("sdl.c", 98);
  main_gl_context = SDL_GL_CreateContext(main_window);
  if(main_gl_context == ((void *)0)) sdl_error_("sdl.c", 101);
}

variable_types: r int

variable_types:
fs
unsigned int


decompiled_code:
__int64 sdl_cleanup()
{
SDL_GL_DeleteContext(main_gl_context);
SDL_DestroyWindow(main_window);
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func4()
{
SDL_GL_DeleteContext(main_gl_context);
SDL_DestroyWindow(main_window);
return SDL_Quit();
}

original_code:
void sdl_cleanup(void)
{
  SDL_GL_DeleteContext(main_gl_context);
  SDL_DestroyWindow(main_window);
  SDL_Quit();
}

canonical_original_code:
void sdl_cleanup()
{
  SDL_GL_DeleteContext(main_gl_context);
  SDL_DestroyWindow(main_window);
  SDL_Quit();
}


getkey

decompiled_code:
__int64 __fastcall getkey(unsigned __int8 a1)
{
return *(unsigned __int8 *)(SDL_GetKeyboardState(0LL) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1)
{
return *(unsigned __int8 *)(SDL_GetKeyboardState(0LL) + a1);
}

original_code:
uint8_t getkey(uint8_t key)
{
  const uint8_t *keyboard = SDL_GetKeyboardState(((void *)0));
  return keyboard[key];
}

canonical_original_code:
unsigned char getkey(unsigned char key)
{
  unsigned char *keyboard = SDL_GetKeyboardState(((void *)0));
  return keyboard[key];
}

variable_types:
key
unsigned char

variable_types:
keyboard
unsigned char *

return_type: unsigned char


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

result = SDL_GetTicks();
prev_time = result;
return result;
}

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

result = SDL_GetTicks();
prev_time = result;
return result;
}

original_code: void time_reset(void) { prev_time = SDL_GetTicks(); }

canonical_original_code: void time_reset() { prev_time = SDL_GetTicks(); }


decompiled_code:
__int64 get_rel_time()
{
int v0; // eax
unsigned int v1; // edx

v0 = SDL_GetTicks();
v1 = v0 - prev_time;
prev_time = v0;
return v1;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // eax
unsigned int v1; // edx

v0 = SDL_GetTicks();
v1 = v0 - prev_time;
prev_time = v0;
return v1;
}

original_code:
uint32_t get_rel_time(void)
{
  uint32_t time = SDL_GetTicks();
  uint32_t rel_time = time - prev_time;
  prev_time = time;
  return rel_time;
}

canonical_original_code:
unsigned int get_rel_time()
{
  unsigned int time = SDL_GetTicks();
  unsigned int rel_time = time - prev_time;
  prev_time = time;
  return rel_time;
}

variable_types:
time
unsigned int

return_type: unsigned int


repo: sakkko/Karmanet

decompiled_code:
void __fastcall __noreturn retx_func(void *a1)
{
char *v1; // rax
const char **v2; // rbx
const char *v3; // rbp
__int16 v4; // ax
char *v5; // rax
__int16 v6; // ax
int v7; // eax
int v8; // eax
char *v9; // rax
struct timespec v10; // [rsp+0h] [rbp-48h] BYREF
char dest[44]; // [rsp+1Ch] [rbp-2Ch] BYREF

v10.tv_sec = 1LL;
v10.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v10, 0LL);
v8 = pthread_mutex_lock(&retx_mutex);
if ( v8 )
{
v1 = strerror(v8);
fprintf(stderr, "retx_func error - can't acquire lock: %s\n", v1);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v2 = (const char **)packet_list_head;
if ( packet_list_head )
break;
LABEL_16:
v7 = pthread_mutex_unlock(&retx_mutex);
if ( v7 )
{
v9 = strerror(v7);
fprintf(stderr, "retx_func error - can't release lock: %s\n", v9);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
while ( 1 )
{
v3 = *v2;
v4 = *((_WORD *)*v2 + 529);
if ( v4 <= 0 )
{
if ( !v4 )
{
strncpy(dest, v3 + 4, 3uLL);
dest[3] = 0;
printf(
"Pacchetto: INDEX=%u CMD=%s NRETX=%d COUNTDOWN %d\n",
*((unsigned __int16 *)v3 + 4),
dest,
(unsigned int)*((__int16 *)v3 + 528),
0LL);
if ( *((_WORD *)v3 + 528) == 6 || *((_WORD *)v3 + 530) == 6 )
{
if ( (int)write_err(*((unsigned int *)a1 + 4), *((_QWORD *)a1 + 1), dest) < 0 )
{
v5 = strerror(0);
fprintf(stderr, "retx_func error - write_err failed: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
*((_WORD *)v3 + 529) = -1;
v2 = (const char **)v2[1];
goto LABEL_5;
}
if ( (int)send_packet(*(unsigned int *)v3, v3 + 1040, v3 + 4) < 0 )
{
fwrite("retx_func error - send_packet failed\n", 1uLL, 0x25uLL, stderr);
*((_WORD *)v3 + 529) = *((_WORD *)v3 + 528);
++*((_WORD *)v3 + 530);
v2 = (const char **)v2[1];
goto LABEL_5;
}
v6 = *((_WORD *)v3 + 528) + 1;
*((_WORD *)v3 + 528) = v6;
*((_WORD *)v3 + 529) = v6;
}
}
else
{
*((_WORD *)v3 + 529) = v4 - 1;
}
v2 = (const char **)v2[1];
LABEL_5:
if ( !v2 )
goto LABEL_16;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
char *v1; // rax
const char **v2; // rbx
const char *v3; // rbp
__int16 v4; // ax
char *v5; // rax
__int16 v6; // ax
int v7; // eax
int v8; // eax
char *v9; // rax
struct timespec v10; // [rsp+0h] [rbp-48h] BYREF
char dest[44]; // [rsp+1Ch] [rbp-2Ch] BYREF

v10.tv_sec = 1LL;
v10.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v10, 0LL);
v8 = pthread_mutex_lock(&retx_mutex);
if ( v8 )
{
v1 = strerror(v8);
fprintf(stderr, "retx_func error - can't acquire lock: %s\n", v1);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v2 = (const char **)packet_list_head;
if ( packet_list_head )
break;
LABEL_16:
v7 = pthread_mutex_unlock(&retx_mutex);
if ( v7 )
{
v9 = strerror(v7);
fprintf(stderr, "retx_func error - can't release lock: %s\n", v9);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
while ( 1 )
{
v3 = *v2;
v4 = *((_WORD *)*v2 + 529);
if ( v4 <= 0 )
{
if ( !v4 )
{
strncpy(dest, v3 + 4, 3uLL);
dest[3] = 0;
printf(
"Pacchetto: INDEX=%u CMD=%s NRETX=%d COUNTDOWN %d\n",
*((unsigned __int16 *)v3 + 4),
dest,
(unsigned int)*((__int16 *)v3 + 528),
0LL);
if ( *((_WORD *)v3 + 528) == 6 || *((_WORD *)v3 + 530) == 6 )
{
if ( (int)write_err(*((unsigned int *)a1 + 4), *((_QWORD *)a1 + 1), dest) < 0 )
{
v5 = strerror(0);
fprintf(stderr, "retx_func error - write_err failed: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
*((_WORD *)v3 + 529) = -1;
v2 = (const char **)v2[1];
goto LABEL_5;
}
if ( (int)send_packet(*(unsigned int *)v3, v3 + 1040, v3 + 4) < 0 )
{
fwrite("retx_func error - send_packet failed\n", 1uLL, 0x25uLL, stderr);
*((_WORD *)v3 + 529) = *((_WORD *)v3 + 528);
++*((_WORD *)v3 + 530);
v2 = (const char **)v2[1];
goto LABEL_5;
}
v6 = *((_WORD *)v3 + 528) + 1;
*((_WORD *)v3 + 528) = v6;
*((_WORD *)v3 + 529) = v6;
}
}
else
{
*((_WORD *)v3 + 529) = v4 - 1;
}
v2 = (const char **)v2[1];
LABEL_5:
if ( !v2 )
goto LABEL_16;
}
}

original_code:
void retx_func(void *args) {
 struct retx_info *rtxinfo = (struct retx_info *)args;
 char cmdstr[3 + 1];
 struct node *iterator;
 struct packet_node *pck_node;
 struct timespec sleep_time;
 int rc;
 sleep_time.tv_sec = 1;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
   fprintf(stderr, "retx_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  iterator = packet_list_head;
  while (iterator != ((void *)0)) {
   pck_node = (struct packet_node *)iterator->data;
   if (pck_node->countdown > 0) {
    pck_node->countdown --;
   } else if (pck_node->countdown == 0) {
    strncpy(cmdstr, pck_node->pck.cmd, 3);
    cmdstr[3] = 0;
    printf("Pacchetto: INDEX=%u CMD=%s NRETX=%d COUNTDOWN %d\n", pck_node->pck.index, cmdstr, pck_node->nretx, pck_node->countdown);
    if (pck_node->nretx == 6 || pck_node->errors == 6) {
     if (write_err(rtxinfo->retx_wr_pipe, rtxinfo->pipe_mutex, cmdstr) < 0) {
      fprintf(stderr, "retx_func error - write_err failed: %s\n", strerror(rc));
      pthread_exit((void *)-1);
     }
     pck_node->countdown = -1;
     iterator = iterator->next;
     continue;
    }
    if (send_packet(pck_node->socksd, &pck_node->addrto, &pck_node->pck) < 0) {
     fprintf(stderr, "retx_func error - send_packet failed\n");
     pck_node->countdown = pck_node->nretx;
     pck_node->errors ++;
     iterator = iterator->next;
     continue;
    }
    pck_node->nretx ++;
    pck_node->countdown = pck_node->nretx;
   }
   iterator = iterator->next;
  }
  if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
   fprintf(stderr, "retx_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
 }
}

canonical_original_code:
void retx_func(void *args) {
 struct retx_info *rtxinfo = (struct retx_info *)args;
 char cmdstr[-1];
 struct node *iterator;
 struct packet_node *pck_node;
 struct timespec sleep_time;
 int rc;
 sleep_time.tv_sec = 1;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
   fprintf(stderr, "retx_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  iterator = packet_list_head;
  while (iterator != ((void *)0)) {
   pck_node = (struct packet_node *)iterator->data;
   if (pck_node->countdown > 0) {
    pck_node->countdown --;
   } else if (pck_node->countdown == 0) {
    strncpy(cmdstr, pck_node->pck.cmd, 3);
    cmdstr[3] = 0;
    printf("Pacchetto: INDEX=%u CMD=%s NRETX=%d COUNTDOWN %d\n", pck_node->pck.index, cmdstr, pck_node->nretx, pck_node->countdown);
    if (pck_node->nretx == 6 || pck_node->errors == 6) {
     if (write_err(rtxinfo->retx_wr_pipe, rtxinfo->pipe_mutex, cmdstr) < 0) {
      fprintf(stderr, "retx_func error - write_err failed: %s\n", strerror(rc));
      pthread_exit((void *)-1);
     }
     pck_node->countdown = -1;
     iterator = iterator->next;
     continue;
    }
    if (send_packet(pck_node->socksd, &pck_node->addrto, &pck_node->pck) < 0) {
     fprintf(stderr, "retx_func error - send_packet failed\n");
     pck_node->countdown = pck_node->nretx;
     pck_node->errors ++;
     iterator = iterator->next;
     continue;
    }
    pck_node->nretx ++;
    pck_node->countdown = pck_node->nretx;
   }
   iterator = iterator->next;
  }
  if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
   fprintf(stderr, "retx_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
 }
}

variable_types:
args
void *

variable_types: struct retx_info { unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *pipe_mutex; int retx_wr_pipe; } *

variable_types:
cmdstr
char [-1]

variable_types: struct node { void *data; struct node *next; struct node *prev; } *

variable_types: struct packet_node { int socksd; struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; } pck; 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]; } addrto; short nretx; short countdown; short errors; } *

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: rc int

user_defined_types: struct retx_info { unsigned long thread; union pthread_mutex_t *pipe_mutex; int retx_wr_pipe; }

user_defined_types: struct packet_node { int socksd; struct packet pck; struct sockaddr_in addrto; short nretx; short countdown; short errors; }

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: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __fastcall retx_send(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // eax
int result; // eax
char *v6; // rax
char *v7; // rax

v4 = pthread_mutex_lock(&retx_mutex);
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "retx_send error - can't acquire lock: %s\n", v6);
result = -1;
}
else if ( (int)send_packet(a1, a2, a3) < 0 )
{
fwrite("retx_send error - send_packet failed\n", 1uLL, 0x25uLL, stderr);
result = -1;
}
else
{
insert_packet(a1, a3, a2, 1LL, 1LL, 0LL);
result = pthread_mutex_unlock(&retx_mutex);
if ( result )
{
v7 = strerror(result);
fprintf(stderr, "retx_send error - can't release lock: %s\n", v7);
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // eax
int result; // eax
char *v6; // rax
char *v7; // rax

v4 = pthread_mutex_lock(&retx_mutex);
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "retx_send error - can't acquire lock: %s\n", v6);
result = -1;
}
else if ( (int)send_packet(a1, a2, a3) < 0 )
{
fwrite("retx_send error - send_packet failed\n", 1uLL, 0x25uLL, stderr);
result = -1;
}
else
{
insert_packet(a1, a3, a2, 1LL, 1LL, 0LL);
result = pthread_mutex_unlock(&retx_mutex);
if ( result )
{
v7 = strerror(result);
fprintf(stderr, "retx_send error - can't release lock: %s\n", v7);
result = -1;
}
}
return result;
}

original_code:
int retx_send(int socksd, const struct sockaddr_in *addr, const struct packet *pck) {
 int rc;
 if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_send error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (send_packet(socksd, addr, pck) < 0) {
  fprintf(stderr, "retx_send error - send_packet failed\n");
  return -1;
 }
 insert_packet(socksd, pck, addr, 1, 1, 0);
 if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_send error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int retx_send(int socksd, struct sockaddr_in *addr, struct packet *pck) {
 int rc;
 if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_send error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (send_packet(socksd, addr, pck) < 0) {
  fprintf(stderr, "retx_send error - send_packet failed\n");
  return -1;
 }
 insert_packet(socksd, pck, addr, 1, 1, 0);
 if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_send error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

variable_types: socksd int

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

variable_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; } *


decompiled_code:
int __fastcall retx_stop(unsigned __int16 a1, char *a2)
{
__int64 v2; // rax
__int64 v3; // rbx
int v4; // eax
int result; // eax
char *v6; // rax
char *v7; // rax

v2 = get_packet_node(a1);
if ( !v2 )
return 1;
v3 = v2;
if ( a2 )
strncpy(a2, (const char *)(*(_QWORD *)v2 + 4LL), 3uLL);
v4 = pthread_mutex_lock(&retx_mutex);
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "retx_stop error - can't acquire lock: %s\n", v6);
result = -1;
}
else
{
remove_packet_node(v3);
result = pthread_mutex_unlock(&retx_mutex);
if ( result )
{
v7 = strerror(result);
fprintf(stderr, "retx_stop error - can't release lock: %s\n", v7);
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int16 a1, char *a2)
{
__int64 v2; // rax
__int64 v3; // rbx
int v4; // eax
int result; // eax
char *v6; // rax
char *v7; // rax

v2 = get_packet_node(a1);
if ( !v2 )
return 1;
v3 = v2;
if ( a2 )
strncpy(a2, (const char *)(*(_QWORD *)v2 + 4LL), 3uLL);
v4 = pthread_mutex_lock(&retx_mutex);
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "retx_stop error - can't acquire lock: %s\n", v6);
result = -1;
}
else
{
remove_packet_node(v3);
result = pthread_mutex_unlock(&retx_mutex);
if ( result )
{
v7 = strerror(result);
fprintf(stderr, "retx_stop error - can't release lock: %s\n", v7);
result = -1;
}
}
return result;
}

original_code:
int retx_stop(int pck_index, char *pck_cmd) {
 struct node *tmp_node;
 int rc;
 tmp_node = get_packet_node(pck_index);
 if (tmp_node == ((void *)0)) {
  return 1;
 }
 if (pck_cmd != ((void *)0)) {
  strncpy(pck_cmd, ((struct packet_node *)tmp_node->data)->pck.cmd, 3);
 }
 if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_stop error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 remove_packet_node(tmp_node);
 if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
  fprintf(stderr, "retx_stop error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

user_defined_types: struct node { void *data; struct node *next; struct node *prev; }

user_defined_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; }


decompiled_code:
int __fastcall retx_run(void *arg)
{
int result; // eax
char *v2; // rax

result = pthread_create((pthread_t *)arg, 0LL, (void *(*)(void *))retx_func, arg);
if ( result )
{
v2 = strerror(result);
fprintf(stderr, "thread_run error - can't create thread: %s\n", v2);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(void *arg)
{
int result; // eax
char *v2; // rax

result = pthread_create((pthread_t *)arg, 0LL, (void *(*)(void *))retx_func, arg);
if ( result )
{
v2 = strerror(result);
fprintf(stderr, "thread_run error - can't create thread: %s\n", v2);
result = -1;
}
return result;
}

original_code:
int retx_run(struct retx_info *rtxinfo) {
 int rc;
 if ((rc = pthread_create(&rtxinfo->thread, ((void *)0), (void *)retx_func, (void *)rtxinfo)) != 0) {
  fprintf(stderr, "thread_run error - can't create thread: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall mutex_send(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
char *v8; // rax
char *v9; // rax

v4 = pthread_mutex_lock(&retx_mutex);
v5 = v4;
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "mutx_send error - can't acquire lock: %s\n", v8);
v5 = -1;
}
else
{
if ( (int)send_packet(a1, a2, a3) < 0 )
{
perror("mutex_send error - send_packet failed");
v5 = -1;
}
v6 = pthread_mutex_unlock(&retx_mutex);
if ( v6 )
{
v9 = strerror(v6);
fprintf(stderr, "mutex_send error - can't release lock: %s\n", v9);
v5 = -1;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
char *v8; // rax
char *v9; // rax

v4 = pthread_mutex_lock(&retx_mutex);
v5 = v4;
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "mutx_send error - can't acquire lock: %s\n", v8);
v5 = -1;
}
else
{
if ( (int)send_packet(a1, a2, a3) < 0 )
{
perror("mutex_send error - send_packet failed");
v5 = -1;
}
v6 = pthread_mutex_unlock(&retx_mutex);
if ( v6 )
{
v9 = strerror(v6);
fprintf(stderr, "mutex_send error - can't release lock: %s\n", v9);
v5 = -1;
}
}
return v5;
}

original_code:
int mutex_send(int socksd, const struct sockaddr_in *addr, const struct packet *pck) {
 int rc, ret;
 ret = 0;
 if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
  fprintf(stderr, "mutx_send error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (send_packet(socksd, addr, pck) < 0) {
  perror("mutex_send error - send_packet failed");
  ret = -1;
 }
 if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
  fprintf(stderr, "mutex_send error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return ret;
}

canonical_original_code:
int mutex_send(int socksd, struct sockaddr_in *addr, struct packet *pck) {
 int rc; int ret;
 ret = 0;
 if ((rc = pthread_mutex_lock(&retx_mutex)) != 0) {
  fprintf(stderr, "mutx_send error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (send_packet(socksd, addr, pck) < 0) {
  perror("mutex_send error - send_packet failed");
  ret = -1;
 }
 if ((rc = pthread_mutex_unlock(&retx_mutex)) != 0) {
  fprintf(stderr, "mutex_send error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return ret;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall insert_request(__int16 a1, __int64 a2, __int16 a3)
{
_QWORD *v4; // rdx
__int64 result; // rax
_QWORD *v6; // rax

v4 = get_request_node(a1, a2, a3);
result = 1LL;
if ( !v4 )
{
v6 = new_request_node(a1, a2, a3);
request_list_head = insert_node(request_list_head, v6);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1, __int64 a2, __int16 a3)
{
_QWORD *v4; // rdx
__int64 result; // rax
_QWORD *v6; // rax

v4 = get_request_node(a1, a2, a3);
result = 1LL;
if ( !v4 )
{
v6 = new_request_node(a1, a2, a3);
request_list_head = insert_node(request_list_head, v6);
result = 0LL;
}
return result;
}

original_code:
int insert_request(unsigned short id, unsigned long ip_sender, unsigned short port) {
 struct node *tmp_node;
 struct request_node *new_node;
 if (request_list_head == ((void *)0)) {
 }
 if ((tmp_node = get_request_node(id, ip_sender, port)) == ((void *)0)) {
  new_node = new_request_node(id, ip_sender, port);
  request_list_head = insert_node(request_list_head, new_node);
 } else {
  return 1;
 }
 return 0;
}

variable_types:
id
unsigned short

variable_types:
port
unsigned short

variable_types: struct request_node { unsigned short id; unsigned long ip_sender; unsigned short port; unsigned short ttl; } *


decompiled_code:
void **__fastcall remove_request(__int16 a1, __int64 a2, __int16 a3)
{
void **result; // rax
void **v4; // rbx

result = (void **)get_request_node(a1, a2, a3);
if ( result )
{
v4 = result;
free(*result);
*v4 = 0LL;
result = (void **)remove_node(request_list_head, v4);
request_list_head = (__int64)result;
}
return result;
}

canonical_decompiled_code:
void **__fastcall func1(__int16 a1, __int64 a2, __int16 a3)
{
void **result; // rax
void **v4; // rbx

result = (void **)get_request_node(a1, a2, a3);
if ( result )
{
v4 = result;
free(*result);
*v4 = 0LL;
result = (void **)remove_node(request_list_head, v4);
request_list_head = (__int64)result;
}
return result;
}

original_code:
void remove_request(unsigned short id, unsigned long ip_sender, unsigned short port) {
 struct node *tmp_node;
 if ((tmp_node = get_request_node(id, ip_sender, port)) != ((void *)0)) {
  free(tmp_node->data);
  tmp_node->data = ((void *)0);
  request_list_head = remove_node(request_list_head, tmp_node);
 }
}


decompiled_code:
void __fastcall remove_request_node(void **a1)
{
if ( a1 )
{
if ( request_list_head )
{
free(*a1);
*a1 = 0LL;
request_list_head = remove_node(request_list_head, a1);
}
}
}

canonical_decompiled_code:
void __fastcall func2(void **a1)
{
if ( a1 )
{
if ( request_list_head )
{
free(*a1);
*a1 = 0LL;
request_list_head = remove_node(request_list_head, a1);
}
}
}

original_code:
void remove_request_node(struct node *request_node) {
 if (request_node == ((void *)0) || request_list_head == ((void *)0)) {
  return;
 }
 free(request_node->data);
 request_node->data = ((void *)0);
 request_list_head = remove_node(request_list_head, request_node);
}


decompiled_code:
void free_list_request()
{
void **i; // rdi

for ( i = (void **)request_list_head; request_list_head; i = (void **)request_list_head )
remove_request_node(i);
}

canonical_decompiled_code:
void func3()
{
void **i; // rdi

for ( i = (void **)request_list_head; request_list_head; i = (void **)request_list_head )
func2(i);
}

original_code:
void free_list_request() {
 while (request_list_head != ((void *)0)) {
  remove_request_node(request_list_head);
 }
}


decompiled_code:
unsigned __int64 __fastcall get_file_info(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
unsigned __int64 result; // rax

v2 = get_file_size();
*(_DWORD *)(a2 + 292) = v2;
result = v2 / 0x7D000uLL;
if ( v2 != 512000 * (_DWORD)result )
result = (unsigned int)(result + 1);
*(_DWORD *)(a2 + 296) = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
unsigned __int64 result; // rax

v2 = get_file_size();
*(_DWORD *)(a2 + 292) = v2;
result = v2 / 0x7D000uLL;
if ( v2 != 512000 * (_DWORD)result )
result = (unsigned int)(result + 1);
*(_DWORD *)(a2 + 296) = result;
return result;
}

original_code:
void get_file_info(int fd, struct transfer_node *ulnode) {
 ulnode->file_info.file_size = get_file_size(fd);
 ulnode->file_info.chunk_number = ulnode->file_info.file_size / 512000;
 if (ulnode->file_info.file_size % 512000) {
  ulnode->file_info.chunk_number ++;
 }
}

variable_types: fd int

variable_types: struct transfer_node { int socksd; 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]; } addrto; struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; } file_info; } *

user_defined_types: struct transfer_node { int socksd; struct sockaddr_in addrto; struct transfer_info file_info; }


original_code:
int get_part_number(int chunk_number, const struct transfer_node *ulnode) {
 int part;
 if (chunk_number == ulnode->file_info.chunk_number - 1) {
  part = (ulnode->file_info.file_size - chunk_number * 512000) / 1024;
  if ((ulnode->file_info.file_size - chunk_number * 512000) % 1024) {
   part ++;
  }
 } else {
  part = 512000 / 1024;
 }
 return part;
}

canonical_original_code:
int get_part_number(int chunk_number, struct transfer_node *ulnode) {
 int part;
 if (chunk_number == ulnode->file_info.chunk_number - 1) {
  part = (ulnode->file_info.file_size - chunk_number * 512000) / 1024;
  if ((ulnode->file_info.file_size - chunk_number * 512000) % 1024) {
   part ++;
  }
 } else {
  part = 512000 / 1024;
 }
 return part;
}

variable_types: part int

user_defined_types: struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; }


decompiled_code:
__int64 __fastcall send_chunk(int fd, int a2, unsigned int *a3)
{
int v4; // eax
int v5; // er12
int i; // ebp
int v8; // eax
int v9; // ebx
_WORD v10[5]; // [rsp+0h] [rbp-438h] BYREF
char buf[1070]; // [rsp+Ah] [rbp-42Eh] BYREF

new_packet(v10, &unk_BDC, (unsigned __int16)a2, 0LL, 0LL, 1LL);
lseek(fd, 512000 * a2, 0);
v4 = get_part_number(a2, (__int64)a3);
if ( v4 <= 0 )
return 0LL;
v5 = v4;
for ( i = 0; i != v5; ++i )
{
v8 = read(fd, buf, 0x400uLL);
v9 = v8;
if ( v8 < 0 )
{
fwrite("uploader_func error - read failed\n", 1uLL, 0x22uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !v8 )
{
fwrite("send_part error - invalid part number\n", 1uLL, 0x26uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( v5 - 1 == i )
{
unset_nextchunk_flag(v10);
}
else
{
set_nextchunk_flag(v10);
if ( v9 != 1024 )
{
fwrite("uploader_func error - internal error\n", 1uLL, 0x25uLL, stderr);
return 0xFFFFFFFFLL;
}
}
v10[4] = v9;
if ( (int)send_packet_tcp(*a3, v10) < 0 )
{
fwrite("uploader_func error - send_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
return 0xFFFFFFFFLL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, int a2, unsigned int *a3)
{
int v4; // eax
int v5; // er12
int i; // ebp
int v8; // eax
int v9; // ebx
_WORD v10[5]; // [rsp+0h] [rbp-438h] BYREF
char buf[1070]; // [rsp+Ah] [rbp-42Eh] BYREF

new_packet(v10, &unk_BDC, (unsigned __int16)a2, 0LL, 0LL, 1LL);
lseek(fd, 512000 * a2, 0);
v4 = func1(a2, (__int64)a3);
if ( v4 <= 0 )
return 0LL;
v5 = v4;
for ( i = 0; i != v5; ++i )
{
v8 = read(fd, buf, 0x400uLL);
v9 = v8;
if ( v8 < 0 )
{
fwrite("uploader_func error - read failed\n", 1uLL, 0x22uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !v8 )
{
fwrite("send_part error - invalid part number\n", 1uLL, 0x26uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( v5 - 1 == i )
{
unset_nextchunk_flag(v10);
}
else
{
set_nextchunk_flag(v10);
if ( v9 != 1024 )
{
fwrite("uploader_func error - internal error\n", 1uLL, 0x25uLL, stderr);
return 0xFFFFFFFFLL;
}
}
v10[4] = v9;
if ( (int)send_packet_tcp(*a3, v10) < 0 )
{
fwrite("uploader_func error - send_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
return 0xFFFFFFFFLL;
}
}
return 0LL;
}

original_code:
int send_chunk(int fd, int chunk_index, const struct transfer_node *ulnode) {
 struct packet send_pck;
 int j, nread;
 int part_number;
 new_packet(&send_pck, "DAT", chunk_index, ((void *)0), 0, 1);
 lseek(fd, chunk_index * 512000, 0);
 part_number = get_part_number(chunk_index, ulnode);
 for (j = 0; j < part_number; j ++) {
  if ((nread = read(fd, send_pck.data, 1024)) < 0) {
   fprintf(stderr, "uploader_func error - read failed\n");
   return -1;
  } else if (nread == 0) {
   fprintf(stderr, "send_part error - invalid part number\n");
   return -1;
  }
  if (j == part_number - 1) {
   unset_nextchunk_flag(&send_pck);
  } else {
   set_nextchunk_flag(&send_pck);
   if (nread != 1024) {
    fprintf(stderr, "uploader_func error - internal error\n");
    return -1;
   }
  }
  send_pck.data_len = nread;
  if (send_packet_tcp(ulnode->socksd, &send_pck) < 0) {
   fprintf(stderr, "uploader_func error - send_packet_tcp failed\n");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int send_chunk(int fd, int chunk_index, struct transfer_node *ulnode) {
 struct packet send_pck;
 int j; int nread;
 int part_number;
 new_packet(&send_pck, "DAT", chunk_index, ((void *)0), 0, 1);
 lseek(fd, chunk_index * 512000, 0);
 part_number = get_part_number(chunk_index, ulnode);
 for (j = 0; j < part_number; j ++) {
  if ((nread = read(fd, send_pck.data, 1024)) < 0) {
   fprintf(stderr, "uploader_func error - read failed\n");
   return -1;
  } else if (nread == 0) {
   fprintf(stderr, "send_part error - invalid part number\n");
   return -1;
  }
  if (j == part_number - 1) {
   unset_nextchunk_flag(&send_pck);
  } else {
   set_nextchunk_flag(&send_pck);
   if (nread != 1024) {
    fprintf(stderr, "uploader_func error - internal error\n");
    return -1;
   }
  }
  send_pck.data_len = nread;
  if (send_packet_tcp(ulnode->socksd, &send_pck) < 0) {
   fprintf(stderr, "uploader_func error - send_packet_tcp failed\n");
   return -1;
  }
 }
 return 0;
}

variable_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; }

variable_types: j int

variable_types: nread int


original_code:
int create_infofile_str(char *data, const unsigned char *md5, const struct transfer_node *ulnode) {
 int offset;
 strcpy(data, ulnode->file_info.filename);
 offset = strlen(ulnode->file_info.filename);
 data[offset] = 0;
 offset ++;
 memcpy(data + offset, md5, 16);
 offset += 16;
 memcpy(data + offset, (char *)&ulnode->file_info.file_size, sizeof(ulnode->file_info.file_size));
 offset += sizeof(ulnode->file_info.file_size);
 memcpy(data + offset, (char *)&ulnode->file_info.chunk_number, sizeof(ulnode->file_info.chunk_number));
 offset += sizeof(ulnode->file_info.chunk_number);
 return offset;
}

canonical_original_code:
int create_infofile_str(char *data, unsigned char *md5, struct transfer_node *ulnode) {
 int offset;
 strcpy(data, ulnode->file_info.filename);
 offset = strlen(ulnode->file_info.filename);
 data[offset] = 0;
 offset ++;
 memcpy(data + offset, md5, 16);
 offset += 16;
 memcpy(data + offset, (char *)&ulnode->file_info.file_size, sizeof(ulnode->file_info.file_size));
 offset += sizeof(ulnode->file_info.file_size);
 memcpy(data + offset, (char *)&ulnode->file_info.chunk_number, sizeof(ulnode->file_info.chunk_number));
 offset += sizeof(ulnode->file_info.chunk_number);
 return offset;
}

variable_types:
data
char *

variable_types: offset int


decompiled_code:
void __fastcall __noreturn uploader_func(void *a1)
{
pthread_mutex_t *v2; // rbp
char *v3; // rax
char *v4; // rax
char *v5; // rax
int v6; // eax
int v7; // eax
int v8; // eax
int v9; // eax
int v10; // eax
int v11; // er12
int v12; // eax
char v13[16]; // [rsp+0h] [rbp-C68h] BYREF
char file[1024]; // [rsp+10h] [rbp-C58h] BYREF
char v15[4]; // [rsp+410h] [rbp-858h] BYREF
unsigned __int16 v16; // [rsp+414h] [rbp-854h]
__int16 v17; // [rsp+418h] [rbp-850h]
__m128i v18[64]; // [rsp+41Ah] [rbp-84Eh] BYREF
char v19[8]; // [rsp+820h] [rbp-448h] BYREF
__int16 v20; // [rsp+828h] [rbp-440h]
char v21[1086]; // [rsp+82Ah] [rbp-43Eh] BYREF

v2 = (pthread_mutex_t *)((char *)a1 + 16);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = pthread_mutex_lock(v2);
if ( v6 )
{
v3 = strerror(v6);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v3);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v7 = pthread_cond_wait((pthread_cond_t *)((char *)a1 + 56), v2);
if ( v7 )
{
v4 = strerror(v7);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v8 = pthread_mutex_unlock(v2);
if ( v8 )
{
v5 = strerror(v8);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
sprintf(v13, "GETU %d", *((unsigned int *)a1 + 103));
v9 = recv_packet_tcp(*((unsigned int *)a1 + 26), v15);
if ( v9 >= 0 )
break;
fwrite("uploader_func errror - recv_packet_tcp failed\n", 1uLL, 0x2EuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
{
LABEL_12:
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
continue;
}
}
if ( v9 )
break;
fwrite("uploader_func errror - connection closed by peer\n", 1uLL, 0x31uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
puts("Ricevuto get");
if ( !memcmp(v15, "GET", 3uLL) )
break;
fwrite("packet not expected\n", 1uLL, 0x14uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
if ( v17 == 16 )
break;
fwrite("bad packet format\n", 1uLL, 0x12uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
v10 = get_filepath(file, (char *)a1 + 124, v18);
if ( v10 >= 0 )
break;
fwrite("uploader_func error - get_filepath failed\n", 1uLL, 0x2AuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
if ( v10 != 1 )
break;
fwrite("uploader_func error - file richiesto non presente\n", 1uLL, 0x32uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
v11 = open(file, 0);
if ( v11 >= 0 )
break;
perror("uploader_func error - open failed\n");
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
new_get_packet(v19, 0LL, 0LL, 0LL, 1LL);
get_file_info((unsigned int)v11, (__int64)a1 + 104);
v20 = create_infofile_str(v21, v18, (__int64)a1 + 104);
*(__m128i *)((char *)a1 + 379) = _mm_loadu_si128(v18);
print_file_info((char *)a1 + 124);
if ( (int)send_packet_tcp(*((unsigned int *)a1 + 26), v19) >= 0 )
break;
fwrite("uploader_func error - send_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
}
printf("Iniziato upload del file %s\n", (const char *)a1 + 124);
while ( 1 )
{
v12 = recv_packet_tcp(*((unsigned int *)a1 + 26), v15);
if ( v12 <= 0 )
break;
if ( !memcmp(v15, "GET", 3uLL) )
{
if ( (int)send_chunk(v11, v16, (unsigned int *)a1 + 26) < 0 )
{
fwrite("uploader_func error - send_chunk failed\n", 1uLL, 0x28uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_56;
}
}
else
{
fwrite("uploader_func error - packet not expected\n", 1uLL, 0x2AuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
LABEL_56:
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
}
}
if ( v12 < 0 )
fwrite("uploader_func error - recv_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
sprintf(v13, "TRM %d\n", *((unsigned int *)a1 + 103));
write_on_pipe(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13);
printf("Completato upload del file %s\n", (const char *)a1 + 124);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
pthread_mutex_t *v2; // rbp
char *v3; // rax
char *v4; // rax
char *v5; // rax
int v6; // eax
int v7; // eax
int v8; // eax
int v9; // eax
int v10; // eax
int v11; // er12
int v12; // eax
char v13[16]; // [rsp+0h] [rbp-C68h] BYREF
char file[1024]; // [rsp+10h] [rbp-C58h] BYREF
char v15[4]; // [rsp+410h] [rbp-858h] BYREF
unsigned __int16 v16; // [rsp+414h] [rbp-854h]
__int16 v17; // [rsp+418h] [rbp-850h]
__m128i v18[64]; // [rsp+41Ah] [rbp-84Eh] BYREF
char v19[8]; // [rsp+820h] [rbp-448h] BYREF
__int16 v20; // [rsp+828h] [rbp-440h]
char v21[1086]; // [rsp+82Ah] [rbp-43Eh] BYREF

v2 = (pthread_mutex_t *)((char *)a1 + 16);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = pthread_mutex_lock(v2);
if ( v6 )
{
v3 = strerror(v6);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v3);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v7 = pthread_cond_wait((pthread_cond_t *)((char *)a1 + 56), v2);
if ( v7 )
{
v4 = strerror(v7);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v8 = pthread_mutex_unlock(v2);
if ( v8 )
{
v5 = strerror(v8);
fprintf(stderr, "uploader_func error - can't release lock: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
sprintf(v13, "GETU %d", *((unsigned int *)a1 + 103));
v9 = recv_packet_tcp(*((unsigned int *)a1 + 26), v15);
if ( v9 >= 0 )
break;
fwrite("uploader_func errror - recv_packet_tcp failed\n", 1uLL, 0x2EuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
{
LABEL_12:
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
continue;
}
}
if ( v9 )
break;
fwrite("uploader_func errror - connection closed by peer\n", 1uLL, 0x31uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
puts("Ricevuto get");
if ( !memcmp(v15, "GET", 3uLL) )
break;
fwrite("packet not expected\n", 1uLL, 0x14uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
if ( v17 == 16 )
break;
fwrite("bad packet format\n", 1uLL, 0x12uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
v10 = get_filepath(file, (char *)a1 + 124, v18);
if ( v10 >= 0 )
break;
fwrite("uploader_func error - get_filepath failed\n", 1uLL, 0x2AuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
if ( v10 != 1 )
break;
fwrite("uploader_func error - file richiesto non presente\n", 1uLL, 0x32uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
v11 = open(file, 0);
if ( v11 >= 0 )
break;
perror("uploader_func error - open failed\n");
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_12;
}
new_get_packet(v19, 0LL, 0LL, 0LL, 1LL);
func0((unsigned int)v11, (__int64)a1 + 104);
v20 = func3(v21, v18, (__int64)a1 + 104);
*(__m128i *)((char *)a1 + 379) = _mm_loadu_si128(v18);
print_file_info((char *)a1 + 124);
if ( (int)send_packet_tcp(*((unsigned int *)a1 + 26), v19) >= 0 )
break;
fwrite("uploader_func error - send_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
}
printf("Iniziato upload del file %s\n", (const char *)a1 + 124);
while ( 1 )
{
v12 = recv_packet_tcp(*((unsigned int *)a1 + 26), v15);
if ( v12 <= 0 )
break;
if ( !memcmp(v15, "GET", 3uLL) )
{
if ( (int)func2(v11, v16, (unsigned int *)a1 + 26) < 0 )
{
fwrite("uploader_func error - send_chunk failed\n", 1uLL, 0x28uLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
goto LABEL_56;
}
}
else
{
fwrite("uploader_func error - packet not expected\n", 1uLL, 0x2AuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
if ( (int)write_err(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13) < 0 )
LABEL_56:
fwrite("uploader_func error - write_err failed\n", 1uLL, 0x27uLL, stderr);
}
}
if ( v12 < 0 )
fwrite("uploader_func error - recv_packet_tcp failed\n", 1uLL, 0x2DuLL, stderr);
if ( (int)close_sock(*((unsigned int *)a1 + 26)) < 0 )
perror("uploader_func error - close_sock failed");
if ( close(v11) < 0 )
perror("uploader_func error - close failed");
sprintf(v13, "TRM %d\n", *((unsigned int *)a1 + 103));
write_on_pipe(*((unsigned int *)a1 + 101), *((_QWORD *)a1 + 1), v13);
printf("Completato upload del file %s\n", (const char *)a1 + 124);
}
}

original_code:
void uploader_func(void *args) {
 struct uploader_info *ulinfo = (struct uploader_info *)args;
 struct packet send_pck, recv_pck;
 int fd;
 int rc;
 char buf[1024];
 char str[16];
 int i;
 while (1) {
  if ((rc = pthread_mutex_lock(&ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if ((rc = pthread_cond_wait(&ulinfo->th_cond, &ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if ((rc = pthread_mutex_unlock(&ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  sprintf(str, "GETU %d", ulinfo->index);
  if ((i = recv_packet_tcp(ulinfo->ulnode.socksd, &recv_pck)) < 0) {
   fprintf(stderr, "uploader_func errror - recv_packet_tcp failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  } else if (i == 0) {
   fprintf(stderr, "uploader_func errror - connection closed by peer\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  printf("Ricevuto get\n");
  if (strncmp(recv_pck.cmd, "GET", 3)) {
   fprintf(stderr, "packet not expected\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if (recv_pck.data_len != 16) {
   fprintf(stderr, "bad packet format\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if ((i = get_filepath(buf, ulinfo->ulnode.file_info.filename, (const unsigned char *)recv_pck.data)) < 0) {
   fprintf(stderr, "uploader_func error - get_filepath failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  } else if (i == 1) {
   fprintf(stderr, "uploader_func error - file richiesto non presente\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if ((fd = open(buf, 00)) < 0) {
   perror("uploader_func error - open failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  new_get_packet(&send_pck, 0, ((void *)0), 0, 1);
  get_file_info(fd, &ulinfo->ulnode);
  send_pck.data_len = create_infofile_str(send_pck.data, (const unsigned char *)recv_pck.data, &ulinfo->ulnode);
  memcpy(ulinfo->ulnode.file_info.md5, recv_pck.data, 16);
  print_file_info(&ulinfo->ulnode.file_info);
  if (send_packet_tcp(ulinfo->ulnode.socksd, &send_pck) < 0) {
   fprintf(stderr, "uploader_func error - send_packet_tcp failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   if (close(fd) < 0) {
    perror("uploader_func error - close failed");
   }
   continue;
  }
  printf("Iniziato upload del file %s\n", ulinfo->ulnode.file_info.filename);
  while ((i = recv_packet_tcp(ulinfo->ulnode.socksd, &recv_pck)) > 0) {
   if (strncmp(recv_pck.cmd, "GET", 3)) {
    fprintf(stderr, "uploader_func error - packet not expected\n");
    if (close_sock(ulinfo->ulnode.socksd) < 0) {
     perror("uploader_func error - close_sock failed");
    }
    if (close(fd) < 0) {
     perror("uploader_func error - close failed");
    }
    if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
     fprintf(stderr, "uploader_func error - write_err failed\n");
    }
    continue;
   }
   if (send_chunk(fd, recv_pck.index, &ulinfo->ulnode) < 0) {
    fprintf(stderr, "uploader_func error - send_chunk failed\n");
    if (close_sock(ulinfo->ulnode.socksd) < 0) {
     perror("uploader_func error - close_sock failed");
    }
    if (close(fd) < 0) {
     perror("uploader_func error - close failed");
    }
    if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
     fprintf(stderr, "uploader_func error - write_err failed\n");
    }
    continue;
   }
  }
  if (i < 0) {
   fprintf(stderr, "uploader_func error - recv_packet_tcp failed\n");
  }
  if (close_sock(ulinfo->ulnode.socksd) < 0) {
   perror("uploader_func error - close_sock failed");
  }
  if (close(fd) < 0) {
   perror("uploader_func error - close failed");
  }
  sprintf(str, "TRM %d\n", ulinfo->index);
  write_on_pipe(ulinfo->th_pipe, ulinfo->pipe_mutex, str);
  printf("Completato upload del file %s\n", ulinfo->ulnode.file_info.filename);
 }
}

canonical_original_code:
void uploader_func(void *args) {
 struct uploader_info *ulinfo = (struct uploader_info *)args;
 struct packet send_pck; struct packet recv_pck;
 int fd;
 int rc;
 char buf[1024];
 char str[16];
 int i;
 while (1) {
  if ((rc = pthread_mutex_lock(&ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if ((rc = pthread_cond_wait(&ulinfo->th_cond, &ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if ((rc = pthread_mutex_unlock(&ulinfo->th_mutex)) != 0) {
   fprintf(stderr, "uploader_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  sprintf(str, "GETU %d", ulinfo->index);
  if ((i = recv_packet_tcp(ulinfo->ulnode.socksd, &recv_pck)) < 0) {
   fprintf(stderr, "uploader_func errror - recv_packet_tcp failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  } else if (i == 0) {
   fprintf(stderr, "uploader_func errror - connection closed by peer\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  printf("Ricevuto get\n");
  if (strncmp(recv_pck.cmd, "GET", 3)) {
   fprintf(stderr, "packet not expected\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if (recv_pck.data_len != 16) {
   fprintf(stderr, "bad packet format\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if ((i = get_filepath(buf, ulinfo->ulnode.file_info.filename, (unsigned char *)recv_pck.data)) < 0) {
   fprintf(stderr, "uploader_func error - get_filepath failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  } else if (i == 1) {
   fprintf(stderr, "uploader_func error - file richiesto non presente\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  if ((fd = open(buf, 00)) < 0) {
   perror("uploader_func error - open failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   continue;
  }
  new_get_packet(&send_pck, 0, ((void *)0), 0, 1);
  get_file_info(fd, &ulinfo->ulnode);
  send_pck.data_len = create_infofile_str(send_pck.data, (unsigned char *)recv_pck.data, &ulinfo->ulnode);
  memcpy(ulinfo->ulnode.file_info.md5, recv_pck.data, 16);
  print_file_info(&ulinfo->ulnode.file_info);
  if (send_packet_tcp(ulinfo->ulnode.socksd, &send_pck) < 0) {
   fprintf(stderr, "uploader_func error - send_packet_tcp failed\n");
   if (close_sock(ulinfo->ulnode.socksd) < 0) {
    perror("uploader_func error - close_sock failed");
   }
   if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
    fprintf(stderr, "uploader_func error - write_err failed\n");
   }
   if (close(fd) < 0) {
    perror("uploader_func error - close failed");
   }
   continue;
  }
  printf("Iniziato upload del file %s\n", ulinfo->ulnode.file_info.filename);
  while ((i = recv_packet_tcp(ulinfo->ulnode.socksd, &recv_pck)) > 0) {
   if (strncmp(recv_pck.cmd, "GET", 3)) {
    fprintf(stderr, "uploader_func error - packet not expected\n");
    if (close_sock(ulinfo->ulnode.socksd) < 0) {
     perror("uploader_func error - close_sock failed");
    }
    if (close(fd) < 0) {
     perror("uploader_func error - close failed");
    }
    if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
     fprintf(stderr, "uploader_func error - write_err failed\n");
    }
    continue;
   }
   if (send_chunk(fd, recv_pck.index, &ulinfo->ulnode) < 0) {
    fprintf(stderr, "uploader_func error - send_chunk failed\n");
    if (close_sock(ulinfo->ulnode.socksd) < 0) {
     perror("uploader_func error - close_sock failed");
    }
    if (close(fd) < 0) {
     perror("uploader_func error - close failed");
    }
    if (write_err(ulinfo->th_pipe, ulinfo->pipe_mutex, str) < 0) {
     fprintf(stderr, "uploader_func error - write_err failed\n");
    }
    continue;
   }
  }
  if (i < 0) {
   fprintf(stderr, "uploader_func error - recv_packet_tcp failed\n");
  }
  if (close_sock(ulinfo->ulnode.socksd) < 0) {
   perror("uploader_func error - close_sock failed");
  }
  if (close(fd) < 0) {
   perror("uploader_func error - close failed");
  }
  sprintf(str, "TRM %d\n", ulinfo->index);
  write_on_pipe(ulinfo->th_pipe, ulinfo->pipe_mutex, str);
  printf("Completato upload del file %s\n", ulinfo->ulnode.file_info.filename);
 }
}

variable_types:
args
void *

variable_types: struct uploader_info { unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *pipe_mutex; union pthread_mutex_t th_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } th_cond; struct transfer_node { int socksd; 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]; } addrto; struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; } file_info; } ulnode; int th_pipe; int sleeping; int index; } *

variable_types:
buf
char [1024]

variable_types:
str
char [16]

variable_types: i int

user_defined_types: struct uploader_info { unsigned long thread; union pthread_mutex_t *pipe_mutex; union pthread_mutex_t th_mutex; union pthread_cond_t th_cond; struct transfer_node ulnode; int th_pipe; int sleeping; int index; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }


decompiled_code:
int __fastcall uploader_run(void *arg)
{
int result; // eax
char *v2; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

pthread_attr_init(&v3);
pthread_attr_setdetachstate(&v3, 0);
result = pthread_create((pthread_t *)arg, &v3, (void *(*)(void *))uploader_func, arg);
if ( result )
{
v2 = strerror(result);
fprintf(stderr, "uploader_run error - can't create thread: %s\n", v2);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(void *arg)
{
int result; // eax
char *v2; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

pthread_attr_init(&v3);
pthread_attr_setdetachstate(&v3, 0);
result = pthread_create((pthread_t *)arg, &v3, (void *(*)(void *))uploader_func, arg);
if ( result )
{
v2 = strerror(result);
fprintf(stderr, "uploader_run error - can't create thread: %s\n", v2);
result = -1;
}
return result;
}

original_code:
int uploader_run(struct uploader_info *ulinfo) {
 int rc;
 pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 if ((rc = pthread_create(&ulinfo->thread, &attr, (void *)uploader_func, (void *)ulinfo)) != 0) {
  fprintf(stderr, "uploader_run error - can't create thread: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int uploader_run(struct uploader_info *ulinfo) {
 int rc;
 union pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 if ((rc = pthread_create(&ulinfo->thread, &attr, (void *)uploader_func, (void *)ulinfo)) != 0) {
  fprintf(stderr, "uploader_run error - can't create thread: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

variable_types: rc int

variable_types: union pthread_attr_t { char __size[56]; long __align; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


decompiled_code:
__int64 __fastcall uploader_init(int a1, int a2, __int64 a3)
{
unsigned int v3; // er15
__int64 v7; // rbx
int v8; // ebp
__int64 v9; // rdi
int v10; // eax
int v11; // eax
_DWORD *v12; // rdi
char *v13; // rax
char *v14; // rax

v3 = 0;
if ( !ul_pool )
{
ul_pool = (__int64)malloc(416LL * a1);
if ( a1 <= 0 )
{
v3 = 0;
}
else
{
v7 = 0LL;
v8 = 0;
while ( 1 )
{
v9 = ul_pool + v7;
*(_DWORD *)(v9 + 404) = a2;
*(_QWORD *)(v9 + 8) = a3;
v10 = pthread_cond_init((pthread_cond_t *)(v9 + 56), 0LL);
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "uploader_init error - pthread_cond_init failed: %s\n", v13);
return (unsigned int)-1;
}
v11 = pthread_mutex_init((pthread_mutex_t *)(ul_pool + v7 + 16), 0LL);
v3 = v11;
if ( v11 )
{
v14 = strerror(v11);
fprintf(stderr, "uploader_init error - pthread_mutex_init failed: %s\n", v14);
return (unsigned int)-1;
}
v12 = (_DWORD *)(ul_pool + v7);
v12[102] = 1;
v12[103] = v8;
if ( uploader_run(v12) < 0 )
break;
++v8;
v7 += 416LL;
if ( a1 == v8 )
return v3;
}
fwrite("uploader_init error - uploader_run failed\n", 1uLL, 0x2AuLL, stderr);
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2, __int64 a3)
{
unsigned int v3; // er15
__int64 v7; // rbx
int v8; // ebp
__int64 v9; // rdi
int v10; // eax
int v11; // eax
_DWORD *v12; // rdi
char *v13; // rax
char *v14; // rax

v3 = 0;
if ( !ul_pool )
{
ul_pool = (__int64)malloc(416LL * a1);
if ( a1 <= 0 )
{
v3 = 0;
}
else
{
v7 = 0LL;
v8 = 0;
while ( 1 )
{
v9 = ul_pool + v7;
*(_DWORD *)(v9 + 404) = a2;
*(_QWORD *)(v9 + 8) = a3;
v10 = pthread_cond_init((pthread_cond_t *)(v9 + 56), 0LL);
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "uploader_init error - pthread_cond_init failed: %s\n", v13);
return (unsigned int)-1;
}
v11 = pthread_mutex_init((pthread_mutex_t *)(ul_pool + v7 + 16), 0LL);
v3 = v11;
if ( v11 )
{
v14 = strerror(v11);
fprintf(stderr, "uploader_init error - pthread_mutex_init failed: %s\n", v14);
return (unsigned int)-1;
}
v12 = (_DWORD *)(ul_pool + v7);
v12[102] = 1;
v12[103] = v8;
if ( func5(v12) < 0 )
break;
++v8;
v7 += 416LL;
if ( a1 == v8 )
return v3;
}
fwrite("uploader_init error - uploader_run failed\n", 1uLL, 0x2AuLL, stderr);
v3 = -1;
}
}
return v3;
}

original_code:
int uploader_init(int nthread, int th_pipe, pthread_mutex_t *mutex) {
 int i, rc;
 if (ul_pool != ((void *)0)) {
  return 0;
 }
 ul_pool = (struct uploader_info *)malloc(sizeof(struct uploader_info) * nthread);
 for (i = 0; i < nthread; i ++) {
  ul_pool[i].th_pipe = th_pipe;
  ul_pool[i].pipe_mutex = mutex;
  if ((rc = pthread_cond_init(&ul_pool[i].th_cond, ((void *)0))) != 0) {
   fprintf(stderr, "uploader_init error - pthread_cond_init failed: %s\n", strerror(rc));
   return -1;
  }
  if ((rc = pthread_mutex_init(&ul_pool[i].th_mutex, ((void *)0))) != 0) {
   fprintf(stderr, "uploader_init error - pthread_mutex_init failed: %s\n", strerror(rc));
   return -1;
  }
  ul_pool[i].sleeping = 1;
  ul_pool[i].index = i;
  if (uploader_run(&ul_pool[i]) < 0) {
   fprintf(stderr, "uploader_init error - uploader_run failed\n");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int uploader_init(int nthread, int th_pipe, union pthread_mutex_t *mutex) {
 int i; int rc;
 if (ul_pool != ((void *)0)) {
  return 0;
 }
 ul_pool = (struct uploader_info *)malloc(sizeof(struct uploader_info) * nthread);
 for (i = 0; i < nthread; i ++) {
  ul_pool[i].th_pipe = th_pipe;
  ul_pool[i].pipe_mutex = mutex;
  if ((rc = pthread_cond_init(&ul_pool[i].th_cond, ((void *)0))) != 0) {
   fprintf(stderr, "uploader_init error - pthread_cond_init failed: %s\n", strerror(rc));
   return -1;
  }
  if ((rc = pthread_mutex_init(&ul_pool[i].th_mutex, ((void *)0))) != 0) {
   fprintf(stderr, "uploader_init error - pthread_mutex_init failed: %s\n", strerror(rc));
   return -1;
  }
  ul_pool[i].sleeping = 1;
  ul_pool[i].index = i;
  if (uploader_run(&ul_pool[i]) < 0) {
   fprintf(stderr, "uploader_init error - uploader_run failed\n");
   return -1;
  }
 }
 return 0;
}

variable_types: nthread int

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *


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

result = a1 / 64;
*(_QWORD *)(a2 + 8 * result) |= 1LL << (a1 % 64);
if ( maxd < a1 )
maxd = a1;
return result;
}

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

result = a1 / 64;
*(_QWORD *)(a2 + 8 * result) |= 1LL << (a1 % 64);
if ( maxd < a1 )
maxd = a1;
return result;
}

original_code:
void add_fd(int fd, fd_set *fdset) {
 ((void) (((fdset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (fd > maxd) {
  maxd = fd;
 }
}

canonical_original_code:
void add_fd(int fd, struct fd_set *fdset) {
 ((void) (((fdset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (fd > maxd) {
  maxd = fd;
 }
}

variable_types: fd int

variable_types: struct fd_set { long __fds_bits[-1]; } *


writen

decompiled_code:
size_t __fastcall writen(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( v6 >= 0 )
return -1LL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func1(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( v6 >= 0 )
return -1LL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

original_code:
ssize_t writen(int fd, const void *buf, size_t n)
{
  size_t nleft;
  ssize_t nwritten;
  const char *ptr;
  ptr = buf;
  nleft = n;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) <= 0) {
       if ((nwritten < 0) && ((*__errno_location ()) == 4)) nwritten = 0;
       else return(-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return(n-nleft);
}

canonical_original_code:
long writen(int fd, void *buf, unsigned long n)
{
  unsigned long nleft;
  long nwritten;
  char *ptr;
  ptr = buf;
  nleft = n;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) <= 0) {
       if ((nwritten < 0) && ((*__errno_location ()) == 4)) nwritten = 0;
       else return(-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return(n-nleft);
}

variable_types:
buf
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nwritten long

variable_types:
ptr
char *

return_type: long


readline

decompiled_code:
__int64 __fastcall readline(int fd, _BYTE *a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
char v7; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v5 = 1;
}
else
{
v5 = 1;
while ( 1 )
{
v6 = read(fd, buf, 1uLL);
if ( v6 != 1 )
break;
++a2;
v7 = buf[0];
*(a2 - 1) = buf[0];
if ( v7 != 10 && a3 != ++v5 )
continue;
goto LABEL_6;
}
if ( v6 )
return (unsigned int)-1;
if ( v5 == 1 )
return 0;
}
LABEL_6:
*a2 = 0;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, _BYTE *a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
char v7; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v5 = 1;
}
else
{
v5 = 1;
while ( 1 )
{
v6 = read(fd, buf, 1uLL);
if ( v6 != 1 )
break;
++a2;
v7 = buf[0];
*(a2 - 1) = buf[0];
if ( v7 != 10 && a3 != ++v5 )
continue;
goto LABEL_6;
}
if ( v6 )
return (unsigned int)-1;
if ( v5 == 1 )
return 0;
}
LABEL_6:
*a2 = 0;
return v5;
}

original_code:
int readline(int fd, void *vptr, int maxlen)
{
  int n, rc;
  char c, *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\n') break;
   }
   else
      if (rc == 0) {
   if (n == 1) return(0);
   else break;
      }
      else return(-1);
  }
  *ptr = 0;
  return(n);
}

canonical_original_code:
int readline(int fd, void *vptr, int maxlen)
{
  int n; int rc;
  char c; char *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\n') break;
   }
   else
      if (rc == 0) {
   if (n == 1) return(0);
   else break;
      }
      else return(-1);
  }
  *ptr = 0;
  return(n);
}

variable_types:
vptr
void *

variable_types: maxlen int

variable_types: n int

variable_types: c char


readstr

decompiled_code:
__int64 __fastcall readstr(int fd, _BYTE *a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
char v7; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v5 = 1;
}
else
{
v5 = 1;
while ( 1 )
{
v6 = read(fd, buf, 1uLL);
if ( v6 != 1 )
break;
++a2;
v7 = buf[0];
*(a2 - 1) = buf[0];
if ( v7 )
{
if ( a3 != ++v5 )
continue;
}
goto LABEL_6;
}
if ( v6 )
return (unsigned int)-1;
if ( v5 == 1 )
return 0;
}
LABEL_6:
*a2 = 0;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, _BYTE *a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
char v7; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v5 = 1;
}
else
{
v5 = 1;
while ( 1 )
{
v6 = read(fd, buf, 1uLL);
if ( v6 != 1 )
break;
++a2;
v7 = buf[0];
*(a2 - 1) = buf[0];
if ( v7 )
{
if ( a3 != ++v5 )
continue;
}
goto LABEL_6;
}
if ( v6 )
return (unsigned int)-1;
if ( v5 == 1 )
return 0;
}
LABEL_6:
*a2 = 0;
return v5;
}

original_code:
int readstr(int fd, void *vptr, int maxlen)
{
  int n, rc;
  char c, *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\0') break;
   }
   else
      if (rc == 0) {
   if (n == 1) return(0);
   else break;
      }
      else return(-1);
  }
  *ptr = 0;
  return(n);
}

canonical_original_code:
int readstr(int fd, void *vptr, int maxlen)
{
  int n; int rc;
  char c; char *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\0') break;
   }
   else
      if (rc == 0) {
   if (n == 1) return(0);
   else break;
      }
      else return(-1);
  }
  *ptr = 0;
  return(n);
}


ltob

decompiled_code:
__int64 __fastcall ltob(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // [rsp+0h] [rbp-8h]

v3 = a2;
for ( result = 0LL; result != 8; ++result )
*(_BYTE *)(a1 + result) = *((_BYTE *)&v3 + result);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // [rsp+0h] [rbp-8h]

v3 = a2;
for ( result = 0LL; result != 8; ++result )
*(_BYTE *)(a1 + result) = *((_BYTE *)&v3 + result);
return result;
}

original_code:
void ltob(char *dest, long src) {
 char *it = (char *)&src;
 int i;
 for (i = 0; i < sizeof(long); i ++) {
  dest[i] = it[i];
 }
}

variable_types:
dest
char *

variable_types: src long

variable_types:
it
char *


stob

decompiled_code:
__int64 __fastcall stob(_WORD *a1, __int16 a2)
{
*a1 = a2;
return HIBYTE(a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(_WORD *a1, __int16 a2)
{
*a1 = a2;
return HIBYTE(a2);
}

original_code:
void stob(char *dest, short src) {
 char *it = (char *)&src;
 int i;
 for (i = 0; i < sizeof(short); i ++) {
  dest[i] = it[i];
 }
}

variable_types: src short

variable_types: i int


btol

decompiled_code:
__int64 __fastcall btol(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
unsigned long btol(const char *str) {
 unsigned long *ret = (unsigned long *)str;
 return *ret;
}

canonical_original_code:
unsigned long btol(char *str) {
 unsigned long *ret = (unsigned long *)str;
 return *ret;
}

variable_types:
str
char *

variable_types:
ret
unsigned long *

return_type: unsigned long


btos

decompiled_code:
__int64 __fastcall btos(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 *a1)
{
return *a1;
}

original_code:
unsigned short btos(const char *str) {
 unsigned short *ret = (unsigned short *)str;
 return *ret;
}

canonical_original_code:
unsigned short btos(char *str) {
 unsigned short *ret = (unsigned short *)str;
 return *ret;
}

variable_types:
ret
unsigned short *

return_type: unsigned short