decompiled_code:
void __fastcall __noreturn main(__int64 a1, char **a2, char **a3)
{
__int64 v3; // rax
void *v4; // rbp
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = sub_4014D0(a1, a2);
v4 = (void *)v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)sub_4015D0(v3) )
{
puts("Error: Could not locate serio mouse");
sub_401570(v4);
exit(1);
}
sub_401A30(v4, v5, 767LL);
sub_4018E0(v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)sub_401A30(v4, v5, 4328LL)
|| (unsigned int)sub_401A30(v4, 0LL, 230LL)
|| (unsigned int)sub_401A30(v4, 0LL, 230LL)
|| (unsigned int)sub_401A30(v4, 0LL, 230LL)
|| (unsigned int)sub_401A30(v4, v5, 1001LL) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_9;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( (unsigned int)sub_401A30(v4, v5, 4328LL)
|| (unsigned int)sub_401A30(v4, 0LL, 231LL)
|| (unsigned int)sub_401A30(v4, 0LL, 231LL)
|| (unsigned int)sub_401A30(v4, 0LL, 231LL)
|| (unsigned int)sub_401A30(v4, v5, 1001LL) )
{
puts("E7 report failed, not an ALPS touchpad");
}
else
{
if ( LOBYTE(v5[0]) != 115 )
{
LABEL_24:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( HIBYTE(v5[0]) == 3 )
{
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_24;
puts("V5 ALPS touchpad found");
}
}
else
{
if ( *(_WORD *)((char *)v5 + 1) != 25602 )
goto LABEL_24;
puts("V3 ALPS touchpad found");
}
}
LABEL_9:
sub_401570(v4);
sub_4018C0(v4);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn main(__int64 a1, char **a2, char **a3)
{
__int64 v3; // rax
void *v4; // rbp
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = func6(a1, a2);
v4 = (void *)v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)func9(v3) )
{
puts("Error: Could not locate serio mouse");
func8(v4);
exit(1);
}
func13(v4, v5, 767LL);
func11(v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)func13(v4, v5, 4328LL)
|| (unsigned int)func13(v4, 0LL, 230LL)
|| (unsigned int)func13(v4, 0LL, 230LL)
|| (unsigned int)func13(v4, 0LL, 230LL)
|| (unsigned int)func13(v4, v5, 1001LL) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_9;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( (unsigned int)func13(v4, v5, 4328LL)
|| (unsigned int)func13(v4, 0LL, 231LL)
|| (unsigned int)func13(v4, 0LL, 231LL)
|| (unsigned int)func13(v4, 0LL, 231LL)
|| (unsigned int)func13(v4, v5, 1001LL) )
{
puts("E7 report failed, not an ALPS touchpad");
}
else
{
if ( LOBYTE(v5[0]) != 115 )
{
LABEL_24:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( HIBYTE(v5[0]) == 3 )
{
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_24;
puts("V5 ALPS touchpad found");
}
}
else
{
if ( *(_WORD *)((char *)v5 + 1) != 25602 )
goto LABEL_24;
puts("V3 ALPS touchpad found");
}
}
LABEL_9:
func8(v4);
func10(v4);
exit(0);
}


main

canonical_name: main

repo: timjb/tlpi-exercises

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts("before a()");
a("before a()", argv);
puts("before b()");
b();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts("before a()");
func7("before a()", argv);
puts("before b()");
func8();
}

original_code:
int main (int argc, char *argv[]) {
  printf("before a()\n");
  a();
  printf("before b()\n");
  b();
  printf("after b()\n");
  exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
  printf("before a()\n");
  a();
  printf("before b()\n");
  b();
  printf("after b()\n");
  exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


a

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

if ( _setjmp(jmp_env) )
result = puts("jumping with longjmp");
else
result = puts("first time setjmp");
return result;
}

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

if ( _setjmp(jmp_env) )
result = puts("jumping with longjmp");
else
result = puts("first time setjmp");
return result;
}

original_code:
void a (void) {
  if (_setjmp (jmp_env) == 0) {
    printf("first time setjmp\n");
  } else {
    printf("jumping with longjmp\n");
  }
}

canonical_original_code:
void a() {
  if (_setjmp (jmp_env) == 0) {
    printf("first time setjmp\n");
  } else {
    printf("jumping with longjmp\n");
  }
}

return_type: void


b

decompiled_code:
void __noreturn b()
{
longjmp(jmp_env, 1);
}

canonical_decompiled_code:
void __noreturn func8()
{
longjmp(jmp_env, 1);
}

original_code:
void b (void) {
  longjmp(jmp_env, 1);
}

canonical_original_code:
void b() {
  longjmp(jmp_env, 1);
}


repo: timocramer/gbasm

