repo: billyauhk/elektroL-decode

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int16 v4; // r14
int v5; // er12
char *v6; // rax
int v7; // er13
__off_t v8; // rsi
size_t v9; // rdx
__off_t v10; // rsi
unsigned int v11; // eax
unsigned int v12; // ebx
unsigned int v13; // edx
char *v14; // rax
char *v15; // rsi
char v16; // dl
char v17; // cl
struct stat v19; // [rsp+0h] [rbp-898h] BYREF
char dest[2056]; // [rsp+90h] [rbp-808h] BYREF

if ( argc <= 1 )
{
puts("No filename!");
exit(-1);
}
printf("Filename: %s\n", argv[1]);
if ( (unsigned int)stat((char *)argv[1], &v19) == -1 )
{
perror("stat");
exit(-1);
}
printf("File size: %lld bytes\n", v19.st_size);
v3 = argv[1];
v4 = v3[strlen(v3) - 5];
printf("Channel number: %u\n", (unsigned __int16)(v4 - 48));
v5 = open(argv[1], 0);
if ( v5 == -1 )
{
perror("Cannot open input file");
exit(-1);
}
puts("File opened");
strncpy(dest, argv[1], 0x7D0uLL);
if ( dest[1999] )
{
puts("File name too long!");
exit(-1);
}
v6 = &dest[strlen(dest) - 3];
*(_WORD *)v6 = 25200;
v6[2] = 109;
printf("Output file name: %s\n", dest);
v7 = open(dest, 577, 420LL);
if ( v7 == -1 )
perror("Cannot open output file");
if ( (unsigned __int16)(v4 - 48) > 3u )
{
if ( (unsigned __int16)(v4 - 52) > 5u )
{
puts("Unknown channel name...");
exit(-1);
}
}
else if ( v4 != 48 )
{
v8 = lseek(v5, -248020992LL, 2);
if ( (_DWORD)v8 == -1 )
{
printf("Size; %d\n", 248020992LL);
perror("Cannot seek to position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v8);
strcpy(dest, "P5 11136 11136 1023\n");
v9 = 20LL;
goto LABEL_14;
}
v10 = lseek(v5, -15501312LL, 2);
if ( (_DWORD)v10 == -1 )
{
perror("Cannot seek to the position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v10);
strcpy(dest, "P5 2784 2784 1023\n");
v9 = 18LL;
LABEL_14:
if ( (unsigned int)write(v7, dest, v9) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
while ( 1 )
{
v11 = read(v5, dest, 0x7D0uLL);
v12 = v11;
if ( !v11 )
break;
v13 = v11 >> 1;
if ( v11 >> 1 )
{
v14 = &dest[1];
v15 = &dest[2 * v13 + 1];
do
{
v16 = *(v14 - 1);
v17 = *v14;
v14 += 2;
*(v14 - 3) = v17;
*(v14 - 2) = v16;
}
while ( v15 != v14 );
}
if ( v12 != (unsigned int)write(v7, dest, v12) )
perror("Something wrong with output file writing");
}
if ( close(v5) == -1 )
perror("Cannot close input file");
if ( close(v7) == -1 )
perror("Cannot close output file");
puts("Program ends here");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int16 v4; // r14
int v5; // er12
char *v6; // rax
int v7; // er13
__off_t v8; // rsi
size_t v9; // rdx
__off_t v10; // rsi
unsigned int v11; // eax
unsigned int v12; // ebx
unsigned int v13; // edx
char *v14; // rax
char *v15; // rsi
char v16; // dl
char v17; // cl
struct stat v19; // [rsp+0h] [rbp-898h] BYREF
char dest[2056]; // [rsp+90h] [rbp-808h] BYREF

if ( argc <= 1 )
{
puts("No filename!");
exit(-1);
}
printf("Filename: %s\n", argv[1]);
if ( (unsigned int)func9((char *)argv[1], &v19) == -1 )
{
perror("stat");
exit(-1);
}
printf("File size: %lld bytes\n", v19.st_size);
v3 = argv[1];
v4 = v3[strlen(v3) - 5];
printf("Channel number: %u\n", (unsigned __int16)(v4 - 48));
v5 = open(argv[1], 0);
if ( v5 == -1 )
{
perror("Cannot open input file");
exit(-1);
}
puts("File opened");
strncpy(dest, argv[1], 0x7D0uLL);
if ( dest[1999] )
{
puts("File name too long!");
exit(-1);
}
v6 = &dest[strlen(dest) - 3];
*(_WORD *)v6 = 25200;
v6[2] = 109;
printf("Output file name: %s\n", dest);
v7 = open(dest, 577, 420LL);
if ( v7 == -1 )
perror("Cannot open output file");
if ( (unsigned __int16)(v4 - 48) > 3u )
{
if ( (unsigned __int16)(v4 - 52) > 5u )
{
puts("Unknown channel name...");
exit(-1);
}
}
else if ( v4 != 48 )
{
v8 = lseek(v5, -248020992LL, 2);
if ( (_DWORD)v8 == -1 )
{
printf("Size; %d\n", 248020992LL);
perror("Cannot seek to position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v8);
strcpy(dest, "P5 11136 11136 1023\n");
v9 = 20LL;
goto LABEL_14;
}
v10 = lseek(v5, -15501312LL, 2);
if ( (_DWORD)v10 == -1 )
{
perror("Cannot seek to the position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v10);
strcpy(dest, "P5 2784 2784 1023\n");
v9 = 18LL;
LABEL_14:
if ( (unsigned int)write(v7, dest, v9) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
while ( 1 )
{
v11 = read(v5, dest, 0x7D0uLL);
v12 = v11;
if ( !v11 )
break;
v13 = v11 >> 1;
if ( v11 >> 1 )
{
v14 = &dest[1];
v15 = &dest[2 * v13 + 1];
do
{
v16 = *(v14 - 1);
v17 = *v14;
v14 += 2;
*(v14 - 3) = v17;
*(v14 - 2) = v16;
}
while ( v15 != v14 );
}
if ( v12 != (unsigned int)write(v7, dest, v12) )
perror("Something wrong with output file writing");
}
if ( close(v5) == -1 )
perror("Cannot close input file");
if ( close(v7) == -1 )
perror("Cannot close output file");
puts("Program ends here");
return 0;
}

original_code:
int main(int argc, char* argv[]){
  int retVal;
  int infile, outfile;
  long long filesize;
  unsigned short channel;
  struct stat sb;
  unsigned int readCount, i;
  char buffer[2000];
  char tempByte;
  if(argc>1){
    printf("Filename: %s\n",argv[1]);
  }else{
    printf("No filename!\n");
    exit(-1);
  }
  if((retVal = stat(argv[1],&sb)) == -1){
    perror("stat");exit(-1);
  }else{
    filesize = (long long) sb.st_size;
    printf("File size: %lld bytes\n",filesize);
  }
  channel = argv[1][strlen(argv[1])-5]-'0';
  printf("Channel number: %u\n",channel);
  if((infile = open(argv[1],00)) == -1){
    perror("Cannot open input file");exit(-1);
  }else{
    printf("File opened\n");
  }
  strncpy(buffer,argv[1],2000);
  if(buffer[2000 -1] != '\0'){
    printf("File name too long!\n");exit(-1);
  }
  strncpy(buffer+strlen(buffer)-3,"pbm",3);
  printf("Output file name: %s\n",buffer);
  if((outfile = open(buffer,01|0100|01000,0644)) == -1){
    perror("Cannot open output file");
  }
  switch(channel){
    case 1: case 2: case 3:
      if((retVal = lseek(infile,-11136*11136*2,2)) == -1){
        printf("Size; %d\n",11136*11136*2);
        perror("Cannot seek to position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 11136 11136 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    case 4: case 5: case 6: case 7:
    case 8: case 9: case 0:
      if((retVal = lseek(infile,-2784*2784*2,2)) == -1){
        perror("Cannot seek to the position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 2784 2784 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    default:
      printf("Unknown channel name...\n");exit(-1);
      break;
  }
  while((readCount = read(infile,buffer,2000)) != 0){
    for(i=0;i<(readCount >> 1);i++){
      buffer[2*i+1] ^= buffer[2*i+0];
      buffer[2*i+0] ^= buffer[2*i+1];
      buffer[2*i+1] ^= buffer[2*i+0];
    }
    if((retVal = write(outfile,buffer,readCount)) != readCount){
      perror("Something wrong with output file writing");
    }
  }
  if((retVal = close(infile)) == -1){
    perror("Cannot close input file");
  }
  if((retVal = close(outfile)) == -1){
    perror("Cannot close output file");
  }
  printf("Program ends here\n");
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  int retVal;
  int infile; int outfile;
  long long filesize;
  unsigned short channel;
  struct stat sb;
  unsigned int readCount; unsigned int i;
  char buffer[2000];
  char tempByte;
  if(argc>1){
    printf("Filename: %s\n",argv[1]);
  }else{
    printf("No filename!\n");
    exit(-1);
  }
  if((retVal = stat(argv[1],&sb)) == -1){
    perror("stat");exit(-1);
  }else{
    filesize = (long long) sb.st_size;
    printf("File size: %lld bytes\n",filesize);
  }
  channel = argv[1][strlen(argv[1])-5]-'0';
  printf("Channel number: %u\n",channel);
  if((infile = open(argv[1],00)) == -1){
    perror("Cannot open input file");exit(-1);
  }else{
    printf("File opened\n");
  }
  strncpy(buffer,argv[1],2000);
  if(buffer[2000 -1] != '\0'){
    printf("File name too long!\n");exit(-1);
  }
  strncpy(buffer+strlen(buffer)-3,"pbm",3);
  printf("Output file name: %s\n",buffer);
  if((outfile = open(buffer,01|0100|01000,0644)) == -1){
    perror("Cannot open output file");
  }
  switch(channel){
    case 1: case 2: case 3:
      if((retVal = lseek(infile,-11136*11136*2,2)) == -1){
        printf("Size; %d\n",11136*11136*2);
        perror("Cannot seek to position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 11136 11136 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    case 4: case 5: case 6: case 7:
    case 8: case 9: case 0:
      if((retVal = lseek(infile,-2784*2784*2,2)) == -1){
        perror("Cannot seek to the position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 2784 2784 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    default:
      printf("Unknown channel name...\n");exit(-1);
      break;
  }
  while((readCount = read(infile,buffer,2000)) != 0){
    for(i=0;i<(readCount >> 1);i++){
      buffer[2*i+1] ^= buffer[2*i+0];
      buffer[2*i+0] ^= buffer[2*i+1];
      buffer[2*i+1] ^= buffer[2*i+0];
    }
    if((retVal = write(outfile,buffer,readCount)) != readCount){
      perror("Something wrong with output file writing");
    }
  }
  if((retVal = close(infile)) == -1){
    perror("Cannot close input file");
  }
  if((retVal = close(outfile)) == -1){
    perror("Cannot close output file");
  }
  printf("Program ends here\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: retVal int

variable_types: infile int

variable_types: outfile int

variable_types:
filesize
long long

variable_types:
channel
unsigned short

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

variable_types:
readCount
unsigned int

variable_types:
i
unsigned int

variable_types:
buffer
char [2000]

variable_types: tempByte char

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


repo: bioinfo-cnb/bionmf-gpu

decompiled_code:
__int64 __fastcall print_generate_matrix_help(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"Tool to generate (in CPU) a matrix with random values\n"
"\n"
"Usage:\n"
"\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(
0LL,
"\n"
"Note: Some of the previous options are read for compatibility reasons, but they are ignored by "
"the program.\n"
"\n"
"<output_filename>\n"
"\tOutput file (mandatory if 'help' is not requested).\n"
"\n"
"<rows> <columns>\n"
"\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n"
"\n"
"<max_value>\n"
"\tRange for random values: [0.0 ... <max_value>].\n"
"\tValid values are in range (%g ... %g). The default is %g\n"
"\n"
"-h,-H\tPrints this help message.\n"
"\n",
1.175494350822288e-38,
3.402823466385289e38,
1.0) == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_generate_matrix_help( execname )");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"Tool to generate (in CPU) a matrix with random values\n"
"\n"
"Usage:\n"
"\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(
0LL,
"\n"
"Note: Some of the previous options are read for compatibility reasons, but they are ignored by "
"the program.\n"
"\n"
"<output_filename>\n"
"\tOutput file (mandatory if 'help' is not requested).\n"
"\n"
"<rows> <columns>\n"
"\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n"
"\n"
"<max_value>\n"
"\tRange for random values: [0.0 ... <max_value>].\n"
"\tValid values are in range (%g ... %g). The default is %g\n"
"\n"
"-h,-H\tPrints this help message.\n"
"\n",
1.175494350822288e-38,
3.402823466385289e38,
1.0) == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_generate_matrix_help( execname )");
result = 1LL;
}
return result;
}

original_code:
static int print_generate_matrix_help( char const *restrict const execname )
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_generate_matrix_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "Tool to generate (in CPU) a matrix with random values\n\n"
    "Usage:\n\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
    "\t%s -h\n\n", execname, execname );
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\nNote: Some of the previous options are read for compatibility reasons, "
    "but they are ignored by the program.\n\n"
    "<output_filename>\n\tOutput file (mandatory if 'help' is not requested).\n\n"
    "<rows> <columns>\n\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n\n"
    "<max_value>\n\tRange for random values: [0.0 ... <max_value>].\n"
    "\tValid values are in range (%g ... %g). The default is %g\n\n"
    "-h,-H\tPrints this help message.\n\n", ( ( 1.17549435082228750796873653722224568e-38F ) ), ( 3.40282346638528859811704183484516925e+38F ), ( (1.0f) ) ) != 0 )
  status = 1;
 return status;
}

canonical_original_code:
static int print_generate_matrix_help(char *execname)
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_generate_matrix_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "Tool to generate (in CPU) a matrix with random values\n\n"
    "Usage:\n\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
    "\t%s -h\n\n", execname, execname );
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\nNote: Some of the previous options are read for compatibility reasons, "
    "but they are ignored by the program.\n\n"
    "<output_filename>\n\tOutput file (mandatory if 'help' is not requested).\n\n"
    "<rows> <columns>\n\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n\n"
    "<max_value>\n\tRange for random values: [0.0 ... <max_value>].\n"
    "\tValid values are in range (%g ... %g). The default is %g\n\n"
    "-h,-H\tPrints this help message.\n\n", ( ( 1.17549435082228750796873653722224568e-38F ) ), ( 3.40282346638528859811704183484516925e+38F ), ( (1.0f) ) ) != 0 )
  status = 1;
 return status;
}

variable_types:
execname
char *

variable_types: status int

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
unsigned __int64 v4; // r12
unsigned __int64 v5; // r14
float *v6; // r13
unsigned int v7; // eax
unsigned int v8; // ebp
float *v9; // rbx
__int64 i; // r14
__int64 v11; // rax
int v12; // ebx
__int64 v13; // rax
int *v14; // rax
__int64 v16; // [rsp+0h] [rbp-B8h]
unsigned int v17; // [rsp+Ch] [rbp-ACh]
char v18; // [rsp+13h] [rbp-A5h] BYREF
unsigned int v19; // [rsp+14h] [rbp-A4h] BYREF
unsigned int v20; // [rsp+18h] [rbp-A0h] BYREF
float v21; // [rsp+1Ch] [rbp-9Ch] BYREF
char v22[48]; // [rsp+20h] [rbp-98h] BYREF
__int64 v23; // [rsp+50h] [rbp-68h] BYREF
unsigned int v24; // [rsp+60h] [rbp-58h]
unsigned int v25; // [rsp+7Ch] [rbp-3Ch]

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v18 = 0;
if ( (unsigned int)check_arguments((unsigned int)argc, argv, &v18, &v23) )
goto LABEL_14;
if ( v18 )
{
LODWORD(v13) = print_generate_matrix_help(*argv);
return v13;
}
v19 = 0;
v3 = v24;
v16 = v23;
v20 = 0;
v21 = 1.0;
if ( (unsigned int)check_additional_arguments_constprop_0(argc, (__int64)argv, v25, &v19, &v20, &v21) )
goto LABEL_14;
v17 = get_padding(v20);
v4 = v19 * (unsigned __int64)v20;
v5 = v17 * (unsigned __int64)v19;
print_message(
0LL,
"Generating a %u-by-%u data matrix (%zu items), with random values in range [0 .. %g]...\n",
v19,
v20,
v4,
v21);
if ( (v20 == 1) + (v19 == 1) )
append_printed_error(
0LL,
"\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n");
v6 = (float *)malloc(4 * v5);
if ( !v6 )
{
v14 = _errno_location();
print_errnum(0LL, (unsigned int)*v14, "malloc(matrix, items=%zu, %zu with padding)", v4, v5);
LABEL_14:
LODWORD(v13) = 1;
return v13;
}
v7 = get_seed();
srandom(v7);
if ( v19 )
{
v8 = 0;
v9 = v6;
do
{
for ( i = 0LL; v20 > (unsigned int)i; ++i )
v9[i] = (float)(4.6566129e-10 * v21) * (float)(int)random();
++v8;
v9 += v17;
}
while ( v19 > v8 );
}
v11 = new_empty_matrix_tags(v22);
v12 = matrix_save(v16, v3, v6, v19, v20, v17, 0LL, v22, 1LL, v11);
free(v6);
return v12 != 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
unsigned __int64 v4; // r12
unsigned __int64 v5; // r14
float *v6; // r13
unsigned int v7; // eax
unsigned int v8; // ebp
float *v9; // rbx
__int64 i; // r14
__int64 v11; // rax
int v12; // ebx
__int64 v13; // rax
int *v14; // rax
__int64 v16; // [rsp+0h] [rbp-B8h]
unsigned int v17; // [rsp+Ch] [rbp-ACh]
char v18; // [rsp+13h] [rbp-A5h] BYREF
unsigned int v19; // [rsp+14h] [rbp-A4h] BYREF
unsigned int v20; // [rsp+18h] [rbp-A0h] BYREF
float v21; // [rsp+1Ch] [rbp-9Ch] BYREF
char v22[48]; // [rsp+20h] [rbp-98h] BYREF
__int64 v23; // [rsp+50h] [rbp-68h] BYREF
unsigned int v24; // [rsp+60h] [rbp-58h]
unsigned int v25; // [rsp+7Ch] [rbp-3Ch]

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v18 = 0;
if ( (unsigned int)check_arguments((unsigned int)argc, argv, &v18, &v23) )
goto LABEL_14;
if ( v18 )
{
LODWORD(v13) = func0(*argv);
return v13;
}
v19 = 0;
v3 = v24;
v16 = v23;
v20 = 0;
v21 = 1.0;
if ( (unsigned int)func1(argc, (__int64)argv, v25, &v19, &v20, &v21) )
goto LABEL_14;
v17 = get_padding(v20);
v4 = v19 * (unsigned __int64)v20;
v5 = v17 * (unsigned __int64)v19;
print_message(
0LL,
"Generating a %u-by-%u data matrix (%zu items), with random values in range [0 .. %g]...\n",
v19,
v20,
v4,
v21);
if ( (v20 == 1) + (v19 == 1) )
append_printed_error(
0LL,
"\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n");
v6 = (float *)malloc(4 * v5);
if ( !v6 )
{
v14 = _errno_location();
print_errnum(0LL, (unsigned int)*v14, "malloc(matrix, items=%zu, %zu with padding)", v4, v5);
LABEL_14:
LODWORD(v13) = 1;
return v13;
}
v7 = get_seed();
srandom(v7);
if ( v19 )
{
v8 = 0;
v9 = v6;
do
{
for ( i = 0LL; v20 > (unsigned int)i; ++i )
v9[i] = (float)(4.6566129e-10 * v21) * (float)(int)random();
++v8;
v9 += v17;
}
while ( v19 > v8 );
}
v11 = new_empty_matrix_tags(v22);
v12 = matrix_save(v16, v3, v6, v19, v20, v17, 0LL, v22, 1LL, v11);
free(v6);
return v12 != 0;
}

original_code:
int main( int argc, char *argv[] )
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char const *restrict *restrict) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_generate_matrix_help( argv[0] );
 char const *restrict const filename = arguments.filename;
 file_fmt_t const output_file_fmt = arguments.output_file_fmt;
 index_t const idx_other_args = arguments.idx_other_args;
 index_t nrows = 0, ncols = 0;
 real max_value = ( (1.0f) );
 status = check_additional_arguments( argc, (char const *restrict *restrict) argv, idx_other_args, &nrows, &ncols, &max_value );
 if ( status != 0 )
  return 1;
 index_t const pitch = get_padding( ncols );
 size_t const nitems = (size_t) nrows * (size_t) ncols;
 size_t const nitems_padding = (size_t) nrows * (size_t) pitch;
 print_message( shown_by_all, "Generating a %" "u" "-by-%" "u" " data matrix (%zu"
   " items), with random values in range [0 .. %g]...\n", nrows, ncols, nitems, max_value );
 if ( (nrows == 1) + (ncols == 1) )
  append_printed_error( shown_by_all,
     "\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n" );
 real *restrict const matrix = (real *restrict) malloc( nitems_padding * sizeof(real) );
 if ( ! matrix ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "malloc(matrix, items=%zu, %zu with padding)", nitems, nitems_padding );
  return 1;
 }
 index_t seed = get_seed();
 srandom( (unsigned int) seed );
 real const denominator = (real) 2147483647;
 real *pmatrix = matrix;
 for ( index_t i=0 ; i < nrows ; i++, pmatrix += pitch ) {
  real *pmatrix_r = pmatrix;
  for ( index_t j=0 ; j < ncols ; j++, pmatrix_r++ ) {
   real const numerator = (real) random();
   real const val0_1 = numerator / denominator;
   real const val = val0_1 * max_value;
   *pmatrix_r = val;
  }
 }
 _Bool const transpose = 0;
 _Bool const verbose = 1;
 struct matrix_tags_t const mt = new_empty_matrix_tags();
 status = matrix_save( filename, output_file_fmt, matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( matrix );
 if ( status != 0 )
  return 1;
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char **) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_generate_matrix_help( argv[0] );
 char *filename = arguments.filename;
 enum file_format output_file_fmt = arguments.output_file_fmt;
 unsigned int idx_other_args = arguments.idx_other_args;
 unsigned int nrows = 0; unsigned int ncols = 0;
 float max_value = ( (1.0f) );
 status = check_additional_arguments( argc, (char **) argv, idx_other_args, &nrows, &ncols, &max_value );
 if ( status != 0 )
  return 1;
 unsigned int pitch = get_padding( ncols );
 unsigned long nitems = (size_t) nrows * (size_t) ncols;
 unsigned long nitems_padding = (size_t) nrows * (size_t) pitch;
 print_message( shown_by_all, "Generating a %" "u" "-by-%" "u" " data matrix (%zu"
   " items), with random values in range [0 .. %g]...\n", nrows, ncols, nitems, max_value );
 if ( (nrows == 1) + (ncols == 1) )
  append_printed_error( shown_by_all,
     "\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n" );
 float *matrix = (real *restrict) malloc( nitems_padding * sizeof(real) );
 if ( ! matrix ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "malloc(matrix, items=%zu, %zu with padding)", nitems, nitems_padding );
  return 1;
 }
 unsigned int seed = get_seed();
 srandom( (unsigned int) seed );
 float denominator = (real) 2147483647;
 float *pmatrix = matrix;
 for ( unsigned int i = 0; i < nrows ; i++, pmatrix += pitch ) {
  float *pmatrix_r = pmatrix;
  for ( unsigned int j = 0; j < ncols ; j++, pmatrix_r++ ) {
   float numerator = (real) random();
   float val0_1 = numerator / denominator;
   float val = val0_1 * max_value;
   *pmatrix_r = val;
  }
 }
 _Bool transpose = 0;
 _Bool verbose = 1;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 status = matrix_save( filename, output_file_fmt, matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( matrix );
 if ( status != 0 )
  return 1;
 return 0;
}

variable_types: struct input_arguments { char *filename; _Bool numeric_hdrs; _Bool numeric_lbls; enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 } input_file_fmt; enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 } output_file_fmt; unsigned int k; unsigned int kp; unsigned int nIters; unsigned int niter_test_conv; unsigned int stop_threshold; unsigned int gpu_device; unsigned int idx_other_args; }

variable_types:
filename
char *

variable_types:
nrows
unsigned int

variable_types:
ncols
unsigned int

variable_types:
pitch
unsigned int

variable_types:
nitems
unsigned long

variable_types:
matrix
float *

variable_types:
seed
unsigned int

variable_types: denominator float

variable_types:
pmatrix
float *

variable_types:
j
unsigned int

variable_types: numerator float

variable_types: val float

variable_types: struct matrix_tags_t { char *name; struct tag_t { char *tokens; char **ptokens; } headers; struct tag_t labels; }

user_defined_types: struct input_arguments { char *filename; _Bool numeric_hdrs; _Bool numeric_lbls; enum file_format input_file_fmt; enum file_format output_file_fmt; unsigned int k; unsigned int kp; unsigned int nIters; unsigned int niter_test_conv; unsigned int stop_threshold; unsigned int gpu_device; unsigned int idx_other_args; }

user_defined_types: struct matrix_tags_t { char *name; struct tag_t headers; struct tag_t labels; }


decompiled_code:
__int64 __fastcall print_file_converter_help(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"\t<< ASCII-Binary file converter >>\n"
"\n"
"Usage:\n"
"\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)append_printed_message(
0LL,
"\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n") )
v1 = 0;
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(0LL, "\n-h,-H\tPrints this help message.\n\n") == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_file_converter_help( execname )");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"\t<< ASCII-Binary file converter >>\n"
"\n"
"Usage:\n"
"\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)append_printed_message(
0LL,
"\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n") )
v1 = 0;
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(0LL, "\n-h,-H\tPrints this help message.\n\n") == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_file_converter_help( execname )");
result = 1LL;
}
return result;
}

