LoadTheme

repo: atextor/blockmaster

decompiled_code:
__int64 __fastcall LoadTheme(const char *a1)
{
__int64 v2; // rbx
char v3; // dl
char v4; // al
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rdi
int v8; // edx
__int64 v9; // rbx
char v10; // al
__int64 v11; // rdi
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rdi
__int64 v15; // rbx
char i; // al
__int64 v17; // rax
__int64 v18; // rax
__int64 v19; // rdi
__int64 v20; // rax
__int64 v21; // rdi
__int64 v22; // rbx
char j; // al
__int64 v24; // rax
__int64 v25; // rax
__int64 v26; // rdi
__int64 result; // rax
char v28[10]; // [rsp+Fh] [rbp-E9h] BYREF
char v29[17]; // [rsp+1Bh] [rbp-DDh] BYREF
_WORD v30[8]; // [rsp+2Eh] [rbp-CAh] BYREF
char v31; // [rsp+3Eh] [rbp-BAh]
char v32[12]; // [rsp+44h] [rbp-B4h] BYREF
_OWORD v33[3]; // [rsp+50h] [rbp-A8h]
char v34[8]; // [rsp+80h] [rbp-78h] BYREF
char s[104]; // [rsp+90h] [rbp-68h] BYREF

v2 = 0LL;
strcpy((char *)v30, "abcdefghij");
strcpy(v32, "blockXX.bmp");
v33[0] = _mm_load_si128((const __m128i *)&LC5);
strcpy(v29, "guyX.bmp");
strcpy(&v29[9], "menuX.bmp");
v33[1] = _mm_load_si128((const __m128i *)&LC6);
strcpy((char *)&v30[5] + 1, "aniXXY.bmp");
strcpy(v34, "23");
strcpy(v28, "01234");
strcpy(&v28[6], "01234");
v33[2] = _mm_load_si128((const __m128i *)&LC7);
printf(" Loading Theme %s...\n", a1);
v3 = 48;
v4 = 48;
while ( 1 )
{
v32[5] = v4;
v32[6] = v3;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, v32);
v5 = SDL_RWFromFile(s, "rb");
v6 = SDL_LoadBMP_RW(v5, 1LL);
*(_QWORD *)((char *)&block + 4 * v2) = v6;
v7 = v6;
if ( !v6 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v7 = *(_QWORD *)((char *)&block + 4 * v2);
}
v2 += 2LL;
SDL_SetColorKey(v7, 4096LL, 0LL);
if ( v2 == 50 )
break;
v4 = *((_BYTE *)v33 + v2);
v3 = *((_BYTE *)v33 + v2 + 1);
}
v8 = animation[10];
v9 = 0LL;
v30[7] = 12592;
v10 = 97;
if ( v8 > 0 )
{
while ( 1 )
{
v31 = v10;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, (const char *)&v30[5] + 1);
v12 = SDL_RWFromFile(s, "rb");
v13 = SDL_LoadBMP_RW(v12, 1LL);
animation[v9] = v13;
v14 = v13;
if ( !v13 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v14 = animation[v9];
}
SDL_SetColorKey(v14, 4096LL, 0LL);
v11 = animation[v9++];
SDL_SetAlpha(v11, &unk_10000, 100LL);
if ( SLODWORD(animation[10]) <= (int)v9 )
break;
v10 = *((_BYTE *)v30 + v9);
}
}
v15 = 0LL;
for ( i = 48; ; i = v28[v15] )
{
v29[3] = i;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, v29);
v17 = SDL_RWFromFile(s, "rb");
v18 = SDL_LoadBMP_RW(v17, 1LL);
characters[v15] = v18;
v19 = v18;
if ( !v18 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v19 = characters[v15];
}
++v15;
SDL_SetColorKey(v19, 4096LL, 0LL);
if ( v15 == 5 )
break;
}
SDL_SetAlpha(characters[4], &unk_10000, 15LL);
sprintf(s, "%s%s/font.bmp", (const char *)(unsigned int)((_DWORD)&path + 510), a1);
v20 = SDL_RWFromFile(s, "rb");
font = SDL_LoadBMP_RW(v20, 1LL);
v21 = font;
if ( !font )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v21 = font;
}
v22 = 0LL;
SDL_SetColorKey(v21, 4096LL, 0LL);
InitFont(font);
for ( j = 48; ; j = v28[v22 + 6] )
{
v29[13] = j;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, &v29[9]);
v24 = SDL_RWFromFile(s, "rb");
v25 = SDL_LoadBMP_RW(v24, 1LL);
*((_QWORD *)&menu + v22) = v25;
v26 = v25;
if ( !v25 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v26 = *((_QWORD *)&menu + v22);
}
++v22;
result = SDL_SetColorKey(v26, 4096LL, 0LL);
if ( v22 == 5 )
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
__int64 v2; // rbx
char v3; // dl
char v4; // al
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rdi
int v8; // edx
__int64 v9; // rbx
char v10; // al
__int64 v11; // rdi
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rdi
__int64 v15; // rbx
char i; // al
__int64 v17; // rax
__int64 v18; // rax
__int64 v19; // rdi
__int64 v20; // rax
__int64 v21; // rdi
__int64 v22; // rbx
char j; // al
__int64 v24; // rax
__int64 v25; // rax
__int64 v26; // rdi
__int64 result; // rax
char v28[10]; // [rsp+Fh] [rbp-E9h] BYREF
char v29[17]; // [rsp+1Bh] [rbp-DDh] BYREF
_WORD v30[8]; // [rsp+2Eh] [rbp-CAh] BYREF
char v31; // [rsp+3Eh] [rbp-BAh]
char v32[12]; // [rsp+44h] [rbp-B4h] BYREF
_OWORD v33[3]; // [rsp+50h] [rbp-A8h]
char v34[8]; // [rsp+80h] [rbp-78h] BYREF
char s[104]; // [rsp+90h] [rbp-68h] BYREF

v2 = 0LL;
strcpy((char *)v30, "abcdefghij");
strcpy(v32, "blockXX.bmp");
v33[0] = _mm_load_si128((const __m128i *)&LC5);
strcpy(v29, "guyX.bmp");
strcpy(&v29[9], "menuX.bmp");
v33[1] = _mm_load_si128((const __m128i *)&LC6);
strcpy((char *)&v30[5] + 1, "aniXXY.bmp");
strcpy(v34, "23");
strcpy(v28, "01234");
strcpy(&v28[6], "01234");
v33[2] = _mm_load_si128((const __m128i *)&LC7);
printf(" Loading Theme %s...\n", a1);
v3 = 48;
v4 = 48;
while ( 1 )
{
v32[5] = v4;
v32[6] = v3;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, v32);
v5 = SDL_RWFromFile(s, "rb");
v6 = SDL_LoadBMP_RW(v5, 1LL);
*(_QWORD *)((char *)&block + 4 * v2) = v6;
v7 = v6;
if ( !v6 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v7 = *(_QWORD *)((char *)&block + 4 * v2);
}
v2 += 2LL;
SDL_SetColorKey(v7, 4096LL, 0LL);
if ( v2 == 50 )
break;
v4 = *((_BYTE *)v33 + v2);
v3 = *((_BYTE *)v33 + v2 + 1);
}
v8 = animation[10];
v9 = 0LL;
v30[7] = 12592;
v10 = 97;
if ( v8 > 0 )
{
while ( 1 )
{
v31 = v10;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, (const char *)&v30[5] + 1);
v12 = SDL_RWFromFile(s, "rb");
v13 = SDL_LoadBMP_RW(v12, 1LL);
animation[v9] = v13;
v14 = v13;
if ( !v13 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v14 = animation[v9];
}
SDL_SetColorKey(v14, 4096LL, 0LL);
v11 = animation[v9++];
SDL_SetAlpha(v11, &unk_10000, 100LL);
if ( SLODWORD(animation[10]) <= (int)v9 )
break;
v10 = *((_BYTE *)v30 + v9);
}
}
v15 = 0LL;
for ( i = 48; ; i = v28[v15] )
{
v29[3] = i;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, v29);
v17 = SDL_RWFromFile(s, "rb");
v18 = SDL_LoadBMP_RW(v17, 1LL);
characters[v15] = v18;
v19 = v18;
if ( !v18 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v19 = characters[v15];
}
++v15;
SDL_SetColorKey(v19, 4096LL, 0LL);
if ( v15 == 5 )
break;
}
SDL_SetAlpha(characters[4], &unk_10000, 15LL);
sprintf(s, "%s%s/font.bmp", (const char *)(unsigned int)((_DWORD)&path + 510), a1);
v20 = SDL_RWFromFile(s, "rb");
font = SDL_LoadBMP_RW(v20, 1LL);
v21 = font;
if ( !font )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v21 = font;
}
v22 = 0LL;
SDL_SetColorKey(v21, 4096LL, 0LL);
InitFont(font);
for ( j = 48; ; j = v28[v22 + 6] )
{
v29[13] = j;
sprintf(s, "%s%s/%s", (const char *)(unsigned int)((_DWORD)&path + 510), a1, &v29[9]);
v24 = SDL_RWFromFile(s, "rb");
v25 = SDL_LoadBMP_RW(v24, 1LL);
*((_QWORD *)&menu + v22) = v25;
v26 = v25;
if ( !v25 )
{
printf("*** Loading of %s failed.\n", s);
quit(1LL);
v26 = *((_QWORD *)&menu + v22);
}
++v22;
result = SDL_SetColorKey(v26, 4096LL, 0LL);
if ( v22 == 5 )
break;
}
return result;
}

