decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(
stderr,
"Usage: \t%s read --ip/-i ip_address \n"
"\t\t [--memory-address/-m memory_address] (default to 0x40000000)\n"
"\t\t [--length/-l length] (default to 4)\n"
"\t\t [--big-endian/-b] \n"
"\t\t [--verbose/-v]\n"
"or: \t%s write --ip/-i ip_address \n"
"\t\t [--memory-address/-m memory_address] (default to 0x40000000)\n"
"\t\t [[--file/-f filename]/[--word/-w word_value]] (default to -w 0x1234CAFE)\n"
"\t\t [--check-copy/-c] (not implemented yet)\n"
"\t\t [--big-endian/-b] \n"
"\t\t [--verbose/-v]\n"
"or: \t%s run [--run-address/-r memory_address] (default to 0x00)\n"
"\t\t --ip/-i ip_address\n"
"or: \t%s help\n",
a1,
a1,
a1,
a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return fprintf(
stderr,
"Usage: \t%s read --ip/-i ip_address \n"
"\t\t [--memory-address/-m memory_address] (default to 0x40000000)\n"
"\t\t [--length/-l length] (default to 4)\n"
"\t\t [--big-endian/-b] \n"
"\t\t [--verbose/-v]\n"
"or: \t%s write --ip/-i ip_address \n"
"\t\t [--memory-address/-m memory_address] (default to 0x40000000)\n"
"\t\t [[--file/-f filename]/[--word/-w word_value]] (default to -w 0x1234CAFE)\n"
"\t\t [--check-copy/-c] (not implemented yet)\n"
"\t\t [--big-endian/-b] \n"
"\t\t [--verbose/-v]\n"
"or: \t%s run [--run-address/-r memory_address] (default to 0x00)\n"
"\t\t --ip/-i ip_address\n"
"or: \t%s help\n",
a1,
a1,
a1,
a1);
}

original_code:
static void print_usage(const char* binary_name)
{
 fprintf(stderr,
  "Usage: "
  "\t%s read --ip/-i ip_address "
  "\n\t\t [--memory-address/-m memory_address] (default to 0x40000000)"
  "\n\t\t [--length/-l length] (default to 4)"
  "\n\t\t [--big-endian/-b] "
  "\n\t\t [--verbose/-v]\n"
  "or: "
  "\t%s write --ip/-i ip_address "
  "\n\t\t [--memory-address/-m memory_address] (default to 0x40000000)"
  "\n\t\t [[--file/-f filename]/[--word/-w word_value]] (default to -w 0x1234CAFE)"
  "\n\t\t [--check-copy/-c] (not implemented yet)"
  "\n\t\t [--big-endian/-b] "
  "\n\t\t [--verbose/-v]\n"
  "or: "
  "\t%s run [--run-address/-r memory_address] (default to 0x00)"
  "\n\t\t --ip/-i ip_address\n"
  "or: "
  "\t%s help\n",
  binary_name, binary_name, binary_name, binary_name);
}

canonical_original_code:
static void print_usage(char *binary_name)
{
 fprintf(stderr,
  "Usage: "
  "\t%s read --ip/-i ip_address "
  "\n\t\t [--memory-address/-m memory_address] (default to 0x40000000)"
  "\n\t\t [--length/-l length] (default to 4)"
  "\n\t\t [--big-endian/-b] "
  "\n\t\t [--verbose/-v]\n"
  "or: "
  "\t%s write --ip/-i ip_address "
  "\n\t\t [--memory-address/-m memory_address] (default to 0x40000000)"
  "\n\t\t [[--file/-f filename]/[--word/-w word_value]] (default to -w 0x1234CAFE)"
  "\n\t\t [--check-copy/-c] (not implemented yet)"
  "\n\t\t [--big-endian/-b] "
  "\n\t\t [--verbose/-v]\n"
  "or: "
  "\t%s run [--run-address/-r memory_address] (default to 0x00)"
  "\n\t\t --ip/-i ip_address\n"
  "or: "
  "\t%s help\n",
  binary_name, binary_name, binary_name, binary_name);
}


repo: casasnovas/greth

decompiled_code:
__int64 __fastcall treat_options(int argc, char **argv)
{
int v2; // ebp
char **v3; // rbx
const char *v4; // r8
int v5; // eax
unsigned int v6; // er12

v2 = argc;
v3 = argv;
memset(config, 0, sizeof(config));
if ( argc <= 1 )
{
v6 = 1;
print_usage(*argv);
}
else
{
v4 = argv[1];
if ( !memcmp(v4, "write", 5uLL) )
{
config[88] = 1;
++v3;
--v2;
}
else if ( !strncmp(v4, "read", 4uLL) )
{
config[88] = 2;
++v3;
--v2;
}
while ( 2 )
{
v5 = getopt_long(v2, v3, "f:bhi:l:m:r:vw:", &l_opts, 0LL);
if ( v5 != -1 )
{
switch ( v5 )
{
case 'b':
config[90] = 1;
continue;
case 'c':
continue;
case 'f':
strncpy((char *)(unsigned int)((_DWORD)config + 24), optarg, 0x40uLL);
continue;
case 'h':
print_usage(*v3);
exit(0);
case 'i':
strncpy((char *)(unsigned int)((_DWORD)config + 4), optarg, 0x14uLL);
continue;
case 'l':
*(_DWORD *)&config[100] = strtoul(optarg, 0LL, 0);
continue;
case 'm':
*(_DWORD *)&config[96] = strtoul(optarg, 0LL, 0) & 0xFFFFFFFC;
continue;
case 'v':
config[89] = 1;
continue;
case 'w':
*(_DWORD *)&config[104] = strtoul(optarg, 0LL, 0);
continue;
default:
goto LABEL_7;
}
}
break;
}
v6 = check_config();
if ( v6 )
{
LABEL_7:
v6 = -1;
print_usage(*v3);
}
else if ( config[89] )
{
fprintf(
stderr,
"/**\n"
" * greth - read/write from/to ahb bus\n"
" * \t by Quentin Casasnovas\n"
" */\n"
"\n"
"Configuration :\n"
"\t IP:\t\t %s\n"
"\t Address:\t 0x%08x\n"
"\t Verbose:\t %d\n"
"\t Big endian:\t %d\n"
"\n",
(const char *)(unsigned int)((_DWORD)config + 4),
*(unsigned int *)&config[96],
(unsigned int)config[89],
(unsigned int)config[90]);
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(int argc, char **argv)
{
int v2; // ebp
char **v3; // rbx
const char *v4; // r8
int v5; // eax
unsigned int v6; // er12

v2 = argc;
v3 = argv;
memset(config, 0, sizeof(config));
if ( argc <= 1 )
{
v6 = 1;
func1(*argv);
}
else
{
v4 = argv[1];
if ( !memcmp(v4, "write", 5uLL) )
{
config[88] = 1;
++v3;
--v2;
}
else if ( !strncmp(v4, "read", 4uLL) )
{
config[88] = 2;
++v3;
--v2;
}
while ( 2 )
{
v5 = getopt_long(v2, v3, "f:bhi:l:m:r:vw:", &l_opts, 0LL);
if ( v5 != -1 )
{
switch ( v5 )
{
case 'b':
config[90] = 1;
continue;
case 'c':
continue;
case 'f':
strncpy((char *)(unsigned int)((_DWORD)config + 24), optarg, 0x40uLL);
continue;
case 'h':
func1(*v3);
exit(0);
case 'i':
strncpy((char *)(unsigned int)((_DWORD)config + 4), optarg, 0x14uLL);
continue;
case 'l':
*(_DWORD *)&config[100] = strtoul(optarg, 0LL, 0);
continue;
case 'm':
*(_DWORD *)&config[96] = strtoul(optarg, 0LL, 0) & 0xFFFFFFFC;
continue;
case 'v':
config[89] = 1;
continue;
case 'w':
*(_DWORD *)&config[104] = strtoul(optarg, 0LL, 0);
continue;
default:
goto LABEL_7;
}
}
break;
}
v6 = func0();
if ( v6 )
{
LABEL_7:
v6 = -1;
func1(*v3);
}
else if ( config[89] )
{
fprintf(
stderr,
"/**\n"
" * greth - read/write from/to ahb bus\n"
" * \t by Quentin Casasnovas\n"
" */\n"
"\n"
"Configuration :\n"
"\t IP:\t\t %s\n"
"\t Address:\t 0x%08x\n"
"\t Verbose:\t %d\n"
"\t Big endian:\t %d\n"
"\n",
(const char *)(unsigned int)((_DWORD)config + 4),
*(unsigned int *)&config[96],
(unsigned int)config[89],
(unsigned int)config[90]);
}
}
return v6;
}

original_code:
static int treat_options(int argc, char** argv)
{
 int option = 0;
 memset(&config, 0x0, sizeof (greth_conf_t));
 if (argc <= 1) {
  print_usage(argv[0]);
  return (1);
 }
 if (strncmp(argv[1], "write", 5) == 0) {
  config.action = 1;
  ++argv;
  --argc;
 }
 else if (strncmp(argv[1], "read", 4) == 0) {
  config.action = 2;
  ++argv;
  --argc;
 }
 while ((option = getopt_long(argc, argv, "f:bhi:l:m:r:vw:", l_opts, ((void *)0))) != (-1)) {
  switch (option) {
  case 'f':
   strncpy(config.filename, optarg, 64);
   break;
  case 'b':
   config.big_endian = 1;
   break;
  case 'c':
   break;
  case 'h':
   print_usage(argv[0]);
   exit (0);
   break;
  case 'i':
   strncpy(config.ip, optarg, 20);
   break;
  case 'l':
   config.data_size = strtoul(optarg, ((void *)0), 0);
   break;
  case 'm':
   config.memory_address = strtoul(optarg, ((void *)0), 0);
   config.memory_address &= ~3;
   break;
  case 'v':
   config.verbose = 1;
   break;
  case 'w':
   config.word = strtoul(optarg, ((void *)0), 0);
   break;
  default:
   goto error_args;
   break;
  }
 }
 if (check_config() != 0)
  goto error_args;
 if (config.verbose)
  fprintf(stderr,
   "/**\n" " * greth - read/write from/to ahb bus\n" " * \t by Quentin Casasnovas\n" " */\n\n"
   "Configuration :\n"
   "\t IP:\t\t %s\n"
   "\t Address:\t 0x%08x\n"
   "\t Verbose:\t %d\n"
   "\t Big endian:\t %d\n\n",
   config.ip, config.memory_address,
   config.verbose, config.big_endian);
 return (0);
 error_args:
 print_usage(argv[0]);
 return (-1);
}

canonical_original_code:
static int treat_options(int argc, char **argv)
{
 int option = 0;
 memset(&config, 0x0, sizeof (greth_conf_t));
 if (argc <= 1) {
  print_usage(argv[0]);
  return (1);
 }
 if (strncmp(argv[1], "write", 5) == 0) {
  config.action = 1;
  ++argv;
  --argc;
 }
 else if (strncmp(argv[1], "read", 4) == 0) {
  config.action = 2;
  ++argv;
  --argc;
 }
 while ((option = getopt_long(argc, argv, "f:bhi:l:m:r:vw:", l_opts, ((void *)0))) != (-1)) {
  switch (option) {
  case 'f':
   strncpy(config.filename, optarg, 64);
   break;
  case 'b':
   config.big_endian = 1;
   break;
  case 'c':
   break;
  case 'h':
   print_usage(argv[0]);
   exit (0);
   break;
  case 'i':
   strncpy(config.ip, optarg, 20);
   break;
  case 'l':
   config.data_size = strtoul(optarg, ((void *)0), 0);
   break;
  case 'm':
   config.memory_address = strtoul(optarg, ((void *)0), 0);
   config.memory_address &= ~3;
   break;
  case 'v':
   config.verbose = 1;
   break;
  case 'w':
   config.word = strtoul(optarg, ((void *)0), 0);
   break;
  default:
   goto error_args;
   break;
  }
 }
 if (check_config() != 0)
  goto error_args;
 if (config.verbose)
  fprintf(stderr,
   "/**\n" " * greth - read/write from/to ahb bus\n" " * \t by Quentin Casasnovas\n" " */\n\n"
   "Configuration :\n"
   "\t IP:\t\t %s\n"
   "\t Address:\t 0x%08x\n"
   "\t Verbose:\t %d\n"
   "\t Big endian:\t %d\n\n",
   config.ip, config.memory_address,
   config.verbose, config.big_endian);
 return (0);
 error_args:
 print_usage(argv[0]);
 return (-1);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: option int

return_type: int


main

canonical_name: main

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

v3 = treat_options(argc, (char **)argv);
if ( !v3 )
{
v3 = create_connections();
if ( !v3 )
{
if ( config[88] == 1 )
{
if ( config[24] )
{
v5 = send_file();
if ( !v5 )
goto LABEL_7;
}
else
{
v5 = send_word();
if ( !v5 )
goto LABEL_7;
}
return v5;
}
LABEL_7:
close_connections();
return v3;
}
}
return v3;
}

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

v3 = func2(argc, (char **)argv);
if ( !v3 )
{
v3 = create_connections();
if ( !v3 )
{
if ( config[88] == 1 )
{
if ( config[24] )
{
v5 = send_file();
if ( !v5 )
goto LABEL_7;
}
else
{
v5 = send_word();
if ( !v5 )
goto LABEL_7;
}
return v5;
}
LABEL_7:
close_connections();
return v3;
}
}
return v3;
}

original_code:
int main(int argc, char** argv)
{
 int err_num;
 if ((err_num = treat_options(argc, argv)) != 0)
  goto error;
 if ((err_num = create_connections()) != 0)
  goto error;
 if (config.action == 1) {
  if (config.filename[0] != 0) {
   if ((err_num = send_file()) != 0)
    goto error;
  }
  else {
   if ((err_num = send_word()) != 0)
    goto error;
  }
 }
 close_connections();
 return (0);
 error:
 return (err_num);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int err_num;
 if ((err_num = treat_options(argc, argv)) != 0)
  goto error;
 if ((err_num = create_connections()) != 0)
  goto error;
 if (config.action == 1) {
  if (config.filename[0] != 0) {
   if ((err_num = send_file()) != 0)
    goto error;
  }
  else {
   if ((err_num = send_word()) != 0)
    goto error;
  }
 }
 close_connections();
 return (0);
 error:
 return (err_num);
}


decompiled_code:
size_t __fastcall swap_bytes(unsigned int *a1, int a2)
{
__int64 v2; // rdx
unsigned int v3; // eax
size_t result; // rax

if ( a2 )
{
v2 = (__int64)&a1[a2 - 1 + 1];
do
{
v3 = *a1++;
result = _byteswap_ulong(v3);
*(a1 - 1) = result;
}
while ( a1 != (unsigned int *)v2 );
}
if ( config[89] )
result = fwrite("Bytes have been swapped to big endian.\n", 1uLL, 0x27uLL, stderr);
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(unsigned int *a1, int a2)
{
__int64 v2; // rdx
unsigned int v3; // eax
size_t result; // rax

if ( a2 )
{
v2 = (__int64)&a1[a2 - 1 + 1];
do
{
v3 = *a1++;
result = _byteswap_ulong(v3);
*(a1 - 1) = result;
}
while ( a1 != (unsigned int *)v2 );
}
if ( config[89] )
result = fwrite("Bytes have been swapped to big endian.\n", 1uLL, 0x27uLL, stderr);
return result;
}

original_code:
static inline void swap_bytes(unsigned int* data_i,
       unsigned int data_size)
{
 unsigned int i = 0;
 for (i = 0; i < data_size; ++i)
  data_i[i] = ((((data_i[i]) & 0xff) << 24) | (((data_i[i]) & 0xff00) << 8) | (((data_i[i]) & 0xff0000) >> 8) | (((data_i[i]) & 0xff000000) >> 24));
 if (config.verbose)
  fprintf(stderr,
   "Bytes have been swapped to big endian.\n");
}

canonical_original_code:
static inline void swap_bytes(unsigned int *data_i, unsigned int data_size)
{
 unsigned int i = 0;
 for (i = 0; i < data_size; ++i)
  data_i[i] = ((((data_i[i]) & 0xff) << 24) | (((data_i[i]) & 0xff00) << 8) | (((data_i[i]) & 0xff0000) >> 8) | (((data_i[i]) & 0xff000000) >> 24));
 if (config.verbose)
  fprintf(stderr,
   "Bytes have been swapped to big endian.\n");
}

variable_types:
i
unsigned int

return_type: void


original_code:
static int send_fragmented(void)
{
 unsigned int nb_ack_packet = 0;
 unsigned int address_offset = 0;
 unsigned int nb_sent_packet = 0;
 unsigned int local_data_size = config.data_size;
 edcl_packet_t* packet = ((void *)0);
 packet = calloc(1, ((local_data_size) < (456) ? (local_data_size) : (456)) + sizeof (edcl_header_t));
 if (packet == ((void *)0))
  goto error_malloc;
 do {
  bzero(&(packet->header), sizeof (edcl_header_t));
  (packet->header).layer_field |= ((1 & 0x1) << 9);
  (packet->header).layer_field |= (((config.sequence_number) & 0x3f) << 10) | (((config.sequence_number) & 0x3fc0) >> 6);
  (packet->header).address = ((((config.memory_address + address_offset) & 0xff) << 24) | (((config.memory_address + address_offset) & 0xff00) << 8) | (((config.memory_address + address_offset) & 0xff0000) >> 8) | (((config.memory_address + address_offset) & 0xff000000) >> 24));
  (packet->header).layer_field |= (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x001) >> 0 << 31) | (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x1fe) >> 1 << 16) | (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x200) >> 9 << 8);
  if (config.verbose)
   fprintf(stderr,
    " [%d] Trying to write at 0x%08x.",
    config.sequence_number,
    config.memory_address + address_offset);
  memcpy(packet->data,
         config.data_c + address_offset,
         ((local_data_size) < (456) ? (local_data_size) : (456)));
  if (sendto(config.socket,
      packet,
      ((local_data_size) < (456) ? (local_data_size) : (456)) + sizeof (edcl_header_t),
      0, config.serv_info->ai_addr, config.serv_info->ai_addrlen)
      == -1)
   goto error_send;
  if (recvfrom(config.socket, packet,
        sizeof (edcl_header_t) + ((local_data_size) < (456) ? (local_data_size) : (456)),
        0, ((void *)0), 0) == -1)
   goto error_recvfrom;
  if (!(((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) & 0x20000) >> 17)) {
   if (config.verbose)
    fprintf(stderr,
     "\t[OK]\n");
   address_offset += ((local_data_size) < (456) ? (local_data_size) : (456));
   local_data_size -= ((local_data_size) < (456) ? (local_data_size) : (456));
   ++config.sequence_number;
   ++nb_ack_packet;
  }
  else {
   if (config.verbose)
    fprintf(stderr,
     "\t[Failed]\n  Wrong sequence number (%d) should be: %d. Fixed.\n",
     config.sequence_number, ((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) >> 18));
   config.sequence_number = ((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) >> 18);
  }
  ++nb_sent_packet;
 } while (local_data_size > 0);
 if (config.verbose) {
  fprintf(stderr,
   "The datas have been fragmented in %d packets.\n", nb_ack_packet);
  fprintf(stderr,
   "\t %d packets have been sent (%d have been lost).\n",
   nb_sent_packet,
   nb_sent_packet - nb_ack_packet);
 }
 free(packet);
 return (0);
 error_recvfrom:
 error_send:
 if (config.verbose)
  fprintf(stderr,
   "Error while sending the packet.\n");
 free(packet);
 return ((*__errno_location ()));
 error_malloc:
 if (config.verbose)
  fprintf(stderr,
   "Unable to allocate the packet.\n");
 return ((*__errno_location ()));
}

