main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // er12
char v5; // al
char v7; // [rsp+7h] [rbp-10051h] BYREF
unsigned int v8; // [rsp+8h] [rbp-10050h] BYREF
unsigned int v9; // [rsp+Ch] [rbp-1004Ch] BYREF
char v10[65608]; // [rsp+10h] [rbp-10048h] BYREF

v3 = 0LL;
opterr = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "i:");
if ( v4 == -1 )
break;
if ( v4 == 105 )
v3 = optarg;
}
if ( !v3 )
{
puts("ERROR no input file (-i) given.");
return v4;
}
puts("\n\n*** MSP430SIM ***\n");
puts("- s\tstep");
puts("- r\treset");
puts("- q\tquit\n");
printf("Loading bin file from: %s\n", v3);
v4 = bin_load(v3, v10, 63488LL);
if ( v4 == -1 )
{
printf("ERROR opening file: %s\n", v3);
return v4;
}
cpu_reset(v10);
while ( 1 )
{
while ( 1 )
{
_isoc99_scanf("%c", &v7);
if ( v7 == 114 )
{
bin_load(v3, v10, 63488LL);
cpu_reset(v10);
puts("RESET");
v5 = v7;
goto LABEL_13;
}
if ( v7 > 114 )
break;
if ( v7 == 109 )
{
printf("\nfrom address (hex): ");
_isoc99_scanf("%04x", &v8);
printf("to address (hex): ");
_isoc99_scanf("%04x", &v9);
util_printmem(v10, v8, v9);
v5 = v7;
goto LABEL_13;
}
if ( v7 == 113 )
{
puts("QUIT");
v5 = v7;
goto LABEL_13;
}
}
if ( v7 == 115 )
{
util_printreg(v10);
util_printmem(v10, 4294967288LL, 8LL);
if ( (unsigned int)cpu_process_instruction(v10) )
puts("\nERROR illigal instruction");
else
puts("\nOK");
v5 = v7;
LABEL_13:
if ( v5 == 113 )
return 0;
}
}
}

original_code:
int main(int argc, char **argv) {
 char *binfile = ((void *)0);
 char c;
 int o;
 int s;
 int e;
 opterr = 0;
 while ((o = getopt (argc, argv, "i:")) != -1) {
  switch (o) {
   case 'i':
     binfile = optarg;
    break;
    }
 }
 if(!binfile) {
  printf("ERROR no input file (-i) given.\n");
  return -1;
 }
 context_t ctx;
 printf("\n\n*** MSP430SIM ***\n\n");
 printf("- s\tstep\n");
 printf("- r\treset\n");
 printf("- q\tquit\n\n");
 printf("Loading bin file from: %s\n", binfile);
 if(bin_load(binfile, &ctx, 0xF800) == -1) {
  printf("ERROR opening file: %s\n", binfile);
  return -1;
 }
 cpu_reset(&ctx);
 do {
  scanf("%c", &c);
  switch(c) {
   case 's':
    util_printreg(&ctx);
    util_printmem(&ctx, -8, 8);
    if(cpu_process_instruction(&ctx) != 0) {
     printf("\nERROR illigal instruction\n");
    }
    else {
     printf("\nOK\n");
    }
    break;
   case 'r':
    bin_load(binfile, &ctx, 0xF800);
    cpu_reset(&ctx);
    printf("RESET\n");
    break;
   case 'm':
    printf("\nfrom address (hex): ");
    scanf("%04x", &s);
    printf("to   address (hex): ");
    scanf("%04x", &e);
    util_printmem(&ctx, s, e);
    break;
   case 'q':
    printf("QUIT\n");
    break;
  }
 }
 while(c != 'q');
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 char *binfile = ((void *)0);
 char c;
 int o;
 int s;
 int e;
 opterr = 0;
 while ((o = getopt (argc, argv, "i:")) != -1) {
  switch (o) {
   case 'i':
     binfile = optarg;
    break;
    }
 }
 if(!binfile) {
  printf("ERROR no input file (-i) given.\n");
  return -1;
 }
 struct context_t ctx;
 printf("\n\n*** MSP430SIM ***\n\n");
 printf("- s\tstep\n");
 printf("- r\treset\n");
 printf("- q\tquit\n\n");
 printf("Loading bin file from: %s\n", binfile);
 if(bin_load(binfile, &ctx, 0xF800) == -1) {
  printf("ERROR opening file: %s\n", binfile);
  return -1;
 }
 cpu_reset(&ctx);
 do {
  scanf("%c", &c);
  switch(c) {
   case 's':
    util_printreg(&ctx);
    util_printmem(&ctx, -8, 8);
    if(cpu_process_instruction(&ctx) != 0) {
     printf("\nERROR illigal instruction\n");
    }
    else {
     printf("\nOK\n");
    }
    break;
   case 'r':
    bin_load(binfile, &ctx, 0xF800);
    cpu_reset(&ctx);
    printf("RESET\n");
    break;
   case 'm':
    printf("\nfrom address (hex): ");
    scanf("%04x", &s);
    printf("to   address (hex): ");
    scanf("%04x", &e);
    util_printmem(&ctx, s, e);
    break;
   case 'q':
    printf("QUIT\n");
    break;
  }
 }
 while(c != 'q');
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
binfile
char *

variable_types: c char

variable_types: o int

variable_types: s int

variable_types: e int

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; }