original_code:
void LoadTheme(char themename[])
{
 char blockToFile[] = "00010202030405060708091011121314151617181920212223";
 char charToFile[] = "01234";
 char menuToFile[] = "01234";
 char aniToFile[] = "01";
 char aniphaseToFile[] = "abcdefghij";
 char fullpath[80];
    char blockfilename[] = "blockXX.bmp";
 char charfilename[] = "guyX.bmp";
 char menufilename[] = "menuX.bmp";
 char anifilename[] = "aniXXY.bmp";
 int i, j;
 printf (" Loading Theme %s...\n", themename);
    for (i = 0; i<25; i++) {
  blockfilename[5] = blockToFile[(i*2)];
        blockfilename[6] = blockToFile[(i*2)+1];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, blockfilename);
        block[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!block[i]) {
            printf ("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(block[i], 0x00001000, 0);
    }
 for (i = 0; i<1; i++) {
  anifilename[3] = aniToFile[(i*2)];
  anifilename[4] = aniToFile[(i*2)+1];
  for (j = 0; j<animation[i].numphases; j++) {
   anifilename[5] = aniphaseToFile[j];
   sprintf(fullpath, "%s%s/%s", path.themes, themename, anifilename);
   animation[i].gfx[j] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
   if (!animation[i].gfx[j]) {
    printf ("*** Loading of %s failed.\n", fullpath);
    quit(1);
   }
   SDL_SetColorKey(animation[i].gfx[j], 0x00001000, 0);
   SDL_SetAlpha(animation[0].gfx[j], 0x00010000, 100);
  }
 }
 for (i = 0; i<5; i++) {
  charfilename[3] = charToFile[i];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, charfilename);
  characters[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!characters[i]) {
   printf("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(characters[i], 0x00001000, 0);
 }
 SDL_SetAlpha(characters[4], 0x00010000, 15);
 sprintf(fullpath, "%s%s/font.bmp", path.themes, themename);
 font[0] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
 if (!font[0]) {
  printf( "*** Loading of %s failed.\n", fullpath);
  quit(1);
 }
 SDL_SetColorKey(font[0], 0x00001000, 0);
 InitFont(font[0]);
 for (i = 0; i<5; i++) {
  menufilename[4] = menuToFile[i];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, menufilename);
  menu[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!menu[i]) {
   printf("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(menu[i], 0x00001000, 0);
 }
}

canonical_original_code:
void LoadTheme(char themename[])
{
 char blockToFile[] = "00010202030405060708091011121314151617181920212223";
 char charToFile[] = "01234";
 char menuToFile[] = "01234";
 char aniToFile[] = "01";
 char aniphaseToFile[] = "abcdefghij";
 char fullpath[80];
    char blockfilename[] = "blockXX.bmp";
 char charfilename[] = "guyX.bmp";
 char menufilename[] = "menuX.bmp";
 char anifilename[] = "aniXXY.bmp";
 int i; int j;
 printf (" Loading Theme %s...\n", themename);
    for (i = 0; i<25; i++) {
  blockfilename[5] = blockToFile[(i*2)];
        blockfilename[6] = blockToFile[(i*2)+1];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, blockfilename);
        block[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!block[i]) {
            printf ("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(block[i], 0x00001000, 0);
    }
 for (i = 0; i<1; i++) {
  anifilename[3] = aniToFile[(i*2)];
  anifilename[4] = aniToFile[(i*2)+1];
  for (j = 0; j<animation[i].numphases; j++) {
   anifilename[5] = aniphaseToFile[j];
   sprintf(fullpath, "%s%s/%s", path.themes, themename, anifilename);
   animation[i].gfx[j] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
   if (!animation[i].gfx[j]) {
    printf ("*** Loading of %s failed.\n", fullpath);
    quit(1);
   }
   SDL_SetColorKey(animation[i].gfx[j], 0x00001000, 0);
   SDL_SetAlpha(animation[0].gfx[j], 0x00010000, 100);
  }
 }
 for (i = 0; i<5; i++) {
  charfilename[3] = charToFile[i];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, charfilename);
  characters[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!characters[i]) {
   printf("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(characters[i], 0x00001000, 0);
 }
 SDL_SetAlpha(characters[4], 0x00010000, 15);
 sprintf(fullpath, "%s%s/font.bmp", path.themes, themename);
 font[0] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
 if (!font[0]) {
  printf( "*** Loading of %s failed.\n", fullpath);
  quit(1);
 }
 SDL_SetColorKey(font[0], 0x00001000, 0);
 InitFont(font[0]);
 for (i = 0; i<5; i++) {
  menufilename[4] = menuToFile[i];
  sprintf(fullpath, "%s%s/%s", path.themes, themename, menufilename);
  menu[i] = SDL_LoadBMP_RW(SDL_RWFromFile(fullpath, "rb"), 1);
  if (!menu[i]) {
   printf("*** Loading of %s failed.\n", fullpath);
   quit(1);
  }
  SDL_SetColorKey(menu[i], 0x00001000, 0);
 }
}

variable_types:
themename
char []

variable_types:
blockToFile
char []

variable_types:
charToFile
char []

variable_types:
menuToFile
char []

variable_types:
aniToFile
char []

variable_types:
aniphaseToFile
char []

variable_types:
fullpath
char [80]

variable_types:
blockfilename
char []

variable_types:
charfilename
char []

variable_types:
menufilename
char []

variable_types:
anifilename
char []

variable_types: i int


removeBlock

decompiled_code:
void __fastcall removeBlock(int a1, int a2)
{
level[150 * a1 + a2] = 0;
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
level[150 * a1 + a2] = 0;
}

original_code:
void removeBlock(int x, int y)
{
 level.data[x][y] = 0;
}

variable_types: x int

variable_types: y int

return_type: void


takeItem

decompiled_code:
void __fastcall takeItem(int a1, int a2)
{
int v2; // eax

v2 = level[150 * a1 + a2];
if ( v2 == 14 )
{
++*(&inventory + 1);
removeBlock(a1, a2);
}
else if ( v2 > 14 )
{
if ( v2 == 15 )
{
++*(&inventory + 3);
removeBlock(a1, a2);
}
}
else if ( v2 == 12 )
{
++inventory;
removeBlock(a1, a2);
}
else if ( v2 == 13 )
{
++*(&inventory + 2);
removeBlock(a1, a2);
}
}

original_code:
int takeItem(int x, int y)
{
 switch ( level.data[x][y] )
 {
  case 12:
   inventory.redKeys++;
   removeBlock(x, y);
   break;
  case 13:
   inventory.greenKeys++;
   removeBlock(x, y);
   break;
  case 14:
   inventory.blueKeys++;
   removeBlock(x, y);
   break;
  case 15:
   inventory.yellowKeys++;
   removeBlock(x, y);
   break;
  default:
   return 0;
   break;
 }
 return 1;
}

return_type: int


hasBlockBit

decompiled_code:
__int64 __fastcall hasBlockBit(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // er12
bool v6; // al

switch ( level[150 * (int)a1 + (int)a2] )
{
case 0:
case 2:
case 4:
case 5:
case 6:
case 7:
case 10:
case 20:
return (a3 == 8) | (unsigned __int8)(a3 == 1);
case 3:
case 11:
return a3 == 1;
case 8:
return ((a3 - 8) & 0xFFFFFFF7) == 0;
case 9:
v4 = player[5 * chosenGuy + 2];
if ( v4 == 2 )
{
if ( (unsigned int)hasBlockBit(a1, (unsigned int)(a2 - 1), 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
else if ( (int)v4 > 2 )
{
if ( v4 == 3 && (unsigned int)hasBlockBit((unsigned int)(a1 - 1), a2, 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
else
{
if ( !v4 )
{
if ( (unsigned int)hasBlockBit(a1, (unsigned int)(a2 + 1), 8LL) != 1 )
return v4;
return (unsigned int)(a3 - 1) <= 1;
}
if ( v4 == 1 && (unsigned int)hasBlockBit((unsigned int)(a1 + 1), a2, 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
return 0LL;
case 12:
case 13:
case 14:
case 15:
return (a3 == 4) | (unsigned __int8)(a3 == 1);
case 16:
v4 = 0;
if ( inventory <= 0 )
return v4;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 17:
if ( *(&inventory + 2) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 18:
if ( *(&inventory + 1) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 19:
if ( *(&inventory + 3) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 21:
v4 = 0;
if ( toggleBlocks == 1 )
return (a3 == 8) | (unsigned __int8)(a3 == 1);
return v4;
case 22:
if ( toggleBlocks )
return 0LL;
return (a3 == 8) | (unsigned __int8)(a3 == 1);
case 23:
v6 = ((a3 - 8) & 0xFFFFFFF7) == 0;
v4 = v6;
if ( *(&inventory + 4) > 0 )
v4 = v6 || a3 == 1;
return v4;
case 24:
return (*(&inventory + 5) > 0) & (unsigned __int8)(a3 == 1);
default:
return 0LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // er12
bool v6; // al

switch ( level[150 * (int)a1 + (int)a2] )
{
case 0:
case 2:
case 4:
case 5:
case 6:
case 7:
case 10:
case 20:
return (a3 == 8) | (unsigned __int8)(a3 == 1);
case 3:
case 11:
return a3 == 1;
case 8:
return ((a3 - 8) & 0xFFFFFFF7) == 0;
case 9:
v4 = player[5 * chosenGuy + 2];
if ( v4 == 2 )
{
if ( (unsigned int)func2(a1, (unsigned int)(a2 - 1), 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
else if ( (int)v4 > 2 )
{
if ( v4 == 3 && (unsigned int)func2((unsigned int)(a1 - 1), a2, 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
else
{
if ( !v4 )
{
if ( (unsigned int)func2(a1, (unsigned int)(a2 + 1), 8LL) != 1 )
return v4;
return (unsigned int)(a3 - 1) <= 1;
}
if ( v4 == 1 && (unsigned int)func2((unsigned int)(a1 + 1), a2, 8LL) == 1 )
return (unsigned int)(a3 - 1) <= 1;
}
return 0LL;
case 12:
case 13:
case 14:
case 15:
return (a3 == 4) | (unsigned __int8)(a3 == 1);
case 16:
v4 = 0;
if ( inventory <= 0 )
return v4;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 17:
if ( *(&inventory + 2) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 18:
if ( *(&inventory + 1) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 19:
if ( *(&inventory + 3) <= 0 )
return 0LL;
return (a3 == 32) | (unsigned __int8)(a3 == 1);
case 21:
v4 = 0;
if ( toggleBlocks == 1 )
return (a3 == 8) | (unsigned __int8)(a3 == 1);
return v4;
case 22:
if ( toggleBlocks )
return 0LL;
return (a3 == 8) | (unsigned __int8)(a3 == 1);
case 23:
v6 = ((a3 - 8) & 0xFFFFFFF7) == 0;
v4 = v6;
if ( *(&inventory + 4) > 0 )
v4 = v6 || a3 == 1;
return v4;
case 24:
return (*(&inventory + 5) > 0) & (unsigned __int8)(a3 == 1);
default:
return 0LL;
}
}

original_code:
int hasBlockBit(int x, int y, int Bit)
{
 switch( level.data[x][y] )
 {
  case 0:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 1:
   break;
  case 2:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 3:
   if (Bit == 1) return 1;
   break;
  case 4:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 5:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 6:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 7:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 8:
   if ((Bit == 8) || (Bit == 16)) return 1;
   break;
  case 9:
   switch( player[chosenGuy].direction ) {
    case 0:
     if ( hasBlockBit(x, y+1, 8) == 1)
      if ((Bit == 1) || (Bit == 2)) return 1;
     break;
    case 1:
     if ( hasBlockBit(x+1, y, 8) == 1)
      if ((Bit == 1) || (Bit == 2)) return 1;
     break;
    case 2:
     if ( hasBlockBit(x, y-1, 8) == 1)
      if ((Bit == 1) || (Bit == 2)) return 1;
     break;
    case 3:
     if ( hasBlockBit(x-1, y, 8) == 1)
      if ((Bit == 1) || (Bit == 2)) return 1;
     break;
   }
   break;
  case 10:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 11:
   if (Bit == 1) return 1;
   break;
  case 12:
   if ((Bit == 1) || (Bit == 4)) return 1;
   break;
  case 13:
   if ((Bit == 1) || (Bit == 4)) return 1;
   break;
  case 14:
   if ((Bit == 1) || (Bit == 4)) return 1;
   break;
  case 15:
   if ((Bit == 1) || (Bit == 4)) return 1;
   break;
  case 16:
   if (inventory.redKeys > 0)
    if ((Bit == 1) || (Bit == 32)) return 1;
   break;
  case 17:
   if (inventory.greenKeys > 0)
    if ((Bit == 1) || (Bit == 32)) return 1;
   break;
  case 18:
   if (inventory.blueKeys > 0)
    if ((Bit == 1) || (Bit == 32)) return 1;
   break;
  case 19:
   if (inventory.yellowKeys > 0)
    if ((Bit == 1) || (Bit == 32)) return 1;
   break;
  case 20:
   if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 21:
   if (toggleBlocks == 1)
    if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 22:
   if (toggleBlocks == 0)
    if ((Bit == 1) || (Bit == 8)) return 1;
   break;
  case 23:
   if (inventory.flippers > 0) {
    if ((Bit == 1) || (Bit == 8) ||
     (Bit == 16)) return 1;
   } else {
    if ((Bit == 8) || (Bit == 16)) return 1;
   }
   break;
  case 24:
   if (inventory.fireshoes > 0)
    if (Bit == 1) return 1;
   break;
 }
 return 0;
}

variable_types: Bit int


movePlayer

decompiled_code:
__int64 __fastcall movePlayer(int a1, int a2)
{
unsigned int v2; // ebx
unsigned int v3; // ebp
__int64 v4; // rax
int v5; // er14
unsigned int v6; // er10
unsigned int v7; // er11
__int64 result; // rax
__int64 v9; // rax
unsigned int v10; // er10
unsigned int v11; // er11
int v12; // er10
int v13; // er11
__int64 v14; // rax
__int64 v15; // rax

if ( a2 == 2 )
{
v15 = 5LL * a1;
v3 = player[v15];
v2 = player[v15 + 1] - 1;
}
else if ( a2 > 2 )
{
if ( a2 == 3 )
{
v9 = 5LL * a1;
v2 = player[v9 + 1];
v3 = player[v9] - 1;
}
}
else if ( a2 )
{
if ( a2 == 1 )
{
v4 = 5LL * a1;
v2 = player[v4 + 1];
v3 = player[v4] + 1;
}
}
else
{
v14 = 5LL * a1;
v3 = player[v14];
v2 = player[v14 + 1] + 1;
}
v5 = level[150 * v3 + v2];
if ( (unsigned int)hasBlockBit(v3, v2, 4) )
takeItem(v3, v2);
if ( (unsigned int)hasBlockBit(v3, v2, 2) )
{
if ( (unsigned int)hasBlockBit(v6, v7, 16) )
{
removeBlock(v10, v11);
removeBlock(v3, v2);
}
else if ( (unsigned int)hasBlockBit(v10, v11, 8) )
{
level[150 * v12 + v13] = level[150 * v3 + v2];
removeBlock(v3, v2);
addActivity(v3, v2);
}
}
if ( (unsigned int)(level[150 * v3 + v2] - 4) <= 3 )
addActivity(v3, v2);
switch ( v5 )
{
case 16:
--inventory;
removeBlock(v3, v2);
break;
case 17:
--*(&inventory + 2);
removeBlock(v3, v2);
break;
case 18:
--*(&inventory + 1);
removeBlock(v3, v2);
break;
case 19:
--*(&inventory + 3);
removeBlock(v3, v2);
break;
case 20:
toggleBlocks = toggleBlocks == 0;
break;
}
result = hasBlockBit(v3, v2, 1);
if ( (_DWORD)result )
{
result = 20LL * a1;
*(int *)((char *)player + result) = v3;
*(int *)((char *)&player[1] + result) = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
unsigned int v2; // ebx
unsigned int v3; // ebp
__int64 v4; // rax
int v5; // er14
unsigned int v6; // er10
unsigned int v7; // er11
__int64 result; // rax
__int64 v9; // rax
unsigned int v10; // er10
unsigned int v11; // er11
int v12; // er10
int v13; // er11
__int64 v14; // rax
__int64 v15; // rax

if ( a2 == 2 )
{
v15 = 5LL * a1;
v3 = player[v15];
v2 = player[v15 + 1] - 1;
}
else if ( a2 > 2 )
{
if ( a2 == 3 )
{
v9 = 5LL * a1;
v2 = player[v9 + 1];
v3 = player[v9] - 1;
}
}
else if ( a2 )
{
if ( a2 == 1 )
{
v4 = 5LL * a1;
v2 = player[v4 + 1];
v3 = player[v4] + 1;
}
}
else
{
v14 = 5LL * a1;
v3 = player[v14];
v2 = player[v14 + 1] + 1;
}
v5 = level[150 * v3 + v2];
if ( (unsigned int)func2(v3, v2, 4) )
func1(v3, v2);
if ( (unsigned int)func2(v3, v2, 2) )
{
if ( (unsigned int)func2(v6, v7, 16) )
{
func0(v10, v11);
func0(v3, v2);
}
else if ( (unsigned int)func2(v10, v11, 8) )
{
level[150 * v12 + v13] = level[150 * v3 + v2];
func0(v3, v2);
addActivity(v3, v2);
}
}
if ( (unsigned int)(level[150 * v3 + v2] - 4) <= 3 )
addActivity(v3, v2);
switch ( v5 )
{
case 16:
--inventory;
func0(v3, v2);
break;
case 17:
--*(&inventory + 2);
func0(v3, v2);
break;
case 18:
--*(&inventory + 1);
func0(v3, v2);
break;
case 19:
--*(&inventory + 3);
func0(v3, v2);
break;
case 20:
toggleBlocks = toggleBlocks == 0;
break;
}
result = func2(v3, v2, 1);
if ( (_DWORD)result )
{
result = 20LL * a1;
*(int *)((char *)player + result) = v3;
*(int *)((char *)&player[1] + result) = v2;
}
return result;
}

original_code:
void movePlayer(int whichPlayer, int direction)
{
 int targetBlock, targetX, targetY, targetX2, targetY2;
 switch ( direction )
 {
  case 2:
   targetX = player[whichPlayer].coord_x;
   targetY = player[whichPlayer].coord_y - 1;
   targetX2 = player[whichPlayer].coord_x;
   targetY2 = player[whichPlayer].coord_y - 2;
   break;
  case 0:
   targetX = player[whichPlayer].coord_x;
   targetY = player[whichPlayer].coord_y + 1;
   targetX2 = player[whichPlayer].coord_x;
   targetY2 = player[whichPlayer].coord_y + 2;
   break;
  case 3:
   targetX = player[whichPlayer].coord_x - 1;
   targetY = player[whichPlayer].coord_y;
   targetX2 = player[whichPlayer].coord_x - 2;
   targetY2 = player[whichPlayer].coord_y;
   break;
  case 1:
   targetX = player[whichPlayer].coord_x + 1;
   targetY = player[whichPlayer].coord_y;
   targetX2 = player[whichPlayer].coord_x + 2;
   targetY2 = player[whichPlayer].coord_y;
   break;
 }
 targetBlock = level.data[targetX][targetY];
 if ( hasBlockBit(targetX, targetY, 4) ) {
  takeItem(targetX, targetY);
 }
 if ( hasBlockBit(targetX, targetY, 2) ) {
  if ( hasBlockBit(targetX2, targetY2, 16) ) {
   removeBlock(targetX2, targetY2);
   removeBlock(targetX, targetY);
  } else if ( hasBlockBit(targetX2, targetY2, 8) ) {
   level.data[targetX2][targetY2] = level.data[targetX][targetY];
   removeBlock(targetX, targetY);
   addActivity(targetX, targetY, 0);
  }
 }
 if ( (level.data[targetX][targetY] == 5) ||
   (level.data[targetX][targetY] == 7) ||
   (level.data[targetX][targetY] == 6) ||
   (level.data[targetX][targetY] == 4)) {
  addActivity(targetX, targetY, 1);
 }
 if (targetBlock == 16) {
  inventory.redKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 17) {
  inventory.greenKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 18) {
  inventory.blueKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 19) {
  inventory.yellowKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 20) {
  toggleBlocks = !toggleBlocks;
 }
 if ( hasBlockBit(targetX, targetY, 1) ) {
  player[whichPlayer].coord_x = targetX;
  player[whichPlayer].coord_y = targetY;
 }
 return;
}

canonical_original_code:
void movePlayer(int whichPlayer, int direction)
{
 int targetBlock; int targetX; int targetY; int targetX2; int targetY2;
 switch ( direction )
 {
  case 2:
   targetX = player[whichPlayer].coord_x;
   targetY = player[whichPlayer].coord_y - 1;
   targetX2 = player[whichPlayer].coord_x;
   targetY2 = player[whichPlayer].coord_y - 2;
   break;
  case 0:
   targetX = player[whichPlayer].coord_x;
   targetY = player[whichPlayer].coord_y + 1;
   targetX2 = player[whichPlayer].coord_x;
   targetY2 = player[whichPlayer].coord_y + 2;
   break;
  case 3:
   targetX = player[whichPlayer].coord_x - 1;
   targetY = player[whichPlayer].coord_y;
   targetX2 = player[whichPlayer].coord_x - 2;
   targetY2 = player[whichPlayer].coord_y;
   break;
  case 1:
   targetX = player[whichPlayer].coord_x + 1;
   targetY = player[whichPlayer].coord_y;
   targetX2 = player[whichPlayer].coord_x + 2;
   targetY2 = player[whichPlayer].coord_y;
   break;
 }
 targetBlock = level.data[targetX][targetY];
 if ( hasBlockBit(targetX, targetY, 4) ) {
  takeItem(targetX, targetY);
 }
 if ( hasBlockBit(targetX, targetY, 2) ) {
  if ( hasBlockBit(targetX2, targetY2, 16) ) {
   removeBlock(targetX2, targetY2);
   removeBlock(targetX, targetY);
  } else if ( hasBlockBit(targetX2, targetY2, 8) ) {
   level.data[targetX2][targetY2] = level.data[targetX][targetY];
   removeBlock(targetX, targetY);
   addActivity(targetX, targetY, 0);
  }
 }
 if ( (level.data[targetX][targetY] == 5) ||
   (level.data[targetX][targetY] == 7) ||
   (level.data[targetX][targetY] == 6) ||
   (level.data[targetX][targetY] == 4)) {
  addActivity(targetX, targetY, 1);
 }
 if (targetBlock == 16) {
  inventory.redKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 17) {
  inventory.greenKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 18) {
  inventory.blueKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 19) {
  inventory.yellowKeys--;
  removeBlock(targetX, targetY);
 }
 if (targetBlock == 20) {
  toggleBlocks = !toggleBlocks;
 }
 if ( hasBlockBit(targetX, targetY, 1) ) {
  player[whichPlayer].coord_x = targetX;
  player[whichPlayer].coord_y = targetY;
 }
 return;
}

variable_types: whichPlayer int

variable_types: direction int

variable_types: targetBlock int

variable_types: targetX int

variable_types: targetY int


quit

decompiled_code:
void __fastcall __noreturn quit(int status)
{
puts("Quitting...");
SDL_Quit();
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int status)
{
puts("Quitting...");
SDL_Quit();
exit(status);
}

original_code:
void quit(int ret_val)
{
    printf ("Quitting...\n");
    SDL_Quit();
    exit(ret_val);
}


addActivity

decompiled_code:
void addActivity()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void addActivity(int x, int y, int activityType)
{
 switch (activityType) {
  case 0:
   break;
  case 1:
   break;
  default:
   break;
 }
 return;
}

variable_types: activityType int


ConfigSetting

decompiled_code:
int __fastcall ConfigSetting(char *s, char *nptr, unsigned int a3)
{
int result; // eax
size_t i; // rbx
char sa[50]; // [rsp+6h] [rbp-32h] BYREF

if ( a3 == -1 && *s == 45 )
{
for ( i = 0LL; strlen(s) > i; ++i )
s[i] = s[i + 1];
}
if ( !strcmp(s, "vid_fullscreen") )
{
vid = atoi(nptr);
result = printf("Setting Fullscreen to %s\n", nptr);
}
else if ( !strcmp(s, "path_data") )
{
strcpy(path, nptr);
printf("Setting Datapath to %s\n", nptr);
result = sprintf((char *)(unsigned int)((_DWORD)path + 255), "%s%s", path, "levels/");
}
else if ( a3 == -1 )
{
result = printf("*** Unrecognized Option: '%s' at commandline", s);
}
else
{
sprintf(sa, "%d", a3);
result = printf("*** Unrecognized Option: '%s' in configfile at line %s ", s, sa);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *s, char *nptr, unsigned int a3)
{
int result; // eax
size_t i; // rbx
char sa[50]; // [rsp+6h] [rbp-32h] BYREF

if ( a3 == -1 && *s == 45 )
{
for ( i = 0LL; strlen(s) > i; ++i )
s[i] = s[i + 1];
}
if ( !strcmp(s, "vid_fullscreen") )
{
vid = atoi(nptr);
result = printf("Setting Fullscreen to %s\n", nptr);
}
else if ( !strcmp(s, "path_data") )
{
strcpy(path, nptr);
printf("Setting Datapath to %s\n", nptr);
result = sprintf((char *)(unsigned int)((_DWORD)path + 255), "%s%s", path, "levels/");
}
else if ( a3 == -1 )
{
result = printf("*** Unrecognized Option: '%s' at commandline", s);
}
else
{
sprintf(sa, "%d", a3);
result = printf("*** Unrecognized Option: '%s' in configfile at line %s ", s, sa);
}
return result;
}

original_code:
void ConfigSetting(char *setting, char *param, int line)
{
    int i;
    char string[10];
    if (line == -1)
        if (*setting == '-') for (i=0; i<(strlen(setting)); i++) setting[i]=setting[i+1];
   if ( strcmp(setting, "vid_fullscreen") == 0) {
        vid.fullscreen = atoi(param);
        printf ("Setting Fullscreen to %s\n", param);
    }
    else if ( strcmp(setting, "path_data") == 0) {
        strcpy(path.data, param);
        printf ("Setting Datapath to %s\n", param);
  sprintf(path.levels, "%s%s", path.data, "levels/");
    }
    else {
        if (line != -1) {
            sprintf(string,"%d",line);
            printf ("*** Unrecognized Option: '%s' in configfile at line %s ", setting, string);
        } else
            printf ("*** Unrecognized Option: '%s' at commandline", setting);
    }
}

variable_types:
setting
char *

variable_types:
param
char *

variable_types: line int

variable_types:
string
char [10]


LevelInfo

decompiled_code:
int __fastcall LevelInfo(char *s1, char *src)
{
int v2; // eax
int result; // eax
int v4; // ebp
char s[10]; // [rsp+Ch] [rbp-2Ch] BYREF
char nptr[34]; // [rsp+16h] [rbp-22h] BYREF

if ( !strcmp(s1, "requiredversion") )
{
sprintf(s, "%d%d%d", 0LL, 0LL, 0LL);
sprintf(nptr, "%c%c%c", (unsigned int)*src, (unsigned int)src[2], (unsigned int)src[3]);
v4 = atoi(s);
if ( v4 < atoi(nptr) )
{
printf("*** Engine Version too old. This Level requires a newer Version: %s\n", src);
quit(1LL);
}
result = printf(" Required Version: %s\n", src);
}
else if ( !strcmp(s1, "authorname") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90088), src);
result = printf(" Author: %s\n", src);
}
else if ( !strcmp(s1, "levelname") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90008), src);
result = printf(" Levelname: %s\n", src);
}
else if ( !strcmp(s1, "shortdesc") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90168), src);
result = printf(" Description: %s\n", src);
}
else if ( !strcmp(s1, "difficulty") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90423), src);
result = printf(" Difficulty: %s\n", src);
}
else if ( !strcmp(s1, "playercount") )
{
v2 = atoi(src);
if ( (unsigned int)(v2 - 1) > 2 )
{
playerCount = 1;
printf("*** Playercount out of range (setting to 1)");
}
else
{
playerCount = v2;
}
result = printf(" Number of Players: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x1") )
{
player = atoi(src);
result = printf(" Player 1 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x2") )
{
*(&player + 5) = atoi(src);
result = printf(" Player 2 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x3") )
{
*(&player + 10) = atoi(src);
result = printf(" Player 3 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y1") )
{
*(&player + 1) = atoi(src);
result = printf(" Player 1 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y2") )
{
*(&player + 6) = atoi(src);
result = printf(" Player 2 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y3") )
{
*(&player + 11) = atoi(src);
result = printf(" Player 3 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_1") )
{
*(&player + 2) = atoi(src);
result = printf(" Player 1 Direction: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_2") )
{
*(&player + 7) = atoi(src);
result = printf(" Player 2 Direction: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_3") )
{
*(&player + 12) = atoi(src);
result = printf(" Player 3 Direction: %s\n", src);
}
else if ( !strcmp(s1, "message1") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90583), src);
result = printf(" Message 1: %s\n", src);
}
else if ( !strcmp(s1, "message2") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90838), src);
result = printf(" Message 2: %s\n", src);
}
else if ( !strcmp(s1, "message3") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91093), src);
result = printf(" Message 3: %s\n", src);
}
else if ( !strcmp(s1, "message4") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91348), src);
result = printf(" Message 4: %s\n", src);
}
else if ( !strcmp(s1, "message5") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91603), src);
result = printf(" Message 5: %s\n", src);
}
else if ( !strcmp(s1, "theme") )
{
result = (unsigned int)strcpy((char *)(unsigned int)((_DWORD)level + 90503), src);
}
else
{
result = printf("*** Unrecognized Levelinfo: %s\n", s1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *s1, char *src)
{
int v2; // eax
int result; // eax
int v4; // ebp
char s[10]; // [rsp+Ch] [rbp-2Ch] BYREF
char nptr[34]; // [rsp+16h] [rbp-22h] BYREF

if ( !strcmp(s1, "requiredversion") )
{
sprintf(s, "%d%d%d", 0LL, 0LL, 0LL);
sprintf(nptr, "%c%c%c", (unsigned int)*src, (unsigned int)src[2], (unsigned int)src[3]);
v4 = atoi(s);
if ( v4 < atoi(nptr) )
{
printf("*** Engine Version too old. This Level requires a newer Version: %s\n", src);
quit(1LL);
}
result = printf(" Required Version: %s\n", src);
}
else if ( !strcmp(s1, "authorname") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90088), src);
result = printf(" Author: %s\n", src);
}
else if ( !strcmp(s1, "levelname") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90008), src);
result = printf(" Levelname: %s\n", src);
}
else if ( !strcmp(s1, "shortdesc") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90168), src);
result = printf(" Description: %s\n", src);
}
else if ( !strcmp(s1, "difficulty") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90423), src);
result = printf(" Difficulty: %s\n", src);
}
else if ( !strcmp(s1, "playercount") )
{
v2 = atoi(src);
if ( (unsigned int)(v2 - 1) > 2 )
{
playerCount = 1;
printf("*** Playercount out of range (setting to 1)");
}
else
{
playerCount = v2;
}
result = printf(" Number of Players: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x1") )
{
player = atoi(src);
result = printf(" Player 1 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x2") )
{
*(&player + 5) = atoi(src);
result = printf(" Player 2 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_x3") )
{
*(&player + 10) = atoi(src);
result = printf(" Player 3 Start X: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y1") )
{
*(&player + 1) = atoi(src);
result = printf(" Player 1 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y2") )
{
*(&player + 6) = atoi(src);
result = printf(" Player 2 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerstart_y3") )
{
*(&player + 11) = atoi(src);
result = printf(" Player 3 Start Y: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_1") )
{
*(&player + 2) = atoi(src);
result = printf(" Player 1 Direction: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_2") )
{
*(&player + 7) = atoi(src);
result = printf(" Player 2 Direction: %s\n", src);
}
else if ( !strcmp(s1, "playerdirection_3") )
{
*(&player + 12) = atoi(src);
result = printf(" Player 3 Direction: %s\n", src);
}
else if ( !strcmp(s1, "message1") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90583), src);
result = printf(" Message 1: %s\n", src);
}
else if ( !strcmp(s1, "message2") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 90838), src);
result = printf(" Message 2: %s\n", src);
}
else if ( !strcmp(s1, "message3") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91093), src);
result = printf(" Message 3: %s\n", src);
}
else if ( !strcmp(s1, "message4") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91348), src);
result = printf(" Message 4: %s\n", src);
}
else if ( !strcmp(s1, "message5") )
{
strcpy((char *)(unsigned int)((_DWORD)level + 91603), src);
result = printf(" Message 5: %s\n", src);
}
else if ( !strcmp(s1, "theme") )
{
result = (unsigned int)strcpy((char *)(unsigned int)((_DWORD)level + 90503), src);
}
else
{
result = printf("*** Unrecognized Levelinfo: %s\n", s1);
}
return result;
}

