decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
const char **v4; // r12
int v5; // er15
int v6; // ebx
char v7; // al
bool v8; // zf
int v9; // eax
__int64 v11; // rcx
__int64 v12; // rcx
__int64 v13; // r15
char *v14; // rax
int v15; // er15
const char *v16; // rcx
char v17; // al
char v18; // dl
char *v19; // r15
unsigned __int8 *v20; // rax
int v21; // ecx
unsigned int v22; // ebx
unsigned __int8 *v23; // rcx
int v24; // eax
unsigned int v25; // ebx
char *s2; // [rsp+0h] [rbp-1058h]
char *s2a; // [rsp+0h] [rbp-1058h]
unsigned int s2b; // [rsp+0h] [rbp-1058h]
int s2c; // [rsp+0h] [rbp-1058h]
const char **v30; // [rsp+8h] [rbp-1050h]
__int64 v31; // [rsp+10h] [rbp-1048h]
unsigned int v32; // [rsp+18h] [rbp-1040h]
unsigned int v33; // [rsp+1Ch] [rbp-103Ch]
char dest[4152]; // [rsp+20h] [rbp-1038h] BYREF

DEXUXDEBUGMODE = 0;
dest[0] = 0;
if ( argc > 1 )
{
v4 = argv;
v33 = 0;
v32 = 0;
v5 = 1;
v6 = 1;
while ( 1 )
{
v11 = v6;
envp = (const char **)v4[v11];
if ( *(_BYTE *)envp != 45 )
goto LABEL_47;
v7 = *((_BYTE *)envp + 1);
if ( !v7 )
break;
if ( v7 != 68 )
{
if ( v7 == 100 )
{
if ( *((_BYTE *)envp + 2) )
{
argv = (const char **)((char *)envp + 2);
strncpy(dest, (const char *)envp + 2, 0x1000uLL);
}
else if ( argc != ++v6 )
{
argv = (const char **)v4[v11 + 1];
strncpy(dest, (const char *)argv, 0x1000uLL);
}
goto LABEL_45;
}
if ( v7 != 102 )
{
if ( v7 == 104 )
{
fprintf(stderr, "DEXUX client version %s\n", "0.5beta");
fprintf(stderr, "Written by %s\n", "William Ono and Ben Galliart");
fprintf(stderr, "Copyright %s\n", "2000");
fwrite("This program is covered by the General Public License\n\n", 1uLL, 0x37uLL, stderr);
fwrite("Command line options:\n", 1uLL, 0x16uLL, stderr);
fwrite(" -D -- debug messages (very messy)\n", 1uLL, 0x2FuLL, stderr);
fwrite(" -d [device] -- port that the card reader is connected\n", 1uLL, 0x3AuLL, stderr);
fwrite(" -f [format] -- format to save files blocks/cards which have been read\n", 1uLL, 0x4AuLL, stderr);
fwrite(" Formats available:\n", 1uLL, 0x17uLL, stderr);
fwrite(" Single game formats: psx, mcs, xpm\n", 1uLL, 0x28uLL, stderr);
fwrite(" NOTE: XPM is an image format\n", 1uLL, 0x21uLL, stderr);
fwrite(" Entire game card image formats: mcs, mc, ddf, vgs, gme\n\n", 1uLL, 0x3DuLL, stderr);
fwrite(" l[ist]\n", 1uLL, 9uLL, stderr);
fwrite(" r[ead] [block#] filename\n", 1uLL, 0x1BuLL, stderr);
fwrite(" w[rite] filename\n", 1uLL, 0x13uLL, stderr);
fwrite(" d[elete] block# [additional block#s]\n", 1uLL, 0x29uLL, stderr);
argv = (const char **)(main + 1);
fwrite(" u[ndelete] block# [addtional block#s]\n", 1uLL, 0x28uLL, stderr);
}
else
{
argv = (const char **)"Unknown option skipped: %s, -h to get help\n";
fprintf(stderr, "Unknown option skipped: %s, -h to get help\n", (const char *)envp);
}
goto LABEL_45;
}
if ( argc == ++v6 )
goto LABEL_45;
s2 = (char *)v4[v11 + 1];
argv = (const char **)s2;
if ( !strcmp("mcd", s2) )
{
v32 = 0;
}
else
{
argv = (const char **)s2;
if ( !strcmp("mc", s2) )
{
v32 = 0;
}
else
{
argv = (const char **)s2;
if ( !strcmp("ddf", s2) )
{
v32 = 0;
}
else
{
argv = (const char **)s2;
if ( !strcmp("vgs", s2) )
{
v32 = 1;
}
else
{
argv = (const char **)s2;
if ( !strcmp("gme", s2) )
{
v32 = 2;
}
else
{
argv = (const char **)s2;
if ( !strcmp("psx", s2) )
{
v33 = 0;
}
else
{
argv = (const char **)s2;
if ( !strcmp("mcs", s2) )
{
v33 = 1;
}
else
{
argv = (const char **)s2;
v8 = strcmp("xpm", s2) == 0;
v9 = 2;
if ( !v8 )
v9 = v33;
v33 = v9;
}
}
}
}
}
}
}
LABEL_47:
if ( v5 )
{
LABEL_48:
if ( DEXUXDEBUGMODE )
{
LABEL_10:
argv = (const char **)"dexuxopen(%s)\n";
fprintf(stderr, "dexuxopen(%s)\n", dest);
}
v3 = dexuxopen(dest);
if ( DEXUXDEBUGMODE )
{
argv = (const char **)(main + 1);
fwrite("dexuxwake()\n", 1uLL, 0xCuLL, stderr);
}
if ( (unsigned int)dexuxwake(v3) )
{
fwrite("Fatal error: Could not find the DexDrive\n", 1uLL, 0x29uLL, stderr);
return 1;
}
if ( DEXUXDEBUGMODE )
{
argv = (const char **)(main + 1);
fwrite("dexuxstatus()\n", 1uLL, 0xEuLL, stderr);
}
if ( (unsigned int)dexuxstatus(v3) )
{
argv = (const char **)(main + 1);
fwrite("Please insert memory card\n", 1uLL, 0x1AuLL, stderr);
while ( (unsigned int)dexuxstatus(v3) )
usleep(0xC350u);
}
}
v12 = v6;
v13 = v6;
v14 = (char *)&v4[v13];
envp = (const char **)*(unsigned __int8 *)v4[v13];
if ( (_BYTE)envp == 108 )
{
if ( DEXUXDEBUGMODE )
{
LABEL_118:
argv = (const char **)(main + 1);
fwrite("dexlist()\n", 1uLL, 0xAuLL, stderr);
}
dexlist(v3);
v5 = 0;
goto LABEL_45;
}
goto LABEL_56;
}
DEXUXDEBUGMODE = 1;
if ( v5 )
goto LABEL_10;
v12 = v6;
v13 = v6;
v14 = (char *)&v4[v13];
envp = (const char **)*(unsigned __int8 *)v4[v13];
if ( (_BYTE)envp == 108 )
goto LABEL_118;
LABEL_56:
if ( (_BYTE)envp == 119 )
{
if ( argc == ++v6 )
{
fwrite("Please specify save image filename to", 1uLL, 0x25uLL, stderr);
argv = (const char **)(main + 1);
fwrite(" be written to card\n", 1uLL, 0x14uLL, stderr);
v5 = 0;
}
else
{
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexwrite(%s)\n", v4[v13 + 1]);
argv = (const char **)v4[v13 + 1];
dexwrite(v3, argv);
v5 = 0;
}
goto LABEL_45;
}
if ( (_BYTE)envp != 114 )
{
if ( (_BYTE)envp == 100 )
{
v19 = v14;
for ( s2c = v6 + 1; ; ++s2c )
{
v6 = s2c - 1;
v20 = (unsigned __int8 *)*((_QWORD *)v19 + 1);
if ( !v20 )
{
v5 = 0;
goto LABEL_45;
}
envp = (const char **)*v20;
if ( (unsigned __int8)((_BYTE)envp - 49) > 8u )
{
v5 = 0;
goto LABEL_45;
}
v21 = v20[1];
if ( (_BYTE)v21 )
{
argv = (const char **)(unsigned int)(v21 - 48);
if ( (unsigned __int8)(v21 - 48) > 5u || (_BYTE)envp != 49 )
{
v5 = 0;
goto LABEL_45;
}
if ( v20[2] )
{
v5 = 0;
goto LABEL_45;
}
v22 = (char)v21 - 38;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexdelete(%d, 0)\n", v22);
}
else
{
v22 = (char)envp - 48;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexdelete(%d, 0)\n", v22);
}
argv = (const char **)v22;
dexdelete(v3, v22, 0LL);
v19 += 8;
}
}
v5 = 0;
if ( (_BYTE)envp != 117 )
goto LABEL_45;
envp = (const char **)(unsigned int)(v6 + 1);
if ( argc != (_DWORD)envp )
{
v15 = v6;
s2a = v14;
v31 = (__int64)&(&v4[argc - 2 - v6])[v12 + 1];
while ( 1 )
{
v6 = v15++;
v23 = (unsigned __int8 *)*((_QWORD *)s2a + 1);
v24 = *v23;
envp = (const char **)(unsigned int)(v24 - 49);
if ( (unsigned __int8)(v24 - 49) > 8u )
{
v5 = 0;
goto LABEL_45;
}
envp = (const char **)v23[1];
if ( (_BYTE)envp )
{
argv = (const char **)(unsigned int)((_DWORD)envp - 48);
if ( (unsigned __int8)((_BYTE)envp - 48) > 5u || (_BYTE)v24 != 49 )
{
v5 = 0;
goto LABEL_45;
}
if ( v23[2] )
{
v5 = 0;
goto LABEL_45;
}
v25 = (char)envp - 38;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexdelete(%d, 1)\n", v25);
}
else
{
v25 = (char)v24 - 48;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexdelete(%d, 1)\n", v25);
}
argv = (const char **)v25;
dexdelete(v3, v25, 1LL);
s2a += 8;
if ( s2a == (char *)v31 )
{
v6 = v15;
v5 = 0;
goto LABEL_45;
}
}
}
LABEL_112:
if ( DEXUXDEBUGMODE )
{
argv = (const char **)(main + 1);
fwrite("dexuxclose()\n", 1uLL, 0xDuLL, stderr);
}
goto LABEL_114;
}
if ( argc == v6 + 1 )
{
fwrite("Please specify filename to save the", 1uLL, 0x23uLL, stderr);
argv = (const char **)(main + 1);
fwrite(" image too\n", 1uLL, 0xBuLL, stderr);
++v6;
v5 = 0;
goto LABEL_45;
}
v30 = &v4[v13 + 1];
v16 = *v30;
v17 = **v30;
if ( (unsigned __int8)(v17 - 49) > 8u )
{
LABEL_74:
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexreadfull(%d, %s)\n", v32, v16);
argv = (const char **)v32;
dexreadfull(v3, v32, *v30);
++v6;
v5 = 0;
goto LABEL_45;
}
v18 = v16[1];
if ( v18 )
{
if ( (unsigned __int8)(v18 - 48) > 5u || v17 != 49 || v16[2] )
goto LABEL_74;
s2b = v18 - 38;
}
else
{
s2b = v17 - 48;
}
v6 += 2;
if ( argc == v6 )
{
fwrite("Please specify filename to save the", 1uLL, 0x23uLL, stderr);
argv = (const char **)(main + 1);
fwrite(" image too\n", 1uLL, 0xBuLL, stderr);
v5 = 0;
}
else
{
if ( DEXUXDEBUGMODE )
fprintf(stderr, "dexreadsingle(%d, %d, %s)\n", v33, s2b, v4[v13 + 2]);
argv = (const char **)v33;
dexreadsingle(v3, v33, s2b, v4[v13 + 2]);
v5 = 0;
}
LABEL_45:
if ( argc <= ++v6 )
goto LABEL_112;
}
if ( !v5 )
goto LABEL_45;
goto LABEL_48;
}
LABEL_114:
dexuxclose(v3, argv, envp);
return 0;
}