decompiled_code:
int __fastcall util_printreg(unsigned __int16 *a1)
{
unsigned int v1; // ebp
unsigned __int16 *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rsi

v1 = 0;
v2 = a1;
puts("\n\nRegister Dump\n-------------");
do
{
v3 = v1++;
printf("R%02d |", v3);
}
while ( v1 != 16 );
putchar(10);
do
{
v4 = *v2++;
printf("%04x|", v4);
}
while ( a1 + 16 != v2 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(unsigned __int16 *a1)
{
unsigned int v1; // ebp
unsigned __int16 *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rsi

v1 = 0;
v2 = a1;
puts("\n\nRegister Dump\n-------------");
do
{
v3 = v1++;
printf("R%02d |", v3);
}
while ( v1 != 16 );
putchar(10);
do
{
v4 = *v2++;
printf("%04x|", v4);
}
while ( a1 + 16 != v2 );
return putchar(10);
}

original_code:
void util_printreg(context_t *c) {
 int i;
 printf("\n\nRegister Dump\n-------------\n");
 for(i = 0; i < 16; i++) {
  printf("R%02d |", i);
 }
 printf("\n");
 for(i = 0; i < 16; i++) {
  printf("%04x|", *(uint16_t *)(&c->reg[i]));
 }
 printf("\n");
}

canonical_original_code:
void util_printreg(struct context_t *c) {
 int i;
 printf("\n\nRegister Dump\n-------------\n");
 for(i = 0; i < 16; i++) {
  printf("R%02d |", i);
 }
 printf("\n");
 for(i = 0; i < 16; i++) {
  printf("%04x|", *(unsigned short *)(&c->reg[i]));
 }
 printf("\n");
}

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; } *

variable_types: i int

return_type: void

user_defined_types: struct context_t { struct word_t reg[16]; unsigned char mem[65535]; }


decompiled_code:
int __fastcall util_printmem(__int64 a1, int a2, int a3)
{
int v3; // er15
unsigned int v5; // ebx
int result; // eax
__int64 v7; // rsi
unsigned __int8 *v8; // rbp
int v9; // eax
char v10[120]; // [rsp+0h] [rbp-78h] BYREF

v3 = 0;
v5 = a2;
if ( a2 < 0 )
{
v9 = *(unsigned __int16 *)cpu_rregw(a1, 0LL);
v5 = v9 + a2;
v3 = v9;
if ( v9 + a2 < 0 )
v5 = 0;
a3 += v9;
if ( a3 > 0xFFFF )
a3 = 0xFFFF;
}
for ( result = printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", v5, (unsigned int)a3);
a3 >= (int)v5;
result = printf("%04x|%02x%02x\t%s\n", v7, v8[1], *v8, v10) )
{
v8 = (unsigned __int8 *)cpu_rmemw(a1, (unsigned __int16)v5);
if ( v5 == v3 )
printf("-> ");
else
printf(" ");
cpu_disasm_instruction(a1, (unsigned __int16)v5, v10);
v7 = v5;
v5 += 2;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, int a2, int a3)
{
int v3; // er15
unsigned int v5; // ebx
int result; // eax
__int64 v7; // rsi
unsigned __int8 *v8; // rbp
int v9; // eax
char v10[120]; // [rsp+0h] [rbp-78h] BYREF

v3 = 0;
v5 = a2;
if ( a2 < 0 )
{
v9 = *(unsigned __int16 *)cpu_rregw(a1, 0LL);
v5 = v9 + a2;
v3 = v9;
if ( v9 + a2 < 0 )
v5 = 0;
a3 += v9;
if ( a3 > 0xFFFF )
a3 = 0xFFFF;
}
for ( result = printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", v5, (unsigned int)a3);
a3 >= (int)v5;
result = printf("%04x|%02x%02x\t%s\n", v7, v8[1], *v8, v10) )
{
v8 = (unsigned __int8 *)cpu_rmemw(a1, (unsigned __int16)v5);
if ( v5 == v3 )
printf("-> ");
else
printf(" ");
cpu_disasm_instruction(a1, (unsigned __int16)v5, v10);
v7 = v5;
v5 += 2;
}
return result;
}

original_code:
void util_printmem(context_t *c, int s, int e) {
 char buf[64];
 word_t *w;
 int i;
 int ds;
 int de;
 word_t *pc;
 maddr_t a = 0;
 if(s < 0) {
  pc = cpu_rregw(c, 0);
  a = *((maddr_t *)(pc));
  ds = (int)(a) + s;
  if(ds < 0) ds = 0;
  de = a + e;
  if(de > 0xFFFF) de = 0xFFFF;
 }
 else {
  ds = s;
  de = e;
 }
 printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", ds,de);
 for(i = ds; i <= de; i += sizeof(word_t)) {
  w = cpu_rmemw(c, i);
  if(i == a) printf("-> "); else printf("   ");
  cpu_disasm_instruction(c, i, buf);
  printf("%04x|%02x%02x\t%s\n", i, w->h, w->l, buf);
 }
}

canonical_original_code:
void util_printmem(struct context_t *c, int s, int e) {
 char buf[64];
 struct word_t *w;
 int i;
 int ds;
 int de;
 struct word_t *pc;
 unsigned short a = 0;
 if(s < 0) {
  pc = cpu_rregw(c, 0);
  a = *((unsigned short *)(pc));
  ds = (int)(a) + s;
  if(ds < 0) ds = 0;
  de = a + e;
  if(de > 0xFFFF) de = 0xFFFF;
 }
 else {
  ds = s;
  de = e;
 }
 printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", ds,de);
 for(i = ds; i <= de; i += sizeof(word_t)) {
  w = cpu_rmemw(c, i);
  if(i == a) printf("-> "); else printf("   ");
  cpu_disasm_instruction(c, i, buf);
  printf("%04x|%02x%02x\t%s\n", i, w->h, w->l, buf);
 }
}

variable_types:
buf
char [64]

variable_types: struct word_t { unsigned char l; unsigned char h; } *

variable_types: ds int

variable_types: de int

variable_types:
a
unsigned short


canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
runPsProg(argc, argv, envp);
return 0;
}

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

original_code:
int main(int argc, char ** argv)
{
   runPsProg();
   return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
   runPsProg();
   return 0;
}

return_type: int


runPsProg

decompiled_code:
__int64 runPsProg()
{
initArray((unsigned int)arraySize);
readPs();
return print();
}

canonical_decompiled_code:
__int64 func7()
{
func8((unsigned int)arraySize);
func13();
return func17();
}

original_code:
void runPsProg()
{
   initArray(arraySize);
   readPs();
   print();
}


initArray

decompiled_code:
void *__fastcall initArray(int a1)
{
void *result; // rax

result = calloc(a1, 0x408uLL);
array = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(int a1)
{
void *result; // rax

result = calloc(a1, 0x408uLL);
array = result;
return result;
}

original_code:
void initArray(int size)
{
   array = calloc(size, sizeof(PrintOut));
}

variable_types: size int


expandArray

decompiled_code:
int __fastcall expandArray(int a1, int a2)
{
void *v2; // rax

v2 = realloc(array, 1032LL * (a2 + a1));
array = v2;
if ( v2 )
{
arraySize = a2 + a1;
}
else
{
LODWORD(v2) = puts("Cannot allocate more memory.");
arraySize = 0;
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func9(int a1, int a2)
{
void *v2; // rax

v2 = realloc(array, 1032LL * (a2 + a1));
array = v2;
if ( v2 )
{
arraySize = a2 + a1;
}
else
{
LODWORD(v2) = puts("Cannot allocate more memory.");
arraySize = 0;
}
return (int)v2;
}

original_code:
void expandArray(int curSize, int expSize)
{
    int totalSize = expSize + curSize;
 array = realloc(array, totalSize*sizeof(PrintOut));
 if (array == ((void *)0))
    {
        printf("Cannot allocate more memory.\n");
        arraySize = 0;
    }
    else
    {
        arraySize = totalSize;
    }
}

variable_types: curSize int

variable_types: expSize int

variable_types: totalSize int


checkFull

decompiled_code:
_BOOL8 checkFull()
{
return arraySize - 2 == elements;
}

canonical_decompiled_code:
_BOOL8 func10()
{
return arraySize - 2 == elements;
}

original_code:
int checkFull()
{
  if(elements == (arraySize-2))
  {return 1;}
  else
  {return 0;}
}


saveData

decompiled_code:
int __fastcall saveData(char *src, char *nptr, char *a3, __int64 a4)
{
const char *v6; // r15
int v7; // ebx
__int64 v8; // r13
char *v9; // r12
int result; // eax
char v11[1025]; // [rsp+1h] [rbp-839h]
char v12[1024]; // [rsp+2h] [rbp-838h] BYREF
char dest[1080]; // [rsp+402h] [rbp-438h] BYREF

strcpy(dest, src);
v6 = stripSyms(dest, "()");
v7 = elements + 1;
v8 = a4 + 1032LL * elements;
v9 = &stpcpy((char *)(v8 + 8), a3)[-v8 - 8];
memcpy(v12, (const void *)(v8 + 8), (size_t)(v9 + 1));
v11[(_QWORD)v9] = 0;
strcpy((char *)(v8 + 8), v12);
*(_DWORD *)v8 = atoi(v6);
result = atoi(nptr);
elements = v7;
*(_DWORD *)(v8 + 4) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func11(char *src, char *nptr, char *a3, __int64 a4)
{
const char *v6; // r15
int v7; // ebx
__int64 v8; // r13
char *v9; // r12
int result; // eax
char v11[1025]; // [rsp+1h] [rbp-839h]
char v12[1024]; // [rsp+2h] [rbp-838h] BYREF
char dest[1080]; // [rsp+402h] [rbp-438h] BYREF

strcpy(dest, src);
v6 = stripSyms(dest, "()");
v7 = elements + 1;
v8 = a4 + 1032LL * elements;
v9 = &stpcpy((char *)(v8 + 8), a3)[-v8 - 8];
memcpy(v12, (const void *)(v8 + 8), (size_t)(v9 + 1));
v11[(_QWORD)v9] = 0;
strcpy((char *)(v8 + 8), v12);
*(_DWORD *)v8 = atoi(v6);
result = atoi(nptr);
elements = v7;
*(_DWORD *)(v8 + 4) = result;
return result;
}

original_code:
void saveData(char * processID, char * parentID, char * command, PrintOut *emptyArr)
{
    char temp [1024];
    char temp2 [1024];
    strcpy(temp2, processID);
    char * strippedPID = stripSyms(temp2,"()");
 strcpy(emptyArr[elements].CMD, command);
    strcpy(temp,emptyArr[elements].CMD);
    temp[strlen(temp)-1] = '\0';
    strcpy(emptyArr[elements].CMD,temp);
 emptyArr[elements].PID = atoi(strippedPID);
 emptyArr[elements].PPID = atoi(parentID);
    elements++;
}

canonical_original_code:
void saveData(char *processID, char *parentID, char *command, struct PrintOut *emptyArr)
{
    char temp[1024];
    char temp2[1024];
    strcpy(temp2, processID);
    char *strippedPID = stripSyms(temp2,"()");
 strcpy(emptyArr[elements].CMD, command);
    strcpy(temp,emptyArr[elements].CMD);
    temp[strlen(temp)-1] = '\0';
    strcpy(emptyArr[elements].CMD,temp);
 emptyArr[elements].PID = atoi(strippedPID);
 emptyArr[elements].PPID = atoi(parentID);
    elements++;
}

variable_types:
processID
char *

variable_types:
parentID
char *

variable_types:
command
char *

variable_types: struct PrintOut { int PID; int PPID; char CMD[1024]; } *

variable_types:
temp
char [1024]

variable_types:
strippedPID
char *


parsePsLine

decompiled_code:
int __fastcall parsePsLine(char *a1)
{
char *v1; // rbp
char *v2; // r13
char *v3; // rax
char *v4; // r12
int v5; // ebx
char *v6; // rax

v3 = strtok(a1, " ");
if ( v3 )
{
v4 = v3;
v5 = 1;
while ( 1 )
{
if ( v5++ == 2 )
{
v2 = v4;
}
else if ( v5 == 3 )
{
v1 = v4;
}
v6 = strtok(0LL, " ");
if ( !v6 )
break;
v4 = v6;
}
}
else
{
v4 = 0LL;
}
return saveData(v2, v1, v4, (__int64)array);
}

canonical_decompiled_code:
int __fastcall func12(char *a1)
{
char *v1; // rbp
char *v2; // r13
char *v3; // rax
char *v4; // r12
int v5; // ebx
char *v6; // rax

v3 = strtok(a1, " ");
if ( v3 )
{
v4 = v3;
v5 = 1;
while ( 1 )
{
if ( v5++ == 2 )
{
v2 = v4;
}
else if ( v5 == 3 )
{
v1 = v4;
}
v6 = strtok(0LL, " ");
if ( !v6 )
break;
v4 = v6;
}
}
else
{
v4 = 0LL;
}
return func11(v2, v1, v4, (__int64)array);
}

original_code:
void parsePsLine(char * bufLine)
{
       char * processID; char * parentID; char * command; char * current;
       int c = 1; char * nextWordPtr; char * buf = bufLine; char * currentTemp;
          int PID; int PPID; char tempCMD[1024];
          PID = 2; PPID = 3;
       nextWordPtr = strtok(buf," ");
          while (nextWordPtr != ((void *)0)) {
          current = nextWordPtr;
          if(c == PID)
          {processID = nextWordPtr;}
          else if ( c == PPID)
          {parentID = nextWordPtr;}
           nextWordPtr = strtok(((void *)0)," ");
           c++;
          }
          command = current;
          saveData(processID,parentID,command,array);
}

canonical_original_code:
void parsePsLine(char *bufLine)
{
       char *processID; char *parentID; char *command; char *current;
       int c = 1; char *nextWordPtr; char *buf = bufLine; char *currentTemp;
          int PID; int PPID; char tempCMD[1024];
          PID = 2; PPID = 3;
       nextWordPtr = strtok(buf," ");
          while (nextWordPtr != ((void *)0)) {
          current = nextWordPtr;
          if(c == PID)
          {processID = nextWordPtr;}
          else if ( c == PPID)
          {parentID = nextWordPtr;}
           nextWordPtr = strtok(((void *)0)," ");
           c++;
          }
          command = current;
          saveData(processID,parentID,command,array);
}

variable_types:
bufLine
char *

variable_types:
current
char *

variable_types: c int

variable_types:
nextWordPtr
char *

variable_types:
buf
char *

variable_types:
currentTemp
char *

variable_types: PID int

variable_types: PPID int

variable_types:
tempCMD
char [1024]


readPs

decompiled_code:
int readPs()
{
FILE *v0; // rax
FILE *v1; // rdi
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = popen("/bin/ps -aefc", "r");
psAux = v0;
v1 = v0;
if ( v0 )
{
fgets(v3, 1024, v0);
while ( fgets(v3, 1024, psAux) )
{
if ( checkFull() )
expandArray(arraySize, expandSize);
parsePsLine(v3);
}
v1 = psAux;
}
return pclose(v1);
}

canonical_decompiled_code:
int func13()
{
FILE *v0; // rax
FILE *v1; // rdi
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = popen("/bin/ps -aefc", "r");
psAux = v0;
v1 = v0;
if ( v0 )
{
fgets(v3, 1024, v0);
while ( fgets(v3, 1024, psAux) )
{
if ( func10() )
func9(arraySize, expandSize);
func12(v3);
}
v1 = psAux;
}
return pclose(v1);
}

original_code:
void readPs(void)
{
      char buf[1024];
      psAux = popen("/bin/ps -aefc", "r");
      if (psAux)
      {
          fgets(buf, sizeof(buf), psAux);
          while(fgets(buf, sizeof(buf), psAux))
          {
             if(checkFull() == 1)
             {expandArray(arraySize,expandSize);}
             parsePsLine(buf);
          }
     }
     pclose(psAux);
}

canonical_original_code:
void readPs()
{
      char buf[1024];
      psAux = popen("/bin/ps -aefc", "r");
      if (psAux)
      {
          fgets(buf, sizeof(buf), psAux);
          while(fgets(buf, sizeof(buf), psAux))
          {
             if(checkFull() == 1)
             {expandArray(arraySize,expandSize);}
             parsePsLine(buf);
          }
     }
     pclose(psAux);
}

variable_types:
buf
char [1024]


stripThree

decompiled_code:
__int64 __fastcall stripThree(__int64 a1)
{
return a1 + 3;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return a1 + 3;
}

original_code:
char *stripThree(char *string)
{
    return string+3;
}

variable_types:
string
char *

return_type: char *


printTabs

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

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

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

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

original_code:
void printTabs(int numTabs)
{
    int j;
    for(j = 0; j < numTabs; j++)
    {
        printf("\t");
    }
}

variable_types: numTabs int

variable_types: j int


printTree

decompiled_code:
__int64 __fastcall printTree(int a1, int a2)
{
__int64 result; // rax
int v3; // ebp
__int64 v4; // rbx

result = (unsigned int)elements;
if ( elements > 0 )
{
v3 = 0;
v4 = 0LL;
do
{
result = (__int64)array;
if ( *(_DWORD *)((char *)array + v4 + 4) == a2 )
{
printTabs(a1);
printf(
"%s [PID: %d] [PPID: %d]\n",
(const char *)array + v4 + 8,
*(unsigned int *)((char *)array + v4),
*(unsigned int *)((char *)array + v4 + 4));
result = printTree((unsigned int)(a1 + 1), *(unsigned int *)((char *)array + v4));
}
++v3;
v4 += 1032LL;
}
while ( elements > v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, int a2)
{
__int64 result; // rax
int v3; // ebp
__int64 v4; // rbx

result = (unsigned int)elements;
if ( elements > 0 )
{
v3 = 0;
v4 = 0LL;
do
{
result = (__int64)array;
if ( *(_DWORD *)((char *)array + v4 + 4) == a2 )
{
func15(a1);
printf(
"%s [PID: %d] [PPID: %d]\n",
(const char *)array + v4 + 8,
*(unsigned int *)((char *)array + v4),
*(unsigned int *)((char *)array + v4 + 4));
result = func16((unsigned int)(a1 + 1), *(unsigned int *)((char *)array + v4));
}
++v3;
v4 += 1032LL;
}
while ( elements > v3 );
}
return result;
}

original_code:
void printTree(int depth, int curID)
{
     int j;
     int r;
     for(j = 0; j < elements; j ++)
     {
        if(array[j].PPID == curID)
        {
           printTabs(depth);
           printf("%s [PID: %d] [PPID: %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
           r = array[j].PID;
           printTree(depth+1,r);
        }
     }
     return;
}

variable_types: depth int

variable_types: curID int

variable_types: r int


print

decompiled_code:
void print()
{
char *v0; // rdi
int v1; // ebp
__int64 v2; // rbx

v0 = (char *)array;
if ( elements > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
if ( !*(_DWORD *)&v0[v2 + 4] )
{
printf("%s [PID %d] [PPID %d]\n", &v0[v2 + 8], *(unsigned int *)&v0[v2], 0LL);
printTree(2, *(_DWORD *)((char *)array + v2));
v0 = (char *)array;
}
++v1;
v2 += 1032LL;
}
while ( elements > v1 );
}
free(v0);
}

canonical_decompiled_code:
void func17()
{
char *v0; // rdi
int v1; // ebp
__int64 v2; // rbx

v0 = (char *)array;
if ( elements > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
if ( !*(_DWORD *)&v0[v2 + 4] )
{
printf("%s [PID %d] [PPID %d]\n", &v0[v2 + 8], *(unsigned int *)&v0[v2], 0LL);
func16(2, *(_DWORD *)((char *)array + v2));
v0 = (char *)array;
}
++v1;
v2 += 1032LL;
}
while ( elements > v1 );
}
free(v0);
}

original_code:
void print(void)
{
   int j;
   int r;
   int p = 0;
   for(j = 0; j < elements; j ++)
   {
       if(array[j].PPID == p)
       {
          printf("%s [PID %d] [PPID %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
          r = array[j].PID;
          printTree(2,r);
       }
   }
   free(array);
}

canonical_original_code:
void print()
{
   int j;
   int r;
   int p = 0;
   for(j = 0; j < elements; j ++)
   {
       if(array[j].PPID == p)
       {
          printf("%s [PID %d] [PPID %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
          r = array[j].PID;
          printTree(2,r);
       }
   }
   free(array);
}

variable_types: p int


testArrayInit

decompiled_code:
_BOOL8 testArrayInit()
{
int v0; // edx

initArray(arraySize);
v0 = 0;
if ( arraySize > 0 )
v0 = arraySize;
return v0 != arraySize;
}

canonical_decompiled_code:
_BOOL8 func18()
{
int v0; // edx

func8(arraySize);
v0 = 0;
if ( arraySize > 0 )
v0 = arraySize;
return v0 != arraySize;
}

original_code:
int testArrayInit(void) {
    int expectedStartingArraySize = 100;
    int i;
    int countSize = 0;
    initArray(arraySize);
    for(i = 0; i < arraySize; i++)
 {
        array[i];
        countSize++;
    }
    if(countSize == arraySize)
    {return 0;}
    else
    {return 1;}
}

canonical_original_code:
int testArrayInit() {
    int expectedStartingArraySize = 100;
    int i;
    int countSize = 0;
    initArray(arraySize);
    for(i = 0; i < arraySize; i++)
 {
        array[i];
        countSize++;
    }
    if(countSize == arraySize)
    {return 0;}
    else
    {return 1;}
}

variable_types: expectedStartingArraySize int

variable_types: i int

variable_types: countSize int


testReadPs

decompiled_code:
_BOOL8 testReadPs()
{
readPs();
return elements <= 0;
}

canonical_decompiled_code:
_BOOL8 func19()
{
func13();
return elements <= 0;
}

original_code:
int testReadPs(void) {
    int countSize = 0;
    int numRead = 0;
    readPs();
    numRead = elements;
    if(numRead > 0)
    {
      if( numRead > 100)
      {return 0;}
      else if( numRead <= 100)
      {return 0;}
    }
    else
    {return 1;}
}

canonical_original_code:
int testReadPs() {
    int countSize = 0;
    int numRead = 0;
    readPs();
    numRead = elements;
    if(numRead > 0)
    {
      if( numRead > 100)
      {return 0;}
      else if( numRead <= 100)
      {return 0;}
    }
    else
    {return 1;}
}

variable_types: numRead int


teststripSyms

decompiled_code:
__int64 teststripSyms()
{
stripSyms("(9)", "()");
return 1LL;
}

original_code:
int teststripSyms()
{
   char * expected = "9";
   char * input = "(9)";
   char * actual = stripSyms(input,"()");
   if(expected == input)
   {return 0;}
   else
   {return 1;}
}

canonical_original_code:
int teststripSyms()
{
   char *expected = "9";
   char *input = "(9)";
   char *actual = stripSyms(input,"()");
   if(expected == input)
   {return 0;}
   else
   {return 1;}
}

variable_types:
expected
char *

variable_types:
input
char *

variable_types:
actual
char *


runTests

decompiled_code:
__int64 runTests()
{
_BOOL4 v0; // ebx
_BOOL4 v1; // ebx
int v2; // ebp

v0 = testArrayInit();
v1 = testReadPs() + v0;
v2 = teststripSyms();
free(array);
return (unsigned int)(v1 + v2);
}

canonical_decompiled_code:
__int64 func21()
{
_BOOL4 v0; // ebx
_BOOL4 v1; // ebx
int v2; // ebp

v0 = func18();
v1 = func19() + v0;
v2 = func20();
free(array);
return (unsigned int)(v1 + v2);
}

original_code:
int runTests() {
    int passCount = 0;
    passCount += testArrayInit();
    passCount += testReadPs();
    passCount += teststripSyms();
    free(array);
    return passCount;
}

variable_types: passCount int


repo: weongyo/varnish-perf

decompiled_code:
void *__fastcall cot_callwhell_alloc(__int64 a1)
{
int v1; // edx
int v3; // eax
size_t v4; // rdi
int v5; // ecx
int v6; // eax
void *result; // rax
int *v8; // rax

v1 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 20) = 1LL;
if ( v1 <= 1 )
{
v4 = 1LL;
v6 = 0;
}
else
{
v3 = 1;
LODWORD(v4) = 1;
do
{
LODWORD(v4) = 2 * v4;
v5 = v3++;
}
while ( (int)v4 < v1 );
*(_DWORD *)(a1 + 24) = v5;
v6 = v4 - 1;
*(_DWORD *)(a1 + 20) = v4;
v4 = (int)v4;
}
*(_DWORD *)(a1 + 28) = v6;
result = calloc(v4, 0x10uLL);
*(_QWORD *)(a1 + 32) = result;
if ( !result )
{
v8 = _errno_location();
result = (void *)VAS_Fail(
"cot_callwhell_alloc",
"vcallout.c",
202LL,
"(cb->callwheel) != 0",
(unsigned int)*v8,
0LL);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1)
{
int v1; // edx
int v3; // eax
size_t v4; // rdi
int v5; // ecx
int v6; // eax
void *result; // rax
int *v8; // rax

v1 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a1 + 20) = 1LL;
if ( v1 <= 1 )
{
v4 = 1LL;
v6 = 0;
}
else
{
v3 = 1;
LODWORD(v4) = 1;
do
{
LODWORD(v4) = 2 * v4;
v5 = v3++;
}
while ( (int)v4 < v1 );
*(_DWORD *)(a1 + 24) = v5;
v6 = v4 - 1;
*(_DWORD *)(a1 + 20) = v4;
v4 = (int)v4;
}
*(_DWORD *)(a1 + 28) = v6;
result = calloc(v4, 0x10uLL);
*(_QWORD *)(a1 + 32) = result;
if ( !result )
{
v8 = _errno_location();
result = (void *)VAS_Fail(
"cot_callwhell_alloc",
"vcallout.c",
202LL,
"(cb->callwheel) != 0",
(unsigned int)*v8,
0LL);
}
return result;
}

original_code:
static void
cot_callwhell_alloc(struct callout_block *cb)
{
 for (cb->callwheelsize = 1, cb->callwheelbits = 0;
      cb->callwheelsize < cb->ncallout;
      cb->callwheelsize <<= 1, ++cb->callwheelbits)
  ;
 cb->callwheelmask = cb->callwheelsize - 1;
 cb->callwheel = calloc(cb->callwheelsize, sizeof(struct callout_tailq));
 do { do { if (!((cb->callwheel) != 0)) VAS_Fail(__func__, "vcallout.c", 202, "(cb->callwheel) != 0", (*__errno_location ()), 0); } while (0); } while (0);
}

variable_types: struct callout_block { long ticks; long softticks; int ncallout; int callwheelsize; int callwheelbits; int callwheelmask; struct callout_tailq { struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *vtqh_first; struct callout **vtqh_last; } *callwheel; struct callout *nextsoftcheck; } *

user_defined_types: struct callout_tailq { struct callout *vtqh_first; struct callout **vtqh_last; }

user_defined_types: struct callout_block { long ticks; long softticks; int ncallout; int callwheelsize; int callwheelbits; int callwheelmask; struct callout_tailq *callwheel; struct callout *nextsoftcheck; }


original_code:
void
callout_init(struct callout *c, int id)
{
 bzero(c, sizeof *c);
 c->magic = 0x2d634820;
 c->c_id = id;
}

variable_types: struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *

variable_types: id int


original_code:
void
COT_clock(struct callout_block *cb)
{
 struct callout *c;
 struct callout_tailq *bucket;
 clock_t curticks;
 int steps;
 int depth;
 int mpcalls;
 int mtxcalls;
 int gcalls;
 mpcalls = 0;
 mtxcalls = 0;
 gcalls = 0;
 depth = 0;
 steps = 0;
 while (cb->softticks != cb->ticks) {
  cb->softticks++;
  curticks = cb->softticks;
  bucket = &cb->callwheel[curticks & cb->callwheelmask];
  c = ((bucket)->vtqh_first);
  while (c) {
   depth++;
   if (c->c_time != curticks) {
    c = ((c)->c_links.tqe.vtqe_next);
    ++steps;
    if (steps >= 100) {
     cb->nextsoftcheck = c;
     c = cb->nextsoftcheck;
     steps = 0;
    }
   } else {
    void (*c_func)(void *);
    void *c_arg;
    cb->nextsoftcheck = ((c)->c_links.tqe.vtqe_next);
    do { if (((((c))->c_links.tqe.vtqe_next)) != ((void *)0)) (((c))->c_links.tqe.vtqe_next)->c_links.tqe.vtqe_prev = (c)->c_links.tqe.vtqe_prev; else { (bucket)->vtqh_last = (c)->c_links.tqe.vtqe_prev; } *(c)->c_links.tqe.vtqe_prev = (((c))->c_links.tqe.vtqe_next); } while (0);
    c_func = c->c_func;
    c_arg = c->c_arg;
    c->c_flags = (c->c_flags & ~0x0004);
    mpcalls++;
    if (callout_debug)
     fprintf(stdout,
         "callout mpsafe %p func %p "
         "arg %p", c, c_func, c_arg);
    c_func(c_arg);
    if (callout_debug)
     fprintf(stdout,
         "callout %p finished", c);
    steps = 0;
    c = cb->nextsoftcheck;
   }
  }
 }
 avg_depth += (depth * 1000 - avg_depth) >> 8;
 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
 avg_mtxcalls += (mtxcalls * 1000 - avg_mtxcalls) >> 8;
 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
 cb->nextsoftcheck = ((void *)0);
}

canonical_original_code:
void COT_clock(struct callout_block *cb)
{
 struct callout *c;
 struct callout_tailq *bucket;
 long curticks;
 int steps;
 int depth;
 int mpcalls;
 int mtxcalls;
 int gcalls;
 mpcalls = 0;
 mtxcalls = 0;
 gcalls = 0;
 depth = 0;
 steps = 0;
 while (cb->softticks != cb->ticks) {
  cb->softticks++;
  curticks = cb->softticks;
  bucket = &cb->callwheel[curticks & cb->callwheelmask];
  c = ((bucket)->vtqh_first);
  while (c) {
   depth++;
   if (c->c_time != curticks) {
    c = ((c)->c_links.tqe.vtqe_next);
    ++steps;
    if (steps >= 100) {
     cb->nextsoftcheck = c;
     c = cb->nextsoftcheck;
     steps = 0;
    }
   } else {
    void (*c_func)(void *);
    void *c_arg;
    cb->nextsoftcheck = ((c)->c_links.tqe.vtqe_next);
    do { if (((((c))->c_links.tqe.vtqe_next)) != ((void *)0)) (((c))->c_links.tqe.vtqe_next)->c_links.tqe.vtqe_prev = (c)->c_links.tqe.vtqe_prev; else { (bucket)->vtqh_last = (c)->c_links.tqe.vtqe_prev; } *(c)->c_links.tqe.vtqe_prev = (((c))->c_links.tqe.vtqe_next); } while (0);
    c_func = c->c_func;
    c_arg = c->c_arg;
    c->c_flags = (c->c_flags & ~0x0004);
    mpcalls++;
    if (callout_debug)
     fprintf(stdout,
         "callout mpsafe %p func %p "
         "arg %p", c, c_func, c_arg);
    c_func(c_arg);
    if (callout_debug)
     fprintf(stdout,
         "callout %p finished", c);
    steps = 0;
    c = cb->nextsoftcheck;
   }
  }
 }
 avg_depth += (depth * 1000 - avg_depth) >> 8;
 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
 avg_mtxcalls += (mtxcalls * 1000 - avg_mtxcalls) >> 8;
 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
 cb->nextsoftcheck = ((void *)0);
}

variable_types: struct callout_tailq { struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *vtqh_first; struct callout **vtqh_last; } *

variable_types: curticks long

variable_types: steps int

variable_types: mpcalls int

variable_types: mtxcalls int

variable_types: gcalls int


decompiled_code:
clock_t __fastcall COT_ticks(clock_t *a1)
{
clock_t result; // rax
struct tms v2; // [rsp+0h] [rbp-28h] BYREF

result = times(&v2);
*a1 = result;
return result;
}

canonical_decompiled_code:
clock_t __fastcall func5(clock_t *a1)
{
clock_t result; // rax
struct tms v2; // [rsp+0h] [rbp-28h] BYREF

result = times(&v2);
*a1 = result;
return result;
}

original_code:
void
COT_ticks(struct callout_block *cb)
{
 struct tms tms;
 cb->ticks = times(&tms);
}

variable_types: struct tms { long tms_utime; long tms_stime; long tms_cutime; long tms_cstime; }

user_defined_types: struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; }


decompiled_code:
void __fastcall COT_fini(__int64 a1)
{
free(*(void **)(a1 + 32));
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
free(*(void **)(a1 + 32));
}

original_code:
void
COT_fini(struct callout_block *cb)
{
 free(cb->callwheel);
}


decompiled_code:
__int64 __fastcall humanize_number(char *s, size_t maxlen, __int64 a3, const char *a4, int a5, char a6)
{
int *v12; // rax
__int16 *v13; // r9
__int64 v14; // r11
__int64 v15; // rdx
const char *v16; // r8
size_t v17; // rdi
int i; // er10
const char *v19; // r9
__int64 v20; // rdx
__int64 v21; // rsi
int v22; // eax
int *v25; // rax
int *v26; // rax
int v27; // [rsp+Ch] [rbp-5Ch]
const char *v28; // [rsp+18h] [rbp-50h]
__int64 v29; // [rsp+20h] [rbp-48h]
__int16 *v30; // [rsp+28h] [rbp-40h]

if ( !s )
{
v26 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 51LL, "buf != NULL", (unsigned int)*v26, 0LL);
}
if ( !a4 )
{
v25 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 52LL, "suffix != NULL", (unsigned int)*v25, 0LL);
}
if ( a5 < 0 )
{
v12 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 53LL, "scale >= 0", (unsigned int)*v12, 0LL);
}
if ( (a6 & 8) != 0 )
{
v13 = (__int16 *)&unk_350;
v14 = 1000LL;
if ( (a6 & 4) != 0 )
v13 = L"BkMGTPE";
}
else
{
v13 = L"";
v14 = 1024LL;
if ( (a6 & 4) != 0 )
v13 = (__int16 *)&unk_35E;
}
if ( a5 > 6 && (a5 & 0x30) == 0 || !s || !a4 )
return (unsigned int)-1;
if ( maxlen )
*s = 0;
v27 = 1;
v15 = 2LL;
if ( a3 < 0 )
{
v27 = -1;
a3 = -a3;
v15 = 3LL;
}
v16 = (const char *)&unk_3A2;
if ( (a6 & 2) == 0 )
{
++v15;
v16 = " ";
}
v30 = v13;
v29 = v14;
v28 = v16;
v17 = strlen(a4) + v15;
if ( v17 + 1 > maxlen )
return (unsigned int)-1;
i = a5 & 0x30;
v19 = (const char *)v30;
if ( (a5 & 0x30) == 0 )
{
if ( a5 <= 0 )
return (unsigned int)snprintf(s, maxlen, "%jd%s%s%s", a3 * v27, v28, v19, a4);
do
{
++i;
v20 = a3 % v29;
a3 /= v29;
}
while ( a5 > i && i <= 6 );
LABEL_35:
if ( (int)v20 <= 949 && a3 <= 9 && i )
{
if ( (a6 & 1) != 0 )
{
if ( v17 + 3 <= maxlen )
return (unsigned int)snprintf(
s,
maxlen,
"%d%s%d%s%s%s",
(unsigned int)(a3 * v27),
",",
(unsigned int)(v20 + 50) / 0x64uLL,
v28,
(const char *)&v30[i],
a4);
return (unsigned int)-1;
}
v19 = (const char *)&v30[i];
}
else
{
v19 = (const char *)&v30[i];
a3 += (unsigned int)(v20 + 50) / 0x3E8uLL;
}
return (unsigned int)snprintf(s, maxlen, "%jd%s%s%s", a3 * v27, v28, v19, a4);
}
v21 = 1LL;
v22 = maxlen - v17 - 1;
if ( (int)maxlen - (int)v17 > 0 )
{
do
v21 *= 10LL;
while ( v22-- != 0 );
}
LODWORD(v20) = 0;
for ( i = 0; (a3 >= v21 || v21 - 1 == a3 && (int)v20 > 949) && i != 7; ++i )
{
v20 = a3 % v29;
a3 /= v29;
}
if ( (a5 & 0x10) == 0 )
goto LABEL_35;
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s, size_t maxlen, __int64 a3, const char *a4, int a5, char a6)
{
int *v12; // rax
__int16 *v13; // r9
__int64 v14; // r11
__int64 v15; // rdx
const char *v16; // r8
size_t v17; // rdi
int i; // er10
const char *v19; // r9
__int64 v20; // rdx
__int64 v21; // rsi
int v22; // eax
int *v25; // rax
int *v26; // rax
int v27; // [rsp+Ch] [rbp-5Ch]
const char *v28; // [rsp+18h] [rbp-50h]
__int64 v29; // [rsp+20h] [rbp-48h]
__int16 *v30; // [rsp+28h] [rbp-40h]

if ( !s )
{
v26 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 51LL, "buf != NULL", (unsigned int)*v26, 0LL);
}
if ( !a4 )
{
v25 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 52LL, "suffix != NULL", (unsigned int)*v25, 0LL);
}
if ( a5 < 0 )
{
v12 = _errno_location();
VAS_Fail("humanize_number", "humanize_number.c", 53LL, "scale >= 0", (unsigned int)*v12, 0LL);
}
if ( (a6 & 8) != 0 )
{
v13 = (__int16 *)&unk_350;
v14 = 1000LL;
if ( (a6 & 4) != 0 )
v13 = L"BkMGTPE";
}
else
{
v13 = L"";
v14 = 1024LL;
if ( (a6 & 4) != 0 )
v13 = (__int16 *)&unk_35E;
}
if ( a5 > 6 && (a5 & 0x30) == 0 || !s || !a4 )
return (unsigned int)-1;
if ( maxlen )
*s = 0;
v27 = 1;
v15 = 2LL;
if ( a3 < 0 )
{
v27 = -1;
a3 = -a3;
v15 = 3LL;
}
v16 = (const char *)&unk_3A2;
if ( (a6 & 2) == 0 )
{
++v15;
v16 = " ";
}
v30 = v13;
v29 = v14;
v28 = v16;
v17 = strlen(a4) + v15;
if ( v17 + 1 > maxlen )
return (unsigned int)-1;
i = a5 & 0x30;
v19 = (const char *)v30;
if ( (a5 & 0x30) == 0 )
{
if ( a5 <= 0 )
return (unsigned int)snprintf(s, maxlen, "%jd%s%s%s", a3 * v27, v28, v19, a4);
do
{
++i;
v20 = a3 % v29;
a3 /= v29;
}
while ( a5 > i && i <= 6 );
LABEL_35:
if ( (int)v20 <= 949 && a3 <= 9 && i )
{
if ( (a6 & 1) != 0 )
{
if ( v17 + 3 <= maxlen )
return (unsigned int)snprintf(
s,
maxlen,
"%d%s%d%s%s%s",
(unsigned int)(a3 * v27),
",",
(unsigned int)(v20 + 50) / 0x64uLL,
v28,
(const char *)&v30[i],
a4);
return (unsigned int)-1;
}
v19 = (const char *)&v30[i];
}
else
{
v19 = (const char *)&v30[i];
a3 += (unsigned int)(v20 + 50) / 0x3E8uLL;
}
return (unsigned int)snprintf(s, maxlen, "%jd%s%s%s", a3 * v27, v28, v19, a4);
}
v21 = 1LL;
v22 = maxlen - v17 - 1;
if ( (int)maxlen - (int)v17 > 0 )
{
do
v21 *= 10LL;
while ( v22-- != 0 );
}
LODWORD(v20) = 0;
for ( i = 0; (a3 >= v21 || v21 - 1 == a3 && (int)v20 > 949) && i != 7; ++i )
{
v20 = a3 % v29;
a3 /= v29;
}
if ( (a5 & 0x10) == 0 )
goto LABEL_35;
return (unsigned int)i;
}

original_code:
int
humanize_number(char *buf, size_t len, int64_t quotient,
    const char *suffix, int scale, int flags)
{
 const char *prefixes, *sep;
 int i, r, remainder, maxscale, s1, s2, sign;
 int64_t divisor, max;
 size_t baselen;
 do { if (!(buf != ((void *)0))) VAS_Fail(__func__, "humanize_number.c", 51, "buf != NULL", (*__errno_location ()), 0); } while (0);
 do { if (!(suffix != ((void *)0))) VAS_Fail(__func__, "humanize_number.c", 52, "suffix != NULL", (*__errno_location ()), 0); } while (0);
 do { if (!(scale >= 0)) VAS_Fail(__func__, "humanize_number.c", 53, "scale >= 0", (*__errno_location ()), 0); } while (0);
 remainder = 0;
 if (flags & 0x08) {
  divisor = 1000;
  if (flags & 0x04)
   prefixes = "B\0k\0M\0G\0T\0P\0E";
  else
   prefixes = "\0\0k\0M\0G\0T\0P\0E";
 } else {
  divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0K\0M\0G\0T\0P\0E";
  else
   prefixes = "\0\0K\0M\0G\0T\0P\0E";
 }
 maxscale = 7;
 if (scale >= maxscale &&
     (scale & (0x20 | 0x10)) == 0)
  return (-1);
 if (buf == ((void *)0) || suffix == ((void *)0))
  return (-1);
 if (len > 0)
  buf[0] = '\0';
 if (quotient < 0) {
  sign = -1;
  quotient = -quotient;
  baselen = 3;
 } else {
  sign = 1;
  baselen = 2;
 }
 if (flags & 0x02)
  sep = "";
 else {
  sep = " ";
  baselen++;
 }
 baselen += strlen(suffix);
 if (len < baselen + 1)
  return (-1);
 if (scale & (0x20 | 0x10)) {
  for (max = 1, i = len - baselen; i-- > 0;)
   max *= 10;
  for (i = 0;
      (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
      i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
  if (scale & 0x10)
   return (i);
 } else {
  for (i = 0; i < scale && i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
 }
 if (quotient <= 9 && remainder < 950 && i > 0 && flags & 0x01) {
  if (len < baselen + 1 + 2)
   return (-1);
  s1 = (int)quotient + ((remainder + 50) / 1000);
  s2 = ((remainder + 50) / 100) % 10;
  r = snprintf(buf, len, "%d%s%d%s%s%s",
      sign * s1, ",", s2,
      sep, (&prefixes[(i) << 1]), suffix);
 } else {
  r = snprintf(buf, len, "%jd%s%s%s",
      sign * (quotient + (remainder + 50) / 1000),
      sep, (&prefixes[(i) << 1]), suffix);
 }
 return (r);
}

canonical_original_code:
int humanize_number(char *buf, unsigned long len, long quotient, char *suffix, int scale, int flags)
{
 char *prefixes; char *sep;
 int i; int r; int remainder; int maxscale; int s1; int s2; int sign;
 long divisor; long max;
 unsigned long baselen;
 do { if (!(buf != ((void *)0))) VAS_Fail(__func__, "humanize_number.c", 51, "buf != NULL", (*__errno_location ()), 0); } while (0);
 do { if (!(suffix != ((void *)0))) VAS_Fail(__func__, "humanize_number.c", 52, "suffix != NULL", (*__errno_location ()), 0); } while (0);
 do { if (!(scale >= 0)) VAS_Fail(__func__, "humanize_number.c", 53, "scale >= 0", (*__errno_location ()), 0); } while (0);
 remainder = 0;
 if (flags & 0x08) {
  divisor = 1000;
  if (flags & 0x04)
   prefixes = "B\0k\0M\0G\0T\0P\0E";
  else
   prefixes = "\0\0k\0M\0G\0T\0P\0E";
 } else {
  divisor = 1024;
  if (flags & 0x04)
   prefixes = "B\0K\0M\0G\0T\0P\0E";
  else
   prefixes = "\0\0K\0M\0G\0T\0P\0E";
 }
 maxscale = 7;
 if (scale >= maxscale &&
     (scale & (0x20 | 0x10)) == 0)
  return (-1);
 if (buf == ((void *)0) || suffix == ((void *)0))
  return (-1);
 if (len > 0)
  buf[0] = '\0';
 if (quotient < 0) {
  sign = -1;
  quotient = -quotient;
  baselen = 3;
 } else {
  sign = 1;
  baselen = 2;
 }
 if (flags & 0x02)
  sep = "";
 else {
  sep = " ";
  baselen++;
 }
 baselen += strlen(suffix);
 if (len < baselen + 1)
  return (-1);
 if (scale & (0x20 | 0x10)) {
  for (max = 1, i = len - baselen; i-- > 0;)
   max *= 10;
  for (i = 0;
      (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
      i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
  if (scale & 0x10)
   return (i);
 } else {
  for (i = 0; i < scale && i < maxscale; i++) {
   remainder = quotient % divisor;
   quotient /= divisor;
  }
 }
 if (quotient <= 9 && remainder < 950 && i > 0 && flags & 0x01) {
  if (len < baselen + 1 + 2)
   return (-1);
  s1 = (int)quotient + ((remainder + 50) / 1000);
  s2 = ((remainder + 50) / 100) % 10;
  r = snprintf(buf, len, "%d%s%d%s%s%s",
      sign * s1, ",", s2,
      sep, (&prefixes[(i) << 1]), suffix);
 } else {
  r = snprintf(buf, len, "%jd%s%s%s",
      sign * (quotient + (remainder + 50) / 1000),
      sep, (&prefixes[(i) << 1]), suffix);
 }
 return (r);
}

variable_types:
buf
char *

variable_types:
len
unsigned long

variable_types: quotient long

variable_types:
suffix
char *

variable_types: scale int

variable_types: flags int

variable_types:
prefixes
char *

variable_types:
sep
char *

variable_types: i int

variable_types: r int

variable_types: remainder int

variable_types: maxscale int

variable_types: sign int

variable_types: divisor long

variable_types: max long

variable_types:
baselen
unsigned long


decompiled_code:
void __fastcall __noreturn VAS_Fail_default(const char *a1, const char *a2, unsigned int a3, const char *a4, unsigned int a5, int a6)
{
char *v7; // rax

if ( a6 )
fprintf(stderr, "Missing errorhandling code in %s(), %s line %d:\n Condition(%s) not true.\n", a1, a2, a3, a4);
else
fprintf(stderr, "Assert error in %s(), %s line %d:\n Condition(%s) not true.\n", a1, a2, a3, a4);
if ( a5 )
{
v7 = strerror(a5);
fprintf(stderr, " errno = %d (%s)\n", a5, v7);
}
VAS_Fail_default_cold();
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, const char *a2, unsigned int a3, const char *a4, unsigned int a5, int a6)
{
char *v7; // rax

if ( a6 )
fprintf(stderr, "Missing errorhandling code in %s(), %s line %d:\n Condition(%s) not true.\n", a1, a2, a3, a4);
else
fprintf(stderr, "Assert error in %s(), %s line %d:\n Condition(%s) not true.\n", a1, a2, a3, a4);
if ( a5 )
{
v7 = strerror(a5);
fprintf(stderr, " errno = %d (%s)\n", a5, v7);
}
func1();
}

original_code:
static void
VAS_Fail_default(const char *func, const char *file, int line,
    const char *cond, int err, int xxx)
{
 if (xxx) {
  fprintf(stderr,
      "Missing errorhandling code in %s(), %s line %d:\n"
      "  Condition(%s) not true.\n",
      func, file, line, cond);
 } else {
  fprintf(stderr,
      "Assert error in %s(), %s line %d:\n"
      "  Condition(%s) not true.\n",
      func, file, line, cond);
 }
 if (err)
  fprintf(stderr,
      "  errno = %d (%s)\n", err, strerror(err));
 abort();
}

canonical_original_code:
static void VAS_Fail_default(char *func, char *file, int line, char *cond, int err, int xxx)
{
 if (xxx) {
  fprintf(stderr,
      "Missing errorhandling code in %s(), %s line %d:\n"
      "  Condition(%s) not true.\n",
      func, file, line, cond);
 } else {
  fprintf(stderr,
      "Assert error in %s(), %s line %d:\n"
      "  Condition(%s) not true.\n",
      func, file, line, cond);
 }
 if (err)
  fprintf(stderr,
      "  errno = %d (%s)\n", err, strerror(err));
 abort();
}

variable_types:
func
char *

variable_types:
file
char *

variable_types: line int

variable_types:
cond
char *

variable_types: err int

variable_types: xxx int


repo: wernsey/miscsrc

decompiled_code:
__int64 __fastcall get_char(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rax

result = *(unsigned int *)(a1 + 84);
if ( (_DWORD)result != -1 )
{
if ( (_DWORD)result )
{
*(_DWORD *)(a1 + 84) = 0;
result = (unsigned int)(char)result;
}
else
{
v2 = *(int *)(a1 + 80);
if ( (int)v2 > 63
|| (*(_DWORD *)(a1 + 80) = v2 + 1, result = (unsigned int)*(char *)(a1 + v2 + 16), !(_BYTE)result) )
{
if ( (*(unsigned int (__fastcall **)(__int64, __int64, _QWORD))a1)(a1 + 16, 64LL, *(_QWORD *)(a1 + 8)) )
{
result = (unsigned int)*(char *)(a1 + 16);
*(_DWORD *)(a1 + 80) = 1;
}
else
{
*(_DWORD *)(a1 + 84) = -1;
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rax

result = *(unsigned int *)(a1 + 84);
if ( (_DWORD)result != -1 )
{
if ( (_DWORD)result )
{
*(_DWORD *)(a1 + 84) = 0;
result = (unsigned int)(char)result;
}
else
{
v2 = *(int *)(a1 + 80);
if ( (int)v2 > 63
|| (*(_DWORD *)(a1 + 80) = v2 + 1, result = (unsigned int)*(char *)(a1 + v2 + 16), !(_BYTE)result) )
{
if ( (*(unsigned int (__fastcall **)(__int64, __int64, _QWORD))a1)(a1 + 16, 64LL, *(_QWORD *)(a1 + 8)) )
{
result = (unsigned int)*(char *)(a1 + 16);
*(_DWORD *)(a1 + 80) = 1;
}
else
{
*(_DWORD *)(a1 + 84) = -1;
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
static int get_char(CsvContext *csv) {
    char c = 0;
    if(csv->last_char == (-1)) {
        return (-1);
    } else if(csv->last_char) {
        c = csv->last_char;
        csv->last_char = 0;
        return c;
    }
    if(csv->in_pos >= 64 || (c = csv->raw_buffer[csv->in_pos++]) == '\0') {
        int cnt = csv->get_data(csv->raw_buffer, 64, csv->data);
        if(!cnt) {
            csv->last_char = (-1);
            return (-1);
        }
        csv->in_pos = 0;
        c = csv->raw_buffer[csv->in_pos++];
    }
    return c;
}

canonical_original_code:
static int get_char(struct CsvContext *csv) {
    char c = 0;
    if(csv->last_char == (-1)) {
        return (-1);
    } else if(csv->last_char) {
        c = csv->last_char;
        csv->last_char = 0;
        return c;
    }
    if(csv->in_pos >= 64 || (c = csv->raw_buffer[csv->in_pos++]) == '\0') {
        int cnt = csv->get_data(csv->raw_buffer, 64, csv->data);
        if(!cnt) {
            csv->last_char = (-1);
            return (-1);
        }
        csv->in_pos = 0;
        c = csv->raw_buffer[csv->in_pos++];
    }
    return c;
}

variable_types: struct CsvContext { int (*get_data)(char *b, int n, void *d); void *data; char raw_buffer[64]; int in_pos; int last_char; char buffer[256]; char *fields[32]; int nf; enum csv_error_code { CSV_OK=0, CSV_ERR_BUFFER=1, CSV_ERR_FIELDS=2, CSV_ERR_BAD_QUOTE=3, CSV_ERR_LINE_END=4 } err; } *

variable_types: c char

variable_types: cnt int

user_defined_types: struct CsvContext { int (*get_data)(char *b, int n, void *d); void *data; char raw_buffer[64]; int in_pos; int last_char; char buffer[256]; char *fields[32]; int nf; enum csv_error_code err; }


decompiled_code:
void __fastcall unget_char(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 84) = a2;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 84) = a2;
}

original_code:
static void unget_char(CsvContext *csv, int c) {
    csv->last_char = c;
}

canonical_original_code:
static void unget_char(struct CsvContext *csv, int c) {
    csv->last_char = c;
}

variable_types: c int

user_defined_types: enum csv_error_code { CSV_OK=0, CSV_ERR_BUFFER=1, CSV_ERR_FIELDS=2, CSV_ERR_BAD_QUOTE=3, CSV_ERR_LINE_END=4 }


decompiled_code:
__int64 __fastcall csv_read_record(__int64 a1)
{
unsigned __int64 v1; // r13
unsigned int v2; // er15
__int64 v3; // r14
unsigned __int64 v4; // r12
__int64 result; // rax
int v6; // eax
int v7; // ebp
int v8; // esi
int v9; // eax
unsigned __int64 v10; // rax
int v11; // ebp

if ( *(_DWORD *)(a1 + 84) == -1 )
return 0LL;
v2 = 0;
v3 = 0x200000000801LL;
v4 = 0LL;
*(_QWORD *)(a1 + 600) = 0LL;
while ( 2 )
{
switch ( v2 )
{
case 1u:
result = *(unsigned int *)(a1 + 600);
if ( (_DWORD)result != 32 )
{
do
v11 = get_char(a1);
while ( strchr(s, v11) );
v2 = 3;
*(_QWORD *)(a1 + 8LL * *(int *)(a1 + 600) + 344) = a1 + v4 + 88;
if ( v11 != 34 )
{
v1 = v4;
v2 = 2;
unget_char(a1, v11);
}
continue;
}
*(_DWORD *)(a1 + 604) = 2;
return result;
case 2u:
v9 = get_char(a1);
v7 = v9;
if ( v9 != 13 )
{
v10 = (unsigned int)(v9 + 1);
if ( (unsigned int)v10 > 0x2D || !_bittest64(&v3, v10) )
goto LABEL_9;
goto LABEL_19;
}
v7 = get_char(a1);
if ( v7 == 10 )
{
LABEL_19:
while ( v4 > v1 && strchr(s, *(char *)(a1 + v4 + 87)) )
--v4;
v2 = (v7 != 44) + 4;
continue;
}
*(_DWORD *)(a1 + 604) = 4;
result = *(unsigned int *)(a1 + 600);
break;
case 3u:
v6 = get_char(a1);
LOBYTE(v7) = v6;
if ( v6 == -1 )
goto LABEL_39;
if ( v6 != 34 )
{
LABEL_9:
if ( v4 == 255 )
goto LABEL_30;
goto LABEL_10;
}
v7 = get_char(a1);
if ( v7 == 34 )
{
if ( v4 == 255 )
{
LABEL_30:
*(_DWORD *)(a1 + 604) = 1;
return *(unsigned int *)(a1 + 600);
}
LABEL_10:
*(_BYTE *)(a1 + v4++ + 88) = v7;
if ( v2 > 5 )
{
LABEL_11:
v8 = get_char(a1);
if ( v8 == -1 )
return 0LL;
v2 = 1;
unget_char(a1, v8);
}
continue;
}
while ( strchr(s, v7) )
v7 = get_char(a1);
if ( v7 == -1 || v7 == 10 )
{
v2 = 5;
}
else
{
if ( v7 != 44 )
{
LABEL_39:
*(_DWORD *)(a1 + 604) = 3;
return *(unsigned int *)(a1 + 600);
}
v2 = 4;
}
continue;
case 4u:
case 5u:
result = *(unsigned int *)(a1 + 600);
if ( v4 == 255 )
{
*(_DWORD *)(a1 + 604) = 1;
return result;
}
result = (unsigned int)(result + 1);
*(_BYTE *)(a1 + v4 + 88) = 0;
*(_DWORD *)(a1 + 600) = result;
if ( v2 == 5 )
return result;
++v4;
v2 = 1;
continue;
default:
goto LABEL_11;
}
return result;
}
}

original_code:
int csv_read_record(CsvContext *csv) {
    int c = 0;
    size_t start, bump = 0;
    enum parse_state {
        RECORD_START, FIELD_START, FIELD, QUOTE, FIELD_END, RECORD_END
    } state = RECORD_START;
    if(csv->last_char == (-1)) return 0;
    csv->nf = 0;
    csv->err = CSV_OK;
    for(;;) {
        switch(state) {
            case RECORD_START:
                c = get_char(csv);
                if(c == (-1))
                    return 0;
                state = FIELD_START;
                unget_char(csv, c);
            break;
            case FIELD_START:
                if(csv->nf == 32) {
                    csv->err = CSV_ERR_FIELDS;
                    return csv->nf;
                }
                c = get_char(csv);
                while(strchr(" \t\v\f", c))
                    c = get_char(csv);
                csv->fields[csv->nf] = &csv->buffer[bump];
                if(c == '\"')
                    state = QUOTE;
                else {
                    unget_char(csv, c);
                    start = bump;
                    state = FIELD;
                }
            break;
            case FIELD:
                c = get_char(csv);
                if(c == '\r') {
                    c = get_char(csv);
                    if(c != '\n') {
                        csv->err = CSV_ERR_LINE_END;
                        return csv->nf;
                    }
                }
                if(c == (-1) || c == '\n' || c == ',') {
                    while(bump > start && strchr(" \t\v\f", csv->buffer[bump-1]))
                        bump--;
                    state = c == ',' ? FIELD_END : RECORD_END;
                } else {
                    if(bump == 256 - 1) {
                        csv->err = CSV_ERR_BUFFER;
                        return csv->nf;
                    }
                    csv->buffer[bump++] = c;
                }
            break;
            case QUOTE:
                c = get_char(csv);
                if(c == (-1)) {
                    csv->err = CSV_ERR_BAD_QUOTE;
                    return csv->nf;
                }
                if(c == '\"') {
                    c = get_char(csv);
                    if(c != '\"') {
                        while(strchr(" \t\v\f", c))
                            c = get_char(csv);
                        if(c == (-1) || c == '\n') {
                            state = RECORD_END;
                        } else if(c == ',') {
                            state = FIELD_END;
                        } else {
                            csv->err = CSV_ERR_BAD_QUOTE;
                            return csv->nf;
                        }
                        break;
                    }
                }
                if(bump == 256 - 1) {
                    csv->err = CSV_ERR_BUFFER;
                    return csv->nf;
                }
                csv->buffer[bump++] = c;
            break;
            case FIELD_END:
            case RECORD_END:
                if(bump == 256 - 1) {
                    csv->err = CSV_ERR_BUFFER;
                    return csv->nf;
                }
                csv->buffer[bump++] = '\0';
                csv->nf++;
                if(state == RECORD_END)
                    return csv->nf;
                else
                    state = FIELD_START;
            break;
        }
    }
}

canonical_original_code:
int csv_read_record(struct CsvContext *csv) {
    int c = 0;
    unsigned long start; unsigned long bump = 0;
    enum parse_state state = RECORD_START;
    if(csv->last_char == (-1)) return 0;
    csv->nf = 0;
    csv->err = CSV_OK;
    for(;;) {
        switch(state) {
            case RECORD_START:
                c = get_char(csv);
                if(c == (-1))
                    return 0;
                state = FIELD_START;
                unget_char(csv, c);
            break;
            case FIELD_START:
                if(csv->nf == 32) {
                    csv->err = CSV_ERR_FIELDS;
                    return csv->nf;
                }
                c = get_char(csv);
                while(strchr(" \t\v\f", c))
                    c = get_char(csv);
                csv->fields[csv->nf] = &csv->buffer[bump];
                if(c == '\"')
                    state = QUOTE;
                else {
                    unget_char(csv, c);
                    start = bump;
                    state = FIELD;
                }
            break;
            case FIELD:
                c = get_char(csv);
                if(c == '\r') {
                    c = get_char(csv);
                    if(c != '\n') {
                        csv->err = CSV_ERR_LINE_END;
                        return csv->nf;
                    }
                }
                if(c == (-1) || c == '\n' || c == ',') {
                    while(bump > start && strchr(" \t\v\f", csv->buffer[bump-1]))
                        bump--;
                    state = c == ',' ? FIELD_END : RECORD_END;
                } else {
                    if(bump == 256 - 1) {
                        csv->err = CSV_ERR_BUFFER;
                        return csv->nf;
                    }
                    csv->buffer[bump++] = c;
                }
            break;
            case QUOTE:
                c = get_char(csv);
                if(c == (-1)) {
                    csv->err = CSV_ERR_BAD_QUOTE;
                    return csv->nf;
                }
                if(c == '\"') {
                    c = get_char(csv);
                    if(c != '\"') {
                        while(strchr(" \t\v\f", c))
                            c = get_char(csv);
                        if(c == (-1) || c == '\n') {
                            state = RECORD_END;
                        } else if(c == ',') {
                            state = FIELD_END;
                        } else {
                            csv->err = CSV_ERR_BAD_QUOTE;
                            return csv->nf;
                        }
                        break;
                    }
                }
                if(bump == 256 - 1) {
                    csv->err = CSV_ERR_BUFFER;
                    return csv->nf;
                }
                csv->buffer[bump++] = c;
            break;
            case FIELD_END:
            case RECORD_END:
                if(bump == 256 - 1) {
                    csv->err = CSV_ERR_BUFFER;
                    return csv->nf;
                }
                csv->buffer[bump++] = '\0';
                csv->nf++;
                if(state == RECORD_END)
                    return csv->nf;
                else
                    state = FIELD_START;
            break;
        }
    }
}

variable_types:
start
unsigned long

variable_types:
bump
unsigned long

variable_types: enum parse_state { RECORD_START=0, FIELD_START=1, FIELD=2, QUOTE=3, FIELD_END=4, RECORD_END=5 }


decompiled_code:
void __fastcall csv_context_custom(_QWORD *a1, __int64 a2, __int64 a3)
{
*a1 = a2;
a1[1] = a3;
a1[10] = 64LL;
a1[75] = 0LL;
}

original_code:
void csv_context_custom(CsvContext *csv, csv_read_data_fun fun, void *data) {
    csv->get_data = fun;
    csv->data = data;
    csv->last_char = 0;
    csv->in_pos = 64;
    csv->nf = 0;
    csv->err = CSV_OK;
}

canonical_original_code:
void csv_context_custom(struct CsvContext *csv, int (*fun)(char *b, int n, void *d), void *data) {
    csv->get_data = fun;
    csv->data = data;
    csv->last_char = 0;
    csv->in_pos = 64;
    csv->nf = 0;
    csv->err = CSV_OK;
}

variable_types:
fun
int (*)(char *b, int n, void *d)

variable_types:
data
void *


decompiled_code:
__int64 __fastcall csv_count(__int64 a1)
{
return *(unsigned int *)(a1 + 600);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned int *)(a1 + 600);
}

original_code:
int csv_count(CsvContext *csv) {
    return csv->nf;
}

canonical_original_code:
int csv_count(struct CsvContext *csv) {
    return csv->nf;
}


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

result = &unk_4AE;
if ( a2 >= 0 && *(_DWORD *)(a1 + 600) > a2 )
result = *(void **)(a1 + 8LL * a2 + 344);
return result;
}

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

result = &unk_4AE;
if ( a2 >= 0 && *(_DWORD *)(a1 + 600) > a2 )
result = *(void **)(a1 + 8LL * a2 + 344);
return result;
}

original_code:
const char *csv_field(CsvContext *csv, int i) {
    if(i < 0 || i >= csv->nf) return "";
    return csv->fields[i];
}

canonical_original_code:
char *csv_field(struct CsvContext *csv, int i) {
    if(i < 0 || i >= csv->nf) return "";
    return csv->fields[i];
}

variable_types: i int


decompiled_code:
__int64 __fastcall csv_get_error(__int64 a1)
{
return *(unsigned int *)(a1 + 604);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return *(unsigned int *)(a1 + 604);
}

original_code:
enum csv_error_code csv_get_error(CsvContext *csv) {
    return csv->err;
}

canonical_original_code:
enum csv_error_code csv_get_error(struct CsvContext *csv) {
    return csv->err;
}

return_type: enum csv_error_code { CSV_OK=0, CSV_ERR_BUFFER=1, CSV_ERR_FIELDS=2, CSV_ERR_BAD_QUOTE=3, CSV_ERR_LINE_END=4 }


original_code:
static int csv_set_int(csv_file *csv, int row, int col, char *value)
{
 csv_row *rp;
 if(!csv || row < 0 || col < 0) return -4;
 if(row >= csv->nrows)
 {
  if(row >= csv->arows)
  {
   int i, ns = (((row+1)>(csv->arows + (csv->arows >> 1)))?(row+1):(csv->arows + (csv->arows >> 1)));
   void *op = csv->rows;
   if(ns <= 2) ns = 3;
   csv->rows = realloc(csv->rows, ns * sizeof *csv->rows);
   if(!csv->rows)
   {
    csv->rows = op;
    return -1;
   }
   for(i = csv->arows; i < ns; i++)
   {
    csv->rows[i].cols = calloc(csv->def_cols, sizeof *csv->rows[i].cols);
    if(!csv->rows[i].cols)
     return -1;
    csv->rows[i].acols = csv->def_cols;
    csv->rows[i].ncols = 0;
   }
   csv->arows = ns;
  }
  csv->nrows = row + 1;
  ((void) (0));
 }
 ((void) (0));
 rp = &csv->rows[row];
 if(col >= rp->ncols)
 {
  if(col >= rp->acols)
  {
   int i, ns = (((col+1)>(rp->acols + (rp->acols >> 1)))?(col+1):(rp->acols + (rp->acols >> 1)));
   void *op = rp->cols;
   if(ns <= 2) ns = 3;
   rp->cols = realloc(rp->cols, ns * sizeof *rp->cols);
   if(!rp->cols)
   {
    rp->cols = op;
    return -1;
   }
   for(i = rp->acols; i < ns; i++)
    rp->cols[i] = ((void *)0);
   rp->acols = ns;
  }
  rp->ncols = col + 1;
  ((void) (0));
 }
 ((void) (0));
 if(rp->cols[col])
  free(rp->cols[col]);
 rp->cols[col] = value;
 if(rp->cols[col])
  return 1;
 return -1;
}

canonical_original_code:
static int csv_set_int(struct csv_file *csv, int row, int col, char *value)
{
 struct csv_row *rp;
 if(!csv || row < 0 || col < 0) return -4;
 if(row >= csv->nrows)
 {
  if(row >= csv->arows)
  {
   int i; int ns = (((row+1)>(csv->arows + (csv->arows >> 1)))?(row+1):(csv->arows + (csv->arows >> 1)));
   void *op = csv->rows;
   if(ns <= 2) ns = 3;
   csv->rows = realloc(csv->rows, ns * sizeof *csv->rows);
   if(!csv->rows)
   {
    csv->rows = op;
    return -1;
   }
   for(i = csv->arows; i < ns; i++)
   {
    csv->rows[i].cols = calloc(csv->def_cols, sizeof *csv->rows[i].cols);
    if(!csv->rows[i].cols)
     return -1;
    csv->rows[i].acols = csv->def_cols;
    csv->rows[i].ncols = 0;
   }
   csv->arows = ns;
  }
  csv->nrows = row + 1;
  ((void) (0));
 }
 ((void) (0));
 rp = &csv->rows[row];
 if(col >= rp->ncols)
 {
  if(col >= rp->acols)
  {
   int i; int ns = (((col+1)>(rp->acols + (rp->acols >> 1)))?(col+1):(rp->acols + (rp->acols >> 1)));
   void *op = rp->cols;
   if(ns <= 2) ns = 3;
   rp->cols = realloc(rp->cols, ns * sizeof *rp->cols);
   if(!rp->cols)
   {
    rp->cols = op;
    return -1;
   }
   for(i = rp->acols; i < ns; i++)
    rp->cols[i] = ((void *)0);
   rp->acols = ns;
  }
  rp->ncols = col + 1;
  ((void) (0));
 }
 ((void) (0));
 if(rp->cols[col])
  free(rp->cols[col]);
 rp->cols[col] = value;
 if(rp->cols[col])
  return 1;
 return -1;
}

variable_types: struct csv_file { int nrows; int arows; struct csv_row { int ncols; int acols; char **cols; } *rows; int def_cols; } *

variable_types: row int

variable_types: col int

variable_types:
value
char *

variable_types: ns int

variable_types:
op
void *

user_defined_types: struct csv_file { int nrows; int arows; struct csv_row *rows; int def_cols; }


decompiled_code:
const char *__fastcall csv_errstr(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 + 6);
result = "Unknown";
if ( (unsigned int)v1 <= 7 )
result = CSWTCH_10[v1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 + 6);
result = "Unknown";
if ( (unsigned int)v1 <= 7 )
result = CSWTCH_10[v1];
return result;
}

original_code:
const char *csv_errstr(int err)
{
 switch(err)
 {
  case 1: return "Success";
  case -1: return "Out of memory";
  case -2: return "Unable to open file for writing";
  case -3: return "Unable to read file";
  case -4: return "Invalid parameter";
  case -5 : return "Unterminated string";
  case -6 : return "Expected a field or record separator after the \"";
 }
 return "Unknown";
}

variable_types: err int


original_code:
void csv_free(csv_file *csv)
{
 int r,c;
 for(r = 0; r < csv->arows; r++)
 {
  for(c = 0; c < csv->rows[r].acols; c++)
   if(csv->rows[r].cols[c])
    free(csv->rows[r].cols[c]);
  free(csv->rows[r].cols);
 }
 free(csv->rows);
 free(csv);
}

canonical_original_code:
void csv_free(struct csv_file *csv)
{
 int r; int c;
 for(r = 0; r < csv->arows; r++)
 {
  for(c = 0; c < csv->rows[r].acols; c++)
   if(csv->rows[r].cols[c])
    free(csv->rows[r].cols[c]);
  free(csv->rows[r].cols);
 }
 free(csv->rows);
 free(csv);
}

variable_types: r int

user_defined_types: struct csv_row { int ncols; int acols; char **cols; }


decompiled_code:
__int64 __fastcall csv_rowcount(unsigned int *a1)
{
__int64 result; // rax

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

original_code:
int csv_rowcount(csv_file *csv)
{
 if(!csv) return 0;
 return csv->nrows;
}

canonical_original_code:
int csv_rowcount(struct csv_file *csv)
{
 if(!csv) return 0;
 return csv->nrows;
}


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

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)a1 > a2 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 16LL * a2);
}
return result;
}

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

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)a1 > a2 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 16LL * a2);
}
return result;
}

original_code:
int csv_colcount(csv_file *csv, int row)
{
 if(!csv || row >= csv->nrows) return 0;
 return csv->rows[row].ncols;
}

canonical_original_code:
int csv_colcount(struct csv_file *csv, int row)
{
 if(!csv || row >= csv->nrows) return 0;
 return csv->rows[row].ncols;
}


decompiled_code:
void *__fastcall csv_get(__int64 a1, int a2, int a3)
{
void *v3; // r8
__int64 v4; // rax

v3 = &unk_AA5;
if ( !a1 )
return v3;
if ( *(_DWORD *)a1 <= a2 )
return v3;
v4 = *(_QWORD *)(a1 + 8) + 16LL * a2;
if ( *(_DWORD *)v4 <= a3 )
return v3;
if ( a2 >= 0 && a3 >= 0 )
{
v3 = *(void **)(*(_QWORD *)(v4 + 8) + 8LL * a3);
if ( !v3 )
v3 = &unk_AA5;
return v3;
}
return &unk_AA5;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, int a2, int a3)
{
void *v3; // r8
__int64 v4; // rax

v3 = &unk_AA5;
if ( !a1 )
return v3;
if ( *(_DWORD *)a1 <= a2 )
return v3;
v4 = *(_QWORD *)(a1 + 8) + 16LL * a2;
if ( *(_DWORD *)v4 <= a3 )
return v3;
if ( a2 >= 0 && a3 >= 0 )
{
v3 = *(void **)(*(_QWORD *)(v4 + 8) + 8LL * a3);
if ( !v3 )
v3 = &unk_AA5;
return v3;
}
return &unk_AA5;
}

original_code:
const char *csv_get(csv_file *csv, int row, int col)
{
 if(!csv || row >= csv->nrows || col >= csv->rows[row].ncols || row < 0 || col < 0 || !csv->rows[row].cols[col])
  return "";
 return csv->rows[row].cols[col];
}

canonical_original_code:
char *csv_get(struct csv_file *csv, int row, int col)
{
 if(!csv || row >= csv->nrows || col >= csv->rows[row].ncols || row < 0 || col < 0 || !csv->rows[row].cols[col])
  return "";
 return csv->rows[row].cols[col];
}


decompiled_code:
__int64 __fastcall csv_set(__int64 a1, int a2, int a3, __int64 a4)
{
__int64 v5; // rax

v5 = my_strdup(a4);
return csv_set_int(a1, a2, a3, v5);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, int a3, __int64 a4)
{
__int64 v5; // rax

v5 = my_strdup(a4);
return func0(a1, a2, a3, v5);
}

original_code:
int csv_set(csv_file *csv, int row, int col, const char *value)
{
 return csv_set_int(csv, row, col, my_strdup(value));
}

canonical_original_code:
int csv_set(struct csv_file *csv, int row, int col, char *value)
{
 return csv_set_int(csv, row, col, my_strdup(value));
}


decompiled_code:
__int64 csv_setx(__int64 a1, int a2, int a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

va_start(arg, a4);
vsnprintf(s, 0x200uLL, a4, arg);
return csv_set(a1, a2, a3, (__int64)s);
}

canonical_decompiled_code:
__int64 func10(__int64 a1, int a2, int a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

va_start(arg, a4);
vsnprintf(s, 0x200uLL, a4, arg);
return func9(a1, a2, a3, (__int64)s);
}

original_code:
int csv_setx(csv_file *csv, int row, int col, const char *fmt, ...)
{
 char buffer[512];
 va_list arg;
 __builtin_va_start(arg,fmt);
 vsnprintf (buffer, 512, fmt, arg);
 __builtin_va_end(arg);
 return csv_set(csv, row, col, buffer);
}

canonical_original_code:
int csv_setx(struct csv_file *csv, int row, int col, char *fmt, ...)
{
 char buffer[512];
 __builtin_va_list arg;
 __builtin_va_start(arg,fmt);
 vsnprintf (buffer, 512, fmt, arg);
 __builtin_va_end(arg);
 return csv_set(csv, row, col, buffer);
}

variable_types:
fmt
char *

variable_types:
buffer
char [512]


decompiled_code:
__int64 __fastcall my_stricmp(char *a1, char *a2)
{
char *v2; // r12
char *v3; // rbp
__int64 v4; // rbx
const __int32_t *v5; // rdx
__int32_t v7; // ecx
__int32_t v8; // eax

v2 = a2;
v3 = a1;
v4 = *a1;
v5 = *_ctype_tolower_loc();
if ( (_BYTE)v4 )
{
while ( 1 )
{
v8 = v5[v4];
v7 = v5[*v2];
if ( v7 != v8 )
break;
v4 = *++v3;
if ( !(_BYTE)v4 )
return (unsigned int)(*v5 - v5[v2[1]]);
++v2;
}
}
else
{
v7 = v5[*a2];
v8 = *v5;
}
return (unsigned int)(v8 - v7);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, char *a2)
{
char *v2; // r12
char *v3; // rbp
__int64 v4; // rbx
const __int32_t *v5; // rdx
__int32_t v7; // ecx
__int32_t v8; // eax

v2 = a2;
v3 = a1;
v4 = *a1;
v5 = *_ctype_tolower_loc();
if ( (_BYTE)v4 )
{
while ( 1 )
{
v8 = v5[v4];
v7 = v5[*v2];
if ( v7 != v8 )
break;
v4 = *++v3;
if ( !(_BYTE)v4 )
return (unsigned int)(*v5 - v5[v2[1]]);
++v2;
}
}
else
{
v7 = v5[*a2];
v8 = *v5;
}
return (unsigned int)(v8 - v7);
}

original_code:
int my_stricmp(const char *p, const char *q) {
    for(;*p && tolower(*p) == tolower(*q); p++, q++);
    return tolower(*p) - tolower(*q);
}

canonical_original_code:
int my_stricmp(char *p, char *q) {
    for(;*p && tolower(*p) == tolower(*q); p++, q++);
    return tolower(*p) - tolower(*q);
}

variable_types:
p
char *

variable_types:
q
char *


decompiled_code:
void *__fastcall my_strdup(void *src)
{
size_t v1; // r12
void *v2; // rax
void *v3; // r8

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, v1);
return v3;
}

canonical_decompiled_code:
void *__fastcall func1(void *src)
{
size_t v1; // r12
void *v2; // rax
void *v3; // r8

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
v3 = v2;
if ( v2 )
v3 = memcpy(v2, src, v1);
return v3;
}

original_code:
char *my_strdup(const char *s) {
    char *a;
    size_t len = strlen(s);
    a = malloc(len + 1);
    if(!a) return ((void *)0);
    memcpy(a, s, len + 1);
    return a;
}

canonical_original_code:
char *my_strdup(char *s) {
    char *a;
    unsigned long len = strlen(s);
    a = malloc(len + 1);
    if(!a) return ((void *)0);
    memcpy(a, s, len + 1);
    return a;
}

variable_types:
s
char *

variable_types:
a
char *

variable_types:
len
unsigned long


decompiled_code:
char *__fastcall my_strtok_r(char *s, char *a2, char **a3)
{
char *v3; // r13
char *v6; // rax
int v7; // esi
char *v8; // rbx
char *v10; // rax

v3 = s;
if ( !s )
{
v3 = *a3;
if ( **a3 )
goto LABEL_3;
LABEL_10:
*a3 = v3;
return 0LL;
}
if ( !*s )
goto LABEL_10;
LABEL_3:
v6 = strpbrk(v3, a2);
if ( v6 )
{
*v6 = 0;
*a3 = v6 + 1;
v7 = v6[1];
if ( v6[1] )
{
v8 = v6 + 2;
do
{
if ( !strchr(a2, v7) )
break;
*a3 = v8;
v7 = *v8++;
}
while ( (_BYTE)v7 );
}
return v3;
}
*a3 = v3;
if ( !*v3 )
return v3;
v10 = v3 + 1;
do
*a3 = v10++;
while ( *(v10 - 1) );
return v3;
}

canonical_decompiled_code:
char *__fastcall func4(char *s, char *a2, char **a3)
{
char *v3; // r13
char *v6; // rax
int v7; // esi
char *v8; // rbx
char *v10; // rax

v3 = s;
if ( !s )
{
v3 = *a3;
if ( **a3 )
goto LABEL_3;
LABEL_10:
*a3 = v3;
return 0LL;
}
if ( !*s )
goto LABEL_10;
LABEL_3:
v6 = strpbrk(v3, a2);
if ( v6 )
{
*v6 = 0;
*a3 = v6 + 1;
v7 = v6[1];
if ( v6[1] )
{
v8 = v6 + 2;
do
{
if ( !strchr(a2, v7) )
break;
*a3 = v8;
v7 = *v8++;
}
while ( (_BYTE)v7 );
}
return v3;
}
*a3 = v3;
if ( !*v3 )
return v3;
v10 = v3 + 1;
do
*a3 = v10++;
while ( *(v10 - 1) );
return v3;
}

original_code:
char *my_strtok_r(char *str, const char *delim, char **saveptr) {
    char *s;
    if(!str)
        str = *saveptr;
    if(!str[0]) {
        *saveptr = str;
        return ((void *)0);
    }
    s = strpbrk(str, delim);
    if(s) {
        s[0] = '\0';
        *saveptr = s + 1;
        while((*saveptr)[0] && strchr(delim, (*saveptr)[0]))
            (*saveptr)++;
    } else
        for(*saveptr = str; (*saveptr)[0]; (*saveptr)++);
    return str;
}

canonical_original_code:
char *my_strtok_r(char *str, char *delim, char **saveptr) {
    char *s;
    if(!str)
        str = *saveptr;
    if(!str[0]) {
        *saveptr = str;
        return ((void *)0);
    }
    s = strpbrk(str, delim);
    if(s) {
        s[0] = '\0';
        *saveptr = s + 1;
        while((*saveptr)[0] && strchr(delim, (*saveptr)[0]))
            (*saveptr)++;
    } else
        for(*saveptr = str; (*saveptr)[0]; (*saveptr)++);
    return str;
}

variable_types:
str
char *

variable_types:
delim
char *

variable_types:
saveptr
char **


decompiled_code:
_QWORD *list_create()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
}
return result;
}