original_code:
void LevelInfo(char *info, char *param)
{
    char string1[10];
    char string2[10];
   if ( strcmp(info, "requiredversion") == 0) {
        sprintf(string1, "%d%d%d", 0, 0, 0);
        sprintf(string2, "%c%c%c", param[0], param[2], param[3]);
        if (atoi(string1) < atoi(string2)) {
            printf ("*** Engine Version too old. This Level requires a newer Version: %s\n", param);
            quit(1);
        }
        printf (" Required Version: %s\n", param);
    } else if ( strcmp(info, "authorname") == 0) {
        strcpy(level.author, param);
        printf (" Author: %s\n", param);
    } else if ( strcmp(info, "levelname") == 0) {
        strcpy(level.title, param);
        printf (" Levelname: %s\n", param);
    } else if ( strcmp(info, "shortdesc") == 0) {
        strcpy(level.desc, param);
        printf (" Description: %s\n", param);
    } else if ( strcmp(info, "difficulty") == 0) {
        strcpy(level.difficulty, param);
        printf (" Difficulty: %s\n", param);
    }
 else if ( strcmp(info, "playercount") == 0) {
        playerCount = atoi(param);
  if ((playerCount > 3) || (playerCount < 1)) {
   playerCount = 1;
   printf ("*** Playercount out of range (setting to 1)");
  }
        printf(" Number of Players: %s\n", param);
    } else if ( strcmp(info, "playerstart_x1") == 0) {
        player[0].coord_x = atoi(param);
        printf(" Player 1 Start X: %s\n", param);
    } else if ( strcmp(info, "playerstart_x2") == 0) {
        player[1].coord_x = atoi(param);
        printf(" Player 2 Start X: %s\n", param);
    } else if ( strcmp(info, "playerstart_x3") == 0) {
        player[2].coord_x = atoi(param);
        printf(" Player 3 Start X: %s\n", param);
    } else if ( strcmp(info, "playerstart_y1") == 0) {
        player[0].coord_y = atoi(param);
        printf(" Player 1 Start Y: %s\n", param);
    } else if ( strcmp(info, "playerstart_y2") == 0) {
        player[1].coord_y = atoi(param);
        printf(" Player 2 Start Y: %s\n", param);
    } else if ( strcmp(info, "playerstart_y3") == 0) {
        player[2].coord_y = atoi(param);
        printf(" Player 3 Start Y: %s\n", param);
 } else if ( strcmp(info, "playerdirection_1") == 0) {
  player[0].direction = atoi(param);
  printf(" Player 1 Direction: %s\n", param);
 } else if ( strcmp(info, "playerdirection_2") == 0) {
  player[1].direction = atoi(param);
  printf(" Player 2 Direction: %s\n", param);
 } else if ( strcmp(info, "playerdirection_3") == 0) {
  player[2].direction = atoi(param);
  printf(" Player 3 Direction: %s\n", param);
    }
    else if ( strcmp(info, "message1") == 0) {
        strcpy(level.message1, param);
        printf(" Message 1: %s\n", param);
    } else if ( strcmp(info, "message2") == 0) {
        strcpy(level.message2, param);
        printf(" Message 2: %s\n", param);
    } else if ( strcmp(info, "message3") == 0) {
        strcpy(level.message3, param);
        printf(" Message 3: %s\n", param);
    } else if ( strcmp(info, "message4") == 0) {
        strcpy(level.message4, param);
        printf(" Message 4: %s\n", param);
    } else if ( strcmp(info, "message5") == 0) {
        strcpy(level.message5, param);
        printf(" Message 5: %s\n", param);
    }
    else if ( strcmp(info, "theme") == 0) {
  strcpy(level.theme, param);
    }
    else {
        printf ("*** Unrecognized Levelinfo: %s\n", info);
    }
}

