drawText

decompiled_code:
__int64 __fastcall drawText(int a1, int a2, char *a3, float a4)
{
__int64 result; // rax
float v5; // xmm4_4
char *v6; // r12
int v7; // er13
double v8; // xmm1_8
int v9; // ebp
double v10; // xmm0_8
unsigned __int8 v11; // bl
int v12; // er14
char v13; // r15
float v14; // xmm2_4
float v15; // [rsp+8h] [rbp-48h]
float v16; // [rsp+Ch] [rbp-44h]
float v17; // [rsp+10h] [rbp-40h]
float v18; // [rsp+14h] [rbp-3Ch]

result = (unsigned __int8)*a3;
v5 = a4 / (float)width2;
v18 = (float)a1 / (float)width2;
v17 = (float)a2 / (float)height2;
if ( (_BYTE)result )
{
v6 = a3;
v7 = -1;
do
{
while ( (_BYTE)result == 10 )
{
++v6;
v7 = -1;
result = (unsigned __int8)*v6;
v17 = v17 - (float)(9.0 * v5);
if ( !(_BYTE)result )
return result;
}
++v7;
*(_QWORD *)&v8 = LODWORD(v17);
v9 = 0;
v16 = (float)((float)(9 * v7) * v5) + v18;
do
{
*(_QWORD *)&v10 = LODWORD(v16);
v11 = 0x80;
v12 = 0;
v13 = gfxPrimitivesFontdata[8 * *v6 + v9];
do
{
while ( (v11 & (unsigned __int8)v13) == 0 )
{
++v12;
*(float *)&v10 = *(float *)&v10 + v5;
v11 >>= 1;
if ( v12 > 7 )
goto LABEL_11;
}
v14 = *(float *)&v10;
do
{
v11 >>= 1;
v14 = v14 + v5;
++v12;
}
while ( (v11 & (unsigned __int8)v13) != 0 );
v15 = v5;
++v12;
v11 >>= 1;
drawBox(v10, v8, v14, v5 + *(float *)&v8);
v5 = v15;
*(_QWORD *)&v8 = LODWORD(v8);
*(_QWORD *)&v10 = LODWORD(v14);
*(float *)&v10 = v14 + v15;
}
while ( v12 <= 7 );
LABEL_11:
++v9;
*(float *)&v8 = *(float *)&v8 - v5;
}
while ( v9 != 8 );
result = (unsigned __int8)*++v6;
}
while ( (_BYTE)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char *a3, float a4)
{
__int64 result; // rax
float v5; // xmm4_4
char *v6; // r12
int v7; // er13
double v8; // xmm1_8
int v9; // ebp
double v10; // xmm0_8
unsigned __int8 v11; // bl
int v12; // er14
char v13; // r15
float v14; // xmm2_4
float v15; // [rsp+8h] [rbp-48h]
float v16; // [rsp+Ch] [rbp-44h]
float v17; // [rsp+10h] [rbp-40h]
float v18; // [rsp+14h] [rbp-3Ch]

result = (unsigned __int8)*a3;
v5 = a4 / (float)width2;
v18 = (float)a1 / (float)width2;
v17 = (float)a2 / (float)height2;
if ( (_BYTE)result )
{
v6 = a3;
v7 = -1;
do
{
while ( (_BYTE)result == 10 )
{
++v6;
v7 = -1;
result = (unsigned __int8)*v6;
v17 = v17 - (float)(9.0 * v5);
if ( !(_BYTE)result )
return result;
}
++v7;
*(_QWORD *)&v8 = LODWORD(v17);
v9 = 0;
v16 = (float)((float)(9 * v7) * v5) + v18;
do
{
*(_QWORD *)&v10 = LODWORD(v16);
v11 = 0x80;
v12 = 0;
v13 = gfxPrimitivesFontdata[8 * *v6 + v9];
do
{
while ( (v11 & (unsigned __int8)v13) == 0 )
{
++v12;
*(float *)&v10 = *(float *)&v10 + v5;
v11 >>= 1;
if ( v12 > 7 )
goto LABEL_11;
}
v14 = *(float *)&v10;
do
{
v11 >>= 1;
v14 = v14 + v5;
++v12;
}
while ( (v11 & (unsigned __int8)v13) != 0 );
v15 = v5;
++v12;
v11 >>= 1;
drawBox(v10, v8, v14, v5 + *(float *)&v8);
v5 = v15;
*(_QWORD *)&v8 = LODWORD(v8);
*(_QWORD *)&v10 = LODWORD(v14);
*(float *)&v10 = v14 + v15;
}
while ( v12 <= 7 );
LABEL_11:
++v9;
*(float *)&v8 = *(float *)&v8 - v5;
}
while ( v9 != 8 );
result = (unsigned __int8)*++v6;
}
while ( (_BYTE)result );
}
return result;
}

original_code:
void drawText(int X, int Y, float size, char *string)
{
 float x = (float) X / width2;
 float y = (float) Y / height2;
 size /= width2;
 int j = -1;
 int i = -1;
 int y2;
 int x2;
 register uint8_t row;
 register uint8_t test;
 float tempx, tempy;
 while (string[++j] != '\0') {
  i++;
  if (string[j] == '\n') {
   i = -1;
   y -= size * 9;
   continue;
  }
  tempy = y;
  for (y2 = 0; y2 < 8; y2++) {
   row = gfxPrimitivesFontdata[8 * string[j] + y2];
   test = 128;
   tempx = x + i * 9 * size;
   for (x2 = 0; x2 < 8; x2++) {
    if (row & test) {
     float oldX = tempx;
     do {
      tempx += size;
      test /= 2;
      x2++;
     } while (row & test);
     drawBox(oldX, tempy, tempx,
      tempy + size);
    }
    tempx += size;
    test /= 2;
   }
   tempy -= size;
  }
 }
}

canonical_original_code:
void drawText(int X, int Y, float size, char *string)
{
 float x = (float) X / width2;
 float y = (float) Y / height2;
 size /= width2;
 int j = -1;
 int i = -1;
 int y2;
 int x2;
 register unsigned char row;
 register unsigned char test;
 float tempx; float tempy;
 while (string[++j] != '\0') {
  i++;
  if (string[j] == '\n') {
   i = -1;
   y -= size * 9;
   continue;
  }
  tempy = y;
  for (y2 = 0; y2 < 8; y2++) {
   row = gfxPrimitivesFontdata[8 * string[j] + y2];
   test = 128;
   tempx = x + i * 9 * size;
   for (x2 = 0; x2 < 8; x2++) {
    if (row & test) {
     float oldX = tempx;
     do {
      tempx += size;
      test /= 2;
      x2++;
     } while (row & test);
     drawBox(oldX, tempy, tempx,
      tempy + size);
    }
    tempx += size;
    test /= 2;
   }
   tempy -= size;
  }
 }
}

variable_types: X int

variable_types: Y int

variable_types: size float

variable_types:
string
char *

variable_types: x float

variable_types: y float

variable_types: j int

variable_types: i int

variable_types:
row
unsigned char

variable_types:
test
unsigned char

variable_types: tempx float

variable_types: tempy float

variable_types: oldX float


print

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

result = a1 % 4;
if ( a1 % 4 == 2 )
{
fwrite(&unk_40200A, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
else if ( result == 3 )
{
fwrite(&unk_40200D, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
else if ( (a1 & 3) != 0 )
{
if ( result == 1 )
{
fwrite(&unk_402007, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
}
else
{
fwrite(&unk_402004, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
return result;
}

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

result = a1 % 4;
if ( a1 % 4 == 2 )
{
fwrite(&unk_40200A, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
else if ( result == 3 )
{
fwrite(&unk_40200D, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
else if ( (a1 & 3) != 0 )
{
if ( result == 1 )
{
fwrite(&unk_402007, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
}
else
{
fwrite(&unk_402004, 1uLL, 2uLL, _bss_start);
result = fflush(_bss_start);
}
return result;
}

original_code:
void print(int spin) {
    switch (spin%4) {
    case 0:
      fprintf(stdout, "\b|");
      fflush(stdout);
      break;
    case 1:
      fprintf(stdout, "\b/");
      fflush(stdout);
      break;
    case 2:
      fprintf(stdout, "\b-");
      fflush(stdout);
      break;
    case 3:
      fprintf(stdout, "\b\\");
      fflush(stdout);
      break;
    }
    return;
}

variable_types: spin int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
__int64 v7; // rbx
__int64 v8; // rdx
unsigned int v10; // er14
int v11; // ebp
char *i; // r12
struct timespec *v13; // r15
char j; // al
char *v15; // r12
int k; // ebp
const struct timespec *v17; // r15
char l; // al
int fd; // [rsp+Ch] [rbp-8Ch]
int *v20; // [rsp+10h] [rbp-88h]
unsigned int v21; // [rsp+18h] [rbp-80h]
int v22; // [rsp+1Ch] [rbp-7Ch]
__int16 buf; // [rsp+2Ah] [rbp-6Eh] BYREF
char v24; // [rsp+2Ch] [rbp-6Ch] BYREF
__int16 v25; // [rsp+2Dh] [rbp-6Bh]
char v26; // [rsp+2Fh] [rbp-69h]
struct timespec requested_time; // [rsp+30h] [rbp-68h] BYREF
int v28; // [rsp+40h] [rbp-58h] BYREF
int v29; // [rsp+44h] [rbp-54h]
int v30; // [rsp+48h] [rbp-50h]
int v31; // [rsp+4Ch] [rbp-4Ch]
int v32; // [rsp+50h] [rbp-48h]
char v33[68]; // [rsp+54h] [rbp-44h] BYREF

v25 = 513;
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 500000000LL;
v26 = 18;
fd = open("/dev/dio0", 2);
v28 = fd;
v29 = open("/dev/dio1", 2);
v3 = v29;
v30 = open("/dev/dio2", 2);
v4 = v30;
v31 = open("/dev/dio3", 2);
v5 = v31;
buf = 896;
v6 = open("/dev/dio4", 2);
v32 = v6;
write(fd, &buf, 2uLL);
write(v3, &buf, 2uLL);
write(v4, &buf, 2uLL);
write(v5, &buf, 2uLL);
write(v6, &buf, 2uLL);
v22 = 0;
v7 = 0LL;
if ( !fd )
goto LABEL_4;
while ( ++v7 != 5 )
{
while ( !*(&v28 + v7) )
{
LABEL_4:
v8 = (unsigned int)v7++;
fprintf(_bss_start, "Device %d failed to open\n", v8);
v22 = -1;
if ( v7 == 5 )
goto LABEL_5;
}
}
LABEL_5:
if ( v22 )
return -1;
v10 = 0;
fwrite("Checking\n", 1uLL, 9uLL, _bss_start);
fwrite("-> ", 1uLL, 4uLL, _bss_start);
fflush(_bss_start);
do
{
v11 = fd;
v20 = &v28;
for ( i = (char *)&v28; ; v11 = *(_DWORD *)i )
{
LOBYTE(buf) = 0;
v13 = (struct timespec *)&v24;
for ( j = 0; ; j = v13->tv_sec )
{
v13 = (struct timespec *)((char *)v13 + 1);
HIBYTE(buf) = j;
write(v11, &buf, 2uLL);
if ( v13 == &requested_time )
break;
}
i += 4;
if ( i == v33 )
break;
}
v15 = (char *)&v28;
nanosleep(&requested_time, 0LL);
v21 = v10 + 1;
print(v10);
for ( k = fd; ; k = *(_DWORD *)v15 )
{
LOBYTE(buf) = -1;
v17 = (const struct timespec *)&v24;
for ( l = 0; ; l = v17->tv_sec )
{
v17 = (const struct timespec *)((char *)v17 + 1);
HIBYTE(buf) = l;
write(k, &buf, 2uLL);
if ( v17 == &requested_time )
break;
}
v15 += 4;
if ( v15 == v33 )
break;
}
v10 += 2;
nanosleep(v17, 0LL);
print(v21);
}
while ( v10 != 62 );
fwrite("\bDone!\n", 1uLL, 7uLL, _bss_start);
fflush(_bss_start);
do
close(*v20++);
while ( v20 != (int *)v33 );
return v22;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
__int64 v7; // rbx
__int64 v8; // rdx
unsigned int v10; // er14
int v11; // ebp
char *i; // r12
struct timespec *v13; // r15
char j; // al
char *v15; // r12
int k; // ebp
const struct timespec *v17; // r15
char l; // al
int fd; // [rsp+Ch] [rbp-8Ch]
int *v20; // [rsp+10h] [rbp-88h]
unsigned int v21; // [rsp+18h] [rbp-80h]
int v22; // [rsp+1Ch] [rbp-7Ch]
__int16 buf; // [rsp+2Ah] [rbp-6Eh] BYREF
char v24; // [rsp+2Ch] [rbp-6Ch] BYREF
__int16 v25; // [rsp+2Dh] [rbp-6Bh]
char v26; // [rsp+2Fh] [rbp-69h]
struct timespec requested_time; // [rsp+30h] [rbp-68h] BYREF
int v28; // [rsp+40h] [rbp-58h] BYREF
int v29; // [rsp+44h] [rbp-54h]
int v30; // [rsp+48h] [rbp-50h]
int v31; // [rsp+4Ch] [rbp-4Ch]
int v32; // [rsp+50h] [rbp-48h]
char v33[68]; // [rsp+54h] [rbp-44h] BYREF

v25 = 513;
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 500000000LL;
v26 = 18;
fd = open("/dev/dio0", 2);
v28 = fd;
v29 = open("/dev/dio1", 2);
v3 = v29;
v30 = open("/dev/dio2", 2);
v4 = v30;
v31 = open("/dev/dio3", 2);
v5 = v31;
buf = 896;
v6 = open("/dev/dio4", 2);
v32 = v6;
write(fd, &buf, 2uLL);
write(v3, &buf, 2uLL);
write(v4, &buf, 2uLL);
write(v5, &buf, 2uLL);
write(v6, &buf, 2uLL);
v22 = 0;
v7 = 0LL;
if ( !fd )
goto LABEL_4;
while ( ++v7 != 5 )
{
while ( !*(&v28 + v7) )
{
LABEL_4:
v8 = (unsigned int)v7++;
fprintf(_bss_start, "Device %d failed to open\n", v8);
v22 = -1;
if ( v7 == 5 )
goto LABEL_5;
}
}
LABEL_5:
if ( v22 )
return -1;
v10 = 0;
fwrite("Checking\n", 1uLL, 9uLL, _bss_start);
fwrite("-> ", 1uLL, 4uLL, _bss_start);
fflush(_bss_start);
do
{
v11 = fd;
v20 = &v28;
for ( i = (char *)&v28; ; v11 = *(_DWORD *)i )
{
LOBYTE(buf) = 0;
v13 = (struct timespec *)&v24;
for ( j = 0; ; j = v13->tv_sec )
{
v13 = (struct timespec *)((char *)v13 + 1);
HIBYTE(buf) = j;
write(v11, &buf, 2uLL);
if ( v13 == &requested_time )
break;
}
i += 4;
if ( i == v33 )
break;
}
v15 = (char *)&v28;
nanosleep(&requested_time, 0LL);
v21 = v10 + 1;
func7(v10);
for ( k = fd; ; k = *(_DWORD *)v15 )
{
LOBYTE(buf) = -1;
v17 = (const struct timespec *)&v24;
for ( l = 0; ; l = v17->tv_sec )
{
v17 = (const struct timespec *)((char *)v17 + 1);
HIBYTE(buf) = l;
write(k, &buf, 2uLL);
if ( v17 == &requested_time )
break;
}
v15 += 4;
if ( v15 == v33 )
break;
}
v10 += 2;
nanosleep(v17, 0LL);
func7(v21);
}
while ( v10 != 62 );
fwrite("\bDone!\n", 1uLL, 7uLL, _bss_start);
fflush(_bss_start);
do
close(*v20++);
while ( v20 != (int *)v33 );
return v22;
}

original_code:
int main(void) {
  int group[5], i, j, err = 0, dur = 0, spin = 0;
  unsigned char zero_byte = 0x00, full_byte = 0xff;
  unsigned char write_msg[2], port[4];
  struct timespec pause;
  pause.tv_sec = 0;
  pause.tv_nsec = 500000000;
  port[0] = 0x00;
  port[1] = 0x01;
  port[2] = 0x02;
  port[3] = 0x12;
  group[0] = open("/dev/dio0", 02);
  group[1] = open("/dev/dio1", 02);
  group[2] = open("/dev/dio2", 02);
  group[3] = open("/dev/dio3", 02);
  group[4] = open("/dev/dio4", 02);
  write_msg[1] = 0x03;
  write_msg[0] = 0x80;
  write(group[0], write_msg, 2);
  write(group[1], write_msg, 2);
  write(group[2], write_msg, 2);
  write(group[3], write_msg, 2);
  write(group[4], write_msg, 2);
  for ( i = 0; i < 5; i++ ) {
    if (!group[i]) {
      fprintf(stdout, "Device %d failed to open\n", i);
      err = -1;
    }
  }
  if (err)
    return err;
  fprintf(stdout, "Checking\n");
  fprintf(stdout, "->  ");
  fflush(stdout);
  while (1) {
    for ( i = 0; i < 5; i++) {
      write_msg[0] = zero_byte;
      for ( j = 0; j < 4; j++) {
 write_msg[1] = port[j];
 write(group[i], write_msg, 2);
      }
    }
    nanosleep(&pause, ((void *)0));
    print(spin++);
    for ( i = 0; i < 5; i++) {
      write_msg[0] = full_byte;
      for ( j = 0; j < 4; j++) {
 write_msg[1] = port[j];
 write(group[i], write_msg, 2);
      }
    }
    nanosleep(&pause, ((void *)0));
    print(spin++);
    if (dur++ >= 30)
      break;
  }
  fprintf(stdout, "\bDone!\n");
  fflush(stdout);
  for (i = 0; i < 5; i++)
    close(group[i]);
  return 0;
}

canonical_original_code:
int main() {
  int group[5]; int i; int j; int err = 0; int dur = 0; int spin = 0;
  unsigned char zero_byte = 0x00; unsigned char full_byte = 0xff;
  unsigned char write_msg[2]; unsigned char port[4];
  struct timespec pause;
  pause.tv_sec = 0;
  pause.tv_nsec = 500000000;
  port[0] = 0x00;
  port[1] = 0x01;
  port[2] = 0x02;
  port[3] = 0x12;
  group[0] = open("/dev/dio0", 02);
  group[1] = open("/dev/dio1", 02);
  group[2] = open("/dev/dio2", 02);
  group[3] = open("/dev/dio3", 02);
  group[4] = open("/dev/dio4", 02);
  write_msg[1] = 0x03;
  write_msg[0] = 0x80;
  write(group[0], write_msg, 2);
  write(group[1], write_msg, 2);
  write(group[2], write_msg, 2);
  write(group[3], write_msg, 2);
  write(group[4], write_msg, 2);
  for ( i = 0; i < 5; i++ ) {
    if (!group[i]) {
      fprintf(stdout, "Device %d failed to open\n", i);
      err = -1;
    }
  }
  if (err)
    return err;
  fprintf(stdout, "Checking\n");
  fprintf(stdout, "->  ");
  fflush(stdout);
  while (1) {
    for ( i = 0; i < 5; i++) {
      write_msg[0] = zero_byte;
      for ( j = 0; j < 4; j++) {
 write_msg[1] = port[j];
 write(group[i], write_msg, 2);
      }
    }
    nanosleep(&pause, ((void *)0));
    print(spin++);
    for ( i = 0; i < 5; i++) {
      write_msg[0] = full_byte;
      for ( j = 0; j < 4; j++) {
 write_msg[1] = port[j];
 write(group[i], write_msg, 2);
      }
    }
    nanosleep(&pause, ((void *)0));
    print(spin++);
    if (dur++ >= 30)
      break;
  }
  fprintf(stdout, "\bDone!\n");
  fflush(stdout);
  for (i = 0; i < 5; i++)
    close(group[i]);
  return 0;
}

variable_types:
group
int [5]

variable_types: err int

variable_types: dur int

variable_types:
port
unsigned char [4]

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

return_type: int


decompiled_code:
__int64 get_input()
{
char v0; // bp
__int64 v1; // rbx
__int64 result; // rax
int v3; // edi
unsigned __int8 buf; // [rsp+Eh] [rbp-3Ah] BYREF
char v5; // [rsp+Fh] [rbp-39h]
_BYTE s[40]; // [rsp+10h] [rbp-38h] BYREF

fwrite("input group (0-4) > ", 1uLL, 0x14uLL, stdout);
fflush(stdout);
fgets(s, 20, stdin);
v0 = s[0];
if ( s[0] == 113 )
return 4294967287LL;
v1 = atoi(s);
if ( v0 != 48 && !(_DWORD)v1 )
return 1LL;
*(_DWORD *)&s[16] = 0;
*(_OWORD *)s = 0LL;
if ( (unsigned int)v1 > 4 )
return 1LL;
fwrite("input port (a,b,c,d) > ", 1uLL, 0x18uLL, stdout);
fflush(stdout);
fgets(s, 20, stdin);
s[19] = 0;
*(_WORD *)&s[17] = 0;
*(_OWORD *)&s[1] = 0LL;
switch ( s[0] )
{
case 'a':
v5 = 0;
goto LABEL_9;
case 'b':
v5 = 1;
goto LABEL_9;
case 'c':
v5 = 2;
goto LABEL_9;
case 'd':
v5 = 18;
LABEL_9:
fwrite("input hex data > ", 1uLL, 0x11uLL, stdout);
fflush(stdout);
buf = 0;
fgets(s, 20, stdin);
switch ( s[1] )
{
case '0':
goto LABEL_14;
case '1':
buf |= 1u;
goto LABEL_14;
case '2':
buf |= 2u;
goto LABEL_14;
case '3':
buf |= 3u;
goto LABEL_14;
case '4':
buf |= 4u;
goto LABEL_14;
case '5':
buf |= 5u;
goto LABEL_14;
case '6':
buf |= 6u;
goto LABEL_14;
case '7':
buf |= 7u;
goto LABEL_14;
case '8':
buf |= 8u;
goto LABEL_14;
case '9':
buf |= 9u;
goto LABEL_14;
case 'A':
case 'a':
buf |= 0xAu;
goto LABEL_14;
case 'B':
case 'b':
buf |= 0xBu;
goto LABEL_14;
case 'C':
case 'c':
buf |= 0xCu;
goto LABEL_14;
case 'D':
case 'd':
buf |= 0xDu;
goto LABEL_14;
case 'E':
case 'e':
buf |= 0xEu;
goto LABEL_14;
case 'F':
case 'f':
buf |= 0xFu;
LABEL_14:
switch ( s[0] )
{
case '0':
goto LABEL_17;
case '1':
buf |= 0x10u;
goto LABEL_17;
case '2':
buf |= 0x20u;
goto LABEL_17;
case '3':
buf |= 0x30u;
goto LABEL_17;
case '4':
buf |= 0x40u;
goto LABEL_17;
case '5':
buf |= 0x50u;
goto LABEL_17;
case '6':
buf |= 0x60u;
goto LABEL_17;
case '7':
buf |= 0x70u;
goto LABEL_17;
case '8':
buf |= 0x80u;
goto LABEL_17;
case '9':
buf |= 0x90u;
goto LABEL_17;
case 'A':
case 'a':
buf |= 0xA0u;
goto LABEL_17;
case 'B':
case 'b':
buf |= 0xB0u;
goto LABEL_17;
case 'C':
case 'c':
buf |= 0xC0u;
goto LABEL_17;
case 'D':
case 'd':
buf |= 0xD0u;
goto LABEL_17;
case 'E':
case 'e':
buf |= 0xE0u;
goto LABEL_17;
case 'F':
case 'f':
buf |= 0xF0u;
goto LABEL_17;
case 'q':
goto LABEL_31;
default:
return 1LL;
}
case 'q':
LABEL_31:
result = 0xFFFFFFFFLL;
break;
default:
switch ( s[0] )
{
case '0':
break;
case '1':
buf |= 0x10u;
break;
case '2':
buf |= 0x20u;
break;
case '3':
buf |= 0x30u;
break;
case '4':
buf |= 0x40u;
break;
case '5':
buf |= 0x50u;
break;
case '6':
buf |= 0x60u;
break;
case '7':
buf |= 0x70u;
break;
case '8':
buf |= 0x80u;
break;
case '9':
buf |= 0x90u;
break;
case 'A':
case 'a':
buf |= 0xA0u;
break;
case 'B':
case 'b':
buf |= 0xB0u;
break;
case 'C':
case 'c':
buf |= 0xC0u;
break;
case 'D':
case 'd':
buf |= 0xD0u;
break;
case 'E':
case 'e':
buf |= 0xE0u;
break;
case 'F':
case 'f':
buf |= 0xF0u;
break;
case 'q':
goto LABEL_31;
default:
return 1LL;
}
buf >>= 4;
LABEL_17:
v3 = *(&dev + v1);
*(_DWORD *)&s[16] = 0;
*(_OWORD *)s = 0LL;
write(v3, &buf, 2uLL);
result = 0LL;
break;
}
break;
case 'q':
result = 4294967293LL;
break;
default:
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
char v0; // bp
__int64 v1; // rbx
__int64 result; // rax
int v3; // edi
unsigned __int8 buf; // [rsp+Eh] [rbp-3Ah] BYREF
char v5; // [rsp+Fh] [rbp-39h]
_BYTE s[40]; // [rsp+10h] [rbp-38h] BYREF

fwrite("input group (0-4) > ", 1uLL, 0x14uLL, stdout);
fflush(stdout);
fgets(s, 20, stdin);
v0 = s[0];
if ( s[0] == 113 )
return 4294967287LL;
v1 = atoi(s);
if ( v0 != 48 && !(_DWORD)v1 )
return 1LL;
*(_DWORD *)&s[16] = 0;
*(_OWORD *)s = 0LL;
if ( (unsigned int)v1 > 4 )
return 1LL;
fwrite("input port (a,b,c,d) > ", 1uLL, 0x18uLL, stdout);
fflush(stdout);
fgets(s, 20, stdin);
s[19] = 0;
*(_WORD *)&s[17] = 0;
*(_OWORD *)&s[1] = 0LL;
switch ( s[0] )
{
case 'a':
v5 = 0;
goto LABEL_9;
case 'b':
v5 = 1;
goto LABEL_9;
case 'c':
v5 = 2;
goto LABEL_9;
case 'd':
v5 = 18;
LABEL_9:
fwrite("input hex data > ", 1uLL, 0x11uLL, stdout);
fflush(stdout);
buf = 0;
fgets(s, 20, stdin);
switch ( s[1] )
{
case '0':
goto LABEL_14;
case '1':
buf |= 1u;
goto LABEL_14;
case '2':
buf |= 2u;
goto LABEL_14;
case '3':
buf |= 3u;
goto LABEL_14;
case '4':
buf |= 4u;
goto LABEL_14;
case '5':
buf |= 5u;
goto LABEL_14;
case '6':
buf |= 6u;
goto LABEL_14;
case '7':
buf |= 7u;
goto LABEL_14;
case '8':
buf |= 8u;
goto LABEL_14;
case '9':
buf |= 9u;
goto LABEL_14;
case 'A':
case 'a':
buf |= 0xAu;
goto LABEL_14;
case 'B':
case 'b':
buf |= 0xBu;
goto LABEL_14;
case 'C':
case 'c':
buf |= 0xCu;
goto LABEL_14;
case 'D':
case 'd':
buf |= 0xDu;
goto LABEL_14;
case 'E':
case 'e':
buf |= 0xEu;
goto LABEL_14;
case 'F':
case 'f':
buf |= 0xFu;
LABEL_14:
switch ( s[0] )
{
case '0':
goto LABEL_17;
case '1':
buf |= 0x10u;
goto LABEL_17;
case '2':
buf |= 0x20u;
goto LABEL_17;
case '3':
buf |= 0x30u;
goto LABEL_17;
case '4':
buf |= 0x40u;
goto LABEL_17;
case '5':
buf |= 0x50u;
goto LABEL_17;
case '6':
buf |= 0x60u;
goto LABEL_17;
case '7':
buf |= 0x70u;
goto LABEL_17;
case '8':
buf |= 0x80u;
goto LABEL_17;
case '9':
buf |= 0x90u;
goto LABEL_17;
case 'A':
case 'a':
buf |= 0xA0u;
goto LABEL_17;
case 'B':
case 'b':
buf |= 0xB0u;
goto LABEL_17;
case 'C':
case 'c':
buf |= 0xC0u;
goto LABEL_17;
case 'D':
case 'd':
buf |= 0xD0u;
goto LABEL_17;
case 'E':
case 'e':
buf |= 0xE0u;
goto LABEL_17;
case 'F':
case 'f':
buf |= 0xF0u;
goto LABEL_17;
case 'q':
goto LABEL_31;
default:
return 1LL;
}
case 'q':
LABEL_31:
result = 0xFFFFFFFFLL;
break;
default:
switch ( s[0] )
{
case '0':
break;
case '1':
buf |= 0x10u;
break;
case '2':
buf |= 0x20u;
break;
case '3':
buf |= 0x30u;
break;
case '4':
buf |= 0x40u;
break;
case '5':
buf |= 0x50u;
break;
case '6':
buf |= 0x60u;
break;
case '7':
buf |= 0x70u;
break;
case '8':
buf |= 0x80u;
break;
case '9':
buf |= 0x90u;
break;
case 'A':
case 'a':
buf |= 0xA0u;
break;
case 'B':
case 'b':
buf |= 0xB0u;
break;
case 'C':
case 'c':
buf |= 0xC0u;
break;
case 'D':
case 'd':
buf |= 0xD0u;
break;
case 'E':
case 'e':
buf |= 0xE0u;
break;
case 'F':
case 'f':
buf |= 0xF0u;
break;
case 'q':
goto LABEL_31;
default:
return 1LL;
}
buf >>= 4;
LABEL_17:
v3 = *(&dev + v1);
*(_DWORD *)&s[16] = 0;
*(_OWORD *)s = 0LL;
write(v3, &buf, 2uLL);
result = 0LL;
break;
}
break;
case 'q':
result = 4294967293LL;
break;
default:
return 1LL;
}
return result;
}

original_code:
int get_input(void) {
  int grp, i, single = 0;
  unsigned char temp[2];
  char in[20];
  fputs("input group (0-4) > ", stdout);
  fflush(stdout);
  fgets(in, 20, stdin);
  if (in[0] == 'q')
    return -9;
  grp = atoi(&in[0]);
  if (!grp && in[0] != '0')
    return 1;
  memset(in, '\0', 20);
  if (grp < 0 || grp > 4)
    return 1;
  fputs("input port (a,b,c,d)  > ", stdout);
  fflush(stdout);
  fgets(in, 20, stdin);
  memset(&in[1], '\0', 19);
  switch(in[0]) {
  case 'a':
    temp[1] = 0x00;
    break;
  case 'b':
    temp[1] = 0x01;
    break;
  case 'c':
    temp[1] = 0x02;
    break;
  case 'd':
    temp[1] = 0x12;
    break;
  case 'q':
    return -3;
  default:
    return 1;
  }
  fputs("input hex data > ", stdout);
  fflush(stdout);
  temp[0] = 0x00;
  fgets(in, 20, stdin);
  for (i = 1; i >= 0; i--) {
    switch (in[i]) {
    case '0':
      temp[0] |= (0x00 >> i*4);
      break;
    case '1':
      temp[0] |= (0x10 >> i*4);
      break;
    case '2':
      temp[0] |= (0x20 >> i*4);
      break;
    case '3':
      temp[0] |= (0x30 >> i*4);
      break;
    case '4':
      temp[0] |= (0x40 >> i*4);
      break;
    case '5':
      temp[0] |= (0x50 >> i*4);
      break;
    case '6':
      temp[0] |= (0x60 >> i*4);
      break;
    case '7':
      temp[0] |= (0x70 >> i*4);
      break;
    case '8':
      temp[0] |= (0x80 >> i*4);
      break;
    case '9':
      temp[0] |= (0x90 >> i*4);
      break;
    case 'A':
    case 'a':
      temp[0] |= (0xa0 >> i*4);
      break;
    case 'B':
    case 'b':
      temp[0] |= (0xb0 >> i*4);
      break;
    case 'C':
    case 'c':
      temp[0] |= (0xc0 >> i*4);
      break;
    case 'D':
    case 'd':
      temp[0] |= (0xd0 >> i*4);
      break;
    case 'E':
    case 'e':
      temp[0] |= (0xe0 >> i*4);
      break;
    case 'F':
    case 'f':
      temp[0] |= (0xf0 >> i*4);
      break;
    case 'q':
      return -1;
    default:
      if (i) {
 single = 1;
 continue;
      }
      return 1;
    }
    if (single)
      temp[0] >>= 4;
  }
  memset(in, '\0', 20);
  write(dev[grp], temp, 2);
  return 0;
}

canonical_original_code:
int get_input() {
  int grp; int i; int single = 0;
  unsigned char temp[2];
  char in[20];
  fputs("input group (0-4) > ", stdout);
  fflush(stdout);
  fgets(in, 20, stdin);
  if (in[0] == 'q')
    return -9;
  grp = atoi(&in[0]);
  if (!grp && in[0] != '0')
    return 1;
  memset(in, '\0', 20);
  if (grp < 0 || grp > 4)
    return 1;
  fputs("input port (a,b,c,d)  > ", stdout);
  fflush(stdout);
  fgets(in, 20, stdin);
  memset(&in[1], '\0', 19);
  switch(in[0]) {
  case 'a':
    temp[1] = 0x00;
    break;
  case 'b':
    temp[1] = 0x01;
    break;
  case 'c':
    temp[1] = 0x02;
    break;
  case 'd':
    temp[1] = 0x12;
    break;
  case 'q':
    return -3;
  default:
    return 1;
  }
  fputs("input hex data > ", stdout);
  fflush(stdout);
  temp[0] = 0x00;
  fgets(in, 20, stdin);
  for (i = 1; i >= 0; i--) {
    switch (in[i]) {
    case '0':
      temp[0] |= (0x00 >> i*4);
      break;
    case '1':
      temp[0] |= (0x10 >> i*4);
      break;
    case '2':
      temp[0] |= (0x20 >> i*4);
      break;
    case '3':
      temp[0] |= (0x30 >> i*4);
      break;
    case '4':
      temp[0] |= (0x40 >> i*4);
      break;
    case '5':
      temp[0] |= (0x50 >> i*4);
      break;
    case '6':
      temp[0] |= (0x60 >> i*4);
      break;
    case '7':
      temp[0] |= (0x70 >> i*4);
      break;
    case '8':
      temp[0] |= (0x80 >> i*4);
      break;
    case '9':
      temp[0] |= (0x90 >> i*4);
      break;
    case 'A':
    case 'a':
      temp[0] |= (0xa0 >> i*4);
      break;
    case 'B':
    case 'b':
      temp[0] |= (0xb0 >> i*4);
      break;
    case 'C':
    case 'c':
      temp[0] |= (0xc0 >> i*4);
      break;
    case 'D':
    case 'd':
      temp[0] |= (0xd0 >> i*4);
      break;
    case 'E':
    case 'e':
      temp[0] |= (0xe0 >> i*4);
      break;
    case 'F':
    case 'f':
      temp[0] |= (0xf0 >> i*4);
      break;
    case 'q':
      return -1;
    default:
      if (i) {
 single = 1;
 continue;
      }
      return 1;
    }
    if (single)
      temp[0] >>= 4;
  }
  memset(in, '\0', 20);
  write(dev[grp], temp, 2);
  return 0;
}

variable_types: grp int

variable_types: single int

variable_types:
temp
unsigned char [2]

variable_types:
in
char [20]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
__int64 v4; // rbx
__int64 v5; // rbx
int v6; // edi
__int64 i; // rbx
int v8; // edi
__int16 buf[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = 0;
v4 = 0LL;
dev = open("/dev/dio0", 2, envp);
dword_404094 = open("/dev/dio1", 2);
dword_404098 = open("/dev/dio2", 2);
dword_40409C = open("/dev/dio3", 2);
dword_4040A0 = open("/dev/dio4", 2);
do
{
if ( !*(&dev + v4) )
{
v3 = 1;
printf("Well that went badly for dev %d\n", (unsigned int)v4);
}
++v4;
}
while ( v4 != 5 );
if ( v3 )
return 2;
v5 = 0LL;
buf[0] = 896;
do
{
v6 = *(&dev + v5++);
write(v6, buf, 2uLL);
}
while ( v5 != 5 );
while ( (int)get_input() >= 0 )
;
for ( i = 0LL; i != 5; ++i )
{
v8 = *(&dev + i);
close(v8);
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
__int64 v4; // rbx
__int64 v5; // rbx
int v6; // edi
__int64 i; // rbx
int v8; // edi
__int16 buf[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v3 = 0;
v4 = 0LL;
dev = open("/dev/dio0", 2, envp);
dword_404094 = open("/dev/dio1", 2);
dword_404098 = open("/dev/dio2", 2);
dword_40409C = open("/dev/dio3", 2);
dword_4040A0 = open("/dev/dio4", 2);
do
{
if ( !*(&dev + v4) )
{
v3 = 1;
printf("Well that went badly for dev %d\n", (unsigned int)v4);
}
++v4;
}
while ( v4 != 5 );
if ( v3 )
return 2;
v5 = 0LL;
buf[0] = 896;
do
{
v6 = *(&dev + v5++);
write(v6, buf, 2uLL);
}
while ( v5 != 5 );
while ( (int)func7() >= 0 )
;
for ( i = 0LL; i != 5; ++i )
{
v8 = *(&dev + i);
close(v8);
}
return v3;
}

original_code:
int main(void) {
  int grp, bad = 0;
  unsigned char write_msg[2];
  dev[0] = open("/dev/dio0", 02);
  dev[1] = open("/dev/dio1", 02);
  dev[2] = open("/dev/dio2", 02);
  dev[3] = open("/dev/dio3", 02);
  dev[4] = open("/dev/dio4", 02);
  for(grp = 0; grp < 5; grp++) {
    if (!dev[grp]) {
      printf("Well that went badly for dev %d\n", grp);
      bad = 1;
    }
  }
  if (bad)
    return 2;
  write_msg[1] = 0x03;
  write_msg[0] = 0x80;
  for (grp = 0; grp < 5; grp++)
    write(dev[grp], write_msg, 2);
  while (1) {
    if (get_input() < 0)
      break;
  }
  for(grp = 0; grp < 5; grp++)
    close(dev[grp]);
  return 0;
}

canonical_original_code:
int main() {
  int grp; int bad = 0;
  unsigned char write_msg[2];
  dev[0] = open("/dev/dio0", 02);
  dev[1] = open("/dev/dio1", 02);
  dev[2] = open("/dev/dio2", 02);
  dev[3] = open("/dev/dio3", 02);
  dev[4] = open("/dev/dio4", 02);
  for(grp = 0; grp < 5; grp++) {
    if (!dev[grp]) {
      printf("Well that went badly for dev %d\n", grp);
      bad = 1;
    }
  }
  if (bad)
    return 2;
  write_msg[1] = 0x03;
  write_msg[0] = 0x80;
  for (grp = 0; grp < 5; grp++)
    write(dev[grp], write_msg, 2);
  while (1) {
    if (get_input() < 0)
      break;
  }
  for(grp = 0; grp < 5; grp++)
    close(dev[grp]);
  return 0;
}

variable_types: bad int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE *v3; // rax
_BYTE *v4; // rbp
int v5; // ecx
char v6; // di
char v7; // si
int v8; // eax
__int64 v9; // rdx
_BYTE *v10; // rbx
_BYTE *v11; // r13
FILE *v12; // r12
int v13; // er13
int v14; // er12
int v15; // ebx
int v16; // er15
int v17; // er14
int v18; // eax
char buf; // [rsp+Ah] [rbp-3Eh] BYREF
char v21; // [rsp+Bh] [rbp-3Dh] BYREF
int v22[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v21 = 6;
v3 = malloc(0x4000uLL);
if ( !v3 )
{
puts("ERROR MALLOCING");
exit(2);
}
v4 = v3;
v5 = 16380;
v6 = -1;
v7 = 0;
while ( 1 )
{
v8 = v5 + 4;
do
{
v9 = (unsigned int)(v8 - 1);
v4[v9] = v7;
--v8;
}
while ( (_DWORD)v9 != v5 );
--v6;
if ( !(_DWORD)v9 )
break;
v7 = 0;
if ( (v6 & 1) == 0 )
v7 = ((v6 & 3) == 0) - 2;
v5 -= 4;
}
v10 = v4;
v11 = v4 + 0x4000;
v12 = fopen("first_test_results.txt", "w");
do
{
while ( !*v10 )
{
++v10;
fwrite("00", 1uLL, 2uLL, v12);
if ( v11 == v10 )
goto LABEL_15;
}
if ( *v10 == 0xFF )
fwrite("ff", 1uLL, 2uLL, v12);
else
fwrite("??", 1uLL, 2uLL, v12);
++v10;
}
while ( v11 != v10 );
LABEL_15:
fclose(v12);
v13 = open("/dev/timing5", 1);
v14 = open("/dev/timing1", 2);
v15 = open("/dev/timing12", 1);
v16 = open("/dev/timing11", 1);
v17 = open("/dev/timing9", 1);
if ( v14 <= 0 || v13 <= 0 )
{
puts("Couldn't open do_csr");
exit(3);
}
buf = 84;
write(v16, &buf, 1uLL);
write(v17, &v21, 1uLL);
v22[0] = 1621;
write(v14, v22, 4uLL);
write(v13, v4, 0x4000uLL);
v18 = v22[0];
BYTE1(v18) = BYTE1(v22[0]) & 0xFD | 1;
v22[0] = v18;
write(v14, v22, 4uLL);
close(v14);
close(v13);
close(v16);
close(v17);
close(v15);
free(v4);
return 0;
}

original_code:
int main(void) {
  int DO_CSR;
  __u32 cmd;
  DO_CSR = open("/dev/timing1", 02);
  if ( DO_CSR < 1 ) {
    printf("Couldn't open do_csr\n");
    exit(3);
  }
  cmd = 0x00000601;
  cmd |= 0x00000001;
  cmd = (cmd & 0xfffffff9) | 0x00000004;
  cmd &= 0xfffffeff;
  cmd |= 0x00000200;
  write(DO_CSR, &cmd, sizeof(__u32));
  close(DO_CSR);
  return 0;
}

canonical_original_code:
int main() {
  int DO_CSR;
  unsigned int cmd;
  DO_CSR = open("/dev/timing1", 02);
  if ( DO_CSR < 1 ) {
    printf("Couldn't open do_csr\n");
    exit(3);
  }
  cmd = 0x00000601;
  cmd |= 0x00000001;
  cmd = (cmd & 0xfffffff9) | 0x00000004;
  cmd &= 0xfffffeff;
  cmd |= 0x00000200;
  write(DO_CSR, &cmd, sizeof(__u32));
  close(DO_CSR);
  return 0;
}

variable_types:
cmd
unsigned int


repo: schaars/simple-web-server

decompiled_code:
void __fastcall create_file_manager(__int64 a1)
{
webdir = a1;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
webdir = a1;
}

original_code:
void create_file_manager(char *web_dir) {
   webdir = web_dir;
}


decompiled_code:
void delete_file_manager()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void delete_file_manager() {
}

canonical_original_code:
void delete_file_manager() {
}


decompiled_code:
void *__fastcall file_manager_get(const char *a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rdi
int v5; // eax
int v6; // er12
void *v7; // r13
void *result; // rax
int *v9; // rax
char *v10; // rax
char *ptr; // [rsp+8h] [rbp-B8h] BYREF
struct stat v12; // [rsp+10h] [rbp-B0h] BYREF

asprintf(&ptr, "%s%s", (const char *)webdir, a1);
if ( stat(ptr, &v12) < 0 )
{
*a2 = 404;
free(ptr);
result = 0LL;
}
else
{
v4 = ptr;
*a3 = v12.st_size;
v5 = open(v4, 0);
v6 = v5;
if ( v5 == -1 )
{
*a2 = 401;
v7 = 0LL;
free(ptr);
}
else
{
v7 = mmap(0LL, v12.st_size, 1, 1, v5, 0LL);
if ( v7 == (void *)-1LL )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(
stderr,
"%c[%d;%dm(%s,%d) mmap %s size %ld fail: %s\n%c[%d;%dm\n",
27LL,
0LL,
31LL,
"file_manager_get",
61LL,
ptr,
v12.st_size,
v10,
27LL,
0LL,
37LL);
exit(-1);
}
*a2 = 200;
close(v6);
free(ptr);
}
result = v7;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(const char *a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rdi
int v5; // eax
int v6; // er12
void *v7; // r13
void *result; // rax
int *v9; // rax
char *v10; // rax
char *ptr; // [rsp+8h] [rbp-B8h] BYREF
struct stat v12; // [rsp+10h] [rbp-B0h] BYREF

asprintf(&ptr, "%s%s", (const char *)webdir, a1);
if ( stat(ptr, &v12) < 0 )
{
*a2 = 404;
free(ptr);
result = 0LL;
}
else
{
v4 = ptr;
*a3 = v12.st_size;
v5 = open(v4, 0);
v6 = v5;
if ( v5 == -1 )
{
*a2 = 401;
v7 = 0LL;
free(ptr);
}
else
{
v7 = mmap(0LL, v12.st_size, 1, 1, v5, 0LL);
if ( v7 == (void *)-1LL )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(
stderr,
"%c[%d;%dm(%s,%d) mmap %s size %ld fail: %s\n%c[%d;%dm\n",
27LL,
0LL,
31LL,
"file_manager_get",
61LL,
ptr,
v12.st_size,
v10,
27LL,
0LL,
37LL);
exit(-1);
}
*a2 = 200;
close(v6);
free(ptr);
}
result = v7;
}
return result;
}

original_code:
char* file_manager_get(const char *file, int *status, int *length) {
   char *path;
   asprintf(&path, "%s%s", webdir, file);
   struct stat st;
   if (stat(path, &st) < 0) {
      *status = 404;
      free(path);
      return ((void *)0);
   }
   *length = st.st_size;
   int fd;
   if ((fd = open(path, 00)) == -1)
   {
      *status = 401;
      free(path);
      return ((void *)0);
   }
   char *c = (char*) mmap(0, st.st_size, 0x1, 0x01, fd, 0);
   if (c == ((void *) -1))
   {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "mmap %s size %ld fail: %s\n" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 61, path, (long) st.st_size, strerror((*__errno_location ())), 0x1B, 0, 37); exit(-1); } while(0);
   }
   *status = 200;
   close(fd);
   free(path);
   return c;
}

canonical_original_code:
char *file_manager_get(char *file, int *status, int *length) {
   char *path;
   asprintf(&path, "%s%s", webdir, file);
   struct stat st;
   if (stat(path, &st) < 0) {
      *status = 404;
      free(path);
      return ((void *)0);
   }
   *length = st.st_size;
   int fd;
   if ((fd = open(path, 00)) == -1)
   {
      *status = 401;
      free(path);
      return ((void *)0);
   }
   char *c = (char*) mmap(0, st.st_size, 0x1, 0x01, fd, 0);
   if (c == ((void *) -1))
   {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "mmap %s size %ld fail: %s\n" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 61, path, (long) st.st_size, strerror((*__errno_location ())), 0x1B, 0, 37); exit(-1); } while(0);
   }
   *status = 200;
   close(fd);
   free(path);
   return c;
}

variable_types:
file
char *

variable_types:
status
int *

variable_types:
length
int *

variable_types:
path
char *

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

variable_types: fd int

variable_types:
c
char *

return_type: char *

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


decompiled_code:
int __fastcall file_manager_release(void *a1, int a2)
{
return munmap(a1, a2);
}

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

original_code:
void file_manager_release(char *c, const int length) {
   munmap(c, length);
}

canonical_original_code:
void file_manager_release(char *c, int length) {
   munmap(c, length);
}

variable_types: length int


decompiled_code:
__int64 __fastcall file_manager_get_sendfile(const char *a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rdi
unsigned int v5; // er12
__int64 result; // rax
char *ptr; // [rsp+8h] [rbp-A8h] BYREF
struct stat v8; // [rsp+10h] [rbp-A0h] BYREF

asprintf(&ptr, "%s%s", (const char *)webdir, a1);
if ( stat(ptr, &v8) < 0 )
{
*a2 = 404;
free(ptr);
result = 0LL;
}
else
{
v4 = ptr;
*a3 = v8.st_size;
v5 = open(v4, 0);
if ( v5 == -1 )
{
*a2 = 401;
free(ptr);
result = 0LL;
}
else
{
free(ptr);
result = v5;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rdi
unsigned int v5; // er12
__int64 result; // rax
char *ptr; // [rsp+8h] [rbp-A8h] BYREF
struct stat v8; // [rsp+10h] [rbp-A0h] BYREF

asprintf(&ptr, "%s%s", (const char *)webdir, a1);
if ( stat(ptr, &v8) < 0 )
{
*a2 = 404;
free(ptr);
result = 0LL;
}
else
{
v4 = ptr;
*a3 = v8.st_size;
v5 = open(v4, 0);
if ( v5 == -1 )
{
*a2 = 401;
free(ptr);
result = 0LL;
}
else
{
free(ptr);
result = v5;
}
}
return result;
}

original_code:
int file_manager_get_sendfile(const char *file, int *status, int *length) {
   char *path;
   asprintf(&path, "%s%s", webdir, file);
   struct stat st;
   if (stat(path, &st) < 0) {
      *status = 404;
      free(path);
      return 0;
   }
   *length = st.st_size;
   int fd;
   if ((fd = open(path, 00)) == -1)
   {
      *status = 401;
      free(path);
      return 0;
   }
   free(path);
   return fd;
}

canonical_original_code:
int file_manager_get_sendfile(char *file, int *status, int *length) {
   char *path;
   asprintf(&path, "%s%s", webdir, file);
   struct stat st;
   if (stat(path, &st) < 0) {
      *status = 404;
      free(path);
      return 0;
   }
   *length = st.st_size;
   int fd;
   if ((fd = open(path, 00)) == -1)
   {
      *status = 401;
      free(path);
      return 0;
   }
   free(path);
   return fd;
}

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


decompiled_code:
// attributes: thunk
int file_manager_release_sendfile(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func5(int fd)
{
return close(fd);
}

original_code:
void file_manager_release_sendfile(int fd) {
   close(fd);
}


decompiled_code:
__int64 __fastcall file_manager_get_filetype(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(a2, "image/gif");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(a2, "image/jpeg");
}
else
{
strcpy(a2, "text/plain");
result = 28265LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(a2, "image/gif");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(a2, "image/jpeg");
}
else
{
strcpy(a2, "text/plain");
result = 28265LL;
}
return result;
}

original_code:
void file_manager_get_filetype(const char *filename, char *filetype)
{
   if (strstr(filename, ".html")) {
      strcpy(filetype, "text/html");
   } else if (strstr(filename, ".gif")) {
      strcpy(filetype, "image/gif");
   } else if (strstr(filename, ".jpg")) {
      strcpy(filetype, "image/jpeg");
   } else {
      strcpy(filetype, "text/plain");
   }
}

canonical_original_code:
void file_manager_get_filetype(char *filename, char *filetype)
{
   if (strstr(filename, ".html")) {
      strcpy(filetype, "text/html");
   } else if (strstr(filename, ".gif")) {
      strcpy(filetype, "image/gif");
   } else if (strstr(filename, ".jpg")) {
      strcpy(filetype, "image/jpeg");
   } else {
      strcpy(filetype, "text/plain");
   }
}

variable_types:
filename
char *

variable_types:
filetype
char *


decompiled_code:
void __fastcall handle_signal(int a1)
{
if ( a1 == 2 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Exit on signal %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"handle_signal",
19LL,
2LL,
27LL,
0LL,
37LL);
exit(-1);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 2 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Exit on signal %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"handle_signal",
19LL,
2LL,
27LL,
0LL,
37LL);
exit(-1);
}
}

original_code:
void handle_signal(int sig) {
   if (sig == 2) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Exit on signal %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 19, sig, 0x1B, 0, 37); exit(-1); } while(0);
   }
}

variable_types: sig int


decompiled_code:
__int64 __fastcall accept_connections(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int optval; // [rsp+Ch] [rbp-24h] BYREF
struct sockaddr v6; // [rsp+10h] [rbp-20h] BYREF

v1 = socket(2, 1, 0);
if ( v1 == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Error while creating the socket%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
26LL,
27LL,
0LL,
37LL);
goto LABEL_8;
}
v2 = v1;
optval = 1;
if ( setsockopt(v1, 6, 1, &optval, 4u) == -1 )
perror("Cannot set TCP NO DELAY:");
*(_DWORD *)&v6.sa_data[2] = _bswap_32_constprop_0();
v6.sa_family = 2;
*(_WORD *)v6.sa_data = _bswap_16(a1);
if ( bind(v2, &v6, v3) == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Cannot bind socket!%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
39LL,
27LL,
0LL,
37LL);
LABEL_8:
exit(-1);
}
if ( listen(v2, 100) == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Cannot listen on socket!%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
43LL,
27LL,
0LL,
37LL);
goto LABEL_8;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int optval; // [rsp+Ch] [rbp-24h] BYREF
struct sockaddr v6; // [rsp+10h] [rbp-20h] BYREF

v1 = socket(2, 1, 0);
if ( v1 == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Error while creating the socket%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
26LL,
27LL,
0LL,
37LL);
goto LABEL_8;
}
v2 = v1;
optval = 1;
if ( setsockopt(v1, 6, 1, &optval, 4u) == -1 )
perror("Cannot set TCP NO DELAY:");
*(_DWORD *)&v6.sa_data[2] = func2();
v6.sa_family = 2;
*(_WORD *)v6.sa_data = func1(a1);
if ( bind(v2, &v6, v3) == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Cannot bind socket!%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
39LL,
27LL,
0LL,
37LL);
LABEL_8:
exit(-1);
}
if ( listen(v2, 100) == -1 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Cannot listen on socket!%c[%d;%dm\n",
27LL,
0LL,
31LL,
"accept_connections",
43LL,
27LL,
0LL,
37LL);
goto LABEL_8;
}
return v2;
}

original_code:
int accept_connections(const int port) {
   int bootstrap = socket(2, SOCK_STREAM, 0);
   if (bootstrap == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Error while creating the socket" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 26, 0x1B, 0, 37); exit(-1); } while(0);
   }
   int flag = 1;
   if (setsockopt(bootstrap, IPPROTO_TCP, 1, (char*)&flag, sizeof(int)) == -1) {
      perror("Cannot set TCP NO DELAY:");
   }
   struct sockaddr_in bootstrap_sin;
   bootstrap_sin.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
   bootstrap_sin.sin_family = 2;
   bootstrap_sin.sin_port = htons(port);
   if (bind(bootstrap, (struct sockaddr*)&bootstrap_sin, sizeof(bootstrap_sin)) == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Cannot bind socket!" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 39, 0x1B, 0, 37); exit(-1); } while(0);
   }
   if (listen(bootstrap, 100) == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Cannot listen on socket!" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 43, 0x1B, 0, 37); exit(-1); } while(0);
   }
   ;
   return bootstrap;
}

canonical_original_code:
int accept_connections(int port) {
   int bootstrap = socket(2, SOCK_STREAM, 0);
   if (bootstrap == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Error while creating the socket" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 26, 0x1B, 0, 37); exit(-1); } while(0);
   }
   int flag = 1;
   if (setsockopt(bootstrap, IPPROTO_TCP, 1, (char *)&flag, sizeof(int)) == -1) {
      perror("Cannot set TCP NO DELAY:");
   }
   struct sockaddr_in bootstrap_sin;
   bootstrap_sin.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
   bootstrap_sin.sin_family = 2;
   bootstrap_sin.sin_port = htons(port);
   if (bind(bootstrap, (struct sockaddr *)&bootstrap_sin, sizeof(bootstrap_sin)) == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Cannot bind socket!" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 39, 0x1B, 0, 37); exit(-1); } while(0);
   }
   if (listen(bootstrap, 100) == -1) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Cannot listen on socket!" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 43, 0x1B, 0, 37); exit(-1); } while(0);
   }
   ;
   return bootstrap;
}

variable_types: port int

variable_types: bootstrap int

variable_types: flag 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]; }

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

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


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
int v4; // er14
unsigned int v5; // er13
int v6; // eax
unsigned int v7; // eax

v3 = 0LL;
v4 = -1;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "p:s:d:");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'p':
v4 = atoi(optarg);
break;
case 's':
v5 = atoi(optarg);
break;
case 'd':
v3 = strdup(optarg);
break;
default:
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Usage: %s -p port -s pool_size -d web_directory%c[%d;%dm\n",
27LL,
0LL,
31LL,
"main",
68LL,
*argv,
27LL,
0LL,
37LL);
goto LABEL_7;
}
}
if ( v5 != 0 && v4 != -1 && v3 )
{
signal(2, handle_signal);
create_file_manager(v3);
v7 = accept_connections(v4);
create_pool(v5, v7);
while ( 1 )
sleep(0xAu);
}
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Usage: %s -p port -s pool_size -d web_directory%c[%d;%dm\n",
27LL,
0LL,
31LL,
"main",
73LL,
*argv,
27LL,
0LL,
37LL);
LABEL_7:
exit(-1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
int v4; // er14
unsigned int v5; // er13
int v6; // eax
unsigned int v7; // eax

v3 = 0LL;
v4 = -1;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "p:s:d:");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'p':
v4 = atoi(optarg);
break;
case 's':
v5 = atoi(optarg);
break;
case 'd':
v3 = strdup(optarg);
break;
default:
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Usage: %s -p port -s pool_size -d web_directory%c[%d;%dm\n",
27LL,
0LL,
31LL,
"main",
68LL,
*argv,
27LL,
0LL,
37LL);
goto LABEL_7;
}
}
if ( v5 != 0 && v4 != -1 && v3 )
{
signal(2, handle_signal);
create_file_manager(v3);
v7 = func3(v4);
create_pool(v5, v7);
while ( 1 )
sleep(0xAu);
}
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Usage: %s -p port -s pool_size -d web_directory%c[%d;%dm\n",
27LL,
0LL,
31LL,
"main",
73LL,
*argv,
27LL,
0LL,
37LL);
LABEL_7:
exit(-1);
}

