decompiled_code:
const char *__fastcall get_field_type(unsigned __int8 a1)
{
const char *result; // rax

if ( a1 <= 0xF5u )
return field_type_names[a1];
result = "new decimal";
if ( a1 >= 0xF7u )
result = CSWTCH_23[(unsigned __int8)(a1 + 9)];
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(unsigned __int8 a1)
{
const char *result; // rax

if ( a1 <= 0xF5u )
return field_type_names[a1];
result = "new decimal";
if ( a1 >= 0xF7u )
result = CSWTCH_23[(unsigned __int8)(a1 + 9)];
return result;
}

original_code:
const char *get_field_type(u_char type)
{
   if(type < 246)
      return field_type_names[type];
   switch(type) {
      case MYSQL_TYPE_NEWDECIMAL: return "new decimal";
      case MYSQL_TYPE_ENUM: return "enum";
      case MYSQL_TYPE_SET: return "set";
      case MYSQL_TYPE_TINY_BLOB: return "tiny BLOB";
      case MYSQL_TYPE_MEDIUM_BLOB: return "medium BLOB";
      case MYSQL_TYPE_LONG_BLOB: return "long BLOB";
      case MYSQL_TYPE_BLOB: return "BLOB";
      case MYSQL_TYPE_VAR_STRING: return "var string";
      case MYSQL_TYPE_STRING: return "string";
      case MYSQL_TYPE_GEOMETRY: return "geometry";
   }
   return "unknown";
}

canonical_original_code:
char *get_field_type(unsigned char type)
{
   if(type < 246)
      return field_type_names[type];
   switch(type) {
      case MYSQL_TYPE_NEWDECIMAL: return "new decimal";
      case MYSQL_TYPE_ENUM: return "enum";
      case MYSQL_TYPE_SET: return "set";
      case MYSQL_TYPE_TINY_BLOB: return "tiny BLOB";
      case MYSQL_TYPE_MEDIUM_BLOB: return "medium BLOB";
      case MYSQL_TYPE_LONG_BLOB: return "long BLOB";
      case MYSQL_TYPE_BLOB: return "BLOB";
      case MYSQL_TYPE_VAR_STRING: return "var string";
      case MYSQL_TYPE_STRING: return "string";
      case MYSQL_TYPE_GEOMETRY: return "geometry";
   }
   return "unknown";
}

variable_types:
type
unsigned char


repo: comstud/mysqlsniffer

decompiled_code:
int __fastcall unmask_caps(int a1)
{
printf("(Caps: ");
if ( (a1 & 1) != 0 )
{
printf("Long password, ");
if ( (a1 & 2) == 0 )
{
LABEL_3:
if ( (a1 & 4) == 0 )
goto LABEL_4;
goto LABEL_22;
}
}
else if ( (a1 & 2) == 0 )
{
goto LABEL_3;
}
printf("Found rows, ");
if ( (a1 & 4) == 0 )
{
LABEL_4:
if ( (a1 & 8) == 0 )
goto LABEL_5;
goto LABEL_23;
}
LABEL_22:
printf("Get all column flags, ");
if ( (a1 & 8) == 0 )
{
LABEL_5:
if ( (a1 & 0x10) == 0 )
goto LABEL_6;
goto LABEL_24;
}
LABEL_23:
printf("Connect w/DB, ");
if ( (a1 & 0x10) == 0 )
{
LABEL_6:
if ( (a1 & 0x20) == 0 )
goto LABEL_7;
goto LABEL_25;
}
LABEL_24:
printf("No schema, ");
if ( (a1 & 0x20) == 0 )
{
LABEL_7:
if ( (a1 & 0x40) == 0 )
goto LABEL_8;
goto LABEL_26;
}
LABEL_25:
printf("Compression, ");
if ( (a1 & 0x40) == 0 )
{
LABEL_8:
if ( (a1 & 0x80u) == 0 )
goto LABEL_9;
goto LABEL_27;
}
LABEL_26:
printf("ODBC client, ");
if ( (a1 & 0x80u) == 0 )
{
LABEL_9:
if ( (a1 & 0x100) == 0 )
goto LABEL_10;
goto LABEL_28;
}
LABEL_27:
printf("LOAD DATA LOCAL, ");
if ( (a1 & 0x100) == 0 )
{
LABEL_10:
if ( (a1 & 0x200) == 0 )
goto LABEL_11;
goto LABEL_29;
}
LABEL_28:
printf("Ignore space, ");
if ( (a1 & 0x200) == 0 )
{
LABEL_11:
if ( (a1 & 0x400) == 0 )
goto LABEL_12;
goto LABEL_30;
}
LABEL_29:
printf("4.1 protocol, ");
if ( (a1 & 0x400) == 0 )
{
LABEL_12:
if ( (a1 & 0x800) == 0 )
goto LABEL_13;
goto LABEL_31;
}
LABEL_30:
printf("Interactive, ");
if ( (a1 & 0x800) == 0 )
{
LABEL_13:
if ( (a1 & 0x1000) == 0 )
goto LABEL_14;
goto LABEL_32;
}
LABEL_31:
printf("SSL, ");
if ( (a1 & 0x1000) == 0 )
{
LABEL_14:
if ( (a1 & 0x2000) == 0 )
goto LABEL_15;
goto LABEL_33;
}
LABEL_32:
printf("Ignore SIGPIPE, ");
if ( (a1 & 0x2000) == 0 )
{
LABEL_15:
if ( (a1 & 0x4000) == 0 )
goto LABEL_16;
goto LABEL_34;
}
LABEL_33:
printf("Transactions, ");
if ( (a1 & 0x4000) == 0 )
{
LABEL_16:
if ( (a1 & 0x8000) == 0 )
goto LABEL_17;
goto LABEL_35;
}
LABEL_34:
printf("Reserved, ");
if ( (a1 & 0x8000) == 0 )
{
LABEL_17:
if ( (a1 & 0x10000) == 0 )
goto LABEL_18;
LABEL_36:
printf("Multi-statements, ");
if ( (a1 & 0x20000) == 0 )
return putchar(41);
goto LABEL_37;
}
LABEL_35:
printf("4.1 authentication, ");
if ( (a1 & 0x10000) != 0 )
goto LABEL_36;
LABEL_18:
if ( (a1 & 0x20000) == 0 )
return putchar(41);
LABEL_37:
printf("Multi-results");
return putchar(41);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
printf("(Caps: ");
if ( (a1 & 1) != 0 )
{
printf("Long password, ");
if ( (a1 & 2) == 0 )
{
LABEL_3:
if ( (a1 & 4) == 0 )
goto LABEL_4;
goto LABEL_22;
}
}
else if ( (a1 & 2) == 0 )
{
goto LABEL_3;
}
printf("Found rows, ");
if ( (a1 & 4) == 0 )
{
LABEL_4:
if ( (a1 & 8) == 0 )
goto LABEL_5;
goto LABEL_23;
}
LABEL_22:
printf("Get all column flags, ");
if ( (a1 & 8) == 0 )
{
LABEL_5:
if ( (a1 & 0x10) == 0 )
goto LABEL_6;
goto LABEL_24;
}
LABEL_23:
printf("Connect w/DB, ");
if ( (a1 & 0x10) == 0 )
{
LABEL_6:
if ( (a1 & 0x20) == 0 )
goto LABEL_7;
goto LABEL_25;
}
LABEL_24:
printf("No schema, ");
if ( (a1 & 0x20) == 0 )
{
LABEL_7:
if ( (a1 & 0x40) == 0 )
goto LABEL_8;
goto LABEL_26;
}
LABEL_25:
printf("Compression, ");
if ( (a1 & 0x40) == 0 )
{
LABEL_8:
if ( (a1 & 0x80u) == 0 )
goto LABEL_9;
goto LABEL_27;
}
LABEL_26:
printf("ODBC client, ");
if ( (a1 & 0x80u) == 0 )
{
LABEL_9:
if ( (a1 & 0x100) == 0 )
goto LABEL_10;
goto LABEL_28;
}
LABEL_27:
printf("LOAD DATA LOCAL, ");
if ( (a1 & 0x100) == 0 )
{
LABEL_10:
if ( (a1 & 0x200) == 0 )
goto LABEL_11;
goto LABEL_29;
}
LABEL_28:
printf("Ignore space, ");
if ( (a1 & 0x200) == 0 )
{
LABEL_11:
if ( (a1 & 0x400) == 0 )
goto LABEL_12;
goto LABEL_30;
}
LABEL_29:
printf("4.1 protocol, ");
if ( (a1 & 0x400) == 0 )
{
LABEL_12:
if ( (a1 & 0x800) == 0 )
goto LABEL_13;
goto LABEL_31;
}
LABEL_30:
printf("Interactive, ");
if ( (a1 & 0x800) == 0 )
{
LABEL_13:
if ( (a1 & 0x1000) == 0 )
goto LABEL_14;
goto LABEL_32;
}
LABEL_31:
printf("SSL, ");
if ( (a1 & 0x1000) == 0 )
{
LABEL_14:
if ( (a1 & 0x2000) == 0 )
goto LABEL_15;
goto LABEL_33;
}
LABEL_32:
printf("Ignore SIGPIPE, ");
if ( (a1 & 0x2000) == 0 )
{
LABEL_15:
if ( (a1 & 0x4000) == 0 )
goto LABEL_16;
goto LABEL_34;
}
LABEL_33:
printf("Transactions, ");
if ( (a1 & 0x4000) == 0 )
{
LABEL_16:
if ( (a1 & 0x8000) == 0 )
goto LABEL_17;
goto LABEL_35;
}
LABEL_34:
printf("Reserved, ");
if ( (a1 & 0x8000) == 0 )
{
LABEL_17:
if ( (a1 & 0x10000) == 0 )
goto LABEL_18;
LABEL_36:
printf("Multi-statements, ");
if ( (a1 & 0x20000) == 0 )
return putchar(41);
goto LABEL_37;
}
LABEL_35:
printf("4.1 authentication, ");
if ( (a1 & 0x10000) != 0 )
goto LABEL_36;
LABEL_18:
if ( (a1 & 0x20000) == 0 )
return putchar(41);
LABEL_37:
printf("Multi-results");
return putchar(41);
}

original_code:
void unmask_caps(u_int caps)
{
   printf("(Caps: ");
   if(caps & 1) printf("Long password, ");
   if(caps & 2) printf("Found rows, ");
   if(caps & 4) printf("Get all column flags, ");
   if(caps & 8) printf("Connect w/DB, ");
   if(caps & 16) printf("No schema, ");
   if(caps & 32) printf("Compression, ");
   if(caps & 64) printf("ODBC client, ");
   if(caps & 128) printf("LOAD DATA LOCAL, ");
   if(caps & 256) printf("Ignore space, ");
   if(caps & 512) printf("4.1 protocol, ");
   if(caps & 1024) printf("Interactive, ");
   if(caps & 2048) printf("SSL, ");
   if(caps & 4096) printf("Ignore SIGPIPE, ");
   if(caps & 8192) printf("Transactions, ");
   if(caps & 16384) printf("Reserved, ");
   if(caps & 32768) printf("4.1 authentication, ");
   if(caps & 65536) printf("Multi-statements, ");
   if(caps & 131072) printf("Multi-results");
   printf(")");
}

canonical_original_code:
void unmask_caps(unsigned int caps)
{
   printf("(Caps: ");
   if(caps & 1) printf("Long password, ");
   if(caps & 2) printf("Found rows, ");
   if(caps & 4) printf("Get all column flags, ");
   if(caps & 8) printf("Connect w/DB, ");
   if(caps & 16) printf("No schema, ");
   if(caps & 32) printf("Compression, ");
   if(caps & 64) printf("ODBC client, ");
   if(caps & 128) printf("LOAD DATA LOCAL, ");
   if(caps & 256) printf("Ignore space, ");
   if(caps & 512) printf("4.1 protocol, ");
   if(caps & 1024) printf("Interactive, ");
   if(caps & 2048) printf("SSL, ");
   if(caps & 4096) printf("Ignore SIGPIPE, ");
   if(caps & 8192) printf("Transactions, ");
   if(caps & 16384) printf("Reserved, ");
   if(caps & 32768) printf("4.1 authentication, ");
   if(caps & 65536) printf("Multi-statements, ");
   if(caps & 131072) printf("Multi-results");
   printf(")");
}

variable_types:
caps
unsigned int

return_type: void


decompiled_code:
int __fastcall unmask_status(__int16 a1)
{
printf("(Status: ");
if ( (a1 & 1) != 0 )
{
printf("Transaction started, ");
if ( (a1 & 2) == 0 )
{
LABEL_3:
if ( (a1 & 4) == 0 )
goto LABEL_4;
goto LABEL_14;
}
}
else if ( (a1 & 2) == 0 )
{
goto LABEL_3;
}
printf("Auto-commit, ");
if ( (a1 & 4) == 0 )
{
LABEL_4:
if ( (a1 & 8) == 0 )
goto LABEL_5;
goto LABEL_15;
}
LABEL_14:
printf("More results, ");
if ( (a1 & 8) == 0 )
{
LABEL_5:
if ( (a1 & 0x10) == 0 )
goto LABEL_6;
goto LABEL_16;
}
LABEL_15:
printf("Next query, ");
if ( (a1 & 0x10) == 0 )
{
LABEL_6:
if ( (a1 & 0x20) == 0 )
goto LABEL_7;
goto LABEL_17;
}
LABEL_16:
printf("No good index used, ");
if ( (a1 & 0x20) == 0 )
{
LABEL_7:
if ( (a1 & 0x40) == 0 )
goto LABEL_8;
goto LABEL_18;
}
LABEL_17:
printf("No index used, ");
if ( (a1 & 0x40) == 0 )
{
LABEL_8:
if ( (a1 & 0x80u) == 0 )
goto LABEL_9;
goto LABEL_19;
}
LABEL_18:
printf("Cursor ready, ");
if ( (a1 & 0x80u) == 0 )
{
LABEL_9:
if ( (a1 & 0x100) == 0 )
goto LABEL_10;
LABEL_20:
printf("DB dropped, ");
if ( (a1 & 0x200) == 0 )
return putchar(41);
goto LABEL_21;
}
LABEL_19:
printf("Last row sent, ");
if ( (a1 & 0x100) != 0 )
goto LABEL_20;
LABEL_10:
if ( (a1 & 0x200) == 0 )
return putchar(41);
LABEL_21:
printf("No backslash escapes");
return putchar(41);
}

canonical_decompiled_code:
int __fastcall func10(__int16 a1)
{
printf("(Status: ");
if ( (a1 & 1) != 0 )
{
printf("Transaction started, ");
if ( (a1 & 2) == 0 )
{
LABEL_3:
if ( (a1 & 4) == 0 )
goto LABEL_4;
goto LABEL_14;
}
}
else if ( (a1 & 2) == 0 )
{
goto LABEL_3;
}
printf("Auto-commit, ");
if ( (a1 & 4) == 0 )
{
LABEL_4:
if ( (a1 & 8) == 0 )
goto LABEL_5;
goto LABEL_15;
}
LABEL_14:
printf("More results, ");
if ( (a1 & 8) == 0 )
{
LABEL_5:
if ( (a1 & 0x10) == 0 )
goto LABEL_6;
goto LABEL_16;
}
LABEL_15:
printf("Next query, ");
if ( (a1 & 0x10) == 0 )
{
LABEL_6:
if ( (a1 & 0x20) == 0 )
goto LABEL_7;
goto LABEL_17;
}
LABEL_16:
printf("No good index used, ");
if ( (a1 & 0x20) == 0 )
{
LABEL_7:
if ( (a1 & 0x40) == 0 )
goto LABEL_8;
goto LABEL_18;
}
LABEL_17:
printf("No index used, ");
if ( (a1 & 0x40) == 0 )
{
LABEL_8:
if ( (a1 & 0x80u) == 0 )
goto LABEL_9;
goto LABEL_19;
}
LABEL_18:
printf("Cursor ready, ");
if ( (a1 & 0x80u) == 0 )
{
LABEL_9:
if ( (a1 & 0x100) == 0 )
goto LABEL_10;
LABEL_20:
printf("DB dropped, ");
if ( (a1 & 0x200) == 0 )
return putchar(41);
goto LABEL_21;
}
LABEL_19:
printf("Last row sent, ");
if ( (a1 & 0x100) != 0 )
goto LABEL_20;
LABEL_10:
if ( (a1 & 0x200) == 0 )
return putchar(41);
LABEL_21:
printf("No backslash escapes");
return putchar(41);
}

original_code:
void unmask_status(u_short status)
{
   printf("(Status: ");
   if(status & 1) printf("Transaction started, ");
   if(status & 2) printf("Auto-commit, ");
   if(status & 4) printf("More results, ");
   if(status & 8) printf("Next query, ");
   if(status & 16) printf("No good index used, ");
   if(status & 32) printf("No index used, ");
   if(status & 64) printf("Cursor ready, ");
   if(status & 128) printf("Last row sent, ");
   if(status & 256) printf("DB dropped, ");
   if(status & 512) printf("No backslash escapes");
   printf(")");
}

canonical_original_code:
void unmask_status(unsigned short status)
{
   printf("(Status: ");
   if(status & 1) printf("Transaction started, ");
   if(status & 2) printf("Auto-commit, ");
   if(status & 4) printf("More results, ");
   if(status & 8) printf("Next query, ");
   if(status & 16) printf("No good index used, ");
   if(status & 32) printf("No index used, ");
   if(status & 64) printf("Cursor ready, ");
   if(status & 128) printf("Last row sent, ");
   if(status & 256) printf("DB dropped, ");
   if(status & 512) printf("No backslash escapes");
   printf(")");
}

variable_types:
status
unsigned short


repo: concurrency/kroc

decompiled_code:
__int64 __fastcall get_pixel(_DWORD *a1, int a2, int a3)
{
return *((unsigned __int8 *)a1 + a2 + *a1 * a3 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, int a2, int a3)
{
return *((unsigned __int8 *)a1 + a2 + *a1 * a3 + 16);
}

original_code:
static inline unsigned int get_pixel (image_t *img, int x, int y)
{
 return (unsigned int) img->data[(img->width * y) + x];
}

canonical_original_code:
static inline unsigned int get_pixel(struct _image_t *img, int x, int y)
{
 return (unsigned int) img->data[(img->width * y) + x];
}

variable_types: struct _image_t { int width; int height; struct _image_t *next; unsigned char data[1]; } *

variable_types: x int

variable_types: y int

return_type: unsigned int


decompiled_code:
__int64 __fastcall set_pixel(_DWORD *a1, int a2, int a3, unsigned int a4)
{
__int64 result; // rax

result = 255LL;
if ( a4 >= 0xFF )
LOBYTE(a4) = -1;
*((_BYTE *)a1 + a2 + *a1 * a3 + 16) = a4;
return result;
}

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

result = 255LL;
if ( a4 >= 0xFF )
LOBYTE(a4) = -1;
*((_BYTE *)a1 + a2 + *a1 * a3 + 16) = a4;
return result;
}

original_code:
static inline void set_pixel (image_t *img, int x, int y, unsigned int v)
{
 img->data[(img->width * y) + x] = (unsigned char) (v < 255 ? v : 255);
}

canonical_original_code:
static inline void set_pixel(struct _image_t *img, int x, int y, unsigned int v)
{
 img->data[(img->width * y) + x] = (unsigned char) (v < 255 ? v : 255);
}

variable_types:
v
unsigned int

user_defined_types: struct _image_t { int width; int height; struct _image_t *next; unsigned char data[1]; }


decompiled_code:
// attributes: thunk
void image_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func9(void *ptr)
{
free(ptr);
}

original_code:
static void image_free (image_t *img)
{
 free (img);
}

canonical_original_code:
static void image_free(struct _image_t *img)
{
 free (img);
}


main

canonical_name: main

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

if ( argc <= 1 )
{
v3 = 64;
v4 = 1;
v5 = 0LL;
}
else
{
v3 = atoi(argv[1]);
if ( argc == 2 )
{
v4 = 1;
v5 = 0LL;
}
else
{
v4 = atoi(argv[2]);
if ( argc == 3 )
v5 = 0LL;
else
v5 = (unsigned int)atoi(argv[3]);
}
}
proc_main(v3, v4, v5);
return 0;
}

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