original_code:
static int print_file_converter_help( char const *restrict const execname )
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_file_converter_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "\t<< ASCII-Binary file converter >>\n\n"
    "Usage:\n\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n\t%s -h\n\n",
    execname, execname );
 if ( append_printed_message( shown_by_all,
    "\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n" ) != 0 )
  status = 0;
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\n-h,-H\tPrints this help message.\n\n" ) != 0 )
  status = 1;
 return status;
}

canonical_original_code:
static int print_file_converter_help(char *execname)
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_file_converter_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "\t<< ASCII-Binary file converter >>\n\n"
    "Usage:\n\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n\t%s -h\n\n",
    execname, execname );
 if ( append_printed_message( shown_by_all,
    "\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n" ) != 0 )
  status = 0;
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\n-h,-H\tPrints this help message.\n\n" ) != 0 )
  status = 1;
 return status;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
char *v4; // r15
unsigned __int8 v5; // bl
unsigned __int8 v6; // r13
unsigned int v7; // er14
__int64 v8; // rcx
size_t v9; // rbx
size_t v10; // rax
char *v11; // r13
int *v12; // rax
char *v13; // rcx
int *v14; // rbx
__int64 v15; // rax
__int64 v16; // rcx
char *v17; // rsi
__int64 *v18; // rdi
__int64 v20; // [rsp-30h] [rbp-F8h] BYREF
char *v21; // [rsp+8h] [rbp-C0h]
char v22; // [rsp+1Bh] [rbp-ADh] BYREF
unsigned int v23; // [rsp+1Ch] [rbp-ACh] BYREF
unsigned int v24; // [rsp+20h] [rbp-A8h] BYREF
unsigned int v25; // [rsp+24h] [rbp-A4h] BYREF
__int64 v26; // [rsp+28h] [rbp-A0h] BYREF
char v27[48]; // [rsp+30h] [rbp-98h] BYREF
char *s; // [rsp+60h] [rbp-68h] BYREF
unsigned __int8 v29; // [rsp+68h] [rbp-60h]
unsigned __int8 v30; // [rsp+69h] [rbp-5Fh]
int v31; // [rsp+6Ch] [rbp-5Ch]
unsigned int v32; // [rsp+70h] [rbp-58h]