canonical_original_code:
static int send_fragmented()
{
 unsigned int nb_ack_packet = 0;
 unsigned int address_offset = 0;
 unsigned int nb_sent_packet = 0;
 unsigned int local_data_size = config.data_size;
 struct edcl_packet_t *packet = ((void *)0);
 packet = calloc(1, ((local_data_size) < (456) ? (local_data_size) : (456)) + sizeof (edcl_header_t));
 if (packet == ((void *)0))
  goto error_malloc;
 do {
  bzero(&(packet->header), sizeof (edcl_header_t));
  (packet->header).layer_field |= ((1 & 0x1) << 9);
  (packet->header).layer_field |= (((config.sequence_number) & 0x3f) << 10) | (((config.sequence_number) & 0x3fc0) >> 6);
  (packet->header).address = ((((config.memory_address + address_offset) & 0xff) << 24) | (((config.memory_address + address_offset) & 0xff00) << 8) | (((config.memory_address + address_offset) & 0xff0000) >> 8) | (((config.memory_address + address_offset) & 0xff000000) >> 24));
  (packet->header).layer_field |= (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x001) >> 0 << 31) | (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x1fe) >> 1 << 16) | (((((local_data_size) < (456) ? (local_data_size) : (456))) & 0x200) >> 9 << 8);
  if (config.verbose)
   fprintf(stderr,
    " [%d] Trying to write at 0x%08x.",
    config.sequence_number,
    config.memory_address + address_offset);
  memcpy(packet->data,
         config.data_c + address_offset,
         ((local_data_size) < (456) ? (local_data_size) : (456)));
  if (sendto(config.socket,
      packet,
      ((local_data_size) < (456) ? (local_data_size) : (456)) + sizeof (edcl_header_t),
      0, config.serv_info->ai_addr, config.serv_info->ai_addrlen)
      == -1)
   goto error_send;
  if (recvfrom(config.socket, packet,
        sizeof (edcl_header_t) + ((local_data_size) < (456) ? (local_data_size) : (456)),
        0, ((void *)0), 0) == -1)
   goto error_recvfrom;
  if (!(((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) & 0x20000) >> 17)) {
   if (config.verbose)
    fprintf(stderr,
     "\t[OK]\n");
   address_offset += ((local_data_size) < (456) ? (local_data_size) : (456));
   local_data_size -= ((local_data_size) < (456) ? (local_data_size) : (456));
   ++config.sequence_number;
   ++nb_ack_packet;
  }
  else {
   if (config.verbose)
    fprintf(stderr,
     "\t[Failed]\n  Wrong sequence number (%d) should be: %d. Fixed.\n",
     config.sequence_number, ((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) >> 18));
   config.sequence_number = ((((((packet->header).layer_field) & 0xff) << 24) | ((((packet->header).layer_field) & 0xff00) << 8) | ((((packet->header).layer_field) & 0xff0000) >> 8) | ((((packet->header).layer_field) & 0xff000000) >> 24)) >> 18);
  }
  ++nb_sent_packet;
 } while (local_data_size > 0);
 if (config.verbose) {
  fprintf(stderr,
   "The datas have been fragmented in %d packets.\n", nb_ack_packet);
  fprintf(stderr,
   "\t %d packets have been sent (%d have been lost).\n",
   nb_sent_packet,
   nb_sent_packet - nb_ack_packet);
 }
 free(packet);
 return (0);
 error_recvfrom:
 error_send:
 if (config.verbose)
  fprintf(stderr,
   "Error while sending the packet.\n");
 free(packet);
 return ((*__errno_location ()));
 error_malloc:
 if (config.verbose)
  fprintf(stderr,
   "Unable to allocate the packet.\n");
 return ((*__errno_location ()));
}

variable_types: struct edcl_packet_t { struct edcl_header_t { unsigned short offset; unsigned int layer_field; unsigned int address; } header; unsigned int data[]; } *

user_defined_types: struct edcl_packet_t { struct edcl_header_t header; unsigned int data[]; }