if ( argc <= 1 )
{
v3 = 64;
v4 = 1;
v5 = 0LL;
}
else
{
v3 = atoi(argv[1]);
if ( argc == 2 )
{
v4 = 1;
v5 = 0LL;
}
else
{
v4 = atoi(argv[2]);
if ( argc == 3 )
v5 = 0LL;
else
v5 = (unsigned int)atoi(argv[3]);
}
}
func11(v3, v4, v5);
return 0;
}

original_code:
int main (int argc, char *argv[])
{
 int elements = 1;
 int frames = 64;
 int queuing = 0;
 if (argc >= 2)
  frames = atoi (argv[1]);
 if (argc >= 3)
  elements = atoi (argv[2]);
 if (argc >= 4)
  queuing = atoi (argv[3]);
 proc_main (frames, elements, queuing);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int elements = 1;
 int frames = 64;
 int queuing = 0;
 if (argc >= 2)
  frames = atoi (argv[1]);
 if (argc >= 3)
  elements = atoi (argv[2]);
 if (argc >= 4)
  queuing = atoi (argv[3]);
 proc_main (frames, elements, queuing);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: elements int

variable_types: frames int

variable_types: queuing int

return_type: int


decompiled_code:
int __fastcall proc_main(int a1, int a2, int a3)
{
int i; // ebx
pthread_t newthread[6]; // [rsp+8h] [rbp-30h] BYREF

pthread_mutex_init(&lock, 0LL);
pthread_cond_init(&want_work, 0LL);
pthread_cond_init(&work_available, 0LL);
pthread_cond_init(&worker_terminated, 0LL);
workers = a2;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
pthread_create(newthread, 0LL, element, 0LL);
}
return server_constprop_0(a1, a3);
}

canonical_decompiled_code:
int __fastcall func11(int a1, int a2, int a3)
{
int i; // ebx
pthread_t newthread[6]; // [rsp+8h] [rbp-30h] BYREF

pthread_mutex_init(&lock, 0LL);
pthread_cond_init(&want_work, 0LL);
pthread_cond_init(&work_available, 0LL);
pthread_cond_init(&worker_terminated, 0LL);
workers = a2;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
pthread_create(newthread, 0LL, element, 0LL);
}
return func10(a1, a3);
}

original_code:
static void proc_main (int frames, int elements, int queuing)
{
 int i;
 pthread_mutex_init (&lock, ((void *)0));
 pthread_cond_init (&want_work, ((void *)0));
 pthread_cond_init (&work_available, ((void *)0));
 pthread_cond_init (&worker_terminated, ((void *)0));
 workers = elements;
 for (i = 0; i < elements; ++i) {
  pthread_t thread;
  pthread_create (&thread, ((void *)0), element, ((void *)0));
 }
 server (frames, elements, queuing);
}

canonical_original_code:
static void proc_main(int frames, int elements, int queuing)
{
 int i;
 pthread_mutex_init (&lock, ((void *)0));
 pthread_cond_init (&want_work, ((void *)0));
 pthread_cond_init (&work_available, ((void *)0));
 pthread_cond_init (&worker_terminated, ((void *)0));
 workers = elements;
 for (i = 0; i < elements; ++i) {
  unsigned long thread;
  pthread_create (&thread, ((void *)0), element, ((void *)0));
 }
 server (frames, elements, queuing);
}

variable_types: i int

variable_types:
thread
unsigned long


element