original_code:
int main(int argc, char **argv) {
   int port = -1;
   int pool_size = 0;
   char* web_dir = ((void *)0);
   int opt;
   while ((opt = getopt(argc, argv, "p:s:d:")) != (-1)) {
      switch (opt) {
         case 'p':
            port = atoi(optarg);
            break;
         case 's':
            pool_size = atoi(optarg);
            break;
         case 'd':
            web_dir = strdup(optarg);
            break;
         default:
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Usage: %s -p port -s pool_size -d web_directory" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 68, argv[0], 0x1B, 0, 37); exit(-1); } while(0);
      }
   }
   if (port == -1 || pool_size == 0 || web_dir == ((void *)0)) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Usage: %s -p port -s pool_size -d web_directory" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 73, argv[0], 0x1B, 0, 37); exit(-1); } while(0);
   }
   signal(2, handle_signal);
   create_file_manager(web_dir);
   int bootstrap_s = accept_connections(port);
   create_pool(pool_size, bootstrap_s);
   while (1) {
      sleep(10);
   }
   delete_pool();
   delete_file_manager();
   free(web_dir);
   return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
   int port = -1;
   int pool_size = 0;
   char *web_dir = ((void *)0);
   int opt;
   while ((opt = getopt(argc, argv, "p:s:d:")) != (-1)) {
      switch (opt) {
         case 'p':
            port = atoi(optarg);
            break;
         case 's':
            pool_size = atoi(optarg);
            break;
         case 'd':
            web_dir = strdup(optarg);
            break;
         default:
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Usage: %s -p port -s pool_size -d web_directory" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 68, argv[0], 0x1B, 0, 37); exit(-1); } while(0);
      }
   }
   if (port == -1 || pool_size == 0 || web_dir == ((void *)0)) {
      do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Usage: %s -p port -s pool_size -d web_directory" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 73, argv[0], 0x1B, 0, 37); exit(-1); } while(0);
   }
   signal(2, handle_signal);
   create_file_manager(web_dir);
   int bootstrap_s = accept_connections(port);
   create_pool(pool_size, bootstrap_s);
   while (1) {
      sleep(10);
   }
   delete_pool();
   delete_file_manager();
   free(web_dir);
   return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int


