decompiled_code:
__int64 __fastcall fix16_sin_parabola(int a1)
{
int v1; // ebx
unsigned int v2; // eax
int v3; // ebx
int v4; // eax

v1 = fix16_mul(83443LL, (unsigned int)a1);
v2 = fix16_mul(4294940736LL, (unsigned int)a1);
v3 = fix16_mul(v2, (a1 >> 31) ^ (unsigned int)(a1 + (a1 >> 31))) + v1;
v4 = fix16_mul((unsigned int)v3, (v3 >> 31) ^ (unsigned int)(v3 + (v3 >> 31)));
return v3 + (unsigned int)fix16_mul(14746LL, (unsigned int)(v4 - v3));
}

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

v1 = fix16_mul(83443LL, (unsigned int)a1);
v2 = fix16_mul(4294940736LL, (unsigned int)a1);
v3 = fix16_mul(v2, (a1 >> 31) ^ (unsigned int)(a1 + (a1 >> 31))) + v1;
v4 = fix16_mul((unsigned int)v3, (v3 >> 31) ^ (unsigned int)(v3 + (v3 >> 31)));
return v3 + (unsigned int)fix16_mul(14746LL, (unsigned int)(v4 - v3));
}

original_code:
fix16_t fix16_sin_parabola(fix16_t inAngle)
{
 fix16_t abs_inAngle, abs_retval, retval;
 fix16_t mask;
 mask = (inAngle >> (sizeof(fix16_t)*8 -1));
 abs_inAngle = (inAngle + mask) ^ mask;
 retval = fix16_mul(FOUR_DIV_PI, inAngle) + fix16_mul( fix16_mul(_FOUR_DIV_PI2, inAngle), abs_inAngle );
 mask = (retval >> (sizeof(fix16_t)*8 -1));
 abs_retval = (retval + mask) ^ mask;
 retval += fix16_mul(X4_CORRECTION_COMPONENT, fix16_mul(retval, abs_retval) - retval );
 return retval;
}

canonical_original_code:
int fix16_sin_parabola(int inAngle)
{
 int abs_inAngle; int abs_retval; int retval;
 int mask;
 mask = (inAngle >> (sizeof(fix16_t)*8 -1));
 abs_inAngle = (inAngle + mask) ^ mask;
 retval = fix16_mul(FOUR_DIV_PI, inAngle) + fix16_mul( fix16_mul(_FOUR_DIV_PI2, inAngle), abs_inAngle );
 mask = (retval >> (sizeof(fix16_t)*8 -1));
 abs_retval = (retval + mask) ^ mask;
 retval += fix16_mul(X4_CORRECTION_COMPONENT, fix16_mul(retval, abs_retval) - retval );
 return retval;
}

variable_types: inAngle int

variable_types: retval int

variable_types: mask int


repo: eceforge/vitasign

decompiled_code:
__int64 __fastcall fix16_sin(int a1)
{
unsigned int v1; // ebx
__int64 v2; // r12
unsigned int v3; // er14
unsigned int v4; // eax
int v5; // er13
int v6; // eax
unsigned int v7; // er13
int v8; // ebx
int v9; // eax
unsigned int v10; // ebx
int v11; // er13
unsigned int v12; // eax
int v13; // er13
int v14; // eax
__int64 result; // rax

v1 = a1 % 411774;
if ( a1 % 411774 <= 205887 )
{
if ( (int)v1 < -205887 )
v1 += 411774;
}
else
{
v1 -= 411774;
}
v2 = (a1 >> 5) & 0xFFF;
if ( fix16_sin_cache_index[v2] == a1 )
return (unsigned int)fix16_sin_cache_value[v2];
v3 = fix16_mul(v1, v1);
v4 = fix16_mul(v1, v3);
v5 = (int)v4 / -6;
v6 = fix16_mul(v4, v3);
v7 = v1 + v5;
v8 = v6 / 120;
v9 = fix16_mul(v6, v3);
v10 = v7 + v8;
v11 = v9 / -5040;
v12 = fix16_mul(v9, v3);
v13 = (int)v12 / 362880 + v10 + v11;
v14 = fix16_mul(v12, v3);
fix16_sin_cache_index[v2] = a1;
result = (unsigned int)(v13 + v14 / -39916800);
fix16_sin_cache_value[v2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
unsigned int v1; // ebx
__int64 v2; // r12
unsigned int v3; // er14
unsigned int v4; // eax
int v5; // er13
int v6; // eax
unsigned int v7; // er13
int v8; // ebx
int v9; // eax
unsigned int v10; // ebx
int v11; // er13
unsigned int v12; // eax
int v13; // er13
int v14; // eax
__int64 result; // rax

v1 = a1 % 411774;
if ( a1 % 411774 <= 205887 )
{
if ( (int)v1 < -205887 )
v1 += 411774;
}
else
{
v1 -= 411774;
}
v2 = (a1 >> 5) & 0xFFF;
if ( fix16_sin_cache_index[v2] == a1 )
return (unsigned int)fix16_sin_cache_value[v2];
v3 = fix16_mul(v1, v1);
v4 = fix16_mul(v1, v3);
v5 = (int)v4 / -6;
v6 = fix16_mul(v4, v3);
v7 = v1 + v5;
v8 = v6 / 120;
v9 = fix16_mul(v6, v3);
v10 = v7 + v8;
v11 = v9 / -5040;
v12 = fix16_mul(v9, v3);
v13 = (int)v12 / 362880 + v10 + v11;
v14 = fix16_mul(v12, v3);
fix16_sin_cache_index[v2] = a1;
result = (unsigned int)(v13 + v14 / -39916800);
fix16_sin_cache_value[v2] = result;
return result;
}

original_code:
fix16_t fix16_sin(fix16_t inAngle) {
 fix16_t tempAngle = inAngle % (fix16_pi << 1);
 if(tempAngle > fix16_pi)
  tempAngle -= (fix16_pi << 1);
 else if(tempAngle < -fix16_pi)
  tempAngle += (fix16_pi << 1);
 fix16_t tempIndex = ((inAngle >> 5) & 0x00000FFF);
 if(_fix16_sin_cache_index[tempIndex] == inAngle)
  return _fix16_sin_cache_value[tempIndex];
 fix16_t tempAngleSq = fix16_mul(tempAngle, tempAngle);
 fix16_t tempOut = tempAngle;
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 6);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut += (tempAngle / 120);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 5040);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut += (tempAngle / 362880);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 39916800);
 _fix16_sin_cache_index[tempIndex] = inAngle;
 _fix16_sin_cache_value[tempIndex] = tempOut;
 return tempOut;
}

canonical_original_code:
int fix16_sin(int inAngle) {
 int tempAngle = inAngle % (fix16_pi << 1);
 if(tempAngle > fix16_pi)
  tempAngle -= (fix16_pi << 1);
 else if(tempAngle < -fix16_pi)
  tempAngle += (fix16_pi << 1);
 int tempIndex = ((inAngle >> 5) & 0x00000FFF);
 if(_fix16_sin_cache_index[tempIndex] == inAngle)
  return _fix16_sin_cache_value[tempIndex];
 int tempAngleSq = fix16_mul(tempAngle, tempAngle);
 int tempOut = tempAngle;
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 6);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut += (tempAngle / 120);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 5040);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut += (tempAngle / 362880);
 tempAngle = fix16_mul(tempAngle, tempAngleSq);
 tempOut -= (tempAngle / 39916800);
 _fix16_sin_cache_index[tempIndex] = inAngle;
 _fix16_sin_cache_value[tempIndex] = tempOut;
 return tempOut;
}

variable_types: tempAngle int

variable_types: tempIndex int

variable_types: tempAngleSq int

variable_types: tempOut int

return_type: int


decompiled_code:
__int64 __fastcall fix16_cos(int a1)
{
return fix16_sin(a1 + 102943);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return func1(a1 + 102943);
}

original_code:
fix16_t fix16_cos(fix16_t inAngle) {
 return fix16_sin(inAngle + (fix16_pi >> 1));
}

canonical_original_code:
int fix16_cos(int inAngle) {
 return fix16_sin(inAngle + (fix16_pi >> 1));
}


decompiled_code:
__int64 __fastcall fix16_tan(int a1)
{
unsigned int v1; // er12
unsigned int v2; // eax

v1 = fix16_cos(a1);
v2 = fix16_sin(a1);
return fix16_sdiv(v2, v1);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
unsigned int v1; // er12
unsigned int v2; // eax

v1 = func2(a1);
v2 = func1(a1);
return fix16_sdiv(v2, v1);
}

original_code:
fix16_t fix16_tan(fix16_t inAngle) {
 return fix16_sdiv(fix16_sin(inAngle), fix16_cos(inAngle));
}

canonical_original_code:
int fix16_tan(int inAngle) {
 return fix16_sdiv(fix16_sin(inAngle), fix16_cos(inAngle));
}


decompiled_code:
__int64 __fastcall fix16_atan2(int a1, int a2)
{
__int64 v4; // rbp
__int64 v5; // r14
int v6; // esi
__int64 v7; // rdi
unsigned int v8; // er15
unsigned int v9; // eax
unsigned int v10; // eax
int v11; // ebx
__int64 result; // rax
unsigned int v13; // er15
unsigned int v14; // eax
unsigned int v15; // eax
int v16; // ebx

v4 = ((unsigned __int16)(a1 ^ a2) ^ (unsigned __int16)((unsigned __int64)(a1 ^ a2) >> 20)) & 0xFFF;
v5 = v4 + 4096;
if ( fix16_atan_cache_index[v4] == a2 && fix16_atan_cache_index[v5] == a1 )
return (unsigned int)fix16_atan_cache_value[v4];
v6 = (a1 >> 31) ^ (a1 + (a1 >> 31));
v7 = (unsigned int)(a2 + v6);
if ( a2 < 0 )
{
v13 = fix16_div(v7, (unsigned int)(v6 - a2));
v14 = fix16_mul(v13, v13);
v15 = fix16_mul(v14, v13);
v16 = fix16_mul(12864LL, v15);
result = v16 - (unsigned int)fix16_mul(64336LL, v13) + 154415;
}
else
{
v8 = fix16_div((unsigned int)(a2 - v6), (unsigned int)v7);
v9 = fix16_mul(v8, v8);
v10 = fix16_mul(v9, v8);
v11 = fix16_mul(12864LL, v10);
result = v11 - (unsigned int)fix16_mul(64336LL, v8) + 51471;
}
fix16_atan_cache_index[v4] = a2;
fix16_atan_cache_index[v5] = a1;
if ( a1 < 0 )
result = (unsigned int)-(int)result;
fix16_atan_cache_value[v4] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2)
{
__int64 v4; // rbp
__int64 v5; // r14
int v6; // esi
__int64 v7; // rdi
unsigned int v8; // er15
unsigned int v9; // eax
unsigned int v10; // eax
int v11; // ebx
__int64 result; // rax
unsigned int v13; // er15
unsigned int v14; // eax
unsigned int v15; // eax
int v16; // ebx

v4 = ((unsigned __int16)(a1 ^ a2) ^ (unsigned __int16)((unsigned __int64)(a1 ^ a2) >> 20)) & 0xFFF;
v5 = v4 + 4096;
if ( fix16_atan_cache_index[v4] == a2 && fix16_atan_cache_index[v5] == a1 )
return (unsigned int)fix16_atan_cache_value[v4];
v6 = (a1 >> 31) ^ (a1 + (a1 >> 31));
v7 = (unsigned int)(a2 + v6);
if ( a2 < 0 )
{
v13 = fix16_div(v7, (unsigned int)(v6 - a2));
v14 = fix16_mul(v13, v13);
v15 = fix16_mul(v14, v13);
v16 = fix16_mul(12864LL, v15);
result = v16 - (unsigned int)fix16_mul(64336LL, v13) + 154415;
}
else
{
v8 = fix16_div((unsigned int)(a2 - v6), (unsigned int)v7);
v9 = fix16_mul(v8, v8);
v10 = fix16_mul(v9, v8);
v11 = fix16_mul(12864LL, v10);
result = v11 - (unsigned int)fix16_mul(64336LL, v8) + 51471;
}
fix16_atan_cache_index[v4] = a2;
fix16_atan_cache_index[v5] = a1;
if ( a1 < 0 )
result = (unsigned int)-(int)result;
fix16_atan_cache_value[v4] = result;
return result;
}

original_code:
fix16_t fix16_atan2(fix16_t inY , fix16_t inX) {
 fix16_t abs_inY, mask, angle, r, r_3;
 uintptr_t hash = (inX ^ inY);
 hash ^= hash >> 20;
 hash &= 0x0FFF;
 if((_fix16_atan_cache_index[0][hash] == inX) && (_fix16_atan_cache_index[1][hash] == inY))
  return _fix16_atan_cache_value[hash];
 mask = (inY >> (sizeof(fix16_t)*8 -1));
 abs_inY = (inY + mask) ^ mask;
 if (inX >= 0)
 {
  r = fix16_div( (inX - abs_inY), (inX + abs_inY));
  r_3 = fix16_mul(fix16_mul(r, r),r);
  angle = fix16_mul(0x00003240 , r_3) - fix16_mul(0x0000FB50,r) + PI_DIV_4;
 } else {
  r = fix16_div( (inX + abs_inY), (abs_inY - inX));
  r_3 = fix16_mul(fix16_mul(r, r),r);
  angle = fix16_mul(0x00003240 , r_3) - fix16_mul(0x0000FB50,r) + THREE_PI_DIV_4;
 }
 if (inY < 0)
 {
  angle = -angle;
 }
 _fix16_atan_cache_index[0][hash] = inX;
 _fix16_atan_cache_index[1][hash] = inY;
 _fix16_atan_cache_value[hash] = angle;
 return angle;
}

