main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rdi
int v4; // er12
const char *v5; // rbp
const char **v6; // rbx
__int64 v7; // rax
clock_t v8; // rax
double v10; // xmm1_8
double v11; // [rsp+8h] [rbp-20h]

v3 = (unsigned int)(argc - 1);
if ( !(_DWORD)v3 )
LABEL_24:
PrintUsage(v3, argv, envp);
v4 = v3;
v3 = (unsigned __int64)&DefaultOptions;
*(_QWORD *)&DefaultOptions = 0LL;
DWORD2(DefaultOptions) = 858927408;
WORD6(DefaultOptions) = 12592;
*((_QWORD *)&DefaultOptions + 2) = -1LL;
opt_SetCurrentOptions((const __m128i *)&DefaultOptions);
v5 = argv[1];
if ( *v5 == 45 )
{
v6 = argv + 2;
while ( 2 )
{
envp = (const char **)*((unsigned __int8 *)v5 + 1);
switch ( (char)envp )
{
case 'b':
case 'e':
case 'g':
case 'z':
v3 = (unsigned __int64)(v5 + 1);
opt_Parse((__int64)(v5 + 1));
goto LABEL_6;
case 'h':
goto LABEL_24;
case 'i':
v3 = (unsigned __int64)(v5 + 2);
fstk_AddIncludePath(v5 + 2);
goto LABEL_6;
case 'o':
v3 = (unsigned __int64)(v5 + 2);
out_SetFileName(v5 + 2);
LABEL_6:
v5 = *v6;
--v4;
++v6;
if ( *v5 != 45 || !v4 )
break;
continue;
default:
printf("*ERROR*\t :\n\tUnknown option '%c'\n", (unsigned int)(char)envp);
exit(5);
}
}
}
v7 = *((_QWORD *)&CurrentOptions + 2);
DefaultOptions = (__int128)_mm_load_si128((const __m128i *)&CurrentOptions);
*((_QWORD *)&DefaultOptions + 2) = v7;
setuplex();
printf("Assembling %s\n", v5);
v8 = clock();
nLineNo = 1LL;
nStartClock = v8;
nTotalLines = 0LL;
nIFDepth = 0LL;
nPC = 0LL;
nPass = 1LL;
nErrors = 0LL;
sym_PrepPass1();
if ( !fstk_Init(v5) )
{
printf("File '%s' not found\n", v5);
exit(5);
}
puts("Pass 1...");
yy_set_state(0LL);
opt_SetCurrentOptions((const __m128i *)&DefaultOptions);
if ( (unsigned int)yyparse() || nErrors )
{
printf("Assembly aborted in pass 1 (%ld errors)!\n", nErrors);
exit(5);
}
if ( nIFDepth )
{
printf("*ERROR*\t:\tUnterminated IF construct (%ld levels)!\n", nIFDepth);
exit(5);
}
nTotalLines = 0LL;
nLineNo = 1LL;
nPC = 0LL;
nPass = 2LL;
sym_PrepPass2();
out_PrepPass2();
fstk_Init(v5);
yy_set_state(0LL);
opt_SetCurrentOptions((const __m128i *)&DefaultOptions);
puts("Pass 2...");
if ( (unsigned int)yyparse() || nErrors )
{
printf("Assembly aborted in pass 2 (%ld errors)!\n", nErrors);
exit(5);
}
nEndClock = clock();
v11 = (double)((int)nEndClock - (int)nStartClock) / 1000000.0;
printf(
"Success! %ld lines in %d.%02d seconds ",
nTotalLines,
(unsigned int)(int)v11,
(unsigned int)((int)(100.0 * v11) % 100));
if ( v11 == 0.0 )
{
puts("(INFINITY lines/minute)");
}
else
{
if ( nTotalLines < 0 )
v10 = (double)(int)(nTotalLines & 1 | ((unsigned __int64)nTotalLines >> 1))
+ (double)(int)(nTotalLines & 1 | ((unsigned __int64)nTotalLines >> 1));
else
v10 = (double)(int)nTotalLines;
printf("(%d lines/minute)\n", (unsigned int)(int)(v10 * (60.0 / v11)));
}
out_WriteObject(v11);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 v3; // rdi
int v4; // er12
const char *v5; // rbp
const char **v6; // rbx
__int64 v7; // rax
clock_t v8; // rax
double v10; // xmm1_8
double v11; // [rsp+8h] [rbp-20h]