decompiled_code:
__int64 __fastcall read_request(int fd, __int64 a2, int a3)
{
int v3; // er15
int v5; // eax
int v6; // ebx
__int64 result; // rax
_BYTE *v8; // [rsp+8h] [rbp-38h]

v3 = 0;
v8 = (_BYTE *)(a2 + a3 - 1);
if ( a3 > 0 )
{
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v3), a3 - v3);
v6 = v5;
*v8 = 0;
if ( v5 == -1 )
{
perror("Read error: ");
return 0xFFFFFFFFLL;
}
if ( !v5 )
return 1LL;
if ( strchr((const char *)(a2 + v3), 10) )
break;
v3 += v6;
if ( a3 <= v3 )
goto LABEL_8;
}
result = 0LL;
}
else
{
LABEL_8:
*v8 = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __int64 a2, int a3)
{
int v3; // er15
int v5; // eax
int v6; // ebx
__int64 result; // rax
_BYTE *v8; // [rsp+8h] [rbp-38h]

v3 = 0;
v8 = (_BYTE *)(a2 + a3 - 1);
if ( a3 > 0 )
{
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v3), a3 - v3);
v6 = v5;
*v8 = 0;
if ( v5 == -1 )
{
perror("Read error: ");
return 0xFFFFFFFFLL;
}
if ( !v5 )
return 1LL;
if ( strchr((const char *)(a2 + v3), 10) )
break;
v3 += v6;
if ( a3 <= v3 )
goto LABEL_8;
}
result = 0LL;
}
else
{
LABEL_8:
*v8 = 0;
result = 1LL;
}
return result;
}