variable_types:
info
char *


RenderLevel

decompiled_code:
__int64 RenderLevel()
{
int v0; // ebp
int v1; // er13
__int64 v2; // rdx
int v3; // er11
int v4; // er11
__int64 result; // rax
int v6; // er15
int v7; // er10
int v8; // ebx
__int64 v9; // r14
int v10; // edi
int v11; // eax
__int64 v12; // rsi
int v13; // ecx
int v14; // edx
int v15; // eax
bool v16; // zf
int v17; // edi
int v18; // eax
int v19; // ecx
__int64 v20; // rsi
int *v21; // rax
int i; // er10
__int16 v23; // dx
__int64 v24; // rdi
int v25; // eax
int v26; // et2
int v27; // [rsp+0h] [rbp-58h]
int *v28; // [rsp+8h] [rbp-50h]
__int64 v29; // [rsp+10h] [rbp-48h]
int v30; // [rsp+18h] [rbp-40h]
int v31; // [rsp+1Ch] [rbp-3Ch]

v0 = viewY;
v27 = -1;
v1 = viewStartX;
v31 = 0;
v2 = 20LL * chosenGuy;
v3 = *(int *)((char *)player + v2);
LODWORD(v2) = *(int *)((char *)&player[1] + v2);
v4 = v3 - viewX / 2;
beginX = v4;
result = (unsigned int)(viewY / 2);
beginY = v2 - result;
while ( 2 )
{
v6 = v4 + viewX;
if ( v4 + viewX < v4 )
goto LABEL_37;
v7 = beginY;
v8 = 25 * v31;
result = 600LL * v6;
v29 = result;
do
{
if ( v0 < 0 )
goto LABEL_36;
v9 = v7;
v10 = v7;
v11 = v7;
if ( levelsizeX <= v6 )
goto LABEL_26;
LABEL_6:
if ( levelsizeY > (int)v9 && v6 >= 0 && (int)v9 >= 0 )
{
v12 = *(int *)((char *)&level[v9] + v29);
if ( (_DWORD)v12 != 22 )
{
if ( (_DWORD)v12 == 24 )
{
v27 = 1;
}
else if ( (_DWORD)v12 == 21 )
{
if ( toggleBlocks )
{
blitMe = 22;
v13 = 0;
v12 = 22LL;
blockHeight[22] = 0;
}
else
{
blitMe = 21;
v13 = 1;
blockHeight[21] = 1;
}
goto LABEL_13;
}
blitMe = *(int *)((char *)&level[v9] + v29);
v13 = blockHeight[(int)v12];
goto LABEL_13;
}
v13 = toggleBlocks;
if ( toggleBlocks == 1 )
{
blitMe = 21;
v12 = 21LL;
blockHeight[21] = 1;
}
else
{
blitMe = 22;
v13 = 0;
blockHeight[22] = 0;
}
goto LABEL_13;
}
LABEL_26:
while ( 1 )
{
blitMe = 0;
v13 = blockHeight[0];
v12 = 0LL;
LABEL_13:
v14 = -22 * player[5 * chosenGuy + 1] + 22 * v10 + 22 * (v6 - v4) - v1;
v15 = 22 * player[5 * chosenGuy] + -22 * v6 + 22 * (v11 - v7) - viewStartY;
v16 = v13 == 0;
renderX = v14;
v17 = v8 + v15;
v18 = v15 + v8 - 25;
v19 = v17;
if ( !v16 )
v19 = v18;
renderY = v19;
if ( v14 <= 539 )
{
if ( v19 > 479 && v14 < -59 )
goto LABEL_21;
}
else if ( v19 < -39 )
{
goto LABEL_21;
}
if ( v27 == -1 )
{
BlitImage(*((_QWORD *)&block + v12), screen, v14, v19);
}
else
{
if ( HIDWORD(animation[11]) == 1 )
{
v25 = rand();
LOWORD(v19) = renderY;
v26 = v25 % SLODWORD(animation[10]);
v20 = v26;
LOWORD(v14) = renderX;
HIDWORD(animation[10]) = v26;
}
else
{
v20 = SHIDWORD(animation[10]);
}
BlitImage(animation[v20], screen, v14, v19);
v27 = -1;
}
LABEL_21:
v21 = player;
for ( i = 0; playerCount > i; v21 += 5 )
{
if ( *v21 == v6 && v21[1] == (_DWORD)v9 )
{
v28 = v21;
v30 = i;
v23 = renderX + 15;
v24 = characters[v21[2]];
if ( blockHeight[blitMe] )
{
BlitImage(v24, screen, v23, renderY + 5);
v21 = v28;
i = v30;
}
else
{
BlitImage(v24, screen, v23, renderY - 25);
i = v30;
v21 = v28;
}
}
++i;
}
++v9;
BlitImage(characters[4], screen, 327, 193);
v7 = beginY;
result = (unsigned int)(v0 + beginY);
if ( (int)result < (int)v9 )
break;
v4 = beginX;
v10 = v9;
v11 = v9;
if ( levelsizeX > v6 )
goto LABEL_6;
}
v4 = beginX;
LABEL_36:
v29 -= 600LL;
--v6;
}
while ( v6 >= v4 );
LABEL_37:
if ( v31 != 1 )
{
v31 = 1;
v4 = beginX;
continue;
}
return result;
}
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // ebp
int v1; // er13
__int64 v2; // rdx
int v3; // er11
int v4; // er11
__int64 result; // rax
int v6; // er15
int v7; // er10
int v8; // ebx
__int64 v9; // r14
int v10; // edi
int v11; // eax
__int64 v12; // rsi
int v13; // ecx
int v14; // edx
int v15; // eax
bool v16; // zf
int v17; // edi
int v18; // eax
int v19; // ecx
__int64 v20; // rsi
int *v21; // rax
int i; // er10
__int16 v23; // dx
__int64 v24; // rdi
int v25; // eax
int v26; // et2
int v27; // [rsp+0h] [rbp-58h]
int *v28; // [rsp+8h] [rbp-50h]
__int64 v29; // [rsp+10h] [rbp-48h]
int v30; // [rsp+18h] [rbp-40h]
int v31; // [rsp+1Ch] [rbp-3Ch]

v0 = viewY;
v27 = -1;
v1 = viewStartX;
v31 = 0;
v2 = 20LL * chosenGuy;
v3 = *(int *)((char *)player + v2);
LODWORD(v2) = *(int *)((char *)&player[1] + v2);
v4 = v3 - viewX / 2;
beginX = v4;
result = (unsigned int)(viewY / 2);
beginY = v2 - result;
while ( 2 )
{
v6 = v4 + viewX;
if ( v4 + viewX < v4 )
goto LABEL_37;
v7 = beginY;
v8 = 25 * v31;
result = 600LL * v6;
v29 = result;
do
{
if ( v0 < 0 )
goto LABEL_36;
v9 = v7;
v10 = v7;
v11 = v7;
if ( levelsizeX <= v6 )
goto LABEL_26;
LABEL_6:
if ( levelsizeY > (int)v9 && v6 >= 0 && (int)v9 >= 0 )
{
v12 = *(int *)((char *)&level[v9] + v29);
if ( (_DWORD)v12 != 22 )
{
if ( (_DWORD)v12 == 24 )
{
v27 = 1;
}
else if ( (_DWORD)v12 == 21 )
{
if ( toggleBlocks )
{
blitMe = 22;
v13 = 0;
v12 = 22LL;
blockHeight[22] = 0;
}
else
{
blitMe = 21;
v13 = 1;
blockHeight[21] = 1;
}
goto LABEL_13;
}
blitMe = *(int *)((char *)&level[v9] + v29);
v13 = blockHeight[(int)v12];
goto LABEL_13;
}
v13 = toggleBlocks;
if ( toggleBlocks == 1 )
{
blitMe = 21;
v12 = 21LL;
blockHeight[21] = 1;
}
else
{
blitMe = 22;
v13 = 0;
blockHeight[22] = 0;
}
goto LABEL_13;
}
LABEL_26:
while ( 1 )
{
blitMe = 0;
v13 = blockHeight[0];
v12 = 0LL;
LABEL_13:
v14 = -22 * player[5 * chosenGuy + 1] + 22 * v10 + 22 * (v6 - v4) - v1;
v15 = 22 * player[5 * chosenGuy] + -22 * v6 + 22 * (v11 - v7) - viewStartY;
v16 = v13 == 0;
renderX = v14;
v17 = v8 + v15;
v18 = v15 + v8 - 25;
v19 = v17;
if ( !v16 )
v19 = v18;
renderY = v19;
if ( v14 <= 539 )
{
if ( v19 > 479 && v14 < -59 )
goto LABEL_21;
}
else if ( v19 < -39 )
{
goto LABEL_21;
}
if ( v27 == -1 )
{
func0(*((_QWORD *)&block + v12), screen, v14, v19);
}
else
{
if ( HIDWORD(animation[11]) == 1 )
{
v25 = rand();
LOWORD(v19) = renderY;
v26 = v25 % SLODWORD(animation[10]);
v20 = v26;
LOWORD(v14) = renderX;
HIDWORD(animation[10]) = v26;
}
else
{
v20 = SHIDWORD(animation[10]);
}
func0(animation[v20], screen, v14, v19);
v27 = -1;
}
LABEL_21:
v21 = player;
for ( i = 0; playerCount > i; v21 += 5 )
{
if ( *v21 == v6 && v21[1] == (_DWORD)v9 )
{
v28 = v21;
v30 = i;
v23 = renderX + 15;
v24 = characters[v21[2]];
if ( blockHeight[blitMe] )
{
func0(v24, screen, v23, renderY + 5);
v21 = v28;
i = v30;
}
else
{
func0(v24, screen, v23, renderY - 25);
i = v30;
v21 = v28;
}
}
++i;
}
++v9;
func0(characters[4], screen, 327, 193);
v7 = beginY;
result = (unsigned int)(v0 + beginY);
if ( (int)result < (int)v9 )
break;
v4 = beginX;
v10 = v9;
v11 = v9;
if ( levelsizeX > v6 )
goto LABEL_6;
}
v4 = beginX;
LABEL_36:
v29 -= 600LL;
--v6;
}
while ( v6 >= v4 );
LABEL_37:
if ( v31 != 1 )
{
v31 = 1;
v4 = beginX;
continue;
}
return result;
}
}

original_code:
void RenderLevel()
{
 int x, y, i, j;
 int animated = -1;
 int whichani;
 beginX = player[chosenGuy].coord_x - (viewX/2);
 beginY = player[chosenGuy].coord_y - (viewY/2);
 for (j=0; j<2; j++) {
  for (x=(viewX+beginX); x>=beginX; x--) {
   for (y=beginY; y<=(viewY+beginY); y++) {
    if ((( x <= levelsizeX-1 ) && ( y <= levelsizeY-1 )) && (( x >= 0 ) && ( y >= 0)))
     switch ( level.data[x][y] )
     {
      case 21:
       if ( toggleBlocks == 0)
       {
        blitMe = 21;
        blockHeight[blitMe] = 1;
       } else {
        blitMe = 22;
        blockHeight[blitMe] = 0;
       }
       break;
      case 22:
       if ( toggleBlocks == 1)
       {
        blitMe = 21;
        blockHeight[blitMe] = 1;
       } else {
        blitMe = 22;
        blockHeight[blitMe] = 0;
       }
       break;
      case 24:
       whichani = 0;
       animated = 1;
      default:
       blitMe = level.data[x][y];
       break;
     }
    else
     blitMe = 0;
    renderX = ((x-beginX)*22) - viewStartX + (y*22) - (player[chosenGuy].coord_y*22);
    if ( (blockHeight[blitMe] == 0))
     renderY = ((y-beginY)*22) - viewStartY - (x*22) + (player[chosenGuy].coord_x*22) + (25*j);
    else
     renderY = ((y-beginY)*22) - viewStartY - (x*22) + (player[chosenGuy].coord_x*22) - 25 + (25*j);
    if (( renderX < 540 && renderY < 480 ) || ( renderX > -60 && renderY > -40))
    {
     if ( animated == -1 )
      if (j==0)
       BlitImage(block[blitMe], screen, renderX, renderY);
      else
       BlitImage(block[blitMe], screen, renderX, renderY);
     else {
      if (animation[whichani].random == 1) animation[whichani].currentphase = rand() % animation[whichani].numphases;
      BlitImage(animation[whichani].gfx[animation[whichani].currentphase], screen, renderX, renderY);
      animated = -1;
     }
    }
    for (i=0; i<playerCount; i++) {
     if ((player[i].coord_x == x) && (player[i].coord_y == y))
     {
      if ( blockHeight[blitMe] == 0)
      {
       BlitImage(characters[player[i].direction], screen, renderX + 15, renderY - 25);
      } else {
       BlitImage(characters[player[i].direction], screen, renderX + 15, renderY + 5);
      }
     }
    }
    BlitImage(characters[4], screen, 327, 193);
   }
  }
 }
}