canonical_original_code:
int fix16_atan2(int inY, int inX) {
 int abs_inY; int mask; int angle; int r; int r_3;
 unsigned long hash = (inX ^ inY);
 hash ^= hash >> 20;
 hash &= 0x0FFF;
 if((_fix16_atan_cache_index[0][hash] == inX) && (_fix16_atan_cache_index[1][hash] == inY))
  return _fix16_atan_cache_value[hash];
 mask = (inY >> (sizeof(fix16_t)*8 -1));
 abs_inY = (inY + mask) ^ mask;
 if (inX >= 0)
 {
  r = fix16_div( (inX - abs_inY), (inX + abs_inY));
  r_3 = fix16_mul(fix16_mul(r, r),r);
  angle = fix16_mul(0x00003240 , r_3) - fix16_mul(0x0000FB50,r) + PI_DIV_4;
 } else {
  r = fix16_div( (inX + abs_inY), (abs_inY - inX));
  r_3 = fix16_mul(fix16_mul(r, r),r);
  angle = fix16_mul(0x00003240 , r_3) - fix16_mul(0x0000FB50,r) + THREE_PI_DIV_4;
 }
 if (inY < 0)
 {
  angle = -angle;
 }
 _fix16_atan_cache_index[0][hash] = inX;
 _fix16_atan_cache_index[1][hash] = inY;
 _fix16_atan_cache_value[hash] = angle;
 return angle;
}

variable_types: inY int

variable_types: inX int

variable_types: angle int

variable_types: r int

variable_types:
hash
unsigned long


decompiled_code:
__int64 __fastcall fix16_atan(int a1)
{
return fix16_atan2(a1, (int)&unk_10000);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
return func4(a1, (int)&unk_10000);
}

original_code:
fix16_t fix16_atan(fix16_t inValue) {
 return fix16_atan2(inValue, fix16_one);
}

canonical_original_code:
int fix16_atan(int inValue) {
 return fix16_atan2(inValue, fix16_one);
}

variable_types: inValue int


decompiled_code:
__int64 __fastcall fix16_asin(__int64 a1)
{
int v1; // eax
unsigned int v2; // eax
int v3; // eax

if ( (unsigned int)((unsigned int)&unk_10000 + a1) > 0x20000 )
return 0LL;
v1 = fix16_mul(a1, (unsigned int)a1);
v2 = fix16_sqrt((unsigned int)((_DWORD)&unk_10000 - v1));
v3 = fix16_div((unsigned int)a1, v2);
return fix16_atan(v3);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // eax
unsigned int v2; // eax
int v3; // eax

if ( (unsigned int)((unsigned int)&unk_10000 + a1) > 0x20000 )
return 0LL;
v1 = fix16_mul(a1, (unsigned int)a1);
v2 = fix16_sqrt((unsigned int)((_DWORD)&unk_10000 - v1));
v3 = fix16_div((unsigned int)a1, v2);
return func5(v3);
}

original_code:
fix16_t fix16_asin(fix16_t inValue) {
 if((inValue > fix16_one) || (inValue < -fix16_one))
  return 0;
 fix16_t tempOut;
 tempOut = (fix16_one - fix16_mul(inValue, inValue));
 tempOut = fix16_div(inValue, fix16_sqrt(tempOut));
 tempOut = fix16_atan(tempOut);
 return tempOut;
}

canonical_original_code:
int fix16_asin(int inValue) {
 if((inValue > fix16_one) || (inValue < -fix16_one))
  return 0;
 int tempOut;
 tempOut = (fix16_one - fix16_mul(inValue, inValue));
 tempOut = fix16_div(inValue, fix16_sqrt(tempOut));
 tempOut = fix16_atan(tempOut);
 return tempOut;
}


decompiled_code:
__int64 __fastcall fix16_acos(__int64 a1)
{
return 102943 - (unsigned int)fix16_asin(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return 102943 - (unsigned int)func6(a1);
}

original_code:
fix16_t fix16_acos(fix16_t inValue) {
 return ((fix16_pi >> 1) - fix16_asin(inValue));
}

canonical_original_code:
int fix16_acos(int inValue) {
 return ((fix16_pi >> 1) - fix16_asin(inValue));
}


decompiled_code:
__int64 __fastcall uint32_log2(unsigned int a1)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
{
if ( a1 > 0xFFFF )
{
a1 >>= 16;
LODWORD(result) = 16;
}
if ( a1 > 0xFF )
{
a1 >>= 8;
LODWORD(result) = result + 8;
}
if ( a1 > 0xF )
{
a1 >>= 4;
LODWORD(result) = result + 4;
}
if ( a1 > 3 )
{
a1 >>= 2;
LODWORD(result) = result + 2;
}
result = (a1 != 1) + (unsigned int)result;
}
return result;
}

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

result = 0LL;
if ( a1 )
{
if ( a1 > 0xFFFF )
{
a1 >>= 16;
LODWORD(result) = 16;
}
if ( a1 > 0xFF )
{
a1 >>= 8;
LODWORD(result) = result + 8;
}
if ( a1 > 0xF )
{
a1 >>= 4;
LODWORD(result) = result + 4;
}
if ( a1 > 3 )
{
a1 >>= 2;
LODWORD(result) = result + 2;
}
result = (a1 != 1) + (unsigned int)result;
}
return result;
}

original_code:
uint32_t uint32_log2(uint32_t inVal) {
 if(inVal == 0)
  return 0;
 uint32_t tempOut = 0;
 if(inVal >= (1 << 16)) { inVal >>= 16; tempOut += 16; }
 if(inVal >= (1 << 8)) { inVal >>= 8; tempOut += 8; }
 if(inVal >= (1 << 4)) { inVal >>= 4; tempOut += 4; }
 if(inVal >= (1 << 2)) { inVal >>= 2; tempOut += 2; }
 if(inVal >= (1 << 1)) { tempOut += 1; }
 return tempOut;
}

canonical_original_code:
unsigned int uint32_log2(unsigned int inVal) {
 if(inVal == 0)
  return 0;
 unsigned int tempOut = 0;
 if(inVal >= (1 << 16)) { inVal >>= 16; tempOut += 16; }
 if(inVal >= (1 << 8)) { inVal >>= 8; tempOut += 8; }
 if(inVal >= (1 << 4)) { inVal >>= 4; tempOut += 4; }
 if(inVal >= (1 << 2)) { inVal >>= 2; tempOut += 2; }
 if(inVal >= (1 << 1)) { tempOut += 1; }
 return tempOut;
}

variable_types:
inVal
unsigned int

variable_types:
tempOut
unsigned int

return_type: unsigned int


original_code:
fix16_t fix16_sqrt(fix16_t inValue) {
  uint8_t neg = (inValue < 0);
  uint32_t num = (neg ? -inValue : inValue);
  uint32_t result = 0;
  uint32_t bit;
  uint8_t n;
  if (num & 0xFFF00000)
    bit = (uint32_t)1 << 30;
  else
    bit = (uint32_t)1 << 18;
  while (bit > num) bit >>= 2;
  for (n = 0; n < 2; n++)
  {
    while (bit)
    {
      if (num >= result + bit)
      {
        num -= result + bit;
        result = (result >> 1) + bit;
      }
      else
      {
        result = (result >> 1);
      }
      bit >>= 2;
    }
    if (n == 0)
    {
      if (num > 65535)
      {
        num -= result;
        num = (num << 16) - 0x8000;
        result = (result << 16) + 0x8000;
      }
      else
      {
        num <<= 16;
        result <<= 16;
      }
      bit = 1 << 14;
    }
  }
  if (num > result)
  {
    result++;
  }
  return (neg ? -result : result);
}

canonical_original_code:
int fix16_sqrt(int inValue) {
  unsigned char neg = (inValue < 0);
  unsigned int num = (neg ? -inValue : inValue);
  unsigned int result = 0;
  unsigned int bit;
  unsigned char n;
  if (num & 0xFFF00000)
    bit = (unsigned int)1 << 30;
  else
    bit = (unsigned int)1 << 18;
  while (bit > num) bit >>= 2;
  for (n = 0; n < 2; n++)
  {
    while (bit)
    {
      if (num >= result + bit)
      {
        num -= result + bit;
        result = (result >> 1) + bit;
      }
      else
      {
        result = (result >> 1);
      }
      bit >>= 2;
    }
    if (n == 0)
    {
      if (num > 65535)
      {
        num -= result;
        num = (num << 16) - 0x8000;
        result = (result << 16) + 0x8000;
      }
      else
      {
        num <<= 16;
        result <<= 16;
      }
      bit = 1 << 14;
    }
  }
  if (num > result)
  {
    result++;
  }
  return (neg ? -result : result);
}

variable_types:
neg
unsigned char

variable_types:
num
unsigned int

variable_types:
result
unsigned int

variable_types:
bit
unsigned int

variable_types:
n
unsigned char


decompiled_code:
__int64 __fastcall int64_from_int32(int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return a1;
}

original_code: static inline int64_t int64_from_int32(int32_t x) { return (int64_t)x; }

canonical_original_code: static inline long int64_from_int32(int x) { return (long)x; }

variable_types: x int

return_type: long


decompiled_code:
__int64 __fastcall int64_lo(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return a1;
}

original_code: static inline uint32_t int64_lo(int64_t x) { return (x & ((1ULL << 32) - 1)); }

canonical_original_code: static inline unsigned int int64_lo(long x) { return (x & ((1ULL << 32) - 1)); }

variable_types: x long


decompiled_code:
__int64 __fastcall int64_add(__int64 a1, __int64 a2)
{
return a1 + a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
return a1 + a2;
}

original_code: static inline int64_t int64_add(int64_t x, int64_t y) { return (x + y); }

canonical_original_code: static inline long int64_add(long x, long y) { return (x + y); }

variable_types: y long


decompiled_code:
__int64 __fastcall int64_mul_i64_i32(__int64 a1, int a2)
{
return a1 * a2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
return a1 * a2;
}

original_code: static inline int64_t int64_mul_i64_i32(int64_t x, int32_t y) { return (x * y); }

canonical_original_code: static inline long int64_mul_i64_i32(long x, int y) { return (x * y); }

variable_types: y int


decompiled_code:
__int64 __fastcall int64_div_i64_i32(__int64 a1, int a2)
{
return a1 / a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
return a1 / a2;
}

original_code: static inline int64_t int64_div_i64_i32(int64_t x, int32_t y) { return (x / y); }

canonical_original_code: static inline long int64_div_i64_i32(long x, int y) { return (x / y); }