decompiled_code:
__int64 send_word()
{
bool v0; // zf
__int64 result; // rax

v0 = BYTE2(config[22]) == 0;
config[25] = 4;
if ( v0 )
config[26] = _byteswap_ulong(config[26]);
*(_QWORD *)&config[30] = &config[26];
result = send_fragmented();
if ( (_DWORD)result )
{
fwrite("Unable to send the word.\n", 1uLL, 0x19uLL, stderr);
result = (unsigned int)*_errno_location();
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
bool v0; // zf
__int64 result; // rax

v0 = BYTE2(config[22]) == 0;
config[25] = 4;
if ( v0 )
config[26] = _byteswap_ulong(config[26]);
*(_QWORD *)&config[30] = &config[26];
result = func1();
if ( (_DWORD)result )
{
fwrite("Unable to send the word.\n", 1uLL, 0x19uLL, stderr);
result = (unsigned int)*_errno_location();
}
return result;
}

original_code:
int send_word(void)
{
 config.data_size = 4;
 if (!(config.big_endian))
  config.word = ((((config.word) & 0xff) << 24) | (((config.word) & 0xff00) << 8) | (((config.word) & 0xff0000) >> 8) | (((config.word) & 0xff000000) >> 24));
 config.data = &(config.word);
 if (send_fragmented() != 0)
  goto error_send;
 return (0);
 error_send:
 fprintf(stderr,
  "Unable to send the word.\n");
 return ((*__errno_location ()));
}

canonical_original_code:
int send_word()
{
 config.data_size = 4;
 if (!(config.big_endian))
  config.word = ((((config.word) & 0xff) << 24) | (((config.word) & 0xff00) << 8) | (((config.word) & 0xff0000) >> 8) | (((config.word) & 0xff000000) >> 24));
 config.data = &(config.word);
 if (send_fragmented() != 0)
  goto error_send;
 return (0);
 error_send:
 fprintf(stderr,
  "Unable to send the word.\n");
 return ((*__errno_location ()));
}


decompiled_code:
__int64 create_connections()
{
__int64 result; // rax
struct addrinfo v1; // [rsp+0h] [rbp-38h] BYREF

*(_OWORD *)&v1.ai_flags = 0LL;
*(_OWORD *)&v1.ai_addrlen = 0LL;
*(_OWORD *)&v1.ai_canonname = 0LL;
*(_QWORD *)&v1.ai_family = 0x200000002LL;
getaddrinfo(
(const char *)(unsigned int)((_DWORD)&config + 4),
"4242",
&v1,
(struct addrinfo **)(unsigned int)((_DWORD)&config + 112));
LODWORD(config) = socket((*(&config + 14))->ai_family, (*(&config + 14))->ai_socktype, (*(&config + 14))->ai_protocol);
if ( (_DWORD)config == -1 )
{
if ( *((_BYTE *)&config + 89) )
fwrite("Error while trying to connect to the ethernet IP.\n", 1uLL, 0x32uLL, stderr);
result = (unsigned int)*_errno_location();
}
else
{
if ( *((_BYTE *)&config + 89) )
fwrite("Connection to the ethernet IP opened.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
struct addrinfo v1; // [rsp+0h] [rbp-38h] BYREF

*(_OWORD *)&v1.ai_flags = 0LL;
*(_OWORD *)&v1.ai_addrlen = 0LL;
*(_OWORD *)&v1.ai_canonname = 0LL;
*(_QWORD *)&v1.ai_family = 0x200000002LL;
getaddrinfo(
(const char *)(unsigned int)((_DWORD)&config + 4),
"4242",
&v1,
(struct addrinfo **)(unsigned int)((_DWORD)&config + 112));
LODWORD(config) = socket((*(&config + 14))->ai_family, (*(&config + 14))->ai_socktype, (*(&config + 14))->ai_protocol);
if ( (_DWORD)config == -1 )
{
if ( *((_BYTE *)&config + 89) )
fwrite("Error while trying to connect to the ethernet IP.\n", 1uLL, 0x32uLL, stderr);
result = (unsigned int)*_errno_location();
}
else
{
if ( *((_BYTE *)&config + 89) )
fwrite("Connection to the ethernet IP opened.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int create_connections(void)
{
 unsigned short int source_port;
 struct addrinfo hints;
 memset(&hints, 0, sizeof (struct addrinfo));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 getaddrinfo(config.ip, "4242", &hints, &(config.serv_info));
 if ((config.socket = socket(config.serv_info->ai_family,
        config.serv_info->ai_socktype,
        config.serv_info->ai_protocol)) == -1)
  goto error_socket;
 if (config.verbose)
  fprintf(stderr,
   "Connection to the ethernet IP opened.\n");
 return (0);
 error_connect:
 close(config.socket);
 error_socket:
 if (config.verbose)
  fprintf(stderr,
   "Error while trying to connect to the ethernet IP.\n");
 return ((*__errno_location ()));
}

canonical_original_code:
int create_connections()
{
 unsigned short source_port;
 struct addrinfo hints;
 memset(&hints, 0, sizeof (struct addrinfo));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 getaddrinfo(config.ip, "4242", &hints, &(config.serv_info));
 if ((config.socket = socket(config.serv_info->ai_family,
        config.serv_info->ai_socktype,
        config.serv_info->ai_protocol)) == -1)
  goto error_socket;
 if (config.verbose)
  fprintf(stderr,
   "Connection to the ethernet IP opened.\n");
 return (0);
 error_connect:
 close(config.socket);
 error_socket:
 if (config.verbose)
  fprintf(stderr,
   "Error while trying to connect to the ethernet IP.\n");
 return ((*__errno_location ()));
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
int close_connections()
{
if ( *((_BYTE *)&config + 89) )
fwrite("Connection closed.\n", 1uLL, 0x13uLL, stderr);
freeaddrinfo(*(&config + 14));
return close((int)config);
}

canonical_decompiled_code:
int func1()
{
if ( *((_BYTE *)&config + 89) )
fwrite("Connection closed.\n", 1uLL, 0x13uLL, stderr);
freeaddrinfo(*(&config + 14));
return close((int)config);
}

original_code:
void close_connections(void)
{
 if (config.verbose)
  fprintf(stderr,
   "Connection closed.\n");
 freeaddrinfo(config.serv_info);
 close(config.socket);
}

canonical_original_code:
void close_connections()
{
 if (config.verbose)
  fprintf(stderr,
   "Connection closed.\n");
 freeaddrinfo(config.serv_info);
 close(config.socket);
}


wait

repo: casfire/BinToHeader

decompiled_code:
int __fastcall wait(const char *a1)
{
fflush(stdin);
return fgetc(stdin);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
fflush(stdin);
return fgetc(stdin);
}

original_code:
void wait() {
 fflush(stdin);
 fgetc(stdin);
}


isNumber

decompiled_code:
_BOOL8 __fastcall isNumber(char a1)
{
return (unsigned __int8)(a1 - 48) <= 9u;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(char a1)
{
return (unsigned __int8)(a1 - 48) <= 9u;
}

original_code:
int isNumber(char c) {
 return (c >= '0' && c <= '9') ? 1 : 0;
}

variable_types: c char


isAlphanumeric

decompiled_code:
__int64 __fastcall isAlphanumeric(char a1)
{
unsigned int v1; // er8

v1 = 1;
if ( (unsigned __int8)((a1 & 0xDF) - 65) > 0x19u )
LOBYTE(v1) = isNumber(a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1)
{
unsigned int v1; // er8

v1 = 1;
if ( (unsigned __int8)((a1 & 0xDF) - 65) > 0x19u )
LOBYTE(v1) = func8(a1);
return v1;
}

original_code:
int isAlphanumeric(char c) {
 if (c >= 'a' && c <= 'z') return 1;
 if (c >= 'A' && c <= 'Z') return 1;
 if (isNumber(c)) return 1;
 return 0;
}


main

repo: castl/easyperf

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rsi
int v4; // ebx
char v5; // cl
__int64 v6; // r12
__int64 v8[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v9[5]; // [rsp+10h] [rbp-28h] BYREF

v3 = 60LL;
v4 = 10;
perf_init(2uLL, v8[0]);
do
{
v5 = v4++;
v6 = 1LL << v5;
perf_read_all(v8, v3);
foo(v6);
perf_read_all(v9, v3);
v3 = v6;
printf("[%6lu] Cycles: %10lu, Instrs: %10lu\n", v6, v9[0] - v8[0], v9[1] - v8[1]);
}
while ( v4 != 20 );
perf_close();
return 0;
}

original_code:
int main(void) {
 uint64_t start[2], end[2];
 unsigned long i;
 perf_init(2, (0x3C | (0x0 << 8)), (0xC0 | (0x0 << 8)));
 for (i = 0; i<10; i++) {
  unsigned long loops = 1ul << (i + 10);
  perf_read_all(start);
  foo(loops);
  perf_read_all(end);
  printf("[%6lu] Cycles: %10lu, Instrs: %10lu\n",
    loops, end[0] - start[0], end[1] - start[1]);
 }
 perf_close();
 return 0;
}

canonical_original_code:
int main() {
 unsigned long start[2]; unsigned long end[2];
 unsigned long i;
 perf_init(2, (0x3C | (0x0 << 8)), (0xC0 | (0x0 << 8)));
 for (i = 0; i<10; i++) {
  unsigned long loops = 1ul << (i + 10);
  perf_read_all(start);
  foo(loops);
  perf_read_all(end);
  printf("[%6lu] Cycles: %10lu, Instrs: %10lu\n",
    loops, end[0] - start[0], end[1] - start[1]);
 }
 perf_close();
 return 0;
}

variable_types:
start
unsigned long [2]

variable_types:
end
unsigned long [2]

variable_types:
i
unsigned long

variable_types:
loops
unsigned long


decompiled_code:
void perf_close()
{
unsigned int v0; // ebx
__int64 v1; // rdx

if ( number )
{
v0 = 0;
do
{
v1 = v0++;
close(*((_DWORD *)fds + v1));
}
while ( number > v0 );
}
number = 0;
if ( attr )
{
free(attr);
attr = 0LL;
}
if ( fds )
{
free(fds);
fds = 0LL;
}
}

canonical_decompiled_code:
void func9()
{
unsigned int v0; // ebx
__int64 v1; // rdx

if ( number )
{
v0 = 0;
do
{
v1 = v0++;
close(*((_DWORD *)fds + v1));
}
while ( number > v0 );
}
number = 0;
if ( attr )
{
free(attr);
attr = 0LL;
}
if ( fds )
{
free(fds);
fds = 0LL;
}
}

original_code:
void perf_close() {
 unsigned int i;
 for (i=0; i<number; i++) {
  close(fds[i]);
 }
 number = 0;
 if (attr) {
  free(attr);
  attr = ((void *)0);
 }
 if (fds) {
  free(fds);
  fds = ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall perf_read(unsigned int a1)
{
__int64 buf; // [rsp+8h] [rbp-10h] BYREF

if ( number <= a1 )
__assert_fail("ctr < number", "easyperf.c", 0x64u, "perf_read");
if ( (unsigned int)read(*((_DWORD *)fds + a1), &buf, 8uLL) != 8 )
__assert_fail("rc == sizeof(val)", "easyperf.c", 0x66u, "perf_read");
return buf;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1)
{
__int64 buf; // [rsp+8h] [rbp-10h] BYREF

if ( number <= a1 )
__assert_fail("ctr < number", "easyperf.c", 0x64u, "perf_read");
if ( (unsigned int)read(*((_DWORD *)fds + a1), &buf, 8uLL) != 8 )
__assert_fail("rc == sizeof(val)", "easyperf.c", 0x66u, "perf_read");
return buf;
}

original_code:
uint64_t perf_read(unsigned int ctr) {
 uint64_t val;
 int rc;
 ((void) sizeof ((ctr < number) ? 1 : 0), __extension__ ({ if (ctr < number) ; else __assert_fail ("ctr < number", "easyperf.c", 100, __extension__ __PRETTY_FUNCTION__); }));
 rc = read(fds[ctr], &val, sizeof(val));
 ((void) sizeof ((rc == sizeof(val)) ? 1 : 0), __extension__ ({ if (rc == sizeof(val)) ; else __assert_fail ("rc == sizeof(val)", "easyperf.c", 102, __extension__ __PRETTY_FUNCTION__); }));
 return val;
}

canonical_original_code:
unsigned long perf_read(unsigned int ctr) {
 unsigned long val;
 int rc;
 ((void) sizeof ((ctr < number) ? 1 : 0), __extension__ ({ if (ctr < number) ; else __assert_fail ("ctr < number", "easyperf.c", 100, __extension__ __PRETTY_FUNCTION__); }));
 rc = read(fds[ctr], &val, sizeof(val));
 ((void) sizeof ((rc == sizeof(val)) ? 1 : 0), __extension__ ({ if (rc == sizeof(val)) ; else __assert_fail ("rc == sizeof(val)", "easyperf.c", 102, __extension__ __PRETTY_FUNCTION__); }));
 return val;
}

variable_types:
ctr
unsigned int

variable_types:
val
unsigned long

variable_types: rc int

return_type: unsigned long


decompiled_code:
__int64 __fastcall perf_read_all(__int64 a1)
{
__int64 result; // rax
unsigned int v3; // ebx
__int64 v4; // rax
unsigned int v5; // edi
__int64 *v6; // rbp
bool v7; // cc

result = (unsigned int)number;
if ( number )
{
v3 = 0;
do
{
v4 = v3;
v5 = v3++;
v6 = (__int64 *)(a1 + 8 * v4);
result = perf_read(v5);
v7 = number <= v3;
*v6 = result;
}
while ( !v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
__int64 result; // rax
unsigned int v3; // ebx
__int64 v4; // rax
unsigned int v5; // edi
__int64 *v6; // rbp
bool v7; // cc

result = (unsigned int)number;
if ( number )
{
v3 = 0;
do
{
v4 = v3;
v5 = v3++;
v6 = (__int64 *)(a1 + 8 * v4);
result = func10(v5);
v7 = number <= v3;
*v6 = result;
}
while ( !v7 );
}
return result;
}

original_code:
void perf_read_all(uint64_t* vals) {
 unsigned int i;
 for (i=0; i<number; i++) {
  vals[i] = perf_read(i);
 }
}

canonical_original_code:
void perf_read_all(unsigned long *vals) {
 unsigned int i;
 for (i=0; i<number; i++) {
  vals[i] = perf_read(i);
 }
}

variable_types:
vals
unsigned long *


foo

decompiled_code:
void __fastcall foo(__int64 a1)
{
__int64 i; // rbx
double v2; // xmm0_8

if ( a1 )
{
for ( i = 0LL; i != a1; ++i )
{
while ( (i & 1) != 0 )
{
++i;
d = d * d;
if ( a1 == i )
return;
}
if ( d < 0.0 )
v2 = sqrt(d);
else
v2 = sqrt(d);
d = v2;
}
}
}

canonical_decompiled_code:
void __fastcall func12(__int64 a1)
{
__int64 i; // rbx
double v2; // xmm0_8

if ( a1 )
{
for ( i = 0LL; i != a1; ++i )
{
while ( (i & 1) != 0 )
{
++i;
d = d * d;
if ( a1 == i )
return;
}
if ( d < 0.0 )
v2 = sqrt(d);
else
v2 = sqrt(d);
d = v2;
}
}
}

original_code:
void foo(unsigned long l) {
 unsigned long i;
 for (i=0; i<l; i++) {
  if (i & 1) {
   d = pow(d, 2);
  } else {
   d = sqrt(d);
  }
 }
}

variable_types:
l
unsigned long


decompiled_code:
__int64 __fastcall t2(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // ebx
__int64 v4; // rdi
int v5; // ebp
__int64 v6; // rdx
__int64 result; // rax

if ( (_DWORD)a1 )
{
v3 = a1;
v4 = (unsigned int)(a1 - 1);
v5 = t2(v4);
adjust_frame(v4, a2, v6);
result = (unsigned int)(v5 * v3);
}
else
{
ct = 2;
adjust_frame(a1, a2, a3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // ebx
__int64 v4; // rdi
int v5; // ebp
__int64 v6; // rdx
__int64 result; // rax

if ( (_DWORD)a1 )
{
v3 = a1;
v4 = (unsigned int)(a1 - 1);
v5 = func7(v4);
func9(v4, a2, v6);
result = (unsigned int)(v5 * v3);
}
else
{
ct = 2;
func9(a1, a2, a3);
result = 1LL;
}
return result;
}

original_code:
int t2(int numero_t2){
 int a[10];
 int temp = 1;
 if (numero_t2){
  temp = t2(numero_t2-1);
  adjust_frame();
  return numero_t2 * temp;
 }else{
  ct=2;
  adjust_frame();
  return 1;
 }
}

variable_types:
a
int [10]

variable_types: temp int


decompiled_code:
__int64 __fastcall adjust_frame(__int64 a1, __int64 a2)
{
return yield(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
return func8(a1, a2);
}

original_code:
void adjust_frame(){
 yield();
 return;
}

canonical_original_code:
void adjust_frame(){
 yield();
 return;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
fin = 0LL;
start = 0x100000001LL;
dword_4044B0 = 0;
dword_4044C0 = 1;
ct = 3;
adjust_frame(argc, argv, envp);
printf(
"Thread 0 - Resultado:%i\nThread 1 - Resultado:%i\nThread 2 - Resultado:%i\n",
(unsigned int)fact_t0,
(unsigned int)fact_t1,
(unsigned int)fact_t2);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
fin = 0LL;
start = 0x100000001LL;
dword_4044B0 = 0;
dword_4044C0 = 1;
ct = 3;
func9(argc, argv, envp);
printf(
"Thread 0 - Resultado:%i\nThread 1 - Resultado:%i\nThread 2 - Resultado:%i\n",
(unsigned int)fact_t0,
(unsigned int)fact_t1,
(unsigned int)fact_t2);
return 0;
}

original_code:
int main(int argc, char *argv[]){
 int i;
 for(i=0;i<=2;i++){
  fin[i] = 0;
  start[i] = 1;
 }
 ct=3;
 adjust_frame();
 int salidat0 = fact_t0;
 int salidat1 = fact_t1;
 int salidat2 = fact_t2;
 printf("Thread 0 - Resultado:%i\nThread 1 - Resultado:%i\nThread 2 - Resultado:%i\n",salidat0,salidat1,salidat2);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int


yield

decompiled_code:
__int64 __fastcall yield(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
int v3; // er9
int v4; // edi
__int64 result; // rax
int v6; // ecx
int v7; // [rsp+34h] [rbp-4h]
void *retaddr; // [rsp+38h] [rbp+0h]

v2 = ct;
v3 = start;
t_frame[ct] = v7;
t_return[v2] = (int)retaddr;
if ( v3 )
{
LODWORD(start) = 0;
LODWORD(result) = t0((unsigned int)numero_t0);
v4 = 1;
LODWORD(fin) = 1;
fact_t0 = result;
LODWORD(result) = ct;
}
else if ( HIDWORD(start) )
{
HIDWORD(start) = 0;
LODWORD(result) = t1((unsigned int)numero_t1);
v4 = fin;
HIDWORD(fin) = 1;
fact_t1 = result;
LODWORD(result) = ct;
}
else if ( dword_4044C0 )
{
dword_4044C0 = 0;
LODWORD(result) = t2((unsigned int)numero_t2, a2, v2);
v4 = fin;
dword_4044B0 = 1;
fact_t2 = result;
LODWORD(result) = ct;
}
else
{
v4 = fin;
LODWORD(result) = v2;
}
v6 = 3;
do
{
result = (unsigned int)(((int)result + 1) % 3);
if ( !*((_DWORD *)&fin + (int)result) )
break;
--v6;
}
while ( v6 );
ct = result;
if ( v4 && HIDWORD(fin) )
{
result = (unsigned int)dword_4044B0;
if ( dword_4044B0 )
ct = 3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
int v3; // er9
int v4; // edi
__int64 result; // rax
int v6; // ecx
int v7; // [rsp+34h] [rbp-4h]
void *retaddr; // [rsp+38h] [rbp+0h]

v2 = ct;
v3 = start;
t_frame[ct] = v7;
t_return[v2] = (int)retaddr;
if ( v3 )
{
LODWORD(start) = 0;
LODWORD(result) = func10((unsigned int)numero_t0);
v4 = 1;
LODWORD(fin) = 1;
fact_t0 = result;
LODWORD(result) = ct;
}
else if ( HIDWORD(start) )
{
HIDWORD(start) = 0;
LODWORD(result) = func11((unsigned int)numero_t1);
v4 = fin;
HIDWORD(fin) = 1;
fact_t1 = result;
LODWORD(result) = ct;
}
else if ( dword_4044C0 )
{
dword_4044C0 = 0;
LODWORD(result) = func7((unsigned int)numero_t2, a2, v2);
v4 = fin;
dword_4044B0 = 1;
fact_t2 = result;
LODWORD(result) = ct;
}
else
{
v4 = fin;
LODWORD(result) = v2;
}
v6 = 3;
do
{
result = (unsigned int)(((int)result + 1) % 3);
if ( !*((_DWORD *)&fin + (int)result) )
break;
--v6;
}
while ( v6 );
ct = result;
if ( v4 && HIDWORD(fin) )
{
result = (unsigned int)dword_4044B0;
if ( dword_4044B0 )
ct = 3;
}
return result;
}


decompiled_code:
__int64 __fastcall t0(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 v3; // rdi
int v4; // ebp
__int64 result; // rax

if ( (_DWORD)a1 )
{
v2 = a1;
v3 = (unsigned int)(a1 - 1);
v4 = t0(v3);
adjust_frame(v3, a2);
result = (unsigned int)(v4 * v2);
}
else
{
ct = 0;
adjust_frame(a1, a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 v3; // rdi
int v4; // ebp
__int64 result; // rax

if ( (_DWORD)a1 )
{
v2 = a1;
v3 = (unsigned int)(a1 - 1);
v4 = func10(v3);
func9(v3, a2);
result = (unsigned int)(v4 * v2);
}
else
{
ct = 0;
func9(a1, a2);
result = 1LL;
}
return result;
}

original_code:
int t0(int numero_t0){
 int a[10];
 int temp = 1;
 if (numero_t0){
  temp = t0(numero_t0-1);
  adjust_frame();
  return numero_t0 * temp;
 }else{
  ct=0;
  adjust_frame();
  return 1;
 }
}


decompiled_code:
__int64 __fastcall t1(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 v3; // rdi
int v4; // ebp
__int64 result; // rax

if ( (_DWORD)a1 )
{
v2 = a1;
v3 = (unsigned int)(a1 - 1);
v4 = t1(v3);
adjust_frame(v3, a2);
result = (unsigned int)(v4 * v2);
}
else
{
ct = 1;
adjust_frame(a1, a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 v3; // rdi
int v4; // ebp
__int64 result; // rax

if ( (_DWORD)a1 )
{
v2 = a1;
v3 = (unsigned int)(a1 - 1);
v4 = func11(v3);
func9(v3, a2);
result = (unsigned int)(v4 * v2);
}
else
{
ct = 1;
func9(a1, a2);
result = 1LL;
}
return result;
}

original_code:
int t1(int numero_t1){
 int a[10];
 int temp = 1;
 if (numero_t1){
  temp = t1(numero_t1-1);
  adjust_frame();
  return numero_t1 * temp;
 }else{
  ct=1;
  adjust_frame();
  return 1;
 }
}


anotherfunction

decompiled_code:
int __fastcall anotherfunction(char *a1)
{
char *command; // [rsp+8h] [rbp-10h] BYREF

command = a1;
printf("ARGUMENTO: %p\n", &command);
printf("ARGUMENTO: %s\n", command);
return system(command);
}

canonical_decompiled_code:
int __fastcall func7(char *a1)
{
char *command; // [rsp+8h] [rbp-10h] BYREF

command = a1;
printf("ARGUMENTO: %p\n", &command);
printf("ARGUMENTO: %s\n", command);
return system(command);
}

original_code:
int anotherfunction(char * argument){
 printf("ARGUMENTO: %p\n",&argument);
 printf("ARGUMENTO: %s\n",argument);
 system(argument);
}

canonical_original_code:
int anotherfunction(char *argument){
 printf("ARGUMENTO: %p\n",&argument);
 printf("ARGUMENTO: %s\n",argument);
 system(argument);
}

variable_types:
argument
char *


decompiled_code:
void __fastcall funcion1(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10, int a11, char a12)
{
int v12; // eax
char *v13; // r12
int v14; // er13
char *v15; // rbx
char *v16; // rbx
unsigned int i; // ebp
__int64 v18; // rcx
__int64 v19; // rdx
char *v20; // rsi
unsigned int j; // ebx
char v22; // r8
__int64 v23; // rcx
unsigned __int8 v24; // al
__int64 v25; // r9
char *v26; // rsi
__int64 v27; // rdx
__int64 v28; // [rsp-10h] [rbp-48h]
char buf[52]; // [rsp+4h] [rbp-34h] BYREF

puts("Introduzca la entrada:");
__isoc99_scanf();
v12 = open("stack.bin", 577, 511LL);
if ( v12 == -1 )
{
perror("No se pudo abrir el fichero.\n");
}
else
{
v13 = buf;
v14 = v12;
v15 = buf;
do
{
if ( write(v14, v15, 1uLL) == -1 )
{
perror("No se pudo escribir en fichero.\n");
return;
}
++v15;
}
while ( &a12 != v15 );
v16 = buf;
for ( i = 0; i != 100; ++i )
{
v18 = (unsigned int)*v16;
v19 = i;
v20 = v16++;
printf("%p, %i - %c\n", v20, v19, v18);
}
for ( j = 0; j != 100; j += 4 )
{
v22 = v13[2];
v23 = (unsigned __int8)v13[3];
v24 = *v13;
v28 = (unsigned int)*v13;
v25 = (unsigned __int8)v13[1];
v26 = v13;
v13 += 4;
v27 = j;
printf(
"%p, %i - %02X%02X%02X%02X - %c%c%c%c\n",
v26,
v27,
v23,
(unsigned __int8)v22,
v25,
v24,
(unsigned int)(char)v23,
(unsigned int)v22,
(unsigned int)(char)v25,
v28);
}
close(v14);
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10, int a11, char a12)
{
int v12; // eax
char *v13; // r12
int v14; // er13
char *v15; // rbx
char *v16; // rbx
unsigned int i; // ebp
__int64 v18; // rcx
__int64 v19; // rdx
char *v20; // rsi
unsigned int j; // ebx
char v22; // r8
__int64 v23; // rcx
unsigned __int8 v24; // al
__int64 v25; // r9
char *v26; // rsi
__int64 v27; // rdx
__int64 v28; // [rsp-10h] [rbp-48h]
char buf[52]; // [rsp+4h] [rbp-34h] BYREF

puts("Introduzca la entrada:");
__isoc99_scanf();
v12 = open("stack.bin", 577, 511LL);
if ( v12 == -1 )
{
perror("No se pudo abrir el fichero.\n");
}
else
{
v13 = buf;
v14 = v12;
v15 = buf;
do
{
if ( write(v14, v15, 1uLL) == -1 )
{
perror("No se pudo escribir en fichero.\n");
return;
}
++v15;
}
while ( &a12 != v15 );
v16 = buf;
for ( i = 0; i != 100; ++i )
{
v18 = (unsigned int)*v16;
v19 = i;
v20 = v16++;
printf("%p, %i - %c\n", v20, v19, v18);
}
for ( j = 0; j != 100; j += 4 )
{
v22 = v13[2];
v23 = (unsigned __int8)v13[3];
v24 = *v13;
v28 = (unsigned int)*v13;
v25 = (unsigned __int8)v13[1];
v26 = v13;
v13 += 4;
v27 = j;
printf(
"%p, %i - %02X%02X%02X%02X - %c%c%c%c\n",
v26,
v27,
v23,
(unsigned __int8)v22,
v25,
v24,
(unsigned int)(char)v23,
(unsigned int)v22,
(unsigned int)(char)v25,
v28);
}
close(v14);
}
}

original_code:
int funcion1(int p1,int p2){
  int i = 12,file = 1;
  char a[12];
  printf("Introduzca la entrada:\n");
  scanf("%s",a);
  if((file = open ("stack.bin", 01 | 0100 | 01000, 0777)) == -1){
   perror("No se pudo abrir el fichero.\n");
   return;
  }
  for(i=0;i<100;i++){
     if(write(file,&a[i],sizeof(char)) == -1){
    perror("No se pudo escribir en fichero.\n");
    return;
   }
  }
  for (i=0;i<100;i++){
   printf("%p, %i - %c\n",(a+i),i,a[i]);
  }
  for (i=0;i<100;i+=4){
   printf("%p, %i - %02X%02X%02X%02X - %c%c%c%c\n",(a+i),i,(unsigned char) a[i+3],(unsigned char) a[i+2], (unsigned char) a[i+1], (unsigned char) a[i], a[i+3],a[i+2],a[i+1],a[i]);
  }
  close(file);
}

canonical_original_code:
int funcion1(int p1, int p2){
  int i = 12; int file = 1;
  char a[12];
  printf("Introduzca la entrada:\n");
  scanf("%s",a);
  if((file = open ("stack.bin", 01 | 0100 | 01000, 0777)) == -1){
   perror("No se pudo abrir el fichero.\n");
   return;
  }
  for(i=0;i<100;i++){
     if(write(file,&a[i],sizeof(char)) == -1){
    perror("No se pudo escribir en fichero.\n");
    return;
   }
  }
  for (i=0;i<100;i++){
   printf("%p, %i - %c\n",(a+i),i,a[i]);
  }
  for (i=0;i<100;i+=4){
   printf("%p, %i - %02X%02X%02X%02X - %c%c%c%c\n",(a+i),i,(unsigned char) a[i+3],(unsigned char) a[i+2], (unsigned char) a[i+1], (unsigned char) a[i], a[i+3],a[i+2],a[i+1],a[i]);
  }
  close(file);
}

variable_types: file int

variable_types:
a
char [12]


slow

decompiled_code:
void slow()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

variable_types: a long


main

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

f1_first = open("entrada.txt", 577, 448LL);
f1_r = open("entrada.txt", 0, 448LL);
v3 = open("entrada.txt", 1025, 448LL);
f2 = 1;
f1_w = v3;
*(__m128i *)mensaje = _mm_load_si128((const __m128i *)&xmmword_402060);
strcpy((char *)&qword_4040D0, "\\n'-----\n");
xmmword_4040B0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_402070);
numbytes = 6;
xmmword_4040C0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_402080);
v4 = strlen(mensaje);
write(1, mensaje, v4);
if ( write(f1_first, "VALOR\n", numbytes) < 0 )
perror("Error escritura:");
close(f1_first);
t0();
puts("\nFIN");
close(f1_r);
close(f1_w);
close(f2);
return 0;
}

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

f1_first = open("entrada.txt", 577, 448LL);
f1_r = open("entrada.txt", 0, 448LL);
v3 = open("entrada.txt", 1025, 448LL);
f2 = 1;
f1_w = v3;
*(__m128i *)mensaje = _mm_load_si128((const __m128i *)&xmmword_402060);
strcpy((char *)&qword_4040D0, "\\n'-----\n");
xmmword_4040B0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_402070);
numbytes = 6;
xmmword_4040C0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_402080);
v4 = strlen(mensaje);
write(1, mensaje, v4);
if ( write(f1_first, "VALOR\n", numbytes) < 0 )
perror("Error escritura:");
close(f1_first);
func10();
puts("\nFIN");
close(f1_r);
close(f1_w);
close(f2);
return 0;
}

original_code:
int main(){
 f1_first = open("entrada.txt", 01|0100|01000, 0700);
 f1_r = open("entrada.txt", 00,0700);
 f1_w = open("entrada.txt", 01|02000, 0700);
 f2 = 1;
 numbytes = strlen("VALOR\n");
 sprintf(mensaje,"\n-----Creación del fichero con el texto: 'VALOR\\n'-----\n");
 write(1,mensaje,strlen(mensaje));
 if(write(f1_first,"VALOR\n",numbytes) < 0)
     perror("Error escritura:");
    close(f1_first);
    t0();
  printf("\nFIN\n");
 close(f1_r);
 close(f1_w);
 close(f2);
}


decompiled_code:
__int64 t1()
{
__int64 result; // rax
size_t v1; // rax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 1; i <= 5; result = (unsigned int)i )
{
ct = 0;
sprintf(mensaje, format, (unsigned int)i);
v1 = strlen(mensaje);
write(1, mensaje, v1);
if ( write(f1_w, &buffer, numbytes) < 0 )
perror("Error escritura:");
yield();
++i;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
size_t v1; // rax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 1; i <= 5; result = (unsigned int)i )
{
ct = 0;
sprintf(mensaje, format, (unsigned int)i);
v1 = strlen(mensaje);
write(1, mensaje, v1);
if ( write(f1_w, &buffer, numbytes) < 0 )
perror("Error escritura:");
func9();
++i;
}
return result;
}

original_code:
void t1(){
 volatile char a[10];
 volatile int i;
 for(i=1;i<=5;i++){
  ct = 0;
  sprintf(mensaje,"WRITE Iteración:%i-t0\n",i);
  write(1,mensaje,strlen(mensaje));
  if(write(f1_w,&buffer,numbytes) < 0)
      perror("Error escritura:");
  yield();
 }
}

canonical_original_code:
void t1(){
 char a[10];
 int i;
 for(i=1;i<=5;i++){
  ct = 0;
  sprintf(mensaje,"WRITE Iteración:%i-t0\n",i);
  write(1,mensaje,strlen(mensaje));
  if(write(f1_w,&buffer,numbytes) < 0)
      perror("Error escritura:");
  yield();
 }
}

variable_types:
a
char [10]


yield

decompiled_code:
_BOOL8 yield()
{
__int64 v0; // rdx
int v1; // eax
int v2; // eax
_BOOL8 result; // rax
int v4; // [rsp+30h] [rbp-8h]
int v5; // [rsp+34h] [rbp-4h]

v0 = ct;
v1 = startt1;
dword_4048D8[ct] = v4;
tr[v0] = v5;
if ( v1 )
{
startt1 = 0;
t1();
v2 = ct;
}
else
{
v2 = v0;
}
result = v2 == 0;
ct = result;
return result;
}

canonical_decompiled_code:
_BOOL8 func9()
{
__int64 v0; // rdx
int v1; // eax
int v2; // eax
_BOOL8 result; // rax
int v4; // [rsp+30h] [rbp-8h]
int v5; // [rsp+34h] [rbp-4h]

v0 = ct;
v1 = startt1;
dword_4048D8[ct] = v4;
tr[v0] = v5;
if ( v1 )
{
startt1 = 0;
func8();
v2 = ct;
}
else
{
v2 = v0;
}
result = v2 == 0;
ct = result;
return result;
}


decompiled_code:
__int64 t0()
{
size_t v0; // rax
__int64 result; // rax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 1; i <= 5; result = (unsigned int)i )
{
ct = 1;
sprintf(mensaje, aReadIteraci, (unsigned int)i);
v0 = strlen(mensaje);
write(1, mensaje, v0);
numbytes = read(f1_r, &buffer, numbytes);
write(f2, &buffer, numbytes);
yield();
++i;
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
size_t v0; // rax
__int64 result; // rax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 1; i <= 5; result = (unsigned int)i )
{
ct = 1;
sprintf(mensaje, aReadIteraci, (unsigned int)i);
v0 = strlen(mensaje);
write(1, mensaje, v0);
numbytes = read(f1_r, &buffer, numbytes);
write(f2, &buffer, numbytes);
func9();
++i;
}
return result;
}

original_code:
void t0(){
 volatile char a[10];
 volatile int i;
 for(i=1;i<=5;i++){
  ct = 1;
  sprintf(mensaje,"READ Iteración:%i-t1\n",i);
  write(1,mensaje,strlen(mensaje));
  numbytes = read(f1_r, &buffer, numbytes);
  write(f2, &buffer, numbytes);
  yield();
 }
}

canonical_original_code:
void t0(){
 char a[10];
 int i;
 for(i=1;i<=5;i++){
  ct = 1;
  sprintf(mensaje,"READ Iteración:%i-t1\n",i);
  write(1,mensaje,strlen(mensaje));
  numbytes = read(f1_r, &buffer, numbytes);
  write(f2, &buffer, numbytes);
  yield();
 }
}


main

repo: catharsis/in-trigger

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rbp
unsigned int v5; // er12
int *v7; // rax
char *v8; // r8
int *v9; // rbx
int *v10; // rax
char *v11; // r8
int *v12; // rbx
__int64 v13; // rcx
int *v14; // rax
char *v15; // r8
int *v16; // rbx
int *v17; // rax
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
if ( (unsigned int)(argc - 2) > 1 )
{
v7 = __errno_location();
v8 = "None";
v9 = v7;
if ( *v7 )
v8 = strerror(*v7);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Wrong number of arguments\n", "trigger.c", 163LL, v8);
*v9 = 0;
goto LABEL_11;
}
v3 = argv[1];
if ( !strcmp(v3, "-h") )
{
print_usage();
exit(0);
}
command = strdup(v3);
if ( !command )
{
v17 = __errno_location();
v11 = "None";
v12 = v17;
if ( *v17 )
v11 = strerror(*v17);
v13 = 168LL;
goto LABEL_22;
}
if ( argc == 3 )
{
ptr = strdup(argv[2]);
if ( !ptr )
{
v10 = __errno_location();
v11 = "None";
v12 = v10;
if ( *v10 )
v11 = strerror(*v10);
v13 = 170LL;
LABEL_22:
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", v13, v11);
*v12 = 0;
goto LABEL_11;
}
}
v4 = (char *)determine_watch_path(&ptr);
if ( v4 )
{
v5 = open_inotify_fd();
fprintf(stderr, "[INFO] (%s:%d) Inotify instance initialized ...\n", "trigger.c", 175LL);
add_watch(v5, v4);
fprintf(stderr, "[INFO] (%s:%d) Watch pattern '%s' added for '%s' ...\n", "trigger.c", 177LL, (const char *)ptr, v4);
process_events(v5);
fprintf(stderr, "[INFO] (%s:%d) Stopped watching '%s' ...\n", "trigger.c", 179LL, v4);
free(command);
free(v4);
free(ptr);
return 0;
}
v14 = __errno_location();
v15 = "None";
v16 = v14;
if ( *v14 )
v15 = strerror(*v14);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Failed to determine directory to watch\n", "trigger.c", 173LL, v15);
*v16 = 0;
LABEL_11:
print_usage();
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rbp
unsigned int v5; // er12
int *v7; // rax
char *v8; // r8
int *v9; // rbx
int *v10; // rax
char *v11; // r8
int *v12; // rbx
__int64 v13; // rcx
int *v14; // rax
char *v15; // r8
int *v16; // rbx
int *v17; // rax
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
if ( (unsigned int)(argc - 2) > 1 )
{
v7 = __errno_location();
v8 = "None";
v9 = v7;
if ( *v7 )
v8 = strerror(*v7);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Wrong number of arguments\n", "trigger.c", 163LL, v8);
*v9 = 0;
goto LABEL_11;
}
v3 = argv[1];
if ( !strcmp(v3, "-h") )
{
func14();
exit(0);
}
command = strdup(v3);
if ( !command )
{
v17 = __errno_location();
v11 = "None";
v12 = v17;
if ( *v17 )
v11 = strerror(*v17);
v13 = 168LL;
goto LABEL_22;
}
if ( argc == 3 )
{
ptr = strdup(argv[2]);
if ( !ptr )
{
v10 = __errno_location();
v11 = "None";
v12 = v10;
if ( *v10 )
v11 = strerror(*v10);
v13 = 170LL;
LABEL_22:
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", v13, v11);
*v12 = 0;
goto LABEL_11;
}
}
v4 = (char *)func8(&ptr);
if ( v4 )
{
v5 = func9();
fprintf(stderr, "[INFO] (%s:%d) Inotify instance initialized ...\n", "trigger.c", 175LL);
func10(v5, v4);
fprintf(stderr, "[INFO] (%s:%d) Watch pattern '%s' added for '%s' ...\n", "trigger.c", 177LL, (const char *)ptr, v4);
func13(v5);
fprintf(stderr, "[INFO] (%s:%d) Stopped watching '%s' ...\n", "trigger.c", 179LL, v4);
free(command);
free(v4);
free(ptr);
return 0;
}
v14 = __errno_location();
v15 = "None";
v16 = v14;
if ( *v14 )
v15 = strerror(*v14);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Failed to determine directory to watch\n", "trigger.c", 173LL, v15);
*v16 = 0;
LABEL_11:
func14();
return 1;
}

original_code:
int main (int argc, char **argv)
{
 int inotify_fd, wd;
 char *path = ((void *)0);
 char *pattern = ((void *)0);
 if(!((argc > 1 && argc < 4))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Wrong number of arguments" "\n", "trigger.c", 163, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 if (strcmp(argv[1], "-h") == 0) {
  print_usage();
  exit(0);
 }
 if(!((command = strdup(argv[1])))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 168, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 if (argc == 3)
  if(!((pattern = strdup(argv[2])))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 170, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 path = determine_watch_path(&pattern);
 if(!(path)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to determine directory to watch" "\n", "trigger.c", 173, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 inotify_fd = open_inotify_fd();
 fprintf(stderr, "[INFO] (%s:%d) " "Inotify instance initialized ..." "\n", "trigger.c", 175);
 add_watch(inotify_fd, path);
 fprintf(stderr, "[INFO] (%s:%d) " "Watch pattern '%s' added for '%s' ..." "\n", "trigger.c", 177, pattern, path);
 process_events(inotify_fd, (const char *) pattern);
 fprintf(stderr, "[INFO] (%s:%d) " "Stopped watching '%s' ..." "\n", "trigger.c", 179, path);
 free(command);
 free(path);
 free(pattern);
 return 0;
error:
 print_usage();
 return 1;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int inotify_fd; int wd;
 char *path = ((void *)0);
 char *pattern = ((void *)0);
 if(!((argc > 1 && argc < 4))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Wrong number of arguments" "\n", "trigger.c", 163, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 if (strcmp(argv[1], "-h") == 0) {
  print_usage();
  exit(0);
 }
 if(!((command = strdup(argv[1])))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 168, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 if (argc == 3)
  if(!((pattern = strdup(argv[2])))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 170, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 path = determine_watch_path(&pattern);
 if(!(path)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to determine directory to watch" "\n", "trigger.c", 173, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 inotify_fd = open_inotify_fd();
 fprintf(stderr, "[INFO] (%s:%d) " "Inotify instance initialized ..." "\n", "trigger.c", 175);
 add_watch(inotify_fd, path);
 fprintf(stderr, "[INFO] (%s:%d) " "Watch pattern '%s' added for '%s' ..." "\n", "trigger.c", 177, pattern, path);
 process_events(inotify_fd, (char *) pattern);
 fprintf(stderr, "[INFO] (%s:%d) " "Stopped watching '%s' ..." "\n", "trigger.c", 179, path);
 free(command);
 free(path);
 free(pattern);
 return 0;
error:
 print_usage();
 return 1;
}

variable_types:
argv
char **

variable_types: wd int

variable_types:
path
char *

variable_types:
pattern
char *


decompiled_code:
void sigint_handler()
{
done = 1;
}

canonical_decompiled_code:
void func7()
{
done = 1;
}

original_code:
static void sigint_handler(int signum)
{
 done = 1;
}

variable_types: signum int


decompiled_code:
char *__fastcall determine_watch_path(const char **a1)
{
const char *v1; // r12
char *v2; // r13
char *v3; // rbp
char *v4; // r14
char *v5; // r12
char *v6; // r12
char *v8; // rax
int *v9; // rax
char *v10; // r8
int *v11; // rbx
__int64 v12; // rcx
int *v13; // rbx
char *v14; // rax
const char *v15; // r8
const char *v16; // r9
char *v17; // rax
int *v18; // rax
int *v19; // rax
char *v20; // r8
int *v21; // rbx
int *v22; // rax
char *v23; // [rsp+8h] [rbp-30h]

v1 = *a1;
if ( !*a1 )
{
LABEL_6:
v6 = getcwd(0LL, 0LL);
if ( !v6 )
{
v13 = __errno_location();
v14 = strerror(*v13);
v15 = "None";
v16 = v14;
if ( *v13 )
{
v23 = v14;
v17 = strerror(*v13);
v16 = v23;
v15 = v17;
}
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Failed to get current working directory: %s\n",
"trigger.c",
143LL,
v15,
v16);
}
return v6;
}
v2 = strdup(*a1);
if ( !v2 )
{
v9 = __errno_location();
v10 = "None";
v11 = v9;
if ( *v9 )
v10 = strerror(*v9);
v12 = 129LL;
goto LABEL_14;
}
v3 = strdup(v1);
if ( !v3 )
{
v18 = __errno_location();
v10 = "None";
v11 = v18;
if ( *v18 )
v10 = strerror(*v18);
v12 = 130LL;
goto LABEL_14;
}
v4 = __xpg_basename(v2);
v5 = dirname(v3);
if ( !strcmp(v5, ".") )
{
free(v2);
free(v3);
goto LABEL_6;
}
v6 = strdup(v5);
if ( v6 )
{
free((void *)*a1);
v8 = strdup(v4);
*a1 = v8;
if ( v8 )
{
free(v2);
free(v3);
return v6;
}
v22 = __errno_location();
v10 = "None";
v11 = v22;
if ( *v22 )
v10 = strerror(*v22);
v12 = 136LL;
LABEL_14:
v6 = 0LL;
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", v12, v10);
*v11 = 0;
return v6;
}
v19 = __errno_location();
v20 = "None";
v21 = v19;
if ( *v19 )
v20 = strerror(*v19);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", 134LL, v20);
*v21 = 0;
return v6;
}

canonical_decompiled_code:
char *__fastcall func8(const char **a1)
{
const char *v1; // r12
char *v2; // r13
char *v3; // rbp
char *v4; // r14
char *v5; // r12
char *v6; // r12
char *v8; // rax
int *v9; // rax
char *v10; // r8
int *v11; // rbx
__int64 v12; // rcx
int *v13; // rbx
char *v14; // rax
const char *v15; // r8
const char *v16; // r9
char *v17; // rax
int *v18; // rax
int *v19; // rax
char *v20; // r8
int *v21; // rbx
int *v22; // rax
char *v23; // [rsp+8h] [rbp-30h]

v1 = *a1;
if ( !*a1 )
{
LABEL_6:
v6 = getcwd(0LL, 0LL);
if ( !v6 )
{
v13 = __errno_location();
v14 = strerror(*v13);
v15 = "None";
v16 = v14;
if ( *v13 )
{
v23 = v14;
v17 = strerror(*v13);
v16 = v23;
v15 = v17;
}
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Failed to get current working directory: %s\n",
"trigger.c",
143LL,
v15,
v16);
}
return v6;
}
v2 = strdup(*a1);
if ( !v2 )
{
v9 = __errno_location();
v10 = "None";
v11 = v9;
if ( *v9 )
v10 = strerror(*v9);
v12 = 129LL;
goto LABEL_14;
}
v3 = strdup(v1);
if ( !v3 )
{
v18 = __errno_location();
v10 = "None";
v11 = v18;
if ( *v18 )
v10 = strerror(*v18);
v12 = 130LL;
goto LABEL_14;
}
v4 = __xpg_basename(v2);
v5 = dirname(v3);
if ( !strcmp(v5, ".") )
{
free(v2);
free(v3);
goto LABEL_6;
}
v6 = strdup(v5);
if ( v6 )
{
free((void *)*a1);
v8 = strdup(v4);
*a1 = v8;
if ( v8 )
{
free(v2);
free(v3);
return v6;
}
v22 = __errno_location();
v10 = "None";
v11 = v22;
if ( *v22 )
v10 = strerror(*v22);
v12 = 136LL;
LABEL_14:
v6 = 0LL;
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", v12, v10);
*v11 = 0;
return v6;
}
v19 = __errno_location();
v20 = "None";
v21 = v19;
if ( *v19 )
v20 = strerror(*v19);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "trigger.c", 134LL, v20);
*v21 = 0;
return v6;
}

original_code:
static char * determine_watch_path(char **pattern)
{
 char *p = ((void *)0);
 char *dirc = ((void *)0);
 char *basec= ((void *)0);
 char *dname= ((void *)0);
 char *bname = ((void *)0);
 if (pattern != ((void *)0) && *pattern != ((void *)0)) {
  if(!((dirc= strdup(*pattern)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 129, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  if(!((basec= strdup(*pattern)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 130, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  bname = __xpg_basename(dirc);
  dname = dirname(basec);
  if (0 != strcmp(dname, ".")) {
   if(!((p = strdup(dname)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 134, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
   free(*pattern);
   if(!((*pattern = strdup(bname)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 136, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  }
  free(dirc);
  free(basec);
 }
 if (p == ((void *)0)) {
  if ((p = getcwd(p, 0)) == ((void *)0) ) {
   fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to get current working directory: %s" "\n", "trigger.c", 143, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), strerror((*__errno_location ())));
   return ((void *)0);
  }
 }
 return p;
error:
 return ((void *)0);
}

canonical_original_code:
static char *determine_watch_path(char **pattern)
{
 char *p = ((void *)0);
 char *dirc = ((void *)0);
 char *basec = ((void *)0);
 char *dname = ((void *)0);
 char *bname = ((void *)0);
 if (pattern != ((void *)0) && *pattern != ((void *)0)) {
  if(!((dirc= strdup(*pattern)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 129, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  if(!((basec= strdup(*pattern)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 130, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  bname = __xpg_basename(dirc);
  dname = dirname(basec);
  if (0 != strcmp(dname, ".")) {
   if(!((p = strdup(dname)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 134, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
   free(*pattern);
   if(!((*pattern = strdup(bname)))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "trigger.c", 136, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  }
  free(dirc);
  free(basec);
 }
 if (p == ((void *)0)) {
  if ((p = getcwd(p, 0)) == ((void *)0) ) {
   fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to get current working directory: %s" "\n", "trigger.c", 143, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), strerror((*__errno_location ())));
   return ((void *)0);
  }
 }
 return p;
error:
 return ((void *)0);
}

variable_types:
pattern
char **

variable_types:
p
char *

variable_types:
dirc
char *

variable_types:
basec
char *

variable_types:
dname
char *

variable_types:
bname
char *

return_type: char *


decompiled_code:
int open_inotify_fd()
{
int result; // eax
int *v1; // rax
char *v2; // r8
int *v3; // rbx

result = inotify_init();
if ( result < 0 )
{
v1 = __errno_location();
v2 = "None";
v3 = v1;
if ( *v1 )
v2 = strerror(*v1);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unable to initialize inotify instance!\n", "trigger.c", 38LL, v2);
*v3 = 0;
exit(1);
}
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax
int *v1; // rax
char *v2; // r8
int *v3; // rbx

result = inotify_init();
if ( result < 0 )
{
v1 = __errno_location();
v2 = "None";
v3 = v1;
if ( *v1 )
v2 = strerror(*v1);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unable to initialize inotify instance!\n", "trigger.c", 38LL, v2);
*v3 = 0;
exit(1);
}
return result;
}

original_code:
static int open_inotify_fd()
{
 int fd;
 fd = inotify_init();
 if(!(fd >= 0)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Unable to initialize inotify instance!" "\n", "trigger.c", 38, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
 return fd;
error:
 exit(1);
}

variable_types: fd int


decompiled_code:
int __fastcall add_watch(int a1, const char *a2)
{
int result; // eax
int *v3; // rax
char *v4; // r8
int *v5; // rbp

result = inotify_add_watch(a1, a2, 2u);
if ( result < 0 )
{
v3 = __errno_location();
v4 = "None";
v5 = v3;
if ( *v3 )
v4 = strerror(*v3);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unable to add watch for '%s'\n", "trigger.c", 28LL, v4, a2);
*v5 = 0;
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(int a1, const char *a2)
{
int result; // eax
int *v3; // rax
char *v4; // r8
int *v5; // rbp

result = inotify_add_watch(a1, a2, 2u);
if ( result < 0 )
{
v3 = __errno_location();
v4 = "None";
v5 = v3;
if ( *v3 )
v4 = strerror(*v3);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unable to add watch for '%s'\n", "trigger.c", 28LL, v4, a2);
*v5 = 0;
exit(1);
}
return result;
}

original_code:
static void add_watch(int fd, const char *pathname)
{
 int wd;
 wd = inotify_add_watch(fd, pathname, watch_mask);
 if(!(wd >= 0)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Unable to add watch for '%s'" "\n", "trigger.c", 28, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), pathname); (*__errno_location ())=0; goto error; };
 return;
error:
 exit(1);
}

canonical_original_code:
static void add_watch(int fd, char *pathname)
{
 int wd;
 wd = inotify_add_watch(fd, pathname, watch_mask);
 if(!(wd >= 0)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Unable to add watch for '%s'" "\n", "trigger.c", 28, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), pathname); (*__errno_location ())=0; goto error; };
 return;
error:
 exit(1);
}

variable_types:
pathname
char *


decompiled_code:
__int64 __fastcall should_trigger(__int64 a1, const char *a2)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) )
{
if ( a2 && fnmatch(a2, (const char *)(a1 + 16), 1) )
{
fprintf(
stderr,
"DEBUG %s:%d: Ignoring %s in accordance with pattern\n",
"trigger.c",
52LL,
(const char *)(a1 + 16));
result = 0LL;
}
else
{
result = 1LL;
}
}
else
{
fprintf(stderr, "DEBUG %s:%d: Ignoring event with no name length\n", "trigger.c", 56LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, const char *a2)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) )
{
if ( a2 && fnmatch(a2, (const char *)(a1 + 16), 1) )
{
fprintf(
stderr,
"DEBUG %s:%d: Ignoring %s in accordance with pattern\n",
"trigger.c",
52LL,
(const char *)(a1 + 16));
result = 0LL;
}
else
{
result = 1LL;
}
}
else
{
fprintf(stderr, "DEBUG %s:%d: Ignoring event with no name length\n", "trigger.c", 56LL);
result = 0LL;
}
return result;
}

original_code:
static int should_trigger(struct inotify_event *event, const char *pattern)
{
 if ( event->len ) {
  if ( !pattern || !fnmatch(pattern, event->name, (1 << 0))) {
   return 1;
  }
  else {
   fprintf(stderr, "DEBUG %s:%d: " "Ignoring %s in accordance with pattern" "\n", "trigger.c", 52, event->name);
  }
 }
 else {
  fprintf(stderr, "DEBUG %s:%d: " "Ignoring event with no name length" "\n", "trigger.c", 56);
 }
 return 0;
}

canonical_original_code:
static int should_trigger(struct inotify_event *event, char *pattern)
{
 if ( event->len ) {
  if ( !pattern || !fnmatch(pattern, event->name, (1 << 0))) {
   return 1;
  }
  else {
   fprintf(stderr, "DEBUG %s:%d: " "Ignoring %s in accordance with pattern" "\n", "trigger.c", 52, event->name);
  }
 }
 else {
  fprintf(stderr, "DEBUG %s:%d: " "Ignoring event with no name length" "\n", "trigger.c", 56);
 }
 return 0;
}

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


decompiled_code:
const char *__fastcall mask2str(unsigned int a1)
{
int *v2; // rax
char *v3; // r8

if ( (a1 & 1) != 0 )
return "IN_ACCESS";
if ( (a1 & 4) != 0 )
return "IN_ATTRIB";
if ( (a1 & 8) != 0 )
return "IN_CLOSE_WRITE";
if ( (a1 & 0x10) != 0 )
return "IN_CLOSE_NOWRITE";
if ( (a1 & 0x100) != 0 )
return "IN_CREATE";
if ( (a1 & 0x200) != 0 )
return "IN_DELETE";
if ( (a1 & 0x400) != 0 )
return "IN_DELETE_SELF";
if ( (a1 & 2) != 0 )
return "IN_MODIFY";
if ( (a1 & 0x800) != 0 )
return "IN_MOVE_SELF";
if ( (a1 & 0x40) != 0 )
return "IN_MOVED_FROM";
if ( (a1 & 0x80u) != 0 )
return "IN_MOVED_TO";
if ( (a1 & 0x20) == 0 )
{
v2 = __errno_location();
v3 = "None";
if ( *v2 )
v3 = strerror(*v2);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unknown event in event_mask %d\n", "trigger.c", 78LL, v3, a1);
__assert_fail("!\"Unknown event in event_mask\"", "trigger.c", 0x4Fu, "mask2str");
}
return "IN_OPEN";
}

canonical_decompiled_code:
const char *__fastcall func12(unsigned int a1)
{
int *v2; // rax
char *v3; // r8

if ( (a1 & 1) != 0 )
return "IN_ACCESS";
if ( (a1 & 4) != 0 )
return "IN_ATTRIB";
if ( (a1 & 8) != 0 )
return "IN_CLOSE_WRITE";
if ( (a1 & 0x10) != 0 )
return "IN_CLOSE_NOWRITE";
if ( (a1 & 0x100) != 0 )
return "IN_CREATE";
if ( (a1 & 0x200) != 0 )
return "IN_DELETE";
if ( (a1 & 0x400) != 0 )
return "IN_DELETE_SELF";
if ( (a1 & 2) != 0 )
return "IN_MODIFY";
if ( (a1 & 0x800) != 0 )
return "IN_MOVE_SELF";
if ( (a1 & 0x40) != 0 )
return "IN_MOVED_FROM";
if ( (a1 & 0x80u) != 0 )
return "IN_MOVED_TO";
if ( (a1 & 0x20) == 0 )
{
v2 = __errno_location();
v3 = "None";
if ( *v2 )
v3 = strerror(*v2);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Unknown event in event_mask %d\n", "trigger.c", 78LL, v3, a1);
__assert_fail("!\"Unknown event in event_mask\"", "trigger.c", 0x4Fu, "mask2str");
}
return "IN_OPEN";
}

original_code:
static const char * mask2str(int event_mask)
{
 if( 0x00000001 & event_mask ) return "IN_ACCESS";
 else if( 0x00000004 & event_mask ) return "IN_ATTRIB";
 else if( 0x00000008 & event_mask ) return "IN_CLOSE_WRITE";
 else if( 0x00000010 & event_mask ) return "IN_CLOSE_NOWRITE";
 else if( 0x00000100 & event_mask ) return "IN_CREATE";
 else if( 0x00000200 & event_mask ) return "IN_DELETE";
 else if( 0x00000400 & event_mask ) return "IN_DELETE_SELF";
 else if( 0x00000002 & event_mask) return "IN_MODIFY";
 else if( 0x00000800 & event_mask ) return "IN_MOVE_SELF";
 else if( 0x00000040 & event_mask ) return "IN_MOVED_FROM";
 else if( 0x00000080 & event_mask ) return "IN_MOVED_TO";
 else if( 0x00000020 & event_mask ) return "IN_OPEN";
 else {
  fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Unknown event in event_mask %d" "\n", "trigger.c", 78, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), event_mask);
  ((void) sizeof ((!"Unknown event in event_mask") ? 1 : 0), __extension__ ({ if (!"Unknown event in event_mask") ; else __assert_fail ("!\"Unknown event in event_mask\"", "trigger.c", 79, __extension__ __PRETTY_FUNCTION__); }));
 }
}

canonical_original_code:
static char *mask2str(int event_mask)
{
 if( 0x00000001 & event_mask ) return "IN_ACCESS";
 else if( 0x00000004 & event_mask ) return "IN_ATTRIB";
 else if( 0x00000008 & event_mask ) return "IN_CLOSE_WRITE";
 else if( 0x00000010 & event_mask ) return "IN_CLOSE_NOWRITE";
 else if( 0x00000100 & event_mask ) return "IN_CREATE";
 else if( 0x00000200 & event_mask ) return "IN_DELETE";
 else if( 0x00000400 & event_mask ) return "IN_DELETE_SELF";
 else if( 0x00000002 & event_mask) return "IN_MODIFY";
 else if( 0x00000800 & event_mask ) return "IN_MOVE_SELF";
 else if( 0x00000040 & event_mask ) return "IN_MOVED_FROM";
 else if( 0x00000080 & event_mask ) return "IN_MOVED_TO";
 else if( 0x00000020 & event_mask ) return "IN_OPEN";
 else {
  fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Unknown event in event_mask %d" "\n", "trigger.c", 78, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))), event_mask);
  ((void) sizeof ((!"Unknown event in event_mask") ? 1 : 0), __extension__ ({ if (!"Unknown event in event_mask") ; else __assert_fail ("!\"Unknown event in event_mask\"", "trigger.c", 79, __extension__ __PRETTY_FUNCTION__); }));
 }
}


decompiled_code:
int __fastcall process_events(int fd, const char *a2)
{
int i; // eax
int v3; // ebx
int *v5; // rax
char *v6; // r8
int *v7; // rbx
const char *v8; // rax
int v9; // eax
int *v10; // rax
char *v11; // r8
int *v12; // rbx
unsigned int v13[4]; // [rsp+0h] [rbp-140h] BYREF
char v14[304]; // [rsp+10h] [rbp-130h] BYREF

done = 0;
signal(2, (__sighandler_t)sigint_handler);
for ( i = 0; !i; i = done )
{
signal(2, 0LL);
v3 = read(fd, v13, 0x108uLL);
signal(2, (__sighandler_t)sigint_handler);
if ( v3 < 0 )
{
v5 = __errno_location();
v6 = "None";
v7 = v5;
if ( *v5 )
v6 = strerror(*v5);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Invalid read from inotify instance\n", "trigger.c", 94LL, v6);
*v7 = 0;
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
if ( (unsigned int)should_trigger((__int64)v13, a2) )
{
v8 = mask2str(v13[1]);
fprintf(stderr, "DEBUG %s:%d: Triggering on event %s\n", "trigger.c", 97LL, v8);
fprintf(stdout, "%sTrigger (%s)%s: Executing command '%s'\n", "\x1B[32m", v14, "\x1B[m", command);
v9 = system(command);
if ( !__OFSUB__((v9 & 0x7F) + 1, 1) && (v9 & 0x7F) != 0 && (v9 & 0x7E) == 2 )
{
fprintf(stdout, "%sTrigger (%s)%s: Interrupted by signal %d\n", "\x1B[32m", v14, "\x1B[m", v9 & 0x7F);
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
if ( v9 == -1 )
{
v10 = __errno_location();
v11 = "None";
v12 = v10;
if ( *v10 )
v11 = strerror(*v10);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Failed to execute command\n", "trigger.c", 105LL, v11);
*v12 = 0;
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
fprintf(stdout, "%sTrigger (%s)%s: done.\n", "\x1B[32m", v14, "\x1B[m");
}
}
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}

canonical_decompiled_code:
int __fastcall func13(int fd, const char *a2)
{
int i; // eax
int v3; // ebx
int *v5; // rax
char *v6; // r8
int *v7; // rbx
const char *v8; // rax
int v9; // eax
int *v10; // rax
char *v11; // r8
int *v12; // rbx
unsigned int v13[4]; // [rsp+0h] [rbp-140h] BYREF
char v14[304]; // [rsp+10h] [rbp-130h] BYREF

done = 0;
signal(2, (__sighandler_t)sigint_handler);
for ( i = 0; !i; i = done )
{
signal(2, 0LL);
v3 = read(fd, v13, 0x108uLL);
signal(2, (__sighandler_t)sigint_handler);
if ( v3 < 0 )
{
v5 = __errno_location();
v6 = "None";
v7 = v5;
if ( *v5 )
v6 = strerror(*v5);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Invalid read from inotify instance\n", "trigger.c", 94LL, v6);
*v7 = 0;
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
if ( (unsigned int)func11((__int64)v13, a2) )
{
v8 = func12(v13[1]);
fprintf(stderr, "DEBUG %s:%d: Triggering on event %s\n", "trigger.c", 97LL, v8);
fprintf(stdout, "%sTrigger (%s)%s: Executing command '%s'\n", "\x1B[32m", v14, "\x1B[m", command);
v9 = system(command);
if ( !__OFSUB__((v9 & 0x7F) + 1, 1) && (v9 & 0x7F) != 0 && (v9 & 0x7E) == 2 )
{
fprintf(stdout, "%sTrigger (%s)%s: Interrupted by signal %d\n", "\x1B[32m", v14, "\x1B[m", v9 & 0x7F);
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
if ( v9 == -1 )
{
v10 = __errno_location();
v11 = "None";
v12 = v10;
if ( *v10 )
v11 = strerror(*v10);
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) Failed to execute command\n", "trigger.c", 105LL, v11);
*v12 = 0;
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}
fprintf(stdout, "%sTrigger (%s)%s: done.\n", "\x1B[32m", v14, "\x1B[m");
}
}
return fprintf(stderr, "[INFO] (%s:%d) Terminating ...\n", "trigger.c", 111LL);
}

original_code:
static void process_events(int fd, const char * pattern)
{
 struct inotify_event *event;
 int ret, len, bufsize = sizeof(event) + 255 + 1;
 char buffer[bufsize];
 done = 0;
 signal(2, sigint_handler);
 while (!done) {
  signal(2, ((__sighandler_t) 0));
  len = read(fd, buffer, bufsize);
  signal(2, sigint_handler);
  if(!(len >= 0)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Invalid read from inotify instance" "\n", "trigger.c", 94, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  event = (struct inotify_event * ) buffer;
  if (should_trigger(event, pattern)) {
   fprintf(stderr, "DEBUG %s:%d: " "Triggering on event %s" "\n", "trigger.c", 97, mask2str(event->mask));
   fprintf(stdout, "%sTrigger (%s)%s: " "Executing command '%s'" "\n", "\033[32m", event->name, "\033[m", command);
   ret = system(command);
   if ((((signed char) (((ret) & 0x7f) + 1) >> 1) > 0) &&
     (((ret) & 0x7f) == 2 || ((ret) & 0x7f) == 3)) {
    fprintf(stdout, "%sTrigger (%s)%s: " "Interrupted by signal %d" "\n", "\033[32m", event->name, "\033[m", ((ret) & 0x7f));
    break;
   }
   if(!((-1 != ret))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to execute command" "\n", "trigger.c", 105, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
   fprintf(stdout, "%sTrigger (%s)%s: " "done." "\n", "\033[32m", event->name, "\033[m");
  }
 }
error:
 fprintf(stderr, "[INFO] (%s:%d) " "Terminating ..." "\n", "trigger.c", 111);
}

canonical_original_code:
static void process_events(int fd, char *pattern)
{
 struct inotify_event *event;
 int ret; int len; int bufsize = sizeof(event) + 255 + 1;
 char buffer[-1];
 done = 0;
 signal(2, sigint_handler);
 while (!done) {
  signal(2, ((void (*)(int)) 0));
  len = read(fd, buffer, bufsize);
  signal(2, sigint_handler);
  if(!(len >= 0)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Invalid read from inotify instance" "\n", "trigger.c", 94, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  event = (struct inotify_event * ) buffer;
  if (should_trigger(event, pattern)) {
   fprintf(stderr, "DEBUG %s:%d: " "Triggering on event %s" "\n", "trigger.c", 97, mask2str(event->mask));
   fprintf(stdout, "%sTrigger (%s)%s: " "Executing command '%s'" "\n", "\033[32m", event->name, "\033[m", command);
   ret = system(command);
   if ((((char) (((ret) & 0x7f) + 1) >> 1) > 0) &&
     (((ret) & 0x7f) == 2 || ((ret) & 0x7f) == 3)) {
    fprintf(stdout, "%sTrigger (%s)%s: " "Interrupted by signal %d" "\n", "\033[32m", event->name, "\033[m", ((ret) & 0x7f));
    break;
   }
   if(!((-1 != ret))) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Failed to execute command" "\n", "trigger.c", 105, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
   fprintf(stdout, "%sTrigger (%s)%s: " "done." "\n", "\033[32m", event->name, "\033[m");
  }
 }
error:
 fprintf(stderr, "[INFO] (%s:%d) " "Terminating ..." "\n", "trigger.c", 111);
}

variable_types: ret int

variable_types: len int

variable_types: bufsize int

variable_types:
buffer
char [-1]

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


decompiled_code:
int print_usage()
{
return puts("Usage: in-trigger command [pattern]");
}

canonical_decompiled_code:
int func14()
{
return puts("Usage: in-trigger command [pattern]");
}

original_code:
static void print_usage()
{
 printf("Usage: in-trigger command [pattern]\n");
}


repo: catherinemoresco/catsays

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printbubble((char *)argv[1]);
rendercat();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func9((char *)argv[1]);
func7();
return 0;
}

original_code:
int main(int argc, char **argv)
{
 printbubble(argv[1]);
 rendercat();
}

variable_types: argc int

variable_types:
argv
char **


printspaces

decompiled_code:
int __fastcall printspaces(int a1)
{
int i; // ebx
int result; // eax

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
result = putchar(32);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
int i; // ebx
int result; // eax

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
result = putchar(32);
}
return result;
}

original_code:
void printspaces(int n)
{
 int i;
 for (i=0; i < n; i++)
 {
  printf(" ");
 }
}

variable_types: n int

variable_types: i int


printbubble

decompiled_code:
int __fastcall printbubble(char *s)
{
int v1; // ebx
int v2; // er12
char *i; // rbp
int v4; // ebx
int v5; // ebx

v1 = 30;
putchar(32);
do
{
putchar(95);
--v1;
}
while ( v1 );
v2 = 29;
printf("\n[ ");
for ( i = strtok(s, " \n"); i; i = strtok(0LL, " \n") )
{
v4 = v2 - 1 - strlen(i);
if ( v4 > 0 )
{
v2 = v4;
printf("%s ", i);
}
else
{
printspaces(v2);
printf("]\n[ %s ", i);
v2 = 28 - strlen(i);
}
}
v5 = 30;
printspaces(v2);
printf("]\n ");
do
{
putchar(45);
--v5;
}
while ( v5 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func9(char *s)
{
int v1; // ebx
int v2; // er12
char *i; // rbp
int v4; // ebx
int v5; // ebx

v1 = 30;
putchar(32);
do
{
putchar(95);
--v1;
}
while ( v1 );
v2 = 29;
printf("\n[ ");
for ( i = strtok(s, " \n"); i; i = strtok(0LL, " \n") )
{
v4 = v2 - 1 - strlen(i);
if ( v4 > 0 )
{
v2 = v4;
printf("%s ", i);
}
else
{
func8(v2);
printf("]\n[ %s ", i);
v2 = 28 - strlen(i);
}
}
v5 = 30;
func8(v2);
printf("]\n ");
do
{
putchar(45);
--v5;
}
while ( v5 );
return putchar(10);
}

original_code:
void printbubble(char* s)
{
 int i;
 char* x;
 int spaceleft;
 printf(" ");
 for (i = 0; i < 30; i++)
 {
  printf("_");
 }
 printf("\n[ ");
 spaceleft = 29;
 x = strtok(s," \n");
 while (x != ((void *)0))
 {
  int testspace = spaceleft - strlen(x) - 1;
  if (testspace > 0)
  {
   printf("%s ", x);
   spaceleft = testspace;
  }
  else {
  printspaces(spaceleft);
  printf("]\n[ %s ", x);
  spaceleft = 29 - strlen(x) -1;
  }
  x = strtok(((void *)0), " \n");
 }
 printspaces(spaceleft);
 printf("]\n ");
 for (i = 0; i < 30; i++)
 {
  printf("-");
 }
 printf("\n");
}

canonical_original_code:
void printbubble(char *s)
{
 int i;
 char *x;
 int spaceleft;
 printf(" ");
 for (i = 0; i < 30; i++)
 {
  printf("_");
 }
 printf("\n[ ");
 spaceleft = 29;
 x = strtok(s," \n");
 while (x != ((void *)0))
 {
  int testspace = spaceleft - strlen(x) - 1;
  if (testspace > 0)
  {
   printf("%s ", x);
   spaceleft = testspace;
  }
  else {
  printspaces(spaceleft);
  printf("]\n[ %s ", x);
  spaceleft = 29 - strlen(x) -1;
  }
  x = strtok(((void *)0), " \n");
 }
 printspaces(spaceleft);
 printf("]\n ");
 for (i = 0; i < 30; i++)
 {
  printf("-");
 }
 printf("\n");
}

variable_types:
s
char *

variable_types:
x
char *

variable_types: spaceleft int

variable_types: testspace int


repo: catmaker/rpi-displays

decompiled_code:
__int64 __fastcall i2cdev_testopen(const char *a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 v5; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 2);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( ioctl(v2, 0x703uLL, a2) < 0 )
return (unsigned int)-2;
if ( (int)read(v3, &v5, 4uLL) < 0 )
v3 = -3;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 v5; // [rsp+0h] [rbp-28h] BYREF

v2 = open(a1, 2);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( ioctl(v2, 0x703uLL, a2) < 0 )
return (unsigned int)-2;
if ( (int)read(v3, &v5, 4uLL) < 0 )
v3 = -3;
return v3;
}

original_code:
int i2cdev_testopen(const char *devbusname, int i2caddr_test)
{
    int fd, r;
    unsigned char i2c_buffer[16];
    fd = open(devbusname, 02);
    if (fd < 0)
 return -1;
    r = ioctl(fd, 0x0703, i2caddr_test);
    if (r < 0)
       return -2;
    r = read(fd, i2c_buffer, 4);
    if (r < 0)
 return -3;
    return fd;
}

canonical_original_code:
int i2cdev_testopen(char *devbusname, int i2caddr_test)
{
    int fd; int r;
    unsigned char i2c_buffer[16];
    fd = open(devbusname, 02);
    if (fd < 0)
 return -1;
    r = ioctl(fd, 0x0703, i2caddr_test);
    if (r < 0)
       return -2;
    r = read(fd, i2c_buffer, 4);
    if (r < 0)
 return -3;
    return fd;
}

variable_types:
devbusname
char *

variable_types: r int


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

result = (unsigned int)a1;
if ( a1 > 9 )
result = 9LL;
if ( (int)result < 0 )
result = 0LL;
fontn = result;
return result;
}

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

result = (unsigned int)a1;
if ( a1 > 9 )
result = 9LL;
if ( (int)result < 0 )
result = 0LL;
fontn = result;
return result;
}

original_code:
int lcd_setfont(int f)
{
    if (f < 0)
 f = 0;
    if (f > 9)
 f = 9;
    fontn = f;
    return f;
}

variable_types: f int


decompiled_code:
void lcd_home()
{
cur_y = 0;
cur_x = 0;
}

canonical_decompiled_code:
void func2()
{
cur_y = 0;
cur_x = 0;
}

original_code:
void lcd_home(void)
{
    cur_x = cur_y = 0;
}

canonical_original_code:
void lcd_home()
{
    cur_x = cur_y = 0;
}


decompiled_code:
void __fastcall lcd_gotoxy(int a1, int a2)
{
cur_x = a1;
cur_y = a2;
}

canonical_decompiled_code:
void __fastcall func3(int a1, int a2)
{
cur_x = a1;
cur_y = a2;
}

original_code:
void lcd_gotoxy(int x, int y)
{
    cur_x = x;
    cur_y = y;
}

variable_types: x int

variable_types: y int


original_code:
int lcd_putc(int c, int update)
{
    int x, y, height, width;
    unsigned char *p;
    const struct font_desc *font;
    font = fonts[fontn];
    width = font->width;
    height = font->height;
    p = (unsigned char *) font->data;
    p += (c * height);
    for (y = 0; y < height; y++) {
 for (x = 0; x < width; x++) {
     drawPixel(cur_x + x, cur_y + y,
        ((1 << (8 - 1 - x)) & *(p + y)) ? 1 : 0);
 }
    }
    if (update)
 display();
    cur_x += width;
    return 0;
}

canonical_original_code:
int lcd_putc(int c, int update)
{
    int x; int y; int height; int width;
    unsigned char *p;
    struct font_desc *font;
    font = fonts[fontn];
    width = font->width;
    height = font->height;
    p = (unsigned char *) font->data;
    p += (c * height);
    for (y = 0; y < height; y++) {
 for (x = 0; x < width; x++) {
     drawPixel(cur_x + x, cur_y + y,
        ((1 << (8 - 1 - x)) & *(p + y)) ? 1 : 0);
 }
    }
    if (update)
 display();
    cur_x += width;
    return 0;
}

variable_types: c int

variable_types: update int

variable_types: height int

variable_types: width int

variable_types:
p
unsigned char *

variable_types: struct font_desc { int idx; char *name; int width; void *data; int pref; } *


decompiled_code:
__int64 __fastcall lcd_putchar(int a1)
{
return lcd_putc(a1, 0);
}

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

original_code:
int lcd_putchar(int c)
{
    return lcd_putc(c, 0);
}


decompiled_code:
__int64 __fastcall lcd_puts(char *a1)
{
_BYTE *v1; // rbx
__int64 v2; // rdi

v1 = a1;
v2 = (unsigned int)*a1;
if ( (_BYTE)v2 )
{
do
{
++v1;
lcd_putc(v2, 0);
display(v2);
usleep(0x186A0u);
v2 = (unsigned int)(char)*v1;
}
while ( *v1 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
_BYTE *v1; // rbx
__int64 v2; // rdi

v1 = a1;
v2 = (unsigned int)*a1;
if ( (_BYTE)v2 )
{
do
{
++v1;
func4(v2, 0);
display(v2);
usleep(0x186A0u);
v2 = (unsigned int)(char)*v1;
}
while ( *v1 );
}
return 0LL;
}

original_code:
int lcd_puts(char *s)
{
    while (*s) {
 lcd_putc(*s++, 0);
 display();
 usleep((100) * 1000);
    }
    return 0;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rdx
unsigned int v5; // ebp
int v6; // er13
int v8; // eax

v3 = i2cdev_testopen("/dev/i2c-0", 0x3Cu);
v4 = 1LL;
v5 = v3;
if ( v3 >= 0 || (v8 = i2cdev_testopen("/dev/i2c-1", 0x3Cu), v4 = 2LL, v5 = v8, v8 >= 0) )
{
v6 = 0;
printf("i2cdev_testopen: successful, fd_i2cbus = %d, rpi_rev = %d\n", v5, v4);
lcd_init(v5);
invertDisplay(1LL);
usleep(0x30D40u);
invertDisplay(0LL);
usleep(0x30D40u);
invertDisplay(1LL);
usleep(0x30D40u);
invertDisplay(0LL);
usleep(0x30D40u);
display(200000LL);
startscrollleft(0LL, 127LL);
usleep(0xF4240u);
stopscroll();
usleep(0x30D40u);
clearDisplay();
display(200000LL);
if ( argc > 1 )
lcd_puts((char *)argv[1]);
}
else
{
v6 = -1;
puts("i2cdev_testopen: /dev/i2c-x unsuccessful, OLED not found.");
}
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rdx
unsigned int v5; // ebp
int v6; // er13
int v8; // eax

v3 = func0("/dev/i2c-0", 0x3Cu);
v4 = 1LL;
v5 = v3;
if ( v3 >= 0 || (v8 = func0("/dev/i2c-1", 0x3Cu), v4 = 2LL, v5 = v8, v8 >= 0) )
{
v6 = 0;
printf("i2cdev_testopen: successful, fd_i2cbus = %d, rpi_rev = %d\n", v5, v4);
lcd_init(v5);
invertDisplay(1LL);
usleep(0x30D40u);
invertDisplay(0LL);
usleep(0x30D40u);
invertDisplay(1LL);
usleep(0x30D40u);
invertDisplay(0LL);
usleep(0x30D40u);
display(200000LL);
startscrollleft(0LL, 127LL);
usleep(0xF4240u);
stopscroll();
usleep(0x30D40u);
clearDisplay();
display(200000LL);
if ( argc > 1 )
func6((char *)argv[1]);
}
else
{
v6 = -1;
puts("i2cdev_testopen: /dev/i2c-x unsuccessful, OLED not found.");
}
return v6;
}

original_code:
int main(int argc, char * argv[])
{
    int rpi_rev, fd_adafruit;
    rpi_rev = 1;
    fd_adafruit = i2cdev_testopen("/dev/i2c-0", 0x3c);
    if (fd_adafruit < 0) {
 rpi_rev = 2;
 fd_adafruit = i2cdev_testopen("/dev/i2c-1", 0x3c);
    }
    if (fd_adafruit < 0) {
 printf("i2cdev_testopen: /dev/i2c-x unsuccessful, OLED not found.\n");
 return -1;
    }
    printf("i2cdev_testopen: successful, fd_i2cbus = %d, rpi_rev = %d\n",
    fd_adafruit,
    rpi_rev);
    lcd_init(fd_adafruit);
    invertDisplay(1);
    usleep((200) * 1000);
    invertDisplay(0);
    usleep((200) * 1000);
    invertDisplay(1);
    usleep((200) * 1000);
    invertDisplay(0);
    usleep((200) * 1000);
    display();
    startscrollleft(0, 127);
    usleep((1000) * 1000);
    stopscroll();
    usleep((200) * 1000);
    clearDisplay();
    display();
    if (argc > 1)
 lcd_puts(argv[1]);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int rpi_rev; int fd_adafruit;
    rpi_rev = 1;
    fd_adafruit = i2cdev_testopen("/dev/i2c-0", 0x3c);
    if (fd_adafruit < 0) {
 rpi_rev = 2;
 fd_adafruit = i2cdev_testopen("/dev/i2c-1", 0x3c);
    }
    if (fd_adafruit < 0) {
 printf("i2cdev_testopen: /dev/i2c-x unsuccessful, OLED not found.\n");
 return -1;
    }
    printf("i2cdev_testopen: successful, fd_i2cbus = %d, rpi_rev = %d\n",
    fd_adafruit,
    rpi_rev);
    lcd_init(fd_adafruit);
    invertDisplay(1);
    usleep((200) * 1000);
    invertDisplay(0);
    usleep((200) * 1000);
    invertDisplay(1);
    usleep((200) * 1000);
    invertDisplay(0);
    usleep((200) * 1000);
    display();
    startscrollleft(0, 127);
    usleep((1000) * 1000);
    stopscroll();
    usleep((200) * 1000);
    clearDisplay();
    display();
    if (argc > 1)
 lcd_puts(argv[1]);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


drawPixel

decompiled_code:
char __fastcall drawPixel(__int16 a1, __int16 a2, __int16 a3)
{
int v3; // eax
__int64 v4; // rdi
char v5; // cl
char result; // al
char v7; // cl

v3 = 1 << (a2 % 8);
v4 = a1 + ((a2 / 8) << 7);
v5 = ~(_BYTE)v3;
result = buffer[v4] | v3;
v7 = buffer[v4] & v5;
if ( a3 != 1 )
result = v7;
buffer[v4] = result;
return result;
}

canonical_decompiled_code:
char __fastcall func3(__int16 a1, __int16 a2, __int16 a3)
{
int v3; // eax
__int64 v4; // rdi
char v5; // cl
char result; // al
char v7; // cl

v3 = 1 << (a2 % 8);
v4 = a1 + ((a2 / 8) << 7);
v5 = ~(_BYTE)v3;
result = buffer[v4] | v3;
v7 = buffer[v4] & v5;
if ( a3 != 1 )
result = v7;
buffer[v4] = result;
return result;
}

canonical_original_code:
void drawPixel(short x, short y, unsigned short color)
{
    if (color == 1)
 buffer[x + (y / 8) * 128] |= ( 1<<(( y % 8)) );
    else
 buffer[x + (y / 8) * 128] &= ~( 1<<((y % 8)) );
}

variable_types: x short

variable_types: y short

variable_types:
color
unsigned short


decompiled_code:
int __fastcall lcd_WD(char a1)
{
char v2[24]; // [rsp+0h] [rbp-18h] BYREF

v2[1] = a1;
v2[0] = 64;
return i2c_writeblock_isra_0((__int64)v2, 2);
}

canonical_decompiled_code:
int __fastcall func4(char a1)
{
char v2[24]; // [rsp+0h] [rbp-18h] BYREF

v2[1] = a1;
v2[0] = 64;
return func2((__int64)v2, 2);
}

original_code:
void lcd_WD(unsigned char value)
{
    unsigned char obuf[16];
    obuf[0] = 0x40;
    obuf[1] = value;
    i2c_writeblock(obuf, 2);
}

variable_types:
value
unsigned char

variable_types:
obuf
unsigned char [16]


decompiled_code:
int __fastcall lcd_WC(char a1)
{
char v2[24]; // [rsp+0h] [rbp-18h] BYREF

v2[1] = a1;
v2[0] = 0;
return i2c_writeblock_isra_0((__int64)v2, 2);
}

canonical_decompiled_code:
int __fastcall func5(char a1)
{
char v2[24]; // [rsp+0h] [rbp-18h] BYREF

v2[1] = a1;
v2[0] = 0;
return func2((__int64)v2, 2);
}

original_code:
void lcd_WC(unsigned char value)
{
    unsigned char obuf[16];
    obuf[0] = 0x00;
    obuf[1] = value;
    i2c_writeblock(obuf, 2);
}


decompiled_code:
int __fastcall lcd_WC2(char a1, char a2)
{
char v3[24]; // [rsp+0h] [rbp-18h] BYREF

v3[1] = a1;
v3[3] = a2;
v3[0] = 0x80;
v3[2] = 0;
return i2c_writeblock_isra_0((__int64)v3, 4);
}

original_code:
void lcd_WC2(unsigned char value1, unsigned char value2)
{
    unsigned char obuf[16];
    obuf[0] = 0x80;
    obuf[1] = value1;
    obuf[2] = 0x00;
    obuf[3] = value2;
    i2c_writeblock(obuf, 4);
}


original_code:
void lcd_WD4(unsigned char value)
{
    unsigned char obuf[16];
    obuf[0] = 0xc0;
    obuf[1] = value;
    obuf[2] = 0xc0;
    obuf[3] = value;
    obuf[4] = 0xc0;
    obuf[5] = value;
    obuf[6] = 0x40;
    obuf[7] = value;
    i2c_writeblock(obuf, 8);
}


decompiled_code:
int __fastcall ssd1306_command(char a1)
{
return lcd_WC(a1);
}

canonical_decompiled_code:
int __fastcall func8(char a1)
{
return func5(a1);
}

original_code:
void ssd1306_command(uint8_t c)
{
    lcd_WC(c);
}

canonical_original_code:
void ssd1306_command(unsigned char c)
{
    lcd_WC(c);
}

variable_types:
c
unsigned char


decompiled_code:
int __fastcall ssd1306_data(char a1)
{
return lcd_WD(a1);
}

canonical_decompiled_code:
int __fastcall func9(char a1)
{
return func4(a1);
}

original_code:
void ssd1306_data(uint8_t c) {
    lcd_WD(c);
}

canonical_original_code:
void ssd1306_data(unsigned char c) {
    lcd_WD(c);
}


invertDisplay

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

if ( a1 )
result = ssd1306_command(167);
else
result = ssd1306_command(166);
return result;
}

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

if ( a1 )
result = func8(167);
else
result = func8(166);
return result;
}

original_code:
void invertDisplay(uint8_t i) {
  if (i) {
    ssd1306_command(0xA7);
  } else {
    ssd1306_command(0xA6);
  }
}

canonical_original_code:
void invertDisplay(unsigned char i) {
  if (i) {
    ssd1306_command(0xA7);
  } else {
    ssd1306_command(0xA6);
  }
}

variable_types:
i
unsigned char


display

variable_types:
obuf
unsigned char [32]

variable_types: i int

variable_types: x int


decompiled_code:
__int64 __fastcall lcd_init(int a1)
{
__int64 i; // rax
int v2; // edx
__int64 result; // rax
int v4; // edx

fd_oled = a1;
ssd1306_command(174);
ssd1306_command(213);
ssd1306_command(128);
ssd1306_command(168);
ssd1306_command(31);
ssd1306_command(211);
ssd1306_command(0);
ssd1306_command(64);
ssd1306_command(141);
ssd1306_command(20);
ssd1306_command(32);
ssd1306_command(0);
ssd1306_command(161);
ssd1306_command(200);
ssd1306_command(218);
ssd1306_command(2);
ssd1306_command(129);
ssd1306_command(143);
ssd1306_command(217);
ssd1306_command(241);
ssd1306_command(219);
ssd1306_command(64);
ssd1306_command(164);
ssd1306_command(166);
ssd1306_command(175);
for ( i = 0LL; i != 512; ++i )
{
v2 = (unsigned __int8)buffer[i];
buffer[i] = 0;
tempbuf[i] = v2;
}
display();
for ( result = 0LL; result != 512; byte_67F[result] = v4 )
v4 = tempbuf[result++];
return result;
}

original_code:
void lcd_init(int fd_i2cdev)
{
    int i;
    int vccstate = 0;
    fd_oled = fd_i2cdev;
    ssd1306_command(0xAE);
    ssd1306_command(0xD5);
    ssd1306_command(0x80);
    ssd1306_command(0xA8);
    ssd1306_command(0x1F);
    ssd1306_command(0xD3);
    ssd1306_command(0x0);
    ssd1306_command(0x40 | 0x0);
    ssd1306_command(0x8D);
    if (vccstate == 0x1) {
 ssd1306_command(0x10);
    }
    else {
 ssd1306_command(0x14);
    }
    ssd1306_command(0x20);
    ssd1306_command(0x00);
    ssd1306_command(0xA0 | 0x1);
    ssd1306_command(0xC8);
    ssd1306_command(0xDA);
    ssd1306_command(0x02);
    ssd1306_command(0x81);
    ssd1306_command(0x8F);
    ssd1306_command(0xD9);
    if (vccstate == 0x1) {
 ssd1306_command(0x22);
    }
    else {
 ssd1306_command(0xF1);
    }
    ssd1306_command(0xDB);
    ssd1306_command(0x40);
    ssd1306_command(0xA4);
    ssd1306_command(0xA6);
    ssd1306_command(0xAF);
    for (i = 0; i < (16*32); i++) {
 tempbuf[i] = buffer[i];
 buffer[i] = 0;
    }
    display();
    for (i = 0; i < (16*32); i++) {
 buffer[i] = tempbuf[i];
    }
}

variable_types: vccstate int


clearDisplay

decompiled_code:
__int64 clearDisplay()
{
__int64 result; // rax

result = 0LL;
memset(buffer, 0, sizeof(buffer));
return result;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 result; // rax

result = 0LL;
memset(buffer, 0, sizeof(buffer));
return result;
}

original_code:
void clearDisplay(void)
{
    memset(buffer, 0, (128*32/8));
}

canonical_original_code:
void clearDisplay()
{
    memset(buffer, 0, (128*32/8));
}


stopscroll

decompiled_code:
int stopscroll()
{
return ssd1306_command(46);
}

canonical_decompiled_code:
int func14()
{
return func8(46);
}

original_code:
void stopscroll(void)
{
    ssd1306_command(0x2E);
}

canonical_original_code:
void stopscroll()
{
    ssd1306_command(0x2E);
}