decompiled_code:
__int64 __fastcall show_column_number(__int64 a1)
{
int v2; // er12
unsigned int v3; // eax
int v5; // er12
unsigned int v6; // edi

if ( *(int *)(a1 + 4) > 10 )
{
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v5 = 1;
do
{
v6 = v5++;
ft_putchar((unsigned __int8)(v6 / 0xA % 0xA + 48));
}
while ( *(_DWORD *)(a1 + 4) >= v5 );
}
ft_putchar(10LL);
}
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v2 = 1;
do
{
v3 = v2++;
ft_putchar((unsigned __int8)(v3 % 0xA + 48));
}
while ( *(_DWORD *)(a1 + 4) >= v2 );
}
return ft_putchar(10LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v2; // er12
unsigned int v3; // eax
int v5; // er12
unsigned int v6; // edi

if ( *(int *)(a1 + 4) > 10 )
{
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v5 = 1;
do
{
v6 = v5++;
ft_putchar((unsigned __int8)(v6 / 0xA % 0xA + 48));
}
while ( *(_DWORD *)(a1 + 4) >= v5 );
}
ft_putchar(10LL);
}
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v2 = 1;
do
{
v3 = v2++;
ft_putchar((unsigned __int8)(v3 % 0xA + 48));
}
while ( *(_DWORD *)(a1 + 4) >= v2 );
}
return ft_putchar(10LL);
}

original_code:
void show_column_number(t_size *size)
{
 int i;
 i = 1;
 if (size->l > 10)
 {
  ft_putstr("    ");
  while (i <= size->l)
  {
   ft_putchar('0' + (i / 10) % 10);
   i++;
  }
  ft_putchar('\n');
  i = 1;
 }
 ft_putstr("    ");
 while (i <= size->l)
 {
  ft_putchar('0' + i % 10);
  i++;
 }
 ft_putchar('\n');
}

canonical_original_code:
void show_column_number(struct s_size *size)
{
 int i;
 i = 1;
 if (size->l > 10)
 {
  ft_putstr("    ");
  while (i <= size->l)
  {
   ft_putchar('0' + (i / 10) % 10);
   i++;
  }
  ft_putchar('\n');
  i = 1;
 }
 ft_putstr("    ");
 while (i <= size->l)
 {
  ft_putchar('0' + i % 10);
  i++;
 }
 ft_putchar('\n');
}

variable_types: struct s_size { int h; int l; int m; } *

variable_types: i int


decompiled_code:
__int64 __fastcall show_carac(char a1, int a2, int a3)
{
__int64 v4; // rax
char v5; // dl
int v6; // ecx
int v7; // eax
__int64 result; // rax
__int64 v9; // rcx
char *v10; // rbx
__int64 v11; // rcx
char *v12; // rbx

v4 = 0LL;
v5 = 45;
do
{
v6 = v4;
if ( a1 == v5 )
{
v7 = v4 + 14;
if ( a2 == 1 )
v6 = v7;
if ( a3 )
{
if ( a3 == 1 )
{
v11 = 2LL * v6;
v12 = (char *)&g_finish + v11 * 8;
ft_putstr((&off_528)[v11]);
ft_putchar((unsigned int)v12[1]);
}
result = ft_putstr("\x1B[0m");
}
else
{
v9 = 2LL * v6;
v10 = &g_show[v9 * 8];
ft_putstr((&off_608)[v9]);
ft_putchar((unsigned int)v10[1]);
result = ft_putstr("\x1B[0m");
}
return result;
}
++v4;
v5 = g_show[16 * v4];
}
while ( v5 );
if ( a2 == 1 )
ft_putstr("\x1B[47m");
ft_putchar(32LL);
return ft_putstr("\x1B[0m");
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, int a2, int a3)
{
__int64 v4; // rax
char v5; // dl
int v6; // ecx
int v7; // eax
__int64 result; // rax
__int64 v9; // rcx
char *v10; // rbx
__int64 v11; // rcx
char *v12; // rbx

v4 = 0LL;
v5 = 45;
do
{
v6 = v4;
if ( a1 == v5 )
{
v7 = v4 + 14;
if ( a2 == 1 )
v6 = v7;
if ( a3 )
{
if ( a3 == 1 )
{
v11 = 2LL * v6;
v12 = (char *)&g_finish + v11 * 8;
ft_putstr((&off_528)[v11]);
ft_putchar((unsigned int)v12[1]);
}
result = ft_putstr("\x1B[0m");
}
else
{
v9 = 2LL * v6;
v10 = &g_show[v9 * 8];
ft_putstr((&off_608)[v9]);
ft_putchar((unsigned int)v10[1]);
result = ft_putstr("\x1B[0m");
}
return result;
}
++v4;
v5 = g_show[16 * v4];
}
while ( v5 );
if ( a2 == 1 )
ft_putstr("\x1B[47m");
ft_putchar(32LL);
return ft_putstr("\x1B[0m");
}

original_code:
void show_carac(char c, int r, int f)
{
 int i;
 i = 0;
 while (g_show[i].c)
    {
  if (g_show[i].c == c)
  {
   if (r == 1)
    i += 14;
   if (f == 0)
   {
    ft_putstr(g_show[i].color);
    ft_putchar(g_show[i].show_c);
   }
   if (f == 1)
   {
    ft_putstr(g_finish[i].color);
    ft_putchar(g_finish[i].show_c);
   }
   ft_putstr("\033[0m");
   return ;
  }
  i++;
 }
 if (g_show[i].c == 0)
 {
  if (r == 1)
   ft_putstr("\033[47m");
  ft_putchar(' ');
  ft_putstr("\033[0m");
    }
}

variable_types: c char

variable_types: r int

variable_types: f int

return_type: void


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

ft_putnbr((unsigned int)(char)a1);
if ( a1 > 9 )
result = ft_putstr(" |");
else
result = ft_putstr(" |");
return result;
}

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

ft_putnbr((unsigned int)(char)a1);
if ( a1 > 9 )
result = ft_putstr(" |");
else
result = ft_putstr(" |");
return result;
}

original_code:
void show_beg_of_line(int line)
{
 ft_putnbr(line);
 if (line < 10)
  ft_putstr("  |");
 else
  ft_putstr(" |");
}

variable_types: line int


decompiled_code:
__int64 __fastcall show_border_line(__int64 a1, char a2)
{
int v2; // ebx

v2 = 0;
ft_putstr(" *");
ft_putstr("\x1B[1;37m");
if ( *(int *)(a1 + 4) > 0 )
{
do
{
++v2;
ft_putchar((unsigned int)a2);
}
while ( *(_DWORD *)(a1 + 4) > v2 );
}
ft_putstr("\x1B[0m");
return ft_putstr("*\n");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char a2)
{
int v2; // ebx

v2 = 0;
ft_putstr(" *");
ft_putstr("\x1B[1;37m");
if ( *(int *)(a1 + 4) > 0 )
{
do
{
++v2;
ft_putchar((unsigned int)a2);
}
while ( *(_DWORD *)(a1 + 4) > v2 );
}
ft_putstr("\x1B[0m");
return ft_putstr("*\n");
}

original_code:
void show_border_line(t_size *size, char c)
{
  int i;
  i = 0;
  ft_putstr("   *");
  ft_putstr("\033[1;37m");
  while (i < size->l)
    {
      ft_putchar(c);
      i++;
    }
  ft_putstr("\033[0m");
  ft_putstr("*\n");
}

canonical_original_code:
void show_border_line(struct s_size *size, char c)
{
  int i;
  i = 0;
  ft_putstr("   *");
  ft_putstr("\033[1;37m");
  while (i < size->l)
    {
      ft_putchar(c);
      i++;
    }
  ft_putstr("\033[0m");
  ft_putstr("*\n");
}


decompiled_code:
__int64 __fastcall show_tab(_QWORD *a1, int *a2, _DWORD *a3)
{
int v4; // er12
__int64 v6; // rbx
char v7; // di

v4 = 0;
show_column_number((__int64)a2);
show_border_line((__int64)a2, 95);
if ( *a2 > 0 )
{
do
{
v6 = 0LL;
show_beg_of_line(v4 + 1);
if ( a2[1] > 0 )
{
do
{
v7 = *(_BYTE *)(*a1 + v6);
if ( *a3 == v4 && a3[1] == (_DWORD)v6 )
show_carac(v7, 1, 0);
else
show_carac(v7, 0, 0);
++v6;
}
while ( a2[1] > (int)v6 );
}
++v4;
++a1;
ft_putstr("|\n");
}
while ( *a2 > v4 );
}
return show_border_line((__int64)a2, 45);
}

