decompiled_code:
int log_debug(const char *a1, __int64 a2, const char *a3, unsigned int a4, unsigned int a5, const char *a6, ...)
{
__int64 v7; // r13
__int64 v8; // r14
struct tm *v10; // rax
time_t v12; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a6);
v7 = a4;
v8 = a5;
time(&v12);
v10 = localtime(&v12);
fprintf(
_bss_start,
"%02d-%02d %02d:%02d:%02d %s/%s(%d)[%d]: ",
(unsigned int)(v10->tm_mon + 1),
(unsigned int)v10->tm_mday,
(unsigned int)v10->tm_hour,
(unsigned int)v10->tm_min,
(unsigned int)v10->tm_sec,
a1,
a3,
v7,
v8);
vfprintf(_bss_start, a6, arg);
return fputc(10, _bss_start);
}

canonical_decompiled_code:
int func9(const char *a1, __int64 a2, const char *a3, unsigned int a4, unsigned int a5, const char *a6, ...)
{
__int64 v7; // r13
__int64 v8; // r14
struct tm *v10; // rax
time_t v12; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a6);
v7 = a4;
v8 = a5;
time(&v12);
v10 = localtime(&v12);
fprintf(
_bss_start,
"%02d-%02d %02d:%02d:%02d %s/%s(%d)[%d]: ",
(unsigned int)(v10->tm_mon + 1),
(unsigned int)v10->tm_mday,
(unsigned int)v10->tm_hour,
(unsigned int)v10->tm_min,
(unsigned int)v10->tm_sec,
a1,
a3,
v7,
v8);
vfprintf(_bss_start, a6, arg);
return fputc(10, _bss_start);
}

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


repo: seabat/TohriSDK

decompiled_code:
int __fastcall log_dump(__int64 a1, int a2, __int64 a3)
{
int v3; // er13
int v4; // er15
__int64 v5; // rbp
int v6; // er14
int v7; // er12
__int64 v8; // rbx
int result; // eax

v3 = a2;
v4 = a3;
fprintf(_bss_start, "end_index=%d\n", a3);
v5 = a2 + a1;
v6 = v4 + 1 - a2;
do
{
if ( v4 > v3 + 15 )
{
v7 = 16;
}
else
{
v7 = v6;
if ( v6 <= 0 )
goto LABEL_5;
}
v8 = 0LL;
do
fprintf(_bss_start, "%02x", (unsigned int)*(char *)(v5 + v8++));
while ( v7 > (int)v8 );
LABEL_5:
result = fputc(10, _bss_start);
v3 += 16;
v5 += 16LL;
v6 -= 16;
}
while ( v4 >= v3 );
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1, int a2, __int64 a3)
{
int v3; // er13
int v4; // er15
__int64 v5; // rbp
int v6; // er14
int v7; // er12
__int64 v8; // rbx
int result; // eax

v3 = a2;
v4 = a3;
fprintf(_bss_start, "end_index=%d\n", a3);
v5 = a2 + a1;
v6 = v4 + 1 - a2;
do
{
if ( v4 > v3 + 15 )
{
v7 = 16;
}
else
{
v7 = v6;
if ( v6 <= 0 )
goto LABEL_5;
}
v8 = 0LL;
do
fprintf(_bss_start, "%02x", (unsigned int)*(char *)(v5 + v8++));
while ( v7 > (int)v8 );
LABEL_5:
result = fputc(10, _bss_start);
v3 += 16;
v5 += 16LL;
v6 -= 16;
}
while ( v4 >= v3 );
return result;
}

original_code:
void log_dump( char* buf, int start_index, int end_index ){
 int dump_index = start_index;
 int dump_num = 0;
 int i = 0;
 fprintf(stderr, "end_index=%d\n", end_index);
 do{
  if( (end_index+1)<=dump_index+16 ){
   dump_num = end_index-dump_index+1;
  }else{
   dump_num = 16;
  }
  for(i=0; i<dump_num; i++){
   fprintf(stderr, "%02x", buf[dump_index+i]);
  }
  fprintf(stderr, "\n");
  dump_index += 16;
 }while( dump_index<=end_index );
}

canonical_original_code:
void log_dump(char *buf, int start_index, int end_index){
 int dump_index = start_index;
 int dump_num = 0;
 int i = 0;
 fprintf(stderr, "end_index=%d\n", end_index);
 do{
  if( (end_index+1)<=dump_index+16 ){
   dump_num = end_index-dump_index+1;
  }else{
   dump_num = 16;
  }
  for(i=0; i<dump_num; i++){
   fprintf(stderr, "%02x", buf[dump_index+i]);
  }
  fprintf(stderr, "\n");
  dump_index += 16;
 }while( dump_index<=end_index );
}

variable_types:
buf
char *

variable_types: i int

return_type: void


decompiled_code:
float __fastcall lp_filter(float a1, float a2, float a3)
{
return (float)((float)(1.0 - a3) * a2) + (float)(a3 * a1);
}

canonical_decompiled_code:
float __fastcall func0(float a1, float a2, float a3)
{
return (float)((float)(1.0 - a3) * a2) + (float)(a3 * a1);
}

original_code:
static inline float lp_filter(float a, float b, float d)
{
    return d*a + (1-d)*b;
}

variable_types: a float

variable_types: b float

variable_types: d float

return_type: float


decompiled_code:
__int64 compute_initial_orientation()
{
double v0; // xmm0_8
__int64 result; // rax
_BYTE v2[28]; // [rsp+0h] [rbp-58h] BYREF
int v3[3]; // [rsp+1Ch] [rbp-3Ch] BYREF
float v4; // [rsp+28h] [rbp-30h] BYREF
float v5; // [rsp+2Ch] [rbp-2Ch]
int v6; // [rsp+30h] [rbp-28h]
int v7[3]; // [rsp+34h] [rbp-24h] BYREF
int v8[6]; // [rsp+40h] [rbp-18h] BYREF

v7[0] = dword_F60[0];
v7[1] = dword_F60[1];
v7[2] = dword_F60[2];
v4 = *(float *)&dword_F60[6];
v5 = *(float *)&dword_F60[7];
v6 = dword_F60[8];
v3[0] = 0;
v3[1] = 0;
v3[2] = 1065353216;
normalize(v7);
normalize(&v4);
vec2vec2quat(v8, v7, v3);
rotvecquat(&v4, v8);
v6 = 0;
normalize(&v4);
v0 = atan2(v5, v4);
*(float *)&v0 = v0;
angvec2quat(v2, v3, COERCE_DOUBLE(*(_QWORD *)&v0 ^ LC2));
result = quatmult2(v2, v8);
q0 = v8[0];
q1 = v8[1];
q2 = v8[2];
q3 = v8[3];
return result;
}

original_code:
static void compute_initial_orientation()
{
    float q[4], a[3] = {of[0], of[1], of[2]}, m[3] = {of[6], of[7], of[8]};
    float g[3] = {0, 0, 1};
    normalize(a);
    normalize(m);
    vec2vec2quat(q, a, g);
    rotvecquat(m, q);
    m[2] = 0;
    normalize(m);
    float r[4];
    angvec2quat(r, -atan2(m[1], m[0]), g);
    quatmult2(r, q);
    q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3];
}

canonical_original_code:
static void compute_initial_orientation()
{
    float q[4]; float a[3] = {of[0], of[1], of[2]}; float m[3] = {of[6], of[7], of[8]};
    float g[3] = {0, 0, 1};
    normalize(a);
    normalize(m);
    vec2vec2quat(q, a, g);
    rotvecquat(m, q);
    m[2] = 0;
    normalize(m);
    float r[4];
    angvec2quat(r, -atan2(m[1], m[0]), g);
    quatmult2(r, q);
    q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3];
}

variable_types:
q
float [4]

variable_types:
a
float [3]

variable_types:
m
float [3]

variable_types:
g
float [3]

variable_types:
r
float [4]


decompiled_code:
ssize_t __fastcall i2c_read(char a1, int a2)
{
buf = a1;
write(file, &buf, 1uLL);
return read(file, &buf, a2);
}

canonical_decompiled_code:
ssize_t __fastcall func2(char a1, int a2)
{
buf = a1;
write(file, &buf, 1uLL);
return read(file, &buf, a2);
}

original_code:
void i2c_read(uint8_t addr, int len)
{
    buf[0] = addr;
    write(file, buf, 1);
    read(file, buf, len);
}

canonical_original_code:
void i2c_read(unsigned char addr, int len)
{
    buf[0] = addr;
    write(file, buf, 1);
    read(file, buf, len);
}

variable_types:
addr
unsigned char

variable_types: len int


decompiled_code:
ssize_t __fastcall i2c_write_byte(char a1, char a2)
{
buf[0] = a1;
buf[1] = a2;
return write(file, buf, 2uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func3(char a1, char a2)
{
buf[0] = a1;
buf[1] = a2;
return write(file, buf, 2uLL);
}

original_code:
void i2c_write_byte(uint8_t addr, uint8_t value)
{
    buf[0] = addr;
    buf[1] = value;
    write(file, buf, 2);
}

canonical_original_code:
void i2c_write_byte(unsigned char addr, unsigned char value)
{
    buf[0] = addr;
    buf[1] = value;
    write(file, buf, 2);
}

variable_types:
value
unsigned char


decompiled_code:
void __fastcall read_sensors(double a1, double a2)
{
__int64 v2; // rax
float v3; // xmm2_4
__int64 v4; // rax
__int64 v5; // rax
int v6[11]; // [rsp+0h] [rbp-2Ch]

if ( file != -1 )
{
if ( ioctl(file, 0x703uLL, 64LL) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
i2c_read(2, 6);
if ( (buf[0] & 1) != 0 && (buf[2] & 1) != 0 && (buf[4] & 1) != 0 )
{
v2 = 0LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
dof[v2] = (float)((float)((unsigned __int8)buf[2 * v2] + (buf[2 * v2 + 1] << 8)) * 0.000061035156) + dof[v2];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v2] + 1.0;
cof[v2++] = *(float *)&a1;
}
while ( v2 != 3 );
}
else
{
fwrite("accel miss\n", 1uLL, 0xBuLL, stderr);
}
if ( ioctl(file, 0x703uLL, 104LL, a1, a2) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
i2c_read(26, 1);
if ( (buf[0] & 1) != 0 )
{
i2c_read(29, 6);
v3 = *(float *)&gyro_scale;
v4 = 0LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
dof[v4 + 3] = (float)((float)((unsigned __int8)buf[2 * v4 + 1] + (buf[2 * v4] << 8)) / v3) + dof[v4 + 3];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v4 + 3] + 1.0;
cof[v4++ + 3] = *(float *)&a1;
}
while ( v4 != 3 );
}
else
{
fwrite("gyro miss\n", 1uLL, 0xAuLL, stderr);
}
if ( ioctl(file, 0x703uLL, 30LL, a1, a2) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
i2c_read(9, 1);
if ( (buf[0] & 1) != 0 )
{
i2c_read(3, 6);
v5 = 6LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
v6[6] = 0;
v6[7] = 4;
v6[8] = 2;
dof[v5] = (float)((float)((buf[v6[v5]] << 8) + (unsigned __int8)buf[v6[v5] + 1]) * 0.001953125) + dof[v5];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v5] + 1.0;
cof[v5++] = *(float *)&a1;
}
while ( v5 != 9 );
}
else
{
fwrite("mag miss\n", 1uLL, 9uLL, stderr);
}
i2c_write_byte(2, 1);
if ( ioctl(file, 0x703uLL, 119LL, a1, a2) != -1 )
{
i2c_read(246, 3);
dof[9] = (float)((float)(buf[2] + (buf[0] << 24) + (buf[1] << 16)) * 0.000000059604645) + dof[9];
cof[9] = cof[9] + 1.0;
i2c_write_byte(244, 244);
}
}
}

canonical_decompiled_code:
void __fastcall func4(double a1, double a2)
{
__int64 v2; // rax
float v3; // xmm2_4
__int64 v4; // rax
__int64 v5; // rax
int v6[11]; // [rsp+0h] [rbp-2Ch]

if ( file != -1 )
{
if ( ioctl(file, 0x703uLL, 64LL) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
func2(2, 6);
if ( (buf[0] & 1) != 0 && (buf[2] & 1) != 0 && (buf[4] & 1) != 0 )
{
v2 = 0LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
dof[v2] = (float)((float)((unsigned __int8)buf[2 * v2] + (buf[2 * v2 + 1] << 8)) * 0.000061035156) + dof[v2];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v2] + 1.0;
cof[v2++] = *(float *)&a1;
}
while ( v2 != 3 );
}
else
{
fwrite("accel miss\n", 1uLL, 0xBuLL, stderr);
}
if ( ioctl(file, 0x703uLL, 104LL, a1, a2) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
func2(26, 1);
if ( (buf[0] & 1) != 0 )
{
func2(29, 6);
v3 = *(float *)&gyro_scale;
v4 = 0LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
dof[v4 + 3] = (float)((float)((unsigned __int8)buf[2 * v4 + 1] + (buf[2 * v4] << 8)) / v3) + dof[v4 + 3];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v4 + 3] + 1.0;
cof[v4++ + 3] = *(float *)&a1;
}
while ( v4 != 3 );
}
else
{
fwrite("gyro miss\n", 1uLL, 0xAuLL, stderr);
}
if ( ioctl(file, 0x703uLL, 30LL, a1, a2) < 0 )
{
fwrite("Failed to aquire bus address\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
func2(9, 1);
if ( (buf[0] & 1) != 0 )
{
func2(3, 6);
v5 = 6LL;
*(_QWORD *)&a2 = (unsigned int)LC0;
do
{
v6[6] = 0;
v6[7] = 4;
v6[8] = 2;
dof[v5] = (float)((float)((buf[v6[v5]] << 8) + (unsigned __int8)buf[v6[v5] + 1]) * 0.001953125) + dof[v5];
*(_QWORD *)&a1 = (unsigned int)LC0;
*(float *)&a1 = cof[v5] + 1.0;
cof[v5++] = *(float *)&a1;
}
while ( v5 != 9 );
}
else
{
fwrite("mag miss\n", 1uLL, 9uLL, stderr);
}
func3(2, 1);
if ( ioctl(file, 0x703uLL, 119LL, a1, a2) != -1 )
{
func2(246, 3);
dof[9] = (float)((float)(buf[2] + (buf[0] << 24) + (buf[1] << 16)) * 0.000000059604645) + dof[9];
cof[9] = cof[9] + 1.0;
func3(244, 244);
}
}
}