canonical_original_code:
void RenderLevel()
{
 int x; int y; int i; int j;
 int animated = -1;
 int whichani;
 beginX = player[chosenGuy].coord_x - (viewX/2);
 beginY = player[chosenGuy].coord_y - (viewY/2);
 for (j=0; j<2; j++) {
  for (x=(viewX+beginX); x>=beginX; x--) {
   for (y=beginY; y<=(viewY+beginY); y++) {
    if ((( x <= levelsizeX-1 ) && ( y <= levelsizeY-1 )) && (( x >= 0 ) && ( y >= 0)))
     switch ( level.data[x][y] )
     {
      case 21:
       if ( toggleBlocks == 0)
       {
        blitMe = 21;
        blockHeight[blitMe] = 1;
       } else {
        blitMe = 22;
        blockHeight[blitMe] = 0;
       }
       break;
      case 22:
       if ( toggleBlocks == 1)
       {
        blitMe = 21;
        blockHeight[blitMe] = 1;
       } else {
        blitMe = 22;
        blockHeight[blitMe] = 0;
       }
       break;
      case 24:
       whichani = 0;
       animated = 1;
      default:
       blitMe = level.data[x][y];
       break;
     }
    else
     blitMe = 0;
    renderX = ((x-beginX)*22) - viewStartX + (y*22) - (player[chosenGuy].coord_y*22);
    if ( (blockHeight[blitMe] == 0))
     renderY = ((y-beginY)*22) - viewStartY - (x*22) + (player[chosenGuy].coord_x*22) + (25*j);
    else
     renderY = ((y-beginY)*22) - viewStartY - (x*22) + (player[chosenGuy].coord_x*22) - 25 + (25*j);
    if (( renderX < 540 && renderY < 480 ) || ( renderX > -60 && renderY > -40))
    {
     if ( animated == -1 )
      if (j==0)
       BlitImage(block[blitMe], screen, renderX, renderY);
      else
       BlitImage(block[blitMe], screen, renderX, renderY);
     else {
      if (animation[whichani].random == 1) animation[whichani].currentphase = rand() % animation[whichani].numphases;
      BlitImage(animation[whichani].gfx[animation[whichani].currentphase], screen, renderX, renderY);
      animated = -1;
     }
    }
    for (i=0; i<playerCount; i++) {
     if ((player[i].coord_x == x) && (player[i].coord_y == y))
     {
      if ( blockHeight[blitMe] == 0)
      {
       BlitImage(characters[player[i].direction], screen, renderX + 15, renderY - 25);
      } else {
       BlitImage(characters[player[i].direction], screen, renderX + 15, renderY + 5);
      }
     }
    }
    BlitImage(characters[4], screen, 327, 193);
   }
  }
 }
}

variable_types: j int

variable_types: animated int

variable_types: whichani int


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

now = SDL_GetTicks();
result = 0LL;
if ( now < (unsigned int)next_time )
result = (unsigned int)(next_time - now);
return result;
}

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

now = SDL_GetTicks();
result = 0LL;
if ( now < (unsigned int)next_time )
result = (unsigned int)(next_time - now);
return result;
}

original_code:
Uint32 time_left(void)
{
 now = SDL_GetTicks();
    if(next_time <= now)
        return 0;
    else
        return next_time - now;
}

canonical_original_code:
unsigned int time_left()
{
 now = SDL_GetTicks();
    if(next_time <= now)
        return 0;
    else
        return next_time - now;
}

return_type: unsigned int


HandleAfterWalk

decompiled_code:
__int64 __fastcall HandleAfterWalk(int a1, int a2)
{
__int64 v2; // rsi

v2 = a2 + 150LL * a1;
if ( level[v2] == 10 )
level[v2] = 8;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // rsi

v2 = a2 + 150LL * a1;
if ( level[v2] == 10 )
level[v2] = 8;
return 1LL;
}

original_code:
int HandleAfterWalk(int x, int y)
{
 switch( level.data[x][y] )
 {
  case 10 :
   level.data[x][y] = 8;
   return 1;
   break;
  default:
   return 1;
   break;
 }
}

variable_types: x int


TextWidth