v3 = (unsigned int)(argc - 1);
if ( !(_DWORD)v3 )
LABEL_24:
func6(v3, argv, envp);
v4 = v3;
v3 = (unsigned __int64)&DefaultOptions;
*(_QWORD *)&DefaultOptions = 0LL;
DWORD2(DefaultOptions) = 858927408;
WORD6(DefaultOptions) = 12592;
*((_QWORD *)&DefaultOptions + 2) = -1LL;
func0((const __m128i *)&DefaultOptions);
v5 = argv[1];
if ( *v5 == 45 )
{
v6 = argv + 2;
while ( 2 )
{
envp = (const char **)*((unsigned __int8 *)v5 + 1);
switch ( (char)envp )
{
case 'b':
case 'e':
case 'g':
case 'z':
v3 = (unsigned __int64)(v5 + 1);
func3((__int64)(v5 + 1));
goto LABEL_6;
case 'h':
goto LABEL_24;
case 'i':
v3 = (unsigned __int64)(v5 + 2);
fstk_AddIncludePath(v5 + 2);
goto LABEL_6;
case 'o':
v3 = (unsigned __int64)(v5 + 2);
out_SetFileName(v5 + 2);
LABEL_6:
v5 = *v6;
--v4;
++v6;
if ( *v5 != 45 || !v4 )
break;
continue;
default:
printf("*ERROR*\t :\n\tUnknown option '%c'\n", (unsigned int)(char)envp);
exit(5);
}
}
}
v7 = *((_QWORD *)&CurrentOptions + 2);
DefaultOptions = (__int128)_mm_load_si128((const __m128i *)&CurrentOptions);
*((_QWORD *)&DefaultOptions + 2) = v7;
setuplex();
printf("Assembling %s\n", v5);
v8 = clock();
nLineNo = 1LL;
nStartClock = v8;
nTotalLines = 0LL;
nIFDepth = 0LL;
nPC = 0LL;
nPass = 1LL;
nErrors = 0LL;
sym_PrepPass1();
if ( !fstk_Init(v5) )
{
printf("File '%s' not found\n", v5);
exit(5);
}
puts("Pass 1...");
yy_set_state(0LL);
func0((const __m128i *)&DefaultOptions);
if ( (unsigned int)yyparse() || nErrors )
{
printf("Assembly aborted in pass 1 (%ld errors)!\n", nErrors);
exit(5);
}
if ( nIFDepth )
{
printf("*ERROR*\t:\tUnterminated IF construct (%ld levels)!\n", nIFDepth);
exit(5);
}
nTotalLines = 0LL;
nLineNo = 1LL;
nPC = 0LL;
nPass = 2LL;
sym_PrepPass2();
out_PrepPass2();
fstk_Init(v5);
yy_set_state(0LL);
func0((const __m128i *)&DefaultOptions);
puts("Pass 2...");
if ( (unsigned int)yyparse() || nErrors )
{
printf("Assembly aborted in pass 2 (%ld errors)!\n", nErrors);
exit(5);
}
nEndClock = clock();
v11 = (double)((int)nEndClock - (int)nStartClock) / 1000000.0;
printf(
"Success! %ld lines in %d.%02d seconds ",
nTotalLines,
(unsigned int)(int)v11,
(unsigned int)((int)(100.0 * v11) % 100));
if ( v11 == 0.0 )
{
puts("(INFINITY lines/minute)");
}
else
{
if ( nTotalLines < 0 )
v10 = (double)(int)(nTotalLines & 1 | ((unsigned __int64)nTotalLines >> 1))
+ (double)(int)(nTotalLines & 1 | ((unsigned __int64)nTotalLines >> 1));
else
v10 = (double)(int)nTotalLines;
printf("(%d lines/minute)\n", (unsigned int)(int)(v10 * (60.0 / v11)));
}
out_WriteObject(v11);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 char *tzMainfile;
 int argn = 1;
 argc -= 1;
 if (argc == 0)
  PrintUsage();
 DefaultOptions.endian = 0;
 DefaultOptions.gbgfx[0] = '0';
 DefaultOptions.gbgfx[1] = '1';
 DefaultOptions.gbgfx[2] = '2';
 DefaultOptions.gbgfx[3] = '3';
 DefaultOptions.binary[0] = '0';
 DefaultOptions.binary[1] = '1';
 DefaultOptions.fillchar = -1;
 opt_SetCurrentOptions(&DefaultOptions);
 while (argv[argn][0] == '-' && argc) {
  switch (argv[argn][1]) {
  case 'h':
   PrintUsage();
   break;
  case 'i':
   fstk_AddIncludePath(&(argv[argn][2]));
   break;
  case 'o':
   out_SetFileName(&(argv[argn][2]));
   break;
  case 'e':
  case 'g':
  case 'b':
  case 'z':
   opt_Parse(&argv[argn][1]);
   break;
  default:
   printf("*ERROR*\t :\n\tUnknown option '%c'\n",
          argv[argn][1]);
   exit(5);
   break;
  }
  argn += 1;
  argc -= 1;
 }
 DefaultOptions = CurrentOptions;
 tzMainfile = argv[argn];
 setuplex();
 printf("Assembling %s\n", tzMainfile);
 nStartClock = clock();
 nLineNo = 1;
 nTotalLines = 0;
 nIFDepth = 0;
 nPC = 0;
 nPass = 1;
 nErrors = 0;
 sym_PrepPass1();
 if (fstk_Init(tzMainfile)) {
  printf("Pass 1...\n");
  yy_set_state(LEX_STATE_NORMAL);
  opt_SetCurrentOptions(&DefaultOptions);
  if (yyparse() == 0 && nErrors == 0) {
   if (nIFDepth == 0) {
    nTotalLines = 0;
    nLineNo = 1;
    nIFDepth = 0;
    nPC = 0;
    nPass = 2;
    nErrors = 0;
    sym_PrepPass2();
    out_PrepPass2();
    fstk_Init(tzMainfile);
    yy_set_state(LEX_STATE_NORMAL);
    opt_SetCurrentOptions(&DefaultOptions);
    printf("Pass 2...\n");
    if (yyparse() == 0 && nErrors == 0) {
     double timespent;
     nEndClock = clock();
     timespent =
         ((double)(nEndClock - nStartClock))
         / (double)((__clock_t) 1000000);
     printf
         ("Success! %ld lines in %d.%02d seconds ",
          nTotalLines, (int)timespent,
          ((int)(timespent * 100.0)) % 100);
     if (timespent == 0)
      printf
          ("(INFINITY lines/minute)\n");
     else
      printf("(%d lines/minute)\n",
             (int)(60 / timespent *
            nTotalLines));
     out_WriteObject();
    } else {
     printf
         ("Assembly aborted in pass 2 (%ld errors)!\n",
          nErrors);
     exit(5);
    }
   } else {
    printf
        ("*ERROR*\t:\tUnterminated IF construct (%ld levels)!\n",
         nIFDepth);
    exit(5);
   }
  } else {
   printf("Assembly aborted in pass 1 (%ld errors)!\n",
          nErrors);
   exit(5);
  }
 } else {
  printf("File '%s' not found\n", tzMainfile);
  exit(5);
 }
 return (0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char *tzMainfile;
 int argn = 1;
 argc -= 1;
 if (argc == 0)
  PrintUsage();
 DefaultOptions.endian = 0;
 DefaultOptions.gbgfx[0] = '0';
 DefaultOptions.gbgfx[1] = '1';
 DefaultOptions.gbgfx[2] = '2';
 DefaultOptions.gbgfx[3] = '3';
 DefaultOptions.binary[0] = '0';
 DefaultOptions.binary[1] = '1';
 DefaultOptions.fillchar = -1;
 opt_SetCurrentOptions(&DefaultOptions);
 while (argv[argn][0] == '-' && argc) {
  switch (argv[argn][1]) {
  case 'h':
   PrintUsage();
   break;
  case 'i':
   fstk_AddIncludePath(&(argv[argn][2]));
   break;
  case 'o':
   out_SetFileName(&(argv[argn][2]));
   break;
  case 'e':
  case 'g':
  case 'b':
  case 'z':
   opt_Parse(&argv[argn][1]);
   break;
  default:
   printf("*ERROR*\t :\n\tUnknown option '%c'\n",
          argv[argn][1]);
   exit(5);
   break;
  }
  argn += 1;
  argc -= 1;
 }
 DefaultOptions = CurrentOptions;
 tzMainfile = argv[argn];
 setuplex();
 printf("Assembling %s\n", tzMainfile);
 nStartClock = clock();
 nLineNo = 1;
 nTotalLines = 0;
 nIFDepth = 0;
 nPC = 0;
 nPass = 1;
 nErrors = 0;
 sym_PrepPass1();
 if (fstk_Init(tzMainfile)) {
  printf("Pass 1...\n");
  yy_set_state(LEX_STATE_NORMAL);
  opt_SetCurrentOptions(&DefaultOptions);
  if (yyparse() == 0 && nErrors == 0) {
   if (nIFDepth == 0) {
    nTotalLines = 0;
    nLineNo = 1;
    nIFDepth = 0;
    nPC = 0;
    nPass = 2;
    nErrors = 0;
    sym_PrepPass2();
    out_PrepPass2();
    fstk_Init(tzMainfile);
    yy_set_state(LEX_STATE_NORMAL);
    opt_SetCurrentOptions(&DefaultOptions);
    printf("Pass 2...\n");
    if (yyparse() == 0 && nErrors == 0) {
     double timespent;
     nEndClock = clock();
     timespent =
         ((double)(nEndClock - nStartClock))
         / (double)((long) 1000000);
     printf
         ("Success! %ld lines in %d.%02d seconds ",
          nTotalLines, (int)timespent,
          ((int)(timespent * 100.0)) % 100);
     if (timespent == 0)
      printf
          ("(INFINITY lines/minute)\n");
     else
      printf("(%d lines/minute)\n",
             (int)(60 / timespent *
            nTotalLines));
     out_WriteObject();
    } else {
     printf
         ("Assembly aborted in pass 2 (%ld errors)!\n",
          nErrors);
     exit(5);
    }
   } else {
    printf
        ("*ERROR*\t:\tUnterminated IF construct (%ld levels)!\n",
         nIFDepth);
    exit(5);
   }
  } else {
   printf("Assembly aborted in pass 1 (%ld errors)!\n",
          nErrors);
   exit(5);
  }
 } else {
  printf("File '%s' not found\n", tzMainfile);
  exit(5);
 }
 return (0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
tzMainfile
char *

variable_types: argn int

variable_types: timespent double

return_type: int


repo: vegard/rgbds-linux

decompiled_code:
__int64 __fastcall sym_GetValue(char *s1)
{
__int64 result; // rax
__int64 v2; // rbx

if ( *s1 == 64 && !s1[1] )
return nPC;
v2 = tHash[calchash(s1)];
if ( v2 )
{
while ( strcmp(s1, *(const char **)v2) )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
goto LABEL_9;
}
result = *(_QWORD *)(v2 + 8);
}
else
{
LABEL_9:
sprintf(&temptext, "Unknown symbol '%s'", s1);
fatalerror(&temptext);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s1)
{
__int64 result; // rax
__int64 v2; // rbx

if ( *s1 == 64 && !s1[1] )
return nPC;
v2 = tHash[func0(s1)];
if ( v2 )
{
while ( strcmp(s1, *(const char **)v2) )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
goto LABEL_9;
}
result = *(_QWORD *)(v2 + 8);
}
else
{
LABEL_9:
sprintf(&temptext, "Unknown symbol '%s'", s1);
fatalerror(&temptext);
result = 0LL;
}
return result;
}

original_code:
SLONG sym_GetValue(char *tzName)
{
 if (strcmp(tzName, "@") == 0) {
  return (nPC);
 } else {
  struct ISymbol **ppSym;
  ppSym = &(tHash[calchash(tzName)]);
  while (*ppSym) {
   if (strcmp(tzName, (*ppSym)->pzName)) {
    ppSym = &((*ppSym)->pNext);
   } else {
    return ((*ppSym)->nValue);
   }
  }
  sprintf(temptext, "Unknown symbol '%s'", tzName);
  fatalerror(temptext);
  return (0);
 }
}

canonical_original_code:
long sym_GetValue(char *tzName)
{
 if (strcmp(tzName, "@") == 0) {
  return (nPC);
 } else {
  struct ISymbol **ppSym;
  ppSym = &(tHash[calchash(tzName)]);
  while (*ppSym) {
   if (strcmp(tzName, (*ppSym)->pzName)) {
    ppSym = &((*ppSym)->pNext);
   } else {
    return ((*ppSym)->nValue);
   }
  }
  sprintf(temptext, "Unknown symbol '%s'", tzName);
  fatalerror(temptext);
  return (0);
 }
}

variable_types:
tzName
char *

variable_types: struct ISymbol { char *pzName; long nValue; long nBank; struct ISymbol *pNext; } **

return_type: long


decompiled_code:
__int64 __fastcall sym_GetBank(char *s1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = tHash[calchash(s1)];
if ( v1 )
{
while ( strcmp(s1, *(const char **)v1) )
{
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
goto LABEL_6;
}
result = *(_QWORD *)(v1 + 16);
}
else
{
LABEL_6:
sprintf(&temptext, "Unknown symbol '%s'", s1);
fatalerror(&temptext);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = tHash[func0(s1)];
if ( v1 )
{
while ( strcmp(s1, *(const char **)v1) )
{
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
goto LABEL_6;
}
result = *(_QWORD *)(v1 + 16);
}
else
{
LABEL_6:
sprintf(&temptext, "Unknown symbol '%s'", s1);
fatalerror(&temptext);
result = 0LL;
}
return result;
}

original_code:
SLONG sym_GetBank(char *tzName)
{
 struct ISymbol **ppSym;
 ppSym = &(tHash[calchash(tzName)]);
 while (*ppSym) {
  if (strcmp(tzName, (*ppSym)->pzName)) {
   ppSym = &((*ppSym)->pNext);
  } else {
   return ((*ppSym)->nBank);
  }
 }
 sprintf(temptext, "Unknown symbol '%s'", tzName);
 fatalerror(temptext);
 return (0);
}

canonical_original_code:
long sym_GetBank(char *tzName)
{
 struct ISymbol **ppSym;
 ppSym = &(tHash[calchash(tzName)]);
 while (*ppSym) {
  if (strcmp(tzName, (*ppSym)->pzName)) {
   ppSym = &((*ppSym)->pNext);
  } else {
   return ((*ppSym)->nBank);
  }
 }
 sprintf(temptext, "Unknown symbol '%s'", tzName);
 fatalerror(temptext);
 return (0);
}

user_defined_types: struct ISymbol { char *pzName; long nValue; long nBank; struct ISymbol *pNext; }


decompiled_code:
void __fastcall sym_CreateSymbol(void *src, __int64 a2, char a3)
{
char v3; // r14
__int64 v5; // rax
__int64 v6; // rbx
__int64 *v7; // r12
_QWORD *v8; // rax
_QWORD *v9; // rbx
size_t v10; // r14
void *v11; // rax
_QWORD *v12; // rax

v3 = a3;
if ( *(_BYTE *)src != 64 || *((_BYTE *)src + 1) )
{
v5 = calchash((char *)src);
v6 = tHash[v5];
v7 = &tHash[v5];
if ( v6 )
{
while ( 1 )
{
while ( strcmp((const char *)src, *(const char **)v6) )
{
v7 = (__int64 *)(v6 + 24);
v6 = *(_QWORD *)(v6 + 24);
if ( !v6 )
goto LABEL_10;
}
if ( v3 == -1 )
break;
sprintf(&temptext, "Symbol '%s' defined more than once\n", (const char *)src);
fatalerror(&temptext);
v6 = *v7;
if ( !*v7 )
goto LABEL_10;
}
}
else
{
LABEL_10:
v8 = malloc(0x20uLL);
*v7 = (__int64)v8;
v9 = v8;
if ( v8 )
{
v10 = strlen((const char *)src) + 1;
v11 = malloc(v10);
*v9 = v11;
if ( v11 )
{
memcpy(v11, src, v10);
v12 = (_QWORD *)*v7;
v12[1] = a2;
v12[2] = a3;
v12[3] = 0LL;
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func4(void *src, __int64 a2, char a3)
{
char v3; // r14
__int64 v5; // rax
__int64 v6; // rbx
__int64 *v7; // r12
_QWORD *v8; // rax
_QWORD *v9; // rbx
size_t v10; // r14
void *v11; // rax
_QWORD *v12; // rax

v3 = a3;
if ( *(_BYTE *)src != 64 || *((_BYTE *)src + 1) )
{
v5 = func0((char *)src);
v6 = tHash[v5];
v7 = &tHash[v5];
if ( v6 )
{
while ( 1 )
{
while ( strcmp((const char *)src, *(const char **)v6) )
{
v7 = (__int64 *)(v6 + 24);
v6 = *(_QWORD *)(v6 + 24);
if ( !v6 )
goto LABEL_10;
}
if ( v3 == -1 )
break;
sprintf(&temptext, "Symbol '%s' defined more than once\n", (const char *)src);
fatalerror(&temptext);
v6 = *v7;
if ( !*v7 )
goto LABEL_10;
}
}
else
{
LABEL_10:
v8 = malloc(0x20uLL);
*v7 = (__int64)v8;
v9 = v8;
if ( v8 )
{
v10 = strlen((const char *)src) + 1;
v11 = malloc(v10);
*v9 = v11;
if ( v11 )
{
memcpy(v11, src, v10);
v12 = (_QWORD *)*v7;
v12[1] = a2;
v12[2] = a3;
v12[3] = 0LL;
}
}
}
}
}

original_code:
void sym_CreateSymbol(char *tzName, SLONG nValue, SBYTE nBank)
{
 if (strcmp(tzName, "@") == 0)
  return;
 struct ISymbol **ppSym;
 ppSym = &(tHash[calchash(tzName)]);
 while (*ppSym) {
  if (strcmp(tzName, (*ppSym)->pzName)) {
   ppSym = &((*ppSym)->pNext);
  } else {
   if (nBank == -1)
    return;
   sprintf(temptext,
     "Symbol '%s' defined more than once\n",
     tzName);
   fatalerror(temptext);
  }
 }
 if ((*ppSym = malloc(sizeof **ppSym))) {
  if (((*ppSym)->pzName = malloc(strlen(tzName) + 1))) {
   strcpy((*ppSym)->pzName, tzName);
   (*ppSym)->nValue = nValue;
   (*ppSym)->nBank = nBank;
   (*ppSym)->pNext = ((void *)0);
  }
 }
}

canonical_original_code:
void sym_CreateSymbol(char *tzName, long nValue, char nBank)
{
 if (strcmp(tzName, "@") == 0)
  return;
 struct ISymbol **ppSym;
 ppSym = &(tHash[calchash(tzName)]);
 while (*ppSym) {
  if (strcmp(tzName, (*ppSym)->pzName)) {
   ppSym = &((*ppSym)->pNext);
  } else {
   if (nBank == -1)
    return;
   sprintf(temptext,
     "Symbol '%s' defined more than once\n",
     tzName);
   fatalerror(temptext);
  }
 }
 if ((*ppSym = malloc(sizeof **ppSym))) {
  if (((*ppSym)->pzName = malloc(strlen(tzName) + 1))) {
   strcpy((*ppSym)->pzName, tzName);
   (*ppSym)->nValue = nValue;
   (*ppSym)->nBank = nBank;
   (*ppSym)->pNext = ((void *)0);
  }
 }
}

variable_types: nValue long

variable_types: nBank char

return_type: void


calchash

decompiled_code:
__int64 __fastcall calchash(char *a1)
{
__int64 v1; // rax
__int64 v2; // rcx

v1 = *a1;
if ( !(_BYTE)v1 )
return 0LL;
v2 = 0LL;
do
{
++a1;
v2 += v1;
v1 = *a1;
}
while ( (_BYTE)v1 );
return v2 % 73;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
__int64 v1; // rax
__int64 v2; // rcx

v1 = *a1;
if ( !(_BYTE)v1 )
return 0LL;
v2 = 0LL;
do
{
++a1;
v2 += v1;
v1 = *a1;
}
while ( (_BYTE)v1 );
return v2 % 73;
}

original_code:
SLONG calchash(char *s)
{
 SLONG r = 0;
 while (*s)
  r += *s++;
 return (r % 73);
}

canonical_original_code:
long calchash(char *s)
{
 long r = 0;
 while (*s)
  r += *s++;
 return (r % 73);
}

variable_types:
s
char *

variable_types: r long


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

result = 0LL;
memset(&tHash, 0, 0x248uLL);
return result;
}

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

result = 0LL;
memset(&tHash, 0, 0x248uLL);
return result;
}

original_code:
void sym_Init(void)
{
 SLONG i;
 for (i = 0; i < 73; i += 1)
  tHash[i] = ((void *)0);
}

canonical_original_code:
void sym_Init()
{
 long i;
 for (i = 0; i < 73; i += 1)
  tHash[i] = ((void *)0);
}

variable_types: i long


FatalError

decompiled_code:
void __fastcall __noreturn FatalError(const char *a1)
{
printf("\n***ERROR: %s\n\n", a1);
PrintUsage();
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
printf("\n***ERROR: %s\n\n", a1);
func7();
}

original_code:
void FatalError(char *s)
{
 printf("\n***ERROR: %s\n\n", s);
 PrintUsage();
}


PrintUsage

decompiled_code:
void __noreturn PrintUsage()
{
puts("RGBFix v1.02 (part of ASMotor 1.10-linux-g74eea59)\n");
puts("Usage: rgbfix [options] image[.gb]");
puts("Options:");
puts("\t-h\t\tThis text");
puts("\t-d\t\tDebug: Don't change image");
puts("\t-p\t\tPad image to valid size\n\t\t\tPads to 32/64/128/256/512kB as appropriate");
puts("\t-r\t\ttRuncate image to valid size\n\t\t\tTruncates to 32/64/128/256/512kB as appropriate");
puts("\t-t<name>\tChange cartridge title field (16 characters)");
puts(
"\t-v\t\tValidate header\n"
"\t\t\tCorrects - Nintendo Character Area (0x0104)\n"
"\t\t\t\t - ROM type (0x0147)\n"
"\t\t\t\t - ROM size (0x0148)\n"
"\t\t\t\t - Checksums (0x014D-0x014F)");
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("RGBFix v1.02 (part of ASMotor 1.10-linux-g74eea59)\n");
puts("Usage: rgbfix [options] image[.gb]");
puts("Options:");
puts("\t-h\t\tThis text");
puts("\t-d\t\tDebug: Don't change image");
puts("\t-p\t\tPad image to valid size\n\t\t\tPads to 32/64/128/256/512kB as appropriate");
puts("\t-r\t\ttRuncate image to valid size\n\t\t\tTruncates to 32/64/128/256/512kB as appropriate");
puts("\t-t<name>\tChange cartridge title field (16 characters)");
puts(
"\t-v\t\tValidate header\n"
"\t\t\tCorrects - Nintendo Character Area (0x0104)\n"
"\t\t\t\t - ROM type (0x0147)\n"
"\t\t\t\t - ROM size (0x0148)\n"
"\t\t\t\t - Checksums (0x014D-0x014F)");
exit(0);
}

original_code:
void PrintUsage(void)
{
 printf("RGBFix v" "1.02"
  " (part of ASMotor " "1.10-linux" "-g74eea59" ")\n\n");
 printf("Usage: rgbfix [options] image[.gb]\n");
 printf("Options:\n");
 printf("\t-h\t\tThis text\n");
 printf("\t-d\t\tDebug: Don't change image\n");
 printf("\t-p\t\tPad image to valid size\n\t\t\tPads to 32/64/128/256/512kB as appropriate\n");
 printf("\t-r\t\ttRuncate image to valid size\n\t\t\tTruncates to 32/64/128/256/512kB as appropriate\n");
 printf("\t-t<name>\tChange cartridge title field (16 characters)\n");
 printf("\t-v\t\tValidate header\n\t\t\tCorrects - Nintendo Character Area (0x0104)\n\t\t\t\t - ROM type (0x0147)\n\t\t\t\t - ROM size (0x0148)\n\t\t\t\t - Checksums (0x014D-0x014F)\n");
 exit(0);
}

canonical_original_code:
void PrintUsage()
{
 printf("RGBFix v" "1.02"
  " (part of ASMotor " "1.10-linux" "-g74eea59" ")\n\n");
 printf("Usage: rgbfix [options] image[.gb]\n");
 printf("Options:\n");
 printf("\t-h\t\tThis text\n");
 printf("\t-d\t\tDebug: Don't change image\n");
 printf("\t-p\t\tPad image to valid size\n\t\t\tPads to 32/64/128/256/512kB as appropriate\n");
 printf("\t-r\t\ttRuncate image to valid size\n\t\t\tTruncates to 32/64/128/256/512kB as appropriate\n");
 printf("\t-t<name>\tChange cartridge title field (16 characters)\n");
 printf("\t-v\t\tValidate header\n\t\t\tCorrects - Nintendo Character Area (0x0104)\n\t\t\t\t - ROM type (0x0147)\n\t\t\t\t - ROM size (0x0148)\n\t\t\t\t - Checksums (0x014D-0x014F)\n");
 exit(0);
}


TruncateFileName

decompiled_code:
char *__fastcall TruncateFileName(char *dest, char *src)
{
char *v2; // rbp
size_t v3; // r8
size_t v4; // rax
char v6; // dl

v2 = src;
v3 = strlen(src);
v4 = v3 - 1;
if ( v3 )
{
while ( 1 )
{
v6 = src[v4];
if ( v6 == 47 || v6 == 92 )
break;
if ( v4-- == 0 )
return strcpy(dest, v2);
}
v2 = &src[v4 + 1];
}
return strcpy(dest, v2);
}

canonical_decompiled_code:
char *__fastcall func10(char *dest, char *src)
{
char *v2; // rbp
size_t v3; // r8
size_t v4; // rax
char v6; // dl

v2 = src;
v3 = strlen(src);
v4 = v3 - 1;
if ( v3 )
{
while ( 1 )
{
v6 = src[v4];
if ( v6 == 47 || v6 == 92 )
break;
if ( v4-- == 0 )
return strcpy(dest, v2);
}
v2 = &src[v4 + 1];
}
return strcpy(dest, v2);
}

original_code:
void TruncateFileName(char *dest, char *src)
{
 SLONG l;
 l = strlen(src) - 1;
 while ((l >= 0) && (src[l] != '\\') && (src[l] != '/'))
  l -= 1;
 strcpy(dest, &src[l + 1]);
}

canonical_original_code:
void TruncateFileName(char *dest, char *src)
{
 long l;
 l = strlen(src) - 1;
 while ((l >= 0) && (src[l] != '\\') && (src[l] != '/'))
  l -= 1;
 strcpy(dest, &src[l + 1]);
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types: l long


decompiled_code:
char *__fastcall lib_Find(char *s1, char *a2)
{
char *v2; // rbx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v2 = s1;
TruncateFileName(v4, a2);
if ( !s1 )
return 0LL;
while ( strcmp(v2, v4) )
{
v2 = (char *)*((_QWORD *)v2 + 35);
if ( !v2 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func11(char *s1, char *a2)
{
char *v2; // rbx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v2 = s1;
func10(v4, a2);
if ( !s1 )
return 0LL;
while ( strcmp(v2, v4) )
{
v2 = (char *)*((_QWORD *)v2 + 35);
if ( !v2 )
return 0LL;
}
return v2;
}

original_code:
sLibrary *lib_Find(sLibrary * lib, char *filename)
{
 char truncname[256];
 TruncateFileName(truncname, filename);
 while (lib) {
  if (strcmp(lib->tName, truncname) == 0)
   break;
  lib = lib->pNext;
 }
 return (lib);
}

canonical_original_code:
struct LibraryWrapper *lib_Find(struct LibraryWrapper *lib, char *filename)
{
 char truncname[256];
 TruncateFileName(truncname, filename);
 while (lib) {
  if (strcmp(lib->tName, truncname) == 0)
   break;
  lib = lib->pNext;
 }
 return (lib);
}

variable_types:
lib
struct LibraryWrapper *

variable_types:
filename
char *

variable_types:
truncname
char [256]

return_type: struct LibraryWrapper { char tName[256]; unsigned short uwTime; unsigned short uwDate; long nByteLength; unsigned char *pData; struct LibraryWrapper *pNext; } *


decompiled_code:
char *__fastcall lib_DeleteModule(char *a1, char *a2)
{
char *v2; // rbp
char **v3; // rbx
char *result; // rax
void *v5; // rdi
char *s1; // [rsp+8h] [rbp-120h] BYREF
char s2[280]; // [rsp+10h] [rbp-118h] BYREF

s1 = a1;
TruncateFileName(s2, a2);
if ( a1 )
{
v2 = s1;
v3 = &s1;
while ( strcmp(v2, s2) )
{
v2 = (char *)*((_QWORD *)*v3 + 35);
v3 = (char **)(*v3 + 280);
if ( !v2 )
goto LABEL_5;
}
v5 = (void *)*((_QWORD *)v2 + 34);
if ( v5 )
free(v5);
*v3 = (char *)*((_QWORD *)v2 + 35);
free(v2);
printf("Module '%s' deleted from library\n", s2);
result = s1;
}
else
{
LABEL_5:
fatalerror("Module not found");
result = a1;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func13(char *a1, char *a2)
{
char *v2; // rbp
char **v3; // rbx
char *result; // rax
void *v5; // rdi
char *s1; // [rsp+8h] [rbp-120h] BYREF
char s2[280]; // [rsp+10h] [rbp-118h] BYREF

s1 = a1;
func10(s2, a2);
if ( a1 )
{
v2 = s1;
v3 = &s1;
while ( strcmp(v2, s2) )
{
v2 = (char *)*((_QWORD *)*v3 + 35);
v3 = (char **)(*v3 + 280);
if ( !v2 )
goto LABEL_5;
}
v5 = (void *)*((_QWORD *)v2 + 34);
if ( v5 )
free(v5);
*v3 = (char *)*((_QWORD *)v2 + 35);
free(v2);
printf("Module '%s' deleted from library\n", s2);
result = s1;
}
else
{
LABEL_5:
fatalerror("Module not found");
result = a1;
}
return result;
}

original_code:
sLibrary *lib_DeleteModule(sLibrary * lib, char *filename)
{
 char truncname[256];
 sLibrary **pp, **first;
 BBOOL found = 0;
 pp = &lib;
 first = pp;
 TruncateFileName(truncname, filename);
 while ((*pp) && (!found)) {
  if (strcmp((*pp)->tName, truncname) == 0) {
   sLibrary *t;
   t = *pp;
   if (t->pData)
    free(t->pData);
   *pp = t->pNext;
   free(t);
   found = 1;
  }
  pp = &((*pp)->pNext);
 }
 if (!found)
  fatalerror("Module not found");
 else
  printf("Module '%s' deleted from library\n", truncname);
 return (*first);
}

canonical_original_code:
struct LibraryWrapper *lib_DeleteModule(struct LibraryWrapper *lib, char *filename)
{
 char truncname[256];
 struct LibraryWrapper **pp; struct LibraryWrapper **first;
 char found = 0;
 pp = &lib;
 first = pp;
 TruncateFileName(truncname, filename);
 while ((*pp) && (!found)) {
  if (strcmp((*pp)->tName, truncname) == 0) {
   struct LibraryWrapper *t;
   t = *pp;
   if (t->pData)
    free(t->pData);
   *pp = t->pNext;
   free(t);
   found = 1;
  }
  pp = &((*pp)->pNext);
 }
 if (!found)
  fatalerror("Module not found");
 else
  printf("Module '%s' deleted from library\n", truncname);
 return (*first);
}

variable_types: struct LibraryWrapper { char tName[256]; unsigned short uwTime; unsigned short uwDate; long nByteLength; unsigned char *pData; struct LibraryWrapper *pNext; } **

variable_types: found char


decompiled_code:
void __fastcall lib_Free(void *ptr)
{
_QWORD *i; // rbp
void *v2; // rdi
_QWORD *v3; // rbx

if ( ptr )
{
for ( i = ptr; ; i = v3 )
{
v2 = (void *)i[34];
if ( v2 )
free(v2);
v3 = (_QWORD *)i[35];
free(i);
if ( !v3 )
break;
}
}
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
_QWORD *i; // rbp
void *v2; // rdi
_QWORD *v3; // rbx

if ( ptr )
{
for ( i = ptr; ; i = v3 )
{
v2 = (void *)i[34];
if ( v2 )
free(v2);
v3 = (_QWORD *)i[35];
free(i);
if ( !v3 )
break;
}
}
}

original_code:
void lib_Free(sLibrary * lib)
{
 while (lib) {
  sLibrary *l;
  if (lib->pData)
   free(lib->pData);
  l = lib;
  lib = lib->pNext;
  free(l);
 }
}

canonical_original_code:
void lib_Free(struct LibraryWrapper *lib)
{
 while (lib) {
  struct LibraryWrapper *l;
  if (lib->pData)
   free(lib->pData);
  l = lib;
  lib = lib->pNext;
  free(l);
 }
}

variable_types: struct LibraryWrapper { char tName[256]; unsigned short uwTime; unsigned short uwDate; long nByteLength; unsigned char *pData; struct LibraryWrapper *pNext; } *


AllocSection

decompiled_code:
__int64 AllocSection()
{
__int64 *v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx
_QWORD *v3; // rax
__int64 v4; // rdx
__int64 result; // rax

v0 = (__int64 *)&pSections;
if ( oReadLib == 1 )
v0 = (__int64 *)&pLibSections;
v1 = *v0;
if ( *v0 )
{
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 72);
}
while ( v1 );
v0 = (__int64 *)(v2 + 72);
}
v3 = malloc(0x50uLL);
*v0 = (__int64)v3;
if ( v3 )
{
v4 = tSymbols;
v3[9] = 0LL;
v3[7] = v4;
result = *v0;
*(_QWORD *)(result + 64) = 0LL;
*(_BYTE *)(result + 16) = 0;
}
else
{
fatalerror("Out of memory!");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 *v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx
_QWORD *v3; // rax
__int64 v4; // rdx
__int64 result; // rax

v0 = (__int64 *)&pSections;
if ( oReadLib == 1 )
v0 = (__int64 *)&pLibSections;
v1 = *v0;
if ( *v0 )
{
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 72);
}
while ( v1 );
v0 = (__int64 *)(v2 + 72);
}
v3 = malloc(0x50uLL);
*v0 = (__int64)v3;
if ( v3 )
{
v4 = tSymbols;
v3[9] = 0LL;
v3[7] = v4;
result = *v0;
*(_QWORD *)(result + 64) = 0LL;
*(_BYTE *)(result + 16) = 0;
}
else
{
fatalerror("Out of memory!");
result = 0LL;
}
return result;
}

original_code:
struct sSection *AllocSection(void)
{
 struct sSection **ppSections;
 if (oReadLib == 1)
  ppSections = &pLibSections;
 else
  ppSections = &pSections;
 while (*ppSections)
  ppSections = &((*ppSections)->pNext);
 *ppSections = malloc(sizeof **ppSections);
 if (!*ppSections) {
  fatalerror("Out of memory!");
  return ((void *)0);
 }
 (*ppSections)->tSymbols = tSymbols;
 (*ppSections)->pNext = ((void *)0);
 (*ppSections)->pPatches = ((void *)0);
 (*ppSections)->oAssigned = 0;
 return *ppSections;
}

canonical_original_code:
struct sSection *AllocSection()
{
 struct sSection **ppSections;
 if (oReadLib == 1)
  ppSections = &pLibSections;
 else
  ppSections = &pSections;
 while (*ppSections)
  ppSections = &((*ppSections)->pNext);
 *ppSections = malloc(sizeof **ppSections);
 if (!*ppSections) {
  fatalerror("Out of memory!");
  return ((void *)0);
 }
 (*ppSections)->tSymbols = tSymbols;
 (*ppSections)->pNext = ((void *)0);
 (*ppSections)->pPatches = ((void *)0);
 (*ppSections)->oAssigned = 0;
 return *ppSections;
}

variable_types: struct sSection { long nBank; long nOrg; char oAssigned; long nByteSize; enum eSectionType { SECT_BSS=0, SECT_VRAM=1, SECT_CODE=2, SECT_HOME=3, SECT_HRAM=4 } Type; unsigned char *pData; long nNumberOfSymbols; struct sSymbol { char *pzName; enum eSymbolType { SYM_LOCAL=0, SYM_IMPORT=1, SYM_EXPORT=2 } Type; long nSectionID; struct sSection *pSection; long nOffset; } **tSymbols; struct sPatch { char *pzFilename; long nLineNo; long nOffset; enum ePatchType { PATCH_BYTE=0, PATCH_WORD_L=1, PATCH_LONG_L=2, PATCH_WORD_B=3, PATCH_LONG_B=4 } Type; long nRPNSize; unsigned char *pRPN; struct sPatch *pNext; char oRelocPatch; } *pPatches; struct sSection *pNext; } **

user_defined_types: struct sSection { long nBank; long nOrg; char oAssigned; long nByteSize; enum eSectionType Type; unsigned char *pData; long nNumberOfSymbols; struct sSymbol **tSymbols; struct sPatch *pPatches; struct sSection *pNext; }

user_defined_types: struct sPatch { char *pzFilename; long nLineNo; long nOffset; enum ePatchType Type; long nRPNSize; unsigned char *pRPN; struct sPatch *pNext; char oRelocPatch; }

user_defined_types: struct sSymbol { char *pzName; enum eSymbolType Type; long nSectionID; struct sSection *pSection; long nOffset; }


symboldefined

decompiled_code:
__int64 __fastcall symboldefined(char *s2)
{
_QWORD *v1; // r12
__int64 v2; // rbp
__int64 v3; // r14
__int64 v4; // rbx
__int64 v5; // rax
int v6; // edx

v1 = (_QWORD *)pSections;
if ( !pSections )
return 0LL;
while ( 1 )
{
v2 = v1[6];
if ( v2 )
break;
LABEL_10:
v1 = (_QWORD *)v1[9];
if ( !v1 )
return 0LL;
}
v3 = v1[7];
v4 = 0LL;
while ( 1 )
{
v5 = *(_QWORD *)(v3 + 8 * v4);
v6 = *(_DWORD *)(v5 + 8);
if ( (v6 == 2 || !v6 && *(_QWORD **)(v5 + 24) == v1) && !strcmp(*(const char **)v5, s2) )
return 1LL;
if ( ++v4 == v2 )
goto LABEL_10;
}
}

original_code:
static BBOOL symboldefined(char *name)
{
 struct sSection *pSect;
 pSect = pSections;
 while (pSect) {
  ULONG i;
  for (i = 0; i < pSect->nNumberOfSymbols; i += 1) {
   if ((pSect->tSymbols[i]->Type == SYM_EXPORT)
       || ((pSect->tSymbols[i]->Type == SYM_LOCAL)
    && (pSect == pSect->tSymbols[i]->pSection))) {
    if (strcmp(pSect->tSymbols[i]->pzName, name) ==
        0)
     return (1);
   }
  }
  pSect = pSect->pNext;
 }
 return (0);
}

canonical_original_code:
static char symboldefined(char *name)
{
 struct sSection *pSect;
 pSect = pSections;
 while (pSect) {
  unsigned long i;
  for (i = 0; i < pSect->nNumberOfSymbols; i += 1) {
   if ((pSect->tSymbols[i]->Type == SYM_EXPORT)
       || ((pSect->tSymbols[i]->Type == SYM_LOCAL)
    && (pSect == pSect->tSymbols[i]->pSection))) {
    if (strcmp(pSect->tSymbols[i]->pzName, name) ==
        0)
     return (1);
   }
  }
  pSect = pSect->pNext;
 }
 return (0);
}

variable_types:
name
char *

variable_types: struct sSection { long nBank; long nOrg; char oAssigned; long nByteSize; enum eSectionType { SECT_BSS=0, SECT_VRAM=1, SECT_CODE=2, SECT_HOME=3, SECT_HRAM=4 } Type; unsigned char *pData; long nNumberOfSymbols; struct sSymbol { char *pzName; enum eSymbolType { SYM_LOCAL=0, SYM_IMPORT=1, SYM_EXPORT=2 } Type; long nSectionID; struct sSection *pSection; long nOffset; } **tSymbols; struct sPatch { char *pzFilename; long nLineNo; long nOffset; enum ePatchType { PATCH_BYTE=0, PATCH_WORD_L=1, PATCH_LONG_L=2, PATCH_WORD_B=3, PATCH_LONG_B=4 } Type; long nRPNSize; unsigned char *pRPN; struct sPatch *pNext; char oRelocPatch; } *pPatches; struct sSection *pNext; } *

variable_types:
i
unsigned long

return_type: char


addmodulecontaining

decompiled_code:
__int64 __fastcall addmodulecontaining(char *s2)
{
void *v1; // r13
_QWORD *v2; // rbp
__int64 v3; // r14
__int64 v4; // r15
__int64 v5; // rbx
__int64 v6; // rax
int v7; // edx
__int64 v8; // rax
__int64 v9; // rdx
void *v10; // rdx

v1 = &pLibSections;
v2 = (_QWORD *)pLibSections;
if ( !pLibSections )
return 0LL;
while ( 1 )
{
v3 = v2[6];
if ( v3 )
break;
LABEL_13:
v1 = v2 + 9;
v2 = (_QWORD *)v2[9];
if ( !v2 )
return 0LL;
}
v4 = v2[7];
v5 = 0LL;
while ( 1 )
{
v6 = *(_QWORD *)(v4 + 8 * v5);
v7 = *(_DWORD *)(v6 + 8);
if ( (v7 == 2 || !v7 && *(_QWORD **)(v6 + 24) == v2) && !strcmp(*(const char **)v6, s2) )
break;
if ( ++v5 == v3 )
goto LABEL_13;
}
v8 = pSections;
if ( pSections )
{
do
{
v9 = v8;
v8 = *(_QWORD *)(v8 + 72);
}
while ( v8 );
v10 = (void *)(v9 + 72);
}
else
{
v10 = &pSections;
}
*(_QWORD *)v10 = v2;
*(_QWORD *)v1 = *(_QWORD *)(*(_QWORD *)v1 + 72LL);
*(_QWORD *)(*(_QWORD *)v10 + 72LL) = 0LL;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2)
{
void *v1; // r13
_QWORD *v2; // rbp
__int64 v3; // r14
__int64 v4; // r15
__int64 v5; // rbx
__int64 v6; // rax
int v7; // edx
__int64 v8; // rax
__int64 v9; // rdx
void *v10; // rdx

v1 = &pLibSections;
v2 = (_QWORD *)pLibSections;
if ( !pLibSections )
return 0LL;
while ( 1 )
{
v3 = v2[6];
if ( v3 )
break;
LABEL_13:
v1 = v2 + 9;
v2 = (_QWORD *)v2[9];
if ( !v2 )
return 0LL;
}
v4 = v2[7];
v5 = 0LL;
while ( 1 )
{
v6 = *(_QWORD *)(v4 + 8 * v5);
v7 = *(_DWORD *)(v6 + 8);
if ( (v7 == 2 || !v7 && *(_QWORD **)(v6 + 24) == v2) && !strcmp(*(const char **)v6, s2) )
break;
if ( ++v5 == v3 )
goto LABEL_13;
}
v8 = pSections;
if ( pSections )
{
do
{
v9 = v8;
v8 = *(_QWORD *)(v8 + 72);
}
while ( v8 );
v10 = (void *)(v9 + 72);
}
else
{
v10 = &pSections;
}
*(_QWORD *)v10 = v2;
*(_QWORD *)v1 = *(_QWORD *)(*(_QWORD *)v1 + 72LL);
*(_QWORD *)(*(_QWORD *)v10 + 72LL) = 0LL;
return 1LL;
}

original_code:
static BBOOL addmodulecontaining(char *name)
{
 struct sSection **ppLSect;
 ppLSect = &pLibSections;
 while (*ppLSect) {
  ULONG i;
  for (i = 0; i < (*ppLSect)->nNumberOfSymbols; i += 1) {
   if (((*ppLSect)->tSymbols[i]->Type == SYM_EXPORT)
       || (((*ppLSect)->tSymbols[i]->Type == SYM_LOCAL)
    && ((*ppLSect) ==
        (*ppLSect)->tSymbols[i]->pSection))) {
    if (strcmp
        ((*ppLSect)->tSymbols[i]->pzName,
         name) == 0) {
     struct sSection **ppSect;
     ppSect = &pSections;
     while (*ppSect)
      ppSect = &((*ppSect)->pNext);
     *ppSect = *ppLSect;
     *ppLSect = (*ppLSect)->pNext;
     (*ppSect)->pNext = ((void *)0);
     return (1);
    }
   }
  }
  ppLSect = &((*ppLSect)->pNext);
 }
 return (0);
}

canonical_original_code:
static char addmodulecontaining(char *name)
{
 struct sSection **ppLSect;
 ppLSect = &pLibSections;
 while (*ppLSect) {
  unsigned long i;
  for (i = 0; i < (*ppLSect)->nNumberOfSymbols; i += 1) {
   if (((*ppLSect)->tSymbols[i]->Type == SYM_EXPORT)
       || (((*ppLSect)->tSymbols[i]->Type == SYM_LOCAL)
    && ((*ppLSect) ==
        (*ppLSect)->tSymbols[i]->pSection))) {
    if (strcmp
        ((*ppLSect)->tSymbols[i]->pzName,
         name) == 0) {
     struct sSection **ppSect;
     ppSect = &pSections;
     while (*ppSect)
      ppSect = &((*ppSect)->pNext);
     *ppSect = *ppLSect;
     *ppLSect = (*ppLSect)->pNext;
     (*ppSect)->pNext = ((void *)0);
     return (1);
    }
   }
  }
  ppLSect = &((*ppLSect)->pNext);
 }
 return (0);
}

user_defined_types: enum eSectionType { SECT_BSS=0, SECT_VRAM=1, SECT_CODE=2, SECT_HOME=3, SECT_HRAM=4 }

user_defined_types: enum ePatchType { PATCH_BYTE=0, PATCH_WORD_L=1, PATCH_LONG_L=2, PATCH_WORD_B=3, PATCH_LONG_B=4 }

user_defined_types: enum eSymbolType { SYM_LOCAL=0, SYM_IMPORT=1, SYM_EXPORT=2 }


AddNeededModules

decompiled_code:
void AddNeededModules()
{
__int64 j; // rcx
__int64 v1; // rax
void *v2; // rdx
__int64 v3; // rdx
_QWORD *v4; // rbp
unsigned __int64 i; // rbx
__int64 v6; // rax
char *v7; // r12

if ( (options & 2) != 0 )
{
if ( (unsigned __int8)addmodulecontaining(&smartlinkstartsymbol) )
{
printf("Smart linking with symbol '%s'\n", &smartlinkstartsymbol);
}
else
{
sprintf(&temptext, "Can't find start symbol '%s'", &smartlinkstartsymbol);
fatalerror(&temptext);
}
v4 = (_QWORD *)pSections;
if ( pSections )
{
do
{
for ( i = 0LL; v4[6] > i; ++i )
{
while ( 1 )
{
v6 = *(_QWORD *)(v4[7] + 8 * i);
if ( *(_DWORD *)(v6 + 8) <= 1u )
{
v7 = *(char **)v6;
if ( !(unsigned __int8)symboldefined(*(char **)v6) )
break;
}
if ( v4[6] <= ++i )
goto LABEL_17;
}
addmodulecontaining(v7);
}
LABEL_17:
v4 = (_QWORD *)v4[9];
}
while ( v4 );
}
}
else
{
for ( j = pLibSections; j; *(_QWORD *)(*(_QWORD *)v2 + 72LL) = 0LL )
{
v1 = pSections;
v2 = &pSections;
if ( pSections )
{
do
{
v3 = v1;
v1 = *(_QWORD *)(v1 + 72);
}
while ( v1 );
v2 = (void *)(v3 + 72);
}
*(_QWORD *)v2 = j;
j = *(_QWORD *)(j + 72);
pLibSections = j;
}
}
}

canonical_decompiled_code:
void func2()
{
__int64 j; // rcx
__int64 v1; // rax
void *v2; // rdx
__int64 v3; // rdx
_QWORD *v4; // rbp
unsigned __int64 i; // rbx
__int64 v6; // rax
char *v7; // r12

if ( (options & 2) != 0 )
{
if ( (unsigned __int8)func1(&smartlinkstartsymbol) )
{
printf("Smart linking with symbol '%s'\n", &smartlinkstartsymbol);
}
else
{
sprintf(&temptext, "Can't find start symbol '%s'", &smartlinkstartsymbol);
fatalerror(&temptext);
}
v4 = (_QWORD *)pSections;
if ( pSections )
{
do
{
for ( i = 0LL; v4[6] > i; ++i )
{
while ( 1 )
{
v6 = *(_QWORD *)(v4[7] + 8 * i);
if ( *(_DWORD *)(v6 + 8) <= 1u )
{
v7 = *(char **)v6;
if ( !(unsigned __int8)func0(*(char **)v6) )
break;
}
if ( v4[6] <= ++i )
goto LABEL_17;
}
func1(v7);
}
LABEL_17:
v4 = (_QWORD *)v4[9];
}
while ( v4 );
}
}
else
{
for ( j = pLibSections; j; *(_QWORD *)(*(_QWORD *)v2 + 72LL) = 0LL )
{
v1 = pSections;
v2 = &pSections;
if ( pSections )
{
do
{
v3 = v1;
v1 = *(_QWORD *)(v1 + 72);
}
while ( v1 );
v2 = (void *)(v3 + 72);
}
*(_QWORD *)v2 = j;
j = *(_QWORD *)(j + 72);
pLibSections = j;
}
}
}

original_code:
void AddNeededModules(void)
{
 struct sSection *pSect;
 if ((options & 0x02) == 0) {
  struct sSection **ppLSect;
  ppLSect = &pLibSections;
  while (*ppLSect) {
   struct sSection **ppSect;
   ppSect = &pSections;
   while (*ppSect)
    ppSect = &((*ppSect)->pNext);
   *ppSect = *ppLSect;
   *ppLSect = (*ppLSect)->pNext;
   (*ppSect)->pNext = ((void *)0);
  }
  return;
 }
 if (options & 0x02) {
  if (!addmodulecontaining(smartlinkstartsymbol)) {
   sprintf(temptext, "Can't find start symbol '%s'",
    smartlinkstartsymbol);
   fatalerror(temptext);
  } else
   printf("Smart linking with symbol '%s'\n",
          smartlinkstartsymbol);
 }
 pSect = pSections;
 while (pSect) {
  ULONG i;
  for (i = 0; i < pSect->nNumberOfSymbols; i += 1) {
   if ((pSect->tSymbols[i]->Type == SYM_IMPORT)
       || (pSect->tSymbols[i]->Type == SYM_LOCAL)) {
    if (!symboldefined(pSect->tSymbols[i]->pzName)) {
     addmodulecontaining(pSect->tSymbols[i]->
           pzName);
    }
   }
  }
  pSect = pSect->pNext;
 }
}

canonical_original_code:
void AddNeededModules()
{
 struct sSection *pSect;
 if ((options & 0x02) == 0) {
  struct sSection **ppLSect;
  ppLSect = &pLibSections;
  while (*ppLSect) {
   struct sSection **ppSect;
   ppSect = &pSections;
   while (*ppSect)
    ppSect = &((*ppSect)->pNext);
   *ppSect = *ppLSect;
   *ppLSect = (*ppLSect)->pNext;
   (*ppSect)->pNext = ((void *)0);
  }
  return;
 }
 if (options & 0x02) {
  if (!addmodulecontaining(smartlinkstartsymbol)) {
   sprintf(temptext, "Can't find start symbol '%s'",
    smartlinkstartsymbol);
   fatalerror(temptext);
  } else
   printf("Smart linking with symbol '%s'\n",
          smartlinkstartsymbol);
 }
 pSect = pSections;
 while (pSect) {
  unsigned long i;
  for (i = 0; i < pSect->nNumberOfSymbols; i += 1) {
   if ((pSect->tSymbols[i]->Type == SYM_IMPORT)
       || (pSect->tSymbols[i]->Type == SYM_LOCAL)) {
    if (!symboldefined(pSect->tSymbols[i]->pzName)) {
     addmodulecontaining(pSect->tSymbols[i]->
           pzName);
    }
   }
  }
  pSect = pSect->pNext;
 }
}


decompiled_code:
char *__fastcall out_Setname(char *src)
{
char *result; // rax

result = strcpy(tzOutname, src);
oOutput = 1;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char *src)
{
char *result; // rax

result = strcpy(tzOutname, src);
oOutput = 1;
return result;
}

original_code:
void out_Setname(char *tzOutputfile)
{
 strcpy(tzOutname, tzOutputfile);
 oOutput = 1;
}

variable_types:
tzOutputfile
char *


Output

decompiled_code:
void Output()
{
if ( oOutput )
{
if ( outputtype )
{
if ( outputtype == 1 )
PSION2_Output();
}
else
{
GBROM_Output();
}
}
}

canonical_decompiled_code:
void func5()
{
if ( oOutput )
{
if ( outputtype )
{
if ( outputtype == 1 )
func4();
}
else
{
func3();
}
}
}

original_code:
void Output(void)
{
 if (oOutput) {
  switch (outputtype) {
  case OUTPUT_GBROM:
   GBROM_Output();
   break;
  case OUTPUT_PSION2:
   PSION2_Output();
   break;
  }
 }
}

canonical_original_code:
void Output()
{
 if (oOutput) {
  switch (outputtype) {
  case OUTPUT_GBROM:
   GBROM_Output();
   break;
  case OUTPUT_PSION2:
   PSION2_Output();
   break;
  }
 }
}


yywrap

decompiled_code:
// attributes: thunk
__int64 yywrap()
{
return popcontext();
}

canonical_decompiled_code:
__int64 func3()
{
return func2();
}

original_code:
int yywrap(void)
{
 return (popcontext());
}

canonical_original_code:
int yywrap()
{
 return (popcontext());
}


decompiled_code:
char *__fastcall fstk_AddIncludePath(char *src)
{
__int64 v1; // rax

v1 = NextIncPath++;
return strcpy(&IncludePaths[513 * v1], src);
}

canonical_decompiled_code:
char *__fastcall func5(char *src)
{
__int64 v1; // rax

v1 = NextIncPath++;
return strcpy(&IncludePaths[513 * v1], src);
}

original_code:
void fstk_AddIncludePath(char *s)
{
 strcpy(IncludePaths[NextIncPath++], s);
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall fstk_RunMacroArg(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rax
const char *v3; // rbp
size_t v4; // rax

v1 = a1 - 48;
if ( a1 == 64 )
v1 = -1LL;
v2 = sym_FindMacroArg(v1);
if ( !v2 )
return fatalerror("No such macroargument");
v3 = (const char *)v2;
pushcontext();
nCurrentStatus = 2LL;
sprintf(tzCurrentFileName, "%c", (unsigned __int8)v1);
v4 = strlen(v3);
CurrentFlexHandle = yy_scan_bytes(v3, v4);
return yy_switch_to_buffer(CurrentFlexHandle);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rax
const char *v3; // rbp
size_t v4; // rax

v1 = a1 - 48;
if ( a1 == 64 )
v1 = -1LL;
v2 = sym_FindMacroArg(v1);
if ( !v2 )
return fatalerror("No such macroargument");
v3 = (const char *)v2;
func1();
nCurrentStatus = 2LL;
sprintf(tzCurrentFileName, "%c", (unsigned __int8)v1);
v4 = strlen(v3);
CurrentFlexHandle = yy_scan_bytes(v3, v4);
return yy_switch_to_buffer(CurrentFlexHandle);
}

original_code:
void fstk_RunMacroArg(SLONG s)
{
 char *sym;
 if (s == '@')
  s = -1;
 else
  s -= '0';
 if ((sym = sym_FindMacroArg(s)) != ((void *)0)) {
  pushcontext();
  nCurrentStatus = 2;
  sprintf(tzCurrentFileName, "%c", (UBYTE) s);
  CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
  yy_switch_to_buffer(CurrentFlexHandle);
 } else
  fatalerror("No such macroargument");
}

canonical_original_code:
void fstk_RunMacroArg(long s)
{
 char *sym;
 if (s == '@')
  s = -1;
 else
  s -= '0';
 if ((sym = sym_FindMacroArg(s)) != ((void *)0)) {
  pushcontext();
  nCurrentStatus = 2;
  sprintf(tzCurrentFileName, "%c", (unsigned char) s);
  CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
  yy_switch_to_buffer(CurrentFlexHandle);
 } else
  fatalerror("No such macroargument");
}

variable_types: s long

variable_types:
sym
char *


decompiled_code:
void __fastcall fstk_RunRept(__int64 a1)
{
if ( a1 )
{
pushcontext();
sym_UseCurrentMacroArgs();
++nMacroCount;
sym_SetMacroArgID();
sym_UseNewMacroArgs();
nCurrentREPTBlockCount = a1;
nCurrentStatus = 3LL;
pCurrentREPTBlock = tzNewMacro;
nCurrentREPTBlockSize = ulNewMacroSize;
CurrentFlexHandle = yy_scan_bytes(tzNewMacro, ulNewMacroSize);
yy_switch_to_buffer(CurrentFlexHandle);
}
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
if ( a1 )
{
func1();
sym_UseCurrentMacroArgs();
++nMacroCount;
sym_SetMacroArgID();
sym_UseNewMacroArgs();
nCurrentREPTBlockCount = a1;
nCurrentStatus = 3LL;
pCurrentREPTBlock = tzNewMacro;
nCurrentREPTBlockSize = ulNewMacroSize;
CurrentFlexHandle = yy_scan_bytes(tzNewMacro, ulNewMacroSize);
yy_switch_to_buffer(CurrentFlexHandle);
}
}

original_code:
void fstk_RunRept(ULONG count)
{
 if (count) {
  pushcontext();
  sym_UseCurrentMacroArgs();
  sym_SetMacroArgID(nMacroCount++);
  sym_UseNewMacroArgs();
  nCurrentREPTBlockCount = count;
  nCurrentStatus = 3;
  nCurrentREPTBlockSize = ulNewMacroSize;
  pCurrentREPTBlock = tzNewMacro;
  CurrentFlexHandle =
      yy_scan_bytes(pCurrentREPTBlock, nCurrentREPTBlockSize);
  yy_switch_to_buffer(CurrentFlexHandle);
 }
}

canonical_original_code:
void fstk_RunRept(unsigned long count)
{
 if (count) {
  pushcontext();
  sym_UseCurrentMacroArgs();
  sym_SetMacroArgID(nMacroCount++);
  sym_UseNewMacroArgs();
  nCurrentREPTBlockCount = count;
  nCurrentStatus = 3;
  nCurrentREPTBlockSize = ulNewMacroSize;
  pCurrentREPTBlock = tzNewMacro;
  CurrentFlexHandle =
      yy_scan_bytes(pCurrentREPTBlock, nCurrentREPTBlockSize);
  yy_switch_to_buffer(CurrentFlexHandle);
 }
}

variable_types:
count
unsigned long


decompiled_code:
__int64 __fastcall fstk_Init(char *src)
{
FILE *v1; // rbp
__int64 result; // rax
char v3[536]; // [rsp+0h] [rbp-218h] BYREF

sym_AddString("__FILE__", src);
strcpy(v3, src);
fstk_FindFile(v3);
pFileStack = 0LL;
v1 = fopen(v3, "rt");
pCurrentFile = v1;
result = 0LL;
if ( v1 )
{
nMacroCount = 0LL;
nCurrentStatus = 0LL;
strcpy(tzCurrentFileName, v3);
CurrentFlexHandle = yy_create_buffer(v1);
yy_switch_to_buffer(CurrentFlexHandle);
result = 1LL;
nLineNo = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *src)
{
FILE *v1; // rbp
__int64 result; // rax
char v3[536]; // [rsp+0h] [rbp-218h] BYREF

sym_AddString("__FILE__", src);
strcpy(v3, src);
fstk_FindFile(v3);
pFileStack = 0LL;
v1 = fopen(v3, "rt");
pCurrentFile = v1;
result = 0LL;
if ( v1 )
{
nMacroCount = 0LL;
nCurrentStatus = 0LL;
strcpy(tzCurrentFileName, v3);
CurrentFlexHandle = yy_create_buffer(v1);
yy_switch_to_buffer(CurrentFlexHandle);
result = 1LL;
nLineNo = 1LL;
}
return result;
}

original_code:
ULONG fstk_Init(char *s)
{
 char tzFileName[512 + 1];
 sym_AddString("__FILE__", s);
 strcpy(tzFileName, s);
 fstk_FindFile(tzFileName);
 pFileStack = ((void *)0);
 if ((pCurrentFile = fopen(tzFileName, "rt")) != ((void *)0)) {
  nMacroCount = 0;
  nCurrentStatus = 0;
  strcpy(tzCurrentFileName, tzFileName);
  CurrentFlexHandle = yy_create_buffer(pCurrentFile);
  yy_switch_to_buffer(CurrentFlexHandle);
  nLineNo = 1;
  return (1);
 } else
  return (0);
}

canonical_original_code:
unsigned long fstk_Init(char *s)
{
 char tzFileName[-1];
 sym_AddString("__FILE__", s);
 strcpy(tzFileName, s);
 fstk_FindFile(tzFileName);
 pFileStack = ((void *)0);
 if ((pCurrentFile = fopen(tzFileName, "rt")) != ((void *)0)) {
  nMacroCount = 0;
  nCurrentStatus = 0;
  strcpy(tzCurrentFileName, tzFileName);
  CurrentFlexHandle = yy_create_buffer(pCurrentFile);
  yy_switch_to_buffer(CurrentFlexHandle);
  nLineNo = 1;
  return (1);
 } else
  return (0);
}

variable_types:
tzFileName
char [-1]

return_type: unsigned long


SetSymfileName

decompiled_code:
size_t __fastcall SetSymfileName(const char *a1)
{
FILE *v1; // rcx

stream = fopen(a1, "wt");
v1 = stream;
if ( !stream )
{
fatalerror("Unable to open symfile for writing");
v1 = stream;
}
return fwrite(";File generated by xLink v1.06c\n\n", 1uLL, 0x21uLL, v1);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
FILE *v1; // rcx

stream = fopen(a1, "wt");
v1 = stream;
if ( !stream )
{
fatalerror("Unable to open symfile for writing");
v1 = stream;
}
return fwrite(";File generated by xLink v1.06c\n\n", 1uLL, 0x21uLL, v1);
}

original_code:
void SetSymfileName(char *name)
{
 sf = fopen(name, "wt");
 if (!sf)
  fatalerror("Unable to open symfile for writing");
 fprintf(sf, ";File generated by xLink v" "1.06c" "\n\n");
}


CloseMapfile

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

if ( mf )
{
result = fclose(mf);
mf = 0LL;
}
if ( stream )
{
result = fclose(stream);
stream = 0LL;
}
return result;
}

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

if ( mf )
{
result = fclose(mf);
mf = 0LL;
}
if ( stream )
{
result = fclose(stream);
stream = 0LL;
}
return result;
}

original_code:
void CloseMapfile(void)
{
 if (mf) {
  fclose(mf);
  mf = ((void *)0);
 }
 if (sf) {
  fclose(sf);
  sf = ((void *)0);
 }
}

canonical_original_code:
void CloseMapfile()
{
 if (mf) {
  fclose(mf);
  mf = ((void *)0);
 }
 if (sf) {
  fclose(sf);
  sf = ((void *)0);
 }
}


MapfileCloseBank

decompiled_code:
int __fastcall MapfileCloseBank(__int64 a1)
{
int result; // eax

if ( mf )
{
if ( *((_QWORD *)&MaxAvail + currentbank) == a1 )
result = fwrite(" EMPTY\n\n", 1uLL, 9uLL, mf);
else
result = fprintf(mf, " SLACK: $%04lX bytes\n\n", a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
int result; // eax

if ( mf )
{
if ( *((_QWORD *)&MaxAvail + currentbank) == a1 )
result = fwrite(" EMPTY\n\n", 1uLL, 9uLL, mf);
else
result = fprintf(mf, " SLACK: $%04lX bytes\n\n", a1);
}
return result;
}

original_code:
void MapfileCloseBank(SLONG slack)
{
 if (!mf)
  return;
 if (slack == MaxAvail[currentbank])
  fprintf(mf, "  EMPTY\n\n");
 else
  fprintf(mf, "    SLACK: $%04lX bytes\n\n", slack);
}

canonical_original_code:
void MapfileCloseBank(long slack)
{
 if (!mf)
  return;
 if (slack == MaxAvail[currentbank])
  fprintf(mf, "  EMPTY\n\n");
 else
  fprintf(mf, "    SLACK: $%04lX bytes\n\n", slack);
}

variable_types: slack long


decompiled_code:
void on_timer_sig()
{
float v0; // [rsp+8h] [rbp-10h] BYREF
float v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

dht_get(&v0, v1);
base_save(db_sensor, v0, v1[0]);
alarm(timeint);
}

canonical_decompiled_code:
void func0()
{
float v0; // [rsp+8h] [rbp-10h] BYREF
float v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

dht_get(&v0, v1);
base_save(db_sensor, v0, v1[0]);
alarm(timeint);
}

original_code:
void on_timer_sig (int arg) {
  float temp, hum;
  dht_get (&temp, &hum);
  base_save (db_sensor, temp, hum);
  alarm (timeint);
}

canonical_original_code:
void on_timer_sig(int arg) {
  float temp; float hum;
  dht_get (&temp, &hum);
  base_save (db_sensor, temp, hum);
  alarm (timeint);
}

variable_types: arg int

variable_types: temp float

variable_types: hum float


decompiled_code:
void __noreturn on_exit_sig()
{
server_stop(server);
base_close(db_server);
exit(1);
}

canonical_decompiled_code:
void __noreturn func1()
{
server_stop(server);
base_close(db_server);
exit(1);
}

original_code:
void on_exit_sig(int arg) {
  server_stop (server);
  base_close (db_server);
  exit (1);
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
printf("USAGE:\n\t%s [-p port] [-d dbpath] [-t interval] [-s]\n", a1);
puts("Options:");
puts("\t-s\tRun only HTTP server");
puts("Defaults:");
printf("\tport=%d\n", 8080LL);
printf("\tdbpath='%s'\n", "data.db");
return printf("\tinterval=%d\n", 300LL);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
printf("USAGE:\n\t%s [-p port] [-d dbpath] [-t interval] [-s]\n", a1);
puts("Options:");
puts("\t-s\tRun only HTTP server");
puts("Defaults:");
printf("\tport=%d\n", 8080LL);
printf("\tdbpath='%s'\n", "data.db");
return printf("\tinterval=%d\n", 300LL);
}

original_code:
void print_usage(char *name) {
  printf ("USAGE:\n\t%s [-p port] [-d dbpath] [-t interval] [-s]\n", name);
  printf ("Options:\n");
  printf ("\t-s\tRun only HTTP server\n");
  printf ("Defaults:\n");
  printf ("\tport=%d\n", 8080);
  printf ("\tdbpath='%s'\n", "data.db");
  printf ("\tinterval=%d\n", (5*60));
}


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

if ( (unsigned int)base_init(&db_server, dbpath) )
{
fprintf(stderr, "Cannot open base '%s'\n", (const char *)dbpath);
exit(1);
}
result = server_start((unsigned int)port, db_server);
server = result;
if ( !result )
{
fprintf(stderr, "Cannot start server on port %d\n", (unsigned int)port);
exit(1);
}
return result;
}

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

if ( (unsigned int)base_init(&db_server, dbpath) )
{
fprintf(stderr, "Cannot open base '%s'\n", (const char *)dbpath);
exit(1);
}
result = server_start((unsigned int)port, db_server);
server = result;
if ( !result )
{
fprintf(stderr, "Cannot start server on port %d\n", (unsigned int)port);
exit(1);
}
return result;
}

original_code:
void http_start (void) {
  int ret;
  ret = base_init (&db_server, dbpath);
  if (ret != 0) {
    fprintf (stderr, "Cannot open base '%s'\n", dbpath);
    exit (1);
  }
  server = server_start(port, db_server);
  if (server == ((void *)0)) {
    fprintf (stderr, "Cannot start server on port %d\n", port);
    exit (1);
  }
}

canonical_original_code:
void http_start() {
  int ret;
  ret = base_init (&db_server, dbpath);
  if (ret != 0) {
    fprintf (stderr, "Cannot open base '%s'\n", dbpath);
    exit (1);
  }
  server = server_start(port, db_server);
  if (server == ((void *)0)) {
    fprintf (stderr, "Cannot start server on port %d\n", port);
    exit (1);
  }
}

variable_types: ret int


decompiled_code:
unsigned int sensor_start()
{
if ( (unsigned int)base_init(&db_sensor, dbpath) )
{
fprintf(stderr, "Cannot open base '%s'\n", (const char *)dbpath);
exit(1);
}
signal(14, (__sighandler_t)on_timer_sig);
return alarm(timeint);
}

canonical_decompiled_code:
unsigned int func4()
{
if ( (unsigned int)base_init(&db_sensor, dbpath) )
{
fprintf(stderr, "Cannot open base '%s'\n", (const char *)dbpath);
exit(1);
}
signal(14, (__sighandler_t)on_timer_sig);
return alarm(timeint);
}

original_code:
void sensor_start(void) {
  int ret;
  ret = base_init(&db_sensor, dbpath);
  if (ret != 0) {
    fprintf (stderr, "Cannot open base '%s'\n", dbpath);
    exit (1);
  }
  signal (14, &on_timer_sig);
  alarm (timeint);
}

canonical_original_code:
void sensor_start() {
  int ret;
  ret = base_init(&db_sensor, dbpath);
  if (ret != 0) {
    fprintf (stderr, "Cannot open base '%s'\n", dbpath);
    exit (1);
  }
  signal (14, &on_timer_sig);
  alarm (timeint);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v5; // eax
int v6; // eax
int v8; // eax
const char *v9; // rax
int v10; // eax
unsigned int v11; // er12
FILE *v12; // rbp
__int64 v13; // r12
const char *v14; // rsi

opterr = 0;
v3 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "hp:d:t:s");
if ( v5 == -1 )
break;
if ( v5 == 63 )
{
v11 = optopt;
v12 = stderr;
if ( (optopt & 0xFFFFFFEF) == 100 || optopt == 112 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
}
else if ( ((*_ctype_b_loc())[v11] & 0x4000) != 0 )
{
fprintf(v12, "Unknown option `-%c'.\n", v11);
}
else
{
fprintf(v12, "Unknown option character `\\x%x'.\n", v11);
}
goto LABEL_27;
}
switch ( v5 )
{
case 'd':
dbpath = (__int64)optarg;
break;
case 'h':
print_usage(*argv);
return 0;
case 'p':
port = atoi(optarg);
break;
case 's':
v3 = 1;
break;
case 't':
v6 = atoi(optarg);
if ( v6 < 0 )
v6 = 0;
timeint = v6;
break;
default:
abort();
}
}
v8 = port;
if ( port < 0 )
v8 = 8080;
port = v8;
v9 = (const char *)dbpath;
if ( !dbpath )
v9 = "data.db";
dbpath = (__int64)v9;
v10 = timeint;
if ( !timeint )
v10 = 300;
timeint = v10;
if ( optind >= argc )
{
http_start();
if ( !v3 )
sensor_start();
signal(15, (__sighandler_t)on_exit_sig);
signal(2, (__sighandler_t)on_exit_sig);
while ( 1 )
pause();
}
v13 = optind;
do
{
v14 = argv[v13++];
printf("Non-option argument %s\n", v14);
}
while ( argc > (int)v13 );
LABEL_27:
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v5; // eax
int v6; // eax
int v8; // eax
const char *v9; // rax
int v10; // eax
unsigned int v11; // er12
FILE *v12; // rbp
__int64 v13; // r12
const char *v14; // rsi

opterr = 0;
v3 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "hp:d:t:s");
if ( v5 == -1 )
break;
if ( v5 == 63 )
{
v11 = optopt;
v12 = stderr;
if ( (optopt & 0xFFFFFFEF) == 100 || optopt == 112 )
{
fprintf(stderr, "Option -%c requires an argument.\n", (unsigned int)optopt);
}
else if ( ((*_ctype_b_loc())[v11] & 0x4000) != 0 )
{
fprintf(v12, "Unknown option `-%c'.\n", v11);
}
else
{
fprintf(v12, "Unknown option character `\\x%x'.\n", v11);
}
goto LABEL_27;
}
switch ( v5 )
{
case 'd':
dbpath = (__int64)optarg;
break;
case 'h':
func2(*argv);
return 0;
case 'p':
port = atoi(optarg);
break;
case 's':
v3 = 1;
break;
case 't':
v6 = atoi(optarg);
if ( v6 < 0 )
v6 = 0;
timeint = v6;
break;
default:
abort();
}
}
v8 = port;
if ( port < 0 )
v8 = 8080;
port = v8;
v9 = (const char *)dbpath;
if ( !dbpath )
v9 = "data.db";
dbpath = (__int64)v9;
v10 = timeint;
if ( !timeint )
v10 = 300;
timeint = v10;
if ( optind >= argc )
{
func3();
if ( !v3 )
func4();
signal(15, (__sighandler_t)on_exit_sig);
signal(2, (__sighandler_t)on_exit_sig);
while ( 1 )
pause();
}
v13 = optind;
do
{
v14 = argv[v13++];
printf("Non-option argument %s\n", v14);
}
while ( argc > (int)v13 );
LABEL_27:
exit(1);
}

original_code:
int main (int argc, char **argv) {
  int ret;
  int i;
  char* port_s = ((void *)0);
  char* timeint_s = ((void *)0);
  int http_only = 0;
  int c;
  opterr = 0;
  while ((c = getopt (argc, argv, "hp:d:t:s")) != -1)
    switch (c) {
    case 'h':
      print_usage (argv[0]);
      return 0;
    case 'p':
      port_s = optarg;
      port = atoi (port_s);
      break;
    case 'd':
      dbpath = optarg;
      break;
    case 't':
      timeint_s = optarg;
      ret = atoi (timeint_s);
      timeint = ret < 0 ? 0 : (unsigned int)ret;
      break;
    case 's':
      http_only = 1;
      break;
    case '?':
      if (optopt == 'p' ||
   optopt == 'd' ||
   optopt == 't')
 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
      else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint))
 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
      else
 fprintf (stderr,
   "Unknown option character `\\x%x'.\n",
   optopt);
      exit (1);
    default:
      abort ();
    }
  port = port < 0 ? 8080 : port;
  dbpath = dbpath == ((void *)0) ? "data.db" : dbpath;
  timeint = timeint < 1 ? (5*60) : timeint;
  if (optind < argc) {
    for (i = optind; i < argc; i++)
      printf ("Non-option argument %s\n", argv[i]);
    exit (1);
  }
  http_start ();
  if (!http_only) {
    sensor_start ();
  }
  signal (15, &on_exit_sig);
  signal (2, &on_exit_sig);
  while (1)
    pause ();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  int ret;
  int i;
  char *port_s = ((void *)0);
  char *timeint_s = ((void *)0);
  int http_only = 0;
  int c;
  opterr = 0;
  while ((c = getopt (argc, argv, "hp:d:t:s")) != -1)
    switch (c) {
    case 'h':
      print_usage (argv[0]);
      return 0;
    case 'p':
      port_s = optarg;
      port = atoi (port_s);
      break;
    case 'd':
      dbpath = optarg;
      break;
    case 't':
      timeint_s = optarg;
      ret = atoi (timeint_s);
      timeint = ret < 0 ? 0 : (unsigned int)ret;
      break;
    case 's':
      http_only = 1;
      break;
    case '?':
      if (optopt == 'p' ||
   optopt == 'd' ||
   optopt == 't')
 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
      else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint))
 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
      else
 fprintf (stderr,
   "Unknown option character `\\x%x'.\n",
   optopt);
      exit (1);
    default:
      abort ();
    }
  port = port < 0 ? 8080 : port;
  dbpath = dbpath == ((void *)0) ? "data.db" : dbpath;
  timeint = timeint < 1 ? (5*60) : timeint;
  if (optind < argc) {
    for (i = optind; i < argc; i++)
      printf ("Non-option argument %s\n", argv[i]);
    exit (1);
  }
  http_start ();
  if (!http_only) {
    sensor_start ();
  }
  signal (15, &on_exit_sig);
  signal (2, &on_exit_sig);
  while (1)
    pause ();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: c int


decompiled_code:
__int64 __fastcall pi_mmio_set_high(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(pi_mmio_gpio + 28) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(pi_mmio_gpio + 28) = result;
return result;
}

original_code:
static inline void pi_mmio_set_high(const int gpio_number) {
  *(pi_mmio_gpio+7) = 1 << gpio_number;
}

canonical_original_code:
static inline void pi_mmio_set_high(int gpio_number) {
  *(pi_mmio_gpio+7) = 1 << gpio_number;
}


decompiled_code:
__int64 __fastcall pi_mmio_set_low(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(pi_mmio_gpio + 40) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(pi_mmio_gpio + 40) = result;
return result;
}

original_code:
static inline void pi_mmio_set_low(const int gpio_number) {
  *(pi_mmio_gpio+10) = 1 << gpio_number;
}

canonical_original_code:
static inline void pi_mmio_set_low(int gpio_number) {
  *(pi_mmio_gpio+10) = 1 << gpio_number;
}


decompiled_code:
__int64 __fastcall pi_mmio_input(char a1)
{
return *(_DWORD *)(pi_mmio_gpio + 52) & (unsigned int)(1 << a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1)
{
return *(_DWORD *)(pi_mmio_gpio + 52) & (unsigned int)(1 << a1);
}

original_code:
static inline uint32_t pi_mmio_input(const int gpio_number) {
  return *(pi_mmio_gpio+13) & (1 << gpio_number);
}

canonical_original_code:
static inline unsigned int pi_mmio_input(int gpio_number) {
  return *(pi_mmio_gpio+13) & (1 << gpio_number);
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall pi_dht_read(int a1, unsigned int a2, float *a3, float *a4)
{
__int64 v8; // rsi
__int64 v9; // rbx
int v10; // eax
__int64 v11; // rax
int v12; // edx
bool v13; // zf
int v14; // eax
unsigned __int64 v15; // rbx
int v16; // eax
__int64 v17; // rax
char v18; // dl
int v19; // edi
int v20; // edx
__int64 result; // rax
float v22; // xmm0_4
int i; // [rsp+0h] [rbp-184h]
int v24; // [rsp+0h] [rbp-184h]
int j; // [rsp+0h] [rbp-184h]
int v26; // [rsp+0h] [rbp-184h]
int v27; // [rsp+7h] [rbp-17Dh]
char v28; // [rsp+Bh] [rbp-179h]
int v29[94]; // [rsp+Ch] [rbp-178h] BYREF

if ( !a3 )
return 4294967293LL;
if ( !a4 )
return 4294967293LL;
*a4 = 0.0;
*a3 = 0.0;
if ( (int)pi_mmio_init() < 0 )
return 4294967292LL;
memset(v29, 0, 0x148uLL);
pi_mmio_set_output(a2);
set_max_priority();
pi_mmio_set_high(a2);
sleep_milliseconds(500LL);
pi_mmio_set_low(a2);
busy_wait_milliseconds(20LL);
pi_mmio_set_input(a2);
for ( i = 0; i <= 49; ++i )
;
v8 = 32000LL;
while ( 1 )
{
LODWORD(v9) = pi_mmio_input(a2);
if ( !(_DWORD)v9 )
break;
v8 = (unsigned int)(v8 - 1);
if ( !(_DWORD)v8 )
{
LABEL_28:
((void (*)(void))set_default_priority)();
return 0xFFFFFFFFLL;
}
}
v24 = 0;
do
{
while ( !(unsigned int)pi_mmio_input(a2) )
{
v10 = v29[v24] + 1;
v29[v24] = v10;
if ( v10 > 31999 )
goto LABEL_28;
}
while ( 1 )
{
v13 = (unsigned int)pi_mmio_input(a2) == 0;
v14 = v24;
if ( v13 )
break;
v11 = v24 + 1;
v12 = v29[v11] + 1;
v29[v11] = v12;
if ( v12 > 31999 )
goto LABEL_28;
}
v24 += 2;
}
while ( v14 + 2 <= 81 );
set_default_priority(a2, v8);
for ( j = 2; j <= 81; j += 2 )
v9 = (unsigned int)(v29[j] + v9);
v15 = (unsigned __int64)(3435973837LL * v9) >> 37;
v26 = 3;
v27 = 0;
v28 = 0;
do
{
v16 = v26 - 3;
if ( v26 - 3 < 0 )
v16 = v26 + 12;
v17 = v16 >> 4;
v18 = 2 * *((_BYTE *)&v27 + v17);
*((_BYTE *)&v27 + v17) = v18;
if ( v29[v26] >= (unsigned int)v15 )
*((_BYTE *)&v27 + v17) = v18 | 1;
v26 += 2;
}
while ( v26 <= 81 );
v19 = HIBYTE(v27);
v20 = BYTE2(v27);
if ( v28 != HIBYTE(v27) + BYTE2(v27) + (_BYTE)v27 + BYTE1(v27) )
return 4294967294LL;
if ( a1 == 11 )
{
result = 0LL;
*a3 = (float)(unsigned __int8)v27;
*a4 = (float)v20;
}
else if ( a1 == 22 )
{
*a3 = (float)(BYTE1(v27) + ((unsigned __int8)v27 << 8)) / 10.0;
v22 = (float)(v19 + ((v20 & 0x7F) << 8)) / 10.0;
if ( (v20 & 0x80u) != 0 )
LODWORD(v22) ^= LC2;
*a4 = v22;
result = 0LL;
}
else
{
result = 0LL;
}
return result;
}

original_code:
int pi_dht_read(int type, int pin, float* humidity, float* temperature) {
  volatile int i;
  if (humidity == ((void *)0) || temperature == ((void *)0)) {
    return -3;
  }
  *temperature = 0.0f;
  *humidity = 0.0f;
  if (pi_mmio_init() < 0) {
    return -4;
  }
  int pulseCounts[41*2] = {0};
  pi_mmio_set_output(pin);
  set_max_priority();
  pi_mmio_set_high(pin);
  sleep_milliseconds(500);
  pi_mmio_set_low(pin);
  busy_wait_milliseconds(20);
  pi_mmio_set_input(pin);
  for (i = 0; i < 50; ++i) {
  }
  uint32_t count = 0;
  while (pi_mmio_input(pin)) {
    if (++count >= 32000) {
      set_default_priority();
      return -1;
    }
  }
  for (i=0; i < 41*2; i+=2) {
    while (!pi_mmio_input(pin)) {
      if (++pulseCounts[i] >= 32000) {
        set_default_priority();
        return -1;
      }
    }
    while (pi_mmio_input(pin)) {
      if (++pulseCounts[i+1] >= 32000) {
        set_default_priority();
        return -1;
      }
    }
  }
  set_default_priority();
  uint32_t threshold = 0;
  for (i=2; i < 41*2; i+=2) {
    threshold += pulseCounts[i];
  }
  threshold /= 41 -1;
  uint8_t data[5] = {0};
  for (i=3; i < 41*2; i+=2) {
    int index = (i-3)/16;
    data[index] <<= 1;
    if (pulseCounts[i] >= threshold) {
      data[index] |= 1;
    }
  }
  if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
    if (type == 11) {
      *humidity = (float)data[0];
      *temperature = (float)data[2];
    }
    else if (type == 22) {
      *humidity = (data[0] * 256 + data[1]) / 10.0f;
      *temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
      if (data[2] & 0x80) {
        *temperature *= -1.0f;
      }
    }
    return 0;
  }
  else {
    return -2;
  }
}

canonical_original_code:
int pi_dht_read(int type, int pin, float *humidity, float *temperature) {
  int i;
  if (humidity == ((void *)0) || temperature == ((void *)0)) {
    return -3;
  }
  *temperature = 0.0f;
  *humidity = 0.0f;
  if (pi_mmio_init() < 0) {
    return -4;
  }
  int pulseCounts[-1] = {0};
  pi_mmio_set_output(pin);
  set_max_priority();
  pi_mmio_set_high(pin);
  sleep_milliseconds(500);
  pi_mmio_set_low(pin);
  busy_wait_milliseconds(20);
  pi_mmio_set_input(pin);
  for (i = 0; i < 50; ++i) {
  }
  unsigned int count = 0;
  while (pi_mmio_input(pin)) {
    if (++count >= 32000) {
      set_default_priority();
      return -1;
    }
  }
  for (i=0; i < 41*2; i+=2) {
    while (!pi_mmio_input(pin)) {
      if (++pulseCounts[i] >= 32000) {
        set_default_priority();
        return -1;
      }
    }
    while (pi_mmio_input(pin)) {
      if (++pulseCounts[i+1] >= 32000) {
        set_default_priority();
        return -1;
      }
    }
  }
  set_default_priority();
  unsigned int threshold = 0;
  for (i=2; i < 41*2; i+=2) {
    threshold += pulseCounts[i];
  }
  threshold /= 41 -1;
  unsigned char data[5] = {0};
  for (i=3; i < 41*2; i+=2) {
    int index = (i-3)/16;
    data[index] <<= 1;
    if (pulseCounts[i] >= threshold) {
      data[index] |= 1;
    }
  }
  if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
    if (type == 11) {
      *humidity = (float)data[0];
      *temperature = (float)data[2];
    }
    else if (type == 22) {
      *humidity = (data[0] * 256 + data[1]) / 10.0f;
      *temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
      if (data[2] & 0x80) {
        *temperature *= -1.0f;
      }
    }
    return 0;
  }
  else {
    return -2;
  }
}

variable_types: type int

variable_types: pin int

variable_types:
humidity
float *

variable_types:
temperature
float *

variable_types:
pulseCounts
int [-1]

variable_types:
count
unsigned int

variable_types:
threshold
unsigned int

variable_types:
data
unsigned char [5]

variable_types: index int


decompiled_code:
__time_t __fastcall busy_wait_milliseconds(unsigned int a1)
{
__time_t result; // rax
__int64 v2; // rbp
int v3; // edx
__time_t v4; // rbp
__suseconds_t v5; // rbx
struct timeval v6; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v6, 0LL);
result = v6.tv_sec;
v2 = a1 / 0x3E8;
v3 = 1000 * v2;
v4 = v6.tv_sec + v2;
v5 = v6.tv_usec + 1000 * (a1 - v3);
if ( v5 > 999999 )
{
++v4;
v5 -= 1000000LL;
}
while ( result != v4 )
{
if ( result >= v4 )
return result;
LABEL_4:
gettimeofday(&v6, 0LL);
result = v6.tv_sec;
}
if ( v6.tv_usec < v5 )
goto LABEL_4;
return result;
}