original_code:
link_list *list_create()
{
 link_list *list;
 list = malloc(sizeof *list);
 if(!list)
  return ((void *)0);
 list->first = ((void *)0);
 list->last = ((void *)0);
 return list;
}

canonical_original_code:
struct link_list *list_create()
{
 struct link_list *list;
 list = malloc(sizeof *list);
 if(!list)
  return ((void *)0);
 list->first = ((void *)0);
 list->last = ((void *)0);
 return list;
}

variable_types: struct link_list { struct list_element { struct list_element *next; void *data; } *first; } *

user_defined_types: struct link_list { struct list_element *first; }


decompiled_code:
void __fastcall list_destroy(void *ptr, void (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx
_QWORD *v3; // rax

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
if ( a2 )
a2(v3[2]);
free(*(void **)ptr);
*(_QWORD *)ptr = v2;
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr, void (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx
_QWORD *v3; // rax

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
if ( a2 )
a2(v3[2]);
free(*(void **)ptr);
*(_QWORD *)ptr = v2;
}
while ( v2 );
}
free(ptr);
}

original_code:
void list_destroy(link_list *list, list_dtor dtor)
{
 while(list->first) {
  list_element *next = list->first->next;
  if(dtor) dtor(list->first->data);
  free(list->first);
  list->first = next;
 }
 free(list);
}