decompiled_code:
__int64 __fastcall TextWidth(_BYTE *a1)
{
return TextWidth2((__int64)InternalFont, a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(_BYTE *a1)
{
return func5((__int64)InternalFont, a1);
}

original_code:
int TextWidth(char *text)
{
    return TextWidth2(&InternalFont, text);
}

variable_types:
text
char *


unitPathAddNode

repo: atextor/demon-defense

decompiled_code:
__int64 __fastcall unitPathAddNode(__int64 a1, __int16 a2, __int16 a3)
{
__int64 result; // rax

result = *(unsigned int *)(a1 + 16);
if ( (int)result < *(_DWORD *)(a1 + 20) )
{
*(_WORD *)(*(_QWORD *)a1 + 2LL * (int)result) = a2;
*(_WORD *)(*(_QWORD *)(a1 + 8) + 2LL * (int)result) = a3;
*(_DWORD *)(a1 + 24) = result;
result = (unsigned int)(result + 1);
*(_DWORD *)(a1 + 16) = result;
}
return result;
}

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

result = *(unsigned int *)(a1 + 16);
if ( (int)result < *(_DWORD *)(a1 + 20) )
{
*(_WORD *)(*(_QWORD *)a1 + 2LL * (int)result) = a2;
*(_WORD *)(*(_QWORD *)(a1 + 8) + 2LL * (int)result) = a3;
*(_DWORD *)(a1 + 24) = result;
result = (unsigned int)(result + 1);
*(_DWORD *)(a1 + 16) = result;
}
return result;
}

original_code:
static void unitPathAddNode(UnitPath *path, int x, int y) {
 if (path->numNodes < path->maxNodes) {
  path->xCoords[path->numNodes] = x;
  path->yCoords[path->numNodes] = y;
  path->currentPath = path->numNodes;
  if (path->numNodes < path->maxNodes) {
   path->numNodes++;
  }
 }
}

canonical_original_code:
static void unitPathAddNode(struct UnitPath *path, int x, int y) {
 if (path->numNodes < path->maxNodes) {
  path->xCoords[path->numNodes] = x;
  path->yCoords[path->numNodes] = y;
  path->currentPath = path->numNodes;
  if (path->numNodes < path->maxNodes) {
   path->numNodes++;
  }
 }
}

variable_types: struct UnitPath { unsigned short *xCoords; unsigned short *yCoords; int numNodes; int maxNodes; int currentPath; } *

variable_types: y int


lumpName

decompiled_code:
char *__fastcall lumpName(char *s, unsigned int a2, unsigned int a3, char a4)
{
__int64 v4; // r8
const char *v5; // rdx
bool v6; // zf
__int64 v7; // rcx
const char *v8; // rsi
const char *v9; // rax

v4 = a3;
v5 = (&off_828)[3 * a2];
switch ( (int)v4 )
{
case 1:
case 5:
sprintf(s, "%s%c%d", v5, (unsigned int)a4, v4);
return s;
case 2:
case 8:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c8%c2";
v9 = "%s%c2%c8";
goto LABEL_3;
case 3:
case 7:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c7%c3";
v9 = "%s%c3%c7";
goto LABEL_3;
case 4:
case 6:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c6%c4";
v9 = "%s%c4%c6";
LABEL_3:
if ( !v6 )
v8 = v9;
sprintf(s, v8, v5, v7, (unsigned int)v7);
break;
default:
return s;
}
return s;
}

canonical_decompiled_code:
char *__fastcall func1(char *s, unsigned int a2, unsigned int a3, char a4)
{
__int64 v4; // r8
const char *v5; // rdx
bool v6; // zf
__int64 v7; // rcx
const char *v8; // rsi
const char *v9; // rax

v4 = a3;
v5 = (&off_828)[3 * a2];
switch ( (int)v4 )
{
case 1:
case 5:
sprintf(s, "%s%c%d", v5, (unsigned int)a4, v4);
return s;
case 2:
case 8:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c8%c2";
v9 = "%s%c2%c8";
goto LABEL_3;
case 3:
case 7:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c7%c3";
v9 = "%s%c3%c7";
goto LABEL_3;
case 4:
case 6:
v6 = a2 == 5;
v7 = (unsigned int)a4;
v8 = "%s%c6%c4";
v9 = "%s%c4%c6";
LABEL_3:
if ( !v6 )
v8 = v9;
sprintf(s, v8, v5, v7, (unsigned int)v7);
break;
default:
return s;
}
return s;
}

original_code:
static char *lumpName(char *buf, UnitTypes unitType, int direction,
 char frame) {
 char *unitName = unitDefinitions[unitType].lumpName;
 switch (direction) {
  case 1:
  case 5: sprintf(buf, "%s%c%d", unitName, frame, direction);
   break;
  case 2:
  case 8: sprintf(buf, unitType == LOSTSOUL ? "%s%c8%c2" : "%s%c2%c8",
    unitName, frame, frame);
   break;
  case 3:
  case 7: sprintf(buf, unitType == LOSTSOUL ? "%s%c7%c3" : "%s%c3%c7",
    unitName, frame, frame);
   break;
  case 4:
  case 6: sprintf(buf, unitType == LOSTSOUL ? "%s%c6%c4" : "%s%c4%c6",
    unitName, frame, frame);
   break;
 }
 return buf;
}

canonical_original_code:
static char *lumpName(char *buf, enum UnitTypes unitType, int direction, char frame) {
 char *unitName = unitDefinitions[unitType].lumpName;
 switch (direction) {
  case 1:
  case 5: sprintf(buf, "%s%c%d", unitName, frame, direction);
   break;
  case 2:
  case 8: sprintf(buf, unitType == LOSTSOUL ? "%s%c8%c2" : "%s%c2%c8",
    unitName, frame, frame);
   break;
  case 3:
  case 7: sprintf(buf, unitType == LOSTSOUL ? "%s%c7%c3" : "%s%c3%c7",
    unitName, frame, frame);
   break;
  case 4:
  case 6: sprintf(buf, unitType == LOSTSOUL ? "%s%c6%c4" : "%s%c4%c6",
    unitName, frame, frame);
   break;
 }
 return buf;
}

variable_types:
buf
char *

variable_types: enum UnitTypes { PLAYER=0, IMP=1, SOLDIER=2, SHOTGUNNER=3, PINKY=4, LOSTSOUL=5, CACODEMON=6, HELLKNIGHT=7, BARONOFHELL=8, ARACHNOTRON=9, CYBERDEMON=10 }

variable_types: direction int

variable_types: frame char

variable_types:
unitName
char *

return_type: char *


sgInit

decompiled_code:
__int64 __fastcall sgInit(char a1)
{
__int64 result; // rax
const char *v2; // rax
const char *v3; // rax

if ( (int)SDL_Init(32LL) < 0 )
{
v3 = (const char *)SDL_GetError();
fprintf(stderr, "Couldn't initialize SDL: %s\n", v3);
result = 1LL;
}
else
{
screen = SDL_SetVideoMode(800LL, 600LL, 0LL, a1 != 0 ? 0x80000000 : 0);
if ( screen )
{
colorSource = sgCreateSurface(1u, 1u);
result = 0LL;
}
else
{
v2 = (const char *)SDL_GetError();
fprintf(stderr, "Couldn't set video mode: %s\n", v2);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
__int64 result; // rax
const char *v2; // rax
const char *v3; // rax

if ( (int)SDL_Init(32LL) < 0 )
{
v3 = (const char *)SDL_GetError();
fprintf(stderr, "Couldn't initialize SDL: %s\n", v3);
result = 1LL;
}
else
{
screen = SDL_SetVideoMode(800LL, 600LL, 0LL, a1 != 0 ? 0x80000000 : 0);
if ( screen )
{
colorSource = func0(1u, 1u);
result = 0LL;
}
else
{
v2 = (const char *)SDL_GetError();
fprintf(stderr, "Couldn't set video mode: %s\n", v2);
result = 1LL;
}
}
return result;
}

original_code:
int sgInit(_Bool fullscreen) {
 Uint32 videoflags;
 if (SDL_Init(0x00000020) < 0) {
  fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  return 1;
 }
 videoflags = 0x00000000;
 if (fullscreen)
  videoflags |= 0x80000000;
 screen = SDL_SetVideoMode(800, 600, 0, videoflags);
 if (screen == ((void *)0)) {
  fprintf(stderr, "Couldn't set video mode: %s\n",
   SDL_GetError());
  return 1;
 }
 colorSource = sgCreateSurface(1, 1);
 return 0;
}

canonical_original_code:
int sgInit(_Bool fullscreen) {
 unsigned int videoflags;
 if (SDL_Init(0x00000020) < 0) {
  fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  return 1;
 }
 videoflags = 0x00000000;
 if (fullscreen)
  videoflags |= 0x80000000;
 screen = SDL_SetVideoMode(800, 600, 0, videoflags);
 if (screen == ((void *)0)) {
  fprintf(stderr, "Couldn't set video mode: %s\n",
   SDL_GetError());
  return 1;
 }
 colorSource = sgCreateSurface(1, 1);
 return 0;
}

variable_types:
videoflags
unsigned int


sgCreateColor

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

if ( colorSource )
result = SDL_MapRGB(*(_QWORD *)(colorSource + 8), a1, a2, a3);
else
result = 0LL;
return result;
}

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

if ( colorSource )
result = SDL_MapRGB(*(_QWORD *)(colorSource + 8), a1, a2, a3);
else
result = 0LL;
return result;
}

original_code:
Uint32 sgCreateColor(Uint8 r, Uint8 g, Uint8 b) {
 Uint32 result;
 if (colorSource != ((void *)0)) {
  result = SDL_MapRGB(colorSource->format, r, g, b);
 } else {
  result = 0;
 }
 return result;
}

canonical_original_code:
unsigned int sgCreateColor(unsigned char r, unsigned char g, unsigned char b) {
 unsigned int result;
 if (colorSource != ((void *)0)) {
  result = SDL_MapRGB(colorSource->format, r, g, b);
 } else {
  result = 0;
 }
 return result;
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char

variable_types:
result
unsigned int


sgDrawText

decompiled_code:
int __fastcall sgDrawText(const char *a1, int a2, int a3, int a4)
{
return sgDrawUTF8Text(screen, a2, a3, a4, a1);
}

canonical_decompiled_code:
int __fastcall func6(const char *a1, int a2, int a3, int a4)
{
return func5(screen, a2, a3, a4, a1);
}

original_code:
void sgDrawText(char *text, int x, int y, Uint32 color) {
 sgDrawUTF8Text(screen, x, y, color, text);
}

canonical_original_code:
void sgDrawText(char *text, int x, int y, unsigned int color) {
 sgDrawUTF8Text(screen, x, y, color, text);
}

variable_types:
color
unsigned int


sgClearScreen

decompiled_code:
__int64 sgClearScreen()
{
return SDL_FillRect(screen, 0LL, 0LL);
}

canonical_decompiled_code:
__int64 func8()
{
return SDL_FillRect(screen, 0LL, 0LL);
}

original_code:
void sgClearScreen() {
 SDL_FillRect(screen, 0, 0);
}


sgUpdateScreen

decompiled_code:
__int64 sgUpdateScreen()
{
return SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
}

canonical_decompiled_code:
__int64 func9()
{
return SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
}

original_code:
void sgUpdateScreen() {
 SDL_UpdateRect(screen, 0, 0, 0, 0);
}


wadLumpNameToOffset

decompiled_code:
__int64 __fastcall wadLumpNameToOffset(char *src)
{
int v1; // er13
const char *v2; // rbx
const char *v3; // r14
size_t v4; // r12
char *v5; // rcx
char *v6; // rcx
char *v7; // rbx
char *v8; // r13
__int64 result; // rax
__int64 v10; // rdx

v1 = 0;
v2 = cache_2;
v3 = cache_2;
v4 = (int)strlen(src);
do
{
if ( !strncmp(v3, src, v4) )
return qword_7D0[4 * v1];
++v1;
v3 += 32;
}
while ( v1 != 5 );
do
{
v5 = (char *)v2;
v2 += 32;
v6 = strncpy(v5, v2, 8uLL);
*((_QWORD *)v6 + 2) = *((_QWORD *)v6 + 6);
*((_QWORD *)v6 + 3) = *((_QWORD *)v6 + 7);
}
while ( v2 != dest );
if ( lumpMapLength <= 0 )
{
LABEL_11:
fprintf(stderr, "Lump Name '%s' not found in WAD, aborting\n", src);
exit(1);
}
v7 = lumpMap;
v8 = &lumpMap[32 * (lumpMapLength - 1) + 32];
while ( strncmp(v7, src, v4) )
{
v7 += 32;
if ( v7 == v8 )
goto LABEL_11;
}
strncpy(dest, src, 8uLL);
result = *((_QWORD *)v7 + 2);
v10 = *((_QWORD *)v7 + 3);
qword_850 = result;
qword_858 = v10;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src)
{
int v1; // er13
const char *v2; // rbx
const char *v3; // r14
size_t v4; // r12
char *v5; // rcx
char *v6; // rcx
char *v7; // rbx
char *v8; // r13
__int64 result; // rax
__int64 v10; // rdx

v1 = 0;
v2 = cache_2;
v3 = cache_2;
v4 = (int)strlen(src);
do
{
if ( !strncmp(v3, src, v4) )
return qword_7D0[4 * v1];
++v1;
v3 += 32;
}
while ( v1 != 5 );
do
{
v5 = (char *)v2;
v2 += 32;
v6 = strncpy(v5, v2, 8uLL);
*((_QWORD *)v6 + 2) = *((_QWORD *)v6 + 6);
*((_QWORD *)v6 + 3) = *((_QWORD *)v6 + 7);
}
while ( v2 != dest );
if ( lumpMapLength <= 0 )
{
LABEL_11:
fprintf(stderr, "Lump Name '%s' not found in WAD, aborting\n", src);
exit(1);
}
v7 = lumpMap;
v8 = &lumpMap[32 * (lumpMapLength - 1) + 32];
while ( strncmp(v7, src, v4) )
{
v7 += 32;
if ( v7 == v8 )
goto LABEL_11;
}
strncpy(dest, src, 8uLL);
result = *((_QWORD *)v7 + 2);
v10 = *((_QWORD *)v7 + 3);
qword_850 = result;
qword_858 = v10;
return result;
}

original_code:
static long wadLumpNameToOffset(char *lumpName) {
 int i;
 int stringLength;
 static LumpMap cache[5];
 stringLength = strlen(lumpName);
 for (i = 0; i < 5; i++) {
  if (strncmp(cache[i].lumpName, lumpName, stringLength) == 0) {
   return cache[i].lumpOffset;
  }
 }
 for (i = 0; i < 5 - 1; i++) {
  strncpy(cache[i].lumpName, cache[i + 1].lumpName, 8);
  cache[i].lumpOffset = cache[i + 1].lumpOffset;
  cache[i].lumpSize = cache[i + 1].lumpSize;
 }
 for (i = 0; i < lumpMapLength; i++) {
  if (strncmp(lumpMap[i].lumpName, lumpName, stringLength) == 0) {
   strncpy(cache[5 - 1].lumpName, lumpName, 8);
   cache[5 - 1].lumpOffset = lumpMap[i].lumpOffset;
   cache[5 -1 ].lumpSize = lumpMap[i].lumpSize;
   return lumpMap[i].lumpOffset;
  }
 }
 fprintf(stderr, "Lump Name '%s' not found in WAD, aborting\n", lumpName);
 exit(1);
 return 0;
}

canonical_original_code:
static long wadLumpNameToOffset(char *lumpName) {
 int i;
 int stringLength;
 static struct LumpMap cache[5];
 stringLength = strlen(lumpName);
 for (i = 0; i < 5; i++) {
  if (strncmp(cache[i].lumpName, lumpName, stringLength) == 0) {
   return cache[i].lumpOffset;
  }
 }
 for (i = 0; i < 5 - 1; i++) {
  strncpy(cache[i].lumpName, cache[i + 1].lumpName, 8);
  cache[i].lumpOffset = cache[i + 1].lumpOffset;
  cache[i].lumpSize = cache[i + 1].lumpSize;
 }
 for (i = 0; i < lumpMapLength; i++) {
  if (strncmp(lumpMap[i].lumpName, lumpName, stringLength) == 0) {
   strncpy(cache[5 - 1].lumpName, lumpName, 8);
   cache[5 - 1].lumpOffset = lumpMap[i].lumpOffset;
   cache[5 -1 ].lumpSize = lumpMap[i].lumpSize;
   return lumpMap[i].lumpOffset;
  }
 }
 fprintf(stderr, "Lump Name '%s' not found in WAD, aborting\n", lumpName);
 exit(1);
 return 0;
}

variable_types:
lumpName
char *

variable_types: i int

variable_types: stringLength int

variable_types: struct LumpMap { char lumpName[9]; long lumpOffset; long lumpSize; } [5]

return_type: long


wadReadShort

decompiled_code:
_BOOL8 __fastcall wadReadShort(_WORD *a1)
{
__int16 v1; // bp
__int16 v2; // ax
FILE *v3; // rbp
int v4; // er8
_BOOL8 result; // rax

v1 = getc(wadFile);
v2 = v1 | ((unsigned __int16)getc(wadFile) << 8);
v3 = wadFile;
*a1 = v2;
v4 = feof(v3);
result = 1LL;
if ( !v4 )
result = ferror(v3) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_WORD *a1)
{
__int16 v1; // bp
__int16 v2; // ax
FILE *v3; // rbp
int v4; // er8
_BOOL8 result; // rax

v1 = getc(wadFile);
v2 = v1 | ((unsigned __int16)getc(wadFile) << 8);
v3 = wadFile;
*a1 = v2;
v4 = feof(v3);
result = 1LL;
if ( !v4 )
result = ferror(v3) != 0;
return result;
}

original_code:
static short wadReadShort(short *value) {
 *value = getc(wadFile)
  | (getc(wadFile) << 8);
 return feof(wadFile) || ferror(wadFile);
}

variable_types:
value
short *

return_type: short


wadReadLong

decompiled_code:
_BOOL8 __fastcall wadReadLong(_QWORD *a1)
{
int v2; // ebx
int v3; // ebp
int v4; // ebp
int v5; // eax
int v6; // ebx
FILE *v7; // rbp
FILE *v8; // rdi
int v9; // er8
_BOOL8 result; // rax

v2 = getc(wadFile);
v3 = getc(wadFile);
v4 = (getc(wadFile) << 16) | (v3 << 8);
v5 = getc(wadFile);
v6 = v4 | v2;
v7 = wadFile;
v8 = wadFile;
*a1 = (v5 << 24) | v6;
v9 = feof(v8);
result = 1LL;
if ( !v9 )
result = ferror(v7) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_QWORD *a1)
{
int v2; // ebx
int v3; // ebp
int v4; // ebp
int v5; // eax
int v6; // ebx
FILE *v7; // rbp
FILE *v8; // rdi
int v9; // er8
_BOOL8 result; // rax

v2 = getc(wadFile);
v3 = getc(wadFile);
v4 = (getc(wadFile) << 16) | (v3 << 8);
v5 = getc(wadFile);
v6 = v4 | v2;
v7 = wadFile;
v8 = wadFile;
*a1 = (v5 << 24) | v6;
v9 = feof(v8);
result = 1LL;
if ( !v9 )
result = ferror(v7) != 0;
return result;
}

original_code:
static int wadReadLong(long *value) {
 *value = getc(wadFile)
  | (getc(wadFile) << 8)
  | (getc(wadFile) << 16)
  | (getc(wadFile) << 24);
 return feof(wadFile) || ferror(wadFile);
}

variable_types:
value
long *


wadReadLumpPalette

decompiled_code:
__int64 __fastcall wadReadLumpPalette(__int64 off)
{
const char *v1; // r12
unsigned __int8 v2; // bl
unsigned __int8 v3; // bp
unsigned __int8 v4; // al
__int64 result; // rax

v1 = (const char *)&palette;
fseek(wadFile, off, 0);
do
{
v1 += 4;
v2 = getc(wadFile);
v3 = getc(wadFile);
v4 = getc(wadFile);
result = sgCreateColor(v2, v3, v4);
*((_DWORD *)v1 - 1) = result;
}
while ( v1 != "Lump Name '%s' not found in WAD, aborting\n" );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 off)
{
const char *v1; // r12
unsigned __int8 v2; // bl
unsigned __int8 v3; // bp
unsigned __int8 v4; // al
__int64 result; // rax

v1 = (const char *)&palette;
fseek(wadFile, off, 0);
do
{
v1 += 4;
v2 = getc(wadFile);
v3 = getc(wadFile);
v4 = getc(wadFile);
result = sgCreateColor(v2, v3, v4);
*((_DWORD *)v1 - 1) = result;
}
while ( v1 != "Lump Name '%s' not found in WAD, aborting\n" );
return result;
}

original_code:
static void wadReadLumpPalette(long offset) {
 int i;
 Uint8 red;
 Uint8 green;
 Uint8 blue;
 fseek(wadFile, offset, 0);
 for (i = 0; i < 256; i++) {
  red = (Uint8)getc(wadFile);
  green = (Uint8)getc(wadFile);
  blue = (Uint8)getc(wadFile);
  palette[i] = sgCreateColor(red, green, blue);
 }
}

canonical_original_code:
static void wadReadLumpPalette(long offset) {
 int i;
 unsigned char red;
 unsigned char green;
 unsigned char blue;
 fseek(wadFile, offset, 0);
 for (i = 0; i < 256; i++) {
  red = (unsigned char)getc(wadFile);
  green = (unsigned char)getc(wadFile);
  blue = (unsigned char)getc(wadFile);
  palette[i] = sgCreateColor(red, green, blue);
 }
}

variable_types: offset long

variable_types:
red
unsigned char

variable_types:
green
unsigned char

variable_types:
blue
unsigned char


wadReadDir

decompiled_code:
void __fastcall wadReadDir(__int64 a1, __int64 a2)
{
void *v2; // rbp
__int64 v3; // rbx
char *v4; // rax
__int64 off; // [rsp+0h] [rbp-48h] BYREF
__int64 v6; // [rsp+8h] [rbp-40h] BYREF
fpos_t pos; // [rsp+10h] [rbp-38h] BYREF

v2 = calloc(1uLL, 9uLL);
lumpMap = (char *)malloc(32 * a1);
lumpMapLength = a1;
fseek(wadFile, a2, 0);
if ( a1 > 0 )
{
v3 = 0LL;
do
{
while ( 1 )
{
if ( wadReadLong(&off) )
_assert_fail("result == 0", "wadread.c", 0x39u, "wadReadDir");
if ( wadReadLong(&v6) )
_assert_fail("result == 0", "wadread.c", 0x3Bu, "wadReadDir");
if ( (unsigned int)fread(v2, 8uLL, 1uLL, wadFile) != 1 )
_assert_fail("result == 1", "wadread.c", 0x3Du, "wadReadDir");
v4 = strncpy(&lumpMap[32 * v3], (const char *)v2, 8uLL);
v4[8] = 0;
*((_QWORD *)v4 + 2) = off;
*((_QWORD *)v4 + 3) = v6;
if ( !strcmp((const char *)v2, "PLAYPAL") )
break;
if ( a1 == ++v3 )
goto LABEL_9;
}
++v3;
fgetpos(wadFile, &pos);
wadReadLumpPalette(off);
fsetpos(wadFile, &pos);
}
while ( a1 != v3 );
}
LABEL_9:
free(v2);
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, __int64 a2)
{
void *v2; // rbp
__int64 v3; // rbx
char *v4; // rax
__int64 off; // [rsp+0h] [rbp-48h] BYREF
__int64 v6; // [rsp+8h] [rbp-40h] BYREF
fpos_t pos; // [rsp+10h] [rbp-38h] BYREF

v2 = calloc(1uLL, 9uLL);
lumpMap = (char *)malloc(32 * a1);
lumpMapLength = a1;
fseek(wadFile, a2, 0);
if ( a1 > 0 )
{
v3 = 0LL;
do
{
while ( 1 )
{
if ( func2(&off) )
_assert_fail("result == 0", "wadread.c", 0x39u, "wadReadDir");
if ( func2(&v6) )
_assert_fail("result == 0", "wadread.c", 0x3Bu, "wadReadDir");
if ( (unsigned int)fread(v2, 8uLL, 1uLL, wadFile) != 1 )
_assert_fail("result == 1", "wadread.c", 0x3Du, "wadReadDir");
v4 = strncpy(&lumpMap[32 * v3], (const char *)v2, 8uLL);
v4[8] = 0;
*((_QWORD *)v4 + 2) = off;
*((_QWORD *)v4 + 3) = v6;
if ( !strcmp((const char *)v2, "PLAYPAL") )
break;
if ( a1 == ++v3 )
goto LABEL_9;
}
++v3;
fgetpos(wadFile, &pos);
func3(off);
fsetpos(wadFile, &pos);
}
while ( a1 != v3 );
}
LABEL_9:
free(v2);
}

original_code:
static void wadReadDir(long numLumps, long dirOffset) {
 int i;
 int result;
 long lumpOffset;
 long lumpSize;
 char *lumpName;
 fpos_t filepos;
 lumpName = (char*)calloc(1, 9);
 lumpMap = (LumpMap*)malloc(sizeof(LumpMap) * numLumps);
 lumpMapLength = numLumps;
 fseek(wadFile, dirOffset, 0);
 for (i = 0; i < numLumps; i++) {
  result = wadReadLong(&lumpOffset);
  ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 57, __extension__ __PRETTY_FUNCTION__));
  result = wadReadLong(&lumpSize);
  ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 59, __extension__ __PRETTY_FUNCTION__));
  result = fread(lumpName, 8, 1, wadFile);
  ((result == 1) ? (void) (0) : __assert_fail ("result == 1", "wadread.c", 61, __extension__ __PRETTY_FUNCTION__));
  strncpy(lumpMap[i].lumpName, lumpName, 8);
  lumpMap[i].lumpName[8] = 0;
  lumpMap[i].lumpOffset = lumpOffset;
  lumpMap[i].lumpSize = lumpSize;
  if (strcmp(lumpName, "PLAYPAL") == 0) {
   fgetpos(wadFile, &filepos);
   wadReadLumpPalette(lumpOffset);
   fsetpos(wadFile, &filepos);
  }
 }
 free(lumpName);
}