canonical_decompiled_code:
__time_t __fastcall func0(unsigned int a1)
{
__time_t result; // rax
__int64 v2; // rbp
int v3; // edx
__time_t v4; // rbp
__suseconds_t v5; // rbx
struct timeval v6; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v6, 0LL);
result = v6.tv_sec;
v2 = a1 / 0x3E8;
v3 = 1000 * v2;
v4 = v6.tv_sec + v2;
v5 = v6.tv_usec + 1000 * (a1 - v3);
if ( v5 > 999999 )
{
++v4;
v5 -= 1000000LL;
}
while ( result != v4 )
{
if ( result >= v4 )
return result;
LABEL_4:
gettimeofday(&v6, 0LL);
result = v6.tv_sec;
}
if ( v6.tv_usec < v5 )
goto LABEL_4;
return result;
}

original_code:
void busy_wait_milliseconds(uint32_t millis) {
  struct timeval deltatime;
  deltatime.tv_sec = millis / 1000;
  deltatime.tv_usec = (millis % 1000) * 1000;
  struct timeval walltime;
  gettimeofday(&walltime, ((void *)0));
  struct timeval endtime;
  do { (&endtime)->tv_sec = (&walltime)->tv_sec + (&deltatime)->tv_sec; (&endtime)->tv_usec = (&walltime)->tv_usec + (&deltatime)->tv_usec; if ((&endtime)->tv_usec >= 1000000) { ++(&endtime)->tv_sec; (&endtime)->tv_usec -= 1000000; } } while (0);
  while ((((&walltime)->tv_sec == (&endtime)->tv_sec) ? ((&walltime)->tv_usec < (&endtime)->tv_usec) : ((&walltime)->tv_sec < (&endtime)->tv_sec))) {
    gettimeofday(&walltime, ((void *)0));
  }
}