original_code:
int main (int argc, char **argv) {
  char devport[4096];
  int argcnt, dexuxfd;
  int dexclosed = 1, fullsaveformat = 0, singlesaveformat = 0;
  DEXUXDEBUGMODE = 0;
  devport[0] = 0;
  for (argcnt = 1; argcnt < argc; argcnt++) {
    if ((argv[argcnt][0] == '-') && (argv[argcnt][1] != '\0')) {
      if (argv[argcnt][1] == 'D') {
        DEXUXDEBUGMODE = 1;
      } else if (argv[argcnt][1] == 'd') {
        if (argv[argcnt][2] == '\0') {
          argcnt++;
          if (argcnt == argc) continue;
          strncpy(devport, argv[argcnt], 4096);
          continue;
        } else {
          char *pargv;
          pargv = argv[argcnt];
          pargv++; pargv++;
          strncpy(devport, pargv, 4096);
          continue;
        }
      } else if (argv[argcnt][1] == 'f') {
        argcnt++;
        if (argcnt == argc) continue;
        if (strcmp("mcd", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("mc", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("ddf", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("vgs", argv[argcnt]) == 0) {
          fullsaveformat=1;
        } else if (strcmp("gme", argv[argcnt]) == 0) {
          fullsaveformat=2;
        } else if (strcmp("psx", argv[argcnt]) == 0) {
          singlesaveformat=0;
        } else if (strcmp("mcs", argv[argcnt]) == 0) {
          singlesaveformat=1;
        } else if (strcmp("xpm", argv[argcnt]) == 0) {
          singlesaveformat=2;
        }
      } else if (argv[argcnt][1] == 'h') {
        fprintf(stderr, "DEXUX client version %s\n", "0.5beta");
        fprintf(stderr, "Written by %s\n", "William Ono and Ben Galliart");
        fprintf(stderr, "Copyright %s\n", "2000");
        fprintf(stderr, "This program is covered by the General Public License\n\n");
 fprintf(stderr, "Command line options:\n");
        fprintf(stderr, "  -D            -- debug messages (very messy)\n");
        fprintf(stderr, "  -d [device]   -- port that the card reader is connected\n");
 fprintf(stderr, "  -f [format]   -- format to save files blocks/cards which have been read\n");
        fprintf(stderr, "    Formats available:\n");
        fprintf(stderr, "    Single game formats:  psx, mcs, xpm\n");
        fprintf(stderr, "    NOTE: XPM is an image format\n");
        fprintf(stderr, "    Entire game card image formats:  mcs, mc, ddf, vgs, gme\n\n");
        fprintf(stderr, "  l[ist]\n");
        fprintf(stderr, "  r[ead] [block#] filename\n");
        fprintf(stderr, "  w[rite] filename\n");
        fprintf(stderr, "  d[elete]   block# [additional block#s]\n");
        fprintf(stderr, "  u[ndelete] block# [addtional block#s]\n");
        continue;
      } else {
        fprintf(stderr, "Unknown option skipped: %s, -h to get help\n", argv[argcnt]);
        continue;
      }
    }
    if (dexclosed) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxopen(%s)\n", devport);
        dexuxfd = dexuxopen(devport);
        if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxwake()\n");
        if (dexuxwake(dexuxfd)) {
          fprintf(stderr, "Fatal error: Could not find the DexDrive\n");
          return(1);
        }
        if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxstatus()\n");
        if (dexuxstatus(dexuxfd)) {
          fprintf(stderr, "Please insert memory card\n");
          while (dexuxstatus(dexuxfd)) { usleep(50000); }
        }
 dexclosed = 0;
    }
    if (argv[argcnt][0] == 'l') {
      if (DEXUXDEBUGMODE) fprintf(stderr, "dexlist()\n");
      dexlist(dexuxfd);
      continue;
    } else if (argv[argcnt][0] == 'w') {
      argcnt++;
      if (argcnt == argc) {
        fprintf(stderr, "Please specify save image filename to");
        fprintf(stderr, " be written to card\n");
        continue;
      }
      if (DEXUXDEBUGMODE) fprintf(stderr, "dexwrite(%s)\n", argv[argcnt]);
      dexwrite(dexuxfd, argv[argcnt]);
      continue;
    } else if (argv[argcnt][0] == 'r') {
      int blocknum = 0;
      argcnt++;
      if (argcnt == argc) {
        fprintf(stderr, "Please specify filename to save the");
        fprintf(stderr, " image too\n");
        continue;
      }
      if ((argv[argcnt][0] >= '1') && (argv[argcnt][0] <= '9')) {
        if (argv[argcnt][1] == '\0') {
          blocknum = argv[argcnt][0] - '0';
        } else if ((argv[argcnt][0] == '1') &&
                   (argv[argcnt][1] >= '0') &&
                   (argv[argcnt][1] <= '5') &&
                   (argv[argcnt][2] == '\0')) {
          blocknum = 10 + argv[argcnt][1] - '0';
        }
      }
      if (blocknum == 0) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexreadfull(%d, %s)\n",
   fullsaveformat, argv[argcnt]);
        dexreadfull(dexuxfd, fullsaveformat, argv[argcnt]);
      } else {
        argcnt++;
        if (argcnt == argc) {
          fprintf(stderr, "Please specify filename to save the");
          fprintf(stderr, " image too\n");
          continue;
        }
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexreadsingle(%d, %d, %s)\n",
          singlesaveformat, blocknum, argv[argcnt]);
        dexreadsingle(dexuxfd, singlesaveformat, blocknum, argv[argcnt]);
      }
      continue;
    } else if (argv[argcnt][0] == 'd') {
      int blocknum = 16;
      while ((blocknum > 0) && (argv[argcnt + 1] != ((void *)0))) {
        if ((argv[argcnt + 1][0] >= '1') &&
            (argv[argcnt + 1][0] <= '9')) {
          if (argv[argcnt + 1][1] == '\0') {
            argcnt++;
            blocknum = argv[argcnt][0] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 0)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 0);
          } else if ((argv[argcnt + 1][0] == '1') &&
                     (argv[argcnt + 1][1] >= '0') &&
                     (argv[argcnt + 1][1] <= '5') &&
                     (argv[argcnt + 1][2] == '\0')) {
            argcnt++;
            blocknum = 10 + argv[argcnt][1] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 0)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 0);
          } else {
            blocknum = 0;
          }
        } else blocknum = 0;
      }
      continue;
    } else if (argv[argcnt][0] == 'u') {
      int blocknum = 16;
      while ((blocknum > 0) && (argcnt + 1 != argc)) {
        if ((argv[argcnt + 1][0] >= '1') &&
            (argv[argcnt + 1][0] <= '9')) {
          if (argv[argcnt + 1][1] == '\0') {
            argcnt++;
            blocknum = argv[argcnt][0] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 1)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 1);
          } else if ((argv[argcnt + 1][0] == '1') &&
                     (argv[argcnt + 1][1] >= '0') &&
                     (argv[argcnt + 1][1] <= '5') &&
                     (argv[argcnt + 1][2] == '\0')) {
            argcnt++;
            blocknum = 10 + argv[argcnt][1] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 1)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 1);
          } else {
            blocknum = 0;
          }
        } else blocknum = 0;
      }
      continue;
    }
  }
  if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxclose()\n");
  dexuxclose(dexuxfd);
  return(0);
}

canonical_original_code:
int main(int argc, char **argv) {
  char devport[4096];
  int argcnt; int dexuxfd;
  int dexclosed = 1; int fullsaveformat = 0; int singlesaveformat = 0;
  DEXUXDEBUGMODE = 0;
  devport[0] = 0;
  for (argcnt = 1; argcnt < argc; argcnt++) {
    if ((argv[argcnt][0] == '-') && (argv[argcnt][1] != '\0')) {
      if (argv[argcnt][1] == 'D') {
        DEXUXDEBUGMODE = 1;
      } else if (argv[argcnt][1] == 'd') {
        if (argv[argcnt][2] == '\0') {
          argcnt++;
          if (argcnt == argc) continue;
          strncpy(devport, argv[argcnt], 4096);
          continue;
        } else {
          char *pargv;
          pargv = argv[argcnt];
          pargv++; pargv++;
          strncpy(devport, pargv, 4096);
          continue;
        }
      } else if (argv[argcnt][1] == 'f') {
        argcnt++;
        if (argcnt == argc) continue;
        if (strcmp("mcd", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("mc", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("ddf", argv[argcnt]) == 0) {
          fullsaveformat=0;
        } else if (strcmp("vgs", argv[argcnt]) == 0) {
          fullsaveformat=1;
        } else if (strcmp("gme", argv[argcnt]) == 0) {
          fullsaveformat=2;
        } else if (strcmp("psx", argv[argcnt]) == 0) {
          singlesaveformat=0;
        } else if (strcmp("mcs", argv[argcnt]) == 0) {
          singlesaveformat=1;
        } else if (strcmp("xpm", argv[argcnt]) == 0) {
          singlesaveformat=2;
        }
      } else if (argv[argcnt][1] == 'h') {
        fprintf(stderr, "DEXUX client version %s\n", "0.5beta");
        fprintf(stderr, "Written by %s\n", "William Ono and Ben Galliart");
        fprintf(stderr, "Copyright %s\n", "2000");
        fprintf(stderr, "This program is covered by the General Public License\n\n");
 fprintf(stderr, "Command line options:\n");
        fprintf(stderr, "  -D            -- debug messages (very messy)\n");
        fprintf(stderr, "  -d [device]   -- port that the card reader is connected\n");
 fprintf(stderr, "  -f [format]   -- format to save files blocks/cards which have been read\n");
        fprintf(stderr, "    Formats available:\n");
        fprintf(stderr, "    Single game formats:  psx, mcs, xpm\n");
        fprintf(stderr, "    NOTE: XPM is an image format\n");
        fprintf(stderr, "    Entire game card image formats:  mcs, mc, ddf, vgs, gme\n\n");
        fprintf(stderr, "  l[ist]\n");
        fprintf(stderr, "  r[ead] [block#] filename\n");
        fprintf(stderr, "  w[rite] filename\n");
        fprintf(stderr, "  d[elete]   block# [additional block#s]\n");
        fprintf(stderr, "  u[ndelete] block# [addtional block#s]\n");
        continue;
      } else {
        fprintf(stderr, "Unknown option skipped: %s, -h to get help\n", argv[argcnt]);
        continue;
      }
    }
    if (dexclosed) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxopen(%s)\n", devport);
        dexuxfd = dexuxopen(devport);
        if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxwake()\n");
        if (dexuxwake(dexuxfd)) {
          fprintf(stderr, "Fatal error: Could not find the DexDrive\n");
          return(1);
        }
        if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxstatus()\n");
        if (dexuxstatus(dexuxfd)) {
          fprintf(stderr, "Please insert memory card\n");
          while (dexuxstatus(dexuxfd)) { usleep(50000); }
        }
 dexclosed = 0;
    }
    if (argv[argcnt][0] == 'l') {
      if (DEXUXDEBUGMODE) fprintf(stderr, "dexlist()\n");
      dexlist(dexuxfd);
      continue;
    } else if (argv[argcnt][0] == 'w') {
      argcnt++;
      if (argcnt == argc) {
        fprintf(stderr, "Please specify save image filename to");
        fprintf(stderr, " be written to card\n");
        continue;
      }
      if (DEXUXDEBUGMODE) fprintf(stderr, "dexwrite(%s)\n", argv[argcnt]);
      dexwrite(dexuxfd, argv[argcnt]);
      continue;
    } else if (argv[argcnt][0] == 'r') {
      int blocknum = 0;
      argcnt++;
      if (argcnt == argc) {
        fprintf(stderr, "Please specify filename to save the");
        fprintf(stderr, " image too\n");
        continue;
      }
      if ((argv[argcnt][0] >= '1') && (argv[argcnt][0] <= '9')) {
        if (argv[argcnt][1] == '\0') {
          blocknum = argv[argcnt][0] - '0';
        } else if ((argv[argcnt][0] == '1') &&
                   (argv[argcnt][1] >= '0') &&
                   (argv[argcnt][1] <= '5') &&
                   (argv[argcnt][2] == '\0')) {
          blocknum = 10 + argv[argcnt][1] - '0';
        }
      }
      if (blocknum == 0) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexreadfull(%d, %s)\n",
   fullsaveformat, argv[argcnt]);
        dexreadfull(dexuxfd, fullsaveformat, argv[argcnt]);
      } else {
        argcnt++;
        if (argcnt == argc) {
          fprintf(stderr, "Please specify filename to save the");
          fprintf(stderr, " image too\n");
          continue;
        }
 if (DEXUXDEBUGMODE) fprintf(stderr, "dexreadsingle(%d, %d, %s)\n",
          singlesaveformat, blocknum, argv[argcnt]);
        dexreadsingle(dexuxfd, singlesaveformat, blocknum, argv[argcnt]);
      }
      continue;
    } else if (argv[argcnt][0] == 'd') {
      int blocknum = 16;
      while ((blocknum > 0) && (argv[argcnt + 1] != ((void *)0))) {
        if ((argv[argcnt + 1][0] >= '1') &&
            (argv[argcnt + 1][0] <= '9')) {
          if (argv[argcnt + 1][1] == '\0') {
            argcnt++;
            blocknum = argv[argcnt][0] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 0)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 0);
          } else if ((argv[argcnt + 1][0] == '1') &&
                     (argv[argcnt + 1][1] >= '0') &&
                     (argv[argcnt + 1][1] <= '5') &&
                     (argv[argcnt + 1][2] == '\0')) {
            argcnt++;
            blocknum = 10 + argv[argcnt][1] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 0)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 0);
          } else {
            blocknum = 0;
          }
        } else blocknum = 0;
      }
      continue;
    } else if (argv[argcnt][0] == 'u') {
      int blocknum = 16;
      while ((blocknum > 0) && (argcnt + 1 != argc)) {
        if ((argv[argcnt + 1][0] >= '1') &&
            (argv[argcnt + 1][0] <= '9')) {
          if (argv[argcnt + 1][1] == '\0') {
            argcnt++;
            blocknum = argv[argcnt][0] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 1)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 1);
          } else if ((argv[argcnt + 1][0] == '1') &&
                     (argv[argcnt + 1][1] >= '0') &&
                     (argv[argcnt + 1][1] <= '5') &&
                     (argv[argcnt + 1][2] == '\0')) {
            argcnt++;
            blocknum = 10 + argv[argcnt][1] - '0';
     if (DEXUXDEBUGMODE) fprintf(stderr, "dexdelete(%d, 1)\n", blocknum);
            dexdelete(dexuxfd, blocknum, 1);
          } else {
            blocknum = 0;
          }
        } else blocknum = 0;
      }
      continue;
    }
  }
  if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxclose()\n");
  dexuxclose(dexuxfd);
  return(0);
}

variable_types:
argv
char **

variable_types:
devport
char [4096]

variable_types: argcnt int

variable_types: dexuxfd int

variable_types: dexclosed int

variable_types: fullsaveformat int

variable_types: singlesaveformat int

variable_types:
pargv
char *

variable_types: blocknum int


dexuxbinreverse

original_code:
int dexuxbinreverse(int inbyte) {
  int outbyte;
  outbyte = (inbyte & 1) << 7;
  outbyte = outbyte + ((inbyte & 2) << 5);
  outbyte = outbyte + ((inbyte & 4) << 3);
  outbyte = outbyte + ((inbyte & 8) << 1);
  outbyte = outbyte + ((inbyte & 16) >> 1);
  outbyte = outbyte + ((inbyte & 32) >> 3);
  outbyte = outbyte + ((inbyte & 64) >> 5);
  outbyte = outbyte + ((inbyte & 128) >> 7);
  return(outbyte);
}

variable_types: inbyte int

variable_types: outbyte int

return_type: int


dexuxwake