original_code:
static void read_sensors()
{
    if(file == -1)
        return;
    if(ioctl(file, 0x0703, 0x40) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    int i;
    i2c_read(0x02, 6);
    if(buf[0] & 1 && buf[2] & 1 && buf[4] & 1) {
        for(i=0; i<3; i++) {
            dof[i] += (float)(((int16_t)buf[2*i+1]<<8) + (uint8_t)buf[2*i]) / 16384;
            cof[i]++;
        }
    } else
        fprintf(stderr, "accel miss\n");
    if(ioctl(file, 0x0703, 0x68) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    i2c_read(0x1a, 1);
    if(buf[0] & 1) {
        i2c_read(0x1d, 6);
        for(i=0; i<3; i++) {
            dof[3+i] += (float)(((int16_t)buf[2*i+0]<<8) + (uint8_t)buf[2*i+1]) / gyro_scale;
            cof[3+i]++;
        }
    } else
        fprintf(stderr, "gyro miss\n");
    if(ioctl(file, 0x0703, 0x1e) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    i2c_read(0x09, 1);
    if(buf[0] & 1) {
        i2c_read(0x03, 6);
        for(i=0; i<3; i++) {
            int ind[3] = {0, 4, 2};
            dof[6+i] += (float)((int)((int16_t)buf[ind[i]]<<8) + (uint8_t)buf[ind[i]+1]) / 512;
            cof[6+i]++;
        }
    } else
        fprintf(stderr, "mag miss\n");
    i2c_write_byte(0x02, 0x01);
    if(ioctl(file, 0x0703, 0x77) != -1) {
        i2c_read(0xf6, 3);
        dof[9] += (float)(((int32_t)buf[0]<<24) +
                          ((int32_t)buf[1]<<16) +
                          ((int32_t)buf[2])) / (1<<24); cof[9]++;
        i2c_write_byte(0xf4, 0xf4);
    }
}

canonical_original_code:
static void read_sensors()
{
    if(file == -1)
        return;
    if(ioctl(file, 0x0703, 0x40) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    int i;
    i2c_read(0x02, 6);
    if(buf[0] & 1 && buf[2] & 1 && buf[4] & 1) {
        for(i=0; i<3; i++) {
            dof[i] += (float)(((short)buf[2*i+1]<<8) + (unsigned char)buf[2*i]) / 16384;
            cof[i]++;
        }
    } else
        fprintf(stderr, "accel miss\n");
    if(ioctl(file, 0x0703, 0x68) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    i2c_read(0x1a, 1);
    if(buf[0] & 1) {
        i2c_read(0x1d, 6);
        for(i=0; i<3; i++) {
            dof[3+i] += (float)(((short)buf[2*i+0]<<8) + (unsigned char)buf[2*i+1]) / gyro_scale;
            cof[3+i]++;
        }
    } else
        fprintf(stderr, "gyro miss\n");
    if(ioctl(file, 0x0703, 0x1e) < 0) {
        fprintf(stderr, "Failed to aquire bus address\n");
        exit(1);
    }
    i2c_read(0x09, 1);
    if(buf[0] & 1) {
        i2c_read(0x03, 6);
        for(i=0; i<3; i++) {
            int ind[3] = {0, 4, 2};
            dof[6+i] += (float)((int)((short)buf[ind[i]]<<8) + (unsigned char)buf[ind[i]+1]) / 512;
            cof[6+i]++;
        }
    } else
        fprintf(stderr, "mag miss\n");
    i2c_write_byte(0x02, 0x01);
    if(ioctl(file, 0x0703, 0x77) != -1) {
        i2c_read(0xf6, 3);
        dof[9] += (float)(((int)buf[0]<<24) +
                          ((int)buf[1]<<16) +
                          ((int)buf[2])) / (1<<24); cof[9]++;
        i2c_write_byte(0xf4, 0xf4);
    }
}

variable_types:
ind
int [3]


decompiled_code:
int probe_i2c_devices()
{
int result; // eax

if ( !ioctl(file, 0x703uLL, 64LL) )
{
i2c_read(0, 2);
if ( buf[0] == 3 && buf[1] == 20 )
puts("found bma180");
}
if ( !ioctl(file, 0x703uLL, 104LL) )
{
i2c_read(0, 1);
if ( buf[0] == 104 )
{
puts("found itg3200");
i2c_write_byte(21, 7);
i2c_write_byte(22, 29);
i2c_write_byte(62, 1);
}
}
if ( !ioctl(file, 0x703uLL, 30LL) )
{
i2c_read(10, 3);
if ( buf[0] == 72 && buf[1] == 52 && buf[2] == 51 )
puts("found hmc5883");
}
result = ioctl(file, 0x703uLL, 119LL);
if ( !result )
result = puts("found bmp085");
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

if ( !ioctl(file, 0x703uLL, 64LL) )
{
func2(0, 2);
if ( buf[0] == 3 && buf[1] == 20 )
puts("found bma180");
}
if ( !ioctl(file, 0x703uLL, 104LL) )
{
func2(0, 1);
if ( buf[0] == 104 )
{
puts("found itg3200");
func3(21, 7);
func3(22, 29);
func3(62, 1);
}
}
if ( !ioctl(file, 0x703uLL, 30LL) )
{
func2(10, 3);
if ( buf[0] == 72 && buf[1] == 52 && buf[2] == 51 )
puts("found hmc5883");
}
result = ioctl(file, 0x703uLL, 119LL);
if ( !result )
result = puts("found bmp085");
return result;
}


decompiled_code:
void write_config(void)
{
config(1LL);
}

canonical_decompiled_code:
void func7(void)
{
func6(1LL);
}

original_code:
static void write_config()
{
    config(WRITE);
}


decompiled_code:
int read_config()
{
return config(0LL);
}

canonical_decompiled_code:
int func8()
{
return func6(0LL);
}

original_code:
static void read_config()
{
    config(READ);
}


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

gettimeofday(&v1, 0LL);
return (double)(LODWORD(v1.tv_usec) - LODWORD(start.tv_usec)) / 1000000.0
+ (double)(LODWORD(v1.tv_sec) - LODWORD(start.tv_sec));
}

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

gettimeofday(&v1, 0LL);
return (double)(LODWORD(v1.tv_usec) - LODWORD(start.tv_usec)) / 1000000.0
+ (double)(LODWORD(v1.tv_sec) - LODWORD(start.tv_sec));
}

original_code:
float imu_runtime()
{
    struct timeval current;
    gettimeofday(&current, ((void *)0));
    return current.tv_sec - start.tv_sec + (current.tv_usec - start.tv_usec)/1e6;
}

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


decompiled_code:
int imu_init()
{
__int64 i; // rax
int result; // eax
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-48h] BYREF

read_config();
atexit(write_config);
file = open(i2c_device, 2);
if ( file < 0 )
{
fprintf(stderr, "failed to open: '%s'\n", i2c_device);
result = 1;
}
else
{
init_mag_calibration();
probe_i2c_devices();
for ( i = 0LL; i != 10; ++i )
{
cof[i] = 0.0;
dof[i] = 0.0;
dword_F60[i] = 0;
}
if ( pthread_attr_init(&attr) )
exit(1);
result = pthread_create(&newthread, &attr, sensors_loop, 0LL);
if ( result )
exit(1);
}
return result;
}

canonical_decompiled_code:
int func10()
{
__int64 i; // rax
int result; // eax
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-48h] BYREF

func8();
atexit(write_config);
file = open(i2c_device, 2);
if ( file < 0 )
{
fprintf(stderr, "failed to open: '%s'\n", i2c_device);
result = 1;
}
else
{
init_mag_calibration();
func5();
for ( i = 0LL; i != 10; ++i )
{
cof[i] = 0.0;
dof[i] = 0.0;
dword_F60[i] = 0;
}
if ( pthread_attr_init(&attr) )
exit(1);
result = pthread_create(&newthread, &attr, sensors_loop, 0LL);
if ( result )
exit(1);
}
return result;
}

original_code:
int imu_init()
{
    read_config();
    atexit(write_config);
    if((file = open(i2c_device, 02)) < 0) {
        fprintf(stderr, "failed to open: '%s'\n", i2c_device);
        return 1;
    }
    init_mag_calibration();
    probe_i2c_devices();
    int i;
    for(i=0; i<SENSOR_COUNT; i++)
        of[i] = dof[i] = cof[i] = 0;
    pthread_attr_t attr;
    if(pthread_attr_init(&attr) != 0)
        exit(1);
    pthread_t thread;
    if(pthread_create(&thread, &attr, sensors_loop, ((void *)0)) != 0)
        exit(1);
    return 0;
}

canonical_original_code:
int imu_init()
{
    read_config();
    atexit(write_config);
    if((file = open(i2c_device, 02)) < 0) {
        fprintf(stderr, "failed to open: '%s'\n", i2c_device);
        return 1;
    }
    init_mag_calibration();
    probe_i2c_devices();
    int i;
    for(i=0; i<SENSOR_COUNT; i++)
        of[i] = dof[i] = cof[i] = 0;
    union pthread_attr_t attr;
    if(pthread_attr_init(&attr) != 0)
        exit(1);
    unsigned long thread;
    if(pthread_create(&thread, &attr, sensors_loop, ((void *)0)) != 0)
        exit(1);
    return 0;
}

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

variable_types:
thread
unsigned long

return_type: int


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

for ( result = 0LL; result != 9; ++result )
*(_DWORD *)(a1 + result * 4) = dword_F60[result];
return result * 4;
}

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

for ( result = 0LL; result != 9; ++result )
*(_DWORD *)(a1 + result * 4) = dword_F60[result];
return result * 4;
}

original_code:
void imu_read(float s[9])
{
    int i;
    for(i=0; i<9; i++)
        s[i] = of[i];
}

variable_types:
s
float [9]


original_code:
void imu_yaw_offset(float angle)
{
    float q[4], g[3] = {0, 0, 1};
    angvec2quat(q, ((angle)/180*3.14159265358979323846), g);
    quatmult1(alignment, q);
}

canonical_original_code:
void imu_yaw_offset(float angle)
{
    float q[4]; float g[3] = {0, 0, 1};
    angvec2quat(q, ((angle)/180*3.14159265358979323846), g);
    quatmult1(alignment, q);
}

variable_types: angle float


decompiled_code:
__int64 __fastcall imu_quaternion(_DWORD *a1)
{
*a1 = q0;
a1[1] = q1;
a1[2] = q2;
a1[3] = q3;
return quatmult1(a1, &alignment);
}

canonical_decompiled_code:
__int64 __fastcall func13(_DWORD *a1)
{
*a1 = q0;
a1[1] = q1;
a1[2] = q2;
a1[3] = q3;
return quatmult1(a1, &alignment);
}

original_code:
void imu_quaternion(float q[4])
{
    q[0] = q0, q[1] = q1, q[2] = q2, q[3] = q3;
    quatmult1(q, alignment);
 }