decompiled_code:
__int64 __fastcall fix16_exp(int a1)
{
__int64 result; // rax
__int64 v2; // rbx
__int64 v3; // rax
__int64 v4; // r8
__int64 v5; // r8
__int64 v6; // rax
__int64 v7; // r11
__int64 v8; // rax
int v9; // er10
int v10; // esi
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // r9
__int64 v14; // rax
int v15; // ecx

result = 0x10000LL;
if ( a1 )
{
if ( a1 == 0x10000 )
{
result = 178145LL;
}
else
{
result = 0x7FFFFFFFLL;
if ( a1 <= 681391 )
{
result = 0LL;
if ( a1 >= -726817 )
{
v2 = ((unsigned __int16)a1 ^ (unsigned __int16)(a1 >> 4)) & 0xFFF;
if ( fix16_exp_cache_index[v2] == a1 )
{
result = (unsigned int)fix16_exp_cache_value[v2];
}
else
{
int64_from_int32(a1);
v3 = int64_from_int32(0x10000);
int64_add(v3, v4);
int64_from_int32(0x8000);
do
{
v6 = int64_mul_i64_i32(v5, a1);
v8 = int64_add(v6, v7);
v10 = v9;
v11 = int64_shift_constprop_0(v8);
v12 = int64_div_i64_i32(v11, v10);
v14 = int64_add(v13, v12);
}
while ( v15 != 13 );
fix16_exp_cache_index[v2] = a1;
result = int64_lo(v14);
fix16_exp_cache_value[v2] = result;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
__int64 result; // rax
__int64 v2; // rbx
__int64 v3; // rax
__int64 v4; // r8
__int64 v5; // r8
__int64 v6; // rax
__int64 v7; // r11
__int64 v8; // rax
int v9; // er10
int v10; // esi
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // r9
__int64 v14; // rax
int v15; // ecx

result = 0x10000LL;
if ( a1 )
{
if ( a1 == 0x10000 )
{
result = 178145LL;
}
else
{
result = 0x7FFFFFFFLL;
if ( a1 <= 681391 )
{
result = 0LL;
if ( a1 >= -726817 )
{
v2 = ((unsigned __int16)a1 ^ (unsigned __int16)(a1 >> 4)) & 0xFFF;
if ( fix16_exp_cache_index[v2] == a1 )
{
result = (unsigned int)fix16_exp_cache_value[v2];
}
else
{
func0(a1);
v3 = func0(0x10000);
func2(v3, v4);
func0(0x8000);
do
{
v6 = func3(v5, a1);
v8 = func2(v6, v7);
v10 = v9;
v11 = func5(v8);
v12 = func4(v11, v10);
v14 = func2(v13, v12);
}
while ( v15 != 13 );
fix16_exp_cache_index[v2] = a1;
result = func1(v14);
fix16_exp_cache_value[v2] = result;
}
}
}
}
}
return result;
}

original_code:
fix16_t fix16_exp(fix16_t inValue) {
 if(inValue == 0)
  return fix16_one;
 if(inValue == fix16_one)
  return fix16_e;
 if(inValue > 681391)
  return fix16_max;
 if(inValue < -726817)
  return 0;
 fix16_t tempIndex = (inValue ^ (inValue >> 16));
 tempIndex = (inValue ^ (inValue >> 4)) & 0x0FFF;
 if(_fix16_exp_cache_index[tempIndex] == inValue)
  return _fix16_exp_cache_value[tempIndex];
 int64_t tempOut = int64_add(int64_from_int32(fix16_one), int64_from_int32(inValue));
 int64_t tempValue = int64_from_int32(inValue);
 uint32_t i, n;
 for(i = 3, n = 2; i < 13; n *= i, i++) {
  tempValue = int64_mul_i64_i32(tempValue, inValue);
  tempValue = int64_add(tempValue, int64_from_int32(fix16_one >> 1));
  tempValue = int64_shift(tempValue, -16);
  tempOut = int64_add(tempOut, int64_div_i64_i32(tempValue, n));
 }
 _fix16_exp_cache_index[tempIndex] = inValue;
 _fix16_exp_cache_value[tempIndex] = int64_lo(tempOut);
 return int64_lo(tempOut);
}

canonical_original_code:
int fix16_exp(int inValue) {
 if(inValue == 0)
  return fix16_one;
 if(inValue == fix16_one)
  return fix16_e;
 if(inValue > 681391)
  return fix16_max;
 if(inValue < -726817)
  return 0;
 int tempIndex = (inValue ^ (inValue >> 16));
 tempIndex = (inValue ^ (inValue >> 4)) & 0x0FFF;
 if(_fix16_exp_cache_index[tempIndex] == inValue)
  return _fix16_exp_cache_value[tempIndex];
 long tempOut = int64_add(int64_from_int32(fix16_one), int64_from_int32(inValue));
 long tempValue = int64_from_int32(inValue);
 unsigned int i; unsigned int n;
 for(i = 3, n = 2; i < 13; n *= i, i++) {
  tempValue = int64_mul_i64_i32(tempValue, inValue);
  tempValue = int64_add(tempValue, int64_from_int32(fix16_one >> 1));
  tempValue = int64_shift(tempValue, -16);
  tempOut = int64_add(tempOut, int64_div_i64_i32(tempValue, n));
 }
 _fix16_exp_cache_index[tempIndex] = inValue;
 _fix16_exp_cache_value[tempIndex] = int64_lo(tempOut);
 return int64_lo(tempOut);
}

variable_types: tempIndex int

variable_types: tempOut long

variable_types: tempValue long

variable_types:
i
unsigned int

variable_types:
n
unsigned int


avlcmp

repo: echoline/NeoIRCD

decompiled_code:
__int64 avlcmp()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
int avlcmp(Avl *l, Avl *r) {
 return 0;
}

canonical_original_code:
int avlcmp(struct Avl *l, struct Avl *r) {
 return 0;
}

variable_types: struct Avl { struct Avl *p; struct Avl *n[2]; int bal; } *

variable_types:
r
struct Avl *


hashf

decompiled_code:
__int64 __fastcall hashf(__int64 a1, char a2)
{
return a1 & ((1 << a2) - 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char a2)
{
return a1 & ((1 << a2) - 1);
}

original_code:
unsigned long hashf(Avl *k, unsigned int order) {
 return ((unsigned long int)k & ((1 << order) - 1));
}

canonical_original_code:
unsigned long hashf(struct Avl *k, unsigned int order) {
 return ((unsigned long)k & ((1 << order) - 1));
}

variable_types:
order
unsigned int

return_type: unsigned long

user_defined_types: struct Avl { struct Avl *p; struct Avl *n[2]; int bal; }


main

canonical_name: main

repo: echoline/kinectfs

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbx
int v4; // eax
int v5; // ebp
int v6; // eax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

v3 = malloc(0x12C000uLL);
if ( argc == 2 )
{
while ( 1 )
{
v4 = open(argv[1], 0);
v5 = v4;
if ( v4 < 0 || (unsigned int)fstat(v4, &v8) )
break;
printf("--ffserver\r\nContent-Type: image/jpeg\r\nContent-Length: %ld\r\n\r\n", v8.st_size);
fflush(stdout);
while ( 1 )
{
v6 = read(v5, v3, 0x12C000uLL);
if ( v6 <= 0 )
break;
write(1, v3, v6);
}
fflush(stdout);
close(v5);
}
}
else
{
fprintf(stderr, "usage:\n\t%s /path/to/jpg\n", *argv);
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbx
int v4; // eax
int v5; // ebp
int v6; // eax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

v3 = malloc(0x12C000uLL);
if ( argc == 2 )
{
while ( 1 )
{
v4 = open(argv[1], 0);
v5 = v4;
if ( v4 < 0 || (unsigned int)func9(v4, &v8) )
break;
printf("--ffserver\r\nContent-Type: image/jpeg\r\nContent-Length: %ld\r\n\r\n", v8.st_size);
fflush(stdout);
while ( 1 )
{
v6 = read(v5, v3, 0x12C000uLL);
if ( v6 <= 0 )
break;
write(1, v3, v6);
}
fflush(stdout);
close(v5);
}
}
else
{
fprintf(stderr, "usage:\n\t%s /path/to/jpg\n", *argv);
}
return -1;
}

original_code:
int
main(int argc, char **argv) {
 int o;
 int rc;
 unsigned char *bbuf = malloc(640*480*4);
 struct stat statbuf;
 if (argc != 2) {
  fprintf(stderr, "usage:\n\t%s /path/to/jpg\n", argv[0]);
  return -1;
 }
 while (1) {
  o = open(argv[1], 00);
  if (o < 0)
   return -1;
  if (fstat(o, &statbuf) != 0)
   return -1;
  printf("--ffserver\r\nContent-Type: image/jpeg\r\nContent-Length: %ld\r\n\r\n", statbuf.st_size);
  fflush(stdout);
  while ((rc = read(o, bbuf, 640*480*4)) > 0)
   write(1, bbuf, rc);
  fflush(stdout);
  close(o);
 }
 free(bbuf);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: o int

variable_types: rc int

variable_types:
bbuf
unsigned char *

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

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


repo: eclesh/slowtail

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // r13
size_t v5; // rax
size_t v6; // rbx
int result; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
char s[1048]; // [rsp+10h] [rbp-418h] BYREF

v3 = *argv;
if ( argc == 2 )
{
v4 = (char *)argv[1];
if ( strlen(v4) + 8 > 0x400 )
{
fprintf(stderr, "%s: filename too long (max is %ld)\n", v3, 1016LL);
result = 1;
}
else
{
v5 = strlen(s);
strncat(s, v4, 1023 - v5);
v6 = 1023 - strlen(s);
strncat(s, ".offset", v6);
if ( (unsigned int)read_offset(s) )
{
v12 = __errno_location();
v13 = strerror(*v12);
fprintf(stderr, "%s: can't open offset file: %s\n", v3, v13);
result = 1;
}
else if ( (unsigned int)print_logs(v4) )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "%s: can't read '%s': %s\n", v3, v4, v11);
result = 1;
}
else
{
result = ((__int64 (__fastcall *)(char *))write_offset)(s);
if ( result )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "%s: can't write offset file: %s\n", v3, v9);
result = 1;
}
}
}
}
else
{
fwrite("no filename specified\n", 1uLL, 0x16uLL, stderr);
usage(v3);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // r13
size_t v5; // rax
size_t v6; // rbx
int result; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
char s[1048]; // [rsp+10h] [rbp-418h] BYREF

v3 = *argv;
if ( argc == 2 )
{
v4 = (char *)argv[1];
if ( strlen(v4) + 8 > 0x400 )
{
fprintf(stderr, "%s: filename too long (max is %ld)\n", v3, 1016LL);
result = 1;
}
else
{
v5 = strlen(s);
strncat(s, v4, 1023 - v5);
v6 = 1023 - strlen(s);
strncat(s, ".offset", v6);
if ( (unsigned int)func7(s) )
{
v12 = __errno_location();
v13 = strerror(*v12);
fprintf(stderr, "%s: can't open offset file: %s\n", v3, v13);
result = 1;
}
else if ( (unsigned int)func9(v4) )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "%s: can't read '%s': %s\n", v3, v4, v11);
result = 1;
}
else
{
result = ((__int64 (__fastcall *)(char *))write_offset)(s);
if ( result )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "%s: can't write offset file: %s\n", v3, v9);
result = 1;
}
}
}
}
else
{
fwrite("no filename specified\n", 1uLL, 0x16uLL, stderr);
func10(v3);
result = 1;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 char offset_file[1024];
 char *file_name, *prog;
 ino_t inode;
 off_t offset;
 prog = argv[0];
 if (argc != 2) {
  fprintf(stderr, "no filename specified\n");
  usage(prog);
  return 1;
 }
 file_name = argv[1];
 if (strlen(".offset") + strlen(file_name) + 1 > 1024) {
  fprintf(stderr, "%s: filename too long (max is %ld)\n",
   prog, 1024 - strlen(".offset") - 1);
  return 1;
 }
 strncat(offset_file, file_name,
  sizeof(offset_file) - strlen(offset_file) - 1);
 strncat(offset_file, ".offset",
  sizeof(offset_file) - strlen(offset_file) - 1);
 if (read_offset(offset_file, &inode, &offset) != 0) {
  fprintf(stderr, "%s: can't open offset file: %s\n",
   prog, strerror((*__errno_location ())));
  return 1;
 }
 if (print_logs(file_name, &inode, &offset) != 0) {
  fprintf(stderr, "%s: can't read '%s': %s\n",
   prog, file_name, strerror((*__errno_location ())));
  return 1;
 }
 if (write_offset(offset_file, inode, offset) != 0) {
  fprintf(stderr, "%s: can't write offset file: %s\n",
   prog, strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 char offset_file[1024];
 char *file_name; char *prog;
 unsigned long inode;
 long offset;
 prog = argv[0];
 if (argc != 2) {
  fprintf(stderr, "no filename specified\n");
  usage(prog);
  return 1;
 }
 file_name = argv[1];
 if (strlen(".offset") + strlen(file_name) + 1 > 1024) {
  fprintf(stderr, "%s: filename too long (max is %ld)\n",
   prog, 1024 - strlen(".offset") - 1);
  return 1;
 }
 strncat(offset_file, file_name,
  sizeof(offset_file) - strlen(offset_file) - 1);
 strncat(offset_file, ".offset",
  sizeof(offset_file) - strlen(offset_file) - 1);
 if (read_offset(offset_file, &inode, &offset) != 0) {
  fprintf(stderr, "%s: can't open offset file: %s\n",
   prog, strerror((*__errno_location ())));
  return 1;
 }
 if (print_logs(file_name, &inode, &offset) != 0) {
  fprintf(stderr, "%s: can't read '%s': %s\n",
   prog, file_name, strerror((*__errno_location ())));
  return 1;
 }
 if (write_offset(offset_file, inode, offset) != 0) {
  fprintf(stderr, "%s: can't write offset file: %s\n",
   prog, strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}

variable_types:
prog
char *

variable_types:
inode
unsigned long

variable_types: offset long


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(stderr, "usage: %s <filename>\n", a1);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
return fprintf(stderr, "usage: %s <filename>\n", a1);
}

original_code:
void usage(char *progname)
{
 fprintf(stderr, "usage: %s <filename>\n", progname);
}

variable_types:
progname
char *

return_type: void


parent

decompiled_code:
void __fastcall __noreturn parent(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&mutex_cond);
if ( !pthread_cond_wait(&var_cond, &mutex_cond) )
{
worms = rand() % 7 + 1;
printf("\nParent %s put %d worms on plate.\n", (const char *)a1 + 4, (unsigned int)worms);
}
pthread_mutex_unlock(&mutex_cond);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&mutex_cond);
if ( !pthread_cond_wait(&var_cond, &mutex_cond) )
{
worms = rand() % 7 + 1;
printf("\nParent %s put %d worms on plate.\n", (const char *)a1 + 4, (unsigned int)worms);
}
pthread_mutex_unlock(&mutex_cond);
}
}

original_code:
void *parent(void *dad){
    struct DATA *aa = (struct DATA *)dad ;
    while(1){
        pthread_mutex_lock(&mutex_cond);
        if(pthread_cond_wait(&var_cond, &mutex_cond) == 0){
            worms = (rand()%7)+1;
            printf("\nParent %s put %d worms on plate.\n", aa->name, worms);
        }
        pthread_mutex_unlock(&mutex_cond);
    }
}

canonical_original_code:
void *parent(void *dad){
    struct DATA *aa = (struct DATA *)dad;
    while(1){
        pthread_mutex_lock(&mutex_cond);
        if(pthread_cond_wait(&var_cond, &mutex_cond) == 0){
            worms = (rand()%7)+1;
            printf("\nParent %s put %d worms on plate.\n", aa->name, worms);
        }
        pthread_mutex_unlock(&mutex_cond);
    }
}

variable_types:
dad
void *

variable_types: struct DATA { int num; char name[10]; } *

return_type: void *