canonical_original_code:
void busy_wait_milliseconds(unsigned int millis) {
  struct timeval deltatime;
  deltatime.tv_sec = millis / 1000;
  deltatime.tv_usec = (millis % 1000) * 1000;
  struct timeval walltime;
  gettimeofday(&walltime, ((void *)0));
  struct timeval endtime;
  do { (&endtime)->tv_sec = (&walltime)->tv_sec + (&deltatime)->tv_sec; (&endtime)->tv_usec = (&walltime)->tv_usec + (&deltatime)->tv_usec; if ((&endtime)->tv_usec >= 1000000) { ++(&endtime)->tv_sec; (&endtime)->tv_usec -= 1000000; } } while (0);
  while ((((&walltime)->tv_sec == (&endtime)->tv_sec) ? ((&walltime)->tv_usec < (&endtime)->tv_usec) : ((&walltime)->tv_sec < (&endtime)->tv_sec))) {
    gettimeofday(&walltime, ((void *)0));
  }
}

variable_types:
millis
unsigned int

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int __fastcall sleep_milliseconds(unsigned int a1)
{
int *v1; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = a1 / 0x3E8;
v3.tv_nsec = 1000000LL * (a1 % 0x3E8);
do
{
LODWORD(v1) = clock_nanosleep(1, 0, &v3, &v3);
if ( !(_DWORD)v1 )
break;
v1 = _errno_location();
}
while ( *v1 == 4 );
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1)
{
int *v1; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = a1 / 0x3E8;
v3.tv_nsec = 1000000LL * (a1 % 0x3E8);
do
{
LODWORD(v1) = clock_nanosleep(1, 0, &v3, &v3);
if ( !(_DWORD)v1 )
break;
v1 = _errno_location();
}
while ( *v1 == 4 );
return (int)v1;
}