decompiled_code:
__int64 __fastcall imu_orientation(__int64 a1)
{
_DWORD v2[6]; // [rsp+0h] [rbp-18h] BYREF

imu_quaternion(v2);
return quaternion_pitch_roll_yaw(v2, a1, a1 + 4, a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
_DWORD v2[6]; // [rsp+0h] [rbp-18h] BYREF

func13(v2);
return quaternion_pitch_roll_yaw(v2, a1, a1 + 4, a1 + 8);
}

original_code:
void imu_orientation(float a[3])
{
    float q[4];
    imu_quaternion(q);
    quaternion_pitch_roll_yaw(q, a+0, a+1, a+2);
}


decompiled_code:
void __fastcall __noreturn sensors_loop(void *a1, double a2, double a3)
{
struct timespec *v3; // rdi
double v4; // xmm0_8
float v5; // xmm0_4
float *v6; // rax
__int64 i; // rax
float v8; // ebp
__int64 j; // rbx
__int64 k; // rax
unsigned int v11; // ebx
float v12; // [rsp+14h] [rbp-44h]
float v13; // [rsp+18h] [rbp-40h]
float v14; // [rsp+1Ch] [rbp-3Ch]
char v15[8]; // [rsp+24h] [rbp-34h] BYREF
float v16; // [rsp+2Ch] [rbp-2Ch]
struct timespec requested_time; // [rsp+30h] [rbp-28h] BYREF

gettimeofday(&start, 0LL);
v14 = 0.0;
v12 = 0.0;
while ( 1 )
{
v13 = imu_runtime();
v5 = (float)(v12 - v13) + 0.02;
if ( v5 < 0.0 )
{
v3 = (struct timespec *)"system too slow to maintain inertial sensors filter\n";
fwrite("system too slow to maintain inertial sensors filter\n", 1uLL, 0x34uLL, stderr);
}
else
{
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = (unsigned int)(int)(v5 * 1000000000.0);
v3 = &requested_time;
nanosleep(&requested_time, 0LL);
}
v12 = imu_runtime();
*(_QWORD *)&v4 = LODWORD(v13);
*(float *)&v4 = v13 - v14;
if ( (float)(v13 - v14) >= 0.2 )
{
v14 = v14 + 0.2;
v6 = cof;
v4 = 0.0;
do
{
if ( *v6 == 0.0 )
goto LABEL_7;
++v6;
}
while ( v6 != (float *)(unsigned int)((_DWORD)cof + 40) );
for ( i = 0LL; i != 10; ++i )
{
dword_F60[i] = dof[i] / cof[i];
cof[i] = 0.0;
dof[i] = 0.0;
}
v8 = *(float *)&gyro_bias_lowpass;
for ( j = 3LL; j != 6; ++j )
*(float *)&of_bias[j] = lp_filter(*(float *)&of_bias[j], dword_F60[j], v8);
for ( k = 0LL; k != 10; ++k )
dword_F60[k] = dword_F60[k] - *(float *)&of_bias[k];
if ( initial_values_0 )
{
initial_values_0 = 0;
compute_initial_orientation();
}
*(_QWORD *)&a3 = LODWORD(dword_F60[4]);
*(_QWORD *)&v4 = LODWORD(dword_F60[3]);
MadgwickAHRSupdate(
*(float *)&v4,
*(float *)&a3,
dword_F60[5],
dword_F60[0],
dword_F60[1],
dword_F60[2],
dword_F60[6],
dword_F60[7],
v3);
if ( (unsigned int)calibrate_mag((unsigned int)((_DWORD)dword_F60 + 24), &requested_time) )
{
imu_orientation((__int64)v15);
v11 = LODWORD(v16);
*(float *)&dword_EF8 = *(float *)&requested_time.tv_sec + *(float *)&dword_EF8;
*(float *)&dword_EFC = *(float *)&dword_EFC + *((float *)&requested_time.tv_sec + 1);
*(float *)&dword_F00 = *(float *)&dword_F00 + *(float *)&requested_time.tv_nsec;
imu_orientation((__int64)v15);
*(_QWORD *)&v4 = _mm_cvtsi32_si128(v11).m128i_u64[0];
*(float *)&v4 = *(float *)&v4 - v16;
imu_yaw_offset(*(float *)&v4);
}
}
LABEL_7:
read_sensors(v4, a3);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func15(void *a1, double a2, double a3)
{
struct timespec *v3; // rdi
double v4; // xmm0_8
float v5; // xmm0_4
float *v6; // rax
__int64 i; // rax
float v8; // ebp
__int64 j; // rbx
__int64 k; // rax
unsigned int v11; // ebx
float v12; // [rsp+14h] [rbp-44h]
float v13; // [rsp+18h] [rbp-40h]
float v14; // [rsp+1Ch] [rbp-3Ch]
char v15[8]; // [rsp+24h] [rbp-34h] BYREF
float v16; // [rsp+2Ch] [rbp-2Ch]
struct timespec requested_time; // [rsp+30h] [rbp-28h] BYREF

gettimeofday(&start, 0LL);
v14 = 0.0;
v12 = 0.0;
while ( 1 )
{
v13 = func9();
v5 = (float)(v12 - v13) + 0.02;
if ( v5 < 0.0 )
{
v3 = (struct timespec *)"system too slow to maintain inertial sensors filter\n";
fwrite("system too slow to maintain inertial sensors filter\n", 1uLL, 0x34uLL, stderr);
}
else
{
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = (unsigned int)(int)(v5 * 1000000000.0);
v3 = &requested_time;
nanosleep(&requested_time, 0LL);
}
v12 = func9();
*(_QWORD *)&v4 = LODWORD(v13);
*(float *)&v4 = v13 - v14;
if ( (float)(v13 - v14) >= 0.2 )
{
v14 = v14 + 0.2;
v6 = cof;
v4 = 0.0;
do
{
if ( *v6 == 0.0 )
goto LABEL_7;
++v6;
}
while ( v6 != (float *)(unsigned int)((_DWORD)cof + 40) );
for ( i = 0LL; i != 10; ++i )
{
dword_F60[i] = dof[i] / cof[i];
cof[i] = 0.0;
dof[i] = 0.0;
}
v8 = *(float *)&gyro_bias_lowpass;
for ( j = 3LL; j != 6; ++j )
*(float *)&of_bias[j] = func0(*(float *)&of_bias[j], dword_F60[j], v8);
for ( k = 0LL; k != 10; ++k )
dword_F60[k] = dword_F60[k] - *(float *)&of_bias[k];
if ( initial_values_0 )
{
initial_values_0 = 0;
func1();
}
*(_QWORD *)&a3 = LODWORD(dword_F60[4]);
*(_QWORD *)&v4 = LODWORD(dword_F60[3]);
MadgwickAHRSupdate(
*(float *)&v4,
*(float *)&a3,
dword_F60[5],
dword_F60[0],
dword_F60[1],
dword_F60[2],
dword_F60[6],
dword_F60[7],
v3);
if ( (unsigned int)calibrate_mag((unsigned int)((_DWORD)dword_F60 + 24), &requested_time) )
{
func14((__int64)v15);
v11 = LODWORD(v16);
*(float *)&dword_EF8 = *(float *)&requested_time.tv_sec + *(float *)&dword_EF8;
*(float *)&dword_EFC = *(float *)&dword_EFC + *((float *)&requested_time.tv_sec + 1);
*(float *)&dword_F00 = *(float *)&dword_F00 + *(float *)&requested_time.tv_nsec;
func14((__int64)v15);
*(_QWORD *)&v4 = _mm_cvtsi32_si128(v11).m128i_u64[0];
*(float *)&v4 = *(float *)&v4 - v16;
func12(*(float *)&v4);
}
}
LABEL_7:
func4(v4, a3);
}
}

original_code:
static void *sensors_loop(void *v)
{
    float freq = 50;
    float l = 0, l2 = 0;
    gettimeofday(&start, ((void *)0));
    for(;;) {
        float n = imu_runtime();
        float s = l - n + 1.0/freq;
        if(s<0) {
            fprintf(stderr, "system too slow to maintain inertial sensors filter\n");
        } else {
            struct timespec ts = {0, (s)*1e9};
            nanosleep(&ts, ((void *)0));
        }
        l = imu_runtime();
        float p = 1.0f/filter_sample_count;
        if(n - l2 >= p) {
            l2 += p;
            int i;
            for(i=0; i<SENSOR_COUNT; i++)
                if(cof[i] == 0)
                    goto skip;
            for(i=0; i<SENSOR_COUNT; i++) {
                of[i] = dof[i] / cof[i];
                dof[i] = cof[i] = 0;
            }
            for(i=3; i<6; i++)
                of_bias[i] = lp_filter(of_bias[i], of[i], gyro_bias_lowpass);
            for(i=0; i<SENSOR_COUNT; i++)
                of[i] -= of_bias[i];
            static int initial_values = 1;
            if(initial_values) {
                initial_values = 0;
                compute_initial_orientation();
            }
            MadgwickAHRSupdate(of[3], of[4], of[5], of[0], of[1], of[2], of[6], of[7], of[8]);
            float magstate[4];
            if(calibrate_mag(of + 6, magstate)) {
                float a[3];
                imu_orientation(a);
                float yaw = a[2];
                for(i=0; i<3; i++)
                    of_bias[MAGX+i] += magstate[i];
                imu_orientation(a);
                imu_yaw_offset(yaw - a[2]);
            }
        }
    skip:
        read_sensors();
    }
}

canonical_original_code:
static void *sensors_loop(void *v)
{
    float freq = 50;
    float l = 0; float l2 = 0;
    gettimeofday(&start, ((void *)0));
    for(;;) {
        float n = imu_runtime();
        float s = l - n + 1.0/freq;
        if(s<0) {
            fprintf(stderr, "system too slow to maintain inertial sensors filter\n");
        } else {
            struct timespec ts = {0, (s)*1e9};
            nanosleep(&ts, ((void *)0));
        }
        l = imu_runtime();
        float p = 1.0f/filter_sample_count;
        if(n - l2 >= p) {
            l2 += p;
            int i;
            for(i=0; i<SENSOR_COUNT; i++)
                if(cof[i] == 0)
                    goto skip;
            for(i=0; i<SENSOR_COUNT; i++) {
                of[i] = dof[i] / cof[i];
                dof[i] = cof[i] = 0;
            }
            for(i=3; i<6; i++)
                of_bias[i] = lp_filter(of_bias[i], of[i], gyro_bias_lowpass);
            for(i=0; i<SENSOR_COUNT; i++)
                of[i] -= of_bias[i];
            static int initial_values = 1;
            if(initial_values) {
                initial_values = 0;
                compute_initial_orientation();
            }
            MadgwickAHRSupdate(of[3], of[4], of[5], of[0], of[1], of[2], of[6], of[7], of[8]);
            float magstate[4];
            if(calibrate_mag(of + 6, magstate)) {
                float a[3];
                imu_orientation(a);
                float yaw = a[2];
                for(i=0; i<3; i++)
                    of_bias[MAGX+i] += magstate[i];
                imu_orientation(a);
                imu_yaw_offset(yaw - a[2]);
            }
        }
    skip:
        read_sensors();
    }
}

variable_types:
v
void *

variable_types: freq float

variable_types: l float

variable_types: n float

variable_types: s float

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

variable_types: p float

variable_types: i int

variable_types:
magstate
float [4]

variable_types: yaw float

return_type: void *


decompiled_code:
__int64 imu_level()
{
float v0; // ebx
__int64 v2; // [rsp+0h] [rbp-28h] BYREF
float v3; // [rsp+8h] [rbp-20h]

imu_orientation((__int64)&v2);
v0 = v3;
alignment = q0;
*(&alignment + 1) = q1 ^ LC2;
*(&alignment + 2) = q2 ^ LC2;
*(&alignment + 3) = q3 ^ LC2;
imu_orientation((__int64)&v2);
return imu_yaw_offset(v0 - v3);
}

canonical_decompiled_code:
__int64 func16()
{
float v0; // ebx
__int64 v2; // [rsp+0h] [rbp-28h] BYREF
float v3; // [rsp+8h] [rbp-20h]

func14((__int64)&v2);
v0 = v3;
alignment = q0;
*(&alignment + 1) = q1 ^ LC2;
*(&alignment + 2) = q2 ^ LC2;
*(&alignment + 3) = q3 ^ LC2;
func14((__int64)&v2);
return func12(v0 - v3);
}

original_code:
void imu_level()
{
    float a[6];
    imu_orientation(a);
    float yaw = a[2];
    alignment[0] = q0;
    alignment[1] = -q1;
    alignment[2] = -q2;
    alignment[3] = -q3;
    imu_orientation(a);
    imu_yaw_offset(yaw - a[2]);
}

variable_types:
a
float [6]


decompiled_code:
__int64 __fastcall imu_rate(__int64 a1)
{
float v2; // [rsp+4h] [rbp-34h] BYREF
float v3; // [rsp+8h] [rbp-30h]
float v4; // [rsp+Ch] [rbp-2Ch]
int v5[4]; // [rsp+10h] [rbp-28h] BYREF
float v6; // [rsp+20h] [rbp-18h] BYREF
float v7; // [rsp+24h] [rbp-14h]
float v8; // [rsp+28h] [rbp-10h]
float v9; // [rsp+2Ch] [rbp-Ch]

imu_quaternion(&v6);
v2 = dword_F60[3];
v3 = dword_F60[4];
v4 = dword_F60[5];
rotvecquat(&v2, &alignment);
*(float *)v5 = (float)((float)((float)((float)(COERCE_FLOAT(LODWORD(v7) ^ LC2) * v2) - (float)(v8 * v3))
- (float)(v9 * v4))
* 0.5)
+ 1.0;
*(float *)&v5[1] = (float)((float)((float)(v2 * v6) + (float)(v8 * v4)) - (float)(v3 * v9)) * 0.5;
*(float *)&v5[2] = (float)((float)((float)(v3 * v6) - (float)(v7 * v4)) + (float)(v9 * v2)) * 0.5;
*(float *)&v5[3] = (float)((float)((float)(v4 * v6) + (float)(v7 * v3)) - (float)(v2 * v8)) * 0.5;
quatnormalize(v5);
return quaternion_pitch_roll_yaw(v5, a1, a1 + 4, a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
float v2; // [rsp+4h] [rbp-34h] BYREF
float v3; // [rsp+8h] [rbp-30h]
float v4; // [rsp+Ch] [rbp-2Ch]
int v5[4]; // [rsp+10h] [rbp-28h] BYREF
float v6; // [rsp+20h] [rbp-18h] BYREF
float v7; // [rsp+24h] [rbp-14h]
float v8; // [rsp+28h] [rbp-10h]
float v9; // [rsp+2Ch] [rbp-Ch]

func13(&v6);
v2 = dword_F60[3];
v3 = dword_F60[4];
v4 = dword_F60[5];
rotvecquat(&v2, &alignment);
*(float *)v5 = (float)((float)((float)((float)(COERCE_FLOAT(LODWORD(v7) ^ LC2) * v2) - (float)(v8 * v3))
- (float)(v9 * v4))
* 0.5)
+ 1.0;
*(float *)&v5[1] = (float)((float)((float)(v2 * v6) + (float)(v8 * v4)) - (float)(v3 * v9)) * 0.5;
*(float *)&v5[2] = (float)((float)((float)(v3 * v6) - (float)(v7 * v4)) + (float)(v9 * v2)) * 0.5;
*(float *)&v5[3] = (float)((float)((float)(v4 * v6) + (float)(v7 * v3)) - (float)(v2 * v8)) * 0.5;
quatnormalize(v5);
return quaternion_pitch_roll_yaw(v5, a1, a1 + 4, a1 + 8);
}

variable_types:
a
float [3]

variable_types:
q
float [4]

variable_types:
r
float [4]

variable_types:
g
float [3]


cleanup

decompiled_code:
void cleanup(void)
{
servo_close(servo);
werase(stdscr);
if ( !isendwin() )
endwin();
}

canonical_decompiled_code:
void func1(void)
{
servo_close(servo);
werase(stdscr);
if ( !isendwin() )
endwin();
}

original_code:
void cleanup()
{
    servo_close(servo);
    werase(stdscr);
    if(isendwin() == 0)
        endwin();
}


decompiled_code:
__int64 __fastcall analyze_state(__int64 *a1)
{
int v1; // eax
int v2; // edx
__int64 result; // rax
__int64 v4; // r12
__int64 i; // rbx
unsigned __int64 v6; // rcx
int v7; // esi
__int64 v8; // rax
_DWORD *v9; // rdx
char *v10; // rcx
_DWORD *v11; // rax
char *v12; // rdx
double v13; // xmm0_8
int v14; // er13
float v15; // [rsp+0h] [rbp-9A8h] BYREF
float v16; // [rsp+4h] [rbp-9A4h]
int v17; // [rsp+8h] [rbp-9A0h]
int v18; // [rsp+Ch] [rbp-99Ch]
char v19[2456]; // [rsp+10h] [rbp-998h] BYREF

v1 = sensorlogpos++;
v2 = sensorlogpos;
result = 3LL * v1;
sensorlog[result] = *a1;
if ( v2 == 100 )
{
sensorlogpos = 0;
sensorlogready = 1;
}
else if ( !sensorlogready )
{
return result;
}
v4 = 0LL;
for ( i = 0LL; i != 6; ++i )
{
v7 = sensorlogpos;
if ( (unsigned int)sensorlogpos > 0x63 )
{
v6 = 0LL;
}
else
{
v8 = sensorlogpos;
v9 = (_DWORD *)&sensorlog[3 * sensorlogpos] + i;
v10 = &v19[v4 - 4LL * sensorlogpos];
do
{
*(_DWORD *)&v10[4 * v8++] = *v9;
v9 += 6;
}
while ( (unsigned int)v8 <= 0x63 );
v6 = (unsigned int)(100 - v7);
}
if ( v7 )
{
v11 = (_DWORD *)sensorlog + i;
v12 = &v19[400 * i + 4 * (int)v6];
v6 = (unsigned __int64)&sensorlog[3 * (unsigned int)(v7 - 1) + 3] + 4 * i;
do
{
*(_DWORD *)v12 = *v11;
v11 += 6;
v12 += 4;
}
while ( v11 != (_DWORD *)v6 );
}
v13 = CalcBestLine(&v19[v4], 100LL, &v15, v6);
printf("line %d %f %f, %f\n", (unsigned int)i, v15, v16, *(float *)&v13);
v15 = 1.0;
v16 = 10.0;
v17 = 0;
v18 = 0;
v14 = 5;
do
{
result = CalcBestSine(&v19[v4], 100LL, &v15);
--v14;
}
while ( v14 );
v4 += 400LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1)
{
int v1; // eax
int v2; // edx
__int64 result; // rax
__int64 v4; // r12
__int64 i; // rbx
unsigned __int64 v6; // rcx
int v7; // esi
__int64 v8; // rax
_DWORD *v9; // rdx
char *v10; // rcx
_DWORD *v11; // rax
char *v12; // rdx
double v13; // xmm0_8
int v14; // er13
float v15; // [rsp+0h] [rbp-9A8h] BYREF
float v16; // [rsp+4h] [rbp-9A4h]
int v17; // [rsp+8h] [rbp-9A0h]
int v18; // [rsp+Ch] [rbp-99Ch]
char v19[2456]; // [rsp+10h] [rbp-998h] BYREF

v1 = sensorlogpos++;
v2 = sensorlogpos;
result = 3LL * v1;
sensorlog[result] = *a1;
if ( v2 == 100 )
{
sensorlogpos = 0;
sensorlogready = 1;
}
else if ( !sensorlogready )
{
return result;
}
v4 = 0LL;
for ( i = 0LL; i != 6; ++i )
{
v7 = sensorlogpos;
if ( (unsigned int)sensorlogpos > 0x63 )
{
v6 = 0LL;
}
else
{
v8 = sensorlogpos;
v9 = (_DWORD *)&sensorlog[3 * sensorlogpos] + i;
v10 = &v19[v4 - 4LL * sensorlogpos];
do
{
*(_DWORD *)&v10[4 * v8++] = *v9;
v9 += 6;
}
while ( (unsigned int)v8 <= 0x63 );
v6 = (unsigned int)(100 - v7);
}
if ( v7 )
{
v11 = (_DWORD *)sensorlog + i;
v12 = &v19[400 * i + 4 * (int)v6];
v6 = (unsigned __int64)&sensorlog[3 * (unsigned int)(v7 - 1) + 3] + 4 * i;
do
{
*(_DWORD *)v12 = *v11;
v11 += 6;
v12 += 4;
}
while ( v11 != (_DWORD *)v6 );
}
v13 = CalcBestLine(&v19[v4], 100LL, &v15, v6);
printf("line %d %f %f, %f\n", (unsigned int)i, v15, v16, *(float *)&v13);
v15 = 1.0;
v16 = 10.0;
v17 = 0;
v18 = 0;
v14 = 5;
do
{
result = CalcBestSine(&v19[v4], 100LL, &v15);
--v14;
}
while ( v14 );
v4 += 400LL;
}
return result;
}

original_code:
void analyze_state(float state[6])
{
    memcpy(sensorlog[sensorlogpos++], state, sizeof state);
    if(sensorlogpos == (sizeof sensorlog) / (sizeof *sensorlog)) {
        sensorlogpos = 0;
        sensorlogready = 1;
    }
    if(sensorlogready) {
        float curlog[6][(sizeof sensorlog) / (sizeof *sensorlog)];
        int s, i, j;
        for(s=0; s<6; s++) {
            for(j = 0, i = sensorlogpos; i<(sizeof sensorlog) / (sizeof *sensorlog); i++, j++)
                curlog[s][j] = sensorlog[i][s];
            for(i = 0; i != sensorlogpos; i++, j++)
                curlog[s][j] = sensorlog[i][s];
            float X[4], r;
            r = CalcBestLine(curlog[s], (sizeof sensorlog) / (sizeof *sensorlog), X);
            printf("line %d %f %f, %f\n", s, X[0], X[1], r);
            X[0] = 1, X[1] = 10, X[2] = 0, X[3] = 0;
            int iters;
            for(iters = 0; iters < 5; iters++) {
                r = CalcBestSine(curlog[s], (sizeof sensorlog) / (sizeof *sensorlog), X);
            }
            if(r) {
            }
        }
    }
}

canonical_original_code:
void analyze_state(float state[6])
{
    memcpy(sensorlog[sensorlogpos++], state, sizeof state);
    if(sensorlogpos == (sizeof sensorlog) / (sizeof *sensorlog)) {
        sensorlogpos = 0;
        sensorlogready = 1;
    }
    if(sensorlogready) {
        float curlog[6][-1];
        int s; int i; int j;
        for(s=0; s<6; s++) {
            for(j = 0, i = sensorlogpos; i<(sizeof sensorlog) / (sizeof *sensorlog); i++, j++)
                curlog[s][j] = sensorlog[i][s];
            for(i = 0; i != sensorlogpos; i++, j++)
                curlog[s][j] = sensorlog[i][s];
            float X[4]; float r;
            r = CalcBestLine(curlog[s], (sizeof sensorlog) / (sizeof *sensorlog), X);
            printf("line %d %f %f, %f\n", s, X[0], X[1], r);
            X[0] = 1, X[1] = 10, X[2] = 0, X[3] = 0;
            int iters;
            for(iters = 0; iters < 5; iters++) {
                r = CalcBestSine(curlog[s], (sizeof sensorlog) / (sizeof *sensorlog), X);
            }
            if(r) {
            }
        }
    }
}

variable_types:
state
float [6]

variable_types:
curlog
float [6][-1]

variable_types: s int

variable_types: j int

variable_types:
X
float [4]

variable_types: r float

variable_types: iters int


decompiled_code:
int read_input()
{
int result; // eax

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
result = wgetch(stdscr);
if ( result != 61 )
break;
LABEL_4:
*(float *)&desired_heading = *(float *)&desired_heading + 1.0;
}
if ( result <= 61 )
break;
if ( result == 97 )
{
engauged = engauged == 0;
}
else if ( result <= 97 )
{
if ( result == 95 )
goto LABEL_10;
}
else if ( result == 108 )
{
imu_level();
}
else if ( result == 113 )
{
exit(1);
}
}
if ( result != 45 )
break;
LABEL_10:
*(float *)&desired_heading = *(float *)&desired_heading - 1.0;
}
if ( result <= 45 )
break;
if ( result == 48 )
*(float *)&desired_heading = *(float *)&desired_heading + *(float *)&dword_FC8;
}
if ( result == -1 )
return result;
if ( result == 43 )
goto LABEL_4;
}
}

