decompiled_code:
_QWORD *process_label()
{
char *v0; // r12
int v1; // ebx
char *v2; // rbp
size_t v3; // rax
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *result; // rax

v0 = (char *)get_next_token();
v1 = strlen(v0);
v2 = (char *)calloc(v1, 1uLL);
strncpy(v2, v0 + 1, v1 - 1);
v3 = strlen(v2);
printf("Raw label: %s -- Label name: %s -- last char: %d\n", v0, v2, (unsigned int)v2[v3 - 1]);
free(v0);
v4 = calloc(0x18uLL, 1uLL);
*v4 = v2;
v5 = v4;
result = calloc(0x38uLL, 1uLL);
*(_DWORD *)result = 2;
result[3] = v5;
return result;
}

canonical_decompiled_code:
_QWORD *func3()
{
char *v0; // r12
int v1; // ebx
char *v2; // rbp
size_t v3; // rax
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *result; // rax

v0 = (char *)get_next_token();
v1 = strlen(v0);
v2 = (char *)calloc(v1, 1uLL);
strncpy(v2, v0 + 1, v1 - 1);
v3 = strlen(v2);
printf("Raw label: %s -- Label name: %s -- last char: %d\n", v0, v2, (unsigned int)v2[v3 - 1]);
free(v0);
v4 = calloc(0x18uLL, 1uLL);
*v4 = v2;
v5 = v4;
result = calloc(0x38uLL, 1uLL);
*(_DWORD *)result = 2;
result[3] = v5;
return result;
}

original_code:
codelist_entry *process_label(){
 char *label_raw = get_next_token();
 int raw_size = strlen(label_raw);
 char *label_name = calloc(raw_size, sizeof(char));
 strncpy(label_name, label_raw + 1, raw_size - 1);
 printf("Raw label: %s -- Label name: %s -- last char: %d\n", label_raw, label_name, (int)label_name[strlen(label_name) - 1]);
 free(label_raw);
 code_label *label = calloc(sizeof(code_label), 1);
 label->label_name = label_name;
 codelist_entry *cl = calloc(sizeof(codelist_entry), 1);
 cl->type = CODELIST_ENTRY_TYPE_LABEL;
 cl->label = label;
 return cl;
}

canonical_original_code:
struct codelist_entry_t *process_label(){
 char *label_raw = get_next_token();
 int raw_size = strlen(label_raw);
 char *label_name = calloc(raw_size, sizeof(char));
 strncpy(label_name, label_raw + 1, raw_size - 1);
 printf("Raw label: %s -- Label name: %s -- last char: %d\n", label_raw, label_name, (int)label_name[strlen(label_name) - 1]);
 free(label_raw);
 struct code_label_t *label = calloc(sizeof(code_label), 1);
 label->label_name = label_name;
 struct codelist_entry_t *cl = calloc(sizeof(codelist_entry), 1);
 cl->type = CODELIST_ENTRY_TYPE_LABEL;
 cl->label = label;
 return cl;
}

return_type: struct codelist_entry_t { enum codelist_entry_type { CODELIST_ENTRY_TYPE_OP=0, CODELIST_ENTRY_TYPE_SPECIAL_OP=1, CODELIST_ENTRY_TYPE_LABEL=2, CODELIST_ENTRY_TYPE_DATA=3 } type; unsigned short op; struct token_val_t { enum vm_loc_type { LOC_REGISTER=0, LOC_MEMORY=1, LOC_REGISTER_MEMORY=2, LOC_REGISTER_MEMORY_OFFSET=3, LOC_PUSHPOP=4, LOC_PEEK=5, LOC_PICK=6, LOC_LITERAL=7, LOC_SP=8, LOC_PC=9, LOC_EX=10, LOC_LABEL=11, LOC_LABEL_MEM=12, LOC_IA=13 } type; unsigned short reg; unsigned short loc; unsigned short val; char *label_name; } *aval; struct token_val_t *bval; struct code_label_t { char *label_name; unsigned short loc; struct code_label_t *next; } *label; int *data; unsigned short data_size; unsigned short codeloc; struct codelist_entry_t *next; } *


decompiled_code:
int __fastcall get_data_from_nums(char *s, __int64 a2)
{
char *v2; // rbp
int v3; // ebx
char *v4; // rax

v2 = (char *)calloc(4uLL, 8uLL);
v3 = sscanf(s, "%i %i %i %i %i %i %i %i", v2, v2 + 4, v2 + 8, v2 + 12, v2 + 16, v2 + 20, v2 + 24, v2 + 28);
LODWORD(v4) = v3 & 0xFFFFFFF7;
if ( (v3 & 0xFFFFFFF7) != 0 )
{
v4 = (char *)realloc(v2, 2LL * v3);
v2 = v4;
}
else if ( !v3 )
{
LODWORD(v4) = puts("Error when parsing numbers from DAT field");
}
*(_QWORD *)(a2 + 32) = v2;
*(_WORD *)(a2 + 40) = v3;
return (int)v4;
}

canonical_decompiled_code:
int __fastcall func4(char *s, __int64 a2)
{
char *v2; // rbp
int v3; // ebx
char *v4; // rax

v2 = (char *)calloc(4uLL, 8uLL);
v3 = sscanf(s, "%i %i %i %i %i %i %i %i", v2, v2 + 4, v2 + 8, v2 + 12, v2 + 16, v2 + 20, v2 + 24, v2 + 28);
LODWORD(v4) = v3 & 0xFFFFFFF7;
if ( (v3 & 0xFFFFFFF7) != 0 )
{
v4 = (char *)realloc(v2, 2LL * v3);
v2 = v4;
}
else if ( !v3 )
{
LODWORD(v4) = puts("Error when parsing numbers from DAT field");
}
*(_QWORD *)(a2 + 32) = v2;
*(_WORD *)(a2 + 40) = v3;
return (int)v4;
}

original_code:
void get_data_from_nums(const char *str, codelist_entry *entry){
 int *data = calloc(sizeof(int), 8);
 int size = sscanf(str, "%i %i %i %i %i %i %i %i",
          data, data + 1, data + 2, data + 3,
          data + 4, data + 5, data + 6, data + 7);
 if(size != 8 && size != 0){
  data = realloc(data, sizeof(short) * size);
 }
 if(size == 0){
  printf("Error when parsing numbers from DAT field\n");
 }
 entry->data = data;
 entry->data_size = size;
}

canonical_original_code:
void get_data_from_nums(char *str, struct codelist_entry_t *entry){
 int *data = calloc(sizeof(int), 8);
 int size = sscanf(str, "%i %i %i %i %i %i %i %i",
          data, data + 1, data + 2, data + 3,
          data + 4, data + 5, data + 6, data + 7);
 if(size != 8 && size != 0){
  data = realloc(data, sizeof(short) * size);
 }
 if(size == 0){
  printf("Error when parsing numbers from DAT field\n");
 }
 entry->data = data;
 entry->data_size = size;
}

variable_types:
str
char *

variable_types: struct codelist_entry_t { enum codelist_entry_type { CODELIST_ENTRY_TYPE_OP=0, CODELIST_ENTRY_TYPE_SPECIAL_OP=1, CODELIST_ENTRY_TYPE_LABEL=2, CODELIST_ENTRY_TYPE_DATA=3 } type; unsigned short op; struct token_val_t { enum vm_loc_type { LOC_REGISTER=0, LOC_MEMORY=1, LOC_REGISTER_MEMORY=2, LOC_REGISTER_MEMORY_OFFSET=3, LOC_PUSHPOP=4, LOC_PEEK=5, LOC_PICK=6, LOC_LITERAL=7, LOC_SP=8, LOC_PC=9, LOC_EX=10, LOC_LABEL=11, LOC_LABEL_MEM=12, LOC_IA=13 } type; unsigned short reg; unsigned short loc; unsigned short val; char *label_name; } *aval; struct token_val_t *bval; struct code_label_t { char *label_name; unsigned short loc; struct code_label_t *next; } *label; int *data; unsigned short data_size; unsigned short codeloc; struct codelist_entry_t *next; } *

variable_types:
data
int *

variable_types: size int

return_type: void

user_defined_types: struct codelist_entry_t { enum codelist_entry_type type; unsigned short op; struct token_val_t *aval; struct token_val_t *bval; struct code_label_t *label; int *data; unsigned short data_size; unsigned short codeloc; struct codelist_entry_t *next; }

user_defined_types: struct token_val_t { enum vm_loc_type type; unsigned short reg; unsigned short loc; unsigned short val; char *label_name; }


original_code:
void get_data_from_quoted_string(char *str, codelist_entry *entry){
 remove_string_ends(str);
 int len = strlen(str);
 int size = ceil(len / 2.0f);
 int *data = calloc(sizeof(int), size);
 int tmp;
 for(int i = 0; i < size; i++){
  tmp = str[i * 2];
  tmp += str[(i * 2) + 1] << 8;
  data[i] = (tmp & 0xFFFF);
 }
 entry->data = data;
 entry->data_size = size;
}

canonical_original_code:
void get_data_from_quoted_string(char *str, struct codelist_entry_t *entry){
 remove_string_ends(str);
 int len = strlen(str);
 int size = ceil(len / 2.0f);
 int *data = calloc(sizeof(int), size);
 int tmp;
 for(int i = 0; i < size; i++){
  tmp = str[i * 2];
  tmp += str[(i * 2) + 1] << 8;
  data[i] = (tmp & 0xFFFF);
 }
 entry->data = data;
 entry->data_size = size;
}

variable_types: len int

variable_types: tmp int

variable_types: i int

user_defined_types: enum codelist_entry_type { CODELIST_ENTRY_TYPE_OP=0, CODELIST_ENTRY_TYPE_SPECIAL_OP=1, CODELIST_ENTRY_TYPE_LABEL=2, CODELIST_ENTRY_TYPE_DATA=3 }

user_defined_types: struct code_label_t { char *label_name; unsigned short loc; struct code_label_t *next; }

user_defined_types: enum vm_loc_type { LOC_REGISTER=0, LOC_MEMORY=1, LOC_REGISTER_MEMORY=2, LOC_REGISTER_MEMORY_OFFSET=3, LOC_PUSHPOP=4, LOC_PEEK=5, LOC_PICK=6, LOC_LITERAL=7, LOC_SP=8, LOC_PC=9, LOC_EX=10, LOC_LABEL=11, LOC_LABEL_MEM=12, LOC_IA=13 }


decompiled_code:
_QWORD *process_dat()
{
const char *v0; // rbx
size_t v1; // r12
char *v2; // rbp
_QWORD *v3; // r12
__int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rbx
int v7; // eax

v0 = (const char *)get_rest_of_orig_line();
v1 = (int)(strlen(v0) - 3);
v2 = (char *)calloc(v1, 1uLL);
strncpy(v2, v0 + 4, v1);
trim(v2);
printf("Dat: %s\n", v2);
v3 = calloc(0x38uLL, 1uLL);
if ( (unsigned __int8)is_quoted_string(v2) )
get_data_from_quoted_string(v2, (__int64)v3);
else
get_data_from_nums(v2, (__int64)v3);
printf("Data: ");
if ( *((_WORD *)v3 + 20) )
{
v4 = 0LL;
do
{
v5 = *(unsigned int *)(v3[4] + 4 * v4++);
printf("0x%04x ", v5);
}
while ( *((unsigned __int16 *)v3 + 20) > (int)v4 );
}
putchar(10);
if ( *((_WORD *)v3 + 20) )
{
v6 = 0LL;
do
{
v7 = *(_DWORD *)(v3[4] + 4 * v6++);
printf("%c%c", BYTE1(v7), (unsigned __int8)v7);
}
while ( *((unsigned __int16 *)v3 + 20) > (int)v6 );
}
putchar(10);
return v3;
}

canonical_decompiled_code:
_QWORD *func6()
{
const char *v0; // rbx
size_t v1; // r12
char *v2; // rbp
_QWORD *v3; // r12
__int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rbx
int v7; // eax

v0 = (const char *)get_rest_of_orig_line();
v1 = (int)(strlen(v0) - 3);
v2 = (char *)calloc(v1, 1uLL);
strncpy(v2, v0 + 4, v1);
trim(v2);
printf("Dat: %s\n", v2);
v3 = calloc(0x38uLL, 1uLL);
if ( (unsigned __int8)is_quoted_string(v2) )
func5(v2, (__int64)v3);
else
func4(v2, (__int64)v3);
printf("Data: ");
if ( *((_WORD *)v3 + 20) )
{
v4 = 0LL;
do
{
v5 = *(unsigned int *)(v3[4] + 4 * v4++);
printf("0x%04x ", v5);
}
while ( *((unsigned __int16 *)v3 + 20) > (int)v4 );
}
putchar(10);
if ( *((_WORD *)v3 + 20) )
{
v6 = 0LL;
do
{
v7 = *(_DWORD *)(v3[4] + 4 * v6++);
printf("%c%c", BYTE1(v7), (unsigned __int8)v7);
}
while ( *((unsigned __int16 *)v3 + 20) > (int)v6 );
}
putchar(10);
return v3;
}

original_code:
codelist_entry *process_dat(){
 char *orig_line = get_rest_of_orig_line();
 int len = strlen(orig_line);
 char *buf = calloc(len - 3, sizeof(char));
 strncpy(buf, orig_line + 4, len - 3);
 trim(buf);
 printf("Dat: %s\n", buf);
 codelist_entry *entry = calloc(sizeof(codelist_entry), 1);
 if(is_quoted_string(buf)){
  get_data_from_quoted_string(buf, entry);
 }else{
  get_data_from_nums(buf, entry);
 }
 printf("Data: ");
 for(int i = 0; i < entry->data_size; i++){
  printf("0x%04x ", entry->data[i]);
 }
 printf("\n");
 for(int i = 0; i < entry->data_size; i++){
  unsigned short cur = entry->data[i];
  printf("%c%c", (cur >> 8), (cur & 0xFF));
 }
 printf("\n");
 return entry;
}

canonical_original_code:
struct codelist_entry_t *process_dat(){
 char *orig_line = get_rest_of_orig_line();
 int len = strlen(orig_line);
 char *buf = calloc(len - 3, sizeof(char));
 strncpy(buf, orig_line + 4, len - 3);
 trim(buf);
 printf("Dat: %s\n", buf);
 struct codelist_entry_t *entry = calloc(sizeof(codelist_entry), 1);
 if(is_quoted_string(buf)){
  get_data_from_quoted_string(buf, entry);
 }else{
  get_data_from_nums(buf, entry);
 }
 printf("Data: ");
 for(int i = 0; i < entry->data_size; i++){
  printf("0x%04x ", entry->data[i]);
 }
 printf("\n");
 for(int i = 0; i < entry->data_size; i++){
  unsigned short cur = entry->data[i];
  printf("%c%c", (cur >> 8), (cur & 0xFF));
 }
 printf("\n");
 return entry;
}

variable_types:
buf
char *

variable_types:
cur
unsigned short


decompiled_code:
__int64 process_op()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 0LL;
}

original_code:
codelist_entry *process_op(){
 return ((void *)0);
}

canonical_original_code:
struct codelist_entry_t *process_op(){
 return ((void *)0);
}


decompiled_code:
_QWORD *process_line()
{
const char *v0; // rbp
size_t v1; // rax
_QWORD *result; // rax
__int64 v3; // rax

eat_whitespace();
v0 = (const char *)*input_file_stack;
v1 = strlen((const char *)*input_file_stack);
printf("Processing line: %s -- len: %d\n", v0, v1);
if ( (unsigned __int8)is_line_blank() || (unsigned __int8)is_line_comment() )
{
puts("Line comment/empty/blank");
return 0LL;
}
if ( (unsigned __int8)is_line_label() )
{
puts("Line is label");
result = process_label();
}
else
{
if ( !(unsigned __int8)is_line_dat() )
{
if ( !(unsigned __int8)is_line_include() )
{
puts("Processing code line");
return (_QWORD *)process_op();
}
puts("Line is include");
v3 = get_include_filename();
open_input_m4(v3);
return 0LL;
}
puts("Line is data");
result = process_dat();
}
return result;
}