original_code:
void show_tab(char **map, t_size *size, t_cursor *cursor)
{
 int i;
 int j;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
 {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   if (cursor->x == i && cursor->y == j)
    show_carac(map[i][j], 1 , 0);
   else
    show_carac(map[i][j], 0, 0);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
}

canonical_original_code:
void show_tab(char **map, struct s_size *size, struct s_cursor *cursor)
{
 int i;
 int j;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
 {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   if (cursor->x == i && cursor->y == j)
    show_carac(map[i][j], 1 , 0);
   else
    show_carac(map[i][j], 0, 0);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
}

variable_types:
map
char **

variable_types:
cursor
struct s_cursor { int x; int y; } *

variable_types: j int

user_defined_types:
struct s_size { int h; int l; int m; }
struct s_cursor { int x; int y; }


decompiled_code:
__int64 __fastcall show_finish(_QWORD *a1, int *a2, int a3)
{
int v5; // er13
__int64 v6; // rbx
char v7; // di
time_t timer; // [rsp+8h] [rbp-30h] BYREF

timer = time(&timer);
ft_putstr("\x1B[H\x1B[2J");
show_column_number((__int64)a2);
show_border_line((__int64)a2, 95);
if ( *a2 > 0 )
{
v5 = 0;
do
{
show_beg_of_line(++v5);
if ( a2[1] > 0 )
{
v6 = 0LL;
do
{
v7 = *(_BYTE *)(*a1 + v6++);
show_carac(v7, 0, 1);
}
while ( a2[1] > (int)v6 );
}
++a1;
ft_putstr("|\n");
}
while ( v5 < *a2 );
}
show_border_line((__int64)a2, 45);
if ( a3 )
ft_putstr_color("You lose the GAME... 2 times!\n", "\x1B[31m");
else
ft_putstr_color("You win the GAME!\n\n", "\x1B[32m");
ft_putstr("Made By Jchichep alias Bueno\n");
return quit();
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, int *a2, int a3)
{
int v5; // er13
__int64 v6; // rbx
char v7; // di
time_t timer; // [rsp+8h] [rbp-30h] BYREF

timer = time(&timer);
ft_putstr("\x1B[H\x1B[2J");
func0((__int64)a2);
func3((__int64)a2, 95);
if ( *a2 > 0 )
{
v5 = 0;
do
{
func2(++v5);
if ( a2[1] > 0 )
{
v6 = 0LL;
do
{
v7 = *(_BYTE *)(*a1 + v6++);
func1(v7, 0, 1);
}
while ( a2[1] > (int)v6 );
}
++a1;
ft_putstr("|\n");
}
while ( v5 < *a2 );
}
func3((__int64)a2, 45);
if ( a3 )
ft_putstr_color("You lose the GAME... 2 times!\n", "\x1B[31m");
else
ft_putstr_color("You win the GAME!\n\n", "\x1B[32m");
ft_putstr("Made By Jchichep alias Bueno\n");
return quit();
}

original_code:
void show_finish(char **map, t_size *size, int win, time_t *t)
{
 int i;
 int j;
 time_t res;
 double timer;
 res = time(&res);
 timer = difftime(res, *t);
 timer = timer;
 ft_putstr("\033[H\033[2J");
 win = win;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
    {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   show_carac(map[i][j], 0, 1);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
 if (win == 0)
 {
  ft_putstr_color("You win the GAME!\n\n", "\033[32m");
 }
 else
  ft_putstr_color("You lose the GAME... 2 times!\n", "\033[31m");
 ft_putstr("Made By Jchichep alias Bueno\n");
 quit();
}

canonical_original_code:
void show_finish(char **map, struct s_size *size, int win, long *t)
{
 int i;
 int j;
 long res;
 double timer;
 res = time(&res);
 timer = difftime(res, *t);
 timer = timer;
 ft_putstr("\033[H\033[2J");
 win = win;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
    {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   show_carac(map[i][j], 0, 1);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
 if (win == 0)
 {
  ft_putstr_color("You win the GAME!\n\n", "\033[32m");
 }
 else
  ft_putstr_color("You lose the GAME... 2 times!\n", "\033[31m");
 ft_putstr("Made By Jchichep alias Bueno\n");
 quit();
}

variable_types: win int

variable_types:
t
long *

variable_types: res long

variable_types: timer double


decompiled_code:
__int64 easy_level()
{
__int64 v1; // [rsp+4h] [rbp-14h] BYREF
int v2; // [rsp+Ch] [rbp-Ch]

v1 = 0xA0000000ALL;
v2 = 10;
ft_putstr("\x1B[?25l\n");
return start_game(&v1);
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v1; // [rsp+4h] [rbp-14h] BYREF
int v2; // [rsp+Ch] [rbp-Ch]

v1 = 0xA0000000ALL;
v2 = 10;
ft_putstr("\x1B[?25l\n");
return start_game(&v1);
}

original_code:
void easy_level()
{
 t_size size;
 size.h = 10;
 size.l = 10;
 size.m = 10;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void easy_level()
{
 struct s_size size;
 size.h = 10;
 size.l = 10;
 size.m = 10;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

variable_types:
size
struct s_size { int h; int l; int m; }


decompiled_code:
__int64 med_level()
{
__int64 v1; // [rsp+4h] [rbp-14h] BYREF
int v2; // [rsp+Ch] [rbp-Ch]

v1 = 0x1000000010LL;
v2 = 40;
ft_putstr("\x1B[?25l\n");
return start_game(&v1);
}

original_code:
void med_level()
{
 t_size size;
 size.h = 16;
 size.l = 16;
 size.m = 40;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void med_level()
{
 struct s_size size;
 size.h = 16;
 size.l = 16;
 size.m = 40;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}


decompiled_code:
__int64 hard_level()
{
__int64 v1; // [rsp+4h] [rbp-14h] BYREF
int v2; // [rsp+Ch] [rbp-Ch]

v1 = 0x1E00000010LL;
v2 = 99;
ft_putstr("\x1B[?25l\n");
return start_game(&v1);
}

original_code:
void hard_level()
{
 t_size size;
 size.h = 16;
 size.l = 30;
 size.m = 99;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void hard_level()
{
 struct s_size size;
 size.h = 16;
 size.l = 30;
 size.m = 99;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}


decompiled_code:
_BOOL8 __fastcall verify_arg(_DWORD *a1)
{
_BOOL4 v1; // er8
int v2; // edx

v1 = 0;
if ( (unsigned int)(*a1 - 1) <= 0x27 )
{
v2 = a1[1];
if ( (unsigned int)(v2 - 1) <= 0x4F )
v1 = a1[2] < v2 * *a1;
}
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_DWORD *a1)
{
_BOOL4 v1; // er8
int v2; // edx

v1 = 0;
if ( (unsigned int)(*a1 - 1) <= 0x27 )
{
v2 = a1[1];
if ( (unsigned int)(v2 - 1) <= 0x4F )
v1 = a1[2] < v2 * *a1;
}
return v1;
}

original_code:
int verify_arg(t_size *size)
{
 if (size->h < 1 || size->h > 40)
  return (0);
 if (size->l < 1 || size->l > 80)
  return (0);
 if (size->m >= size->h * size->l)
  return (0);
 return (1);
}

canonical_original_code:
int verify_arg(struct s_size *size)
{
 if (size->h < 1 || size->h > 40)
  return (0);
 if (size->l < 1 || size->l > 80)
  return (0);
 if (size->m >= size->h * size->l)
  return (0);
 return (1);
}

return_type: int


decompiled_code:
__int64 custom_level()
{
void *v0; // rbx
_BOOL4 v1; // eax
int v3[5]; // [rsp+4h] [rbp-14h] BYREF

unset_raw_mode();
v0 = malloc(0x400uLL);
while ( 1 )
{
ft_bzero(v0, 1024LL);
ft_putstr("Quelle hauteur? (maximum 40)\n");
read(0, v0, 0x3FFuLL);
v3[0] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Quelle longueur? (maximum 80)\n");
read(0, v0, 0x3FFuLL);
v3[1] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
read(0, v0, 0x3FFuLL);
v3[2] = ft_atoi(v0);
v1 = verify_arg(v3);
if ( v1 )
break;
if ( !v1 )
ft_putstr("Valeur incorrecte \n");
}
set_raw_mode();
ft_putstr("\x1B[?25l\n");
return start_game(v3);
}

canonical_decompiled_code:
__int64 func4()
{
void *v0; // rbx
_BOOL4 v1; // eax
int v3[5]; // [rsp+4h] [rbp-14h] BYREF

unset_raw_mode();
v0 = malloc(0x400uLL);
while ( 1 )
{
ft_bzero(v0, 1024LL);
ft_putstr("Quelle hauteur? (maximum 40)\n");
read(0, v0, 0x3FFuLL);
v3[0] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Quelle longueur? (maximum 80)\n");
read(0, v0, 0x3FFuLL);
v3[1] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
read(0, v0, 0x3FFuLL);
v3[2] = ft_atoi(v0);
v1 = func3(v3);
if ( v1 )
break;
if ( !v1 )
ft_putstr("Valeur incorrecte \n");
}
set_raw_mode();
ft_putstr("\x1B[?25l\n");
return start_game(v3);
}

original_code:
void custom_level()
{
 t_size size;
 int verify;
 char *str;
 unset_raw_mode();
 str = malloc(sizeof(*str) * 1024);
 verify = -1;
 while (verify != 1)
 {
  if (verify == 0)
   ft_putstr("Valeur incorrecte \n");
  ft_bzero(str, 1024);
  ft_putstr("Quelle hauteur? (maximum 40)\n");
  read(0, str, 1023);
  size.h = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Quelle longueur? (maximum 80)\n");
  read(0, str, 1023);
  size.l = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
  read(0, str, 1023);
  size.m = ft_atoi(str);
  verify = verify_arg(&size);
 }
 set_raw_mode();
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void custom_level()
{
 struct s_size size;
 int verify;
 char *str;
 unset_raw_mode();
 str = malloc(sizeof(*str) * 1024);
 verify = -1;
 while (verify != 1)
 {
  if (verify == 0)
   ft_putstr("Valeur incorrecte \n");
  ft_bzero(str, 1024);
  ft_putstr("Quelle hauteur? (maximum 40)\n");
  read(0, str, 1023);
  size.h = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Quelle longueur? (maximum 80)\n");
  read(0, str, 1023);
  size.l = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
  read(0, str, 1023);
  size.m = ft_atoi(str);
  verify = verify_arg(&size);
 }
 set_raw_mode();
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

variable_types: verify int

variable_types:
str
char *


quit

decompiled_code:
void __noreturn quit()
{
unset_raw_mode();
ft_putstr("\x1B[?12l\x1B[?25h");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
unset_raw_mode();
ft_putstr("\x1B[?12l\x1B[?25h");
exit(0);
}


main

canonical_name: main

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

_sysv_signal(2, (__sighandler_t)&catch_sigint);
v3 = show_menu();
get_option(v3, &g_option);
unset_raw_mode();
return 0;
}

original_code:
int main()
{
 int lvl;
 signal(2, catch_sigint);
 lvl = show_menu();
 get_option(lvl, g_option);
 unset_raw_mode();
 return (0);
}

variable_types: lvl int


original_code:
int check_case(char **map, t_cursor *cursor, t_size *size)
{
 int ret;
 int i;
 int j;
 ret = 0;
 i = cursor->x - 1;
 while (i < cursor->x + 2)
 {
  j = cursor->y - 1;
  while (j < cursor->y + 2)
  {
   if (!(i < 0 || j < 0 || i >= size->h || j >= size->l)
    && (map[i][j] == 'X' || map[i][j] == 'F'))
    ret++;
   j++;
  }
  i++;
 }
 return (ret);
}

canonical_original_code:
int check_case(char **map, struct s_cursor *cursor, struct s_size *size)
{
 int ret;
 int i;
 int j;
 ret = 0;
 i = cursor->x - 1;
 while (i < cursor->x + 2)
 {
  j = cursor->y - 1;
  while (j < cursor->y + 2)
  {
   if (!(i < 0 || j < 0 || i >= size->h || j >= size->l)
    && (map[i][j] == 'X' || map[i][j] == 'F'))
    ret++;
   j++;
  }
  i++;
 }
 return (ret);
}

variable_types: struct s_size { int h; int l; int m; } *

variable_types: ret int

user_defined_types:
struct s_cursor { int x; int y; }
struct s_size { int h; int l; int m; }


finish

original_code:
int finish(char **map, t_size *size)
{
 int i;
 int j;
 i = 0;
 j = 0;
 while (i < size->h)
 {
  while (j < size->l)
  {
   if (map[i][j] == ' ' || map[i][j] == 'W')
    return (0);
   j++;
  }
  j = 0;
  i++;
 }
 return (1);
}

canonical_original_code:
int finish(char **map, struct s_size *size)
{
 int i;
 int j;
 i = 0;
 j = 0;
 while (i < size->h)
 {
  while (j < size->l)
  {
   if (map[i][j] == ' ' || map[i][j] == 'W')
    return (0);
   j++;
  }
  j = 0;
  i++;
 }
 return (1);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ft_atoi(_BYTE *a1)
{
_BYTE *v1; // rdi
int v2; // edx
__int64 result; // rax
int v4; // eax

v1 = &a1[*a1 == 45];
v2 = (char)*v1;
for ( result = 0LL; (unsigned __int8)(*v1 - 48) <= 9u; result = (unsigned int)(v4 + 48) )
{
++v1;
v4 = 10 * result - v2;
v2 = (char)*v1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
_BYTE *v1; // rdi
int v2; // edx
__int64 result; // rax
int v4; // eax

v1 = &a1[*a1 == 45];
v2 = (char)*v1;
for ( result = 0LL; (unsigned __int8)(*v1 - 48) <= 9u; result = (unsigned int)(v4 + 48) )
{
++v1;
v4 = 10 * result - v2;
v2 = (char)*v1;
}
return result;
}

original_code:
int ft_atoi(char *str)
{
 int ret;
 int i;
 int neg;
 i = 0;
 if (str[0] == '-')
 {
  neg = 1;
  i = 1;
 }
 ret = 0;
 while (str[i] >= '0' && str[i] <= '9')
 {
  if (neg == 1)
   ret = ret * 10 - str[i] + '0';
  else
   ret = ret * 10 + str[i] - '0';
  i++;
 }
 return (ret);
}

variable_types: neg int


decompiled_code:
ssize_t __fastcall ft_putchar(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func1(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

original_code:
void ft_putchar(char c)
{
 (void)write(1, &c, 1);
}

variable_types: c char


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

if ( a2 > 0 )
result = memset(a1, 0, a2);
return result;
}

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

if ( a2 > 0 )
result = memset(a1, 0, a2);
return result;
}

original_code:
void ft_bzero(char *str, int nb)
{
 int i;
 i = 0;
 while (i < nb)
 {
  str[i++] = 0;
 }
}

variable_types: nb int


decompiled_code:
__int64 __fastcall ft_strlen(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // er8

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // er8

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

original_code:
int ft_strlen(char *str)
{
 int i;
 i = 0;
 while (str[i])
 {
  i++;
 }
 return (i);
}


decompiled_code:
ssize_t __fastcall ft_putstr(void *buf)
{
int v1; // eax

v1 = ft_strlen(buf);
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func4(void *buf)
{
int v1; // eax

v1 = func3(buf);
return write(1, buf, v1);
}

original_code:
void ft_putstr(char *str)
{
 (void)write(1, str, ft_strlen(str));
}


decompiled_code:
ssize_t __fastcall ft_putnbr(int a1)
{
int v1; // ebx

v1 = a1;
if ( a1 == 0x80000000 )
{
ft_putstr("-2147483648");
}
else if ( a1 >= 0 )
{
goto LABEL_3;
}
v1 = -a1;
ft_putchar(45);
LABEL_3:
if ( v1 > 9 )
ft_putnbr((unsigned int)v1 / 0xAuLL);
return ft_putchar((unsigned __int8)(v1 % 0xAu) + 48);
}

canonical_decompiled_code:
ssize_t __fastcall func5(int a1)
{
int v1; // ebx

v1 = a1;
if ( a1 == 0x80000000 )
{
func4("-2147483648");
}
else if ( a1 >= 0 )
{
goto LABEL_3;
}
v1 = -a1;
func1(45);
LABEL_3:
if ( v1 > 9 )
func5((unsigned int)v1 / 0xAuLL);
return func1((unsigned __int8)(v1 % 0xAu) + 48);
}


decompiled_code:
ssize_t __fastcall ft_putstr_color(void *buf, void *a2)
{
ft_putstr(a2);
ft_putstr(buf);
return ft_putstr("\x1B[0m");
}

canonical_decompiled_code:
ssize_t __fastcall func6(void *buf, void *a2)
{
func4(a2);
func4(buf);
return func4("\x1B[0m");
}

original_code:
void ft_putstr_color(char *str, char *color)
{
 ft_putstr(color);
 ft_putstr(str);
 ft_putstr("\033[0m");
}

variable_types:
color
char *


original_code:
int check_map(char **map, t_cursor *cursor, t_size *size)
{
 char nb;
 t_cursor temp;
 temp.x = cursor->x - 1;
 temp.y = cursor->y - 1;
 if (cursor->x < 0 || cursor->y < 0
  || cursor->x >= size->h || cursor->y >= size->l)
  return (0);
 if (map[cursor->x][cursor->y] != ' ' && map[cursor->x][cursor->y] != 'X')
  return (0);
 nb = '0';
 if (map[cursor->x][cursor->y] == 'X')
 {
  map[cursor->x][cursor->y] = 'B';
  return (1);
 }
 nb += check_case(map, cursor, size);
 map[cursor->x][cursor->y] = nb;
 if (nb == '0')
 {
  map[cursor->x][cursor->y] = '-';
  while (temp.x < cursor->x + 2)
  {
   temp.y = cursor->y - 1;
   while (temp.y < cursor->y + 2)
   {
    check_map(map, &temp, size);
    temp.y++;
   }
   temp.x++;
  }
 }
 return (0);
}

canonical_original_code:
int check_map(char **map, struct s_cursor *cursor, struct s_size *size)
{
 char nb;
 struct s_cursor temp;
 temp.x = cursor->x - 1;
 temp.y = cursor->y - 1;
 if (cursor->x < 0 || cursor->y < 0
  || cursor->x >= size->h || cursor->y >= size->l)
  return (0);
 if (map[cursor->x][cursor->y] != ' ' && map[cursor->x][cursor->y] != 'X')
  return (0);
 nb = '0';
 if (map[cursor->x][cursor->y] == 'X')
 {
  map[cursor->x][cursor->y] = 'B';
  return (1);
 }
 nb += check_case(map, cursor, size);
 map[cursor->x][cursor->y] = nb;
 if (nb == '0')
 {
  map[cursor->x][cursor->y] = '-';
  while (temp.x < cursor->x + 2)
  {
   temp.y = cursor->y - 1;
   while (temp.y < cursor->y + 2)
   {
    check_map(map, &temp, size);
    temp.y++;
   }
   temp.x++;
  }
 }
 return (0);
}

variable_types:
map
char **

variable_types:
cursor
struct s_cursor { int x; int y; } *

variable_types: nb char

variable_types:
temp
struct s_cursor { int x; int y; }


move

decompiled_code:
void move()
{
JUMPOUT(0x122LL);
}

canonical_decompiled_code:
void func1()
{
JUMPOUT(0x122LL);
}

original_code:
int move(int key, t_cursor *cursor, t_size *size, char **map)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (i == 4 && g_option[i].key == key)
   quit();
  else if (i == 5 && g_option[i].key == key)
   flag(map, cursor);
  else if (g_option[i].key == key)
  {
   g_option[i].ptr_func(cursor, size);
   return (0);
  }
  i++;
 }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

canonical_original_code:
int move(int key, struct s_cursor *cursor, struct s_size *size, char **map)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (i == 4 && g_option[i].key == key)
   quit();
  else if (i == 5 && g_option[i].key == key)
   flag(map, cursor);
  else if (g_option[i].key == key)
  {
   g_option[i].ptr_func(cursor, size);
   return (0);
  }
  i++;
 }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

variable_types: key int


original_code:
void set_mines(char **map, t_size *size)
{
 int mh;
 int mw;
 int monboard;
 srand(time(0));
 monboard = 0;
 while (monboard < size->m)
 {
  mh = rand() % size->h;
  mw = rand() % size->l;
  if (map[mh][mw] == ' ')
  {
   map[mh][mw] = 'X';
   monboard++;
  }
 }
}

canonical_original_code:
void set_mines(char **map, struct s_size *size)
{
 int mh;
 int mw;
 int monboard;
 srand(time(0));
 monboard = 0;
 while (monboard < size->m)
 {
  mh = rand() % size->h;
  mw = rand() % size->l;
  if (map[mh][mw] == ' ')
  {
   map[mh][mw] = 'X';
   monboard++;
  }
 }
}

variable_types: mh int

variable_types: mw int

variable_types: monboard int


decompiled_code:
__int64 __fastcall start_game(int *a1)
{
int v1; // ebx
_QWORD *v2; // r12
time_t v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = 0;
time(&v4);
v5[0] = 0LL;
v2 = setmap(a1);
while ( !(unsigned int)finish(v2, a1) )
{
ft_putstr("\x1B[H\x1B[2J");
show_tab(v2, a1, v5);
if ( (unsigned int)get_key() == 10 )
{
if ( !v1 )
{
*(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) = 48;
set_mines((__int64)v2, a1);
*(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) = 32;
}
v1 = 1;
check_map((__int64)v2, (int *)v5, a1);
if ( *(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) == 66 )
show_finish(v2, a1, 1LL, &v4);
}
move();
}
return show_finish(v2, a1, 0LL, &v4);
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1)
{
int v1; // ebx
_QWORD *v2; // r12
time_t v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = 0;
time(&v4);
v5[0] = 0LL;
v2 = setmap(a1);
while ( !(unsigned int)finish(v2, a1) )
{
ft_putstr("\x1B[H\x1B[2J");
show_tab(v2, a1, v5);
if ( (unsigned int)get_key() == 10 )
{
if ( !v1 )
{
*(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) = 48;
func2((__int64)v2, a1);
*(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) = 32;
}
v1 = 1;
func0((__int64)v2, (int *)v5, a1);
if ( *(_BYTE *)(v2[SLODWORD(v5[0])] + SHIDWORD(v5[0])) == 66 )
show_finish(v2, a1, 1LL, &v4);
}
func1();
}
return show_finish(v2, a1, 0LL, &v4);
}

original_code:
void start_game(t_size *size)
{
 char **tab;
 t_cursor cursor;
 int key;
 int setmines;
 time_t t;
 time(&t);
 setmines = 0;
 cursor.x = 0;
 cursor.y = 0;
 tab = setmap(size);
 while (finish(tab, size) == 0)
 {
  ft_putstr("\033[H\033[2J");
  show_tab(tab, size, &cursor);
  key = get_key();
  if (key == '\n')
  {
   if (setmines == 0)
   {
    tab[cursor.x][cursor.y] = '0';
    set_mines(tab, size);
    tab[cursor.x][cursor.y] = ' ';
    setmines = 1;
   }
   check_map(tab, &cursor, size);
   if (tab[cursor.x][cursor.y] == 'B')
    show_finish(tab, size, 1, &t);
  }
  move(key, &cursor, size, tab);
 }
 show_finish(tab, size, 0, &t);
}

canonical_original_code:
void start_game(struct s_size *size)
{
 char **tab;
 struct s_cursor cursor;
 int key;
 int setmines;
 long t;
 time(&t);
 setmines = 0;
 cursor.x = 0;
 cursor.y = 0;
 tab = setmap(size);
 while (finish(tab, size) == 0)
 {
  ft_putstr("\033[H\033[2J");
  show_tab(tab, size, &cursor);
  key = get_key();
  if (key == '\n')
  {
   if (setmines == 0)
   {
    tab[cursor.x][cursor.y] = '0';
    set_mines(tab, size);
    tab[cursor.x][cursor.y] = ' ';
    setmines = 1;
   }
   check_map(tab, &cursor, size);
   if (tab[cursor.x][cursor.y] == 'B')
    show_finish(tab, size, 1, &t);
  }
  move(key, &cursor, size, tab);
 }
 show_finish(tab, size, 0, &t);
}

variable_types:
tab
char **

variable_types:
cursor
struct s_cursor { int x; int y; }

variable_types: setmines int

variable_types: t long


decompiled_code:
void catch_sigint()
{
ft_putstr("Please use q to quit the program\n");
_sysv_signal(2, catch_sigint);
}

canonical_decompiled_code:
void func0()
{
ft_putstr("Please use q to quit the program\n");
_sysv_signal(2, catch_sigint);
}

original_code:
void catch_sigint(int sig)
{
 sig = sig;
 ft_putstr("Please use q to quit the program\n");
 signal(2, catch_sigint);
}

variable_types: sig int


decompiled_code:
__int64 __fastcall get_option(int a1, __int64 a2)
{
while ( 1 )
{
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
if ( a1 == *(_DWORD *)a2 )
break;
a2 += 16LL;
}
(*(void (**)(void))(a2 + 8))();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
while ( 1 )
{
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
if ( a1 == *(_DWORD *)a2 )
break;
a2 += 16LL;
}
(*(void (**)(void))(a2 + 8))();
return 0LL;
}

original_code:
int get_option(int key, t_opt *g_option)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (g_option[i].key == key)
  {
   g_option[i].ptr_func();
   return (0);
  }
  i++;
    }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

canonical_original_code:
int get_option(int key, struct s_option *g_option)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (g_option[i].key == key)
  {
   g_option[i].ptr_func();
   return (0);
  }
  i++;
    }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

variable_types: struct s_option { int key; void (*ptr_func)(); } *

variable_types: i int


decompiled_code:
int unset_raw_mode()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag += 10;
*(_WORD *)&v1.c_cc[5] = 256;
return tcsetattr(0, 0, &v1);
}

canonical_decompiled_code:
int func2()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag += 10;
*(_WORD *)&v1.c_cc[5] = 256;
return tcsetattr(0, 0, &v1);
}

original_code:
void unset_raw_mode()
{
 struct termios t;
 tcgetattr(0, &t);
 t.c_lflag += 0000002;
 t.c_lflag += 0000010;
 t.c_cc[6] = 1;
 t.c_cc[5] = 0;
 tcsetattr(0, 0, &t);
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int set_raw_mode()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag &= 0xFFFFFFF5;
*(_WORD *)&v1.c_cc[5] = 256;
return tcsetattr(0, 0, &v1);
}

canonical_decompiled_code:
int func3()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag &= 0xFFFFFFF5;
*(_WORD *)&v1.c_cc[5] = 256;
return tcsetattr(0, 0, &v1);
}

original_code:
void set_raw_mode()
{
 struct termios t;
 tcgetattr(0, &t);
 t.c_lflag &= ~0000002;
 t.c_lflag &= ~0000010;
 t.c_cc[6] = 1;
 t.c_cc[5] = 0;
 tcsetattr(0, 0, &t);
}

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
__int64 get_key()
{
__int64 result; // rax
char buf; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah] BYREF
char v3[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, &buf, 1uLL);
result = (unsigned int)buf;
if ( buf == 27 )
{
read(0, &v2, 1uLL);
if ( v2 == 91 )
read(0, v3, 1uLL);
result = (unsigned int)buf;
if ( buf == 27 && v2 == 91 )
{
switch ( v3[0] )
{
case 'B':
result = 256LL;
break;
case 'A':
result = 257LL;
break;
case 'D':
result = 258LL;
break;
case 'C':
result = 259LL;
break;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
char buf; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah] BYREF
char v3[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, &buf, 1uLL);
result = (unsigned int)buf;
if ( buf == 27 )
{
read(0, &v2, 1uLL);
if ( v2 == 91 )
read(0, v3, 1uLL);
result = (unsigned int)buf;
if ( buf == 27 && v2 == 91 )
{
switch ( v3[0] )
{
case 'B':
result = 256LL;
break;
case 'A':
result = 257LL;
break;
case 'D':
result = 258LL;
break;
case 'C':
result = 259LL;
break;
}
}
}
return result;
}

variable_types:
tab
char [3]


decompiled_code:
__int64 show_menu()
{
int v0; // eax

ft_putstr_color("Press 1 for easy\n", "\x1B[32m");
ft_putstr_color("Press 2 for medium\n", "\x1B[33m");
ft_putstr_color("Press 3 for hard\n", "\x1B[31m");
ft_putstr_color("Press 4 for custom\n", "\x1B[34m");
set_raw_mode();
do
v0 = get_key();
while ( (unsigned int)(v0 - 49) > 3 && v0 != 113 );
return (unsigned int)(v0 - 48);
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // eax

ft_putstr_color("Press 1 for easy\n", "\x1B[32m");
ft_putstr_color("Press 2 for medium\n", "\x1B[33m");
ft_putstr_color("Press 3 for hard\n", "\x1B[31m");
ft_putstr_color("Press 4 for custom\n", "\x1B[34m");
set_raw_mode();
do
v0 = func0();
while ( (unsigned int)(v0 - 49) > 3 && v0 != 113 );
return (unsigned int)(v0 - 48);
}

original_code:
int show_menu()
{
 int key;
 key = 0;
 ft_putstr_color("Press 1 for easy\n", "\033[32m");
 ft_putstr_color("Press 2 for medium\n", "\033[33m");
 ft_putstr_color("Press 3 for hard\n", "\033[31m");
 ft_putstr_color("Press 4 for custom\n", "\033[34m");
 set_raw_mode();
 while ((key < '1' || key > '4') && key != 'q')
    {
  key = get_key();
    }
 return (key - '0');
}


repo: kevinriehm/evt-black-box

decompiled_code:
__int64 is_alive()
{
int v0; // edi
int v1; // edi
FILE *v3; // rax

pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 258);
v0 = pidfd;
if ( pidfd < 0 )
{
pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 256);
v0 = pidfd;
}
pidfp = fdopen(v0, "w");
if ( pidfp )
{
v1 = pidfd;
if ( pidfd >= 0 )
return (unsigned int)flock(v1, 6) >> 31;
}
else
{
v3 = fdopen(pidfd, "r");
v1 = pidfd;
pidfp = v3;
if ( pidfd >= 0 && v3 )
return (unsigned int)flock(v1, 6) >> 31;
}
syslog(4, "cannot open PID file (%m)");
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // edi
int v1; // edi
FILE *v3; // rax

pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 258);
v0 = pidfd;
if ( pidfd < 0 )
{
pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 256);
v0 = pidfd;
}
pidfp = fdopen(v0, "w");
if ( pidfp )
{
v1 = pidfd;
if ( pidfd >= 0 )
return (unsigned int)flock(v1, 6) >> 31;
}
else
{
v3 = fdopen(pidfd, "r");
v1 = pidfd;
pidfp = v3;
if ( pidfd >= 0 && v3 )
return (unsigned int)flock(v1, 6) >> 31;
}
syslog(4, "cannot open PID file (%m)");
return 0LL;
}

original_code:
int is_alive() {
 pidfd = open("/var/opt/staevt.com/angel""/.angeld.pid",02 | 0400);
 if(pidfd < 0) pidfd = open("/var/opt/staevt.com/angel""/.angeld.pid",00 | 0400);
 pidfp = fdopen(pidfd,"w");
 if(!pidfp) pidfp = fdopen(pidfd,"r");
 if(pidfd < 0 || !pidfp) {
  syslog(4,"cannot open PID file (%m)");
  return 0;
 }
 return flock(pidfd,2 | 4) < 0;
}


decompiled_code:
__int64 __fastcall try_open(const char *a1, int a2)
{
int v2; // er12
struct termios termios_p; // [rsp+0h] [rbp-50h] BYREF

v2 = open(a1, a2);
if ( v2 < 0 )
{
syslog(4, "cannot open '%s' (%m)", a1);
}
else
{
syslog(5, "opening '%s'", a1);
*(_OWORD *)&termios_p.c_iflag = 0LL;
*(_OWORD *)&termios_p.c_line = 0LL;
*(_OWORD *)&termios_p.c_cc[15] = 0LL;
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0xDu);
cfsetispeed(&termios_p, 0xDu);
termios_p.c_iflag |= 1u;
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
tcsetattr(v2, 0, &termios_p);
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2)
{
int v2; // er12
struct termios termios_p; // [rsp+0h] [rbp-50h] BYREF

v2 = open(a1, a2);
if ( v2 < 0 )
{
syslog(4, "cannot open '%s' (%m)", a1);
}
else
{
syslog(5, "opening '%s'", a1);
*(_OWORD *)&termios_p.c_iflag = 0LL;
*(_OWORD *)&termios_p.c_line = 0LL;
*(_OWORD *)&termios_p.c_cc[15] = 0LL;
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0xDu);
cfsetispeed(&termios_p, 0xDu);
termios_p.c_iflag |= 1u;
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
tcsetattr(v2, 0, &termios_p);
}
return (unsigned int)v2;
}

original_code:
int try_open(char *path, int flags) {
 int fd;
 struct termios serialios;
 fd = open(path,flags);
 if(fd < 0) syslog(4,"cannot open '%s' (%m)",path);
 else {
  syslog(5,"opening '%s'",path);
  memset(&serialios,0,sizeof(serialios));
  cfsetospeed(&serialios,0000015);
  cfsetispeed(&serialios,0000015);
  serialios.c_cflag |= 0000060;
  serialios.c_iflag |= 0000001;
  serialios.c_lflag |= 0000200;
  tcsetattr(fd,0,&serialios);
 }
 return fd;
}

variable_types:
path
char *

variable_types: flags int

variable_types: fd int


decompiled_code:
int find_serial()
{
__int64 v0; // rbx
int v1; // er12
int result; // eax
int v3; // eax
struct pollfd fds; // [rsp+8h] [rbp-20A8h] BYREF
glob_t pglob; // [rsp+10h] [rbp-20A0h] BYREF
_BYTE v6[5]; // [rsp+60h] [rbp-2050h] BYREF
char dest[4107]; // [rsp+65h] [rbp-204Bh] BYREF
char buf[4]; // [rsp+1070h] [rbp-1040h] BYREF
int v9; // [rsp+1074h] [rbp-103Ch]
char src[4144]; // [rsp+1080h] [rbp-1030h] BYREF

v0 = 0LL;
v1 = inotify_add_watch(inotifyfd, "/dev", 0x100u);
syslog(5, "looking for serial device");
glob("/dev/ttyACM*", 0, 0LL, &pglob);
if ( pglob.gl_pathc )
{
while ( 1 )
{
result = try_open(pglob.gl_pathv[v0], 256);
serialfd = result;
if ( result >= 0 )
break;
if ( pglob.gl_pathc <= ++v0 )
goto LABEL_6;
}
}
else
{
LABEL_6:
globfree(&pglob);
syslog(6, "nothing there, using inotify");
fds.events = 1;
fds.fd = inotifyfd;
if ( serialfd < 0 )
{
do
{
while ( 1 )
{
poll(&fds, 1uLL, -1);
if ( (int)read(inotifyfd, buf, 0x1010uLL) >= 0 )
break;
syslog(4, "read error (%m)");
if ( serialfd >= 0 )
goto LABEL_13;
}
v3 = serialfd;
if ( !((memcmp(src, "ttyACM", 6uLL) != 0) | v9 & 0x40000000) )
{
qmemcpy(v6, "/dev/", sizeof(v6));
strcpy(dest, src);
v3 = try_open(v6, 256);
serialfd = v3;
}
}
while ( v3 < 0 );
LABEL_13:
result = inotify_rm_watch(inotifyfd, v1);
}
else
{
result = inotify_rm_watch(inotifyfd, v1);
}
}
return result;
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rbx
int v1; // er12
int result; // eax
int v3; // eax
struct pollfd fds; // [rsp+8h] [rbp-20A8h] BYREF
glob_t pglob; // [rsp+10h] [rbp-20A0h] BYREF
_BYTE v6[5]; // [rsp+60h] [rbp-2050h] BYREF
char dest[4107]; // [rsp+65h] [rbp-204Bh] BYREF
char buf[4]; // [rsp+1070h] [rbp-1040h] BYREF
int v9; // [rsp+1074h] [rbp-103Ch]
char src[4144]; // [rsp+1080h] [rbp-1030h] BYREF

v0 = 0LL;
v1 = inotify_add_watch(inotifyfd, "/dev", 0x100u);
syslog(5, "looking for serial device");
glob("/dev/ttyACM*", 0, 0LL, &pglob);
if ( pglob.gl_pathc )
{
while ( 1 )
{
result = func2(pglob.gl_pathv[v0], 256);
serialfd = result;
if ( result >= 0 )
break;
if ( pglob.gl_pathc <= ++v0 )
goto LABEL_6;
}
}
else
{
LABEL_6:
globfree(&pglob);
syslog(6, "nothing there, using inotify");
fds.events = 1;
fds.fd = inotifyfd;
if ( serialfd < 0 )
{
do
{
while ( 1 )
{
poll(&fds, 1uLL, -1);
if ( (int)read(inotifyfd, buf, 0x1010uLL) >= 0 )
break;
syslog(4, "read error (%m)");
if ( serialfd >= 0 )
goto LABEL_13;
}
v3 = serialfd;
if ( !((memcmp(src, "ttyACM", 6uLL) != 0) | v9 & 0x40000000) )
{
qmemcpy(v6, "/dev/", sizeof(v6));
strcpy(dest, src);
v3 = func2(v6, 256);
serialfd = v3;
}
}
while ( v3 < 0 );
LABEL_13:
result = inotify_rm_watch(inotifyfd, v1);
}
else
{
result = inotify_rm_watch(inotifyfd, v1);
}
}
return result;
}

original_code:
void find_serial() {
 int wd;
 glob_t g;
 int nread, i;
 struct pollfd pollfd;
 struct inotify_event *e;
 char buf[sizeof(struct inotify_event) + 4096 + 1],
  path[4096 + 1];
 wd = inotify_add_watch(inotifyfd,"/dev",0x00000100);
 e = (struct inotify_event *) buf;
 syslog(5,"looking for serial device");
 glob("/dev/""ttyACM""*",0,((void *)0),&g);
 for(i = 0; i < g.gl_pathc; i++) {
  serialfd = try_open(g.gl_pathv[i],00 | 0400);
  if(serialfd >= 0) return;
 }
 globfree(&g);
 syslog(6,"nothing there, using inotify");
 pollfd.fd = inotifyfd;
 pollfd.events = 0x001;
 while(serialfd < 0) {
  poll(&pollfd,1,-1);
  nread = read(inotifyfd,e,sizeof(struct inotify_event)
   + 4096);
  if(nread < 0) syslog(4,"read error (%m)");
  else if(strncmp(e->name,"ttyACM",strlen("ttyACM")) == 0
    && !(e->mask & 0x40000000)) {
   strcpy(path,"/dev/");
   strcat(path,e->name);
   serialfd = try_open(path,00 | 0400);
  }
 }
 inotify_rm_watch(inotifyfd,wd);
}

canonical_original_code:
void find_serial() {
 int wd;
 struct glob_t g;
 int nread; int i;
 struct pollfd pollfd;
 struct inotify_event *e;
 char buf[-1]; char path[-1];
 wd = inotify_add_watch(inotifyfd,"/dev",0x00000100);
 e = (struct inotify_event *) buf;
 syslog(5,"looking for serial device");
 glob("/dev/""ttyACM""*",0,((void *)0),&g);
 for(i = 0; i < g.gl_pathc; i++) {
  serialfd = try_open(g.gl_pathv[i],00 | 0400);
  if(serialfd >= 0) return;
 }
 globfree(&g);
 syslog(6,"nothing there, using inotify");
 pollfd.fd = inotifyfd;
 pollfd.events = 0x001;
 while(serialfd < 0) {
  poll(&pollfd,1,-1);
  nread = read(inotifyfd,e,sizeof(struct inotify_event)
   + 4096);
  if(nread < 0) syslog(4,"read error (%m)");
  else if(strncmp(e->name,"ttyACM",strlen("ttyACM")) == 0
    && !(e->mask & 0x40000000)) {
   strcpy(path,"/dev/");
   strcat(path,e->name);
   serialfd = try_open(path,00 | 0400);
  }
 }
 inotify_rm_watch(inotifyfd,wd);
}

variable_types: wd int

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

variable_types: nread int

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

variable_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; } *

variable_types:
buf
char [-1]

variable_types:
path
char [-1]


decompiled_code:
void kill_daemon()
{
__pid_t pid; // [rsp+Ch] [rbp-4h] BYREF

if ( pidfp )
{
_isoc99_fscanf(pidfp, "%i", &pid);
if ( pid > 0 )
{
syslog(6, "sending SIGTERM to old instance");
kill(pid, 15);
}
}
else
{
syslog(4, "PID file not open");
}
}

canonical_decompiled_code:
void func4()
{
__pid_t pid; // [rsp+Ch] [rbp-4h] BYREF

if ( pidfp )
{
_isoc99_fscanf(pidfp, "%i", &pid);
if ( pid > 0 )
{
syslog(6, "sending SIGTERM to old instance");
kill(pid, 15);
}
}
else
{
syslog(4, "PID file not open");
}
}

original_code:
void kill_daemon() {
 pid_t pid;
 if(!pidfp) {
  syslog(4,"PID file not open");
  return;
 }
 fscanf(pidfp,"%i",&pid);
 if(pid > 0) {
  syslog(6,"sending SIGTERM to old instance");
  kill(pid,15);
 }
}

canonical_original_code:
void kill_daemon() {
 int pid;
 if(!pidfp) {
  syslog(4,"PID file not open");
  return;
 }
 fscanf(pidfp,"%i",&pid);
 if(pid > 0) {
  syslog(6,"sending SIGTERM to old instance");
  kill(pid,15);
 }
}

variable_types: pid int


decompiled_code:
int make_daemon()
{
__pid_t v0; // eax
unsigned int v1; // eax
int v2; // ebx

v0 = fork();
if ( v0 < 0 )
{
syslog(4, "cannot fork");
}
else if ( v0 )
{
exit(0);
}
syslog(6, "now operating as a daemon");
v1 = getpid();
v2 = fprintf(pidfp, "%i\n", v1);
if ( v2 <= 0 )
syslog(4, "cannot write PID to file (%m)");
ftruncate(pidfd, v2);
fflush(pidfp);
fsync(pidfd);
if ( setsid() < 0 )
syslog(4, "cannot set sid");
if ( chdir("/") < 0 )
syslog(4, "cannot change directory");
close(0);
close(1);
return close(2);
}

canonical_decompiled_code:
int func5()
{
__pid_t v0; // eax
unsigned int v1; // eax
int v2; // ebx

v0 = fork();
if ( v0 < 0 )
{
syslog(4, "cannot fork");
}
else if ( v0 )
{
exit(0);
}
syslog(6, "now operating as a daemon");
v1 = getpid();
v2 = fprintf(pidfp, "%i\n", v1);
if ( v2 <= 0 )
syslog(4, "cannot write PID to file (%m)");
ftruncate(pidfd, v2);
fflush(pidfp);
fsync(pidfd);
if ( setsid() < 0 )
syslog(4, "cannot set sid");
if ( chdir("/") < 0 )
syslog(4, "cannot change directory");
close(0);
close(1);
return close(2);
}

original_code:
void make_daemon() {
 int n;
 pid_t pid, sid;
 pid = fork();
 if(pid < 0) syslog(4,"cannot fork");
 if(pid > 0) exit(0);
 syslog(6,"now operating as a daemon");
 n = fprintf(pidfp,"%i\n",getpid());
 if(n <= 0) syslog(4,"cannot write PID to file (%m)");
 ftruncate(pidfd,n);
 fflush(pidfp);
 fsync(pidfd);
 sid = setsid();
 if(sid < 0) syslog(4,"cannot set sid");
 if(chdir("/") < 0) syslog(4,"cannot change directory");
 close(0);
 close(1);
 close(2);
}

canonical_original_code:
void make_daemon() {
 int n;
 int pid; int sid;
 pid = fork();
 if(pid < 0) syslog(4,"cannot fork");
 if(pid > 0) exit(0);
 syslog(6,"now operating as a daemon");
 n = fprintf(pidfp,"%i\n",getpid());
 if(n <= 0) syslog(4,"cannot write PID to file (%m)");
 ftruncate(pidfd,n);
 fflush(pidfp);
 fsync(pidfd);
 sid = setsid();
 if(sid < 0) syslog(4,"cannot set sid");
 if(chdir("/") < 0) syslog(4,"cannot change directory");
 close(0);
 close(1);
 close(2);
}

variable_types: n int

variable_types: sid int


decompiled_code:
int init_com()
{
char *v0; // r12
char *v1; // rdi
char *v2; // r12
int v3; // eax
int v4; // er13
size_t v5; // r12
char *v7; // [rsp+8h] [rbp-A8h] BYREF
struct stat buf; // [rsp+10h] [rbp-A0h] BYREF

umask(0);
inotifyfd = inotify_init();
find_serial();
v0 = getenv("ANGEL_DB");
if ( !v0 )
v0 = "/var/opt/staevt.com/angel/angel.sqlite3";
if ( (unsigned int)sqlite3_open(v0, &db) )
die("cannot open database '%s'", v0);
sqlite3_exec(db, "PRAGMA journal_mode=WAL", 0LL, 0LL, &v7);
v1 = v7;
if ( v7
|| (sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS cars (car TEXT, time_received TIMESTAMP DEFAULT (strftime('%s','now')),time INTEGER"
", amperage REAL, voltage REAL, latitude REAL,longitude REAL, speed REAL, PRIMARY KEY (car, time_received))",
0LL),
(v1 = v7) != 0LL) )
{
LABEL_12:
die(v1);
}
v2 = getenv("ANGEL_KEY");
if ( !v2 )
v2 = "/var/opt/staevt.com/angel/hmac.key";
v3 = open(v2, 0);
v4 = v3;
if ( v3 < 0 )
die("cannot open HMAC key file '%s'", v2);
fstat(v3, &buf);
hmackeysize = buf.st_size;
v5 = SLODWORD(buf.st_size);
hmackey = (__int64)malloc(SLODWORD(buf.st_size));
if ( !hmackey )
{
v1 = "cannot allocate memory for HMAC key";
goto LABEL_12;
}
read(v4, (void *)hmackey, v5);
return close(v4);
}

canonical_decompiled_code:
int func6()
{
char *v0; // r12
char *v1; // rdi
char *v2; // r12
int v3; // eax
int v4; // er13
size_t v5; // r12
char *v7; // [rsp+8h] [rbp-A8h] BYREF
struct stat buf; // [rsp+10h] [rbp-A0h] BYREF

umask(0);
inotifyfd = inotify_init();
func3();
v0 = getenv("ANGEL_DB");
if ( !v0 )
v0 = "/var/opt/staevt.com/angel/angel.sqlite3";
if ( (unsigned int)sqlite3_open(v0, &db) )
func0("cannot open database '%s'", v0);
sqlite3_exec(db, "PRAGMA journal_mode=WAL", 0LL, 0LL, &v7);
v1 = v7;
if ( v7
|| (sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS cars (car TEXT, time_received TIMESTAMP DEFAULT (strftime('%s','now')),time INTEGER"
", amperage REAL, voltage REAL, latitude REAL,longitude REAL, speed REAL, PRIMARY KEY (car, time_received))",
0LL),
(v1 = v7) != 0LL) )
{
LABEL_12:
func0(v1);
}
v2 = getenv("ANGEL_KEY");
if ( !v2 )
v2 = "/var/opt/staevt.com/angel/hmac.key";
v3 = open(v2, 0);
v4 = v3;
if ( v3 < 0 )
func0("cannot open HMAC key file '%s'", v2);
fstat(v3, &buf);
hmackeysize = buf.st_size;
v5 = SLODWORD(buf.st_size);
hmackey = (__int64)malloc(SLODWORD(buf.st_size));
if ( !hmackey )
{
v1 = "cannot allocate memory for HMAC key";
goto LABEL_12;
}
read(v4, (void *)hmackey, v5);
return close(v4);
}

original_code:
void init_com() {
 char *msg;
 int err, fd;
 struct stat stat;
 char *dbname, *keyname;
 umask(0);
 inotifyfd = inotify_init();
 find_serial();
 dbname = getenv("ANGEL_DB");
 if(!dbname) dbname = "/var/opt/staevt.com/angel/angel.sqlite3";
 err = sqlite3_open(dbname,&db);
 if(err != 0) die("cannot open database '%s'",dbname);
 sqlite3_exec(db,"PRAGMA journal_mode=WAL",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS cars ("
  "car TEXT, time_received TIMESTAMP"
   " DEFAULT (strftime('%s','now')),"
  "time INTEGER, amperage REAL, voltage REAL, latitude REAL,"
  "longitude REAL, speed REAL, PRIMARY KEY (car, time_received)"
 ")",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 keyname = getenv("ANGEL_KEY");
 if(!keyname) keyname = "/var/opt/staevt.com/angel/hmac.key";
 fd = open(keyname,00);
 if(fd < 0) die("cannot open HMAC key file '%s'",keyname);
 fstat(fd,&stat);
 hmackeysize = stat.st_size;
 hmackey = malloc(hmackeysize);
 if(!hmackey) die("cannot allocate memory for HMAC key");
 read(fd,hmackey,hmackeysize);
 close(fd);
}

canonical_original_code:
void init_com() {
 char *msg;
 int err; int fd;
 struct stat stat;
 char *dbname; char *keyname;
 umask(0);
 inotifyfd = inotify_init();
 find_serial();
 dbname = getenv("ANGEL_DB");
 if(!dbname) dbname = "/var/opt/staevt.com/angel/angel.sqlite3";
 err = sqlite3_open(dbname,&db);
 if(err != 0) die("cannot open database '%s'",dbname);
 sqlite3_exec(db,"PRAGMA journal_mode=WAL",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS cars ("
  "car TEXT, time_received TIMESTAMP"
   " DEFAULT (strftime('%s','now')),"
  "time INTEGER, amperage REAL, voltage REAL, latitude REAL,"
  "longitude REAL, speed REAL, PRIMARY KEY (car, time_received)"
 ")",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 keyname = getenv("ANGEL_KEY");
 if(!keyname) keyname = "/var/opt/staevt.com/angel/hmac.key";
 fd = open(keyname,00);
 if(fd < 0) die("cannot open HMAC key file '%s'",keyname);
 fstat(fd,&stat);
 hmackeysize = stat.st_size;
 hmackey = malloc(hmackeysize);
 if(!hmackey) die("cannot allocate memory for HMAC key");
 read(fd,hmackey,hmackeysize);
 close(fd);
}

variable_types:
msg
char *

variable_types: err int

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:
dbname
char *

variable_types:
keyname
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:
__int64 __fastcall tolower_ct(unsigned int a1)
{
__int64 result; // rax

result = a1 + 32;
if ( (unsigned __int8)(a1 - 65) >= 0x1Au )
result = a1;
return result;
}

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

result = a1 + 32;
if ( (unsigned __int8)(a1 - 65) >= 0x1Au )
result = a1;
return result;
}

original_code:
char tolower_ct(char c) {
 return c + (c >= 'A')*(c <= 'Z')*0x20;
}

variable_types: c char

return_type: char


decompiled_code:
__int64 __fastcall strieq_ct(const char *a1, const char *a2)
{
const char *v2; // rbx
const char *v3; // r12
unsigned int v4; // er14
size_t v5; // r13
unsigned int v6; // edi
char v7; // r13
char v8; // al

v2 = a1;
v3 = a2;
if ( !a1 || !a2 )
return 0LL;
v4 = 0;
v5 = strlen(a1);
if ( v5 == strlen(a2) )
{
v6 = *a1;
if ( *v2 )
{
do
{
v7 = tolower_ct(v6);
v8 = tolower_ct(*v3);
v6 = *++v2;
++v3;
v4 |= v7 == v8;
}
while ( (_BYTE)v6 );
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, const char *a2)
{
const char *v2; // rbx
const char *v3; // r12
unsigned int v4; // er14
size_t v5; // r13
unsigned int v6; // edi
char v7; // r13
char v8; // al

v2 = a1;
v3 = a2;
if ( !a1 || !a2 )
return 0LL;
v4 = 0;
v5 = strlen(a1);
if ( v5 == strlen(a2) )
{
v6 = *a1;
if ( *v2 )
{
do
{
v7 = func9(v6);
v8 = func9(*v3);
v6 = *++v2;
++v3;
v4 |= v7 == v8;
}
while ( (_BYTE)v6 );
}
}
return v4;
}

original_code:
int strieq_ct(char *a, char *b) {
 int equal = 0;
 if(!a || !b || strlen(a) != strlen(b)) return 0;
 while(*a) {
  equal |= tolower_ct(*a) == tolower_ct(*b);
  a++, b++;
 }
 return equal;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types: equal int


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

result = 0xBF800000BF800000LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0xBF800000BF800000LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 24) = -1082130432;
return result;
}

original_code:
void reset_datum(struct datum *datum) {
 datum->milliseconds = 0;
 datum->amperage = -1;
 datum->voltage = -1;
 datum->latitude = 0;
 datum->longitude = 0;
 datum->speed = -1;
}

variable_types: struct datum { long milliseconds; float amperage; float voltage; float latitude; float speed; } *


parse

decompiled_code:
unsigned __int64 __fastcall parse(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r12
unsigned __int64 result; // rax
char *v3; // r12
float v4; // [rsp+0h] [rbp-30h] BYREF
char v5[32]; // [rsp+8h] [rbp-28h] BYREF

v1 = a1;
result = reset_datum((__int64)&v4);
if ( a1 )
{
LABEL_4:
result = *v1;
if ( (_BYTE)result )
{
if ( (_BYTE)result != 123 )
goto LABEL_3;
result = v1[1];
if ( (_BYTE)result )
{
v3 = (char *)(v1 + 1);
while ( 1 )
{
switch ( (char)result )
{
case 'a':
_isoc99_sscanf(v3, "a:%f;", v5);
goto LABEL_10;
case 'g':
_isoc99_sscanf(v3, "g:%f,%f;", &v5[8], &v5[12]);
goto LABEL_10;
case 's':
_isoc99_sscanf(v3, "s:%f;", &v5[16]);
goto LABEL_10;
case 't':
_isoc99_sscanf(v3, "t:%li;", &v4);
goto LABEL_10;
case 'v':
_isoc99_sscanf(v3, "v:%f;", &v5[4]);
goto LABEL_10;
case '}':
store(&v4);
reset_datum((__int64)&v4);
result = (unsigned __int8)v3[1];
if ( !(_BYTE)result )
return result;
if ( (_BYTE)result != 123 )
{
v1 = (unsigned __int8 *)(v3 + 1);
LABEL_3:
++v1;
goto LABEL_4;
}
result = (unsigned __int8)v3[2];
if ( !(_BYTE)result )
return result;
v3 += 2;
break;
default:
LABEL_10:
v3 = strchr(v3, 59) + 1;
result = (unsigned __int8)*v3;
if ( !(_BYTE)result )
return result;
continue;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func12(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r12
unsigned __int64 result; // rax
char *v3; // r12
float v4; // [rsp+0h] [rbp-30h] BYREF
char v5[32]; // [rsp+8h] [rbp-28h] BYREF

v1 = a1;
result = func11((__int64)&v4);
if ( a1 )
{
LABEL_4:
result = *v1;
if ( (_BYTE)result )
{
if ( (_BYTE)result != 123 )
goto LABEL_3;
result = v1[1];
if ( (_BYTE)result )
{
v3 = (char *)(v1 + 1);
while ( 1 )
{
switch ( (char)result )
{
case 'a':
_isoc99_sscanf(v3, "a:%f;", v5);
goto LABEL_10;
case 'g':
_isoc99_sscanf(v3, "g:%f,%f;", &v5[8], &v5[12]);
goto LABEL_10;
case 's':
_isoc99_sscanf(v3, "s:%f;", &v5[16]);
goto LABEL_10;
case 't':
_isoc99_sscanf(v3, "t:%li;", &v4);
goto LABEL_10;
case 'v':
_isoc99_sscanf(v3, "v:%f;", &v5[4]);
goto LABEL_10;
case '}':
func7(&v4);
func11((__int64)&v4);
result = (unsigned __int8)v3[1];
if ( !(_BYTE)result )
return result;
if ( (_BYTE)result != 123 )
{
v1 = (unsigned __int8 *)(v3 + 1);
LABEL_3:
++v1;
goto LABEL_4;
}
result = (unsigned __int8)v3[2];
if ( !(_BYTE)result )
return result;
v3 += 2;
break;
default:
LABEL_10:
v3 = strchr(v3, 59) + 1;
result = (unsigned __int8)*v3;
if ( !(_BYTE)result )
return result;
continue;
}
}
}
}
}
return result;
}

original_code:
void parse(char *block) {
 struct datum datum;
 enum { START, FIELD } state;
 reset_datum(&datum);
 for(state = START; block && *block; block++) {
  switch(state) {
  case START: if(*block == '{') state = FIELD; break;
  case FIELD:
   switch(*block) {
   case 'a': sscanf(block,"a:%f;",&datum.amperage); break;
   case 'g': sscanf(block,"g:%f,%f;",&datum.latitude,
    &datum.longitude); break;
   case 's': sscanf(block,"s:%f;",&datum.speed); break;
   case 't': sscanf(block,"t:%""l" "i"";",
    &datum.milliseconds); break;
   case 'v': sscanf(block,"v:%f;",&datum.voltage); break;
   case '}':
    store(&datum);
    reset_datum(&datum);
    state = START;
    break;
   default: break;
   }
   if(state == FIELD)
    block = strchr(block,';');
   break;
  default: state = START; break;
  }
 }
}

canonical_original_code:
void parse(char *block) {
 struct datum datum;
 enum { START=0, FIELD=1 } state;
 reset_datum(&datum);
 for(state = START; block && *block; block++) {
  switch(state) {
  case START: if(*block == '{') state = FIELD; break;
  case FIELD:
   switch(*block) {
   case 'a': sscanf(block,"a:%f;",&datum.amperage); break;
   case 'g': sscanf(block,"g:%f,%f;",&datum.latitude,
    &datum.longitude); break;
   case 's': sscanf(block,"s:%f;",&datum.speed); break;
   case 't': sscanf(block,"t:%""l" "i"";",
    &datum.milliseconds); break;
   case 'v': sscanf(block,"v:%f;",&datum.voltage); break;
   case '}':
    store(&datum);
    reset_datum(&datum);
    state = START;
    break;
   default: break;
   }
   if(state == FIELD)
    block = strchr(block,';');
   break;
  default: state = START; break;
  }
 }
}

variable_types:
block
char *

variable_types: struct datum { long milliseconds; float amperage; float voltage; float latitude; float speed; }

variable_types:
state
enum { START=0, FIELD=1 }


monitor

decompiled_code:
void __noreturn monitor()
{
int v0; // er12
char v1; // al
__int64 v2; // rbx
int i; // er14
unsigned __int8 v4[1032]; // [rsp+0h] [rbp-430h] BYREF
struct pollfd fds; // [rsp+408h] [rbp-28h] BYREF

fds.fd = serialfd;
fds.events = 17;
while ( 1 )
{
v0 = poll(&fds, 1uLL, -1);
if ( v0 < 0 )
syslog(4, "poll error (%m)");
v1 = fds.revents;
if ( (fds.revents & 1) != 0 )
{
v4[0] = 0;
v2 = 0LL;
for ( i = 0; ; i = 1 )
{
if ( strchr((const char *)v4, 123) && strchr((const char *)v4, 125) )
goto LABEL_16;
v2 += read(serialfd, &v4[v2], v0);
if ( !i && !strchr((const char *)v4, 123) )
break;
if ( v2 > 1023 )
{
v4[1023] = 0;
goto LABEL_16;
}
if ( v2 < 0 )
goto LABEL_18;
v4[v2] = 0;
v0 = 1;
}
if ( v2 >= 0 )
{
LABEL_16:
parse(v4);
v1 = fds.revents;
}
else
{
LABEL_18:
syslog(4, "read error (%m)");
parse(v4);
v1 = fds.revents;
}
}
if ( (v1 & 0x10) != 0 )
{
syslog(5, "serial device removed");
close(serialfd);
find_serial();
fds.fd = serialfd;
}
}
}

canonical_decompiled_code:
void __noreturn func13()
{
int v0; // er12
char v1; // al
__int64 v2; // rbx
int i; // er14
unsigned __int8 v4[1032]; // [rsp+0h] [rbp-430h] BYREF
struct pollfd fds; // [rsp+408h] [rbp-28h] BYREF

fds.fd = serialfd;
fds.events = 17;
while ( 1 )
{
v0 = poll(&fds, 1uLL, -1);
if ( v0 < 0 )
syslog(4, "poll error (%m)");
v1 = fds.revents;
if ( (fds.revents & 1) != 0 )
{
v4[0] = 0;
v2 = 0LL;
for ( i = 0; ; i = 1 )
{
if ( strchr((const char *)v4, 123) && strchr((const char *)v4, 125) )
goto LABEL_16;
v2 += read(serialfd, &v4[v2], v0);
if ( !i && !strchr((const char *)v4, 123) )
break;
if ( v2 > 1023 )
{
v4[1023] = 0;
goto LABEL_16;
}
if ( v2 < 0 )
goto LABEL_18;
v4[v2] = 0;
v0 = 1;
}
if ( v2 >= 0 )
{
LABEL_16:
func12(v4);
v1 = fds.revents;
}
else
{
LABEL_18:
syslog(4, "read error (%m)");
func12(v4);
v1 = fds.revents;
}
}
if ( (v1 & 0x10) != 0 )
{
syslog(5, "serial device removed");
close(serialfd);
func3();
fds.fd = serialfd;
}
}
}

original_code:
void monitor() {
 static const int bufsize = 1024;
 int nready;
 int reading;
 ssize_t nread;
 char buf[bufsize];
 struct pollfd pollfd;
 pollfd.fd = serialfd;
 pollfd.events = 0x001 | 0x010;
 while(1) {
  nready = poll(&pollfd,1,-1);
  if(nready < 0) syslog(4,"poll error (%m)");
  if(pollfd.revents & 0x001) {
   buf[0] = '\0';
   reading = nread = 0;
   while(nread >= 0
    && (!strchr(buf,'{') || !strchr(buf,'}'))) {
    nread += read(serialfd,buf + nread,nready);
    if(!reading && !strchr(buf,'{')) break;
    else reading = 1;
    if(nread >= bufsize) {
     buf[bufsize - 1] = '\0';
     break;
    }
    if(nread >= 0) buf[nread] = '\0';
    nready = 1;
   }
   if(nread < 0) syslog(4,"read error (%m)");
   parse(buf);
  }
  if(pollfd.revents & 0x010) {
   syslog(5,"serial device removed");
   close(serialfd);
   find_serial();
   pollfd.fd = serialfd;
  }
 }
}

canonical_original_code:
void monitor() {
 static int bufsize = 1024;
 int nready;
 int reading;
 long nread;
 char buf[-1];
 struct pollfd pollfd;
 pollfd.fd = serialfd;
 pollfd.events = 0x001 | 0x010;
 while(1) {
  nready = poll(&pollfd,1,-1);
  if(nready < 0) syslog(4,"poll error (%m)");
  if(pollfd.revents & 0x001) {
   buf[0] = '\0';
   reading = nread = 0;
   while(nread >= 0
    && (!strchr(buf,'{') || !strchr(buf,'}'))) {
    nread += read(serialfd,buf + nread,nready);
    if(!reading && !strchr(buf,'{')) break;
    else reading = 1;
    if(nread >= bufsize) {
     buf[bufsize - 1] = '\0';
     break;
    }
    if(nread >= 0) buf[nread] = '\0';
    nready = 1;
   }
   if(nread < 0) syslog(4,"read error (%m)");
   parse(buf);
  }
  if(pollfd.revents & 0x010) {
   syslog(5,"serial device removed");
   close(serialfd);
   find_serial();
   pollfd.fd = serialfd;
  }
 }
}

variable_types: bufsize int

variable_types: nready int

variable_types: reading int

variable_types: nread long

variable_types:
buf
char [-1]

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


die

decompiled_code:
void __noreturn die(char *fmt, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-C8h] BYREF

va_start(ap, fmt);
vsyslog(2, fmt, ap);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(char *fmt, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-C8h] BYREF

va_start(ap, fmt);
vsyslog(2, fmt, ap);
exit(1);
}

original_code:
void die(char *format, ...) {
 va_list ap;
 __builtin_va_start(ap,format);
 vsyslog(2,format,ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void die(char *format, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,format);
 vsyslog(2,format,ap);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
format
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // cf
bool v4; // zf
const char *v5; // r12
const char *v6; // rdi
__int64 v7; // rcx
const char *v8; // rsi
int v9; // ebx
int v10; // eax
int v11; // er12
int v12; // ebx

openlog("angeld", 3, 8);
v3 = argc == 0;
v4 = argc == 1;
if ( argc <= 1 )
goto LABEL_17;
v5 = argv[1];
v6 = "start";
v7 = 6LL;
v8 = v5;
do
{
if ( !v7 )
break;
v3 = *v8 < (unsigned int)*v6;
v4 = *v8++ == *v6++;
--v7;
}
while ( v4 );
v9 = (char)((!v3 && !v4) - v3);
if ( (!v3 && !v4) == v3 )
{
v10 = is_alive(v6, v8);
LOBYTE(v11) = v10 == 0;
goto LABEL_7;
}
if ( strcmp(v5, "restart") )
{
v11 = strcmp(v5, "stop");
if ( !v11 )
{
v9 = ((__int64 (*)(void))is_alive)();
if ( v9 )
{
v12 = v9 & 1;
if ( !v12 )
goto LABEL_12;
LABEL_10:
v12 = 1;
kill_daemon();
goto LABEL_11;
}
goto LABEL_16;
}
LABEL_17:
puts("usage: angeld (start|restart|stop)");
exit(0);
}
v9 = 1;
LOBYTE(v11) = 0;
v10 = ((__int64 (*)(void))is_alive)();
if ( !v10 )
{
LABEL_16:
v11 = v9;
syslog(6, "nothing to kill");
v12 = 0;
goto LABEL_11;
}
LABEL_7:
v12 = v10 & v9;
if ( (v12 & 1) != 0 || !v10 )
{
v11 = ((unsigned __int8)v12 | (unsigned __int8)v11) & 1;
if ( !v12 )
goto LABEL_11;
goto LABEL_10;
}
v11 = (unsigned __int8)v11;
syslog(6, "angeld already alive");
v12 = 0;
LABEL_11:
if ( v11 )
{
make_daemon();
init_com();
monitor();
}
LABEL_12:
closelog();
return v12 ^ 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // cf
bool v4; // zf
const char *v5; // r12
const char *v6; // rdi
__int64 v7; // rcx
const char *v8; // rsi
int v9; // ebx
int v10; // eax
int v11; // er12
int v12; // ebx

openlog("angeld", 3, 8);
v3 = argc == 0;
v4 = argc == 1;
if ( argc <= 1 )
goto LABEL_17;
v5 = argv[1];
v6 = "start";
v7 = 6LL;
v8 = v5;
do
{
if ( !v7 )
break;
v3 = *v8 < (unsigned int)*v6;
v4 = *v8++ == *v6++;
--v7;
}
while ( v4 );
v9 = (char)((!v3 && !v4) - v3);
if ( (!v3 && !v4) == v3 )
{
v10 = func1(v6, v8);
LOBYTE(v11) = v10 == 0;
goto LABEL_7;
}
if ( strcmp(v5, "restart") )
{
v11 = strcmp(v5, "stop");
if ( !v11 )
{
v9 = ((__int64 (*)(void))is_alive)();
if ( v9 )
{
v12 = v9 & 1;
if ( !v12 )
goto LABEL_12;
LABEL_10:
v12 = 1;
func4();
goto LABEL_11;
}
goto LABEL_16;
}
LABEL_17:
puts("usage: angeld (start|restart|stop)");
exit(0);
}
v9 = 1;
LOBYTE(v11) = 0;
v10 = ((__int64 (*)(void))is_alive)();
if ( !v10 )
{
LABEL_16:
v11 = v9;
syslog(6, "nothing to kill");
v12 = 0;
goto LABEL_11;
}
LABEL_7:
v12 = v10 & v9;
if ( (v12 & 1) != 0 || !v10 )
{
v11 = ((unsigned __int8)v12 | (unsigned __int8)v11) & 1;
if ( !v12 )
goto LABEL_11;
goto LABEL_10;
}
v11 = (unsigned __int8)v11;
syslog(6, "angeld already alive");
v12 = 0;
LABEL_11:
if ( v11 )
{
func5();
func6();
func13();
}
LABEL_12:
closelog();
return v12 ^ 1;
}

original_code:
int main(int argc, char **argv) {
 int alive, kill, live;
 openlog("angeld",0x01 | 0x02,(1<<3));
 kill = 0;
 live = 0;
 if(argc <= 1) goto help;
 if(strcmp(argv[1],"start") == 0) live = 1;
 else if(strcmp(argv[1],"restart") == 0) kill = live = 1;
 else if(strcmp(argv[1],"stop") == 0) kill = 1;
 else {
help:
  puts("usage: angeld (start|restart|stop)");
  exit(0);
 }
 if(kill || live) {
  alive = is_alive();
  if(kill && !alive) syslog(6,"nothing to kill");
  kill &= alive;
  if(!kill && live && alive)
   syslog(6,"angeld already alive");
  live &= kill || !alive;
 }
 if(kill) kill_daemon();
 if(live) {
  make_daemon();
  init_com();
  monitor();
 }
 closelog();
 return !(kill || live);
}

canonical_original_code:
int main(int argc, char **argv) {
 int alive; int kill; int live;
 openlog("angeld",0x01 | 0x02,(1<<3));
 kill = 0;
 live = 0;
 if(argc <= 1) goto help;
 if(strcmp(argv[1],"start") == 0) live = 1;
 else if(strcmp(argv[1],"restart") == 0) kill = live = 1;
 else if(strcmp(argv[1],"stop") == 0) kill = 1;
 else {
help:
  puts("usage: angeld (start|restart|stop)");
  exit(0);
 }
 if(kill || live) {
  alive = is_alive();
  if(kill && !alive) syslog(6,"nothing to kill");
  kill &= alive;
  if(!kill && live && alive)
   syslog(6,"angeld already alive");
  live &= kill || !alive;
 }
 if(kill) kill_daemon();
 if(live) {
  make_daemon();
  init_com();
  monitor();
 }
 closelog();
 return !(kill || live);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: alive int

variable_types: kill int

variable_types: live int


canonical_decompiled_code:
void func0()
{
waitingforstart = 1;
}

original_code:
static void start_race() {
 waitingforstart = 1;
}


decompiled_code:
void __noreturn good_exit()
{
exit(0);
}

canonical_decompiled_code:
void __noreturn func1()
{
exit(0);
}

original_code:
static void good_exit() {
 exit(0);
}

canonical_original_code:
static void good_exit() {
 exit(0);
}


cmd

decompiled_code:
ssize_t cmd(char *format, ...)
{
size_t v1; // rax
int v2; // ebx
gcc_va_list arg; // [rsp+8h] [rbp-D8h] BYREF

va_start(arg, format);
vsprintf(buf_0, format, arg);
v1 = strlen(buf_0);
v2 = write(auxfd, buf_0, v1);
if ( v2 < strlen(buf_0) )
die("cannot write to serial port");
*(_WORD *)buf_0 = 2573;
return write(auxfd, buf_0, 2uLL);
}

canonical_decompiled_code:
ssize_t func2(char *format, ...)
{
size_t v1; // rax
int v2; // ebx
gcc_va_list arg; // [rsp+8h] [rbp-D8h] BYREF

va_start(arg, format);
vsprintf(buf_0, format, arg);
v1 = strlen(buf_0);
v2 = write(auxfd, buf_0, v1);
if ( v2 < strlen(buf_0) )
die("cannot write to serial port");
*(_WORD *)buf_0 = 2573;
return write(auxfd, buf_0, 2uLL);
}

original_code:
static void cmd(char *cmd, ...) {
 static char buf[1000];
 int nread;
 va_list ap;
 __builtin_va_start(ap,cmd);
 vsprintf(buf,cmd,ap);
 nread = write(auxfd,buf,strlen(buf));
 if(nread < strlen(buf)) die("cannot write to serial port");
 __builtin_va_end(ap);
 buf[0] = '\r';
 buf[1] = '\n';
 write(auxfd,buf,2);
}

canonical_original_code:
static void cmd(char *cmd, ...) {
 static char buf[1000];
 int nread;
 __builtin_va_list ap;
 __builtin_va_start(ap,cmd);
 vsprintf(buf,cmd,ap);
 nread = write(auxfd,buf,strlen(buf));
 if(nread < strlen(buf)) die("cannot write to serial port");
 __builtin_va_end(ap);
 buf[0] = '\r';
 buf[1] = '\n';
 write(auxfd,buf,2);
}

variable_types:
cmd
char *

variable_types:
buf
char [1000]

variable_types: nread int


decompiled_code:
ssize_t horn_off()
{
return cmd("h0");
}

canonical_decompiled_code:
ssize_t func3()
{
return func2("h0");
}

original_code:
static void horn_off() {
 cmd("h0");
}

canonical_original_code:
static void horn_off() {
 cmd("h0");
}


decompiled_code:
ssize_t horn_on()
{
return cmd("h1");
}

canonical_decompiled_code:
ssize_t func4()
{
return func2("h1");
}

original_code:
static void horn_on() {
 cmd("h1");
}

canonical_original_code:
static void horn_on() {
 cmd("h1");
}


decompiled_code:
ssize_t wiper_off()
{
return cmd("w0");
}

canonical_decompiled_code:
ssize_t func5()
{
return func2("w0");
}

original_code:
static void wiper_off() {
 cmd("w0");
}

canonical_original_code:
static void wiper_off() {
 cmd("w0");
}


decompiled_code:
ssize_t wiper_on()
{
return cmd("w255");
}

canonical_decompiled_code:
ssize_t func6()
{
return func2("w255");
}

original_code:
static void wiper_on() {
 cmd("w255");
}

canonical_original_code:
static void wiper_on() {
 cmd("w255");
}


decompiled_code:
ssize_t __fastcall set_light(int a1, unsigned int a2, float a3)
{
const char *v3; // r13

v3 = lights[a1];
cmd("l%sp%i", v3, (unsigned int)(int)(float)(a3 * 255.0));
return cmd("l%sb%i", v3, a2);
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, unsigned int a2, float a3)
{
const char *v3; // r13

v3 = lights[a1];
func2("l%sp%i", v3, (unsigned int)(int)(float)(a3 * 255.0));
return func2("l%sb%i", v3, a2);
}

original_code:
static void set_light(enum lights light, float power, int blinking) {
 cmd("l%sp%i",lights[light],(int) (0xFF*power));
 cmd("l%sb%i",lights[light],blinking);
}

variable_types: enum lights { EL_WIRE=0, HEADLIGHTS=1, FRONT_LEFT=2, FRONT_RIGHT=3, BACK_LEFT=4, BACK_RIGHT=5 }

variable_types: power float

variable_types: blinking int


decompiled_code:
ssize_t head_low()
{
return set_light(1, 0, 0.0078431377);
}

original_code:
static void head_low() {
 set_light(HEADLIGHTS,(2.0/0xFF),0);
}


decompiled_code:
ssize_t head_high()
{
return set_light(1, 0, 1.0);
}

canonical_decompiled_code:
ssize_t func9()
{
return func7(1, 0, 1.0);
}

original_code:
static void head_high() {
 set_light(HEADLIGHTS,1,0);
}


decompiled_code:
ssize_t turn_right_stop()
{
set_light(3, 0, 0.0);
return set_light(5, 0, 0.0078431377);
}

original_code:
static void turn_right_stop() {
 set_light(FRONT_RIGHT,0,0);
 set_light(BACK_RIGHT,(2.0/0xFF),0);
}


decompiled_code:
ssize_t turn_right()
{
set_light(3, 1u, 1.0);
return set_light(5, 1u, 1.0);
}

original_code:
static void turn_right() {
 set_light(FRONT_RIGHT,1,1);
 set_light(BACK_RIGHT,1,1);
}


decompiled_code:
ssize_t turn_left_stop()
{
set_light(2, 0, 0.0);
return set_light(4, 0, 0.0078431377);
}

original_code:
static void turn_left_stop() {
 set_light(FRONT_LEFT,0,0);
 set_light(BACK_LEFT,(2.0/0xFF),0);
}


decompiled_code:
ssize_t turn_left()
{
set_light(2, 1u, 1.0);
return set_light(4, 1u, 1.0);
}

original_code:
static void turn_left() {
 set_light(FRONT_LEFT,1,1);
 set_light(BACK_LEFT,1,1);
}


decompiled_code:
void car_init()
{
logfp = fopen("angel.log", "a");
gui_bind("turn_left", turn_left);
gui_bind("turn_left_stop", turn_left_stop);
gui_bind("turn_right", turn_right);
gui_bind("turn_right_stop", turn_right_stop);
gui_bind("highbeams_on", head_high);
gui_bind("highbeams_off", head_low);
gui_bind("wiper_on", wiper_on);
gui_bind("wiper_off", wiper_off);
gui_bind("horn_on", horn_on);
gui_bind("horn_off", horn_off);
gui_bind("start_race", start_race);
gui_bind("exit", good_exit);
backdropobj = gui_find_obj("backdrop", 0LL);
gui_find_obj("map", 0LL);
needleobj = gui_find_obj("needle", 0LL);
timerobj = gui_find_obj("timer", 0LL);
speedobj = gui_find_obj("speed", 0LL);
set_light(0, 0, 1.0);
set_light(1, 0, 0.0078431377);
set_light(4, 0, 0.0078431377);
set_light(5, 0, 0.0078431377);
gui_set_value(needleobj, 0LL);
start_race();
}

canonical_decompiled_code:
void func14()
{
logfp = fopen("angel.log", "a");
gui_bind("turn_left", turn_left);
gui_bind("turn_left_stop", turn_left_stop);
gui_bind("turn_right", turn_right);
gui_bind("turn_right_stop", turn_right_stop);
gui_bind("highbeams_on", head_high);
gui_bind("highbeams_off", head_low);
gui_bind("wiper_on", wiper_on);
gui_bind("wiper_off", wiper_off);
gui_bind("horn_on", horn_on);
gui_bind("horn_off", horn_off);
gui_bind("start_race", start_race);
gui_bind("exit", good_exit);
backdropobj = gui_find_obj("backdrop", 0LL);
gui_find_obj("map", 0LL);
needleobj = gui_find_obj("needle", 0LL);
timerobj = gui_find_obj("timer", 0LL);
speedobj = gui_find_obj("speed", 0LL);
func7(0, 0, 1.0);
func7(1, 0, 0.0078431377);
func7(4, 0, 0.0078431377);
func7(5, 0, 0.0078431377);
gui_set_value(needleobj, 0LL);
func0();
}

original_code:
void car_init() {
 logfp = fopen("angel.log","a");
 gui_bind("turn_left",turn_left);
 gui_bind("turn_left_stop",turn_left_stop);
 gui_bind("turn_right",turn_right);
 gui_bind("turn_right_stop",turn_right_stop);
 gui_bind("highbeams_on",head_high);
 gui_bind("highbeams_off",head_low);
 gui_bind("wiper_on",wiper_on);
 gui_bind("wiper_off",wiper_off);
 gui_bind("horn_on",horn_on);
 gui_bind("horn_off",horn_off);
 gui_bind("start_race",start_race);
 gui_bind("exit",good_exit);
 backdropobj = gui_find_obj("backdrop",((void *)0));
 mapobj = gui_find_obj("map",((void *)0));
 needleobj = gui_find_obj("needle",((void *)0));
 timerobj = gui_find_obj("timer",((void *)0));
 speedobj = gui_find_obj("speed",((void *)0));
 set_light(EL_WIRE,1,0);
 set_light(HEADLIGHTS,(2.0/0xFF),0);
 set_light(BACK_LEFT,(2.0/0xFF),0);
 set_light(BACK_RIGHT,(2.0/0xFF),0);
 gui_set_value(needleobj,0.0);
 start_race();
}


decompiled_code:
int car_stop()
{
return fclose(logfp);
}

canonical_decompiled_code:
int func15()
{
return fclose(logfp);
}

original_code:
void car_stop() {
 fclose(logfp);
}

canonical_original_code:
void car_stop() {
 fclose(logfp);
}


decompiled_code:
void __fastcall car_handle_data_block(_BYTE *a1)
{
char v1; // al
const char *v2; // r12
char v3; // al
char *v4; // rax
double v5; // xmm4_8
char *v6; // rax
char *v7; // r12
char v8; // al
time_t v9; // r13
time_t v10; // rax
time_t v11; // rax
double x; // [rsp+8h] [rbp-18h]

if ( a1 )
{
v1 = *a1;
if ( *a1 )
{
while ( 1 )
{
while ( 1 )
{
v2 = a1 + 1;
if ( v1 == 123 )
break;
v1 = *++a1;
if ( !v1 )
return;
}
while ( 2 )
{
v3 = *v2;
if ( !*v2 )
return;
LABEL_8:
switch ( v3 )
{
case 'a':
_isoc99_sscanf(v2, "a:%f;", &amperage);
goto LABEL_10;
case 'g':
_isoc99_sscanf(v2, "g:%f,%f;", &latitude, &longitude);
goto LABEL_10;
case 's':
_isoc99_sscanf(v2, "s:%f;", &mph);
goto LABEL_10;
case 't':
_isoc99_sscanf(v2, "t:%li;", &gpstime);
goto LABEL_10;
case 'v':
_isoc99_sscanf(v2, "v:%f;", &voltage);
goto LABEL_10;
case '}':
if ( waitingforstart )
{
v5 = 0.0;
if ( *(float *)&mph > 0.1 )
{
v11 = time(0LL);
v5 = 0.0;
waitingforstart = 0;
racestart = v11;
}
}
else
{
v9 = racestart;
v10 = time(0LL);
v5 = difftime(v10, v9);
}
x = v5;
gui_set_value(backdropobj, 0LL);
gui_set_value(needleobj, 1LL);
fmod(x, 60.0);
fmod(x, 3600.0);
gui_set_value(timerobj, 3LL);
gui_set_value(speedobj, 1LL);
if ( logfp )
fprintf(
logfp,
"%li,%f,%f,%f,%f,%f,%f",
gpstime,
*(float *)&amperage,
*(float *)&voltage,
*(float *)&latitude,
*(float *)&longitude,
x,
*(float *)&mph);
v6 = strchr(v2, 59);
if ( !v6 )
return;
v7 = v6 + 1;
v8 = v6[1];
if ( !v8 )
return;
if ( v8 == 123 )
{
v2 = v7 + 1;
continue;
}
v1 = v7[1];
a1 = v7 + 1;
if ( !v1 )
return;
break;
default:
LABEL_10:
v4 = strchr(v2, 59);
if ( !v4 )
return;
v2 = v4 + 1;
v3 = v4[1];
if ( !v3 )
return;
goto LABEL_8;
}
break;
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func16(_BYTE *a1)
{
char v1; // al
const char *v2; // r12
char v3; // al
char *v4; // rax
double v5; // xmm4_8
char *v6; // rax
char *v7; // r12
char v8; // al
time_t v9; // r13
time_t v10; // rax
time_t v11; // rax
double x; // [rsp+8h] [rbp-18h]

if ( a1 )
{
v1 = *a1;
if ( *a1 )
{
while ( 1 )
{
while ( 1 )
{
v2 = a1 + 1;
if ( v1 == 123 )
break;
v1 = *++a1;
if ( !v1 )
return;
}
while ( 2 )
{
v3 = *v2;
if ( !*v2 )
return;
LABEL_8:
switch ( v3 )
{
case 'a':
_isoc99_sscanf(v2, "a:%f;", &amperage);
goto LABEL_10;
case 'g':
_isoc99_sscanf(v2, "g:%f,%f;", &latitude, &longitude);
goto LABEL_10;
case 's':
_isoc99_sscanf(v2, "s:%f;", &mph);
goto LABEL_10;
case 't':
_isoc99_sscanf(v2, "t:%li;", &gpstime);
goto LABEL_10;
case 'v':
_isoc99_sscanf(v2, "v:%f;", &voltage);
goto LABEL_10;
case '}':
if ( waitingforstart )
{
v5 = 0.0;
if ( *(float *)&mph > 0.1 )
{
v11 = time(0LL);
v5 = 0.0;
waitingforstart = 0;
racestart = v11;
}
}
else
{
v9 = racestart;
v10 = time(0LL);
v5 = difftime(v10, v9);
}
x = v5;
gui_set_value(backdropobj, 0LL);
gui_set_value(needleobj, 1LL);
fmod(x, 60.0);
fmod(x, 3600.0);
gui_set_value(timerobj, 3LL);
gui_set_value(speedobj, 1LL);
if ( logfp )
fprintf(
logfp,
"%li,%f,%f,%f,%f,%f,%f",
gpstime,
*(float *)&amperage,
*(float *)&voltage,
*(float *)&latitude,
*(float *)&longitude,
x,
*(float *)&mph);
v6 = strchr(v2, 59);
if ( !v6 )
return;
v7 = v6 + 1;
v8 = v6[1];
if ( !v8 )
return;
if ( v8 == 123 )
{
v2 = v7 + 1;
continue;
}
v1 = v7[1];
a1 = v7 + 1;
if ( !v1 )
return;
break;
default:
LABEL_10:
v4 = strchr(v2, 59);
if ( !v4 )
return;
v2 = v4 + 1;
v3 = v4[1];
if ( !v3 )
return;
goto LABEL_8;
}
break;
}
}
}
}
}

original_code:
void car_handle_data_block(char *str) {
 double timersecs;
 enum { START, FIELD } state;
 for(state = START; str && *str; str++) {
  switch(state) {
  case START:
   if(*str == '{') state = FIELD;
   break;
  case FIELD:
   switch(*str) {
   case 'a': sscanf(str,"a:%f;",&amperage); break;
   case 'g': sscanf(str,"g:%f,%f;",&latitude,&longitude);
    break;
   case 's': sscanf(str,"s:%f;",&mph); break;
   case 't': sscanf(str,"t:%""l" "i"";",&gpstime); break;
   case 'v': sscanf(str,"v:%f;",&voltage); break;
   case '}':
    if(waitingforstart) {
     if(mph > 0.1) {
      racestart = time(((void *)0));
      waitingforstart = 0;
     }
     timersecs = 0;
    } else timersecs = difftime(time(((void *)0)),racestart);
    gui_set_value(backdropobj,0);
    gui_set_value(needleobj,1,mph);
    gui_set_value(timerobj,3,
     floor(timersecs / 3600),
     floor(fmod(timersecs,3600)/60),
     floor(fmod(timersecs,60)));
    gui_set_value(speedobj,1,mph);
    if(logfp)
     fprintf(logfp,"%""l" "i"",%f,%f,%f,%f,"
      "%f,%f",gpstime,amperage,
      voltage,latitude,longitude,
      timersecs,mph);
    state = START;
    break;
   default: break;
   }
   if(!(str = strchr(str,';')))
    return;
   break;
  }
 }
}

canonical_original_code:
void car_handle_data_block(char *str) {
 double timersecs;
 enum { START=0, FIELD=1 } state;
 for(state = START; str && *str; str++) {
  switch(state) {
  case START:
   if(*str == '{') state = FIELD;
   break;
  case FIELD:
   switch(*str) {
   case 'a': sscanf(str,"a:%f;",&amperage); break;
   case 'g': sscanf(str,"g:%f,%f;",&latitude,&longitude);
    break;
   case 's': sscanf(str,"s:%f;",&mph); break;
   case 't': sscanf(str,"t:%""l" "i"";",&gpstime); break;
   case 'v': sscanf(str,"v:%f;",&voltage); break;
   case '}':
    if(waitingforstart) {
     if(mph > 0.1) {
      racestart = time(((void *)0));
      waitingforstart = 0;
     }
     timersecs = 0;
    } else timersecs = difftime(time(((void *)0)),racestart);
    gui_set_value(backdropobj,0);
    gui_set_value(needleobj,1,mph);
    gui_set_value(timerobj,3,
     floor(timersecs / 3600),
     floor(fmod(timersecs,3600)/60),
     floor(fmod(timersecs,60)));
    gui_set_value(speedobj,1,mph);
    if(logfp)
     fprintf(logfp,"%""l" "i"",%f,%f,%f,%f,"
      "%f,%f",gpstime,amperage,
      voltage,latitude,longitude,
      timersecs,mph);
    state = START;
    break;
   default: break;
   }
   if(!(str = strchr(str,';')))
    return;
   break;
  }
 }
}

variable_types:
str
char *

variable_types: timersecs double

variable_types:
state
enum { START=0, FIELD=1 }


decompiled_code:
void event_redraw()
{
wantdraw = 1;
}

canonical_decompiled_code:
void func0()
{
wantdraw = 1;
}

original_code:
void event_redraw() {
 wantdraw = 1;
}

canonical_original_code:
void event_redraw() {
 wantdraw = 1;
}


decompiled_code:
__int64 __fastcall event_set_max_wait(float a1)
{
float v1; // xmm1_4
float v2; // xmm0_4
__int64 result; // rax

if ( a1 < 0.0 )
{
v1 = (float)maxwait;
}
else if ( maxwait < 0 || (v1 = (float)maxwait, (float)maxwait > a1) )
{
v2 = a1 * 1000.0;
result = (unsigned int)(int)v2;
maxwait = (int)v2;
return result;
}
result = (unsigned int)(int)v1;
maxwait = (int)v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(float a1)
{
float v1; // xmm1_4
float v2; // xmm0_4
__int64 result; // rax

if ( a1 < 0.0 )
{
v1 = (float)maxwait;
}
else if ( maxwait < 0 || (v1 = (float)maxwait, (float)maxwait > a1) )
{
v2 = a1 * 1000.0;
result = (unsigned int)(int)v2;
maxwait = (int)v2;
return result;
}
result = (unsigned int)(int)v1;
maxwait = (int)v1;
return result;
}

original_code:
void event_set_max_wait(float seconds) {
 maxwait = seconds >= 0 && (maxwait < 0 || seconds < maxwait)
  ? seconds*1000 : maxwait;
}

variable_types: seconds float


decompiled_code:
void event_refresh()
{
wantrefresh = 1;
}

canonical_decompiled_code:
void func3()
{
wantrefresh = 1;
}


decompiled_code:
char *egl_error_string()
{
int v0; // eax
char *v1; // r8
__int64 v2; // rax

v0 = eglGetError();
v1 = (char *)&unk_448;
v2 = (unsigned int)(v0 - 12288);
if ( (unsigned int)v2 <= 0xE )
v1 = CSWTCH_43[v2];
return v1;
}

canonical_decompiled_code:
char *func0()
{
int v0; // eax
char *v1; // r8
__int64 v2; // rax

v0 = eglGetError();
v1 = (char *)&unk_448;
v2 = (unsigned int)(v0 - 12288);
if ( (unsigned int)v2 <= 0xE )
v1 = CSWTCH_43[v2];
return v1;
}

original_code:
const char *egl_error_string() {
 switch(eglGetError()) {
 case 0x3000: return "EGL_SUCCESS";
 case 0x3001: return "EGL_NOT_INITIALIZED";
 case 0x3002: return "EGL_BAD_ACCESS";
 case 0x3003: return "EGL_BAD_ALLOC";
 case 0x3004: return "EGL_BAD_ATTRIBUTE";
 case 0x3006: return "EGL_BAD_CONTEXT";
 case 0x3005: return "EGL_BAD_CONFIG";
 case 0x3007: return "EGL_BAD_CURRENT_DISPLAY";
 case 0x3008: return "EGL_BAD_DISPLAY";
 case 0x300D: return "EGL_BAD_SURFACE";
 case 0x3009: return "EGL_BAD_MATCH";
 case 0x300C: return "EGL_BAD_PARAMETER";
 case 0x300A: return "EGL_BAD_NATIVE_PIXMAP";
 case 0x300B: return "EGL_BAD_NATIVE_WINDOW";
 case 0x300E: return "EGL_CONTEXT_LOST";
 default: return "";
 }
}

return_type: char *


decompiled_code:
__int64 display_update()
{
return eglSwapBuffers(edisplay, surface);
}

canonical_decompiled_code:
__int64 func1()
{
return eglSwapBuffers(edisplay, surface);
}

original_code:
void display_update() {
 eglSwapBuffers(edisplay,surface);
}


decompiled_code:
int display_stop()
{
eglMakeCurrent(edisplay, 0LL, 0LL, 0LL);
eglDestroyContext(edisplay, context);
eglDestroySurface(edisplay, surface);
eglTerminate(edisplay);
XDestroyWindow(xdisplay, window);
return XCloseDisplay(xdisplay);
}

canonical_decompiled_code:
int func3()
{
eglMakeCurrent(edisplay, 0LL, 0LL, 0LL);
eglDestroyContext(edisplay, context);
eglDestroySurface(edisplay, surface);
eglTerminate(edisplay);
XDestroyWindow(xdisplay, window);
return XCloseDisplay(xdisplay);
}

original_code:
void display_stop() {
 eglMakeCurrent(edisplay,0,0,((EGLContext) (0)));
 eglDestroyContext(edisplay,context);
 eglDestroySurface(edisplay,surface);
 eglTerminate(edisplay);
 XDestroyWindow(xdisplay,window);
 XCloseDisplay(xdisplay);
}


ror

decompiled_code:
__int64 __fastcall ror(int a1, char a2)
{
return (unsigned int)__ROR4__(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, char a2)
{
return (unsigned int)__ROR4__(a1, a2);
}

original_code: static inline uint32_t ror(uint32_t a, int b) { return a >> b | a << (32 - b); }

canonical_original_code: static inline unsigned int ror(unsigned int a, int b) { return a >> b | a << (32 - b); }

variable_types:
a
unsigned int

variable_types: b int

return_type: unsigned int


decompiled_code:
int __fastcall sha256_str(char *s, unsigned __int8 *a2)
{
char *v3; // rbx
char *v4; // r13
__int64 v5; // rdx
char *v6; // rdi
int result; // eax

v3 = s;
v4 = s + 64;
do
{
v5 = *a2;
v6 = v3;
v3 += 2;
++a2;
result = sprintf(v6, "%.2x", v5);
}
while ( v4 != v3 );
return result;
}

canonical_decompiled_code:
int __fastcall func6(char *s, unsigned __int8 *a2)
{
char *v3; // rbx
char *v4; // r13
__int64 v5; // rdx
char *v6; // rdi
int result; // eax

v3 = s;
v4 = s + 64;
do
{
v5 = *a2;
v6 = v3;
v3 += 2;
++a2;
result = sprintf(v6, "%.2x", v5);
}
while ( v4 != v3 );
return result;
}

original_code:
void sha256_str(char *str, uint8_t hash[32])
{
 int i;
 for(i = 0; i < 32; i++)
  sprintf(str + 2*i,"%.2x",hash[i]);
}

canonical_original_code:
void sha256_str(char *str, unsigned char hash[32])
{
 int i;
 for(i = 0; i < 32; i++)
  sprintf(str + 2*i,"%.2x",hash[i]);
}

variable_types:
hash
unsigned char [32]

variable_types: i int


decompiled_code:
int aux_init()
{
unsigned int v0; // er12
clock_t v1; // rbx
int v2; // edx
int v4; // [rsp+Ch] [rbp-C4h] BYREF
struct termios termios_p; // [rsp+10h] [rbp-C0h] BYREF
char buf[128]; // [rsp+50h] [rbp-80h] BYREF

auxfd = open("/dev/ttyACM0", 2306);
if ( auxfd < 0 )
die("cannot open serial port '/dev/ttyACM0'");
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
*(_OWORD *)&termios_p.c_iflag = 0LL;
v0 = 1;
*(_OWORD *)&termios_p.c_line = 0LL;
*(_OWORD *)&termios_p.c_cc[15] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0x1002u);
cfsetispeed(&termios_p, 0x1002u);
termios_p.c_iflag |= 1u;
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
tcsetattr(auxfd, 0, &termios_p);
do
{
printf("connecting to auxiliary, attempt %i...\n", v0);
write(auxfd, "?\r\n", 3uLL);
v1 = clock();
while ( clock() - v1 <= 999999 )
{
ioctl(auxfd, 0x541BuLL, &v4);
v2 = 99;
if ( v4 <= 99 )
v2 = v4;
read(auxfd, buf, v2);
buf[99] = 0;
if ( strstr(buf, "OK") )
return puts("connected to auxiliary board");
}
++v0;
}
while ( v0 != 11 );
die("cannot communciate with auxiliary board");
return puts("connected to auxiliary board");
}

canonical_decompiled_code:
int func0()
{
unsigned int v0; // er12
clock_t v1; // rbx
int v2; // edx
int v4; // [rsp+Ch] [rbp-C4h] BYREF
struct termios termios_p; // [rsp+10h] [rbp-C0h] BYREF
char buf[128]; // [rsp+50h] [rbp-80h] BYREF

auxfd = open("/dev/ttyACM0", 2306);
if ( auxfd < 0 )
die("cannot open serial port '/dev/ttyACM0'");
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
*(_OWORD *)&termios_p.c_iflag = 0LL;
v0 = 1;
*(_OWORD *)&termios_p.c_line = 0LL;
*(_OWORD *)&termios_p.c_cc[15] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0x1002u);
cfsetispeed(&termios_p, 0x1002u);
termios_p.c_iflag |= 1u;
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
tcsetattr(auxfd, 0, &termios_p);
do
{
printf("connecting to auxiliary, attempt %i...\n", v0);
write(auxfd, "?\r\n", 3uLL);
v1 = clock();
while ( clock() - v1 <= 999999 )
{
ioctl(auxfd, 0x541BuLL, &v4);
v2 = 99;
if ( v4 <= 99 )
v2 = v4;
read(auxfd, buf, v2);
buf[99] = 0;
if ( strstr(buf, "OK") )
return puts("connected to auxiliary board");
}
++v0;
}
while ( v0 != 11 );
die("cannot communciate with auxiliary board");
return puts("connected to auxiliary board");
}

original_code:
void aux_init()
{
 int n;
 clock_t c;
 int retries;
 char buf[100];
 struct termios portios;
 auxfd = open("/dev/ttyACM0",02 | 0400 | 04000);
 if(auxfd < 0) die("cannot open serial port '" "/dev/ttyACM0" "'");
 memset(&portios,0,sizeof(portios));
 cfsetospeed(&portios,0010002);
 cfsetispeed(&portios,0010002);
 portios.c_cflag |= 0000060;
 portios.c_iflag |= 0000001;
 portios.c_lflag |= 0000200;
 tcsetattr(auxfd,0,&portios);
 retries = 10;
 while(retries--) {
  printf("connecting to auxiliary, attempt %i...\n",10 - retries);
  write(auxfd,"?\r\n",3);
  c = clock();
  while((clock() - c) < ((__clock_t) 1000000)) {
   ioctl(auxfd,0x541B,&n);
   read(auxfd,buf,n < 100 ? n : 100 - 1);
   buf[100 - 1] = '\0';
   if(strstr(buf,"OK")) goto connected;
  }
 };
 die("cannot communciate with auxiliary board");
connected:
 puts("connected to auxiliary board");
}

canonical_original_code:
void aux_init()
{
 int n;
 long c;
 int retries;
 char buf[100];
 struct termios portios;
 auxfd = open("/dev/ttyACM0",02 | 0400 | 04000);
 if(auxfd < 0) die("cannot open serial port '" "/dev/ttyACM0" "'");
 memset(&portios,0,sizeof(portios));
 cfsetospeed(&portios,0010002);
 cfsetispeed(&portios,0010002);
 portios.c_cflag |= 0000060;
 portios.c_iflag |= 0000001;
 portios.c_lflag |= 0000200;
 tcsetattr(auxfd,0,&portios);
 retries = 10;
 while(retries--) {
  printf("connecting to auxiliary, attempt %i...\n",10 - retries);
  write(auxfd,"?\r\n",3);
  c = clock();
  while((clock() - c) < ((long) 1000000)) {
   ioctl(auxfd,0x541B,&n);
   read(auxfd,buf,n < 100 ? n : 100 - 1);
   buf[100 - 1] = '\0';
   if(strstr(buf,"OK")) goto connected;
  }
 };
 die("cannot communciate with auxiliary board");
connected:
 puts("connected to auxiliary board");
}

variable_types: n int

variable_types: c long

variable_types: retries int

variable_types:
buf
char [100]

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int aux_stop()
{
return close(auxfd);
}

canonical_decompiled_code:
int func1()
{
return close(auxfd);
}

original_code:
void aux_stop() {
 close(auxfd);
}

canonical_original_code:
void aux_stop() {
 close(auxfd);
}


repo: kevinwang/troll

decompiled_code:
__int64 __fastcall ftw_add(const char *filename, const struct stat *status, int flag)
{
if ( !flag )
write_blob(filename, status);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *filename, const struct stat *status, int flag)
{
if ( !flag )
write_blob(filename, status);
return 0LL;
}

original_code:
int ftw_add(const char *fpath, const struct stat *sb, int typeflag) {
    if (typeflag == FTW_F) {
        write_blob((char *) fpath);
    }
    return 0;
}

canonical_original_code:
int ftw_add(char *fpath, struct stat *sb, int typeflag) {
    if (typeflag == FTW_F) {
        write_blob((char *) fpath);
    }
    return 0;
}

variable_types:
fpath
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: typeflag int


decompiled_code:
__int64 __fastcall cmd_add(int a1, __int64 a2)
{
const char **v2; // rbx
__int64 v3; // rbp
const char *v4; // rdi
int *v6; // rax
char *v7; // rax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

die_if_not_troll_repo();
if ( a1 == 1 )
{
puts("Nothing specified, nothing added.");
return 0LL;
}
if ( a1 <= 1 )
return 0LL;
v2 = (const char **)(a2 + 8);
v3 = a2 + 8LL * (unsigned int)(a1 - 2) + 16;
while ( !stat(*v2, &v8) )
{
v4 = *v2;
if ( (v8.st_mode & 0xF000) == 0x4000 )
{
++v2;
ftw(v4, (__ftw_func_t)ftw_add, 256);
if ( v2 == (const char **)v3 )
return 0LL;
}
else
{
write_blob(v4, &v8);
if ( ++v2 == (const char **)v3 )
return 0LL;
}
}
v6 = _errno_location();
v7 = strerror(*v6);
printf("Ain't gon' work: %s\n", v7);
return 128LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
const char **v2; // rbx
__int64 v3; // rbp
const char *v4; // rdi
int *v6; // rax
char *v7; // rax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

die_if_not_troll_repo();
if ( a1 == 1 )
{
puts("Nothing specified, nothing added.");
return 0LL;
}
if ( a1 <= 1 )
return 0LL;
v2 = (const char **)(a2 + 8);
v3 = a2 + 8LL * (unsigned int)(a1 - 2) + 16;
while ( !stat(*v2, &v8) )
{
v4 = *v2;
if ( (v8.st_mode & 0xF000) == 0x4000 )
{
++v2;
ftw(v4, (__ftw_func_t)ftw_add, 256);
if ( v2 == (const char **)v3 )
return 0LL;
}
else
{
write_blob(v4, &v8);
if ( ++v2 == (const char **)v3 )
return 0LL;
}
}
v6 = _errno_location();
v7 = strerror(*v6);
printf("Ain't gon' work: %s\n", v7);
return 128LL;
}

original_code:
int cmd_add(int argc, const char *argv[]) {
    die_if_not_troll_repo();
    if (argc == 1) {
        printf("Nothing specified, nothing added.\n");
        return 0;
    }
    int i;
    for (i = 1; i < argc; i++) {
        struct stat buf;
        if (stat(argv[i], &buf) != 0) {
            printf("Ain't gon' work: %s\n", strerror((*__errno_location ())));
            return 128;
        }
        if (((((buf.st_mode)) & 0170000) == (0040000))) {
            ftw(argv[i], ftw_add, 256);
        }
        else {
            write_blob((char *) argv[i]);
        }
    }
    return 0;
}

canonical_original_code:
int cmd_add(int argc, char *argv[]) {
    die_if_not_troll_repo();
    if (argc == 1) {
        printf("Nothing specified, nothing added.\n");
        return 0;
    }
    int i;
    for (i = 1; i < argc; i++) {
        struct stat buf;
        if (stat(argv[i], &buf) != 0) {
            printf("Ain't gon' work: %s\n", strerror((*__errno_location ())));
            return 128;
        }
        if (((((buf.st_mode)) & 0170000) == (0040000))) {
            ftw(argv[i], ftw_add, 256);
        }
        else {
            write_blob((char *) argv[i]);
        }
    }
    return 0;
}

variable_types: argc int

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


decompiled_code:
__int64 __fastcall file_hash(const char *a1)
{
int v1; // er12
__off_t v2; // r13
char *v3; // rbp
int v4; // ebx
__int64 v5; // r13
struct stat v7; // [rsp+0h] [rbp-B8h] BYREF

v1 = open(a1, 0);
fstat(v1, &v7);
v2 = v7.st_size;
v3 = (char *)malloc(v7.st_size + 15);
sprintf(v3, "blob %lu", v2);
v4 = strlen(v3) + 1;
read(v1, &v3[v4], v7.st_size);
v5 = hash_object(v3, (unsigned int)(LODWORD(v7.st_size) + v4));
close(v1);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // er12
__off_t v2; // r13
char *v3; // rbp
int v4; // ebx
__int64 v5; // r13
struct stat v7; // [rsp+0h] [rbp-B8h] BYREF

v1 = open(a1, 0);
fstat(v1, &v7);
v2 = v7.st_size;
v3 = (char *)malloc(v7.st_size + 15);
sprintf(v3, "blob %lu", v2);
v4 = strlen(v3) + 1;
read(v1, &v3[v4], v7.st_size);
v5 = hash_object(v3, (unsigned int)(LODWORD(v7.st_size) + v4));
close(v1);
return v5;
}

original_code:
char *file_hash(char *filename) {
    int fd;
    fd = open(filename, 00);
    struct stat buf;
    fstat(fd, &buf);
    char *data = (char *) malloc((size_t) buf.st_size + 15);
    sprintf(data, "blob %lu", (unsigned long) buf.st_size);
    int left_part_len = strlen(data) + 1;
    read(fd, data + left_part_len, buf.st_size);
    int full_len = left_part_len + buf.st_size;
    char *hash = hash_object(data, full_len);
    close(fd);
    return hash;
}

variable_types:
filename
char *

variable_types: fd int

variable_types:
data
char *

variable_types:
hash
char *

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


decompiled_code:
__int64 cmd_status()
{
char *v0; // rbx
const char *v1; // rbp
size_t v2; // rax
char *v3; // r13
size_t v4; // rax
size_t v5; // rax
char *v6; // r12
size_t v7; // rax
int v8; // er15
int v9; // er14
char *v10; // rax
size_t v11; // rdx
size_t n; // [rsp+8h] [rbp-40h]
const char *na; // [rsp+8h] [rbp-40h]

die_if_not_troll_repo();
v0 = getcwd(0LL, 0LL);
v1 = (const char *)get_repo_troll_dir();
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 12);
v4 = strlen(v3);
strcpy(stpcpy(&v3[v4], v1), "unstaged.tmp");
v5 = strlen(v1);
v6 = (char *)malloc(v5 + 13);
v7 = strlen(v6);
strcpy(stpcpy(&v6[v7], v1), "untracked.tmp");
v8 = open(v3, 66, 420LL);
v9 = open(v6, 66, 420LL);
n = strlen(v1) - 7;
v10 = (char *)malloc(n);
v11 = n;
na = v10;
strncat(v10, v1, v11);
free(v3);
free(v6);
search_dirs(v8, v9, na);
puts("Changes not staged for commit: ");
puts("Untracked files: ");
chdir(v0);
close(v8);
close(v9);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
char *v0; // rbx
const char *v1; // rbp
size_t v2; // rax
char *v3; // r13
size_t v4; // rax
size_t v5; // rax
char *v6; // r12
size_t v7; // rax
int v8; // er15
int v9; // er14
char *v10; // rax
size_t v11; // rdx
size_t n; // [rsp+8h] [rbp-40h]
const char *na; // [rsp+8h] [rbp-40h]

die_if_not_troll_repo();
v0 = getcwd(0LL, 0LL);
v1 = (const char *)get_repo_troll_dir();
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 12);
v4 = strlen(v3);
strcpy(stpcpy(&v3[v4], v1), "unstaged.tmp");
v5 = strlen(v1);
v6 = (char *)malloc(v5 + 13);
v7 = strlen(v6);
strcpy(stpcpy(&v6[v7], v1), "untracked.tmp");
v8 = open(v3, 66, 420LL);
v9 = open(v6, 66, 420LL);
n = strlen(v1) - 7;
v10 = (char *)malloc(n);
v11 = n;
na = v10;
strncat(v10, v1, v11);
free(v3);
free(v6);
func1(v8, v9, na);
puts("Changes not staged for commit: ");
puts("Untracked files: ");
chdir(v0);
close(v8);
close(v9);
return 0LL;
}

original_code:
int cmd_status(int argc, const char *argv[]) {
    die_if_not_troll_repo();
    char *currdir = getcwd(((void *)0), 0);
    char *trollpath = get_repo_troll_dir();
    char *usta = (char *)malloc(strlen(trollpath) + 12);
    strcat(usta, trollpath);
    strcat(usta, "unstaged.tmp");
    char *utra = (char *)malloc(strlen(trollpath) + 13);
    strcat(utra, trollpath);
    strcat(utra, "untracked.tmp");
    int unstaged = open(usta,02 | 0100, 0644);
    int untracked = open(utra,02 | 0100, 0644);
    char *repo_path = (char *) malloc(strlen(trollpath) - 7);
    strncat(repo_path,trollpath,strlen(trollpath) - 7);
    free(usta);
    free(utra);
    search_dirs(unstaged, untracked, repo_path);
    printf("Changes not staged for commit: \n");
    printf("Untracked files: \n");
    chdir(currdir);
    close(unstaged);
    close(untracked);
    return 0;
}

canonical_original_code:
int cmd_status(int argc, char *argv[]) {
    die_if_not_troll_repo();
    char *currdir = getcwd(((void *)0), 0);
    char *trollpath = get_repo_troll_dir();
    char *usta = (char *)malloc(strlen(trollpath) + 12);
    strcat(usta, trollpath);
    strcat(usta, "unstaged.tmp");
    char *utra = (char *)malloc(strlen(trollpath) + 13);
    strcat(utra, trollpath);
    strcat(utra, "untracked.tmp");
    int unstaged = open(usta,02 | 0100, 0644);
    int untracked = open(utra,02 | 0100, 0644);
    char *repo_path = (char *) malloc(strlen(trollpath) - 7);
    strncat(repo_path,trollpath,strlen(trollpath) - 7);
    free(usta);
    free(utra);
    search_dirs(unstaged, untracked, repo_path);
    printf("Changes not staged for commit: \n");
    printf("Untracked files: \n");
    chdir(currdir);
    close(unstaged);
    close(untracked);
    return 0;
}

variable_types:
currdir
char *

variable_types:
trollpath
char *

variable_types:
usta
char *

variable_types:
utra
char *

variable_types: unstaged int

variable_types: untracked int