canonical_decompiled_code:
int func4()
{
int result; // eax

while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
result = wgetch(stdscr);
if ( result != 61 )
break;
LABEL_4:
*(float *)&desired_heading = *(float *)&desired_heading + 1.0;
}
if ( result <= 61 )
break;
if ( result == 97 )
{
engauged = engauged == 0;
}
else if ( result <= 97 )
{
if ( result == 95 )
goto LABEL_10;
}
else if ( result == 108 )
{
imu_level();
}
else if ( result == 113 )
{
exit(1);
}
}
if ( result != 45 )
break;
LABEL_10:
*(float *)&desired_heading = *(float *)&desired_heading - 1.0;
}
if ( result <= 45 )
break;
if ( result == 48 )
*(float *)&desired_heading = *(float *)&desired_heading + *(float *)&dword_FC8;
}
if ( result == -1 )
return result;
if ( result == 43 )
goto LABEL_4;
}
}

original_code:
void read_input()
{
    for(;;)
        switch(wgetch(stdscr)) {
        case -1: return;
        case 'q': exit(1);
        case '+': case '=': desired_heading++; break;
        case '_': case '-': desired_heading--; break;
        case '0': desired_heading += states[YAW]; break;
        case 'a': engauged = !engauged; break;
        case 'l': imu_level(); break;
        }
}