original_code:
int read_request(const int s, char *req, const int length) {
   int pos = 0;
   while (pos < length) {
      int r = read(s, req+pos, length-pos);
      req[length-1] = '\0';
      if (r == -1) {
         perror("Read error: ");
         return -1;
      } else if (r == 0) {
         return 1;
      } else {
         if (strchr(req+pos, '\n')) {
            return 0;
         }
      }
      pos += r;
   }
   req[length-1] = '\0';
   return 1;
}

canonical_original_code:
int read_request(int s, char *req, int length) {
   int pos = 0;
   while (pos < length) {
      int r = read(s, req+pos, length-pos);
      req[length-1] = '\0';
      if (r == -1) {
         perror("Read error: ");
         return -1;
      } else if (r == 0) {
         return 1;
      } else {
         if (strchr(req+pos, '\n')) {
            return 0;
         }
      }
      pos += r;
   }
   req[length-1] = '\0';
   return 1;
}

variable_types: s int

variable_types:
req
char *

variable_types: pos int

variable_types: r int


decompiled_code:
void __fastcall write_complete(int fd, __int64 a2, int a3)
{
int v3; // er14
int v5; // eax

if ( a3 > 0 )
{
v3 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v3), a3 - v3);
if ( v5 == -1 )
break;
v3 += v5;
if ( a3 <= v3 )
return;
}
perror("Write error: ");
}
}

