decompiled_code:
__int64 __fastcall readADXL345(int fd)
{
__int64 result; // rax
int v2; // eax
char buf[5]; // [rsp+8h] [rbp-10h] BYREF
char v4; // [rsp+Dh] [rbp-Bh]

selectDevice(fd, 0x53u, "ADXL345");
writeToDevice(fd, "2", 1);
if ( read(fd, buf, 6uLL) == 6 )
{
reported_25 = 0;
rawAcc[0] = -((buf[1] << 8) | buf[0]);
rawAcc[1] = -((buf[3] << 8) | buf[2]);
v2 = v4 << 8;
LOWORD(v2) = buf[4];
result = (v4 << 8) | (unsigned int)v2;
rawAcc[2] = result;
}
else
{
result = (unsigned int)reported_25;
if ( !reported_25 )
{
result = fwrite("Unable to read from ADXL345\n", 1uLL, 0x1CuLL, stderr);
reported_25 = 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd)
{
__int64 result; // rax
int v2; // eax
char buf[5]; // [rsp+8h] [rbp-10h] BYREF
char v4; // [rsp+Dh] [rbp-Bh]

func3(fd, 0x53u, "ADXL345");
func4(fd, "2", 1);
if ( read(fd, buf, 6uLL) == 6 )
{
reported_25 = 0;
rawAcc[0] = -((buf[1] << 8) | buf[0]);
rawAcc[1] = -((buf[3] << 8) | buf[2]);
v2 = v4 << 8;
LOWORD(v2) = buf[4];
result = (v4 << 8) | (unsigned int)v2;
rawAcc[2] = result;
}
else
{
result = (unsigned int)reported_25;
if ( !reported_25 )
{
result = fwrite("Unable to read from ADXL345\n", 1uLL, 0x1CuLL, stderr);
reported_25 = 1;
}
}
return result;
}

original_code:
void readADXL345(int i2c)
{
   char buf[8];
   static int reported = 0;
   selectDevice(i2c, 0x53, "ADXL345");
   writeToDevice(i2c, "\x32", 1);
   if (read(i2c, buf, 6) != 6)
   {
      if (!reported)
      {
         fprintf(stderr, "Unable to read from ADXL345\n");
         reported = 1;
      }
   }
   else
   {
      reported = 0;
      {rawAcc[0] = -((buf[1]<<8) | buf[0]); rawAcc[1] = -((buf[3]<<8) | buf[2]); rawAcc[2] = ((buf[5]<<8) | buf[4]);};
   }
}

variable_types:
buf
char [8]


decompiled_code:
size_t __fastcall readITG3200(int fd)
{
size_t result; // rax
int v2; // eax
char buf[4]; // [rsp+8h] [rbp-10h] BYREF
char v4; // [rsp+Ch] [rbp-Ch]
char v5; // [rsp+Dh] [rbp-Bh]

selectDevice(fd, 0x68u, "ITG3200");
writeToDevice(fd, &unk_D76, 1);
if ( read(fd, buf, 6uLL) == 6 )
{
reported_24 = 0;
rawGyr[0] = (buf[2] << 8) | buf[3];
rawGyr[1] = -((buf[0] << 8) | buf[1]);
v2 = v4 << 8;
LOWORD(v2) = v5;
result = -((v4 << 8) | v2);
rawGyr[2] = result;
}
else
{
result = (unsigned int)reported_24;
if ( !reported_24 )
{
result = fwrite("Unable to read from ITG3200\n", 1uLL, 0x1CuLL, stderr);
reported_24 = 1;
}
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func6(int fd)
{
size_t result; // rax
int v2; // eax
char buf[4]; // [rsp+8h] [rbp-10h] BYREF
char v4; // [rsp+Ch] [rbp-Ch]
char v5; // [rsp+Dh] [rbp-Bh]

func3(fd, 0x68u, "ITG3200");
func4(fd, &unk_D76, 1);
if ( read(fd, buf, 6uLL) == 6 )
{
reported_24 = 0;
rawGyr[0] = (buf[2] << 8) | buf[3];
rawGyr[1] = -((buf[0] << 8) | buf[1]);
v2 = v4 << 8;
LOWORD(v2) = v5;
result = -((v4 << 8) | v2);
rawGyr[2] = result;
}
else
{
result = (unsigned int)reported_24;
if ( !reported_24 )
{
result = fwrite("Unable to read from ITG3200\n", 1uLL, 0x1CuLL, stderr);
reported_24 = 1;
}
}
return result;
}

original_code:
void readITG3200(int i2c)
{
   char buf[8];
   static int reported = 0;
   selectDevice(i2c, 0x68, "ITG3200");
   writeToDevice(i2c, "\x1D", 1);
   if (read(i2c, buf, 6) != 6)
   {
      if (!reported)
      {
         fprintf(stderr, "Unable to read from ITG3200\n");
         reported = 1;
      }
   }
   else
   {
      reported = 0;
      {rawGyr[0] = ((buf[2]<<8) | buf[3]); rawGyr[1] = -((buf[0]<<8) | buf[1]); rawGyr[2] = -((buf[4]<<8) | buf[5]);};
   }
}

variable_types: reported int

return_type: void


decompiled_code:
__int64 initI2Cdevices()
{
int v0; // eax
unsigned int v1; // er12

v0 = open("/dev/i2c-0", 2);
if ( v0 < 0 )
{
perror("Failed to open i2c bus");
exit(1);
}
v1 = v0;
selectDevice(v0, 0x53u, "ADXL345");
writeToDevice(v1, &unk_E80, 2);
writeToDevice(v1, &unk_DB7, 2);
writeToDevice(v1, &unk_DBA, 2);
writeToDevice(v1, &unk_E83, 2);
writeToDevice(v1, &unk_DBD, 2);
selectDevice(v1, 0x68u, "ITG3200");
writeToDevice(v1, &unk_DC0, 2);
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
unsigned int v1; // er12

v0 = open("/dev/i2c-0", 2);
if ( v0 < 0 )
{
perror("Failed to open i2c bus");
exit(1);
}
v1 = v0;
func3(v0, 0x53u, "ADXL345");
func4(v1, &unk_E80, 2);
func4(v1, &unk_DB7, 2);
func4(v1, &unk_DBA, 2);
func4(v1, &unk_E83, 2);
func4(v1, &unk_DBD, 2);
func3(v1, 0x68u, "ITG3200");
func4(v1, &unk_DC0, 2);
return v1;
}

original_code:
int initI2Cdevices(void)
{
   int i2c;
   if ((i2c = open("/dev/i2c-0", 02)) < 0)
   {
      perror("Failed to open i2c bus");
      exit(1);
   }
   selectDevice(i2c, 0x53, "ADXL345");
   writeToDevice(i2c, "\x2d\x00", 2);
   writeToDevice(i2c, "\x2d\x10", 2);
   writeToDevice(i2c, "\x2d\x08", 2);
   writeToDevice(i2c, "\x31\x00", 2);
   writeToDevice(i2c, "\x31\x0b", 2);
   selectDevice(i2c, 0x68, "ITG3200");
   writeToDevice(i2c, "\x16\b00011000", 2);
   return i2c;
}

canonical_original_code:
int initI2Cdevices()
{
   int i2c;
   if ((i2c = open("/dev/i2c-0", 02)) < 0)
   {
      perror("Failed to open i2c bus");
      exit(1);
   }
   selectDevice(i2c, 0x53, "ADXL345");
   writeToDevice(i2c, "\x2d\x00", 2);
   writeToDevice(i2c, "\x2d\x10", 2);
   writeToDevice(i2c, "\x2d\x08", 2);
   writeToDevice(i2c, "\x31\x00", 2);
   writeToDevice(i2c, "\x31\x0b", 2);
   selectDevice(i2c, 0x68, "ITG3200");
   writeToDevice(i2c, "\x16\b00011000", 2);
   return i2c;
}

return_type: int


estimateAngle

original_code:
float estimateAngle(int acc, int gyro, float oldAng, int elapsed)
{
   float angleAcc, angleInc, estAngle;
   float secs;
   secs = (float) elapsed / 1e6f;
   angleAcc = (float) acc * 90.0f / 256.0f;
   angleInc = (float) gyro * secs * 2000.0f / 32768.0f;
   estAngle = 0.75 * (oldAng + angleInc) + 0.25 * angleAcc;
   return estAngle;
}

canonical_original_code:
float estimateAngle(int acc, int gyro, float oldAng, int elapsed)
{
   float angleAcc; float angleInc; float estAngle;
   float secs;
   secs = (float) elapsed / 1e6f;
   angleAcc = (float) acc * 90.0f / 256.0f;
   angleInc = (float) gyro * secs * 2000.0f / 32768.0f;
   estAngle = 0.75 * (oldAng + angleInc) + 0.25 * angleAcc;
   return estAngle;
}

variable_types: acc int

variable_types: gyro int

variable_types: oldAng float

variable_types: elapsed int

variable_types: angleAcc float

variable_types: angleInc float

variable_types: estAngle float

variable_types: secs float

return_type: float


servoTick

decompiled_code:
__int64 servoTick()
{
int v0; // ecx
int v1; // ecx
int v2; // ecx
__int64 result; // rax

gpioServo(17LL, (unsigned int)wid1_23);
gpioServo(18LL, (unsigned int)wid2_22);
gpioServo(21LL, (unsigned int)wid3_21);
v0 = wid1_23;
wid1_23 += inc1_20;
if ( (unsigned int)(wid1_23 - 1000) > 0x3E8 )
{
wid1_23 = v0;
inc1_20 = -inc1_20;
}
v1 = wid2_22;
wid2_22 += inc2_19;
if ( (unsigned int)(wid2_22 - 1000) > 0x3E8 )
{
wid2_22 = v1;
inc2_19 = -inc2_19;
}
v2 = wid3_21;
wid3_21 += inc3_18;
result = (unsigned int)(wid3_21 - 1000);
if ( (unsigned int)result > 0x3E8 )
{
wid3_21 = v2;
inc3_18 = -inc3_18;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // ecx
int v1; // ecx
int v2; // ecx
__int64 result; // rax

gpioServo(17LL, (unsigned int)wid1_23);
gpioServo(18LL, (unsigned int)wid2_22);
gpioServo(21LL, (unsigned int)wid3_21);
v0 = wid1_23;
wid1_23 += inc1_20;
if ( (unsigned int)(wid1_23 - 1000) > 0x3E8 )
{
wid1_23 = v0;
inc1_20 = -inc1_20;
}
v1 = wid2_22;
wid2_22 += inc2_19;
if ( (unsigned int)(wid2_22 - 1000) > 0x3E8 )
{
wid2_22 = v1;
inc2_19 = -inc2_19;
}
v2 = wid3_21;
wid3_21 += inc3_18;
result = (unsigned int)(wid3_21 - 1000);
if ( (unsigned int)result > 0x3E8 )
{
wid3_21 = v2;
inc3_18 = -inc3_18;
}
return result;
}

canonical_original_code:
void servoTick()
{
   static int wid1 = 1500; static int wid2 = 1500; static int wid3 = 1500;
   static int inc1 = 50; static int inc2 = 75; static int inc3 = 100;
   gpioServo(17, wid1);
   gpioServo(18, wid2);
   gpioServo(21, wid3);
   wid1+=inc1; if ((wid1<1000) || (wid1>2000)) {inc1 = -inc1; wid1+=inc1;}
   wid2+=inc2; if ((wid2<1000) || (wid2>2000)) {inc2 = -inc2; wid2+=inc2;}
   wid3+=inc3; if ((wid3<1000) || (wid3>2000)) {inc3 = -inc3; wid3+=inc3;}
}


putTTY

decompiled_code:
ssize_t __fastcall putTTY(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func10(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
return write(1, buf, v1);
}

original_code:
void putTTY(char * buf)
{
   write(1, buf, strlen(buf));
}

canonical_original_code:
void putTTY(char *buf)
{
   write(1, buf, strlen(buf));
}

variable_types:
buf
char *


putTTYstr

decompiled_code:
ssize_t __fastcall putTTYstr(unsigned int a1, unsigned int a2, const char *a3)
{
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

sprintf(v4, "\x1B[%d;%dH%s", a1, a2, a3);
return putTTY(v4);
}

canonical_decompiled_code:
ssize_t __fastcall func11(unsigned int a1, unsigned int a2, const char *a3)
{
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

sprintf(v4, "\x1B[%d;%dH%s", a1, a2, a3);
return func10(v4);
}

original_code:
void putTTYstr(int row, int col, char * buf)
{
   char str[256];
   sprintf(str, "\033[%d;%dH%s", row, col, buf);
   putTTY(str);
}

canonical_original_code:
void putTTYstr(int row, int col, char *buf)
{
   char str[256];
   sprintf(str, "\033[%d;%dH%s", row, col, buf);
   putTTY(str);
}

variable_types: row int

variable_types: col int

variable_types:
str
char [256]


motorTick

decompiled_code:
ssize_t motorTick()
{
__int64 i; // rbx
int v1; // esi
bool v2; // sf
__int64 v3; // rsi
__int64 v4; // r8
unsigned int v5; // ebp
__int64 v6; // rcx
ssize_t result; // rax
int v8; // ecx
char v9[280]; // [rsp+0h] [rbp-118h] BYREF

for ( i = 0LL; ; ++i )
{
v1 = inc_29[i];
v2 = speed_30[i] + v1 < 0;
v3 = (unsigned int)(speed_30[i] + v1);
speed_30[i] = v3;
if ( v2 )
{
gpioPWM((unsigned int)gpio_in1_28[i], (unsigned int)-(int)v3);
gpioPWM((unsigned int)gpio_in2_27[i], 0LL);
v8 = speed_30[i];
if ( v8 < -205 )
inc_29[i] = -inc_29[i];
v5 = i + 1;
v6 = (unsigned int)-v8;
v4 = 0LL;
}
else
{
gpioPWM((unsigned int)gpio_in2_27[i], v3);
gpioPWM((unsigned int)gpio_in1_28[i], 0LL);
v4 = (unsigned int)speed_30[i];
if ( (int)v4 > 205 )
inc_29[i] = -inc_29[i];
v5 = i + 1;
v6 = 0LL;
}
sprintf(v9, "MOT%d IN1=%3d IN2=%3d", v5, v6, v4);
if ( i )
break;
putTTYstr(5u, 1u, v9);
LABEL_10:
;
}
result = putTTYstr(7u, 1u, v9);
if ( v5 != 2 )
goto LABEL_10;
return result;
}

canonical_decompiled_code:
ssize_t func12()
{
__int64 i; // rbx
int v1; // esi
bool v2; // sf
__int64 v3; // rsi
__int64 v4; // r8
unsigned int v5; // ebp
__int64 v6; // rcx
ssize_t result; // rax
int v8; // ecx
char v9[280]; // [rsp+0h] [rbp-118h] BYREF

for ( i = 0LL; ; ++i )
{
v1 = inc_29[i];
v2 = speed_30[i] + v1 < 0;
v3 = (unsigned int)(speed_30[i] + v1);
speed_30[i] = v3;
if ( v2 )
{
gpioPWM((unsigned int)gpio_in1_28[i], (unsigned int)-(int)v3);
gpioPWM((unsigned int)gpio_in2_27[i], 0LL);
v8 = speed_30[i];
if ( v8 < -205 )
inc_29[i] = -inc_29[i];
v5 = i + 1;
v6 = (unsigned int)-v8;
v4 = 0LL;
}
else
{
gpioPWM((unsigned int)gpio_in2_27[i], v3);
gpioPWM((unsigned int)gpio_in1_28[i], 0LL);
v4 = (unsigned int)speed_30[i];
if ( (int)v4 > 205 )
inc_29[i] = -inc_29[i];
v5 = i + 1;
v6 = 0LL;
}
sprintf(v9, "MOT%d IN1=%3d IN2=%3d", v5, v6, v4);
if ( i )
break;
func11(5u, 1u, v9);
LABEL_10:
;
}
result = func11(7u, 1u, v9);
if ( v5 != 2 )
goto LABEL_10;
return result;
}

original_code:
void motorTick(void)
{
   static int gpio_in1[2]={8, 9};
   static int gpio_in2[2]={7, 11};
   static int speed [2]={ 80, 80};
   static int inc [2]={ -50, 50};
   int i;
   char str[256];
   for (i=0; i<2; i++)
   {
      speed[i]+=inc[i];
      if (speed[i]<0)
      {
         gpioPWM(gpio_in1[i], -speed[i]);
         gpioPWM(gpio_in2[i], 0);
         if (speed[i] < -205) inc[i] = -inc[i];
         sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, -speed[i], 0);
      }
      else
      {
         gpioPWM(gpio_in2[i], speed[i]);
         gpioPWM(gpio_in1[i], 0);
         if (speed[i] > 205) inc[i] = -inc[i];
         sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, 0, speed[i]);
      }
      if (i) putTTYstr(7, 1, str); else putTTYstr(5, 1, str);
   }
}

canonical_original_code:
void motorTick()
{
   static int gpio_in1[2] = {8, 9};
   static int gpio_in2[2] = {7, 11};
   static int speed[2] = { 80, 80};
   static int inc[2] = { -50, 50};
   int i;
   char str[256];
   for (i=0; i<2; i++)
   {
      speed[i]+=inc[i];
      if (speed[i]<0)
      {
         gpioPWM(gpio_in1[i], -speed[i]);
         gpioPWM(gpio_in2[i], 0);
         if (speed[i] < -205) inc[i] = -inc[i];
         sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, -speed[i], 0);
      }
      else
      {
         gpioPWM(gpio_in2[i], speed[i]);
         gpioPWM(gpio_in1[i], 0);
         if (speed[i] > 205) inc[i] = -inc[i];
         sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, 0, speed[i]);
      }
      if (i) putTTYstr(7, 1, str); else putTTYstr(5, 1, str);
   }
}

variable_types:
speed
int [2]

variable_types:
inc
int [2]

variable_types: i int


decompiled_code:
ssize_t i2cTick()
{
int v0; // eax
int v1; // ebp
int v2; // ebx
int v3; // edi
int v4; // esi
int v5; // edx
int v6; // eax
int v7; // er8
int v8; // edi
int v9; // esi
int v10; // ecx
int v11; // edx
ssize_t result; // rax
int v13; // ecx
__int64 v14; // rdx
float v15; // xmm0_4
int v16; // edi
int v17; // esi
int v18; // edx
float v19; // xmm0_4
int v20; // edi
int v21; // esi
float v22; // xmm3_4
int v23; // edx
unsigned int v24; // eax
__int64 v25; // rsi
__int64 v26; // rsi
__int64 v27; // rsi
char v28[280]; // [rsp+0h] [rbp-118h] BYREF

if ( inited_17 )
{
v0 = gpioTick();
v1 = lastTick_16;
v2 = v0;
lastTick_16 = v0;
readADXL345(i2c_15);
readITG3200(i2c_15);
if ( calibrated_14 )
{
v15 = estimateAngle(rawAcc[0], rawGyr[0] - gyroCalibX_12, v2 - v1, *(float *)&X_13);
v16 = rawAcc[1];
v17 = rawGyr[1];
X_13 = LODWORD(v15);
v19 = estimateAngle(v16, v17 - gyroCalibY_10, v18, *(float *)&Y_11);
v20 = rawAcc[2];
v21 = rawGyr[2];
v22 = v19;
Y_11 = LODWORD(v19);
Z_9 = estimateAngle(v20, v21 - gyroCalibZ_8, v23, *(float *)&Z_9);
v25 = (unsigned int)(int)(float)((float)((float)(v22 * 1000.0) / 90.0) + 1500.0);
if ( (int)v25 > 2500 )
v25 = v24;
if ( (int)v25 < 500 )
v25 = 500LL;
gpioServo(17LL, v25);
v26 = (unsigned int)(int)(float)(1500.0 - (float)((float)(500.0 * *(float *)&X_13) / 90.0));
if ( (int)v26 > 2000 )
v26 = 2000LL;
if ( (int)v26 < 1000 )
v26 = 1000LL;
gpioServo(18LL, v26);
v27 = (unsigned int)(int)(float)(1500.0 - (float)((float)(500.0 * *(float *)&Y_11) / 90.0));
if ( (int)v27 > 2200 )
v27 = 2200LL;
if ( (int)v27 < 800 )
v27 = 800LL;
gpioServo(21LL, v27);
sprintf(v28, "X=%4.0f Y=%4.0f Z=%4.0f ", *(float *)&X_13, *(float *)&Y_11, *(float *)&Z_9);
result = putTTYstr(1u, 1u, v28);
}
else
{
v3 = rawAcc[1];
v4 = rawAcc[2];
v5 = rawGyr[1];
v6 = rawGyr[2];
v7 = accCalibX_7 + rawAcc[0];
++calibrations_4;
v8 = accCalibY_6 + v3;
v9 = accCalibZ_5 + v4;
accCalibX_7 = v7;
v10 = gyroCalibX_12 + rawGyr[0];
v11 = gyroCalibY_10 + v5;
accCalibY_6 = v8;
result = (unsigned int)(gyroCalibZ_8 + v6);
accCalibZ_5 = v9;
gyroCalibX_12 = v10;
gyroCalibY_10 = v11;
gyroCalibZ_8 = result;
if ( calibrations_4 > 199 )
{
calibrated_14 = 1;
accCalibX_7 = v7 / 200;
accCalibY_6 = v8 / 200;
accCalibZ_5 = v9 / 200;
gyroCalibX_12 = v10 / 200;
v13 = v11 / 200;
v14 = (int)result;
result = (unsigned int)((int)result >> 31);
gyroCalibY_10 = v13;
gyroCalibZ_8 = ((int)((unsigned __int64)(1374389535 * v14) >> 32) >> 6) - result;
}
}
}
else
{
i2c_15 = initI2Cdevices();
gpioServo(17LL, 1500LL);
gpioServo(18LL, 1500LL);
result = gpioServo(21LL, 1500LL);
inited_17 = 1;
}
return result;
}

canonical_decompiled_code:
ssize_t func13()
{
int v0; // eax
int v1; // ebp
int v2; // ebx
int v3; // edi
int v4; // esi
int v5; // edx
int v6; // eax
int v7; // er8
int v8; // edi
int v9; // esi
int v10; // ecx
int v11; // edx
ssize_t result; // rax
int v13; // ecx
__int64 v14; // rdx
float v15; // xmm0_4
int v16; // edi
int v17; // esi
int v18; // edx
float v19; // xmm0_4
int v20; // edi
int v21; // esi
float v22; // xmm3_4
int v23; // edx
unsigned int v24; // eax
__int64 v25; // rsi
__int64 v26; // rsi
__int64 v27; // rsi
char v28[280]; // [rsp+0h] [rbp-118h] BYREF

if ( inited_17 )
{
v0 = gpioTick();
v1 = lastTick_16;
v2 = v0;
lastTick_16 = v0;
func5(i2c_15);
func6(i2c_15);
if ( calibrated_14 )
{
v15 = func8(rawAcc[0], rawGyr[0] - gyroCalibX_12, v2 - v1, *(float *)&X_13);
v16 = rawAcc[1];
v17 = rawGyr[1];
X_13 = LODWORD(v15);
v19 = func8(v16, v17 - gyroCalibY_10, v18, *(float *)&Y_11);
v20 = rawAcc[2];
v21 = rawGyr[2];
v22 = v19;
Y_11 = LODWORD(v19);
Z_9 = func8(v20, v21 - gyroCalibZ_8, v23, *(float *)&Z_9);
v25 = (unsigned int)(int)(float)((float)((float)(v22 * 1000.0) / 90.0) + 1500.0);
if ( (int)v25 > 2500 )
v25 = v24;
if ( (int)v25 < 500 )
v25 = 500LL;
gpioServo(17LL, v25);
v26 = (unsigned int)(int)(float)(1500.0 - (float)((float)(500.0 * *(float *)&X_13) / 90.0));
if ( (int)v26 > 2000 )
v26 = 2000LL;
if ( (int)v26 < 1000 )
v26 = 1000LL;
gpioServo(18LL, v26);
v27 = (unsigned int)(int)(float)(1500.0 - (float)((float)(500.0 * *(float *)&Y_11) / 90.0));
if ( (int)v27 > 2200 )
v27 = 2200LL;
if ( (int)v27 < 800 )
v27 = 800LL;
gpioServo(21LL, v27);
sprintf(v28, "X=%4.0f Y=%4.0f Z=%4.0f ", *(float *)&X_13, *(float *)&Y_11, *(float *)&Z_9);
result = func11(1u, 1u, v28);
}
else
{
v3 = rawAcc[1];
v4 = rawAcc[2];
v5 = rawGyr[1];
v6 = rawGyr[2];
v7 = accCalibX_7 + rawAcc[0];
++calibrations_4;
v8 = accCalibY_6 + v3;
v9 = accCalibZ_5 + v4;
accCalibX_7 = v7;
v10 = gyroCalibX_12 + rawGyr[0];
v11 = gyroCalibY_10 + v5;
accCalibY_6 = v8;
result = (unsigned int)(gyroCalibZ_8 + v6);
accCalibZ_5 = v9;
gyroCalibX_12 = v10;
gyroCalibY_10 = v11;
gyroCalibZ_8 = result;
if ( calibrations_4 > 199 )
{
calibrated_14 = 1;
accCalibX_7 = v7 / 200;
accCalibY_6 = v8 / 200;
accCalibZ_5 = v9 / 200;
gyroCalibX_12 = v10 / 200;
v13 = v11 / 200;
v14 = (int)result;
result = (unsigned int)((int)result >> 31);
gyroCalibY_10 = v13;
gyroCalibZ_8 = ((int)((unsigned __int64)(1374389535 * v14) >> 32) >> 6) - result;
}
}
}
else
{
i2c_15 = func7();
gpioServo(17LL, 1500LL);
gpioServo(18LL, 1500LL);
result = gpioServo(21LL, 1500LL);
inited_17 = 1;
}
return result;
}

original_code:
void i2cTick(void)
{
   static int inited = 0;
   static int calibrated = 0;
   static int calibrations = 0;
   static int accCalibX = 0, accCalibY = 0, accCalibZ = 0;
   static int gyroCalibX = 0, gyroCalibY = 0, gyroCalibZ = 0;
   static int i2c;
   static float X=0.0, Y=0.0, Z=0.0;
   static uint32_t lastTick;
   uint32_t tick;
   int elapsed;
   int pulse;
   char str[256];
   if (inited)
   {
      tick = gpioTick();
      elapsed = tick - lastTick;
      lastTick = tick;
      readADXL345(i2c);
      readITG3200(i2c);
      if (calibrated)
      {
         X = estimateAngle(
            rawAcc[0], rawGyr[0] -gyroCalibX, X, elapsed);
         Y = estimateAngle(
            rawAcc[1], rawGyr[1] - gyroCalibY, Y, elapsed);
         Z = estimateAngle(
            rawAcc[2], rawGyr[2] - gyroCalibZ, Z, elapsed);
         pulse = 1500 + (Y * 1000 / 90);
         if (pulse < 500) pulse = 500;
         if (pulse > 2500) pulse = 2500;
         gpioServo(17, pulse);
         pulse = 1500 - (X * 500 / 90);
         if (pulse < 1000) pulse = 1000;
         if (pulse > 2000) pulse = 2000;
         gpioServo(18, pulse);
         pulse = 1500 - (Y * 500 / 90);
         if (pulse < 800) pulse = 800;
         if (pulse > 2200) pulse = 2200;
         gpioServo(21, pulse);
         sprintf(str, "X=%4.0f Y=%4.0f Z=%4.0f ", X, Y, Z);
         putTTYstr(1, 1, str);
      }
      else
      {
         accCalibX+=rawAcc[0];
         accCalibY+=rawAcc[1];
         accCalibZ+=rawAcc[2];
         gyroCalibX+=rawGyr[0];
         gyroCalibY+=rawGyr[1];
         gyroCalibZ+=rawGyr[2];
         if (++calibrations >= 200)
         {
            accCalibX /= 200;
            accCalibY /= 200;
            accCalibZ /= 200;
            gyroCalibX /= 200;
            gyroCalibY /= 200;
            gyroCalibZ /= 200;
            calibrated = 1;
         }
      }
   }
   else
   {
      i2c = initI2Cdevices();
      gpioServo(17, 1500);
      gpioServo(18, 1500);
      gpioServo(21, 1500);
      inited = 1;
   }
}

canonical_original_code:
void i2cTick()
{
   static int inited = 0;
   static int calibrated = 0;
   static int calibrations = 0;
   static int accCalibX = 0; static int accCalibY = 0; static int accCalibZ = 0;
   static int gyroCalibX = 0; static int gyroCalibY = 0; static int gyroCalibZ = 0;
   static int i2c;
   static float X = 0.0; static float Y = 0.0; static float Z = 0.0;
   static unsigned int lastTick;
   unsigned int tick;
   int elapsed;
   int pulse;
   char str[256];
   if (inited)
   {
      tick = gpioTick();
      elapsed = tick - lastTick;
      lastTick = tick;
      readADXL345(i2c);
      readITG3200(i2c);
      if (calibrated)
      {
         X = estimateAngle(
            rawAcc[0], rawGyr[0] -gyroCalibX, X, elapsed);
         Y = estimateAngle(
            rawAcc[1], rawGyr[1] - gyroCalibY, Y, elapsed);
         Z = estimateAngle(
            rawAcc[2], rawGyr[2] - gyroCalibZ, Z, elapsed);
         pulse = 1500 + (Y * 1000 / 90);
         if (pulse < 500) pulse = 500;
         if (pulse > 2500) pulse = 2500;
         gpioServo(17, pulse);
         pulse = 1500 - (X * 500 / 90);
         if (pulse < 1000) pulse = 1000;
         if (pulse > 2000) pulse = 2000;
         gpioServo(18, pulse);
         pulse = 1500 - (Y * 500 / 90);
         if (pulse < 800) pulse = 800;
         if (pulse > 2200) pulse = 2200;
         gpioServo(21, pulse);
         sprintf(str, "X=%4.0f Y=%4.0f Z=%4.0f ", X, Y, Z);
         putTTYstr(1, 1, str);
      }
      else
      {
         accCalibX+=rawAcc[0];
         accCalibY+=rawAcc[1];
         accCalibZ+=rawAcc[2];
         gyroCalibX+=rawGyr[0];
         gyroCalibY+=rawGyr[1];
         gyroCalibZ+=rawGyr[2];
         if (++calibrations >= 200)
         {
            accCalibX /= 200;
            accCalibY /= 200;
            accCalibZ /= 200;
            gyroCalibX /= 200;
            gyroCalibY /= 200;
            gyroCalibZ /= 200;
            calibrated = 1;
         }
      }
   }
   else
   {
      i2c = initI2Cdevices();
      gpioServo(17, 1500);
      gpioServo(18, 1500);
      gpioServo(21, 1500);
      inited = 1;
   }
}

variable_types: inited int

variable_types: calibrated int

variable_types: calibrations int

variable_types: accCalibX int

variable_types: accCalibY int

variable_types: accCalibZ int

variable_types: gyroCalibX int

variable_types: gyroCalibY int

variable_types: gyroCalibZ int

variable_types: X float

variable_types: Y float

variable_types: Z float

variable_types:
lastTick
unsigned int

variable_types:
tick
unsigned int

variable_types: pulse int


sonarAlert

decompiled_code:
ssize_t __fastcall sonarAlert(__int64 a1, int a2, int a3)
{
int v3; // edx
ssize_t result; // rax
char v5[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a2 == 1 )
{
startTick_3 = a3;
}
else if ( !a2 )
{
v3 = a3 - startTick_3;
if ( v3 <= 26099 )
{
sprintf(v5, "Sonar %3d cms", (unsigned int)((v3 + 29) / 58));
result = putTTYstr(0xFu, 1u, v5);
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func14(__int64 a1, int a2, int a3)
{
int v3; // edx
ssize_t result; // rax
char v5[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a2 == 1 )
{
startTick_3 = a3;
}
else if ( !a2 )
{
v3 = a3 - startTick_3;
if ( v3 <= 26099 )
{
sprintf(v5, "Sonar %3d cms", (unsigned int)((v3 + 29) / 58));
result = func11(0xFu, 1u, v5);
}
}
return result;
}

original_code:
void sonarAlert(int gpio, int level, uint32_t tick)
{
   static uint32_t startTick;
   int diffTick;
   char str[256];
   if (level == 1)
   {
      startTick = tick;
   }
   else if (level == 0)
   {
      diffTick = tick - startTick;
      if (diffTick < 26100)
      {
         sprintf(str, "Sonar %3d cms", (diffTick+29)/58);
         putTTYstr(15, 1, str);
      }
   }
}

canonical_original_code:
void sonarAlert(int gpio, int level, unsigned int tick)
{
   static unsigned int startTick;
   int diffTick;
   char str[256];
   if (level == 1)
   {
      startTick = tick;
   }
   else if (level == 0)
   {
      diffTick = tick - startTick;
      if (diffTick < 26100)
      {
         sprintf(str, "Sonar %3d cms", (diffTick+29)/58);
         putTTYstr(15, 1, str);
      }
   }
}

variable_types: gpio int

variable_types: level int

variable_types:
startTick
unsigned int

variable_types: diffTick int

variable_types:
str
char [256]


LDRalert

decompiled_code:
ssize_t __fastcall LDRalert(__int64 a1, int a2, int a3)
{
ssize_t result; // rax
char v4[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a2 == 1 )
{
sprintf(v4, "LDR %4d micros", (unsigned int)(a3 - LDRrechargeTick));
result = putTTYstr(0x11u, 1u, v4);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func15(__int64 a1, int a2, int a3)
{
ssize_t result; // rax
char v4[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a2 == 1 )
{
sprintf(v4, "LDR %4d micros", (unsigned int)(a3 - LDRrechargeTick));
result = func11(0x11u, 1u, v4);
}
return result;
}

original_code:
void LDRalert(int pin, int level, uint32_t tick)
{
   int diffTick;
   char str[256];
   if (level == 1)
   {
      diffTick = tick - LDRrechargeTick;
      sprintf(str, "LDR %4d micros", diffTick);
      putTTYstr(17, 1, str);
   }
}

canonical_original_code:
void LDRalert(int pin, int level, unsigned int tick)
{
   int diffTick;
   char str[256];
   if (level == 1)
   {
      diffTick = tick - LDRrechargeTick;
      sprintf(str, "LDR %4d micros", diffTick);
      putTTYstr(17, 1, str);
   }
}

variable_types: pin int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v4[280]; // [rsp+0h] [rbp-118h] BYREF

if ( argc > 1 )
{
micros = atoi(argv[1]);
if ( argc != 2 )
millis = atoi(argv[2]);
}
putTTY("\x1Bc");
gpioCfgBufferSize((unsigned int)millis, argv);
gpioCfgClock((unsigned int)micros, 1LL, 1LL);
if ( (int)gpioInitialise() >= 0 )
{
gpioSetMode(17LL, 1LL);
gpioSetMode(18LL, 1LL);
gpioSetMode(21LL, 1LL);
gpioSetMode(4LL, 1LL);
gpioSetMode(14LL, 1LL);
gpioSetMode(15LL, 1LL);
gpioSetMode(22LL, 1LL);
gpioSetMode(8LL, 1LL);
gpioSetMode(7LL, 1LL);
gpioSetMode(9LL, 1LL);
gpioSetMode(11LL, 1LL);
gpioSetMode(24LL, 1LL);
gpioWrite(24LL, 0LL);
gpioSetMode(10LL, 0LL);
gpioSetMode(23LL, 0LL);
gpioSetMode(25LL, 0LL);
gpioSetTimerFunc(0LL, 20LL, i2cTick);
gpioSetTimerFunc(1LL, 1000LL, LEDlaserTick);
gpioSetTimerFunc(2LL, 3000LL, motorTick);
gpioSetTimerFunc(3LL, 100LL, sonarLDRtick);
gpioSetAlertFunc(23LL, launchpadAlert);
gpioSetAlertFunc(10LL, sonarAlert);
gpioSetAlertFunc(25LL, LDRalert);
while ( 1 )
{
sleep(1u);
sprintf(v4, "TI pulses %8ld", launchpadPulses);
putTTYstr(9u, 1u, v4);
sprintf(v4, "+/-5 %8ld", launchpad5);
putTTYstr(0xAu, 6u, v4);
sprintf(v4, "+/-10 %8ld", launchpad10);
putTTYstr(0xBu, 5u, v4);
sprintf(v4, "+/-15 %8ld", launchpad15);
putTTYstr(0xCu, 5u, v4);
sprintf(v4, "Others %8ld (last %d) ", launchpadOutRange, (unsigned int)launchpadErr);
putTTYstr(0xDu, 4u, v4);
}
}
gpioTerminate();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v4[280]; // [rsp+0h] [rbp-118h] BYREF

if ( argc > 1 )
{
micros = atoi(argv[1]);
if ( argc != 2 )
millis = atoi(argv[2]);
}
func10("\x1Bc");
gpioCfgBufferSize((unsigned int)millis, argv);
gpioCfgClock((unsigned int)micros, 1LL, 1LL);
if ( (int)gpioInitialise() >= 0 )
{
gpioSetMode(17LL, 1LL);
gpioSetMode(18LL, 1LL);
gpioSetMode(21LL, 1LL);
gpioSetMode(4LL, 1LL);
gpioSetMode(14LL, 1LL);
gpioSetMode(15LL, 1LL);
gpioSetMode(22LL, 1LL);
gpioSetMode(8LL, 1LL);
gpioSetMode(7LL, 1LL);
gpioSetMode(9LL, 1LL);
gpioSetMode(11LL, 1LL);
gpioSetMode(24LL, 1LL);
gpioWrite(24LL, 0LL);
gpioSetMode(10LL, 0LL);
gpioSetMode(23LL, 0LL);
gpioSetMode(25LL, 0LL);
gpioSetTimerFunc(0LL, 20LL, i2cTick);
gpioSetTimerFunc(1LL, 1000LL, LEDlaserTick);
gpioSetTimerFunc(2LL, 3000LL, motorTick);
gpioSetTimerFunc(3LL, 100LL, sonarLDRtick);
gpioSetAlertFunc(23LL, launchpadAlert);
gpioSetAlertFunc(10LL, sonarAlert);
gpioSetAlertFunc(25LL, LDRalert);
while ( 1 )
{
sleep(1u);
sprintf(v4, "TI pulses %8ld", launchpadPulses);
func11(9u, 1u, v4);
sprintf(v4, "+/-5 %8ld", launchpad5);
func11(0xAu, 6u, v4);
sprintf(v4, "+/-10 %8ld", launchpad10);
func11(0xBu, 5u, v4);
sprintf(v4, "+/-15 %8ld", launchpad15);
func11(0xCu, 5u, v4);
sprintf(v4, "Others %8ld (last %d) ", launchpadOutRange, (unsigned int)launchpadErr);
func11(0xDu, 4u, v4);
}
}
gpioTerminate();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
   char str[256];
   if (argc > 1) micros = atoi(argv[1]);
   if (argc > 2) millis = atoi(argv[2]);
   putTTY("\033c");
   gpioCfgBufferSize(millis);
   gpioCfgClock(micros, 1, 1);
   version = gpioInitialise();
   if (version >= 0)
   {
      gpioSetMode(17, 1);
      gpioSetMode(18, 1);
      gpioSetMode(21, 1);
      gpioSetMode(4, 1);
      gpioSetMode(14, 1);
      gpioSetMode(15, 1);
      gpioSetMode(22, 1);
      gpioSetMode(8, 1);
      gpioSetMode(7, 1);
      gpioSetMode(9, 1);
      gpioSetMode(11, 1);
      gpioSetMode(24, 1);
      gpioWrite (24, 0);
      gpioSetMode(10, 0);
      gpioSetMode(23, 0);
      gpioSetMode(25, 0);
      gpioSetTimerFunc(0, 20, i2cTick);
      gpioSetTimerFunc(1, 1000, LEDlaserTick);
      gpioSetTimerFunc(2, 3000, motorTick);
      gpioSetTimerFunc(3, 100, sonarLDRtick);
      gpioSetAlertFunc(23, launchpadAlert);
      gpioSetAlertFunc(10, sonarAlert);
      gpioSetAlertFunc(25, LDRalert);
      while (1)
      {
         sleep(1);
         sprintf(str, "TI pulses %8ld", launchpadPulses);
         putTTYstr(9, 1, str);
         sprintf(str, "+/-5 %8ld", launchpad5);
         putTTYstr(10, 6, str);
         sprintf(str, "+/-10 %8ld", launchpad10);
         putTTYstr(11, 5, str);
         sprintf(str, "+/-15 %8ld", launchpad15);
         putTTYstr(12, 5, str);
         sprintf(str, "Others %8ld (last %d)    ",
            launchpadOutRange, launchpadErr);
         putTTYstr(13, 4, str);
      }
   }
   gpioTerminate();
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


cmdMatch

decompiled_code:
__int64 __fastcall cmdMatch(char *s1)
{
__int64 i; // rbx

for ( i = 0LL; i != 34; ++i )
{
if ( !strcasecmp(s1, (&s2)[3 * i]) )
return (unsigned int)i;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s1)
{
__int64 i; // rbx

for ( i = 0LL; i != 34; ++i )
{
if ( !strcasecmp(s1, (&s2)[3 * i]) )
return (unsigned int)i;
}
return 0xFFFFFFFFLL;
}

original_code:
static int cmdMatch(char * str)
{
   int i;
   for (i=0; i<(sizeof(cmdInfo)/sizeof(cmdInfo_t)); i++)
   {
      if (strcasecmp(str, cmdInfo[i].name) == 0) return i;
   }
   return -1;
}

canonical_original_code:
static int cmdMatch(char *str)
{
   int i;
   for (i=0; i<(sizeof(cmdInfo)/sizeof(cmdInfo_t)); i++)
   {
      if (strcasecmp(str, cmdInfo[i].name) == 0) return i;
   }
   return -1;
}

variable_types:
str
char *

variable_types: i int


cmdParse

decompiled_code:
__int64 __fastcall cmdParse(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 v4; // rax
int v5; // edx
const __int32_t **v7; // rax
char *v8; // rax
const __int32_t **v9; // rax
char *v10; // rax
char v11; // [rsp+6h] [rbp-22h]
char s1[32]; // [rsp+8h] [rbp-20h] BYREF

__isoc99_sscanf();
*(_DWORD *)a2 = -1;
v2 = cmdMatch(s1);
v3 = v2;
if ( v2 >= 0 )
{
v4 = 6LL * v2;
v5 = cmdInfo[v4];
*(_QWORD *)(a2 + 4) = 0LL;
*(_DWORD *)a2 = v5;
switch ( dword_405110[v4] )
{
case 1:
if ( (unsigned int)__isoc99_sscanf() == 1 )
return v3;
return (unsigned int)-1;
case 2:
case 7:
if ( (unsigned int)__isoc99_sscanf() == 2 )
return v3;
return (unsigned int)-1;
case 3:
case 4:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
return v3;
case 6:
return v3;
case 8:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
v9 = __ctype_toupper_loc();
v10 = strchr("RW540123", (*v9)[v11]);
if ( !v10 )
return (unsigned int)-1;
*(_DWORD *)(a2 + 8) = v10 - "RW540123";
return v3;
case 9:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
v7 = __ctype_toupper_loc();
v8 = strchr("ODU", (*v7)[v11]);
if ( !v8 )
return (unsigned int)-1;
*(_DWORD *)(a2 + 8) = v8 - "ODU";
return v3;
default:
return (unsigned int)-1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 v4; // rax
int v5; // edx
const __int32_t **v7; // rax
char *v8; // rax
const __int32_t **v9; // rax
char *v10; // rax
char v11; // [rsp+6h] [rbp-22h]
char s1[32]; // [rsp+8h] [rbp-20h] BYREF

__isoc99_sscanf();
*(_DWORD *)a2 = -1;
v2 = func8(s1);
v3 = v2;
if ( v2 >= 0 )
{
v4 = 6LL * v2;
v5 = cmdInfo[v4];
*(_QWORD *)(a2 + 4) = 0LL;
*(_DWORD *)a2 = v5;
switch ( dword_405110[v4] )
{
case 1:
if ( (unsigned int)__isoc99_sscanf() == 1 )
return v3;
return (unsigned int)-1;
case 2:
case 7:
if ( (unsigned int)__isoc99_sscanf() == 2 )
return v3;
return (unsigned int)-1;
case 3:
case 4:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
return v3;
case 6:
return v3;
case 8:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
v9 = __ctype_toupper_loc();
v10 = strchr("RW540123", (*v9)[v11]);
if ( !v10 )
return (unsigned int)-1;
*(_DWORD *)(a2 + 8) = v10 - "RW540123";
return v3;
case 9:
if ( (unsigned int)__isoc99_sscanf() != 3 )
return (unsigned int)-1;
v7 = __ctype_toupper_loc();
v8 = strchr("ODU", (*v7)[v11]);
if ( !v8 )
return (unsigned int)-1;
*(_DWORD *)(a2 + 8) = v8 - "ODU";
return v3;
default:
return (unsigned int)-1;
}
}
return v3;
}

original_code:
int cmdParse(char * buf, cmdCmd_t * cmd)
{
   char str[8];
   int f, valid, idx, val;
   char * ptr;
   char c, t;
   sscanf(buf, " %7s", str);
   cmd->cmd = -1;
   idx = cmdMatch(str);
   if (idx < 0) return idx;
   valid = 0;
   cmd->cmd = cmdInfo[idx].cmd;
   cmd->p1 = 0;
   cmd->p2 = 0;
   switch (cmdInfo[idx].vt)
   {
      case 1:
         f = sscanf(buf, " %7s %c", str, &t);
         if (f == 1) valid = 1;
         break;
      case 2:
         f = sscanf(buf, " %7s %d %c", str, &cmd->p1, &t);
         if (f == 2) valid = 1;
         break;
      case 3:
         f = sscanf(buf, " %7s %d %d %c", str, &cmd->p1, &cmd->p2, &t);
         if (f == 3) valid = 1;
         break;
      case 4:
         f = sscanf(buf, " %7s %d %x %c", str, &cmd->p1, &cmd->p2, &t);
         if (f == 3) valid = 1;
         break;
      case 6:
         valid = 1;
         break;
      case 7:
         f = sscanf(buf, " %7s %x %c", str, &cmd->p1, &t);
         if (f == 2) valid = 1;
         break;
      case 8:
         f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
         if (f == 3)
         {
            val = toupper(c);
            ptr = strchr(fmtMdeStr, val);
            if (ptr != ((void *)0))
            {
               val = ptr - fmtMdeStr;
               cmd->p2 = val;
               valid = 1;
            }
         }
         break;
      case 9:
         f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
         if (f == 3)
         {
            val = toupper(c);
            ptr = strchr(fmtPudStr, val);
            if (ptr != ((void *)0))
            {
               val = ptr - fmtPudStr;
               cmd->p2 = val;
               valid = 1;
            }
         }
         break;
   }
   if (valid) return idx;
   else return -1;
}

canonical_original_code:
int cmdParse(char *buf, struct cmdCmd_t *cmd)
{
   char str[8];
   int f; int valid; int idx; int val;
   char *ptr;
   char c; char t;
   sscanf(buf, " %7s", str);
   cmd->cmd = -1;
   idx = cmdMatch(str);
   if (idx < 0) return idx;
   valid = 0;
   cmd->cmd = cmdInfo[idx].cmd;
   cmd->p1 = 0;
   cmd->p2 = 0;
   switch (cmdInfo[idx].vt)
   {
      case 1:
         f = sscanf(buf, " %7s %c", str, &t);
         if (f == 1) valid = 1;
         break;
      case 2:
         f = sscanf(buf, " %7s %d %c", str, &cmd->p1, &t);
         if (f == 2) valid = 1;
         break;
      case 3:
         f = sscanf(buf, " %7s %d %d %c", str, &cmd->p1, &cmd->p2, &t);
         if (f == 3) valid = 1;
         break;
      case 4:
         f = sscanf(buf, " %7s %d %x %c", str, &cmd->p1, &cmd->p2, &t);
         if (f == 3) valid = 1;
         break;
      case 6:
         valid = 1;
         break;
      case 7:
         f = sscanf(buf, " %7s %x %c", str, &cmd->p1, &t);
         if (f == 2) valid = 1;
         break;
      case 8:
         f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
         if (f == 3)
         {
            val = toupper(c);
            ptr = strchr(fmtMdeStr, val);
            if (ptr != ((void *)0))
            {
               val = ptr - fmtMdeStr;
               cmd->p2 = val;
               valid = 1;
            }
         }
         break;
      case 9:
         f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
         if (f == 3)
         {
            val = toupper(c);
            ptr = strchr(fmtPudStr, val);
            if (ptr != ((void *)0))
            {
               val = ptr - fmtPudStr;
               cmd->p2 = val;
               valid = 1;
            }
         }
         break;
   }
   if (valid) return idx;
   else return -1;
}

variable_types:
buf
char *

variable_types: struct cmdCmd_t { unsigned int cmd; unsigned int p1; unsigned int p2; unsigned int res; } *

variable_types:
str
char [8]

variable_types: f int

variable_types: valid int

variable_types: idx int

variable_types: val int

variable_types:
ptr
char *

variable_types: c char

variable_types: t char


cmdFatal

decompiled_code:
void __noreturn cmdFatal(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-150h] BYREF
char s[128]; // [rsp+20h] [rbp-138h] BYREF

va_start(arg, format);
vsnprintf(s, 0x80uLL, format, arg);
fprintf(stderr, "%s\n", s);
fflush(stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-150h] BYREF
char s[128]; // [rsp+20h] [rbp-138h] BYREF

va_start(arg, format);
vsnprintf(s, 0x80uLL, format, arg);
fprintf(stderr, "%s\n", s);
fflush(stderr);
exit(1);
}

original_code:
void cmdFatal(char *fmt, ...)
{
   char buf[128];
   va_list ap;
   __builtin_va_start(ap,fmt);
   vsnprintf(buf, sizeof(buf), fmt, ap);
   __builtin_va_end(ap);
   fprintf(stderr, "%s\n", buf);
   fflush(stderr);
   exit(1);
}

canonical_original_code:
void cmdFatal(char *fmt, ...)
{
   char buf[128];
   __builtin_va_list ap;
   __builtin_va_start(ap,fmt);
   vsnprintf(buf, sizeof(buf), fmt, ap);
   __builtin_va_end(ap);
   fprintf(stderr, "%s\n", buf);
   fflush(stderr);
   exit(1);
}

variable_types:
fmt
char *

variable_types:
buf
char [128]


cmdErrStr

decompiled_code:
const char *__fastcall cmdErrStr(int a1)
{
__int64 v1; // rax
int i; // edx

v1 = 0LL;
for ( i = -1; ; i = errInfo[4 * v1] )
{
if ( i == a1 )
return (&off_4024C8)[2 * (int)v1];
if ( ++v1 == 38 )
break;
}
return "unknown error";
}

canonical_decompiled_code:
const char *__fastcall func11(int a1)
{
__int64 v1; // rax
int i; // edx

v1 = 0LL;
for ( i = -1; ; i = errInfo[4 * v1] )
{
if ( i == a1 )
return (&off_4024C8)[2 * (int)v1];
if ( ++v1 == 38 )
break;
}
return "unknown error";
}

original_code:
char * cmdErrStr(int error)
{
   int i;
   for (i=0; i<(sizeof(errInfo)/sizeof(errInfo_t)); i++)
   {
      if (errInfo[i].error == error) return errInfo[i].str;
   }
   return "unknown error";
}

canonical_original_code:
char *cmdErrStr(int error)
{
   int i;
   for (i=0; i<(sizeof(errInfo)/sizeof(errInfo_t)); i++)
   {
      if (errInfo[i].error == error) return errInfo[i].str;
   }
   return "unknown error";
}

variable_types: error int

return_type: char *


timeStamp

decompiled_code:
char *timeStamp()
{
struct timeval v1; // [rsp+0h] [rbp-58h] BYREF
struct tm tp; // [rsp+10h] [rbp-48h] BYREF

gettimeofday(&v1, 0LL);
localtime_r(&v1.tv_sec, &tp);
strftime(buf_0, 0x20uLL, "%F %T", &tp);
return buf_0;
}

canonical_decompiled_code:
char *func7()
{
struct timeval v1; // [rsp+0h] [rbp-58h] BYREF
struct tm tp; // [rsp+10h] [rbp-48h] BYREF

gettimeofday(&v1, 0LL);
localtime_r(&v1.tv_sec, &tp);
strftime(buf_0, 0x20uLL, "%F %T", &tp);
return buf_0;
}

original_code:
static char * timeStamp()
{
   static char buf[32];
   struct timeval now;
   struct tm tmp;
   gettimeofday(&now, ((void *)0));
   localtime_r(&now.tv_sec, &tmp);
   strftime(buf, sizeof(buf), "%F %T", &tmp);
   return buf;
}

canonical_original_code:
static char *timeStamp()
{
   static char buf[32];
   struct timeval now;
   struct tm tmp;
   gettimeofday(&now, ((void *)0));
   localtime_r(&now.tv_sec, &tmp);
   strftime(buf, sizeof(buf), "%F %T", &tmp);
   return buf;
}

variable_types:
buf
char [32]

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

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


symbol

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

result = (unsigned int)(a1 + 71);
if ( a1 <= 25 )
result = (unsigned int)(a1 + 65);
return result;
}

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

result = (unsigned int)(a1 + 71);
if ( a1 <= 25 )
result = (unsigned int)(a1 + 65);
return result;
}

original_code:
int symbol(int bit)
{
   if (bit < 26) return ('A' + bit);
   else return ('a' + bit - 26);
}

variable_types: bit int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
const char *v4; // rax
unsigned int v5; // eax
__int64 v6; // rdx
int v7; // ebx
int v8; // er14
unsigned int v10; // er12
int v11; // er13
int v12; // ebx
__int64 v13; // rsi
unsigned int v14; // eax
char buf[4]; // [rsp+4h] [rbp-34h] BYREF
int v16; // [rsp+8h] [rbp-30h]
int v17; // [rsp+Ch] [rbp-2Ch]

if ( (unsigned int)read(0, buf, 0xCuLL) != 12 )
exit(-1);
v3 = 0;
v4 = (const char *)timeStamp();
printf("$date %s $end\n", v4);
puts("$version pig2vcd V1 $end");
puts("$timescale 1 us $end");
puts("$scope module top $end");
do
{
v5 = symbol(v3);
v6 = v3++;
printf("$var wire 1 %c %d $end\n", v5, v6);
}
while ( v3 != 32 );
v7 = 0;
puts("$upscope $end");
puts("$enddefinitions $end");
v8 = v16;
while ( (unsigned int)read(0, buf, 0xCuLL) == 12 )
{
if ( v17 != v7 )
{
v10 = 0;
printf("#%u\n", (unsigned int)(v16 - v8));
v11 = v17;
v12 = v17 ^ v7;
do
{
if ( (v12 & (1 << v10)) != 0 )
{
v13 = 48 - ((unsigned int)((v17 & (1 << v10)) == 0) - 1);
v14 = symbol(v10);
printf("%c%c\n", v13, v14);
}
++v10;
}
while ( v10 != 32 );
v7 = v11;
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
const char *v4; // rax
unsigned int v5; // eax
__int64 v6; // rdx
int v7; // ebx
int v8; // er14
unsigned int v10; // er12
int v11; // er13
int v12; // ebx
__int64 v13; // rsi
unsigned int v14; // eax
char buf[4]; // [rsp+4h] [rbp-34h] BYREF
int v16; // [rsp+8h] [rbp-30h]
int v17; // [rsp+Ch] [rbp-2Ch]

if ( (unsigned int)read(0, buf, 0xCuLL) != 12 )
exit(-1);
v3 = 0;
v4 = (const char *)func7();
printf("$date %s $end\n", v4);
puts("$version pig2vcd V1 $end");
puts("$timescale 1 us $end");
puts("$scope module top $end");
do
{
v5 = func8(v3);
v6 = v3++;
printf("$var wire 1 %c %d $end\n", v5, v6);
}
while ( v3 != 32 );
v7 = 0;
puts("$upscope $end");
puts("$enddefinitions $end");
v8 = v16;
while ( (unsigned int)read(0, buf, 0xCuLL) == 12 )
{
if ( v17 != v7 )
{
v10 = 0;
printf("#%u\n", (unsigned int)(v16 - v8));
v11 = v17;
v12 = v17 ^ v7;
do
{
if ( (v12 & (1 << v10)) != 0 )
{
v13 = 48 - ((unsigned int)((v17 & (1 << v10)) == 0) - 1);
v14 = func8(v10);
printf("%c%c\n", v13, v14);
}
++v10;
}
while ( v10 != 32 );
v7 = v11;
}
}
return 0;
}

original_code:
int main(int argc, char * argv[])
{
   int b, r, v;
   uint32_t t0;
   uint32_t lastLevel, changed;
   gpioReport_t report;
   r=read(0, &report, (sizeof(gpioReport_t)));
   if (r != (sizeof(gpioReport_t))) exit(-1);
   printf("$date %s $end\n", timeStamp());
   printf("$version pig2vcd V1 $end\n");
   printf("$timescale 1 us $end\n");
   printf("$scope module top $end\n");
   for (b=0; b<32; b++)
      printf("$var wire 1 %c %d $end\n", symbol(b), b);
   printf("$upscope $end\n");
   printf("$enddefinitions $end\n");
   t0 = report.tick;
   lastLevel =0;
   while ((r=read(0, &report, (sizeof(gpioReport_t)))) == (sizeof(gpioReport_t)))
   {
      if (report.level != lastLevel)
      {
         printf("#%u\n", report.tick - t0);
         changed = report.level ^ lastLevel;
         lastLevel = report.level;
         for (b=0; b<32; b++)
         {
            if (changed & (1<<b))
            {
               if (report.level & (1<<b)) v='1'; else v='0';
               printf("%c%c\n", v, symbol(b));
            }
         }
      }
   }
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
   int b; int r; int v;
   unsigned int t0;
   unsigned int lastLevel; unsigned int changed;
   struct gpioReport_t report;
   r=read(0, &report, (sizeof(gpioReport_t)));
   if (r != (sizeof(gpioReport_t))) exit(-1);
   printf("$date %s $end\n", timeStamp());
   printf("$version pig2vcd V1 $end\n");
   printf("$timescale 1 us $end\n");
   printf("$scope module top $end\n");
   for (b=0; b<32; b++)
      printf("$var wire 1 %c %d $end\n", symbol(b), b);
   printf("$upscope $end\n");
   printf("$enddefinitions $end\n");
   t0 = report.tick;
   lastLevel =0;
   while ((r=read(0, &report, (sizeof(gpioReport_t)))) == (sizeof(gpioReport_t)))
   {
      if (report.level != lastLevel)
      {
         printf("#%u\n", report.tick - t0);
         changed = report.level ^ lastLevel;
         lastLevel = report.level;
         for (b=0; b<32; b++)
         {
            if (changed & (1<<b))
            {
               if (report.level & (1<<b)) v='1'; else v='0';
               printf("%c%c\n", v, symbol(b));
            }
         }
      }
   }
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: b int

variable_types: r int

variable_types: v int

variable_types:
lastLevel
unsigned int

variable_types:
changed
unsigned int

variable_types: struct gpioReport_t { unsigned short seqno; unsigned short flags; unsigned int tick; unsigned int level; }


repo: jzawodn/arm-neon-vfp-test

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
double v4; // xmm1_8
int i; // eax
double v6; // xmm2_8
double v8; // [rsp+8h] [rbp-20h]
double v9; // [rsp+10h] [rbp-18h]
double v10; // [rsp+18h] [rbp-10h]

if ( argc != 4 )
exit(1);
v10 = atof(argv[1]);
v9 = atof(argv[2]);
v8 = time_msec();
v3 = 1000000 * atoi(argv[3]);
if ( v3 <= 0 )
{
v4 = 1.0;
}
else
{
v4 = 1.0;
for ( i = 0; i != v3; ++i )
v4 = v4 * v10 / v9;
}
v6 = time_msec() - v8;
printf("ans = %f %d loop/msec\n", v4, (unsigned int)(int)((double)v3 / v6));
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
double v4; // xmm1_8
int i; // eax
double v6; // xmm2_8
double v8; // [rsp+8h] [rbp-20h]
double v9; // [rsp+10h] [rbp-18h]
double v10; // [rsp+18h] [rbp-10h]

if ( argc != 4 )
exit(1);
v10 = atof(argv[1]);
v9 = atof(argv[2]);
v8 = func7();
v3 = 1000000 * atoi(argv[3]);
if ( v3 <= 0 )
{
v4 = 1.0;
}
else
{
v4 = 1.0;
for ( i = 0; i != v3; ++i )
v4 = v4 * v10 / v9;
}
v6 = func7() - v8;
printf("ans = %f %d loop/msec\n", v4, (unsigned int)(int)((double)v3 / v6));
return 0;
}

original_code:
int main(int argc, char *argv[]) {
        if(argc != 4) exit(1);
        double f1 = atof(argv[1]);
        double f2 = atof(argv[2]);
        double ans = 1.0;
        msec_t start = time_msec();
        long iterations = atoi(argv[3]) * 1000 * 1000;
        for(int i=0; i<iterations; i++) {
          ans *= f1;
          ans /= f2;
        }
        msec_t end = time_msec();
        printf("ans = %f %d loop/msec\n", ans, (int)(iterations/(end - start)));
        return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
        if(argc != 4) exit(1);
        double f1 = atof(argv[1]);
        double f2 = atof(argv[2]);
        double ans = 1.0;
        double start = time_msec();
        long iterations = atoi(argv[3]) * 1000 * 1000;
        for(int i = 0; i<iterations; i++) {
          ans *= f1;
          ans /= f2;
        }
        double end = time_msec();
        printf("ans = %f %d loop/msec\n", ans, (int)(iterations/(end - start)));
        return 0;
}

variable_types: ans double

variable_types: start double

variable_types: iterations long

variable_types: i int

variable_types: end double


decompiled_code:
double time_msec()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_sec) * 1000.0 + (double)SLODWORD(v1.tv_usec) / 1000.0;
}

canonical_decompiled_code:
double func7()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_sec) * 1000.0 + (double)SLODWORD(v1.tv_usec) / 1000.0;
}

original_code:
msec_t time_msec(void) {
        struct timeval now_tv;
        gettimeofday(&now_tv, ((void *)0));
        return (msec_t)now_tv.tv_sec * (msec_t)1000 + now_tv.tv_usec / (msec_t)1000;
}

canonical_original_code:
double time_msec() {
        struct timeval now_tv;
        gettimeofday(&now_tv, ((void *)0));
        return (double)now_tv.tv_sec * (double)1000 + now_tv.tv_usec / (double)1000;
}

return_type: double

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


ExitError

decompiled_code:
void __fastcall __noreturn ExitError(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void ExitError(const char* theErrorMessage) {
    perror(theErrorMessage);
    exit(1);
}

canonical_original_code:
void ExitError(char *theErrorMessage) {
    perror(theErrorMessage);
    exit(1);
}

variable_types:
theErrorMessage
char *


MaximumHelper

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

original_code:
int MaximumHelper(int a, int b) {
    return (a < b) ? b : a;
}

variable_types: a int


ConnectToServer

decompiled_code:
__int64 __fastcall ConnectToServer(char *name, int a2)
{
int v2; // er12
struct hostent *v3; // rax
socklen_t v4; // edx
struct sockaddr v6[2]; // [rsp+0h] [rbp-28h] BYREF

if ( !name
|| a2 <= 0
|| (v2 = socket(2, 1, 0), v2 < 0)
|| (v3 = gethostbyname(name)) == 0LL
|| (v6[0].sa_family = 2,
memmove(&v6[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length),
*(_WORD *)v6[0].sa_data = _bswap_16(a2),
connect(v2, v6, v4) < 0) )
{
v2 = -1;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *name, int a2)
{
int v2; // er12
struct hostent *v3; // rax
socklen_t v4; // edx
struct sockaddr v6[2]; // [rsp+0h] [rbp-28h] BYREF

if ( !name
|| a2 <= 0
|| (v2 = socket(2, 1, 0), v2 < 0)
|| (v3 = gethostbyname(name)) == 0LL
|| (v6[0].sa_family = 2,
memmove(&v6[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length),
*(_WORD *)v6[0].sa_data = func0(a2),
connect(v2, v6, v4) < 0) )
{
v2 = -1;
}
return (unsigned int)v2;
}

original_code:
int ConnectToServer(char* hostname, int port) {
    if ((hostname == ((void *)0)) || (port <= 0)) {
        return -1;
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        return -1;
    }
    struct hostent* server_name = gethostbyname(hostname);
    if (server_name == ((void *)0)) {
        return -1;
    }
    struct sockaddr_in serv_sockaddr;
    serv_sockaddr.sin_family = 2;
    bcopy((char *) server_name->h_addr_list[0], (char *) &serv_sockaddr.sin_addr.s_addr,
            server_name->h_length);
    serv_sockaddr.sin_port = htons(port);
    if (connect(sd, (struct sockaddr *) &serv_sockaddr, sizeof (serv_sockaddr)) < 0) {
        return -1;
    }
    return (sd);
}

canonical_original_code:
int ConnectToServer(char *hostname, int port) {
    if ((hostname == ((void *)0)) || (port <= 0)) {
        return -1;
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        return -1;
    }
    struct hostent *server_name = gethostbyname(hostname);
    if (server_name == ((void *)0)) {
        return -1;
    }
    struct sockaddr_in serv_sockaddr;
    serv_sockaddr.sin_family = 2;
    bcopy((char *) server_name->h_addr_list[0], (char *) &serv_sockaddr.sin_addr.s_addr,
            server_name->h_length);
    serv_sockaddr.sin_port = htons(port);
    if (connect(sd, (struct sockaddr *) &serv_sockaddr, sizeof (serv_sockaddr)) < 0) {
        return -1;
    }
    return (sd);
}

variable_types:
hostname
char *

variable_types: port int

variable_types: sd int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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

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

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


RemoteSocketInfo

decompiled_code:
int __fastcall RemoteSocketInfo(int a1, char *a2, _DWORD *a3, char a4)
{
int result; // eax
struct hostent *v7; // rax
char *v8; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
result = getpeername(a1, &addr, &len);
if ( !result )
{
if ( a3 )
{
result = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
*a3 = (unsigned __int16)result;
}
if ( a2 )
{
v7 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
if ( a4 || !v7 )
{
v8 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
result = (unsigned int)strncpy(a2, v8, 0x80uLL);
}
else
{
result = (unsigned int)strncpy(a2, v7->h_name, 0x80uLL);
}
a2[127] = 0;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, char *a2, _DWORD *a3, char a4)
{
int result; // eax
struct hostent *v7; // rax
char *v8; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
result = getpeername(a1, &addr, &len);
if ( !result )
{
if ( a3 )
{
result = (unsigned __int16)func0(*(__int16 *)addr.sa_data);
*a3 = (unsigned __int16)result;
}
if ( a2 )
{
v7 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
if ( a4 || !v7 )
{
v8 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
result = (unsigned int)strncpy(a2, v8, 0x80uLL);
}
else
{
result = (unsigned int)strncpy(a2, v7->h_name, 0x80uLL);
}
a2[127] = 0;
}
}
return result;
}

original_code:
void RemoteSocketInfo(int sockfd, char* hostname, int* port, char hostname_bool) {
    struct sockaddr_in remote_addr;
    socklen_t remote_addr_len = sizeof (remote_addr);
    if (getpeername(sockfd, (struct sockaddr*) &remote_addr, &remote_addr_len) == 0) {
        if (port != ((void *)0)) *port = ntohs(remote_addr.sin_port);
        if (hostname != ((void *)0)) {
            struct hostent* myhostent = gethostbyaddr(
                    (char *) &remote_addr.sin_addr.s_addr,
                    sizeof (remote_addr.sin_addr.s_addr), 2);
            if ((hostname_bool) || (myhostent == ((void *)0))) {
                strncpy(hostname, inet_ntoa(remote_addr.sin_addr), 128);
            } else {
                strncpy(hostname, myhostent->h_name, 128);
            }
            hostname[(128 - 1)] = '\0';
        }
    }
}

canonical_original_code:
void RemoteSocketInfo(int sockfd, char *hostname, int *port, char hostname_bool) {
    struct sockaddr_in remote_addr;
    unsigned int remote_addr_len = sizeof (remote_addr);
    if (getpeername(sockfd, (struct sockaddr *) &remote_addr, &remote_addr_len) == 0) {
        if (port != ((void *)0)) *port = ntohs(remote_addr.sin_port);
        if (hostname != ((void *)0)) {
            struct hostent *myhostent = gethostbyaddr(
                    (char *) &remote_addr.sin_addr.s_addr,
                    sizeof (remote_addr.sin_addr.s_addr), 2);
            if ((hostname_bool) || (myhostent == ((void *)0))) {
                strncpy(hostname, inet_ntoa(remote_addr.sin_addr), 128);
            } else {
                strncpy(hostname, myhostent->h_name, 128);
            }
            hostname[(128 - 1)] = '\0';
        }
    }
}

variable_types: sockfd int

variable_types:
port
int *

user_defined_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }


SocketInit

decompiled_code:
__int64 __fastcall SocketInit(int a1)
{
int v1; // er12
socklen_t v2; // edx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

if ( a1 <= 0
|| (v1 = socket(2, 1, 0), v1 < 0)
|| (v4.sa_family = 2, *(_DWORD *)&v4.sa_data[2] = 0, *(_WORD *)v4.sa_data = _bswap_16(a1), bind(v1, &v4, v2) < 0)
|| listen(v1, 5) < 0 )
{
v1 = -1;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
int v1; // er12
socklen_t v2; // edx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

if ( a1 <= 0
|| (v1 = socket(2, 1, 0), v1 < 0)
|| (v4.sa_family = 2, *(_DWORD *)&v4.sa_data[2] = 0, *(_WORD *)v4.sa_data = func0(a1), bind(v1, &v4, v2) < 0)
|| listen(v1, 5) < 0 )
{
v1 = -1;
}
return (unsigned int)v1;
}

original_code:
int SocketInit(int port) {
    if (port <= 0) {
        return -1;
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        return -1;
    }
    struct sockaddr_in serv_sockaddr;
    serv_sockaddr.sin_family = 2;
    serv_sockaddr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    serv_sockaddr.sin_port = htons(port);
    if (bind(sd, (struct sockaddr *) &serv_sockaddr, sizeof (serv_sockaddr)) < 0) {
        return -1;
    }
    if (listen(sd, 5) < 0) {
        return -1;
    }
    return (sd);
}

canonical_original_code:
int SocketInit(int port) {
    if (port <= 0) {
        return -1;
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        return -1;
    }
    struct sockaddr_in serv_sockaddr;
    serv_sockaddr.sin_family = 2;
    serv_sockaddr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    serv_sockaddr.sin_port = htons(port);
    if (bind(sd, (struct sockaddr *) &serv_sockaddr, sizeof (serv_sockaddr)) < 0) {
        return -1;
    }
    if (listen(sd, 5) < 0) {
        return -1;
    }
    return (sd);
}


LocalSocketInfo

decompiled_code:
int __fastcall LocalSocketInfo(int a1, char *a2, _DWORD *a3)
{
int result; // eax
struct hostent *v5; // rax
char *v6; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
result = getsockname(a1, &addr, &len);
if ( !result )
{
if ( a3 )
{
result = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
*a3 = (unsigned __int16)result;
}
if ( a2 )
{
result = gethostname(a2, 0x80uLL);
if ( result < 0 )
{
v5 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
if ( v5 )
{
result = (unsigned int)strncpy(a2, v5->h_name, 0x80uLL);
}
else
{
v6 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
result = (unsigned int)strncpy(a2, v6, 0x80uLL);
}
}
a2[127] = 0;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int a1, char *a2, _DWORD *a3)
{
int result; // eax
struct hostent *v5; // rax
char *v6; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
result = getsockname(a1, &addr, &len);
if ( !result )
{
if ( a3 )
{
result = (unsigned __int16)func0(*(__int16 *)addr.sa_data);
*a3 = (unsigned __int16)result;
}
if ( a2 )
{
result = gethostname(a2, 0x80uLL);
if ( result < 0 )
{
v5 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
if ( v5 )
{
result = (unsigned int)strncpy(a2, v5->h_name, 0x80uLL);
}
else
{
v6 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
result = (unsigned int)strncpy(a2, v6, 0x80uLL);
}
}
a2[127] = 0;
}
}
return result;
}

original_code:
void LocalSocketInfo(int sockfd, char* hostname, int* port) {
    struct sockaddr_in local_addr;
    socklen_t local_addr_len = sizeof (local_addr);
    if (getsockname(sockfd, (struct sockaddr*) &local_addr, &local_addr_len) == 0) {
        if (port != ((void *)0)) *port = ntohs(local_addr.sin_port);
        if (hostname != ((void *)0)) {
            if (gethostname(hostname, 128) < 0) {
                struct hostent* myhostent = gethostbyaddr(
                        (char *) &local_addr.sin_addr.s_addr,
                        sizeof (local_addr.sin_addr.s_addr), 2);
                if (myhostent == ((void *)0)) {
                    strncpy(hostname, inet_ntoa(local_addr.sin_addr), 128);
                } else {
                    strncpy(hostname, myhostent->h_name, 128);
                }
            }
            hostname[(128 - 1)] = '\0';
        }
    }
}

canonical_original_code:
void LocalSocketInfo(int sockfd, char *hostname, int *port) {
    struct sockaddr_in local_addr;
    unsigned int local_addr_len = sizeof (local_addr);
    if (getsockname(sockfd, (struct sockaddr *) &local_addr, &local_addr_len) == 0) {
        if (port != ((void *)0)) *port = ntohs(local_addr.sin_port);
        if (hostname != ((void *)0)) {
            if (gethostname(hostname, 128) < 0) {
                struct hostent *myhostent = gethostbyaddr(
                        (char *) &local_addr.sin_addr.s_addr,
                        sizeof (local_addr.sin_addr.s_addr), 2);
                if (myhostent == ((void *)0)) {
                    strncpy(hostname, inet_ntoa(local_addr.sin_addr), 128);
                } else {
                    strncpy(hostname, myhostent->h_name, 128);
                }
            }
            hostname[(128 - 1)] = '\0';
        }
    }
}


AcceptConnection

decompiled_code:
__int64 __fastcall AcceptConnection(int a1)
{
int v1; // er12
unsigned __int16 v2; // ax
const char **v3; // rcx
unsigned __int16 v5; // bx
char *v6; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
return (unsigned int)-1;
if ( gethostbyaddr(&addr.sa_data[2], 4u, 2) )
{
v2 = _bswap_16(*(__int16 *)addr.sa_data);
printf("admin - accept %s:%hu\n", *v3, v2);
return (unsigned int)v1;
}
v5 = _bswap_16(*(__int16 *)addr.sa_data);
v6 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("admin - accept %s:%hu\n", v6, v5);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
int v1; // er12
unsigned __int16 v2; // ax
const char **v3; // rcx
unsigned __int16 v5; // bx
char *v6; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
return (unsigned int)-1;
if ( gethostbyaddr(&addr.sa_data[2], 4u, 2) )
{
v2 = func0(*(__int16 *)addr.sa_data);
printf("admin - accept %s:%hu\n", *v3, v2);
return (unsigned int)v1;
}
v5 = func0(*(__int16 *)addr.sa_data);
v6 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("admin - accept %s:%hu\n", v6, v5);
return (unsigned int)v1;
}

original_code:
int AcceptConnection(int sockfd) {
    if (sockfd < 0) {
        return -1;
    }
    struct sockaddr_in cli_sockaddr;
    socklen_t client_socklen = sizeof (cli_sockaddr);
    int newsockfd = accept(sockfd, (struct sockaddr *) &cli_sockaddr, &client_socklen);
    if (newsockfd < 0) {
        return -1;
    }
    struct hostent *hp = gethostbyaddr((char *) &cli_sockaddr.sin_addr.s_addr, sizeof (cli_sockaddr.sin_addr.s_addr), 2);
    if (hp == ((void *)0)) {
        printf("admin - accept %s:%hu\n", inet_ntoa(cli_sockaddr.sin_addr), ntohs(cli_sockaddr.sin_port));
    } else {
        printf("admin - accept %s:%hu\n", hp->h_name, ntohs(cli_sockaddr.sin_port));
    }
    return newsockfd;
}

canonical_original_code:
int AcceptConnection(int sockfd) {
    if (sockfd < 0) {
        return -1;
    }
    struct sockaddr_in cli_sockaddr;
    unsigned int client_socklen = sizeof (cli_sockaddr);
    int newsockfd = accept(sockfd, (struct sockaddr *) &cli_sockaddr, &client_socklen);
    if (newsockfd < 0) {
        return -1;
    }
    struct hostent *hp = gethostbyaddr((char *) &cli_sockaddr.sin_addr.s_addr, sizeof (cli_sockaddr.sin_addr.s_addr), 2);
    if (hp == ((void *)0)) {
        printf("admin - accept %s:%hu\n", inet_ntoa(cli_sockaddr.sin_addr), ntohs(cli_sockaddr.sin_port));
    } else {
        printf("admin - accept %s:%hu\n", hp->h_name, ntohs(cli_sockaddr.sin_port));
    }
    return newsockfd;
}

variable_types: newsockfd int


ReadMsg

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

if ( a3 <= 0 || a2 == 0LL || a1 < 0 )
return 0xFFFFFFFFLL;
LODWORD(result) = read(a1, a2, a3);
*((_BYTE *)a2 + (int)result) = 0;
return (unsigned int)result;
}

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

if ( a3 <= 0 || a2 == 0LL || a1 < 0 )
return 0xFFFFFFFFLL;
LODWORD(result) = read(a1, a2, a3);
*((_BYTE *)a2 + (int)result) = 0;
return (unsigned int)result;
}

original_code:
int ReadMsg(int fd, char* buff, int size) {
    if ((fd < 0) || (buff == ((void *)0)) || (size <= 0)) {
        return -1;
    }
    int nread = read(fd, buff, size);
    buff[nread] = '\0';
    return (nread);
}

canonical_original_code:
int ReadMsg(int fd, char *buff, int size) {
    if ((fd < 0) || (buff == ((void *)0)) || (size <= 0)) {
        return -1;
    }
    int nread = read(fd, buff, size);
    buff[nread] = '\0';
    return (nread);
}

variable_types: fd int

variable_types:
buff
char *

variable_types: size int

variable_types: nread int


SendMsg

decompiled_code:
ssize_t __fastcall SendMsg(int a1, const void *a2, int a3)
{
ssize_t result; // rax

if ( a3 <= 0 || a2 == 0LL || a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = write(a1, a2, a3);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, const void *a2, int a3)
{
ssize_t result; // rax

if ( a3 <= 0 || a2 == 0LL || a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = write(a1, a2, a3);
return result;
}

original_code:
int SendMsg(int fd, char* buff, int size) {
    if ((fd < 0) || (buff == ((void *)0)) || (size <= 0)) {
        return -1;
    }
    return (write(fd, buff, size));
}

canonical_original_code:
int SendMsg(int fd, char *buff, int size) {
    if ((fd < 0) || (buff == ((void *)0)) || (size <= 0)) {
        return -1;
    }
    return (write(fd, buff, size));
}


halfPerimiterWireLength

decompiled_code:
__int64 __fastcall halfPerimiterWireLength(__int64 a1, int a2)
{
unsigned int *v3; // rdx
__int64 result; // rax
signed int v5; // er9
signed int v6; // er8
int *v7; // rax
int v8; // edx
signed int v9; // edi
signed int v10; // esi
__int64 v11; // r11
__int64 v12; // rdx
signed int v13; // ecx
signed int v14; // edx

v3 = (unsigned int *)(a1 + 40LL * a2);
result = *v3;
if ( (_DWORD)result )
{
v5 = v3[2];
v6 = v3[1];
v7 = (int *)*((_QWORD *)v3 + 2);
v8 = v3[3];
if ( v8 > 0 )
{
v9 = v6;
v10 = v5;
v11 = (__int64)&v7[v8 - 1 + 1];
while ( 1 )
{
while ( 1 )
{
v12 = a1 + 40LL * *v7;
v13 = *(_DWORD *)(v12 + 8);
v14 = *(_DWORD *)(v12 + 4);
if ( v13 > v10 )
break;
if ( v5 > v13 )
v5 = v13;
if ( v14 > v9 )
goto LABEL_12;
LABEL_7:
if ( v6 > v14 )
v6 = v14;
if ( (int *)v11 == ++v7 )
return (unsigned int)(v10 - v5 + v9 - v6);
}
v10 = v13;
if ( v14 <= v9 )
goto LABEL_7;
LABEL_12:
++v7;
v9 = v14;
if ( (int *)v11 == v7 )
return (unsigned int)(v10 - v5 + v9 - v6);
}
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
unsigned int *v3; // rdx
__int64 result; // rax
signed int v5; // er9
signed int v6; // er8
int *v7; // rax
int v8; // edx
signed int v9; // edi
signed int v10; // esi
__int64 v11; // r11
__int64 v12; // rdx
signed int v13; // ecx
signed int v14; // edx

v3 = (unsigned int *)(a1 + 40LL * a2);
result = *v3;
if ( (_DWORD)result )
{
v5 = v3[2];
v6 = v3[1];
v7 = (int *)*((_QWORD *)v3 + 2);
v8 = v3[3];
if ( v8 > 0 )
{
v9 = v6;
v10 = v5;
v11 = (__int64)&v7[v8 - 1 + 1];
while ( 1 )
{
while ( 1 )
{
v12 = a1 + 40LL * *v7;
v13 = *(_DWORD *)(v12 + 8);
v14 = *(_DWORD *)(v12 + 4);
if ( v13 > v10 )
break;
if ( v5 > v13 )
v5 = v13;
if ( v14 > v9 )
goto LABEL_12;
LABEL_7:
if ( v6 > v14 )
v6 = v14;
if ( (int *)v11 == ++v7 )
return (unsigned int)(v10 - v5 + v9 - v6);
}
v10 = v13;
if ( v14 <= v9 )
goto LABEL_7;
LABEL_12:
++v7;
v9 = v14;
if ( (int *)v11 == v7 )
return (unsigned int)(v10 - v5 + v9 - v6);
}
}
result = 0LL;
}
return result;
}

original_code:
static int halfPerimiterWireLength(gate_t* gateList, int gate) {
   if (!((gateList+gate)->gateNum)) {
      return 0;
   }
   int minX = (gateList+gate)->col;
   int maxX = (gateList+gate)->col;
   int minY = (gateList+gate)->row;
   int maxY = (gateList+gate)->row;
   int* toGates = (gateList+gate)->netsTo;
   int curX = 0;
   int curY = 0;
   for ( int i = 0; i < (gateList+gate)->netToCount; i++) {
      curX = (gateList+toGates[i])->col;
      curY = (gateList+toGates[i])->row;
      if (curX > maxX) {
         maxX = curX;
      } else if (curX < minX) {
         minX = curX;
      }
      if (curY > maxY) {
         maxY = curY;
      } else if (curY < minY) {
         minY = curY;
      }
   }
   return (maxX-minX)+(maxY-minY);
}

canonical_original_code:
static int halfPerimiterWireLength(struct gate *gateList, int gate) {
   if (!((gateList+gate)->gateNum)) {
      return 0;
   }
   int minX = (gateList+gate)->col;
   int maxX = (gateList+gate)->col;
   int minY = (gateList+gate)->row;
   int maxY = (gateList+gate)->row;
   int *toGates = (gateList+gate)->netsTo;
   int curX = 0;
   int curY = 0;
   for ( int i = 0; i < (gateList+gate)->netToCount; i++) {
      curX = (gateList+toGates[i])->col;
      curY = (gateList+toGates[i])->row;
      if (curX > maxX) {
         maxX = curX;
      } else if (curX < minX) {
         minX = curX;
      }
      if (curY > maxY) {
         maxY = curY;
      } else if (curY < minY) {
         minY = curY;
      }
   }
   return (maxX-minX)+(maxY-minY);
}

variable_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; } *

variable_types: gate int

variable_types: minX int

variable_types: maxX int

variable_types: minY int

variable_types: maxY int

variable_types:
toGates
int *

variable_types: curX int

variable_types: curY int


decompiled_code:
// attributes: thunk
__int64 __fastcall grid_routeLen(__int64 a1, int a2)
{
return halfPerimiterWireLength(a1, a2);
}

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

original_code:
inline int grid_routeLen(gate_t* gateList, int gate) {
   return halfPerimiterWireLength(gateList,gate);
}

canonical_original_code:
inline int grid_routeLen(struct gate *gateList, int gate) {
   return halfPerimiterWireLength(gateList,gate);
}

user_defined_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; }


swap

canonical_original_code:
int swap(struct gate **grid, int x1, int y1, int x2, int y2) {
   struct gate *temp = grid[x1+25*y1];
   grid[x1+25*y1] = grid[x2+25*y2];
   grid[x2+25*y2] = temp;
   if (grid[x1+25*y1]) {
      grid[x1+25*y1]->col = x1;
      grid[x1+25*y1]->row = y1;
   }
   if (grid[x2+25*y2]) {
      grid[x2+25*y2]->col = x2;
      grid[x2+25*y2]->row = y2;
   }
   return 0;
}

variable_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; } **


original_code:
inline int grid_partialLen(grid_t* grid, gate_t* gates, int x1, int y1, int x2, int y2) {
   int len = 0;
   if (grid[x1+25*y1]) {
      len += grid_routeLen(gates,grid[x1+25*y1]->gateNum);
      for (int i =0; i < grid[x1+25*y1]->netFromCount; i++) {
         if (grid[x1+25*y1]->netsFrom[i] != grid[x1+25*y1]->gateNum) {
            len += grid_routeLen(gates,grid[x1+25*y1]->netsFrom[i]);
         }
      }
   }
   if (grid[x2+25*y2]) {
      len += grid_routeLen(gates,grid[x2+25*y2]->gateNum);
      for (int i =0; i < grid[x2+25*y2]->netFromCount; i++) {
         if (grid[x2+25*y2]->netsFrom[i] != grid[x2+25*y2]->gateNum) {
            len += grid_routeLen(gates,grid[x2+25*y2]->netsFrom[i]);
         }
      }
   }
   return len;
}

canonical_original_code:
inline int grid_partialLen(struct gate **grid, struct gate *gates, int x1, int y1, int x2, int y2) {
   int len = 0;
   if (grid[x1+25*y1]) {
      len += grid_routeLen(gates,grid[x1+25*y1]->gateNum);
      for (int i = 0; i < grid[x1+25*y1]->netFromCount; i++) {
         if (grid[x1+25*y1]->netsFrom[i] != grid[x1+25*y1]->gateNum) {
            len += grid_routeLen(gates,grid[x1+25*y1]->netsFrom[i]);
         }
      }
   }
   if (grid[x2+25*y2]) {
      len += grid_routeLen(gates,grid[x2+25*y2]->gateNum);
      for (int i = 0; i < grid[x2+25*y2]->netFromCount; i++) {
         if (grid[x2+25*y2]->netsFrom[i] != grid[x2+25*y2]->gateNum) {
            len += grid_routeLen(gates,grid[x2+25*y2]->netsFrom[i]);
         }
      }
   }
   return len;
}

variable_types:
gates
struct gate *

variable_types: len int

variable_types: i int


decompiled_code:
__int64 __fastcall grid_init(_QWORD *a1, void **a2)
{
char *v2; // rbp
void *v3; // rax
FILE *v4; // rdx
char *v5; // rax
__int64 result; // rax

v2 = (char *)malloc(0xC8uLL);
v3 = calloc(0x271uLL, 8uLL);
v4 = stdin;
*a1 = v3;
if ( fgets(v2, 200, v4) )
{
v5 = strtok(v2, " ");
GATE_COUNT = atoi(v5) + 3;
printf("Gates: %d\n", (unsigned int)GATE_COUNT);
*a2 = calloc(GATE_COUNT, 0x28uLL);
strtok(0LL, " ");
free(v2);
result = 0LL;
}
else
{
free(v2);
result = (unsigned int)-(a2 == 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, void **a2)
{
char *v2; // rbp
void *v3; // rax
FILE *v4; // rdx
char *v5; // rax
__int64 result; // rax

v2 = (char *)malloc(0xC8uLL);
v3 = calloc(0x271uLL, 8uLL);
v4 = stdin;
*a1 = v3;
if ( fgets(v2, 200, v4) )
{
v5 = strtok(v2, " ");
GATE_COUNT = atoi(v5) + 3;
printf("Gates: %d\n", (unsigned int)GATE_COUNT);
*a2 = calloc(GATE_COUNT, 0x28uLL);
strtok(0LL, " ");
free(v2);
result = 0LL;
}
else
{
free(v2);
result = (unsigned int)-(a2 == 0LL);
}
return result;
}

original_code:
int grid_init(grid_t** grid, gate_t** gates) {
   char * buffer = malloc(200);
   char * tok;
   *grid = calloc(25*25,sizeof(grid_t));
   if ( fgets(buffer,200,stdin) ) {
      tok = strtok( buffer, (const char*) " " );
      GATE_COUNT = atoi(tok)+3;
      printf("Gates: %d\n",GATE_COUNT);
      *gates = calloc(GATE_COUNT,sizeof(gate_t));
      tok = strtok( ((void *)0), (const char*) " " );
   }
   free(buffer);
   if (!grid || !gates) {
      return -1;
   }
   return 0;
}

canonical_original_code:
int grid_init(struct gate ***grid, struct gate **gates) {
   char *buffer = malloc(200);
   char *tok;
   *grid = calloc(25*25,sizeof(grid_t));
   if ( fgets(buffer,200,stdin) ) {
      tok = strtok( buffer, (char *) " " );
      GATE_COUNT = atoi(tok)+3;
      printf("Gates: %d\n",GATE_COUNT);
      *gates = calloc(GATE_COUNT,sizeof(gate_t));
      tok = strtok( ((void *)0), (char *) " " );
   }
   free(buffer);
   if (!grid || !gates) {
      return -1;
   }
   return 0;
}

variable_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; } ***

variable_types:
gates
struct gate **

variable_types:
buffer
char *

variable_types:
tok
char *


original_code:
int grid_fill(grid_t* grid, gate_t* gates) {
   char * buffer = malloc(200);
   int* pointerBuf = 0;
   char*tok = 0;
   int curGateNum = 0;
   int pointGateNum = 0;
   int count;
   int xPlace = 0;
   int yPlace = 0;
   while (1) {
      if ( fgets(buffer,200,stdin) ) {
         tok = strtok( buffer, (const char*) " " );
         curGateNum = atoi(tok);
         tok = strtok( ((void *)0), (const char*) " " );
         pointerBuf = malloc(sizeof(int)*atoi(tok));
         gates[curGateNum].gateNum = curGateNum;
         do {
            rand_rdrand(&xPlace,25);
            gates[curGateNum].col = xPlace;
            rand_rdrand(&yPlace,25);
            gates[curGateNum].row = yPlace;
         } while ( grid[xPlace + 25*yPlace] );
         grid[xPlace + 25*yPlace] = gates+curGateNum;
         count = 0;
         while (1) {
            tok = strtok( ((void *)0),(const char*)" " );
            if ( !tok || (tok[0] == '/') ) {
               break;
            }
            pointGateNum = atoi(tok);
            pointerBuf[count++] = pointGateNum;
            gates[pointGateNum].netFromCount += 1;
            gates[pointGateNum].netsFrom = realloc(gates[pointGateNum].netsFrom, gates[pointGateNum].netFromCount*sizeof(void*));
            gates[pointGateNum].netsFrom[gates[pointGateNum].netFromCount-1] = curGateNum;
         }
         gates[curGateNum].netToCount = count;
         gates[curGateNum].netsTo = pointerBuf;
      } else if (feof(stdin)) {
         break;
      }
   }
   free(buffer);
   return 0;
}

canonical_original_code:
int grid_fill(struct gate **grid, struct gate *gates) {
   char *buffer = malloc(200);
   int *pointerBuf = 0;
   char *tok = 0;
   int curGateNum = 0;
   int pointGateNum = 0;
   int count;
   int xPlace = 0;
   int yPlace = 0;
   while (1) {
      if ( fgets(buffer,200,stdin) ) {
         tok = strtok( buffer, (char *) " " );
         curGateNum = atoi(tok);
         tok = strtok( ((void *)0), (char *) " " );
         pointerBuf = malloc(sizeof(int)*atoi(tok));
         gates[curGateNum].gateNum = curGateNum;
         do {
            rand_rdrand(&xPlace,25);
            gates[curGateNum].col = xPlace;
            rand_rdrand(&yPlace,25);
            gates[curGateNum].row = yPlace;
         } while ( grid[xPlace + 25*yPlace] );
         grid[xPlace + 25*yPlace] = gates+curGateNum;
         count = 0;
         while (1) {
            tok = strtok( ((void *)0),(char *)" " );
            if ( !tok || (tok[0] == '/') ) {
               break;
            }
            pointGateNum = atoi(tok);
            pointerBuf[count++] = pointGateNum;
            gates[pointGateNum].netFromCount += 1;
            gates[pointGateNum].netsFrom = realloc(gates[pointGateNum].netsFrom, gates[pointGateNum].netFromCount*sizeof(void *));
            gates[pointGateNum].netsFrom[gates[pointGateNum].netFromCount-1] = curGateNum;
         }
         gates[curGateNum].netToCount = count;
         gates[curGateNum].netsTo = pointerBuf;
      } else if (feof(stdin)) {
         break;
      }
   }
   free(buffer);
   return 0;
}

variable_types:
pointerBuf
int *

variable_types: curGateNum int

variable_types: pointGateNum int

variable_types: count int

variable_types: xPlace int

variable_types: yPlace int


decompiled_code:
__int64 __fastcall grid_free(void *ptr, void *a2)
{
char *v3; // rbx
int v4; // ebp
void *v5; // rdi

if ( GATE_COUNT > 0 )
{
v3 = (char *)a2 + 16;
v4 = 0;
do
{
v5 = (void *)*((_QWORD *)v3 + 2);
if ( v5 )
free(v5);
if ( *(_QWORD *)v3 )
free(*(void **)v3);
++v4;
v3 += 40;
}
while ( GATE_COUNT > v4 );
}
free(a2);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *ptr, void *a2)
{
char *v3; // rbx
int v4; // ebp
void *v5; // rdi

if ( GATE_COUNT > 0 )
{
v3 = (char *)a2 + 16;
v4 = 0;
do
{
v5 = (void *)*((_QWORD *)v3 + 2);
if ( v5 )
free(v5);
if ( *(_QWORD *)v3 )
free(*(void **)v3);
++v4;
v3 += 40;
}
while ( GATE_COUNT > v4 );
}
free(a2);
free(ptr);
return 0LL;
}

original_code:
int grid_free(grid_t* grid, gate_t* gates)
{
   int count = 0;
   while (count < GATE_COUNT) {
      if (gates[count].netsFrom) {
         free(gates[count].netsFrom);
      }
      if (gates[count].netsTo) {
         free(gates[count].netsTo);
      }
      count++;
   }
   free(gates);
   free(grid);
   return 0;
}

canonical_original_code:
int grid_free(struct gate **grid, struct gate *gates)
{
   int count = 0;
   while (count < GATE_COUNT) {
      if (gates[count].netsFrom) {
         free(gates[count].netsFrom);
      }
      if (gates[count].netsTo) {
         free(gates[count].netsTo);
      }
      count++;
   }
   free(gates);
   free(grid);
   return 0;
}


decompiled_code:
__int64 __fastcall grid_dumpGrid(__int64 a1)
{
int v1; // ebx
_QWORD *v2; // rbp
int v3; // er12
_QWORD *v4; // rbx
int v5; // ebx

v1 = 25;
do
{
putchar(45);
--v1;
}
while ( v1 );
v2 = (_QWORD *)(a1 + 5000);
v3 = 0;
putchar(10);
do
{
v4 = v2 - 625;
do
{
while ( *v4 )
{
v4 += 25;
putchar(49);
if ( v4 == v2 )
goto LABEL_8;
}
v4 += 25;
putchar(32);
}
while ( v4 != v2 );
LABEL_8:
++v3;
v2 = v4 + 1;
putchar(10);
}
while ( v3 != 25 );
v5 = 25;
do
{
putchar(45);
--v5;
}
while ( v5 );
putchar(10);
return 0LL;
}

original_code:
int grid_dumpGrid(grid_t* grid) {
   for (int i =0; i < 25; i++) {
      printf("-");
   }
   printf("\n");
   for (int i = 0; i < 25; i++) {
      for (int j =0; j < 25; j++) {
         if (grid[i+25*j]) {
            printf("1");
         } else {
            printf(" ");
         }
      }
      printf("\n");
   }
   for (int i =0; i < 25; i++) {
      printf("-");
   }
   printf("\n");
   return 0;
}

canonical_original_code:
int grid_dumpGrid(struct gate **grid) {
   for (int i = 0; i < 25; i++) {
      printf("-");
   }
   printf("\n");
   for (int i = 0; i < 25; i++) {
      for (int j = 0; j < 25; j++) {
         if (grid[i+25*j]) {
            printf("1");
         } else {
            printf(" ");
         }
      }
      printf("\n");
   }
   for (int i = 0; i < 25; i++) {
      printf("-");
   }
   printf("\n");
   return 0;
}

variable_types: j int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // er13
__int64 v5; // rsi
int v6; // er12
__int64 v7; // rdx
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rdx
int v11; // ebp
int v12; // ebx
unsigned int v14; // [rsp+0h] [rbp-48h] BYREF
unsigned int v15; // [rsp+4h] [rbp-44h] BYREF
unsigned int v16; // [rsp+8h] [rbp-40h] BYREF
unsigned int v17; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v18; // [rsp+10h] [rbp-38h] BYREF
__int64 v19[6]; // [rsp+18h] [rbp-30h] BYREF

v18 = 0LL;
v19[0] = 0LL;
grid_init(v19, &v18, envp);
rand_init();
grid_fill(v19[0], v18);
if ( GATE_COUNT <= 0 )
{
v4 = 0;
}
else
{
v3 = 0;
v4 = 0;
do
{
v5 = (unsigned int)v3++;
v4 += grid_routeLen(v18, v5);
}
while ( GATE_COUNT > v3 );
}
v6 = 0;
grid_dumpGrid(v19[0]);
printf("Manhattan distance: %d\n", v4);
do
{
while ( 1 )
{
rand_rdrand(&v14, "e: %d\n", v7);
rand_rdrand(&v15, "e: %d\n", v8);
rand_rdrand(&v16, "e: %d\n", v9);
rand_rdrand(&v17, "e: %d\n", v10);
v11 = grid_partialLen(v19[0], v18, v14, v16, v15, v17);
swap(v19[0], v14, v16, v15, v17);
v12 = grid_partialLen(v19[0], v18, v14, v16, v15, v17);
if ( v12 - v11 >= 0 )
break;
v4 += v12 - v11;
v6 = 0;
}
swap(v19[0], v14, v16, v15, v17);
v6 += v11 == v12;
}
while ( v6 <= 1000000 );
grid_dumpGrid(v19[0]);
printf("Manhattan Length: %d\n", v4);
grid_free(v19[0], v18);
return 0;
}

original_code:
int main()
{
   gate_t* gates = ((void *)0);
   grid_t* grid = ((void *)0);
   grid_init(&grid,&gates);
   rand_init();
   grid_fill(grid,gates);
   int totalLen = 0;
   int count = 0;
   int x1,x2,y1,y2;
   int befLen, aftLen;
   int repeats = 0;
   while (count < GATE_COUNT) {
      totalLen += grid_routeLen(gates,count++);
   }
   grid_dumpGrid(grid);
   printf("Manhattan distance: %d\n",totalLen);
   while (1) {
      rand_rdrand(&x1,25);
      rand_rdrand(&x2,25);
      rand_rdrand(&y1,25);
      rand_rdrand(&y2,25);
      befLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      swap(grid, x1, y1, x2, y2);
      aftLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      if ( aftLen-befLen < 0 ) {
         repeats = 0;
         totalLen += aftLen-befLen;
      } else {
         swap(grid, x1, y1, x2, y2);
         if ( aftLen == befLen ) {
            repeats++;
         }
         if (repeats > 1000000) {
            break;
         }
      }
   }
   grid_dumpGrid(grid);
   printf("Manhattan Length: %d\n",totalLen);
   grid_free(grid,gates);
   return 0;
}

canonical_original_code:
int main()
{
   struct gate *gates = ((void *)0);
   struct gate **grid = ((void *)0);
   grid_init(&grid,&gates);
   rand_init();
   grid_fill(grid,gates);
   int totalLen = 0;
   int count = 0;
   int x1; int x2; int y1; int y2;
   int befLen; int aftLen;
   int repeats = 0;
   while (count < GATE_COUNT) {
      totalLen += grid_routeLen(gates,count++);
   }
   grid_dumpGrid(grid);
   printf("Manhattan distance: %d\n",totalLen);
   while (1) {
      rand_rdrand(&x1,25);
      rand_rdrand(&x2,25);
      rand_rdrand(&y1,25);
      rand_rdrand(&y2,25);
      befLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      swap(grid, x1, y1, x2, y2);
      aftLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      if ( aftLen-befLen < 0 ) {
         repeats = 0;
         totalLen += aftLen-befLen;
      } else {
         swap(grid, x1, y1, x2, y2);
         if ( aftLen == befLen ) {
            repeats++;
         }
         if (repeats > 1000000) {
            break;
         }
      }
   }
   grid_dumpGrid(grid);
   printf("Manhattan Length: %d\n",totalLen);
   grid_free(grid,gates);
   return 0;
}

variable_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; } *

variable_types: totalLen int

variable_types: befLen int

variable_types: aftLen int

variable_types: repeats int


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

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

original_code:
int rand_init() {
   return 0;
}

canonical_original_code:
int rand_init() {
   return 0;
}


decompiled_code:
__int64 __fastcall rand_rdrand(unsigned int *a1, int a2)
{
char v2; // cf
unsigned int v3; // er8

v3 = 10;
do
{
__asm { rdrand rax }
if ( v2 )
break;
--v3;
}
while ( v3 );
*a1 = (unsigned int)(a2 * WORD1(_RAX)) >> 16;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, int a2)
{
char v2; // cf
unsigned int v3; // er8

v3 = 10;
do
{
__asm { rdrand rax }
if ( v2 )
break;
--v3;
}
while ( v3 );
*a1 = (unsigned int)(a2 * WORD1(_RAX)) >> 16;
return v3;
}

original_code:
inline int rand_rdrand(int *v, int range) {
   int ok;
    asm volatile("1: " ".byte 0x48,0x0f,0xc7,0xf0" "\n\t"
             "jc 2f\n\t"
             "decl %0\n\t"
             "jnz 1b\n\t"
             "2:"
             : "=r" (ok), "=a" (*v)
             : "0" (10));
   *v = (((unsigned int)*v>>16)*range)>>16;
   return ok;
}

variable_types:
v
int *

variable_types: range int

variable_types: ok int


decompiled_code:
void draw_triangle()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void
draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2,
    byte r, byte g, byte b)
{
}

canonical_original_code:
void draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2, unsigned char r, unsigned char g, unsigned char b)
{
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char


decompiled_code:
void draw_triangle_optimized()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void
draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2,
    byte r, byte g, byte b)
{
}

canonical_original_code:
void draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2, unsigned char r, unsigned char g, unsigned char b)
{
}


printDebugv

decompiled_code:
int __fastcall printDebugv(const char *a1, const char *a2, unsigned int a3)
{
return fprintf(stderr, "%s : line %d in %s\n", a1, a3, a2);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2, unsigned int a3)
{
return fprintf(stderr, "%s : line %d in %s\n", a1, a3, a2);
}

original_code:
void printDebugv( const char *msg, const char *file, const int line) {
 fprintf(stderr, "%s : line %d in %s\n", msg, line, file);
}

canonical_original_code:
void printDebugv(char *msg, char *file, int line) {
 fprintf(stderr, "%s : line %d in %s\n", msg, line, file);
}

variable_types:
msg
char *

variable_types:
file
char *

variable_types: line int


printDebug

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

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

original_code:
void printDebug( const char *msg) {
 fprintf(stderr, "%s\n", msg);
}

canonical_original_code:
void printDebug(char *msg) {
 fprintf(stderr, "%s\n", msg);
}


escape

decompiled_code:
char *__fastcall escape(char *src)
{
int v1; // ebp
char *v2; // rsi
int v3; // eax
char v4; // cl
__int64 v5; // rcx
int v6; // edx

v1 = strlen(src);
memset(empty, 0, 0x1388uLL);
strcpy(empty, src);
memset(buf, 0, sizeof(buf));
if ( v1 > 0 )
{
v2 = &src[v1 - 1 + 1];
v3 = 0;
do
{
while ( 1 )
{
v4 = *src;
if ( *src == 34 )
break;
++src;
buf[v3++] = v4;
if ( src == v2 )
return buf;
}
++src;
buf[v3] = 38;
buf[v3 + 1] = 113;
v5 = v3 + 4;
buf[v3 + 2] = 117;
buf[v3 + 3] = 111;
v6 = v3 + 5;
v3 += 6;
buf[v5] = 116;
buf[v6] = 59;
}
while ( src != v2 );
}
return buf;
}

canonical_decompiled_code:
char *__fastcall func5(char *src)
{
int v1; // ebp
char *v2; // rsi
int v3; // eax
char v4; // cl
__int64 v5; // rcx
int v6; // edx

v1 = strlen(src);
memset(empty, 0, 0x1388uLL);
strcpy(empty, src);
memset(buf, 0, sizeof(buf));
if ( v1 > 0 )
{
v2 = &src[v1 - 1 + 1];
v3 = 0;
do
{
while ( 1 )
{
v4 = *src;
if ( *src == 34 )
break;
++src;
buf[v3++] = v4;
if ( src == v2 )
return buf;
}
++src;
buf[v3] = 38;
buf[v3 + 1] = 113;
v5 = v3 + 4;
buf[v3 + 2] = 117;
buf[v3 + 3] = 111;
v6 = v3 + 5;
v3 += 6;
buf[v5] = 116;
buf[v6] = 59;
}
while ( src != v2 );
}
return buf;
}

original_code:
char* escape(char *msg) {
 int len = strlen(msg);
 int i,b=0;
 memset ( empty, 0, 5000);
 strcpy( empty, msg );
 memset ( buf, 0, 5000);
 for(i=0; i<len; i++) {
  if (msg[i] == '"') {
   buf[b++] = '&';
   buf[b++] = 'q';
   buf[b++] = 'u';
   buf[b++] = 'o';
   buf[b++] = 't';
   buf[b++] = ';';
  }else{
   buf[b++] = msg[i];
  }
 }
 return buf;
}

canonical_original_code:
char *escape(char *msg) {
 int len = strlen(msg);
 int i; int b = 0;
 memset ( empty, 0, 5000);
 strcpy( empty, msg );
 memset ( buf, 0, 5000);
 for(i=0; i<len; i++) {
  if (msg[i] == '"') {
   buf[b++] = '&';
   buf[b++] = 'q';
   buf[b++] = 'u';
   buf[b++] = 'o';
   buf[b++] = 't';
   buf[b++] = ';';
  }else{
   buf[b++] = msg[i];
  }
 }
 return buf;
}

variable_types: len int

variable_types: i int

variable_types: b int


suma

decompiled_code:
__int64 __fastcall suma(_DWORD *a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax

if ( a2 <= 0 )
return 0LL;
v2 = (__int64)&a1[a2 - 1 + 1];
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + result);
while ( a1 != (_DWORD *)v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax

if ( a2 <= 0 )
return 0LL;
v2 = (__int64)&a1[a2 - 1 + 1];
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + result);
while ( a1 != (_DWORD *)v2 );
return result;
}

original_code:
int suma(int tab[], int size)
{
  int result = 0;
  for (int i = 0; i < size; ++i)
    result += tab[i];
  return result;
}

variable_types:
tab
int []

variable_types: size int

variable_types: result int


decompiled_code:
double __fastcall srednia_arytmetyczna(_DWORD *a1, int a2)
{
return (double)(int)suma(a1, a2) / (double)a2;
}

canonical_decompiled_code:
double __fastcall func8(_DWORD *a1, int a2)
{
return (double)(int)func7(a1, a2) / (double)a2;
}

original_code:
double srednia_arytmetyczna(int tab[], int size)
{
  return suma(tab, size) / (double)size;
}


decompiled_code:
void __fastcall srednia_geometryczna(__int64 a1, int a2)
{
__int64 v2; // rax
double v3; // xmm0_8

if ( a2 <= 0 )
{
pow(1.0, 1.0 / (double)a2);
}
else
{
v2 = a1 + 4LL * (unsigned int)(a2 - 1) + 4;
v3 = 1.0;
do
{
a1 += 4LL;
v3 = v3 * (double)*(int *)(a1 - 4);
}
while ( a1 != v2 );
pow(v3, 1.0 / (double)a2);
}
}

original_code:
double srednia_geometryczna(int tab[], int size)
{
  double result = 1.0;
  for (int i = 0; i < size; ++i)
    result *= tab[i];
  return pow(result, 1.0/size);
}

variable_types: result double


decompiled_code:
__int64 __fastcall rownanie_kwadratowe(double *a1, double *a2, double a3, double a4, double a5)
{
double v6; // xmm4_8
__int64 result; // rax
double v8; // xmm2_8
double v9; // xmm4_8
double v10; // xmm3_8
double x; // [rsp+8h] [rbp-30h]

v6 = a4 * a4 - a3 * 4.0 * a5;
if ( v6 > 0.0 )
{
v8 = -a4;
if ( v6 < 0.0 )
{
x = v6;
*a1 = (v8 - sqrt(v6)) / (a3 + a3);
v10 = a3 + a3;
v9 = sqrt(x);
}
else
{
v9 = sqrt(v6);
v10 = a3 + a3;
*a1 = (v8 - v9) / (a3 + a3);
}
result = 2LL;
*a2 = (v9 - a4) / v10;
}
else
{
result = 0LL;
if ( v6 >= 0.0 )
{
result = 1LL;
*a1 = -a4 / (a3 + a3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(double *a1, double *a2, double a3, double a4, double a5)
{
double v6; // xmm4_8
__int64 result; // rax
double v8; // xmm2_8
double v9; // xmm4_8
double v10; // xmm3_8
double x; // [rsp+8h] [rbp-30h]

v6 = a4 * a4 - a3 * 4.0 * a5;
if ( v6 > 0.0 )
{
v8 = -a4;
if ( v6 < 0.0 )
{
x = v6;
*a1 = (v8 - sqrt(v6)) / (a3 + a3);
v10 = a3 + a3;
v9 = sqrt(x);
}
else
{
v9 = sqrt(v6);
v10 = a3 + a3;
*a1 = (v8 - v9) / (a3 + a3);
}
result = 2LL;
*a2 = (v9 - a4) / v10;
}
else
{
result = 0LL;
if ( v6 >= 0.0 )
{
result = 1LL;
*a1 = -a4 / (a3 + a3);
}
}
return result;
}

original_code:
int rownanie_kwadratowe(double a, double b, double c, double *x1, double *x2)
{
  double delta = b*b - 4*a*c;
  if (delta > 0) {
    *x1 = (-b - sqrt(delta))/(2*a);
    *x2 = (-b + sqrt(delta))/(2*a);
    return 2;
  } else if (delta < 0) {
    return 0;
  } else {
    *x1 = -b/(2*a);
    return 1;
  }
}

variable_types: a double

variable_types: b double

variable_types: c double

variable_types: delta double


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
int v4; // ebx
int result; // eax
double v6; // [rsp+8h] [rbp-40h]
double v7; // [rsp+10h] [rbp-38h]
double v8; // [rsp+20h] [rbp-28h] BYREF
double v9[4]; // [rsp+28h] [rbp-20h] BYREF

puts(s);
if ( argc <= 3 )
{
puts(aSpos);
result = -1;
}
else
{
v7 = atof(argv[1]);
v6 = atof(argv[2]);
v3 = atof(argv[3]);
if ( v7 == 0.0 )
{
puts(aToNieJestR);
result = -1;
}
else
{
v4 = rownanie_kwadratowe(&v8, v9, v7);
printf(format, v7, v6, v3);
if ( v4 == 2 )
{
printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", v8, v9[0]);
result = 0;
}
else
{
if ( v4 == 1 )
printf(aR_0, v8);
else
puts(aR);
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
int v4; // ebx
int result; // eax
double v6; // [rsp+8h] [rbp-40h]
double v7; // [rsp+10h] [rbp-38h]
double v8; // [rsp+20h] [rbp-28h] BYREF
double v9[4]; // [rsp+28h] [rbp-20h] BYREF

puts(s);
if ( argc <= 3 )
{
puts(aSpos);
result = -1;
}
else
{
v7 = atof(argv[1]);
v6 = atof(argv[2]);
v3 = atof(argv[3]);
if ( v7 == 0.0 )
{
puts(aToNieJestR);
result = -1;
}
else
{
v4 = func7(&v8, v9, v7);
printf(format, v7, v6, v3);
if ( v4 == 2 )
{
printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", v8, v9[0]);
result = 0;
}
else
{
if ( v4 == 1 )
printf(aR_0, v8);
else
puts(aR);
result = 0;
}
}
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
  puts("Program obliczający pierwiastki równania kwadratowego.");
  if (argc < 4) {
    puts("Sposób użycia: cw08-03 a b c");
    return -1;
  }
  double a = atof(argv[1]);
  double b = atof(argv[2]);
  double c = atof(argv[3]);
  if (a == 0.0) {
    puts("To nie jest równanie kwadratowe!");
    return -1;
  }
  double x1, x2;
  int ilosc_rozwiazan;
  ilosc_rozwiazan = rownanie_kwadratowe(a, b, c, &x1, &x2);
  printf("Dla równania kwadratowego o współczynnikach:\n a = %.2f, b = %.2f i c = %.2f\n", a, b, c);
  if (ilosc_rozwiazan == 2) {
    printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", x1, x2);
  } else if (ilosc_rozwiazan == 1) {
    printf("Równanie ma jedno rozwiązanie:\n x = %.2f\n", x1);
  } else {
    puts("Równanie nie posiada rozwiązań");
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  puts("Program obliczający pierwiastki równania kwadratowego.");
  if (argc < 4) {
    puts("Sposób użycia: cw08-03 a b c");
    return -1;
  }
  double a = atof(argv[1]);
  double b = atof(argv[2]);
  double c = atof(argv[3]);
  if (a == 0.0) {
    puts("To nie jest równanie kwadratowe!");
    return -1;
  }
  double x1; double x2;
  int ilosc_rozwiazan;
  ilosc_rozwiazan = rownanie_kwadratowe(a, b, c, &x1, &x2);
  printf("Dla równania kwadratowego o współczynnikach:\n a = %.2f, b = %.2f i c = %.2f\n", a, b, c);
  if (ilosc_rozwiazan == 2) {
    printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", x1, x2);
  } else if (ilosc_rozwiazan == 1) {
    printf("Równanie ma jedno rozwiązanie:\n x = %.2f\n", x1);
  } else {
    puts("Równanie nie posiada rozwiązań");
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


distance

decompiled_code:
void __fastcall distance(const char *a1, double a2, double a3, double a4, double a5)
{
double v5; // xmm2_8

v5 = (a4 - a2) * (a4 - a2) + (a5 - a3) * (a5 - a3);
if ( v5 < 0.0 )
sqrt(v5);
}

canonical_decompiled_code:
void __fastcall func7(const char *a1, double a2, double a3, double a4, double a5)
{
double v5; // xmm2_8

v5 = (a4 - a2) * (a4 - a2) + (a5 - a3) * (a5 - a3);
if ( v5 < 0.0 )
sqrt(v5);
}

original_code:
double distance(struct Point p1, struct Point p2)
{
  double dx = p2.x - p1.x;
  double dy = p2.y - p1.y;
  return sqrt(dx*dx + dy*dy);
}

variable_types: dx double

variable_types: dy double


podaj

decompiled_code:
int __fastcall podaj(const char *a1)
{
int v1; // ebx
int result; // eax
double v3; // [rsp+8h] [rbp-20h]

do
{
do
{
printf("Podaj %s: ", a1);
v1 = __isoc99_scanf();
result = getchar();
}
while ( v1 != 1 );
}
while ( v3 <= 0.0 );
return result;
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
int v1; // ebx
int result; // eax
double v3; // [rsp+8h] [rbp-20h]

do
{
do
{
printf("Podaj %s: ", a1);
v1 = __isoc99_scanf();
result = getchar();
}
while ( v1 != 1 );
}
while ( v3 <= 0.0 );
return result;
}

original_code:
double podaj(char *nazwa)
{
  int poprawnie_odczytane = 0;
  double wartosc;
  do {
    printf("Podaj %s: ", nazwa);
    poprawnie_odczytane = scanf("%lf", &wartosc);
    getchar();
  } while (poprawnie_odczytane != 1 || wartosc <= 0);
  return wartosc;
}

variable_types:
nazwa
char *

variable_types: wartosc double


decompiled_code:
int __fastcall pole_prostokata(double a1)
{
podaj("a");
podaj("b");
return printf(aPoleProstok, a1 * a1);
}

canonical_decompiled_code:
int __fastcall func8(double a1)
{
func7("a");
func7("b");
return printf(aPoleProstok, a1 * a1);
}

original_code:
void pole_prostokata()
{
  double a = podaj("a");
  double b = podaj("b");
  printf("Pole prostokąta wynosi: %f\n", a*b);
}