decompiled_code:
int setup_ncurses()
{
int result; // eax

initscr();
raw();
cbreak();
nodelay(stdscr, 0);
noecho();
result = fcntl(0, 4, 2048LL);
if ( result == -1 )
{
puts("failed to set stdin non blocking");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

initscr();
raw();
cbreak();
nodelay(stdscr, 0);
noecho();
result = fcntl(0, 4, 2048LL);
if ( result == -1 )
{
puts("failed to set stdin non blocking");
exit(1);
}
return result;
}

original_code:
void setup_ncurses()
{
    initscr();
    raw();
    cbreak();
    nodelay(stdscr, 0);
    noecho();
    if(fcntl(0, 4, 04000) == -1) {
        printf("failed to set stdin non blocking\n");
        exit(1);
    }
}


decompiled_code:
int __fastcall print_valuew(float a1)
{
return printw("%c%05.2f ", 2 * (unsigned int)(a1 < 0.0) + 43, COERCE_FLOAT(LODWORD(a1) & LC5));
}

canonical_decompiled_code:
int __fastcall func6(float a1)
{
return printw("%c%05.2f ", 2 * (unsigned int)(a1 < 0.0) + 43, COERCE_FLOAT(LODWORD(a1) & LC5));
}

original_code:
void print_valuew(float v)
{
    printw("%c%05.2f ", v < 0 ? '-' : '+', fabs(v));
}

variable_types: v float


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
float v3; // ebp
__int64 i; // rbx
float v5; // xmm0_4
float v6; // xmm1_4
const char *v7; // rcx
struct timespec v8; // [rsp+0h] [rbp-48h] BYREF
int v9[2]; // [rsp+10h] [rbp-38h] BYREF
float v10; // [rsp+18h] [rbp-30h]
char v11[44]; // [rsp+1Ch] [rbp-2Ch] BYREF

imu_init(argc, argv, envp);
servo = servo_open("/dev/ttyUSB0");
atexit(cleanup);
servo_set_mode(servo, 0LL, "POSITION");
setup_ncurses();
printw(
"Autopilot ncurses Control Interface\n"
"Keys include:\n"
"\t+/- - set desired heading\n"
"\t0 - desired heading to current heading\n"
"\ta - engauged/disengauge autopilot\n"
"\tl - level imu\n");
while ( 1 )
{
imu_orientation(v9);
imu_rate(v11);
for ( i = 0LL; i != 6; ++i )
{
if ( (_DWORD)i == 2 )
{
v3 = v10;
dword_FC8 = heading_resolve(v10 - *(float *)&desired_heading);
}
else
{
states[i] = v9[i];
}
v5 = *(float *)&v9[i];
v6 = (float)(v5 / 10.0) + dword_FD8[i];
dword_FD8[i] = v6;
if ( v6 <= 1.0 )
{
if ( v6 < -1.0 )
dword_FD8[i] = -1.0;
}
else
{
dword_FD8[i] = 1.0;
}
dword_FF0[i] = (float)(v5 - *(float *)&states[i]) * 10.0;
}
heel = lp_filter(*(float *)&heel, *(float *)&dword_FC4, 0.99000001);
command = pid_command();
if ( (int)imu_runtime() > 5 && engauged )
servo_command(servo, 0LL, *(float *)&command);
read_input();
mvprintw(6, 0, "heading: ");
print_valuew(v3);
mvprintw(6, 40, "rate: ");
print_valuew(*(float *)&dword_FD4);
mvprintw(7, 0, "desired heading: ");
print_valuew(*(float *)&desired_heading);
mvprintw(7, 40, "heading error: ");
print_valuew(*(float *)&dword_FC8);
v7 = (const char *)servo_read_status(servo);
if ( v7 )
mvprintw(8, 0, "Status: %s ", v7);
v8.tv_sec = 0LL;
v8.tv_nsec = 100000000LL;
nanosleep(&v8, 0LL);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
float v3; // ebp
__int64 i; // rbx
float v5; // xmm0_4
float v6; // xmm1_4
const char *v7; // rcx
struct timespec v8; // [rsp+0h] [rbp-48h] BYREF
int v9[2]; // [rsp+10h] [rbp-38h] BYREF
float v10; // [rsp+18h] [rbp-30h]
char v11[44]; // [rsp+1Ch] [rbp-2Ch] BYREF

imu_init(argc, argv, envp);
servo = servo_open("/dev/ttyUSB0");
atexit(cleanup);
servo_set_mode(servo, 0LL, "POSITION");
func5();
printw(
"Autopilot ncurses Control Interface\n"
"Keys include:\n"
"\t+/- - set desired heading\n"
"\t0 - desired heading to current heading\n"
"\ta - engauged/disengauge autopilot\n"
"\tl - level imu\n");
while ( 1 )
{
imu_orientation(v9);
imu_rate(v11);
for ( i = 0LL; i != 6; ++i )
{
if ( (_DWORD)i == 2 )
{
v3 = v10;
dword_FC8 = heading_resolve(v10 - *(float *)&desired_heading);
}
else
{
states[i] = v9[i];
}
v5 = *(float *)&v9[i];
v6 = (float)(v5 / 10.0) + dword_FD8[i];
dword_FD8[i] = v6;
if ( v6 <= 1.0 )
{
if ( v6 < -1.0 )
dword_FD8[i] = -1.0;
}
else
{
dword_FD8[i] = 1.0;
}
dword_FF0[i] = (float)(v5 - *(float *)&states[i]) * 10.0;
}
heel = func0(*(float *)&heel, *(float *)&dword_FC4, 0.99000001);
command = pid_command();
if ( (int)imu_runtime() > 5 && engauged )
servo_command(servo, 0LL, *(float *)&command);
func4();
mvprintw(6, 0, "heading: ");
func6(v3);
mvprintw(6, 40, "rate: ");
func6(*(float *)&dword_FD4);
mvprintw(7, 0, "desired heading: ");
func6(*(float *)&desired_heading);
mvprintw(7, 40, "heading error: ");
func6(*(float *)&dword_FC8);
v7 = (const char *)servo_read_status(servo);
if ( v7 )
mvprintw(8, 0, "Status: %s ", v7);
v8.tv_sec = 0LL;
v8.tv_nsec = 100000000LL;
nanosleep(&v8, 0LL);
}
}

original_code:
int main()
{
    imu_init();
    servo = servo_open("/dev/ttyUSB0");
    atexit(cleanup);
    servo_set_mode(servo, 0, "POSITION");
    setup_ncurses();
    printw("Autopilot ncurses Control Interface\n"
           "Keys include:\n"
           "\t+/- - set desired heading\n"
           "\t0 - desired heading to current heading\n"
           "\ta - engauged/disengauge autopilot\n"
           "\tl - level imu\n");
    for(;;) {
        float X[6];
        imu_orientation(X);
        imu_rate(X+3);
        const float hz = 10;
        int i;
        float heading;
        for(i=0; i<6; i++) {
            if(i == YAW) {
                heading = X[YAW];
                states[i] = heading_resolve(heading - desired_heading);
            } else
                states[i] = X[i];
            states[i+INTEGRAL] += X[i] / hz;
            if(states[i+INTEGRAL] > 1) states[i+INTEGRAL] = 1;
            if(states[i+INTEGRAL] < -1) states[i+INTEGRAL] = -1;
            states[i+DERIVATIVE] = (X[i] - states[i])*hz;
        }
        enum {PORT, STARBOARD, };
        heel = lp_filter(heel, states[ROLL], .99);
        command = pid_command();
        int n = imu_runtime();
        if(n > 5 && engauged)
            servo_command(servo, 0, command);
        read_input();
        mvprintw(6, 0, "heading:        ");
        print_valuew(heading);
        mvprintw(6, 40, "rate:         ");
        print_valuew(states[YAW_RATE]);
        mvprintw(7, 0, "desired heading:          ");
        print_valuew(desired_heading);
        mvprintw(7, 40, "heading error:        ");
        print_valuew(states[YAW]);
        const char *status = servo_read_status(servo);
        if(status)
            mvprintw(8, 0, "Status: %s   ", status);
        struct timespec ts = {0, 1e8};
        nanosleep(&ts, ((void *)0));
    }
}

canonical_original_code:
int main()
{
    imu_init();
    servo = servo_open("/dev/ttyUSB0");
    atexit(cleanup);
    servo_set_mode(servo, 0, "POSITION");
    setup_ncurses();
    printw("Autopilot ncurses Control Interface\n"
           "Keys include:\n"
           "\t+/- - set desired heading\n"
           "\t0 - desired heading to current heading\n"
           "\ta - engauged/disengauge autopilot\n"
           "\tl - level imu\n");
    for(;;) {
        float X[6];
        imu_orientation(X);
        imu_rate(X+3);
        float hz = 10;
        int i;
        float heading;
        for(i=0; i<6; i++) {
            if(i == YAW) {
                heading = X[YAW];
                states[i] = heading_resolve(heading - desired_heading);
            } else
                states[i] = X[i];
            states[i+INTEGRAL] += X[i] / hz;
            if(states[i+INTEGRAL] > 1) states[i+INTEGRAL] = 1;
            if(states[i+INTEGRAL] < -1) states[i+INTEGRAL] = -1;
            states[i+DERIVATIVE] = (X[i] - states[i])*hz;
        }
        enum {PORT, STARBOARD, };
        heel = lp_filter(heel, states[ROLL], .99);
        command = pid_command();
        int n = imu_runtime();
        if(n > 5 && engauged)
            servo_command(servo, 0, command);
        read_input();
        mvprintw(6, 0, "heading:        ");
        print_valuew(heading);
        mvprintw(6, 40, "rate:         ");
        print_valuew(states[YAW_RATE]);
        mvprintw(7, 0, "desired heading:          ");
        print_valuew(desired_heading);
        mvprintw(7, 40, "heading error:        ");
        print_valuew(states[YAW]);
        char *status = servo_read_status(servo);
        if(status)
            mvprintw(8, 0, "Status: %s   ", status);
        struct timespec ts = {0, 1e8};
        nanosleep(&ts, ((void *)0));
    }
}

variable_types:
X
float [6]

variable_types: hz float

variable_types: heading float

variable_types: n int

variable_types:
status
char *

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


original_code:
void angvec2quat(float q[4], float angle, float v[3])
{
   float n = magnitude(v);
   float fac;
   if(n == 0)
      fac = 0;
   else
      fac = sin(angle/2) / n;
   q[0] = cos(angle/2);
   q[1] = v[0]*fac;
   q[2] = v[1]*fac;
   q[3] = v[2]*fac;
}

variable_types:
q
float [4]

variable_types: angle float

variable_types:
v
float [3]

variable_types: n float

variable_types: fac float


rotvecquat

variable_types:
w
float [4]

variable_types:
r
float [4]


decompiled_code:
void __fastcall vec2vec2quat(float *a1, __int64 a2, __int64 a3)
{
double v4; // r14
double v5; // xmm1_8
float v6; // xmm0_4
float v7; // xmm0_4
float v8; // [rsp+Ch] [rbp-3Ch]
float v9[13]; // [rsp+14h] [rbp-34h] BYREF

cross(v9);
v4 = dot(a2, a3);
v8 = magnitude2(a2);
v5 = (float)(magnitude2(a3) * v8);
if ( v5 < 0.0 )
sqrt(v5);
else
v5 = sqrt(v5);
v6 = v4 / v5;
if ( v6 <= 1.0 )
{
if ( v6 < -1.0 )
v6 = *(float *)&LC3;
}
else
{
v6 = *(float *)&LC3;
}
v7 = acos(v6);
angvec2quat(a1, v9, v7);
}

canonical_decompiled_code:
void __fastcall func4(float *a1, __int64 a2, __int64 a3)
{
double v4; // r14
double v5; // xmm1_8
float v6; // xmm0_4
float v7; // xmm0_4
float v8; // [rsp+Ch] [rbp-3Ch]
float v9[13]; // [rsp+14h] [rbp-34h] BYREF

cross(v9);
v4 = dot(a2, a3);
v8 = magnitude2(a2);
v5 = (float)(magnitude2(a3) * v8);
if ( v5 < 0.0 )
sqrt(v5);
else
v5 = sqrt(v5);
v6 = v4 / v5;
if ( v6 <= 1.0 )
{
if ( v6 < -1.0 )
v6 = *(float *)&LC3;
}
else
{
v6 = *(float *)&LC3;
}
v7 = acos(v6);
func2(a1, v9, v7);
}

original_code:
void vec2vec2quat(float x[4], float a[3], float b[3])
{
   float n[3];
   cross(n, a, b);
   float fac = dot(a, b) / sqrt(magnitude2(a) * magnitude2(b));
   if(fac > 1)
      fac = 1;
   if(fac < -1)
       fac = 1;
   float ang = acos(fac);
   angvec2quat(x, ang, n);
}

variable_types:
x
float [4]

variable_types:
a
float [3]

variable_types:
b
float [3]

variable_types:
n
float [3]

variable_types: ang float


quatmagnitude

decompiled_code:
float __fastcall quatmagnitude(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = (float)(a1[3] * a1[3]) + (float)((float)((float)(a1[1] * a1[1]) + (float)(*a1 * *a1)) + (float)(a1[2] * a1[2]));
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

canonical_decompiled_code:
float __fastcall func5(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = (float)(a1[3] * a1[3]) + (float)((float)((float)(a1[1] * a1[1]) + (float)(*a1 * *a1)) + (float)(a1[2] * a1[2]));
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

original_code:
float quatmagnitude(float q[4])
{
    return sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
}


quatnormalize

decompiled_code:
void __fastcall quatnormalize(float *a1)
{
float v1; // xmm0_4

v1 = quatmagnitude(a1);
*a1 = *a1 / v1;
a1[1] = a1[1] / v1;
a1[2] = a1[2] / v1;
a1[3] = a1[3] / v1;
}

original_code:
void quatnormalize(float q[4])
{
    float m = quatmagnitude(q);
    q[0] /= m;
    q[1] /= m;
    q[2] /= m;
    q[3] /= m;
}

variable_types: m float


quatangle

decompiled_code:
float __fastcall quatangle(float *a1)
{
double v1; // xmm0_8

v1 = acos(*a1);
return v1 + v1;
}

canonical_decompiled_code:
float __fastcall func7(float *a1)
{
double v1; // xmm0_8

v1 = acos(*a1);
return v1 + v1;
}

original_code:
float quatangle(float q[4])
{
  return 2*acos(q[0]);
}


subquatangle

decompiled_code:
float __fastcall subquatangle(__int64 a1)
{
float v1; // xmm0_4
double v2; // xmm0_8

v1 = magnitude(a1);
v2 = asin(v1);
return v2 + v2;
}

canonical_decompiled_code:
float __fastcall func8(__int64 a1)
{
float v1; // xmm0_4
double v2; // xmm0_8

v1 = magnitude(a1);
v2 = asin(v1);
return v2 + v2;
}

original_code:
float subquatangle(float v[3])
{
  return 2*asin(magnitude(v));
}


variable_types:
pitch
float *

variable_types:
roll
float *

variable_types:
yaw
float *


original_code: void vector_add(float x[], const float a[], const float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] + b[i]; }

canonical_original_code: void vector_add(float x[], float a[], float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] + b[i]; }

variable_types:
x
float []

variable_types:
a
float []

variable_types:
b
float []

variable_types: n int

variable_types: i int


original_code: void vector_sub(float x[], const float a[], const float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] - b[i]; }

canonical_original_code: void vector_sub(float x[], float a[], float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] - b[i]; }


original_code: void vector_mul(float x[], const float a[], const float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] * b[i]; }

canonical_original_code: void vector_mul(float x[], float a[], float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] * b[i]; }


original_code: void vector_div(float x[], const float a[], const float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] / b[i]; }

canonical_original_code: void vector_div(float x[], float a[], float b[], int n) { int i; for(i = 0; i<n; i++) x[i] = a[i] / b[i]; }