original_code:
void sleep_milliseconds(uint32_t millis) {
  struct timespec sleep;
  sleep.tv_sec = millis / 1000;
  sleep.tv_nsec = (millis % 1000) * 1000000L;
  while (clock_nanosleep(1, 0, &sleep, &sleep) && (*__errno_location ()) == 4);
}

canonical_original_code:
void sleep_milliseconds(unsigned int millis) {
  struct timespec sleep;
  sleep.tv_sec = millis / 1000;
  sleep.tv_nsec = (millis % 1000) * 1000000L;
  while (clock_nanosleep(1, 0, &sleep, &sleep) && (*__errno_location ()) == 4);
}

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


decompiled_code:
int set_max_priority()
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = sched_get_priority_max(1);
return sched_setscheduler(0, 1, &param);
}

canonical_decompiled_code:
int func2()
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = sched_get_priority_max(1);
return sched_setscheduler(0, 1, &param);
}

original_code:
void set_max_priority(void) {
  struct sched_param sched;
  memset(&sched, 0, sizeof(sched));
  sched.sched_priority = sched_get_priority_max(1);
  sched_setscheduler(0, 1, &sched);
}

canonical_original_code:
void set_max_priority() {
  struct sched_param sched;
  memset(&sched, 0, sizeof(sched));
  sched.sched_priority = sched_get_priority_max(1);
  sched_setscheduler(0, 1, &sched);
}

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
int set_default_priority()
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = 0;
return sched_setscheduler(0, 0, &param);
}