canonical_original_code:
void list_destroy(struct link_list *list, void (*dtor)(void *data))
{
 while(list->first) {
  struct list_element *next = list->first->next;
  if(dtor) dtor(list->first->data);
  free(list->first);
  list->first = next;
 }
 free(list);
}

variable_types:
dtor
void (*)(void *data)

variable_types: struct list_element { struct list_element *next; void *data; } *


decompiled_code:
__int64 __fastcall list_iterate(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx
__int64 result; // rax

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v2 = (_QWORD *)*a1;
if ( !*a1 )
return 1LL;
while ( 1 )
{
result = a2(v2[2]);
if ( !(_DWORD)result )
break;
v2 = (_QWORD *)*v2;
if ( !v2 )
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx
__int64 result; // rax

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v2 = (_QWORD *)*a1;
if ( !*a1 )
return 1LL;
while ( 1 )
{
result = a2(v2[2]);
if ( !(_DWORD)result )
break;
v2 = (_QWORD *)*v2;
if ( !v2 )
return 1LL;
}
return result;
}

original_code:
int list_iterate(link_list *list, list_iter iter)
{
 list_element *le;
 if(!list || !iter) return 1;
 for(le = list->first; le; le = le->next)
  if(!iter(le->data))
   return 0;
 return 1;
}

canonical_original_code:
int list_iterate(struct link_list *list, int (*iter)(void *data))
{
 struct list_element *le;
 if(!list || !iter) return 1;
 for(le = list->first; le; le = le->next)
  if(!iter(le->data))
   return 0;
 return 1;
}

variable_types:
iter
int (*)(void *data)

user_defined_types: struct list_element { struct list_element *next; void *data; }


decompiled_code:
__int64 __fastcall list_iterate_reverse(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 v2; // rbx
__int64 result; // rax

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v2 = *(_QWORD *)(a1 + 8);
if ( !v2 )
return 1LL;
while ( 1 )
{
result = a2(*(_QWORD *)(v2 + 16));
if ( !(_DWORD)result )
break;
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 v2; // rbx
__int64 result; // rax

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v2 = *(_QWORD *)(a1 + 8);
if ( !v2 )
return 1LL;
while ( 1 )
{
result = a2(*(_QWORD *)(v2 + 16));
if ( !(_DWORD)result )
break;
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return 1LL;
}
return result;
}

original_code:
int list_iterate_reverse(link_list *list, list_iter iter)
{
 list_element *le;
 if(!list || !iter) return 1;
 for(le = list->last; le; le = le->prev)
  if(!iter(le->data))
   return 0;
 return 1;
}

canonical_original_code:
int list_iterate_reverse(struct link_list *list, int (*iter)(void *data))
{
 struct list_element *le;
 if(!list || !iter) return 1;
 for(le = list->last; le; le = le->prev)
  if(!iter(le->data))
   return 0;
 return 1;
}


decompiled_code:
__int64 __fastcall list_remove_element(_QWORD *a1, __int64 *a2)
{
_QWORD *v2; // rdx
__int64 v3; // r12
__int64 v4; // rcx
__int64 result; // rax

v2 = (_QWORD *)a2[1];
v3 = a2[2];
v4 = *a2;
if ( v2 )
{
if ( v4 )
{
*(_QWORD *)(v4 + 8) = v2;
*v2 = v4;
LABEL_4:
free(a2);
return v3;
}
a1[1] = v2;
*v2 = 0LL;
free(a2);
result = v3;
}
else
{
if ( !v4 )
{
*a1 = 0LL;
a1[1] = 0LL;
goto LABEL_4;
}
*a1 = v4;
*(_QWORD *)(v4 + 8) = 0LL;
free(a2);
result = v3;
}
return result;
}

original_code:
void *list_remove_element(link_list *list, list_element *el)
{
 void *d = el->data;
 ((void) (0));
 if(!el) return ((void *)0);
 if(!el->prev)
 {
  ((void) (0));
  if(!el->next)
  {
   ((void) (0));
   list->first = ((void *)0);
   list->last = ((void *)0);
  }
  else
  {
   list->first = el->next;
   el->next->prev = ((void *)0);
  }
 }
 else
 {
  if(!el->next)
  {
   ((void) (0));
   list->last = el->prev;
   el->prev->next = ((void *)0);
  }
  else
  {
   el->next->prev = el->prev;
   el->prev->next = el->next;
  }
 }
 free(el);
 return d;
}

canonical_original_code:
void *list_remove_element(struct link_list *list, struct list_element *el)
{
 void *d = el->data;
 ((void) (0));
 if(!el) return ((void *)0);
 if(!el->prev)
 {
  ((void) (0));
  if(!el->next)
  {
   ((void) (0));
   list->first = ((void *)0);
   list->last = ((void *)0);
  }
  else
  {
   list->first = el->next;
   el->next->prev = ((void *)0);
  }
 }
 else
 {
  if(!el->next)
  {
   ((void) (0));
   list->last = el->prev;
   el->prev->next = ((void *)0);
  }
  else
  {
   el->next->prev = el->prev;
   el->prev->next = el->next;
  }
 }
 free(el);
 return d;
}

variable_types:
el
struct list_element *

variable_types:
d
void *

return_type: void *


decompiled_code:
_BOOL8 __fastcall list_strcmp(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
int list_strcmp(void *p, void *q)
{
 char *s1 = p, *s2 = q;
 return !strcmp(s1, s2);
}

canonical_original_code:
int list_strcmp(void *p, void *q)
{
 char *s1 = p; char *s2 = q;
 return !strcmp(s1, s2);
}

variable_types:
p
void *

variable_types:
q
void *


original_code:
int list_stricmp(void *p, void *q)
{
 char *s1 = p, *s2 = q;
 for(;s1[0] && s2[0] && tolower(s1[0]) == tolower(s2[0]); s1++, s2++);
 return tolower(s1[0]) == tolower(s2[0]);
}

canonical_original_code:
int list_stricmp(void *p, void *q)
{
 char *s1 = p; char *s2 = q;
 for(;s1[0] && s2[0] && tolower(s1[0]) == tolower(s2[0]); s1++, s2++);
 return tolower(s1[0]) == tolower(s2[0]);
}


decompiled_code:
__int64 __fastcall list_pop_front(__int64 **a1)
{
__int64 result; // rax

if ( *a1 )
result = list_remove_element(a1, *a1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 **a1)
{
__int64 result; // rax

if ( *a1 )
result = func4(a1, *a1);
else
result = 0LL;
return result;
}

original_code:
void *list_pop_front(link_list *list)
{
 ((void) (0));
 if(!list->first)
 {
  ((void) (0));
  return ((void *)0);
 }
 return list_remove_element(list, list->first);
}

canonical_original_code:
void *list_pop_front(struct link_list *list)
{
 ((void) (0));
 if(!list->first)
 {
  ((void) (0));
  return ((void *)0);
 }
 return list_remove_element(list, list->first);
}


decompiled_code:
__int64 __fastcall list_pop_back(_QWORD *a1)
{
__int64 *v1; // rsi
__int64 result; // rax

v1 = (__int64 *)a1[1];
if ( v1 )
result = list_remove_element(a1, v1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1)
{
__int64 *v1; // rsi
__int64 result; // rax

v1 = (__int64 *)a1[1];
if ( v1 )
result = func4(a1, v1);
else
result = 0LL;
return result;
}

original_code:
void *list_pop_back(link_list *list)
{
 ((void) (0));
 if(!list->last)
 {
  ((void) (0));
  return ((void *)0);
 }
 return list_remove_element(list, list->last);
}

canonical_original_code:
void *list_pop_back(struct link_list *list)
{
 ((void) (0));
 if(!list->last)
 {
  ((void) (0));
  return ((void *)0);
 }
 return list_remove_element(list, list->last);
}


decompiled_code:
_BOOL8 __fastcall list_isempty(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
int list_isempty(link_list *list)
{
 ((void) (0));
 return !list->first;
}

canonical_original_code:
int list_isempty(struct link_list *list)
{
 ((void) (0));
 return !list->first;
}


decompiled_code:
__int64 __fastcall list_count(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

v1 = *a1;
for ( i = 0; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

v1 = *a1;
for ( i = 0; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

original_code:
int list_count(link_list *list)
{
 list_element *le;
 int count = 0;
 ((void) (0));
 for(le = list->first; le; le = le->next)
  count++;
 return count;
}

canonical_original_code:
int list_count(struct link_list *list)
{
 struct list_element *le;
 int count = 0;
 ((void) (0));
 for(le = list->first; le; le = le->next)
  count++;
 return count;
}

variable_types: count int


pop

original_code:
static double pop(struct eval *ev)
{
 ((void) (0));
 return ev->st[--ev->sp];
}

variable_types: struct eval { struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } j[1]; char *p; double *st; int st_size; int sp; struct { int type; double n_val; char s_val[11]; } token[2]; int cur_tok; } *

return_type: double

user_defined_types: struct eval { struct __jmp_buf_tag j[1]; char *p; double *st; int st_size; int sp; struct { int type; double n_val; char s_val[11]; } token[2]; int cur_tok; }

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }


lex

original_code:
static int lex(struct eval *ev)
{
 int next_tok;
start:
 next_tok = ev->cur_tok;
 ev->cur_tok = ev->cur_tok?0:1;
 while(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short int) _ISspace)) ev->p++;
 if(!ev->p[0])
 {
  ev->token[next_tok].type = 0;
  goto end;
 }
 else if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short int) _ISdigit) || ev->p[0] == '.')
 {
  char *endp;
  ev->token[next_tok].type = 3;
  ev->token[next_tok].n_val = strtod(ev->p, &endp);
  ev->p = endp;
  goto end;
 }
 else if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short int) _ISalpha))
 {
  int i;
  for(i = 0; ((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short int) _ISalnum) && i < 11 - 1; i++, ev->p++)
   ev->token[next_tok].s_val[i] = ev->p[0];
  if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short int) _ISalpha)) longjmp(ev->j, 3);
  ev->token[next_tok].s_val[i] = '\0';
  ev->token[next_tok].type = 2;
  goto end;
 }
 else if(strchr("+-*/%(),^", ev->p[0]))
 {
  ev->token[next_tok].type = ev->p[0];
  ev->p++;
  goto end;
 }
 else
  longjmp(ev->j, 2);