decompiled_code:
float __fastcall vector_length(float *a1, int a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = COERCE_FLOAT(vector_length2(a1, a2));
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

canonical_decompiled_code:
float __fastcall func4(float *a1, int a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = COERCE_FLOAT(vector_length2(a1, a2));
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

original_code:
float vector_length(const float vec[], int n)
{
   return sqrt(vector_length2(vec, n));
}

canonical_original_code:
float vector_length(float vec[], int n)
{
   return sqrt(vector_length2(vec, n));
}

variable_types:
vec
float []


magnitude

decompiled_code:
float __fastcall magnitude(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = COERCE_FLOAT(magnitude2(a1));
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

canonical_decompiled_code:
float __fastcall func6(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = COERCE_FLOAT(magnitude2(a1));
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

original_code:
float magnitude(const float vec[3])
{
   return sqrt(magnitude2(vec));
}

canonical_original_code:
float magnitude(float vec[3])
{
   return sqrt(magnitude2(vec));
}

variable_types:
vec
float [3]


normalize

decompiled_code:
void __fastcall normalize(float *a1)
{
float v1; // xmm0_4

v1 = magnitude(a1);
*a1 = *a1 / v1;
a1[1] = a1[1] / v1;
a1[2] = a1[2] / v1;
}

original_code:
void normalize(float vec[3])
{
   float mag = magnitude(vec);
   vec[0] /= mag, vec[1] /= mag, vec[2] /= mag;
}

variable_types: mag float


dot

decompiled_code:
float __fastcall dot(float *a1, float *a2)
{
return (float)((float)(*a1 * *a2) + (float)(a1[1] * a2[1])) + (float)(a1[2] * a2[2]);
}

canonical_decompiled_code:
float __fastcall func9(float *a1, float *a2)
{
return (float)((float)(*a1 * *a2) + (float)(a1[1] * a2[1])) + (float)(a1[2] * a2[2]);
}

original_code:
float dot(const float a[3], const float b[3])
{
   return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}


original_code:
float dist2(const float a[3], const float b[3])
{
   float x = a[0] - b[0];
   float y = a[1] - b[1];
   float z = a[2] - b[2];
   return x*x + y*y + z*z;
}

canonical_original_code:
float dist2(float a[3], float b[3])
{
   float x = a[0] - b[0];
   float y = a[1] - b[1];
   float z = a[2] - b[2];
   return x*x + y*y + z*z;
}

variable_types: x float

variable_types: y float

variable_types: z float


dist

decompiled_code:
float __fastcall dist(float *a1, float *a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = dist2(a1, a2);
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

canonical_decompiled_code:
float __fastcall func11(float *a1, float *a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = func10(a1, a2);
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

original_code:
float dist(const float a[3], const float b[3])
{
   return sqrt(dist2(a, b));
}

canonical_original_code:
float dist(float a[3], float b[3])
{
   return sqrt(dist2(a, b));
}

variable_types:
a
float [3]

variable_types:
b
float [3]


decompiled_code:
void __fastcall vector_scale(float *a1, int a2, float a3)
{
float *v3; // rax

if ( a2 > 0 )
{
v3 = a1;
do
{
*v3 = a3 * *v3;
++v3;
}
while ( v3 != &a1[a2 - 1 + 1] );
}
}

canonical_decompiled_code:
void __fastcall func12(float *a1, int a2, float a3)
{
float *v3; // rax

if ( a2 > 0 )
{
v3 = a1;
do
{
*v3 = a3 * *v3;
++v3;
}
while ( v3 != &a1[a2 - 1 + 1] );
}
}

original_code:
void vector_scale(float mag, float x[], int n)
{
   int i;
   for(i = 0; i<n; i++)
      x[i] *= mag;
}


decompiled_code:
void __fastcall vector_from_polar(float *a1, float a2, float a3, float a4)
{
float v4; // xmm0_4
double v5; // rbp
float v6; // xmm0_4
float v7; // xmm0_4
float v8; // xmm0_4
float v9; // xmm0_4
double v10; // [rsp+8h] [rbp-40h]
double v11; // [rsp+10h] [rbp-38h]
double cosx; // [rsp+20h] [rbp-28h] BYREF
double sinx[4]; // [rsp+28h] [rbp-20h] BYREF

v10 = a2;
v4 = (float)(a3 / 180.0) * 3.141592653589793;
sincos(v4, sinx, &cosx);
v5 = sinx[0];
v11 = v10 * cosx;
v6 = (float)(a4 / 180.0) * 3.141592653589793;
sincos(v6, sinx, &cosx);
v7 = v11 * cosx;
*a1 = v7;
v8 = v11 * sinx[0];
a1[1] = v8;
v9 = v5 * v10;
a1[2] = v9;
}

original_code:
void vector_from_polar(float x[3], float radius, float incline, float azimuth)
{
   float ri = ((incline)/180*3.14159265358979323846), ra = ((azimuth)/180*3.14159265358979323846);
   x[0] = radius*cos(ri)*cos(ra);
   x[1] = radius*cos(ri)*sin(ra);
   x[2] = radius*sin(ri);
}

canonical_original_code:
void vector_from_polar(float x[3], float radius, float incline, float azimuth)
{
   float ri = ((incline)/180*3.14159265358979323846); float ra = ((azimuth)/180*3.14159265358979323846);
   x[0] = radius*cos(ri)*cos(ra);
   x[1] = radius*cos(ri)*sin(ra);
   x[2] = radius*sin(ri);
}

variable_types:
x
float [3]

variable_types: radius float

variable_types: incline float

variable_types: azimuth float

variable_types: ri float

variable_types: ra float


MahonyAHRSupdateIMU

decompiled_code:
void __fastcall MahonyAHRSupdateIMU(float a1, float a2, float a3, float a4, float a5, float a6)
{
double v6; // xmm0_8
float v7; // xmm1_4
float v8; // xmm5_4
float v9; // xmm1_4
float v10; // xmm6_4
float v11; // xmm4_4
float v12; // xmm2_4
float v13; // xmm3_4
float v14; // xmm1_4
float v15; // xmm4_4
float v16; // xmm8_4
float v17; // xmm7_4
float v18; // xmm6_4
double v19; // xmm0_8

if ( a6 != 0.0 || a5 != 0.0 || a4 != 0.0 )
{
v6 = invSqrt((float)((float)(a4 * a4) + (float)(a5 * a5)) + (float)(a6 * a6));
v7 = *(float *)&v6;
v8 = a4 * *(float *)&v6;
*(float *)&v6 = a5 * *(float *)&v6;
v9 = v7 * a6;
v10 = (float)(q1 * q3) - (float)(q0 * q2);
v11 = (float)(q0 * q1) + (float)(q2 * q3);
v12 = (float)(q3 * q3) + (float)((float)(q0 * q0) - 0.5);
v13 = (float)(*(float *)&v6 * v12) - (float)(v9 * v11);
v14 = (float)(v9 * v10) - (float)(v12 * v8);
v15 = (float)(v11 * v8) - (float)(*(float *)&v6 * v10);
if ( *(float *)&twoKi <= 0.0 )
{
integralFBx = 0;
integralFBy = 0;
integralFBz = 0;
}
else
{
*(float *)&integralFBx = (float)((float)(*(float *)&twoKi * v13) * 0.1) + *(float *)&integralFBx;
*(float *)&integralFBy = (float)((float)(*(float *)&twoKi * v14) * 0.1) + *(float *)&integralFBy;
*(float *)&integralFBz = (float)((float)(*(float *)&twoKi * v15) * 0.1) + *(float *)&integralFBz;
a1 = *(float *)&integralFBx + a1;
a2 = *(float *)&integralFBy + a2;
a3 = *(float *)&integralFBz + a3;
}
a1 = (float)(v13 * *(float *)&twoKp) + a1;
a2 = (float)(v14 * *(float *)&twoKp) + a2;
a3 = (float)(v15 * *(float *)&twoKp) + a3;
}
v16 = q0;
v17 = q1;
v18 = q2;
q0 = (float)((float)((float)(COERCE_FLOAT(q1 ^ LC4) * (float)(a1 * 0.050000001))
- (float)((float)(a2 * 0.050000001) * q2))
- (float)(q3 * (float)(0.050000001 * a3)))
+ q0;
q1 = (float)((float)((float)((float)(0.050000001 * a3) * q2) + (float)((float)(a1 * 0.050000001) * v16))
- (float)(q3 * (float)(a2 * 0.050000001)))
+ q1;
q2 = (float)((float)((float)((float)(a2 * 0.050000001) * v16) - (float)((float)(0.050000001 * a3) * v17))
+ (float)(q3 * (float)(a1 * 0.050000001)))
+ q2;
q3 = (float)((float)((float)((float)(0.050000001 * a3) * v16) + (float)((float)(a2 * 0.050000001) * v17))
- (float)((float)(a1 * 0.050000001) * v18))
+ q3;
v19 = invSqrt((float)((float)((float)(q0 * q0) + (float)(q1 * q1)) + (float)(q2 * q2)) + (float)(q3 * q3));
q0 = q0 * *(float *)&v19;
q1 = q1 * *(float *)&v19;
q2 = q2 * *(float *)&v19;
q3 = *(float *)&v19 * q3;
}

canonical_decompiled_code:
void __fastcall func0(float a1, float a2, float a3, float a4, float a5, float a6)
{
double v6; // xmm0_8
float v7; // xmm1_4
float v8; // xmm5_4
float v9; // xmm1_4
float v10; // xmm6_4
float v11; // xmm4_4
float v12; // xmm2_4
float v13; // xmm3_4
float v14; // xmm1_4
float v15; // xmm4_4
float v16; // xmm8_4
float v17; // xmm7_4
float v18; // xmm6_4
double v19; // xmm0_8

if ( a6 != 0.0 || a5 != 0.0 || a4 != 0.0 )
{
v6 = invSqrt((float)((float)(a4 * a4) + (float)(a5 * a5)) + (float)(a6 * a6));
v7 = *(float *)&v6;
v8 = a4 * *(float *)&v6;
*(float *)&v6 = a5 * *(float *)&v6;
v9 = v7 * a6;
v10 = (float)(q1 * q3) - (float)(q0 * q2);
v11 = (float)(q0 * q1) + (float)(q2 * q3);
v12 = (float)(q3 * q3) + (float)((float)(q0 * q0) - 0.5);
v13 = (float)(*(float *)&v6 * v12) - (float)(v9 * v11);
v14 = (float)(v9 * v10) - (float)(v12 * v8);
v15 = (float)(v11 * v8) - (float)(*(float *)&v6 * v10);
if ( *(float *)&twoKi <= 0.0 )
{
integralFBx = 0;
integralFBy = 0;
integralFBz = 0;
}
else
{
*(float *)&integralFBx = (float)((float)(*(float *)&twoKi * v13) * 0.1) + *(float *)&integralFBx;
*(float *)&integralFBy = (float)((float)(*(float *)&twoKi * v14) * 0.1) + *(float *)&integralFBy;
*(float *)&integralFBz = (float)((float)(*(float *)&twoKi * v15) * 0.1) + *(float *)&integralFBz;
a1 = *(float *)&integralFBx + a1;
a2 = *(float *)&integralFBy + a2;
a3 = *(float *)&integralFBz + a3;
}
a1 = (float)(v13 * *(float *)&twoKp) + a1;
a2 = (float)(v14 * *(float *)&twoKp) + a2;
a3 = (float)(v15 * *(float *)&twoKp) + a3;
}
v16 = q0;
v17 = q1;
v18 = q2;
q0 = (float)((float)((float)(COERCE_FLOAT(q1 ^ LC4) * (float)(a1 * 0.050000001))
- (float)((float)(a2 * 0.050000001) * q2))
- (float)(q3 * (float)(0.050000001 * a3)))
+ q0;
q1 = (float)((float)((float)((float)(0.050000001 * a3) * q2) + (float)((float)(a1 * 0.050000001) * v16))
- (float)(q3 * (float)(a2 * 0.050000001)))
+ q1;
q2 = (float)((float)((float)((float)(a2 * 0.050000001) * v16) - (float)((float)(0.050000001 * a3) * v17))
+ (float)(q3 * (float)(a1 * 0.050000001)))
+ q2;
q3 = (float)((float)((float)((float)(0.050000001 * a3) * v16) + (float)((float)(a2 * 0.050000001) * v17))
- (float)((float)(a1 * 0.050000001) * v18))
+ q3;
v19 = invSqrt((float)((float)((float)(q0 * q0) + (float)(q1 * q1)) + (float)(q2 * q2)) + (float)(q3 * q3));
q0 = q0 * *(float *)&v19;
q1 = q1 * *(float *)&v19;
q2 = q2 * *(float *)&v19;
q3 = *(float *)&v19 * q3;
}

original_code:
void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az) {
 float recipNorm;
 float halfvx, halfvy, halfvz;
 float halfex, halfey, halfez;
 float qa, qb, qc;
 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  ax *= recipNorm;
  ay *= recipNorm;
  az *= recipNorm;
  halfvx = q1 * q3 - q0 * q2;
  halfvy = q0 * q1 + q2 * q3;
  halfvz = q0 * q0 - 0.5f + q3 * q3;
  halfex = (ay * halfvz - az * halfvy);
  halfey = (az * halfvx - ax * halfvz);
  halfez = (ax * halfvy - ay * halfvx);
  if(twoKi > 0.0f) {
   integralFBx += twoKi * halfex * (1.0f / 10.0f);
   integralFBy += twoKi * halfey * (1.0f / 10.0f);
   integralFBz += twoKi * halfez * (1.0f / 10.0f);
   gx += integralFBx;
   gy += integralFBy;
   gz += integralFBz;
  }
  else {
   integralFBx = 0.0f;
   integralFBy = 0.0f;
   integralFBz = 0.0f;
  }
  gx += twoKp * halfex;
  gy += twoKp * halfey;
  gz += twoKp * halfez;
 }
 gx *= (0.5f * (1.0f / 10.0f));
 gy *= (0.5f * (1.0f / 10.0f));
 gz *= (0.5f * (1.0f / 10.0f));
 qa = q0;
 qb = q1;
 qc = q2;
 q0 += (-qb * gx - qc * gy - q3 * gz);
 q1 += (qa * gx + qc * gz - q3 * gy);
 q2 += (qa * gy - qb * gz + q3 * gx);
 q3 += (qa * gz + qb * gy - qc * gx);
 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
 q0 *= recipNorm;
 q1 *= recipNorm;
 q2 *= recipNorm;
 q3 *= recipNorm;
}

canonical_original_code:
void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az) {
 float recipNorm;
 float halfvx; float halfvy; float halfvz;
 float halfex; float halfey; float halfez;
 float qa; float qb; float qc;
 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  ax *= recipNorm;
  ay *= recipNorm;
  az *= recipNorm;
  halfvx = q1 * q3 - q0 * q2;
  halfvy = q0 * q1 + q2 * q3;
  halfvz = q0 * q0 - 0.5f + q3 * q3;
  halfex = (ay * halfvz - az * halfvy);
  halfey = (az * halfvx - ax * halfvz);
  halfez = (ax * halfvy - ay * halfvx);
  if(twoKi > 0.0f) {
   integralFBx += twoKi * halfex * (1.0f / 10.0f);
   integralFBy += twoKi * halfey * (1.0f / 10.0f);
   integralFBz += twoKi * halfez * (1.0f / 10.0f);
   gx += integralFBx;
   gy += integralFBy;
   gz += integralFBz;
  }
  else {
   integralFBx = 0.0f;
   integralFBy = 0.0f;
   integralFBz = 0.0f;
  }
  gx += twoKp * halfex;
  gy += twoKp * halfey;
  gz += twoKp * halfez;
 }
 gx *= (0.5f * (1.0f / 10.0f));
 gy *= (0.5f * (1.0f / 10.0f));
 gz *= (0.5f * (1.0f / 10.0f));
 qa = q0;
 qb = q1;
 qc = q2;
 q0 += (-qb * gx - qc * gy - q3 * gz);
 q1 += (qa * gx + qc * gz - q3 * gy);
 q2 += (qa * gy - qb * gz + q3 * gx);
 q3 += (qa * gz + qb * gy - qc * gx);
 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
 q0 *= recipNorm;
 q1 *= recipNorm;
 q2 *= recipNorm;
 q3 *= recipNorm;
}

variable_types: gx float

variable_types: gy float

variable_types: gz float

variable_types: ax float

variable_types: ay float

variable_types: az float

variable_types: recipNorm float

variable_types: halfvx float

variable_types: halfvy float

variable_types: halfvz float

variable_types: halfex float

variable_types: halfey float

variable_types: halfez float

variable_types: qa float

variable_types: qb float

variable_types: qc float


MahonyAHRSupdate

original_code:
void MahonyAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
 float recipNorm;
    float q0q0, q0q1, q0q2, q0q3, q1q1, q1q2, q1q3, q2q2, q2q3, q3q3;
 float hx, hy, bx, bz;
 float halfvx, halfvy, halfvz, halfwx, halfwy, halfwz;
 float halfex, halfey, halfez;
 float qa, qb, qc;
 if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) {
  MahonyAHRSupdateIMU(gx, gy, gz, ax, ay, az);
  return;
 }
 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  ax *= recipNorm;
  ay *= recipNorm;
  az *= recipNorm;
  recipNorm = invSqrt(mx * mx + my * my + mz * mz);
  mx *= recipNorm;
  my *= recipNorm;
  mz *= recipNorm;
        q0q0 = q0 * q0;
        q0q1 = q0 * q1;
        q0q2 = q0 * q2;
        q0q3 = q0 * q3;
        q1q1 = q1 * q1;
        q1q2 = q1 * q2;
        q1q3 = q1 * q3;
        q2q2 = q2 * q2;
        q2q3 = q2 * q3;
        q3q3 = q3 * q3;
        hx = 2.0f * (mx * (0.5f - q2q2 - q3q3) + my * (q1q2 - q0q3) + mz * (q1q3 + q0q2));
        hy = 2.0f * (mx * (q1q2 + q0q3) + my * (0.5f - q1q1 - q3q3) + mz * (q2q3 - q0q1));
        bx = sqrt(hx * hx + hy * hy);
        bz = 2.0f * (mx * (q1q3 - q0q2) + my * (q2q3 + q0q1) + mz * (0.5f - q1q1 - q2q2));
  halfvx = q1q3 - q0q2;
  halfvy = q0q1 + q2q3;
  halfvz = q0q0 - 0.5f + q3q3;
        halfwx = bx * (0.5f - q2q2 - q3q3) + bz * (q1q3 - q0q2);
        halfwy = bx * (q1q2 - q0q3) + bz * (q0q1 + q2q3);
        halfwz = bx * (q0q2 + q1q3) + bz * (0.5f - q1q1 - q2q2);
  halfex = (ay * halfvz - az * halfvy) + (my * halfwz - mz * halfwy);
  halfey = (az * halfvx - ax * halfvz) + (mz * halfwx - mx * halfwz);
  halfez = (ax * halfvy - ay * halfvx) + (mx * halfwy - my * halfwx);
  if(twoKi > 0.0f) {
   integralFBx += twoKi * halfex * (1.0f / 10.0f);
   integralFBy += twoKi * halfey * (1.0f / 10.0f);
   integralFBz += twoKi * halfez * (1.0f / 10.0f);
   gx += integralFBx;
   gy += integralFBy;
   gz += integralFBz;
  }
  else {
   integralFBx = 0.0f;
   integralFBy = 0.0f;
   integralFBz = 0.0f;
  }
  gx += twoKp * halfex;
  gy += twoKp * halfey;
  gz += twoKp * halfez;
 }
 gx *= (0.5f * (1.0f / 10.0f));
 gy *= (0.5f * (1.0f / 10.0f));
 gz *= (0.5f * (1.0f / 10.0f));
 qa = q0;
 qb = q1;
 qc = q2;
 q0 += (-qb * gx - qc * gy - q3 * gz);
 q1 += (qa * gx + qc * gz - q3 * gy);
 q2 += (qa * gy - qb * gz + q3 * gx);
 q3 += (qa * gz + qb * gy - qc * gx);
 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
 q0 *= recipNorm;
 q1 *= recipNorm;
 q2 *= recipNorm;
 q3 *= recipNorm;
}