decompiled_code:
void __fastcall __noreturn child_handler(void *a1)
{
int v1; // eax
__int64 v2; // rdx

while ( 1 )
{
do
{
v1 = rand();
sleep(v1 % 4);
}
while ( worms <= 0 );
pthread_mutex_lock(&mutex_cond);
v2 = (unsigned int)(worms - 1);
worms = v2;
if ( !(_DWORD)v2 )
{
pthread_cond_signal(&var_cond);
v2 = (unsigned int)worms;
}
printf("Bird N:%d take a worm. Remain %d worms\n", *(unsigned int *)a1, v2);
pthread_mutex_unlock(&mutex_cond);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
int v1; // eax
__int64 v2; // rdx

while ( 1 )
{
do
{
v1 = rand();
sleep(v1 % 4);
}
while ( worms <= 0 );
pthread_mutex_lock(&mutex_cond);
v2 = (unsigned int)(worms - 1);
worms = v2;
if ( !(_DWORD)v2 )
{
pthread_cond_signal(&var_cond);
v2 = (unsigned int)worms;
}
printf("Bird N:%d take a worm. Remain %d worms\n", *(unsigned int *)a1, v2);
pthread_mutex_unlock(&mutex_cond);
}
}

original_code:
void *child_handler(void *data){
    struct DATA *aa = (struct DATA *)data ;
    while(1){
        sleep(rand()%4);
        if(worms > 0){
                pthread_mutex_lock(&mutex_cond);
            worms-- ;
            if(worms == 0){
                pthread_cond_signal(&var_cond);
            }
            printf("Bird N:%d take a worm. Remain %d worms\n",aa->num, worms);
                pthread_mutex_unlock(&mutex_cond);
        }
    }
}

canonical_original_code:
void *child_handler(void *data){
    struct DATA *aa = (struct DATA *)data;
    while(1){
        sleep(rand()%4);
        if(worms > 0){
                pthread_mutex_lock(&mutex_cond);
            worms-- ;
            if(worms == 0){
                pthread_cond_signal(&var_cond);
            }
            printf("Bird N:%d take a worm. Remain %d worms\n",aa->num, worms);
                pthread_mutex_unlock(&mutex_cond);
        }
    }
}

variable_types:
data
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
unsigned int *v4; // r12
pthread_t *v5; // rbp
pthread_t v6; // rdi
pthread_t newthread; // [rsp+8h] [rbp-130h] BYREF
int arg; // [rsp+10h] [rbp-128h] BYREF
__int64 v10; // [rsp+14h] [rbp-124h]
__int16 v11; // [rsp+1Ch] [rbp-11Ch]
pthread_t v12[10]; // [rsp+20h] [rbp-118h] BYREF
unsigned int v13[50]; // [rsp+70h] [rbp-C8h] BYREF

pthread_mutex_init(&sem, 0LL);
pthread_mutex_init(&mutex_cond, 0LL);
if ( !(worms | scream) )
scream = 1;
if ( pthread_cond_init(&var_cond, 0LL) < 0 )
{
fwrite("variabile condition error.\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v10 = 0x6F42426142LL;
arg = 0;
v11 = 0;
if ( pthread_create(&newthread, 0LL, parent, &arg) < 0 )
{
fwrite("create error for PARENT.\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
v3 = 0LL;
v4 = v13;
do
{
*v4 = v3;
v5 = v12;
if ( pthread_create(&v12[v3], 0LL, child_handler, v4) < 0 )
{
fprintf(stderr, "create error for BIRD N%d.\n", v13[4 * (int)v3]);
exit(1);
}
++v3;
v4 += 4;
}
while ( v3 != 10 );
pthread_join(newthread, 0LL);
do
{
v6 = *v5++;
pthread_join(v6, 0LL);
}
while ( v5 != (pthread_t *)v13 );
return 0;
}

original_code:
int main (int argc, char* argv[]){
    int i = 0;
    pthread_mutex_init (&sem, ((void *)0));
    pthread_mutex_init (&mutex_cond, ((void *)0));
    pthread_t thread_parent;
    pthread_t thread_bird[10];
    struct DATA data[10];
    if(scream == 0 && worms == 0){
        scream = 1 ;
    }
    if (pthread_cond_init(&var_cond, ((void *)0)) < 0)
        { fprintf (stderr, "variabile condition error.\n");
            exit (1);
        }
    struct DATA dad = {0, "BaBBo"} ;
    if (pthread_create(&thread_parent, ((void *)0), parent, (void *)&dad ) < 0)
        { fprintf (stderr, "create error for PARENT.\n");
            exit (1);
        }
    for ( i = 0 ; i < 10 ; i++){
        data[i].num = i ;
        if (pthread_create(&thread_bird[i], ((void *)0), child_handler, (void *)&data[i] ) < 0)
            { fprintf (stderr, "create error for BIRD N%d.\n", data[i].num);
                exit (1);
            }
    }
    pthread_join (thread_parent, ((void *)0));
    for ( i = 0 ; i < 10 ; i++)
        { pthread_join (thread_bird[i], ((void *)0));
        }
    return 0 ;
}

canonical_original_code:
int main(int argc, char *argv[]){
    int i = 0;
    pthread_mutex_init (&sem, ((void *)0));
    pthread_mutex_init (&mutex_cond, ((void *)0));
    unsigned long thread_parent;
    unsigned long thread_bird[10];
    struct DATA data[10];
    if(scream == 0 && worms == 0){
        scream = 1 ;
    }
    if (pthread_cond_init(&var_cond, ((void *)0)) < 0)
        { fprintf (stderr, "variabile condition error.\n");
            exit (1);
        }
    struct DATA dad = {0, "BaBBo"};
    if (pthread_create(&thread_parent, ((void *)0), parent, (void *)&dad ) < 0)
        { fprintf (stderr, "create error for PARENT.\n");
            exit (1);
        }
    for ( i = 0 ; i < 10 ; i++){
        data[i].num = i ;
        if (pthread_create(&thread_bird[i], ((void *)0), child_handler, (void *)&data[i] ) < 0)
            { fprintf (stderr, "create error for BIRD N%d.\n", data[i].num);
                exit (1);
            }
    }
    pthread_join (thread_parent, ((void *)0));
    for ( i = 0 ; i < 10 ; i++)
        { pthread_join (thread_bird[i], ((void *)0));
        }
    return 0 ;
}

variable_types:
argv
char *[]

variable_types: i int

variable_types: struct DATA { int num; char name[10]; } [10]

variable_types:
dad
struct DATA { int num; char name[10]; }


decompiled_code:
void sigchld_handler()
{
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( waitpid(-1, stat_loc, 1) > 0 )
;
}

canonical_decompiled_code:
void func7()
{
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( waitpid(-1, stat_loc, 1) > 0 )
;
}

original_code:
static void
sigchld_handler(int signal)
{
 pid_t pid;
 int status;
 do {
  pid = waitpid(-1, &status, 1);
 } while (pid > 0);
}

canonical_original_code:
static void sigchld_handler(int signal)
{
 int pid;
 int status;
 do {
  pid = waitpid(-1, &status, 1);
 } while (pid > 0);
}

variable_types: signal int

variable_types: pid int

variable_types: status int


decompiled_code:
void __noreturn sigterm_handler()
{
if ( shm_unlink("MY_SHARED_MEMORY") != -1
&& !sem_unlink("SEM_PARENT")
&& !sem_unlink("SEM_MUTEX")
&& !sem_unlink("SEM_BIRD") )
{
exit(0);
}
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
if ( shm_unlink("MY_SHARED_MEMORY") != -1
&& !sem_unlink("SEM_PARENT")
&& !sem_unlink("SEM_MUTEX")
&& !sem_unlink("SEM_BIRD") )
{
exit(0);
}
exit(1);
}

original_code:
static void
sigterm_handler(int signal)
{
    if( shm_unlink("MY_SHARED_MEMORY") == -1 )
        exit(1);
    if(sem_unlink("SEM_PARENT"))
        exit(1);
    if(sem_unlink("SEM_MUTEX"))
        exit(1);
    if(sem_unlink("SEM_BIRD"))
        exit(1);
    exit(0);
}


decompiled_code:
void __noreturn parent_create()
{
unsigned int v0; // eax
unsigned int v1; // eax
int v2; // er12
unsigned int v3; // eax

v0 = getpid();
printf("[PID %d ] I am DAD\n", v0);
while ( 1 )
{
sem_wait(sem_parent);
sem_wait(sem);
v2 = *(_DWORD *)shm_addr;
if ( *(_DWORD *)shm_addr )
{
if ( v2 <= 0 )
{
perror("Acces Plate: ");
exit(1);
}
v1 = getpid();
printf("[PID %d ] I am DAD... On plate there are %d worms. I'm Waiting ...\n", v1, (unsigned int)v2);
}
else
{
*(_DWORD *)shm_addr = 7;
v3 = getpid();
printf("[PID %d ] I am DAD!!! Now there are %d worms on plates\n", v3, 7LL);
}
sem_post(sem);
sem_post(sem_bird);
}
}

canonical_decompiled_code:
void __noreturn func9()
{
unsigned int v0; // eax
unsigned int v1; // eax
int v2; // er12
unsigned int v3; // eax

v0 = getpid();
printf("[PID %d ] I am DAD\n", v0);
while ( 1 )
{
sem_wait(sem_parent);
sem_wait(sem);
v2 = *(_DWORD *)shm_addr;
if ( *(_DWORD *)shm_addr )
{
if ( v2 <= 0 )
{
perror("Acces Plate: ");
exit(1);
}
v1 = getpid();
printf("[PID %d ] I am DAD... On plate there are %d worms. I'm Waiting ...\n", v1, (unsigned int)v2);
}
else
{
*(_DWORD *)shm_addr = 7;
v3 = getpid();
printf("[PID %d ] I am DAD!!! Now there are %d worms on plates\n", v3, 7LL);
}
sem_post(sem);
sem_post(sem_bird);
}
}

original_code:
void
parent_create(void)
{
    printf("[PID %d ] I am DAD\n", getpid());
    while(1) {
        sem_wait(sem_parent);
         sem_wait(sem);
         if(*shm_addr == 0) {
            *shm_addr = *shm_addr + 7 ;
            printf("[PID %d ] I am DAD!!!  Now there are %d worms on plates\n", getpid(), *shm_addr);
             } else if(*shm_addr > 0){
            printf("[PID %d ] I am DAD... On plate there are %d worms. "
                   "I'm Waiting ...\n", getpid(), *shm_addr);
            } else {
            perror("Acces Plate: ");
            exit(1);
         }
         sem_post(sem);
         sem_post(sem_bird);
    }
    perror("Parent");
    exit(1);
}

canonical_original_code:
void parent_create()
{
    printf("[PID %d ] I am DAD\n", getpid());
    while(1) {
        sem_wait(sem_parent);
         sem_wait(sem);
         if(*shm_addr == 0) {
            *shm_addr = *shm_addr + 7 ;
            printf("[PID %d ] I am DAD!!!  Now there are %d worms on plates\n", getpid(), *shm_addr);
             } else if(*shm_addr > 0){
            printf("[PID %d ] I am DAD... On plate there are %d worms. "
                   "I'm Waiting ...\n", getpid(), *shm_addr);
            } else {
            perror("Acces Plate: ");
            exit(1);
         }
         sem_post(sem);
         sem_post(sem_bird);
    }
    perror("Parent");
    exit(1);
}


decompiled_code:
void __noreturn child_create()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ecx
unsigned int v3; // er12
unsigned int v4; // eax

v0 = getpid();
printf("[PID %d ] I am Child\n", v0);
while ( 1 )
{
while ( 1 )
{
v1 = rand();
sleep(v1 % 5);
sem_wait(sem_bird);
sem_wait(sem);
v2 = *(_DWORD *)shm_addr;
if ( *(int *)shm_addr <= 0 )
break;
v3 = v2 - 1;
worms = *(_DWORD *)shm_addr;
*(_DWORD *)shm_addr = v2 - 1;
v4 = getpid();
printf("Worms on plate:%d ... I Bird %d. I'm hangry ... Remain %d Worms\n", (unsigned int)worms, v4, v3);
sem_post(sem);
sem_post(sem_bird);
}
if ( v2 )
{
perror("Child");
exit(1);
}
puts("EHI DAD !!! The plate in Empty... We are hangry yet!!! ");
sem_post(sem);
sem_post(sem_parent);
}
}

canonical_decompiled_code:
void __noreturn func10()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ecx
unsigned int v3; // er12
unsigned int v4; // eax

v0 = getpid();
printf("[PID %d ] I am Child\n", v0);
while ( 1 )
{
while ( 1 )
{
v1 = rand();
sleep(v1 % 5);
sem_wait(sem_bird);
sem_wait(sem);
v2 = *(_DWORD *)shm_addr;
if ( *(int *)shm_addr <= 0 )
break;
v3 = v2 - 1;
worms = *(_DWORD *)shm_addr;
*(_DWORD *)shm_addr = v2 - 1;
v4 = getpid();
printf("Worms on plate:%d ... I Bird %d. I'm hangry ... Remain %d Worms\n", (unsigned int)worms, v4, v3);
sem_post(sem);
sem_post(sem_bird);
}
if ( v2 )
{
perror("Child");
exit(1);
}
puts("EHI DAD !!! The plate in Empty... We are hangry yet!!! ");
sem_post(sem);
sem_post(sem_parent);
}
}

original_code:
void
child_create (void)
{
    printf("[PID %d ] I am Child\n", getpid());
    while(1) {
        sleep(rand()%5);
        sem_wait(sem_bird);
        sem_wait(sem);
        if(*shm_addr > 0) {
            worms = *shm_addr;
            *shm_addr = *shm_addr - 1;
            printf("Worms on plate:%d ... I Bird %d. I'm hangry ... "
                   "Remain %d Worms\n",worms, getpid(), *shm_addr);
            sem_post(sem);
            sem_post(sem_bird);
        } else if(*shm_addr == 0){
            printf("EHI DAD !!! The plate in Empty... We are hangry yet!!! \n");
            sem_post(sem);
            sem_post(sem_parent);
        } else {
            perror("Child");
            exit(1) ;
        }
    }
}

canonical_original_code:
void child_create()
{
    printf("[PID %d ] I am Child\n", getpid());
    while(1) {
        sleep(rand()%5);
        sem_wait(sem_bird);
        sem_wait(sem);
        if(*shm_addr > 0) {
            worms = *shm_addr;
            *shm_addr = *shm_addr - 1;
            printf("Worms on plate:%d ... I Bird %d. I'm hangry ... "
                   "Remain %d Worms\n",worms, getpid(), *shm_addr);
            sem_post(sem);
            sem_post(sem_bird);
        } else if(*shm_addr == 0){
            printf("EHI DAD !!! The plate in Empty... We are hangry yet!!! \n");
            sem_post(sem);
            sem_post(sem_parent);
        } else {
            perror("Child");
            exit(1) ;
        }
    }
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // edi
_DWORD *v4; // rax
__int64 v5; // rcx
__pid_t v6; // eax
__pid_t v7; // eax

printf("[%d]\n", 1LL);
fflush(stdout);
shm_id = shm_open("MY_SHARED_MEMORY", 66, 0x1B6u);
v3 = shm_id;
if ( shm_id == -1 )
{
perror("shm_open: ");
v3 = shm_id;
}
if ( ftruncate(v3, 4LL) == -1 )
perror("ftruncate: ");
v4 = mmap(0LL, 4uLL, 3, 1, shm_id, 0LL);
shm_addr = (__int64)v4;
if ( v4 == (_DWORD *)-1LL )
{
perror("mmap: ");
v4 = (_DWORD *)shm_addr;
}
*v4 = worms;
sem = sem_open("SEM_MUTEX", 64, 2LL, 1LL);
if ( !sem || (sem_bird = sem_open("SEM_BIRD", 64, 2LL, 1LL)) == 0LL )
{
perror("Error initializing semaphore");
exit(1);
}
if ( *(_DWORD *)shm_addr )
{
if ( *(int *)shm_addr <= 0 )
{
perror("Error initializing PARENT semaphore: BAD value Shared Memory");
exit(1);
}
v5 = 0LL;
}
else
{
v5 = 1LL;
}
sem_parent = sem_open("SEM_PARENT", 64, 2LL, v5);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
signal(2, sigterm_handler);
signal(15, sigterm_handler);
signal(17, sigchld_handler);
v6 = fork();
if ( v6 != -1 )
{
if ( !v6 )
goto LABEL_20;
v7 = fork();
if ( v7 != -1 )
{
if ( v7 )
parent_create();
LABEL_20:
signal(2, 0LL);
signal(15, 0LL);
signal(17, 0LL);
child_create();
}
}
perror("Fork Failed");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // edi
_DWORD *v4; // rax
__int64 v5; // rcx
__pid_t v6; // eax
__pid_t v7; // eax

printf("[%d]\n", 1LL);
fflush(stdout);
shm_id = shm_open("MY_SHARED_MEMORY", 66, 0x1B6u);
v3 = shm_id;
if ( shm_id == -1 )
{
perror("shm_open: ");
v3 = shm_id;
}
if ( ftruncate(v3, 4LL) == -1 )
perror("ftruncate: ");
v4 = mmap(0LL, 4uLL, 3, 1, shm_id, 0LL);
shm_addr = (__int64)v4;
if ( v4 == (_DWORD *)-1LL )
{
perror("mmap: ");
v4 = (_DWORD *)shm_addr;
}
*v4 = worms;
sem = sem_open("SEM_MUTEX", 64, 2LL, 1LL);
if ( !sem || (sem_bird = sem_open("SEM_BIRD", 64, 2LL, 1LL)) == 0LL )
{
perror("Error initializing semaphore");
exit(1);
}
if ( *(_DWORD *)shm_addr )
{
if ( *(int *)shm_addr <= 0 )
{
perror("Error initializing PARENT semaphore: BAD value Shared Memory");
exit(1);
}
v5 = 0LL;
}
else
{
v5 = 1LL;
}
sem_parent = sem_open("SEM_PARENT", 64, 2LL, v5);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
signal(2, sigterm_handler);
signal(15, sigterm_handler);
signal(17, sigchld_handler);
v6 = fork();
if ( v6 != -1 )
{
if ( !v6 )
goto LABEL_20;
v7 = fork();
if ( v7 != -1 )
{
if ( v7 )
func9();
LABEL_20:
signal(2, 0LL);
signal(15, 0LL);
signal(17, 0LL);
func10();
}
}
perror("Fork Failed");
exit(1);
}

original_code:
int main(int argc, char* argv[])
{
    key_t shm_key;
    size_t shm_size;
    printf("[%d]\n", 1);
    fflush(stdout);
    shm_key = 1986;
    shm_size = sizeof(worms);
    shm_id = shm_open( "MY_SHARED_MEMORY", 0100 | 02, 0666 ) ;
    if (shm_id == -1)
        perror("shm_open: ");
    if (ftruncate(shm_id, shm_size) == -1)
        perror("ftruncate: ");
    shm_addr = mmap(((void *)0), shm_size, 0x1 | 0x2, 0x01, shm_id, 0);
    if (shm_addr == ((void *) -1))
        perror("mmap: ");
    *shm_addr = worms;
     if((sem = sem_open("SEM_MUTEX", 0100, 02, 1)) == ((sem_t *) 0)){
         perror("Error initializing semaphore");
        exit(1);
    }
     if((sem_bird = sem_open("SEM_BIRD", 0100, 02, 1)) == ((sem_t *) 0)){
         perror("Error initializing semaphore");
         exit(1);
     }
    if(*shm_addr == 0){
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 1)) == ((sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else if(*shm_addr > 0) {
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 0)) == ((sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else {
        perror("Error initializing PARENT semaphore: BAD value Shared Memory");
        exit(1);
    }
 signal(2, sigterm_handler);
 signal(15, sigterm_handler);
 signal(17, sigchld_handler);
    int i;
    for(i = 0; i < 2; i++ ) {
        switch (fork()) {
            case -1 :
                perror("Fork Failed");
                exit(1);
                break;
            case 0 :
                signal(2, ((__sighandler_t) 0));
                signal(15, ((__sighandler_t) 0));
                signal(17, ((__sighandler_t) 0));
                child_create();
                exit(0);
            default :
                break;
        }
    }
    parent_create() ;
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int shm_key;
    unsigned long shm_size;
    printf("[%d]\n", 1);
    fflush(stdout);
    shm_key = 1986;
    shm_size = sizeof(worms);
    shm_id = shm_open( "MY_SHARED_MEMORY", 0100 | 02, 0666 ) ;
    if (shm_id == -1)
        perror("shm_open: ");
    if (ftruncate(shm_id, shm_size) == -1)
        perror("ftruncate: ");
    shm_addr = mmap(((void *)0), shm_size, 0x1 | 0x2, 0x01, shm_id, 0);
    if (shm_addr == ((void *) -1))
        perror("mmap: ");
    *shm_addr = worms;
     if((sem = sem_open("SEM_MUTEX", 0100, 02, 1)) == ((union sem_t *) 0)){
         perror("Error initializing semaphore");
        exit(1);
    }
     if((sem_bird = sem_open("SEM_BIRD", 0100, 02, 1)) == ((union sem_t *) 0)){
         perror("Error initializing semaphore");
         exit(1);
     }
    if(*shm_addr == 0){
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 1)) == ((union sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else if(*shm_addr > 0) {
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 0)) == ((union sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else {
        perror("Error initializing PARENT semaphore: BAD value Shared Memory");
        exit(1);
    }
 signal(2, sigterm_handler);
 signal(15, sigterm_handler);
 signal(17, sigchld_handler);
    int i;
    for(i = 0; i < 2; i++ ) {
        switch (fork()) {
            case -1 :
                perror("Fork Failed");
                exit(1);
                break;
            case 0 :
                signal(2, ((void (*)(int)) 0));
                signal(15, ((void (*)(int)) 0));
                signal(17, ((void (*)(int)) 0));
                child_create();
                exit(0);
            default :
                break;
        }
    }
    parent_create() ;
    return 0;
}

variable_types: argc int

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


repo: ecoluca/Kernel-Example

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r14
unsigned int v4; // er13
int v6; // ebp
int v7; // ebx
int v8; // eax
int v9; // edx
const char **v10; // rsi
unsigned int v11; // er10
size_t v12; // rax
unsigned int v14; // [rsp+0h] [rbp-48h]
int v15; // [rsp+4h] [rbp-44h]
unsigned int v16; // [rsp+4h] [rbp-44h]
int v17; // [rsp+8h] [rbp-40h]
const char **v18; // [rsp+8h] [rbp-40h]

v3 = 0LL;
v4 = 0;
v6 = 0;
v7 = 0;
v15 = 0;
v17 = 0;
v14 = 0;
while ( 1 )
{
v8 = getopt_long(argc, (char *const *)argv, "rw:pibnvh", &long_opts, 0LL);
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'b':
v4 = 1;
break;
case 'h':
myhello_usage(stdout);
exit(0);
case 'i':
v15 = 1;
break;
case 'n':
v14 = 1;
break;
case 'p':
v17 = 1;
break;
case 'r':
v7 = 1;
break;
case 'v':
myhello_version(stdout);
exit(0);
case 'w':
v3 = (const char *)optarg;
v6 = 1;
break;
default:
myhello_usage(stderr);
exit(1);
}
}
v9 = v15 + v17 + v7 + v6;
if ( v9 )
{
if ( v9 == 1 )
{
if ( optind != argc )
{
v10 = &argv[optind];
v11 = argc - optind;
if ( v7 )
{
v8 = myhello_read(v11, v10, v4);
}
else if ( v6 )
{
v18 = &argv[optind];
v16 = argc - optind;
v12 = strlen(v3);
v8 = myhello_write(v16, v18, v3, v12, v4, v14);
}
else if ( v17 )
{
v8 = myhello_poll(v11, v10, v4);
}
else if ( v15 )
{
v8 = myhello_ioctl(v11, v10, v4);
}
exit(v8 != 0);
}
fwrite("You have to provide at least a device.\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
fwrite(
"You have specified multiple functions.\nYou can only perform one function at a time.\n",
1uLL,
0x54uLL,
stderr);
exit(1);
}
fwrite("You have to provide a function.\n", 1uLL, 0x20uLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r14
unsigned int v4; // er13
int v6; // ebp
int v7; // ebx
int v8; // eax
int v9; // edx
const char **v10; // rsi
unsigned int v11; // er10
size_t v12; // rax
unsigned int v14; // [rsp+0h] [rbp-48h]
int v15; // [rsp+4h] [rbp-44h]
unsigned int v16; // [rsp+4h] [rbp-44h]
int v17; // [rsp+8h] [rbp-40h]
const char **v18; // [rsp+8h] [rbp-40h]

v3 = 0LL;
v4 = 0;
v6 = 0;
v7 = 0;
v15 = 0;
v17 = 0;
v14 = 0;
while ( 1 )
{
v8 = getopt_long(argc, (char *const *)argv, "rw:pibnvh", &long_opts, 0LL);
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'b':
v4 = 1;
break;
case 'h':
func8(stdout);
exit(0);
case 'i':
v15 = 1;
break;
case 'n':
v14 = 1;
break;
case 'p':
v17 = 1;
break;
case 'r':
v7 = 1;
break;
case 'v':
func7(stdout);
exit(0);
case 'w':
v3 = (const char *)optarg;
v6 = 1;
break;
default:
func8(stderr);
exit(1);
}
}
v9 = v15 + v17 + v7 + v6;
if ( v9 )
{
if ( v9 == 1 )
{
if ( optind != argc )
{
v10 = &argv[optind];
v11 = argc - optind;
if ( v7 )
{
v8 = func10(v11, v10, v4);
}
else if ( v6 )
{
v18 = &argv[optind];
v16 = argc - optind;
v12 = strlen(v3);
v8 = func9(v16, v18, v3, v12, v4, v14);
}
else if ( v17 )
{
v8 = func11(v11, v10, v4);
}
else if ( v15 )
{
v8 = func12(v11, v10, v4);
}
exit(v8 != 0);
}
fwrite("You have to provide at least a device.\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
fwrite(
"You have specified multiple functions.\nYou can only perform one function at a time.\n",
1uLL,
0x54uLL,
stderr);
exit(1);
}
fwrite("You have to provide a function.\n", 1uLL, 0x20uLL, stderr);
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
 int opt, opt_nonblock, opt_nonewline;
 int opt_read, opt_write, opt_poll, opt_info;
 char *arg_write;
 int retval = 0;
 opt_nonblock = opt_nonewline = 0;
 opt_read = opt_write = opt_poll = opt_info = 0;
 arg_write = ((void *)0);
 while ((opt = getopt_long(argc, argv, "rw:pibnvh", long_opts, ((void *)0))) != -1) {
  switch (opt) {
  case 'r':
   opt_read = 1;
   break;
  case 'w':
   opt_write = 1;
   arg_write = optarg;
   break;
  case 'p':
   opt_poll = 1;
   break;
  case 'i':
   opt_info = 1;
   break;
  case 'b':
   opt_nonblock = 1;
   break;
  case 'n':
   opt_nonewline = 1;
   break;
  case 'v':
   myhello_version(stdout);
   exit(0);
  case 'h':
   myhello_usage(stdout);
   exit(0);
  default:
   myhello_usage(stderr);
   exit(1);
  }
 }
 if (opt_read + opt_write + opt_poll + opt_info == 0) {
  fprintf(stderr, "You have to provide a function.\n");
  exit(1);
 }
 if (opt_read + opt_write + opt_poll + opt_info > 1) {
  fprintf(stderr, "You have specified multiple functions.\n"
   "You can only perform one function at a time.\n");
  exit(1);
 }
 if (argc == optind) {
  fprintf(stderr, "You have to provide at least a device.\n");
  exit(1);
 }
 argc -= optind;
 argv += optind;
 if (opt_read) {
  retval = myhello_read(argc, argv, opt_nonblock);
 } else if (opt_write) {
  retval = myhello_write(argc, argv, arg_write, strlen(arg_write),
   opt_nonblock, opt_nonewline);
 } else if (opt_poll) {
  retval = myhello_poll(argc, argv, opt_nonblock);
 } else if (opt_info) {
  retval = myhello_ioctl(argc, argv, opt_nonblock);
 } else {
  retval = -1;
 }
 exit(retval == 0 ? 0 : 1);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int opt; int opt_nonblock; int opt_nonewline;
 int opt_read; int opt_write; int opt_poll; int opt_info;
 char *arg_write;
 int retval = 0;
 opt_nonblock = opt_nonewline = 0;
 opt_read = opt_write = opt_poll = opt_info = 0;
 arg_write = ((void *)0);
 while ((opt = getopt_long(argc, argv, "rw:pibnvh", long_opts, ((void *)0))) != -1) {
  switch (opt) {
  case 'r':
   opt_read = 1;
   break;
  case 'w':
   opt_write = 1;
   arg_write = optarg;
   break;
  case 'p':
   opt_poll = 1;
   break;
  case 'i':
   opt_info = 1;
   break;
  case 'b':
   opt_nonblock = 1;
   break;
  case 'n':
   opt_nonewline = 1;
   break;
  case 'v':
   myhello_version(stdout);
   exit(0);
  case 'h':
   myhello_usage(stdout);
   exit(0);
  default:
   myhello_usage(stderr);
   exit(1);
  }
 }
 if (opt_read + opt_write + opt_poll + opt_info == 0) {
  fprintf(stderr, "You have to provide a function.\n");
  exit(1);
 }
 if (opt_read + opt_write + opt_poll + opt_info > 1) {
  fprintf(stderr, "You have specified multiple functions.\n"
   "You can only perform one function at a time.\n");
  exit(1);
 }
 if (argc == optind) {
  fprintf(stderr, "You have to provide at least a device.\n");
  exit(1);
 }
 argc -= optind;
 argv += optind;
 if (opt_read) {
  retval = myhello_read(argc, argv, opt_nonblock);
 } else if (opt_write) {
  retval = myhello_write(argc, argv, arg_write, strlen(arg_write),
   opt_nonblock, opt_nonewline);
 } else if (opt_poll) {
  retval = myhello_poll(argc, argv, opt_nonblock);
 } else if (opt_info) {
  retval = myhello_ioctl(argc, argv, opt_nonblock);
 } else {
  retval = -1;
 }
 exit(retval == 0 ? 0 : 1);
}

variable_types: opt int

variable_types: retval int


decompiled_code:
__int64 __fastcall myhello_write(int a1, const char **a2, const void *a3, size_t a4, int a5, int a6)
{
int v10; // eax
int v11; // ebp
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Write only to %s\n", *a2);
v10 = open(*a2, a5 == 0 ? 1 : 2049);
v11 = v10;
if ( v10 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
write(v10, a3, a4);
if ( !a6 )
write(v11, "\n", 1uLL);
close(v11);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, const char **a2, const void *a3, size_t a4, int a5, int a6)
{
int v10; // eax
int v11; // ebp
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Write only to %s\n", *a2);
v10 = open(*a2, a5 == 0 ? 1 : 2049);
v11 = v10;
if ( v10 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
write(v10, a3, a4);
if ( !a6 )
write(v11, "\n", 1uLL);
close(v11);
result = 0LL;
}
return result;
}

original_code:
static int myhello_write(int dev_no, char *dev[], char *buf,
 size_t size, int nonblock, int nonewline)
{
 int fd, flags, retval = 0;
 ssize_t count;
 if (dev_no != 1)
  fprintf(stderr, "Write only to %s\n", dev[0]);
 flags = nonblock ? (01 | 04000) : (01);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 count = write(fd, buf, size);
 if (!nonewline) {
  count += write(fd, "\n", 1);
 }
 close(fd);
 return retval;
err1:
 return retval;
}

canonical_original_code:
static int myhello_write(int dev_no, char *dev[], char *buf, unsigned long size, int nonblock, int nonewline)
{
 int fd; int flags; int retval = 0;
 long count;
 if (dev_no != 1)
  fprintf(stderr, "Write only to %s\n", dev[0]);
 flags = nonblock ? (01 | 04000) : (01);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 count = write(fd, buf, size);
 if (!nonewline) {
  count += write(fd, "\n", 1);
 }
 close(fd);
 return retval;
err1:
 return retval;
}

variable_types:
dev
char *[]

variable_types:
buf
char *

variable_types:
size
unsigned long

variable_types: nonblock int

variable_types: nonewline int

variable_types: fd int

variable_types: flags int

variable_types: count long


decompiled_code:
__int64 __fastcall myhello_read(int a1, const char **a2, int a3)
{
int v4; // er13
void *v5; // rbp
int v6; // eax
signed __int64 v7; // r12
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Read only from %s\n", *a2);
if ( a3 )
a3 = 2048;
v4 = open(*a2, a3);
if ( v4 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x1000uLL);
if ( v5 )
{
while ( 1 )
{
v7 = read(v4, v5, 0x1000uLL);
if ( v7 <= 0 )
break;
v6 = fileno(stdout);
write(v6, v5, v7);
}
free(v5);
close(v4);
result = 0LL;
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
close(v4);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, const char **a2, int a3)
{
int v4; // er13
void *v5; // rbp
int v6; // eax
signed __int64 v7; // r12
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Read only from %s\n", *a2);
if ( a3 )
a3 = 2048;
v4 = open(*a2, a3);
if ( v4 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x1000uLL);
if ( v5 )
{
while ( 1 )
{
v7 = read(v4, v5, 0x1000uLL);
if ( v7 <= 0 )
break;
v6 = fileno(stdout);
write(v6, v5, v7);
}
free(v5);
close(v4);
result = 0LL;
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
close(v4);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int myhello_read(int dev_no, char *dev[], int nonblock)
{
 int fd, flags, retval = 0;
 char *buf;
 size_t size;
 ssize_t count;
 if (dev_no != 1)
  fprintf(stderr, "Read only from %s\n", dev[0]);
 flags = nonblock ? (00 | 04000) : (00);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  count = read(fd, buf, size);
  if (count <= 0)
   break;
  count = write(fileno(stdout), buf, count);
 }
 free(buf);
 close(fd);
 return retval;
err2:
 close(fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_read(int dev_no, char *dev[], int nonblock)
{
 int fd; int flags; int retval = 0;
 char *buf;
 unsigned long size;
 long count;
 if (dev_no != 1)
  fprintf(stderr, "Read only from %s\n", dev[0]);
 flags = nonblock ? (00 | 04000) : (00);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  count = read(fd, buf, size);
  if (count <= 0)
   break;
  count = write(fileno(stdout), buf, count);
 }
 free(buf);
 close(fd);
 return retval;
err2:
 close(fd);
err1:
 return retval;
}


decompiled_code:
__int64 __fastcall myhello_poll(int a1, const char **a2, int a3)
{
int v3; // ebp
struct pollfd *v4; // rbx
__int16 *v5; // r13
const char **v6; // r15
int v7; // eax
void *v9; // r13
__int16 *v10; // r14
ssize_t v11; // rax
int v12; // eax
struct pollfd *v13; // rbp
int v14; // edi
size_t n; // [rsp+0h] [rbp-58h]

v3 = a3;
if ( a3 )
v3 = 2048;
v4 = (struct pollfd *)calloc(a1, 8uLL);
if ( a1 <= 0 )
{
LABEL_9:
v9 = malloc(0x1000uLL);
if ( v9 )
{
LABEL_10:
while ( poll(v4, a1, -1) >= 0 )
{
v10 = &v4->revents;
if ( a1 > 0 )
{
while ( 1 )
{
if ( (*(_BYTE *)v10 & 1) != 0 )
{
v11 = read(*(_DWORD *)(v10 - 3), v9, 0x1000uLL);
if ( v11 < 0 )
{
fprintf(stderr, "Can't read %s\n", *(const char **)((char *)v10 + (_QWORD)a2 - (_QWORD)v4 - 6));
goto LABEL_18;
}
n = v11;
v12 = fileno(stdout);
write(v12, v9, n);
*v10 &= 0xFFFEu;
}
v10 += 4;
if ( v10 == &v4[(unsigned int)(a1 - 1) + 1].revents )
goto LABEL_10;
}
}
}
fwrite("Can't poll devices\n", 1uLL, 0x13uLL, stderr);
LABEL_18:
free(v9);
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
}
if ( a1 > 0 )
{
v13 = &v4[a1];
do
{
v14 = v4->fd;
++v4;
close(v14);
}
while ( v13 != v4 );
}
}
else
{
v5 = &v4->events;
v6 = a2;
while ( 1 )
{
v7 = open(*v6, v3);
*((_DWORD *)v5 - 1) = v7;
*v5 = 1;
if ( v7 < 0 )
break;
++v6;
v5 += 4;
if ( v6 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_9;
}
fprintf(stderr, "Can't open %s\n", *v6);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, const char **a2, int a3)
{
int v3; // ebp
struct pollfd *v4; // rbx
__int16 *v5; // r13
const char **v6; // r15
int v7; // eax
void *v9; // r13
__int16 *v10; // r14
ssize_t v11; // rax
int v12; // eax
struct pollfd *v13; // rbp
int v14; // edi
size_t n; // [rsp+0h] [rbp-58h]

v3 = a3;
if ( a3 )
v3 = 2048;
v4 = (struct pollfd *)calloc(a1, 8uLL);
if ( a1 <= 0 )
{
LABEL_9:
v9 = malloc(0x1000uLL);
if ( v9 )
{
LABEL_10:
while ( poll(v4, a1, -1) >= 0 )
{
v10 = &v4->revents;
if ( a1 > 0 )
{
while ( 1 )
{
if ( (*(_BYTE *)v10 & 1) != 0 )
{
v11 = read(*(_DWORD *)(v10 - 3), v9, 0x1000uLL);
if ( v11 < 0 )
{
fprintf(stderr, "Can't read %s\n", *(const char **)((char *)v10 + (_QWORD)a2 - (_QWORD)v4 - 6));
goto LABEL_18;
}
n = v11;
v12 = fileno(stdout);
write(v12, v9, n);
*v10 &= 0xFFFEu;
}
v10 += 4;
if ( v10 == &v4[(unsigned int)(a1 - 1) + 1].revents )
goto LABEL_10;
}
}
}
fwrite("Can't poll devices\n", 1uLL, 0x13uLL, stderr);
LABEL_18:
free(v9);
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
}
if ( a1 > 0 )
{
v13 = &v4[a1];
do
{
v14 = v4->fd;
++v4;
close(v14);
}
while ( v13 != v4 );
}
}
else
{
v5 = &v4->events;
v6 = a2;
while ( 1 )
{
v7 = open(*v6, v3);
*((_DWORD *)v5 - 1) = v7;
*v5 = 1;
if ( v7 < 0 )
break;
++v6;
v5 += 4;
if ( v6 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_9;
}
fprintf(stderr, "Can't open %s\n", *v6);
}
return 0xFFFFFFFFLL;
}

original_code:
static int myhello_poll(int dev_no, char *dev[], int nonblock)
{
 int flags, i, retval = 0;
 struct pollfd *fds;
 char *buf;
 size_t size;
 ssize_t count;
 flags = nonblock ? (00 | 04000) : (00);
 fds = calloc(dev_no, sizeof(struct pollfd));
 for (i = 0; i < dev_no; i++) {
  fds[i].fd = open(dev[i], flags);
  fds[i].events = 0x001;
  if (fds[i].fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  if (poll(fds, dev_no, -1) < 0) {
   fprintf(stderr, "Can't poll devices\n");
   retval = -1;
   goto err3;
  }
  for (i = 0; i < dev_no; i++) {
   if (fds[i].revents & 0x001) {
    count = read(fds[i].fd, buf, size);
    if (count < 0) {
     fprintf(stderr, "Can't read %s\n", dev[i]);
     retval = -1;
     goto err3;
    }
    count = write(fileno(stdout), buf, count);
    fds[i].revents &= ~0x001;
   }
  }
 }
 free(buf);
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
 return retval;
err3:
 free(buf);
err2:
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_poll(int dev_no, char *dev[], int nonblock)
{
 int flags; int i; int retval = 0;
 struct pollfd *fds;
 char *buf;
 unsigned long size;
 long count;
 flags = nonblock ? (00 | 04000) : (00);
 fds = calloc(dev_no, sizeof(struct pollfd));
 for (i = 0; i < dev_no; i++) {
  fds[i].fd = open(dev[i], flags);
  fds[i].events = 0x001;
  if (fds[i].fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  if (poll(fds, dev_no, -1) < 0) {
   fprintf(stderr, "Can't poll devices\n");
   retval = -1;
   goto err3;
  }
  for (i = 0; i < dev_no; i++) {
   if (fds[i].revents & 0x001) {
    count = read(fds[i].fd, buf, size);
    if (count < 0) {
     fprintf(stderr, "Can't read %s\n", dev[i]);
     retval = -1;
     goto err3;
    }
    count = write(fileno(stdout), buf, count);
    fds[i].revents &= ~0x001;
   }
  }
 }
 free(buf);
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
 return retval;
err3:
 free(buf);
err2:
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
err1:
 return retval;
}

variable_types: i int

variable_types: struct pollfd { int fd; short events; short revents; } *


decompiled_code:
__int64 __fastcall myhello_ioctl(int a1, const char **a2, int a3)
{
const char **v4; // rbx
const char *v5; // rdx
int v6; // eax
int v7; // ebp
unsigned int v9; // [rsp+4h] [rbp-34h] BYREF
unsigned int v10; // [rsp+8h] [rbp-30h] BYREF
unsigned int v11[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = a2;
fwrite("device\t\tsize\tfree\tready\n", 1uLL, 0x18uLL, stdout);
fwrite("-------------------------------------\n", 1uLL, 0x26uLL, stdout);
if ( a3 )
a3 = 2048;
if ( a1 <= 0 )
return 0LL;
while ( 1 )
{
v6 = open(*v4, a3);
v7 = v6;
if ( v6 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v4);
return 0xFFFFFFFFLL;
}
if ( ioctl(v6, 0x80046D00uLL, &v9) < 0 || ioctl(v7, 0x80046D01uLL, &v10) < 0 || ioctl(v7, 0x80046D02uLL, v11) < 0 )
break;
v5 = *v4++;
fprintf(stdout, "%s\t%d\t%d\t%d\n", v5, v9, v10, v11[0]);
close(v7);
if ( v4 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
fprintf(stderr, "Can't ioctl %s\n", *v4);
close(v7);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, const char **a2, int a3)
{
const char **v4; // rbx
const char *v5; // rdx
int v6; // eax
int v7; // ebp
unsigned int v9; // [rsp+4h] [rbp-34h] BYREF
unsigned int v10; // [rsp+8h] [rbp-30h] BYREF
unsigned int v11[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = a2;
fwrite("device\t\tsize\tfree\tready\n", 1uLL, 0x18uLL, stdout);
fwrite("-------------------------------------\n", 1uLL, 0x26uLL, stdout);
if ( a3 )
a3 = 2048;
if ( a1 <= 0 )
return 0LL;
while ( 1 )
{
v6 = open(*v4, a3);
v7 = v6;
if ( v6 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v4);
return 0xFFFFFFFFLL;
}
if ( ioctl(v6, 0x80046D00uLL, &v9) < 0 || ioctl(v7, 0x80046D01uLL, &v10) < 0 || ioctl(v7, 0x80046D02uLL, v11) < 0 )
break;
v5 = *v4++;
fprintf(stdout, "%s\t%d\t%d\t%d\n", v5, v9, v10, v11[0]);
close(v7);
if ( v4 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
fprintf(stderr, "Can't ioctl %s\n", *v4);
close(v7);
return 0xFFFFFFFFLL;
}

original_code:
static int myhello_ioctl(int dev_no, char *dev[], int nonblock)
{
 int fd, flags, i, retval = 0;
 int dev_size, dev_free, dev_ready;
 fprintf(stdout, "device\t\tsize\tfree\tready\n");
 fprintf(stdout, "-------------------------------------\n");
 flags = nonblock ? (00 | 04000) : (00);
 for (i = 0; i < dev_no; i++) {
  fd = open(dev[i], flags);
  if (fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((0)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_size) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_free) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_ready) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  fprintf(stdout, "%s\t%d\t%d\t%d\n", dev[i],
   dev_size, dev_free, dev_ready);
  close(fd);
 }
 return retval;
err2:
 close(fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_ioctl(int dev_no, char *dev[], int nonblock)
{
 int fd; int flags; int i; int retval = 0;
 int dev_size; int dev_free; int dev_ready;
 fprintf(stdout, "device\t\tsize\tfree\tready\n");
 fprintf(stdout, "-------------------------------------\n");
 flags = nonblock ? (00 | 04000) : (00);
 for (i = 0; i < dev_no; i++) {
  fd = open(dev[i], flags);
  if (fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((0)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_size) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_free) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_ready) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  fprintf(stdout, "%s\t%d\t%d\t%d\n", dev[i],
   dev_size, dev_free, dev_ready);
  close(fd);
 }
 return retval;
err2:
 close(fd);
err1:
 return retval;
}


doMove

decompiled_code:
__int64 __fastcall doMove(int fd, void *buf, int *a3)
{
void *v4; // r12
int v5; // eax
__int64 result; // rax

v4 = calloc(0xFuLL, 1uLL);
addchar(v4);
if ( (int)read(*a3, v4, 0xFuLL) < 0 )
{
perror("Fehler beim Lesen der Pipe");
result = 1LL;
}
else
{
sendReplyFormatted((unsigned int)fd, v4);
v5 = recv(fd, buf, 0x1FFuLL, 0);
if ( v5 > 0 )
*((_BYTE *)buf + v5) = 0;
if ( !strcmp((const char *)buf, "+ MOVEOK\n") )
{
puts("\nZug wurde akzeptiert!");
puts("\n**********************************");
result = 0LL;
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, void *buf, int *a3)
{
void *v4; // r12
int v5; // eax
__int64 result; // rax

v4 = calloc(0xFuLL, 1uLL);
addchar(v4);
if ( (int)read(*a3, v4, 0xFuLL) < 0 )
{
perror("Fehler beim Lesen der Pipe");
result = 1LL;
}
else
{
sendReplyFormatted((unsigned int)fd, v4);
v5 = recv(fd, buf, 0x1FFuLL, 0);
if ( v5 > 0 )
*((_BYTE *)buf + v5) = 0;
if ( !strcmp((const char *)buf, "+ MOVEOK\n") )
{
puts("\nZug wurde akzeptiert!");
puts("\n**********************************");
result = 0LL;
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
int doMove(int sock, char* buffer, int fd[]) {
 int size;
 char* reply = calloc(15,sizeof(char));
 addchar(reply);
 size = read(fd[0], reply, 15);
 if (size < 0) {
  perror("Fehler beim Lesen der Pipe");
  return 1;
 }
 sendReplyFormatted(sock, reply);
 size = recv(sock, buffer, 512 - 1, 0);
 if (size > 0) {
  buffer[size] = '\0';
 }
 if (strcmp(buffer, "+ MOVEOK\n") == 0) {
  printf("\nZug wurde akzeptiert!\n");
  printf("\n**********************************\n");
 } else {
  return 1;
 }
 return 0;
}

canonical_original_code:
int doMove(int sock, char *buffer, int fd[]) {
 int size;
 char *reply = calloc(15,sizeof(char));
 addchar(reply);
 size = read(fd[0], reply, 15);
 if (size < 0) {
  perror("Fehler beim Lesen der Pipe");
  return 1;
 }
 sendReplyFormatted(sock, reply);
 size = recv(sock, buffer, 512 - 1, 0);
 if (size > 0) {
  buffer[size] = '\0';
 }
 if (strcmp(buffer, "+ MOVEOK\n") == 0) {
  printf("\nZug wurde akzeptiert!\n");
  printf("\n**********************************\n");
 } else {
  return 1;
 }
 return 0;
}

variable_types: sock int

variable_types:
buffer
char *

variable_types:
fd
int []

variable_types: size int

variable_types:
reply
char *


waitforfds

decompiled_code:
__int64 __fastcall waitforfds(int fd, void *a2, __int64 a3, int *a4)
{
int v4; // er13
int v5; // er12
__int64 v6; // r14
int v7; // ecx
__int64 v8; // rax
__int64 v9; // r12
__int64 v10; // rbx
int v11; // er13
int v12; // eax
__int64 v14; // [rsp+8h] [rbp-F0h]
int v15; // [rsp+18h] [rbp-E0h]
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

v4 = *a4;
if ( (unsigned int)((__int64 (*)(void))parseServerReply)() )
return 1LL;
v5 = v4 + 63;
v6 = 1LL << (fd % 64);
if ( v4 >= 0 )
v5 = v4;
v7 = v4 % 64;
v8 = v5 >> 6;
v9 = fd / 64;
v10 = v8;
if ( v4 < fd )
v4 = fd;
v14 = 1LL << v7;
v11 = v4 + 1;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v9] |= v6;
timeout.tv_sec = 30LL;
readfds.fds_bits[v10] |= v14;
timeout.tv_usec = 0LL;
v12 = select(v11, &readfds, 0LL, 0LL, &timeout);
if ( v12 == -1 )
break;
if ( v12 <= 0 )
{
if ( !v12 )
{
perror("Select timed out!. \n");
return 0LL;
}
}
else if ( (readfds.fds_bits[v9] & v6) != 0 )
{
v15 = recv(fd, a2, 0x1FFuLL, 0);
writelog(logdatei, "src/Select.c:98");
if ( v15 > 0 )
*((_BYTE *)a2 + v15) = 0;
if ( (unsigned int)parseServerReply((unsigned int)fd, a2, a3) )
return 1LL;
}
else if ( (readfds.fds_bits[v10] & v14) != 0 )
{
doMove(fd, a2, a4);
}
}
perror("Error, select failed! \n");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *a2, __int64 a3, int *a4)
{
int v4; // er13
int v5; // er12
__int64 v6; // r14
int v7; // ecx
__int64 v8; // rax
__int64 v9; // r12
__int64 v10; // rbx
int v11; // er13
int v12; // eax
__int64 v14; // [rsp+8h] [rbp-F0h]
int v15; // [rsp+18h] [rbp-E0h]
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

v4 = *a4;
if ( (unsigned int)((__int64 (*)(void))parseServerReply)() )
return 1LL;
v5 = v4 + 63;
v6 = 1LL << (fd % 64);
if ( v4 >= 0 )
v5 = v4;
v7 = v4 % 64;
v8 = v5 >> 6;
v9 = fd / 64;
v10 = v8;
if ( v4 < fd )
v4 = fd;
v14 = 1LL << v7;
v11 = v4 + 1;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v9] |= v6;
timeout.tv_sec = 30LL;
readfds.fds_bits[v10] |= v14;
timeout.tv_usec = 0LL;
v12 = select(v11, &readfds, 0LL, 0LL, &timeout);
if ( v12 == -1 )
break;
if ( v12 <= 0 )
{
if ( !v12 )
{
perror("Select timed out!. \n");
return 0LL;
}
}
else if ( (readfds.fds_bits[v9] & v6) != 0 )
{
v15 = recv(fd, a2, 0x1FFuLL, 0);
writelog(logdatei, "src/Select.c:98");
if ( v15 > 0 )
*((_BYTE *)a2 + v15) = 0;
if ( (unsigned int)parseServerReply((unsigned int)fd, a2, a3) )
return 1LL;
}
else if ( (readfds.fds_bits[v10] & v14) != 0 )
{
func0(fd, a2, a4);
}
}
perror("Error, select failed! \n");
return 1LL;
}

original_code:
int waitforfds(int sock, char* buffer, sharedmem * shm, int fd[]) {
 int rc, size, status, biggest;
 int pipe = fd[0];
 fd_set fds;
 struct timeval timeout;
 if (pipe > sock) {
  biggest = pipe;
 } else {
  biggest = sock;
 }
 status = parseServerReply(sock, buffer, shm);
 if (status != 0) {
  return 1;
 }
 do {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 30;
  timeout.tv_usec = 0;
  rc = select(biggest + 1, &fds, ((void *)0), ((void *)0), &timeout);
  if (rc == -1) {
   perror("Error, select failed! \n");
   return 1;
  }
  if (rc > 0) {
   if (((((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    size = recv(sock, buffer, 512 - 1, 0);
    writelog(logdatei, "src/Select.c" ":" "98");
    if (size > 0) {
     buffer[size] = '\0';
    }
    status = parseServerReply(sock, buffer, shm);
    if (status != 0) {
     return 1;
    }
   } else if (((((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    doMove(sock, buffer, fd);
   }
  } else if (rc == 0) {
   perror("Select timed out!. \n");
  }
 } while (rc != 0);
 return 0;
}

canonical_original_code:
int waitforfds(int sock, char *buffer, struct sharedmem *shm, int fd[]) {
 int rc; int size; int status; int biggest;
 int pipe = fd[0];
 struct fd_set fds;
 struct timeval timeout;
 if (pipe > sock) {
  biggest = pipe;
 } else {
  biggest = sock;
 }
 status = parseServerReply(sock, buffer, shm);
 if (status != 0) {
  return 1;
 }
 do {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 30;
  timeout.tv_usec = 0;
  rc = select(biggest + 1, &fds, ((void *)0), ((void *)0), &timeout);
  if (rc == -1) {
   perror("Error, select failed! \n");
   return 1;
  }
  if (rc > 0) {
   if (((((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    size = recv(sock, buffer, 512 - 1, 0);
    writelog(logdatei, "src/Select.c" ":" "98");
    if (size > 0) {
     buffer[size] = '\0';
    }
    status = parseServerReply(sock, buffer, shm);
    if (status != 0) {
     return 1;
    }
   } else if (((((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    doMove(sock, buffer, fd);
   }
  } else if (rc == 0) {
   perror("Select timed out!. \n");
  }
 } while (rc != 0);
 return 0;
}

variable_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; } player[8]; } *

variable_types: rc int

variable_types: status int

variable_types: biggest int

variable_types: pipe int

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

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

user_defined_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player player[8]; }


readGameField

original_code:
int readGameField(char *buffer, sharedmem * shm) {
 char* buffer2;
 buffer2 = malloc(sizeof(char) * 128);
 addchar(buffer2);
 int i = 0, znr = 0;
 char tmp = '*';
 buffer2 = strstr(buffer, "+ FIELD");
 buffer2 = strtok(buffer2, "\n");
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDFIELD") != 0) {
  sscanf(buffer2, "%*s %d %[0-9* ]", &znr, buffer2);
  for (i = 0; i < shm->fieldX; i++) {
   *(shm->pf + i + ((znr - 4) * (-1)) * shm->fieldX) = -1;
   if (buffer2 == strchr(buffer2, tmp)) {
    sscanf(buffer2, "%*s %[0-9* ]", buffer2);
   } else {
    sscanf(buffer2, "%d %[0-9* ]", (shm->pf + i + ((znr - 4) * (-1)) * (shm->fieldY)), buffer2);
   }
  }
  buffer2 = strtok(((void *)0), "\n");
 }
 return 0;
}

canonical_original_code:
int readGameField(char *buffer, struct sharedmem *shm) {
 char *buffer2;
 buffer2 = malloc(sizeof(char) * 128);
 addchar(buffer2);
 int i = 0; int znr = 0;
 char tmp = '*';
 buffer2 = strstr(buffer, "+ FIELD");
 buffer2 = strtok(buffer2, "\n");
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDFIELD") != 0) {
  sscanf(buffer2, "%*s %d %[0-9* ]", &znr, buffer2);
  for (i = 0; i < shm->fieldX; i++) {
   *(shm->pf + i + ((znr - 4) * (-1)) * shm->fieldX) = -1;
   if (buffer2 == strchr(buffer2, tmp)) {
    sscanf(buffer2, "%*s %[0-9* ]", buffer2);
   } else {
    sscanf(buffer2, "%d %[0-9* ]", (shm->pf + i + ((znr - 4) * (-1)) * (shm->fieldY)), buffer2);
   }
  }
  buffer2 = strtok(((void *)0), "\n");
 }
 return 0;
}

variable_types: znr int

variable_types: tmp char

user_defined_types: struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; }


printGameField

original_code:
int printGameField(sharedmem * shm) {
 int i, j;
 for (i = 0; i < shm->fieldY; i++) {
  printf("\n %d: ", i + 1);
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    printf(" *");
   } else {
    printf(" %d ", *(shm->pf + j + i * shm->fieldY));
   }
  }
 }
 return 0;
}

canonical_original_code:
int printGameField(struct sharedmem *shm) {
 int i; int j;
 for (i = 0; i < shm->fieldY; i++) {
  printf("\n %d: ", i + 1);
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    printf(" *");
   } else {
    printf(" %d ", *(shm->pf + j + i * shm->fieldY));
   }
  }
 }
 return 0;
}

variable_types: j int


handleRecv

decompiled_code:
__int64 __fastcall handleRecv(int a1, _BYTE *a2)
{
int v2; // eax
__int64 result; // rax

v2 = recv(a1, a2, 0x1FFuLL, 0);
if ( v2 > 0 )
a2[v2] = 0;
result = 0LL;
if ( *a2 == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, _BYTE *a2)
{
int v2; // eax
__int64 result; // rax

v2 = recv(a1, a2, 0x1FFuLL, 0);
if ( v2 > 0 )
a2[v2] = 0;
result = 0LL;
if ( *a2 == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", a2);
result = 1LL;
}
return result;
}

original_code:
int handleRecv(int sock, char* buffer) {
 int err;
 err = recv(sock, buffer, 512 - 1, 0);
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 return 0;
}

canonical_original_code:
int handleRecv(int sock, char *buffer) {
 int err;
 err = recv(sock, buffer, 512 - 1, 0);
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 return 0;
}

variable_types: err int


parseServerReply

decompiled_code:
__int64 __fastcall parseServerReply(int fd, char *haystack, __int64 a3)
{
char *v3; // rbp
int v6; // edi
int v7; // eax
unsigned int v8; // er13
__pid_t v9; // edi
unsigned int v11; // [rsp+Ch] [rbp-22Ch] BYREF
char v12[552]; // [rsp+10h] [rbp-228h] BYREF

v3 = haystack;
if ( *haystack == 45 )
{
v8 = 1;
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", haystack);
return v8;
}
if ( !memcmp(haystack, "+ MOVE", 6uLL) && strlen(v3) <= 0xE )
{
_isoc99_sscanf(v3, "%*s %*s %d", a3 + 112);
if ( (unsigned int)handleRecv(fd, v3) )
return 1;
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
printf("\nFuer deinen Zug hast du %d ms und ", *(unsigned int *)(a3 + 112));
}
else
{
if ( !strcmp(v3, "+ WAIT\n") )
{
while ( 1 )
{
puts("\nWarte auf andere Spieler.");
send(fd, "OKWAIT\n", 7uLL, 0);
if ( (unsigned int)handleRecv(fd, v3) )
return 1;
if ( strcmp(v3, "+ WAIT\n") )
{
if ( !memcmp(v3, "+ GAMEOVER", 0xAuLL) || !(unsigned int)handleRecv(fd, v3) )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
goto LABEL_6;
}
return 1;
}
}
}
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", a3 + 112, a3 + 132, a3 + 124, a3 + 128);
}
LABEL_6:
if ( strstr(v3, "+ GAMEOVER") )
{
if ( !memcmp(v3, "+ MOVEOK", 8uLL) )
{
strtok(v3, "\n");
v3 = strtok(0LL, "\n");
}
if ( !strcmp(v3, "+ GAMEOVER\n") || !memcmp(v3, "+ GAMEOVER\n+", 0xCuLL) )
{
puts("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.");
}
else
{
_isoc99_sscanf(v3, "%*s %*s %d %[^\n]\n", &v11, v12);
printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", v11, v12);
}
return 2;
}
if ( strstr(v3, "+ QUIT") )
{
puts("\nDas Spiel ist zu Ende.");
return 2;
}
printf("\nStein %d ist zu setzen!\n", *(unsigned int *)(a3 + 132));
printf(
"\nUnser momentanes Spielfeld mit Groesse %d x %d:\n",
*(unsigned int *)(a3 + 124),
*(unsigned int *)(a3 + 128));
v6 = *(_DWORD *)(a3 + 140);
if ( !v6 )
{
v7 = shmget(5678, 4 * *(int *)(a3 + 128) * (__int64)*(int *)(a3 + 124) + 3, 1021);
*(_DWORD *)(a3 + 140) = v7;
v6 = v7;
if ( v7 <= 0 )
{
writelog(logdatei, "src/ParseServerReply.c:176");
perror("\nFehler beim Initalisieren der Shared Memory pf");
return 1;
}
}
*(_QWORD *)(a3 + 144) = shmat(v6, 0LL, 0);
readGameField(v3, a3);
sendReplyFormatted((unsigned int)fd, "THINKING");
v8 = handleRecv(fd, v3);
if ( v8 || strcmp(v3, "+ OKTHINK\n") && (unsigned int)handleRecv(fd, v3) )
return 1;
v9 = *(_DWORD *)a3;
*(_QWORD *)(a3 + 116) = 0x100000001LL;
kill(v9, 10);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, char *haystack, __int64 a3)
{
char *v3; // rbp
int v6; // edi
int v7; // eax
unsigned int v8; // er13
__pid_t v9; // edi
unsigned int v11; // [rsp+Ch] [rbp-22Ch] BYREF
char v12[552]; // [rsp+10h] [rbp-228h] BYREF

v3 = haystack;
if ( *haystack == 45 )
{
v8 = 1;
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", haystack);
return v8;
}
if ( !memcmp(haystack, "+ MOVE", 6uLL) && strlen(v3) <= 0xE )
{
_isoc99_sscanf(v3, "%*s %*s %d", a3 + 112);
if ( (unsigned int)func2(fd, v3) )
return 1;
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
printf("\nFuer deinen Zug hast du %d ms und ", *(unsigned int *)(a3 + 112));
}
else
{
if ( !strcmp(v3, "+ WAIT\n") )
{
while ( 1 )
{
puts("\nWarte auf andere Spieler.");
send(fd, "OKWAIT\n", 7uLL, 0);
if ( (unsigned int)func2(fd, v3) )
return 1;
if ( strcmp(v3, "+ WAIT\n") )
{
if ( !memcmp(v3, "+ GAMEOVER", 0xAuLL) || !(unsigned int)func2(fd, v3) )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
goto LABEL_6;
}
return 1;
}
}
}
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", a3 + 112, a3 + 132, a3 + 124, a3 + 128);
}
LABEL_6:
if ( strstr(v3, "+ GAMEOVER") )
{
if ( !memcmp(v3, "+ MOVEOK", 8uLL) )
{
strtok(v3, "\n");
v3 = strtok(0LL, "\n");
}
if ( !strcmp(v3, "+ GAMEOVER\n") || !memcmp(v3, "+ GAMEOVER\n+", 0xCuLL) )
{
puts("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.");
}
else
{
_isoc99_sscanf(v3, "%*s %*s %d %[^\n]\n", &v11, v12);
printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", v11, v12);
}
return 2;
}
if ( strstr(v3, "+ QUIT") )
{
puts("\nDas Spiel ist zu Ende.");
return 2;
}
printf("\nStein %d ist zu setzen!\n", *(unsigned int *)(a3 + 132));
printf(
"\nUnser momentanes Spielfeld mit Groesse %d x %d:\n",
*(unsigned int *)(a3 + 124),
*(unsigned int *)(a3 + 128));
v6 = *(_DWORD *)(a3 + 140);
if ( !v6 )
{
v7 = shmget(5678, 4 * *(int *)(a3 + 128) * (__int64)*(int *)(a3 + 124) + 3, 1021);
*(_DWORD *)(a3 + 140) = v7;
v6 = v7;
if ( v7 <= 0 )
{
writelog(logdatei, "src/ParseServerReply.c:176");
perror("\nFehler beim Initalisieren der Shared Memory pf");
return 1;
}
}
*(_QWORD *)(a3 + 144) = shmat(v6, 0LL, 0);
func0(v3, a3);
sendReplyFormatted((unsigned int)fd, "THINKING");
v8 = func2(fd, v3);
if ( v8 || strcmp(v3, "+ OKTHINK\n") && (unsigned int)func2(fd, v3) )
return 1;
v9 = *(_DWORD *)a3;
*(_QWORD *)(a3 + 116) = 0x100000001LL;
kill(v9, 10);
return v8;
}

original_code:
int parseServerReply(int sock, char* buffer, sharedmem * shm) {
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 if (strncmp(buffer, "+ MOVE", 6) == 0 && (strlen(buffer) < 15)) {
  sscanf(buffer, "%*s %*s %d", &(shm->thinkTime));
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
  printf("\nFuer deinen Zug hast du %d ms und ", shm->thinkTime);
 }
 else if (strcmp(buffer, "+ WAIT\n") == 0) {
  do {
   printf("\nWarte auf andere Spieler.\n");
   send(sock, "OKWAIT\n", strlen("OKWAIT\n"), 0);
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  } while (strcmp(buffer, "+ WAIT\n") == 0);
  if (strncmp(buffer, "+ GAMEOVER", 10) != 0) {
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 } else {
  sscanf(buffer, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", &(shm->thinkTime), &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 }
 if (strstr(buffer, "+ GAMEOVER") != ((void *)0) ) {
  int playerNumber;
  char playerName[512];
  if (strncmp(buffer, "+ MOVEOK", 8) == 0) {
   buffer = strtok(buffer, "\n");
   buffer = strtok(((void *)0), "\n");
  }
  if (strcmp(buffer, "+ GAMEOVER\n") == 0 || strncmp(buffer, "+ GAMEOVER\n+", 12) == 0) {
   printf("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.\n");
  } else {
   sscanf(buffer, "%*s %*s %d %[^\n]\n", &playerNumber, playerName);
   printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", playerNumber, playerName);
  }
  return 2;
 }
 if (strstr(buffer, "+ QUIT") != ((void *)0) ) {
  printf("\nDas Spiel ist zu Ende.\n");
  return 2;
 }
 printf("\nStein %d ist zu setzen!\n", shm->StoneToPlace);
 printf("\nUnser momentanes Spielfeld mit Groesse %d x %d:\n", shm->fieldX, shm->fieldY);
 if (shm->pfID == 0) {
  shm->pfID = shmget(5678, (sizeof(int) * (shm->fieldX) * (shm->fieldY) + 3), 01000 | 0775);
  if (shm->pfID < 1) {
   writelog(logdatei, "src/ParseServerReply.c" ":" "176");
   perror("\nFehler beim Initalisieren der Shared Memory pf");
   return 1;
  }
 }
 shm->pf = shmat(shm->pfID, 0, 0);
 readGameField(buffer, shm);
 sendReplyFormatted(sock, "THINKING");
 if (handleRecv(sock, buffer) != 0) {
  return 1;
 }
 if (strcmp(buffer, "+ OKTHINK\n") != 0) {
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
 }
 shm->thinking = 1;
 shm->pleaseThink = 1;
 kill(shm->pidDad, 10);
 return 0;
}

canonical_original_code:
int parseServerReply(int sock, char *buffer, struct sharedmem *shm) {
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 if (strncmp(buffer, "+ MOVE", 6) == 0 && (strlen(buffer) < 15)) {
  sscanf(buffer, "%*s %*s %d", &(shm->thinkTime));
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
  printf("\nFuer deinen Zug hast du %d ms und ", shm->thinkTime);
 }
 else if (strcmp(buffer, "+ WAIT\n") == 0) {
  do {
   printf("\nWarte auf andere Spieler.\n");
   send(sock, "OKWAIT\n", strlen("OKWAIT\n"), 0);
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  } while (strcmp(buffer, "+ WAIT\n") == 0);
  if (strncmp(buffer, "+ GAMEOVER", 10) != 0) {
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 } else {
  sscanf(buffer, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", &(shm->thinkTime), &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 }
 if (strstr(buffer, "+ GAMEOVER") != ((void *)0) ) {
  int playerNumber;
  char playerName[512];
  if (strncmp(buffer, "+ MOVEOK", 8) == 0) {
   buffer = strtok(buffer, "\n");
   buffer = strtok(((void *)0), "\n");
  }
  if (strcmp(buffer, "+ GAMEOVER\n") == 0 || strncmp(buffer, "+ GAMEOVER\n+", 12) == 0) {
   printf("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.\n");
  } else {
   sscanf(buffer, "%*s %*s %d %[^\n]\n", &playerNumber, playerName);
   printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", playerNumber, playerName);
  }
  return 2;
 }
 if (strstr(buffer, "+ QUIT") != ((void *)0) ) {
  printf("\nDas Spiel ist zu Ende.\n");
  return 2;
 }
 printf("\nStein %d ist zu setzen!\n", shm->StoneToPlace);
 printf("\nUnser momentanes Spielfeld mit Groesse %d x %d:\n", shm->fieldX, shm->fieldY);
 if (shm->pfID == 0) {
  shm->pfID = shmget(5678, (sizeof(int) * (shm->fieldX) * (shm->fieldY) + 3), 01000 | 0775);
  if (shm->pfID < 1) {
   writelog(logdatei, "src/ParseServerReply.c" ":" "176");
   perror("\nFehler beim Initalisieren der Shared Memory pf");
   return 1;
  }
 }
 shm->pf = shmat(shm->pfID, 0, 0);
 readGameField(buffer, shm);
 sendReplyFormatted(sock, "THINKING");
 if (handleRecv(sock, buffer) != 0) {
  return 1;
 }
 if (strcmp(buffer, "+ OKTHINK\n") != 0) {
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
 }
 shm->thinking = 1;
 shm->pleaseThink = 1;
 kill(shm->pidDad, 10);
 return 0;
}

variable_types: playerNumber int

variable_types:
playerName
char [512]


sendReplyFormatted

decompiled_code:
void __fastcall sendReplyFormatted(int fd, void *src)
{
size_t v2; // rbx
char *v3; // rbp

v2 = strlen((const char *)src);
v3 = (char *)malloc(v2 + 2);
memcpy(v3, src, v2);
*(_WORD *)&v3[v2] = 10;
send(fd, v3, v2 + 1, 0);
free(v3);
}

canonical_decompiled_code:
void __fastcall func0(int fd, void *src)
{
size_t v2; // rbx
char *v3; // rbp

v2 = strlen((const char *)src);
v3 = (char *)malloc(v2 + 2);
memcpy(v3, src, v2);
*(_WORD *)&v3[v2] = 10;
send(fd, v3, v2 + 1, 0);
free(v3);
}

original_code:
void sendReplyFormatted(int sock, char* reply) {
 char * container;
 container = malloc(sizeof(char) * (strlen(reply) + 2));
 strcpy(container, reply);
 strcat(container, "\n");
 send(sock, container, strlen(container), 0);
 free(container);
}

canonical_original_code:
void sendReplyFormatted(int sock, char *reply) {
 char *container;
 container = malloc(sizeof(char) * (strlen(reply) + 2));
 strcpy(container, reply);
 strcat(container, "\n");
 send(sock, container, strlen(container), 0);
 free(container);
}

variable_types: sock int

variable_types:
container
char *


antistrcat

decompiled_code:
__int64 __fastcall antistrcat(char *src, const char *a2, char *a3)
{
char *v3; // rax

v3 = stpcpy(a3, a2);
strcpy(v3, src);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src, const char *a2, char *a3)
{
char *v3; // rax

v3 = stpcpy(a3, a2);
strcpy(v3, src);
return 0LL;
}

original_code:
int antistrcat(char* dest, char* src, char* temp) {
 strcpy(temp, src);
 strcat(temp, dest);
 return 0;
}

canonical_original_code:
int antistrcat(char *dest, char *src, char *temp) {
 strcpy(temp, src);
 strcat(temp, dest);
 return 0;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
temp
char *