canonical_decompiled_code:
void __fastcall func1(int fd, __int64 a2, int a3)
{
int v3; // er14
int v5; // eax

if ( a3 > 0 )
{
v3 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v3), a3 - v3);
if ( v5 == -1 )
break;
v3 += v5;
if ( a3 <= v3 )
return;
}
perror("Write error: ");
}
}

original_code:
void write_complete(const int fd, const char *buf, const int length) {
   int written = 0;
   while (written < length) {
      int r = write(fd, buf+written, length-written);
      if (r == -1) {
         perror("Write error: ");
         return;
      } else {
         written += r;
      }
   }
}

canonical_original_code:
void write_complete(int fd, char *buf, int length) {
   int written = 0;
   while (written < length) {
      int r = write(fd, buf+written, length-written);
      if (r == -1) {
         perror("Write error: ");
         return;
      } else {
         written += r;
      }
   }
}

variable_types: fd int

variable_types:
buf
char *

variable_types: written int


clienterror

decompiled_code:
void __fastcall clienterror(int fd, const char *a2, const char *a3, const char *a4, const char *a5)
{
int v8; // eax
int v9; // eax
size_t v10; // rax
int v11; // eax
int v12; // eax
char v13[16]; // [rsp+0h] [rbp-2130h] BYREF
char v14[16]; // [rsp+10h] [rbp-2120h] BYREF
char s[16]; // [rsp+100h] [rbp-2030h] BYREF
__m128i v16; // [rsp+110h] [rbp-2020h]
char v17[16]; // [rsp+120h] [rbp-2010h] BYREF

strcpy(v17, "rror</title>");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC8);
v16 = _mm_load_si128((const __m128i *)&LC9);
sprintf(s, "%s\r\n", s);
sprintf(s, "%s%s: %s\r\n", s, a3, a4);
sprintf(s, "%s
%s: %s
\r\n", s, a5, a2);
sprintf(s, "%s
The Simple Web Server\r\n", s);
sprintf(v13, "HTTP/1.0 %s %s\r\n", a3, a4);
v8 = strlen(v13);
write_complete(fd, (__int64)v13, v8);
strcpy(v14, "xt/html\r\n");
*(__m128i *)v13 = _mm_load_si128((const __m128i *)&LC10);
v9 = strlen(v13);
write_complete(fd, (__int64)v13, v9);
v10 = strlen(s);
sprintf(v13, "Content-length: %d\r\n\r\n", v10);
v11 = strlen(v13);
write_complete(fd, (__int64)v13, v11);
v12 = strlen(s);
write_complete(fd, (__int64)s, v12);
}

canonical_decompiled_code:
void __fastcall func2(int fd, const char *a2, const char *a3, const char *a4, const char *a5)
{
int v8; // eax
int v9; // eax
size_t v10; // rax
int v11; // eax
int v12; // eax
char v13[16]; // [rsp+0h] [rbp-2130h] BYREF
char v14[16]; // [rsp+10h] [rbp-2120h] BYREF
char s[16]; // [rsp+100h] [rbp-2030h] BYREF
__m128i v16; // [rsp+110h] [rbp-2020h]
char v17[16]; // [rsp+120h] [rbp-2010h] BYREF

strcpy(v17, "rror</title>");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC8);
v16 = _mm_load_si128((const __m128i *)&LC9);
sprintf(s, "%s\r\n", s);
sprintf(s, "%s%s: %s\r\n", s, a3, a4);
sprintf(s, "%s
%s: %s
\r\n", s, a5, a2);
sprintf(s, "%s
The Simple Web Server\r\n", s);
sprintf(v13, "HTTP/1.0 %s %s\r\n", a3, a4);
v8 = strlen(v13);
func1(fd, (__int64)v13, v8);
strcpy(v14, "xt/html\r\n");
*(__m128i *)v13 = _mm_load_si128((const __m128i *)&LC10);
v9 = strlen(v13);
func1(fd, (__int64)v13, v9);
v10 = strlen(s);
sprintf(v13, "Content-length: %d\r\n\r\n", v10);
v11 = strlen(v13);
func1(fd, (__int64)v13, v11);
v12 = strlen(s);
func1(fd, (__int64)s, v12);
}

original_code:
void clienterror(const int fd, const char *cause, const char *errnum,
      const char *shortmsg, const char *longmsg)
{
   char buf[256], body[8192];
   sprintf(body, "<title>Simple Web Server Error</title>");
   sprintf(body, "%s\r\n", body);
   sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
   sprintf(body, "%s
%s: %s
\r\n", body, longmsg, cause);
   sprintf(body, "%s
The Simple Web Server\r\n", body);
   sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
   write_complete(fd, buf, strlen(buf));
   sprintf(buf, "Content-type: text/html\r\n");
   write_complete(fd, buf, strlen(buf));
   sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
   write_complete(fd, buf, strlen(buf));
   write_complete(fd, body, strlen(body));
}

canonical_original_code:
void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg)
{
   char buf[256]; char body[8192];
   sprintf(body, "<title>Simple Web Server Error</title>");
   sprintf(body, "%s\r\n", body);
   sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
   sprintf(body, "%s
%s: %s
\r\n", body, longmsg, cause);
   sprintf(body, "%s
The Simple Web Server\r\n", body);
   sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
   write_complete(fd, buf, strlen(buf));
   sprintf(buf, "Content-type: text/html\r\n");
   write_complete(fd, buf, strlen(buf));
   sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
   write_complete(fd, buf, strlen(buf));
   write_complete(fd, body, strlen(body));
}

variable_types:
cause
char *

variable_types:
errnum
char *

variable_types:
shortmsg
char *

variable_types:
longmsg
char *

variable_types:
buf
char [256]

variable_types:
body
char [8192]


clientsuccess

decompiled_code:
void __fastcall clientsuccess(int fd, __int64 a2, unsigned int a3)
{
int v4; // eax
char v5[256]; // [rsp+0h] [rbp-2110h] BYREF
char s[16]; // [rsp+100h] [rbp-2010h] BYREF
__int16 v7; // [rsp+110h] [rbp-2000h]

file_manager_get_filetype(a2, v5);
v7 = 10;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC14);
sprintf(s, "%sServer: Simple Web Server\r\n", s);
sprintf(s, "%sContent-length: %d\r\n", s, a3);
sprintf(s, "%sContent-type: %s\r\n\r\n", s, v5);
v4 = strlen(s);
write_complete(fd, (__int64)s, v4);
}

canonical_decompiled_code:
void __fastcall func3(int fd, __int64 a2, unsigned int a3)
{
int v4; // eax
char v5[256]; // [rsp+0h] [rbp-2110h] BYREF
char s[16]; // [rsp+100h] [rbp-2010h] BYREF
__int16 v7; // [rsp+110h] [rbp-2000h]

file_manager_get_filetype(a2, v5);
v7 = 10;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC14);
sprintf(s, "%sServer: Simple Web Server\r\n", s);
sprintf(s, "%sContent-length: %d\r\n", s, a3);
sprintf(s, "%sContent-type: %s\r\n\r\n", s, v5);
v4 = strlen(s);
func1(fd, (__int64)s, v4);
}

original_code:
void clientsuccess(const int fd, const char *filename, const int length, const int status) {
   char filetype[256], buf[8192];
   file_manager_get_filetype(filename, filetype);
   sprintf(buf, "HTTP/1.0 200 OK\r\n");
   sprintf(buf, "%sServer: Simple Web Server\r\n", buf);
   sprintf(buf, "%sContent-length: %d\r\n", buf, length);
   sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
   write_complete(fd, buf, strlen(buf));
}

canonical_original_code:
void clientsuccess(int fd, char *filename, int length, int status) {
   char filetype[256]; char buf[8192];
   file_manager_get_filetype(filename, filetype);
   sprintf(buf, "HTTP/1.0 200 OK\r\n");
   sprintf(buf, "%sServer: Simple Web Server\r\n", buf);
   sprintf(buf, "%sContent-length: %d\r\n", buf, length);
   sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
   write_complete(fd, buf, strlen(buf));
}

variable_types:
filename
char *

variable_types: length int

variable_types: status int

variable_types:
filetype
char [256]

variable_types:
buf
char [8192]


decompiled_code:
void __fastcall __noreturn start_thread(void *a1)
{
int v1; // eax
int v2; // eax
int v3; // er12
__int64 v4; // r13
socklen_t addr_len; // [rsp+4h] [rbp-43Ch] BYREF
unsigned int v6; // [rsp+8h] [rbp-438h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-434h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-430h] BYREF
char v9[256]; // [rsp+20h] [rbp-420h] BYREF
int v10; // [rsp+120h] [rbp-320h] BYREF
char v11; // [rsp+21Fh] [rbp-221h]
char v12[256]; // [rsp+220h] [rbp-220h] BYREF
__int64 v13; // [rsp+320h] [rbp-120h] BYREF
char v14; // [rsp+328h] [rbp-118h]
char v15; // [rsp+41Fh] [rbp-21h]

while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v2 = accept(listen_socket, &addr, &addr_len);
v3 = v2;
if ( v2 != -1 )
break;
perror("An invalid socket has been accepted:");
}
if ( (unsigned int)read_request(v2, (__int64)v9, 256)
|| (v1 = _isoc99_sscanf(v9, "%255s %255s %255s", &v10, v12, &v13), v15 = 0, v12[255] = 0, v11 = 0, v1 != 3) )
{
clienterror(v3, v9, "400", "Bad request", "The request is not well-formatted");
}
else if ( v10 == 5522759 )
{
if ( v13 != 0x302E312F50545448LL || v14 )
{
clienterror(
v3,
(const char *)&v13,
"505",
"HTTP version not supported",
"The server does not implement this version of HTTP");
}
else
{
v4 = file_manager_get(v12, &v6, &v7);
if ( v4 )
{
clientsuccess(v3, (__int64)v12, v7);
write_complete(v3, v4, v7);
file_manager_release(v4, v7);
}
else if ( v6 == 404 )
{
clienterror(v3, v12, "404", "Not found", "The file cannot be found");
}
else
{
if ( v6 != 401 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Internal error: unknown status %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"start_thread",
140LL,
v6,
27LL,
0LL,
37LL);
exit(-1);
}
clienterror(v3, v12, "401", "Not authorized", "The file cannot be accessed");
}
}
}
else
{
clienterror(v3, v9, "501", "Not implemented", "The request is not implemented");
}
close(v3);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
int v1; // eax
int v2; // eax
int v3; // er12
__int64 v4; // r13
socklen_t addr_len; // [rsp+4h] [rbp-43Ch] BYREF
unsigned int v6; // [rsp+8h] [rbp-438h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-434h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-430h] BYREF
char v9[256]; // [rsp+20h] [rbp-420h] BYREF
int v10; // [rsp+120h] [rbp-320h] BYREF
char v11; // [rsp+21Fh] [rbp-221h]
char v12[256]; // [rsp+220h] [rbp-220h] BYREF
__int64 v13; // [rsp+320h] [rbp-120h] BYREF
char v14; // [rsp+328h] [rbp-118h]
char v15; // [rsp+41Fh] [rbp-21h]

while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v2 = accept(listen_socket, &addr, &addr_len);
v3 = v2;
if ( v2 != -1 )
break;
perror("An invalid socket has been accepted:");
}
if ( (unsigned int)func0(v2, (__int64)v9, 256)
|| (v1 = _isoc99_sscanf(v9, "%255s %255s %255s", &v10, v12, &v13), v15 = 0, v12[255] = 0, v11 = 0, v1 != 3) )
{
func2(v3, v9, "400", "Bad request", "The request is not well-formatted");
}
else if ( v10 == 5522759 )
{
if ( v13 != 0x302E312F50545448LL || v14 )
{
func2(
v3,
(const char *)&v13,
"505",
"HTTP version not supported",
"The server does not implement this version of HTTP");
}
else
{
v4 = file_manager_get(v12, &v6, &v7);
if ( v4 )
{
func3(v3, (__int64)v12, v7);
func1(v3, v4, v7);
file_manager_release(v4, v7);
}
else if ( v6 == 404 )
{
func2(v3, v12, "404", "Not found", "The file cannot be found");
}
else
{
if ( v6 != 401 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Internal error: unknown status %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"start_thread",
140LL,
v6,
27LL,
0LL,
37LL);
exit(-1);
}
func2(v3, v12, "401", "Not authorized", "The file cannot be accessed");
}
}
}
else
{
func2(v3, v9, "501", "Not implemented", "The request is not implemented");
}
close(v3);
}
}