end:
 if(ev->token[ev->cur_tok].type == 1)
  goto start;
 return ev->token[ev->cur_tok].type;
}

canonical_original_code:
static int lex(struct eval *ev)
{
 int next_tok;
start:
 next_tok = ev->cur_tok;
 ev->cur_tok = ev->cur_tok?0:1;
 while(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short) _ISspace)) ev->p++;
 if(!ev->p[0])
 {
  ev->token[next_tok].type = 0;
  goto end;
 }
 else if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short) _ISdigit) || ev->p[0] == '.')
 {
  char *endp;
  ev->token[next_tok].type = 3;
  ev->token[next_tok].n_val = strtod(ev->p, &endp);
  ev->p = endp;
  goto end;
 }
 else if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short) _ISalpha))
 {
  int i;
  for(i = 0; ((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short) _ISalnum) && i < 11 - 1; i++, ev->p++)
   ev->token[next_tok].s_val[i] = ev->p[0];
  if(((*__ctype_b_loc ())[(int) ((ev->p[0]))] & (unsigned short) _ISalpha)) longjmp(ev->j, 3);
  ev->token[next_tok].s_val[i] = '\0';
  ev->token[next_tok].type = 2;
  goto end;
 }
 else if(strchr("+-*/%(),^", ev->p[0]))
 {
  ev->token[next_tok].type = ev->p[0];
  ev->p++;
  goto end;
 }
 else
  longjmp(ev->j, 2);