decompiled_code:
_BOOL8 __fastcall dexuxwake(int fd)
{
_BOOL4 v2; // ebp
int v4; // ebp
bool v5; // zf
char v6[8]; // [rsp+0h] [rbp-118h] BYREF
char v7; // [rsp+8h] [rbp-110h]

write(fd, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0x20uLL);
v6[(int)read(fd, v6, 4uLL)] = 0;
v2 = 1;
if ( !memcmp(v6, "IAI!", 4uLL) )
{
write(fd, &unk_254, 0x15uLL);
v6[(int)read(fd, v6, 9uLL)] = 0;
if ( DEXUXDEBUGMODE )
fprintf(
stderr,
"dexuxwake(): version responce: %.2x %.2x %.2x %.2x %.2x %.2x (firmware version 1.%d)\n",
(unsigned int)v6[3],
(unsigned int)v6[4],
(unsigned int)v6[5],
(unsigned int)v6[6],
(unsigned int)v6[7],
(unsigned int)v7,
(unsigned int)(v7 - 58));
v2 = 1;
if ( !memcmp(v6, "IAI@\x1BPSXF", 8uLL) )
{
v4 = 10;
do
{
write(fd, "IAI'", 4uLL);
tcdrain(fd);
usleep(0x3E8u);
dexuxclear((unsigned int)fd);
--v4;
}
while ( v4 );
write(fd, &unk_24A, 5uLL);
v6[(int)read(fd, v6, 0xFEuLL)] = 0;
v5 = memcmp(v6, &unk_250, 3uLL) == 0;
v2 = !v5;
if ( v5 )
dexuxclear((unsigned int)fd);
else
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int fd)
{
_BOOL4 v2; // ebp
int v4; // ebp
bool v5; // zf
char v6[8]; // [rsp+0h] [rbp-118h] BYREF
char v7; // [rsp+8h] [rbp-110h]

write(fd, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0x20uLL);
v6[(int)read(fd, v6, 4uLL)] = 0;
v2 = 1;
if ( !memcmp(v6, "IAI!", 4uLL) )
{
write(fd, &unk_254, 0x15uLL);
v6[(int)read(fd, v6, 9uLL)] = 0;
if ( DEXUXDEBUGMODE )
fprintf(
stderr,
"dexuxwake(): version responce: %.2x %.2x %.2x %.2x %.2x %.2x (firmware version 1.%d)\n",
(unsigned int)v6[3],
(unsigned int)v6[4],
(unsigned int)v6[5],
(unsigned int)v6[6],
(unsigned int)v6[7],
(unsigned int)v7,
(unsigned int)(v7 - 58));
v2 = 1;
if ( !memcmp(v6, "IAI@\x1BPSXF", 8uLL) )
{
v4 = 10;
do
{
write(fd, "IAI'", 4uLL);
tcdrain(fd);
usleep(0x3E8u);
dexuxclear((unsigned int)fd);
--v4;
}
while ( v4 );
write(fd, &unk_24A, 5uLL);
v6[(int)read(fd, v6, 0xFEuLL)] = 0;
v5 = memcmp(v6, &unk_250, 3uLL) == 0;
v2 = !v5;
if ( v5 )
dexuxclear((unsigned int)fd);
else
v2 = 1;
}
}
return v2;
}

original_code:
int dexuxwake(int fd) {
  char buffer[255];
  int readlength;
  int i;
  write(fd, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 32);
  readlength = read(fd, &buffer, 4);
  buffer[readlength]='\0';
  if (strncmp(buffer, "IAI!", 4)) {
          return(1);
  }
  write(fd, "IAI\x00\x10\x29\x23\xBE\x84\xE1\x6C\xD6\xAE\x52\x90\x49\xF1\xF1\xBB\xE9\xEB", 21);
  readlength = read(fd, &buffer, 9);
  buffer[readlength]='\0';
  if (DEXUXDEBUGMODE) fprintf(stderr, "dexuxwake(): version responce: %.2x %.2x %.2x %.2x %.2x %.2x (firmware version 1.%d)\n", buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[8] - 58);
  if (strncmp(buffer, "IAI\x40\x1B\x50\x53\x58\x46",8)) {
          return(1);
  }
  for (i=0; i<10; i++) {
    write(fd, "IAI\x27",4);
    tcdrain(fd);
    usleep(1000);
    dexuxclear(fd);
  }
  write(fd, "IAI\x07\x01",5);
  readlength = read(fd, &buffer, 254);
  buffer[readlength]='\0';
  if (strncmp(buffer, "IAI",3)) {
          return(1);
  }
  dexuxclear(fd);
  return(0);
}

variable_types: fd int

variable_types:
buffer
char [255]

variable_types: readlength int

variable_types: i int


dexwrite

decompiled_code:
__int64 __fastcall dexwrite(unsigned int a1, const char *a2)
{
unsigned int v2; // ebx
char v4[262149]; // [rsp+0h] [rbp-40028h] BYREF
char v5[3]; // [rsp+40005h] [rbp-23h] BYREF
unsigned int v6; // [rsp+40008h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+4000Ch] [rbp-1Ch] BYREF

v7[0] = 0;
v6 = 0;
v2 = 1;
if ( !(unsigned int)dexreadfile(a2, v4, v5, &v6, v7) )
{
v2 = 0;
if ( DEXUXDEBUGMODE )
fprintf(stderr, " dexreadfile -> %s, %s, %u, %u, %u\n", a2, v5, v6, v4, v7[0]);
if ( v6 == 16 )
dexwritefull(a1, a2, v5, &v4[v7[0]]);
else
dexwritesingle(a1, a2, v5, v4);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, const char *a2)
{
unsigned int v2; // ebx
char v4[262149]; // [rsp+0h] [rbp-40028h] BYREF
char v5[3]; // [rsp+40005h] [rbp-23h] BYREF
unsigned int v6; // [rsp+40008h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+4000Ch] [rbp-1Ch] BYREF

v7[0] = 0;
v6 = 0;
v2 = 1;
if ( !(unsigned int)dexreadfile(a2, v4, v5, &v6, v7) )
{
v2 = 0;
if ( DEXUXDEBUGMODE )
fprintf(stderr, " dexreadfile -> %s, %s, %u, %u, %u\n", a2, v5, v6, v4, v7[0]);
if ( v6 == 16 )
dexwritefull(a1, a2, v5, &v4[v7[0]]);
else
dexwritesingle(a1, a2, v5, v4);
}
return v2;
}

original_code:
int dexwrite(int dexuxfd, char *fname) {
  int offset=0, imagesize=0;
  char fileformat[3];
  unsigned char inputbuffer[262145], *pblock;
  if (dexreadfile(fname, inputbuffer, fileformat, &imagesize, &offset)) {
    return(1);
  }
  if (DEXUXDEBUGMODE) fprintf(stderr, "  dexreadfile -> %s, %s, %u, %u, %u\n", fname, fileformat, imagesize, inputbuffer, offset);
  if (imagesize == 16) {
    pblock = inputbuffer + offset;
    dexwritefull(dexuxfd, fname, fileformat, pblock);
  } else {
    dexwritesingle(dexuxfd, fname, fileformat, inputbuffer, imagesize);
  }
  return(0);
}

canonical_original_code:
int dexwrite(int dexuxfd, char *fname) {
  int offset = 0; int imagesize = 0;
  char fileformat[3];
  unsigned char inputbuffer[262145]; unsigned char *pblock;
  if (dexreadfile(fname, inputbuffer, fileformat, &imagesize, &offset)) {
    return(1);
  }
  if (DEXUXDEBUGMODE) fprintf(stderr, "  dexreadfile -> %s, %s, %u, %u, %u\n", fname, fileformat, imagesize, inputbuffer, offset);
  if (imagesize == 16) {
    pblock = inputbuffer + offset;
    dexwritefull(dexuxfd, fname, fileformat, pblock);
  } else {
    dexwritesingle(dexuxfd, fname, fileformat, inputbuffer, imagesize);
  }
  return(0);
}

variable_types:
fname
char *

variable_types: offset int

variable_types: imagesize int

variable_types:
fileformat
char [3]

variable_types:
inputbuffer
unsigned char [262145]

variable_types:
pblock
unsigned char *


dexuxclear

decompiled_code:
__int64 __fastcall dexuxclear(int a1)
{
tcflush(a1, 0);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
tcflush(a1, 0);
return 0LL;
}

original_code:
int dexuxclear(int fd) {
  tcflush(fd, 0);
  return(0);
}


dexuxputblock

decompiled_code:
__int64 __fastcall dexuxputblock(unsigned int a1, int a2, __int64 a3)
{
__int64 v3; // rbp
int i; // er12
__int64 j; // rax
char v8[142]; // [rsp+0h] [rbp-B8h] BYREF
char v9[42]; // [rsp+8Eh] [rbp-2Ah] BYREF

v3 = a2 << 6;
for ( i = 0; i != 0x2000; i += 128 )
{
v9[0] = dexuxframetop(v3);
v9[1] = dexuxframebottom(v3);
for ( j = 0LL; j != 128; ++j )
v8[j] = *(_BYTE *)(a3 + j);
dexuxputframe(a1, v9, v8);
++v3;
a3 += 128LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, int a2, __int64 a3)
{
__int64 v3; // rbp
int i; // er12
__int64 j; // rax
char v8[142]; // [rsp+0h] [rbp-B8h] BYREF
char v9[42]; // [rsp+8Eh] [rbp-2Ah] BYREF

v3 = a2 << 6;
for ( i = 0; i != 0x2000; i += 128 )
{
v9[0] = dexuxframetop(v3);
v9[1] = dexuxframebottom(v3);
for ( j = 0LL; j != 128; ++j )
v8[j] = *(_BYTE *)(a3 + j);
dexuxputframe(a1, v9, v8);
++v3;
a3 += 128LL;
}
return 0LL;
}

original_code:
int dexuxputblock(int fd, int blocknumber, char *blockdata) {
  char frametoput[2], *pframetoput, framedata[129], *pframedata;
  long frameaddress;
  int framenum, charnum;
  pframetoput = frametoput;
  pframedata = framedata;
  for (framenum = 0; framenum <= 63; framenum++) {
    frameaddress = (blocknumber * 0x40) + framenum;
    *(pframetoput + 0) = dexuxframetop(frameaddress);
    *(pframetoput + 1) = dexuxframebottom(frameaddress);
    for (charnum = 0; charnum <= 127; charnum++) {
      *(pframedata + charnum) = blockdata[ (framenum * 128) + charnum ];
    }
    dexuxputframe(fd, frametoput, framedata);
  }
  return(0);
}

canonical_original_code:
int dexuxputblock(int fd, int blocknumber, char *blockdata) {
  char frametoput[2]; char *pframetoput; char framedata[129]; char *pframedata;
  long frameaddress;
  int framenum; int charnum;
  pframetoput = frametoput;
  pframedata = framedata;
  for (framenum = 0; framenum <= 63; framenum++) {
    frameaddress = (blocknumber * 0x40) + framenum;
    *(pframetoput + 0) = dexuxframetop(frameaddress);
    *(pframetoput + 1) = dexuxframebottom(frameaddress);
    for (charnum = 0; charnum <= 127; charnum++) {
      *(pframedata + charnum) = blockdata[ (framenum * 128) + charnum ];
    }
    dexuxputframe(fd, frametoput, framedata);
  }
  return(0);
}

variable_types: blocknumber int

variable_types:
blockdata
char *

variable_types:
frametoput
char [2]

variable_types:
pframetoput
char *

variable_types:
framedata
char [129]

variable_types:
pframedata
char *

variable_types: frameaddress long

variable_types: framenum int

variable_types: charnum int


dexuxjistoascii

original_code:
int dexuxjistoascii(unsigned char *shiftjis, unsigned char *ascii) {
  unsigned char *pascii;
  int i, asciipos = 0;
  pascii = ascii;
  memset(ascii, 0, 65);
  for (i = 0; i <= 63; i++) {
    if (shiftjis[i] > 0x00 && shiftjis[i] <= 0x7F) {
      *(pascii+asciipos++) = shiftjis[i];
    } else if (shiftjis[i] >= 0xA0 && shiftjis[i] <= 0xDF) {
      *(pascii+asciipos++) = '.';
    } else if (shiftjis[i] == 0x81) {
      switch(shiftjis[++i]) {
          case 0x40: *(pascii+asciipos++) = ' '; break;
          case 0x43: *(pascii+asciipos++) = ','; break;
          case 0x44: *(pascii+asciipos++) = '.'; break;
          case 0x46: *(pascii+asciipos++) = ':'; break;
          case 0x47: *(pascii+asciipos++) = ';'; break;
          case 0x48: *(pascii+asciipos++) = '?'; break;
          case 0x49: *(pascii+asciipos++) = '!'; break;
          case 0x5E: *(pascii+asciipos++) = '/'; break;
          case 0x6D: *(pascii+asciipos++) = '['; break;
          case 0x6E: *(pascii+asciipos++) = ']'; break;
          default: *(pascii+asciipos++) = '.'; break;
      }
    } else if (shiftjis[i] == 0x82) {
      i++;
      if (shiftjis[i] < 0x40) {
          *(pascii+asciipos++) = '!';
      } else if (shiftjis[i] >= 0x40 && shiftjis[i] <= 0x4E) {
          *(pascii+asciipos++) = '.';
      } else if (shiftjis[i] >= 0x4F && shiftjis[i] <= 0x58) {
          *(pascii+asciipos++) = shiftjis[i] - 0x1F;
      } else if (shiftjis[i] >= 0x60 && shiftjis[i] <= 0x79) {
          *(pascii+asciipos++) = shiftjis[i] - 0x1F;
      } else if (shiftjis[i] >= 0x7A && shiftjis[i] <= 0x7E) {
          *(pascii+asciipos++) = '.';
      } else if (shiftjis[i] == 0x7F) {
          *(pascii+asciipos++) = '!';
      }
      else if (shiftjis[i] >= 0x81 && shiftjis[i] <= 0xA6)
          *(pascii+asciipos++) = shiftjis[i] - 0x20;
      else switch(shiftjis[i]) {
          default: *(pascii+asciipos++) = '.'; break;
      }
    } else if (shiftjis[i] >= 0x83 && shiftjis[i] <= 0x9F) {
      i++; *(pascii+asciipos++) = '?';
    } else if (shiftjis[i] >= 0xE0 && shiftjis[i] <= 0xFC) {
      i++; *(pascii+asciipos++) = '?';
    } else if (shiftjis[i] != 0) {
      *(pascii+asciipos++) = '?';
    }
  }
  return(0);
}

canonical_original_code:
int dexuxjistoascii(unsigned char *shiftjis, unsigned char *ascii) {
  unsigned char *pascii;
  int i; int asciipos = 0;
  pascii = ascii;
  memset(ascii, 0, 65);
  for (i = 0; i <= 63; i++) {
    if (shiftjis[i] > 0x00 && shiftjis[i] <= 0x7F) {
      *(pascii+asciipos++) = shiftjis[i];
    } else if (shiftjis[i] >= 0xA0 && shiftjis[i] <= 0xDF) {
      *(pascii+asciipos++) = '.';
    } else if (shiftjis[i] == 0x81) {
      switch(shiftjis[++i]) {
          case 0x40: *(pascii+asciipos++) = ' '; break;
          case 0x43: *(pascii+asciipos++) = ','; break;
          case 0x44: *(pascii+asciipos++) = '.'; break;
          case 0x46: *(pascii+asciipos++) = ':'; break;
          case 0x47: *(pascii+asciipos++) = ';'; break;
          case 0x48: *(pascii+asciipos++) = '?'; break;
          case 0x49: *(pascii+asciipos++) = '!'; break;
          case 0x5E: *(pascii+asciipos++) = '/'; break;
          case 0x6D: *(pascii+asciipos++) = '['; break;
          case 0x6E: *(pascii+asciipos++) = ']'; break;
          default: *(pascii+asciipos++) = '.'; break;
      }
    } else if (shiftjis[i] == 0x82) {
      i++;
      if (shiftjis[i] < 0x40) {
          *(pascii+asciipos++) = '!';
      } else if (shiftjis[i] >= 0x40 && shiftjis[i] <= 0x4E) {
          *(pascii+asciipos++) = '.';
      } else if (shiftjis[i] >= 0x4F && shiftjis[i] <= 0x58) {
          *(pascii+asciipos++) = shiftjis[i] - 0x1F;
      } else if (shiftjis[i] >= 0x60 && shiftjis[i] <= 0x79) {
          *(pascii+asciipos++) = shiftjis[i] - 0x1F;
      } else if (shiftjis[i] >= 0x7A && shiftjis[i] <= 0x7E) {
          *(pascii+asciipos++) = '.';
      } else if (shiftjis[i] == 0x7F) {
          *(pascii+asciipos++) = '!';
      }
      else if (shiftjis[i] >= 0x81 && shiftjis[i] <= 0xA6)
          *(pascii+asciipos++) = shiftjis[i] - 0x20;
      else switch(shiftjis[i]) {
          default: *(pascii+asciipos++) = '.'; break;
      }
    } else if (shiftjis[i] >= 0x83 && shiftjis[i] <= 0x9F) {
      i++; *(pascii+asciipos++) = '?';
    } else if (shiftjis[i] >= 0xE0 && shiftjis[i] <= 0xFC) {
      i++; *(pascii+asciipos++) = '?';
    } else if (shiftjis[i] != 0) {
      *(pascii+asciipos++) = '?';
    }
  }
  return(0);
}

variable_types:
shiftjis
unsigned char *

variable_types:
ascii
unsigned char *

variable_types:
pascii
unsigned char *

variable_types: asciipos int


dexuxgetblock

decompiled_code:
__int64 __fastcall dexuxgetblock(unsigned int a1, int a2, __int64 a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // r13
char v7[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v3 = a3;
v4 = a2 << 6;
v5 = a3 + 0x2000;
do
{
v7[0] = dexuxframetop(v4);
v7[1] = dexuxframebottom(v4);
dexuxgetframe(a1, v7, v3);
v3 += 128LL;
++v4;
}
while ( v3 != v5 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, int a2, __int64 a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // r13
char v7[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v3 = a3;
v4 = a2 << 6;
v5 = a3 + 0x2000;
do
{
v7[0] = dexuxframetop(v4);
v7[1] = dexuxframebottom(v4);
dexuxgetframe(a1, v7, v3);
v3 += 128LL;
++v4;
}
while ( v3 != v5 );
return 0LL;
}

original_code:
int dexuxgetblock(int fd, int blocknumber, char *blockdata) {
  char *pblockdata, *pframetoget, frametoget[2], framebuf[129];
  long frameaddress;
  int framenum;
  pblockdata = blockdata; pframetoget = frametoget;
  for (framenum = 0; framenum <= 63; framenum++) {
    frameaddress = (blocknumber * 0x40) + framenum;
    *(pframetoget + 0) = dexuxframetop(frameaddress);
    *(pframetoget + 1) = dexuxframebottom(frameaddress);
    dexuxgetframe(fd, frametoget, pblockdata);
    pblockdata += 128;
  }
  return(0);
}

canonical_original_code:
int dexuxgetblock(int fd, int blocknumber, char *blockdata) {
  char *pblockdata; char *pframetoget; char frametoget[2]; char framebuf[129];
  long frameaddress;
  int framenum;
  pblockdata = blockdata; pframetoget = frametoget;
  for (framenum = 0; framenum <= 63; framenum++) {
    frameaddress = (blocknumber * 0x40) + framenum;
    *(pframetoget + 0) = dexuxframetop(frameaddress);
    *(pframetoget + 1) = dexuxframebottom(frameaddress);
    dexuxgetframe(fd, frametoget, pblockdata);
    pblockdata += 128;
  }
  return(0);
}

variable_types: fd int

variable_types:
pblockdata
char *

variable_types:
pframetoget
char *

variable_types:
frametoget
char [2]

variable_types:
framebuf
char [129]


dexuxframetop

decompiled_code:
__int64 __fastcall dexuxframetop(__int64 a1)
{
return a1 / 256;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1 / 256;
}

original_code:
int dexuxframetop(long frameaddress) {
  return(frameaddress / 256);
}


dexlist

decompiled_code:
__int64 __fastcall dexlist(unsigned int a1)
{
int v1; // ebx
unsigned int i; // er14
__int64 j; // rax
char *v4; // rbx
int v5; // er12
int v6; // ebp
int v7; // eax
unsigned int v8; // ebp
unsigned int v9; // ebx
int v10; // er12
char *v11; // rbx
int v12; // ebp
int v13; // eax
char *v14; // rbx
int v15; // er12
int v16; // ebp
int v17; // eax
__int64 v19; // [rsp+8h] [rbp-220h]
unsigned int v20; // [rsp+18h] [rbp-210h]
unsigned int v21; // [rsp+1Ch] [rbp-20Ch]
char v22; // [rsp+2Eh] [rbp-1FAh] BYREF
char v23; // [rsp+2Fh] [rbp-1F9h]
char v24[32]; // [rsp+30h] [rbp-1F8h] BYREF
char v25[48]; // [rsp+50h] [rbp-1D8h] BYREF
char v26[80]; // [rsp+80h] [rbp-1A8h] BYREF
char v27[144]; // [rsp+D0h] [rbp-158h] BYREF
char v28[5]; // [rsp+160h] [rbp-C8h] BYREF
unsigned __int8 v29; // [rsp+165h] [rbp-C3h]
char v30; // [rsp+166h] [rbp-C2h]
unsigned __int8 v31; // [rsp+16Ah] [rbp-BEh]
unsigned __int8 v32; // [rsp+16Bh] [rbp-BDh]
char v33; // [rsp+16Ch] [rbp-BCh] BYREF
char v34; // [rsp+176h] [rbp-B2h] BYREF
char v35; // [rsp+17Eh] [rbp-AAh] BYREF

v19 = 64LL;
v21 = 0;
v20 = 15;
v1 = 0;
for ( i = 1; i != 16; ++i )
{
v22 = 0;
v23 = i;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "Getting address %d %d\n", 0LL, i);
dexuxgetframe(a1, &v22, v28);
if ( (v28[0] & 0xF0) != 0xA0 )
{
--v20;
v21 += (v28[0] & 0xF0) == 80;
}
if ( (v28[0] & 0xF) == 1 )
{
if ( !v1 )
{
fwrite("# Title ", 1uLL, 0x24uLL, stderr);
fwrite("Size Del Product Code ID Frames\n", 1uLL, 0x27uLL, stderr);
}
if ( (int)i > 9 )
fprintf(stderr, "%d ", i);
else
fprintf(stderr, "%d ", i);
v22 = dexuxframetop(v19);
v23 = dexuxframebottom(v19);
dexuxgetframe(a1, &v22, v27);
for ( j = 4LL; j != 68; ++j )
v25[j + 45] = v27[j];
dexuxjistoascii(v26, v24);
v4 = v24;
v5 = 1;
do
{
v6 = (unsigned __int8)*v4;
v7 = isprint(v6);
if ( v5 && v7 )
{
fputc(v6, stderr);
}
else
{
fputc(32, stderr);
v5 = 0;
}
++v4;
}
while ( v25 != v4 );
if ( v30 == 1 )
{
if ( v29 > 0x3Fu )
fprintf(stderr, " %d ", (unsigned __int8)((v29 >> 5) + 8));
else
fprintf(stderr, " %d ", (unsigned __int8)((v29 >> 5) + 8));
}
else
{
fprintf(stderr, " %d ", v29 >> 5);
}
if ( (v28[0] & 0xF0) == 80 )
fwrite(" N ", 1uLL, 4uLL, stderr);
else
fwrite(" Y ", 1uLL, 4uLL, stderr);
v8 = v31;
if ( isprint(v31) && (v9 = v32, isprint(v32)) )
{
fprintf(stderr, "%c%c-", v8, v9);
v10 = 1;
}
else
{
fwrite(" ", 1uLL, 3uLL, stderr);
v10 = 0;
}
v11 = &v33;
do
{
v12 = (unsigned __int8)*v11;
v13 = isprint(v12);
if ( v10 && v13 )
{
fputc(v12, stderr);
}
else
{
fputc(32, stderr);
v10 = 0;
}
++v11;
}
while ( &v34 != v11 );
fputc(32, stderr);
v14 = &v34;
v15 = 1;
do
{
v16 = (unsigned __int8)*v14;
v17 = isprint(v16);
if ( v15 && v17 )
{
fputc(v16, stderr);
}
else
{
fputc(32, stderr);
v15 = 0;
}
++v14;
}
while ( v14 != &v35 );
fprintf(stderr, " %d\n", v27[2] & 3);
v1 = 1;
}
v19 += 64LL;
}
if ( v1 )
fprintf(stderr, " Blocks in use: %d Blocks available: %d\n", v21, v20);
else
fprintf(stderr, " Blocks in use: None Blocks available: %d\n", v20);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
int v1; // ebx
unsigned int i; // er14
__int64 j; // rax
char *v4; // rbx
int v5; // er12
int v6; // ebp
int v7; // eax
unsigned int v8; // ebp
unsigned int v9; // ebx
int v10; // er12
char *v11; // rbx
int v12; // ebp
int v13; // eax
char *v14; // rbx
int v15; // er12
int v16; // ebp
int v17; // eax
__int64 v19; // [rsp+8h] [rbp-220h]
unsigned int v20; // [rsp+18h] [rbp-210h]
unsigned int v21; // [rsp+1Ch] [rbp-20Ch]
char v22; // [rsp+2Eh] [rbp-1FAh] BYREF
char v23; // [rsp+2Fh] [rbp-1F9h]
char v24[32]; // [rsp+30h] [rbp-1F8h] BYREF
char v25[48]; // [rsp+50h] [rbp-1D8h] BYREF
char v26[80]; // [rsp+80h] [rbp-1A8h] BYREF
char v27[144]; // [rsp+D0h] [rbp-158h] BYREF
char v28[5]; // [rsp+160h] [rbp-C8h] BYREF
unsigned __int8 v29; // [rsp+165h] [rbp-C3h]
char v30; // [rsp+166h] [rbp-C2h]
unsigned __int8 v31; // [rsp+16Ah] [rbp-BEh]
unsigned __int8 v32; // [rsp+16Bh] [rbp-BDh]
char v33; // [rsp+16Ch] [rbp-BCh] BYREF
char v34; // [rsp+176h] [rbp-B2h] BYREF
char v35; // [rsp+17Eh] [rbp-AAh] BYREF

v19 = 64LL;
v21 = 0;
v20 = 15;
v1 = 0;
for ( i = 1; i != 16; ++i )
{
v22 = 0;
v23 = i;
if ( DEXUXDEBUGMODE )
fprintf(stderr, "Getting address %d %d\n", 0LL, i);
dexuxgetframe(a1, &v22, v28);
if ( (v28[0] & 0xF0) != 0xA0 )
{
--v20;
v21 += (v28[0] & 0xF0) == 80;
}
if ( (v28[0] & 0xF) == 1 )
{
if ( !v1 )
{
fwrite("# Title ", 1uLL, 0x24uLL, stderr);
fwrite("Size Del Product Code ID Frames\n", 1uLL, 0x27uLL, stderr);
}
if ( (int)i > 9 )
fprintf(stderr, "%d ", i);
else
fprintf(stderr, "%d ", i);
v22 = dexuxframetop(v19);
v23 = dexuxframebottom(v19);
dexuxgetframe(a1, &v22, v27);
for ( j = 4LL; j != 68; ++j )
v25[j + 45] = v27[j];
dexuxjistoascii(v26, v24);
v4 = v24;
v5 = 1;
do
{
v6 = (unsigned __int8)*v4;
v7 = isprint(v6);
if ( v5 && v7 )
{
fputc(v6, stderr);
}
else
{
fputc(32, stderr);
v5 = 0;
}
++v4;
}
while ( v25 != v4 );
if ( v30 == 1 )
{
if ( v29 > 0x3Fu )
fprintf(stderr, " %d ", (unsigned __int8)((v29 >> 5) + 8));
else
fprintf(stderr, " %d ", (unsigned __int8)((v29 >> 5) + 8));
}
else
{
fprintf(stderr, " %d ", v29 >> 5);
}
if ( (v28[0] & 0xF0) == 80 )
fwrite(" N ", 1uLL, 4uLL, stderr);
else
fwrite(" Y ", 1uLL, 4uLL, stderr);
v8 = v31;
if ( isprint(v31) && (v9 = v32, isprint(v32)) )
{
fprintf(stderr, "%c%c-", v8, v9);
v10 = 1;
}
else
{
fwrite(" ", 1uLL, 3uLL, stderr);
v10 = 0;
}
v11 = &v33;
do
{
v12 = (unsigned __int8)*v11;
v13 = isprint(v12);
if ( v10 && v13 )
{
fputc(v12, stderr);
}
else
{
fputc(32, stderr);
v10 = 0;
}
++v11;
}
while ( &v34 != v11 );
fputc(32, stderr);
v14 = &v34;
v15 = 1;
do
{
v16 = (unsigned __int8)*v14;
v17 = isprint(v16);
if ( v15 && v17 )
{
fputc(v16, stderr);
}
else
{
fputc(32, stderr);
v15 = 0;
}
++v14;
}
while ( v14 != &v35 );
fprintf(stderr, " %d\n", v27[2] & 3);
v1 = 1;
}
v19 += 64LL;
}
if ( v1 )
fprintf(stderr, " Blocks in use: %d Blocks available: %d\n", v21, v20);
else
fprintf(stderr, " Blocks in use: None Blocks available: %d\n", v20);
return 0LL;
}

original_code:
int dexlist(int dexuxfd) {
  int blockcnt, titlecnt, productcnt, idcnt, printable;
  int foundentry=0, blocksavailable=15, blocksused=0;
  unsigned char headerdata[129], blockdata[129];
  unsigned char title_shiftjis[65], *ptitle_shiftjis, title_ascii[65];
  char chunkrequest[2], *pchunkrequest;
  pchunkrequest = chunkrequest;
  ptitle_shiftjis = title_shiftjis;
  for (blockcnt=1; blockcnt<=15; blockcnt++) {
    *(pchunkrequest+0) = 0;
    *(pchunkrequest+1) = blockcnt;
    if (DEXUXDEBUGMODE) fprintf(stderr, "Getting address %d %d\n", *(pchunkrequest+0), *(pchunkrequest+1));
    dexuxgetframe(dexuxfd, chunkrequest, headerdata);
    if ((headerdata[0] & 240) != 0xA0) blocksavailable--;
    if ((headerdata[0] & 240) == 0x50) blocksused++;
    if ((headerdata[0] & 15) == 0x01) {
      if (foundentry == 0) {
        foundentry = 1;
        fprintf(stderr, "#  Title                            ");
        fprintf(stderr, "Size Del Product Code     ID    Frames\n");
      }
      if (blockcnt < 10) {
        fprintf(stderr, "%d  ", blockcnt);
      } else {
        fprintf(stderr, "%d ", blockcnt);
      }
      *(pchunkrequest+0) = dexuxframetop(blockcnt * 0x40);
      *(pchunkrequest+1) = dexuxframebottom(blockcnt * 0x40);
      dexuxgetframe(dexuxfd, chunkrequest, blockdata);
      for (titlecnt=0x04; titlecnt <= 0x43;
           *(ptitle_shiftjis+titlecnt-4) = blockdata[titlecnt++]) ;
      dexuxjistoascii(title_shiftjis, title_ascii);
      printable=1;
      for (titlecnt=0; titlecnt < 32; titlecnt++)
        if (isprint(title_ascii[titlecnt]) && printable) {
          fprintf(stderr, "%c", title_ascii[titlecnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      if (headerdata[6] != 0x01) {
        fprintf(stderr, "   %d  ", headerdata[5] >> 5);
      } else if (headerdata[5] < 0x40) {
        fprintf(stderr, "   %d  ", (headerdata[5] >> 5) + 8);
      } else {
        fprintf(stderr, "  %d  ", (headerdata[5] >> 5) + 8);
      }
      if ((headerdata[0] & 240) == 0x50) {
        fprintf(stderr, " N  ");
      } else {
        fprintf(stderr, " Y  ");
      }
      printable=1;
      if (isprint(headerdata[0x0A]) && isprint(headerdata[0x0B])) {
        fprintf(stderr, "%c%c-",
                headerdata[0x0A], headerdata[0x0B]);
      } else {
        printable=0;
        fprintf(stderr, "   ");
      }
      for (productcnt = 0x0C; productcnt <= 0x15; productcnt++) {
        if (isprint(headerdata[productcnt]) && printable) {
          fprintf(stderr, "%c", headerdata[productcnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      }
      fprintf(stderr, " ");
      printable=1;
      for (idcnt = 0x16; idcnt <= 0x1D; idcnt++) {
        if (isprint(headerdata[idcnt]) && printable) {
          fprintf(stderr, "%c", headerdata[idcnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      }
      fprintf(stderr, "    %d\n", blockdata[0x02] & 3);
    }
  }
  if (foundentry) {
    fprintf(stderr, "      Blocks in use:  %d     Blocks available:  %d\n",
            blocksused, blocksavailable);
  } else {
    fprintf(stderr, "      Blocks in use: None    Blocks available:  %d\n",
            blocksavailable);
  }
  return(0);
}

canonical_original_code:
int dexlist(int dexuxfd) {
  int blockcnt; int titlecnt; int productcnt; int idcnt; int printable;
  int foundentry = 0; int blocksavailable = 15; int blocksused = 0;
  unsigned char headerdata[129]; unsigned char blockdata[129];
  unsigned char title_shiftjis[65]; unsigned char *ptitle_shiftjis; unsigned char title_ascii[65];
  char chunkrequest[2]; char *pchunkrequest;
  pchunkrequest = chunkrequest;
  ptitle_shiftjis = title_shiftjis;
  for (blockcnt=1; blockcnt<=15; blockcnt++) {
    *(pchunkrequest+0) = 0;
    *(pchunkrequest+1) = blockcnt;
    if (DEXUXDEBUGMODE) fprintf(stderr, "Getting address %d %d\n", *(pchunkrequest+0), *(pchunkrequest+1));
    dexuxgetframe(dexuxfd, chunkrequest, headerdata);
    if ((headerdata[0] & 240) != 0xA0) blocksavailable--;
    if ((headerdata[0] & 240) == 0x50) blocksused++;
    if ((headerdata[0] & 15) == 0x01) {
      if (foundentry == 0) {
        foundentry = 1;
        fprintf(stderr, "#  Title                            ");
        fprintf(stderr, "Size Del Product Code     ID    Frames\n");
      }
      if (blockcnt < 10) {
        fprintf(stderr, "%d  ", blockcnt);
      } else {
        fprintf(stderr, "%d ", blockcnt);
      }
      *(pchunkrequest+0) = dexuxframetop(blockcnt * 0x40);
      *(pchunkrequest+1) = dexuxframebottom(blockcnt * 0x40);
      dexuxgetframe(dexuxfd, chunkrequest, blockdata);
      for (titlecnt=0x04; titlecnt <= 0x43;
           *(ptitle_shiftjis+titlecnt-4) = blockdata[titlecnt++]) ;
      dexuxjistoascii(title_shiftjis, title_ascii);
      printable=1;
      for (titlecnt=0; titlecnt < 32; titlecnt++)
        if (isprint(title_ascii[titlecnt]) && printable) {
          fprintf(stderr, "%c", title_ascii[titlecnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      if (headerdata[6] != 0x01) {
        fprintf(stderr, "   %d  ", headerdata[5] >> 5);
      } else if (headerdata[5] < 0x40) {
        fprintf(stderr, "   %d  ", (headerdata[5] >> 5) + 8);
      } else {
        fprintf(stderr, "  %d  ", (headerdata[5] >> 5) + 8);
      }
      if ((headerdata[0] & 240) == 0x50) {
        fprintf(stderr, " N  ");
      } else {
        fprintf(stderr, " Y  ");
      }
      printable=1;
      if (isprint(headerdata[0x0A]) && isprint(headerdata[0x0B])) {
        fprintf(stderr, "%c%c-",
                headerdata[0x0A], headerdata[0x0B]);
      } else {
        printable=0;
        fprintf(stderr, "   ");
      }
      for (productcnt = 0x0C; productcnt <= 0x15; productcnt++) {
        if (isprint(headerdata[productcnt]) && printable) {
          fprintf(stderr, "%c", headerdata[productcnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      }
      fprintf(stderr, " ");
      printable=1;
      for (idcnt = 0x16; idcnt <= 0x1D; idcnt++) {
        if (isprint(headerdata[idcnt]) && printable) {
          fprintf(stderr, "%c", headerdata[idcnt]);
        } else {
          printable=0;
          fprintf(stderr, " ");
        }
      }
      fprintf(stderr, "    %d\n", blockdata[0x02] & 3);
    }
  }
  if (foundentry) {
    fprintf(stderr, "      Blocks in use:  %d     Blocks available:  %d\n",
            blocksused, blocksavailable);
  } else {
    fprintf(stderr, "      Blocks in use: None    Blocks available:  %d\n",
            blocksavailable);
  }
  return(0);
}

variable_types: dexuxfd int

variable_types: blockcnt int

variable_types: titlecnt int

variable_types: productcnt int

variable_types: idcnt int

variable_types: printable int

variable_types: foundentry int

variable_types: blocksavailable int

variable_types: blocksused int

variable_types:
headerdata
unsigned char [129]

variable_types:
blockdata
unsigned char [129]

variable_types:
chunkrequest
char [2]

variable_types:
pchunkrequest
char *


dexuxframebottom

decompiled_code:
__int64 __fastcall dexuxframebottom(__int64 a1)
{
return a1 % 256;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1 % 256;
}

original_code:
int dexuxframebottom(long frameaddress) {
  return(frameaddress % 256);
}

variable_types: frameaddress long


dexuxgetframe

decompiled_code:
ssize_t __fastcall dexuxgetframe(int fd, void *buf, void *a3)
{
char bufa[4]; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

dexuxclear();
write(fd, &unk_12C, 4uLL);
write(fd, (char *)buf + 1, 1uLL);
write(fd, buf, 1uLL);
usleep(0x3E8u);
ioctl(fd, 0x541BuLL, v6);
if ( DEXUXDEBUGMODE )
LABEL_2:
fprintf(stderr, " available = %d\n", (unsigned int)v6[0]);
while ( v6[0] <= 131 )
{
usleep(0x3E8u);
ioctl(fd, 0x541BuLL, v6);
if ( DEXUXDEBUGMODE )
goto LABEL_2;
}
read(fd, bufa, 4uLL);
return read(fd, a3, 0x80uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd, void *buf, void *a3)
{
char bufa[4]; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

dexuxclear();
write(fd, &unk_12C, 4uLL);
write(fd, (char *)buf + 1, 1uLL);
write(fd, buf, 1uLL);
usleep(0x3E8u);
ioctl(fd, 0x541BuLL, v6);
if ( DEXUXDEBUGMODE )
LABEL_2:
fprintf(stderr, " available = %d\n", (unsigned int)v6[0]);
while ( v6[0] <= 131 )
{
usleep(0x3E8u);
ioctl(fd, 0x541BuLL, v6);
if ( DEXUXDEBUGMODE )
goto LABEL_2;
}
read(fd, bufa, 4uLL);
return read(fd, a3, 0x80uLL);
}

original_code:
int dexuxgetframe(int fd, unsigned char *address, char *data) {
  int available, readlength;
  char trashdata[4];
  dexuxclear(fd);
  write(fd, "IAI\x02", 4);
  write(fd, address+1, 1);
  write(fd, address+0, 1);
  usleep(1000);
  ioctl(fd, 0x541B, &available);
  if (DEXUXDEBUGMODE) fprintf(stderr, "  available = %d\n", available);
  while (available < 132) {
    usleep(1000);
    ioctl(fd, 0x541B, &available);
    if (DEXUXDEBUGMODE) fprintf(stderr, "  available = %d\n", available);
  }
  read(fd, trashdata, 4);
  readlength = read(fd, data, 128);
  return(readlength);
}

canonical_original_code:
int dexuxgetframe(int fd, unsigned char *address, char *data) {
  int available; int readlength;
  char trashdata[4];
  dexuxclear(fd);
  write(fd, "IAI\x02", 4);
  write(fd, address+1, 1);
  write(fd, address+0, 1);
  usleep(1000);
  ioctl(fd, 0x541B, &available);
  if (DEXUXDEBUGMODE) fprintf(stderr, "  available = %d\n", available);
  while (available < 132) {
    usleep(1000);
    ioctl(fd, 0x541B, &available);
    if (DEXUXDEBUGMODE) fprintf(stderr, "  available = %d\n", available);
  }
  read(fd, trashdata, 4);
  readlength = read(fd, data, 128);
  return(readlength);
}

variable_types: fd int

variable_types:
address
unsigned char *

variable_types:
data
char *

variable_types: available int

variable_types: readlength int

variable_types:
trashdata
char [4]


dexuxopen

decompiled_code:
__int64 __fastcall dexuxopen(const char *a1)
{
unsigned int v1; // ebx
char *v3; // rax
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

if ( *a1 )
{
v1 = open(a1, 2306);
}
else if ( getenv("DEXUXPORT") )
{
v3 = getenv("DEXUXPORT");
v1 = open(v3, 2306);
}
else
{
v1 = open("/dev/ttyS1", 2306);
}
if ( v1 == -1 )
{
perror("open_port: unable to open device for dex drive\n");
}
else
{
fcntl(v1, 4, 0LL);
tcgetattr(v1, &v4);
cfsetispeed(&v4, 0xFu);
cfsetospeed(&v4, 0xFu);
v4.c_cflag = v4.c_cflag & 0x7FFFFE8F | 0x8B0;
v4.c_lflag &= 0xFFFFFFE4;
v4.c_iflag = v4.c_iflag & 0xFFFFE3E7 | 4;
v4.c_oflag &= 0xFFFFFFFE;
v4.c_cc[5] = 10;
tcsetattr(v1, 2, &v4);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ebx
char *v3; // rax
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

if ( *a1 )
{
v1 = open(a1, 2306);
}
else if ( getenv("DEXUXPORT") )
{
v3 = getenv("DEXUXPORT");
v1 = open(v3, 2306);
}
else
{
v1 = open("/dev/ttyS1", 2306);
}
if ( v1 == -1 )
{
perror("open_port: unable to open device for dex drive\n");
}
else
{
fcntl(v1, 4, 0LL);
tcgetattr(v1, &v4);
cfsetispeed(&v4, 0xFu);
cfsetospeed(&v4, 0xFu);
v4.c_cflag = v4.c_cflag & 0x7FFFFE8F | 0x8B0;
v4.c_lflag &= 0xFFFFFFE4;
v4.c_iflag = v4.c_iflag & 0xFFFFE3E7 | 4;
v4.c_oflag &= 0xFFFFFFFE;
v4.c_cc[5] = 10;
tcsetattr(v1, 2, &v4);
}
return v1;
}

original_code:
int dexuxopen(char *devname) {
  int fd;
  struct termios options;
  if (devname[0] != 0) {
    fd = open(devname, 02 | 0400 | 04000);
  } else if (getenv("DEXUXPORT") != ((void *)0)) {
    fd = open(getenv("DEXUXPORT"), 02 | 0400 | 04000);
  } else {
    fd = open("/dev/ttyS1", 02 | 0400 | 04000);
  }
  if (fd == -1) {
    perror("open_port: unable to open device for dex drive\n");
    return(fd);
  }
  fcntl(fd, 4, 0);
  tcgetattr(fd, &options);
  cfsetispeed(&options, 0000017);
  cfsetospeed(&options, 0000017);
  options.c_cflag |= (0004000 | 0000200);
  options.c_cflag &= ~0000060;
  options.c_cflag |= 0000060;
  options.c_cflag &= ~0000400;
  options.c_cflag &= ~0000100;
  options.c_cflag &= ~020000000000;
  options.c_lflag &= ~(0000002|0000010|0000020|0000001);
  options.c_iflag &= ~0000020;
  options.c_iflag |= 0000004;
  options.c_iflag &= ~0000010;
  options.c_iflag &= ~(0002000 | 0010000 | 0004000);
  options.c_oflag &= ~0000001;
  options.c_cc[5] = 10;
  tcsetattr(fd, 2, &options);
  return(fd);
}

variable_types:
devname
char *

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


dexwritefull

decompiled_code:
__int64 __fastcall dexwritefull(unsigned int a1, const char *a2, const char *a3, __int64 a4)
{
int i; // ebx
int v7; // ebx
unsigned int j; // ebx
char v10[2]; // [rsp+Eh] [rbp-BAh] BYREF
char v11[184]; // [rsp+10h] [rbp-B8h] BYREF

for ( i = 1; i != 16; ++i )
{
v10[0] = 0;
v10[1] = i;
dexuxgetframe(a1, v10, v11);
if ( (v11[0] & 0xF0) != 0xA0 )
{
fprintf(stderr, "Detected file format %s which contains", a3);
fwrite(" a full memorycard image\n", 1uLL, 0x19uLL, stderr);
fwrite("But the card in the drive still has data which", 1uLL, 0x2EuLL, stderr);
fwrite(" has not been deleted\n", 1uLL, 0x16uLL, stderr);
fprintf(stderr, "Skipping file: %s\n", a2);
return 1LL;
}
}
fprintf(stderr, "Progress for: %s\n", a2);
fputc(124, stderr);
v7 = 64;
do
{
fputc(45, stderr);
--v7;
}
while ( v7 );
fwrite("|\n ", 1uLL, 3uLL, stderr);
for ( j = 0; j != 16; ++j )
{
dexuxputblock(a1, j, a4);
a4 += 0x2000LL;
fwrite("****", 1uLL, 4uLL, stderr);
}
fputc(10, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, const char *a2, const char *a3, __int64 a4)
{
int i; // ebx
int v7; // ebx
unsigned int j; // ebx
char v10[2]; // [rsp+Eh] [rbp-BAh] BYREF
char v11[184]; // [rsp+10h] [rbp-B8h] BYREF

for ( i = 1; i != 16; ++i )
{
v10[0] = 0;
v10[1] = i;
dexuxgetframe(a1, v10, v11);
if ( (v11[0] & 0xF0) != 0xA0 )
{
fprintf(stderr, "Detected file format %s which contains", a3);
fwrite(" a full memorycard image\n", 1uLL, 0x19uLL, stderr);
fwrite("But the card in the drive still has data which", 1uLL, 0x2EuLL, stderr);
fwrite(" has not been deleted\n", 1uLL, 0x16uLL, stderr);
fprintf(stderr, "Skipping file: %s\n", a2);
return 1LL;
}
}
fprintf(stderr, "Progress for: %s\n", a2);
fputc(124, stderr);
v7 = 64;
do
{
fputc(45, stderr);
--v7;
}
while ( v7 );
fwrite("|\n ", 1uLL, 3uLL, stderr);
for ( j = 0; j != 16; ++j )
{
dexuxputblock(a1, j, a4);
a4 += 0x2000LL;
fwrite("****", 1uLL, 4uLL, stderr);
}
fputc(10, stderr);
return 0LL;
}

original_code:
int dexwritefull(int dexuxfd, char *fname, char *fileformat, unsigned char *pblock) {
  int progresscnt, blockcnt;
  unsigned char headerdata[129];
  unsigned char framerequest[2], *pframerequest;
    pframerequest = framerequest;
    for (blockcnt = 1; blockcnt <= 15; blockcnt++) {
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blockcnt;
      dexuxgetframe(dexuxfd, framerequest, headerdata);
      if ((headerdata[0] & 240) != 0xA0) {
 fprintf(stderr, "Detected file format %s which contains", fileformat);
        fprintf(stderr, " a full memorycard image\n");
        fprintf(stderr, "But the card in the drive still has data which");
        fprintf(stderr, " has not been deleted\n");
        fprintf(stderr, "Skipping file: %s\n", fname);
        return(1);
      }
    }
    fprintf(stderr, "Progress for: %s\n", fname);
    fprintf(stderr, "|");
    for (progresscnt = 0; progresscnt < 64; progresscnt++) {
      fprintf(stderr, "-");
    }
    fprintf(stderr, "|\n ");
    for (blockcnt = 0; blockcnt <= 15; blockcnt++) {
      dexuxputblock(dexuxfd, blockcnt, pblock);
      pblock += 8192;
      fprintf(stderr, "****");
    }
    fprintf(stderr, "\n");
  return(0);
}

canonical_original_code:
int dexwritefull(int dexuxfd, char *fname, char *fileformat, unsigned char *pblock) {
  int progresscnt; int blockcnt;
  unsigned char headerdata[129];
  unsigned char framerequest[2]; unsigned char *pframerequest;
    pframerequest = framerequest;
    for (blockcnt = 1; blockcnt <= 15; blockcnt++) {
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blockcnt;
      dexuxgetframe(dexuxfd, framerequest, headerdata);
      if ((headerdata[0] & 240) != 0xA0) {
 fprintf(stderr, "Detected file format %s which contains", fileformat);
        fprintf(stderr, " a full memorycard image\n");
        fprintf(stderr, "But the card in the drive still has data which");
        fprintf(stderr, " has not been deleted\n");
        fprintf(stderr, "Skipping file: %s\n", fname);
        return(1);
      }
    }
    fprintf(stderr, "Progress for: %s\n", fname);
    fprintf(stderr, "|");
    for (progresscnt = 0; progresscnt < 64; progresscnt++) {
      fprintf(stderr, "-");
    }
    fprintf(stderr, "|\n ");
    for (blockcnt = 0; blockcnt <= 15; blockcnt++) {
      dexuxputblock(dexuxfd, blockcnt, pblock);
      pblock += 8192;
      fprintf(stderr, "****");
    }
    fprintf(stderr, "\n");
  return(0);
}

variable_types: dexuxfd int

variable_types:
fname
char *

variable_types:
fileformat
char *

variable_types:
pblock
unsigned char *

variable_types: progresscnt int

variable_types: blockcnt int

variable_types:
framerequest
unsigned char [2]

variable_types:
pframerequest
unsigned char *


dexdelete

decompiled_code:
__int64 __fastcall dexdelete(__int64 a1, char a2, unsigned int a3)
{
char v4; // dl
char v5; // al
char v7[2]; // [rsp+Eh] [rbp-AAh] BYREF
char v8[8]; // [rsp+10h] [rbp-A8h] BYREF
unsigned __int8 v9; // [rsp+18h] [rbp-A0h]
char v10; // [rsp+8Fh] [rbp-29h]

v7[0] = 0;
v7[1] = a2;
dexuxgetframe(a1, v7, v8);
v4 = v8[0] & 0xF0;
if ( !a3 && v4 != -96 )
{
v5 = (v8[0] & 0xF) - 96;
goto LABEL_7;
}
if ( a3 == 1 && v4 != 80 )
{
v5 = (v8[0] & 0xF) + 80;
LABEL_7:
v8[0] = v5;
v10 = dexuxstrxor(v8, 127LL);
dexuxputframe((unsigned int)a1, v7, v8);
}
if ( v9 != 0xFF )
dexdelete((unsigned int)a1, (unsigned int)v9 + 1, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2, unsigned int a3)
{
char v4; // dl
char v5; // al
char v7[2]; // [rsp+Eh] [rbp-AAh] BYREF
char v8[8]; // [rsp+10h] [rbp-A8h] BYREF
unsigned __int8 v9; // [rsp+18h] [rbp-A0h]
char v10; // [rsp+8Fh] [rbp-29h]

v7[0] = 0;
v7[1] = a2;
dexuxgetframe(a1, v7, v8);
v4 = v8[0] & 0xF0;
if ( !a3 && v4 != -96 )
{
v5 = (v8[0] & 0xF) - 96;
goto LABEL_7;
}
if ( a3 == 1 && v4 != 80 )
{
v5 = (v8[0] & 0xF) + 80;
LABEL_7:
v8[0] = v5;
v10 = dexuxstrxor(v8, 127LL);
dexuxputframe((unsigned int)a1, v7, v8);
}
if ( v9 != 0xFF )
func0((unsigned int)a1, (unsigned int)v9 + 1, a3);
return 0LL;
}

original_code:
int dexdelete(int dexuxfd, int blocknum, int undeleteflag) {
  unsigned char headerdata[129];
  char chunkrequest[2], *pchunkrequest;
  pchunkrequest = chunkrequest;
  *(pchunkrequest+0) = 0;
  *(pchunkrequest+1) = blocknum;
  dexuxgetframe(dexuxfd, chunkrequest, headerdata);
  if ((((headerdata[0] & 240) != 0xA0) && (undeleteflag == 0)) ||
      (((headerdata[0] & 240) != 0x50) && (undeleteflag == 1))) {
    if (undeleteflag == 0) {
      headerdata[0] = (headerdata[0] & 15) + 0xA0;
    } else {
      headerdata[0] = (headerdata[0] & 15) + 0x50;
    }
    headerdata[127] = dexuxstrxor(headerdata, 127);
    dexuxputframe(dexuxfd, chunkrequest, headerdata);
  }
  if (headerdata[8] != 0xFF) {
    dexdelete(dexuxfd, headerdata[8] + 1, undeleteflag);
  }
  return(0);
}

canonical_original_code:
int dexdelete(int dexuxfd, int blocknum, int undeleteflag) {
  unsigned char headerdata[129];
  char chunkrequest[2]; char *pchunkrequest;
  pchunkrequest = chunkrequest;
  *(pchunkrequest+0) = 0;
  *(pchunkrequest+1) = blocknum;
  dexuxgetframe(dexuxfd, chunkrequest, headerdata);
  if ((((headerdata[0] & 240) != 0xA0) && (undeleteflag == 0)) ||
      (((headerdata[0] & 240) != 0x50) && (undeleteflag == 1))) {
    if (undeleteflag == 0) {
      headerdata[0] = (headerdata[0] & 15) + 0xA0;
    } else {
      headerdata[0] = (headerdata[0] & 15) + 0x50;
    }
    headerdata[127] = dexuxstrxor(headerdata, 127);
    dexuxputframe(dexuxfd, chunkrequest, headerdata);
  }
  if (headerdata[8] != 0xFF) {
    dexdelete(dexuxfd, headerdata[8] + 1, undeleteflag);
  }
  return(0);
}

variable_types: blocknum int

variable_types: undeleteflag int

variable_types:
headerdata
unsigned char [129]

variable_types:
chunkrequest
char [2]

variable_types:
pchunkrequest
char *


dexuxstrxor

decompiled_code:
__int64 __fastcall dexuxstrxor(char *a1, __int64 a2)
{
char *v2; // r9
__int64 result; // rax

if ( a2 <= 0 )
return 0LL;
v2 = a1;
LODWORD(result) = 0;
do
{
result = (((unsigned __int8)result ^ (unsigned __int8)*v2) & 2 | ((unsigned __int8)result ^ (unsigned __int8)*v2) & 1)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 4)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 8)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x10)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x20)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x40)
+ (unsigned int)(unsigned __int8)(((unsigned __int8)(*v2 >> 7) ^ (unsigned __int8)((int)result >> 7)) << 7);
++v2;
}
while ( v2 != &a1[a2] );
return result;
}

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

if ( a2 <= 0 )
return 0LL;
v2 = a1;
LODWORD(result) = 0;
do
{
result = (((unsigned __int8)result ^ (unsigned __int8)*v2) & 2 | ((unsigned __int8)result ^ (unsigned __int8)*v2) & 1)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 4)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 8)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x10)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x20)
+ (((unsigned __int8)result ^ (unsigned __int8)*v2) & 0x40)
+ (unsigned int)(unsigned __int8)(((unsigned __int8)(*v2 >> 7) ^ (unsigned __int8)((int)result >> 7)) << 7);
++v2;
}
while ( v2 != &a1[a2] );
return result;
}

variable_types:
instring
char *

variable_types: stringlength long

variable_types: outbyte int

variable_types: i long


dexuxputframe

decompiled_code:
ssize_t __fastcall dexuxputframe(int fd, char *a2, __int64 a3)
{
__int64 i; // rax
ssize_t result; // rax
char v6; // [rsp+Fh] [rbp-A9h] BYREF
char buf[139]; // [rsp+10h] [rbp-A8h] BYREF
char v8[29]; // [rsp+9Bh] [rbp-1Dh] BYREF

buf[132] = 0;
buf[0] = *a2;
buf[1] = a2[1];
buf[2] = dexuxbinreverse((unsigned int)buf[0]);
buf[3] = dexuxbinreverse((unsigned int)a2[1]);
for ( i = 0LL; i != 128; ++i )
buf[i + 4] = *(_BYTE *)(a3 + i);
dexuxclear((unsigned int)fd);
v6 = dexuxstrxor(buf, 132LL);
write(fd, &unk_108, 4uLL);
write(fd, buf, 0x84uLL);
write(fd, &v6, 1uLL);
result = read(fd, v8, 4uLL);
if ( memcmp(v8, "IAI)", 4uLL) )
result = 1LL;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd, char *a2, __int64 a3)
{
__int64 i; // rax
ssize_t result; // rax
char v6; // [rsp+Fh] [rbp-A9h] BYREF
char buf[139]; // [rsp+10h] [rbp-A8h] BYREF
char v8[29]; // [rsp+9Bh] [rbp-1Dh] BYREF

buf[132] = 0;
buf[0] = *a2;
buf[1] = a2[1];
buf[2] = dexuxbinreverse((unsigned int)buf[0]);
buf[3] = dexuxbinreverse((unsigned int)a2[1]);
for ( i = 0LL; i != 128; ++i )
buf[i + 4] = *(_BYTE *)(a3 + i);
dexuxclear((unsigned int)fd);
v6 = dexuxstrxor(buf, 132LL);
write(fd, &unk_108, 4uLL);
write(fd, buf, 0x84uLL);
write(fd, &v6, 1uLL);
result = read(fd, v8, 4uLL);
if ( memcmp(v8, "IAI)", 4uLL) )
result = 1LL;
return result;
}

original_code:
int dexuxputframe(int fd, char *address, char *data) {
  char buffer[5];
  char sendstring[133], *psendstring;
  char checkbyte;
  int readlength;
  int i;
  psendstring = sendstring;
  *(psendstring+132) = 0;
  *(psendstring+0) = *(address+0);
  *(psendstring+1) = *(address+1);
  *(psendstring+2) = dexuxbinreverse( *(address+0) );
  *(psendstring+3) = dexuxbinreverse( *(address+1) );
  for (i = 0; i <= 127; i++) {
    *(psendstring+4+i) = *(data+i);
  }
  dexuxclear(fd);
  checkbyte = dexuxstrxor(sendstring, 132);
  write(fd, "IAI\x04", 4);
  write(fd, sendstring, 132);
  write(fd, &checkbyte, 1);
  readlength = read(fd, buffer, 4);
  if (strncmp(buffer, "IAI\x29", 4)) {
          buffer[readlength] = 0;
          return(1);
  }
  return(readlength);
}

canonical_original_code:
int dexuxputframe(int fd, char *address, char *data) {
  char buffer[5];
  char sendstring[133]; char *psendstring;
  char checkbyte;
  int readlength;
  int i;
  psendstring = sendstring;
  *(psendstring+132) = 0;
  *(psendstring+0) = *(address+0);
  *(psendstring+1) = *(address+1);
  *(psendstring+2) = dexuxbinreverse( *(address+0) );
  *(psendstring+3) = dexuxbinreverse( *(address+1) );
  for (i = 0; i <= 127; i++) {
    *(psendstring+4+i) = *(data+i);
  }
  dexuxclear(fd);
  checkbyte = dexuxstrxor(sendstring, 132);
  write(fd, "IAI\x04", 4);
  write(fd, sendstring, 132);
  write(fd, &checkbyte, 1);
  readlength = read(fd, buffer, 4);
  if (strncmp(buffer, "IAI\x29", 4)) {
          buffer[readlength] = 0;
          return(1);
  }
  return(readlength);
}

variable_types: fd int

variable_types:
address
char *

variable_types:
data
char *

variable_types:
buffer
char [5]

variable_types:
sendstring
char [133]

variable_types:
psendstring
char *

variable_types: checkbyte char

variable_types: readlength int

variable_types: i int


dexuxclose

decompiled_code:
__int64 __fastcall dexuxclose(int fd)
{
write(fd, "IAI\a", 5uLL);
close(fd);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
write(fd, "IAI\a", 5uLL);
close(fd);
return 0LL;
}

original_code:
int dexuxclose(int fd) {
  write(fd, "IAI\x07\x00", 5);
  close(fd);
  return(0);
}


dexuxstatus

decompiled_code:
__int64 __fastcall dexuxstatus(int fd)
{
int v1; // edx
unsigned __int64 v2; // rcx
__int64 result; // rax
unsigned __int64 v4; // rcx
bool v5; // zf
unsigned __int64 v6; // rcx
char v7[280]; // [rsp+0h] [rbp-118h] BYREF

dexuxclear();
write(fd, &unk_D8, 4uLL);
v1 = read(fd, v7, 0xFEuLL);
v2 = v1;
v7[v1] = 0;
if ( (unsigned __int64)v1 > 5 )
v2 = 5LL;
result = 1LL;
if ( memcmp(v7, "IAI\"", v2) )
{
v4 = v1;
if ( (unsigned __int64)v1 > 6 )
v4 = 6LL;
v5 = memcmp(v7, &unk_E2, v4) == 0;
result = !v5;
if ( !v5 )
{
v6 = v1;
if ( (unsigned __int64)v1 > 5 )
v6 = 5LL;
v5 = memcmp(v7, "IAI#", v6) == 0;
result = !v5;
if ( !v5 )
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
int v1; // edx
unsigned __int64 v2; // rcx
__int64 result; // rax
unsigned __int64 v4; // rcx
bool v5; // zf
unsigned __int64 v6; // rcx
char v7[280]; // [rsp+0h] [rbp-118h] BYREF

dexuxclear();
write(fd, &unk_D8, 4uLL);
v1 = read(fd, v7, 0xFEuLL);
v2 = v1;
v7[v1] = 0;
if ( (unsigned __int64)v1 > 5 )
v2 = 5LL;
result = 1LL;
if ( memcmp(v7, "IAI\"", v2) )
{
v4 = v1;
if ( (unsigned __int64)v1 > 6 )
v4 = 6LL;
v5 = memcmp(v7, &unk_E2, v4) == 0;
result = !v5;
if ( !v5 )
{
v6 = v1;
if ( (unsigned __int64)v1 > 5 )
v6 = 5LL;
v5 = memcmp(v7, "IAI#", v6) == 0;
result = !v5;
if ( !v5 )
result = 2LL;
}
}
return result;
}

original_code:
int dexuxstatus(int fd) {
  char buffer[255];
  int readlength;
  dexuxclear(fd);
  write(fd, "IAI\x01",4);
  readlength = read(fd, &buffer, 254);
  buffer[readlength]='\0';
  if (!strncmp(buffer, "IAI\x22",readlength)) {
    return(1);
  } else if (!strncmp(buffer, "IAI\x23\x10",readlength)) {
    return(0);
  } else if (!strncmp(buffer, "IAI\x23\x00",readlength)) {
    return(0);
  } else {
    return(2);
  }
}

variable_types:
buffer
char [255]


dexwritesingle

decompiled_code:
__int64 __fastcall dexwritesingle(unsigned int a1, const char *a2, const char *a3, __int64 a4, int a5)
{
__int64 j; // rax
char *v9; // rax
__int64 v10; // r14
int v11; // er15
unsigned int v12; // ebx
__int64 i; // rax
__int64 v15; // r13
__int64 v16; // rdx
unsigned int v17; // ebx
unsigned __int8 v18; // [rsp+Eh] [rbp-19Ah] BYREF
unsigned __int8 v19; // [rsp+Fh] [rbp-199h]
char v20[144]; // [rsp+10h] [rbp-198h] BYREF
unsigned __int8 v21; // [rsp+A0h] [rbp-108h] BYREF
unsigned __int8 v22; // [rsp+A1h] [rbp-107h]
unsigned __int8 v23; // [rsp+A2h] [rbp-106h]
unsigned __int8 v24; // [rsp+A3h] [rbp-105h]
unsigned __int8 v25; // [rsp+A4h] [rbp-104h]
unsigned __int8 v26; // [rsp+A5h] [rbp-103h]
bool v27; // [rsp+A6h] [rbp-102h]
unsigned __int8 v28; // [rsp+A7h] [rbp-101h]
unsigned __int8 v29; // [rsp+A8h] [rbp-100h]
unsigned __int8 v30; // [rsp+A9h] [rbp-FFh]
char v31[20]; // [rsp+AAh] [rbp-FEh]
char v32; // [rsp+BEh] [rbp-EAh] BYREF
char v33[137]; // [rsp+11Fh] [rbp-89h] BYREF

if ( DEXUXDEBUGMODE )
{
fwrite("dexwritesingle()\n Creating TOC entry...\n", 1uLL, 0x29uLL, stderr);
if ( !strcmp(a3, "PSX") )
{
if ( DEXUXDEBUGMODE )
fprintf(stderr, " File is in PSX format with imagesize of %d\n", (unsigned int)a5);
goto LABEL_3;
}
if ( DEXUXDEBUGMODE )
fprintf(stderr, " File is in MCS format with imagesize of %d\n", (unsigned int)a5);
LABEL_12:
for ( i = 0LL; i != 128; ++i )
*(&v21 + i) = *(_BYTE *)(a4 + i);
v10 = a4 + 128;
goto LABEL_8;
}
if ( strcmp(a3, "PSX") )
goto LABEL_12;
LABEL_3:
v21 = 81;
v22 = 0;
v23 = 0;
v24 = 0;
v25 = 0;
v26 = 32 * a5;
v27 = a5 > 7;
v28 = 0;
v30 = 0;
for ( j = 0LL; j != 21; ++j )
v31[j] = *(_BYTE *)(a4 + j);
v9 = &v32;
do
*v9++ = 0;
while ( v9 != v33 );
v33[0] = dexuxstrxor(&v21, 127LL, v33);
v10 = a4 + 74;
LABEL_8:
v11 = 0;
v12 = 1;
while ( 1 )
{
v18 = 0;
v19 = v12;
dexuxgetframe(a1, &v18, v20);
if ( (v20[0] & 0xF0) == 0xA0 )
{
*(_DWORD *)&v33[4 * v11++ + 17] = v12;
if ( DEXUXDEBUGMODE )
fprintf(stderr, " Available block found: #%d\n", v12);
}
if ( v11 == a5 )
break;
if ( ++v12 == 16 )
{
if ( v11 < a5 )
{
fprintf(stderr, "File requires %d block(s) but only", (unsigned int)a5);
fprintf(stderr, " %d block(s) available on the card\n", (unsigned int)v11);
fprintf(stderr, "Skipping file: %s\n", a2);
return 1LL;
}
break;
}
}
fprintf(stderr, "Progress for %s (%d blocks in size):\n", a2, (unsigned int)a5);
if ( a5 > 0 )
{
v15 = 0LL;
while ( 1 )
{
fprintf(stderr, " Writting block #%d\n", (unsigned int)(v15 + 1));
if ( a5 - 1 != (_DWORD)v15 )
{
if ( DEXUXDEBUGMODE )
{
fwrite(" blockcnt != (imagesize -1)\n", 1uLL, 0x1DuLL, stderr);
if ( (int)v15 > 0 )
{
if ( DEXUXDEBUGMODE )
fwrite(" blockcnt > 0\n", 1uLL, 0xFuLL, stderr);
goto LABEL_29;
}
}
else
{
if ( (int)v15 <= 0 )
goto LABEL_30;
LABEL_29:
v21 = 82;
}
LABEL_30:
v29 = v33[4 * v15 + 21] - 1;
v30 = 0;
goto LABEL_31;
}
if ( DEXUXDEBUGMODE )
break;
if ( (int)v15 > 0 )
goto LABEL_40;
LABEL_41:
v29 = -1;
v30 = -1;
LABEL_31:
if ( DEXUXDEBUGMODE )
{
fwrite(" headerdata\n", 1uLL, 0xDuLL, stderr);
if ( DEXUXDEBUGMODE )
{
fwrite(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15\n", 1uLL, 0x46uLL, stderr);
if ( DEXUXDEBUGMODE )
fprintf(
stderr,
" %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %c %c %c %c %c %c %c %c %c %c %c %c\n",
v21,
v22,
v23,
v24,
v25,
v26,
v27,
v28,
v29,
v30,
(unsigned __int8)v31[0],
(unsigned __int8)v31[1],
(unsigned __int8)v31[2],
(unsigned __int8)v31[3],
(unsigned __int8)v31[4],
(unsigned __int8)v31[5],
(unsigned __int8)v31[6],
(unsigned __int8)v31[7],
(unsigned __int8)v31[8],
(unsigned __int8)v31[9],
(unsigned __int8)v31[10],
(unsigned __int8)v31[11]);
}
}
v33[0] = dexuxstrxor(&v21, 127LL, v16);
v18 = 0;
v17 = *(_DWORD *)&v33[4 * v15 + 17];
v19 = v17;
if ( DEXUXDEBUGMODE )
{
fprintf(stderr, " Writting to block #%d\n", v17);
if ( DEXUXDEBUGMODE )
fprintf(stderr, " Address %d %d\n", v18, v19);
}
dexuxputframe(a1, &v18, &v21);
dexuxputblock(a1, v17, v10);
v10 += 0x2000LL;
if ( ++v15 == a5 )
return 0LL;
}
fwrite(" blockcnt == (imagesize - 1)\n", 1uLL, 0x1EuLL, stderr);
if ( (int)v15 <= 0 )
goto LABEL_41;
if ( DEXUXDEBUGMODE )
fwrite(" blockcnt > 0\n", 1uLL, 0xFuLL, stderr);
LABEL_40:
v21 = 83;
goto LABEL_41;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, const char *a2, const char *a3, __int64 a4, int a5)
{
__int64 j; // rax
char *v9; // rax
__int64 v10; // r14
int v11; // er15
unsigned int v12; // ebx
__int64 i; // rax
__int64 v15; // r13
__int64 v16; // rdx
unsigned int v17; // ebx
unsigned __int8 v18; // [rsp+Eh] [rbp-19Ah] BYREF
unsigned __int8 v19; // [rsp+Fh] [rbp-199h]
char v20[144]; // [rsp+10h] [rbp-198h] BYREF
unsigned __int8 v21; // [rsp+A0h] [rbp-108h] BYREF
unsigned __int8 v22; // [rsp+A1h] [rbp-107h]
unsigned __int8 v23; // [rsp+A2h] [rbp-106h]
unsigned __int8 v24; // [rsp+A3h] [rbp-105h]
unsigned __int8 v25; // [rsp+A4h] [rbp-104h]
unsigned __int8 v26; // [rsp+A5h] [rbp-103h]
bool v27; // [rsp+A6h] [rbp-102h]
unsigned __int8 v28; // [rsp+A7h] [rbp-101h]
unsigned __int8 v29; // [rsp+A8h] [rbp-100h]
unsigned __int8 v30; // [rsp+A9h] [rbp-FFh]
char v31[20]; // [rsp+AAh] [rbp-FEh]
char v32; // [rsp+BEh] [rbp-EAh] BYREF
char v33[137]; // [rsp+11Fh] [rbp-89h] BYREF

if ( DEXUXDEBUGMODE )
{
fwrite("dexwritesingle()\n Creating TOC entry...\n", 1uLL, 0x29uLL, stderr);
if ( !strcmp(a3, "PSX") )
{
if ( DEXUXDEBUGMODE )
fprintf(stderr, " File is in PSX format with imagesize of %d\n", (unsigned int)a5);
goto LABEL_3;
}
if ( DEXUXDEBUGMODE )
fprintf(stderr, " File is in MCS format with imagesize of %d\n", (unsigned int)a5);
LABEL_12:
for ( i = 0LL; i != 128; ++i )
*(&v21 + i) = *(_BYTE *)(a4 + i);
v10 = a4 + 128;
goto LABEL_8;
}
if ( strcmp(a3, "PSX") )
goto LABEL_12;
LABEL_3:
v21 = 81;
v22 = 0;
v23 = 0;
v24 = 0;
v25 = 0;
v26 = 32 * a5;
v27 = a5 > 7;
v28 = 0;
v30 = 0;
for ( j = 0LL; j != 21; ++j )
v31[j] = *(_BYTE *)(a4 + j);
v9 = &v32;
do
*v9++ = 0;
while ( v9 != v33 );
v33[0] = dexuxstrxor(&v21, 127LL, v33);
v10 = a4 + 74;
LABEL_8:
v11 = 0;
v12 = 1;
while ( 1 )
{
v18 = 0;
v19 = v12;
dexuxgetframe(a1, &v18, v20);
if ( (v20[0] & 0xF0) == 0xA0 )
{
*(_DWORD *)&v33[4 * v11++ + 17] = v12;
if ( DEXUXDEBUGMODE )
fprintf(stderr, " Available block found: #%d\n", v12);
}
if ( v11 == a5 )
break;
if ( ++v12 == 16 )
{
if ( v11 < a5 )
{
fprintf(stderr, "File requires %d block(s) but only", (unsigned int)a5);
fprintf(stderr, " %d block(s) available on the card\n", (unsigned int)v11);
fprintf(stderr, "Skipping file: %s\n", a2);
return 1LL;
}
break;
}
}
fprintf(stderr, "Progress for %s (%d blocks in size):\n", a2, (unsigned int)a5);
if ( a5 > 0 )
{
v15 = 0LL;
while ( 1 )
{
fprintf(stderr, " Writting block #%d\n", (unsigned int)(v15 + 1));
if ( a5 - 1 != (_DWORD)v15 )
{
if ( DEXUXDEBUGMODE )
{
fwrite(" blockcnt != (imagesize -1)\n", 1uLL, 0x1DuLL, stderr);
if ( (int)v15 > 0 )
{
if ( DEXUXDEBUGMODE )
fwrite(" blockcnt > 0\n", 1uLL, 0xFuLL, stderr);
goto LABEL_29;
}
}
else
{
if ( (int)v15 <= 0 )
goto LABEL_30;
LABEL_29:
v21 = 82;
}
LABEL_30:
v29 = v33[4 * v15 + 21] - 1;
v30 = 0;
goto LABEL_31;
}
if ( DEXUXDEBUGMODE )
break;
if ( (int)v15 > 0 )
goto LABEL_40;
LABEL_41:
v29 = -1;
v30 = -1;
LABEL_31:
if ( DEXUXDEBUGMODE )
{
fwrite(" headerdata\n", 1uLL, 0xDuLL, stderr);
if ( DEXUXDEBUGMODE )
{
fwrite(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15\n", 1uLL, 0x46uLL, stderr);
if ( DEXUXDEBUGMODE )
fprintf(
stderr,
" %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %c %c %c %c %c %c %c %c %c %c %c %c\n",
v21,
v22,
v23,
v24,
v25,
v26,
v27,
v28,
v29,
v30,
(unsigned __int8)v31[0],
(unsigned __int8)v31[1],
(unsigned __int8)v31[2],
(unsigned __int8)v31[3],
(unsigned __int8)v31[4],
(unsigned __int8)v31[5],
(unsigned __int8)v31[6],
(unsigned __int8)v31[7],
(unsigned __int8)v31[8],
(unsigned __int8)v31[9],
(unsigned __int8)v31[10],
(unsigned __int8)v31[11]);
}
}
v33[0] = dexuxstrxor(&v21, 127LL, v16);
v18 = 0;
v17 = *(_DWORD *)&v33[4 * v15 + 17];
v19 = v17;
if ( DEXUXDEBUGMODE )
{
fprintf(stderr, " Writting to block #%d\n", v17);
if ( DEXUXDEBUGMODE )
fprintf(stderr, " Address %d %d\n", v18, v19);
}
dexuxputframe(a1, &v18, &v21);
dexuxputblock(a1, v17, v10);
v10 += 0x2000LL;
if ( ++v15 == a5 )
return 0LL;
}
fwrite(" blockcnt == (imagesize - 1)\n", 1uLL, 0x1EuLL, stderr);
if ( (int)v15 <= 0 )
goto LABEL_41;
if ( DEXUXDEBUGMODE )
fwrite(" blockcnt > 0\n", 1uLL, 0xFuLL, stderr);
LABEL_40:
v21 = 83;
goto LABEL_41;
}
return 0LL;
}

original_code:
int dexwritesingle(int dexuxfd, char *fname, char *fileformat, unsigned char *pblock, int imagesize) {
  int copycnt, progresscnt;
  int blockcnt, offset=0, blocksavailable=0, blocksavaillist[15];
  unsigned char headerdata[129], headerdatain[129], framerequest[2], *pframerequest;
    pframerequest = framerequest;
    if (DEXUXDEBUGMODE) fprintf(stderr, "dexwritesingle()\n  Creating TOC entry...\n");
    if (strcmp(fileformat, "PSX") == 0) {
      if (DEXUXDEBUGMODE) fprintf(stderr, "  File is in PSX format with imagesize of %d\n", imagesize);
      headerdata[0] = 0x51;
      headerdata[1] = 0;
      headerdata[2] = 0;
      headerdata[3] = 0;
      headerdata[4] = 0;
      if (imagesize < 8) {
        headerdata[5] = imagesize << 5;
        headerdata[6] = 0;
      } else {
        headerdata[5] = (imagesize - 8) << 5;
        headerdata[6] = 1;
      }
      headerdata[7] = 0;
      headerdata[9] = 0;
      for (copycnt = 0; copycnt <= 20; copycnt++) {
        headerdata[copycnt + 10] = pblock[copycnt];
      }
      for (copycnt = 0x1E; copycnt < 0x7F; copycnt++) {
        headerdata[copycnt] = 0;
      }
      headerdata[127] = dexuxstrxor(headerdata, 127);
      pblock += 74;
    } else {
      if (DEXUXDEBUGMODE) fprintf(stderr, "  File is in MCS format with imagesize of %d\n", imagesize);
      for (copycnt = 0; copycnt < 128; copycnt++) {
        headerdata[copycnt] = pblock[copycnt];
      }
      pblock += 128;
    }
    for (blockcnt = 1; blockcnt <= 15; blockcnt++) {
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blockcnt;
      dexuxgetframe(dexuxfd, framerequest, headerdatain);
      if ((headerdatain[0] & 240) == 0xA0) {
        blocksavaillist[blocksavailable] = blockcnt;
        blocksavailable++;
        if (DEXUXDEBUGMODE) fprintf(stderr, "  Available block found: #%d\n", blockcnt);
      }
      if (imagesize == blocksavailable) break;
    }
    if (imagesize > blocksavailable) {
      fprintf(stderr, "File requires %d block(s) but only", imagesize);
      fprintf(stderr, " %d block(s) available on the card\n", blocksavailable);
      fprintf(stderr, "Skipping file: %s\n", fname);
      return(1);
    }
    fprintf(stderr, "Progress for %s (%d blocks in size):\n", fname, imagesize);
    for (blockcnt = 0; blockcnt < imagesize; blockcnt++) {
      fprintf(stderr, "  Writting block #%d\n", blockcnt + 1);
      if (blockcnt != (imagesize - 1)) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt != (imagesize -1)\n");
        if (blockcnt > 0) {
          if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt > 0\n");
          headerdata[0] = 0x52;
        }
        headerdata[8] = blocksavaillist[blockcnt + 1] - 1;
        headerdata[9] = 0;
      } else {
        if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt == (imagesize - 1)\n");
        if (blockcnt > 0) {
          if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt > 0\n");
          headerdata[0] = 0x53;
        }
        headerdata[8] = 0xFF;
        headerdata[9] = 0xFF;
      }
      if (DEXUXDEBUGMODE) fprintf(stderr, "  headerdata\n");
      if (DEXUXDEBUGMODE) fprintf(stderr, "    00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15\n");
      if (DEXUXDEBUGMODE) fprintf(stderr, "    %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",
        headerdata[ 0], headerdata[ 1], headerdata[ 2], headerdata[ 3],
        headerdata[ 4], headerdata[ 5], headerdata[ 6], headerdata[ 7],
        headerdata[ 8], headerdata[ 9], headerdata[10], headerdata[11],
        headerdata[12], headerdata[13], headerdata[14], headerdata[15],
        headerdata[16], headerdata[17], headerdata[18], headerdata[19],
        headerdata[20], headerdata[21]);
      headerdata[127] = dexuxstrxor(headerdata, 127);
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blocksavaillist[blockcnt];
      if (DEXUXDEBUGMODE) fprintf(stderr, "  Writting to block #%d\n", blocksavaillist[blockcnt]);
      if (DEXUXDEBUGMODE) fprintf(stderr, "  Address %d %d\n", *(pframerequest+0), *(pframerequest+1));
      dexuxputframe(dexuxfd, framerequest, headerdata);
      dexuxputblock(dexuxfd, blocksavaillist[blockcnt], pblock);
      pblock += 8192;
    }
  return(0);
}

canonical_original_code:
int dexwritesingle(int dexuxfd, char *fname, char *fileformat, unsigned char *pblock, int imagesize) {
  int copycnt; int progresscnt;
  int blockcnt; int offset = 0; int blocksavailable = 0; int blocksavaillist[15];
  unsigned char headerdata[129]; unsigned char headerdatain[129]; unsigned char framerequest[2]; unsigned char *pframerequest;
    pframerequest = framerequest;
    if (DEXUXDEBUGMODE) fprintf(stderr, "dexwritesingle()\n  Creating TOC entry...\n");
    if (strcmp(fileformat, "PSX") == 0) {
      if (DEXUXDEBUGMODE) fprintf(stderr, "  File is in PSX format with imagesize of %d\n", imagesize);
      headerdata[0] = 0x51;
      headerdata[1] = 0;
      headerdata[2] = 0;
      headerdata[3] = 0;
      headerdata[4] = 0;
      if (imagesize < 8) {
        headerdata[5] = imagesize << 5;
        headerdata[6] = 0;
      } else {
        headerdata[5] = (imagesize - 8) << 5;
        headerdata[6] = 1;
      }
      headerdata[7] = 0;
      headerdata[9] = 0;
      for (copycnt = 0; copycnt <= 20; copycnt++) {
        headerdata[copycnt + 10] = pblock[copycnt];
      }
      for (copycnt = 0x1E; copycnt < 0x7F; copycnt++) {
        headerdata[copycnt] = 0;
      }
      headerdata[127] = dexuxstrxor(headerdata, 127);
      pblock += 74;
    } else {
      if (DEXUXDEBUGMODE) fprintf(stderr, "  File is in MCS format with imagesize of %d\n", imagesize);
      for (copycnt = 0; copycnt < 128; copycnt++) {
        headerdata[copycnt] = pblock[copycnt];
      }
      pblock += 128;
    }
    for (blockcnt = 1; blockcnt <= 15; blockcnt++) {
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blockcnt;
      dexuxgetframe(dexuxfd, framerequest, headerdatain);
      if ((headerdatain[0] & 240) == 0xA0) {
        blocksavaillist[blocksavailable] = blockcnt;
        blocksavailable++;
        if (DEXUXDEBUGMODE) fprintf(stderr, "  Available block found: #%d\n", blockcnt);
      }
      if (imagesize == blocksavailable) break;
    }
    if (imagesize > blocksavailable) {
      fprintf(stderr, "File requires %d block(s) but only", imagesize);
      fprintf(stderr, " %d block(s) available on the card\n", blocksavailable);
      fprintf(stderr, "Skipping file: %s\n", fname);
      return(1);
    }
    fprintf(stderr, "Progress for %s (%d blocks in size):\n", fname, imagesize);
    for (blockcnt = 0; blockcnt < imagesize; blockcnt++) {
      fprintf(stderr, "  Writting block #%d\n", blockcnt + 1);
      if (blockcnt != (imagesize - 1)) {
 if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt != (imagesize -1)\n");
        if (blockcnt > 0) {
          if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt > 0\n");
          headerdata[0] = 0x52;
        }
        headerdata[8] = blocksavaillist[blockcnt + 1] - 1;
        headerdata[9] = 0;
      } else {
        if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt == (imagesize - 1)\n");
        if (blockcnt > 0) {
          if (DEXUXDEBUGMODE) fprintf(stderr, "  blockcnt > 0\n");
          headerdata[0] = 0x53;
        }
        headerdata[8] = 0xFF;
        headerdata[9] = 0xFF;
      }
      if (DEXUXDEBUGMODE) fprintf(stderr, "  headerdata\n");
      if (DEXUXDEBUGMODE) fprintf(stderr, "    00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15\n");
      if (DEXUXDEBUGMODE) fprintf(stderr, "    %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",
        headerdata[ 0], headerdata[ 1], headerdata[ 2], headerdata[ 3],
        headerdata[ 4], headerdata[ 5], headerdata[ 6], headerdata[ 7],
        headerdata[ 8], headerdata[ 9], headerdata[10], headerdata[11],
        headerdata[12], headerdata[13], headerdata[14], headerdata[15],
        headerdata[16], headerdata[17], headerdata[18], headerdata[19],
        headerdata[20], headerdata[21]);
      headerdata[127] = dexuxstrxor(headerdata, 127);
      *(pframerequest+0) = 0;
      *(pframerequest+1) = blocksavaillist[blockcnt];
      if (DEXUXDEBUGMODE) fprintf(stderr, "  Writting to block #%d\n", blocksavaillist[blockcnt]);
      if (DEXUXDEBUGMODE) fprintf(stderr, "  Address %d %d\n", *(pframerequest+0), *(pframerequest+1));
      dexuxputframe(dexuxfd, framerequest, headerdata);
      dexuxputblock(dexuxfd, blocksavaillist[blockcnt], pblock);
      pblock += 8192;
    }
  return(0);
}

variable_types: dexuxfd int

variable_types:
fname
char *

variable_types:
fileformat
char *

variable_types:
pblock
unsigned char *

variable_types: imagesize int

variable_types: copycnt int

variable_types: progresscnt int

variable_types: blockcnt int

variable_types: offset int

variable_types: blocksavailable int

variable_types:
blocksavaillist
int [15]

variable_types:
headerdata
unsigned char [129]

variable_types:
headerdatain
unsigned char [129]

variable_types:
framerequest
unsigned char [2]

variable_types:
pframerequest
unsigned char *


repo: mikestaub/Pascal-Compiler

decompiled_code:
__int64 __fastcall init_stack(__int64 a1)
{
*(_QWORD *)(a1 + 800) = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
*(_QWORD *)(a1 + 800) = a1;
return 0LL;
}

original_code:
extern status init_stack ( stack *p_S ) {
  p_S -> top = p_S -> base ;
  return OK ;
}

canonical_original_code:
extern enum status init_stack(struct stack *p_S) {
  p_S -> top = p_S -> base ;
  return OK ;
}

variable_types: struct stack { void *base[100]; void **top; } *

return_type: enum status { OK=0, ERROR=1 }


pop

decompiled_code:
_BOOL8 __fastcall pop(__int64 a1, _QWORD *a2)
{
_BOOL8 result; // rax
_QWORD *v3; // rdx

result = empty_stack(a1);
if ( !result )
{
v3 = (_QWORD *)(*(_QWORD *)(a1 + 800) - 8LL);
*(_QWORD *)(a1 + 800) = v3;
*a2 = *v3;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, _QWORD *a2)
{
_BOOL8 result; // rax
_QWORD *v3; // rdx

result = func1(a1);
if ( !result )
{
v3 = (_QWORD *)(*(_QWORD *)(a1 + 800) - 8LL);
*(_QWORD *)(a1 + 800) = v3;
*a2 = *v3;
result = 0LL;
}
return result;
}

original_code:
extern status pop( stack *p_S , generic_ptr *p_data ) {
  if ( empty_stack ( p_S ) == TRUE ) return ERROR ;
  p_S -> top-- ;
  *p_data = *p_S->top ;
  return OK ;
}

canonical_original_code:
extern enum status pop(struct stack *p_S, void **p_data) {
  if ( empty_stack ( p_S ) == TRUE ) return ERROR ;
  p_S -> top-- ;
  *p_data = *p_S->top ;
  return OK ;
}

user_defined_types: struct stack { void *base[100]; void **top; }


push

original_code:
extern status push ( stack *p_S , generic_ptr data ) {
  if ( ( (p_S) -> top - (p_S) -> base ) == 100 )
    return ERROR ;
  *p_S -> top = data ;
  p_S -> top++ ;
  return OK ;
}

canonical_original_code:
extern enum status push(struct stack *p_S, void *data) {
  if ( ( (p_S) -> top - (p_S) -> base ) == 100 )
    return ERROR ;
  *p_S -> top = data ;
  p_S -> top++ ;
  return OK ;
}

variable_types:
data
void *


top

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

result = pop(a1, a2);
if ( (_DWORD)result != 1 )
result = push(a1, *a2);
return result;
}

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

result = func2(a1, a2);
if ( (_DWORD)result != 1 )
result = func3(a1, *a2);
return result;
}

original_code:
extern status top ( stack *p_S , generic_ptr *p_data ) {
  if ( pop ( p_S , p_data ) == ERROR ) return ERROR ;
       return push ( p_S , *p_data ) ;
}

canonical_original_code:
extern enum status top(struct stack *p_S, void **p_data) {
  if ( pop ( p_S , p_data ) == ERROR ) return ERROR ;
       return push ( p_S , *p_data ) ;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
yydebug = 0;
yyin = fopen(argv[1], "r");
input_file_name = (int)safe_malloc(1LL);
strcpy((char *)input_file_name, argv[1]);
if ( (unsigned int)yyparse() )
puts("The file fails to parse.");
else
cg(program_tac);
fclose(yyin);
return 0;
}

original_code:
int main(int argc, char* argv[]){
  yydebug = 0;
  yyin = fopen(argv[1], "r");
  int len = strlen(argv[1]);
  input_file_name = (char*)safe_malloc(sizeof(char));
  strcpy(input_file_name, argv[1]);
  if(!yyparse()){
     cg(program_tac);
  }
  else
     printf("The file fails to parse.\n");
  fclose(yyin);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  yydebug = 0;
  yyin = fopen(argv[1], "r");
  int len = strlen(argv[1]);
  input_file_name = (char *)safe_malloc(sizeof(char));
  strcpy(input_file_name, argv[1]);
  if(!yyparse()){
     cg(program_tac);
  }
  else
     printf("The file fails to parse.\n");
  fclose(yyin);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: len int


yyerror

decompiled_code:
int __fastcall yyerror(const char *a1)
{
return printf("Error parsing, %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return printf("Error parsing, %s\n", a1);
}

original_code:
void yyerror(char* text){
  printf("Error parsing, %s\n", text);
}

canonical_original_code:
void yyerror(char *text){
  printf("Error parsing, %s\n", text);
}

variable_types:
text
char *

return_type: void


decompiled_code:
__int64 __fastcall push_offset(__int64 a1, int a2)
{
_DWORD *v2; // rax
void *v3; // rbx
unsigned int v4; // ebp

v2 = malloc(4uLL);
if ( !v2 )
return 1;
v3 = v2;
*v2 = a2;
v4 = push(a1, v2);
if ( v4 != 1 )
return 0;
free(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
_DWORD *v2; // rax
void *v3; // rbx
unsigned int v4; // ebp

v2 = malloc(4uLL);
if ( !v2 )
return 1;
v3 = v2;
*v2 = a2;
v4 = push(a1, v2);
if ( v4 != 1 )
return 0;
free(v3);
return v4;
}

original_code:
status push_offset ( stack *p_S , int offset ) {
  int *p_c = (int *) malloc (sizeof(int)) ;
  if ( p_c == ((void *)0) ) return ERROR ;
  *p_c = offset ;
  if ( push(p_S, (generic_ptr) p_c) == ERROR ) {
    free (p_c); return ERROR ;
  }
  return OK ;
}

canonical_original_code:
enum status push_offset(struct stack *p_S, int offset) {
  int *p_c = (int *) malloc (sizeof(int));
  if ( p_c == ((void *)0) ) return ERROR ;
  *p_c = offset ;
  if ( push(p_S, (void *) p_c) == ERROR ) {
    free (p_c); return ERROR ;
  }
  return OK ;
}


decompiled_code:
__int64 __fastcall pop_offset(__int64 a1, _DWORD *a2)
{
__int64 result; // rax
void *v3; // rdi
void *ptr; // [rsp+8h] [rbp-10h] BYREF

result = pop(a1, &ptr);
if ( (_DWORD)result != 1 )
{
v3 = ptr;
*a2 = *(_DWORD *)ptr;
free(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _DWORD *a2)
{
__int64 result; // rax
void *v3; // rdi
void *ptr; // [rsp+8h] [rbp-10h] BYREF

result = pop(a1, &ptr);
if ( (_DWORD)result != 1 )
{
v3 = ptr;
*a2 = *(_DWORD *)ptr;
free(v3);
result = 0LL;
}
return result;
}

original_code:
status pop_offset ( stack *p_S , int *offset ) {
  int *p_data ;
  if ( pop( p_S, (generic_ptr *)&p_data ) == ERROR )
    return ERROR ;
  *offset = *p_data ;
  free (p_data) ;
  return OK ;
}

canonical_original_code:
enum status pop_offset(struct stack *p_S, int *offset) {
  int *p_data;
  if ( pop( p_S, (void **)&p_data ) == ERROR )
    return ERROR ;
  *offset = *p_data ;
  free (p_data) ;
  return OK ;
}

variable_types:
offset
int *


decompiled_code:
_BOOL8 __fastcall top_offset(__int64 a1)
{
char v2[16]; // [rsp+8h] [rbp-10h] BYREF

return (unsigned int)top(a1, v2) == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1)
{
char v2[16]; // [rsp+8h] [rbp-10h] BYREF

return (unsigned int)top(a1, v2) == 1;
}

original_code:
status top_offset ( stack *p_S , int *p_c ) {
  int *p_data ;
  if ( top(p_S, (generic_ptr *) &p_data ) == ERROR )
    return ERROR ;
  p_c = p_data ;
  return OK ;
}

canonical_original_code:
enum status top_offset(struct stack *p_S, int *p_c) {
  int *p_data;
  if ( top(p_S, (void **) &p_data ) == ERROR )
    return ERROR ;
  p_c = p_data ;
  return OK ;
}


error

decompiled_code:
int __fastcall error(const char *a1)
{
return fprintf(stderr, "Compiler: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "Compiler: %s\n", a1);
}

original_code:
void error(char* s){
  fprintf(stderr, "Compiler: %s\n", s);
}

canonical_original_code:
void error(char *s){
  fprintf(stderr, "Compiler: %s\n", s);
}

variable_types:
s
char *


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

result = malloc(a1);
if ( !result )
{
error("malloc() failed");
exit(0);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
func0("malloc() failed");
exit(0);
}
return result;
}

original_code:
void *safe_malloc(int n){
  void *t = malloc(n);
  if(t == ((void *)0)){
    error("malloc() failed");
    exit(0);
  }
  return t;
}

variable_types: n int

variable_types:
t
void *

return_type: void *