original_code:
void* start_thread(void *arg) {
   char request[256];
   char op[256];
   char file[256];
   char version[256];
   while (1) {
      struct sockaddr_in csin;
      int sinsize = sizeof(csin);
      int s = accept(listen_socket, (struct sockaddr*) &csin, (socklen_t*)&sinsize);
      if (s == -1) {
         perror("An invalid socket has been accepted:");
         continue;
      }
      ;
      int r = read_request(s, request, 256);
      ;
      if (r == 0) {
         int m = sscanf(request, "%255s %255s %255s", op, file, version);
         op[256 -1] = file[256 -1] = version[256 -1] = '\0';
         if (m != 3) {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         } else {
            if (strcmp(op, "GET")) {
               clienterror(s, request, "501", "Not implemented", "The request is not implemented");
            } else if (strcmp(version, "HTTP/1.0")) {
               clienterror(s, version, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
            } else {
               int status, length;
                  char *c = file_manager_get(file, &status, &length);
                  if (c != ((void *)0)) {
                     clientsuccess(s, file, length, status);
                     write_complete(s, c, length);
                     file_manager_release(c, length);
                  } else {
                     if (status == 404) {
                        clienterror(s, file, "404", "Not found", "The file cannot be found");
                     } else if (status == 401) {
                        clienterror(s, file, "401", "Not authorized", "The file cannot be accessed");
                     } else {
                        do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Internal error: unknown status %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 140, status, 0x1B, 0, 37); exit(-1); } while(0);
                     }
                  }
               }
            }
         } else {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         }
         close(s);
      }
   }

canonical_original_code:
void *start_thread(void *arg) {
   char request[256];
   char op[256];
   char file[256];
   char version[256];
   while (1) {
      struct sockaddr_in csin;
      int sinsize = sizeof(csin);
      int s = accept(listen_socket, (struct sockaddr*) &csin, (socklen_t*)&sinsize);
      if (s == -1) {
         perror("An invalid socket has been accepted:");
         continue;
      }
      ;
      int r = read_request(s, request, 256);
      ;
      if (r == 0) {
         int m = sscanf(request, "%255s %255s %255s", op, file, version);
         op[256 -1] = file[256 -1] = version[256 -1] = '\0';
         if (m != 3) {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         } else {
            if (strcmp(op, "GET")) {
               clienterror(s, request, "501", "Not implemented", "The request is not implemented");
            } else if (strcmp(version, "HTTP/1.0")) {
               clienterror(s, version, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
            } else {
               int status; int length;
                  char *c = file_manager_get(file, &status, &length);
                  if (c != ((void *)0)) {
                     clientsuccess(s, file, length, status);
                     write_complete(s, c, length);
                     file_manager_release(c, length);
                  } else {
                     if (status == 404) {
                        clienterror(s, file, "404", "Not found", "The file cannot be found");
                     } else if (status == 401) {
                        clienterror(s, file, "401", "Not authorized", "The file cannot be accessed");
                     } else {
                        do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Internal error: unknown status %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 140, status, 0x1B, 0, 37); exit(-1); } while(0);
                     }
                  }
               }
            }
         } else {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         }
         close(s);
      }
   }

variable_types:
arg
void *

variable_types:
request
char [256]

variable_types:
op
char [256]

variable_types:
file
char [256]

variable_types:
version
char [256]

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: sinsize int

variable_types: s int

variable_types: m int

variable_types:
c
char *

return_type: void *


decompiled_code:
void __fastcall create_pool(int a1, int a2)
{
__int64 v2; // rbx
pthread_t v3; // [rsp-20h] [rbp-20h] BYREF

listen_socket = a2;
if ( a1 > 0 )
{
v2 = 0LL;
do
{
if ( pthread_create(&v3, 0LL, (void *(*)(void *))start_thread, 0LL) )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Failed to create the thread %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"create_pool",
166LL,
v2,
27LL,
0LL,
37LL);
exit(-1);
}
v2 = (unsigned int)(v2 + 1);
}
while ( a1 != (_DWORD)v2 );
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2)
{
__int64 v2; // rbx
pthread_t v3; // [rsp-20h] [rbp-20h] BYREF

listen_socket = a2;
if ( a1 > 0 )
{
v2 = 0LL;
do
{
if ( pthread_create(&v3, 0LL, (void *(*)(void *))start_thread, 0LL) )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Failed to create the thread %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"create_pool",
166LL,
v2,
27LL,
0LL,
37LL);
exit(-1);
}
v2 = (unsigned int)(v2 + 1);
}
while ( a1 != (_DWORD)v2 );
}
}

original_code:
void create_pool(const int pool_size, int ls) {
      int i;
      listen_socket = ls;
      for (i=0; i<pool_size; i++) {
         pthread_t thread;
         int rc = pthread_create(&thread, ((void *)0), start_thread, ((void *)0));
         if (rc) {
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Failed to create the thread %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 166, i, 0x1B, 0, 37); exit(-1); } while(0);
         }
      }
   }

canonical_original_code:
void create_pool(int pool_size, int ls) {
      int i;
      listen_socket = ls;
      for (i=0; i<pool_size; i++) {
         unsigned long thread;
         int rc = pthread_create(&thread, ((void *)0), start_thread, ((void *)0));
         if (rc) {
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Failed to create the thread %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 166, i, 0x1B, 0, 37); exit(-1); } while(0);
         }
      }
   }

variable_types: ls int

variable_types: i int

variable_types:
thread
unsigned long

variable_types: rc int


decompiled_code:
void delete_pool()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void delete_pool(void) {
   }

canonical_original_code:
void delete_pool() {
   }