v3 = 1;
process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v22 = 0;
if ( !(unsigned int)check_arguments((unsigned int)argc, argv, &v22, &s) )
{
if ( v22 )
{
v3 = print_file_converter_help(*argv);
}
else
{
v4 = s;
v26 = 0LL;
v5 = v29;
v6 = v30;
v23 = 0;
LODWORD(v21) = v31;
v7 = v32;
v24 = 0;
v25 = 0;
new_empty_matrix_tags(v27);
if ( (unsigned int)matrix_load(v4, v5, v6, (unsigned int)v21, &v26, &v23, &v24, &v25, v27, v8) )
{
print_error(0LL, "Error reading '%s'\n", v4);
}
else
{
v21 = (char *)file_extension[v7];
v9 = strlen(v4);
v10 = strlen(v21);
v11 = (char *)malloc(v9 + v10);
v12 = _errno_location();
v13 = v21;
v14 = v12;
if ( v11 )
{
*v12 = 0;
if ( sprintf(v11, "%s%s", v4, v13) <= 0 )
{
print_errnum(0LL, (unsigned int)*v14, "Error setting output filename (sprintf)");
}
else
{
v15 = print_message(0LL, "Output file: %s\n", v11);
v3 = matrix_save(v11, v7, v26, v23, v24, v25, 0LL, v27, 1LL, v15);
}
free(v11);
}
else
{
print_errnum(0LL, (unsigned int)*v12, "Error setting output filename: malloc(filename_str)");
}
v16 = 10LL;
v17 = v27;
v18 = &v20;
while ( v16 )
{
*(_DWORD *)v18 = *(_DWORD *)v17;
v17 += 4;
v18 = (__int64 *)((char *)v18 + 4);
--v16;
}
matrix_clean(v26, v17);
}
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
char *v4; // r15
unsigned __int8 v5; // bl
unsigned __int8 v6; // r13
unsigned int v7; // er14
__int64 v8; // rcx
size_t v9; // rbx
size_t v10; // rax
char *v11; // r13
int *v12; // rax
char *v13; // rcx
int *v14; // rbx
__int64 v15; // rax
__int64 v16; // rcx
char *v17; // rsi
__int64 *v18; // rdi
__int64 v20; // [rsp-30h] [rbp-F8h] BYREF
char *v21; // [rsp+8h] [rbp-C0h]
char v22; // [rsp+1Bh] [rbp-ADh] BYREF
unsigned int v23; // [rsp+1Ch] [rbp-ACh] BYREF
unsigned int v24; // [rsp+20h] [rbp-A8h] BYREF
unsigned int v25; // [rsp+24h] [rbp-A4h] BYREF
__int64 v26; // [rsp+28h] [rbp-A0h] BYREF
char v27[48]; // [rsp+30h] [rbp-98h] BYREF
char *s; // [rsp+60h] [rbp-68h] BYREF
unsigned __int8 v29; // [rsp+68h] [rbp-60h]
unsigned __int8 v30; // [rsp+69h] [rbp-5Fh]
int v31; // [rsp+6Ch] [rbp-5Ch]
unsigned int v32; // [rsp+70h] [rbp-58h]

v3 = 1;
process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v22 = 0;
if ( !(unsigned int)check_arguments((unsigned int)argc, argv, &v22, &s) )
{
if ( v22 )
{
v3 = func0(*argv);
}
else
{
v4 = s;
v26 = 0LL;
v5 = v29;
v6 = v30;
v23 = 0;
LODWORD(v21) = v31;
v7 = v32;
v24 = 0;
v25 = 0;
new_empty_matrix_tags(v27);
if ( (unsigned int)matrix_load(v4, v5, v6, (unsigned int)v21, &v26, &v23, &v24, &v25, v27, v8) )
{
print_error(0LL, "Error reading '%s'\n", v4);
}
else
{
v21 = (char *)file_extension[v7];
v9 = strlen(v4);
v10 = strlen(v21);
v11 = (char *)malloc(v9 + v10);
v12 = _errno_location();
v13 = v21;
v14 = v12;
if ( v11 )
{
*v12 = 0;
if ( sprintf(v11, "%s%s", v4, v13) <= 0 )
{
print_errnum(0LL, (unsigned int)*v14, "Error setting output filename (sprintf)");
}
else
{
v15 = print_message(0LL, "Output file: %s\n", v11);
v3 = matrix_save(v11, v7, v26, v23, v24, v25, 0LL, v27, 1LL, v15);
}
free(v11);
}
else
{
print_errnum(0LL, (unsigned int)*v12, "Error setting output filename: malloc(filename_str)");
}
v16 = 10LL;
v17 = v27;
v18 = &v20;
while ( v16 )
{
*(_DWORD *)v18 = *(_DWORD *)v17;
v17 += 4;
v18 = (__int64 *)((char *)v18 + 4);
--v16;
}
matrix_clean(v26, v17);
}
}
}
return v3;
}

original_code:
int main( int argc, char *argv[] )
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char const *restrict *restrict) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_file_converter_help( argv[0] );
 char const *restrict const filename = arguments.filename;
 _Bool const numeric_hdrs = arguments.numeric_hdrs;
 _Bool const numeric_lbls = arguments.numeric_lbls;
 file_fmt_t const input_file_fmt = arguments.input_file_fmt;
 file_fmt_t const output_file_fmt = arguments.output_file_fmt;
 real *restrict matrix = ((void *)0);
 index_t nrows = 0, ncols = 0, pitch = 0;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 _Bool const transpose = 0;
 status = matrix_load( filename, numeric_hdrs, numeric_lbls, input_file_fmt, &matrix, &nrows, &ncols, &pitch, &mt );
 if ( status != 0 ) {
  print_error( error_shown_by_all, "Error reading '%s'\n", filename );
  return 1;
 }
 char const *restrict const p_file_ext = file_extension[ output_file_fmt ];
 char *restrict const filename_str = malloc( (strlen(filename) + strlen(p_file_ext)) * sizeof(char) );
 if ( ! filename_str ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename: malloc(filename_str)" );
  matrix_clean( matrix, mt );
  return 1;
 }
 (*__errno_location ()) = 0;
 int const conv = sprintf( filename_str, "%s%s", filename, p_file_ext );
 if ( conv <= 0 ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename (sprintf)" );
  free( filename_str ); matrix_clean( matrix, mt );
  return 1;
 }
 print_message( shown_by_all, "Output file: %s\n", filename_str );
 _Bool const verbose = 1;
 status = matrix_save( filename_str, output_file_fmt, (real const *restrict) matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( filename_str );
 matrix_clean( matrix, mt );
 return status;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char **) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_file_converter_help( argv[0] );
 char *filename = arguments.filename;
 _Bool numeric_hdrs = arguments.numeric_hdrs;
 _Bool numeric_lbls = arguments.numeric_lbls;
 enum file_format input_file_fmt = arguments.input_file_fmt;
 enum file_format output_file_fmt = arguments.output_file_fmt;
 float *matrix = ((void *)0);
 unsigned int nrows = 0; unsigned int ncols = 0; unsigned int pitch = 0;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 _Bool transpose = 0;
 status = matrix_load( filename, numeric_hdrs, numeric_lbls, input_file_fmt, &matrix, &nrows, &ncols, &pitch, &mt );
 if ( status != 0 ) {
  print_error( error_shown_by_all, "Error reading '%s'\n", filename );
  return 1;
 }
 char *p_file_ext = file_extension[ output_file_fmt ];
 char *filename_str = malloc( (strlen(filename) + strlen(p_file_ext)) * sizeof(char) );
 if ( ! filename_str ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename: malloc(filename_str)" );
  matrix_clean( matrix, mt );
  return 1;
 }
 (*__errno_location ()) = 0;
 int conv = sprintf( filename_str, "%s%s", filename, p_file_ext );
 if ( conv <= 0 ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename (sprintf)" );
  free( filename_str ); matrix_clean( matrix, mt );
  return 1;
 }
 print_message( shown_by_all, "Output file: %s\n", filename_str );
 _Bool verbose = 1;
 status = matrix_save( filename_str, output_file_fmt, (float *) matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( filename_str );
 matrix_clean( matrix, mt );
 return status;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 }

variable_types: conv int

user_defined_types: struct tag_t { char *tokens; char **ptokens; }


bail

repo: bitbanger/roommate

decompiled_code:
void __fastcall __noreturn bail(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "%s: %s\n", a1, v2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "%s: %s\n", a1, v2);
exit(1);
}

original_code:
void bail(const char *problem) {
        fprintf(stderr, "%s: %s\n", problem, strerror((*__errno_location ())));
        exit(1);
}

canonical_original_code:
void bail(char *problem) {
        fprintf(stderr, "%s: %s\n", problem, strerror((*__errno_location ())));
        exit(1);
}

variable_types:
problem
char *

return_type: void


sendpkt

decompiled_code:
int __fastcall sendpkt(void *buf)
{
int v1; // eax
int v2; // ebp
void *v3; // rdx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
bail("socket()");
v2 = v1;
v5[0].sa_family = 2;
*(_WORD *)v5[0].sa_data = _bswap_16_constprop_0(2LL, "127.0.0.1", &v5[0].sa_data[2]);
inet_pton(2, "127.0.0.1", v3);
if ( sendto(v2, buf, 4uLL, 0, v5, 0x10u) < 0 )
bail("sendto()");
return close(v2);
}

canonical_decompiled_code:
int __fastcall func10(void *buf)
{
int v1; // eax
int v2; // ebp
void *v3; // rdx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
func9("socket()");
v2 = v1;
v5[0].sa_family = 2;
*(_WORD *)v5[0].sa_data = func7(2LL, "127.0.0.1", &v5[0].sa_data[2]);
inet_pton(2, "127.0.0.1", v3);
if ( sendto(v2, buf, 4uLL, 0, v5, 0x10u) < 0 )
func9("sendto()");
return close(v2);
}

original_code:
void sendpkt(const uint8_t *pkt) {
 int sfd = -1;
 if((sfd = socket(2, SOCK_DGRAM, 0)) < 0)
  bail("socket()");
 struct sockaddr_in addr;
 addr.sin_family = 2;
 addr.sin_port = htons(PORT);
 inet_pton(2, "127.0.0.1", &addr.sin_addr);
 if(sendto(sfd, pkt, PCKT_LEN, 0, (struct sockaddr *)&addr, sizeof addr) < 0)
  bail("sendto()");
 close(sfd);
}

canonical_original_code:
void sendpkt(unsigned char *pkt) {
 int sfd = -1;
 if((sfd = socket(2, SOCK_DGRAM, 0)) < 0)
  bail("socket()");
 struct sockaddr_in addr;
 addr.sin_family = 2;
 addr.sin_port = htons(PORT);
 inet_pton(2, "127.0.0.1", &addr.sin_addr);
 if(sendto(sfd, pkt, PCKT_LEN, 0, (struct sockaddr *)&addr, sizeof addr) < 0)
  bail("sendto()");
 close(sfd);
}

variable_types:
pkt
unsigned char *

variable_types: sfd int

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

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

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


cmdWren

repo: bitcraze/crazyradio-firmware