canonical_original_code:
void MahonyAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
 float recipNorm;
    float q0q0; float q0q1; float q0q2; float q0q3; float q1q1; float q1q2; float q1q3; float q2q2; float q2q3; float q3q3;
 float hx; float hy; float bx; float bz;
 float halfvx; float halfvy; float halfvz; float halfwx; float halfwy; float halfwz;
 float halfex; float halfey; float halfez;
 float qa; float qb; float qc;
 if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) {
  MahonyAHRSupdateIMU(gx, gy, gz, ax, ay, az);
  return;
 }
 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  ax *= recipNorm;
  ay *= recipNorm;
  az *= recipNorm;
  recipNorm = invSqrt(mx * mx + my * my + mz * mz);
  mx *= recipNorm;
  my *= recipNorm;
  mz *= recipNorm;
        q0q0 = q0 * q0;
        q0q1 = q0 * q1;
        q0q2 = q0 * q2;
        q0q3 = q0 * q3;
        q1q1 = q1 * q1;
        q1q2 = q1 * q2;
        q1q3 = q1 * q3;
        q2q2 = q2 * q2;
        q2q3 = q2 * q3;
        q3q3 = q3 * q3;
        hx = 2.0f * (mx * (0.5f - q2q2 - q3q3) + my * (q1q2 - q0q3) + mz * (q1q3 + q0q2));
        hy = 2.0f * (mx * (q1q2 + q0q3) + my * (0.5f - q1q1 - q3q3) + mz * (q2q3 - q0q1));
        bx = sqrt(hx * hx + hy * hy);
        bz = 2.0f * (mx * (q1q3 - q0q2) + my * (q2q3 + q0q1) + mz * (0.5f - q1q1 - q2q2));
  halfvx = q1q3 - q0q2;
  halfvy = q0q1 + q2q3;
  halfvz = q0q0 - 0.5f + q3q3;
        halfwx = bx * (0.5f - q2q2 - q3q3) + bz * (q1q3 - q0q2);
        halfwy = bx * (q1q2 - q0q3) + bz * (q0q1 + q2q3);
        halfwz = bx * (q0q2 + q1q3) + bz * (0.5f - q1q1 - q2q2);
  halfex = (ay * halfvz - az * halfvy) + (my * halfwz - mz * halfwy);
  halfey = (az * halfvx - ax * halfvz) + (mz * halfwx - mx * halfwz);
  halfez = (ax * halfvy - ay * halfvx) + (mx * halfwy - my * halfwx);
  if(twoKi > 0.0f) {
   integralFBx += twoKi * halfex * (1.0f / 10.0f);
   integralFBy += twoKi * halfey * (1.0f / 10.0f);
   integralFBz += twoKi * halfez * (1.0f / 10.0f);
   gx += integralFBx;
   gy += integralFBy;
   gz += integralFBz;
  }
  else {
   integralFBx = 0.0f;
   integralFBy = 0.0f;
   integralFBz = 0.0f;
  }
  gx += twoKp * halfex;
  gy += twoKp * halfey;
  gz += twoKp * halfez;
 }
 gx *= (0.5f * (1.0f / 10.0f));
 gy *= (0.5f * (1.0f / 10.0f));
 gz *= (0.5f * (1.0f / 10.0f));
 qa = q0;
 qb = q1;
 qc = q2;
 q0 += (-qb * gx - qc * gy - q3 * gz);
 q1 += (qa * gx + qc * gz - q3 * gy);
 q2 += (qa * gy - qb * gz + q3 * gx);
 q3 += (qa * gz + qb * gy - qc * gx);
 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
 q0 *= recipNorm;
 q1 *= recipNorm;
 q2 *= recipNorm;
 q3 *= recipNorm;
}

variable_types: mx float

variable_types: my float

variable_types: mz float

variable_types: hx float

variable_types: hy float

variable_types: bx float

variable_types: bz float

variable_types: halfwx float

variable_types: halfwy float

variable_types: halfwz float


cmdcmp

original_code:
static int cmdcmp(const char *cmd, const char *s, char **endptr)
{
    int len = strlen(cmd);
    if(endptr && len <= strlen(s)) {
        *endptr = (char*)s+len;
        while(**endptr == ' ') (*endptr)++;
    }
    return !strncmp(cmd, s, len);
}

canonical_original_code:
static int cmdcmp(char *cmd, char *s, char **endptr)
{
    int len = strlen(cmd);
    if(endptr && len <= strlen(s)) {
        *endptr = (char *)s+len;
        while(**endptr == ' ') (*endptr)++;
    }
    return !strncmp(cmd, s, len);
}

variable_types:
cmd
char *

variable_types:
s
char *

variable_types:
endptr
char **

variable_types: len int


main

repo: sebasalazar/C

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bx
int v4; // ebp
int v5; // eax
int v6; // ebx
size_t v7; // rax
char *endptr; // [rsp+8h] [rbp-420h] BYREF
char s[1008]; // [rsp+10h] [rbp-418h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-28h] BYREF