canonical_original_code:
static void wadReadDir(long numLumps, long dirOffset) {
 int i;
 int result;
 long lumpOffset;
 long lumpSize;
 char *lumpName;
 struct _G_fpos_t filepos;
 lumpName = (char *)calloc(1, 9);
 lumpMap = (struct LumpMap *)malloc(sizeof(LumpMap) * numLumps);
 lumpMapLength = numLumps;
 fseek(wadFile, dirOffset, 0);
 for (i = 0; i < numLumps; i++) {
  result = wadReadLong(&lumpOffset);
  ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 57, __extension__ __PRETTY_FUNCTION__));
  result = wadReadLong(&lumpSize);
  ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 59, __extension__ __PRETTY_FUNCTION__));
  result = fread(lumpName, 8, 1, wadFile);
  ((result == 1) ? (void) (0) : __assert_fail ("result == 1", "wadread.c", 61, __extension__ __PRETTY_FUNCTION__));
  strncpy(lumpMap[i].lumpName, lumpName, 8);
  lumpMap[i].lumpName[8] = 0;
  lumpMap[i].lumpOffset = lumpOffset;
  lumpMap[i].lumpSize = lumpSize;
  if (strcmp(lumpName, "PLAYPAL") == 0) {
   fgetpos(wadFile, &filepos);
   wadReadLumpPalette(lumpOffset);
   fsetpos(wadFile, &filepos);
  }
 }
 free(lumpName);
}

variable_types: numLumps long

variable_types: dirOffset long

variable_types: result int

variable_types: lumpOffset long

variable_types: lumpSize long

variable_types: struct _G_fpos_t { long __pos; struct __mbstate_t { int __count; union { unsigned int __wch; char __wchb[4]; } __value; } __state; }

user_defined_types: struct _G_fpos_t { long __pos; struct __mbstate_t __state; }

user_defined_types: struct LumpMap { char lumpName[9]; long lumpOffset; long lumpSize; }


wadInit

decompiled_code:
__int64 __fastcall wadInit(char *filename)
{
void *v1; // rax
void *v2; // rbp
char dest[5]; // [rsp+Bh] [rbp-2Dh] BYREF
__int64 v5; // [rsp+10h] [rbp-28h] BYREF
__int64 v6[4]; // [rsp+18h] [rbp-20h] BYREF

strcpy(dest, "xWAD");
v1 = calloc(1uLL, 5uLL);
if ( !v1 || (v2 = v1, (wadFile = fopen(filename, "r")) == 0LL) )
{
perror("wadInit");
exit(1);
}
if ( (unsigned int)fread(v2, 4uLL, 1uLL, wadFile) != 1 )
_assert_fail("result == 1", "wadread.c", 0xCEu, "wadInit");
if ( strcmp(strncpy(dest, (const char *)v2, 4uLL), "IWAD") )
{
fprintf(stderr, "Error: %s is no valid WAD file\n", filename);
exit(1);
}
if ( wadReadLong(&v5) )
_assert_fail("result == 0", "wadread.c", 0xD7u, "wadInit");
if ( wadReadLong(v6) )
_assert_fail("result == 0", "wadread.c", 0xD9u, "wadInit");
wadReadDir(v5, v6[0]);
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *filename)
{
void *v1; // rax
void *v2; // rbp
char dest[5]; // [rsp+Bh] [rbp-2Dh] BYREF
__int64 v5; // [rsp+10h] [rbp-28h] BYREF
__int64 v6[4]; // [rsp+18h] [rbp-20h] BYREF

strcpy(dest, "xWAD");
v1 = calloc(1uLL, 5uLL);
if ( !v1 || (v2 = v1, (wadFile = fopen(filename, "r")) == 0LL) )
{
perror("wadInit");
exit(1);
}
if ( (unsigned int)fread(v2, 4uLL, 1uLL, wadFile) != 1 )
_assert_fail("result == 1", "wadread.c", 0xCEu, "wadInit");
if ( strcmp(strncpy(dest, (const char *)v2, 4uLL), "IWAD") )
{
fprintf(stderr, "Error: %s is no valid WAD file\n", filename);
exit(1);
}
if ( func2(&v5) )
_assert_fail("result == 0", "wadread.c", 0xD7u, "wadInit");
if ( func2(v6) )
_assert_fail("result == 0", "wadread.c", 0xD9u, "wadInit");
func4(v5, v6[0]);
free(v2);
return 0LL;
}

original_code:
int wadInit(char *wadFileName) {
 char wadHeaderType[5] = "xWAD";
 long wadHeaderNumLumps;
 long wadHeaderDirOffset;
 int result;
 void *buf;
 buf = calloc(1, 5);
 if (buf == ((void *)0)) {
  perror("wadInit");
  exit(1);
 }
 wadFile = fopen(wadFileName, "r");
 if (wadFile == ((void *)0)) {
  perror("wadInit");
  exit(1);
 }
 ((wadFile != ((void *)0)) ? (void) (0) : __assert_fail ("wadFile != NULL", "wadread.c", 202, __extension__ __PRETTY_FUNCTION__));
 result = fread(buf, 4, 1, wadFile);
 ((result == 1) ? (void) (0) : __assert_fail ("result == 1", "wadread.c", 206, __extension__ __PRETTY_FUNCTION__));
 strncpy(wadHeaderType, buf, 4);
 if (strcmp(wadHeaderType, "IWAD") != 0) {
  fprintf(stderr, "Error: %s is no valid WAD file\n", wadFileName);
  exit(1);
 }
 result = wadReadLong(&wadHeaderNumLumps);
 ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 215, __extension__ __PRETTY_FUNCTION__));
 result = wadReadLong(&wadHeaderDirOffset);
 ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 217, __extension__ __PRETTY_FUNCTION__));
 wadReadDir(wadHeaderNumLumps, wadHeaderDirOffset);
 free(buf);
 return 0;
}

variable_types:
wadFileName
char *

variable_types:
wadHeaderType
char [5]

variable_types: wadHeaderNumLumps long

variable_types: wadHeaderDirOffset long

variable_types:
buf
void *


timeLeft

decompiled_code:
__int64 timeLeft()
{
unsigned int v0; // eax
unsigned int v1; // er8

v0 = SDL_GetTicks();
v1 = nextTime_0 - v0;
if ( nextTime_0 <= v0 )
{
v1 = 0;
nextTime_0 = v0 + 50;
}
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax
unsigned int v1; // er8

v0 = SDL_GetTicks();
v1 = nextTime_0 - v0;
if ( nextTime_0 <= v0 )
{
v1 = 0;
nextTime_0 = v0 + 50;
}
return v1;
}

original_code:
static Uint32 timeLeft() {
 static Uint32 nextTime = 0;
 Uint32 now;
 now = SDL_GetTicks();
 if (nextTime <= now) {
  nextTime = now + 50;
  return 0;
 }
 return (nextTime - now);
}

canonical_original_code:
static unsigned int timeLeft() {
 static unsigned int nextTime = 0;
 unsigned int now;
 now = SDL_GetTicks();
 if (nextTime <= now) {
  nextTime = now + 50;
  return 0;
 }
 return (nextTime - now);
}

variable_types:
nextTime
unsigned int

variable_types:
now
unsigned int


loopDelay

decompiled_code:
__int64 loopDelay()
{
unsigned int v0; // eax

v0 = timeLeft();
return SDL_Delay(v0);
}

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

v0 = func0();
return SDL_Delay(v0);
}

original_code:
void loopDelay() {
 SDL_Delay(timeLeft());
}


main

canonical_name: main

repo: atextor/icat

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
unsigned int v4; // er12
int v5; // eax
int v6; // edx
char **v7; // r14
char *v8; // rbx
int v9; // ebx
int i; // ebx
int j; // er15
int result; // eax
int v13; // [rsp+Ch] [rbp-8Ch]
unsigned int v14; // [rsp+Ch] [rbp-8Ch]
const char **v15; // [rsp+10h] [rbp-88h]
char v16; // [rsp+1Bh] [rbp-7Dh]
unsigned int v17; // [rsp+1Ch] [rbp-7Ch]
int v18; // [rsp+28h] [rbp-70h] BYREF
int v19; // [rsp+2Ch] [rbp-6Ch] BYREF
char templatea[16]; // [rsp+30h] [rbp-68h] BYREF
char v21[16]; // [rsp+40h] [rbp-58h] BYREF
char buf[72]; // [rsp+50h] [rbp-48h] BYREF