decompiled_code:
void *__fastcall element(void *a1)
{
int *v1; // rbp
int *v2; // r12

while ( 1 )
{
pthread_mutex_lock(&lock);
while ( !complete )
{
if ( next_image )
break;
pthread_cond_signal(&want_work);
pthread_cond_wait(&work_available, &lock);
}
v1 = (int *)next_image;
if ( !next_image )
break;
next_image = (void *)*((_QWORD *)next_image + 1);
pthread_mutex_unlock(&lock);
v2 = image_alloc(*v1, v1[1]);
blur_image_constprop_0(v1, v2);
image_free(v1);
image_free(v2);
}
--workers;
pthread_cond_signal(&worker_terminated);
pthread_mutex_unlock(&lock);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func14(void *a1)
{
int *v1; // rbp
int *v2; // r12

while ( 1 )
{
pthread_mutex_lock(&lock);
while ( !complete )
{
if ( next_image )
break;
pthread_cond_signal(&want_work);
pthread_cond_wait(&work_available, &lock);
}
v1 = (int *)next_image;
if ( !next_image )
break;
next_image = (void *)*((_QWORD *)next_image + 1);
pthread_mutex_unlock(&lock);
v2 = image_alloc(*v1, v1[1]);
func13(v1, v2);
func9(v1);
func9(v2);
}
--workers;
pthread_cond_signal(&worker_terminated);
pthread_mutex_unlock(&lock);
return 0LL;
}

original_code:
static void *element (void *p)
{
 for (;;) {
  image_t *img = ((void *)0);
  pthread_mutex_lock (&lock);
  while (!complete && next_image == ((void *)0)) {
   pthread_cond_signal (&want_work);
   pthread_cond_wait (&work_available, &lock);
  }
  img = next_image;
  if (img == ((void *)0)) {
   break;
  } else {
   next_image = img->next;
  }
  pthread_mutex_unlock (&lock);
  {
   image_t *buf = image_alloc (img->width, img->height);
   blur_image (9, img, buf);
   image_free (img);
   image_free (buf);
  }
 }
 workers--;
 pthread_cond_signal (&worker_terminated);
 pthread_mutex_unlock (&lock);
 return ((void *)0);
}

canonical_original_code:
static void *element(void *p)
{
 for (;;) {
  struct _image_t *img = ((void *)0);
  pthread_mutex_lock (&lock);
  while (!complete && next_image == ((void *)0)) {
   pthread_cond_signal (&want_work);
   pthread_cond_wait (&work_available, &lock);
  }
  img = next_image;
  if (img == ((void *)0)) {
   break;
  } else {
   next_image = img->next;
  }
  pthread_mutex_unlock (&lock);
  {
   struct _image_t *buf = image_alloc (img->width, img->height);
   blur_image (9, img, buf);
   image_free (img);
   image_free (buf);
  }
 }
 workers--;
 pthread_cond_signal (&worker_terminated);
 pthread_mutex_unlock (&lock);
 return ((void *)0);
}

variable_types:
p
void *

return_type: void *


repo: conformal/adsuck

decompiled_code:
void __fastcall log_init(int a1)
{
logdebug = a1;
if ( !a1 )
openlog(_progname, 9, 24);
tzset();
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
logdebug = a1;
if ( !a1 )
openlog(_progname, 9, 24);
tzset();
}

original_code:
void
log_init(int n_debug)
{
 extern char *__progname;
 logdebug = n_debug;
 if (!logdebug)
  openlog(__progname, 0x01 | 0x08, (3<<3));
 tzset();
}


vlog

decompiled_code:
void __fastcall vlog(int a1, const char *a2, void *a3)
{
char *ptr; // [rsp+8h] [rbp-20h] BYREF

if ( logdebug )
{
if ( asprintf(&ptr, "%s\n", a2) == -1 )
{
vfprintf(stderr, a2, a3);
fputc(10, stderr);
}
else
{
vfprintf(stderr, ptr, a3);
free(ptr);
}
fflush(stderr);
}
else
{
vsyslog(a1, a2, a3);
}
}

canonical_decompiled_code:
void __fastcall func1(int a1, const char *a2, void *a3)
{
char *ptr; // [rsp+8h] [rbp-20h] BYREF

if ( logdebug )
{
if ( asprintf(&ptr, "%s\n", a2) == -1 )
{
vfprintf(stderr, a2, a3);
fputc(10, stderr);
}
else
{
vfprintf(stderr, ptr, a3);
free(ptr);
}
fflush(stderr);
}
else
{
vsyslog(a1, a2, a3);
}
}

original_code:
void
vlog(int pri, const char *fmt, va_list ap)
{
 char *nfmt;
 if (logdebug) {
  if (asprintf(&nfmt, "%s\n", fmt) == -1) {
   vfprintf(stderr, fmt, ap);
   fprintf(stderr, "\n");
  } else {
   vfprintf(stderr, nfmt, ap);
   free(nfmt);
  }
  fflush(stderr);
 } else
  vsyslog(pri, fmt, ap);
}

canonical_original_code:
void vlog(int pri, char *fmt, __builtin_va_list ap)
{
 char *nfmt;
 if (logdebug) {
  if (asprintf(&nfmt, "%s\n", fmt) == -1) {
   vfprintf(stderr, fmt, ap);
   fprintf(stderr, "\n");
  } else {
   vfprintf(stderr, nfmt, ap);
   free(nfmt);
  }
  fflush(stderr);
 } else
  vsyslog(pri, fmt, ap);
}

variable_types: pri int

variable_types:
fmt
char *

variable_types:
nfmt
char *


logit

decompiled_code:
void logit(int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
vlog(a1, a2, va);
}

canonical_decompiled_code:
void func2(int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
func1(a1, a2, va);
}

original_code:
void
logit(int pri, const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 vlog(pri, fmt, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void logit(int pri, char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vlog(pri, fmt, ap);
 __builtin_va_end(ap);
}


decompiled_code:
void log_warn(const char *a1, ...)
{
int *v2; // rax
int *v3; // rbx
int v4; // edi
char *v5; // rax
char *v6; // rax
void *ptr; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

v2 = _errno_location();
v3 = v2;
if ( a1 )
{
v4 = *v2;
va_start(va, a1);
v5 = strerror(v4);
if ( asprintf((char **)&ptr, "%s: %s", a1, v5) != -1 )
{
vlog(2, (const char *)ptr, va);
free(ptr);
return;
}
vlog(2, a1, va);
}
v6 = strerror(*v3);
logit(2, "%s", v6);
}

canonical_decompiled_code:
void func3(const char *a1, ...)
{
int *v2; // rax
int *v3; // rbx
int v4; // edi
char *v5; // rax
char *v6; // rax
void *ptr; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

v2 = _errno_location();
v3 = v2;
if ( a1 )
{
v4 = *v2;
va_start(va, a1);
v5 = strerror(v4);
if ( asprintf((char **)&ptr, "%s: %s", a1, v5) != -1 )
{
func1(2, (const char *)ptr, va);
free(ptr);
return;
}
func1(2, a1, va);
}
v6 = strerror(*v3);
func2(2, "%s", v6);
}

original_code:
void
log_warn(const char *emsg, ...)
{
 char *nfmt;
 va_list ap;
 if (emsg == ((void *)0))
  logit(2, "%s", strerror((*__errno_location ())));
 else {
  __builtin_va_start(ap,emsg);
  if (asprintf(&nfmt, "%s: %s", emsg, strerror((*__errno_location ()))) == -1) {
   vlog(2, emsg, ap);
   logit(2, "%s", strerror((*__errno_location ())));
  } else {
   vlog(2, nfmt, ap);
   free(nfmt);
  }
  __builtin_va_end(ap);
 }
}

canonical_original_code:
void log_warn(char *emsg, ...)
{
 char *nfmt;
 __builtin_va_list ap;
 if (emsg == ((void *)0))
  logit(2, "%s", strerror((*__errno_location ())));
 else {
  __builtin_va_start(ap,emsg);
  if (asprintf(&nfmt, "%s: %s", emsg, strerror((*__errno_location ()))) == -1) {
   vlog(2, emsg, ap);
   logit(2, "%s", strerror((*__errno_location ())));
  } else {
   vlog(2, nfmt, ap);
   free(nfmt);
  }
  __builtin_va_end(ap);
 }
}

variable_types:
emsg
char *


decompiled_code:
void log_warnx(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
vlog(2, a1, va);
}

canonical_decompiled_code:
void func4(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func1(2, a1, va);
}

original_code:
void
log_warnx(const char *emsg, ...)
{
 va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(2, emsg, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void log_warnx(char *emsg, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(2, emsg, ap);
 __builtin_va_end(ap);
}


decompiled_code:
void log_info(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
vlog(6, a1, va);
}

canonical_decompiled_code:
void func5(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func1(6, a1, va);
}

original_code:
void
log_info(const char *emsg, ...)
{
 va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(6, emsg, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void log_info(char *emsg, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(6, emsg, ap);
 __builtin_va_end(ap);
}


decompiled_code:
void log_debug(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( debugsyslog | logdebug )
{
va_start(va, a1);
vlog(7, a1, va);
}
}

canonical_decompiled_code:
void func6(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( debugsyslog | logdebug )
{
va_start(va, a1);
func1(7, a1, va);
}
}

original_code:
void
log_debug(const char *emsg, ...)
{
 va_list ap;
 if (logdebug || debugsyslog) {
  __builtin_va_start(ap,emsg);
  vlog(7, emsg, ap);
  __builtin_va_end(ap);
 }
}

canonical_original_code:
void log_debug(char *emsg, ...)
{
 __builtin_va_list ap;
 if (logdebug || debugsyslog) {
  __builtin_va_start(ap,emsg);
  vlog(7, emsg, ap);
  __builtin_va_end(ap);
 }
}


fatal

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
int v2; // edi
char *v3; // rax
char *v4; // rax

v2 = *_errno_location();
if ( a1 )
{
if ( v2 )
{
v3 = strerror(v2);
logit(2, "fatal: %s: %s", a1, v3);
}
else
{
logit(2, "fatal: %s", a1);
}
}
else
{
v4 = strerror(v2);
logit(2, "fatal: %s", v4);
}
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
int v2; // edi
char *v3; // rax
char *v4; // rax

v2 = *_errno_location();
if ( a1 )
{
if ( v2 )
{
v3 = strerror(v2);
func2(2, "fatal: %s: %s", a1, v3);
}
else
{
func2(2, "fatal: %s", a1);
}
}
else
{
v4 = strerror(v2);
func2(2, "fatal: %s", v4);
}
exit(1);
}

original_code:
void
fatal(const char *emsg)
{
 if (emsg == ((void *)0))
  logit(2, "fatal: %s", strerror((*__errno_location ())));
 else
  if ((*__errno_location ()))
   logit(2, "fatal: %s: %s",
       emsg, strerror((*__errno_location ())));
  else
   logit(2, "fatal: %s", emsg);
 exit(1);
}

canonical_original_code:
void fatal(char *emsg)
{
 if (emsg == ((void *)0))
  logit(2, "fatal: %s", strerror((*__errno_location ())));
 else
  if ((*__errno_location ()))
   logit(2, "fatal: %s: %s",
       emsg, strerror((*__errno_location ())));
  else
   logit(2, "fatal: %s", emsg);
 exit(1);
}


fatalx

decompiled_code:
void __fastcall __noreturn fatalx(const char *a1)
{
*_errno_location() = 0;
fatal(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
*_errno_location() = 0;
func7(a1);
}

original_code:
void
fatalx(const char *emsg)
{
 (*__errno_location ()) = 0;
 fatal(emsg);
}

canonical_original_code:
void fatalx(char *emsg)
{
 (*__errno_location ()) = 0;
 fatal(emsg);
}


decompiled_code:
const char *__fastcall log_sockaddr(const struct sockaddr *a1)
{
bool v1; // zf
const char *result; // rax

v1 = getnameinfo(a1, 0, buf_0, 0x401u, 0LL, 0, 1) == 0;
result = "(unknown)";
if ( v1 )
result = buf_0;
return result;
}

canonical_decompiled_code:
const char *__fastcall func9(const struct sockaddr *a1)
{
bool v1; // zf
const char *result; // rax

v1 = getnameinfo(a1, 0, buf_0, 0x401u, 0LL, 0, 1) == 0;
result = "(unknown)";
if ( v1 )
result = buf_0;
return result;
}

original_code:
const char *
log_sockaddr(struct sockaddr *sa)
{
 static char buf[1025];
 if (getnameinfo(sa, (0), buf, sizeof(buf), ((void *)0), 0,
     1))
  return ("(unknown)");
 else
  return (buf);
}

canonical_original_code:
char *log_sockaddr(struct sockaddr *sa)
{
 static char buf[1025];
 if (getnameinfo(sa, (0), buf, sizeof(buf), ((void *)0), 0,
     1))
  return ("(unknown)");
 else
  return (buf);
}

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

variable_types:
buf
char [1025]

return_type: char *


decompiled_code:
__int64 monitor_fork()
{
return 0xFFFFFFFFLL;
}

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

original_code:
pid_t
monitor_fork(char *filename)
{
 return (-1);
}

canonical_original_code:
int monitor_fork(char *filename)
{
 return (-1);
}

variable_types:
filename
char *


setresuid

decompiled_code:
__int64 __fastcall setresuid(__uid_t a1, __uid_t a2)
{
__int64 result; // rax

if ( setuid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(seteuid(a2) != 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__uid_t a1, __uid_t a2)
{
__int64 result; // rax

if ( setuid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(seteuid(a2) != 0);
return result;
}

original_code:
int setresuid(uid_t ruid, uid_t euid, uid_t suid)
{
 if (setuid(ruid))
  return -1;
 if (seteuid(euid))
  return -1;
 return 0;
}

canonical_original_code:
int setresuid(unsigned int ruid, unsigned int euid, unsigned int suid)
{
 if (setuid(ruid))
  return -1;
 if (seteuid(euid))
  return -1;
 return 0;
}

variable_types:
ruid
unsigned int

variable_types:
euid
unsigned int

variable_types:
suid
unsigned int


setresgid

decompiled_code:
__int64 __fastcall setresgid(__gid_t a1, __gid_t a2)
{
__int64 result; // rax

if ( setgid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setegid(a2) != 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__gid_t a1, __gid_t a2)
{
__int64 result; // rax

if ( setgid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setegid(a2) != 0);
return result;
}

original_code:
int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
{
 if (setgid(rgid))
  return -1;
 if (setegid(egid))
  return -1;
 return 0;
}

canonical_original_code:
int setresgid(unsigned int rgid, unsigned int egid, unsigned int sgid)
{
 if (setgid(rgid))
  return -1;
 if (setegid(egid))
  return -1;
 return 0;
}

variable_types:
rgid
unsigned int

variable_types:
egid
unsigned int

variable_types:
sgid
unsigned int


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 char *d = dst;
 char *s = src;
 unsigned long n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long

return_type: unsigned long


repo: conformal/clens

decompiled_code:
__int64 __fastcall arc4random_uniform(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // eax

if ( a1 <= 1 )
return 0LL;
if ( a1 > 0x80000000 )
v1 = -a1;
else
v1 = -2 * a1 % a1;
do
v2 = arc4random();
while ( v2 < v1 );
return v2 % a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // eax

if ( a1 <= 1 )
return 0LL;
if ( a1 > 0x80000000 )
v1 = -a1;
else
v1 = -2 * a1 % a1;
do
v2 = arc4random();
while ( v2 < v1 );
return v2 % a1;
}

original_code:
uint32_t
arc4random_uniform(u_int32_t upper_bound)
{
 uint32_t r, min;
 if (upper_bound < 2)
  return 0;
 if (upper_bound > 0x80000000)
  min = 1 + ~upper_bound;
 else {
  min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
 }
 for (;;) {
  r = arc4random();
  if (r >= min)
   break;
 }
 return r % upper_bound;
}

canonical_original_code:
unsigned int arc4random_uniform(unsigned int upper_bound)
{
 unsigned int r; unsigned int min;
 if (upper_bound < 2)
  return 0;
 if (upper_bound > 0x80000000)
  min = 1 + ~upper_bound;
 else {
  min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
 }
 for (;;) {
  r = arc4random();
  if (r >= min)
   break;
 }
 return r % upper_bound;
}

variable_types:
r
unsigned int

variable_types:
min
unsigned int


original_code:
int
scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i, ndigits = 0, fract_digits = 0;
 long long scale_fact = 1, whole = 0, fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short int) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

canonical_original_code:
int scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i; unsigned int ndigits = 0; unsigned int fract_digits = 0;
 long long scale_fact = 1; long long whole = 0; long long fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

variable_types:
scaled
char *

variable_types:
result
long long *

variable_types:
p
char *

variable_types: sign int

variable_types:
i
unsigned int

variable_types:
ndigits
unsigned int

variable_types:
whole
long long

variable_types:
fpart
long long


original_code:
int
fmt_scaled(long long number, char *result)
{
 long long abval, fract = 0;
 unsigned int i;
 unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

canonical_original_code:
int fmt_scaled(long long number, char *result)
{
 long long abval; long long fract = 0;
 unsigned int i;
 enum unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

variable_types:
number
long long

variable_types:
result
char *

variable_types:
abval
long long

variable_types:
fract
long long

variable_types: enum unit_type { NONE=0, KILO=1, MEGA=2, GIGA=3, TERA=4, PETA=5, EXA=6 }


strtonum

decompiled_code:
__int64 __fastcall strtonum(char *nptr, __int64 a2, __int64 a3, _QWORD *a4)
{
int *v6; // rax
int v7; // er15
int *v8; // rbx
int v9; // edx
__int64 v10; // rax
__int64 v11; // r8
char *endptr[16]; // [rsp+8h] [rbp-80h] BYREF

endptr[3] = "invalid";
endptr[5] = "too small";
endptr[7] = "too large";
v6 = _errno_location();
v7 = *v6;
v8 = v6;
*v6 = 0;
if ( a2 > a3 || (v11 = strtoll(nptr, endptr, 10), endptr[0] == nptr) || *endptr[0] )
{
v9 = 22;
v10 = 1LL;
goto LABEL_3;
}
if ( v11 == 0x8000000000000000LL )
{
v9 = *v8;
if ( *v8 == 34 )
{
LABEL_19:
v10 = 2LL;
goto LABEL_3;
}
if ( a2 == 0x8000000000000000LL )
goto LABEL_12;
LABEL_18:
v9 = 34;
goto LABEL_19;
}
if ( a2 > v11 )
goto LABEL_18;
if ( v11 == 0x7FFFFFFFFFFFFFFFLL )
{
v9 = *v8;
if ( *v8 == 34 )
{
v10 = 3LL;
LABEL_3:
if ( a4 )
{
v11 = 0LL;
*a4 = endptr[2 * v10 + 1];
*v8 = v9;
}
else
{
*v8 = v9;
v11 = 0LL;
}
return v11;
}
}
v9 = 34;
v10 = 3LL;
if ( a3 < v11 )
goto LABEL_3;
LABEL_12:
if ( a4 )
*a4 = 0LL;
*v8 = v7;
return v11;
}

original_code:
long long
strtonum(const char *numstr, long long minval, long long maxval,
    const char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval {
  const char *errstr;
  int err;
 } ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

canonical_original_code:
long long strtonum(char *numstr, long long minval, long long maxval, char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

variable_types:
numstr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstrp
char **

variable_types:
ll
long long

variable_types:
ep
char *

variable_types: error int

variable_types: struct errval { char *errstr; int err; } [4]

return_type: long long


decompiled_code:
const char *clens_verstring()
{
return "0.7.0";
}

canonical_decompiled_code:
const char *func0()
{
return "0.7.0";
}

original_code:
const char *
clens_verstring(void)
{
 return vertag;
}

canonical_original_code:
char *clens_verstring()
{
 return vertag;
}


isescaped

decompiled_code:
__int64 __fastcall isescaped(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

original_code:
static int
isescaped(const char *sp, const char *p, int esc)
{
 const char *cp;
 size_t ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

canonical_original_code:
static int isescaped(char *sp, char *p, int esc)
{
 char *cp;
 unsigned long ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

variable_types:
sp
char *

variable_types:
p
char *

variable_types: esc int

variable_types:
cp
char *

variable_types:
ne
unsigned long


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0')
    break;
  }
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 char *d = dst;
 char *s = src;
 unsigned long n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0')
    break;
  }
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long


repo: conformal/cyphertite

decompiled_code:
int __fastcall ct_glob_match(const char **a1, const char *a2)
{
const char *v2; // r8
const char **i; // rbx
int result; // eax

if ( !a1 )
return 1;
v2 = *a1;
if ( !*a1 )
return 1;
for ( i = a1 + 1; ; ++i )
{
result = fnmatch(v2, a2, 0);
if ( !result )
break;
v2 = *i;
if ( !v2 )
return 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char **a1, const char *a2)
{
const char *v2; // r8
const char **i; // rbx
int result; // eax

if ( !a1 )
return 1;
v2 = *a1;
if ( !*a1 )
return 1;
for ( i = a1 + 1; ; ++i )
{
result = fnmatch(v2, a2, 0);
if ( !result )
break;
v2 = *i;
if ( !v2 )
return 1;
}
return result;
}

original_code:
int
ct_glob_match(char **g, char *file)
{
 int i;
 if (g == ((void *)0))
  return (1);
 for (i = 0; g[i] != ((void *)0); i++) {
  if (g[i] == ((void *)0))
   break;
  if (fnmatch(g[i], file, 0) == 0)
   return (0);
 }
 return (1);
}

variable_types:
g
char **

variable_types:
file
char *

variable_types: i int


main

repo: contra/PiFM

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
int v6; // edi
__int64 v7; // rax
char v8; // si
__int64 v9; // rbp
unsigned int v10; // eax
int v11; // eax
int v12; // er15
unsigned __int64 v13; // r12
__off_t v14; // rax
__int64 v15; // rcx
__int64 v16; // r12
_BYTE *v17; // rax
int v18; // eax
__int64 v19; // rsi
__int64 v20; // r8
_DWORD *v21; // rdx
_DWORD *v22; // rcx
int v23; // eax
__int64 v24; // rdx
__int64 v25; // rdi
__int64 v26; // rcx
int v27; // eax
__int64 v28; // rdx
__int64 v29; // rcx
int v30; // eax
__int64 v31; // rdx
__int64 v32; // rcx
int v33; // eax
_DWORD *v34; // r8
__int64 v35; // rdx
__int64 v36; // rdi
__int64 v37; // rcx
int v38; // eax
_DWORD *v39; // rdx
int v40; // er12
int v41; // eax
int v42; // ebx
int v43; // ebp
int v44; // er14
int v45; // eax
signed int v46; // ebx
signed int j; // er15
float v48; // xmm1_4
float v49; // xmm0_4
__int64 v50; // r8
int k; // eax
signed int v52; // edx
_BOOL8 v53; // rsi
int v54; // eax
char s[64]; // [rsp+10h] [rbp-878h] BYREF
struct sigaction buf[13]; // [rsp+50h] [rbp-838h] BYREF

for ( i = 0; i != 64; ++i )
{
memset(&buf[0].sa_mask, 0, 0x90uLL);
v6 = i;
buf[0].sa_handler = (__sighandler_t)terminate;
sigaction(v6, buf, 0LL);
}
dma_reg = map_peripheral(0, 0x24uLL);
pwm_reg = map_peripheral(0, 0x28uLL);
clk_reg = map_peripheral(0, 0xA8uLL);
gpio_reg = map_peripheral(0, 0xB4uLL);
v7 = (__int64)mmap(0LL, 0x43000uLL, 3, 24609, -1, 0LL);
virtbase = v7;
v8 = v7;
if ( v7 != -1 )
{
v9 = v7 & 0xFFF;
if ( (v7 & 0xFFF) == 0 )
{
printf("Virtual memory mapped at %p\n", (const void *)v7);
page_map = (__int64)malloc(0x430uLL);
if ( page_map )
{
v10 = getpid();
sprintf(s, "/proc/%d/pagemap", v10);
v11 = open(s, 0);
v12 = v11;
if ( v11 >= 0 )
{
v13 = (unsigned __int64)virtbase >> 9;
v14 = lseek(v11, (unsigned __int64)virtbase >> 9, 0);
v15 = page_map;
if ( v13 != v14 )
fatal("Failed to seek on %s: %m\n", (char)s);
while ( 1 )
{
v16 = 16 * v9;
v17 = (_BYTE *)(virtbase + (v9 << 12));
*(_QWORD *)(v15 + 16 * v9) = v17;
*v17 = 0;
if ( read(v12, buf, 8uLL) != 8 )
break;
if ( ((__int64)buf[0].sa_handler & 0x80000000000000LL) != 0 )
fatal("Page %d not present (pfn 0x%016llx)\n", v9);
v15 = page_map;
++v9;
*(_DWORD *)(page_map + v16 + 8) = (LODWORD(buf[0].sa_handler) << 12) | 0x40000000;
if ( v9 == 67 )
{
v18 = *(_DWORD *)gpio_reg;
BYTE1(v18) = BYTE1(*(_DWORD *)gpio_reg) & 0x8F | 0x40;
*(_DWORD *)gpio_reg = v18;
*(_DWORD *)(clk_reg + 112) = 1509949446;
udelay(100LL);
v19 = virtbase;
ctl = virtbase;
v20 = virtbase + 256000;
v21 = (_DWORD *)virtbase;
*(_DWORD *)(clk_reg + 112) = 1509949974;
v22 = (_DWORD *)v20;
do
{
*v22 = 1509969920;
*v21 = 67108872;
v23 = mem_virt_to_phys(v22, v19, v21, v22 + 1);
*(_DWORD *)(v24 + 8) = 2114982004;
v25 = v24 + 32;
v24 += 64LL;
*(_DWORD *)(v24 - 60) = v23;
*(_DWORD *)(v24 - 52) = 4;
*(_DWORD *)(v24 - 48) = 0;
v27 = mem_virt_to_phys(v25, v19, v24, v26);
*(_DWORD *)(v28 - 32) = 67436616;
*(_DWORD *)(v28 - 44) = v27;
v30 = mem_virt_to_phys(v19, v19, v28, v29);
*(_DWORD *)(v31 - 24) = 2116075544;
*(_DWORD *)(v31 - 28) = v30;
*(_DWORD *)(v31 - 20) = 4;
*(_DWORD *)(v31 - 16) = 0;
v33 = mem_virt_to_phys(v31, v19, v31, v32);
*(v21 - 3) = v33;
}
while ( v34 != v21 );
*(_DWORD *)(v19 + 255988) = mem_virt_to_phys(v19, v19, v21, v22);
*(_DWORD *)pwm_reg = 0;
udelay(10LL);
*(_DWORD *)(clk_reg + 160) = 1509949446;
udelay(100LL);
*(_DWORD *)(clk_reg + 164) = 1509969920;
udelay(100LL);
*(_DWORD *)(clk_reg + 160) = 1509949462;
udelay(100LL);
*(_DWORD *)(pwm_reg + 16) = 454;
udelay(10LL);
*(_DWORD *)(pwm_reg + 8) = -2147479793;
udelay(10LL);
*(_DWORD *)pwm_reg = 64;
udelay(10LL);
*(_DWORD *)pwm_reg = 33;
udelay(10LL);
*(_DWORD *)dma_reg = 0x80000000;
udelay(10LL);
v35 = dma_reg;
v36 = ctl;
*(_DWORD *)dma_reg = 6;
v38 = mem_virt_to_phys(v36, v19, v35, v37);
v39[1] = v38;
v39[8] = 7;
*v39 = 277348353;
if ( argc <= 1 )
{
v40 = 0;
}
else
{
v40 = open(argv[1], 114);
if ( v40 < 0 )
fatal("Failed to open .wav file\n", 114);
}
v41 = read(v40, buf, 0x800uLL);
if ( v41 >= 0 )
{
if ( v41 > 45 )
{
v42 = ctl;
v43 = v41 >> 1;
v44 = 22;
while ( 1 )
{
usleep(0x2710u);
v45 = mem_phys_to_virt(*(_DWORD *)(dma_reg + 4));
v46 = (unsigned int)(v42 - virtbase) >> 6;
for ( j = ((unsigned int)(v45 - virtbase) >> 6)
- v46
+ ((int)(((unsigned int)(v45 - virtbase) >> 6) - v46) < 0 ? 0xFA0 : 0); j > 9; j -= 10 )
{
v48 = (double)*((__int16 *)&buf[0].sa_handler + v44) * 0.0000152587890625 * 25.0;
v49 = floorf(v48);
v50 = ctl;
for ( k = 0; k != 10; ++k )
{
v52 = v46 + 1;
v53 = (int)((float)(v48 - (float)(int)v49) * 10.0) > k;
if ( v46 == 3999 )
v52 = 0;
*(_DWORD *)(v50 + 4LL * v46 + 256000) = (int)v49 + v53 + 1509969920;
v46 = v52;
}
if ( v43 <= ++v44 )
{
v54 = read(v40, buf, 0x800uLL);
if ( v54 < 0 )
fatal("Error reading data: %m\n", (char)buf);
v43 = v54 >> 1;
if ( !(v54 >> 1) )
terminate(0LL);
v44 = 0;
}
}
v42 = virtbase + (v46 << 6);
}
}
fatal("Initial read of .wav file too short\n", (char)buf);
}
fatal("Failed to read .wav file\n", (char)buf);
}
}
fatal("Failed to read %s: %m\n", (char)s);
}
fatal("Failed to open %s: %m\n", (char)s);
}
fatal("Failed to malloc page_map: %m\n", v8);
}
fatal("Virtual address is not page aligned\n", v7);
}
fatal("Failed to mmap physical pages: %m\n", -1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
int v6; // edi
__int64 v7; // rax
char v8; // si
__int64 v9; // rbp
unsigned int v10; // eax
int v11; // eax
int v12; // er15
unsigned __int64 v13; // r12
__off_t v14; // rax
__int64 v15; // rcx
__int64 v16; // r12
_BYTE *v17; // rax
int v18; // eax
__int64 v19; // rsi
__int64 v20; // r8
_DWORD *v21; // rdx
_DWORD *v22; // rcx
int v23; // eax
__int64 v24; // rdx
__int64 v25; // rdi
__int64 v26; // rcx
int v27; // eax
__int64 v28; // rdx
__int64 v29; // rcx
int v30; // eax
__int64 v31; // rdx
__int64 v32; // rcx
int v33; // eax
_DWORD *v34; // r8
__int64 v35; // rdx
__int64 v36; // rdi
__int64 v37; // rcx
int v38; // eax
_DWORD *v39; // rdx
int v40; // er12
int v41; // eax
int v42; // ebx
int v43; // ebp
int v44; // er14
int v45; // eax
signed int v46; // ebx
signed int j; // er15
float v48; // xmm1_4
float v49; // xmm0_4
__int64 v50; // r8
int k; // eax
signed int v52; // edx
_BOOL8 v53; // rsi
int v54; // eax
char s[64]; // [rsp+10h] [rbp-878h] BYREF
struct sigaction buf[13]; // [rsp+50h] [rbp-838h] BYREF

for ( i = 0; i != 64; ++i )
{
memset(&buf[0].sa_mask, 0, 0x90uLL);
v6 = i;
buf[0].sa_handler = (__sighandler_t)terminate;
sigaction(v6, buf, 0LL);
}
dma_reg = func11(0, 0x24uLL);
pwm_reg = func11(0, 0x28uLL);
clk_reg = func11(0, 0xA8uLL);
gpio_reg = func11(0, 0xB4uLL);
v7 = (__int64)mmap(0LL, 0x43000uLL, 3, 24609, -1, 0LL);
virtbase = v7;
v8 = v7;
if ( v7 != -1 )
{
v9 = v7 & 0xFFF;
if ( (v7 & 0xFFF) == 0 )
{
printf("Virtual memory mapped at %p\n", (const void *)v7);
page_map = (__int64)malloc(0x430uLL);
if ( page_map )
{
v10 = getpid();
sprintf(s, "/proc/%d/pagemap", v10);
v11 = open(s, 0);
v12 = v11;
if ( v11 >= 0 )
{
v13 = (unsigned __int64)virtbase >> 9;
v14 = lseek(v11, (unsigned __int64)virtbase >> 9, 0);
v15 = page_map;
if ( v13 != v14 )
func10("Failed to seek on %s: %m\n", (char)s);
while ( 1 )
{
v16 = 16 * v9;
v17 = (_BYTE *)(virtbase + (v9 << 12));
*(_QWORD *)(v15 + 16 * v9) = v17;
*v17 = 0;
if ( read(v12, buf, 8uLL) != 8 )
break;
if ( ((__int64)buf[0].sa_handler & 0x80000000000000LL) != 0 )
func10("Page %d not present (pfn 0x%016llx)\n", v9);
v15 = page_map;
++v9;
*(_DWORD *)(page_map + v16 + 8) = (LODWORD(buf[0].sa_handler) << 12) | 0x40000000;
if ( v9 == 67 )
{
v18 = *(_DWORD *)gpio_reg;
BYTE1(v18) = BYTE1(*(_DWORD *)gpio_reg) & 0x8F | 0x40;
*(_DWORD *)gpio_reg = v18;
*(_DWORD *)(clk_reg + 112) = 1509949446;
func8(100LL);
v19 = virtbase;
ctl = virtbase;
v20 = virtbase + 256000;
v21 = (_DWORD *)virtbase;
*(_DWORD *)(clk_reg + 112) = 1509949974;
v22 = (_DWORD *)v20;
do
{
*v22 = 1509969920;
*v21 = 67108872;
v23 = func7(v22, v19, v21, v22 + 1);
*(_DWORD *)(v24 + 8) = 2114982004;
v25 = v24 + 32;
v24 += 64LL;
*(_DWORD *)(v24 - 60) = v23;
*(_DWORD *)(v24 - 52) = 4;
*(_DWORD *)(v24 - 48) = 0;
v27 = func7(v25, v19, v24, v26);
*(_DWORD *)(v28 - 32) = 67436616;
*(_DWORD *)(v28 - 44) = v27;
v30 = func7(v19, v19, v28, v29);
*(_DWORD *)(v31 - 24) = 2116075544;
*(_DWORD *)(v31 - 28) = v30;
*(_DWORD *)(v31 - 20) = 4;
*(_DWORD *)(v31 - 16) = 0;
v33 = func7(v31, v19, v31, v32);
*(v21 - 3) = v33;
}
while ( v34 != v21 );
*(_DWORD *)(v19 + 255988) = func7(v19, v19, v21, v22);
*(_DWORD *)pwm_reg = 0;
func8(10LL);
*(_DWORD *)(clk_reg + 160) = 1509949446;
func8(100LL);
*(_DWORD *)(clk_reg + 164) = 1509969920;
func8(100LL);
*(_DWORD *)(clk_reg + 160) = 1509949462;
func8(100LL);
*(_DWORD *)(pwm_reg + 16) = 454;
func8(10LL);
*(_DWORD *)(pwm_reg + 8) = -2147479793;
func8(10LL);
*(_DWORD *)pwm_reg = 64;
func8(10LL);
*(_DWORD *)pwm_reg = 33;
func8(10LL);
*(_DWORD *)dma_reg = 0x80000000;
func8(10LL);
v35 = dma_reg;
v36 = ctl;
*(_DWORD *)dma_reg = 6;
v38 = func7(v36, v19, v35, v37);
v39[1] = v38;
v39[8] = 7;
*v39 = 277348353;
if ( argc <= 1 )
{
v40 = 0;
}
else
{
v40 = open(argv[1], 114);
if ( v40 < 0 )
func10("Failed to open .wav file\n", 114);
}
v41 = read(v40, buf, 0x800uLL);
if ( v41 >= 0 )
{
if ( v41 > 45 )
{
v42 = ctl;
v43 = v41 >> 1;
v44 = 22;
while ( 1 )
{
usleep(0x2710u);
v45 = func12(*(_DWORD *)(dma_reg + 4));
v46 = (unsigned int)(v42 - virtbase) >> 6;
for ( j = ((unsigned int)(v45 - virtbase) >> 6)
- v46
+ ((int)(((unsigned int)(v45 - virtbase) >> 6) - v46) < 0 ? 0xFA0 : 0); j > 9; j -= 10 )
{
v48 = (double)*((__int16 *)&buf[0].sa_handler + v44) * 0.0000152587890625 * 25.0;
v49 = floorf(v48);
v50 = ctl;
for ( k = 0; k != 10; ++k )
{
v52 = v46 + 1;
v53 = (int)((float)(v48 - (float)(int)v49) * 10.0) > k;
if ( v46 == 3999 )
v52 = 0;
*(_DWORD *)(v50 + 4LL * v46 + 256000) = (int)v49 + v53 + 1509969920;
v46 = v52;
}
if ( v43 <= ++v44 )
{
v54 = read(v40, buf, 0x800uLL);
if ( v54 < 0 )
func10("Error reading data: %m\n", (char)buf);
v43 = v54 >> 1;
if ( !(v54 >> 1) )
func9(0LL);
v44 = 0;
}
}
v42 = virtbase + (v46 << 6);
}
}
func10("Initial read of .wav file too short\n", (char)buf);
}
func10("Failed to read .wav file\n", (char)buf);
}
}
func10("Failed to read %s: %m\n", (char)s);
}
func10("Failed to open %s: %m\n", (char)s);
}
func10("Failed to malloc page_map: %m\n", v8);
}
func10("Virtual address is not page aligned\n", v7);
}
func10("Failed to mmap physical pages: %m\n", -1);
}

original_code:
int
main(int argc, char **argv)
{
  int i, fd, pid, freq_ctl;
  char pagemap_fn[64];
  for (i = 0; i < 64; i++) {
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.__sigaction_handler.sa_handler = terminate;
    sigaction(i, &sa, ((void *)0));
  }
  freq_ctl = ((float)(500000000 / 100000000)) * ( 1 << 12 );
  dma_reg = map_peripheral(0x20007000, 0x24);
  pwm_reg = map_peripheral(0x2020C000, 0x28);
  clk_reg = map_peripheral(0x20101000, 0xA8);
  gpio_reg = map_peripheral(0x20200000, 0xB4);
  virtbase = mmap(((void *)0), ((sizeof(struct control_data_s) + 4096 - 1) >> 12) * 4096, 0x1|0x2,
      0x01|0x20|0x04000|0x02000,
      -1, 0);
  if (virtbase == ((void *) -1))
    fatal("Failed to mmap physical pages: %m\n");
  if ((unsigned long)virtbase & (4096 -1))
    fatal("Virtual address is not page aligned\n");
  printf("Virtual memory mapped at %p\n", virtbase);
  page_map = malloc(((sizeof(struct control_data_s) + 4096 - 1) >> 12) * sizeof(*page_map));
  if (page_map == 0)
    fatal("Failed to malloc page_map: %m\n");
  pid = getpid();
  sprintf(pagemap_fn, "/proc/%d/pagemap", pid);
  fd = open(pagemap_fn, 00);
  if (fd < 0)
    fatal("Failed to open %s: %m\n", pagemap_fn);
  if (lseek(fd, (unsigned long)virtbase >> 9, 0) != (unsigned long)virtbase >> 9)
    fatal("Failed to seek on %s: %m\n", pagemap_fn);
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    uint64_t pfn;
    page_map[i].virtaddr = virtbase + i * 4096;
    page_map[i].virtaddr[0] = 0;
    if (read(fd, &pfn, sizeof(pfn)) != sizeof(pfn))
      fatal("Failed to read %s: %m\n", pagemap_fn);
    if ((pfn >> 55)&0xfbf != 0x10c)
      fatal("Page %d not present (pfn 0x%016llx)\n", i, pfn);
    page_map[i].physaddr = (uint32_t)pfn << 12 | 0x40000000;
  }
  gpio_reg[(0x00/4)] = (gpio_reg[(0x00/4)] & ~(7 << 12)) | (4 << 12);
  clk_reg[(0x70/4)] = 0x5A << 24 | 6;
  udelay(100);
  clk_reg[(0x70/4)] = 0x5A << 24 | 1 << 9 | 1 << 4 | 6;
  ctl = (struct control_data_s *)virtbase;
  dma_cb_t *cbp = ctl->cb;
  uint32_t phys_sample_dst = 0x7e101074;
  uint32_t phys_pwm_fifo_addr = 0x7e20c000 + 0x18;
  for (i = 0; i < 4000; i++) {
    ctl->sample[i] = 0x5a << 24 | freq_ctl;
    cbp->info = (1<<26) | (1<<3);
    cbp->src = mem_virt_to_phys(ctl->sample + i);
    cbp->dst = phys_sample_dst;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
    cbp->info = (1<<26) | (1<<3) | (1<<6) | ((5)<<16);
    cbp->src = mem_virt_to_phys(virtbase);
    cbp->dst = phys_pwm_fifo_addr;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
  }
  cbp--;
  cbp->next = mem_virt_to_phys(virtbase);
  pwm_reg[(0x00/4)] = 0;
  udelay(10);
  clk_reg[40] = 0x5A000006;
  udelay(100);
  clk_reg[41] = 0x5A000000 | (5<<12);
  udelay(100);
  clk_reg[40] = 0x5A000016;
  udelay(100);
  pwm_reg[(0x10/4)] = 454;
  udelay(10);
  pwm_reg[(0x08/4)] = (1<<31) | ((15<<8)|(15<<0));
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<6);
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<5) | (1<<0);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<31);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<2) | (1<<1);
  dma_reg[(0x04/4)] = mem_virt_to_phys(ctl->cb);
  dma_reg[(0x20/4)] = 7;
  dma_reg[(0x00/4)] = 0x10880001;
  fd = 0;
  if (argc > 1) {
          fd = open(argv[1], 'r');
    if (fd < 0)
      fatal("Failed to open .wav file\n");
  }
        short data[1024];
        int data_len = read(fd, data, sizeof(data));
  if (data_len < 0)
    fatal("Failed to read .wav file\n");
  data_len /= 2;
  if (data_len < 23)
    fatal("Initial read of .wav file too short\n");
  uint32_t last_cb = (uint32_t)ctl->cb;
  int data_index = 22;
  for (;;) {
    usleep(10000);
    uint32_t cur_cb = mem_phys_to_virt(dma_reg[(0x04/4)]);
    int last_sample = (last_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int this_sample = (cur_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int free_slots = this_sample - last_sample;
    if (free_slots < 0)
      free_slots += 4000;
    while (free_slots >= 10) {
      float dval = (float)(data[data_index])/65536.0 * 25.0;
      int intval = (int)((floor)(dval));
      int frac = (int)((dval - (float)intval) * 10.0);
      int j;
      for (j = 0; j < 10; j++) {
        ctl->sample[last_sample++] = (0x5A << 24 | freq_ctl) + (frac > j ? intval + 1 : intval);
        if (last_sample == 4000)
          last_sample = 0;
      }
      free_slots -= 10;
      if (++data_index >= data_len) {
              data_len = read(fd, data, sizeof(data));
        data_index = 0;
        if (data_len < 0)
          fatal("Error reading data: %m\n");
        data_len /= 2;
        if (data_len == 0)
          terminate(0);
      }
    }
    last_cb = (uint32_t)virtbase + last_sample * sizeof(dma_cb_t) * 2;
  }
  terminate(0);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int i; int fd; int pid; int freq_ctl;
  char pagemap_fn[64];
  for (i = 0; i < 64; i++) {
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.__sigaction_handler.sa_handler = terminate;
    sigaction(i, &sa, ((void *)0));
  }
  freq_ctl = ((float)(500000000 / 100000000)) * ( 1 << 12 );
  dma_reg = map_peripheral(0x20007000, 0x24);
  pwm_reg = map_peripheral(0x2020C000, 0x28);
  clk_reg = map_peripheral(0x20101000, 0xA8);
  gpio_reg = map_peripheral(0x20200000, 0xB4);
  virtbase = mmap(((void *)0), ((sizeof(struct control_data_s) + 4096 - 1) >> 12) * 4096, 0x1|0x2,
      0x01|0x20|0x04000|0x02000,
      -1, 0);
  if (virtbase == ((void *) -1))
    fatal("Failed to mmap physical pages: %m\n");
  if ((unsigned long)virtbase & (4096 -1))
    fatal("Virtual address is not page aligned\n");
  printf("Virtual memory mapped at %p\n", virtbase);
  page_map = malloc(((sizeof(struct control_data_s) + 4096 - 1) >> 12) * sizeof(*page_map));
  if (page_map == 0)
    fatal("Failed to malloc page_map: %m\n");
  pid = getpid();
  sprintf(pagemap_fn, "/proc/%d/pagemap", pid);
  fd = open(pagemap_fn, 00);
  if (fd < 0)
    fatal("Failed to open %s: %m\n", pagemap_fn);
  if (lseek(fd, (unsigned long)virtbase >> 9, 0) != (unsigned long)virtbase >> 9)
    fatal("Failed to seek on %s: %m\n", pagemap_fn);
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    unsigned long pfn;
    page_map[i].virtaddr = virtbase + i * 4096;
    page_map[i].virtaddr[0] = 0;
    if (read(fd, &pfn, sizeof(pfn)) != sizeof(pfn))
      fatal("Failed to read %s: %m\n", pagemap_fn);
    if ((pfn >> 55)&0xfbf != 0x10c)
      fatal("Page %d not present (pfn 0x%016llx)\n", i, pfn);
    page_map[i].physaddr = (unsigned int)pfn << 12 | 0x40000000;
  }
  gpio_reg[(0x00/4)] = (gpio_reg[(0x00/4)] & ~(7 << 12)) | (4 << 12);
  clk_reg[(0x70/4)] = 0x5A << 24 | 6;
  udelay(100);
  clk_reg[(0x70/4)] = 0x5A << 24 | 1 << 9 | 1 << 4 | 6;
  ctl = (struct control_data_s *)virtbase;
  struct dma_cb_t *cbp = ctl->cb;
  unsigned int phys_sample_dst = 0x7e101074;
  unsigned int phys_pwm_fifo_addr = 0x7e20c000 + 0x18;
  for (i = 0; i < 4000; i++) {
    ctl->sample[i] = 0x5a << 24 | freq_ctl;
    cbp->info = (1<<26) | (1<<3);
    cbp->src = mem_virt_to_phys(ctl->sample + i);
    cbp->dst = phys_sample_dst;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
    cbp->info = (1<<26) | (1<<3) | (1<<6) | ((5)<<16);
    cbp->src = mem_virt_to_phys(virtbase);
    cbp->dst = phys_pwm_fifo_addr;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
  }
  cbp--;
  cbp->next = mem_virt_to_phys(virtbase);
  pwm_reg[(0x00/4)] = 0;
  udelay(10);
  clk_reg[40] = 0x5A000006;
  udelay(100);
  clk_reg[41] = 0x5A000000 | (5<<12);
  udelay(100);
  clk_reg[40] = 0x5A000016;
  udelay(100);
  pwm_reg[(0x10/4)] = 454;
  udelay(10);
  pwm_reg[(0x08/4)] = (1<<31) | ((15<<8)|(15<<0));
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<6);
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<5) | (1<<0);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<31);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<2) | (1<<1);
  dma_reg[(0x04/4)] = mem_virt_to_phys(ctl->cb);
  dma_reg[(0x20/4)] = 7;
  dma_reg[(0x00/4)] = 0x10880001;
  fd = 0;
  if (argc > 1) {
          fd = open(argv[1], 'r');
    if (fd < 0)
      fatal("Failed to open .wav file\n");
  }
        short data[1024];
        int data_len = read(fd, data, sizeof(data));
  if (data_len < 0)
    fatal("Failed to read .wav file\n");
  data_len /= 2;
  if (data_len < 23)
    fatal("Initial read of .wav file too short\n");
  unsigned int last_cb = (uint32_t)ctl->cb;
  int data_index = 22;
  for (;;) {
    usleep(10000);
    unsigned int cur_cb = mem_phys_to_virt(dma_reg[(0x04/4)]);
    int last_sample = (last_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int this_sample = (cur_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int free_slots = this_sample - last_sample;
    if (free_slots < 0)
      free_slots += 4000;
    while (free_slots >= 10) {
      float dval = (float)(data[data_index])/65536.0 * 25.0;
      int intval = (int)((floor)(dval));
      int frac = (int)((dval - (float)intval) * 10.0);
      int j;
      for (j = 0; j < 10; j++) {
        ctl->sample[last_sample++] = (0x5A << 24 | freq_ctl) + (frac > j ? intval + 1 : intval);
        if (last_sample == 4000)
          last_sample = 0;
      }
      free_slots -= 10;
      if (++data_index >= data_len) {
              data_len = read(fd, data, sizeof(data));
        data_index = 0;
        if (data_len < 0)
          fatal("Error reading data: %m\n");
        data_len /= 2;
        if (data_len == 0)
          terminate(0);
      }
    }
    last_cb = (unsigned int)virtbase + last_sample * sizeof(dma_cb_t) * 2;
  }
  terminate(0);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: fd int

variable_types: pid int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types:
pfn
unsigned long

variable_types: struct dma_cb_t { unsigned int info; } *

variable_types:
data
short [1024]

variable_types: dval float

variable_types: intval int

variable_types: frac int

variable_types: j int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct control_data_s { struct dma_cb_t cb[-1]; unsigned int sample[4000]; }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
__int64 __fastcall mem_virt_to_phys(int a1)
{
return *(_DWORD *)(page_map + 16LL * ((unsigned int)(a1 - virtbase) >> 12) + 8) + ((a1 - (_DWORD)virtbase) & 0xFFFu);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return *(_DWORD *)(page_map + 16LL * ((unsigned int)(a1 - virtbase) >> 12) + 8) + ((a1 - (_DWORD)virtbase) & 0xFFFu);
}

original_code:
static uint32_t
mem_virt_to_phys(void *virt)
{
  uint32_t offset = (uint8_t *)virt - virtbase;
  return page_map[offset >> 12].physaddr + (offset % 4096);
}

canonical_original_code:
static unsigned int mem_virt_to_phys(void *virt)
{
  unsigned int offset = (uint8_t *)virt - virtbase;
  return page_map[offset >> 12].physaddr + (offset % 4096);
}

variable_types:
virt
void *

variable_types:
offset
unsigned int


udelay

decompiled_code:
int __fastcall udelay(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 1000 * a1;
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 1000 * a1;
return nanosleep(&v2, 0LL);
}

original_code:
static void
udelay(int us)
{
  struct timespec ts = { 0, us * 1000 };
  nanosleep(&ts, ((void *)0));
}

variable_types: us int

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


terminate

decompiled_code:
void __noreturn terminate()
{
if ( dma_reg )
{
*(_DWORD *)dma_reg = 0x80000000;
udelay(10);
}
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
if ( dma_reg )
{
*(_DWORD *)dma_reg = 0x80000000;
func8(10);
}
exit(1);
}

original_code:
static void
terminate(int dummy)
{
  if (dma_reg) {
    dma_reg[(0x00/4)] = (1<<31);
    udelay(10);
  }
  exit(1);
}

variable_types: dummy int


fatal

decompiled_code:
void __noreturn fatal(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(_bss_start, format, arg);
terminate();
}

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

va_start(arg, format);
vfprintf(_bss_start, format, arg);
func9();
}

original_code:
static void
fatal(char *fmt, ...)
{
  va_list ap;
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
  terminate(0);
}

canonical_original_code:
static void fatal(char *fmt, ...)
{
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
  terminate(0);
}

variable_types:
fmt
char *


decompiled_code:
void *__fastcall map_peripheral(unsigned int a1, size_t len)
{
int v2; // eax
int v3; // ebp
void *v4; // r13

v2 = open("/dev/mem", 2);
if ( v2 < 0 )
fatal("Failed to open /dev/mem: %m\n");
v3 = v2;
v4 = mmap(0LL, (unsigned int)len, 3, 1, v2, a1);
if ( v4 == (void *)-1LL )
fatal("Failed to map peripheral at 0x%08x: %m\n", a1);
close(v3);
return v4;
}

canonical_decompiled_code:
void *__fastcall func11(unsigned int a1, size_t len)
{
int v2; // eax
int v3; // ebp
void *v4; // r13

v2 = open("/dev/mem", 2);
if ( v2 < 0 )
func10("Failed to open /dev/mem: %m\n");
v3 = v2;
v4 = mmap(0LL, (unsigned int)len, 3, 1, v2, a1);
if ( v4 == (void *)-1LL )
func10("Failed to map peripheral at 0x%08x: %m\n", a1);
close(v3);
return v4;
}

original_code:
static void *
map_peripheral(uint32_t base, uint32_t len)
{
  int fd = open("/dev/mem", 02);
  void * vaddr;
  if (fd < 0)
    fatal("Failed to open /dev/mem: %m\n");
  vaddr = mmap(((void *)0), len, 0x1|0x2, 0x01, fd, base);
  if (vaddr == ((void *) -1))
    fatal("Failed to map peripheral at 0x%08x: %m\n", base);
  close(fd);
  return vaddr;
}

canonical_original_code:
static void *map_peripheral(unsigned int base, unsigned int len)
{
  int fd = open("/dev/mem", 02);
  void *vaddr;
  if (fd < 0)
    fatal("Failed to open /dev/mem: %m\n");
  vaddr = mmap(((void *)0), len, 0x1|0x2, 0x01, fd, base);
  if (vaddr == ((void *) -1))
    fatal("Failed to map peripheral at 0x%08x: %m\n", base);
  close(fd);
  return vaddr;
}

variable_types:
base
unsigned int

variable_types:
len
unsigned int

variable_types:
vaddr
void *


decompiled_code:
__int64 __fastcall mem_phys_to_virt(unsigned int a1)
{
__int64 v1; // rsi
int v2; // edi
int v3; // edx
_DWORD *i; // rax

v1 = a1;
v2 = a1 & 0xFFF;
v3 = 0;
for ( i = (_DWORD *)(page_map + 8); *i != (v1 & 0xFFFFF000); i += 4 )
{
if ( ++v3 == 67 )
fatal("Failed to reverse map phys addr %08x\n", v1);
}
return (unsigned int)(virtbase + v2 + (v3 << 12));
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1)
{
__int64 v1; // rsi
int v2; // edi
int v3; // edx
_DWORD *i; // rax

v1 = a1;
v2 = a1 & 0xFFF;
v3 = 0;
for ( i = (_DWORD *)(page_map + 8); *i != (v1 & 0xFFFFF000); i += 4 )
{
if ( ++v3 == 67 )
func10("Failed to reverse map phys addr %08x\n", v1);
}
return (unsigned int)(virtbase + v2 + (v3 << 12));
}

original_code:
static uint32_t
mem_phys_to_virt(uint32_t phys)
{
  uint32_t pg_offset = phys & (4096 - 1);
  uint32_t pg_addr = phys - pg_offset;
  int i;
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    if (page_map[i].physaddr == pg_addr) {
      return (uint32_t)virtbase + i * 4096 + pg_offset;
    }
  }
  fatal("Failed to reverse map phys addr %08x\n", phys);
  return 0;
}

canonical_original_code:
static unsigned int mem_phys_to_virt(unsigned int phys)
{
  unsigned int pg_offset = phys & (4096 - 1);
  unsigned int pg_addr = phys - pg_offset;
  int i;
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    if (page_map[i].physaddr == pg_addr) {
      return (unsigned int)virtbase + i * 4096 + pg_offset;
    }
  }
  fatal("Failed to reverse map phys addr %08x\n", phys);
  return 0;
}

variable_types:
phys
unsigned int

variable_types: i int


repo: coobird/pigeon-vm

decompiled_code:
void __fastcall place_entry(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rcx

v4 = a3 + 16LL * a4;
*(_QWORD *)v4 = a1;
*(_DWORD *)(v4 + 8) = a2;
}

original_code:
void place_entry(char* varname, int address, ENTRY* entries, int entry_num) {
 entries[entry_num].varname = varname;
 entries[entry_num].value = address;
}

canonical_original_code:
void place_entry(char *varname, int address, struct ENTRY *entries, int entry_num) {
 entries[entry_num].varname = varname;
 entries[entry_num].value = address;
}

variable_types:
varname
char *

variable_types: address int

variable_types: struct ENTRY { char *varname; int value; } *


decompiled_code:
__int64 __fastcall get_entry(char *s1, __int64 a2, int a3)
{
__int64 v3; // r12
__int64 v4; // rbx

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = a2 + 16LL * (unsigned int)(a3 - 1) + 16;
v4 = a2;
do
{
if ( !strcmp(s1, *(const char **)v4) )
return *(unsigned int *)(v4 + 8);
v4 += 16LL;
}
while ( v4 != v3 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1, __int64 a2, int a3)
{
__int64 v3; // r12
__int64 v4; // rbx

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = a2 + 16LL * (unsigned int)(a3 - 1) + 16;
v4 = a2;
do
{
if ( !strcmp(s1, *(const char **)v4) )
return *(unsigned int *)(v4 + 8);
v4 += 16LL;
}
while ( v4 != v3 );
return 0xFFFFFFFFLL;
}

original_code:
int get_entry(char* varname, ENTRY* entries, int entry_size) {
 int i = 0;
 for (i = 0; i < entry_size; i++) {
  if (strcmp(varname, entries[i].varname) == 0) {
   return entries[i].value;
  }
 }
 return -1;
}

canonical_original_code:
int get_entry(char *varname, struct ENTRY *entries, int entry_size) {
 int i = 0;
 for (i = 0; i < entry_size; i++) {
  if (strcmp(varname, entries[i].varname) == 0) {
   return entries[i].value;
  }
 }
 return -1;
}

user_defined_types: struct ENTRY { char *varname; int value; }


parse

original_code:
void parse(char* line, STATE* state) {
 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
  return;
 }
 int address;
 char opcode[16];
 char value_buffer[16];
 int value;
 sscanf(line, "%i\t%s\t%s", &address, opcode, value_buffer);
 if (value_buffer[0] == '$') {
  value = get_entry(value_buffer, entries, entry_size);
 } else {
  value = atoi(value_buffer);
 }
 int opcode_value;
 if (strncmp("DATA", opcode, 4) == 0) {
  if (opcode[4] == '$') {
   char* varname = malloc(12);
   int i = 0;
   for (i = 0; opcode[i + 4] != '\0' || i > 12; i++) {
    varname[i] = opcode[i + 4];
   }
   varname[i] = '\0';
   place_entry(varname, address, entries, entry_size++);
  }
  state->mem[address] = value;
  return;
 }
 else if (strcmp("ADD", opcode) == 0)
  opcode_value = 0;
 else if (strcmp("SUB", opcode) == 0)
  opcode_value = 1;
 else if (strcmp("AND", opcode) == 0)
  opcode_value = 2;
 else if (strcmp("OR", opcode) == 0)
  opcode_value = 3;
 else if (strcmp("XOR", opcode) == 0)
  opcode_value = 4;
 else if (strcmp("NOT", opcode) == 0)
  opcode_value = 5;
 else if (strcmp("LDM", opcode) == 0)
  opcode_value = 6;
 else if (strcmp("LDI", opcode) == 0)
  opcode_value = 7;
 else if (strcmp("STR", opcode) == 0)
  opcode_value = 8;
 else if (strcmp("JMP", opcode) == 0)
  opcode_value = 9;
 else if (strcmp("JMZ", opcode) == 0)
  opcode_value = 10;
 else if (strcmp("JMN", opcode) == 0)
  opcode_value = 11;
 else if (strcmp("CALL", opcode) == 0)
  opcode_value = 12;
 else if (strcmp("RET", opcode) == 0)
  opcode_value = 13;
 else if (strcmp("PUSH", opcode) == 0)
  opcode_value = 14;
 else if (strcmp("POP", opcode) == 0)
  opcode_value = 15;
 else {
  fprintf(stderr, "Unknown opcode: %s\n", opcode);
  return;
 }
 state->mem[address] = (opcode_value << 12) + value;
}

canonical_original_code:
void parse(char *line, struct state *state) {
 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
  return;
 }
 int address;
 char opcode[16];
 char value_buffer[16];
 int value;
 sscanf(line, "%i\t%s\t%s", &address, opcode, value_buffer);
 if (value_buffer[0] == '$') {
  value = get_entry(value_buffer, entries, entry_size);
 } else {
  value = atoi(value_buffer);
 }
 int opcode_value;
 if (strncmp("DATA", opcode, 4) == 0) {
  if (opcode[4] == '$') {
   char *varname = malloc(12);
   int i = 0;
   for (i = 0; opcode[i + 4] != '\0' || i > 12; i++) {
    varname[i] = opcode[i + 4];
   }
   varname[i] = '\0';
   place_entry(varname, address, entries, entry_size++);
  }
  state->mem[address] = value;
  return;
 }
 else if (strcmp("ADD", opcode) == 0)
  opcode_value = 0;
 else if (strcmp("SUB", opcode) == 0)
  opcode_value = 1;
 else if (strcmp("AND", opcode) == 0)
  opcode_value = 2;
 else if (strcmp("OR", opcode) == 0)
  opcode_value = 3;
 else if (strcmp("XOR", opcode) == 0)
  opcode_value = 4;
 else if (strcmp("NOT", opcode) == 0)
  opcode_value = 5;
 else if (strcmp("LDM", opcode) == 0)
  opcode_value = 6;
 else if (strcmp("LDI", opcode) == 0)
  opcode_value = 7;
 else if (strcmp("STR", opcode) == 0)
  opcode_value = 8;
 else if (strcmp("JMP", opcode) == 0)
  opcode_value = 9;
 else if (strcmp("JMZ", opcode) == 0)
  opcode_value = 10;
 else if (strcmp("JMN", opcode) == 0)
  opcode_value = 11;
 else if (strcmp("CALL", opcode) == 0)
  opcode_value = 12;
 else if (strcmp("RET", opcode) == 0)
  opcode_value = 13;
 else if (strcmp("PUSH", opcode) == 0)
  opcode_value = 14;
 else if (strcmp("POP", opcode) == 0)
  opcode_value = 15;
 else {
  fprintf(stderr, "Unknown opcode: %s\n", opcode);
  return;
 }
 state->mem[address] = (opcode_value << 12) + value;
}

variable_types:
line
char *

variable_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; } *

variable_types:
opcode
char [16]

variable_types: value int


decompiled_code:
void output_state_quiet()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void output_state_quiet(STATE* state) {
}

canonical_original_code:
void output_state_quiet(struct state *state) {
}

user_defined_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; }


decompiled_code:
int __fastcall output_state_json(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

original_code:
void output_state_json(STATE* state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

canonical_original_code:
void output_state_json(struct state *state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

variable_types:
opcode
char [5]


decompiled_code:
int __fastcall output_state_text(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"pc: %4i opcode: %s operand: %4i acc: %5i csp: %2i dsp: %2i\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"pc: %4i opcode: %s operand: %4i acc: %5i csp: %2i dsp: %2i\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

original_code:
void output_state_text(STATE* state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("pc: %4i  opcode: %s operand: %4i  acc: %5i  csp: %2i  dsp: %2i\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

canonical_original_code:
void output_state_text(struct state *state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("pc: %4i  opcode: %s operand: %4i  acc: %5i  csp: %2i  dsp: %2i\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", a1);
puts("Options:");
puts(" -h Displays this help");
puts(" -t Outputs execution states in text format (default)");
puts(" -j Outputs execution states in JSON format");
puts(" -q Suppress execution state output");
puts(" -o Outputs the final accumulator state");
puts(" -v Verbose output");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", a1);
puts("Options:");
puts(" -h Displays this help");
puts(" -t Outputs execution states in text format (default)");
puts(" -j Outputs execution states in JSON format");
puts(" -q Suppress execution state output");
puts(" -o Outputs the final accumulator state");
puts(" -v Verbose output");
return putchar(10);
}

original_code:
void print_usage(char* command) {
 printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", command);
 printf("Options:\n");
 printf("    -h    Displays this help\n");
 printf("    -t    Outputs execution states in text format (default)\n");
 printf("    -j    Outputs execution states in JSON format\n");
 printf("    -q    Suppress execution state output\n");
 printf("    -o    Outputs the final accumulator state\n");
 printf("    -v    Verbose output\n");
 printf("\n");
}

canonical_original_code:
void print_usage(char *command) {
 printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", command);
 printf("Options:\n");
 printf("    -h    Displays this help\n");
 printf("    -t    Outputs execution states in text format (default)\n");
 printf("    -j    Outputs execution states in JSON format\n");
 printf("    -q    Suppress execution state output\n");
 printf("    -o    Outputs the final accumulator state\n");
 printf("    -v    Verbose output\n");
 printf("\n");
}

variable_types:
command
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
int v7; // eax
void *v9; // rbp
int v10; // [rsp+Ch] [rbp-206Ch]
char v11[2]; // [rsp+10h] [rbp-2068h] BYREF
__int16 v12; // [rsp+12h] [rbp-2066h]

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
v10 = 0;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "ovqjth");
if ( v7 == -1 )
{
if ( v6 > 1 )
{
v5 = -1;
fwrite("Can only speficy one of -j, -t, or -q.\n", 1uLL, 0x27uLL, stderr);
}
else
{
v9 = output_state_text;
if ( v3 )
v9 = output_state_json;
if ( v4 )
v9 = output_state_quiet;
if ( v10 )
{
puts("Initializing VM state.");
init_vm(v11);
puts("Loading instructions from stdin.");
load_memory(stdin, v11, 1LL);
puts("Beginning execution.");
run(v11, v9);
puts("Finished execution.");
}
else
{
init_vm(v11);
load_memory(stdin, v11, 0LL);
run(v11, v9);
}
if ( v5 )
{
v5 = 0;
printf("%i\n", (unsigned int)v12);
}
}
}
else
{
switch ( v7 )
{
case 'h':
v5 = 0;
print_usage(*argv);
break;
case 'j':
++v6;
v3 = 1;
continue;
case 'o':
v5 = 1;
continue;
case 'q':
++v6;
v4 = 1;
continue;
case 't':
++v6;
continue;
case 'v':
v10 = 1;
continue;
default:
v5 = -1;
break;
}
}
break;
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // er12
int v6; // ebp
int v7; // eax
void *v9; // rbp
int v10; // [rsp+Ch] [rbp-206Ch]
char v11[2]; // [rsp+10h] [rbp-2068h] BYREF
__int16 v12; // [rsp+12h] [rbp-2066h]

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
v10 = 0;
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "ovqjth");
if ( v7 == -1 )
{
if ( v6 > 1 )
{
v5 = -1;
fwrite("Can only speficy one of -j, -t, or -q.\n", 1uLL, 0x27uLL, stderr);
}
else
{
v9 = output_state_text;
if ( v3 )
v9 = output_state_json;
if ( v4 )
v9 = output_state_quiet;
if ( v10 )
{
puts("Initializing VM state.");
init_vm(v11);
puts("Loading instructions from stdin.");
load_memory(stdin, v11, 1LL);
puts("Beginning execution.");
run(v11, v9);
puts("Finished execution.");
}
else
{
init_vm(v11);
load_memory(stdin, v11, 0LL);
run(v11, v9);
}
if ( v5 )
{
v5 = 0;
printf("%i\n", (unsigned int)v12);
}
}
}
else
{
switch ( v7 )
{
case 'h':
v5 = 0;
func3(*argv);
break;
case 'j':
++v6;
v3 = 1;
continue;
case 'o':
v5 = 1;
continue;
case 'q':
++v6;
v4 = 1;
continue;
case 't':
++v6;
continue;
case 'v':
v10 = 1;
continue;
default:
v5 = -1;
break;
}
}
break;
}
return v5;
}

original_code:
int main(int argc, char** argv) {
 void* callback = &output_state_text;
 int f_verbose = 0, f_json = 0, f_text = 0, f_quiet = 0, f_output = 0;
 int cnt_outflags = 0;
 int opt;
 while ((opt = getopt(argc, argv, "ovqjth")) != -1) {
  switch (opt) {
   case 'v':
    f_verbose = 1;
    break;
   case 'o':
    f_output = 1;
    break;
   case 'q':
    f_quiet = 1;
    cnt_outflags++;
    break;
   case 'j':
    f_json = 1;
    cnt_outflags++;
    break;
   case 't':
    f_text = 1;
    cnt_outflags++;
    break;
   case 'h':
    print_usage(argv[0]);
    return 0;
   default:
    return -1;
    ;
  }
 }
 if (cnt_outflags > 1) {
  fprintf(stderr, "Can only speficy one of -j, -t, or -q.\n");
  return -1;
 }
 if (f_json) callback = &output_state_json;
 if (f_quiet) callback = &output_state_quiet;
 STATE state;
 if (f_verbose) printf("Initializing VM state.\n");
 init_vm(&state);
 if (f_verbose) printf("Loading instructions from stdin.\n");
 load_memory(stdin, &state, f_verbose);
 if (f_verbose) printf("Beginning execution.\n");
 run(&state, callback);
 if (f_verbose) printf("Finished execution.\n");
 if (f_output) printf("%i\n", state.acc);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 void *callback = &output_state_text;
 int f_verbose = 0; int f_json = 0; int f_text = 0; int f_quiet = 0; int f_output = 0;
 int cnt_outflags = 0;
 int opt;
 while ((opt = getopt(argc, argv, "ovqjth")) != -1) {
  switch (opt) {
   case 'v':
    f_verbose = 1;
    break;
   case 'o':
    f_output = 1;
    break;
   case 'q':
    f_quiet = 1;
    cnt_outflags++;
    break;
   case 'j':
    f_json = 1;
    cnt_outflags++;
    break;
   case 't':
    f_text = 1;
    cnt_outflags++;
    break;
   case 'h':
    print_usage(argv[0]);
    return 0;
   default:
    return -1;
    ;
  }
 }
 if (cnt_outflags > 1) {
  fprintf(stderr, "Can only speficy one of -j, -t, or -q.\n");
  return -1;
 }
 if (f_json) callback = &output_state_json;
 if (f_quiet) callback = &output_state_quiet;
 struct state state;
 if (f_verbose) printf("Initializing VM state.\n");
 init_vm(&state);
 if (f_verbose) printf("Loading instructions from stdin.\n");
 load_memory(stdin, &state, f_verbose);
 if (f_verbose) printf("Beginning execution.\n");
 run(&state, callback);
 if (f_verbose) printf("Finished execution.\n");
 if (f_output) printf("%i\n", state.acc);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
callback
void *

variable_types: opt int

variable_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; }


fetch

decompiled_code:
__int16 __fastcall fetch(_WORD *a1)
{
__int16 result; // ax

result = __ROL2__(a1[(unsigned __int16)a1[2] + 20], 4);
*a1 = result;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func1(_WORD *a1)
{
__int16 result; // ax

result = __ROL2__(a1[(unsigned __int16)a1[2] + 20], 4);
*a1 = result;
return result;
}

original_code:
void fetch(STATE* state) {
 unsigned short instr = state->mem[state->pc];
 state->opcode = (instr & 0xF000) >> 12;
 state->operand = instr & 0x0FFF;
}

canonical_original_code:
void fetch(struct state *state) {
 unsigned short instr = state->mem[state->pc];
 state->opcode = (instr & 0xF000) >> 12;
 state->operand = instr & 0x0FFF;
}

variable_types:
instr
unsigned short


execute

original_code:
void execute(STATE* state) {
 unsigned short opcode = state->opcode;
 unsigned short operand = state->operand;
 unsigned short value = state->mem[operand];
 switch (opcode) {
  case 0:
   state->acc += value;
   (state->pc)++;
   break;
  case 1:
   state->acc -= value;
   (state->pc)++;
   break;
  case 2:
   state->acc &= value;
   (state->pc)++;
   break;
  case 3:
   state->acc |= value;
   (state->pc)++;
   break;
  case 4:
   state->acc ^= value;
   (state->pc)++;
   break;
  case 5:
   state->acc = ~state->acc;
   (state->pc)++;
   break;
  case 6:
   state->acc = value;
   (state->pc)++;
   break;
  case 7:
   state->acc = operand;
   (state->pc)++;
   break;
  case 8:
   state->mem[operand] = state->acc;
   (state->pc)++;
   break;
  case 9:
   state->pc = operand;
   break;
  case 10:
   if (state->acc == 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 11:
   if (state->acc < 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 12:
   state->cstack[state->csp] = ++(state->pc);
   (state->csp)++;
   state->pc = operand;
   break;
  case 13:
   state->pc = state->cstack[--(state->csp)];
   break;
  case 14:
   state->dstack[(state->dsp)++] = state->acc;
   (state->pc)++;
   break;
  case 15:
   state->acc = state->dstack[--(state->dsp)];
   (state->pc)++;
   break;
 }
}

canonical_original_code:
void execute(struct state *state) {
 unsigned short opcode = state->opcode;
 unsigned short operand = state->operand;
 unsigned short value = state->mem[operand];
 switch (opcode) {
  case 0:
   state->acc += value;
   (state->pc)++;
   break;
  case 1:
   state->acc -= value;
   (state->pc)++;
   break;
  case 2:
   state->acc &= value;
   (state->pc)++;
   break;
  case 3:
   state->acc |= value;
   (state->pc)++;
   break;
  case 4:
   state->acc ^= value;
   (state->pc)++;
   break;
  case 5:
   state->acc = ~state->acc;
   (state->pc)++;
   break;
  case 6:
   state->acc = value;
   (state->pc)++;
   break;
  case 7:
   state->acc = operand;
   (state->pc)++;
   break;
  case 8:
   state->mem[operand] = state->acc;
   (state->pc)++;
   break;
  case 9:
   state->pc = operand;
   break;
  case 10:
   if (state->acc == 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 11:
   if (state->acc < 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 12:
   state->cstack[state->csp] = ++(state->pc);
   (state->csp)++;
   state->pc = operand;
   break;
  case 13:
   state->pc = state->cstack[--(state->csp)];
   break;
  case 14:
   state->dstack[(state->dsp)++] = state->acc;
   (state->pc)++;
   break;
  case 15:
   state->acc = state->dstack[--(state->dsp)];
   (state->pc)++;
   break;
 }
}

variable_types:
opcode
unsigned short

variable_types:
operand
unsigned short

variable_types:
value
unsigned short


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

if ( a1 )
{
switch ( a1 )
{
case 1:
*(_DWORD *)a2 = 4347219;
break;
case 2:
*(_DWORD *)a2 = 4476481;
break;
case 3:
result = 21071LL;
strcpy(a2, "OR");
break;
case 4:
*(_DWORD *)a2 = 5394264;
break;
case 5:
*(_DWORD *)a2 = 5525326;
break;
case 6:
*(_DWORD *)a2 = 5063756;
break;
case 7:
*(_DWORD *)a2 = 4801612;
break;
case 8:
*(_DWORD *)a2 = 5395539;
break;
case 9:
*(_DWORD *)a2 = 5262666;
break;
case 10:
*(_DWORD *)a2 = 5918026;
break;
case 11:
*(_DWORD *)a2 = 5131594;
break;
case 12:
strcpy(a2, "CALL");
break;
case 13:
*(_DWORD *)a2 = 5522770;
break;
case 14:
strcpy(a2, "PUSH");
break;
case 15:
*(_DWORD *)a2 = 5263184;
break;
}
}
else
{
*(_DWORD *)a2 = 4473921;
}
return result;
}

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

if ( a1 )
{
switch ( a1 )
{
case 1:
*(_DWORD *)a2 = 4347219;
break;
case 2:
*(_DWORD *)a2 = 4476481;
break;
case 3:
result = 21071LL;
strcpy(a2, "OR");
break;
case 4:
*(_DWORD *)a2 = 5394264;
break;
case 5:
*(_DWORD *)a2 = 5525326;
break;
case 6:
*(_DWORD *)a2 = 5063756;
break;
case 7:
*(_DWORD *)a2 = 4801612;
break;
case 8:
*(_DWORD *)a2 = 5395539;
break;
case 9:
*(_DWORD *)a2 = 5262666;
break;
case 10:
*(_DWORD *)a2 = 5918026;
break;
case 11:
*(_DWORD *)a2 = 5131594;
break;
case 12:
strcpy(a2, "CALL");
break;
case 13:
*(_DWORD *)a2 = 5522770;
break;
case 14:
strcpy(a2, "PUSH");
break;
case 15:
*(_DWORD *)a2 = 5263184;
break;
}
}
else
{
*(_DWORD *)a2 = 4473921;
}
return result;
}

original_code:
void get_mnemonic(unsigned short opcode, char mnemonic[5]) {
 if (opcode == 0)
  strcpy(mnemonic, "ADD");
 else if (opcode == 1)
  strcpy(mnemonic, "SUB");
 else if (opcode == 2)
  strcpy(mnemonic, "AND");
 else if (opcode == 3)
  strcpy(mnemonic, "OR");
 else if (opcode == 4)
  strcpy(mnemonic, "XOR");
 else if (opcode == 5)
  strcpy(mnemonic, "NOT");
 else if (opcode == 6)
  strcpy(mnemonic, "LDM");
 else if (opcode == 7)
  strcpy(mnemonic, "LDI");
 else if (opcode == 8)
  strcpy(mnemonic, "STR");
 else if (opcode == 9)
  strcpy(mnemonic, "JMP");
 else if (opcode == 10)
  strcpy(mnemonic, "JMZ");
 else if (opcode == 11)
  strcpy(mnemonic, "JMN");
 else if (opcode == 12)
  strcpy(mnemonic, "CALL");
 else if (opcode == 13)
  strcpy(mnemonic, "RET");
 else if (opcode == 14)
  strcpy(mnemonic, "PUSH");
 else if (opcode == 15)
  strcpy(mnemonic, "POP");
}

variable_types:
mnemonic
char [5]


run

original_code:
void run(STATE* state, void (*callback)(STATE*)) {
 int instruction_count = 0;
 const int MAX_INST_COUNT = 500;
 while (state->pc < 4096 - 1 && instruction_count < MAX_INST_COUNT) {
  instruction_count++;
  fetch(state);
  callback(state);
  execute(state);
 }
 callback(state);
}

canonical_original_code:
void run(struct state *state, void (*callback)(struct state *)) {
 int instruction_count = 0;
 int MAX_INST_COUNT = 500;
 while (state->pc < 4096 - 1 && instruction_count < MAX_INST_COUNT) {
  instruction_count++;
  fetch(state);
  callback(state);
  execute(state);
 }
 callback(state);
}

variable_types:
callback
void (*)(struct state *)