if ( argc == 2 )
{
v3 = strtol(argv[1], &endptr, 0);
if ( *endptr )
{
fwrite("ECHOSERV: Invalid port number.\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
}
else
{
v3 = 2002;
if ( argc > 1 )
{
fwrite("ECHOSERV: Invalid arguments.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
}
v4 = socket(2, 1, 0);
if ( v4 >= 0 )
{
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(v3);
if ( bind(v4, &addr, 0x10u) >= 0 )
{
if ( listen(v4, 1024) >= 0 )
{
while ( 1 )
{
v5 = accept(v4, 0LL, 0LL);
v6 = v5;
if ( v5 < 0 )
break;
Readline(v5);
v7 = strlen(s);
Writeline(v6, s, v7);
if ( close(v6) < 0 )
{
fwrite("ECHOSERV: Error calling close()\n", 1uLL, 0x20uLL, stderr);
exit(1);
}
}
fwrite("ECHOSERV: Error calling accept()\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error calling listen()\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error calling bind()\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error creating listening socket.\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bx
int v4; // ebp
int v5; // eax
int v6; // ebx
size_t v7; // rax
char *endptr; // [rsp+8h] [rbp-420h] BYREF
char s[1008]; // [rsp+10h] [rbp-418h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-28h] BYREF

if ( argc == 2 )
{
v3 = strtol(argv[1], &endptr, 0);
if ( *endptr )
{
fwrite("ECHOSERV: Invalid port number.\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
}
else
{
v3 = 2002;
if ( argc > 1 )
{
fwrite("ECHOSERV: Invalid arguments.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
}
v4 = socket(2, 1, 0);
if ( v4 >= 0 )
{
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func7(0);
*(_WORD *)addr.sa_data = func8(v3);
if ( bind(v4, &addr, 0x10u) >= 0 )
{
if ( listen(v4, 1024) >= 0 )
{
while ( 1 )
{
v5 = accept(v4, 0LL, 0LL);
v6 = v5;
if ( v5 < 0 )
break;
func9(v5);
v7 = strlen(s);
func10(v6, s, v7);
if ( close(v6) < 0 )
{
fwrite("ECHOSERV: Error calling close()\n", 1uLL, 0x20uLL, stderr);
exit(1);
}
}
fwrite("ECHOSERV: Error calling accept()\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error calling listen()\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error calling bind()\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
fwrite("ECHOSERV: Error creating listening socket.\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}

original_code:
int main(int argc, char *argv[]) {
    int list_s;
    int conn_s;
    short int port;
    struct sockaddr_in servaddr;
    char buffer[(1000)];
    char *endptr;
    if ( argc == 2 ) {
 port = strtol(argv[1], &endptr, 0);
 if ( *endptr ) {
     fprintf(stderr, "ECHOSERV: Invalid port number.\n");
     exit(1);
 }
    }
    else if ( argc < 2 ) {
 port = (2002);
    }
    else {
 fprintf(stderr, "ECHOSERV: Invalid arguments.\n");
 exit(1);
    }
    if ( (list_s = socket(2, SOCK_STREAM, 0)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error creating listening socket.\n");
 exit(1);
    }
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    servaddr.sin_port = htons(port);
    if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error calling bind()\n");
 exit(1);
    }
    if ( listen(list_s, (1024)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error calling listen()\n");
 exit(1);
    }
    while ( 1 ) {
 if ( (conn_s = accept(list_s, ((void *)0), ((void *)0)) ) < 0 ) {
     fprintf(stderr, "ECHOSERV: Error calling accept()\n");
     exit(1);
 }
 Readline(conn_s, buffer, (1000)-1);
 Writeline(conn_s, buffer, strlen(buffer));
 if ( close(conn_s) < 0 ) {
     fprintf(stderr, "ECHOSERV: Error calling close()\n");
     exit(1);
 }
    }
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int list_s;
    int conn_s;
    short port;
    struct sockaddr_in servaddr;
    char buffer[-1];
    char *endptr;
    if ( argc == 2 ) {
 port = strtol(argv[1], &endptr, 0);
 if ( *endptr ) {
     fprintf(stderr, "ECHOSERV: Invalid port number.\n");
     exit(1);
 }
    }
    else if ( argc < 2 ) {
 port = (2002);
    }
    else {
 fprintf(stderr, "ECHOSERV: Invalid arguments.\n");
 exit(1);
    }
    if ( (list_s = socket(2, SOCK_STREAM, 0)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error creating listening socket.\n");
 exit(1);
    }
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    servaddr.sin_port = htons(port);
    if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error calling bind()\n");
 exit(1);
    }
    if ( listen(list_s, (1024)) < 0 ) {
 fprintf(stderr, "ECHOSERV: Error calling listen()\n");
 exit(1);
    }
    while ( 1 ) {
 if ( (conn_s = accept(list_s, ((void *)0), ((void *)0)) ) < 0 ) {
     fprintf(stderr, "ECHOSERV: Error calling accept()\n");
     exit(1);
 }
 Readline(conn_s, buffer, (1000)-1);
 Writeline(conn_s, buffer, strlen(buffer));
 if ( close(conn_s) < 0 ) {
     fprintf(stderr, "ECHOSERV: Error calling close()\n");
     exit(1);
 }
    }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: port short

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

variable_types:
buffer
char [-1]

variable_types:
endptr
char *

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


Readline

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

if ( a3 <= 1 )
{
v4 = 1LL;
goto LABEL_12;
}
v4 = a3;
v5 = 1LL;
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 != 1 )
break;
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_12;
}
++a2;
LABEL_5:
if ( ++v5 == v4 )
goto LABEL_12;
}
if ( v7 )
{
if ( *__errno_location() != 4 )
return -1LL;
goto LABEL_5;
}
v4 = 0LL;
if ( v5 == 1 )
return v4;
v4 = v5;
LABEL_12:
*a2 = 0;
return v4;
}

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

if ( a3 <= 1 )
{
v4 = 1LL;
goto LABEL_12;
}
v4 = a3;
v5 = 1LL;
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 != 1 )
break;
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_12;
}
++a2;
LABEL_5:
if ( ++v5 == v4 )
goto LABEL_12;
}
if ( v7 )
{
if ( *__errno_location() != 4 )
return -1LL;
goto LABEL_5;
}
v4 = 0LL;
if ( v5 == 1 )
return v4;
v4 = v5;
LABEL_12:
*a2 = 0;
return v4;
}

original_code:
ssize_t Readline(int sockd, void *vptr, size_t maxlen) {
    ssize_t n, rc;
    char c, *buffer;
    buffer = vptr;
    for ( n = 1; n < maxlen; n++ ) {
 if ( (rc = read(sockd, &c, 1)) == 1 ) {
     *buffer++ = c;
     if ( c == '\n' )
  break;
 }
 else if ( rc == 0 ) {
     if ( n == 1 )
  return 0;
     else
  break;
 }
 else {
     if ( (*__errno_location ()) == 4 )
  continue;
     return -1;
 }
    }
    *buffer = 0;
    return n;
}

canonical_original_code:
long Readline(int sockd, void *vptr, unsigned long maxlen) {
    long n; long rc;
    char c; char *buffer;
    buffer = vptr;
    for ( n = 1; n < maxlen; n++ ) {
 if ( (rc = read(sockd, &c, 1)) == 1 ) {
     *buffer++ = c;
     if ( c == '\n' )
  break;
 }
 else if ( rc == 0 ) {
     if ( n == 1 )
  return 0;
     else
  break;
 }
 else {
     if ( (*__errno_location ()) == 4 )
  continue;
     return -1;
 }
    }
    *buffer = 0;
    return n;
}

variable_types: sockd int

variable_types:
vptr
void *

variable_types:
maxlen
unsigned long

variable_types: n long

variable_types: rc long

variable_types: c char

variable_types:
buffer
char *

return_type: long


Writeline

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

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

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

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

original_code:
ssize_t Writeline(int sockd, const void *vptr, size_t n) {
    size_t nleft;
    ssize_t nwritten;
    const char *buffer;
    buffer = vptr;
    nleft = n;
    while ( nleft > 0 ) {
 if ( (nwritten = write(sockd, buffer, nleft)) <= 0 ) {
     if ( (*__errno_location ()) == 4 )
  nwritten = 0;
     else
  return -1;
 }
 nleft -= nwritten;
 buffer += nwritten;
    }
    return n;
}

canonical_original_code:
long Writeline(int sockd, void *vptr, unsigned long n) {
    unsigned long nleft;
    long nwritten;
    char *buffer;
    buffer = vptr;
    nleft = n;
    while ( nleft > 0 ) {
 if ( (nwritten = write(sockd, buffer, nleft)) <= 0 ) {
     if ( (*__errno_location ()) == 4 )
  nwritten = 0;
     else
  return -1;
 }
 nleft -= nwritten;
 buffer += nwritten;
    }
    return n;
}

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nwritten long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax

fwrite("\nEjemplo de uso Curl en C\n\t\tDesarrollado por Sebastian Salazar Molina\n", 1uLL, 0x46uLL, _bss_start);
fprintf(_bss_start, "\nMensaje Enviado: %s\n", "txtRUN=159978869");
v3 = comunicar("http://consulta.servel.cl", 0x50u, "/", (__int64)"txtRUN=159978869", 0);
if ( v3 )
fprintf(stderr, "\nRespuesta : %s\n", v3);
else
fwrite("\nError de Comunicacion.", 1uLL, 0x17uLL, stderr);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax

fwrite("\nEjemplo de uso Curl en C\n\t\tDesarrollado por Sebastian Salazar Molina\n", 1uLL, 0x46uLL, _bss_start);
fprintf(_bss_start, "\nMensaje Enviado: %s\n", "txtRUN=159978869");
v3 = func12("http://consulta.servel.cl", 0x50u, "/", (__int64)"txtRUN=159978869", 0);
if ( v3 )
fprintf(stderr, "\nRespuesta : %s\n", v3);
else
fwrite("\nError de Comunicacion.", 1uLL, 0x17uLL, stderr);
return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
unsigned __int64 v4; // rbx
unsigned __int64 v5; // rbp
unsigned __int64 v6; // rbp
char *v7; // rax

v3 = (char *)calloc(0x10uLL, 1uLL);
strcpy(v3, "146.83.0.0");
v4 = ip2numero(v3);
v5 = cantidad_direcciones(0x10u);
printf("\n%ld %ld\n", v4, v5);
v6 = v4 + v5;
if ( v4 < v6 )
{
do
{
v7 = numero2ip(v4);
puts(v7);
++v4;
}
while ( v4 != v6 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
unsigned __int64 v4; // rbx
unsigned __int64 v5; // rbp
unsigned __int64 v6; // rbp
char *v7; // rax

v3 = (char *)calloc(0x10uLL, 1uLL);
strcpy(v3, "146.83.0.0");
v4 = func15(v3);
v5 = func16(0x10u);
printf("\n%ld %ld\n", v4, v5);
v6 = v4 + v5;
if ( v4 < v6 )
{
do
{
v7 = func13(v4);
puts(v7);
++v4;
}
while ( v4 != v6 );
}
return 0;
}


decompiled_code:
ssize_t __fastcall send_message(int a1, const void *a2)
{
return write(a1, a2, 8uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func1(int a1, const void *a2)
{
return write(a1, a2, 8uLL);
}

original_code:
void send_message(int sd, struct controlMessage *m)
{
    write(sd,m,sizeof(m));
}

variable_types: sd int

variable_types: struct controlMessage { int type; int argLength; char *arg; } *


decompiled_code:
int __fastcall get_response(int a1, int a2, const void *a3)
{
send_message(a2, a3);
read(a2, 0LL, 4uLL);
if ( *(_DWORD *)func_cd != 13 || *(_DWORD *)func_cd != a1 )
return fwrite("Error of transmission", 1uLL, 0x15uLL, stderr);
read(a2, 0LL, 4uLL);
read(a2, 0LL, *(int *)func_cd);
return fputs(0LL, stdout);
}

canonical_decompiled_code:
int __fastcall func2(int a1, int a2, const void *a3)
{
func1(a2, a3);
read(a2, 0LL, 4uLL);
if ( *(_DWORD *)func_cd != 13 || *(_DWORD *)func_cd != a1 )
return fwrite("Error of transmission", 1uLL, 0x15uLL, stderr);
read(a2, 0LL, 4uLL);
read(a2, 0LL, *(int *)func_cd);
return fputs(0LL, stdout);
}

original_code:
void get_response(int type, int sd, struct controlMessage *m)
{
    send_message(sd,m);
    int* typeMessage=((void *)0);
    read(sd,typeMessage,sizeof(int));
    if(*typeMessage != type || *typeMessage != 13)
    {
        fprintf( stderr, "Error of transmission" );
        return;
    }
    int* length=((void *)0);
    read(sd,length,sizeof(int));
    char* buff=((void *)0);
    read(sd,buff,*length*sizeof(char));
    fprintf( stdout, "%s", buff);
}

canonical_original_code:
void get_response(int type, int sd, struct controlMessage *m)
{
    send_message(sd,m);
    int *typeMessage = ((void *)0);
    read(sd,typeMessage,sizeof(int));
    if(*typeMessage != type || *typeMessage != 13)
    {
        fprintf( stderr, "Error of transmission" );
        return;
    }
    int *length = ((void *)0);
    read(sd,length,sizeof(int));
    char *buff = ((void *)0);
    read(sd,buff,*length*sizeof(char));
    fprintf( stdout, "%s", buff);
}

variable_types: type int

variable_types:
typeMessage
int *

variable_types:
length
int *

variable_types:
buff
char *

user_defined_types: struct controlMessage { int type; int argLength; char *arg; }


decompiled_code:
void __fastcall clear_message(__int64 a1)
{
*(_DWORD *)a1 = 0;
*(_DWORD *)(a1 + 4) = 0;
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
void clear_message(struct controlMessage *m)
{
    m->type=0;
    m->argLength=0;
    m->arg=((void *)0);
}


decompiled_code:
int __fastcall transfer_and_recieve_files(int fd)
{
int v1; // eax
int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( listen(fd, 5) == -1 )
{
perror("The file sender/reciever can't listen on port %i");
close(fd);
exit(1);
}
fprintf(stderr, "The file sender/reciever is listening on port %i ...", 7000LL);
v1 = accept(fd, &v4, addr_len);
v2 = v1;
if ( v1 < 0 )
{
perror("The file sender/reciever can't accept a connection\n");
close(fd);
exit(1);
}
read(v1, 0LL, 4uLL);
fwrite("unauthorized connection to the FTP server", 1uLL, 0x29uLL, stderr);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func4(int fd)
{
int v1; // eax
int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( listen(fd, 5) == -1 )
{
perror("The file sender/reciever can't listen on port %i");
close(fd);
exit(1);
}
fprintf(stderr, "The file sender/reciever is listening on port %i ...", 7000LL);
v1 = accept(fd, &v4, addr_len);
v2 = v1;
if ( v1 < 0 )
{
perror("The file sender/reciever can't accept a connection\n");
close(fd);
exit(1);
}
read(v1, 0LL, 4uLL);
fwrite("unauthorized connection to the FTP server", 1uLL, 0x29uLL, stderr);
return close(v2);
}

original_code:
void transfer_and_recieve_files(int sd)
{
    if(listen(sd,5) == -1)
    {
        perror("The file sender/reciever can't listen on port %i");
        close(sd);
        exit(1);
    }
    fprintf(stderr,"The file sender/reciever is listening on port %i ...",7000);
    int sdf;
    socklen_t sin_size;
    struct sockaddr_in sockaddrC;
    if ((sdf = accept(sd, (struct sockaddr *)&sockaddrC, &sin_size)) < 0)
    {
        perror("The file sender/reciever can't accept a connection\n");
        close(sd);
        exit(1);
    }
    int* typeMessage=((void *)0);
    read(sdf,typeMessage,sizeof(int));
    if(*typeMessage != 8|| *typeMessage != 7)
    {
        fprintf( stderr, "unauthorized connection to the FTP server" );
        close(sdf);
        return;
    }
    int* length=((void *)0);
    read(sdf,length,sizeof(int));
    char* name=((void *)0);
    read(sdf,name,*length*sizeof(char));
    if(*typeMessage == 8)
        recieve_file(sdf,name);
    else
        transfer_file(sdf,name);
    close(sdf);
}

canonical_original_code:
void transfer_and_recieve_files(int sd)
{
    if(listen(sd,5) == -1)
    {
        perror("The file sender/reciever can't listen on port %i");
        close(sd);
        exit(1);
    }
    fprintf(stderr,"The file sender/reciever is listening on port %i ...",7000);
    int sdf;
    unsigned int sin_size;
    struct sockaddr_in sockaddrC;
    if ((sdf = accept(sd, (struct sockaddr *)&sockaddrC, &sin_size)) < 0)
    {
        perror("The file sender/reciever can't accept a connection\n");
        close(sd);
        exit(1);
    }
    int *typeMessage = ((void *)0);
    read(sdf,typeMessage,sizeof(int));
    if(*typeMessage != 8|| *typeMessage != 7)
    {
        fprintf( stderr, "unauthorized connection to the FTP server" );
        close(sdf);
        return;
    }
    int *length = ((void *)0);
    read(sdf,length,sizeof(int));
    char *name = ((void *)0);
    read(sdf,name,*length*sizeof(char));
    if(*typeMessage == 8)
        recieve_file(sdf,name);
    else
        transfer_file(sdf,name);
    close(sdf);
}

variable_types: sdf int

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

variable_types:
name
char *


decompiled_code:
void func_send()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void func_send(char* cmd)
{
}

canonical_original_code:
void func_send(char *cmd)
{
}

variable_types:
cmd
char *


decompiled_code:
__int64 __fastcall recieve_message(int fd)
{
unsigned int v1; // edx
int v3; // [rsp+0h] [rbp-18h] BYREF
int buf; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

clear_message((__int64)&v3);
read(fd, &v3, 4uLL);
read(fd, &buf, 4uLL);
read(fd, &v5, buf);
v1 = 1;
if ( (v3 & 0xFFFFFFFD) != 1 )
{
LOBYTE(v1) = v3 != 9;
if ( v3 == 5 )
{
get_response(3LL, (unsigned int)fd, &v3);
func_cd(v5);
v1 = 1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd)
{
unsigned int v1; // edx
int v3; // [rsp+0h] [rbp-18h] BYREF
int buf; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

func1((__int64)&v3);
read(fd, &v3, 4uLL);
read(fd, &buf, 4uLL);
read(fd, &v5, buf);
v1 = 1;
if ( (v3 & 0xFFFFFFFD) != 1 )
{
LOBYTE(v1) = v3 != 9;
if ( v3 == 5 )
{
get_response(3LL, (unsigned int)fd, &v3);
func_cd(v5);
v1 = 1;
}
}
return v1;
}

original_code:
_Bool recieve_message(int sd)
{
    struct controlMessage mess;
    clear_message(&mess);
    read(sd,&mess.type,sizeof(int));
    read(sd,&mess.argLength,sizeof(int));
    read(sd,&mess.arg,(mess.argLength)*sizeof(char));
    if(mess.type == 1)
        func_send("pwd");
    else if(mess.type == 3)
        func_send("ls");
    else if(mess.type == 5)
    {
        get_response(3,sd,&mess);
        func_cd(mess.arg);
    }
    else if(mess.type == 9)
    {
        return 0;
    }
    return 1;
}

variable_types: struct controlMessage { int type; int argLength; char *arg; }


repo: sebcat/yarlib

decompiled_code:
__int64 __fastcall port_range_next(unsigned int *a1, unsigned int *a2)
{
unsigned int v2; // eax
unsigned int v3; // edx
unsigned int v4; // ecx

if ( !a1 )
_assert_fail("range != NULL", "port.c", 0x74u, "port_range_next");
if ( !a2 )
_assert_fail("dst != NULL", "port.c", 0x75u, "port_range_next");
v2 = *a1;
v3 = a1[1];
if ( *a1 <= v3 )
{
if ( v2 < v3 )
{
v2 += a1[2];
v4 = 0;
if ( v3 < v2 )
return v4;
goto LABEL_5;
}
if ( v2 != v3 )
_assert_fail("0", "port.c", 0x87u, "port_range_next");
v4 = 0;
if ( !a1[2] )
{
LABEL_5:
*a2 = v2;
++a1[2];
return 1;
}
}
else
{
v2 -= a1[2];
v4 = 0;
if ( v3 <= v2 )
goto LABEL_5;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, unsigned int *a2)
{
unsigned int v2; // eax
unsigned int v3; // edx
unsigned int v4; // ecx

if ( !a1 )
_assert_fail("range != NULL", "port.c", 0x74u, "port_range_next");
if ( !a2 )
_assert_fail("dst != NULL", "port.c", 0x75u, "port_range_next");
v2 = *a1;
v3 = a1[1];
if ( *a1 <= v3 )
{
if ( v2 < v3 )
{
v2 += a1[2];
v4 = 0;
if ( v3 < v2 )
return v4;
goto LABEL_5;
}
if ( v2 != v3 )
_assert_fail("0", "port.c", 0x87u, "port_range_next");
v4 = 0;
if ( !a1[2] )
{
LABEL_5:
*a2 = v2;
++a1[2];
return 1;
}
}
else
{
v2 -= a1[2];
v4 = 0;
if ( v3 <= v2 )
goto LABEL_5;
}
return v4;
}

original_code:
static _Bool port_range_next(port_range_t *range, yar_port_t *dst)
{
    yar_port_t curr;
    ((void) sizeof ((range != ((void *)0)) ? 1 : 0), __extension__ ({ if (range != ((void *)0)) ; else __assert_fail ("range != NULL", "port.c", 116, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((dst != ((void *)0)) ? 1 : 0), __extension__ ({ if (dst != ((void *)0)) ; else __assert_fail ("dst != NULL", "port.c", 117, __extension__ __PRETTY_FUNCTION__); }));
    if (range->start > range->end) {
        curr = range->start - range->offset;
        if (curr < range->end) {
            return 0;
        }
    } else if (range->start < range->end) {
        curr = range->start + range->offset;
        if (curr > range->end) {
            return 0;
        }
    } else if (range->start == range->end) {
        curr = range->start;
        if (range->offset != 0) {
            return 0;
        }
    } else {
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "port.c", 135, __extension__ __PRETTY_FUNCTION__); }));
        return 0;
    }
    *dst = curr;
    range->offset++;
    return 1;
}

canonical_original_code:
static _Bool port_range_next(struct port_range_t *range, unsigned int *dst)
{
    unsigned int curr;
    ((void) sizeof ((range != ((void *)0)) ? 1 : 0), __extension__ ({ if (range != ((void *)0)) ; else __assert_fail ("range != NULL", "port.c", 116, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((dst != ((void *)0)) ? 1 : 0), __extension__ ({ if (dst != ((void *)0)) ; else __assert_fail ("dst != NULL", "port.c", 117, __extension__ __PRETTY_FUNCTION__); }));
    if (range->start > range->end) {
        curr = range->start - range->offset;
        if (curr < range->end) {
            return 0;
        }
    } else if (range->start < range->end) {
        curr = range->start + range->offset;
        if (curr > range->end) {
            return 0;
        }
    } else if (range->start == range->end) {
        curr = range->start;
        if (range->offset != 0) {
            return 0;
        }
    } else {
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "port.c", 135, __extension__ __PRETTY_FUNCTION__); }));
        return 0;
    }
    *dst = curr;
    range->offset++;
    return 1;
}

variable_types: struct port_range_t { unsigned int start; unsigned int offset; } *

variable_types:
dst
unsigned int *

variable_types:
curr
unsigned int