decompiled_code:
__int64 cmdWren()
{
unsigned int v0; // er12

buffer = 6;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12

buffer = 6;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

original_code:
int cmdWren() {
  int i=0;
  int ret;
  buffer[i++] = 0x06;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWren() {
  int i = 0;
  int ret;
  buffer[i++] = 0x06;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: i int

variable_types: ret int


cmdWrdis

decompiled_code:
__int64 cmdWrdis()
{
unsigned int v0; // er12

buffer = 4;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // er12

buffer = 4;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

original_code:
int cmdWrdis() {
  int i=0;
  int ret;
  buffer[i++] = 0x04;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWrdis() {
  int i = 0;
  int ret;
  buffer[i++] = 0x04;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}


cmdRdsr

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

buffer = 5;
spiSetCS(0LL);
spiSend(&buffer, 1LL);
v1 = spiReceive(&buffer, 1LL);
spiSetCS(1LL);
*a1 = buffer;
return v1;
}

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

buffer = 5;
spiSetCS(0LL);
spiSend(&buffer, 1LL);
v1 = spiReceive(&buffer, 1LL);
spiSetCS(1LL);
*a1 = buffer;
return v1;
}

original_code:
int cmdRdsr(char *fsr) {
  int i=0;
  int ret;
  buffer[i++] = 0x05;
  spiSetCS(0);
  spiSend(buffer, i);
  ret = spiReceive(buffer, 1);
  spiSetCS(1);
  *fsr = buffer[0];
  return ret;
}

canonical_original_code:
int cmdRdsr(char *fsr) {
  int i = 0;
  int ret;
  buffer[i++] = 0x05;
  spiSetCS(0);
  spiSend(buffer, i);
  ret = spiReceive(buffer, 1);
  spiSetCS(1);
  *fsr = buffer[0];
  return ret;
}

variable_types:
fsr
char *


cmdWrsr

decompiled_code:
__int64 __fastcall cmdWrsr(char a1)
{
unsigned int v1; // er12

LOBYTE(word_701) = a1;
buffer = 1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
unsigned int v1; // er12

LOBYTE(word_701) = a1;
buffer = 1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

original_code:
int cmdWrsr(char fsr) {
  int i=0;
  int ret;
  buffer[i++] = 0x01;
  buffer[i++] = fsr;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWrsr(char fsr) {
  int i = 0;
  int ret;
  buffer[i++] = 0x01;
  buffer[i++] = fsr;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: fsr char


cmdRead

decompiled_code:
__int64 __fastcall cmdRead(__int16 a1, __int64 a2, int a3)
{
unsigned int v6; // ebx
int v7; // er13
__int64 v8; // rdi
__int64 v9; // rsi
unsigned int v10; // er12
unsigned int v12; // [rsp+8h] [rbp-40h]
int v13; // [rsp+Ch] [rbp-3Ch]

if ( a3 <= 0 )
{
v10 = 0;
}
else
{
v6 = 0;
v13 = a3 - 1;
v7 = 0;
v12 = ((a3 - 1) & 0xFFFFFF80) + 128;
do
{
printf("\rRead byte %d/%d (%3d%%)", v6, (unsigned int)a3, (unsigned int)(v7 / a3));
fflush(stdout);
buffer = 3;
word_701 = __ROL2__(a1, 8);
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v8 = a2;
v9 = a3 - v6;
if ( (int)v9 > 128 )
v9 = 128LL;
v6 += 128;
a1 += 128;
a2 += 128LL;
v7 += 12800;
spiReceive(v8, v9);
spiSetCS(1LL);
}
while ( v6 != v12 );
v10 = (v13 & 0xFFFFFF80) + 128;
}
printf("\rRead byte %d/%d (%3d%%)\n", v10, (unsigned int)a3, 100LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1, __int64 a2, int a3)
{
unsigned int v6; // ebx
int v7; // er13
__int64 v8; // rdi
__int64 v9; // rsi
unsigned int v10; // er12
unsigned int v12; // [rsp+8h] [rbp-40h]
int v13; // [rsp+Ch] [rbp-3Ch]

if ( a3 <= 0 )
{
v10 = 0;
}
else
{
v6 = 0;
v13 = a3 - 1;
v7 = 0;
v12 = ((a3 - 1) & 0xFFFFFF80) + 128;
do
{
printf("\rRead byte %d/%d (%3d%%)", v6, (unsigned int)a3, (unsigned int)(v7 / a3));
fflush(stdout);
buffer = 3;
word_701 = __ROL2__(a1, 8);
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v8 = a2;
v9 = a3 - v6;
if ( (int)v9 > 128 )
v9 = 128LL;
v6 += 128;
a1 += 128;
a2 += 128LL;
v7 += 12800;
spiReceive(v8, v9);
spiSetCS(1LL);
}
while ( v6 != v12 );
v10 = (v13 & 0xFFFFFF80) + 128;
}
printf("\rRead byte %d/%d (%3d%%)\n", v10, (unsigned int)a3, 100LL);
return v10;
}

original_code:
int cmdRead(int addr, char *data, int len)
{
  int i=0;
  for(i=0; i<len; i+=128) {
    printf("\rRead byte %d/%d (%3d%%)", i, len, (100*i)/len);
    fflush(stdout);
    buffer[0] = 0x03;
    buffer[1] = ((addr+i)>>8)&0x0FF;
    buffer[2] = (addr+i)&0x0FF;
    spiSetCS(0);
    spiSend(buffer, 3);
    spiReceive(data+i, ((len-i)>128)?128:(len-i) );
    spiSetCS(1);
  }
  printf("\rRead byte %d/%d (%3d%%)\n", i, len, 100);
  return i;
}

canonical_original_code:
int cmdRead(int addr, char *data, int len)
{
  int i = 0;
  for(i=0; i<len; i+=128) {
    printf("\rRead byte %d/%d (%3d%%)", i, len, (100*i)/len);
    fflush(stdout);
    buffer[0] = 0x03;
    buffer[1] = ((addr+i)>>8)&0x0FF;
    buffer[2] = (addr+i)&0x0FF;
    spiSetCS(0);
    spiSend(buffer, 3);
    spiReceive(data+i, ((len-i)>128)?128:(len-i) );
    spiSetCS(1);
  }
  printf("\rRead byte %d/%d (%3d%%)\n", i, len, 100);
  return i;
}

variable_types: addr int

variable_types:
data
char *

variable_types: len int


cmdErasePage

decompiled_code:
__int64 __fastcall cmdErasePage(char a1)
{
unsigned int v1; // er12

LOBYTE(word_701) = a1;
buffer = 82;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1)
{
unsigned int v1; // er12

LOBYTE(word_701) = a1;
buffer = 82;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

original_code:
int cmdErasePage(int pageNo) {
  int i=0;
  int ret;
  buffer[i++] = 0x52;
  buffer[i++] = pageNo;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdErasePage(int pageNo) {
  int i = 0;
  int ret;
  buffer[i++] = 0x52;
  buffer[i++] = pageNo;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: pageNo int


cmdProgram

decompiled_code:
__int64 __fastcall cmdProgram(int a1, __int64 a2, signed int a3)
{
int v3; // er12
signed int v6; // er14
unsigned int v7; // ebp
unsigned int v8; // er15
int v9; // ebp
__int64 v10; // rsi
struct _IO_FILE *v11; // rdi
char *v12; // r14
__int64 v13; // rdx
char *v14; // rax
__int64 v15; // rsi
__int64 result; // rax
signed int v17; // eax
__int16 v18; // [rsp+0h] [rbp-58h]
unsigned int v19; // [rsp+4h] [rbp-54h]
unsigned int v20; // [rsp+8h] [rbp-50h]
int v21; // [rsp+Ch] [rbp-4Ch]
char v22[57]; // [rsp+1Fh] [rbp-39h] BYREF

if ( a1 > 30720 )
{
puts("ERROR: Writing above 30K is disabled!");
result = 0xFFFFFFFFLL;
}
else
{
v3 = a1 + a3;
v6 = a3;
if ( a1 + a3 > 30720 )
{
v6 = 30720 - a1;
printf("WARNING Write above 30K disabled! The new write length is %d\n", (unsigned int)(30720 - a1));
}
v7 = a1 / 512;
v19 = a1 / 512;
v8 = (a1 / 512) << 9;
if ( (v6 & 0x1FF) != 0 )
{
v17 = v6 + 511;
if ( v6 >= 0 )
v17 = v6;
v6 = (v17 & 0xFFFFFE00) + 512;
}
printf("Write the device from 0x%04X, with a length of %d bytes\n", v8, (unsigned int)v6);
v21 = (int)(v6 + v8) / 512;
if ( a1 / 512 >= v21 )
{
puts(" Done!");
}
else
{
v20 = v6 / 512 - 1;
do
{
printf("\rErasing page %d/%d ...", v7, v20);
fflush(stdout);
((void (*)(void))cmdWren)();
cmdErasePage(v7);
do
cmdRdsr(v22);
while ( v22[0] );
++v7;
}
while ( v21 != v7 );
v9 = 0;
puts(" Done!");
do
{
v10 = v19;
printf("\rWriting page %d/%d ...", v19, v20);
v11 = stdout;
fflush(stdout);
v12 = (char *)&page_0;
v18 = (_WORD)v19 << 9;
LODWORD(v13) = v19 << 9;
v14 = (char *)&page_0;
do
{
while ( (int)v13 >= a1 && v3 >= (int)v13 )
{
v15 = v9;
++v14;
++v9;
v13 = (unsigned int)(v13 + 1);
v10 = *(unsigned __int8 *)(a2 + v15);
*(v14 - 1) = v10;
if ( &buffer == v14 )
goto LABEL_16;
}
*v14++ = -1;
v13 = (unsigned int)(v13 + 1);
}
while ( &buffer != v14 );
do
{
LABEL_16:
cmdWren(v11, v10, v13);
buffer = 2;
word_701 = __ROL2__(v18, 8);
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v10 = 128LL;
spiSend(v12, 128LL);
spiSetCS(1LL);
do
{
v11 = (struct _IO_FILE *)v22;
cmdRdsr(v22);
}
while ( v22[0] );
v12 += 128;
v18 += 128;
}
while ( &buffer != v12 );
++v19;
}
while ( v21 != v19 );
}
puts("Done!");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2, signed int a3)
{
int v3; // er12
signed int v6; // er14
unsigned int v7; // ebp
unsigned int v8; // er15
int v9; // ebp
__int64 v10; // rsi
struct _IO_FILE *v11; // rdi
char *v12; // r14
__int64 v13; // rdx
char *v14; // rax
__int64 v15; // rsi
__int64 result; // rax
signed int v17; // eax
__int16 v18; // [rsp+0h] [rbp-58h]
unsigned int v19; // [rsp+4h] [rbp-54h]
unsigned int v20; // [rsp+8h] [rbp-50h]
int v21; // [rsp+Ch] [rbp-4Ch]
char v22[57]; // [rsp+1Fh] [rbp-39h] BYREF

if ( a1 > 30720 )
{
puts("ERROR: Writing above 30K is disabled!");
result = 0xFFFFFFFFLL;
}
else
{
v3 = a1 + a3;
v6 = a3;
if ( a1 + a3 > 30720 )
{
v6 = 30720 - a1;
printf("WARNING Write above 30K disabled! The new write length is %d\n", (unsigned int)(30720 - a1));
}
v7 = a1 / 512;
v19 = a1 / 512;
v8 = (a1 / 512) << 9;
if ( (v6 & 0x1FF) != 0 )
{
v17 = v6 + 511;
if ( v6 >= 0 )
v17 = v6;
v6 = (v17 & 0xFFFFFE00) + 512;
}
printf("Write the device from 0x%04X, with a length of %d bytes\n", v8, (unsigned int)v6);
v21 = (int)(v6 + v8) / 512;
if ( a1 / 512 >= v21 )
{
puts(" Done!");
}
else
{
v20 = v6 / 512 - 1;
do
{
printf("\rErasing page %d/%d ...", v7, v20);
fflush(stdout);
((void (*)(void))cmdWren)();
func5(v7);
do
func2(v22);
while ( v22[0] );
++v7;
}
while ( v21 != v7 );
v9 = 0;
puts(" Done!");
do
{
v10 = v19;
printf("\rWriting page %d/%d ...", v19, v20);
v11 = stdout;
fflush(stdout);
v12 = (char *)&page_0;
v18 = (_WORD)v19 << 9;
LODWORD(v13) = v19 << 9;
v14 = (char *)&page_0;
do
{
while ( (int)v13 >= a1 && v3 >= (int)v13 )
{
v15 = v9;
++v14;
++v9;
v13 = (unsigned int)(v13 + 1);
v10 = *(unsigned __int8 *)(a2 + v15);
*(v14 - 1) = v10;
if ( &buffer == v14 )
goto LABEL_16;
}
*v14++ = -1;
v13 = (unsigned int)(v13 + 1);
}
while ( &buffer != v14 );
do
{
LABEL_16:
func0(v11, v10, v13);
buffer = 2;
word_701 = __ROL2__(v18, 8);
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v10 = 128LL;
spiSend(v12, 128LL);
spiSetCS(1LL);
do
{
v11 = (struct _IO_FILE *)v22;
func2(v22);
}
while ( v22[0] );
v12 += 128;
v18 += 128;
}
while ( &buffer != v12 );
++v19;
}
while ( v21 != v19 );
}
puts("Done!");
result = 0LL;
}
return result;
}

original_code:
int cmdProgram(int addr, char *data, int len) {
  static char page[512];
  char fsr;
  int i, j;
  int oaddr = addr;
  int olen = len;
  int ptr=0;
  if (addr>30*1024) {
    printf("ERROR: Writing above 30K is disabled!\n");
    return -1;
  }
  if((addr+len)>30*1024) {
    len = (30*1024)-addr;
    printf("WARNING Write above 30K disabled! The new write length is %d\n", len);
  }
  addr = (addr/512)*512;
  if(len%512) len = ((len/512)+1)*512;
  printf("Write the device from 0x%04X, with a length of %d bytes\n", addr, len);
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rErasing page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    cmdWren();
    cmdErasePage(i);
    cmdRdsr(&fsr);
    while(fsr)
      cmdRdsr(&fsr);
  }
  printf(" Done!\n");
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rWriting page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    for(j=0;j<512;j++) {
      if(((i*512)+j)<oaddr) page[j]=0xFF;
      else if (((i*512)+j)>(oaddr+olen)) page[j]=0xFF;
      else page[j] = data[ptr++];
    }
    for(j=0;j<4;j++) {
      cmdWren();
      buffer[0] = 0x02;
      buffer[1] = (((i*512)+(j*128))>>8)&0x0FF;
      buffer[2] = ((i*512)+(j*128))&0x0FF;
      spiSetCS(0);
      spiSend(buffer, 3);
      spiSend(page+(j*128), 128);
      spiSetCS(1);
      cmdRdsr(&fsr);
      while(fsr) cmdRdsr(&fsr);
    }
  }
  printf("Done!\n");
  return 0;
}

canonical_original_code:
int cmdProgram(int addr, char *data, int len) {
  static char page[512];
  char fsr;
  int i; int j;
  int oaddr = addr;
  int olen = len;
  int ptr = 0;
  if (addr>30*1024) {
    printf("ERROR: Writing above 30K is disabled!\n");
    return -1;
  }
  if((addr+len)>30*1024) {
    len = (30*1024)-addr;
    printf("WARNING Write above 30K disabled! The new write length is %d\n", len);
  }
  addr = (addr/512)*512;
  if(len%512) len = ((len/512)+1)*512;
  printf("Write the device from 0x%04X, with a length of %d bytes\n", addr, len);
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rErasing page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    cmdWren();
    cmdErasePage(i);
    cmdRdsr(&fsr);
    while(fsr)
      cmdRdsr(&fsr);
  }
  printf(" Done!\n");
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rWriting page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    for(j=0;j<512;j++) {
      if(((i*512)+j)<oaddr) page[j]=0xFF;
      else if (((i*512)+j)>(oaddr+olen)) page[j]=0xFF;
      else page[j] = data[ptr++];
    }
    for(j=0;j<4;j++) {
      cmdWren();
      buffer[0] = 0x02;
      buffer[1] = (((i*512)+(j*128))>>8)&0x0FF;
      buffer[2] = ((i*512)+(j*128))&0x0FF;
      spiSetCS(0);
      spiSend(buffer, 3);
      spiSend(page+(j*128), 128);
      spiSetCS(1);
      cmdRdsr(&fsr);
      while(fsr) cmdRdsr(&fsr);
    }
  }
  printf("Done!\n");
  return 0;
}

variable_types:
page
char [512]

variable_types: j int

variable_types: oaddr int

variable_types: olen int

variable_types: ptr int


holdReset

decompiled_code:
__int64 holdReset()
{
puts("\nHold the reset");
return spiSetResetProg(0LL, 0LL);
}

canonical_decompiled_code:
__int64 func0()
{
puts("\nHold the reset");
return spiSetResetProg(0LL, 0LL);
}

original_code:
void holdReset()
{
  printf("\nHold the reset\n");
  spiSetResetProg(0, 0);
}


activateSPI

decompiled_code:
__int64 activateSPI()
{
spiSetSpiOE(0LL);
return spiSetCS(1LL);
}

canonical_decompiled_code:
__int64 func1()
{
spiSetSpiOE(0LL);
return spiSetCS(1LL);
}

original_code:
void activateSPI()
{
  spiSetSpiOE(0);
  spiSetCS(1);
}


resetProg

decompiled_code:
__int64 resetProg()
{
spiSetResetProg(0LL, 0LL);
usleep(0x1388u);
spiSetResetProg(1LL, 1LL);
usleep(0x1388u);
return activateSPI();
}

canonical_decompiled_code:
__int64 func2()
{
spiSetResetProg(0LL, 0LL);
usleep(0x1388u);
spiSetResetProg(1LL, 1LL);
usleep(0x1388u);
return func1();
}

original_code:
void resetProg()
{
  spiSetResetProg(0, 0);
  usleep(5000);
  spiSetResetProg(1, 1);
  usleep(5000);
  activateSPI();
}


deactivateSPI

decompiled_code:
__int64 deactivateSPI()
{
return spiSetSpiOE(1LL);
}

canonical_decompiled_code:
__int64 func3()
{
return spiSetSpiOE(1LL);
}

original_code:
void deactivateSPI()
{
  spiSetSpiOE(1);
}


resetRun

decompiled_code:
__int64 resetRun()
{
deactivateSPI();
puts("\nReset the nRF24LU01 in run mode");
spiSetResetProg(0LL, 0LL);
usleep(0xC350u);
return spiSetResetProg(1LL, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
func3();
puts("\nReset the nRF24LU01 in run mode");
spiSetResetProg(0LL, 0LL);
usleep(0xC350u);
return spiSetResetProg(1LL, 0LL);
}

original_code:
void resetRun()
{
  deactivateSPI();
  printf("\nReset the nRF24LU01 in run mode\n");
  spiSetResetProg(0, 0);
  usleep(50000);
  spiSetResetProg(1, 0);
}


help

decompiled_code:
int __fastcall help(const char *a1)
{
return printf(
"nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
"\n"
"Usage: %s <COMMAND> <PARAMETERS>\n"
"Commands:\n"
" help:\n"
" Display this message\n"
" read <filename> [addr] [len]:\n"
" read the memory and write a dump in filename\n"
" write <filename> [addr]:\n"
" write the content of the binary <filename> into the nRF24LU flash.\n"
" hexdump [addr] [len]:\n"
" Display a hex dump of the memory\n"
" reset:\n"
" Reset the nRF24LU1+\n"
" holdreset:\n"
" Hold the reset (ie. stop) the nRF24LU1+\n",
a1);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return printf(
"nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
"\n"
"Usage: %s <COMMAND> <PARAMETERS>\n"
"Commands:\n"
" help:\n"
" Display this message\n"
" read <filename> [addr] [len]:\n"
" read the memory and write a dump in filename\n"
" write <filename> [addr]:\n"
" write the content of the binary <filename> into the nRF24LU flash.\n"
" hexdump [addr] [len]:\n"
" Display a hex dump of the memory\n"
" reset:\n"
" Reset the nRF24LU1+\n"
" holdreset:\n"
" Hold the reset (ie. stop) the nRF24LU1+\n",
a1);
}

original_code:
void help(char *prog)
{
  printf("nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
         "\n"
         "Usage: %s <COMMAND> <PARAMETERS>\n"
         "Commands:\n"
         "  help:\n"
         "      Display this message\n"
         "  read <filename> [addr] [len]:\n"
         "      read the memory and write a dump in filename\n"
         "  write <filename> [addr]:\n"
         "      write the content of the binary <filename> into the nRF24LU flash.\n"
         "  hexdump [addr] [len]:\n"
         "      Display a hex dump of the memory\n"
         "  reset:\n"
         "      Reset the nRF24LU1+\n"
         "  holdreset:\n"
         "      Hold the reset (ie. stop) the nRF24LU1+\n", prog);
}

variable_types:
prog
char *


hexdump

decompiled_code:
__int64 __fastcall hexdump(int a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rbp
__int64 v5; // rbx
const unsigned __int16 **v6; // r12
__int64 v7; // rdi
unsigned int v9; // [rsp+8h] [rbp-30h] BYREF
int v10; // [rsp+Ch] [rbp-2Ch] BYREF
char v11[40]; // [rsp+10h] [rbp-28h]

v9 = 0;
v10 = 0x8000;
resetProg();
if ( a1 > 0 )
{
_isoc99_sscanf(*a2, "%d", &v9);
if ( a1 != 1 )
_isoc99_sscanf(a2[1], "%d", &v10);
}
v2 = v9;
if ( (int)v9 > 0x7FFF )
{
v9 = 0x7FFF;
v2 = 0x7FFFLL;
}
v3 = (unsigned int)v10;
if ( v10 + (int)v2 > 0x7FFF )
{
v3 = (unsigned int)(0x8000 - v2);
v10 = 0x8000 - v2;
}
v4 = 0LL;
printf("hexdump, display %d bytes from 0x%04X\n", v3, v2);
cmdRead(v9, buffer, (unsigned int)v10);
if ( v10 > 0 )
{
while ( 1 )
{
if ( (v4 & 0xF) != 0 )
{
if ( (v4 & 7) == 0 )
goto LABEL_13;
}
else
{
printf("0x%04X :", (unsigned int)v4 + v9);
if ( (v4 & 7) == 0 )
{
LABEL_13:
putchar(32);
goto LABEL_9;
}
}
LABEL_9:
printf("%02X", (unsigned __int8)buffer[v4]);
v11[v4 & 0xF] = buffer[v4];
if ( (((_DWORD)v4 + 1) & 0xF) != 0 )
{
if ( v10 <= (int)++v4 )
return resetRun();
}
else
{
v5 = 0LL;
v6 = _ctype_b_loc();
do
{
if ( (v5 & 7) == 0 )
putchar(32);
v7 = v11[v5];
if ( ((*v6)[v7] & 0x4000) == 0 )
LODWORD(v7) = 46;
putchar(v7);
++v5;
}
while ( v5 != 16 );
++v4;
putchar(10);
if ( v10 <= (int)v4 )
return resetRun();
}
}
}
return resetRun();
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rbp
__int64 v5; // rbx
const unsigned __int16 **v6; // r12
__int64 v7; // rdi
unsigned int v9; // [rsp+8h] [rbp-30h] BYREF
int v10; // [rsp+Ch] [rbp-2Ch] BYREF
char v11[40]; // [rsp+10h] [rbp-28h]

v9 = 0;
v10 = 0x8000;
func2();
if ( a1 > 0 )
{
_isoc99_sscanf(*a2, "%d", &v9);
if ( a1 != 1 )
_isoc99_sscanf(a2[1], "%d", &v10);
}
v2 = v9;
if ( (int)v9 > 0x7FFF )
{
v9 = 0x7FFF;
v2 = 0x7FFFLL;
}
v3 = (unsigned int)v10;
if ( v10 + (int)v2 > 0x7FFF )
{
v3 = (unsigned int)(0x8000 - v2);
v10 = 0x8000 - v2;
}
v4 = 0LL;
printf("hexdump, display %d bytes from 0x%04X\n", v3, v2);
cmdRead(v9, buffer, (unsigned int)v10);
if ( v10 > 0 )
{
while ( 1 )
{
if ( (v4 & 0xF) != 0 )
{
if ( (v4 & 7) == 0 )
goto LABEL_13;
}
else
{
printf("0x%04X :", (unsigned int)v4 + v9);
if ( (v4 & 7) == 0 )
{
LABEL_13:
putchar(32);
goto LABEL_9;
}
}
LABEL_9:
printf("%02X", (unsigned __int8)buffer[v4]);
v11[v4 & 0xF] = buffer[v4];
if ( (((_DWORD)v4 + 1) & 0xF) != 0 )
{
if ( v10 <= (int)++v4 )
return func4();
}
else
{
v5 = 0LL;
v6 = _ctype_b_loc();
do
{
if ( (v5 & 7) == 0 )
putchar(32);
v7 = v11[v5];
if ( ((*v6)[v7] & 0x4000) == 0 )
LODWORD(v7) = 46;
putchar(v7);
++v5;
}
while ( v5 != 16 );
++v4;
putchar(10);
if ( v10 <= (int)v4 )
return func4();
}
}
}
return func4();
}

original_code:
void hexdump(int argc, char ** argv) {
  int address = 0;
  int len = 32*1024;
  int i,j;
  char line[16];
  resetProg();
  if(argc>0) sscanf(argv[0], "%d", &address);
  if(argc>1) sscanf(argv[1], "%d", &len);
  if(address>0x7FFF) address=0x7FFF;
  if((address+len)>0x7fff) len=0x7FFF-address+1;
  printf("hexdump, display %d bytes from 0x%04X\n", len, address);
  cmdRead(address, buffer, len);
  for(i=0;i<len;i++) {
    if(!(i%16)) printf("0x%04X :", (unsigned) address+i);
    if(!(i%8)) printf(" ");
    printf("%02X", (unsigned char)buffer[i]);
    line[i%16] = buffer[i];
    if(!((i+1)%16)) {
      for(j=0;j<16;j++) {
        if(!(j%8)) putchar(' ');
        if(((*__ctype_b_loc ())[(int) ((line[j]))] & (unsigned short int) _ISprint)) putchar(line[j]);
        else putchar('.');
      }
      putchar('\n');
    }
  }
  resetRun();
  return;
}

canonical_original_code:
void hexdump(int argc, char **argv) {
  int address = 0;
  int len = 32*1024;
  int i; int j;
  char line[16];
  resetProg();
  if(argc>0) sscanf(argv[0], "%d", &address);
  if(argc>1) sscanf(argv[1], "%d", &len);
  if(address>0x7FFF) address=0x7FFF;
  if((address+len)>0x7fff) len=0x7FFF-address+1;
  printf("hexdump, display %d bytes from 0x%04X\n", len, address);
  cmdRead(address, buffer, len);
  for(i=0;i<len;i++) {
    if(!(i%16)) printf("0x%04X :", (unsigned int) address+i);
    if(!(i%8)) printf(" ");
    printf("%02X", (unsigned char)buffer[i]);
    line[i%16] = buffer[i];
    if(!((i+1)%16)) {
      for(j=0;j<16;j++) {
        if(!(j%8)) putchar(' ');
        if(((*__ctype_b_loc ())[(int) ((line[j]))] & (unsigned short) _ISprint)) putchar(line[j]);
        else putchar('.');
      }
      putchar('\n');
    }
  }
  resetRun();
  return;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: address int

variable_types:
line
char [16]


main

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

v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
goto LABEL_6;
v6 = argv[1];
v8 = 5LL;
v9 = "help";
do
{
if ( !v8 )
break;
v4 = *v6 < (unsigned int)*v9;
v5 = *v6++ == *v9++;
--v8;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
LABEL_6:
help(*argv);
}
else
{
if ( (int)spiInit(v9, v6, envp) < 0 )
exit(1);
puts("Ftdi dongle found");
v11 = argv[1];
if ( !strcmp(v11, "hexdump") )
{
hexdump(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "read") )
{
nrfread(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "write") )
{
nrfwrite(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "reset") )
{
resetRun();
v11 = argv[1];
}
if ( !strcmp(v11, "holdreset") )
holdReset();
spiDeinit();
}
return 0;
}

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

v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
goto LABEL_6;
v6 = argv[1];
v8 = 5LL;
v9 = "help";
do
{
if ( !v8 )
break;
v4 = *v6 < (unsigned int)*v9;
v5 = *v6++ == *v9++;
--v8;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
LABEL_6:
func5(*argv);
}
else
{
if ( (int)spiInit(v9, v6, envp) < 0 )
exit(1);
puts("Ftdi dongle found");
v11 = argv[1];
if ( !strcmp(v11, "hexdump") )
{
func6(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "read") )
{
func7(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "write") )
{
func8(argc - 2, argv + 2);
v11 = argv[1];
}
if ( !strcmp(v11, "reset") )
{
func4();
v11 = argv[1];
}
if ( !strcmp(v11, "holdreset") )
func0();
spiDeinit();
}
return 0;
}

original_code:
int main (int argc, char *argv[])
{
  if(argc<2 || !strcmp(argv[1], "help"))
  {
    help(argv[0]);
    return 0;
  }
  if(spiInit()<0)
    exit(1);
  printf("Ftdi dongle found\n");
  if (!strcmp(argv[1], "hexdump"))
    hexdump(argc-2, argv+2);
  if (!strcmp(argv[1], "read"))
    nrfread(argc-2, argv+2);
  if (!strcmp(argv[1], "write"))
    nrfwrite(argc-2, argv+2);
  if (!strcmp(argv[1], "reset"))
    resetRun();
  if (!strcmp(argv[1], "holdreset"))
    holdReset();
  spiDeinit();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  if(argc<2 || !strcmp(argv[1], "help"))
  {
    help(argv[0]);
    return 0;
  }
  if(spiInit()<0)
    exit(1);
  printf("Ftdi dongle found\n");
  if (!strcmp(argv[1], "hexdump"))
    hexdump(argc-2, argv+2);
  if (!strcmp(argv[1], "read"))
    nrfread(argc-2, argv+2);
  if (!strcmp(argv[1], "write"))
    nrfwrite(argc-2, argv+2);
  if (!strcmp(argv[1], "reset"))
    resetRun();
  if (!strcmp(argv[1], "holdreset"))
    holdReset();
  spiDeinit();
  return 0;
}

variable_types:
argv
char *[]


ftdiSendBuffer

decompiled_code:
__int64 __fastcall ftdiSendBuffer(__int64 a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebp
int v4; // er12
const char *v6; // rax

v2 = 0;
v3 = 11;
do
{
if ( (int)v2 >= (int)a2 )
return v2;
if ( !--v3 )
return v2;
v4 = ftdi_write_data(&ftdic, a1 + (int)v2, a2);
v2 += v4;
}
while ( v4 >= 0 );
v6 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", (unsigned int)v4, v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebp
int v4; // er12
const char *v6; // rax

v2 = 0;
v3 = 11;
do
{
if ( (int)v2 >= (int)a2 )
return v2;
if ( !--v3 )
return v2;
v4 = ftdi_write_data(&ftdic, a1 + (int)v2, a2);
v2 += v4;
}
while ( v4 >= 0 );
v6 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", (unsigned int)v4, v6);
return 0xFFFFFFFFLL;
}

original_code:
int ftdiSendBuffer(char *buff, int len)
{
  int i=0, ret, try=0;
  while((i<len) && ((try++)<10)) {
    ret = ftdi_write_data(&ftdic, (unsigned char*)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

canonical_original_code:
int ftdiSendBuffer(char *buff, int len)
{
  int i = 0; int ret; int try = 0;
  while((i<len) && ((try++)<10)) {
    ret = ftdi_write_data(&ftdic, (unsigned char *)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

variable_types:
buff
char *

variable_types: i int

variable_types: ret int

variable_types: try int


ftdiRecvBuffer

decompiled_code:
__int64 __fastcall ftdiRecvBuffer(__int64 a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebx
int v4; // er12
const char *v6; // rax

v2 = 0;
v3 = 101;
do
{
if ( (int)v2 >= (int)a2 )
return v2;
if ( !--v3 )
return v2;
v4 = ftdi_read_data(&ftdic, a1 + (int)v2, a2);
v2 += v4;
}
while ( v4 >= 0 );
v6 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", (unsigned int)v4, v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebx
int v4; // er12
const char *v6; // rax

v2 = 0;
v3 = 101;
do
{
if ( (int)v2 >= (int)a2 )
return v2;
if ( !--v3 )
return v2;
v4 = ftdi_read_data(&ftdic, a1 + (int)v2, a2);
v2 += v4;
}
while ( v4 >= 0 );
v6 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", (unsigned int)v4, v6);
return 0xFFFFFFFFLL;
}

original_code:
int ftdiRecvBuffer(char* buff, int len)
{
  int i=0, ret, try=0;
  while((i<len) && ((try++)<100)) {
    ret = ftdi_read_data(&ftdic, (unsigned char*)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

canonical_original_code:
int ftdiRecvBuffer(char *buff, int len)
{
  int i = 0; int ret; int try = 0;
  while((i<len) && ((try++)<100)) {
    ret = ftdi_read_data(&ftdic, (unsigned char *)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

variable_types: len int


spiInit

decompiled_code:
__int64 spiInit()
{
int v0; // eax
unsigned int v2; // er12
const char *v3; // rax
const char *v4; // rax
const char *v5; // rax

ftdi_init(&ftdic);
v0 = ftdi_usb_open(&ftdic, 1027LL, 53240LL);
if ( v0 < 0 )
{
v2 = v0;
v3 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", v2, v3);
}
else if ( (int)ftdi_set_interface(&ftdic, 1LL) < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to setup the A channel: %s\n", v5);
}
else
{
if ( (int)ftdi_set_bitmode(&ftdic, 0LL, 2LL) >= 0 )
{
byte_4E8 = 0;
OE = 1;
dword_4C4 = 1;
buffer = 0x4861B00821B1980LL;
return ftdiSendBuffer((__int64)&buffer, 9u);
}
v4 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to set the channel in MPSSE mode: %s\n", v4);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v2; // er12
const char *v3; // rax
const char *v4; // rax
const char *v5; // rax

ftdi_init(&ftdic);
v0 = ftdi_usb_open(&ftdic, 1027LL, 53240LL);
if ( v0 < 0 )
{
v2 = v0;
v3 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", v2, v3);
}
else if ( (int)ftdi_set_interface(&ftdic, 1LL) < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to setup the A channel: %s\n", v5);
}
else
{
if ( (int)ftdi_set_bitmode(&ftdic, 0LL, 2LL) >= 0 )
{
byte_4E8 = 0;
OE = 1;
dword_4C4 = 1;
buffer = 0x4861B00821B1980LL;
return func0((__int64)&buffer, 9u);
}
v4 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to set the channel in MPSSE mode: %s\n", v4);
}
return 0xFFFFFFFFLL;
}

original_code:
int spiInit()
{
  int i, ret;
  ftdi_init(&ftdic);
  if((ret = ftdi_usb_open(&ftdic, 0x0403, 0xcff8)) < 0)
  {
    fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret,
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  if (ftdi_set_interface(&ftdic, INTERFACE_A)<0)
  {
    fprintf(stderr, "Unable to setup the A channel: %s\n",
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  if (ftdi_set_bitmode(&ftdic, 0, BITMODE_MPSSE)<0)
  {
    fprintf(stderr, "Unable to set the channel in MPSSE mode: %s\n",
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  OE=1;
  CS=1;
  i=0;
  buffer[i++] = 0x80;
  buffer[i++] = 0x19;
  buffer[i++] = 0x1B;
  buffer[i++] = 0x82;
  buffer[i++] = 0x00;
  buffer[i++] = 0x1B;
  buffer[i++] = 0x86;
  buffer[i++] = 0x04;
  buffer[i++] = 0;
  return ftdiSendBuffer(buffer, i);
}

canonical_original_code:
int spiInit()
{
  int i; int ret;
  ftdi_init(&ftdic);
  if((ret = ftdi_usb_open(&ftdic, 0x0403, 0xcff8)) < 0)
  {
    fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret,
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  if (ftdi_set_interface(&ftdic, INTERFACE_A)<0)
  {
    fprintf(stderr, "Unable to setup the A channel: %s\n",
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  if (ftdi_set_bitmode(&ftdic, 0, BITMODE_MPSSE)<0)
  {
    fprintf(stderr, "Unable to set the channel in MPSSE mode: %s\n",
            ftdi_get_error_string(&ftdic));
    return -1;
  }
  OE=1;
  CS=1;
  i=0;
  buffer[i++] = 0x80;
  buffer[i++] = 0x19;
  buffer[i++] = 0x1B;
  buffer[i++] = 0x82;
  buffer[i++] = 0x00;
  buffer[i++] = 0x1B;
  buffer[i++] = 0x86;
  buffer[i++] = 0x04;
  buffer[i++] = 0;
  return ftdiSendBuffer(buffer, i);
}


spiSetResetProg

decompiled_code:
__int64 __fastcall spiSetResetProg(int a1, int a2)
{
LOBYTE(buffer) = -126;
if ( a1 )
LOBYTE(a1) = 2;
BYTE2(buffer) = 15;
if ( a2 )
LOBYTE(a1) = a1 | 1;
BYTE1(buffer) = a1;
return ftdiSendBuffer((__int64)&buffer, 3u);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
LOBYTE(buffer) = -126;
if ( a1 )
LOBYTE(a1) = 2;
BYTE2(buffer) = 15;
if ( a2 )
LOBYTE(a1) = a1 | 1;
BYTE1(buffer) = a1;
return func0((__int64)&buffer, 3u);
}

original_code:
int spiSetResetProg(int reset, int prog)
{
  int i=0;
  int value=0;
  if (reset) value |= 0x02;
  if (prog) value |= 0x01;
  buffer[i++] = 0x82;
  buffer[i++] = value;
  buffer[i++] = 0x0F;
  return ftdiSendBuffer(buffer, i);
}

canonical_original_code:
int spiSetResetProg(int reset, int prog)
{
  int i = 0;
  int value = 0;
  if (reset) value |= 0x02;
  if (prog) value |= 0x01;
  buffer[i++] = 0x82;
  buffer[i++] = value;
  buffer[i++] = 0x0F;
  return ftdiSendBuffer(buffer, i);
}

variable_types: reset int

variable_types: prog int

variable_types: value int


spiSetSpiOE

decompiled_code:
__int64 __fastcall spiSetSpiOE(int a1)
{
char v1; // al

v1 = dword_4C4;
OE = a1;
LOBYTE(buffer) = 0x80;
BYTE2(buffer) = 27;
if ( dword_4C4 )
v1 = 8;
if ( a1 )
v1 |= 0x10u;
BYTE1(buffer) = v1;
return ftdiSendBuffer((__int64)&buffer, 3u);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
char v1; // al

v1 = dword_4C4;
OE = a1;
LOBYTE(buffer) = 0x80;
BYTE2(buffer) = 27;
if ( dword_4C4 )
v1 = 8;
if ( a1 )
v1 |= 0x10u;
BYTE1(buffer) = v1;
return func0((__int64)&buffer, 3u);
}

original_code:
int spiSetSpiOE(int oe) {
  int i=0;
  int value=0x00;
  OE=oe;
  if (CS) value |= 0x08;
  if (OE) value |= 0x10;
  buffer[i++] = 0x80;
  buffer[i++] = value;
  buffer[i++] = 0x1B;
  return ftdiSendBuffer(buffer, i);
}

canonical_original_code:
int spiSetSpiOE(int oe) {
  int i = 0;
  int value = 0x00;
  OE=oe;
  if (CS) value |= 0x08;
  if (OE) value |= 0x10;
  buffer[i++] = 0x80;
  buffer[i++] = value;
  buffer[i++] = 0x1B;
  return ftdiSendBuffer(buffer, i);
}

variable_types: oe int


spiSetCS

decompiled_code:
__int64 __fastcall spiSetCS(int a1)
{
dword_4C4 = a1;
if ( a1 )
LOBYTE(a1) = 8;
LOBYTE(buffer) = 0x80;
BYTE2(buffer) = 27;
if ( OE )
LOBYTE(a1) = a1 | 0x10;
BYTE1(buffer) = a1;
return ftdiSendBuffer((__int64)&buffer, 3u);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
dword_4C4 = a1;
if ( a1 )
LOBYTE(a1) = 8;
LOBYTE(buffer) = 0x80;
BYTE2(buffer) = 27;
if ( OE )
LOBYTE(a1) = a1 | 0x10;
BYTE1(buffer) = a1;
return func0((__int64)&buffer, 3u);
}

original_code:
int spiSetCS(int cs)
{
  int i=0;
  int value=0x00;
  CS=cs;
  if (CS) value |= 0x08;
  if (OE) value |= 0x10;
  buffer[i++] = 0x80;
  buffer[i++] = value;
  buffer[i++] = 0x1B;
  return ftdiSendBuffer(buffer, i);
}

canonical_original_code:
int spiSetCS(int cs)
{
  int i = 0;
  int value = 0x00;
  CS=cs;
  if (CS) value |= 0x08;
  if (OE) value |= 0x10;
  buffer[i++] = 0x80;
  buffer[i++] = value;
  buffer[i++] = 0x1B;
  return ftdiSendBuffer(buffer, i);
}

variable_types: cs int


spiDeinit

decompiled_code:
__int64 spiDeinit()
{
ftdi_usb_close(&ftdic);
return ftdi_deinit(&ftdic);
}

canonical_decompiled_code:
__int64 func6()
{
ftdi_usb_close(&ftdic);
return ftdi_deinit(&ftdic);
}

original_code:
void spiDeinit()
{
  ftdi_usb_close(&ftdic);
  ftdi_deinit(&ftdic);
}


spiSend

decompiled_code:
__int64 __fastcall spiSend(char *a1, unsigned int a2)
{
unsigned int v2; // er8

if ( (int)a2 <= 0 )
return 0LL;
if ( (int)a2 > 128 )
{
fwrite("Unable to send more than 128 bytes!\n", 1uLL, 0x24uLL, stderr);
v2 = -1;
}
else
{
LOBYTE(buffer) = 17;
*(_WORD *)((char *)&buffer + 1) = (unsigned __int8)(a2 - 1);
if ( a2 < 8 )
{
if ( (a2 & 4) != 0 )
{
*(_DWORD *)((char *)&buffer + 3) = *(_DWORD *)a1;
*(_DWORD *)((char *)&unk_4DF + a2) = *(_DWORD *)&a1[a2 - 4];
}
else
{
BYTE3(buffer) = *a1;
if ( (a2 & 2) != 0 )
*(_WORD *)((char *)&buffer + a2 + 1) = *(_WORD *)&a1[a2 - 2];
}
}
else
{
*(__int64 *)((char *)&buffer + 3) = *(_QWORD *)a1;
*(_QWORD *)((char *)&unk_4DB + a2) = *(_QWORD *)&a1[a2 - 8];
qmemcpy(
(void *)((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a1 - ((char *)&buffer - ((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL) + 3)),
8LL
* ((a2 + (unsigned int)((unsigned int)&buffer - ((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL) + 3)) >> 3));
}
v2 = ftdiSendBuffer((__int64)&buffer, a2 + 3) - 3;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, unsigned int a2)
{
unsigned int v2; // er8

if ( (int)a2 <= 0 )
return 0LL;
if ( (int)a2 > 128 )
{
fwrite("Unable to send more than 128 bytes!\n", 1uLL, 0x24uLL, stderr);
v2 = -1;
}
else
{
LOBYTE(buffer) = 17;
*(_WORD *)((char *)&buffer + 1) = (unsigned __int8)(a2 - 1);
if ( a2 < 8 )
{
if ( (a2 & 4) != 0 )
{
*(_DWORD *)((char *)&buffer + 3) = *(_DWORD *)a1;
*(_DWORD *)((char *)&unk_4DF + a2) = *(_DWORD *)&a1[a2 - 4];
}
else
{
BYTE3(buffer) = *a1;
if ( (a2 & 2) != 0 )
*(_WORD *)((char *)&buffer + a2 + 1) = *(_WORD *)&a1[a2 - 2];
}
}
else
{
*(__int64 *)((char *)&buffer + 3) = *(_QWORD *)a1;
*(_QWORD *)((char *)&unk_4DB + a2) = *(_QWORD *)&a1[a2 - 8];
qmemcpy(
(void *)((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a1 - ((char *)&buffer - ((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL) + 3)),
8LL
* ((a2 + (unsigned int)((unsigned int)&buffer - ((unsigned __int64)&unk_4EB & 0xFFFFFFFFFFFFFFF8LL) + 3)) >> 3));
}
v2 = func0((__int64)&buffer, a2 + 3) - 3;
}
return v2;
}

original_code:
int spiSend(char *data, int len)
{
  int i=0;
  if(len<1) return 0;
  if(len>128) {
    fprintf(stderr, "Unable to send more than 128 bytes!\n");
    return -1;
  }
  len--;
  buffer[i++] = 0x10 | 0x01;
  buffer[i++] = len&0x0FF;
  buffer[i++] = (len>>8)&0x0FF;
  memcpy(buffer+3, data, len+1);
  return ftdiSendBuffer(buffer, len+1+3)-3;
}

canonical_original_code:
int spiSend(char *data, int len)
{
  int i = 0;
  if(len<1) return 0;
  if(len>128) {
    fprintf(stderr, "Unable to send more than 128 bytes!\n");
    return -1;
  }
  len--;
  buffer[i++] = 0x10 | 0x01;
  buffer[i++] = len&0x0FF;
  buffer[i++] = (len>>8)&0x0FF;
  memcpy(buffer+3, data, len+1);
  return ftdiSendBuffer(buffer, len+1+3)-3;
}

variable_types:
data
char *


spiReceive

decompiled_code:
__int64 __fastcall spiReceive(__int64 a1, signed int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
return 0LL;
if ( a2 > 256 )
{
fwrite("Unable to receive more than 128 bytes!\n", 1uLL, 0x27uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
LOBYTE(buffer) = 32;
*(_WORD *)((char *)&buffer + 1) = (unsigned __int8)(a2 - 1);
if ( (int)ftdiSendBuffer((__int64)&buffer, 3u) < 0 )
result = 0xFFFFFFFFLL;
else
result = ftdiRecvBuffer(a1, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, signed int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
return 0LL;
if ( a2 > 256 )
{
fwrite("Unable to receive more than 128 bytes!\n", 1uLL, 0x27uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
LOBYTE(buffer) = 32;
*(_WORD *)((char *)&buffer + 1) = (unsigned __int8)(a2 - 1);
if ( (int)func0((__int64)&buffer, 3u) < 0 )
result = 0xFFFFFFFFLL;
else
result = func1(a1, a2);
}
return result;
}

original_code:
int spiReceive(char *data, int len)
{
  int i=0;
  if(len<1) return 0;
  if(len>256) {
    fprintf(stderr, "Unable to receive more than 128 bytes!\n");
    return -1;
  }
  len--;
  buffer[i++] = 0x20;
  buffer[i++] = len&0x0FF;
  buffer[i++] = (len>>8)&0x0FF;
  if (ftdiSendBuffer(buffer, i)<0)
    return -1;
  return ftdiRecvBuffer(data, len+1);
}

canonical_original_code:
int spiReceive(char *data, int len)
{
  int i = 0;
  if(len<1) return 0;
  if(len>256) {
    fprintf(stderr, "Unable to receive more than 128 bytes!\n");
    return -1;
  }
  len--;
  buffer[i++] = 0x20;
  buffer[i++] = len&0x0FF;
  buffer[i++] = (len>>8)&0x0FF;
  if (ftdiSendBuffer(buffer, i)<0)
    return -1;
  return ftdiRecvBuffer(data, len+1);
}


indep

decompiled_code:
__int64 __fastcall indep(__int64 a1, __int64 a2)
{
__int64 v3; // rbx
__int64 v4; // rdi

if ( (unsigned int)((__int64 (*)(void))sametree)() )
return 0LL;
v3 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
while ( !(unsigned int)sametree(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v3), a2) )
{
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v3);
if ( *(_DWORD *)(v4 + 16) )
{
if ( !(unsigned int)indep(v4, a2) )
break;
}
if ( *(_DWORD *)(a1 + 16) <= (unsigned int)++v3 )
return 1LL;
}
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v3; // rbx
__int64 v4; // rdi

if ( (unsigned int)((__int64 (*)(void))sametree)() )
return 0LL;
v3 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
while ( !(unsigned int)sametree(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v3), a2) )
{
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v3);
if ( *(_DWORD *)(v4 + 16) )
{
if ( !(unsigned int)func0(v4, a2) )
break;
}
if ( *(_DWORD *)(a1 + 16) <= (unsigned int)++v3 )
return 1LL;
}
return 0LL;
}
return 1LL;
}

original_code:
int indep(exp_tree_t *tree, exp_tree_t *key)
{
 int i;
 if(sametree(tree, key))
  return 0;
 for (i = 0; i < tree->child_count; ++i) {
  if (sametree(tree->child[i], key))
   return 0;
  if (tree->child[i]->child_count)
   if(!indep(tree->child[i], key))
    return 0;
 }
 return 1;
}

canonical_original_code:
int indep(struct exp_tree *tree, struct exp_tree *key)
{
 int i;
 if(sametree(tree, key))
  return 0;
 for (i = 0; i < tree->child_count; ++i) {
  if (sametree(tree->child[i], key))
   return 0;
  if (tree->child[i]->child_count)
   if(!indep(tree->child[i], key))
    return 0;
 }
 return 1;
}

variable_types: struct exp_tree { char head_type; struct token { char type; char *start; int len; int from_line; int from_char; } *tok; unsigned int child_count; unsigned int child_alloc; struct exp_tree **child; } *

variable_types:
key
struct exp_tree *

user_defined_types: struct exp_tree { char head_type; struct token *tok; unsigned int child_count; unsigned int child_alloc; struct exp_tree **child; }


decompiled_code:
__int64 __fastcall find_subtree(__int64 a1, __int64 a2)
{
int v3; // er8
__int64 result; // rax
__int64 v5; // rbx
bool v6; // zf
__int64 v7; // rax
__int64 v8; // rdi

v3 = sametree(a1, a2);
result = a1;
if ( !v3 )
{
v5 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
while ( 1 )
{
v6 = (unsigned int)sametree(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v5), a2) == 0;
v7 = *(_QWORD *)(a1 + 24);
if ( !v6 )
break;
v8 = *(_QWORD *)(v7 + 8 * v5);
if ( *(_DWORD *)(v8 + 16) )
{
result = find_subtree(v8, a2);
if ( result )
return result;
}
if ( *(_DWORD *)(a1 + 16) <= (unsigned int)++v5 )
goto LABEL_9;
}
result = *(_QWORD *)(v7 + 8 * v5);
}
else
{
LABEL_9:
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v3; // er8
__int64 result; // rax
__int64 v5; // rbx
bool v6; // zf
__int64 v7; // rax
__int64 v8; // rdi

v3 = sametree(a1, a2);
result = a1;
if ( !v3 )
{
v5 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
while ( 1 )
{
v6 = (unsigned int)sametree(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8 * v5), a2) == 0;
v7 = *(_QWORD *)(a1 + 24);
if ( !v6 )
break;
v8 = *(_QWORD *)(v7 + 8 * v5);
if ( *(_DWORD *)(v8 + 16) )
{
result = func1(v8, a2);
if ( result )
return result;
}
if ( *(_DWORD *)(a1 + 16) <= (unsigned int)++v5 )
goto LABEL_9;
}
result = *(_QWORD *)(v7 + 8 * v5);
}
else
{
LABEL_9:
result = 0LL;
}
}
return result;
}

original_code:
exp_tree_t* find_subtree(exp_tree_t *tree, exp_tree_t *key)
{
 int i;
 exp_tree_t *catch;
 if(sametree(tree, key))
  return tree;
 for (i = 0; i < tree->child_count; ++i) {
  if (sametree(tree->child[i], key))
   return tree->child[i];
  if (tree->child[i]->child_count)
   if((catch = find_subtree(tree->child[i], key)) != ((void *)0))
    return catch;
 }
 return ((void *)0);
}

canonical_original_code:
struct exp_tree *find_subtree(struct exp_tree *tree, struct exp_tree *key)
{
 int i;
 struct exp_tree *catch;
 if(sametree(tree, key))
  return tree;
 for (i = 0; i < tree->child_count; ++i) {
  if (sametree(tree->child[i], key))
   return tree->child[i];
  if (tree->child[i]->child_count)
   if((catch = find_subtree(tree->child[i], key)) != ((void *)0))
    return catch;
 }
 return ((void *)0);
}

variable_types:
tree
struct exp_tree *

return_type: struct exp_tree { char head_type; struct token { char type; char *start; int len; int from_line; int from_char; } *tok; unsigned int child_count; unsigned int child_alloc; struct exp_tree **child; } *


original_code:
int same_tokens(exp_tree_t *a, exp_tree_t *b)
{
 strncpy(buf_a, b->tok->start, b->tok->len);
 buf_a[b->tok->len] = 0;
 strncpy(buf_b, a->tok->start, a->tok->len);
 buf_b[a->tok->len] = 0;
 return !strcmp(buf_a, buf_b);
}

canonical_original_code:
int same_tokens(struct exp_tree *a, struct exp_tree *b)
{
 strncpy(buf_a, b->tok->start, b->tok->len);
 buf_a[b->tok->len] = 0;
 strncpy(buf_b, a->tok->start, a->tok->len);
 buf_b[a->tok->len] = 0;
 return !strcmp(buf_a, buf_b);
}

variable_types:
b
struct exp_tree *

user_defined_types: struct token { char type; char *start; int len; int from_line; int from_char; }


treematch

original_code:
int treematch(exp_tree_t *a, exp_tree_t *b, dict_t* d)
{
 int i;
 int id;
 if (b->head_type == VARIABLE
 || (b->head_type == MATCH_NUM && a->head_type == NUMBER)
 || (b->head_type == MATCH_VAR && a->head_type == VARIABLE)
 || (b->head_type == MATCH_NAN && a->head_type != NUMBER)
 || (b->head_type == SYMBOL && a->head_type == VARIABLE
  && same_tokens(a, b))) {
  strncpy(buf_a, b->tok->start, b->tok->len);
  buf_a[b->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   if (!sametree(d->match[id], a))
    return 0;
   return 1;
  } else {
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
   return 1;
  }
 }
 if (b->head_type == MATCH_IND_SYM) {
  if (a->head_type != VARIABLE) {
   strncpy(buf_a, b->child[0]->tok->start, b->child[0]->tok->len);
   buf_a[b->child[0]->tok->len] = 0;
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
   return 1;
  } else {
   if (same_tokens(a, b->child[1]))
    return 0;
   else {
    strncpy(buf_a, b->child[0]->tok->start, b->child[0]->tok->len);
    buf_a[b->child[0]->tok->len] = 0;
    strcpy(d->symbol[d->count], buf_a);
    d->match[d->count] = a;
    if (++d->count > d->alloc)
     expand_dict(d, d->count + 10);
    return 1;
   }
  }
 }
 if (b->head_type == MATCH_IND
  || b->head_type == MATCH_CONT
  || b->head_type == MATCH_CONTX) {
  strncpy(buf_a, b->child[1]->tok->start, b->child[1]->tok->len);
  buf_a[b->child[1]->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   if ((b->head_type == MATCH_IND
       && indep(a, d->match[id]))
   || (b->head_type == MATCH_CONT
       && !indep(a, d->match[id]))
   || (b->head_type == MATCH_CONTX
       && !indep(a, d->match[id])
       && !sametree(a, d->match[id]))) {
    strncpy(buf_a, b->child[0]->tok->start,
     b->child[0]->tok->len);
    buf_a[b->child[0]->tok->len] = 0;
    if ((id = dict_search(d, buf_a)) != -1) {
     if (!sametree(d->match[id], a))
      return 0;
     return 1;
    } else {
     strcpy(d->symbol[d->count], buf_a);
     d->match[d->count] = a;
     if (++d->count > d->alloc)
      expand_dict(d, d->count + 10);
     return 1;
    }
   }
  } else {
   printf("[forward substitution issue]\n");
   return 0;
  }
 }
 if (b->head_type == MATCH_FUNC && a->head_type == FUNC) {
  strncpy(buf_a, b->tok->start, b->tok->len);
  buf_a[b->tok->len] = 0;
  strncpy(buf_b, a->tok->start, a->tok->len);
  buf_b[a->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   strncpy(buf_c, d->match[id]->tok->start,
    d->match[id]->tok->len);
   buf_c[d->match[id]->tok->len] = 0;
   if (strcmp(buf_c, buf_b))
    return 0;
  } else {
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
  }
  if (a->child_count != b->child_count)
   return 0;
  for (i = 0; i < a->child_count; ++i) {
   if (!treematch(a->child[i], b->child[i], d))
    return 0;
  }
  return 1;
 }
 if (a->head_type != b->head_type)
  return 0;
 if (a->child_count != b->child_count)
  return 0;
 if (a->tok && b->tok) {
  strncpy(buf_a, a->tok->start, a->tok->len);
  buf_a[a->tok->len] = 0;
  strncpy(buf_b, b->tok->start, b->tok->len);
  buf_b[b->tok->len] = 0;
  if (strcmp(buf_a, buf_b))
   return 0;
 }
 for (i = 0; i < a->child_count; ++i) {
  if (!treematch(a->child[i], b->child[i], d))
   return 0;
 }
 return 1;
}

canonical_original_code:
int treematch(struct exp_tree *a, struct exp_tree *b, struct dict_s *d)
{
 int i;
 int id;
 if (b->head_type == VARIABLE
 || (b->head_type == MATCH_NUM && a->head_type == NUMBER)
 || (b->head_type == MATCH_VAR && a->head_type == VARIABLE)
 || (b->head_type == MATCH_NAN && a->head_type != NUMBER)
 || (b->head_type == SYMBOL && a->head_type == VARIABLE
  && same_tokens(a, b))) {
  strncpy(buf_a, b->tok->start, b->tok->len);
  buf_a[b->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   if (!sametree(d->match[id], a))
    return 0;
   return 1;
  } else {
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
   return 1;
  }
 }
 if (b->head_type == MATCH_IND_SYM) {
  if (a->head_type != VARIABLE) {
   strncpy(buf_a, b->child[0]->tok->start, b->child[0]->tok->len);
   buf_a[b->child[0]->tok->len] = 0;
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
   return 1;
  } else {
   if (same_tokens(a, b->child[1]))
    return 0;
   else {
    strncpy(buf_a, b->child[0]->tok->start, b->child[0]->tok->len);
    buf_a[b->child[0]->tok->len] = 0;
    strcpy(d->symbol[d->count], buf_a);
    d->match[d->count] = a;
    if (++d->count > d->alloc)
     expand_dict(d, d->count + 10);
    return 1;
   }
  }
 }
 if (b->head_type == MATCH_IND
  || b->head_type == MATCH_CONT
  || b->head_type == MATCH_CONTX) {
  strncpy(buf_a, b->child[1]->tok->start, b->child[1]->tok->len);
  buf_a[b->child[1]->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   if ((b->head_type == MATCH_IND
       && indep(a, d->match[id]))
   || (b->head_type == MATCH_CONT
       && !indep(a, d->match[id]))
   || (b->head_type == MATCH_CONTX
       && !indep(a, d->match[id])
       && !sametree(a, d->match[id]))) {
    strncpy(buf_a, b->child[0]->tok->start,
     b->child[0]->tok->len);
    buf_a[b->child[0]->tok->len] = 0;
    if ((id = dict_search(d, buf_a)) != -1) {
     if (!sametree(d->match[id], a))
      return 0;
     return 1;
    } else {
     strcpy(d->symbol[d->count], buf_a);
     d->match[d->count] = a;
     if (++d->count > d->alloc)
      expand_dict(d, d->count + 10);
     return 1;
    }
   }
  } else {
   printf("[forward substitution issue]\n");
   return 0;
  }
 }
 if (b->head_type == MATCH_FUNC && a->head_type == FUNC) {
  strncpy(buf_a, b->tok->start, b->tok->len);
  buf_a[b->tok->len] = 0;
  strncpy(buf_b, a->tok->start, a->tok->len);
  buf_b[a->tok->len] = 0;
  if ((id = dict_search(d, buf_a)) != -1) {
   strncpy(buf_c, d->match[id]->tok->start,
    d->match[id]->tok->len);
   buf_c[d->match[id]->tok->len] = 0;
   if (strcmp(buf_c, buf_b))
    return 0;
  } else {
   strcpy(d->symbol[d->count], buf_a);
   d->match[d->count] = a;
   if (++d->count > d->alloc)
    expand_dict(d, d->count + 10);
  }
  if (a->child_count != b->child_count)
   return 0;
  for (i = 0; i < a->child_count; ++i) {
   if (!treematch(a->child[i], b->child[i], d))
    return 0;
  }
  return 1;
 }
 if (a->head_type != b->head_type)
  return 0;
 if (a->child_count != b->child_count)
  return 0;
 if (a->tok && b->tok) {
  strncpy(buf_a, a->tok->start, a->tok->len);
  buf_a[a->tok->len] = 0;
  strncpy(buf_b, b->tok->start, b->tok->len);
  buf_b[b->tok->len] = 0;
  if (strcmp(buf_a, buf_b))
   return 0;
 }
 for (i = 0; i < a->child_count; ++i) {
  if (!treematch(a->child[i], b->child[i], d))
   return 0;
 }
 return 1;
}

variable_types: struct dict_s { char **symbol; struct exp_tree **match; int count; int alloc; int success; } *

variable_types: id int


matchloop

original_code:
int matchloop(exp_tree_t** rules, int rc, exp_tree_t* tree)
{
 dict_t *dict;
 exp_tree_t *skel;
 int i;
 int mc;
 int m = 0;
 do {
  mc = 0;
  dict = new_dict();
  for (i = 0; i < rc; ++i) {
   if (dict->count)
    dict->count = 0;
   if (treematch(tree, rules[i]->child[0], dict)) {
    ++mc;
    m = 1;
    if (dict->count) {
     skel = copy_tree(rules[i]->child[1]);
     subst(skel, dict, &skel);
     *tree = *skel;
    } else {
     *tree = *copy_tree(rules[i]->child[1]);
    }
    break;
   }
  }
 } while (mc);
 for (i = 0; i < tree->child_count; ++i)
  if (matchloop(rules, rc, tree->child[i])) {
   m = 1;
  }
 return m;
}

canonical_original_code:
int matchloop(struct exp_tree **rules, int rc, struct exp_tree *tree)
{
 struct dict_s *dict;
 struct exp_tree *skel;
 int i;
 int mc;
 int m = 0;
 do {
  mc = 0;
  dict = new_dict();
  for (i = 0; i < rc; ++i) {
   if (dict->count)
    dict->count = 0;
   if (treematch(tree, rules[i]->child[0], dict)) {
    ++mc;
    m = 1;
    if (dict->count) {
     skel = copy_tree(rules[i]->child[1]);
     subst(skel, dict, &skel);
     *tree = *skel;
    } else {
     *tree = *copy_tree(rules[i]->child[1]);
    }
    break;
   }
  }
 } while (mc);
 for (i = 0; i < tree->child_count; ++i)
  if (matchloop(rules, rc, tree->child[i])) {
   m = 1;
  }
 return m;
}

variable_types: rc int

variable_types: struct dict_s { char **symbol; struct exp_tree { char head_type; struct token { char type; char *start; int len; int from_line; int from_char; } *tok; unsigned int child_count; unsigned int child_alloc; struct exp_tree **child; } **match; int count; int alloc; int success; } *

variable_types: mc int

variable_types: m int

user_defined_types: struct dict_s { char **symbol; struct exp_tree **match; int count; int alloc; int success; }


norm

original_code:
int norm(exp_tree_t *et)
{
 int i = 0;
 int j = 0;
 if (et->child_count == 0) {
  return 1;
 } else {
  for (i = 0; i < et->child_count; ++i) {
   j += norm(et->child[i]);
  }
  return j;
 }
}

canonical_original_code:
int norm(struct exp_tree *et)
{
 int i = 0;
 int j = 0;
 if (et->child_count == 0) {
  return 1;
 } else {
  for (i = 0; i < et->child_count; ++i) {
   j += norm(et->child[i]);
  }
  return j;
 }
}

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall check_incomplete(__int64 a1)
{
__int64 result; // rax
_QWORD *v2; // rbx
__int64 v3; // rbp
__int64 v4; // rdx

if ( *(_BYTE *)a1 != 30
|| (v4 = *(_QWORD *)(a1 + 8), result = *(unsigned int *)(v4 + 16), (_DWORD)result != 1)
|| **(_BYTE **)(v4 + 8) != 68 )
{
result = *(unsigned int *)(a1 + 16);
if ( (_DWORD)result )
{
v2 = *(_QWORD **)(a1 + 24);
v3 = (__int64)&v2[(unsigned int)(result - 1) + 1];
while ( 1 )
{
result = check_incomplete(*v2);
if ( (_DWORD)result )
break;
if ( (_QWORD *)v3 == ++v2 )
return result;
}
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 result; // rax
_QWORD *v2; // rbx
__int64 v3; // rbp
__int64 v4; // rdx

if ( *(_BYTE *)a1 != 30
|| (v4 = *(_QWORD *)(a1 + 8), result = *(unsigned int *)(v4 + 16), (_DWORD)result != 1)
|| **(_BYTE **)(v4 + 8) != 68 )
{
result = *(unsigned int *)(a1 + 16);
if ( (_DWORD)result )
{
v2 = *(_QWORD **)(a1 + 24);
v3 = (__int64)&v2[(unsigned int)(result - 1) + 1];
while ( 1 )
{
result = func1(*v2);
if ( (_DWORD)result )
break;
if ( (_QWORD *)v3 == ++v2 )
return result;
}
result = 1LL;
}
}
return result;
}

original_code:
int check_incomplete(exp_tree_t *et)
{
 int i = 0;
 if (et->head_type == FUNC
      && et->tok->len == 1
      && et->tok->start[0] == 'D')
  return 1;
 for (i = 0; i < et->child_count; ++i)
  if (check_incomplete(et->child[i]))
   return 1;
 return 0;
}

canonical_original_code:
int check_incomplete(struct exp_tree *et)
{
 int i = 0;
 if (et->head_type == FUNC
      && et->tok->len == 1
      && et->tok->start[0] == 'D')
  return 1;
 for (i = 0; i < et->child_count; ++i)
  if (check_incomplete(et->child[i]))
   return 1;
 return 0;
}