canonical_decompiled_code:
_QWORD *func8()
{
const char *v0; // rbp
size_t v1; // rax
_QWORD *result; // rax
__int64 v3; // rax

eat_whitespace();
v0 = (const char *)*input_file_stack;
v1 = strlen((const char *)*input_file_stack);
printf("Processing line: %s -- len: %d\n", v0, v1);
if ( (unsigned __int8)is_line_blank() || (unsigned __int8)is_line_comment() )
{
puts("Line comment/empty/blank");
return 0LL;
}
if ( (unsigned __int8)is_line_label() )
{
puts("Line is label");
result = func3();
}
else
{
if ( !(unsigned __int8)is_line_dat() )
{
if ( !(unsigned __int8)is_line_include() )
{
puts("Processing code line");
return (_QWORD *)func7();
}
puts("Line is include");
v3 = get_include_filename();
open_input_m4(v3);
return 0LL;
}
puts("Line is data");
result = func6();
}
return result;
}

original_code:
codelist_entry *process_line(){
 eat_whitespace();
 codelist_entry *ret = ((void *)0);
 printf("Processing line: %s -- len: %d\n", input_file_stack->line_buffer, (int)strlen(input_file_stack->line_buffer));
 if(!is_line_blank() && !is_line_comment()){
  if(is_line_label()){
   printf("Line is label\n");
   return process_label();
  }else if(is_line_dat()){
   printf("Line is data\n");
   return process_dat();
  }else if(is_line_include()){
   printf("Line is include\n");
   open_input_m4(get_include_filename());
   return ((void *)0);
  }else{
   printf("Processing code line\n");
   return process_op();
  }
 }else{
  printf("Line comment/empty/blank\n");
 }
 return ret;
}

canonical_original_code:
struct codelist_entry_t *process_line(){
 eat_whitespace();
 struct codelist_entry_t *ret = ((void *)0);
 printf("Processing line: %s -- len: %d\n", input_file_stack->line_buffer, (int)strlen(input_file_stack->line_buffer));
 if(!is_line_blank() && !is_line_comment()){
  if(is_line_label()){
   printf("Line is label\n");
   return process_label();
  }else if(is_line_dat()){
   printf("Line is data\n");
   return process_dat();
  }else if(is_line_include()){
   printf("Line is include\n");
   open_input_m4(get_include_filename());
   return ((void *)0);
  }else{
   printf("Processing code line\n");
   return process_op();
  }
 }else{
  printf("Line comment/empty/blank\n");
 }
 return ret;
}


decompiled_code:
void __fastcall compile_file(const char *a1, const char *a2)
{
_QWORD *v2; // rbp

printf("Compiling file: %s into %s\n", a1, a2);
puts("Initializing compiler...");
compile_init();
open_input_m4(a1);
while ( (unsigned __int8)is_input_open() )
{
while ( (unsigned __int8)read_input_line() )
{
v2 = process_line();
if ( v2 )
{
puts("Adding codelist entry...");
add_codelist_entry(v2);
}
}
close_input();
}
close_input();
compile_cleanup();
}

canonical_decompiled_code:
void __fastcall func9(const char *a1, const char *a2)
{
_QWORD *v2; // rbp

printf("Compiling file: %s into %s\n", a1, a2);
puts("Initializing compiler...");
func1();
open_input_m4(a1);
while ( (unsigned __int8)is_input_open() )
{
while ( (unsigned __int8)read_input_line() )
{
v2 = func8();
if ( v2 )
{
puts("Adding codelist entry...");
func2(v2);
}
}
close_input();
}
close_input();
func0();
}

original_code:
void compile_file(const char *src, const char *dest){
 printf("Compiling file: %s into %s\n", src, dest);
 printf("Initializing compiler...\n");
 compile_init();
 open_input_m4(src);
 codelist_entry *entry;
 while(is_input_open()){
  while(read_input_line()){
   entry = process_line();
   if(entry != ((void *)0)){
    printf("Adding codelist entry...\n");
    add_codelist_entry(entry);
   }
  }
  close_input();
 }
 close_input();
 compile_cleanup();
}

canonical_original_code:
void compile_file(char *src, char *dest){
 printf("Compiling file: %s into %s\n", src, dest);
 printf("Initializing compiler...\n");
 compile_init();
 open_input_m4(src);
 struct codelist_entry_t *entry;
 while(is_input_open()){
  while(read_input_line()){
   entry = process_line();
   if(entry != ((void *)0)){
    printf("Adding codelist entry...\n");
    add_codelist_entry(entry);
   }
  }
  close_input();
 }
 close_input();
 compile_cleanup();
}

variable_types:
src
char *

variable_types:
dest
char *


decompiled_code:
bool __fastcall is_whitespace(char a1)
{
bool result; // al

result = 1;
if ( (unsigned __int8)(a1 - 9) > 4u )
result = (a1 & 0xDF) == 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func0(char a1)
{
bool result; // al

result = 1;
if ( (unsigned __int8)(a1 - 9) > 4u )
result = (a1 & 0xDF) == 0;
return result;
}

variable_types: c char


decompiled_code:
__int64 eat_whitespace()
{
_DWORD *v0; // rsi
char *i; // rdx
__int64 v2; // rcx
bool v3; // r8
__int64 v4; // r9
__int64 result; // rax

v0 = input_file_stack;
for ( i = (char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5)); ; i = (char *)(v4 + v2) )
{
v3 = is_whitespace(*i);
result = (unsigned int)(v2 + 1);
if ( !v3 )
break;
v0[5] = result;
}
if ( (_DWORD)v2 )
v0[5] = v2 - 1;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
_DWORD *v0; // rsi
char *i; // rdx
__int64 v2; // rcx
bool v3; // r8
__int64 v4; // r9
__int64 result; // rax

v0 = input_file_stack;
for ( i = (char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5)); ; i = (char *)(v4 + v2) )
{
v3 = func0(*i);
result = (unsigned int)(v2 + 1);
if ( !v3 )
break;
v0[5] = result;
}
if ( (_DWORD)v2 )
v0[5] = v2 - 1;
return result;
}

original_code:
void eat_whitespace(){
 char *cur = input_file_stack->line_buffer +
              input_file_stack->line_buffer_pos;
 while(is_whitespace(*cur)){
  cur = input_file_stack->line_buffer +
   input_file_stack->line_buffer_pos++;
 }
 if(input_file_stack->line_buffer_pos != 0){
  input_file_stack->line_buffer_pos--;
 }
}

variable_types:
cur
char *


decompiled_code:
__int64 parser_get_orig_line()
{
return *((_QWORD *)input_file_stack + 1);
}

canonical_decompiled_code:
__int64 func2()
{
return *((_QWORD *)input_file_stack + 1);
}

original_code:
char *parser_get_orig_line(){
 return input_file_stack->orig_line_buffer;
}

return_type: char *


decompiled_code:
bool is_input_open()
{
return input_file_stack != 0LL;
}

canonical_decompiled_code:
bool func3()
{
return input_file_stack != 0LL;
}

original_code:
_Bool is_input_open(){
 return input_file_stack ? 1 : 0;
}


decompiled_code:
__int64 __fastcall open_input(char *filename)
{
printf("Opening file: %s\n", filename);
return push_input_file(filename);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *filename)
{
printf("Opening file: %s\n", filename);
return func4(filename);
}

original_code:
_Bool open_input(const char* filename){
 printf("Opening file: %s\n", filename);
 return push_input_file(filename);
}

canonical_original_code:
_Bool open_input(char *filename){
 printf("Opening file: %s\n", filename);
 return push_input_file(filename);
}

variable_types:
filename
char *


decompiled_code:
__int64 __fastcall open_input_m4(char *src)
{
size_t v1; // r12
size_t v2; // rbx
__int64 v4; // [rsp+0h] [rbp-218h] BYREF
__int16 v5; // [rsp+8h] [rbp-210h]
char dest[4]; // [rsp+100h] [rbp-118h] BYREF

v4 = 0x2F706D742FLL;
v5 = 0;
v1 = strlen(src);
strncat((char *)&v4, src, v1);
v2 = strlen((const char *)&v4);
*(_DWORD *)((char *)&v4 + v2) = 3435822;
strcpy(dest, "m4 ");
strncat(dest, src, v1);
*(_DWORD *)&dest[strlen(dest)] = 2113056;
strncat(dest, (const char *)&v4, v2 + 3);
system(dest);
return open_input((char *)&v4);
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src)
{
size_t v1; // r12
size_t v2; // rbx
__int64 v4; // [rsp+0h] [rbp-218h] BYREF
__int16 v5; // [rsp+8h] [rbp-210h]
char dest[4]; // [rsp+100h] [rbp-118h] BYREF

v4 = 0x2F706D742FLL;
v5 = 0;
v1 = strlen(src);
strncat((char *)&v4, src, v1);
v2 = strlen((const char *)&v4);
*(_DWORD *)((char *)&v4 + v2) = 3435822;
strcpy(dest, "m4 ");
strncat(dest, src, v1);
*(_DWORD *)&dest[strlen(dest)] = 2113056;
strncat(dest, (const char *)&v4, v2 + 3);
system(dest);
return func5((char *)&v4);
}

original_code:
_Bool open_input_m4(const char* filename){
 char m4_filename[256];
 strncpy(m4_filename, "/tmp/", 10);
 strncat(m4_filename, filename, strlen(filename));
 strncat(m4_filename, ".m4", 10);
 char m4_command[256];
 strncpy(m4_command, "m4 ", 4);
 strncat(m4_command, filename, strlen(filename));
 strncat(m4_command, " > ", 10);
 strncat(m4_command, m4_filename, strlen(m4_filename));
 system(m4_command);
 return open_input(m4_filename);
}

canonical_original_code:
_Bool open_input_m4(char *filename){
 char m4_filename[256];
 strncpy(m4_filename, "/tmp/", 10);
 strncat(m4_filename, filename, strlen(filename));
 strncat(m4_filename, ".m4", 10);
 char m4_command[256];
 strncpy(m4_command, "m4 ", 4);
 strncat(m4_command, filename, strlen(filename));
 strncat(m4_command, " > ", 10);
 strncat(m4_command, m4_filename, strlen(m4_filename));
 system(m4_command);
 return open_input(m4_filename);
}


decompiled_code:
void close_input()
{
puts("Closing ");
pop_input_file();
}

canonical_decompiled_code:
void func8()
{
puts("Closing ");
func7();
}

original_code:
void close_input(){
 printf("Closing \n");
 pop_input_file();
}


decompiled_code:
char *get_next_token()
{
size_t v0; // rcx
const char *v1; // rbp
unsigned int v2; // er12
size_t v3; // rbx
char *v4; // rax
char *result; // rax

eat_whitespace();
v0 = 0LL;
v1 = (const char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5));
do
{
v2 = v0;
v3 = v0;
}
while ( !is_whitespace(v1[v0]) );
printf("Token size: %d\n", v2);
*((_DWORD *)input_file_stack + 5) += v2;
v4 = (char *)calloc(v3, 1uLL);
result = strncpy(v4, v1, v3);
result[v3] = 0;
return result;
}

canonical_decompiled_code:
char *func9()
{
size_t v0; // rcx
const char *v1; // rbp
unsigned int v2; // er12
size_t v3; // rbx
char *v4; // rax
char *result; // rax

func1();
v0 = 0LL;
v1 = (const char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5));
do
{
v2 = v0;
v3 = v0;
}
while ( !func0(v1[v0]) );
printf("Token size: %d\n", v2);
*((_DWORD *)input_file_stack + 5) += v2;
v4 = (char *)calloc(v3, 1uLL);
result = strncpy(v4, v1, v3);
result[v3] = 0;
return result;
}

original_code:
char *get_next_token(){
 eat_whitespace();
 char *start = input_file_stack->line_buffer + input_file_stack->line_buffer_pos;
 int size = 0;
 while(!is_whitespace(start[size])){
  size++;
 }
 printf("Token size: %d\n", size);
 input_file_stack->line_buffer_pos += size;
 char *token = calloc(size, sizeof(char));
 strncpy(token, start, size);
 token[size] = '\0';
 return token;
}

variable_types:
start
char *

variable_types:
token
char *


decompiled_code:
char *get_remaining_line()
{
const char *v0; // rbp
size_t v1; // rbx
char *v2; // rax
char *result; // rax

eat_whitespace();
v0 = (const char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5));
v1 = (int)strlen(v0);
v2 = (char *)calloc(v1, 1uLL);
result = strncpy(v2, v0, v1);
result[v1] = 0;
return result;
}

canonical_decompiled_code:
char *func10()
{
const char *v0; // rbp
size_t v1; // rbx
char *v2; // rax
char *result; // rax

func1();
v0 = (const char *)(*(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5));
v1 = (int)strlen(v0);
v2 = (char *)calloc(v1, 1uLL);
result = strncpy(v2, v0, v1);
result[v1] = 0;
return result;
}

original_code:
char *get_remaining_line(){
 eat_whitespace();
 char *start = input_file_stack->line_buffer + input_file_stack->line_buffer_pos;
 int size = strlen(start);
 char *out = calloc(size, sizeof(char));
 strncpy(out, start, size);
 out[size] = '\0';
 return out;
}

variable_types:
out
char *


ltrim

decompiled_code:
__int64 __fastcall ltrim(_BYTE *a1)
{
_BYTE *v1; // rbx
const unsigned __int16 **v2; // rax
char *v3; // rdx
const unsigned __int16 *v4; // rsi
__int64 v5; // rcx
__int64 result; // rax
__int64 v7; // rcx

v1 = a1;
v2 = _ctype_b_loc();
v3 = a1;
v4 = *v2;
while ( 1 )
{
v5 = *v3;
result = v5;
if ( (v4[v5] & 0x2000) == 0 )
break;
++v3;
}
if ( (_BYTE)v5 )
{
v7 = 0LL;
do
{
a1[v7++] = result;
result = (unsigned __int8)v3[v7];
}
while ( (_BYTE)result );
v1 = &a1[v7];
}
*v1 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1)
{
_BYTE *v1; // rbx
const unsigned __int16 **v2; // rax
char *v3; // rdx
const unsigned __int16 *v4; // rsi
__int64 v5; // rcx
__int64 result; // rax
__int64 v7; // rcx

v1 = a1;
v2 = _ctype_b_loc();
v3 = a1;
v4 = *v2;
while ( 1 )
{
v5 = *v3;
result = v5;
if ( (v4[v5] & 0x2000) == 0 )
break;
++v3;
}
if ( (_BYTE)v5 )
{
v7 = 0LL;
do
{
a1[v7++] = result;
result = (unsigned __int8)v3[v7];
}
while ( (_BYTE)result );
v1 = &a1[v7];
}
*v1 = 0;
return result;
}

original_code:
void ltrim(char *s){
 char *cpy = s;
 int i = 0;
 while(((*__ctype_b_loc ())[(int) ((*cpy))] & (unsigned short int) _ISspace)){
  cpy++;
 }
 while(cpy[i]){
  s[i] = cpy[i];
  i++;
 }
 s[i] = '\0';
}

canonical_original_code:
void ltrim(char *s){
 char *cpy = s;
 int i = 0;
 while(((*__ctype_b_loc ())[(int) ((*cpy))] & (unsigned short) _ISspace)){
  cpy++;
 }
 while(cpy[i]){
  s[i] = cpy[i];
  i++;
 }
 s[i] = '\0';
}