decompiled_code:
void __fastcall range_tree_clear(void *ptr)
{
if ( ptr )
{
range_tree_clear(*(void **)ptr);
range_tree_clear(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
func0(*(void **)ptr);
func0(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
void range_tree_clear(range_node* tree)
{
 if(! tree) return;
 range_tree_clear(tree->left);
 range_tree_clear(tree->right);
 free(tree);
 tree = ((void *)0);
}

canonical_original_code:
void range_tree_clear(struct range_node *tree)
{
 if(! tree) return;
 range_tree_clear(tree->left);
 range_tree_clear(tree->right);
 free(tree);
 tree = ((void *)0);
}

variable_types: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; } *


decompiled_code:
__int64 __fastcall range_tree_contains(_QWORD *a1, __int64 a2, int (__fastcall *a3)(_QWORD, __int64))
{
_QWORD *v4; // rbx

if ( !a1 )
return 0LL;
v4 = a1;
if ( !a2 )
_assert_fail("tree && val", "range_tree.c", 0x21u, "range_tree_contains");
while ( 1 )
{
while ( a3(v4[2], a2) > 0 )
{
v4 = (_QWORD *)*v4;
if ( !v4 )
return 0LL;
}
if ( a3(v4[3], a2) >= 0 )
break;
v4 = (_QWORD *)v4[1];
if ( !v4 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, int (__fastcall *a3)(_QWORD, __int64))
{
_QWORD *v4; // rbx

if ( !a1 )
return 0LL;
v4 = a1;
if ( !a2 )
_assert_fail("tree && val", "range_tree.c", 0x21u, "range_tree_contains");
while ( 1 )
{
while ( a3(v4[2], a2) > 0 )
{
v4 = (_QWORD *)*v4;
if ( !v4 )
return 0LL;
}
if ( a3(v4[3], a2) >= 0 )
break;
v4 = (_QWORD *)v4[1];
if ( !v4 )
return 0LL;
}
return 1LL;
}

original_code:
int range_tree_contains(const range_node* tree, const void* val, val_cmp comp)
{
 if(!tree) return 0;
 ((tree && val) ? (void) (0) : __assert_fail ("tree && val", "range_tree.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(comp(tree->min, val) > 0) {
  if(tree->left) return range_tree_contains(tree->left, val, comp);
  else return 0;
 } else if(comp(tree->max, val) < 0) {
  if(tree->right) return range_tree_contains(tree->right, val, comp);
  else return 0;
 }
 return 1;
}

canonical_original_code:
int range_tree_contains(struct range_node *tree, void *val, int (*comp)(void *a, void *b))
{
 if(!tree) return 0;
 ((tree && val) ? (void) (0) : __assert_fail ("tree && val", "range_tree.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(comp(tree->min, val) > 0) {
  if(tree->left) return range_tree_contains(tree->left, val, comp);
  else return 0;
 } else if(comp(tree->max, val) < 0) {
  if(tree->right) return range_tree_contains(tree->right, val, comp);
  else return 0;
 }
 return 1;
}

variable_types:
val
void *

variable_types:
comp
int (*)(void *a, void *b)

user_defined_types: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; }


cmp

decompiled_code:
__int64 __fastcall cmp(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

original_code:
int cmp(const void* a, const void* b)
{
 return *(int*)a - *(int*)b;
}

canonical_original_code:
int cmp(void *a, void *b)
{
 return *(int *)a - *(int *)b;
}

variable_types:
a
void *

variable_types:
b
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5 = 5;
v6[0] = 7;
v3 = range_tree_create(&v5, v6, envp);
range_tree_clear(v3);
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 int a = 5;
 int b = 7;
 range_node* node = range_tree_create(&a, &b);
 range_tree_clear(node);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int a = 5;
 int b = 7;
 struct range_node *node = range_tree_create(&a, &b);
 range_tree_clear(node);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: a int

variable_types: b int


decompiled_code:
__int64 __fastcall range_list_merge_ranges(__int64 a1, int (__fastcall *a2)(__int64, _QWORD), void (__fastcall *a3)(__int64))
{
__int64 i; // r12
_QWORD *v5; // rbx
__int64 v6; // rbp
__int64 v7; // rax
__int64 v8; // rax

if ( !a1 )
_assert_fail("first", "range_list.c", 0xDu, "range_list_merge_ranges");
for ( i = a1; ; i = v8 )
{
if ( a2(*(_QWORD *)(i + 16), *(_QWORD *)(i + 24)) > 0 )
_assert_fail("comp(curr->min, curr->max) <= 0", "range_list.c", 0x11u, "range_list_merge_ranges");
v5 = *(_QWORD **)(i + 8);
v6 = *(_QWORD *)(i + 24);
if ( !v5 )
break;
while ( 1 )
{
if ( a2(*(_QWORD *)(i + 16), v5[2]) > 0 )
_assert_fail("comp(curr->min, tmp->min) <= 0", "range_list.c", 0x15u, "range_list_merge_ranges");
a3(v6);
if ( a2(v6, v5[2]) < 0 )
break;
if ( a2(v6, v5[3]) < 0 )
v6 = v5[3];
v7 = *(_QWORD *)(i + 8);
*v5 = 0LL;
v5 = (_QWORD *)v5[1];
*(_QWORD *)(v7 + 8) = 0LL;
free(*(void **)(i + 8));
*(_QWORD *)(i + 8) = v5;
if ( !v5 )
goto LABEL_13;
}
v8 = *(_QWORD *)(i + 8);
*(_QWORD *)(i + 24) = v6;
*v5 = i;
if ( !v8 )
return i;
}
LABEL_13:
*(_QWORD *)(i + 24) = v6;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int (__fastcall *a2)(__int64, _QWORD), void (__fastcall *a3)(__int64))
{
__int64 i; // r12
_QWORD *v5; // rbx
__int64 v6; // rbp
__int64 v7; // rax
__int64 v8; // rax

if ( !a1 )
_assert_fail("first", "range_list.c", 0xDu, "range_list_merge_ranges");
for ( i = a1; ; i = v8 )
{
if ( a2(*(_QWORD *)(i + 16), *(_QWORD *)(i + 24)) > 0 )
_assert_fail("comp(curr->min, curr->max) <= 0", "range_list.c", 0x11u, "range_list_merge_ranges");
v5 = *(_QWORD **)(i + 8);
v6 = *(_QWORD *)(i + 24);
if ( !v5 )
break;
while ( 1 )
{
if ( a2(*(_QWORD *)(i + 16), v5[2]) > 0 )
_assert_fail("comp(curr->min, tmp->min) <= 0", "range_list.c", 0x15u, "range_list_merge_ranges");
a3(v6);
if ( a2(v6, v5[2]) < 0 )
break;
if ( a2(v6, v5[3]) < 0 )
v6 = v5[3];
v7 = *(_QWORD *)(i + 8);
*v5 = 0LL;
v5 = (_QWORD *)v5[1];
*(_QWORD *)(v7 + 8) = 0LL;
free(*(void **)(i + 8));
*(_QWORD *)(i + 8) = v5;
if ( !v5 )
goto LABEL_13;
}
v8 = *(_QWORD *)(i + 8);
*(_QWORD *)(i + 24) = v6;
*v5 = i;
if ( !v8 )
return i;
}
LABEL_13:
*(_QWORD *)(i + 24) = v6;
return i;
}

original_code:
range_node* range_list_merge_ranges(range_node* first, val_cmp comp, val_inc inc)
{
 ((first) ? (void) (0) : __assert_fail ("first", "range_list.c", 13, __extension__ __PRETTY_FUNCTION__));
 range_node* curr = first;
 range_node* last = first;
 while(curr != ((void *)0)) {
  ((comp(curr->min, curr->max) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, curr->max) <= 0", "range_list.c", 17, __extension__ __PRETTY_FUNCTION__));
  void* new_curr_max = curr->max;
  range_node* tmp = curr->right;
  while(tmp != ((void *)0)) {
   ((comp(curr->min, tmp->min) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, tmp->min) <= 0", "range_list.c", 21, __extension__ __PRETTY_FUNCTION__));
   void* a = new_curr_max;
   inc(a);
   if(comp(a, tmp->min) < 0) break;
   if(comp(new_curr_max, tmp->max) < 0)
    new_curr_max = tmp->max;
   tmp->left = ((void *)0);
   tmp = tmp->right;
   curr->right->right = ((void *)0);
   free(curr->right);
   curr->right = tmp;
  }
  curr->max = new_curr_max;
  if(tmp) tmp->left = curr;
  last = curr;
  curr = curr->right;
 }
 return last;
}

canonical_original_code:
struct range_node *range_list_merge_ranges(struct range_node *first, int (*comp)(void *a, void *b), void (*inc)(void *val))
{
 ((first) ? (void) (0) : __assert_fail ("first", "range_list.c", 13, __extension__ __PRETTY_FUNCTION__));
 struct range_node *curr = first;
 struct range_node *last = first;
 while(curr != ((void *)0)) {
  ((comp(curr->min, curr->max) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, curr->max) <= 0", "range_list.c", 17, __extension__ __PRETTY_FUNCTION__));
  void *new_curr_max = curr->max;
  struct range_node *tmp = curr->right;
  while(tmp != ((void *)0)) {
   ((comp(curr->min, tmp->min) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, tmp->min) <= 0", "range_list.c", 21, __extension__ __PRETTY_FUNCTION__));
   void *a = new_curr_max;
   inc(a);
   if(comp(a, tmp->min) < 0) break;
   if(comp(new_curr_max, tmp->max) < 0)
    new_curr_max = tmp->max;
   tmp->left = ((void *)0);
   tmp = tmp->right;
   curr->right->right = ((void *)0);
   free(curr->right);
   curr->right = tmp;
  }
  curr->max = new_curr_max;
  if(tmp) tmp->left = curr;
  last = curr;
  curr = curr->right;
 }
 return last;
}

variable_types:
first
struct range_node *

variable_types:
inc
void (*)(void *val)

return_type: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; } *


decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
void *v3; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5 = 5;
v6[0] = 7;
v3 = (void *)sub_4011C0(&v5, v6, a3);
sub_401190(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
void *v3; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5 = 5;
v6[0] = 7;
v3 = (void *)sub_4011C0(&v5, v6, a3);
func6(v3);
return 0LL;
}


repo: schachmat/slcp

decompiled_code:
int __fastcall change_col(int a1)
{
fputc(1, edata);
fwrite(&unk_402004, 1uLL, 3uLL, edata);
fputc(a1 + 48, edata);
fputc(109, edata);
return fputc(2, edata);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
fputc(1, edata);
fwrite(&unk_402004, 1uLL, 3uLL, edata);
fputc(a1 + 48, edata);
fputc(109, edata);
return fputc(2, edata);
}

original_code:
static void change_col(unsigned int col)
{
 fputs("\x01", stdout);
 fputs("\033[3", stdout);
 fputc((col > 7 ? 0 : col) + '0', stdout);
 fputc('m', stdout);
 fputs("\x02", stdout);
}

variable_types:
col
unsigned int


catscol

decompiled_code:
int __fastcall catscol(char *s, int a2)
{
change_col(a2);
return fputs(s, edata);
}

canonical_decompiled_code:
int __fastcall func8(char *s, int a2)
{
func7(a2);
return fputs(s, edata);
}

original_code:
static void catscol(const char* str, unsigned int col)
{
 change_col(col);
 fputs(str, stdout);
}

canonical_original_code:
static void catscol(char *str, unsigned int col)
{
 change_col(col);
 fputs(str, stdout);
}

variable_types:
str
char *


ppidCmp

repo: schischi/xcwd

decompiled_code:
__int64 __fastcall ppidCmp(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 1) - *((_DWORD *)a2 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 1) - *((_DWORD *)a2 + 2);
}

original_code:
int ppidCmp(const void *p1, const void *p2)
{
    return ((struct proc_s *)p1)->ppid - ((struct proc_s *)p2)->ppid;
}

canonical_original_code:
int ppidCmp(void *p1, void *p2)
{
    return ((struct proc_s *)p1)->ppid - ((struct proc_s *)p2)->ppid;
}

user_defined_types: struct proc_s { long pid; long ppid; char name[32]; }


nameCmp

decompiled_code:
int __fastcall nameCmp(const void *a1, const void *a2)
{
return strcasecmp((const char *)a1 + 16, (const char *)a2 + 16);
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, const void *a2)
{
return strcasecmp((const char *)a1 + 16, (const char *)a2 + 16);
}

original_code:
int nameCmp(const void *p1, const void *p2)
{
    return strcasecmp(((struct proc_s *)p1)->name,
            ((struct proc_s *)p2)->name);
}

canonical_original_code:
int nameCmp(void *p1, void *p2)
{
    return strcasecmp(((struct proc_s *)p1)->name,
            ((struct proc_s *)p2)->name);
}


freeProcesses

decompiled_code:
void __fastcall freeProcesses(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
static void freeProcesses(processes_t p)
{
    free(p->ps);
    free(p);
}

canonical_original_code:
static void freeProcesses(struct processes_s *p)
{
    free(p->ps);
    free(p);
}

variable_types: struct processes_s { struct proc_s { long pid; long ppid; char name[32]; } *ps; unsigned long n; } *

user_defined_types: struct processes_s { struct proc_s *ps; unsigned long n; }


windowPid

decompiled_code:
__int64 __fastcall windowPid(Window a1)
{
Atom v1; // r12
Atom v2; // rax
__int64 v3; // r12
int v5; // [rsp+Ch] [rbp-3Ch] BYREF
Atom v6; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v7; // [rsp+18h] [rbp-30h] BYREF
unsigned __int64 v8; // [rsp+20h] [rbp-28h] BYREF
unsigned __int8 *v9; // [rsp+28h] [rbp-20h] BYREF

v1 = XInternAtom(dpy, "_NET_WM_PID", 1);
v2 = XInternAtom(dpy, "CARDINAL", 0);
if ( XGetWindowProperty(dpy, a1, v1, 0LL, 1024LL, 0, v2, &v6, &v5, &v7, &v8, &v9) || !v9 )
return -1LL;
v3 = *(_QWORD *)v9;
XFree(v9);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(Window a1)
{
Atom v1; // r12
Atom v2; // rax
__int64 v3; // r12
int v5; // [rsp+Ch] [rbp-3Ch] BYREF
Atom v6; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v7; // [rsp+18h] [rbp-30h] BYREF
unsigned __int64 v8; // [rsp+20h] [rbp-28h] BYREF
unsigned __int8 *v9; // [rsp+28h] [rbp-20h] BYREF

v1 = XInternAtom(dpy, "_NET_WM_PID", 1);
v2 = XInternAtom(dpy, "CARDINAL", 0);
if ( XGetWindowProperty(dpy, a1, v1, 0LL, 1024LL, 0, v2, &v6, &v5, &v7, &v8, &v9) || !v9 )
return -1LL;
v3 = *(_QWORD *)v9;
XFree(v9);
return v3;
}

original_code:
static long windowPid(Window focuswin)
{
    Atom nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1);
    Atom type;
    int format, status;
    long pid = -1;
    unsigned long nitems, after;
    unsigned char *data;
    status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0,
            (XInternAtom(dpy, "CARDINAL", 0)), &type, &format, &nitems, &after, &data);
    if (status == 0 && data) {
        pid = *((long*)data);
        XFree(data);
        do { if (0) fprintf(stderr, "_NET_WM_PID = %lu\n", pid); } while (0);
    }
    else
        do { if (0) fprintf(stderr, "%s", "_NET_WM_PID not found\n"); } while (0);
    return pid;
}

canonical_original_code:
static long windowPid(unsigned long focuswin)
{
    unsigned long nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1);
    unsigned long type;
    int format; int status;
    long pid = -1;
    unsigned long nitems; unsigned long after;
    unsigned char *data;
    status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0,
            (XInternAtom(dpy, "CARDINAL", 0)), &type, &format, &nitems, &after, &data);
    if (status == 0 && data) {
        pid = *((long *)data);
        XFree(data);
        do { if (0) fprintf(stderr, "_NET_WM_PID = %lu\n", pid); } while (0);
    }
    else
        do { if (0) fprintf(stderr, "%s", "_NET_WM_PID not found\n"); } while (0);
    return pid;
}

variable_types:
focuswin
unsigned long

variable_types:
nameAtom
unsigned long

variable_types:
type
unsigned long

variable_types: format int

variable_types: status int

variable_types: pid long

variable_types:
nitems
unsigned long

variable_types:
after
unsigned long

variable_types:
data
unsigned char *

return_type: long


focusedWindow

decompiled_code:
Window focusedWindow()
{
Display *v0; // rax
Atom v1; // r15
Atom v2; // rax
int v4; // [rsp+Ch] [rbp-7Ch] BYREF
unsigned int v5; // [rsp+10h] [rbp-78h] BYREF
int v6; // [rsp+14h] [rbp-74h] BYREF
Atom v7; // [rsp+18h] [rbp-70h] BYREF
Window v8; // [rsp+20h] [rbp-68h] BYREF
Window i; // [rsp+28h] [rbp-60h] BYREF
Window *v10; // [rsp+30h] [rbp-58h] BYREF
unsigned __int64 v11; // [rsp+38h] [rbp-50h] BYREF
unsigned __int64 v12; // [rsp+40h] [rbp-48h] BYREF
unsigned __int8 *v13; // [rsp+48h] [rbp-40h] BYREF

v0 = XOpenDisplay(0LL);
dpy = v0;
if ( !v0 )
exit(1);
v6 = 0;
XGetInputFocus(v0, &v8, &v6);
for ( i = XDefaultRootWindow(dpy); v8 != i; XQueryTree(dpy, v8, &i, &v8, &v10, &v5) )
{
v1 = XInternAtom(dpy, "WM_STATE", 0);
v2 = XInternAtom(dpy, "WM_STATE", 0);
if ( XGetWindowProperty(dpy, v8, v2, 0LL, 1024LL, 0, v1, &v7, &v4, &v11, &v12, &v13) )
break;
if ( v13 )
{
XFree(v13);
return v8;
}
}
return 0LL;
}

canonical_decompiled_code:
Window func4()
{
Display *v0; // rax
Atom v1; // r15
Atom v2; // rax
int v4; // [rsp+Ch] [rbp-7Ch] BYREF
unsigned int v5; // [rsp+10h] [rbp-78h] BYREF
int v6; // [rsp+14h] [rbp-74h] BYREF
Atom v7; // [rsp+18h] [rbp-70h] BYREF
Window v8; // [rsp+20h] [rbp-68h] BYREF
Window i; // [rsp+28h] [rbp-60h] BYREF
Window *v10; // [rsp+30h] [rbp-58h] BYREF
unsigned __int64 v11; // [rsp+38h] [rbp-50h] BYREF
unsigned __int64 v12; // [rsp+40h] [rbp-48h] BYREF
unsigned __int8 *v13; // [rsp+48h] [rbp-40h] BYREF

v0 = XOpenDisplay(0LL);
dpy = v0;
if ( !v0 )
exit(1);
v6 = 0;
XGetInputFocus(v0, &v8, &v6);
for ( i = XDefaultRootWindow(dpy); v8 != i; XQueryTree(dpy, v8, &i, &v8, &v10, &v5) )
{
v1 = XInternAtom(dpy, "WM_STATE", 0);
v2 = XInternAtom(dpy, "WM_STATE", 0);
if ( XGetWindowProperty(dpy, v8, v2, 0LL, 1024LL, 0, v1, &v7, &v4, &v11, &v12, &v13) )
break;
if ( v13 )
{
XFree(v13);
return v8;
}
}
return 0LL;
}

original_code:
static Window focusedWindow()
{
    Atom type;
    Window focuswin, root, *children;
    int format, status;
    unsigned long nitems, after;
    unsigned int nchildren;
    unsigned char *data;
    dpy = XOpenDisplay (((void *)0));
    if (!dpy)
        exit(1);
    XGetInputFocus (dpy, &focuswin, (int[1]){});
    root = XDefaultRootWindow(dpy);
    if(root == focuswin)
        return 0L;
    do {
        status = XGetWindowProperty(dpy, focuswin, (XInternAtom(dpy, "WM_STATE", 0)), 0, 1024, 0,
                (XInternAtom(dpy, "WM_STATE", 0)), &type, &format, &nitems, &after, &data);
        if (status == 0) {
            if (data) {
                XFree(data);
                do { if (0) fprintf(stderr, "Window ID = %lu\n", focuswin); } while (0);
                return focuswin;
            }
        }
        else
            return 0;
        XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
        do { if (0) fprintf(stderr, "%s", "Current window does not have WM_STATE, getting parent\n"); } while (0);
    } while(focuswin != root);
    return 0;
}

canonical_original_code:
static unsigned long focusedWindow()
{
    unsigned long type;
    unsigned long focuswin; unsigned long root; unsigned long *children;
    int format; int status;
    unsigned long nitems; unsigned long after;
    unsigned int nchildren;
    unsigned char *data;
    dpy = XOpenDisplay (((void *)0));
    if (!dpy)
        exit(1);
    XGetInputFocus (dpy, &focuswin, (int [1]){});
    root = XDefaultRootWindow(dpy);
    if(root == focuswin)
        return 0L;
    do {
        status = XGetWindowProperty(dpy, focuswin, (XInternAtom(dpy, "WM_STATE", 0)), 0, 1024, 0,
                (XInternAtom(dpy, "WM_STATE", 0)), &type, &format, &nitems, &after, &data);
        if (status == 0) {
            if (data) {
                XFree(data);
                do { if (0) fprintf(stderr, "Window ID = %lu\n", focuswin); } while (0);
                return focuswin;
            }
        }
        else
            return 0;
        XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
        do { if (0) fprintf(stderr, "%s", "Current window does not have WM_STATE, getting parent\n"); } while (0);
    } while(focuswin != root);
    return 0;
}

variable_types:
root
unsigned long

variable_types:
children
unsigned long *

variable_types:
nchildren
unsigned int

return_type: unsigned long


cwdOfDeepestChild

original_code:
static int cwdOfDeepestChild(processes_t p, long pid)
{
    int i;
    struct proc_s key = { .pid = pid, .ppid = pid},
                  *res = ((void *)0), *lastRes = ((void *)0);
    do {
        if(res) {
            lastRes = res;
            key.ppid = res->pid;
        }
        res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                sizeof(struct proc_s), ppidCmp);
    } while(res);
    if(!lastRes) {
        return readPath(&key);
    }
    for(i = 0; lastRes != p->ps && (lastRes - i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes - i)))
            return 1;
    for(i = 1; lastRes != p->ps + p->n && (lastRes + i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes + i)))
            return 1;
    return 0;
}

canonical_original_code:
static int cwdOfDeepestChild(struct processes_s *p, long pid)
{
    int i;
    struct proc_s key = { .pid = pid, .ppid = pid}; struct proc_s *res = ((void *)0); struct proc_s *lastRes = ((void *)0);
    do {
        if(res) {
            lastRes = res;
            key.ppid = res->pid;
        }
        res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                sizeof(struct proc_s), ppidCmp);
    } while(res);
    if(!lastRes) {
        return readPath(&key);
    }
    for(i = 0; lastRes != p->ps && (lastRes - i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes - i)))
            return 1;
    for(i = 1; lastRes != p->ps + p->n && (lastRes + i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes + i)))
            return 1;
    return 0;
}

variable_types: i int

variable_types: struct proc_s { long pid; long ppid; char name[32]; } *


getHomeDirectory

decompiled_code:
__int64 getHomeDirectory()
{
char *v0; // rax

v0 = getenv("HOME");
fprintf(stdout, "%s\n", v0);
return 1LL;
}

canonical_decompiled_code:
__int64 func9()
{
char *v0; // rax

v0 = getenv("HOME");
fprintf(stdout, "%s\n", v0);
return 1LL;
}

original_code:
int getHomeDirectory()
{
    do { if (0) fprintf(stderr, "%s", "getenv $HOME...\n"); } while (0);
    fprintf(stdout, "%s\n", getenv("HOME"));
    return 1;
}


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
Window v3; // rax
Window v4; // rbp
__int64 v5; // r13
void **v6; // rax
__int64 v7; // r12
size_t v8; // rsi
void *v9; // rdi
unsigned __int64 *v10; // rsi
char *v11; // rdi
int v12; // er13
unsigned __int8 *v14; // r14
unsigned __int64 v15; // rax
int v16; // ebx
const char *v17; // rbp
__int64 *v18; // rax
unsigned __int64 v19; // [rsp+8h] [rbp-60h] BYREF
char key[16]; // [rsp+10h] [rbp-58h] BYREF
char dest[72]; // [rsp+20h] [rbp-48h] BYREF

v3 = focusedWindow();
if ( v3 )
{
*(_QWORD *)&argc = v3;
v4 = v3;
v5 = windowPid(v3);
v6 = (void **)getProcesses();
v7 = (__int64)v6;
if ( v6 )
{
v8 = (size_t)v6[1];
v9 = *v6;
if ( v5 == -1 )
{
qsort(v9, v8, 0x30uLL, nameCmp);
v10 = &v19;
v11 = (char *)v4;
v14 = windowStrings_constprop_0(v4, &v19);
if ( v19 )
{
v15 = 0LL;
v16 = 0;
while ( 1 )
{
v17 = (const char *)&v14[v15];
strcpy(dest, (const char *)&v14[v15]);
v10 = *(unsigned __int64 **)v7;
v11 = key;
v18 = (__int64 *)bsearch(key, *(const void **)v7, *(_QWORD *)(v7 + 8), 0x30uLL, nameCmp);
if ( v18 )
break;
v11 = (char *)v17;
v15 = v16 + (unsigned int)strlen(v17) + 1;
v16 = v15;
if ( v15 >= v19 )
{
if ( !v19 )
goto LABEL_13;
goto LABEL_16;
}
}
v5 = *v18;
if ( !v19 )
goto LABEL_12;
LABEL_16:
v11 = (char *)v14;
free(v14);
LABEL_12:
if ( v5 != -1 )
goto LABEL_5;
}
}
else
{
qsort(v9, v8, 0x30uLL, (__compar_fn_t)ppidCmp);
LABEL_5:
v10 = (unsigned __int64 *)v5;
v11 = (char *)v7;
v12 = 0;
if ( (unsigned int)cwdOfDeepestChild(v7, (__int64)v10) )
goto LABEL_6;
}
LABEL_13:
v12 = getHomeDirectory(v11, v10);
LABEL_6:
freeProcesses((void *)v7);
return v12;
}
}
return getHomeDirectory(*(_QWORD *)&argc, argv);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Window v3; // rax
Window v4; // rbp
__int64 v5; // r13
void **v6; // rax
__int64 v7; // r12
size_t v8; // rsi
void *v9; // rdi
unsigned __int64 *v10; // rsi
char *v11; // rdi
int v12; // er13
unsigned __int8 *v14; // r14
unsigned __int64 v15; // rax
int v16; // ebx
const char *v17; // rbp
__int64 *v18; // rax
unsigned __int64 v19; // [rsp+8h] [rbp-60h] BYREF
char key[16]; // [rsp+10h] [rbp-58h] BYREF
char dest[72]; // [rsp+20h] [rbp-48h] BYREF

v3 = func4();
if ( v3 )
{
*(_QWORD *)&argc = v3;
v4 = v3;
v5 = func3(v3);
v6 = (void **)func5();
v7 = (__int64)v6;
if ( v6 )
{
v8 = (size_t)v6[1];
v9 = *v6;
if ( v5 == -1 )
{
qsort(v9, v8, 0x30uLL, nameCmp);
v10 = &v19;
v11 = (char *)v4;
v14 = func6(v4, &v19);
if ( v19 )
{
v15 = 0LL;
v16 = 0;
while ( 1 )
{
v17 = (const char *)&v14[v15];
strcpy(dest, (const char *)&v14[v15]);
v10 = *(unsigned __int64 **)v7;
v11 = key;
v18 = (__int64 *)bsearch(key, *(const void **)v7, *(_QWORD *)(v7 + 8), 0x30uLL, nameCmp);
if ( v18 )
break;
v11 = (char *)v17;
v15 = v16 + (unsigned int)strlen(v17) + 1;
v16 = v15;
if ( v15 >= v19 )
{
if ( !v19 )
goto LABEL_13;
goto LABEL_16;
}
}
v5 = *v18;
if ( !v19 )
goto LABEL_12;
LABEL_16:
v11 = (char *)v14;
free(v14);
LABEL_12:
if ( v5 != -1 )
goto LABEL_5;
}
}
else
{
qsort(v9, v8, 0x30uLL, (__compar_fn_t)ppidCmp);
LABEL_5:
v10 = (unsigned __int64 *)v5;
v11 = (char *)v7;
v12 = 0;
if ( (unsigned int)func8(v7, (__int64)v10) )
goto LABEL_6;
}
LABEL_13:
v12 = func9(v11, v10);
LABEL_6:
func2((void *)v7);
return v12;
}
}
return func9(*(_QWORD *)&argc, argv);
}