canonical_decompiled_code:
int func3()
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = 0;
return sched_setscheduler(0, 0, &param);
}

original_code:
void set_default_priority(void) {
  struct sched_param sched;
  memset(&sched, 0, sizeof(sched));
  sched.sched_priority = 0;
  sched_setscheduler(0, 0, &sched);
}

canonical_original_code:
void set_default_priority() {
  struct sched_param sched;
  memset(&sched, 0, sizeof(sched));
  sched.sched_priority = 0;
  sched_setscheduler(0, 0, &sched);
}

user_defined_types: struct sched_param { int sched_priority; }


decompiled_code:
char *__fastcall buffer_est_size(__int64 a1)
{
return (char *)result_buffer - a1 + 10485760;
}

canonical_decompiled_code:
char *__fastcall func3(__int64 a1)
{
return (char *)result_buffer - a1 + 10485760;
}

original_code:
long buffer_est_size (char* p) {
  char* end = result_buffer + 10*1024*1024;
  return end - p;
}

canonical_original_code:
long buffer_est_size(char *p) {
  char *end = result_buffer + 10*1024*1024;
  return end - p;
}

variable_types:
p
char *

variable_types:
end
char *


decompiled_code:
_BOOL8 __fastcall check_buffer(__int64 a1, __int64 a2)
{
return a1 + a2 + 1 < (unsigned __int64)result_buffer + 10485760;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1, __int64 a2)
{
return a1 + a2 + 1 < (unsigned __int64)result_buffer + 10485760;
}