variable_types:
s
char *

variable_types:
cpy
char *


rtrim

decompiled_code:
const unsigned __int16 *__fastcall rtrim(const char *a1)
{
const char *v1; // rbx
const unsigned __int16 **v2; // rcx
const unsigned __int16 *result; // rax

v1 = &a1[strlen(a1) - 1];
v2 = _ctype_b_loc();
while ( 1 )
{
result = *v2;
if ( ((*v2)[*v1] & 0x2000) == 0 )
break;
*v1-- = 0;
}
return result;
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func12(const char *a1)
{
const char *v1; // rbx
const unsigned __int16 **v2; // rcx
const unsigned __int16 *result; // rax

v1 = &a1[strlen(a1) - 1];
v2 = _ctype_b_loc();
while ( 1 )
{
result = *v2;
if ( ((*v2)[*v1] & 0x2000) == 0 )
break;
*v1-- = 0;
}
return result;
}

original_code:
void rtrim(char *s){
 char *end = s + (strlen(s) - 1);
 while(((*__ctype_b_loc ())[(int) ((*end))] & (unsigned short int) _ISspace)){
  *end-- = 0;
 }
}

canonical_original_code:
void rtrim(char *s){
 char *end = s + (strlen(s) - 1);
 while(((*__ctype_b_loc ())[(int) ((*end))] & (unsigned short) _ISspace)){
  *end-- = 0;
 }
}

variable_types:
end
char *


trim

decompiled_code:
__int64 __fastcall trim(char *a1)
{
rtrim(a1);
return ltrim(a1);
}

canonical_decompiled_code:
__int64 __fastcall func13(char *a1)
{
func12(a1);
return func11(a1);
}

original_code:
void trim(char *s){
 rtrim(s);
 ltrim(s);
}


decompiled_code:
bool __fastcall is_digit(char a1, int a2)
{
__int32_t v2; // edx
bool result; // al

v2 = (*_ctype_toupper_loc())[a1];
result = 1;
if ( (unsigned __int8)(a1 - 48) > 9u )
{
result = 0;
if ( a2 == 16 )
result = (unsigned __int8)(v2 - 65) <= 5u;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func14(char a1, int a2)
{
__int32_t v2; // edx
bool result; // al

v2 = (*_ctype_toupper_loc())[a1];
result = 1;
if ( (unsigned __int8)(a1 - 48) > 9u )
{
result = 0;
if ( a2 == 16 )
result = (unsigned __int8)(v2 - 65) <= 5u;
}
return result;
}

original_code:
_Bool is_digit(const char c, const int base){
 char upper_c = toupper(c);
 if((c >= '0' && c <= '9')
   || ((base == 16) &&
     (upper_c >= 'A' &&
     upper_c <= 'F'))){
  return 1;
 }else{
  return 0;
 }
}

canonical_original_code:
_Bool is_digit(char c, int base){
 char upper_c = toupper(c);
 if((c >= '0' && c <= '9')
   || ((base == 16) &&
     (upper_c >= 'A' &&
     upper_c <= 'F'))){
  return 1;
 }else{
  return 0;
 }
}

variable_types: base int


decompiled_code:
bool __fastcall is_square_bracketed(const char *a1)
{
bool result; // al

if ( *a1 == 91 )
result = a1[strlen(a1) - 1] == 93;
else
result = 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func15(const char *a1)
{
bool result; // al

if ( *a1 == 91 )
result = a1[strlen(a1) - 1] == 93;
else
result = 0;
return result;
}

original_code:
_Bool is_square_bracketed(const char *str){
 return (str[0] == '[') && (str[strlen(str) - 1] == ']');
}

canonical_original_code:
_Bool is_square_bracketed(char *str){
 return (str[0] == '[') && (str[strlen(str) - 1] == ']');
}

variable_types:
str
char *


decompiled_code:
bool __fastcall is_quoted_string(const char *a1)
{
bool result; // al

if ( *a1 == 34 )
result = a1[strlen(a1) - 1] == 34;
else
result = 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func16(const char *a1)
{
bool result; // al

if ( *a1 == 34 )
result = a1[strlen(a1) - 1] == 34;
else
result = 0;
return result;
}

original_code:
_Bool is_quoted_string(const char *str){
 return (str[0] == '"') && (str[strlen(str) - 1] == '"');
}

canonical_original_code:
_Bool is_quoted_string(char *str){
 return (str[0] == '"') && (str[strlen(str) - 1] == '"');
}


decompiled_code:
__int64 __fastcall get_string_number_base(_BYTE *a1)
{
unsigned int v1; // er12

v1 = 10;
if ( *a1 == 48 && (*_ctype_toupper_loc())[(char)a1[1]] == 88 )
v1 = 16;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func18(_BYTE *a1)
{
unsigned int v1; // er12

v1 = 10;
if ( *a1 == 48 && (*_ctype_toupper_loc())[(char)a1[1]] == 88 )
v1 = 16;
return v1;
}

original_code:
int get_string_number_base(const char *str){
 if(str[0] == '0' && toupper(str[1]) == 'X'){
  return 16;
 }else{
  return 10;
 }
}

canonical_original_code:
int get_string_number_base(char *str){
 if(str[0] == '0' && toupper(str[1]) == 'X'){
  return 16;
 }else{
  return 10;
 }
}

return_type: int


decompiled_code:
bool __fastcall is_string_number(char *a1)
{
const char *v1; // rbx
int v2; // ebp
int v3; // eax
__int64 v4; // r12
bool result; // al

v1 = a1;
v2 = get_string_number_base(a1);
if ( v2 == 16 )
v1 = a1 + 2;
v3 = strlen(v1);
if ( v3 <= 0 )
return 1;
v4 = (__int64)&v1[v3 - 1 + 1];
do
{
result = is_digit(*v1, v2);
if ( !result )
break;
++v1;
}
while ( v1 != (const char *)v4 );
return result;
}

canonical_decompiled_code:
bool __fastcall func19(char *a1)
{
const char *v1; // rbx
int v2; // ebp
int v3; // eax
__int64 v4; // r12
bool result; // al

v1 = a1;
v2 = func18(a1);
if ( v2 == 16 )
v1 = a1 + 2;
v3 = strlen(v1);
if ( v3 <= 0 )
return 1;
v4 = (__int64)&v1[v3 - 1 + 1];
do
{
result = func14(*v1, v2);
if ( !result )
break;
++v1;
}
while ( v1 != (const char *)v4 );
return result;
}

original_code:
_Bool is_string_number(const char *str){
 int base = get_string_number_base(str);
 if(base == 16){
  str += 2;
 }
 int len = strlen(str);
 for(int i = 0; i < len; i++){
  if(!is_digit(str[i], base)){
   return 0;
  }
 }
 return 1;
}

canonical_original_code:
_Bool is_string_number(char *str){
 int base = get_string_number_base(str);
 if(base == 16){
  str += 2;
 }
 int len = strlen(str);
 for(int i = 0; i < len; i++){
  if(!is_digit(str[i], base)){
   return 0;
  }
 }
 return 1;
}

variable_types: len int


decompiled_code:
__int64 __fastcall get_string_as_number(const char *a1)
{
bool v1; // zf
__int64 result; // rax
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

v1 = sscanf(a1, "%i", &v3) == 1;
result = 0LL;
if ( v1 )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(const char *a1)
{
bool v1; // zf
__int64 result; // rax
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

v1 = sscanf(a1, "%i", &v3) == 1;
result = 0LL;
if ( v1 )
result = v3;
return result;
}

original_code:
int get_string_as_number(const char *str){
 int ret;
 if(sscanf(str, "%i", &ret) == 1){
  return ret;
 }else{
  return 0;
 }
}

canonical_original_code:
int get_string_as_number(char *str){
 int ret;
 if(sscanf(str, "%i", &ret) == 1){
  return ret;
 }else{
  return 0;
 }
}

variable_types: ret int


decompiled_code:
void __fastcall str_to_upper(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = _ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

canonical_decompiled_code:
void __fastcall func21(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = _ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

original_code:
void str_to_upper(char *str){
 char *cur = str;
 while(*cur){
  *cur = toupper(*cur);
  cur++;
 }
}


decompiled_code:
__int64 read_input_line()
{
char *v0; // rbx
__int64 result; // rax
char *v2; // rax
char **v3; // rbx
const char *v4; // rbp
size_t v5; // rax
char **v6; // rax

v0 = fgets(*((char **)input_file_stack + 1), 256, *((FILE **)input_file_stack + 3));
result = 0LL;
if ( v0 )
{
v2 = &v0[(int)strlen(v0) - 1];
if ( *v2 == 10 )
*v2 = 0;
v3 = (char **)input_file_stack;
v4 = (const char *)*((_QWORD *)input_file_stack + 1);
v5 = strlen(v4);
strncpy(*v3, v4, v5 + 1);
v6 = (char **)input_file_stack;
*((_DWORD *)input_file_stack + 5) = 0;
str_to_upper(*v6);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func22()
{
char *v0; // rbx
__int64 result; // rax
char *v2; // rax
char **v3; // rbx
const char *v4; // rbp
size_t v5; // rax
char **v6; // rax

v0 = fgets(*((char **)input_file_stack + 1), 256, *((FILE **)input_file_stack + 3));
result = 0LL;
if ( v0 )
{
v2 = &v0[(int)strlen(v0) - 1];
if ( *v2 == 10 )
*v2 = 0;
v3 = (char **)input_file_stack;
v4 = (const char *)*((_QWORD *)input_file_stack + 1);
v5 = strlen(v4);
strncpy(*v3, v4, v5 + 1);
v6 = (char **)input_file_stack;
*((_DWORD *)input_file_stack + 5) = 0;
func21(*v6);
result = 1LL;
}
return result;
}

original_code:
_Bool read_input_line(){
 char *s = fgets(input_file_stack->orig_line_buffer, 256, input_file_stack->fd);
 if(s){
  int len = strlen(s);
  if(s[len - 1] == '\n'){
   s[len - 1] = 0;
  }
  strncpy(input_file_stack->line_buffer,
      input_file_stack->orig_line_buffer,
      strlen(input_file_stack->orig_line_buffer) + 1);
  input_file_stack->line_buffer_pos = 0;
  str_to_upper(input_file_stack->line_buffer);
  return 1;
 }else{
  return 0;
 }
}


decompiled_code:
__int64 get_rest_of_line()
{
return *(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5);
}

canonical_decompiled_code:
__int64 func23()
{
return *(_QWORD *)input_file_stack + *((unsigned int *)input_file_stack + 5);
}

original_code:
char *get_rest_of_line(){
 return input_file_stack->line_buffer +
  input_file_stack->line_buffer_pos;
}


decompiled_code:
bool is_line_comment()
{
return *(_BYTE *)get_rest_of_line() == 59;
}

canonical_decompiled_code:
bool func24()
{
return *(_BYTE *)func23() == 59;
}

original_code:
_Bool is_line_comment(){
 if(get_rest_of_line()[0] == ';'){
  return 1;
 }else{
  return 0;
 }
}


decompiled_code:
bool is_line_dat()
{
unsigned __int8 *v0; // rdx
int v1; // eax
bool result; // al

v0 = (unsigned __int8 *)get_rest_of_line();
v1 = *v0 - 68;
if ( *v0 == 68 && (v1 = v0[1] - 65, v0[1] == 65) )
result = v0[2] == 84;
else
result = v1 == 0;
return result;
}

canonical_decompiled_code:
bool func25()
{
unsigned __int8 *v0; // rdx
int v1; // eax
bool result; // al

v0 = (unsigned __int8 *)func23();
v1 = *v0 - 68;
if ( *v0 == 68 && (v1 = v0[1] - 65, v0[1] == 65) )
result = v0[2] == 84;
else
result = v1 == 0;
return result;
}

original_code:
_Bool is_line_dat(){
 if(strncmp(get_rest_of_line(), "DAT", 3) == 0){
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_line_include(__int64 a1, __int64 a2, __int64 a3)
{
const void *v3; // rsi
unsigned __int64 v4; // rcx

v3 = (const void *)get_rest_of_line("#INCLUDE", a2, a3, 8LL);
return memcmp(v3, "#INCLUDE", v4) == 0;
}

canonical_decompiled_code:
bool __fastcall func26(__int64 a1, __int64 a2, __int64 a3)
{
const void *v3; // rsi
unsigned __int64 v4; // rcx

v3 = (const void *)func23("#INCLUDE", a2, a3, 8LL);
return memcmp(v3, "#INCLUDE", v4) == 0;
}

original_code:
_Bool is_line_include(){
 if(strncmp(get_rest_of_line(), "#INCLUDE", 8) == 0){
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_line_blank(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return *(_BYTE *)get_rest_of_line(a1, a2, a3, a4) == 0;
}

original_code:
_Bool is_line_blank(){
 return (strlen(get_rest_of_line()) == 0 ? 1 : 0);
}


decompiled_code:
bool __fastcall is_line_label(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return *(_BYTE *)get_rest_of_line(a1, a2, a3, a4) == 58;
}

original_code:
_Bool is_line_label(){
 if(get_rest_of_line()[0] == ':'){
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 get_rest_of_orig_line()
{
return *((_QWORD *)input_file_stack + 1) + *((unsigned int *)input_file_stack + 5);
}

canonical_decompiled_code:
__int64 func29()
{
return *((_QWORD *)input_file_stack + 1) + *((unsigned int *)input_file_stack + 5);
}

original_code:
char *get_rest_of_orig_line(){
 return input_file_stack->orig_line_buffer +
  input_file_stack->line_buffer_pos;
}


decompiled_code:
__int64 get_include_filename()
{
return get_rest_of_orig_line() + 9;
}

canonical_decompiled_code:
__int64 func30()
{
return func29() + 9;
}

original_code:
char *get_include_filename(){
 return get_rest_of_orig_line() + strlen("#INCLUDE ");
}


decompiled_code:
__int64 __fastcall codelist_entry_size(int *a1)
{
int v1; // edx
__int64 result; // rax
int v3; // ebp

v1 = *a1;
if ( *a1 == 1 )
return (unsigned int)token_val_size(*((_QWORD *)a1 + 1), 1LL) + 1;
if ( v1 == 3 )
return *((unsigned __int16 *)a1 + 20);
result = 0LL;
if ( !v1 )
{
v3 = token_val_size(*((_QWORD *)a1 + 1), 1LL) + 1;
result = v3 + (unsigned int)token_val_size(*((_QWORD *)a1 + 2), 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1)
{
int v1; // edx
__int64 result; // rax
int v3; // ebp

v1 = *a1;
if ( *a1 == 1 )
return (unsigned int)token_val_size(*((_QWORD *)a1 + 1), 1LL) + 1;
if ( v1 == 3 )
return *((unsigned __int16 *)a1 + 20);
result = 0LL;
if ( !v1 )
{
v3 = token_val_size(*((_QWORD *)a1 + 1), 1LL) + 1;
result = v3 + (unsigned int)token_val_size(*((_QWORD *)a1 + 2), 0LL);
}
return result;
}

original_code:
int codelist_entry_size(codelist_entry *entry){
 int size = 0;
 switch(entry->type){
 case CODELIST_ENTRY_TYPE_OP:
  size = 1;
  size += token_val_size(entry->aval, 1);
  size += token_val_size(entry->bval, 0);
  break;
 case CODELIST_ENTRY_TYPE_SPECIAL_OP:
  size = 1;
  size += token_val_size(entry->aval, 1);
  break;
 case CODELIST_ENTRY_TYPE_DATA:
  size = entry->data_size;
  break;
 case CODELIST_ENTRY_TYPE_LABEL:
  size = 0;
  break;
 }
 return size;
}

canonical_original_code:
int codelist_entry_size(struct codelist_entry_t *entry){
 int size = 0;
 switch(entry->type){
 case CODELIST_ENTRY_TYPE_OP:
  size = 1;
  size += token_val_size(entry->aval, 1);
  size += token_val_size(entry->bval, 0);
  break;
 case CODELIST_ENTRY_TYPE_SPECIAL_OP:
  size = 1;
  size += token_val_size(entry->aval, 1);
  break;
 case CODELIST_ENTRY_TYPE_DATA:
  size = entry->data_size;
  break;
 case CODELIST_ENTRY_TYPE_LABEL:
  size = 0;
  break;
 }
 return size;
}

variable_types: struct codelist_entry_t { enum codelist_entry_type { CODELIST_ENTRY_TYPE_OP=0, CODELIST_ENTRY_TYPE_SPECIAL_OP=1, CODELIST_ENTRY_TYPE_LABEL=2, CODELIST_ENTRY_TYPE_DATA=3 } type; unsigned short op; struct token_val_t { enum vm_loc_type { LOC_REGISTER=0, LOC_MEMORY=1, LOC_REGISTER_MEMORY=2, LOC_REGISTER_MEMORY_OFFSET=3, LOC_PUSHPOP=4, LOC_PEEK=5, LOC_PICK=6, LOC_LITERAL=7, LOC_SP=8, LOC_PC=9, LOC_EX=10, LOC_LABEL=11, LOC_LABEL_MEM=12, LOC_IA=13 } type; unsigned short reg; unsigned short loc; unsigned short val; char *label_name; } *aval; struct token_val_t *bval; struct code_label_t { char *label_name; unsigned short loc; struct code_label_t *next; } *label; int *data; unsigned short data_size; unsigned short codeloc; struct codelist_entry_t *next; } *

variable_types: size int


decompiled_code:
_BOOL8 __fastcall token_val_size(__int64 a1, char a2)
{
_BOOL8 result; // rax

switch ( *(_DWORD *)a1 )
{
case 1:
case 3:
case 6:
case 0xB:
case 0xC:
result = 1LL;
break;
case 7:
result = 1LL;
if ( a2 )
result = *(_WORD *)(a1 + 8) > 0x1Fu;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( *(_DWORD *)a1 )
{
case 1:
case 3:
case 6:
case 0xB:
case 0xC:
result = 1LL;
break;
case 7:
result = 1LL;
if ( a2 )
result = *(_WORD *)(a1 + 8) > 0x1Fu;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int token_val_size(token_val *val, _Bool a_mode){
 switch(val->type){
 case LOC_REGISTER_MEMORY_OFFSET:
 case LOC_PICK:
 case LOC_MEMORY:
 case LOC_LABEL:
 case LOC_LABEL_MEM:
  return 1;
 case LOC_LITERAL:
  if(a_mode && (val->val <= 0x1f)){
   return 0;
  }else{
   return 1;
  }
 case LOC_REGISTER:
 case LOC_REGISTER_MEMORY:
 case LOC_PUSHPOP:
 case LOC_PEEK:
 case LOC_SP:
 case LOC_PC:
 case LOC_EX:
 case LOC_IA:
  return 0;
 }
 return 0;
}

canonical_original_code:
int token_val_size(struct token_val_t *val, _Bool a_mode){
 switch(val->type){
 case LOC_REGISTER_MEMORY_OFFSET:
 case LOC_PICK:
 case LOC_MEMORY:
 case LOC_LABEL:
 case LOC_LABEL_MEM:
  return 1;
 case LOC_LITERAL:
  if(a_mode && (val->val <= 0x1f)){
   return 0;
  }else{
   return 1;
  }
 case LOC_REGISTER:
 case LOC_REGISTER_MEMORY:
 case LOC_PUSHPOP:
 case LOC_PEEK:
 case LOC_SP:
 case LOC_PC:
 case LOC_EX:
 case LOC_IA:
  return 0;
 }
 return 0;
}

variable_types: struct token_val_t { enum vm_loc_type { LOC_REGISTER=0, LOC_MEMORY=1, LOC_REGISTER_MEMORY=2, LOC_REGISTER_MEMORY_OFFSET=3, LOC_PUSHPOP=4, LOC_PEEK=5, LOC_PICK=6, LOC_LITERAL=7, LOC_SP=8, LOC_PC=9, LOC_EX=10, LOC_LABEL=11, LOC_LABEL_MEM=12, LOC_IA=13 } type; unsigned short reg; unsigned short loc; unsigned short val; char *label_name; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // al
char v5[256]; // [rsp+0h] [rbp-208h] BYREF
char dest[264]; // [rsp+100h] [rbp-108h] BYREF

initscr();
cbreak();
noecho();
do
{
while ( 1 )
{
while ( 1 )
{
wclear(stdscr);
printw("Menu:\n--------------\n1) Compile\n2) Run\nq) Exit\n----------\n> ", argv);
v3 = wgetch(stdscr);
if ( v3 != 49 )
break;
wclear(stdscr);
printw("Compiling: \n");
printw("Enter file to compile (test.dasm) : ");
echo();
wrefresh(stdscr);
wgetnstr(stdscr, v5, 255);
printw("Enter file to output (test.dbin) : ");
wrefresh(stdscr);
wgetnstr(stdscr, dest, 255);
noecho();
if ( !v5[0] )
strncpy(v5, "test.dasm", 0x64uLL);
if ( !dest[0] )
strncpy(dest, "test.dbin", 0x64uLL);
argv = (const char **)v5;
printw("Compiling %s to %s\n", v5, dest);
wrefresh(stdscr);
printw("Press any key to continue");
wrefresh(stdscr);
wgetch(stdscr);
}
if ( v3 != 50 )
break;
wclear(stdscr);
printw("Running: \n");
wrefresh(stdscr);
sleep(1u);
}
}
while ( v3 != 113 );
wclear(stdscr);
printw("Exiting... \n");
wrefresh(stdscr);
endwin();
return 0;
}

original_code:
int main(int argc, char **argv){
 initscr();
 cbreak();
 noecho();
 char input;
 char buf1[256];
 char buf2[256];
 while(1){
  wclear(stdscr);
  printw("Menu:\n--------------\n1) Compile\n2) Run\nq) Exit\n----------\n> ");
  input = wgetch(stdscr);
  if(input == '1'){
   wclear(stdscr);
   printw("Compiling: \n");
   printw("Enter file to compile (test.dasm) : ");
   echo();
   wrefresh(stdscr);
   wgetnstr(stdscr, buf1, (255));
   printw("Enter file to output (test.dbin) : ");
   wrefresh(stdscr);
   wgetnstr(stdscr, buf2, (255));
   noecho();
   if(strlen(buf1) == 0){
    strncpy(buf1, "test.dasm", 100);
   }
   if(strlen(buf2) == 0){
    strncpy(buf2, "test.dbin", 100);
   }
   printw("Compiling %s to %s\n", buf1, buf2);
   wrefresh(stdscr);
   printw("Press any key to continue");
   wrefresh(stdscr);
   wgetch(stdscr);
  }
  if(input == '2'){
   wclear(stdscr);
   printw("Running: \n");
   wrefresh(stdscr);
   sleep(1);
  }
  if(input == 'q'){
   wclear(stdscr);
   printw("Exiting... \n");
   wrefresh(stdscr);
   break;
  }
 }
 endwin();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: input char


repo: compudj/nscanf

original_code:
int main(int argc, char **argv)
{
 int ret;
 const char *test[] = {
  "test askdjhfaskljdfh aa % again",
  "test lkjlkjlkjlkjlkj zzzz bb %",
  "net123other",
 };
 char name[10];
 char name2[5];
 int testint;
 name[0] = '\0';
 name2[0] = '\0';
 ret = sscanf(test[0], "test %9s aa %% again", name);
 printf("%d \"%s\"\n", ret, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = sscanf(test[1], "test %9s %4s bb %%", name, name2);
 printf("%d \"%s\" \"%s\"\n", ret, name, name2);
 name[0] = '\0';
 testint = 0;
 ret = sscanf(test[2], "net%d%s", &testint, name);
 printf("%d \"%d\" \"%s\"\n", ret, testint, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = snscanf(test[0],
  "test %s aa %% again",
  (const ssize_t []) { sizeof(name) },
  name);
 printf("%d \"%s\"\n", ret, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = snscanf(test[1],
  "test %s %s bb %%",
  (const ssize_t []) { sizeof(name), sizeof(name2) },
  name, name2),
 printf("%d \"%s\" \"%s\"\n", ret, name, name2);
 name[0] = '\0';
 name2[0] = '\0';
 testint = 0;
 ret = snscanf(test[2],
  "net%d%s",
  (const ssize_t []) { -1, sizeof(name) },
  &testint, name);
 printf("%d \"%d\" \"%s\"\n", ret, testint, name);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int ret;
 char *test[] = {
  "test askdjhfaskljdfh aa % again",
  "test lkjlkjlkjlkjlkj zzzz bb %",
  "net123other",
 };
 char name[10];
 char name2[5];
 int testint;
 name[0] = '\0';
 name2[0] = '\0';
 ret = sscanf(test[0], "test %9s aa %% again", name);
 printf("%d \"%s\"\n", ret, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = sscanf(test[1], "test %9s %4s bb %%", name, name2);
 printf("%d \"%s\" \"%s\"\n", ret, name, name2);
 name[0] = '\0';
 testint = 0;
 ret = sscanf(test[2], "net%d%s", &testint, name);
 printf("%d \"%d\" \"%s\"\n", ret, testint, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = snscanf(test[0],
  "test %s aa %% again",
  (long []) { sizeof(name) },
  name);
 printf("%d \"%s\"\n", ret, name);
 name[0] = '\0';
 name2[0] = '\0';
 ret = snscanf(test[1],
  "test %s %s bb %%",
  (long []) { sizeof(name), sizeof(name2) },
  name, name2),
 printf("%d \"%s\" \"%s\"\n", ret, name, name2);
 name[0] = '\0';
 name2[0] = '\0';
 testint = 0;
 ret = snscanf(test[2],
  "net%d%s",
  (long []) { -1, sizeof(name) },
  &testint, name);
 printf("%d \"%d\" \"%s\"\n", ret, testint, name);
 return 0;
}

variable_types:
test
char *[]

variable_types:
name
char [10]

variable_types: testint int


decompiled_code:
__int64 __fastcall nscanf_prepare_fmt(void *src, char **a2, __int64 *a3)
{
const char *v3; // rbp
const char *v5; // r14
__int64 v6; // r12
char *v7; // rax
char v8; // r15
const char *v9; // rsi
const unsigned __int16 *v10; // rcx
char v11; // al
const char *v12; // rax
signed __int64 v13; // r13
char v15; // al
char *v16; // rdi
char *v17; // rax
char *v18; // r13
char *v19; // rax
char *v20; // r14
_BYTE *v21; // r15
char *v22; // rdi
size_t v23; // rdx
char v24; // r12
const unsigned __int16 **v25; // rax
_BYTE *v26; // rcx
const unsigned __int16 *v27; // rbp
char v28; // al
__int64 v29; // rax
__int64 v30; // rdx
int v31; // eax
char v32; // al
_BYTE *v33; // rdi
char *v34; // rax
char *v35; // r12
char *v36; // rdi
size_t v37; // rdx
const void *v38; // rsi
char *v39; // rdi
size_t v40; // r12
char v41; // [rsp+8h] [rbp-50h]
_BYTE *v42; // [rsp+8h] [rbp-50h]
char *dest; // [rsp+18h] [rbp-40h]

v3 = (const char *)src;
v41 = *(_BYTE *)src;
if ( *(_BYTE *)src )
{
v5 = (const char *)src;
v6 = 0LL;
while ( 1 )
{
v7 = strchrnul(v5, 37);
if ( !*v7 )
break;
v8 = v7[1];
if ( v8 == 37 )
{
v8 = v7[2];
v5 = v7 + 2;
}
else
{
v5 = v7 + 1;
if ( v8 == 42 )
{
v8 = v7[2];
v5 = v7 + 2;
}
if ( !v8 )
goto LABEL_25;
v9 = v5;
v10 = *__ctype_b_loc();
v11 = v8;
while ( (v10[v11] & 0x800) != 0 )
{
v11 = *++v9;
if ( !v11 )
goto LABEL_25;
}
if ( v11 == 36 )
{
v8 = v9[1];
v5 = v9 + 1;
}
if ( v8 == 97 || (v10[v8] & 0x800) != 0 )
{
LABEL_25:
*__errno_location() = 22;
return 0xFFFFFFFFLL;
}
switch ( v8 )
{
case 'L':
case 'j':
case 'q':
case 't':
case 'z':
v8 = v5[1];
v12 = v5 + 1;
goto LABEL_16;
case 'h':
v8 = v5[1];
if ( v8 != 104 )
goto LABEL_31;
goto LABEL_33;
case 'l':
v8 = v5[1];
if ( v8 == 108 )
{
LABEL_33:
v8 = v5[2];
v12 = v5 + 2;
}
else
{
LABEL_31:
v12 = v5 + 1;
}
LABEL_16:
if ( v8 == 97 )
goto LABEL_25;
v5 = v12;
LABEL_18:
if ( v8 == 110 )
goto LABEL_25;
if ( v8 != 91 )
goto LABEL_20;
v15 = v5[1];
v16 = (char *)(v5 + 1);
if ( v15 == 94 )
{
v15 = v5[2];
v16 = (char *)(v5 + 2);
}
v17 = strchrnul(&v16[v15 == 93], 93);
if ( !*v17 )
goto LABEL_25;
v8 = v17[1];
v5 = v17 + 1;
LABEL_20:
++v6;
break;
default:
goto LABEL_18;
}
}
if ( !v8 )
{
v13 = v5 - v3;
goto LABEL_35;
}
}
v13 = v7 - v3;
}
else
{
v13 = 0LL;
v6 = 0LL;
}
LABEL_35:
if ( strlen(v3) != v13 )
__assert_fail("strlen(format) == p - format", "nscanf.c", 0xB8u, "nscanf_prepare_fmt");
dest = (char *)malloc(v13 + 20 * v6 + 1);
if ( dest )
{
v18 = dest;
*dest = 0;
if ( !v41 )
{
LABEL_62:
*a2 = dest;
return 0LL;
}
while ( 1 )
{
v19 = strchrnul(v3, 37);
v20 = v19;
if ( !*v19 )
{
memcpy(v18, v3, v19 - v3);
goto LABEL_62;
}
v21 = v19 + 1;
v22 = v18;
v23 = v19 + 1 - v3;
v18 += v23;
memcpy(v22, v3, v23);
v24 = v20[1];
if ( v24 == 37 )
{
*v18 = 37;
v3 = v20 + 2;
v24 = v20[2];
++v18;
}
else
{
if ( v24 == 42 )
{
*v18 = 42;
v24 = v20[2];
v21 = v20 + 2;
++v18;
}
if ( !v24 )
LABEL_68:
__assert_fail("*p != '\\0'", "nscanf.c", 0xDFu, "nscanf_prepare_fmt");
v25 = __ctype_b_loc();
v26 = v21;
v27 = *v25;
v28 = v24;
while ( (v27[v28] & 0x800) != 0 )
{
v28 = *++v26;
if ( !v28 )
goto LABEL_68;
}
if ( v28 == 36 )
{
v38 = v21;
v39 = v18;
v42 = v26;
v40 = v26 + 1 - v21;
v21 = v26 + 1;
v18 += v40;
memcpy(v39, v38, v40);
v24 = v42[1];
}
if ( v24 == 97 )
__assert_fail("*p != 'a'", "nscanf.c", 0xF0u, "nscanf_prepare_fmt");
if ( (v27[v24] & 0x800) != 0 )
__assert_fail("!isdigit(*p)", "nscanf.c", 0xF1u, "nscanf_prepare_fmt");
v29 = *a3;
if ( *a3 >= 0 )
{
v30 = *a3;
if ( v24 == 91 || v24 == 115 )
{
if ( !v29 )
{
*__errno_location() = 22;
free(dest);
return 0xFFFFFFFFLL;
}
v30 = v29 - 1;
}
v31 = sprintf(v18, "%zu", v30);
v24 = *v21;
v18 += v31;
}
++a3;
switch ( v24 )
{
case 'L':
case 'j':
case 'q':
case 't':
case 'z':
*v18 = v24;
v24 = *++v21;
++v18;
break;
case 'h':
*v18 = 104;
v24 = v21[1];
if ( v24 != 104 )
goto LABEL_57;
v18[1] = 104;
v21 += 2;
v24 = *v21;
v18 += 2;
break;
case 'l':
*v18 = 108;
v24 = v21[1];
if ( v24 == 108 )
{
v18[1] = 108;
v21 += 2;
v24 = *v21;
v18 += 2;
}
else
{
LABEL_57:
++v21;
++v18;
}
break;
default:
break;
}
if ( v24 == 110 || v24 == 97 )
__assert_fail("*p != 'n' && *p != 'a'", "nscanf.c", 0x133u, "nscanf_prepare_fmt");
v3 = v21;
if ( v24 == 91 )
{
v32 = v21[1];
v33 = v21 + 1;
if ( v32 == 94 )
{
v32 = v21[2];
v33 = v21 + 2;
}
v34 = strchrnul(&v33[v32 == 93], 93);
v35 = v34;
if ( !*v34 )
__assert_fail("*p != '\\0'", "nscanf.c", 0x144u, "nscanf_prepare_fmt");
v3 = v34 + 1;
v36 = v18;
v37 = v34 + 1 - v21;
v18 += v37;
memcpy(v36, v21, v37);
v24 = v35[1];
}
}
if ( !v24 )
goto LABEL_62;
}
}
*__errno_location() = 12;
return 0xFFFFFFFFLL;
}

original_code:
static
int nscanf_prepare_fmt(const char *format,
  char **format_out,
  const ssize_t *field_widths)
{
 char *format_a, *dest_p;
 size_t format_a_len;
 const char *p, *prev_p;
 int ret;
 const ssize_t *fw_iter = field_widths;
 size_t nr_fw = 0;
 p = format;
 while (*p != '\0') {
  p = strchrnul(p, '%');
  if (*p == '\0') {
   break;
  }
  p++;
  switch (*p) {
  case '%':
   p++;
   continue;
  case '*':
   p++;
   break;
  default:
   break;
  }
  prev_p = p;
  for (;;) {
   if (*p == '\0') {
    (*__errno_location ()) = 22;
    return -1;
   }
   if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) {
    p++;
    continue;
   }
   if (*p == '$') {
    p++;
    break;
   }
   p = prev_p;
   break;
  }
  if (*p == 'a') {
   (*__errno_location ()) = 22;
   return -1;
  }
  if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) {
   (*__errno_location ()) = 22;
   return -1;
  }
  switch (*p) {
  case 'h':
   p++;
   if (*p == 'h') {
    p++;
   }
   break;
  case 'l':
   p++;
   if (*p == 'l') {
    p++;
   }
   break;
  case 'j':
  case 'z':
  case 't':
  case 'q':
  case 'L':
   p++;
   break;
  default:
   break;
  }
  switch (*p) {
  case '[':
   p++;
   if (*p == '^') {
    p++;
   }
   if (*p == ']') {
    p++;
   }
   p = strchrnul(p, ']');
   if (*p == '\0') {
    (*__errno_location ()) = 22;
    return -1;
   }
   p++;
   break;
  case 'n':
  case 'a':
   (*__errno_location ()) = 22;
   return -1;
  default:
   break;
  }
  nr_fw++;
 }
 ((void) sizeof ((strlen(format) == p - format) ? 1 : 0), __extension__ ({ if (strlen(format) == p - format) ; else __assert_fail ("strlen(format) == p - format", "nscanf.c", 184, __extension__ __PRETTY_FUNCTION__); }));
 format_a_len = p - format + 1 + (nr_fw * 20);
 format_a = malloc(format_a_len);
 if (!format_a) {
  (*__errno_location ()) = 12;
  return -1;
 }
 format_a[0] = '\0';
 p = format;
 dest_p = format_a;
 while (*p != '\0') {
  prev_p = p;
  p = strchrnul(p, '%');
  if (*p == '\0') {
   memcpy(dest_p, prev_p, p - prev_p);
   break;
  }
  p++;
  memcpy(dest_p, prev_p, p - prev_p);
  dest_p += p - prev_p;
  switch (*p) {
  case '%':
   *(dest_p++) = *(p++);
   continue;
  case '*':
   *(dest_p++) = *(p++);
   break;
  default:
   break;
  }
  prev_p = p;
  for (;;) {
   ((void) sizeof ((*p != '\0') ? 1 : 0), __extension__ ({ if (*p != '\0') ; else __assert_fail ("*p != '\\0'", "nscanf.c", 223, __extension__ __PRETTY_FUNCTION__); }));
   if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) {
    p++;
    continue;
   }
   if (*p == '$') {
    p++;
    memcpy(dest_p, prev_p, p - prev_p);
    dest_p += p - prev_p;
    break;
   }
   p = prev_p;
   break;
  }
  ((void) sizeof ((*p != 'a') ? 1 : 0), __extension__ ({ if (*p != 'a') ; else __assert_fail ("*p != 'a'", "nscanf.c", 240, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((!((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) ? 1 : 0), __extension__ ({ if (!((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) ; else __assert_fail ("!isdigit(*p)", "nscanf.c", 241, __extension__ __PRETTY_FUNCTION__); }));
  if (*fw_iter >= 0) {
   size_t width = *fw_iter;
   switch (*p) {
   case 's':
   case '[':
    if (!width) {
     (*__errno_location ()) = 22;
     ret = -1;
     goto end_error;
    }
    width--;
    break;
   default:
    break;
   }
   ret = sprintf(dest_p, "%zu", width);
   if (ret < 0) {
    goto end_error;
   }
   ((void) sizeof ((ret <= 20) ? 1 : 0), __extension__ ({ if (ret <= 20) ; else __assert_fail ("ret <= NSCANF_NUM_WIDTH", "nscanf.c", 273, __extension__ __PRETTY_FUNCTION__); }));
   dest_p += ret;
  }
  fw_iter++;
  switch (*p) {
  case 'h':
   *(dest_p++) = *(p++);
   if (*p == 'h') {
    *(dest_p++) = *(p++);
   }
   break;
  case 'l':
   *(dest_p++) = *(p++);
   if (*p == 'l') {
    *(dest_p++) = *(p++);
   }
   break;
  case 'j':
  case 'z':
  case 't':
  case 'q':
  case 'L':
   *(dest_p++) = *(p++);
   break;
  default:
   break;
  }
  ((void) sizeof ((*p != 'n' && *p != 'a') ? 1 : 0), __extension__ ({ if (*p != 'n' && *p != 'a') ; else __assert_fail ("*p != 'n' && *p != 'a'", "nscanf.c", 307, __extension__ __PRETTY_FUNCTION__); }));
  prev_p = p;
  switch (*p) {
  case '[':
   p++;
   if (*p == '^') {
    p++;
   }
   if (*p == ']') {
    p++;
   }
   p = strchrnul(p, ']');
   ((void) sizeof ((*p != '\0') ? 1 : 0), __extension__ ({ if (*p != '\0') ; else __assert_fail ("*p != '\\0'", "nscanf.c", 324, __extension__ __PRETTY_FUNCTION__); }));
   p++;
   memcpy(dest_p, prev_p, p - prev_p);
   dest_p += p - prev_p;
   break;
  default:
   break;
  }
 }
 *format_out = format_a;
 return 0;
end_error:
 free(format_a);
 return ret;
}

canonical_original_code:
static int nscanf_prepare_fmt(char *format, char **format_out, long *field_widths)
{
 char *format_a; char *dest_p;
 unsigned long format_a_len;
 char *p; char *prev_p;
 int ret;
 long *fw_iter = field_widths;
 unsigned long nr_fw = 0;
 p = format;
 while (*p != '\0') {
  p = strchrnul(p, '%');
  if (*p == '\0') {
   break;
  }
  p++;
  switch (*p) {
  case '%':
   p++;
   continue;
  case '*':
   p++;
   break;
  default:
   break;
  }
  prev_p = p;
  for (;;) {
   if (*p == '\0') {
    (*__errno_location ()) = 22;
    return -1;
   }
   if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) {
    p++;
    continue;
   }
   if (*p == '$') {
    p++;
    break;
   }
   p = prev_p;
   break;
  }
  if (*p == 'a') {
   (*__errno_location ()) = 22;
   return -1;
  }
  if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) {
   (*__errno_location ()) = 22;
   return -1;
  }
  switch (*p) {
  case 'h':
   p++;
   if (*p == 'h') {
    p++;
   }
   break;
  case 'l':
   p++;
   if (*p == 'l') {
    p++;
   }
   break;
  case 'j':
  case 'z':
  case 't':
  case 'q':
  case 'L':
   p++;
   break;
  default:
   break;
  }
  switch (*p) {
  case '[':
   p++;
   if (*p == '^') {
    p++;
   }
   if (*p == ']') {
    p++;
   }
   p = strchrnul(p, ']');
   if (*p == '\0') {
    (*__errno_location ()) = 22;
    return -1;
   }
   p++;
   break;
  case 'n':
  case 'a':
   (*__errno_location ()) = 22;
   return -1;
  default:
   break;
  }
  nr_fw++;
 }
 ((void) sizeof ((strlen(format) == p - format) ? 1 : 0), __extension__ ({ if (strlen(format) == p - format) ; else __assert_fail ("strlen(format) == p - format", "nscanf.c", 184, __extension__ __PRETTY_FUNCTION__); }));
 format_a_len = p - format + 1 + (nr_fw * 20);
 format_a = malloc(format_a_len);
 if (!format_a) {
  (*__errno_location ()) = 12;
  return -1;
 }
 format_a[0] = '\0';
 p = format;
 dest_p = format_a;
 while (*p != '\0') {
  prev_p = p;
  p = strchrnul(p, '%');
  if (*p == '\0') {
   memcpy(dest_p, prev_p, p - prev_p);
   break;
  }
  p++;
  memcpy(dest_p, prev_p, p - prev_p);
  dest_p += p - prev_p;
  switch (*p) {
  case '%':
   *(dest_p++) = *(p++);
   continue;
  case '*':
   *(dest_p++) = *(p++);
   break;
  default:
   break;
  }
  prev_p = p;
  for (;;) {
   ((void) sizeof ((*p != '\0') ? 1 : 0), __extension__ ({ if (*p != '\0') ; else __assert_fail ("*p != '\\0'", "nscanf.c", 223, __extension__ __PRETTY_FUNCTION__); }));
   if (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) {
    p++;
    continue;
   }
   if (*p == '$') {
    p++;
    memcpy(dest_p, prev_p, p - prev_p);
    dest_p += p - prev_p;
    break;
   }
   p = prev_p;
   break;
  }
  ((void) sizeof ((*p != 'a') ? 1 : 0), __extension__ ({ if (*p != 'a') ; else __assert_fail ("*p != 'a'", "nscanf.c", 240, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((!((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) ? 1 : 0), __extension__ ({ if (!((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) ; else __assert_fail ("!isdigit(*p)", "nscanf.c", 241, __extension__ __PRETTY_FUNCTION__); }));
  if (*fw_iter >= 0) {
   unsigned long width = *fw_iter;
   switch (*p) {
   case 's':
   case '[':
    if (!width) {
     (*__errno_location ()) = 22;
     ret = -1;
     goto end_error;
    }
    width--;
    break;
   default:
    break;
   }
   ret = sprintf(dest_p, "%zu", width);
   if (ret < 0) {
    goto end_error;
   }
   ((void) sizeof ((ret <= 20) ? 1 : 0), __extension__ ({ if (ret <= 20) ; else __assert_fail ("ret <= NSCANF_NUM_WIDTH", "nscanf.c", 273, __extension__ __PRETTY_FUNCTION__); }));
   dest_p += ret;
  }
  fw_iter++;
  switch (*p) {
  case 'h':
   *(dest_p++) = *(p++);
   if (*p == 'h') {
    *(dest_p++) = *(p++);
   }
   break;
  case 'l':
   *(dest_p++) = *(p++);
   if (*p == 'l') {
    *(dest_p++) = *(p++);
   }
   break;
  case 'j':
  case 'z':
  case 't':
  case 'q':
  case 'L':
   *(dest_p++) = *(p++);
   break;
  default:
   break;
  }
  ((void) sizeof ((*p != 'n' && *p != 'a') ? 1 : 0), __extension__ ({ if (*p != 'n' && *p != 'a') ; else __assert_fail ("*p != 'n' && *p != 'a'", "nscanf.c", 307, __extension__ __PRETTY_FUNCTION__); }));
  prev_p = p;
  switch (*p) {
  case '[':
   p++;
   if (*p == '^') {
    p++;
   }
   if (*p == ']') {
    p++;
   }
   p = strchrnul(p, ']');
   ((void) sizeof ((*p != '\0') ? 1 : 0), __extension__ ({ if (*p != '\0') ; else __assert_fail ("*p != '\\0'", "nscanf.c", 324, __extension__ __PRETTY_FUNCTION__); }));
   p++;
   memcpy(dest_p, prev_p, p - prev_p);
   dest_p += p - prev_p;
   break;
  default:
   break;
  }
 }
 *format_out = format_a;
 return 0;
end_error:
 free(format_a);
 return ret;
}

variable_types:
format
char *

variable_types:
p
char *

variable_types:
width
unsigned long


vsnscanf

decompiled_code:
__int64 __fastcall vsnscanf(char *s, void *src, __int64 *a3, void *a4)
{
int v5; // er12
char *format; // [rsp+8h] [rbp-20h] BYREF

v5 = nscanf_prepare_fmt(src, &format, a3);
if ( v5 >= 0 )
{
v5 = vsscanf(s, format, a4);
free(format);
}
return (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s, void *src, __int64 *a3, void *a4)
{
int v5; // er12
char *format; // [rsp+8h] [rbp-20h] BYREF

v5 = func6(src, &format, a3);
if ( v5 >= 0 )
{
v5 = vsscanf(s, format, a4);
free(format);
}
return (unsigned int)v5;
}

original_code:
int vsnscanf(const char *str, const char *format,
  const ssize_t *field_widths, va_list ap)
{
 char *format_a;
 int ret;
 ret = nscanf_prepare_fmt(format, &format_a, field_widths);
 if (ret < 0) {
  goto end;
 }
 ret = vsscanf(str, format_a, ap);
 free(format_a);
end:
 return ret;
}

canonical_original_code:
int vsnscanf(char *str, char *format, long *field_widths, __builtin_va_list ap)
{
 char *format_a;
 int ret;
 ret = nscanf_prepare_fmt(format, &format_a, field_widths);
 if (ret < 0) {
  goto end;
 }
 ret = vsscanf(str, format_a, ap);
 free(format_a);
end:
 return ret;
}


snscanf

decompiled_code:
__int64 __fastcall snscanf(char *a1, void *a2)
{
return vsnscanf(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, void *a2)
{
return func7(a1, a2);
}

original_code:
int snscanf(const char *str, const char *format,
  const ssize_t *field_widths, ...)
{
 va_list ap;
 int ret;
 __builtin_va_start(ap,field_widths);
 ret = vsnscanf(str, format, field_widths, ap);
 __builtin_va_end(ap);
 return ret;
}

canonical_original_code:
int snscanf(char *str, char *format, long *field_widths, ...)
{
 __builtin_va_list ap;
 int ret;
 __builtin_va_start(ap,field_widths);
 ret = vsnscanf(str, format, field_widths, ap);
 __builtin_va_end(ap);
 return ret;
}


vnscanf

decompiled_code:
__int64 __fastcall vnscanf(void *src)
{
return vfnscanf(stdin, src);
}

canonical_decompiled_code:
__int64 __fastcall func11(void *src)
{
return func9(stdin, src);
}

original_code:
int vnscanf(const char *format,
  const ssize_t *field_widths, va_list ap)
{
 return vfnscanf(stdin, format, field_widths, ap);
}

canonical_original_code:
int vnscanf(char *format, long *field_widths, __builtin_va_list ap)
{
 return vfnscanf(stdin, format, field_widths, ap);
}


nscanf

decompiled_code:
__int64 __fastcall nscanf(void *src)
{
return vfnscanf(stdin, src);
}

canonical_decompiled_code:
__int64 __fastcall func12(void *src)
{
return func9(stdin, src);
}

original_code:
int nscanf(const char *format, const ssize_t *field_widths, ...)
{
 va_list ap;
 int ret;
 __builtin_va_start(ap,field_widths);
 ret = vfnscanf(stdin, format, field_widths, ap);
 __builtin_va_end(ap);
 return ret;
}

canonical_original_code:
int nscanf(char *format, long *field_widths, ...)
{
 __builtin_va_list ap;
 int ret;
 __builtin_va_start(ap,field_widths);
 ret = vfnscanf(stdin, format, field_widths, ap);
 __builtin_va_end(ap);
 return ret;
}


main

repo: computersforpeace/bbb-floppy

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

v3 = 1;
init_tones(argc, argv, envp);
if ( !(unsigned int)init_drives() )
{
v3 = 0;
puts("Done resetting...");
udelay(1LL, 0LL);
puts("GO!");
play_looney_tunes();
play_mario();
}
return v3;
}

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

v3 = 1;
func10(argc, argv, envp);
if ( !(unsigned int)func24() )
{
v3 = 0;
puts("Done resetting...");
func11(1LL, 0LL);
puts("GO!");
func14();
func15();
}
return v3;
}

original_code:
int main()
{
 init_tones();
 if (init_drives())
  return 1;
 printf("Done resetting...\n");
 udelay(1, 0);
 printf("GO!\n");
 play_looney_tunes();
 play_mario();
 return 0;
}


original_code:
static void add_to_timespec(struct timespec *ts, int us)
{
 ts->tv_nsec += us * 1000;
 ts->tv_sec += ts->tv_nsec / 1000000000;
 ts->tv_nsec %= 1000000000;
}

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

variable_types: us int


decompiled_code:
double __fastcall get_frequency(double a1)
{
return pow(2.0, a1 / 12.0) * 440.0;
}

canonical_decompiled_code:
double __fastcall func8(double a1)
{
return pow(2.0, a1 / 12.0) * 440.0;
}

original_code:
static double get_frequency(double delta_a4)
{
 return 440.0 * pow(2, (double)delta_a4 / 12.0);
}

return_type: double


decompiled_code:
__int64 __fastcall get_period_us(double a1)
{
double v1; // xmm1_8

v1 = get_frequency(a1);
return lround(1000000.0 / v1);
}

canonical_decompiled_code:
__int64 __fastcall func9(double a1)
{
double v1; // xmm1_8

v1 = func8(a1);
return lround(1000000.0 / v1);
}

original_code:
static long int get_period_us(double delta_a4)
{
 return lround(1000000.0 / get_frequency(delta_a4));
}

canonical_original_code:
static long get_period_us(double delta_a4)
{
 return lround(1000000.0 / get_frequency(delta_a4));
}

return_type: long


decompiled_code:
__int64 init_tones()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = -24LL; i != 25; ++i )
{
result = get_period_us((double)(int)i);
qword_404540[i] = result;
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = -24LL; i != 25; ++i )
{
result = func9((double)(int)i);
qword_404540[i] = result;
}
return result;
}

original_code:
static void init_tones(void)
{
 int i;
 for (i = -24; i <= 24; i++)
  tone_period_us[i - -24] = get_period_us(i);
}

canonical_original_code:
static void init_tones()
{
 int i;
 for (i = -24; i <= 24; i++)
  tone_period_us[i - -24] = get_period_us(i);
}

variable_types: i int


tone

decompiled_code:
__int64 __fastcall tone(int a1, int a2)
{
int v2; // ebp
int v3; // ebx
struct timespec *v4; // rcx
__int64 result; // rax
struct timespec v6[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = 1000 * a2 / a1;
if ( clock_gettime(1, v6) < 0 )
{
perror("clock_gettime");
result = (unsigned int)*__errno_location();
}
else
{
v3 = 0;
if ( v2 > 0 )
{
while ( 1 )
{
take_step();
add_to_timespec(v6, a1);
if ( clock_nanosleep(1, 1, v6, v4) < 0 )
break;
if ( v2 == ++v3 )
goto LABEL_7;
}
perror("clock_nanosleep");
result = (unsigned int)*__errno_location();
}
else
{
LABEL_7:
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, int a2)
{
int v2; // ebp
int v3; // ebx
struct timespec *v4; // rcx
__int64 result; // rax
struct timespec v6[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = 1000 * a2 / a1;
if ( clock_gettime(1, v6) < 0 )
{
perror("clock_gettime");
result = (unsigned int)*__errno_location();
}
else
{
v3 = 0;
if ( v2 > 0 )
{
while ( 1 )
{
func23();
func7(v6, a1);
if ( clock_nanosleep(1, 1, v6, v4) < 0 )
break;
if ( v2 == ++v3 )
goto LABEL_7;
}
perror("clock_nanosleep");
result = (unsigned int)*__errno_location();
}
else
{
LABEL_7:
result = 0LL;
}
}
return result;
}

original_code:
int tone(int tone_us, int ms)
{
 int cycles = 1000 * ms / tone_us;
 int i, ret;
 struct timespec ts;
 ret = clock_gettime(1, &ts);
 if (ret < 0) {
  perror("clock_gettime");
  return (*__errno_location ());
 }
 for (i = 0; i < cycles; i++) {
  take_step();
  add_to_timespec(&ts, tone_us);
  ret = clock_nanosleep(1, 1, &ts, ((void *)0));
  if (ret < 0) {
   perror("clock_nanosleep");
   return (*__errno_location ());
  }
 }
 return 0;
}

canonical_original_code:
int tone(int tone_us, int ms)
{
 int cycles = 1000 * ms / tone_us;
 int i; int ret;
 struct timespec ts;
 ret = clock_gettime(1, &ts);
 if (ret < 0) {
  perror("clock_gettime");
  return (*__errno_location ());
 }
 for (i = 0; i < cycles; i++) {
  take_step();
  add_to_timespec(&ts, tone_us);
  ret = clock_nanosleep(1, 1, &ts, ((void *)0));
  if (ret < 0) {
   perror("clock_nanosleep");
   return (*__errno_location ());
  }
 }
 return 0;
}

variable_types: ms int

variable_types: cycles int

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


original_code:
static void play_score(struct note *score, int speed)
{
 struct note *n = score;
 while (n->duration) {
  long int period_us = (tone_period_us[(n->delta_a4 - 12) - -24]);
  int ms = n->duration * 12 * speed;
  tone(period_us, ms);
  ms *= 1500;
  udelay(ms / 1000000, ms % 1000000);
  n++;
 }
}

canonical_original_code:
static void play_score(struct note *score, int speed)
{
 struct note *n = score;
 while (n->duration) {
  long period_us = (tone_period_us[(n->delta_a4 - 12) - -24]);
  int ms = n->duration * 12 * speed;
  tone(period_us, ms);
  ms *= 1500;
  udelay(ms / 1000000, ms % 1000000);
  n++;
 }
}

variable_types: struct note { int delta_a4; int duration; } *

variable_types: speed int


decompiled_code:
__int64 play_looney_tunes()
{
return play_score(looney_tunes, 2);
}

canonical_decompiled_code:
__int64 func14()
{
return func13(looney_tunes, 2);
}

original_code:
static void play_looney_tunes(void)
{
 play_score(looney_tunes, SPEED_FAST);
}

canonical_original_code:
static void play_looney_tunes()
{
 play_score(looney_tunes, SPEED_FAST);
}


decompiled_code:
__int64 play_mario()
{
play_score(mario1, 4);
play_score(mario2, 4);
play_score(mario2, 4);
play_score(mario3, 4);
return play_score(mario3, 4);
}

canonical_decompiled_code:
__int64 func15()
{
func13(mario1, 4);
func13(mario2, 4);
func13(mario2, 4);
func13(mario3, 4);
return func13(mario3, 4);
}

original_code:
static void play_mario(void)
{
 int speed = SPEED_NORMAL;
 play_score(mario1, speed);
 play_score(mario2, speed);
 play_score(mario2, speed);
 play_score(mario3, speed);
 play_score(mario3, speed);
}

canonical_original_code:
static void play_mario()
{
 int speed = SPEED_NORMAL;
 play_score(mario1, speed);
 play_score(mario2, speed);
 play_score(mario2, speed);
 play_score(mario3, speed);
 play_score(mario3, speed);
}


decompiled_code:
int __fastcall open_gpio(unsigned int a1)
{
int result; // eax
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

sprintf(v2, "/sys/class/gpio/gpio%d/value", a1);
result = open(v2, 1);
if ( result < 0 )
{
perror("open");
result = *__errno_location();
}
return result;
}

canonical_decompiled_code:
int __fastcall func16(unsigned int a1)
{
int result; // eax
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

sprintf(v2, "/sys/class/gpio/gpio%d/value", a1);
result = open(v2, 1);
if ( result < 0 )
{
perror("open");
result = *__errno_location();
}
return result;
}

original_code:
static int open_gpio(int idx)
{
 char str[80];
 int fd;
 sprintf(str, "/sys/class/gpio" "/gpio%d" "/value", idx);
 fd = open(str, 01);
 if (fd < 0) {
  perror("open");
  return (*__errno_location ());
 }
 return fd;
}

variable_types: idx int

variable_types:
str
char [80]

variable_types: fd int


decompiled_code:
__int64 __fastcall export_gpio(unsigned int a1)
{
int v1; // ebp
size_t v2; // rax
signed __int64 v3; // rbx
int v4; // eax
int v5; // ebp
ssize_t v6; // rax
char v8[80]; // [rsp+0h] [rbp-F8h] BYREF
struct stat v9; // [rsp+50h] [rbp-A8h] BYREF

sprintf(v8, "/sys/class/gpio/gpio%d", a1);
if ( (unsigned int)stat(v8, &v9) || (v9.st_mode & 0xF000) != 0x4000 )
{
v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
LABEL_11:
perror("open");
return (unsigned int)*__errno_location();
}
sprintf(v8, "%d", a1);
v2 = strlen(v8);
v3 = write(v1, v8, v2);
if ( v3 < 0 )
{
perror("write to export");
return (unsigned int)*__errno_location();
}
if ( strlen(v8) > v3 )
goto LABEL_14;
close(v1);
}
sprintf(v8, "/sys/class/gpio/gpio%d/direction", a1);
v4 = open(v8, 1);
v5 = v4;
if ( v4 < 0 )
goto LABEL_11;
v6 = write(v4, "out", 3uLL);
if ( v6 < 0 )
{
perror("write");
return (unsigned int)*__errno_location();
}
if ( v6 > 2 )
{
close(v5);
return 0LL;
}
LABEL_14:
puts("error: didn't write full length");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int a1)
{
int v1; // ebp
size_t v2; // rax
signed __int64 v3; // rbx
int v4; // eax
int v5; // ebp
ssize_t v6; // rax
char v8[80]; // [rsp+0h] [rbp-F8h] BYREF
struct stat v9; // [rsp+50h] [rbp-A8h] BYREF

sprintf(v8, "/sys/class/gpio/gpio%d", a1);
if ( (unsigned int)func27(v8, &v9) || (v9.st_mode & 0xF000) != 0x4000 )
{
v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
LABEL_11:
perror("open");
return (unsigned int)*__errno_location();
}
sprintf(v8, "%d", a1);
v2 = strlen(v8);
v3 = write(v1, v8, v2);
if ( v3 < 0 )
{
perror("write to export");
return (unsigned int)*__errno_location();
}
if ( strlen(v8) > v3 )
goto LABEL_14;
close(v1);
}
sprintf(v8, "/sys/class/gpio/gpio%d/direction", a1);
v4 = open(v8, 1);
v5 = v4;
if ( v4 < 0 )
goto LABEL_11;
v6 = write(v4, "out", 3uLL);
if ( v6 < 0 )
{
perror("write");
return (unsigned int)*__errno_location();
}
if ( v6 > 2 )
{
close(v5);
return 0LL;
}
LABEL_14:
puts("error: didn't write full length");
return 0xFFFFFFFFLL;
}

original_code:
static int export_gpio(int idx)
{
 char str[80];
 int fd;
 ssize_t ret;
 struct stat sb;
 sprintf(str, "/sys/class/gpio" "/gpio%d", idx);
 if (!stat(str, &sb) == 0 || !((((sb.st_mode)) & 0170000) == (0040000))) {
  fd = open("/sys/class/gpio" "/export", 01);
  if (fd < 0) {
   perror("open");
   return (*__errno_location ());
  }
  sprintf(str, "%d", idx);
  ret = write(fd, str, strlen(str));
  if (ret < 0) {
   perror("write to export");
   return (*__errno_location ());
  } else if ((size_t)ret < strlen(str)) {
   printf("error: didn't write full length\n");
   return -1;
  }
  close(fd);
 }
 sprintf(str, "/sys/class/gpio" "/gpio%d" "/direction", idx);
 fd = open(str, 01);
 if (fd < 0) {
  perror("open");
  return (*__errno_location ());
 }
 ret = write(fd, "out", strlen("out"));
 if (ret < 0) {
  perror("write");
  return (*__errno_location ());
 } else if ((size_t)ret < strlen("out")) {
  printf("error: didn't write full length\n");
  return -1;
 }
 close(fd);
 return 0;
}

canonical_original_code:
static int export_gpio(int idx)
{
 char str[80];
 int fd;
 long ret;
 struct stat sb;
 sprintf(str, "/sys/class/gpio" "/gpio%d", idx);
 if (!stat(str, &sb) == 0 || !((((sb.st_mode)) & 0170000) == (0040000))) {
  fd = open("/sys/class/gpio" "/export", 01);
  if (fd < 0) {
   perror("open");
   return (*__errno_location ());
  }
  sprintf(str, "%d", idx);
  ret = write(fd, str, strlen(str));
  if (ret < 0) {
   perror("write to export");
   return (*__errno_location ());
  } else if ((unsigned long)ret < strlen(str)) {
   printf("error: didn't write full length\n");
   return -1;
  }
  close(fd);
 }
 sprintf(str, "/sys/class/gpio" "/gpio%d" "/direction", idx);
 fd = open(str, 01);
 if (fd < 0) {
  perror("open");
  return (*__errno_location ());
 }
 ret = write(fd, "out", strlen("out"));
 if (ret < 0) {
  perror("write");
  return (*__errno_location ());
 } else if ((unsigned long)ret < strlen("out")) {
  printf("error: didn't write full length\n");
  return -1;
 }
 close(fd);
 return 0;
}

variable_types: ret long

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

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


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

result = export_gpio(0x3Cu);
if ( !result )
{
result = export_gpio(0x32u);
if ( !result )
{
result = open_gpio(0x3Cu);
step_fd = result;
if ( result >= 0 )
{
result = open_gpio(0x32u);
dir_fd = result;
if ( result > 0 )
result = 0;
}
}
}
return result;
}

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

result = func17(0x3Cu);
if ( !result )
{
result = func17(0x32u);
if ( !result )
{
result = func16(0x3Cu);
step_fd = result;
if ( result >= 0 )
{
result = func16(0x32u);
dir_fd = result;
if ( result > 0 )
result = 0;
}
}
}
return result;
}

original_code:
static int init_gpios(void)
{
 int ret;
 ret = export_gpio(60);
 if (ret)
  return ret;
 ret = export_gpio(50);
 if (ret)
  return ret;
 step_fd = open_gpio(60);
 if (step_fd < 0)
  return step_fd;
 dir_fd = open_gpio(50);
 if (dir_fd < 0)
  return dir_fd;
 return 0;
}

canonical_original_code:
static int init_gpios()
{
 int ret;
 ret = export_gpio(60);
 if (ret)
  return ret;
 ret = export_gpio(50);
 if (ret)
  return ret;
 step_fd = open_gpio(60);
 if (step_fd < 0)
  return step_fd;
 dir_fd = open_gpio(50);
 if (dir_fd < 0)
  return dir_fd;
 return 0;
}

variable_types: ret int


decompiled_code:
__int64 reset_floppy_positions()
{
int v0; // ebx

v0 = 80;
set_direction_isra_0(0);
do
{
write_value_isra_0(step_fd, 0);
write_value_isra_0(step_fd, 1u);
udelay_constprop_0_isra_0();
--v0;
}
while ( v0 );
set_direction_isra_0(1u);
return 0LL;
}

canonical_decompiled_code:
__int64 func22()
{
int v0; // ebx

v0 = 80;
func21(0);
do
{
func20(step_fd, 0);
func20(step_fd, 1u);
func19();
--v0;
}
while ( v0 );
func21(1u);
return 0LL;
}

original_code:
static int reset_floppy_positions(void)
{
 int i;
 set_direction(0);
 for (i = 0; i < 80; i++) {
  write_value(step_fd, 0);
  write_value(step_fd, 1);
  udelay(0, 2000);
 }
 set_direction(1);
 return 0;
}

canonical_original_code:
static int reset_floppy_positions()
{
 int i;
 set_direction(0);
 for (i = 0; i < 80; i++) {
  write_value(step_fd, 0);
  write_value(step_fd, 1);
  udelay(0, 2000);
 }
 set_direction(1);
 return 0;
}


decompiled_code:
__int64 take_step()
{
int v0; // edx

write_value_isra_0(step_fd, 0);
write_value_isra_0(step_fd, 1u);
v0 = floppy_position + 2 * floppy_direction - 1;
floppy_position = v0;
if ( floppy_direction )
{
if ( v0 == 79 && floppy_direction == 1 )
set_direction_isra_0(0);
return 0LL;
}
if ( v0 )
return 0LL;
set_direction_isra_0(1u);
return 0LL;
}

canonical_decompiled_code:
__int64 func23()
{
int v0; // edx

func20(step_fd, 0);
func20(step_fd, 1u);
v0 = floppy_position + 2 * floppy_direction - 1;
floppy_position = v0;
if ( floppy_direction )
{
if ( v0 == 79 && floppy_direction == 1 )
func21(0);
return 0LL;
}
if ( v0 )
return 0LL;
func21(1u);
return 0LL;
}

original_code:
int take_step(void)
{
 write_value(step_fd, 0);
 write_value(step_fd, 1);
 floppy_position += floppy_direction * 2 - 1;
 if (floppy_direction == 0 && floppy_position == 0)
  set_direction(1);
 else if (floppy_direction == 1 && floppy_position == 80 - 1)
  set_direction(0);
 return 0;
}

canonical_original_code:
int take_step()
{
 write_value(step_fd, 0);
 write_value(step_fd, 1);
 floppy_position += floppy_direction * 2 - 1;
 if (floppy_direction == 0 && floppy_position == 0)
  set_direction(1);
 else if (floppy_direction == 1 && floppy_position == 80 - 1)
  set_direction(0);
 return 0;
}


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

result = init_gpios();
if ( !result )
result = reset_floppy_positions();
return result;
}

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

result = func18();
if ( !result )
result = func22();
return result;
}

original_code:
int init_drives(void)
{
 int ret;
 ret = init_gpios();
 if (ret)
  return ret;
 return reset_floppy_positions();
}

canonical_original_code:
int init_drives()
{
 int ret;
 ret = init_gpios();
 if (ret)
  return ret;
 return reset_floppy_positions();
}


repo: comstud/mysqlsniffer

decompiled_code:
int __fastcall dump_pkt(unsigned __int8 *a1, int a2, char a3)
{
unsigned __int8 *v4; // rbx
const unsigned __int16 **v5; // r13
bool v6; // bp
__int64 v7; // r12
__int64 v8; // rsi

v4 = a1;
printf("::DUMP:: ");
if ( a2 )
{
v5 = _ctype_b_loc();
v6 = a3 == 0;
v7 = (__int64)&a1[a2 - 1 + 1];
do
{
while ( 1 )
{
v8 = *v4;
if ( ((*v5)[v8] & 0x4000) != 0 && v6 )
break;
++v4;
printf("%02x ", v8);
if ( (unsigned __int8 *)v7 == v4 )
return printf("::DUMP::");
}
++v4;
printf("%c ", v8);
}
while ( (unsigned __int8 *)v7 != v4 );
}
return printf("::DUMP::");
}

canonical_decompiled_code:
int __fastcall func0(unsigned __int8 *a1, int a2, char a3)
{
unsigned __int8 *v4; // rbx
const unsigned __int16 **v5; // r13
bool v6; // bp
__int64 v7; // r12
__int64 v8; // rsi

v4 = a1;
printf("::DUMP:: ");
if ( a2 )
{
v5 = _ctype_b_loc();
v6 = a3 == 0;
v7 = (__int64)&a1[a2 - 1 + 1];
do
{
while ( 1 )
{
v8 = *v4;
if ( ((*v5)[v8] & 0x4000) != 0 && v6 )
break;
++v4;
printf("%02x ", v8);
if ( (unsigned __int8 *)v7 == v4 )
return printf("::DUMP::");
}
++v4;
printf("%c ", v8);
}
while ( (unsigned __int8 *)v7 != v4 );
}
return printf("::DUMP::");
}

original_code:
void dump_pkt(u_char *pkt, u_int len, char only_hex)
{
   int i;
   printf("::DUMP:: ");
   for(i = 0; i < len; i++) {
      if(((*__ctype_b_loc ())[(int) ((pkt[i]))] & (unsigned short int) _ISprint) && !only_hex) printf("%c ", pkt[i]);
      else printf("%02x ", pkt[i]);
   }
   printf("::DUMP::");
}

canonical_original_code:
void dump_pkt(unsigned char *pkt, unsigned int len, char only_hex)
{
   int i;
   printf("::DUMP:: ");
   for(i = 0; i < len; i++) {
      if(((*__ctype_b_loc ())[(int) ((pkt[i]))] & (unsigned short) _ISprint) && !only_hex) printf("%c ", pkt[i]);
      else printf("%02x ", pkt[i]);
   }
   printf("::DUMP::");
}

variable_types:
pkt
unsigned char *

variable_types:
len
unsigned int


decompiled_code:
char *__fastcall get_arg(__int64 a1, unsigned int a2)
{
size_t v2; // rbx

if ( a2 > 0x1000 )
a2 = 4096;
v2 = a2 - 1;
memcpy(arg_0, (const void *)(a1 + 1), v2);
arg_0[v2] = 0;
return arg_0;
}

canonical_decompiled_code:
char *__fastcall func1(__int64 a1, unsigned int a2)
{
size_t v2; // rbx

if ( a2 > 0x1000 )
a2 = 4096;
v2 = a2 - 1;
memcpy(arg_0, (const void *)(a1 + 1), v2);
arg_0[v2] = 0;
return arg_0;
}

original_code:
const u_char *get_arg(u_char *pkt, u_int len)
{
   static u_char arg[4096];
   if(len > 4096) len = 4096;
   pkt++;
   memcpy(arg, pkt, len - 1);
   arg[len - 1] = 0;
   return (const u_char *)arg;
}

canonical_original_code:
unsigned char *get_arg(unsigned char *pkt, unsigned int len)
{
   static unsigned char arg[4096];
   if(len > 4096) len = 4096;
   pkt++;
   memcpy(arg, pkt, len - 1);
   arg[len - 1] = 0;
   return (unsigned char *)arg;
}

variable_types:
arg
unsigned char [4096]

return_type: unsigned char *


decompiled_code:
unsigned __int8 *__fastcall my_strcpy(_DWORD *a1, unsigned __int8 *a2, int a3)
{
__int64 v3; // rcx
unsigned __int8 *v5; // r9
unsigned __int8 *result; // rax
unsigned int v7; // edx
unsigned __int64 v8; // rdi

v3 = *a2;
v5 = a2 + 1;
if ( *a2 )
{
result = &v5[v3];
if ( a1 )
{
v7 = a3 - 1;
if ( v7 > (unsigned int)v3 )
v7 = *a2;
if ( v7 >= 8 )
{
v8 = (unsigned __int64)(a1 + 2);
*(_QWORD *)(v8 - 8) = *(_QWORD *)(a2 + 1);
*(_QWORD *)(v8 + v7 - 16) = *(_QWORD *)&v5[v7 - 8];
qmemcpy(
(void *)(v8 & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - ((unsigned __int8 *)a1 - (v8 & 0xFFFFFFFFFFFFFFF8LL))),
8 * ((v7 + (unsigned int)a1 - (v8 & 0xFFFFFFF8)) >> 3));
*((_BYTE *)a1 + v7) = 0;
}
else
{
if ( (v7 & 4) != 0 )
{
*a1 = *(_DWORD *)(a2 + 1);
*(_DWORD *)((char *)a1 + v7 - 4) = *(_DWORD *)&v5[v7 - 4];
}
else if ( v7 )
{
*(_BYTE *)a1 = a2[1];
if ( (v7 & 2) != 0 )
*(_WORD *)((char *)a1 + v7 - 2) = *(_WORD *)&v5[v7 - 2];
}
*((_BYTE *)a1 + v7) = 0;
}
}
}
else
{
if ( a1 )
*(_BYTE *)a1 = 0;
result = a2 + 1;
}
return result;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func2(_DWORD *a1, unsigned __int8 *a2, int a3)
{
__int64 v3; // rcx
unsigned __int8 *v5; // r9
unsigned __int8 *result; // rax
unsigned int v7; // edx
unsigned __int64 v8; // rdi

v3 = *a2;
v5 = a2 + 1;
if ( *a2 )
{
result = &v5[v3];
if ( a1 )
{
v7 = a3 - 1;
if ( v7 > (unsigned int)v3 )
v7 = *a2;
if ( v7 >= 8 )
{
v8 = (unsigned __int64)(a1 + 2);
*(_QWORD *)(v8 - 8) = *(_QWORD *)(a2 + 1);
*(_QWORD *)(v8 + v7 - 16) = *(_QWORD *)&v5[v7 - 8];
qmemcpy(
(void *)(v8 & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - ((unsigned __int8 *)a1 - (v8 & 0xFFFFFFFFFFFFFFF8LL))),
8 * ((v7 + (unsigned int)a1 - (v8 & 0xFFFFFFF8)) >> 3));
*((_BYTE *)a1 + v7) = 0;
}
else
{
if ( (v7 & 4) != 0 )
{
*a1 = *(_DWORD *)(a2 + 1);
*(_DWORD *)((char *)a1 + v7 - 4) = *(_DWORD *)&v5[v7 - 4];
}
else if ( v7 )
{
*(_BYTE *)a1 = a2[1];
if ( (v7 & 2) != 0 )
*(_WORD *)((char *)a1 + v7 - 2) = *(_WORD *)&v5[v7 - 2];
}
*((_BYTE *)a1 + v7) = 0;
}
}
}
else
{
if ( a1 )
*(_BYTE *)a1 = 0;
result = a2 + 1;
}
return result;
}

original_code:
u_char *my_strcpy(u_char *to, u_char *from, u_int to_size)
{
   u_int str_len, real_str_len;
   str_len = real_str_len = (u_int)*from;
   if(!str_len) {
      if(to) to[0] = 0;
      return (from + 1);
   }
   from++;
   if(!to)
      return (from + str_len);
   if(str_len > (to_size - 1)) str_len = to_size - 1;
   memcpy(to, from, str_len);
   to[str_len] = 0;
   return (from + real_str_len);
}

canonical_original_code:
unsigned char *my_strcpy(unsigned char *to, unsigned char *from, unsigned int to_size)
{
   unsigned int str_len; unsigned int real_str_len;
   str_len = real_str_len = (u_int)*from;
   if(!str_len) {
      if(to) to[0] = 0;
      return (from + 1);
   }
   from++;
   if(!to)
      return (from + str_len);
   if(str_len > (to_size - 1)) str_len = to_size - 1;
   memcpy(to, from, str_len);
   to[str_len] = 0;
   return (from + real_str_len);
}

variable_types:
to
unsigned char *

variable_types:
from
unsigned char *


decompiled_code:
__int64 __fastcall G3(__int64 a1)
{
unsigned int v2; // [rsp+0h] [rbp-4h]

LOWORD(v2) = *(_WORD *)a1;
HIWORD(v2) = *(unsigned __int8 *)(a1 + 2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v2; // [rsp+0h] [rbp-4h]

LOWORD(v2) = *(_WORD *)a1;
HIWORD(v2) = *(unsigned __int8 *)(a1 + 2);
return v2;
}

original_code:
u_int G3(u_char *pkt)
{
   u_char buff[4];
   memcpy(buff, pkt, 3);
   buff[3] = 0;
   return *((u_int *)buff);
}

canonical_original_code:
unsigned int G3(unsigned char *pkt)
{
   unsigned char buff[4];
   memcpy(buff, pkt, 3);
   buff[3] = 0;
   return *((unsigned int *)buff);
}

variable_types:
buff
unsigned char [4]

return_type: unsigned int


original_code:
u_int convert_binary_number(u_char *number, u_short flags, u_int bytes, u_char *to)
{
   char sz[16];
   u_int len;
   long long ln = 0;
   int n = 0;
   if(bytes == 1) n = *number;
   else if(bytes == 2) n = *((u_short *)number);
   else if(bytes == 4) n = *((u_int *)number);
   else if(bytes == 8) { memcpy((void *)&ln, number, 8); }
   if(bytes != 8) {
      if(flags & 32)
         sprintf(sz, "%u", n);
      else
         sprintf(sz, "%d", n);
   }
   else {
      if(flags & 32)
         sprintf(sz, "%llu", ln);
      else
         sprintf(sz, "%lld", ln);
   }
   len = strlen(sz);
   to[0] = len;
   memcpy(to + 1, sz, len);
   return len + 1;
}

canonical_original_code:
unsigned int convert_binary_number(unsigned char *number, unsigned short flags, unsigned int bytes, unsigned char *to)
{
   char sz[16];
   unsigned int len;
   long long ln = 0;
   int n = 0;
   if(bytes == 1) n = *number;
   else if(bytes == 2) n = *((unsigned short *)number);
   else if(bytes == 4) n = *((unsigned int *)number);
   else if(bytes == 8) { memcpy((void *)&ln, number, 8); }
   if(bytes != 8) {
      if(flags & 32)
         sprintf(sz, "%u", n);
      else
         sprintf(sz, "%d", n);
   }
   else {
      if(flags & 32)
         sprintf(sz, "%llu", ln);
      else
         sprintf(sz, "%lld", ln);
   }
   len = strlen(sz);
   to[0] = len;
   memcpy(to + 1, sz, len);
   return len + 1;
}

variable_types:
number
unsigned char *

variable_types:
flags
unsigned short

variable_types:
bytes
unsigned int

variable_types:
sz
char [16]

variable_types:
ln
long long

variable_types: n int


decompiled_code:
__int64 __fastcall convert_binary_datetime(char *a1, __int64 a2)
{
__int64 v2; // rbp
char v4; // r15
size_t v5; // rax
size_t v6; // r13
unsigned __int64 v7; // rsi
__int64 v8; // rbp
char *v9; // r8
unsigned int v10; // ebp
unsigned int v11; // ebp
unsigned int v12; // edx
__int64 v13; // rcx
unsigned __int64 v15; // rbx
__int64 v16; // rbp
unsigned int v17; // ecx
__int64 v18; // rsi
_DWORD *v19; // rdx
unsigned __int64 v20; // rdi
char *v21; // rdx
char *v22; // r14
unsigned int v23; // edx
unsigned int v24; // edx
unsigned int v25; // ecx
__int64 v26; // rsi
char s[8]; // [rsp+5h] [rbp-43h] BYREF

v2 = a2 + 1;
v4 = *a1;
if ( *a1 )
{
sprintf(s, "%04u-%02u-%02u", *(unsigned __int16 *)(a1 + 1), (unsigned __int8)a1[3], (unsigned __int8)a1[4]);
v5 = strlen(s);
*(_BYTE *)a2 = v5;
v6 = v5;
if ( (unsigned int)v5 < 8 )
{
if ( (v5 & 4) != 0 )
{
*(_DWORD *)(a2 + 1) = *(_DWORD *)s;
*(_DWORD *)(v2 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)(a2 + 1) = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)(v2 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
else
{
v7 = (a2 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)(a2 + 1) = *(_QWORD *)s;
*(_QWORD *)(v2 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v8 = v2 - v7;
v9 = &s[-v8];
v10 = (v5 + v8) & 0xFFFFFFF8;
if ( v10 >= 8 )
{
v11 = v10 & 0xFFFFFFF8;
v12 = 0;
do
{
v13 = v12;
v12 += 8;
*(_QWORD *)(v7 + v13) = *(_QWORD *)&v9[v13];
}
while ( v12 < v11 );
}
}
if ( v4 == 7 )
{
sprintf(s, " %02u:%02u:%02u", (unsigned __int8)a1[5], (unsigned __int8)a1[6], (unsigned __int8)a1[7]);
LODWORD(v5) = strlen(s);
v19 = (_DWORD *)(a2 + v6 + 1);
*(_BYTE *)a2 += v5;
if ( (unsigned int)v5 < 8 )
{
if ( (v5 & 4) != 0 )
{
*v19 = *(_DWORD *)s;
*(_DWORD *)((char *)v19 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)v19 = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v19 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
else
{
v20 = (a2 + v6 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)v19 = *(_QWORD *)s;
*(_QWORD *)((char *)v19 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v21 = (char *)v19 - v20;
v22 = (char *)(s - v21);
v23 = (v5 + (_DWORD)v21) & 0xFFFFFFF8;
if ( v23 >= 8 )
{
v24 = v23 & 0xFFFFFFF8;
v25 = 0;
do
{
v26 = v25;
v25 += 8;
*(_QWORD *)(v20 + v26) = *(_QWORD *)&v22[v26];
}
while ( v25 < v24 );
}
}
LOWORD(v5) = v6 + v5;
}
}
else
{
sprintf(s, "%04u-%02u-%02u", 0LL, 0LL, 0LL);
LODWORD(v5) = strlen(s);
*(_BYTE *)a2 = v5;
if ( (unsigned int)v5 >= 8 )
{
*(_QWORD *)(a2 + 1) = *(_QWORD *)s;
v15 = (a2 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)(v2 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v16 = v2 - v15;
if ( (((_DWORD)v16 + (_DWORD)v5) & 0xFFFFFFF8) >= 8 )
{
v17 = 0;
do
{
v18 = v17;
v17 += 8;
*(_QWORD *)(v15 + v18) = *(_QWORD *)&s[v18 - v16];
}
while ( v17 < (((_DWORD)v16 + (_DWORD)v5) & 0xFFFFFFF8) );
}
}
else if ( (v5 & 4) != 0 )
{
*(_DWORD *)(a2 + 1) = *(_DWORD *)s;
*(_DWORD *)(v2 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)(a2 + 1) = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)(v2 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
return (unsigned __int16)(v5 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1, __int64 a2)
{
__int64 v2; // rbp
char v4; // r15
size_t v5; // rax
size_t v6; // r13
unsigned __int64 v7; // rsi
__int64 v8; // rbp
char *v9; // r8
unsigned int v10; // ebp
unsigned int v11; // ebp
unsigned int v12; // edx
__int64 v13; // rcx
unsigned __int64 v15; // rbx
__int64 v16; // rbp
unsigned int v17; // ecx
__int64 v18; // rsi
_DWORD *v19; // rdx
unsigned __int64 v20; // rdi
char *v21; // rdx
char *v22; // r14
unsigned int v23; // edx
unsigned int v24; // edx
unsigned int v25; // ecx
__int64 v26; // rsi
char s[8]; // [rsp+5h] [rbp-43h] BYREF

v2 = a2 + 1;
v4 = *a1;
if ( *a1 )
{
sprintf(s, "%04u-%02u-%02u", *(unsigned __int16 *)(a1 + 1), (unsigned __int8)a1[3], (unsigned __int8)a1[4]);
v5 = strlen(s);
*(_BYTE *)a2 = v5;
v6 = v5;
if ( (unsigned int)v5 < 8 )
{
if ( (v5 & 4) != 0 )
{
*(_DWORD *)(a2 + 1) = *(_DWORD *)s;
*(_DWORD *)(v2 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)(a2 + 1) = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)(v2 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
else
{
v7 = (a2 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)(a2 + 1) = *(_QWORD *)s;
*(_QWORD *)(v2 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v8 = v2 - v7;
v9 = &s[-v8];
v10 = (v5 + v8) & 0xFFFFFFF8;
if ( v10 >= 8 )
{
v11 = v10 & 0xFFFFFFF8;
v12 = 0;
do
{
v13 = v12;
v12 += 8;
*(_QWORD *)(v7 + v13) = *(_QWORD *)&v9[v13];
}
while ( v12 < v11 );
}
}
if ( v4 == 7 )
{
sprintf(s, " %02u:%02u:%02u", (unsigned __int8)a1[5], (unsigned __int8)a1[6], (unsigned __int8)a1[7]);
LODWORD(v5) = strlen(s);
v19 = (_DWORD *)(a2 + v6 + 1);
*(_BYTE *)a2 += v5;
if ( (unsigned int)v5 < 8 )
{
if ( (v5 & 4) != 0 )
{
*v19 = *(_DWORD *)s;
*(_DWORD *)((char *)v19 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)v19 = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v19 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
else
{
v20 = (a2 + v6 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)v19 = *(_QWORD *)s;
*(_QWORD *)((char *)v19 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v21 = (char *)v19 - v20;
v22 = (char *)(s - v21);
v23 = (v5 + (_DWORD)v21) & 0xFFFFFFF8;
if ( v23 >= 8 )
{
v24 = v23 & 0xFFFFFFF8;
v25 = 0;
do
{
v26 = v25;
v25 += 8;
*(_QWORD *)(v20 + v26) = *(_QWORD *)&v22[v26];
}
while ( v25 < v24 );
}
}
LOWORD(v5) = v6 + v5;
}
}
else
{
sprintf(s, "%04u-%02u-%02u", 0LL, 0LL, 0LL);
LODWORD(v5) = strlen(s);
*(_BYTE *)a2 = v5;
if ( (unsigned int)v5 >= 8 )
{
*(_QWORD *)(a2 + 1) = *(_QWORD *)s;
v15 = (a2 + 9) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)(v2 + (unsigned int)v5 - 8) = *(_QWORD *)&s[(unsigned int)v5 - 8];
v16 = v2 - v15;
if ( (((_DWORD)v16 + (_DWORD)v5) & 0xFFFFFFF8) >= 8 )
{
v17 = 0;
do
{
v18 = v17;
v17 += 8;
*(_QWORD *)(v15 + v18) = *(_QWORD *)&s[v18 - v16];
}
while ( v17 < (((_DWORD)v16 + (_DWORD)v5) & 0xFFFFFFF8) );
}
}
else if ( (v5 & 4) != 0 )
{
*(_DWORD *)(a2 + 1) = *(_DWORD *)s;
*(_DWORD *)(v2 + (unsigned int)v5 - 4) = *(_DWORD *)&s[(unsigned int)v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)(a2 + 1) = s[0];
if ( (v5 & 2) != 0 )
*(_WORD *)(v2 + (unsigned int)v5 - 2) = *(_WORD *)&s[(unsigned int)v5 - 2];
}
}
return (unsigned __int16)(v5 + 1);
}

original_code:
u_int convert_binary_datetime(u_char *d, u_char *to)
{
   u_char dt_len = *d;
   u_short year = 0;
   u_char month = 0;
   u_char day = 0;
   u_char hour = 0;
   u_char min = 0;
   u_char sec = 0;
   char sz[11];
   u_short len;
   if(dt_len) {
      d++;
      year = *((u_short *)d);
      d += 2;
      month = *d++;
      day = *d++;
   }
   sprintf(sz, "%04u-%02u-%02u", year, month, day);
   to[0] = len = strlen(sz);
   memcpy(to + 1, sz, len);
   if(dt_len == 7) {
      hour = *d++;
      min = *d++;
      sec = *d++;
      sprintf(sz, " %02u:%02u:%02u", hour, min, sec);
      to[0] = to[0] + strlen(sz);
      memcpy(to + 1 + len, sz, strlen(sz));
      len += strlen(sz);
   }
   return len + 1;
}

canonical_original_code:
unsigned int convert_binary_datetime(unsigned char *d, unsigned char *to)
{
   unsigned char dt_len = *d;
   unsigned short year = 0;
   unsigned char month = 0;
   unsigned char day = 0;
   unsigned char hour = 0;
   unsigned char min = 0;
   unsigned char sec = 0;
   char sz[11];
   unsigned short len;
   if(dt_len) {
      d++;
      year = *((unsigned short *)d);
      d += 2;
      month = *d++;
      day = *d++;
   }
   sprintf(sz, "%04u-%02u-%02u", year, month, day);
   to[0] = len = strlen(sz);
   memcpy(to + 1, sz, len);
   if(dt_len == 7) {
      hour = *d++;
      min = *d++;
      sec = *d++;
      sprintf(sz, " %02u:%02u:%02u", hour, min, sec);
      to[0] = to[0] + strlen(sz);
      memcpy(to + 1 + len, sz, strlen(sz));
      len += strlen(sz);
   }
   return len + 1;
}

variable_types:
d
unsigned char *

variable_types:
year
unsigned short

variable_types:
month
unsigned char

variable_types:
day
unsigned char

variable_types:
hour
unsigned char

variable_types:
min
unsigned char

variable_types:
sec
unsigned char

variable_types:
sz
char [11]

variable_types:
len
unsigned short


original_code:
u_int convert_binary_time(u_char *t, u_char *to)
{
   u_char sign = 0;
   u_short days = 0;
   u_short hours = 0;
   u_short mins = 0;
   u_short secs = 0;
   char sz[11];
   u_short len;
   if(*t) {
      t++;
      sign = *t++;
      days = *((u_int *)t);
      t += 4;
      hours = *t++;
      mins = *t++;
      secs = *t++;
   }
   if(days)
      hours += (days * 24);
   sprintf(sz, "%s%02u:%02u:%02u", (sign ? "-" : ""), hours, mins, secs);
   to[0] = len = strlen(sz);
   memcpy(to + 1, sz, len);
   return len + 1;
}

canonical_original_code:
unsigned int convert_binary_time(unsigned char *t, unsigned char *to)
{
   unsigned char sign = 0;
   unsigned short days = 0;
   unsigned short hours = 0;
   unsigned short mins = 0;
   unsigned short secs = 0;
   char sz[11];
   unsigned short len;
   if(*t) {
      t++;
      sign = *t++;
      days = *((unsigned int *)t);
      t += 4;
      hours = *t++;
      mins = *t++;
      secs = *t++;
   }
   if(days)
      hours += (days * 24);
   sprintf(sz, "%s%02u:%02u:%02u", (sign ? "-" : ""), hours, mins, secs);
   to[0] = len = strlen(sz);
   memcpy(to + 1, sz, len);
   return len + 1;
}

variable_types:
t
unsigned char *

variable_types:
to
unsigned char *

variable_types:
sign
unsigned char

variable_types:
days
unsigned short

variable_types:
hours
unsigned short

variable_types:
mins
unsigned short

variable_types:
secs
unsigned short


decompiled_code:
__int64 __fastcall decode_len(unsigned __int8 *a1)
{
char v1; // al
unsigned int v2; // er8
__int64 result; // rax

v1 = *a1;
if ( *a1 <= 0xFBu )
{
decoded_len = *a1;
result = 1LL;
}
else
{
if ( v1 == -4 )
{
v2 = 2;
decoded_len = *((unsigned __int16 *)a1 + 1);
}
else if ( v1 == -3 )
{
v2 = 4;
decoded_len = *((unsigned int *)a1 + 1);
}
else
{
v2 = 0;
if ( v1 == -2 )
{
v2 = 8;
decoded_len = *(_QWORD *)(a1 + 1);
}
}
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 *a1)
{
char v1; // al
unsigned int v2; // er8
__int64 result; // rax

v1 = *a1;
if ( *a1 <= 0xFBu )
{
decoded_len = *a1;
result = 1LL;
}
else
{
if ( v1 == -4 )
{
v2 = 2;
decoded_len = *((unsigned __int16 *)a1 + 1);
}
else if ( v1 == -3 )
{
v2 = 4;
decoded_len = *((unsigned int *)a1 + 1);
}
else
{
v2 = 0;
if ( v1 == -2 )
{
v2 = 8;
decoded_len = *(_QWORD *)(a1 + 1);
}
}
result = v2;
}
return result;
}

original_code:
u_short decode_len(u_char *pkt)
{
   if(*pkt <= 0xFB) { decoded_len = *pkt; return 1; }
   if(*pkt == 0xFC) { decoded_len = *((u_short *)pkt + 1); return 2; }
   if(*pkt == 0xFD) { decoded_len = *((u_int *)pkt + 1); return 4; }
   if(*pkt == 0xFE) { memcpy((void *)&decoded_len, pkt + 1, 8); return 8; }
   return 0;
}

canonical_original_code:
unsigned short decode_len(unsigned char *pkt)
{
   if(*pkt <= 0xFB) { decoded_len = *pkt; return 1; }
   if(*pkt == 0xFC) { decoded_len = *((unsigned short *)pkt + 1); return 2; }
   if(*pkt == 0xFD) { decoded_len = *((unsigned int *)pkt + 1); return 4; }
   if(*pkt == 0xFE) { memcpy((void *)&decoded_len, pkt + 1, 8); return 8; }
   return 0;
}

variable_types:
pkt
unsigned char *

return_type: unsigned short