end:
 if(ev->token[ev->cur_tok].type == 1)
  goto start;
 return ev->token[ev->cur_tok].type;
}

variable_types:
endp
char *

variable_types: i int

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


istrcmp

original_code:
static int istrcmp(const char *p, const char *q)
{
 for(; tolower(p[0]) == tolower(q[0]) && p[0]; p++, q++);
 return tolower(p[0]) - tolower(q[0]);
}

canonical_original_code:
static int istrcmp(char *p, char *q)
{
 for(; tolower(p[0]) == tolower(q[0]) && p[0]; p++, q++);
 return tolower(p[0]) - tolower(q[0]);
}

variable_types:
p
char *

variable_types:
q
char *


push

decompiled_code:
__int64 __fastcall push(struct __jmp_buf_tag env[1], double a2)
{
__int64 result; // rax
__int64 v3; // rbx
int v4; // er12
void *v5; // rax
__int64 v6; // rdx

result = SHIDWORD(env[1].__jmpbuf[2]);
v3 = env[1].__jmpbuf[1];
if ( (_DWORD)result == LODWORD(env[1].__jmpbuf[2]) )
{
v4 = result + ((int)result >> 1);
v5 = realloc((void *)env[1].__jmpbuf[1], v4);
env[1].__jmpbuf[1] = (__int64)v5;
v6 = (__int64)v5;
if ( !v5 )
{
env[1].__jmpbuf[1] = v3;
longjmp(env, 1);
}
LODWORD(env[1].__jmpbuf[2]) = v4;
result = SHIDWORD(env[1].__jmpbuf[2]);
v3 = v6;
}
HIDWORD(env[1].__jmpbuf[2]) = result + 1;
*(double *)(v3 + 8 * result) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(struct __jmp_buf_tag env[1], double a2)
{
__int64 result; // rax
__int64 v3; // rbx
int v4; // er12
void *v5; // rax
__int64 v6; // rdx

result = SHIDWORD(env[1].__jmpbuf[2]);
v3 = env[1].__jmpbuf[1];
if ( (_DWORD)result == LODWORD(env[1].__jmpbuf[2]) )
{
v4 = result + ((int)result >> 1);
v5 = realloc((void *)env[1].__jmpbuf[1], v4);
env[1].__jmpbuf[1] = (__int64)v5;
v6 = (__int64)v5;
if ( !v5 )
{
env[1].__jmpbuf[1] = v3;
longjmp(env, 1);
}
LODWORD(env[1].__jmpbuf[2]) = v4;
result = SHIDWORD(env[1].__jmpbuf[2]);
v3 = v6;
}
HIDWORD(env[1].__jmpbuf[2]) = result + 1;
*(double *)(v3 + 8 * result) = a2;
return result;
}

original_code:
static void push(struct eval *ev, double d)
{
 if(ev->sp == ev->st_size)
 {
  double *old = ev->st;
  int new_size = ev->st_size + (ev->st_size >> 1);
  ev->st = realloc(ev->st, new_size);
  if(!ev->st)
  {
   ev->st = old;
   longjmp(ev->j, 1);
  }
  ev->st_size = new_size;
 }
 ev->st[ev->sp++] = d;
}

variable_types: d double

variable_types:
old
double *


decompiled_code:
__int64 __fastcall num_expr(struct __jmp_buf_tag env[1])
{
char *v1; // rax

v1 = (char *)env + 32 * SLODWORD(env[1].__saved_mask.__val[2]);
if ( *((_DWORD *)v1 + 56) != 3 )
longjmp(env, 4);
push(env, *((double *)v1 + 29));
return lex(env);
}

canonical_decompiled_code:
__int64 __fastcall func4(struct __jmp_buf_tag env[1])
{
char *v1; // rax

v1 = (char *)env + 32 * SLODWORD(env[1].__saved_mask.__val[2]);
if ( *((_DWORD *)v1 + 56) != 3 )
longjmp(env, 4);
func3(env, *((double *)v1 + 29));
return func1(env);
}

original_code:
static void num_expr(struct eval *ev)
{
 if((ev->token[ev->cur_tok].type) != 3)
  longjmp(ev->j, (4));
 push(ev, ev->token[ev->cur_tok].n_val);
 lex(ev);
}


decompiled_code:
__int64 __fastcall mul_expr(struct __jmp_buf_tag env[1])
{
__int64 result; // rax
int v2; // ebp
double v3; // xmm1_8
double v4; // xmm0_8
double v5; // xmm0_8
double v6; // xmm1_8
double v7; // xmm0_8

pow_expr(env);
while ( 1 )
{
while ( 1 )
{
result = 4LL * SLODWORD(env[1].__saved_mask.__val[2]);
v2 = env[1].__jmpbuf[result + 3];
if ( v2 != 42 )
break;
lex(env);
pow_expr(env);
v6 = pop((__int64)env);
v7 = pop((__int64)env);
push(env, v7 * v6);
}
if ( v2 != 47 && v2 != 37 )
break;
lex(env);
pow_expr(env);
v3 = pop((__int64)env);
v4 = pop((__int64)env);
if ( v2 == 47 )
{
push(env, v4 / v3);
}
else
{
v5 = fmod(v4, v3);
push(env, v5);
}
}
return result * 8;
}

canonical_decompiled_code:
__int64 __fastcall func5(struct __jmp_buf_tag env[1])
{
__int64 result; // rax
int v2; // ebp
double v3; // xmm1_8
double v4; // xmm0_8
double v5; // xmm0_8
double v6; // xmm1_8
double v7; // xmm0_8

func10(env);
while ( 1 )
{
while ( 1 )
{
result = 4LL * SLODWORD(env[1].__saved_mask.__val[2]);
v2 = env[1].__jmpbuf[result + 3];
if ( v2 != 42 )
break;
func1(env);
func10(env);
v6 = func0((__int64)env);
v7 = func0((__int64)env);
func3(env, v7 * v6);
}
if ( v2 != 47 && v2 != 37 )
break;
func1(env);
func10(env);
v3 = func0((__int64)env);
v4 = func0((__int64)env);
if ( v2 == 47 )
{
func3(env, v4 / v3);
}
else
{
v5 = fmod(v4, v3);
func3(env, v5);
}
}
return result * 8;
}

original_code:
static void mul_expr(struct eval *ev)
{
 int t;
 pow_expr(ev);
 while((t = (ev->token[ev->cur_tok].type)) == '*' || t == '/' || t == '%')
 {
   double a,b;
   lex(ev);
   pow_expr(ev);
   b = pop(ev);
   a = pop(ev);
   if(t == '*')
    push(ev, a * b);
   else if(t == '/')
    push(ev, a / b);
   else
    push(ev, fmod(a, b));
 }
}

canonical_original_code:
static void mul_expr(struct eval *ev)
{
 int t;
 pow_expr(ev);
 while((t = (ev->token[ev->cur_tok].type)) == '*' || t == '/' || t == '%')
 {
   double a; double b;
   lex(ev);
   pow_expr(ev);
   b = pop(ev);
   a = pop(ev);
   if(t == '*')
    push(ev, a * b);
   else if(t == '/')
    push(ev, a / b);
   else
    push(ev, fmod(a, b));
 }
}

variable_types: t int

variable_types: a double

variable_types: b double


decompiled_code:
__int64 __fastcall add_expr(struct __jmp_buf_tag env[1])
{
__int64 result; // rax
double v2; // xmm1_8
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // xmm0_8

mul_expr(env);
while ( 1 )
{
while ( 1 )
{
result = LODWORD(env[1].__jmpbuf[4 * SLODWORD(env[1].__saved_mask.__val[2]) + 3]);
if ( (_DWORD)result != 43 )
break;
lex(env);
mul_expr(env);
v4 = pop((__int64)env);
v5 = pop((__int64)env);
push(env, v5 + v4);
}
if ( (_DWORD)result != 45 )
break;
lex(env);
mul_expr(env);
v2 = pop((__int64)env);
v3 = pop((__int64)env);
push(env, v3 - v2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(struct __jmp_buf_tag env[1])
{
__int64 result; // rax
double v2; // xmm1_8
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // xmm0_8

func5(env);
while ( 1 )
{
while ( 1 )
{
result = LODWORD(env[1].__jmpbuf[4 * SLODWORD(env[1].__saved_mask.__val[2]) + 3]);
if ( (_DWORD)result != 43 )
break;
func1(env);
func5(env);
v4 = func0((__int64)env);
v5 = func0((__int64)env);
func3(env, v5 + v4);
}
if ( (_DWORD)result != 45 )
break;
func1(env);
func5(env);
v2 = func0((__int64)env);
v3 = func0((__int64)env);
func3(env, v3 - v2);
}
return result;
}

original_code:
static void add_expr(struct eval *ev)
{
 int t;
 mul_expr(ev);
 while((t =(ev->token[ev->cur_tok].type)) == '+' || t == '-')
 {
   double a,b;
   lex(ev);
   mul_expr(ev);
   b = pop(ev);
   a = pop(ev);
   if(t == '+')
    push(ev, a + b);
   else
    push(ev, a - b);
 }
}

canonical_original_code:
static void add_expr(struct eval *ev)
{
 int t;
 mul_expr(ev);
 while((t =(ev->token[ev->cur_tok].type)) == '+' || t == '-')
 {
   double a; double b;
   lex(ev);
   mul_expr(ev);
   b = pop(ev);
   a = pop(ev);
   if(t == '+')
    push(ev, a + b);
   else
    push(ev, a - b);
 }
}