decompiled_code:
const char *__fastcall command_closing(unsigned __int64 a1)
{
const char *result; // rax

if ( a1 == 234 )
return "], a";
if ( a1 > 0xEA )
{
result = "]";
if ( a1 != 240 && a1 != 250 )
result = "%s: please give a filename\n" + 27;
}
else
{
result = "], sp";
if ( a1 != 8 )
{
result = "%s: please give a filename\n" + 27;
if ( a1 == 224 )
result = "], a";
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func7(unsigned __int64 a1)
{
const char *result; // rax

if ( a1 == 234 )
return "], a";
if ( a1 > 0xEA )
{
result = "]";
if ( a1 != 240 && a1 != 250 )
result = "%s: please give a filename\n" + 27;
}
else
{
result = "], sp";
if ( a1 != 8 )
{
result = "%s: please give a filename\n" + 27;
if ( a1 == 224 )
result = "], a";
}
}
return result;
}

original_code:
static const char* command_closing(size_t i) {
 switch(i) {
 case 0x08: return "], sp";
 case 0xe0: case 0xea: return "], a";
 case 0xf0: case 0xfa: return "]";
 }
 return "";
}

canonical_original_code:
static char *command_closing(unsigned long i) {
 switch(i) {
 case 0x08: return "], sp";
 case 0xe0: case 0xea: return "], a";
 case 0xf0: case 0xfa: return "]";
 }
 return "";
}

variable_types:
i
unsigned long

return_type: char *


decompiled_code:
__int64 __fastcall read_u16l(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int16 *a1)
{
return *a1;
}

original_code:
static uint16_t read_u16l(unsigned char *x) {
 return ((uint16_t)x[1] << 8) | x[0];
}

canonical_original_code:
static unsigned short read_u16l(unsigned char *x) {
 return ((unsigned short)x[1] << 8) | x[0];
}

variable_types:
x
unsigned char *

return_type: unsigned short


decompiled_code:
__int64 __fastcall byte_arguments(unsigned __int8 a1)
{
void *v1; // r8
__int64 result; // rax
__m128i v3; // [rsp+0h] [rbp-48h] BYREF
char v4; // [rsp+10h] [rbp-38h]
__m128i s; // [rsp+20h] [rbp-28h] BYREF
__int64 v6; // [rsp+30h] [rbp-18h]
char v7; // [rsp+38h] [rbp-10h]

v4 = -6;
v3 = _mm_load_si128((const __m128i *)&xmmword_403920);
v6 = 0xF8F6F0EEE8E6E0DELL;
v7 = -2;
s = _mm_load_si128((const __m128i *)&xmmword_403930);
v1 = memchr(&v3, a1, 0x11uLL);
result = 2LL;
if ( !v1 )
result = memchr(&s, a1, 0x19uLL) != 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
void *v1; // r8
__int64 result; // rax
__m128i v3; // [rsp+0h] [rbp-48h] BYREF
char v4; // [rsp+10h] [rbp-38h]
__m128i s; // [rsp+20h] [rbp-28h] BYREF
__int64 v6; // [rsp+30h] [rbp-18h]
char v7; // [rsp+38h] [rbp-10h]

v4 = -6;
v3 = _mm_load_si128((const __m128i *)&xmmword_403920);
v6 = 0xF8F6F0EEE8E6E0DELL;
v7 = -2;
s = _mm_load_si128((const __m128i *)&xmmword_403930);
v1 = memchr(&v3, a1, 0x11uLL);
result = 2LL;
if ( !v1 )
result = memchr(&s, a1, 0x19uLL) != 0LL;
return result;
}

original_code:
static unsigned int byte_arguments(unsigned char opcode) {
 const char two_byte[] = {0x01, 0x08, 0x11, 0x21, 0x31, 0xc2, 0xc3, 0xc4,
 0xca, 0xcc, 0xcd, 0xd2, 0xd4, 0xda, 0xdc, 0xea, 0xfa};
 const char one_byte[] = {0x06, 0x0e, 0x16, 0x18, 0x1e, 0x20, 0x26, 0x28,
 0x2e, 0x30, 0x36, 0x38, 0x3e, 0xc6, 0xce, 0xd6, 0xde, 0xe0, 0xe6, 0xe8,
 0xee, 0xf0, 0xf6, 0xf8, 0xfe};
 if(memchr(two_byte, opcode, sizeof(two_byte)) != ((void *)0))
  return 2;
 if(memchr(one_byte, opcode, sizeof(one_byte)) != ((void *)0))
  return 1;
 return 0;
}

canonical_original_code:
static unsigned int byte_arguments(unsigned char opcode) {
 char two_byte[] = {0x01, 0x08, 0x11, 0x21, 0x31, 0xc2, 0xc3, 0xc4,
 0xca, 0xcc, 0xcd, 0xd2, 0xd4, 0xda, 0xdc, 0xea, 0xfa};
 char one_byte[] = {0x06, 0x0e, 0x16, 0x18, 0x1e, 0x20, 0x26, 0x28,
 0x2e, 0x30, 0x36, 0x38, 0x3e, 0xc6, 0xce, 0xd6, 0xde, 0xe0, 0xe6, 0xe8,
 0xee, 0xf0, 0xf6, 0xf8, 0xfe};
 if(memchr(two_byte, opcode, sizeof(two_byte)) != ((void *)0))
  return 2;
 if(memchr(one_byte, opcode, sizeof(one_byte)) != ((void *)0))
  return 1;
 return 0;
}

variable_types:
opcode
unsigned char

return_type: unsigned int


disassemble

decompiled_code:
void __fastcall disassemble(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // r12
int v5; // eax
int v6; // er15
__int64 v7; // rdx
const char *v8; // rcx
int v9; // er12
const char *v10; // rcx
int v11; // er15
int v12; // er12
__int64 v13; // rdx
unsigned __int64 v14; // [rsp+0h] [rbp-48h]
unsigned __int8 *v15; // [rsp+0h] [rbp-48h]
unsigned __int16 *v16; // [rsp+8h] [rbp-40h]

if ( a2 )
{
v2 = 0LL;
while ( 1 )
{
v3 = (unsigned __int8 *)(a1 + v2);
printf("%06zx: ", v2);
v4 = *(unsigned __int8 *)(a1 + v2);
if ( (_BYTE)v4 == 0xCB )
break;
v5 = byte_arguments(v4);
if ( v5 == 1 )
{
if ( v2 + 1 >= a2 )
{
LABEL_20:
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v10 = command_closing(v4);
v11 = 0;
v15 = (unsigned __int8 *)(a1 + v2 + 1);
v12 = 20 - printf("%s0x%02x%s", &command_map[12 * v4], *v15, v10);
if ( v12 > 0 )
{
do
{
++v11;
putchar(32);
}
while ( v11 != v12 );
}
v2 += 2LL;
printf("; 0x%02x 0x%02x\n", *v3, *v15);
LABEL_5:
if ( v2 >= a2 )
return;
}
else
{
if ( v5 != 2 )
{
if ( !v5 )
{
++v2;
printf("%-20s; 0x%02x\n", &command_map[12 * v4], (unsigned __int8)v4);
}
goto LABEL_5;
}
v14 = v2 + 2;
if ( v2 + 2 >= a2 )
goto LABEL_20;
v6 = 0;
command_closing(v4);
v16 = (unsigned __int16 *)(a1 + v2 + 1);
v7 = (unsigned __int16)read_u16l(v16);
v9 = 20 - printf("%s0x%04x%s", &command_map[12 * v4], v7, v8);
if ( v9 > 0 )
{
do
{
++v6;
putchar(32);
}
while ( v6 != v9 );
}
v2 += 3LL;
printf("; 0x%02x 0x%02x 0x%02x\n", *v3, *(unsigned __int8 *)v16, *(unsigned __int8 *)(a1 + v14));
if ( v2 >= a2 )
return;
}
}
v13 = *(unsigned __int8 *)(a1 + v2 + 1);
v2 += 2LL;
printf("%-20s; 0xcb 0x%02x\n", &bit_command_map[12 * v13], v13);
goto LABEL_5;
}
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // r12
int v5; // eax
int v6; // er15
__int64 v7; // rdx
const char *v8; // rcx
int v9; // er12
const char *v10; // rcx
int v11; // er15
int v12; // er12
__int64 v13; // rdx
unsigned __int64 v14; // [rsp+0h] [rbp-48h]
unsigned __int8 *v15; // [rsp+0h] [rbp-48h]
unsigned __int16 *v16; // [rsp+8h] [rbp-40h]

if ( a2 )
{
v2 = 0LL;
while ( 1 )
{
v3 = (unsigned __int8 *)(a1 + v2);
printf("%06zx: ", v2);
v4 = *(unsigned __int8 *)(a1 + v2);
if ( (_BYTE)v4 == 0xCB )
break;
v5 = func10(v4);
if ( v5 == 1 )
{
if ( v2 + 1 >= a2 )
{
LABEL_20:
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v10 = func7(v4);
v11 = 0;
v15 = (unsigned __int8 *)(a1 + v2 + 1);
v12 = 20 - printf("%s0x%02x%s", &command_map[12 * v4], *v15, v10);
if ( v12 > 0 )
{
do
{
++v11;
putchar(32);
}
while ( v11 != v12 );
}
v2 += 2LL;
printf("; 0x%02x 0x%02x\n", *v3, *v15);
LABEL_5:
if ( v2 >= a2 )
return;
}
else
{
if ( v5 != 2 )
{
if ( !v5 )
{
++v2;
printf("%-20s; 0x%02x\n", &command_map[12 * v4], (unsigned __int8)v4);
}
goto LABEL_5;
}
v14 = v2 + 2;
if ( v2 + 2 >= a2 )
goto LABEL_20;
v6 = 0;
func7(v4);
v16 = (unsigned __int16 *)(a1 + v2 + 1);
v7 = (unsigned __int16)func8(v16);
v9 = 20 - printf("%s0x%04x%s", &command_map[12 * v4], v7, v8);
if ( v9 > 0 )
{
do
{
++v6;
putchar(32);
}
while ( v6 != v9 );
}
v2 += 3LL;
printf("; 0x%02x 0x%02x 0x%02x\n", *v3, *(unsigned __int8 *)v16, *(unsigned __int8 *)(a1 + v14));
if ( v2 >= a2 )
return;
}
}
v13 = *(unsigned __int8 *)(a1 + v2 + 1);
v2 += 2LL;
printf("%-20s; 0xcb 0x%02x\n", &bit_command_map[12 * v13], v13);
goto LABEL_5;
}
}

original_code:
static void disassemble(unsigned char *data, size_t size) {
 size_t i = 0;
 int j, written;
 while(i < size) {
  printf("%06zx: ", i);
  if(data[i] == 0xcb) {
   ++i;
   printf("%-20s; 0xcb 0x%02x\n", bit_command_map[data[i]], data[i]);
   ++i;
   continue;
  }
  switch(byte_arguments(data[i])) {
  case 0:
   printf("%-20s; 0x%02x\n", command_map[data[i]], data[i]);
   ++i;
   break;
  case 1:
   if(i + 1 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%02x%s", command_map[data[i]], data[i+1], command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x\n", data[i], data[i+1]);
   i += 2;
   break;
  case 2:
   if(i + 2 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%04x%s", command_map[data[i]], read_u16l(data + i + 1), command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x 0x%02x\n", data[i], data[i+1], data[i+2]);
   i += 3;
  }
 }
}

canonical_original_code:
static void disassemble(unsigned char *data, unsigned long size) {
 unsigned long i = 0;
 int j; int written;
 while(i < size) {
  printf("%06zx: ", i);
  if(data[i] == 0xcb) {
   ++i;
   printf("%-20s; 0xcb 0x%02x\n", bit_command_map[data[i]], data[i]);
   ++i;
   continue;
  }
  switch(byte_arguments(data[i])) {
  case 0:
   printf("%-20s; 0x%02x\n", command_map[data[i]], data[i]);
   ++i;
   break;
  case 1:
   if(i + 1 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%02x%s", command_map[data[i]], data[i+1], command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x\n", data[i], data[i+1]);
   i += 2;
   break;
  case 2:
   if(i + 2 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%04x%s", command_map[data[i]], read_u16l(data + i + 1), command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x 0x%02x\n", data[i], data[i+1], data[i+2]);
   i += 3;
  }
 }
}

variable_types:
data
unsigned char *

variable_types:
size
unsigned long

variable_types: j int

variable_types: written int


decompiled_code:
void __noreturn gbasm_error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func12(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: error: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void gbasm_error(const char *message, ...) {
 va_list args;
 fprintf(stderr, "%s: error: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

canonical_original_code:
void gbasm_error(char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s: error: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

variable_types:
message
char *


decompiled_code:
void __noreturn no_memory()
{
gbasm_error("not enough memory!");
}

canonical_decompiled_code:
void __noreturn func13()
{
func12("not enough memory!");
}

original_code:
void no_memory(void) {
 gbasm_error("not enough memory!");
}

canonical_original_code:
void no_memory() {
 gbasm_error("not enough memory!");
}


decompiled_code:
int gbasm_warning(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: warning: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func14(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: warning: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

original_code:
void gbasm_warning(const char *message, ...) {
 va_list args;
 fprintf(stderr, "%s: warning: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

canonical_original_code:
void gbasm_warning(char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s: warning: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}


decompiled_code:
void __noreturn location_error(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: error: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func15(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: error: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void location_error(YYLTYPE loc, char *message, ...) {
 va_list args;
 fprintf(stderr, "%s:%d:%d: error: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

canonical_original_code:
void location_error(struct YYLTYPE loc, char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s:%d:%d: error: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

variable_types: struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; }


decompiled_code:
int location_warning(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: warning: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func16(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: warning: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
return fputc(10, stderr);
}

original_code:
void location_warning(YYLTYPE loc, char *message, ...) {
 va_list args;
 fprintf(stderr, "%s:%d:%d: warning: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

canonical_original_code:
void location_warning(struct YYLTYPE loc, char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s:%d:%d: warning: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

user_defined_types: struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
void *ptr; // [rsp+0h] [rbp-18h]

v3 = *argv;
gbasm_filename = (__int64)*argv;
if ( argc <= 1 )
{
fprintf(stderr, "%s: please give a filename\n", v3);
result = 1;
}
else
{
input_filename = (__int64)argv[1];
read_file(input_filename);
((void (__fastcall *)())disassemble)();
free(ptr);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
void *ptr; // [rsp+0h] [rbp-18h]

v3 = *argv;
gbasm_filename = (__int64)*argv;
if ( argc <= 1 )
{
fprintf(stderr, "%s: please give a filename\n", v3);
result = 1;
}
else
{
input_filename = (__int64)argv[1];
func9(input_filename);
((void (__fastcall *)())disassemble)();
free(ptr);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv) {
 unsigned char *data;
 size_t size;
 gbasm_filename = argv[0];
 if(argc < 2) {
  fprintf(stderr, "%s: please give a filename\n", gbasm_filename);
  return 1;
 }
 input_filename = argv[1];
 read_file(input_filename, &size, &data);
 disassemble(data, size);
 free(data);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 unsigned char *data;
 unsigned long size;
 gbasm_filename = argv[0];
 if(argc < 2) {
  fprintf(stderr, "%s: please give a filename\n", gbasm_filename);
  return 1;
 }
 input_filename = argv[1];
 read_file(input_filename, &size, &data);
 disassemble(data, size);
 free(data);
 return 0;
}

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall chr2int(unsigned __int8 a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)a1 - 48;
v1 = a1 & 0xDF;
result = (unsigned int)v1 - 55;
if ( (unsigned __int8)(v1 - 65) >= 0x1Au )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)a1 - 48;
v1 = a1 & 0xDF;
result = (unsigned int)v1 - 55;
if ( (unsigned __int8)(v1 - 65) >= 0x1Au )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static unsigned int chr2int(unsigned char x) {
 if(x >= '0' && x <= '9')
  return x - '0';
 x &= ~0x20;
 if(x >= 'A' && x <= 'Z')
  return x - 'A' + 10;
 return (0x7fffffff * 2U + 1U);
}

variable_types:
x
unsigned char


decompiled_code:
__int64 scan_uint()
{
unsigned __int8 *v0; // rcx
int i; // esi
__int64 result; // rax
unsigned int v3; // er8

v0 = (unsigned __int8 *)src;
for ( i = 0; ; i = result + v3 * i )
{
result = chr2int(*v0);
if ( (unsigned int)result >= v3 )
break;
++yylloc[3];
src = (char *)v0;
}
yylval = i;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned __int8 *v0; // rcx
int i; // esi
__int64 result; // rax
unsigned int v3; // er8

v0 = (unsigned __int8 *)src;
for ( i = 0; ; i = result + v3 * i )
{
result = func0(*v0);
if ( (unsigned int)result >= v3 )
break;
++yylloc[3];
src = (char *)v0;
}
yylval = i;
return result;
}

original_code:
static void scan_uint(unsigned int base) {
 unsigned int i = 0;
 unsigned int c;
 while(1) {
  c = chr2int(*src);
  if(c >= base)
   break;
  i *= base;
  i += c;
  ++src;
  ++yylloc.last_column;
 }
 yylval.integer = i;
}

variable_types:
base
unsigned int

variable_types:
i
unsigned int

variable_types:
c
unsigned int


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

switch ( (char)a1 )
{
case 'a':
result = 7LL;
break;
case 'b':
result = 8LL;
break;
case 'e':
result = 27LL;
break;
case 'f':
result = 12LL;
break;
case 'n':
result = 10LL;
break;
case 'r':
result = 13LL;
break;
case 't':
result = 9LL;
break;
case 'v':
result = 11LL;
break;
default:
result = a1;
break;
}
return result;
}

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

switch ( (char)a1 )
{
case 'a':
result = 7LL;
break;
case 'b':
result = 8LL;
break;
case 'e':
result = 27LL;
break;
case 'f':
result = 12LL;
break;
case 'n':
result = 10LL;
break;
case 'r':
result = 13LL;
break;
case 't':
result = 9LL;
break;
case 'v':
result = 11LL;
break;
default:
result = a1;
break;
}
return result;
}

original_code:
static char slash_char(const char c) {
 switch(c) {
 case 'a': return '\a';
 case 'b': return '\b';
 case 'e': return '\e';
 case 'f': return '\f';
 case 'n': return '\n';
 case 'r': return '\r';
 case 't': return '\t';
 case 'v': return '\v';
 default: return c;
 }
}

canonical_original_code:
static char slash_char(char c) {
 switch(c) {
 case 'a': return '\a';
 case 'b': return '\b';
 case 'e': return '\e';
 case 'f': return '\f';
 case 'n': return '\n';
 case 'r': return '\r';
 case 't': return '\t';
 case 'v': return '\v';
 default: return c;
 }
}

variable_types: c char

return_type: char


decompiled_code:
__int64 scan_multibyte_operator()
{
const char *v0; // r12
size_t v1; // rbp
__int64 v2; // rbx
const char *v3; // r13
__int64 result; // rax

v0 = "&&";
v1 = 2LL;
v2 = 0LL;
v3 = src;
while ( strncmp(v3, v0, v1) )
{
if ( ++v2 == 8 )
return 0LL;
v0 = multibyte_operators[v2];
v1 = strlen(v0);
}
yylloc[3] += v1;
result = (unsigned int)v0;
src = (char *)&v3[v1];
return result;
}

canonical_decompiled_code:
__int64 func3()
{
const char *v0; // r12
size_t v1; // rbp
__int64 v2; // rbx
const char *v3; // r13
__int64 result; // rax

v0 = "&&";
v1 = 2LL;
v2 = 0LL;
v3 = src;
while ( strncmp(v3, v0, v1) )
{
if ( ++v2 == 8 )
return 0LL;
v0 = multibyte_operators[v2];
v1 = strlen(v0);
}
yylloc[3] += v1;
result = (unsigned int)v0;
src = (char *)&v3[v1];
return result;
}

original_code:
static int scan_multibyte_operator(void) {
 size_t i;
 size_t len;
 for(i = 0; i < sizeof(multibyte_operators) / sizeof(multibyte_operators[0]); ++i) {
  len = strlen(multibyte_operators[i]);
  if(strncmp(src, multibyte_operators[i], len) == 0) {
   src += len;
   yylloc.last_column += len;
   return (intptr_t)(multibyte_operators[i]);
  }
 }
 return 0;
}

canonical_original_code:
static int scan_multibyte_operator() {
 unsigned long i;
 unsigned long len;
 for(i = 0; i < sizeof(multibyte_operators) / sizeof(multibyte_operators[0]); ++i) {
  len = strlen(multibyte_operators[i]);
  if(strncmp(src, multibyte_operators[i], len) == 0) {
   src += len;
   yylloc.last_column += len;
   return (long)(multibyte_operators[i]);
  }
 }
 return 0;
}

variable_types:
len
unsigned long


decompiled_code:
char *scan_char()
{
char *v0; // rax
char v1; // dl
char v2; // cl
int v3; // esi
int v4; // ecx
char *result; // rax
char v6; // dl
char *v7; // rcx

v0 = src;
v1 = *src;
if ( !*src )
goto LABEL_11;
v2 = src[1];
if ( !v2 )
goto LABEL_11;
if ( v1 == 92 )
{
++yylloc[3];
src = v0 + 1;
v6 = v0[2];
if ( v6 )
{
if ( v6 == 39 )
{
v1 = slash_char(v0[1]);
v0 = v7;
goto LABEL_5;
}
LABEL_12:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant is too long");
return (char *)scan_int();
}
LABEL_11:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant does not end");
goto LABEL_12;
}
if ( v2 != 39 )
goto LABEL_12;
LABEL_5:
v3 = yylloc[3];
yylval = v1;
v4 = v3 + 2;
if ( v1 == 10 )
{
++yylloc[2];
v4 = 2;
}
result = v0 + 2;
yylloc[3] = v4;
src = result;
return result;
}

canonical_decompiled_code:
char *func4()
{
char *v0; // rax
char v1; // dl
char v2; // cl
int v3; // esi
int v4; // ecx
char *result; // rax
char v6; // dl
char *v7; // rcx

v0 = src;
v1 = *src;
if ( !*src )
goto LABEL_11;
v2 = src[1];
if ( !v2 )
goto LABEL_11;
if ( v1 == 92 )
{
++yylloc[3];
src = v0 + 1;
v6 = v0[2];
if ( v6 )
{
if ( v6 == 39 )
{
v1 = func2(v0[1]);
v0 = v7;
goto LABEL_5;
}
LABEL_12:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant is too long");
return (char *)func5();
}
LABEL_11:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant does not end");
goto LABEL_12;
}
if ( v2 != 39 )
goto LABEL_12;
LABEL_5:
v3 = yylloc[3];
yylval = v1;
v4 = v3 + 2;
if ( v1 == 10 )
{
++yylloc[2];
v4 = 2;
}
result = v0 + 2;
yylloc[3] = v4;
src = result;
return result;
}

original_code:
static void scan_char(void) {
 char new_char;
 if(*src == 0 || *(src + 1) == 0) {
  does_not_end:
  location_error(yylloc, "character constant does not end");
  return;
 }
 if(*src == '\\') {
  ++src;
  ++yylloc.last_column;
  if(*(src + 1) == 0)
   goto does_not_end;
  if(*(src + 1) != '\'')
   goto too_long;
  new_char = slash_char(*src);
 }
 else if(*(src + 1) == '\'')
  new_char = *src;
 else {
  too_long:
  location_error(yylloc, "character constant is too long");
  return;
 }
 yylval.integer = new_char;
 if(new_char == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
 }
 else
  ++yylloc.last_column;
 ++yylloc.last_column;
 src += 2;
}

canonical_original_code:
static void scan_char() {
 char new_char;
 if(*src == 0 || *(src + 1) == 0) {
  does_not_end:
  location_error(yylloc, "character constant does not end");
  return;
 }
 if(*src == '\\') {
  ++src;
  ++yylloc.last_column;
  if(*(src + 1) == 0)
   goto does_not_end;
  if(*(src + 1) != '\'')
   goto too_long;
  new_char = slash_char(*src);
 }
 else if(*(src + 1) == '\'')
  new_char = *src;
 else {
  too_long:
  location_error(yylloc, "character constant is too long");
  return;
 }
 yylval.integer = new_char;
 if(new_char == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
 }
 else
  ++yylloc.last_column;
 ++yylloc.last_column;
 src += 2;
}


decompiled_code:
__int64 scan_int()
{
char *v0; // rax
__int64 v1; // rbx
const unsigned __int16 *v2; // rdx
__int64 result; // rax
int v4; // edx
char v5; // cl
char *v6; // rax

v0 = src;
v1 = *src;
if ( (_BYTE)v1 == 39 )
{
v6 = src + 1;
++yylloc[3];
src = v6;
scan_char();
result = 258LL;
}
else if ( (_BYTE)v1 == 48 )
{
++src;
v4 = yylloc[3];
yylloc[3] = v4 + 1;
v5 = v0[1];
if ( v5 == 120 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
scan_uint(16LL);
result = 258LL;
}
else
{
if ( v5 == 98 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
scan_uint(2LL);
}
else
{
src = v0;
yylloc[3] = v4;
scan_uint(8LL);
}
result = 258LL;
}
}
else
{
v2 = *_ctype_b_loc();
result = 0LL;
if ( (v2[v1] & 0x800) != 0 )
{
scan_uint(10LL);
result = 258LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rax
__int64 v1; // rbx
const unsigned __int16 *v2; // rdx
__int64 result; // rax
int v4; // edx
char v5; // cl
char *v6; // rax

v0 = src;
v1 = *src;
if ( (_BYTE)v1 == 39 )
{
v6 = src + 1;
++yylloc[3];
src = v6;
func4();
result = 258LL;
}
else if ( (_BYTE)v1 == 48 )
{
++src;
v4 = yylloc[3];
yylloc[3] = v4 + 1;
v5 = v0[1];
if ( v5 == 120 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
func1(16LL);
result = 258LL;
}
else
{
if ( v5 == 98 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
func1(2LL);
}
else
{
src = v0;
yylloc[3] = v4;
func1(8LL);
}
result = 258LL;
}
}
else
{
v2 = *_ctype_b_loc();
result = 0LL;
if ( (v2[v1] & 0x800) != 0 )
{
func1(10LL);
result = 258LL;
}
}
return result;
}

original_code:
static int scan_int(void) {
 if(*src == '\'') {
  ++src;
  ++yylloc.last_column;
  scan_char();
 }
 else if(*src == '0') {
  ++src;
  ++yylloc.last_column;
  if(*src == 'x') {
   ++src;
   ++yylloc.last_column;
   scan_uint(16);
  }
  else if(*src == 'b') {
   ++src;
   ++yylloc.last_column;
   scan_uint(2);
  }
  else {
   --src;
   --yylloc.last_column;
   scan_uint(8);
  }
 }
 else if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISdigit))
  scan_uint(10);
 else
  return 0;
 return INTEGER;
}

canonical_original_code:
static int scan_int() {
 if(*src == '\'') {
  ++src;
  ++yylloc.last_column;
  scan_char();
 }
 else if(*src == '0') {
  ++src;
  ++yylloc.last_column;
  if(*src == 'x') {
   ++src;
   ++yylloc.last_column;
   scan_uint(16);
  }
  else if(*src == 'b') {
   ++src;
   ++yylloc.last_column;
   scan_uint(2);
  }
  else {
   --src;
   --yylloc.last_column;
   scan_uint(8);
  }
 }
 else if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISdigit))
  scan_uint(10);
 else
  return 0;
 return INTEGER;
}


decompiled_code:
__int64 scan_string()
{
char *v1; // rax
char *v2; // rdx
char v3; // al
unsigned int v4; // edi
char *v5; // rdx
__int64 v6; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

if ( *src != 34 )
return 0LL;
buffer_init(v7);
v1 = src;
++yylloc[3];
v2 = v1 + 1;
src = v1 + 1;
v3 = v1[1];
if ( v3 == 34 )
{
LABEL_15:
buffer_add_char(v7, 0LL);
v6 = v7[0];
++yylloc[3];
++src;
yylval = v6;
return 259LL;
}
while ( v3 )
{
if ( v3 == 92 )
{
++yylloc[3];
src = v2 + 1;
v4 = v2[1];
if ( !v2[1] )
break;
if ( (_BYTE)v4 != 10 )
{
v3 = slash_char(v4);
goto LABEL_12;
}
v2 += 2;
++yylloc[2];
yylloc[3] = 1;
src = v2;
v3 = *v2;
if ( *v2 == 34 )
goto LABEL_15;
}
else
{
LABEL_12:
buffer_add_char(v7, (unsigned int)v3);
v5 = src;
if ( *src == 10 )
{
++yylloc[2];
yylloc[3] = 1;
}
else
{
++yylloc[3];
}
v2 = v5 + 1;
src = v2;
v3 = *v2;
if ( *v2 == 34 )
goto LABEL_15;
}
}
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "String does not end");
return scan_identifier();
}

canonical_decompiled_code:
__int64 func6()
{
char *v1; // rax
char *v2; // rdx
char v3; // al
unsigned int v4; // edi
char *v5; // rdx
__int64 v6; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

if ( *src != 34 )
return 0LL;
buffer_init(v7);
v1 = src;
++yylloc[3];
v2 = v1 + 1;
src = v1 + 1;
v3 = v1[1];
if ( v3 == 34 )
{
LABEL_15:
buffer_add_char(v7, 0LL);
v6 = v7[0];
++yylloc[3];
++src;
yylval = v6;
return 259LL;
}
while ( v3 )
{
if ( v3 == 92 )
{
++yylloc[3];
src = v2 + 1;
v4 = v2[1];
if ( !v2[1] )
break;
if ( (_BYTE)v4 != 10 )
{
v3 = func2(v4);
goto LABEL_12;
}
v2 += 2;
++yylloc[2];
yylloc[3] = 1;
src = v2;
v3 = *v2;
if ( *v2 == 34 )
goto LABEL_15;
}
else
{
LABEL_12:
buffer_add_char(v7, (unsigned int)v3);
v5 = src;
if ( *src == 10 )
{
++yylloc[2];
yylloc[3] = 1;
}
else
{
++yylloc[3];
}
v2 = v5 + 1;
src = v2;
v3 = *v2;
if ( *v2 == 34 )
goto LABEL_15;
}
}
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "String does not end");
return func7();
}

original_code:
static int scan_string(void) {
 char new_char;
 struct buffer buf;
 if(*src != '"')
  return 0;
 buffer_init(&buf);
 ++yylloc.last_column;
 ++src;
 while(*src != '"') {
  if(*src == 0) {
   string_does_not_end:
   location_error(yylloc, "String does not end");
  }
  if(*src == '\\') {
   ++yylloc.last_column;
   ++src;
   if(*src == 0)
    goto string_does_not_end;
   if(*src == '\n') {
    ++yylloc.last_line;
    yylloc.last_column = 1;
    ++src;
    continue;
   }
   new_char = slash_char(*src);
  }
  else
   new_char = *src;
  buffer_add_char(&buf, new_char);
  if(*src == '\n') {
   ++yylloc.last_line;
   yylloc.last_column = 1;
  }
  else
   ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.string = buf.data;
 ++yylloc.last_column;
 ++src;
 return STRING;
}

canonical_original_code:
static int scan_string() {
 char new_char;
 struct buffer buf;
 if(*src != '"')
  return 0;
 buffer_init(&buf);
 ++yylloc.last_column;
 ++src;
 while(*src != '"') {
  if(*src == 0) {
   string_does_not_end:
   location_error(yylloc, "String does not end");
  }
  if(*src == '\\') {
   ++yylloc.last_column;
   ++src;
   if(*src == 0)
    goto string_does_not_end;
   if(*src == '\n') {
    ++yylloc.last_line;
    yylloc.last_column = 1;
    ++src;
    continue;
   }
   new_char = slash_char(*src);
  }
  else
   new_char = *src;
  buffer_add_char(&buf, new_char);
  if(*src == '\n') {
   ++yylloc.last_line;
   yylloc.last_column = 1;
  }
  else
   ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.string = buf.data;
 ++yylloc.last_column;
 ++src;
 return STRING;
}

variable_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; }


decompiled_code:
__int64 scan_identifier()
{
const unsigned __int16 **v0; // rbx
__int64 v1; // rcx
char *v3; // rax
__int64 v4; // rsi
char *v5; // rax
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

v0 = _ctype_b_loc();
v1 = *src;
if ( ((*v0)[v1] & 0x400) == 0 && (_BYTE)v1 != 95 )
return 0LL;
buffer_init(v6);
buffer_add_char(v6, (unsigned int)*src);
v3 = src;
++yylloc[3];
src = v3 + 1;
v4 = v3[1];
if ( ((*v0)[v4] & 8) == 0 )
goto LABEL_6;
do
{
do
{
buffer_add_char(v6, v4);
v5 = src;
++yylloc[3];
src = v5 + 1;
v4 = (unsigned int)v5[1];
}
while ( ((*v0)[v5[1]] & 8) != 0 );
LABEL_6:
;
}
while ( (_BYTE)v4 == 95 );
buffer_add_char(v6, 0LL);
yylval = v6[0];
return 260LL;
}

canonical_decompiled_code:
__int64 func7()
{
const unsigned __int16 **v0; // rbx
__int64 v1; // rcx
char *v3; // rax
__int64 v4; // rsi
char *v5; // rax
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

v0 = _ctype_b_loc();
v1 = *src;
if ( ((*v0)[v1] & 0x400) == 0 && (_BYTE)v1 != 95 )
return 0LL;
buffer_init(v6);
buffer_add_char(v6, (unsigned int)*src);
v3 = src;
++yylloc[3];
src = v3 + 1;
v4 = v3[1];
if ( ((*v0)[v4] & 8) == 0 )
goto LABEL_6;
do
{
do
{
buffer_add_char(v6, v4);
v5 = src;
++yylloc[3];
src = v5 + 1;
v4 = (unsigned int)v5[1];
}
while ( ((*v0)[v5[1]] & 8) != 0 );
LABEL_6:
;
}
while ( (_BYTE)v4 == 95 );
buffer_add_char(v6, 0LL);
yylval = v6[0];
return 260LL;
}

original_code:
static int scan_identifier(void) {
 struct buffer buf;
 if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalpha) || *src == '_') {
  buffer_init(&buf);
  buffer_add_char(&buf, *src);
 }
 else
  return 0;
 ++yylloc.last_column;
 ++src;
 while((((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalnum) || (*src) == '_')) {
  buffer_add_char(&buf, *src);
  ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.identifier = buf.data;
 return IDENTIFIER;
}

canonical_original_code:
static int scan_identifier() {
 struct buffer buf;
 if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalpha) || *src == '_') {
  buffer_init(&buf);
  buffer_add_char(&buf, *src);
 }
 else
  return 0;
 ++yylloc.last_column;
 ++src;
 while((((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalnum) || (*src) == '_')) {
  buffer_add_char(&buf, *src);
  ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.identifier = buf.data;
 return IDENTIFIER;
}

user_defined_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; }


decompiled_code:
__int64 scan_special_identifier()
{
const unsigned __int16 **v0; // rax
const char *v1; // r13
const unsigned __int16 *v2; // r14
__int64 v3; // rdx
const char *v4; // rbp
__int64 v5; // r12
size_t v6; // rbx
__int64 v7; // rcx
unsigned int v9; // er8

v0 = _ctype_b_loc();
v1 = src;
v2 = *v0;
v3 = *src;
if ( ((*v0)[v3] & 8) == 0 && (_BYTE)v3 != 95 )
return 0LL;
v4 = "A";
v5 = 0LL;
while ( 1 )
{
v6 = strlen(v4);
if ( !strncasecmp(v1, v4, v6) )
{
v7 = v1[v6];
if ( (v2[v7] & 8) == 0 && (_BYTE)v7 != 95 )
break;
}
++v5;
v4 += 12;
if ( v5 == 72 )
return 0LL;
}
yylloc[3] += v6;
v9 = dword_848[3 * v5];
src = (char *)&v1[v6];
return v9;
}

canonical_decompiled_code:
__int64 func8()
{
const unsigned __int16 **v0; // rax
const char *v1; // r13
const unsigned __int16 *v2; // r14
__int64 v3; // rdx
const char *v4; // rbp
__int64 v5; // r12
size_t v6; // rbx
__int64 v7; // rcx
unsigned int v9; // er8

v0 = _ctype_b_loc();
v1 = src;
v2 = *v0;
v3 = *src;
if ( ((*v0)[v3] & 8) == 0 && (_BYTE)v3 != 95 )
return 0LL;
v4 = "A";
v5 = 0LL;
while ( 1 )
{
v6 = strlen(v4);
if ( !strncasecmp(v1, v4, v6) )
{
v7 = v1[v6];
if ( (v2[v7] & 8) == 0 && (_BYTE)v7 != 95 )
break;
}
++v5;
v4 += 12;
if ( v5 == 72 )
return 0LL;
}
yylloc[3] += v6;
v9 = dword_848[3 * v5];
src = (char *)&v1[v6];
return v9;
}

original_code:
static int scan_special_identifier(void) {
 size_t i;
 size_t len;
 if(!(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalnum) || (*src) == '_'))
  return 0;
 for(i = 0; i < sizeof(tokentable) / sizeof(tokentable[0]); ++i) {
  len = strlen(tokentable[i].op);
  if(strncasecmp(src, tokentable[i].op, len) == 0 && !(((*__ctype_b_loc ())[(int) ((src[len]))] & (unsigned short int) _ISalnum) || (src[len]) == '_')) {
   src += len;
   yylloc.last_column += len;
   return tokentable[i].tok;
  }
 }
 return 0;
}

canonical_original_code:
static int scan_special_identifier() {
 unsigned long i;
 unsigned long len;
 if(!(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalnum) || (*src) == '_'))
  return 0;
 for(i = 0; i < sizeof(tokentable) / sizeof(tokentable[0]); ++i) {
  len = strlen(tokentable[i].op);
  if(strncasecmp(src, tokentable[i].op, len) == 0 && !(((*__ctype_b_loc ())[(int) ((src[len]))] & (unsigned short) _ISalnum) || (src[len]) == '_')) {
   src += len;
   yylloc.last_column += len;
   return tokentable[i].tok;
  }
 }
 return 0;
}

variable_types:
i
unsigned long


yylex

decompiled_code:
__int64 yylex()
{
int v0; // esi
char *i; // rax
char v2; // dl
int v3; // ecx
int v4; // edx
int v5; // ecx
int v6; // edx
int v7; // eax
__int64 result; // rax
char *v9; // rax

v0 = yylloc[2];
for ( i = src; ; src = i )
{
v2 = *i;
if ( *i == 32 || v2 == 9 )
{
v3 = yylloc[3] + 1 - (_DWORD)i;
do
{
do
{
v4 = v3 + (_DWORD)i++;
yylloc[3] = v4;
src = i;
v2 = *i;
}
while ( *i == 32 );
}
while ( v2 == 9 );
}
if ( v2 != 59 )
break;
v5 = yylloc[3] + 1 - (_DWORD)i;
while ( 1 )
{
v6 = v5 + (_DWORD)i++;
yylloc[3] = v6;
src = i;
v2 = *i;
if ( *i == 10 )
break;
if ( !v2 )
goto LABEL_8;
}
LABEL_9:
++v0;
++i;
yylloc[3] = 1;
yylloc[2] = v0;
}
LABEL_8:
if ( v2 == 10 )
goto LABEL_9;
v7 = yylloc[3];
yylloc[0] = v0;
yylloc[1] = v7;
result = scan_multibyte_operator();
if ( !(_DWORD)result )
{
result = scan_int();
if ( !(_DWORD)result )
{
result = scan_string();
if ( !(_DWORD)result )
{
result = scan_special_identifier();
if ( !(_DWORD)result )
{
result = scan_identifier();
if ( !(_DWORD)result )
{
v9 = src;
++yylloc[3];
src = v9 + 1;
result = (unsigned int)*v9;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // esi
char *i; // rax
char v2; // dl
int v3; // ecx
int v4; // edx
int v5; // ecx
int v6; // edx
int v7; // eax
__int64 result; // rax
char *v9; // rax

v0 = yylloc[2];
for ( i = src; ; src = i )
{
v2 = *i;
if ( *i == 32 || v2 == 9 )
{
v3 = yylloc[3] + 1 - (_DWORD)i;
do
{
do
{
v4 = v3 + (_DWORD)i++;
yylloc[3] = v4;
src = i;
v2 = *i;
}
while ( *i == 32 );
}
while ( v2 == 9 );
}
if ( v2 != 59 )
break;
v5 = yylloc[3] + 1 - (_DWORD)i;
while ( 1 )
{
v6 = v5 + (_DWORD)i++;
yylloc[3] = v6;
src = i;
v2 = *i;
if ( *i == 10 )
break;
if ( !v2 )
goto LABEL_8;
}
LABEL_9:
++v0;
++i;
yylloc[3] = 1;
yylloc[2] = v0;
}
LABEL_8:
if ( v2 == 10 )
goto LABEL_9;
v7 = yylloc[3];
yylloc[0] = v0;
yylloc[1] = v7;
result = func3();
if ( !(_DWORD)result )
{
result = func5();
if ( !(_DWORD)result )
{
result = func6();
if ( !(_DWORD)result )
{
result = func8();
if ( !(_DWORD)result )
{
result = func7();
if ( !(_DWORD)result )
{
v9 = src;
++yylloc[3];
src = v9 + 1;
result = (unsigned int)*v9;
}
}
}
}
}
return result;
}

original_code:
int yylex(void) {
 int status;
 skip_white_space:
 while(*src == ' ' || *src == '\t') {
  ++yylloc.last_column;
  ++src;
 }
 if(*src == ';') {
  while(*src != '\n' && *src != 0) {
   ++yylloc.last_column;
   ++src;
  }
 }
 if(*src == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
  ++src;
  goto skip_white_space;
 }
 yylloc.first_line = yylloc.last_line;
 yylloc.first_column = yylloc.last_column;
 status = scan_multibyte_operator();
 if(status != 0)
  return status;
 status = scan_int();
 if(status != 0)
  return status;
 status = scan_string();
 if(status != 0)
  return status;
 status = scan_special_identifier();
 if(status != 0)
  return status;
 status = scan_identifier();
 if(status != 0)
  return status;
 ++yylloc.last_column;
 return *src++;
}

canonical_original_code:
int yylex() {
 int status;
 skip_white_space:
 while(*src == ' ' || *src == '\t') {
  ++yylloc.last_column;
  ++src;
 }
 if(*src == ';') {
  while(*src != '\n' && *src != 0) {
   ++yylloc.last_column;
   ++src;
  }
 }
 if(*src == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
  ++src;
  goto skip_white_space;
 }
 yylloc.first_line = yylloc.last_line;
 yylloc.first_column = yylloc.last_column;
 status = scan_multibyte_operator();
 if(status != 0)
  return status;
 status = scan_int();
 if(status != 0)
  return status;
 status = scan_string();
 if(status != 0)
  return status;
 status = scan_special_identifier();
 if(status != 0)
  return status;
 status = scan_identifier();
 if(status != 0)
  return status;
 ++yylloc.last_column;
 return *src++;
}

variable_types: status int


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

result = realloc(*(void **)a1, a2);
if ( result )
{
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)a1 = result;
}
else
{
no_memory();
result = (void *)buffer_init();
}
return result;
}

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

result = realloc(*(void **)a1, a2);
if ( result )
{
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)a1 = result;
}
else
{
no_memory();
result = (void *)func1();
}
return result;
}

original_code:
static void buffer_resize(struct buffer *b, size_t size) {
 char *temp;
 temp = realloc(b->data, size);
 if(temp == ((void *)0))
  no_memory();
 b->data = temp;
 b->alloc_size = size;
}

canonical_original_code:
static void buffer_resize(struct buffer *b, unsigned long size) {
 char *temp;
 temp = realloc(b->data, size);
 if(temp == ((void *)0))
  no_memory();
 b->data = temp;
 b->alloc_size = size;
}

variable_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; } *

variable_types:
temp
char *


original_code:
void buffer_add_char(struct buffer *b, char c) {
 size_t to_alloc;
 if(b == ((void *)0))
  return;
 if(b->write_pos >= b->alloc_size) {
  to_alloc = 128 * (b->write_pos / 128 + 1);
  buffer_resize(b, to_alloc);
 }
 if(b->write_pos > b->size)
  memset(b->data + b->size, 0, b->write_pos - b->size);
 b->data[b->write_pos] = c;
 b->write_pos++;
 if(b->write_pos > b->size)
  b->size = b->write_pos;
}

canonical_original_code:
void buffer_add_char(struct buffer *b, char c) {
 unsigned long to_alloc;
 if(b == ((void *)0))
  return;
 if(b->write_pos >= b->alloc_size) {
  to_alloc = 128 * (b->write_pos / 128 + 1);
  buffer_resize(b, to_alloc);
 }
 if(b->write_pos > b->size)
  memset(b->data + b->size, 0, b->write_pos - b->size);
 b->data[b->write_pos] = c;
 b->write_pos++;
 if(b->write_pos > b->size)
  b->size = b->write_pos;
}


decompiled_code:
void __fastcall buffer_add_str(_QWORD *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
buffer_add_mem(a1, a2, v2);
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
func3(a1, a2, v2);
}

original_code:
void buffer_add_str(struct buffer *b, const char *s) {
 buffer_add_mem(b, s, strlen(s));
}

canonical_original_code:
void buffer_add_str(struct buffer *b, char *s) {
 buffer_add_mem(b, s, strlen(s));
}

variable_types:
s
char *


decompiled_code:
void __fastcall buffer_add_u16l(_QWORD *a1, __int16 a2)
{
buffer_add_char(a1, a2);
buffer_add_char(a1, SHIBYTE(a2));
}

canonical_decompiled_code:
void __fastcall func5(_QWORD *a1, __int16 a2)
{
func2(a1, a2);
func2(a1, SHIBYTE(a2));
}

original_code:
void buffer_add_u16l(struct buffer *b, uint16_t i) {
 buffer_add_char(b, i & 0xff);
 buffer_add_char(b, i >> 8);
}

canonical_original_code:
void buffer_add_u16l(struct buffer *b, unsigned short i) {
 buffer_add_char(b, i & 0xff);
 buffer_add_char(b, i >> 8);
}

variable_types:
i
unsigned short


help

decompiled_code:
int help()
{
return fprintf(
stderr,
"Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
"Options:\n"
" -n gamename set the name that is written into the metadata (max. 15 byte, default: THE GAME)\n"
" -o output-file set the name of the file where the binary will be written to (default: a.gb)\n"
" -m don't write metadata automatically\n"
"\n"
"Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n",
(const char *)gbasm_filename,
(const char *)gbasm_filename);
}

canonical_decompiled_code:
int func0()
{
return fprintf(
stderr,
"Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
"Options:\n"
" -n gamename set the name that is written into the metadata (max. 15 byte, default: THE GAME)\n"
" -o output-file set the name of the file where the binary will be written to (default: a.gb)\n"
" -m don't write metadata automatically\n"
"\n"
"Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n",
(const char *)gbasm_filename,
(const char *)gbasm_filename);
}

original_code:
static void help(void) {
 fprintf(stderr, "Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
 "Options:\n"
 "  -n gamename     set the name that is written into the metadata (max. 15 byte, default: " "THE GAME" ")\n"
 "  -o output-file  set the name of the file where the binary will be written to (default: " "a.gb" ")\n"
 "  -m              don't write metadata automatically\n"
 "\n"
 "Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n", gbasm_filename, gbasm_filename);
}

canonical_original_code:
static void help() {
 fprintf(stderr, "Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
 "Options:\n"
 "  -n gamename     set the name that is written into the metadata (max. 15 byte, default: " "THE GAME" ")\n"
 "  -o output-file  set the name of the file where the binary will be written to (default: " "a.gb" ")\n"
 "  -m              don't write metadata automatically\n"
 "\n"
 "Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n", gbasm_filename, gbasm_filename);
}


decompiled_code:
__int64 __fastcall get_rom_size_code(unsigned __int64 a1)
{
unsigned __int64 v1; // rax
unsigned int v2; // er12

if ( a1 <= 0x8000 )
return 0;
v1 = 0x8000LL;
v2 = 0;
do
{
v1 *= 2LL;
++v2;
}
while ( a1 > v1 );
if ( (char)v2 > 6 )
gbasm_warning("The assembled binary is most certainly too large");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int64 a1)
{
unsigned __int64 v1; // rax
unsigned int v2; // er12

if ( a1 <= 0x8000 )
return 0;
v1 = 0x8000LL;
v2 = 0;
do
{
v1 *= 2LL;
++v2;
}
while ( a1 > v1 );
if ( (char)v2 > 6 )
gbasm_warning("The assembled binary is most certainly too large");
return v2;
}

original_code:
static char get_rom_size_code(size_t size) {
 char x = 0;
 size_t s = 32 * 1024;
 while(size > s) {
  s <<= 1;
  ++x;
 }
 if(x > 6)
  gbasm_warning("The assembled binary is most certainly too large");
 return x;
}

canonical_original_code:
static char get_rom_size_code(unsigned long size) {
 char x = 0;
 unsigned long s = 32 * 1024;
 while(size > s) {
  s <<= 1;
  ++x;
 }
 if(x > 6)
  gbasm_warning("The assembled binary is most certainly too large");
 return x;
}

variable_types:
size
unsigned long

variable_types: x char

variable_types:
s
unsigned long


decompiled_code:
unsigned __int64 warn_if_overwrite()
{
unsigned __int64 result; // rax

result = 256LL;
while ( (unsigned __int64)*(&binary + 1) > result )
{
if ( *((_BYTE *)binary + result) )
return gbasm_warning("There is data that is overwritten by metadata");
if ( ++result == 336 )
return result;
}
return result;
}

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

result = 256LL;
while ( (unsigned __int64)*(&binary + 1) > result )
{
if ( *((_BYTE *)binary + result) )
return gbasm_warning("There is data that is overwritten by metadata");
if ( ++result == 336 )
return result;
}
return result;
}

original_code:
static void warn_if_overwrite(void) {
 size_t i;
 for(i = 0x100; i < 0x150 && i < binary.size; ++i) {
  if(binary.data[i] != 0) {
   gbasm_warning("There is data that is overwritten by metadata");
   return;
  }
 }
}

canonical_original_code:
static void warn_if_overwrite() {
 unsigned long i;
 for(i = 0x100; i < 0x150 && i < binary.size; ++i) {
  if(binary.data[i] != 0) {
   gbasm_warning("There is data that is overwritten by metadata");
   return;
  }
 }
}


decompiled_code:
__int64 __fastcall write_metadata(char *src)
{
__int8 v1; // al
char v2; // dl
__int128 *v3; // rax
__int128 v5[3]; // [rsp+0h] [rbp-58h] BYREF
char dest[8]; // [rsp+30h] [rbp-28h] BYREF
__int64 v7; // [rsp+38h] [rbp-20h]
__m128i v8; // [rsp+40h] [rbp-18h] BYREF

*(_QWORD *)dest = 1043577275LL;
v5[0] = (__int128)_mm_load_si128((const __m128i *)&LC7);
v7 = 0LL;
v5[1] = (__int128)_mm_load_si128((const __m128i *)&LC8);
v5[2] = (__int128)_mm_load_si128((const __m128i *)&LC9);
v8 = _mm_load_si128((const __m128i *)&LC10);
if ( strlen(src) > 0xF )
gbasm_warning("Game name is too long and will be truncated");
strncpy(&dest[4], src, 0xFuLL);
v1 = get_rom_size_code((unsigned __int64)*(&binary + 1));
v2 = 25;
v8.m128i_i8[8] = v1;
v3 = (__int128 *)&dest[4];
do
{
v2 += *(_BYTE *)v3;
v3 = (__int128 *)((char *)v3 + 1);
}
while ( v3 != (__int128 *)((char *)&v8.m128i_u64[1] + 5) );
v8.m128i_i8[13] = -v2;
warn_if_overwrite();
*(&binary + 3) = &loc_100;
return buffer_add_mem(&binary, v5, 80LL);
}

original_code:
static void write_metadata(const char *game_name) {
 unsigned char complement;
 size_t i;
 char m[0x50] = {
  0,
  0xc3,
  0x50, 0x01,
0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,0x03,0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,
0x00,0x08,0x11,0x1F,0x88,0x89,0x00,0x0E,0xDC,0xCC,0x6E,0xE6,0xDD,0xDD,0xD9,0x99,
0xBB,0xBB,0x67,0x63,0x6E,0x0E,0xEC,0xCC,0xDD,0xDC,0x99,0x9F,0xBB,0xB9,0x33,0x3E,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0,
  0, 0,
  0,
  0,
  0,
  0,
  1,
  0,
  0,
  0,
  0xC0,0x1A
 };
 if(strlen(game_name) > 15)
  gbasm_warning("Game name is too long and will be truncated");
 strncpy(m + (4 + 48), game_name, 15);
 m[0x48] = get_rom_size_code(binary.size);
 complement = 25;
 for(i = 0x34; i < 0x4d; ++i)
  complement += m[i];
 m[0x4d] = -complement;
 warn_if_overwrite();
 binary.write_pos = 0x100;
 buffer_add_mem(&binary, m, 0x50);
}

canonical_original_code:
static void write_metadata(char *game_name) {
 unsigned char complement;
 unsigned long i;
 char m[80] = {
  0,
  0xc3,
  0x50, 0x01,
0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,0x03,0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,
0x00,0x08,0x11,0x1F,0x88,0x89,0x00,0x0E,0xDC,0xCC,0x6E,0xE6,0xDD,0xDD,0xD9,0x99,
0xBB,0xBB,0x67,0x63,0x6E,0x0E,0xEC,0xCC,0xDD,0xDC,0x99,0x9F,0xBB,0xB9,0x33,0x3E,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0,
  0, 0,
  0,
  0,
  0,
  0,
  1,
  0,
  0,
  0,
  0xC0,0x1A
 };
 if(strlen(game_name) > 15)
  gbasm_warning("Game name is too long and will be truncated");
 strncpy(m + (4 + 48), game_name, 15);
 m[0x48] = get_rom_size_code(binary.size);
 complement = 25;
 for(i = 0x34; i < 0x4d; ++i)
  complement += m[i];
 m[0x4d] = -complement;
 warn_if_overwrite();
 binary.write_pos = 0x100;
 buffer_add_mem(&binary, m, 0x50);
}

variable_types:
complement
unsigned char

variable_types:
m
char [80]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // r13
char *v5; // rbp
int v6; // eax
void *v8; // r12

v3 = 1;
v4 = "THE GAME";
v5 = "a.gb";
gbasm_filename = (__int64)*argv;
while ( 1 )
{
v6 = _posix_getopt((unsigned int)argc, argv, "ho:n:m");
if ( v6 == -1 )
break;
if ( v6 == 110 )
{
v4 = optarg;
}
else if ( v6 > 110 )
{
if ( v6 == 111 )
v5 = optarg;
}
else
{
if ( v6 == 104 )
{
help();
return 1;
}
if ( v6 == 109 )
v3 = 0;
}
}
if ( optind >= argc )
{
gbasm_error("no input files");
JUMPOUT(0x737LL);
}
input_filename = (__int64)argv[optind];
v8 = (void *)read_file((const char *)input_filename);
src = (__int64)v8;
buffer_init(&binary);
pass = 1;
yyparse();
src = (__int64)v8;
yylloc[0] = 0x100000001LL;
yylloc[1] = 0x100000001LL;
*(&binary + 3) = 0LL;
pass = 2;
yyparse();
if ( v3 )
write_metadata(v4);
write_binary_to_file(v5);
free(v8);
variables_destroy();
free(binary);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // r13
char *v5; // rbp
int v6; // eax
void *v8; // r12

v3 = 1;
v4 = "THE GAME";
v5 = "a.gb";
gbasm_filename = (__int64)*argv;
while ( 1 )
{
v6 = _posix_getopt((unsigned int)argc, argv, "ho:n:m");
if ( v6 == -1 )
break;
if ( v6 == 110 )
{
v4 = optarg;
}
else if ( v6 > 110 )
{
if ( v6 == 111 )
v5 = optarg;
}
else
{
if ( v6 == 104 )
{
func0();
return 1;
}
if ( v6 == 109 )
v3 = 0;
}
}
if ( optind >= argc )
{
gbasm_error("no input files");
JUMPOUT(0x737LL);
}
input_filename = (__int64)argv[optind];
v8 = (void *)func1((const char *)input_filename);
src = (__int64)v8;
buffer_init(&binary);
pass = 1;
yyparse();
src = (__int64)v8;
yylloc[0] = 0x100000001LL;
yylloc[1] = 0x100000001LL;
*(&binary + 3) = 0LL;
pass = 2;
yyparse();
if ( v3 )
func4(v4);
func5(v5);
free(v8);
variables_destroy();
free(binary);
return 0;
}

original_code:
int main(int argc, char **argv) {
 int c;
 const char *output_name = "a.gb";
 const char *game_name = "THE GAME";
 int automatic_metadata = 1;
 char *srcbase;
 gbasm_filename = argv[0];
 while((c = getopt(argc, argv, "ho:n:m")) != -1) {
  switch(c) {
  case 'h':
   help();
   return 1;
  case 'o':
   output_name = optarg;
   break;
  case 'n':
   game_name = optarg;
   break;
  case 'm':
   automatic_metadata = 0;
   break;
  }
 }
 if(optind >= argc) {
  gbasm_error("no input files");
  return 1;
 }
 input_filename = argv[optind];
 src = read_file(input_filename);
 srcbase = src;
 buffer_init(&binary);
 pass = 1;
 yyparse();
 src = srcbase;
 yylloc.first_column = 1;
 yylloc.last_column = 1;
 yylloc.first_line = 1;
 yylloc.last_line = 1;
 binary.write_pos = 0;
 pass = 2;
 yyparse();
 if(automatic_metadata)
  write_metadata(game_name);
 write_binary_to_file(output_name);
 free(srcbase);
 variables_destroy();
 free(binary.data);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 int c;
 char *output_name = "a.gb";
 char *game_name = "THE GAME";
 int automatic_metadata = 1;
 char *srcbase;
 gbasm_filename = argv[0];
 while((c = getopt(argc, argv, "ho:n:m")) != -1) {
  switch(c) {
  case 'h':
   help();
   return 1;
  case 'o':
   output_name = optarg;
   break;
  case 'n':
   game_name = optarg;
   break;
  case 'm':
   automatic_metadata = 0;
   break;
  }
 }
 if(optind >= argc) {
  gbasm_error("no input files");
  return 1;
 }
 input_filename = argv[optind];
 src = read_file(input_filename);
 srcbase = src;
 buffer_init(&binary);
 pass = 1;
 yyparse();
 src = srcbase;
 yylloc.first_column = 1;
 yylloc.last_column = 1;
 yylloc.first_line = 1;
 yylloc.last_line = 1;
 binary.write_pos = 0;
 pass = 2;
 yyparse();
 if(automatic_metadata)
  write_metadata(game_name);
 write_binary_to_file(output_name);
 free(srcbase);
 variables_destroy();
 free(binary.data);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

variable_types:
srcbase
char *


decompiled_code:
void resize_if_necessary()
{
__int64 v0; // rsi
void *v1; // rax

v0 = vars_alloc;
if ( vars_size >= (unsigned __int64)vars_alloc )
{
vars_alloc += 64LL;
v1 = realloc(vars, 16 * (v0 + 64));
if ( v1 )
{
vars = v1;
}
else
{
no_memory();
variables_destroy();
}
}
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rsi
void *v1; // rax

v0 = vars_alloc;
if ( vars_size >= (unsigned __int64)vars_alloc )
{
vars_alloc += 64LL;
v1 = realloc(vars, 16 * (v0 + 64));
if ( v1 )
{
vars = v1;
}
else
{
no_memory();
func1();
}
}
}

original_code:
static void resize_if_necessary(void) {
 struct variable *temp;
 if(vars_size >= vars_alloc) {
  vars_alloc += 64;
  temp = realloc(vars, vars_alloc * sizeof(struct variable));
  if(temp == ((void *)0))
   no_memory();
  vars = temp;
 }
}

canonical_original_code:
static void resize_if_necessary() {
 struct variable *temp;
 if(vars_size >= vars_alloc) {
  vars_alloc += 64;
  temp = realloc(vars, vars_alloc * sizeof(struct variable));
  if(temp == ((void *)0))
   no_memory();
  vars = temp;
 }
}

variable_types: struct variable { char *name; unsigned int value; } *


decompiled_code:
void variables_destroy()
{
void *v0; // r12
void **v1; // rbx
void **v2; // rbp
void *v3; // rdi

v0 = vars;
v1 = (void **)vars;
v2 = (void **)((char *)vars + 16 * vars_size);
if ( vars_size )
{
do
{
v3 = *v1;
v1 += 2;
free(v3);
}
while ( v1 != v2 );
}
free(v0);
vars = 0LL;
vars_alloc = 0LL;
vars_size = 0LL;
}

canonical_decompiled_code:
void func1()
{
void *v0; // r12
void **v1; // rbx
void **v2; // rbp
void *v3; // rdi

v0 = vars;
v1 = (void **)vars;
v2 = (void **)((char *)vars + 16 * vars_size);
if ( vars_size )
{
do
{
v3 = *v1;
v1 += 2;
free(v3);
}
while ( v1 != v2 );
}
free(v0);
vars = 0LL;
vars_alloc = 0LL;
vars_size = 0LL;
}

original_code:
void variables_destroy(void) {
 size_t i;
 for(i = 0; i < vars_size; ++i)
  free(vars[i].name);
 free(vars);
 vars = ((void *)0);
 vars_alloc = 0;
 vars_size = 0;
}

canonical_original_code:
void variables_destroy() {
 unsigned long i;
 for(i = 0; i < vars_size; ++i)
  free(vars[i].name);
 free(vars);
 vars = ((void *)0);
 vars_alloc = 0;
 vars_size = 0;
}


decompiled_code:
const char **__fastcall load_int(char *s1)
{
__int64 v1; // r13
const char **v2; // rbx
__int64 v3; // rbp

v1 = vars_size;
if ( !vars_size )
return 0LL;
v2 = (const char **)vars;
v3 = 0LL;
while ( strcmp(s1, *v2) )
{
++v3;
v2 += 2;
if ( v3 == v1 )
return 0LL;
}
return v2 + 1;
}

canonical_decompiled_code:
const char **__fastcall func2(char *s1)
{
__int64 v1; // r13
const char **v2; // rbx
__int64 v3; // rbp

v1 = vars_size;
if ( !vars_size )
return 0LL;
v2 = (const char **)vars;
v3 = 0LL;
while ( strcmp(s1, *v2) )
{
++v3;
v2 += 2;
if ( v3 == v1 )
return 0LL;
}
return v2 + 1;
}

original_code:
unsigned int* load_int(const char *name) {
 size_t i;
 for(i = 0; i < vars_size; ++i)
  if(strcmp(name, vars[i].name) == 0)
   return &(vars[i].value);
 return ((void *)0);
}

canonical_original_code:
unsigned int *load_int(char *name) {
 unsigned long i;
 for(i = 0; i < vars_size; ++i)
  if(strcmp(name, vars[i].name) == 0)
   return &(vars[i].value);
 return ((void *)0);
}

variable_types:
name
char *

return_type: unsigned int *


decompiled_code:
__int64 __fastcall store_int(char *s, int a2)
{
__int64 v2; // rbp
const char **v3; // r12
__int64 v4; // rbx
__int64 result; // rax
char *v6; // rdx
char *v7; // rax

v2 = vars_size;
if ( vars_size )
{
v3 = (const char **)vars;
v4 = 0LL;
while ( strcmp(s, v3[2 * v4]) )
{
if ( ++v4 == v2 )
goto LABEL_6;
}
result = 1LL;
}
else
{
LABEL_6:
resize_if_necessary();
v6 = strdup(s);
if ( v6 )
{
v7 = (char *)vars + 16 * v2;
vars_size = v2 + 1;
*((_DWORD *)v7 + 2) = a2;
*(_QWORD *)v7 = v6;
result = 0LL;
}
else
{
no_memory();
result = store_label();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s, int a2)
{
__int64 v2; // rbp
const char **v3; // r12
__int64 v4; // rbx
__int64 result; // rax
char *v6; // rdx
char *v7; // rax

v2 = vars_size;
if ( vars_size )
{
v3 = (const char **)vars;
v4 = 0LL;
while ( strcmp(s, v3[2 * v4]) )
{
if ( ++v4 == v2 )
goto LABEL_6;
}
result = 1LL;
}
else
{
LABEL_6:
func0();
v6 = strdup(s);
if ( v6 )
{
v7 = (char *)vars + 16 * v2;
vars_size = v2 + 1;
*((_DWORD *)v7 + 2) = a2;
*(_QWORD *)v7 = v6;
result = 0LL;
}
else
{
no_memory();
result = func4();
}
}
return result;
}

original_code:
int store_int(const char *name, unsigned int value) {
 size_t i;
 char *newname;
 for(i = 0; i < vars_size; ++i)
  if(strcmp(name, vars[i].name) == 0)
   return 1;
 resize_if_necessary();
 newname = strdup(name);
 if(newname == ((void *)0))
  no_memory();
 vars[vars_size].value = value;
 vars[vars_size].name = newname;
 ++vars_size;
 return 0;
}

canonical_original_code:
int store_int(char *name, unsigned int value) {
 unsigned long i;
 char *newname;
 for(i = 0; i < vars_size; ++i)
  if(strcmp(name, vars[i].name) == 0)
   return 1;
 resize_if_necessary();
 newname = strdup(name);
 if(newname == ((void *)0))
  no_memory();
 vars[vars_size].value = value;
 vars[vars_size].name = newname;
 ++vars_size;
 return 0;
}

variable_types:
value
unsigned int

variable_types:
i
unsigned long

variable_types:
newname
char *


decompiled_code:
__int64 __fastcall store_label(char *a1, unsigned int a2)
{
if ( a2 > 0x3FFF )
a2 = (a2 & 0x3FFF) + 0x4000;
return store_int(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, unsigned int a2)
{
if ( a2 > 0x3FFF )
a2 = (a2 & 0x3FFF) + 0x4000;
return func3(a1, a2);
}

original_code:
int store_label(const char *name, unsigned int address) {
 unsigned int real_address;
 if(address < 0x4000)
  real_address = address;
 else
  real_address = (address % 0x4000) + 0x4000;
 return store_int(name, real_address);
}

canonical_original_code:
int store_label(char *name, unsigned int address) {
 unsigned int real_address;
 if(address < 0x4000)
  real_address = address;
 else
  real_address = (address % 0x4000) + 0x4000;
 return store_int(name, real_address);
}

variable_types:
address
unsigned int


repo: timocramer/thinklight-fun

decompiled_code:
char *__fastcall morse_code(char a1)
{
unsigned __int8 v1; // di

if ( (unsigned __int8)(a1 - 97) <= 0x19u )
{
v1 = (a1 & 0xDF) - 39;
return CSWTCH_9[v1];
}
v1 = a1 - 39;
if ( v1 <= 0x38u )
return CSWTCH_9[v1];
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func7(char a1)
{
unsigned __int8 v1; // di

if ( (unsigned __int8)(a1 - 97) <= 0x19u )
{
v1 = (a1 & 0xDF) - 39;
return CSWTCH_9[v1];
}
v1 = a1 - 39;
if ( v1 <= 0x38u )
return CSWTCH_9[v1];
return 0LL;
}

original_code:
static const char* morse_code(char x) {
 if(x >= 'a' && x <= 'z')
  x &= ~0x20;
 switch(x) {
 case '\'': return ".----.";
 case '(': return "-.--.";
 case ')': return "-.--.-";
 case '+': return ".-.-.";
 case ',': return "--..--";
 case '-': return "-....-";
 case '.': return ".-.-.-";
 case '/': return "-..-.";
 case '0': return "-----";
 case '1': return ".----";
 case '2': return "..---";
 case '3': return "...--";
 case '4': return "....-";
 case '5': return ".....";
 case '6': return "-....";
 case '7': return "--...";
 case '8': return "---..";
 case '9': return "----.";
 case ':': return "---...";
 case ';': return "-.-.-.";
 case '=': return "-...-";
 case '?': return "..--..";
 case '@': return ".--.-.";
 case 'A': return ".-";
 case 'B': return "-...";
 case 'C': return "-.-.";
 case 'D': return "-..";
 case 'E': return ".";
 case 'F': return "..-.";
 case 'G': return "--.";
 case 'H': return "....";
 case 'I': return "..";
 case 'J': return ".---";
 case 'K': return "-.-";
 case 'L': return ".-..";
 case 'M': return "--";
 case 'N': return "-.";
 case 'O': return "---";
 case 'P': return ".--.";
 case 'Q': return "--.-";
 case 'R': return ".-.";
 case 'S': return "...";
 case 'T': return "-";
 case 'U': return "..-";
 case 'V': return "...-";
 case 'W': return ".--";
 case 'X': return "-..-";
 case 'Y': return "-.--";
 case 'Z': return "--..";
 case '_': return "..--.-";
 }
 return ((void *)0);
}

canonical_original_code:
static char *morse_code(char x) {
 if(x >= 'a' && x <= 'z')
  x &= ~0x20;
 switch(x) {
 case '\'': return ".----.";
 case '(': return "-.--.";
 case ')': return "-.--.-";
 case '+': return ".-.-.";
 case ',': return "--..--";
 case '-': return "-....-";
 case '.': return ".-.-.-";
 case '/': return "-..-.";
 case '0': return "-----";
 case '1': return ".----";
 case '2': return "..---";
 case '3': return "...--";
 case '4': return "....-";
 case '5': return ".....";
 case '6': return "-....";
 case '7': return "--...";
 case '8': return "---..";
 case '9': return "----.";
 case ':': return "---...";
 case ';': return "-.-.-.";
 case '=': return "-...-";
 case '?': return "..--..";
 case '@': return ".--.-.";
 case 'A': return ".-";
 case 'B': return "-...";
 case 'C': return "-.-.";
 case 'D': return "-..";
 case 'E': return ".";
 case 'F': return "..-.";
 case 'G': return "--.";
 case 'H': return "....";
 case 'I': return "..";
 case 'J': return ".---";
 case 'K': return "-.-";
 case 'L': return ".-..";
 case 'M': return "--";
 case 'N': return "-.";
 case 'O': return "---";
 case 'P': return ".--.";
 case 'Q': return "--.-";
 case 'R': return ".-.";
 case 'S': return "...";
 case 'T': return "-";
 case 'U': return "..-";
 case 'V': return "...-";
 case 'W': return ".--";
 case 'X': return "-..-";
 case 'Y': return "-.--";
 case 'Z': return "--..";
 case '_': return "..--.-";
 }
 return ((void *)0);
}


lock

decompiled_code:
__int64 lock()
{
int v0; // edi
__int64 result; // rax

v0 = open("/tmp/thinklight.lock", 193, 420LL);
result = 1LL;
if ( v0 >= 0 )
{
close(v0);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // edi
__int64 result; // rax

v0 = open("/tmp/thinklight.lock", 193, 420LL);
result = 1LL;
if ( v0 >= 0 )
{
close(v0);
result = 0LL;
}
return result;
}

original_code:
static int lock(void) {
 int fd;
 fd = open("/tmp/thinklight.lock", 0100 | 0200 | 01, 0644);
 if(fd < 0)
  return 1;
 close(fd);
 return 0;
}

canonical_original_code:
static int lock() {
 int fd;
 fd = open("/tmp/thinklight.lock", 0100 | 0200 | 01, 0644);
 if(fd < 0)
  return 1;
 close(fd);
 return 0;
}

variable_types: fd int


decompiled_code:
size_t install_signal_handler()
{
size_t result; // rax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)restore_status_and_exit;
v1.sa_flags = 0;
sigemptyset(&v1.sa_mask);
if ( sigaction(2, &v1, 0LL) == -1 || (result = (unsigned int)(sigaction(15, &v1, 0LL) + 1), !(_DWORD)result) )
result = fwrite(
"Cannot install signal handler! The initial light-status will not be saved!\n",
1uLL,
0x4BuLL,
stderr);
return result;
}

canonical_decompiled_code:
size_t func9()
{
size_t result; // rax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)restore_status_and_exit;
v1.sa_flags = 0;
sigemptyset(&v1.sa_mask);
if ( sigaction(2, &v1, 0LL) == -1 || (result = (unsigned int)(sigaction(15, &v1, 0LL) + 1), !(_DWORD)result) )
result = fwrite(
"Cannot install signal handler! The initial light-status will not be saved!\n",
1uLL,
0x4BuLL,
stderr);
return result;
}

original_code:
static void install_signal_handler(void) {
 struct sigaction sa;
 sa.__sigaction_handler.sa_handler = restore_status_and_exit;
 sa.sa_flags = 0;
 sigemptyset(&sa.sa_mask);
 if((sigaction(2, &sa, ((void *)0)) == -1)
 || (sigaction(15, &sa, ((void *)0)) == -1))
  fputs("Cannot install signal handler! "
    "The initial light-status will not be saved!\n", stderr);
}

canonical_original_code:
static void install_signal_handler() {
 struct sigaction sa;
 sa.__sigaction_handler.sa_handler = restore_status_and_exit;
 sa.sa_flags = 0;
 sigemptyset(&sa.sa_mask);
 if((sigaction(2, &sa, ((void *)0)) == -1)
 || (sigaction(15, &sa, ((void *)0)) == -1))
  fputs("Cannot install signal handler! "
    "The initial light-status will not be saved!\n", stderr);
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
int __fastcall morse_char(char a1)
{
char *v1; // rax
char v2; // dl
char *i; // rbx

v1 = morse_code(a1);
if ( v1 )
{
for ( i = v1; *i; LODWORD(v1) = usleep(0x29810u) )
{
tl_toggle();
if ( *i == 46 )
usleep(0x29810u);
else
usleep(0x7C830u);
tl_toggle();
++i;
}
}
else if ( v2 == 32 )
{
LODWORD(v1) = usleep(0xA6040u);
}
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func10(char a1)
{
char *v1; // rax
char v2; // dl
char *i; // rbx

v1 = func7(a1);
if ( v1 )
{
for ( i = v1; *i; LODWORD(v1) = usleep(0x29810u) )
{
func19();
if ( *i == 46 )
usleep(0x29810u);
else
usleep(0x7C830u);
func19();
++i;
}
}
else if ( v2 == 32 )
{
LODWORD(v1) = usleep(0xA6040u);
}
return (int)v1;
}

original_code:
static void morse_char(char c) {
 const char *p = morse_code(c);
 if(p != ((void *)0)) {
  while(*p != 0) {
   tl_toggle();
   if(*p == '.')
    usleep(170000);
   else
    usleep((170000 * 3));
   tl_toggle();
   usleep(170000);
   ++p;
  }
 }
 else if(c == ' ')
  usleep((170000 * 4));
}

canonical_original_code:
static void morse_char(char c) {
 char *p = morse_code(c);
 if(p != ((void *)0)) {
  while(*p != 0) {
   tl_toggle();
   if(*p == '.')
    usleep(170000);
   else
    usleep((170000 * 3));
   tl_toggle();
   usleep(170000);
   ++p;
  }
 }
 else if(c == ' ')
  usleep((170000 * 4));
}

variable_types: c char

variable_types:
p
char *


decompiled_code:
int __fastcall morse_string(char *a1)
{
char *v1; // rbx
char v2; // di
int result; // eax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = morse_char(v2);
if ( !*++v1 )
break;
result = usleep(0x53020u);
v2 = *v1;
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(char *a1)
{
char *v1; // rbx
char v2; // di
int result; // eax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = func10(v2);
if ( !*++v1 )
break;
result = usleep(0x53020u);
v2 = *v1;
}
while ( *v1 );
}
return result;
}

original_code:
static void morse_string(char *str) {
 char *c = str;
 while(*c != 0) {
  morse_char(*c);
  ++c;
  if(*c != 0)
   usleep((170000 * 2));
 }
}

variable_types:
str
char *

variable_types:
c
char *


unlock

decompiled_code:
void unlock()
{
if ( unlink("/tmp/thinklight.lock") == -1 )
perror("unlink /tmp/thinklight.lock");
}

canonical_decompiled_code:
void func12()
{
if ( unlink("/tmp/thinklight.lock") == -1 )
perror("unlink /tmp/thinklight.lock");
}

original_code:
static void unlock(void) {
 if(unlink("/tmp/thinklight.lock") == -1)
  perror("unlink " "/tmp/thinklight.lock");
}

canonical_original_code:
static void unlock() {
 if(unlink("/tmp/thinklight.lock") == -1)
  perror("unlink " "/tmp/thinklight.lock");
}


decompiled_code:
void __noreturn restore_status_and_exit()
{
if ( initial_status == 1 )
tl_on();
else
tl_off();
tl_deinit();
unlock();
exit(0);
}

canonical_decompiled_code:
void __noreturn func13()
{
if ( initial_status == 1 )
func16();
else
func17();
func15();
func12();
exit(0);
}

original_code:
static void restore_status_and_exit(int s) {
 (void) s;
 if(initial_status == 1)
  tl_on();
 else
  tl_off();
 tl_deinit();
 unlock();
 exit(0);
}

variable_types: s int


decompiled_code:
__int64 tl_init()
{
light_fd = open("/sys/class/leds/tpacpi::thinklight/brightness", 2);
return (unsigned int)light_fd >> 31;
}

canonical_decompiled_code:
__int64 func14()
{
light_fd = open("/sys/class/leds/tpacpi::thinklight/brightness", 2);
return (unsigned int)light_fd >> 31;
}

original_code:
int tl_init(void) {
 light_fd = open("/sys/class/leds/tpacpi::thinklight/brightness", 02);
 if(light_fd < 0)
  return 1;
 return 0;
}

canonical_original_code:
int tl_init() {
 light_fd = open("/sys/class/leds/tpacpi::thinklight/brightness", 02);
 if(light_fd < 0)
  return 1;
 return 0;
}


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

if ( light_fd >= 0 )
{
result = close(light_fd);
light_fd = -1;
}
return result;
}

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

if ( light_fd >= 0 )
{
result = close(light_fd);
light_fd = -1;
}
return result;
}

original_code:
void tl_deinit(void) {
 if(light_fd < 0)
  return;
 close(light_fd);
 light_fd = -1;
}

canonical_original_code:
void tl_deinit() {
 if(light_fd < 0)
  return;
 close(light_fd);
 light_fd = -1;
}


decompiled_code:
ssize_t tl_on()
{
ssize_t result; // rax

if ( light_fd >= 0 )
{
lseek(light_fd, 0LL, 0);
result = write(light_fd, "255", 3uLL);
}
return result;
}

canonical_decompiled_code:
ssize_t func16()
{
ssize_t result; // rax

if ( light_fd >= 0 )
{
lseek(light_fd, 0LL, 0);
result = write(light_fd, "255", 3uLL);
}
return result;
}

original_code:
void tl_on(void) {
 if(light_fd < 0)
  return;
 lseek(light_fd, 0, 0);
 write(light_fd, "255", sizeof("255") - 1);
}

canonical_original_code:
void tl_on() {
 if(light_fd < 0)
  return;
 lseek(light_fd, 0, 0);
 write(light_fd, "255", sizeof("255") - 1);
}


decompiled_code:
ssize_t tl_off()
{
ssize_t result; // rax

if ( light_fd >= 0 )
{
lseek(light_fd, 0LL, 0);
result = write(light_fd, "0", 1uLL);
}
return result;
}

canonical_decompiled_code:
ssize_t func17()
{
ssize_t result; // rax

if ( light_fd >= 0 )
{
lseek(light_fd, 0LL, 0);
result = write(light_fd, "0", 1uLL);
}
return result;
}

original_code:
void tl_off(void) {
 if(light_fd < 0)
  return;
 lseek(light_fd, 0, 0);
 write(light_fd, "0", sizeof("0") - 1);
}

canonical_original_code:
void tl_off() {
 if(light_fd < 0)
  return;
 lseek(light_fd, 0, 0);
 write(light_fd, "0", sizeof("0") - 1);
}


decompiled_code:
__int64 tl_status()
{
__int64 result; // rax
char buf[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( light_fd < 0 )
return 0xFFFFFFFFLL;
lseek(light_fd, 0LL, 0);
if ( read(light_fd, buf, 2uLL) == 2 )
result = buf[0] != 48;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 func18()
{
__int64 result; // rax
char buf[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( light_fd < 0 )
return 0xFFFFFFFFLL;
lseek(light_fd, 0LL, 0);
if ( read(light_fd, buf, 2uLL) == 2 )
result = buf[0] != 48;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int tl_status(void) {
 char buf[sizeof("0")];
 if(light_fd < 0)
  return (-1);
 lseek(light_fd, 0, 0);
 if(read(light_fd, buf, sizeof(buf)) != sizeof(buf))
  return (-1);
 if(buf[0] == '0')
  return 0;
 else
  return 1;
}

canonical_original_code:
int tl_status() {
 char buf[-1];
 if(light_fd < 0)
  return (-1);
 lseek(light_fd, 0, 0);
 if(read(light_fd, buf, sizeof(buf)) != sizeof(buf))
  return (-1);
 if(buf[0] == '0')
  return 0;
 else
  return 1;
}

variable_types:
buf
char [-1]


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

result = tl_status();
if ( !(_DWORD)result )
return tl_on();
if ( (_DWORD)result == 1 )
result = tl_off();
return result;
}

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

result = func18();
if ( !(_DWORD)result )
return func16();
if ( (_DWORD)result == 1 )
result = func17();
return result;
}

original_code:
void tl_toggle(void) {
 int status = tl_status();
 if(status == 0)
  tl_on();
 else if(status == 1)
  tl_off();
}

canonical_original_code:
void tl_toggle() {
 int status = tl_status();
 if(status == 0)
  tl_on();
 else if(status == 1)
  tl_off();
}

variable_types: status int


main

repo: timofurrer/asciitable

variable_types: argc int

variable_types:
argv
char **

variable_types: x int

variable_types: y int

variable_types: rows int

variable_types: cols int


getBoundingRectangle

repo: timpeskett/nmuw

decompiled_code:
unsigned __int64 __fastcall getBoundingRectangle(unsigned __int64 *a1, unsigned int a2, _DWORD *a3, _DWORD *a4, _DWORD *a5, _DWORD *a6)
{
unsigned __int64 result; // rax
int v8; // edx
__int64 v9; // r10
int v10; // edx
int v11; // edx
int v12; // edx
int v13; // edx

result = a2;
*a6 = 0;
*a5 = 0;
*a4 = 0;
*a3 = 0;
if ( (int)result <= 0 )
return result;
v8 = 0;
v9 = (__int64)&a1[(unsigned int)(result - 1) + 1];
while ( 1 )
{
result = *a1;
if ( *a1 )
break;
LABEL_17:
if ( ++a1 == (unsigned __int64 *)v9 )
return result;
}
if ( v8 )
{
do
{
v10 = *(_DWORD *)(result + 20);
if ( *a3 > v10 )
*a3 = v10;
v11 = *(_DWORD *)(result + 28) + v10;
if ( v11 > *a5 )
*a5 = v11;
v12 = *(_DWORD *)(result + 24);
if ( v12 < *a4 )
*a4 = v12;
v13 = *(_DWORD *)(result + 32) + v12;
if ( v13 <= *a6 )
{
if ( ++a1 == (unsigned __int64 *)v9 )
return result;
}
else
{
++a1;
*a6 = v13;
if ( (unsigned __int64 *)v9 == a1 )
return result;
}
LABEL_6:
result = *a1;
}
while ( *a1 );
v8 = 1;
goto LABEL_17;
}
++a1;
*a3 = *(_DWORD *)(result + 20);
*a4 = *(_DWORD *)(result + 24);
*a5 = *(_DWORD *)(result + 20) + *(_DWORD *)(result + 28);
*a6 = *(_DWORD *)(result + 24) + *(_DWORD *)(result + 32);
if ( (unsigned __int64 *)v9 != a1 )
goto LABEL_6;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(unsigned __int64 *a1, unsigned int a2, _DWORD *a3, _DWORD *a4, _DWORD *a5, _DWORD *a6)
{
unsigned __int64 result; // rax
int v8; // edx
__int64 v9; // r10
int v10; // edx
int v11; // edx
int v12; // edx
int v13; // edx

result = a2;
*a6 = 0;
*a5 = 0;
*a4 = 0;
*a3 = 0;
if ( (int)result <= 0 )
return result;
v8 = 0;
v9 = (__int64)&a1[(unsigned int)(result - 1) + 1];
while ( 1 )
{
result = *a1;
if ( *a1 )
break;
LABEL_17:
if ( ++a1 == (unsigned __int64 *)v9 )
return result;
}
if ( v8 )
{
do
{
v10 = *(_DWORD *)(result + 20);
if ( *a3 > v10 )
*a3 = v10;
v11 = *(_DWORD *)(result + 28) + v10;
if ( v11 > *a5 )
*a5 = v11;
v12 = *(_DWORD *)(result + 24);
if ( v12 < *a4 )
*a4 = v12;
v13 = *(_DWORD *)(result + 32) + v12;
if ( v13 <= *a6 )
{
if ( ++a1 == (unsigned __int64 *)v9 )
return result;
}
else
{
++a1;
*a6 = v13;
if ( (unsigned __int64 *)v9 == a1 )
return result;
}
LABEL_6:
result = *a1;
}
while ( *a1 );
v8 = 1;
goto LABEL_17;
}
++a1;
*a3 = *(_DWORD *)(result + 20);
*a4 = *(_DWORD *)(result + 24);
*a5 = *(_DWORD *)(result + 20) + *(_DWORD *)(result + 28);
*a6 = *(_DWORD *)(result + 24) + *(_DWORD *)(result + 32);
if ( (unsigned __int64 *)v9 != a1 )
goto LABEL_6;
return result;
}

original_code:
void getBoundingRectangle( Monitor *mon[], int monLen, int *left, int *top, int *right, int *bottom )
{
 int i;
 boolean set = FALSE;
 *left = *top = *right = *bottom = 0;
 for( i = 0; i < monLen; i++ )
 {
  if( mon[i] != ((void *)0) )
  {
   if( set == FALSE )
   {
    set = TRUE;
    *left = mon[i]->x;
    *top = mon[i]->y;
    *right = mon[i]->x + mon[i]->width;
    *bottom = mon[i]->y + mon[i]->height;
   }
   else
   {
    if( mon[i]->x < *left )
    {
     *left = mon[i]->x;
    }
    if( mon[i]->x + mon[i]->width > *right )
    {
     *right = mon[i]->x + mon[i]->width;
    }
    if( mon[i]->y < *top )
    {
     *top = mon[i]->y;
    }
    if( mon[i]->y + mon[i]->height > *bottom )
    {
     *bottom = mon[i]->y + mon[i]->height;
    }
   }
  }
 }
}

canonical_original_code:
void getBoundingRectangle(struct Monitor *mon[], int monLen, int *left, int *top, int *right, int *bottom)
{
 int i;
 enum boolean set = FALSE;
 *left = *top = *right = *bottom = 0;
 for( i = 0; i < monLen; i++ )
 {
  if( mon[i] != ((void *)0) )
  {
   if( set == FALSE )
   {
    set = TRUE;
    *left = mon[i]->x;
    *top = mon[i]->y;
    *right = mon[i]->x + mon[i]->width;
    *bottom = mon[i]->y + mon[i]->height;
   }
   else
   {
    if( mon[i]->x < *left )
    {
     *left = mon[i]->x;
    }
    if( mon[i]->x + mon[i]->width > *right )
    {
     *right = mon[i]->x + mon[i]->width;
    }
    if( mon[i]->y < *top )
    {
     *top = mon[i]->y;
    }
    if( mon[i]->y + mon[i]->height > *bottom )
    {
     *bottom = mon[i]->y + mon[i]->height;
    }
   }
  }
 }
}

variable_types: struct Monitor { int sockfd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *addr; int addr_size; int x; int width; struct { unsigned long wnd; char *wndName; } *procs; int numProcs; } *[]

variable_types: monLen int

variable_types:
left
int *

variable_types:
top
int *

variable_types:
right
int *

variable_types:
bottom
int *

variable_types: i int

variable_types:
set
enum boolean { FALSE=0, TRUE=1 }

user_defined_types: struct Monitor { int sockfd; struct sockaddr *addr; int addr_size; int x; int width; struct { unsigned long wnd; char *wndName; } *procs; int numProcs; }


isPointInMonitor

decompiled_code:
_BOOL8 __fastcall isPointInMonitor(_DWORD *a1, int a2, int a3)
{
int v3; // eax
_BOOL4 v4; // er8
int v5; // eax

v3 = a1[5];
v4 = 0;
if ( v3 <= a2 && a1[7] + v3 >= a2 )
{
v5 = a1[6];
if ( v5 <= a3 )
v4 = a1[8] + v5 >= a3;
}
return v4;
}

original_code:
boolean isPointInMonitor( Monitor *mon1, int x, int y )
{
 boolean inside = FALSE;
 if( ( mon1->x <= x && x <= mon1->x + mon1->width ) && ( mon1->y <= y && y <= mon1->y + mon1->height ) )
 {
  inside = TRUE;
 }
 return inside;
}

canonical_original_code:
enum boolean isPointInMonitor(struct Monitor *mon1, int x, int y)
{
 enum boolean inside = FALSE;
 if( ( mon1->x <= x && x <= mon1->x + mon1->width ) && ( mon1->y <= y && y <= mon1->y + mon1->height ) )
 {
  inside = TRUE;
 }
 return inside;
}

variable_types: struct Monitor { int sockfd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *addr; int addr_size; int x; int width; struct { unsigned long wnd; char *wndName; } *procs; int numProcs; } *

variable_types:
inside
enum boolean { FALSE=0, TRUE=1 }

return_type: enum boolean { FALSE=0, TRUE=1 }


getMonitorFromPoint

decompiled_code:
__int64 __fastcall getMonitorFromPoint(_DWORD **a1, int a2, int a3, int a4)
{
unsigned int v8; // er9
_BOOL4 v9; // eax
unsigned int v10; // edx
bool v11; // si
__int64 v12; // rcx

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
v8 = 0;
while ( *a1 )
{
v9 = isPointInMonitor(*a1, a3, a4);
v10 = v8 + 1;
v11 = !v9;
a1 = (_DWORD **)(v12 + 8);
if ( a2 <= (int)(v8 + 1) )
goto LABEL_8;
LABEL_4:
if ( !v11 )
goto LABEL_8;
v8 = v10;
}
v10 = v8 + 1;
v11 = 1;
v9 = 0;
++a1;
if ( a2 > (int)(v8 + 1) )
goto LABEL_4;
LABEL_8:
if ( !v9 )
v8 = -1;
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(_DWORD **a1, int a2, int a3, int a4)
{
unsigned int v8; // er9
_BOOL4 v9; // eax
unsigned int v10; // edx
bool v11; // si
__int64 v12; // rcx

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
v8 = 0;
while ( *a1 )
{
v9 = func4(*a1, a3, a4);
v10 = v8 + 1;
v11 = !v9;
a1 = (_DWORD **)(v12 + 8);
if ( a2 <= (int)(v8 + 1) )
goto LABEL_8;
LABEL_4:
if ( !v11 )
goto LABEL_8;
v8 = v10;
}
v10 = v8 + 1;
v11 = 1;
v9 = 0;
++a1;
if ( a2 > (int)(v8 + 1) )
goto LABEL_4;
LABEL_8:
if ( !v9 )
v8 = -1;
return v8;
}

original_code:
int getMonitorFromPoint( Monitor *mon[], int monLen, int x, int y )
{
 int i;
 boolean found = FALSE;
 i = 0;
 while( i < monLen && found == FALSE )
 {
  if( mon[i] != ((void *)0) )
  {
   found = isPointInMonitor( mon[i], x, y );
  }
  i++;
 }
 i--;
 if( found == FALSE )
 {
  i = -1;
 }
 return i;
}

canonical_original_code:
int getMonitorFromPoint(struct Monitor *mon[], int monLen, int x, int y)
{
 int i;
 enum boolean found = FALSE;
 i = 0;
 while( i < monLen && found == FALSE )
 {
  if( mon[i] != ((void *)0) )
  {
   found = isPointInMonitor( mon[i], x, y );
  }
  i++;
 }
 i--;
 if( found == FALSE )
 {
  i = -1;
 }
 return i;
}

variable_types:
found
enum boolean { FALSE=0, TRUE=1 }

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


doesOverlap

original_code:
boolean doesOverlap( Monitor *mon1, Monitor *mon2 )
{
 boolean overlap = FALSE;
 int l1, r1, t1, b1;
 int l2, r2, t2, b2;
 l1 = mon1->x; r1 = mon1->x + mon1->width;
 t1 = mon1->y; b1 = mon1->y + mon1->height;
 l2 = mon2->x; r2 = mon2->x + mon2->width;
 t2 = mon2->y; b2 = mon2->y + mon2->height;
 if( isPointInMonitor( mon1, l2, t2 ) || isPointInMonitor( mon1, r2, b2 ) || isPointInMonitor( mon1, r2, t2 ) || isPointInMonitor( mon1, l2, b2 ) )
 {
  overlap = TRUE;
 }
 else if( isPointInMonitor( mon2, l1, t1 ) || isPointInMonitor( mon2, r1, b1 ) || isPointInMonitor( mon2, r1, t1) || isPointInMonitor( mon2, r1, b1 ) )
 {
  overlap = TRUE;
 }
 else if( t1 < t2 && b1 > b2 && l1 > l2 && r1 < r2 )
 {
  overlap = TRUE;
 }
 else if( t2 < t1 && b2 > b1 && l2 > l1 && r2 < r1 )
 {
  overlap = TRUE;
 }
 return overlap;
}

canonical_original_code:
enum boolean doesOverlap(struct Monitor *mon1, struct Monitor *mon2)
{
 enum boolean overlap = FALSE;
 int l1; int r1; int t1; int b1;
 int l2; int r2; int t2; int b2;
 l1 = mon1->x; r1 = mon1->x + mon1->width;
 t1 = mon1->y; b1 = mon1->y + mon1->height;
 l2 = mon2->x; r2 = mon2->x + mon2->width;
 t2 = mon2->y; b2 = mon2->y + mon2->height;
 if( isPointInMonitor( mon1, l2, t2 ) || isPointInMonitor( mon1, r2, b2 ) || isPointInMonitor( mon1, r2, t2 ) || isPointInMonitor( mon1, l2, b2 ) )
 {
  overlap = TRUE;
 }
 else if( isPointInMonitor( mon2, l1, t1 ) || isPointInMonitor( mon2, r1, b1 ) || isPointInMonitor( mon2, r1, t1) || isPointInMonitor( mon2, r1, b1 ) )
 {
  overlap = TRUE;
 }
 else if( t1 < t2 && b1 > b2 && l1 > l2 && r1 < r2 )
 {
  overlap = TRUE;
 }
 else if( t2 < t1 && b2 > b1 && l2 > l1 && r2 < r1 )
 {
  overlap = TRUE;
 }
 return overlap;
}

variable_types:
overlap
enum boolean { FALSE=0, TRUE=1 }