original_code:
int check_buffer (char* p, size_t n) {
  char* end = result_buffer + 10*1024*1024;
  return (p + n + 1) < end;
}

canonical_original_code:
int check_buffer(char *p, unsigned long n) {
  char *end = result_buffer + 10*1024*1024;
  return (p + n + 1) < end;
}

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall add_to_buffer(void **a1, const char *a2)
{
__int64 v3; // rax
void *v4; // rdi
__int64 v5; // rbp
_BOOL4 v6; // er8
__int64 result; // rax

v3 = strlen(a2);
v4 = *a1;
v5 = v3;
v6 = check_buffer((__int64)v4, v3);
result = 1LL;
if ( v6 )
{
memcpy(v4, a2, v5 + 1);
*a1 = (char *)*a1 + v5;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(void **a1, const char *a2)
{
__int64 v3; // rax
void *v4; // rdi
__int64 v5; // rbp
_BOOL4 v6; // er8
__int64 result; // rax

v3 = strlen(a2);
v4 = *a1;
v5 = v3;
v6 = func4((__int64)v4, v3);
result = 1LL;
if ( v6 )
{
memcpy(v4, a2, v5 + 1);
*a1 = (char *)*a1 + v5;
result = 0LL;
}
return result;
}

original_code:
int add_to_buffer (char** pp, char* str) {
  size_t len = strlen(str);
  if (!check_buffer (*pp, len))
    return 1;
  strcpy (*pp, str);
  (*pp) += len;
  return 0;
}

canonical_original_code:
int add_to_buffer(char **pp, char *str) {
  unsigned long len = strlen(str);
  if (!check_buffer (*pp, len))
    return 1;
  strcpy (*pp, str);
  (*pp) += len;
  return 0;
}

variable_types:
pp
char **

variable_types:
str
char *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall result_iterate(void **a1, int a2, __int64 a3, __int64 a4)
{
__int64 v7; // rbx
char *v8; // rdi
char *v9; // rbp
const char *v10; // rdx
int v11; // eax

if ( !(unsigned int)add_to_buffer(a1, "{") )
{
if ( a2 <= 0 )
return (unsigned int)add_to_buffer(a1, "}, ") != 0;
v7 = 0LL;
while ( 1 )
{
if ( *(_QWORD *)(a3 + 8 * v7) )
{
v8 = (char *)*a1;
v9 = buffer_est_size((__int64)*a1) - 1;
v11 = snprintf(v8, (size_t)v9, v10, *(_QWORD *)(a4 + 8 * v7));
if ( (__int64)v9 <= v11 )
break;
*a1 = (char *)*a1 + v11;
if ( a2 - 1 != (_DWORD)v7 )
{
if ( (unsigned int)add_to_buffer(a1, ", ") )
break;
}
}
if ( ++v7 == a2 )
return (unsigned int)add_to_buffer(a1, "}, ") != 0;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(void **a1, int a2, __int64 a3, __int64 a4)
{
__int64 v7; // rbx
char *v8; // rdi
char *v9; // rbp
const char *v10; // rdx
int v11; // eax

if ( !(unsigned int)func5(a1, "{") )
{
if ( a2 <= 0 )
return (unsigned int)func5(a1, "}, ") != 0;
v7 = 0LL;
while ( 1 )
{
if ( *(_QWORD *)(a3 + 8 * v7) )
{
v8 = (char *)*a1;
v9 = func3((__int64)*a1) - 1;
v11 = snprintf(v8, (size_t)v9, v10, *(_QWORD *)(a4 + 8 * v7));
if ( (__int64)v9 <= v11 )
break;
*a1 = (char *)*a1 + v11;
if ( a2 - 1 != (_DWORD)v7 )
{
if ( (unsigned int)func5(a1, ", ") )
break;
}
}
if ( ++v7 == a2 )
return (unsigned int)func5(a1, "}, ") != 0;
}
}
return 1LL;
}

original_code:
int result_iterate (void* json_v, int col_count, char** cols, char** col_names) {
  int i;
  long size, count;
  char** json = (char**)json_v;
  if (add_to_buffer (json, "{") != 0)
    return 1;
  for (i = 0; i < col_count; i++) {
    if (cols[i] == ((void *)0))
      continue;
    size = buffer_est_size (*json) - 1;
    count = snprintf (*json, (size_t)size, "%s: '%s'", col_names[i], cols[i]);
    if (count >= size)
      return 1;
    (*json) += count;
    if (i != col_count - 1)
      if (add_to_buffer (json, ", ") != 0)
 return 1;
  }
  if (add_to_buffer (json, "}, ") != 0)
    return 1;
  return 0;
}

canonical_original_code:
int result_iterate(void *json_v, int col_count, char **cols, char **col_names) {
  int i;
  long size; long count;
  char **json = (char**)json_v;
  if (add_to_buffer (json, "{") != 0)
    return 1;
  for (i = 0; i < col_count; i++) {
    if (cols[i] == ((void *)0))
      continue;
    size = buffer_est_size (*json) - 1;
    count = snprintf (*json, (unsigned long)size, "%s: '%s'", col_names[i], cols[i]);
    if (count >= size)
      return 1;
    (*json) += count;
    if (i != col_count - 1)
      if (add_to_buffer (json, ", ") != 0)
 return 1;
  }
  if (add_to_buffer (json, "}, ") != 0)
    return 1;
  return 0;
}

variable_types:
cols
char **

variable_types: i int

variable_types: size long

variable_types: count long

variable_types:
json
char **


decompiled_code:
__int64 pi_mmio_init()
{
unsigned int v0; // er12
int v2; // eax

if ( !pi_mmio_gpio )
{
v2 = open("/dev/mem", 1052674);
v0 = v2;
if ( v2 == -1 )
return v0;
pi_mmio_gpio = (__int64)mmap(0LL, 0x1000uLL, 3, 1, v2, 538968064LL);
close(v0);
if ( pi_mmio_gpio == -1 )
{
pi_mmio_gpio = 0LL;
return (unsigned int)-2;
}
}
return 0;
}

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

if ( !pi_mmio_gpio )
{
v2 = open("/dev/mem", 1052674);
v0 = v2;
if ( v2 == -1 )
return v0;
pi_mmio_gpio = (__int64)mmap(0LL, 0x1000uLL, 3, 1, v2, 538968064LL);
close(v0);
if ( pi_mmio_gpio == -1 )
{
pi_mmio_gpio = 0LL;
return (unsigned int)-2;
}
}
return 0;
}

canonical_original_code:
int pi_mmio_init() {
  if (pi_mmio_gpio == ((void *)0)) {
    int fd = open("/dev/mem", 02 | 04010000);
    if (fd == -1) {
      return -1;
    }
    pi_mmio_gpio = (unsigned int *)mmap(((void *)0), 4096, 0x1 | 0x2, 0x01, fd, (0x20000000 + 0x200000));
    close(fd);
    if (pi_mmio_gpio == ((void *) -1)) {
      pi_mmio_gpio = ((void *)0);
      return -2;
    }
  }
  return 0;
}

variable_types: fd int


repo: velovix/super-glitch-engine

original_code:
void pk_psetMonster(monster_t mon, player_t* player) {
 if(player->monCnt > 5) {
  printf("[ERROR] Max monster count reached on player!\n");
  return;
 }
 player->monsters[player->monCnt] = mon;
 player->monCnt++;
}

canonical_original_code:
void pk_psetMonster(struct monster_t mon, struct player_t *player) {
 if(player->monCnt > 5) {
  printf("[ERROR] Max monster count reached on player!\n");
  return;
 }
 player->monsters[player->monCnt] = mon;
 player->monCnt++;
}

variable_types: struct monster_t { int experience; char name[10]; struct monFileObj_t { struct monBasic_f { unsigned char name[10]; unsigned char type1; unsigned char type2; unsigned int height; unsigned int weight; } basic; struct monStats_f { unsigned char b_hp; unsigned char b_att; unsigned char b_def; unsigned char b_spAtt; unsigned char b_spDef; unsigned char b_speed; unsigned char ev_hp; unsigned char ev_att; unsigned char ev_def; unsigned char ev_spAtt; unsigned char ev_spDef; unsigned char ev_speed; } stats; struct monMovelist_f { unsigned int natLearnedCnt; struct monNatMoveEntry_f { unsigned int move; unsigned char level; } *natLearned; unsigned int tmLearnedCnt; unsigned int *tmLearned; } movelist; struct monExperience_f { unsigned char type; } experience; struct monEvolution_f { unsigned char evolves; unsigned int mon; unsigned char level; } evolution; struct monAesthetics_f { unsigned int battleSprite; unsigned int icon; unsigned int body; unsigned int footprint; unsigned int cry; } aesthetics; struct monOthers_f { unsigned char species[13]; unsigned char genderless; unsigned char malePerc; unsigned int ability; unsigned char catchRate; unsigned int heldItem; } etc; } *id; _Bool shiny; struct stats_t { int hp; int mHp; } stats; struct moveMask_t { int cpp; int value; } moves[4]; int condition; }

variable_types: struct player_t { char name[12]; int sprite; _Bool pause; struct monster_t monsters[6]; struct charMover_t { int x; int nextX; int lastX; int animCycle; _Bool step; } mover; } *

user_defined_types: struct monster_t { int experience; char name[10]; struct monFileObj_t *id; _Bool shiny; struct stats_t stats; struct moveMask_t moves[4]; int condition; }

user_defined_types: struct player_t { char name[12]; int sprite; _Bool pause; struct monster_t monsters[6]; struct charMover_t mover; }

user_defined_types: struct monFileObj_t { struct monBasic_f basic; struct monStats_f stats; struct monMovelist_f movelist; struct monExperience_f experience; struct monEvolution_f evolution; struct monAesthetics_f aesthetics; struct monOthers_f etc; }

user_defined_types: struct monMovelist_f { unsigned int natLearnedCnt; struct monNatMoveEntry_f *natLearned; unsigned int tmLearnedCnt; unsigned int *tmLearned; }


decompiled_code:
__int64 __fastcall pk_pMove(__int64 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

if ( !*(_BYTE *)(a3 + 24) )
result = pk_moveChar(a1, a2, a3 + 896);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

if ( !*(_BYTE *)(a3 + 24) )
result = pk_moveChar(a1, a2, a3 + 896);
return result;
}

original_code:
void pk_pMove(int dir, _Bool move, player_t* player) {
 if(player->pause) {
  return;
 }
 pk_moveChar(dir, move, &player->mover);
}

canonical_original_code:
void pk_pMove(int dir, _Bool move, struct player_t *player) {
 if(player->pause) {
  return;
 }
 pk_moveChar(dir, move, &player->mover);
}

variable_types: dir int

variable_types: struct player_t { char name[12]; int sprite; _Bool pause; struct monster_t { int experience; char name[10]; struct monFileObj_t { struct monBasic_f { unsigned char name[10]; unsigned char type1; unsigned char type2; unsigned int height; unsigned int weight; } basic; struct monStats_f { unsigned char b_hp; unsigned char b_att; unsigned char b_def; unsigned char b_spAtt; unsigned char b_spDef; unsigned char b_speed; unsigned char ev_hp; unsigned char ev_att; unsigned char ev_def; unsigned char ev_spAtt; unsigned char ev_spDef; unsigned char ev_speed; } stats; struct monMovelist_f { unsigned int natLearnedCnt; struct monNatMoveEntry_f { unsigned int move; unsigned char level; } *natLearned; unsigned int tmLearnedCnt; unsigned int *tmLearned; } movelist; struct monExperience_f { unsigned char type; } experience; struct monEvolution_f { unsigned char evolves; unsigned int mon; unsigned char level; } evolution; struct monAesthetics_f { unsigned int battleSprite; unsigned int icon; unsigned int body; unsigned int footprint; unsigned int cry; } aesthetics; struct monOthers_f { unsigned char species[13]; unsigned char genderless; unsigned char malePerc; unsigned int ability; unsigned char catchRate; unsigned int heldItem; } etc; } *id; _Bool shiny; struct stats_t { int hp; int mHp; } stats; struct moveMask_t { int cpp; int value; } moves[4]; int condition; } monsters[6]; struct charMover_t { int x; int nextX; int lastX; int animCycle; _Bool step; } mover; } *


decompiled_code:
__int64 __fastcall pk_pgetFrame(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, __int64 a8, __int128 a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, int a23, int a24, int a25, int a26, int a27, int a28, int a29, int a30, int a31, int a32, int a33, int a34, int a35, int a36, int a37, int a38, int a39, int a40, int a41, int a42, int a43, int a44, int a45, int a46, int a47, int a48, int a49, int a50, int a51, int a52, int a53, int a54, int a55, int a56, int a57, int a58, int a59, int a60, int a61, int a62, int a63)
{
return pk_getCharFrame(HIDWORD(a8));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, __int64 a8, __int128 a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, int a23, int a24, int a25, int a26, int a27, int a28, int a29, int a30, int a31, int a32, int a33, int a34, int a35, int a36, int a37, int a38, int a39, int a40, int a41, int a42, int a43, int a44, int a45, int a46, int a47, int a48, int a49, int a50, int a51, int a52, int a53, int a54, int a55, int a56, int a57, int a58, int a59, int a60, int a61, int a62, int a63)
{
return pk_getCharFrame(HIDWORD(a8));
}

original_code:
int pk_pgetFrame(player_t player) {
 return pk_getCharFrame(player.sprite, player.mover);
}

canonical_original_code:
int pk_pgetFrame(struct player_t player) {
 return pk_getCharFrame(player.sprite, player.mover);
}

variable_types: struct player_t { char name[12]; int sprite; _Bool pause; struct monster_t { int experience; char name[10]; struct monFileObj_t { struct monBasic_f { unsigned char name[10]; unsigned char type1; unsigned char type2; unsigned int height; unsigned int weight; } basic; struct monStats_f { unsigned char b_hp; unsigned char b_att; unsigned char b_def; unsigned char b_spAtt; unsigned char b_spDef; unsigned char b_speed; unsigned char ev_hp; unsigned char ev_att; unsigned char ev_def; unsigned char ev_spAtt; unsigned char ev_spDef; unsigned char ev_speed; } stats; struct monMovelist_f { unsigned int natLearnedCnt; struct monNatMoveEntry_f { unsigned int move; unsigned char level; } *natLearned; unsigned int tmLearnedCnt; unsigned int *tmLearned; } movelist; struct monExperience_f { unsigned char type; } experience; struct monEvolution_f { unsigned char evolves; unsigned int mon; unsigned char level; } evolution; struct monAesthetics_f { unsigned int battleSprite; unsigned int icon; unsigned int body; unsigned int footprint; unsigned int cry; } aesthetics; struct monOthers_f { unsigned char species[13]; unsigned char genderless; unsigned char malePerc; unsigned int ability; unsigned char catchRate; unsigned int heldItem; } etc; } *id; _Bool shiny; struct stats_t { int hp; int mHp; } stats; struct moveMask_t { int cpp; int value; } moves[4]; int condition; } monsters[6]; struct charMover_t { int x; int nextX; int lastX; int animCycle; _Bool step; } mover; }


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

*(_DWORD *)a3 = a1;
*(_DWORD *)(a3 + 4) = a2;
*(_QWORD *)(a3 + 8) = malloc(a2 * a1);
result = malloc(a2 * a1);
*(_DWORD *)(a3 + 60) = 0x1000000;
*(_QWORD *)(a3 + 16) = result;
return result;
}

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

*(_DWORD *)a3 = a1;
*(_DWORD *)(a3 + 4) = a2;
*(_QWORD *)(a3 + 8) = malloc(a2 * a1);
result = malloc(a2 * a1);
*(_DWORD *)(a3 + 60) = 0x1000000;
*(_QWORD *)(a3 + 16) = result;
return result;
}

original_code:
void pk_initMap(int w, int h, map_t* map) {
 map->width = w;
 map->height = h;
 map->data = (char*)malloc(w*h*sizeof(char));
 map->cData = (char*)malloc(w*h*sizeof(char));
 map->mapAlloced = 1;
 map->doorAlloced = map->npcAlloced = map->tileColAlloced = 0;
}

canonical_original_code:
void pk_initMap(int w, int h, struct map_t *map) {
 map->width = w;
 map->height = h;
 map->data = (char *)malloc(w*h*sizeof(char));
 map->cData = (char *)malloc(w*h*sizeof(char));
 map->mapAlloced = 1;
 map->doorAlloced = map->npcAlloced = map->tileColAlloced = 0;
}

variable_types: w int

variable_types: h int

variable_types: struct map_t { int width; unsigned char *data; unsigned char *cData; unsigned char *tileColData; struct doorEntry_t { unsigned char dest; unsigned int x; unsigned int destX; unsigned int type; } *doorData; struct npcEntry_t { unsigned char value; unsigned int x; } *npcData; int doorCnt; _Bool doorAlloced; } *

user_defined_types: struct map_t { int width; unsigned char *data; unsigned char *cData; unsigned char *tileColData; struct doorEntry_t *doorData; struct npcEntry_t *npcData; int doorCnt; _Bool doorAlloced; }


original_code:
void pk_freeMap(map_t *map) {
 if(map->mapAlloced) {
  free(map->data);
  free(map->cData);
 }
 if(map->doorAlloced) {
  free(map->doorData);
 }
 if(map->npcAlloced) {
  free(map->npcData);
 }
 if(map->tileColAlloced) {
  free(map->tileColData);
 }
}

canonical_original_code:
void pk_freeMap(struct map_t *map) {
 if(map->mapAlloced) {
  free(map->data);
  free(map->cData);
 }
 if(map->doorAlloced) {
  free(map->doorData);
 }
 if(map->npcAlloced) {
  free(map->npcData);
 }
 if(map->tileColAlloced) {
  free(map->tileColData);
 }
}

user_defined_types: struct doorEntry_t { unsigned char dest; unsigned int x; unsigned int destX; unsigned int type; }

user_defined_types: struct npcEntry_t { unsigned char value; unsigned int x; }


original_code:
col_t pk_findCols(map_t map, int x, int y) {
 col_t out;
 out.left = 1; out.right = 1; out.up = 1; out.down = 1;
 if(x+1 >= map.width) {
  out.right = 0;
 } else if(x-1 < 0) {
  out.left = 0;
 }
 if(y+1 > map.height) {
  out.down = 0;
 } else if(y-1 < 0) {
  out.up = 0;
 }
 if(map.cData[x+((y-1)*map.width)] == 1) {
  out.up = 0;
 }
 if(map.cData[x+((y+1)*map.width)] == 1) {
  out.down = 0;
 }
 if(map.cData[x+1+(y*map.width)] == 1) {
  out.right = 0;
 }
 if(map.cData[x-1+(y*map.width)] == 1) {
  out.left = 0;
 }
 return out;
}

canonical_original_code:
struct col_t pk_findCols(struct map_t map, int x, int y) {
 struct col_t out;
 out.left = 1; out.right = 1; out.up = 1; out.down = 1;
 if(x+1 >= map.width) {
  out.right = 0;
 } else if(x-1 < 0) {
  out.left = 0;
 }
 if(y+1 > map.height) {
  out.down = 0;
 } else if(y-1 < 0) {
  out.up = 0;
 }
 if(map.cData[x+((y-1)*map.width)] == 1) {
  out.up = 0;
 }
 if(map.cData[x+((y+1)*map.width)] == 1) {
  out.down = 0;
 }
 if(map.cData[x+1+(y*map.width)] == 1) {
  out.right = 0;
 }
 if(map.cData[x-1+(y*map.width)] == 1) {
  out.left = 0;
 }
 return out;
}

variable_types: struct map_t { int width; unsigned char *data; unsigned char *cData; unsigned char *tileColData; struct doorEntry_t { unsigned char dest; unsigned int x; unsigned int destX; unsigned int type; } *doorData; struct npcEntry_t { unsigned char value; unsigned int x; } *npcData; int doorCnt; _Bool doorAlloced; }

variable_types: x int

variable_types: y int

variable_types:
out
struct col_t { _Bool left; }

return_type: struct col_t { _Bool left; }


decompiled_code:
__int64 __fastcall pk_isSolid(int a1, __int64 a2)
{
int v2; // edx
unsigned __int8 *v3; // rax
__int64 v4; // rcx

v2 = *(_DWORD *)(a2 + 56);
if ( v2 <= 0 )
return 1LL;
v3 = *(unsigned __int8 **)(a2 + 24);
v4 = (__int64)&v3[v2 - 1 + 1];
while ( *v3 != a1 )
{
if ( ++v3 == (unsigned __int8 *)v4 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2)
{
int v2; // edx
unsigned __int8 *v3; // rax
__int64 v4; // rcx

v2 = *(_DWORD *)(a2 + 56);
if ( v2 <= 0 )
return 1LL;
v3 = *(unsigned __int8 **)(a2 + 24);
v4 = (__int64)&v3[v2 - 1 + 1];
while ( *v3 != a1 )
{
if ( ++v3 == (unsigned __int8 *)v4 )
return 1LL;
}
return 0LL;
}

original_code:
_Bool pk_isSolid(int val, map_t* map) {
 for(int i=0; i<map->tileColCnt; i++) {
  if(val == map->tileColData[i]) {
   return 0;
  }
 }
 return 1;
}

canonical_original_code:
_Bool pk_isSolid(int val, struct map_t *map) {
 for(int i = 0; i<map->tileColCnt; i++) {
  if(val == map->tileColData[i]) {
   return 0;
  }
 }
 return 1;
}

variable_types: val int

variable_types: i int


decompiled_code:
__int64 __fastcall pk_buildColMapM(__int64 a1)
{
__int64 result; // rax
__int64 v2; // r8
char v3; // al
__int64 v4; // r8

result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
if ( (int)result > 0 )
{
v2 = 0LL;
do
{
v3 = pk_isSolid(*(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v2), a1);
*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) = v3;
v2 = v4 + 1;
result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
}
while ( (int)result > (int)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax
__int64 v2; // r8
char v3; // al
__int64 v4; // r8

result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
if ( (int)result > 0 )
{
v2 = 0LL;
do
{
v3 = func3(*(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v2), a1);
*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) = v3;
v2 = v4 + 1;
result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
}
while ( (int)result > (int)v2 );
}
return result;
}

original_code:
void pk_buildColMapM(map_t* map) {
 for(int i=0; i<map->width*map->height; i++) {
  map->cData[i] = pk_isSolid(map->data[i], map);
 }
}

canonical_original_code:
void pk_buildColMapM(struct map_t *map) {
 for(int i = 0; i<map->width*map->height; i++) {
  map->cData[i] = pk_isSolid(map->data[i], map);
 }
}


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

result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
if ( (int)result > 0 )
{
result = 0LL;
do
*(_BYTE *)(*(_QWORD *)(a1 + 16) + result++) = 0;
while ( *(_DWORD *)(a1 + 4) * *(_DWORD *)a1 > (int)result );
}
return result;
}

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

result = (unsigned int)(*(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
if ( (int)result > 0 )
{
result = 0LL;
do
*(_BYTE *)(*(_QWORD *)(a1 + 16) + result++) = 0;
while ( *(_DWORD *)(a1 + 4) * *(_DWORD *)a1 > (int)result );
}
return result;
}

original_code:
void pk_clearColMap(map_t* map) {
 for(int i=0; i<map->width*map->height; i++) {
  map->cData[i] = 0;
 }
}

canonical_original_code:
void pk_clearColMap(struct map_t *map) {
 for(int i = 0; i<map->width*map->height; i++) {
  map->cData[i] = 0;
 }
}


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

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

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

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

original_code:
void pk_setTileData(unsigned char *data, map_t* map) {
 for(int i=0; i<map->width*map->height; i++) {
  map->data[i] = data[i];
 }
}

canonical_original_code:
void pk_setTileData(unsigned char *data, struct map_t *map) {
 for(int i = 0; i<map->width*map->height; i++) {
  map->data[i] = data[i];
 }
}

variable_types:
data
unsigned char *