v3 = 0;
v4 = 1;
v16 = 0;
v17 = 0;
v13 = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "hx:y:w:km:", &long_options_1, 0LL);
if ( v5 == -1 )
break;
if ( v5 != 63 )
{
switch ( v5 )
{
case 'k':
v16 = 1;
break;
case 'm':
if ( !strcmp(optarg, "indexed") )
{
v4 = 1;
}
else if ( !strcmp(optarg, "24bit") )
{
v4 = 2;
}
else
{
if ( strcmp(optarg, "both") )
{
puts("Mode must be one of 'indexed', '24bit', or 'both'.");
exit(1);
}
v4 = 3;
}
break;
case 'w':
v17 = atoi(optarg);
if ( !v17 )
{
puts("Image width must be larger than 0");
exit(1);
}
return result;
case 'x':
v3 = atoi(optarg);
break;
case 'y':
v13 = atoi(optarg);
break;
default:
display_help_0 = 1;
break;
}
}
}
if ( display_help_0 == 1 || _bss_start == argc )
{
print_usage();
exit(0);
}
v6 = 0;
if ( argc - _bss_start < 2 )
v6 = v13;
v14 = v6;
if ( _bss_start < argc )
{
v7 = (char **)&argv[_bss_start];
v15 = &(&argv[_bss_start])[(unsigned __int64)(unsigned int)(argc + ~_bss_start) + 1];
do
{
v8 = *v7;
strcpy(templatea, "/tmp/icatXXXXXX");
if ( *v8 == 45 && !v8[1] )
{
v9 = mkstemp(templatea);
if ( v9 < 0 )
{
perror("mkstemp");
exit(1);
}
while ( read(0, buf, 1uLL) > 0 )
write(v9, buf, 1uLL);
v8 = templatea;
}
if ( !imlib_load_image_immediately() )
{
fprintf(stderr, "Could not load image: %s\n", v8);
exit(1);
}
imlib_context_set_image();
v18 = imlib_image_get_width();
v19 = imlib_image_get_height();
if ( !v16 )
resize_image_if_necessary(&v18, &v19, v17);
if ( v14 )
printf("\x1B[%d;%dH", v14, v3);
for ( i = 0; v19 > i; i += 2 )
{
if ( v3 )
printf("\x1B[%dG", v3);
for ( j = 0; v18 > j; ++j )
{
imlib_image_query_pixel();
imlib_image_query_pixel();
print_pixels(v21, buf, v4);
}
puts("\x1B[0m");
}
imlib_free_image_and_decache();
++v7;
}
while ( v7 != (char **)v15 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
unsigned int v4; // er12
int v5; // eax
int v6; // edx
char **v7; // r14
char *v8; // rbx
int v9; // ebx
int i; // ebx
int j; // er15
int result; // eax
int v13; // [rsp+Ch] [rbp-8Ch]
unsigned int v14; // [rsp+Ch] [rbp-8Ch]
const char **v15; // [rsp+10h] [rbp-88h]
char v16; // [rsp+1Bh] [rbp-7Dh]
unsigned int v17; // [rsp+1Ch] [rbp-7Ch]
int v18; // [rsp+28h] [rbp-70h] BYREF
int v19; // [rsp+2Ch] [rbp-6Ch] BYREF
char templatea[16]; // [rsp+30h] [rbp-68h] BYREF
char v21[16]; // [rsp+40h] [rbp-58h] BYREF
char buf[72]; // [rsp+50h] [rbp-48h] BYREF

v3 = 0;
v4 = 1;
v16 = 0;
v17 = 0;
v13 = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "hx:y:w:km:", &long_options_1, 0LL);
if ( v5 == -1 )
break;
if ( v5 != 63 )
{
switch ( v5 )
{
case 'k':
v16 = 1;
break;
case 'm':
if ( !strcmp(optarg, "indexed") )
{
v4 = 1;
}
else if ( !strcmp(optarg, "24bit") )
{
v4 = 2;
}
else
{
if ( strcmp(optarg, "both") )
{
puts("Mode must be one of 'indexed', '24bit', or 'both'.");
exit(1);
}
v4 = 3;
}
break;
case 'w':
v17 = atoi(optarg);
if ( !v17 )
{
puts("Image width must be larger than 0");
exit(1);
}
return result;
case 'x':
v3 = atoi(optarg);
break;
case 'y':
v13 = atoi(optarg);
break;
default:
display_help_0 = 1;
break;
}
}
}
if ( display_help_0 == 1 || _bss_start == argc )
{
func8();
exit(0);
}
v6 = 0;
if ( argc - _bss_start < 2 )
v6 = v13;
v14 = v6;
if ( _bss_start < argc )
{
v7 = (char **)&argv[_bss_start];
v15 = &(&argv[_bss_start])[(unsigned __int64)(unsigned int)(argc + ~_bss_start) + 1];
do
{
v8 = *v7;
strcpy(templatea, "/tmp/icatXXXXXX");
if ( *v8 == 45 && !v8[1] )
{
v9 = mkstemp(templatea);
if ( v9 < 0 )
{
perror("mkstemp");
exit(1);
}
while ( read(0, buf, 1uLL) > 0 )
write(v9, buf, 1uLL);
v8 = templatea;
}
if ( !imlib_load_image_immediately() )
{
fprintf(stderr, "Could not load image: %s\n", v8);
exit(1);
}
imlib_context_set_image();
v18 = imlib_image_get_width();
v19 = imlib_image_get_height();
if ( !v16 )
func10(&v18, &v19, v17);
if ( v14 )
printf("\x1B[%d;%dH", v14, v3);
for ( i = 0; v19 > i; i += 2 )
{
if ( v3 )
printf("\x1B[%dG", v3);
for ( j = 0; v18 > j; ++j )
{
imlib_image_query_pixel();
imlib_image_query_pixel();
func11(v21, buf, v4);
}
puts("\x1B[0m");
}
imlib_free_image_and_decache();
++v7;
}
while ( v7 != (char **)v15 );
}
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 char *filename;
 static int display_help = 0;
 Imlib_Image image = ((void *)0);
 unsigned int x = 0;
 unsigned int y = 0;
 unsigned int user_w = 0;
 int c;
 _Bool keep_size = 0;
 int mode = MODE_INDEXED;
 for(;;) {
  static struct option long_options[] = {
   {"help", 0, &display_help, 1},
   {"x", 1, 0, 'x'},
   {"y", 1, 0, 'y'},
   {"width",1, 0, 'w'},
   {"keep", 0, 0, 'k'},
   {"mode", 1, 0, 'm'},
   {0, 0, 0, 0}
  };
  c = getopt_long(argc, argv, "hx:y:w:km:", long_options, ((void *)0));
  if (c == -1)
   break;
  switch (c) {
   case 'x':
    x = atoi(optarg);
    if (x < 0) {
     x = 0;
    }
    break;
   case 'y':
    y = atoi(optarg);
    if (y < 0) {
     y = 0;
    }
    break;
   case 'w':
    user_w = atoi(optarg);
    if(user_w < 1) {
     printf("Image width must be larger than 0\n");
     exit(1);
    }
    break;
   case 'h':
    display_help = 1;
    break;
   case 'k':
    keep_size = 1;
    break;
   case 'm':
    if(strcmp(optarg, "indexed") == 0) {
     mode = MODE_INDEXED;
    } else if(strcmp(optarg, "24bit") == 0) {
     mode = MODE_24_BIT;
    } else if(strcmp(optarg, "both") == 0) {
     mode = MODE_BOTH;
    } else {
     printf("Mode must be one of 'indexed', '24bit', or 'both'.\n");
     exit(1);
    }
    break;
   case '?':
    break;
   default:
    display_help = 1;
    break;
  }
 }
 if (display_help == 1 || optind == argc) {
  print_usage();
  exit(0);
 }
 if (argc - optind > 1) {
  y = 0;
 }
 for (int i = optind; i < argc; i++) {
  char tempfile_name[] = "/tmp/icatXXXXXX";
  if (strcmp(argv[i], "-") == 0) {
   int tempfile;
   if ((tempfile = mkstemp(tempfile_name)) < 0) {
    perror("mkstemp");
    exit(1);
   }
   filename = tempfile_name;
   char buf;
   while (read(0, &buf, 1) > 0) {
    write(tempfile, &buf, 1);
   }
  } else {
   filename = argv[i];
  }
  image = imlib_load_image_immediately(filename);
  if (!image) {
   fprintf(stderr, "Could not load image: %s\n", filename);
   exit(1);
  }
  imlib_context_set_image(image);
  int width = imlib_image_get_width();
  int height = imlib_image_get_height();
  if (!keep_size) {
   resize_image_if_necessary(&width, &height, user_w);
  }
  if (y > 0) {
   printf("\x1b[%d;%dH", y, x);
  }
  Imlib_Color pixel1;
  Imlib_Color pixel2;
  for (int h = 0; h < height; h += 2) {
   if (x > 0) {
    printf("\x1b[%dG", x);
   }
   for (int w = 0; w < width; w++) {
    imlib_image_query_pixel(w, h + 1, &pixel1);
    imlib_image_query_pixel(w, h, &pixel2);
    print_pixels(&pixel1, &pixel2, mode);
   }
   printf("\x1b[0m\n");
  }
  imlib_free_image_and_decache();
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 char *filename;
 static int display_help = 0;
 void *image = ((void *)0);
 unsigned int x = 0;
 unsigned int y = 0;
 unsigned int user_w = 0;
 int c;
 _Bool keep_size = 0;
 int mode = MODE_INDEXED;
 for(;;) {
  static struct option long_options[] = {
   {"help", 0, &display_help, 1},
   {"x", 1, 0, 'x'},
   {"y", 1, 0, 'y'},
   {"width",1, 0, 'w'},
   {"keep", 0, 0, 'k'},
   {"mode", 1, 0, 'm'},
   {0, 0, 0, 0}
  };
  c = getopt_long(argc, argv, "hx:y:w:km:", long_options, ((void *)0));
  if (c == -1)
   break;
  switch (c) {
   case 'x':
    x = atoi(optarg);
    if (x < 0) {
     x = 0;
    }
    break;
   case 'y':
    y = atoi(optarg);
    if (y < 0) {
     y = 0;
    }
    break;
   case 'w':
    user_w = atoi(optarg);
    if(user_w < 1) {
     printf("Image width must be larger than 0\n");
     exit(1);
    }
    break;
   case 'h':
    display_help = 1;
    break;
   case 'k':
    keep_size = 1;
    break;
   case 'm':
    if(strcmp(optarg, "indexed") == 0) {
     mode = MODE_INDEXED;
    } else if(strcmp(optarg, "24bit") == 0) {
     mode = MODE_24_BIT;
    } else if(strcmp(optarg, "both") == 0) {
     mode = MODE_BOTH;
    } else {
     printf("Mode must be one of 'indexed', '24bit', or 'both'.\n");
     exit(1);
    }
    break;
   case '?':
    break;
   default:
    display_help = 1;
    break;
  }
 }
 if (display_help == 1 || optind == argc) {
  print_usage();
  exit(0);
 }
 if (argc - optind > 1) {
  y = 0;
 }
 for (int i = optind; i < argc; i++) {
  char tempfile_name[] = "/tmp/icatXXXXXX";
  if (strcmp(argv[i], "-") == 0) {
   int tempfile;
   if ((tempfile = mkstemp(tempfile_name)) < 0) {
    perror("mkstemp");
    exit(1);
   }
   filename = tempfile_name;
   char buf;
   while (read(0, &buf, 1) > 0) {
    write(tempfile, &buf, 1);
   }
  } else {
   filename = argv[i];
  }
  image = imlib_load_image_immediately(filename);
  if (!image) {
   fprintf(stderr, "Could not load image: %s\n", filename);
   exit(1);
  }
  imlib_context_set_image(image);
  int width = imlib_image_get_width();
  int height = imlib_image_get_height();
  if (!keep_size) {
   resize_image_if_necessary(&width, &height, user_w);
  }
  if (y > 0) {
   printf("\x1b[%d;%dH", y, x);
  }
  struct _imlib_color pixel1;
  struct _imlib_color pixel2;
  for (int h = 0; h < height; h += 2) {
   if (x > 0) {
    printf("\x1b[%dG", x);
   }
   for (int w = 0; w < width; w++) {
    imlib_image_query_pixel(w, h + 1, &pixel1);
    imlib_image_query_pixel(w, h, &pixel2);
    print_pixels(&pixel1, &pixel2, mode);
   }
   printf("\x1b[0m\n");
  }
  imlib_free_image_and_decache();
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
filename
char *

variable_types:
image
void *

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types: c int

variable_types: mode int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: i int

variable_types: tempfile int

variable_types: buf char

variable_types: width int

variable_types: height int

variable_types: h int

variable_types: w int


original_code:
uint8_t rgb2xterm(Imlib_Color* pixel) {
 uint8_t c, match = 0;
 uint32_t r, g, b, d, distance;
 distance = 1000000000;
 for(c = 0; c <= 253; c++) {
  r = ((0xff0000 & colors[c]) >> 16) - pixel->red;
  g = ((0x00ff00 & colors[c]) >> 8) - pixel->green;
  b = (0x0000ff & colors[c]) - pixel->blue;
  d = r * r + g * g + b * b;
  if (d < distance) {
   distance = d;
   match = c;
  }
 }
 return match;
}

canonical_original_code:
unsigned char rgb2xterm(struct _imlib_color *pixel) {
 unsigned char c; unsigned char match = 0;
 unsigned int r; unsigned int g; unsigned int b; unsigned int d; unsigned int distance;
 distance = 1000000000;
 for(c = 0; c <= 253; c++) {
  r = ((0xff0000 & colors[c]) >> 16) - pixel->red;
  g = ((0x00ff00 & colors[c]) >> 8) - pixel->green;
  b = (0x0000ff & colors[c]) - pixel->blue;
  d = r * r + g * g + b * b;
  if (d < distance) {
   distance = d;
   match = c;
  }
 }
 return match;
}

variable_types:
pixel
struct _imlib_color { int alpha; } *

variable_types:
c
unsigned char

variable_types:
match
unsigned char

variable_types:
r
unsigned int

variable_types:
g
unsigned int

variable_types:
b
unsigned int

variable_types:
d
unsigned int

variable_types:
distance
unsigned int

return_type: unsigned char


decompiled_code:
int print_usage()
{
return puts(
"icat (0.5) outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
"Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
" [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
"\t-h | --help -- Display this message\n"
"\t-x value -- Specify the column to print the image in (min. 1)\n"
"\t-y value -- Specify the row to print the image in (min. 1)\n"
"\t This is ignored when more than one image is printed.\n"
"\t-w | --width value\n"
"\t Instead of resizing the image to fit the terminal width,\n"
"\t use the provided value as the desired width.\n"
"\t-k | --keep -- Keep image size, i.e. do not automatically resize image to fit\n"
"\t the terminal width.\n"
"\t-m | --mode indexed|24bit|both\n"
"\t Use indexed (256-color, the default), 24-bit color, or both.\n"
"\timagefile -- The image to print. If the file name is \"-\", the file is\n"
"\t read from stdin.\n"
"Big images are automatically resized to your terminal width, unless with the -k option.\n"
"You can achieve the same effect with convert (from the ImageMagick package):\n"
"\tconvert -resize $((COLUMNS - 2))x image.png - | icat -\n"
"Or read images from another source:\n"
"\tcurl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -");
}

canonical_decompiled_code:
int func8()
{
return puts(
"icat (0.5) outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
"Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
" [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
"\t-h | --help -- Display this message\n"
"\t-x value -- Specify the column to print the image in (min. 1)\n"
"\t-y value -- Specify the row to print the image in (min. 1)\n"
"\t This is ignored when more than one image is printed.\n"
"\t-w | --width value\n"
"\t Instead of resizing the image to fit the terminal width,\n"
"\t use the provided value as the desired width.\n"
"\t-k | --keep -- Keep image size, i.e. do not automatically resize image to fit\n"
"\t the terminal width.\n"
"\t-m | --mode indexed|24bit|both\n"
"\t Use indexed (256-color, the default), 24-bit color, or both.\n"
"\timagefile -- The image to print. If the file name is \"-\", the file is\n"
"\t read from stdin.\n"
"Big images are automatically resized to your terminal width, unless with the -k option.\n"
"You can achieve the same effect with convert (from the ImageMagick package):\n"
"\tconvert -resize $((COLUMNS - 2))x image.png - | icat -\n"
"Or read images from another source:\n"
"\tcurl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -");
}

original_code:
void print_usage() {
 printf("icat (" "0.5" ") outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
   "Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
   "            [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
   "	-h | --help  -- Display this message\n"
   "	-x value     -- Specify the column to print the image in (min. 1)\n"
   "	-y value     -- Specify the row to print the image in (min. 1)\n"
   "	                This is ignored when more than one image is printed.\n"
   "	-w | --width value\n"
   "	                Instead of resizing the image to fit the terminal width,\n"
   "	                use the provided value as the desired width.\n"
   "	-k | --keep  -- Keep image size, i.e. do not automatically resize image to fit\n"
   "	                the terminal width.\n"
   "	-m | --mode indexed|24bit|both\n"
   "	                Use indexed (256-color, the default), 24-bit color, or both.\n"
   "	imagefile    -- The image to print. If the file name is \"-\", the file is\n"
   "	                read from stdin.\n"
   "Big images are automatically resized to your terminal width, unless with the -k option.\n"
   "You can achieve the same effect with convert (from the ImageMagick package):\n"
   "	convert -resize $((COLUMNS - 2))x image.png - | icat -\n"
   "Or read images from another source:\n"
   "	curl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -\n");
}


decompiled_code:
__int64 terminal_width()
{
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

ioctl(0, 0x5413uLL, v1);
return v2;
}

canonical_decompiled_code:
__int64 func9()
{
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

ioctl(0, 0x5413uLL, v1);
return v2;
}

original_code:
int terminal_width() {
 int cols = 80;
 struct winsize ts;
 ioctl(0, 0x5413, &ts);
 cols = ts.ws_col;
 return cols;
}

variable_types: cols int

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }


decompiled_code:
__int64 __fastcall resize_image_if_necessary(int *a1, int *a2, int a3)
{
int v3; // ebx
int v4; // edx
int v5; // er14
__int64 result; // rax

if ( a3 )
{
v3 = a3;
v4 = *a1;
LABEL_3:
v5 = (int)(float)((float)((float)v3 / (float)v4) * (float)*a2);
imlib_create_cropped_scaled_image();
imlib_free_image_and_decache();
result = imlib_context_set_image();
*a1 = v3;
*a2 = v5;
return result;
}
result = terminal_width();
v4 = *a1;
v3 = result;
if ( *a1 >= (int)result )
goto LABEL_3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int *a1, int *a2, int a3)
{
int v3; // ebx
int v4; // edx
int v5; // er14
__int64 result; // rax

if ( a3 )
{
v3 = a3;
v4 = *a1;
LABEL_3:
v5 = (int)(float)((float)((float)v3 / (float)v4) * (float)*a2);
imlib_create_cropped_scaled_image();
imlib_free_image_and_decache();
result = imlib_context_set_image();
*a1 = v3;
*a2 = v5;
return result;
}
result = func9();
v4 = *a1;
v3 = result;
if ( *a1 >= (int)result )
goto LABEL_3;
return result;
}

original_code:
void resize_image_if_necessary(int *width, int *height, const int user_width) {
 int resized_width;
 if (!user_width) {
  int cols = terminal_width();
  if(cols > *width) return;
  resized_width = cols;
 } else {
  resized_width = user_width;
 }
 float ratio = ((float)resized_width) / *width;
 int resized_height = *height * ratio;
 Imlib_Image resized_image = imlib_create_cropped_scaled_image(0, 0,
   *width, *height, resized_width, resized_height);
 imlib_free_image_and_decache();
 imlib_context_set_image(resized_image);
 *width = resized_width;
 *height = resized_height;
}

canonical_original_code:
void resize_image_if_necessary(int *width, int *height, int user_width) {
 int resized_width;
 if (!user_width) {
  int cols = terminal_width();
  if(cols > *width) return;
  resized_width = cols;
 } else {
  resized_width = user_width;
 }
 float ratio = ((float)resized_width) / *width;
 int resized_height = *height * ratio;
 void *resized_image = imlib_create_cropped_scaled_image(0, 0,
   *width, *height, resized_width, resized_height);
 imlib_free_image_and_decache();
 imlib_context_set_image(resized_image);
 *width = resized_width;
 *height = resized_height;
}

variable_types:
width
int *

variable_types:
height
int *

variable_types: ratio float