original_code:
int main(int argc, const char *argv[])
{
    (void)argc;
    (void)argv;
    processes_t p;
    long pid;
    int ret = 0;
    Window w = focusedWindow();
    if (w == 0L)
        return getHomeDirectory();
    pid = windowPid(w);
    p = getProcesses();
    if(!p)
        return getHomeDirectory();
    if(pid != -1)
        qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
    else {
        long unsigned int size;
        unsigned int i;
        char* strings;
        struct proc_s *res = ((void *)0), key;
        qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
        strings = windowStrings(w, &size, "WM_CLASS");
        for(i = 0; i < size; i += strlen(strings + i) + 1) {
            do { if (0) fprintf(stderr, "pidof %s\n", strings + i); } while (0);
            strcpy(key.name, strings + i);
            res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                    sizeof(struct proc_s), nameCmp);
            if(res)
                break;
        }
        if (res) {
            pid = res->pid;
            do { if (0) fprintf(stderr, "Found %s (%ld)\n", res->name, res->pid); } while (0);
        }
        if (size)
            free(strings);
    }
    if (pid == -1 || !cwdOfDeepestChild(p, pid))
        ret = getHomeDirectory();
    freeProcesses(p);
    return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    (void)argc;
    (void)argv;
    struct processes_s *p;
    long pid;
    int ret = 0;
    unsigned long w = focusedWindow();
    if (w == 0L)
        return getHomeDirectory();
    pid = windowPid(w);
    p = getProcesses();
    if(!p)
        return getHomeDirectory();
    if(pid != -1)
        qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
    else {
        unsigned long size;
        unsigned int i;
        char *strings;
        struct proc_s *res = ((void *)0); struct proc_s key;
        qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
        strings = windowStrings(w, &size, "WM_CLASS");
        for(i = 0; i < size; i += strlen(strings + i) + 1) {
            do { if (0) fprintf(stderr, "pidof %s\n", strings + i); } while (0);
            strcpy(key.name, strings + i);
            res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                    sizeof(struct proc_s), nameCmp);
            if(res)
                break;
        }
        if (res) {
            pid = res->pid;
            do { if (0) fprintf(stderr, "Found %s (%ld)\n", res->name, res->pid); } while (0);
        }
        if (size)
            free(strings);
    }
    if (pid == -1 || !cwdOfDeepestChild(p, pid))
        ret = getHomeDirectory();
    freeProcesses(p);
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types:
w
unsigned long

variable_types:
size
unsigned long

variable_types:
i
unsigned int

variable_types:
strings
char *

variable_types: struct proc_s { long pid; long ppid; char name[32]; }