decompiled_code:
__int64 rj_reader_start()
{
const char *v1; // rax

rj_reader_thread = SDL_CreateThread(rj_reader_handler, &rj_sock);
if ( rj_reader_thread )
return 0LL;
v1 = (const char *)SDL_GetError();
fprintf(stderr, "%s\n", v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
const char *v1; // rax

rj_reader_thread = SDL_CreateThread(rj_reader_handler, &rj_sock);
if ( rj_reader_thread )
return 0LL;
v1 = (const char *)SDL_GetError();
fprintf(stderr, "%s\n", v1);
return 0xFFFFFFFFLL;
}

original_code:
int rj_reader_start() {
 rj_reader_thread = SDL_CreateThread(rj_reader_handler, (void *) &rj_sock);
 if (rj_reader_thread == ((void *)0)) {
  fprintf(stderr, "%s\n", SDL_GetError());
  return -1;
 }
 return 0;
}


repo: luqmana/RemoteJoyPlus

decompiled_code:
__int64 __fastcall rj_send_event(int a1, int a2)
{
int v3; // edi
int v4; // er12
__int64 v5; // rbx
int v6; // eax
int v8[9]; // [rsp+0h] [rbp-24h] BYREF

v3 = rj_sock;
if ( rj_sock < 0 )
return 0xFFFFFFFFLL;
v4 = 0;
v5 = 0LL;
v8[0] = -1868903185;
v8[1] = a1;
v8[2] = a2;
while ( 1 )
{
v6 = write(v3, (char *)v8 + v5, 12 - v4);
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
{
perror("write");
LABEL_9:
fwrite("Could not write out data to socket.\n", 1uLL, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
LABEL_5:
v3 = rj_sock;
}
v4 += v6;
if ( v4 <= 11 )
{
v5 = v4;
goto LABEL_5;
}
if ( v4 != 12 )
goto LABEL_9;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
int v3; // edi
int v4; // er12
__int64 v5; // rbx
int v6; // eax
int v8[9]; // [rsp+0h] [rbp-24h] BYREF

v3 = rj_sock;
if ( rj_sock < 0 )
return 0xFFFFFFFFLL;
v4 = 0;
v5 = 0LL;
v8[0] = -1868903185;
v8[1] = a1;
v8[2] = a2;
while ( 1 )
{
v6 = write(v3, (char *)v8 + v5, 12 - v4);
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
{
perror("write");
LABEL_9:
fwrite("Could not write out data to socket.\n", 1uLL, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
LABEL_5:
v3 = rj_sock;
}
v4 += v6;
if ( v4 <= 11 )
{
v5 = v4;
goto LABEL_5;
}
if ( v4 != 12 )
goto LABEL_9;
return 0LL;
}

original_code:
int rj_send_event(int type, unsigned int value) {
 struct JoyEvent event;
 if (rj_sock < 0)
  return -1;
 event.magic = (0x909ACCEF);
 event.type = (type);
 event.value = (value);
 const void *buf = (void *) &event;
 int written = 0;
 int len = sizeof(event);
 while (written < len) {
  int ret = write(rj_sock, buf + written, len - written);
  if (ret < 0) {
   if ((*__errno_location ()) != 4) {
    perror("write");
    written = -1;
    break;
   }
  } else
   written += ret;
 }
 if (written != len) {
  fprintf(stderr, "Could not write out data to socket.\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int rj_send_event(int type, unsigned int value) {
 struct JoyEvent event;
 if (rj_sock < 0)
  return -1;
 event.magic = (0x909ACCEF);
 event.type = (type);
 event.value = (value);
 void *buf = (void *) &event;
 int written = 0;
 int len = sizeof(event);
 while (written < len) {
  int ret = write(rj_sock, buf + written, len - written);
  if (ret < 0) {
   if ((*__errno_location ()) != 4) {
    perror("write");
    written = -1;
    break;
   }
  } else
   written += ret;
 }
 if (written != len) {
  fprintf(stderr, "Could not write out data to socket.\n");
  return -1;
 }
 return 0;
}

variable_types: type int

variable_types:
value
unsigned int

variable_types: struct JoyEvent { unsigned int magic; int type; unsigned int value; }

variable_types:
buf
void *

variable_types: written int

variable_types: len int

variable_types: ret int

return_type: int


decompiled_code:
__int64 rj_interface_init()
{
struct hostent *v0; // rax
__int64 result; // rax
char optval[4]; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

rj_sock = socket(2, 1, 0);
if ( rj_sock < 0 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16_constprop_0("localhost");
v0 = gethostbyname("localhost");
if ( v0 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v0->h_addr_list;
if ( connect(rj_sock, &addr, 0x10u) < 0 )
{
perror("connect");
close(rj_sock);
result = 0xFFFFFFFFLL;
}
else
{
setsockopt(rj_sock, 6, 1, optval, 4u);
rj_send_event(5, g_context[261130] | 1);
result = 0LL;
}
}
else
{
fprintf(stderr, "Unknown host %s.\n", "localhost");
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
struct hostent *v0; // rax
__int64 result; // rax
char optval[4]; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

rj_sock = socket(2, 1, 0);
if ( rj_sock < 0 )
{
perror("socket");
result = 0xFFFFFFFFLL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func1("localhost");
v0 = gethostbyname("localhost");
if ( v0 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v0->h_addr_list;
if ( connect(rj_sock, &addr, 0x10u) < 0 )
{
perror("connect");
close(rj_sock);
result = 0xFFFFFFFFLL;
}
else
{
setsockopt(rj_sock, 6, 1, optval, 4u);
func3(5, g_context[261130] | 1);
result = 0LL;
}
}
else
{
fprintf(stderr, "Unknown host %s.\n", "localhost");
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int rj_interface_init() {
 struct sockaddr_in name;
 int flag;
 rj_sock = socket(2, SOCK_STREAM, 0);
 if (rj_sock < 0) {
  perror("socket");
  return -1;
 }
 struct hostent *hostinfo;
 name.sin_family = 2;
 name.sin_port = htons(10004);
 hostinfo = gethostbyname("localhost");
 if (hostinfo == ((void *)0)) {
  fprintf(stderr, "Unknown host %s.\n", "localhost");
  return -1;
 }
 name.sin_addr = *(struct in_addr *) hostinfo->h_addr_list[0];
 if (connect(rj_sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  perror("connect");
  close(rj_sock);
  return -1;
 }
 setsockopt(rj_sock, 6, 1, &flag, sizeof(int));
 rj_send_event(5, 1 | g_context.psp_flags);
 return 0;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: flag int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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


decompiled_code:
int rj_interface_cleanup()
{
int result; // eax
int v1; // edi

if ( rj_reader_thread )
{
result = SDL_KillThread();
v1 = rj_sock;
if ( rj_sock < 0 )
return result;
}
else
{
v1 = rj_sock;
if ( rj_sock < 0 )
return result;
}
return close(v1);
}

canonical_decompiled_code:
int func5()
{
int result; // eax
int v1; // edi

if ( rj_reader_thread )
{
result = SDL_KillThread();
v1 = rj_sock;
if ( rj_sock < 0 )
return result;
}
else
{
v1 = rj_sock;
if ( rj_sock < 0 )
return result;
}
return close(v1);
}

original_code:
void rj_interface_cleanup() {
 if (rj_reader_thread)
  SDL_KillThread(rj_reader_thread);
 if (rj_sock >= 0)
  close(rj_sock);
}

return_type: void


decompiled_code:
char *__fastcall remove_wsp(void *src)
{
int v2; // er13
int v3; // ebp
const unsigned __int16 **v4; // rax
int v5; // edx
const unsigned __int16 *v6; // rdi
const unsigned __int16 **v7; // rbx
char *result; // rax
char *v9; // rsi
__int64 v10; // rbp
char *v11; // rdx

v2 = strlen((const char *)src);
v3 = v2;
v4 = _ctype_b_loc();
v5 = 0;
v6 = *v4;
v7 = v4;
result = (char *)src;
while ( 1 )
{
v9 = result++;
if ( (v6[*v9] & 0x2000) == 0 )
break;
++v5;
}
if ( v5 )
{
v3 = v2 - v5;
result = (char *)memmove(src, v9, v2 - v5 + 1);
}
if ( v3 > 0 )
{
v10 = v3 - 1;
result = (char *)src + v10;
if ( ((*v7)[*((char *)src + v10)] & 0x2000) != 0 )
{
v11 = (char *)src + v10 - 1;
do
{
*result = 0;
result = v11--;
}
while ( ((*v7)[*result] & 0x2000) != 0 );
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(void *src)
{
int v2; // er13
int v3; // ebp
const unsigned __int16 **v4; // rax
int v5; // edx
const unsigned __int16 *v6; // rdi
const unsigned __int16 **v7; // rbx
char *result; // rax
char *v9; // rsi
__int64 v10; // rbp
char *v11; // rdx

v2 = strlen((const char *)src);
v3 = v2;
v4 = _ctype_b_loc();
v5 = 0;
v6 = *v4;
v7 = v4;
result = (char *)src;
while ( 1 )
{
v9 = result++;
if ( (v6[*v9] & 0x2000) == 0 )
break;
++v5;
}
if ( v5 )
{
v3 = v2 - v5;
result = (char *)memmove(src, v9, v2 - v5 + 1);
}
if ( v3 > 0 )
{
v10 = v3 - 1;
result = (char *)src + v10;
if ( ((*v7)[*((char *)src + v10)] & 0x2000) != 0 )
{
v11 = (char *)src + v10 - 1;
do
{
*result = 0;
result = v11--;
}
while ( ((*v7)[*result] & 0x2000) != 0 );
}
}
return result;
}

original_code:
void remove_wsp(char* buf) {
 int len = strlen(buf);
 int i = 0;
 while (((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace))
  i++;
 if (i > 0) {
  len -= i;
  memmove(buf, &buf[i], len + 1);
 }
 if (len <= 0)
  return;
 i = len - 1;
 while(((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace))
  buf[i--] = 0;
}

canonical_original_code:
void remove_wsp(char *buf) {
 int len = strlen(buf);
 int i = 0;
 while (((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace))
  i++;
 if (i > 0) {
  len -= i;
  memmove(buf, &buf[i], len + 1);
 }
 if (len <= 0)
  return;
 i = len - 1;
 while(((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace))
  buf[i--] = 0;
}

variable_types:
buf
char *

variable_types: i int


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

if ( !strcmp(a1, "bios") )
{
pc = 0;
result = 1LL;
}
else
{
fprintf(stderr, "Invalid option : %s\n", a1);
result = 0LL;
}
return result;
}

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

if ( !strcmp(a1, "bios") )
{
pc = 0;
result = 1LL;
}
else
{
fprintf(stderr, "Invalid option : %s\n", a1);
result = 0LL;
}
return result;
}

original_code:
int parse_option(char *attr, char *val)
{
 if (!strcmp(attr,"bios")) {
  pc = 0;
 } else {
  fprintf(stderr,"Invalid option : %s\n",attr);
  return 0;
 }
 return 1;
}

variable_types:
attr
char *

variable_types:
val
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char **v4; // rbp
const char **v5; // r14
const char *v6; // rsi
char *v7; // rax
char *v8; // rsi
size_t v9; // rbx
size_t v10; // r12
const unsigned __int16 **v11; // rax
__int64 v12; // r13
FILE *v13; // rax
FILE *v14; // rbp
__int64 v15; // rbx
unsigned __int16 v16; // di
__int64 v17; // rbx

pc = 1024;
if ( argc <= 1 )
{
puts("Usage: dis48 [file] [-bios]");
exit(0);
}
v4 = (char **)(argv + 1);
v5 = &argv[(unsigned int)(argc - 2) + 2];
do
{
while ( **v4 != 45 )
{
v6 = *v4++;
strncat(&file_3, v6, 0x3FFuLL);
byte_31DF = 0;
if ( v4 == (char **)v5 )
goto LABEL_16;
}
v7 = strtok(*v4, "=");
if ( v7 )
{
strncpy(attr_1, v7 + 1, 0x3FFuLL);
byte_2DDF = 0;
}
else
{
attr_1[0] = 0;
}
v8 = strtok(0LL, "=");
if ( v8 )
{
strncpy(&val_0, v8, 0x3FFuLL);
byte_29DF = 0;
}
else
{
val_0 = 0;
}
v9 = 0LL;
v10 = strlen(attr_1);
while ( v9 < v10 )
{
v11 = _ctype_b_loc();
v12 = attr_1[v9];
if ( ((*v11)[v12] & 0x100) != 0 )
{
attr_1[v9] = (*_ctype_tolower_loc())[v12];
v10 = strlen(attr_1);
}
++v9;
}
if ( !(unsigned int)parse_option(attr_1) )
goto LABEL_18;
++v4;
}
while ( v4 != (char **)v5 );
LABEL_16:
if ( file_3 )
{
v13 = fopen(&file_3, "rb");
v14 = v13;
if ( v13 )
{
fseek(v13, 0LL, 2);
v15 = ftell(v14);
if ( v15 <= 0x2000 )
{
rewind(v14);
if ( v15 != fread(&rom[(unsigned __int16)pc], 1uLL, v15, v14) )
{
printf("Error loading %s\n", argv[1]);
fclose(v14);
exit(0);
}
fclose(v14);
v16 = pc;
v17 = (unsigned __int16)pc + v15;
while ( v16 < v17 )
{
pc += disasm(v16);
v16 = pc;
}
exit(0);
}
printf("Invalid image size");
fclose(v14);
exit(0);
}
printf("Error loading %s\n", argv[1]);
exit(0);
}
fwrite("Error: file name missing\n", 1uLL, 0x19uLL, stderr);
LABEL_18:
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char **v4; // rbp
const char **v5; // r14
const char *v6; // rsi
char *v7; // rax
char *v8; // rsi
size_t v9; // rbx
size_t v10; // r12
const unsigned __int16 **v11; // rax
__int64 v12; // r13
FILE *v13; // rax
FILE *v14; // rbp
__int64 v15; // rbx
unsigned __int16 v16; // di
__int64 v17; // rbx

pc = 1024;
if ( argc <= 1 )
{
puts("Usage: dis48 [file] [-bios]");
exit(0);
}
v4 = (char **)(argv + 1);
v5 = &argv[(unsigned int)(argc - 2) + 2];
do
{
while ( **v4 != 45 )
{
v6 = *v4++;
strncat(&file_3, v6, 0x3FFuLL);
byte_31DF = 0;
if ( v4 == (char **)v5 )
goto LABEL_16;
}
v7 = strtok(*v4, "=");
if ( v7 )
{
strncpy(attr_1, v7 + 1, 0x3FFuLL);
byte_2DDF = 0;
}
else
{
attr_1[0] = 0;
}
v8 = strtok(0LL, "=");
if ( v8 )
{
strncpy(&val_0, v8, 0x3FFuLL);
byte_29DF = 0;
}
else
{
val_0 = 0;
}
v9 = 0LL;
v10 = strlen(attr_1);
while ( v9 < v10 )
{
v11 = _ctype_b_loc();
v12 = attr_1[v9];
if ( ((*v11)[v12] & 0x100) != 0 )
{
attr_1[v9] = (*_ctype_tolower_loc())[v12];
v10 = strlen(attr_1);
}
++v9;
}
if ( !(unsigned int)func1(attr_1) )
goto LABEL_18;
++v4;
}
while ( v4 != (char **)v5 );
LABEL_16:
if ( file_3 )
{
v13 = fopen(&file_3, "rb");
v14 = v13;
if ( v13 )
{
fseek(v13, 0LL, 2);
v15 = ftell(v14);
if ( v15 <= 0x2000 )
{
rewind(v14);
if ( v15 != fread(&rom[(unsigned __int16)pc], 1uLL, v15, v14) )
{
printf("Error loading %s\n", argv[1]);
fclose(v14);
exit(0);
}
fclose(v14);
v16 = pc;
v17 = (unsigned __int16)pc + v15;
while ( v16 < v17 )
{
pc += func0(v16);
v16 = pc;
}
exit(0);
}
printf("Invalid image size");
fclose(v14);
exit(0);
}
printf("Error loading %s\n", argv[1]);
exit(0);
}
fwrite("Error: file name missing\n", 1uLL, 0x19uLL, stderr);
LABEL_18:
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
    int i, cnt, cnt2;
    static char file[1024], attr[1024], val[1024], *p, *binver;
    binver = "UNIX binary";
    printf("%s %s\n", "\nO2EM v" "1.18" " " "(Jan/2007)" "  - ", binver);
    printf(
            "Free Odyssey2 / Videopac+ Emulator - http://o2em.sourceforge.net\n");
    printf("Created by Daniel Boris (c)1996/1998\n");
    printf("Developed by:\n");
    printf("     Andre de la Rocha since version 0.80\n");
    printf("     Arlindo M. de Oliveira since version 1.16\n");
    printf("\n");
    app_data.debug = 0;
    app_data.stick[0] = app_data.stick[1] = 1;
    app_data.sticknumber[0] = app_data.sticknumber[1] = 0;
    set_defjoykeys(0, 0);
    set_defjoykeys(1, 1);
    set_defsystemkeys();
    app_data.bank = 0;
    app_data.limit = 1;
    app_data.sound_en = 1;
    app_data.speed = 100;
    app_data.wsize = 2;
    app_data.fullscreen = 0;
    app_data.scanlines = 0;
    app_data.voice = 1;
    app_data.window_title = "O2EM v" "1.18";
    app_data.svolume = 100;
    app_data.vvolume = 100;
    app_data.filter = 0;
    app_data.exrom = 0;
    app_data.three_k = 0;
    app_data.crc = 0;
    app_data.scshot = scshot;
    app_data.statefile = statefile;
    app_data.euro = 0;
    app_data.openb = 0;
    app_data.vpp = 0;
    app_data.bios = 0;
    app_data.scoretype = 0;
    app_data.scoreaddress = 0;
    app_data.default_highscore = 0;
    app_data.breakpoint = 65535;
    app_data.megaxrom = 0;
    strcpy(file, "");
    strcpy(file_l, "");
    strcpy(bios_l, "");
    strcpy(bios, "");
    strcpy(scshot, "");
    strcpy(statefile, "");
    strcpy(xrom, "");
    strcpy(scorefile, "highscore.txt");
    read_default_config();
    if (argc >= 2) {
        for (i = 1; i < argc; i++) {
            if (argv[i][0] != '-') {
                strncat(file, argv[i], 1024 - 1);
                file[1024 - 1] = 0;
                strcpy(file_v, file);
            } else {
                p = strtok(argv[i], "=");
                if (p) {
                    strncpy(attr, p + 1, 1024 - 1);
                    attr[1024 - 1] = 0;
                } else {
                    strcpy(attr, "");
                }
                p = strtok(((void *)0), "=");
                if (p) {
                    strncpy(val, p, 1024 - 1);
                    val[1024 - 1] = 0;
                    if (!strcmp(attr, "romdir") || !strcmp(attr, "ROMDIR")) {
                        strcpy(romdir, val);
                        strcat(romdir, file);
                        strcpy(file, romdir);
                        strcpy(romdir, val);
                    }
                    if (!strcmp(attr, "biosdir") || !strcmp(attr, "BIOSDIR")) {
                        strcpy(biosdir, val);
                    }
                } else {
                    strcpy(val, "");
                }
                _alemu_strlwr(attr);
                if (!parse_option(attr, val)) {
                    exit(1);
                }
            }
        }
        if (helpflag) {
            helpus();
        }
        if (strlen(file) == 0) {
            fprintf(stderr, "Error: file name missing\n");
            exit(1);
        }
        printf("Starting emulation ...\n");
        _install_allegro_version_check(0, &(*__errno_location ()), (int (*)(void (*)(void)))atexit, (((4)<<16)|((4)<<8)|(2)));
        install_timer();
        init_audio();
        printf("Using Allegro %s\n", allegro_id);
        k = strchr(romdir, '/');
        launcher_flag_r = strchr(file, '\\');
        if (k != 0) {
            strcpy(xrom, romdir);
        } else if (!launcher_flag_r) {
            strcpy(xrom, "roms/");
            strcpy(romdir, file);
            strcpy(file, xrom);
            strcat(file, romdir);
            strcpy(romdir, xrom);
        } else {
            cnt = 0;
            cnt2 = 0;
            for (cnt = 0; file[cnt] != '\0'; cnt = cnt + 1) {
                if (file[cnt] == '\\') {
                    cnt2 = cnt;
                }
            }
            for (cnt = 0; cnt <= cnt2; cnt++) {
                file_l[cnt] = file[cnt];
            }
            strcpy(romdir, file_l);
            strcpy(xrom, romdir);
        }
        file_name(xrom);
        if (contax < 3) {
            printf("\nROMs directory empty!\n");
            exit(1);
        }
        app_data.crc = crc32_file(file);
        crcx = app_data.crc;
        suck_roms();
        launcher_flag_b = strchr(bios, '\\');
        if (!launcher_flag_b) {
            k = strchr(biosdir, '/');
            if (k != 0) {
                strcpy(xbios, biosdir);
            } else {
                strcpy(xbios, "bios/");
                strcpy(biosdir, xbios);
            }
            file_name(xbios);
            if (contax < 3) {
                printf("\nBIOS directory empty!\n");
                exit(1);
            }
            suck_bios();
            c_j = strcmp(bios, "jopac");
            if ((rom_f != 1) && (c_j != 0)) {
                strcpy(bios, g7400);
            }
            if ((!o2flag) && (!jopflag) && (!c52flag) && (!g74flag)) {
                printf("\ndir '%s' without BIOS !\n", biosdir);
                exit(1);
            }
            printf("BIOS found:\n");
            if (!strcmp(bios, "g7400")) {
                strcpy(bios, g7400);
                if (g74flag != 1) {
                    printf("\nG7400 BIOS not found !\n");
                    exit(1);
                }
            }
            if (g74flag)
                printf("  G7400 VP+\n");
            if (!strcmp(bios, "c52")) {
                strcpy(bios, c52);
                if (c52flag != 1) {
                    printf("\nC52 BIOS not found !\n");
                    exit(1);
                }
            }
            if (c52flag)
                printf("  C52\n");
            if (!strcmp(bios, "jopac")) {
                strcpy(bios, jopac);
                if (jopflag != 1) {
                    printf("\nJOPAC BIOS not found !\n");
                    exit(1);
                }
            }
            if (jopflag)
                printf("  JOPAC VP+\n");
            if ((!strcmp(bios, "")) || (!strcmp(bios, "o2rom"))) {
                strcpy(bios, odyssey2);
                if ((!o2flag) && (!c52flag) && (rom_f)) {
                    printf("Odyssey2 BIOS not found !\n");
                    exit(1);
                }
                if ((!o2flag) && (c52flag) && (rom_f)) {
                    printf("\nOdyssey2 BIOS not found !\n");
                    printf("Loading C52 BIOS ... ");
                    strcpy(bios, c52);
                }
            }
            if (o2flag)
                printf("  Odyssey 2\n");
        }
        if (launcher_flag_b) {
            identify_bios(bios);
            if (rom_f != 1) {
                if (!((g74flag) || (jopflag))) {
                    fprintf(stderr, "\nError: ROM only VP+ BIOS");
                    exit(1);
                }
            }
        } else {
            if (rom_f != 1) {
                if (!((g74flag) || (jopflag))) {
                    printf("\nROM only VP+ BIOS\n");
                    exit(1);
                }
                if (!(g74flag)) {
                    printf("\nVP+ G7400 BIOS not found !");
                    printf("\nLoading VP+ Jopac BIOS ...");
                    strcpy(bios, jopac);
                }
            }
        }
        load_bios(bios);
        load_cart(file);
        if (app_data.voice)
            load_voice_samples(path);
        init_display();
        init_cpu();
        init_system();
        set_score(app_data.scoretype, app_data.scoreaddress, app_data.default_highscore);
        int stateError;
        if ((stateError = loadstate(app_data.statefile)) == 0) {
            printf("Savefile loaded.");
        } else if (stateError >= 199) {
            if (stateError == 199)
                fprintf(stderr, "Wrong ROM-File for Savefile.");
            else if (stateError == 200 + 1)
                fprintf(stderr, "Wrong BIOS for Savefile: O2ROM needed.");
            else if (stateError == 200 + 2)
                fprintf(stderr, "Wrong BIOS for Savefile: G7400 ROM needed.");
            else if (stateError == 200 + 3)
                fprintf(stderr, "Wrong BIOS for Savefile: C52 ROM needed.");
            else if (stateError == 200 + 4)
                fprintf(stderr, "Wrong BIOS for Savefile: JOPAC ROM needed.");
            else
                fprintf(stderr, "Wrong BIOS for Savefile: UNKNOWN ROM needed.");
            return (0);
        }
        if (app_data.debug)
            key_debug = 1;
        run();
        if (app_data.scoretype != 0)
            save_highscore(get_score(app_data.scoretype, app_data.scoreaddress),
                    scorefile);
        exit(0);
    }
    if (!strcmp(attr, "help") || !strcmp(attr, "HELP"))
        helpus();
    printf("type o2em -help\n");
    exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int i; int cnt; int cnt2;
    static char file[1024]; static char attr[1024]; static char val[1024]; static char *p; static char *binver;
    binver = "UNIX binary";
    printf("%s %s\n", "\nO2EM v" "1.18" " " "(Jan/2007)" "  - ", binver);
    printf(
            "Free Odyssey2 / Videopac+ Emulator - http://o2em.sourceforge.net\n");
    printf("Created by Daniel Boris (c)1996/1998\n");
    printf("Developed by:\n");
    printf("     Andre de la Rocha since version 0.80\n");
    printf("     Arlindo M. de Oliveira since version 1.16\n");
    printf("\n");
    app_data.debug = 0;
    app_data.stick[0] = app_data.stick[1] = 1;
    app_data.sticknumber[0] = app_data.sticknumber[1] = 0;
    set_defjoykeys(0, 0);
    set_defjoykeys(1, 1);
    set_defsystemkeys();
    app_data.bank = 0;
    app_data.limit = 1;
    app_data.sound_en = 1;
    app_data.speed = 100;
    app_data.wsize = 2;
    app_data.fullscreen = 0;
    app_data.scanlines = 0;
    app_data.voice = 1;
    app_data.window_title = "O2EM v" "1.18";
    app_data.svolume = 100;
    app_data.vvolume = 100;
    app_data.filter = 0;
    app_data.exrom = 0;
    app_data.three_k = 0;
    app_data.crc = 0;
    app_data.scshot = scshot;
    app_data.statefile = statefile;
    app_data.euro = 0;
    app_data.openb = 0;
    app_data.vpp = 0;
    app_data.bios = 0;
    app_data.scoretype = 0;
    app_data.scoreaddress = 0;
    app_data.default_highscore = 0;
    app_data.breakpoint = 65535;
    app_data.megaxrom = 0;
    strcpy(file, "");
    strcpy(file_l, "");
    strcpy(bios_l, "");
    strcpy(bios, "");
    strcpy(scshot, "");
    strcpy(statefile, "");
    strcpy(xrom, "");
    strcpy(scorefile, "highscore.txt");
    read_default_config();
    if (argc >= 2) {
        for (i = 1; i < argc; i++) {
            if (argv[i][0] != '-') {
                strncat(file, argv[i], 1024 - 1);
                file[1024 - 1] = 0;
                strcpy(file_v, file);
            } else {
                p = strtok(argv[i], "=");
                if (p) {
                    strncpy(attr, p + 1, 1024 - 1);
                    attr[1024 - 1] = 0;
                } else {
                    strcpy(attr, "");
                }
                p = strtok(((void *)0), "=");
                if (p) {
                    strncpy(val, p, 1024 - 1);
                    val[1024 - 1] = 0;
                    if (!strcmp(attr, "romdir") || !strcmp(attr, "ROMDIR")) {
                        strcpy(romdir, val);
                        strcat(romdir, file);
                        strcpy(file, romdir);
                        strcpy(romdir, val);
                    }
                    if (!strcmp(attr, "biosdir") || !strcmp(attr, "BIOSDIR")) {
                        strcpy(biosdir, val);
                    }
                } else {
                    strcpy(val, "");
                }
                _alemu_strlwr(attr);
                if (!parse_option(attr, val)) {
                    exit(1);
                }
            }
        }
        if (helpflag) {
            helpus();
        }
        if (strlen(file) == 0) {
            fprintf(stderr, "Error: file name missing\n");
            exit(1);
        }
        printf("Starting emulation ...\n");
        _install_allegro_version_check(0, &(*__errno_location ()), (int (*)(void (*)()))atexit, (((4)<<16)|((4)<<8)|(2)));
        install_timer();
        init_audio();
        printf("Using Allegro %s\n", allegro_id);
        k = strchr(romdir, '/');
        launcher_flag_r = strchr(file, '\\');
        if (k != 0) {
            strcpy(xrom, romdir);
        } else if (!launcher_flag_r) {
            strcpy(xrom, "roms/");
            strcpy(romdir, file);
            strcpy(file, xrom);
            strcat(file, romdir);
            strcpy(romdir, xrom);
        } else {
            cnt = 0;
            cnt2 = 0;
            for (cnt = 0; file[cnt] != '\0'; cnt = cnt + 1) {
                if (file[cnt] == '\\') {
                    cnt2 = cnt;
                }
            }
            for (cnt = 0; cnt <= cnt2; cnt++) {
                file_l[cnt] = file[cnt];
            }
            strcpy(romdir, file_l);
            strcpy(xrom, romdir);
        }
        file_name(xrom);
        if (contax < 3) {
            printf("\nROMs directory empty!\n");
            exit(1);
        }
        app_data.crc = crc32_file(file);
        crcx = app_data.crc;
        suck_roms();
        launcher_flag_b = strchr(bios, '\\');
        if (!launcher_flag_b) {
            k = strchr(biosdir, '/');
            if (k != 0) {
                strcpy(xbios, biosdir);
            } else {
                strcpy(xbios, "bios/");
                strcpy(biosdir, xbios);
            }
            file_name(xbios);
            if (contax < 3) {
                printf("\nBIOS directory empty!\n");
                exit(1);
            }
            suck_bios();
            c_j = strcmp(bios, "jopac");
            if ((rom_f != 1) && (c_j != 0)) {
                strcpy(bios, g7400);
            }
            if ((!o2flag) && (!jopflag) && (!c52flag) && (!g74flag)) {
                printf("\ndir '%s' without BIOS !\n", biosdir);
                exit(1);
            }
            printf("BIOS found:\n");
            if (!strcmp(bios, "g7400")) {
                strcpy(bios, g7400);
                if (g74flag != 1) {
                    printf("\nG7400 BIOS not found !\n");
                    exit(1);
                }
            }
            if (g74flag)
                printf("  G7400 VP+\n");
            if (!strcmp(bios, "c52")) {
                strcpy(bios, c52);
                if (c52flag != 1) {
                    printf("\nC52 BIOS not found !\n");
                    exit(1);
                }
            }
            if (c52flag)
                printf("  C52\n");
            if (!strcmp(bios, "jopac")) {
                strcpy(bios, jopac);
                if (jopflag != 1) {
                    printf("\nJOPAC BIOS not found !\n");
                    exit(1);
                }
            }
            if (jopflag)
                printf("  JOPAC VP+\n");
            if ((!strcmp(bios, "")) || (!strcmp(bios, "o2rom"))) {
                strcpy(bios, odyssey2);
                if ((!o2flag) && (!c52flag) && (rom_f)) {
                    printf("Odyssey2 BIOS not found !\n");
                    exit(1);
                }
                if ((!o2flag) && (c52flag) && (rom_f)) {
                    printf("\nOdyssey2 BIOS not found !\n");
                    printf("Loading C52 BIOS ... ");
                    strcpy(bios, c52);
                }
            }
            if (o2flag)
                printf("  Odyssey 2\n");
        }
        if (launcher_flag_b) {
            identify_bios(bios);
            if (rom_f != 1) {
                if (!((g74flag) || (jopflag))) {
                    fprintf(stderr, "\nError: ROM only VP+ BIOS");
                    exit(1);
                }
            }
        } else {
            if (rom_f != 1) {
                if (!((g74flag) || (jopflag))) {
                    printf("\nROM only VP+ BIOS\n");
                    exit(1);
                }
                if (!(g74flag)) {
                    printf("\nVP+ G7400 BIOS not found !");
                    printf("\nLoading VP+ Jopac BIOS ...");
                    strcpy(bios, jopac);
                }
            }
        }
        load_bios(bios);
        load_cart(file);
        if (app_data.voice)
            load_voice_samples(path);
        init_display();
        init_cpu();
        init_system();
        set_score(app_data.scoretype, app_data.scoreaddress, app_data.default_highscore);
        int stateError;
        if ((stateError = loadstate(app_data.statefile)) == 0) {
            printf("Savefile loaded.");
        } else if (stateError >= 199) {
            if (stateError == 199)
                fprintf(stderr, "Wrong ROM-File for Savefile.");
            else if (stateError == 200 + 1)
                fprintf(stderr, "Wrong BIOS for Savefile: O2ROM needed.");
            else if (stateError == 200 + 2)
                fprintf(stderr, "Wrong BIOS for Savefile: G7400 ROM needed.");
            else if (stateError == 200 + 3)
                fprintf(stderr, "Wrong BIOS for Savefile: C52 ROM needed.");
            else if (stateError == 200 + 4)
                fprintf(stderr, "Wrong BIOS for Savefile: JOPAC ROM needed.");
            else
                fprintf(stderr, "Wrong BIOS for Savefile: UNKNOWN ROM needed.");
            return (0);
        }
        if (app_data.debug)
            key_debug = 1;
        run();
        if (app_data.scoretype != 0)
            save_highscore(get_score(app_data.scoretype, app_data.scoreaddress),
                    scorefile);
        exit(0);
    }
    if (!strcmp(attr, "help") || !strcmp(attr, "HELP"))
        helpus();
    printf("type o2em -help\n");
    exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: cnt int

variable_types:
file
char [1024]

variable_types:
attr
char [1024]

variable_types:
val
char [1024]

variable_types:
p
char *

variable_types:
binver
char *

variable_types: stateError int


disasm

decompiled_code:
__int64 __fastcall disasm(unsigned __int16 a1)
{
__int16 v1; // r13
__int64 v2; // rbp
char v3; // r12
unsigned __int8 v4; // al
__int64 result; // rax

v1 = (unsigned __int8)rom[a1];
v2 = (unsigned __int8)rom[a1];
v3 = rom[a1];
printf("%04x %02x", (unsigned int)(unsigned __int16)(a1 + 1) - 1, v2);
if ( *((_BYTE *)&lookup + 16 * v2 + 8) == 2 )
printf(" %02x", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
else
printf(" ");
printf(" %s", *((const char **)&lookup + 2 * v2));
v4 = *((_BYTE *)&lookup + 16 * v2 + 9);
if ( v4 == 2 )
{
printf(" $%03x\n", (unsigned __int16)(unsigned __int8)rom[(unsigned __int16)(a1 + 1)] | (8 * v1) & 0x700u);
}
else if ( v4 > 2u )
{
if ( v4 == 3 )
printf(" $%02x\n", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
}
else if ( v4 )
{
printf(" #%02x\n", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
}
else
{
putchar(10);
}
if ( (v3 & 0xEF) == 0x83
|| (result = *((unsigned __int8 *)&lookup + 16 * v2 + 8), (unsigned __int8)(a1 + 1 + result - 2) == 255) )
{
putchar(10);
result = *((unsigned __int8 *)&lookup + 16 * v2 + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
__int16 v1; // r13
__int64 v2; // rbp
char v3; // r12
unsigned __int8 v4; // al
__int64 result; // rax

v1 = (unsigned __int8)rom[a1];
v2 = (unsigned __int8)rom[a1];
v3 = rom[a1];
printf("%04x %02x", (unsigned int)(unsigned __int16)(a1 + 1) - 1, v2);
if ( *((_BYTE *)&lookup + 16 * v2 + 8) == 2 )
printf(" %02x", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
else
printf(" ");
printf(" %s", *((const char **)&lookup + 2 * v2));
v4 = *((_BYTE *)&lookup + 16 * v2 + 9);
if ( v4 == 2 )
{
printf(" $%03x\n", (unsigned __int16)(unsigned __int8)rom[(unsigned __int16)(a1 + 1)] | (8 * v1) & 0x700u);
}
else if ( v4 > 2u )
{
if ( v4 == 3 )
printf(" $%02x\n", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
}
else if ( v4 )
{
printf(" #%02x\n", (unsigned __int8)rom[(unsigned __int16)(a1 + 1)]);
}
else
{
putchar(10);
}
if ( (v3 & 0xEF) == 0x83
|| (result = *((unsigned __int8 *)&lookup + 16 * v2 + 8), (unsigned __int8)(a1 + 1 + result - 2) == 255) )
{
putchar(10);
result = *((unsigned __int8 *)&lookup + 16 * v2 + 8);
}
return result;
}

original_code:
static int disasm(ADDRESS p) {
 Byte op,d;
 ADDRESS adr;
 op=rom[p++];
 printf("%04x  %02x",p-1,op);
 if (lookup[op].bytes == 2) printf(" %02x",rom[p]); else printf("   ");
 printf("   %s",lookup[op].mnemonic);
 switch(lookup[op].type) {
  case 0:
   printf("\n");
   break;
  case 1:
   d=rom[p];
   printf(" #%02x\n",d);
   break;
  case 2:
   adr=rom[p];
   adr = adr | ((op & 0xE0) << 3);
   printf(" $%03x\n",adr);
   break;
  case 3:
   printf(" $%02x\n",rom[p]);
   break;
 }
 if (op == 0x83 || op == 0x93 || (((p-2+lookup[op].bytes) & 0xff) == 0xff)) printf("\n");
 return lookup[op].bytes;
}

canonical_original_code:
static int disasm(unsigned short p) {
 unsigned char op; unsigned char d;
 unsigned short adr;
 op=rom[p++];
 printf("%04x  %02x",p-1,op);
 if (lookup[op].bytes == 2) printf(" %02x",rom[p]); else printf("   ");
 printf("   %s",lookup[op].mnemonic);
 switch(lookup[op].type) {
  case 0:
   printf("\n");
   break;
  case 1:
   d=rom[p];
   printf(" #%02x\n",d);
   break;
  case 2:
   adr=rom[p];
   adr = adr | ((op & 0xE0) << 3);
   printf(" $%03x\n",adr);
   break;
  case 3:
   printf(" $%02x\n",rom[p]);
   break;
 }
 if (op == 0x83 || op == 0x93 || (((p-2+lookup[op].bytes) & 0xff) == 0xff)) printf("\n");
 return lookup[op].bytes;
}

variable_types:
p
unsigned short

variable_types:
op
unsigned char

variable_types:
d
unsigned char

variable_types:
adr
unsigned short


decompiled_code:
int __fastcall load_voice_samples(const char *a1)
{
__int64 v1; // rbp
unsigned int v2; // ebx
__int64 *v3; // r14
unsigned int v4; // er15
unsigned int v5; // er12
__int64 v6; // rax
int result; // eax
int v8; // [rsp+4h] [rbp-444h]
__int64 *v9; // [rsp+8h] [rbp-440h]
char s[1080]; // [rsp+10h] [rbp-438h] BYREF

v1 = 0LL;
v2 = 0;
printf("Loading voice samples... ");
fflush(stdout);
v8 = 0;
v9 = &voices;
do
{
v3 = v9;
v4 = 128;
v5 = v8 + 231;
if ( !v8 )
v5 = 228;
do
{
sprintf(s, "%svoice/%02x%02x.wav", a1, v5, v4);
v6 = load_sample(s);
*v3 = v6;
if ( v6 || (sprintf(s, "%svoice/%02X%02X.WAV", a1, v5, v4), v6 = load_sample(s), (*v3 = v6) != 0) )
{
++v2;
if ( !v1 )
v1 = v6;
}
++v4;
++v3;
}
while ( v4 != 256 );
++v8;
v9 += 128;
}
while ( v8 != 9 );
result = printf("%d samples loaded\n", v2);
if ( v2 )
{
result = allocate_voice(v1);
voice_num = result;
if ( result == -1 )
{
result = puts(" ERROR: could not allocate sound card voice");
voice_ok = 0;
}
else
{
voice_ok = 1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
__int64 v1; // rbp
unsigned int v2; // ebx
__int64 *v3; // r14
unsigned int v4; // er15
unsigned int v5; // er12
__int64 v6; // rax
int result; // eax
int v8; // [rsp+4h] [rbp-444h]
__int64 *v9; // [rsp+8h] [rbp-440h]
char s[1080]; // [rsp+10h] [rbp-438h] BYREF

v1 = 0LL;
v2 = 0;
printf("Loading voice samples... ");
fflush(stdout);
v8 = 0;
v9 = &voices;
do
{
v3 = v9;
v4 = 128;
v5 = v8 + 231;
if ( !v8 )
v5 = 228;
do
{
sprintf(s, "%svoice/%02x%02x.wav", a1, v5, v4);
v6 = load_sample(s);
*v3 = v6;
if ( v6 || (sprintf(s, "%svoice/%02X%02X.WAV", a1, v5, v4), v6 = load_sample(s), (*v3 = v6) != 0) )
{
++v2;
if ( !v1 )
v1 = v6;
}
++v4;
++v3;
}
while ( v4 != 256 );
++v8;
v9 += 128;
}
while ( v8 != 9 );
result = printf("%d samples loaded\n", v2);
if ( v2 )
{
result = allocate_voice(v1);
voice_num = result;
if ( result == -1 )
{
result = puts(" ERROR: could not allocate sound card voice");
voice_ok = 0;
}
else
{
voice_ok = 1;
}
}
return result;
}

original_code:
void load_voice_samples(char *path)
{
    int bank, sam, i, ld = 0;
    char name[1024];
    SAMPLE *sp = ((void *)0);
    printf("Loading voice samples...  ");
    fflush(stdout);
    for (i = 0; i < 9; i++) {
        for (sam = 0; sam < 128; sam++) {
            if (i)
                bank = 0xE8 + i - 1;
            else
                bank = 0xE4;
            sprintf(name, "%svoice/%02x%02x.wav", path, bank, sam + 0x80);
            voices[i][sam] = load_sample(name);
            if (!voices[i][sam]) {
                sprintf(name, "%svoice/%02X%02X.WAV", path, bank, sam + 0x80);
                voices[i][sam] = load_sample(name);
            }
            if (voices[i][sam]) {
                ld++;
                if (!sp)
                    sp = voices[i][sam];
            }
        }
    }
    printf("%d samples loaded\n", ld);
    if (ld > 0) {
        voice_num = allocate_voice(sp);
        if (voice_num != -1)
            voice_ok = 1;
        else {
            printf("  ERROR: could not allocate sound card voice\n");
            voice_ok = 0;
        }
    }
}

canonical_original_code:
void load_voice_samples(char *path)
{
    int bank; int sam; int i; int ld = 0;
    char name[1024];
    struct SAMPLE *sp = ((void *)0);
    printf("Loading voice samples...  ");
    fflush(stdout);
    for (i = 0; i < 9; i++) {
        for (sam = 0; sam < 128; sam++) {
            if (i)
                bank = 0xE8 + i - 1;
            else
                bank = 0xE4;
            sprintf(name, "%svoice/%02x%02x.wav", path, bank, sam + 0x80);
            voices[i][sam] = load_sample(name);
            if (!voices[i][sam]) {
                sprintf(name, "%svoice/%02X%02X.WAV", path, bank, sam + 0x80);
                voices[i][sam] = load_sample(name);
            }
            if (voices[i][sam]) {
                ld++;
                if (!sp)
                    sp = voices[i][sam];
            }
        }
    }
    printf("%d samples loaded\n", ld);
    if (ld > 0) {
        voice_num = allocate_voice(sp);
        if (voice_num != -1)
            voice_ok = 1;
        else {
            printf("  ERROR: could not allocate sound card voice\n");
            voice_ok = 0;
        }
    }
}

variable_types:
path
char *

variable_types: bank int

variable_types: sam int

variable_types: ld int

variable_types:
name
char [1024]

variable_types: struct SAMPLE { int bits; int stereo; int freq; int priority; unsigned long len; unsigned long loop_start; unsigned long loop_end; unsigned long param; void *data; } *


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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
result = (unsigned int)voice_st;
if ( voice_st == 2 )
{
result = voice_get_position((unsigned int)voice_num);
if ( (int)result < 0 )
{
result = voice_bank;
if ( (unsigned int)voice_bank <= 8 && (unsigned int)(voice_addr - 128) <= 0x7F )
{
result = voice_addr - 128 + ((__int64)voice_bank << 7);
if ( !voices[result] )
{
LABEL_8:
voice_st = 0;
return result;
}
reallocate_voice((unsigned int)voice_num);
voice_set_volume((unsigned int)voice_num, (unsigned int)(255 * app_data[14] / 100));
voice_start((unsigned int)voice_num);
result = clk_counter;
voice_st = 1;
clk_voice_start = clk_counter;
}
}
}
else if ( voice_st == 1 )
{
result = voice_get_position((unsigned int)voice_num);
if ( (int)result < 0 )
goto LABEL_8;
result = clk_counter - clk_voice_start;
if ( (unsigned __int64)(clk_counter - clk_voice_start) > 0x14 )
goto LABEL_8;
}
}
return result;
}

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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
result = (unsigned int)voice_st;
if ( voice_st == 2 )
{
result = voice_get_position((unsigned int)voice_num);
if ( (int)result < 0 )
{
result = voice_bank;
if ( (unsigned int)voice_bank <= 8 && (unsigned int)(voice_addr - 128) <= 0x7F )
{
result = voice_addr - 128 + ((__int64)voice_bank << 7);
if ( !voices[result] )
{
LABEL_8:
voice_st = 0;
return result;
}
reallocate_voice((unsigned int)voice_num);
voice_set_volume((unsigned int)voice_num, (unsigned int)(255 * app_data[14] / 100));
voice_start((unsigned int)voice_num);
result = clk_counter;
voice_st = 1;
clk_voice_start = clk_counter;
}
}
}
else if ( voice_st == 1 )
{
result = voice_get_position((unsigned int)voice_num);
if ( (int)result < 0 )
goto LABEL_8;
result = clk_counter - clk_voice_start;
if ( (unsigned __int64)(clk_counter - clk_voice_start) > 0x14 )
goto LABEL_8;
}
}
return result;
}

original_code:
void update_voice(void)
{
    if (!voice_ok)
        return;
    if (voice_st == 2) {
        if (voice_get_position(voice_num) < 0) {
            if ((voice_bank >= 0) && (voice_bank < 9) && (voice_addr >= 0x80)
                    && (voice_addr <= 0xff)) {
                if (voices[voice_bank][voice_addr - 0x80]) {
                    reallocate_voice(voice_num,
                            voices[voice_bank][voice_addr - 0x80]);
                    voice_set_volume(voice_num, (255 * app_data.vvolume) / 100);
                    voice_start(voice_num);
                    clk_voice_start = clk_counter;
                    voice_st = 1;
                } else {
                    voice_st = 0;
                }
            }
        }
    } else if (voice_st == 1) {
        if ((voice_get_position(voice_num) < 0)
                || (clk_counter - clk_voice_start > 20)) {
            voice_st = 0;
        }
    }
}

canonical_original_code:
void update_voice()
{
    if (!voice_ok)
        return;
    if (voice_st == 2) {
        if (voice_get_position(voice_num) < 0) {
            if ((voice_bank >= 0) && (voice_bank < 9) && (voice_addr >= 0x80)
                    && (voice_addr <= 0xff)) {
                if (voices[voice_bank][voice_addr - 0x80]) {
                    reallocate_voice(voice_num,
                            voices[voice_bank][voice_addr - 0x80]);
                    voice_set_volume(voice_num, (255 * app_data.vvolume) / 100);
                    voice_start(voice_num);
                    clk_voice_start = clk_counter;
                    voice_st = 1;
                } else {
                    voice_st = 0;
                }
            }
        }
    } else if (voice_st == 1) {
        if ((voice_get_position(voice_num) < 0)
                || (clk_counter - clk_voice_start > 20)) {
            voice_st = 0;
        }
    }
}


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

if ( voice_ok )
{
if ( !voice_st || (update_voice(), result = (unsigned int)voice_st, !voice_st) )
{
if ( (unsigned int)voice_bank <= 8 )
{
result = (unsigned int)(a1 - 128);
if ( (unsigned int)result <= 0x7F )
{
voice_addr = a1;
voice_st = 2;
result = update_voice();
}
}
}
}
return result;
}

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

if ( voice_ok )
{
if ( !voice_st || (func1(), result = (unsigned int)voice_st, !voice_st) )
{
if ( (unsigned int)voice_bank <= 8 )
{
result = (unsigned int)(a1 - 128);
if ( (unsigned int)result <= 0x7F )
{
voice_addr = a1;
voice_st = 2;
result = func1();
}
}
}
}
return result;
}

original_code:
void trigger_voice(int addr)
{
    if (voice_ok) {
        if (voice_st)
            update_voice();
        if ((voice_st == 0) && (voice_bank >= 0) && (voice_bank < 9)
                && (addr >= 0x80) && (addr <= 0xff)) {
            voice_addr = addr;
            voice_st = 2;
            update_voice();
        }
    }
}

variable_types: addr int


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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
if ( a1 <= 8 )
voice_bank = a1;
}
return result;
}

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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
if ( a1 <= 8 )
voice_bank = a1;
}
return result;
}

original_code:
void set_voice_bank(int bank)
{
    if (!voice_ok)
        return;
    if ((bank >= 0) && (bank <= 8))
        voice_bank = bank;
}


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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
update_voice();
result = voice_st != 0;
}
return result;
}

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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
func1();
result = voice_st != 0;
}
return result;
}

original_code:
int get_voice_status(void)
{
    if (voice_ok) {
        update_voice();
        if (voice_st)
            return 1;
    }
    return 0;
}

canonical_original_code:
int get_voice_status()
{
    if (voice_ok) {
        update_voice();
        if (voice_st)
            return 1;
    }
    return 0;
}


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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
result = voice_stop((unsigned int)voice_num);
voice_bank = 0;
voice_addr = 0;
voice_st = 0;
}
return result;
}

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

result = (unsigned int)voice_ok;
if ( voice_ok )
{
result = voice_stop((unsigned int)voice_num);
voice_bank = 0;
voice_addr = 0;
voice_st = 0;
}
return result;
}

original_code:
void reset_voice(void)
{
    if (voice_ok) {
        voice_stop(voice_num);
        voice_bank = 0;
        voice_addr = 0;
        voice_st = 0;
    }
}

canonical_original_code:
void reset_voice()
{
    if (voice_ok) {
        voice_stop(voice_num);
        voice_bank = 0;
        voice_addr = 0;
        voice_st = 0;
    }
}


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

result = (unsigned int)voice_ok;
if ( voice_ok )
result = voice_stop((unsigned int)voice_num);
return result;
}

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

result = (unsigned int)voice_ok;
if ( voice_ok )
result = voice_stop((unsigned int)voice_num);
return result;
}

original_code:
void mute_voice(void)
{
    if (voice_ok) {
        voice_stop(voice_num);
    }
}

canonical_original_code:
void mute_voice()
{
    if (voice_ok) {
        voice_stop(voice_num);
    }
}


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

result = reset_voice();
voice_ok = 0;
return result;
}

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

result = func5();
voice_ok = 0;
return result;
}

original_code:
void close_voice(void)
{
    reset_voice();
    voice_ok = 0;
}

canonical_original_code:
void close_voice()
{
    reset_voice();
    voice_ok = 0;
}


decompiled_code:
int __fastcall set_joykeys(unsigned int a1, int a2, int a3, int a4, int a5, int a6)
{
unsigned __int64 i; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rdx
__int64 v14; // rdx

LODWORD(i) = printf("Setting joypad controller %d \n", a1);
if ( a1 <= 1 )
{
v11 = 5LL * (int)a1;
joykeys[v11] = a2;
joykeys[v11 + 1] = a3;
joykeys[v11 + 2] = a4;
joykeys[v11 + 3] = a5;
joykeys[v11 + 4] = a6;
v12 = 0LL;
memset(joykeystab, 0, sizeof(joykeystab));
do
{
v13 = joykeys[v12];
if ( (unsigned int)(v13 - 1) <= 0x7E )
joykeystab[v13] = 1;
else
joykeys[v12] = 0;
++v12;
}
while ( v12 != 5 );
for ( i = 0LL; i != 20; i += 4LL )
{
v14 = joykeys[i / 4 + 5];
if ( (unsigned int)(v14 - 1) <= 0x7E )
joykeystab[v14] = 1;
else
joykeys[i / 4 + 5] = 0;
}
}
return i;
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, int a2, int a3, int a4, int a5, int a6)
{
unsigned __int64 i; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rdx
__int64 v14; // rdx

LODWORD(i) = printf("Setting joypad controller %d \n", a1);
if ( a1 <= 1 )
{
v11 = 5LL * (int)a1;
joykeys[v11] = a2;
joykeys[v11 + 1] = a3;
joykeys[v11 + 2] = a4;
joykeys[v11 + 3] = a5;
joykeys[v11 + 4] = a6;
v12 = 0LL;
memset(joykeystab, 0, sizeof(joykeystab));
do
{
v13 = joykeys[v12];
if ( (unsigned int)(v13 - 1) <= 0x7E )
joykeystab[v13] = 1;
else
joykeys[v12] = 0;
++v12;
}
while ( v12 != 5 );
for ( i = 0LL; i != 20; i += 4LL )
{
v14 = joykeys[i / 4 + 5];
if ( (unsigned int)(v14 - 1) <= 0x7E )
joykeystab[v14] = 1;
else
joykeys[i / 4 + 5] = 0;
}
}
return i;
}

original_code:
void set_joykeys(int stick_number, int up, int down, int left, int right, int fire){
    printf("Setting joypad controller %d \n", stick_number);
    int i, j;
    if ((stick_number<0) || (stick_number>1)) {
        return;
    }
    joykeys[stick_number][0] = up;
    joykeys[stick_number][1] = down;
    joykeys[stick_number][2] = left;
    joykeys[stick_number][3] = right;
    joykeys[stick_number][4] = fire;
    for (i=0; i<128; i++) {
        joykeystab[i]=0;
    }
    for (j=0; j<2; j++) {
        for (i=0; i<5; i++) {
            if ((joykeys[j][i]<1) || (joykeys[j][i]>127)) {
                joykeys[j][i] = 0;
            } else {
                joykeystab[joykeys[j][i]] = 1;
            }
        }
    }
}

canonical_original_code:
void set_joykeys(int stick_number, int up, int down, int left, int right, int fire){
    printf("Setting joypad controller %d \n", stick_number);
    int i; int j;
    if ((stick_number<0) || (stick_number>1)) {
        return;
    }
    joykeys[stick_number][0] = up;
    joykeys[stick_number][1] = down;
    joykeys[stick_number][2] = left;
    joykeys[stick_number][3] = right;
    joykeys[stick_number][4] = fire;
    for (i=0; i<128; i++) {
        joykeystab[i]=0;
    }
    for (j=0; j<2; j++) {
        for (i=0; i<5; i++) {
            if ((joykeys[j][i]<1) || (joykeys[j][i]>127)) {
                joykeys[j][i] = 0;
            } else {
                joykeystab[joykeys[j][i]] = 1;
            }
        }
    }
}

variable_types: up int

variable_types: down int

variable_types: left int

variable_types: right int

variable_types: fire int

variable_types: i int

variable_types: j int


decompiled_code:
int __fastcall set_defjoykeys(unsigned int a1, unsigned int a2)
{
int result; // eax

printf("Setting keyboard controller %d to %d\n", a1, a2);
if ( a2 )
result = set_joykeys(a1, 23, 19, 1, 4, 75);
else
result = set_joykeys(a1, 84, 85, 82, 83, 12);
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, unsigned int a2)
{
int result; // eax

printf("Setting keyboard controller %d to %d\n", a1, a2);
if ( a2 )
result = func0(a1, 23, 19, 1, 4, 75);
else
result = func0(a1, 84, 85, 82, 83, 12);
return result;
}

original_code:
void set_defjoykeys(int stick_number, int control_scheme){
    printf("Setting keyboard controller %d to %d\n", stick_number, control_scheme);
    if (control_scheme) {
        set_joykeys(stick_number, __allegro_KEY_W, __allegro_KEY_S, __allegro_KEY_A, __allegro_KEY_D, __allegro_KEY_SPACE);
    } else {
        set_joykeys(stick_number,__allegro_KEY_UP,__allegro_KEY_DOWN,__allegro_KEY_LEFT,__allegro_KEY_RIGHT,__allegro_KEY_L);
    }
}


decompiled_code:
__int64 __fastcall set_systemkeys(int a1, int a2, int a3, int a4, int a5, int a6, int a7, unsigned int a8)
{
__int64 result; // rax

syskeys = a1;
*(&syskeys + 1) = a2;
*(&syskeys + 6) = a7;
result = a8;
*(&syskeys + 2) = a3;
*(&syskeys + 3) = a4;
*(&syskeys + 4) = a5;
*(&syskeys + 5) = a6;
*(&syskeys + 7) = a8;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, int a3, int a4, int a5, int a6, int a7, unsigned int a8)
{
__int64 result; // rax

syskeys = a1;
*(&syskeys + 1) = a2;
*(&syskeys + 6) = a7;
result = a8;
*(&syskeys + 2) = a3;
*(&syskeys + 3) = a4;
*(&syskeys + 4) = a5;
*(&syskeys + 5) = a6;
*(&syskeys + 7) = a8;
return result;
}

original_code:
void set_systemkeys(int k_quit,int k_pause,int k_debug,int k_reset,int k_screencap,int k_save,int k_load,int k_inject)
{
    syskeys[0] = k_quit;
    syskeys[1] = k_pause;
    syskeys[2] = k_debug;
    syskeys[3] = k_reset;
    syskeys[4] = k_screencap;
    syskeys[5] = k_save;
    syskeys[6] = k_load;
    syskeys[7] = k_inject;
}

canonical_original_code:
void set_systemkeys(int k_quit, int k_pause, int k_debug, int k_reset, int k_screencap, int k_save, int k_load, int k_inject)
{
    syskeys[0] = k_quit;
    syskeys[1] = k_pause;
    syskeys[2] = k_debug;
    syskeys[3] = k_reset;
    syskeys[4] = k_screencap;
    syskeys[5] = k_save;
    syskeys[6] = k_load;
    syskeys[7] = k_inject;
}


original_code:
void set_defsystemkeys(void)
{
    set_systemkeys(__allegro_KEY_F12,__allegro_KEY_F1,__allegro_KEY_F4,__allegro_KEY_F5,__allegro_KEY_F8,__allegro_KEY_F2,__allegro_KEY_F3,__allegro_KEY_F6);
}

canonical_original_code:
void set_defsystemkeys()
{
    set_systemkeys(__allegro_KEY_F12,__allegro_KEY_F1,__allegro_KEY_F4,__allegro_KEY_F5,__allegro_KEY_F8,__allegro_KEY_F2,__allegro_KEY_F3,__allegro_KEY_F6);
}


decompiled_code:
__int64 handle_key()
{
const char *v0; // rbp
__int64 result; // rax
char *v2; // rax
char *v3; // rbx
__int64 v4; // rcx
const char *v5; // rdx
__int64 v6; // r8
__int64 v7; // rcx
__int64 v8; // rbp
int v9; // eax
_BYTE v10[1048]; // [rsp+0h] [rbp-418h] BYREF

if ( NeedsPoll )
poll_keyboard();
if ( key[syskeys] || key[59] )
{
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[syskeys] || key[59] );
key_done = 1;
}
if ( key[*(&syskeys + 1)] )
{
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 1)] );
mute_audio();
mute_voice();
abaut();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
if ( key[119] && key[67] )
{
*((_DWORD *)&app_data + 10) = *((_DWORD *)&app_data + 10) == 0;
((void (*)(void))grmode)();
abaut();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[67] );
}
}
while ( !key[*(&syskeys + 1)] && !key[59] && !key[syskeys] );
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 1)] );
init_sound_stream();
}
if ( key[*(&syskeys + 5)] )
{
if ( !(unsigned int)savestate(*((_QWORD *)&app_data + 17)) )
display_msg("Savefile saved.", 5LL);
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 5)] );
}
if ( key[*(&syskeys + 6)] )
{
v9 = loadstate(*((_QWORD *)&app_data + 17));
if ( v9 )
{
if ( v9 > 198 )
{
switch ( v9 )
{
case 199:
display_msg("Wrong ROM-File for Savefile.", 5LL);
break;
case 201:
display_msg("Wrong BIOS for Savefile: O2ROM needed.", 5LL);
break;
case 202:
display_msg("Wrong BIOS for Savefile: G7400 ROM needed.", 5LL);
break;
case 203:
display_msg("Wrong BIOS for Savefile: C52 ROM needed.", 5LL);
break;
case 204:
display_msg("Wrong BIOS for Savefile: JOPAC ROM needed.", 5LL);
break;
default:
display_msg("Wrong BIOS for Savefile: UNKNOWN ROM needed.", 5LL);
break;
}
}
}
else
{
display_msg("Savefile loaded.", 5LL);
}
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 6)] );
}
if ( key[*(&syskeys + 2)] )
key_debug = 1;
if ( key[*(&syskeys + 3)] )
{
init_cpu();
init_roms();
init_vpp();
clearscr();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 3)] );
}
if ( key[*(&syskeys + 7)] )
set_score(*((unsigned int *)&app_data + 30), *((unsigned int *)&app_data + 31), *((unsigned int *)&app_data + 32));
if ( key[*(&syskeys + 4)] )
{
v0 = (const char *)*((_QWORD *)&app_data + 14);
if ( *v0 )
{
v2 = strchr(*((const char **)&app_data + 14), 64);
v3 = v2;
if ( v2 )
{
v4 = (unsigned int)scshot_counter_1;
*v2 = 0;
v5 = (const char *)*((_QWORD *)&app_data + 14);
scshot_counter_1 = v4 + 1;
sprintf(name_0, "%s%02d%s", v5, v4, v2 + 1);
*v3 = 64;
}
else
{
strcpy(name_0, v0);
}
get_palette(v10);
if ( gfx_driver )
{
v6 = *(unsigned int *)(gfx_driver + 220LL);
v7 = *(unsigned int *)(gfx_driver + 216LL);
}
else
{
v6 = 0LL;
v7 = 0LL;
}
v8 = create_sub_bitmap(screen, 0LL, 0LL, v7, v6);
save_bitmap(name_0, v8, v10);
destroy_bitmap(v8);
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 4)] );
}
}
result = key[119];
if ( (_BYTE)result )
{
result = key[67];
if ( (_BYTE)result )
{
*((_DWORD *)&app_data + 10) = *((_DWORD *)&app_data + 10) == 0;
((void (*)(void))grmode)();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
result = key[67];
}
while ( (_BYTE)result );
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
const char *v0; // rbp
__int64 result; // rax
char *v2; // rax
char *v3; // rbx
__int64 v4; // rcx
const char *v5; // rdx
__int64 v6; // r8
__int64 v7; // rcx
__int64 v8; // rbp
int v9; // eax
_BYTE v10[1048]; // [rsp+0h] [rbp-418h] BYREF

if ( NeedsPoll )
poll_keyboard();
if ( key[syskeys] || key[59] )
{
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[syskeys] || key[59] );
key_done = 1;
}
if ( key[*(&syskeys + 1)] )
{
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 1)] );
mute_audio();
mute_voice();
abaut();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
if ( key[119] && key[67] )
{
*((_DWORD *)&app_data + 10) = *((_DWORD *)&app_data + 10) == 0;
((void (*)(void))grmode)();
abaut();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[67] );
}
}
while ( !key[*(&syskeys + 1)] && !key[59] && !key[syskeys] );
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 1)] );
init_sound_stream();
}
if ( key[*(&syskeys + 5)] )
{
if ( !(unsigned int)savestate(*((_QWORD *)&app_data + 17)) )
display_msg("Savefile saved.", 5LL);
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 5)] );
}
if ( key[*(&syskeys + 6)] )
{
v9 = loadstate(*((_QWORD *)&app_data + 17));
if ( v9 )
{
if ( v9 > 198 )
{
switch ( v9 )
{
case 199:
display_msg("Wrong ROM-File for Savefile.", 5LL);
break;
case 201:
display_msg("Wrong BIOS for Savefile: O2ROM needed.", 5LL);
break;
case 202:
display_msg("Wrong BIOS for Savefile: G7400 ROM needed.", 5LL);
break;
case 203:
display_msg("Wrong BIOS for Savefile: C52 ROM needed.", 5LL);
break;
case 204:
display_msg("Wrong BIOS for Savefile: JOPAC ROM needed.", 5LL);
break;
default:
display_msg("Wrong BIOS for Savefile: UNKNOWN ROM needed.", 5LL);
break;
}
}
}
else
{
display_msg("Savefile loaded.", 5LL);
}
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 6)] );
}
if ( key[*(&syskeys + 2)] )
key_debug = 1;
if ( key[*(&syskeys + 3)] )
{
init_cpu();
init_roms();
init_vpp();
clearscr();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 3)] );
}
if ( key[*(&syskeys + 7)] )
set_score(*((unsigned int *)&app_data + 30), *((unsigned int *)&app_data + 31), *((unsigned int *)&app_data + 32));
if ( key[*(&syskeys + 4)] )
{
v0 = (const char *)*((_QWORD *)&app_data + 14);
if ( *v0 )
{
v2 = strchr(*((const char **)&app_data + 14), 64);
v3 = v2;
if ( v2 )
{
v4 = (unsigned int)scshot_counter_1;
*v2 = 0;
v5 = (const char *)*((_QWORD *)&app_data + 14);
scshot_counter_1 = v4 + 1;
sprintf(name_0, "%s%02d%s", v5, v4, v2 + 1);
*v3 = 64;
}
else
{
strcpy(name_0, v0);
}
get_palette(v10);
if ( gfx_driver )
{
v6 = *(unsigned int *)(gfx_driver + 220LL);
v7 = *(unsigned int *)(gfx_driver + 216LL);
}
else
{
v6 = 0LL;
v7 = 0LL;
}
v8 = create_sub_bitmap(screen, 0LL, 0LL, v7, v6);
save_bitmap(name_0, v8, v10);
destroy_bitmap(v8);
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
}
while ( key[*(&syskeys + 4)] );
}
}
result = key[119];
if ( (_BYTE)result )
{
result = key[67];
if ( (_BYTE)result )
{
*((_DWORD *)&app_data + 10) = *((_DWORD *)&app_data + 10) == 0;
((void (*)(void))grmode)();
do
{
rest(5LL);
if ( NeedsPoll )
poll_keyboard();
result = key[67];
}
while ( (_BYTE)result );
}
}
return result;
}

original_code:
void handle_key(void){
    if (NeedsPoll) poll_keyboard();
    if (key[syskeys[0]] || key[__allegro_KEY_ESC]) {
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[0]] || key[__allegro_KEY_ESC]);
        key_done=1;
    }
    if (key[syskeys[1]]) {
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[1]]);
        mute_audio();
        mute_voice();
        abaut();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
            if (key[__allegro_KEY_ALT] && key[__allegro_KEY_ENTER]) {
                app_data.fullscreen = app_data.fullscreen ? 0 : 1;
                grmode();
                abaut();
                do {
                    rest(5);
                    if (NeedsPoll) poll_keyboard();
                } while (key[__allegro_KEY_ENTER]);
            }
        } while ((!key[syskeys[1]]) && (!key[__allegro_KEY_ESC]) && (!key[syskeys[0]]));
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[1]]);
        init_sound_stream();
    }
    if (key[syskeys[5]]) {
        if (savestate(app_data.statefile)==0) {
            display_msg("Savefile saved.",5);
        }
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[5]]);
    }
    if (key[syskeys[6]]) {
        int stateError;
        if ((stateError=loadstate(app_data.statefile))==0) {
            display_msg("Savefile loaded.",5);
        } else if (stateError>=199) {
            if (stateError==199) display_msg("Wrong ROM-File for Savefile.",5);
            else if (stateError==200+1) display_msg("Wrong BIOS for Savefile: O2ROM needed.",5);
            else if (stateError==200+2) display_msg("Wrong BIOS for Savefile: G7400 ROM needed.",5);
            else if (stateError==200+3) display_msg("Wrong BIOS for Savefile: C52 ROM needed.",5);
            else if (stateError==200+4) display_msg("Wrong BIOS for Savefile: JOPAC ROM needed.",5);
            else display_msg("Wrong BIOS for Savefile: UNKNOWN ROM needed.",5);
        }
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[6]]);
    }
    if (key[syskeys[2]]) key_debug=1;
    if (key[syskeys[3]]) {
        init_cpu();
        init_roms();
        init_vpp();
        clearscr();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[3]]);
    }
    if (key[syskeys[7]]) {
        set_score(app_data.scoretype, app_data.scoreaddress, app_data.default_highscore);
    }
    if (key[syskeys[4]]) {
        BITMAP *bmp;
        PALETTE pal;
        char *p;
        static char name[1024];
        static int scshot_counter = 0;
        if (strlen(app_data.scshot)>0){
            if ((p=strchr(app_data.scshot,'@'))) {
                *p = 0;
                sprintf(name, "%s%02d%s", app_data.scshot, scshot_counter++, p+1);
                *p = '@';
            } else {
                strcpy(name, app_data.scshot);
            }
            get_palette(pal);
            bmp = create_sub_bitmap(screen, 0, 0, (gfx_driver ? gfx_driver->w : 0), (gfx_driver ? gfx_driver->h : 0));
            save_bitmap(name, bmp, pal);
            destroy_bitmap(bmp);
            do {
                rest(5);
                if (NeedsPoll) poll_keyboard();
            } while (key[syskeys[4]]);
        }
    }
    if (key[__allegro_KEY_ALT] && key[__allegro_KEY_ENTER]) {
        app_data.fullscreen = app_data.fullscreen ? 0 : 1;
        grmode();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[__allegro_KEY_ENTER]);
    }
}

canonical_original_code:
void handle_key(){
    if (NeedsPoll) poll_keyboard();
    if (key[syskeys[0]] || key[__allegro_KEY_ESC]) {
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[0]] || key[__allegro_KEY_ESC]);
        key_done=1;
    }
    if (key[syskeys[1]]) {
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[1]]);
        mute_audio();
        mute_voice();
        abaut();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
            if (key[__allegro_KEY_ALT] && key[__allegro_KEY_ENTER]) {
                app_data.fullscreen = app_data.fullscreen ? 0 : 1;
                grmode();
                abaut();
                do {
                    rest(5);
                    if (NeedsPoll) poll_keyboard();
                } while (key[__allegro_KEY_ENTER]);
            }
        } while ((!key[syskeys[1]]) && (!key[__allegro_KEY_ESC]) && (!key[syskeys[0]]));
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[1]]);
        init_sound_stream();
    }
    if (key[syskeys[5]]) {
        if (savestate(app_data.statefile)==0) {
            display_msg("Savefile saved.",5);
        }
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[5]]);
    }
    if (key[syskeys[6]]) {
        int stateError;
        if ((stateError=loadstate(app_data.statefile))==0) {
            display_msg("Savefile loaded.",5);
        } else if (stateError>=199) {
            if (stateError==199) display_msg("Wrong ROM-File for Savefile.",5);
            else if (stateError==200+1) display_msg("Wrong BIOS for Savefile: O2ROM needed.",5);
            else if (stateError==200+2) display_msg("Wrong BIOS for Savefile: G7400 ROM needed.",5);
            else if (stateError==200+3) display_msg("Wrong BIOS for Savefile: C52 ROM needed.",5);
            else if (stateError==200+4) display_msg("Wrong BIOS for Savefile: JOPAC ROM needed.",5);
            else display_msg("Wrong BIOS for Savefile: UNKNOWN ROM needed.",5);
        }
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[6]]);
    }
    if (key[syskeys[2]]) key_debug=1;
    if (key[syskeys[3]]) {
        init_cpu();
        init_roms();
        init_vpp();
        clearscr();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[syskeys[3]]);
    }
    if (key[syskeys[7]]) {
        set_score(app_data.scoretype, app_data.scoreaddress, app_data.default_highscore);
    }
    if (key[syskeys[4]]) {
        struct BITMAP *bmp;
        struct RGB pal[256];
        char *p;
        static char name[1024];
        static int scshot_counter = 0;
        if (strlen(app_data.scshot)>0){
            if ((p=strchr(app_data.scshot,'@'))) {
                *p = 0;
                sprintf(name, "%s%02d%s", app_data.scshot, scshot_counter++, p+1);
                *p = '@';
            } else {
                strcpy(name, app_data.scshot);
            }
            get_palette(pal);
            bmp = create_sub_bitmap(screen, 0, 0, (gfx_driver ? gfx_driver->w : 0), (gfx_driver ? gfx_driver->h : 0));
            save_bitmap(name, bmp, pal);
            destroy_bitmap(bmp);
            do {
                rest(5);
                if (NeedsPoll) poll_keyboard();
            } while (key[syskeys[4]]);
        }
    }
    if (key[__allegro_KEY_ALT] && key[__allegro_KEY_ENTER]) {
        app_data.fullscreen = app_data.fullscreen ? 0 : 1;
        grmode();
        do {
            rest(5);
            if (NeedsPoll) poll_keyboard();
        } while (key[__allegro_KEY_ENTER]);
    }
}

variable_types: stateError int

variable_types: struct BITMAP { int w; int clip; int cl; struct GFX_VTABLE { int color_depth; int mask_color; void *unwrite_bank; void (*set_clip)(struct BITMAP *bmp); void (*acquire)(struct BITMAP *bmp); void (*release)(struct BITMAP *bmp); struct BITMAP *(*create_sub_bitmap)(struct BITMAP *parent, int x, int y, int width, int height); void (*created_sub_bitmap)(struct BITMAP *bmp, struct BITMAP *parent); int (*getpixel)(struct BITMAP *bmp, int x, int y); void (*putpixel)(struct BITMAP *bmp, int x, int y, int color); void (*vline)(struct BITMAP *bmp, int x, int y_1, int y2, int color); void (*hline)(struct BITMAP *bmp, int x1, int y, int x2, int color); void (*hfill)(struct BITMAP *bmp, int x1, int y, int x2, int color); void (*line)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*fastline)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*rectfill)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*triangle)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int x3, int y3, int color); void (*draw_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_256_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_v_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_h_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_vh_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_trans_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_trans_rgba_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_lit_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int color); void (*draw_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE { int w; int color_depth; int size; char dat[]; } *sprite, int x, int y); void (*draw_trans_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y); void (*draw_trans_rgba_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y); void (*draw_lit_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y, int color); void (*draw_character)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int color, int bg); void (*draw_glyph)(struct BITMAP *bmp, struct FONT_GLYPH { short w; unsigned char dat[]; } *glyph, int x, int y, int color, int bg); void (*blit_from_memory)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_memory)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_from_system)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_system)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self_forward)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self_backward)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_between_formats)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*masked_blit)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*clear_to_color)(struct BITMAP *bitmap, int color); void (*pivot_scaled_sprite_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int cx, int cy, int angle, int scale, int v_flip); void (*do_stretch_blit)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int source_width, int source_height, int dest_x, int dest_y, int dest_width, int dest_height, int masked); void (*draw_gouraud_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int c1, int c2, int c3, int c4); void (*draw_sprite_end)(); void (*blit_end)(); void (*polygon)(struct BITMAP *bmp, int vertices, int *points, int color); void (*rect)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*circle)(struct BITMAP *bmp, int x, int y, int radius, int color); void (*circlefill)(struct BITMAP *bmp, int x, int y, int radius, int color); void (*ellipse)(struct BITMAP *bmp, int x, int y, int rx, int ry, int color); void (*ellipsefill)(struct BITMAP *bmp, int x, int y, int rx, int ry, int color); void (*arc)(struct BITMAP *bmp, int x, int y, int ang1, int ang2, int r, int color); void (*spline)(struct BITMAP *bmp, int points[8], int color); void (*floodfill)(struct BITMAP *bmp, int x, int y, int color); void (*polygon3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, int vc, struct V3D { int x; int u; int c; } *vtx[]); void (*polygon3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, int vc, struct V3D_f { float x; float u; int c; } *vtx[]); void (*triangle3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D *v1, struct V3D *v2, struct V3D *v3); void (*triangle3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D_f *v1, struct V3D_f *v2, struct V3D_f *v3); void (*quad3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D *v1, struct V3D *v2, struct V3D *v3, struct V3D *v4); void (*quad3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D_f *v1, struct V3D_f *v2, struct V3D_f *v3, struct V3D_f *v4); void (*draw_sprite_ex)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int mode, int flip); } *vtable; void *write_bank; void *read_bank; void *dat; unsigned long id; void *extra; int x_ofs; int y_ofs; int seg; unsigned char *line[]; } *

variable_types: struct RGB { unsigned char r; unsigned char filler; } [256]

variable_types:
p
char *

user_defined_types: struct BITMAP { int w; int clip; int cl; struct GFX_VTABLE *vtable; void *write_bank; void *read_bank; void *dat; unsigned long id; void *extra; int x_ofs; int y_ofs; int seg; unsigned char *line[]; }

user_defined_types: struct GFX_VTABLE { int color_depth; int mask_color; void *unwrite_bank; void (*set_clip)(struct BITMAP *bmp); void (*acquire)(struct BITMAP *bmp); void (*release)(struct BITMAP *bmp); struct BITMAP *(*create_sub_bitmap)(struct BITMAP *parent, int x, int y, int width, int height); void (*created_sub_bitmap)(struct BITMAP *bmp, struct BITMAP *parent); int (*getpixel)(struct BITMAP *bmp, int x, int y); void (*putpixel)(struct BITMAP *bmp, int x, int y, int color); void (*vline)(struct BITMAP *bmp, int x, int y_1, int y2, int color); void (*hline)(struct BITMAP *bmp, int x1, int y, int x2, int color); void (*hfill)(struct BITMAP *bmp, int x1, int y, int x2, int color); void (*line)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*fastline)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*rectfill)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*triangle)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int x3, int y3, int color); void (*draw_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_256_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_v_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_h_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_sprite_vh_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_trans_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_trans_rgba_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y); void (*draw_lit_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int color); void (*draw_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y); void (*draw_trans_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y); void (*draw_trans_rgba_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y); void (*draw_lit_rle_sprite)(struct BITMAP *bmp, struct RLE_SPRITE *sprite, int x, int y, int color); void (*draw_character)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int color, int bg); void (*draw_glyph)(struct BITMAP *bmp, struct FONT_GLYPH *glyph, int x, int y, int color, int bg); void (*blit_from_memory)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_memory)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_from_system)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_system)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self_forward)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_to_self_backward)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*blit_between_formats)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*masked_blit)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height); void (*clear_to_color)(struct BITMAP *bitmap, int color); void (*pivot_scaled_sprite_flip)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int cx, int cy, int angle, int scale, int v_flip); void (*do_stretch_blit)(struct BITMAP *source, struct BITMAP *dest, int source_x, int source_y, int source_width, int source_height, int dest_x, int dest_y, int dest_width, int dest_height, int masked); void (*draw_gouraud_sprite)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int c1, int c2, int c3, int c4); void (*draw_sprite_end)(); void (*blit_end)(); void (*polygon)(struct BITMAP *bmp, int vertices, int *points, int color); void (*rect)(struct BITMAP *bmp, int x1, int y_1, int x2, int y2, int color); void (*circle)(struct BITMAP *bmp, int x, int y, int radius, int color); void (*circlefill)(struct BITMAP *bmp, int x, int y, int radius, int color); void (*ellipse)(struct BITMAP *bmp, int x, int y, int rx, int ry, int color); void (*ellipsefill)(struct BITMAP *bmp, int x, int y, int rx, int ry, int color); void (*arc)(struct BITMAP *bmp, int x, int y, int ang1, int ang2, int r, int color); void (*spline)(struct BITMAP *bmp, int points[8], int color); void (*floodfill)(struct BITMAP *bmp, int x, int y, int color); void (*polygon3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, int vc, struct V3D *vtx[]); void (*polygon3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, int vc, struct V3D_f *vtx[]); void (*triangle3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D *v1, struct V3D *v2, struct V3D *v3); void (*triangle3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D_f *v1, struct V3D_f *v2, struct V3D_f *v3); void (*quad3d)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D *v1, struct V3D *v2, struct V3D *v3, struct V3D *v4); void (*quad3d_f)(struct BITMAP *bmp, int type, struct BITMAP *texture, struct V3D_f *v1, struct V3D_f *v2, struct V3D_f *v3, struct V3D_f *v4); void (*draw_sprite_ex)(struct BITMAP *bmp, struct BITMAP *sprite, int x, int y, int mode, int flip); }


keyjoy

decompiled_code:
char __fastcall keyjoy(unsigned int a1)
{
char result; // al

result = -1;
if ( a1 <= 1 )
{
if ( NeedsPoll )
poll_keyboard();
result = (key[joykeys[5 * a1]] == 0) - 2;
if ( key[joykeys[5 * a1 + 1]] )
result &= 0xFBu;
if ( key[joykeys[5 * a1 + 2]] )
result &= 0xF7u;
if ( key[joykeys[5 * a1 + 3]] )
result &= 0xFDu;
if ( key[joykeys[5 * a1 + 4]] )
result &= 0xEFu;
}
return result;
}

canonical_decompiled_code:
char __fastcall func5(unsigned int a1)
{
char result; // al

result = -1;
if ( a1 <= 1 )
{
if ( NeedsPoll )
poll_keyboard();
result = (key[joykeys[5 * a1]] == 0) - 2;
if ( key[joykeys[5 * a1 + 1]] )
result &= 0xFBu;
if ( key[joykeys[5 * a1 + 2]] )
result &= 0xF7u;
if ( key[joykeys[5 * a1 + 3]] )
result &= 0xFDu;
if ( key[joykeys[5 * a1 + 4]] )
result &= 0xEFu;
}
return result;
}

original_code:
Byte keyjoy(int jn){
    Byte d;
    d=0xFF;
    if ((jn>=0) && (jn<=1)){
        if (NeedsPoll) poll_keyboard();
        if (key[joykeys[jn][0]]) d &= 0xFE;
        if (key[joykeys[jn][1]]) d &= 0xFB;
        if (key[joykeys[jn][2]]) d &= 0xF7;
        if (key[joykeys[jn][3]]) d &= 0xFD;
        if (key[joykeys[jn][4]]) d &= 0xEF;
    }
    return d;
}

canonical_original_code:
unsigned char keyjoy(int jn){
    unsigned char d;
    d=0xFF;
    if ((jn>=0) && (jn<=1)){
        if (NeedsPoll) poll_keyboard();
        if (key[joykeys[jn][0]]) d &= 0xFE;
        if (key[joykeys[jn][1]]) d &= 0xFB;
        if (key[joykeys[jn][2]]) d &= 0xF7;
        if (key[joykeys[jn][3]]) d &= 0xFD;
        if (key[joykeys[jn][4]]) d &= 0xEF;
    }
    return d;
}

variable_types: jn int

variable_types:
d
unsigned char

return_type: unsigned char


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

key_done = 0;
key_debug = 0;
install_keyboard();
new_int = 1;
result = keyboard_needs_poll();
NeedsPoll = result;
return result;
}

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

key_done = 0;
key_debug = 0;
install_keyboard();
new_int = 1;
result = keyboard_needs_poll();
NeedsPoll = result;
return result;
}

original_code:
void init_keyboard(void){
    key_done=0;
    key_debug=0;
    install_keyboard();
    new_int=1;
    NeedsPoll = keyboard_needs_poll();
}

canonical_original_code:
void init_keyboard(){
    key_done=0;
    key_debug=0;
    install_keyboard();
    new_int=1;
    NeedsPoll = keyboard_needs_poll();
}


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

result = remove_keyboard();
new_int = 0;
return result;
}

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

result = remove_keyboard();
new_int = 0;
return result;
}

original_code:
void Set_Old_Int9(void){
   remove_keyboard();
   new_int=0;
}

canonical_original_code:
void Set_Old_Int9(){
   remove_keyboard();
   new_int=0;
}


decompiled_code:
__int64 __fastcall get_score(signed int a1, int a2)
{
__int64 result; // rax
float v4; // xmm0_4
int v5; // esi
int v6; // edx
float v7; // xmm1_4
float v8; // xmm0_4
unsigned __int8 *v9; // r9
int v10; // er10
float v11; // xmm2_4
int v12; // edx
char v13; // r8
float v14; // xmm0_4

result = (unsigned int)a1;
if ( a1 )
{
v4 = 0.0;
v5 = a1 % 10;
v6 = a1 / 10 % 10;
v7 = (float)(10 * (a1 / 100 / 10) - a1 / 100 + 3) * 0.5;
if ( v6 != 1 )
{
v6 = -1;
v4 = (float)((float)v5 * v7) - 1.0;
}
v8 = v4 + (float)a2;
v9 = (unsigned __int8 *)&extRAM;
if ( (unsigned int)(a1 - 1000) >= 0x3E8 )
v9 = (unsigned __int8 *)&intRAM;
v10 = (int)v8;
if ( v5 <= 0 )
{
result = 0LL;
}
else
{
v11 = (float)v6;
v12 = 0;
v13 = abs32((int)(float)((float)(v7 - 1.0) + (float)(v7 - 1.0)));
LODWORD(result) = 0;
do
{
v14 = (float)v12++;
result = (((int)v9[v10 + (int)(float)((float)(v14 * v7) * v11)] >> (4 * v13 * (v12 & 1))) & 0xFu)
+ 10 * (_DWORD)result;
}
while ( v12 != v5 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(signed int a1, int a2)
{
__int64 result; // rax
float v4; // xmm0_4
int v5; // esi
int v6; // edx
float v7; // xmm1_4
float v8; // xmm0_4
unsigned __int8 *v9; // r9
int v10; // er10
float v11; // xmm2_4
int v12; // edx
char v13; // r8
float v14; // xmm0_4

result = (unsigned int)a1;
if ( a1 )
{
v4 = 0.0;
v5 = a1 % 10;
v6 = a1 / 10 % 10;
v7 = (float)(10 * (a1 / 100 / 10) - a1 / 100 + 3) * 0.5;
if ( v6 != 1 )
{
v6 = -1;
v4 = (float)((float)v5 * v7) - 1.0;
}
v8 = v4 + (float)a2;
v9 = (unsigned __int8 *)&extRAM;
if ( (unsigned int)(a1 - 1000) >= 0x3E8 )
v9 = (unsigned __int8 *)&intRAM;
v10 = (int)v8;
if ( v5 <= 0 )
{
result = 0LL;
}
else
{
v11 = (float)v6;
v12 = 0;
v13 = abs32((int)(float)((float)(v7 - 1.0) + (float)(v7 - 1.0)));
LODWORD(result) = 0;
do
{
v14 = (float)v12++;
result = (((int)v9[v10 + (int)(float)((float)(v14 * v7) * v11)] >> (4 * v13 * (v12 & 1))) & 0xFu)
+ 10 * (_DWORD)result;
}
while ( v12 != v5 );
}
}
return result;
}

original_code:
int get_score(int scoretype, int scoreaddress)
{
 int score=0;
 if (scoretype!=0)
 {
  int position;
  int i;
  Byte *RAM;
  int count = scoretype%10;
  int direction = ((scoretype/10)%10)==1?1:-1;
  float valuetype = (float) (3-((scoretype/100)%10))/2;
  int ramtype = scoretype/1000;
  position = scoreaddress+ (direction==1?0:(count*valuetype-1));
  RAM = ramtype==1?extRAM:intRAM;
  for(i=0;i<count;i++)
  {
   score = score*10+((RAM[position+(int)(valuetype*i*direction)]>>(((i+1)%2)*4)*(abs((int) ((valuetype-1)*2))))&15);
  }
 }
 return(score);
}

canonical_original_code:
int get_score(int scoretype, int scoreaddress)
{
 int score = 0;
 if (scoretype!=0)
 {
  int position;
  int i;
  unsigned char *RAM;
  int count = scoretype%10;
  int direction = ((scoretype/10)%10)==1?1:-1;
  float valuetype = (float) (3-((scoretype/100)%10))/2;
  int ramtype = scoretype/1000;
  position = scoreaddress+ (direction==1?0:(count*valuetype-1));
  RAM = ramtype==1?extRAM:intRAM;
  for(i=0;i<count;i++)
  {
   score = score*10+((RAM[position+(int)(valuetype*i*direction)]>>(((i+1)%2)*4)*(abs((int) ((valuetype-1)*2))))&15);
  }
 }
 return(score);
}

variable_types: scoretype int

variable_types: scoreaddress int

variable_types: score int

variable_types: position int

variable_types:
RAM
unsigned char *

variable_types: count int

variable_types: direction int

variable_types: valuetype float

variable_types: ramtype int


power

decompiled_code:
__int64 __fastcall power(unsigned int a1, int a2)
{
unsigned int v2; // er8
int v3; // esi
int v4; // eax

v2 = 1;
if ( a2 )
{
if ( a2 <= 1 )
{
v2 = a1;
}
else
{
v3 = a2 + 1;
v2 = a1;
v4 = 2;
do
{
++v4;
v2 *= a1;
}
while ( v4 != v3 );
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, int a2)
{
unsigned int v2; // er8
int v3; // esi
int v4; // eax

v2 = 1;
if ( a2 )
{
if ( a2 <= 1 )
{
v2 = a1;
}
else
{
v3 = a2 + 1;
v2 = a1;
v4 = 2;
do
{
++v4;
v2 *= a1;
}
while ( v4 != v3 );
}
}
return v2;
}

original_code:
int power(int base, int higher)
{
 if (higher==0) {
  return(1);
 } else if (higher==1) {
  return(base);
 } else {
  int i;
  int value=base;
  for (i=2;i<=higher;i++)
  {
   value = value*base;
  }
  return(value);
 }
}

canonical_original_code:
int power(int base, int higher)
{
 if (higher==0) {
  return(1);
 } else if (higher==1) {
  return(base);
 } else {
  int i;
  int value = base;
  for (i=2;i<=higher;i++)
  {
   value = value*base;
  }
  return(value);
 }
}

variable_types: base int

variable_types: higher int

variable_types: i int

variable_types: value int


original_code:
void set_score(int scoretype, int scoreaddress, int score)
{
 if (scoretype!=0 && score>0)
 {
  int position;
  int i;
  Byte *RAM;
  int digit;
  int count = scoretype%10;
  int direction = ((scoretype/10)%10)==1?-1:1;
  float valuetype = (float) (3-((scoretype/100)%10))/2;
  int ramtype = scoretype/1000;
  position = scoreaddress+ (direction==1?0:(count*valuetype-1));
  RAM = ramtype==1?extRAM:intRAM;
  for(i=count-1;i>=0;i--)
  {
   digit = score / power(10,i);
   RAM[position+(int)(valuetype*i*direction)]=((valuetype==0.5)&&(i%2==0))?(RAM[position+(int)(valuetype*i*direction)]<<4)+digit:digit;
   score = score - digit*power(10,i);
  }
 }
}

canonical_original_code:
void set_score(int scoretype, int scoreaddress, int score)
{
 if (scoretype!=0 && score>0)
 {
  int position;
  int i;
  unsigned char *RAM;
  int digit;
  int count = scoretype%10;
  int direction = ((scoretype/10)%10)==1?-1:1;
  float valuetype = (float) (3-((scoretype/100)%10))/2;
  int ramtype = scoretype/1000;
  position = scoreaddress+ (direction==1?0:(count*valuetype-1));
  RAM = ramtype==1?extRAM:intRAM;
  for(i=count-1;i>=0;i--)
  {
   digit = score / power(10,i);
   RAM[position+(int)(valuetype*i*direction)]=((valuetype==0.5)&&(i%2==0))?(RAM[position+(int)(valuetype*i*direction)]<<4)+digit:digit;
   score = score - digit*power(10,i);
  }
 }
}

variable_types: digit int


original_code:
void audio_process(unsigned char *buffer){
 unsigned long aud_data;
 int volume, re_circ, noise, enabled, intena, period, pnt, cnt, rndbit, pos;
 aud_data = (VDCwrite[0xA9] | (VDCwrite[0xA8] << 8) | (VDCwrite[0xA7] << 16));
 intena = VDCwrite[0xA0] & 0x04;
 pnt = cnt = 0;
 noise = VDCwrite[0xAA] & 0x10;
 enabled = VDCwrite[0xAA] & 0x80;
 rndbit = (enabled && noise) ? (rand()%2) : 0;
 while (pnt < 1056) {
  pos = (tweakedaudio) ? (pnt/3) : (500 -1);
  volume = AudioVector[pos] & 0x0F;
  enabled = AudioVector[pos] & 0x80;
  period = (AudioVector[pos] & 0x20) ? 11 : 44;
  re_circ = AudioVector[pos] & 0x40;
  buffer[pnt++] = (enabled) ? ((aud_data & 0x01)^rndbit) * (0x10 * volume) : 0;
  cnt++;
  if (cnt >= period) {
   cnt=0;
   aud_data = (re_circ) ? ((aud_data >> 1) | ((aud_data & 1) << 23)) : (aud_data >> 1);
   rndbit = (enabled && noise) ? (rand()%2) : 0;
   if (enabled && intena && (!sound_IRQ)) {
    sound_IRQ = 1;
    ext_IRQ();
   }
  }
 }
 if (app_data.filter) filter(buffer, 1056);
}

canonical_original_code:
void audio_process(unsigned char *buffer){
 unsigned long aud_data;
 int volume; int re_circ; int noise; int enabled; int intena; int period; int pnt; int cnt; int rndbit; int pos;
 aud_data = (VDCwrite[0xA9] | (VDCwrite[0xA8] << 8) | (VDCwrite[0xA7] << 16));
 intena = VDCwrite[0xA0] & 0x04;
 pnt = cnt = 0;
 noise = VDCwrite[0xAA] & 0x10;
 enabled = VDCwrite[0xAA] & 0x80;
 rndbit = (enabled && noise) ? (rand()%2) : 0;
 while (pnt < 1056) {
  pos = (tweakedaudio) ? (pnt/3) : (500 -1);
  volume = AudioVector[pos] & 0x0F;
  enabled = AudioVector[pos] & 0x80;
  period = (AudioVector[pos] & 0x20) ? 11 : 44;
  re_circ = AudioVector[pos] & 0x40;
  buffer[pnt++] = (enabled) ? ((aud_data & 0x01)^rndbit) * (0x10 * volume) : 0;
  cnt++;
  if (cnt >= period) {
   cnt=0;
   aud_data = (re_circ) ? ((aud_data >> 1) | ((aud_data & 1) << 23)) : (aud_data >> 1);
   rndbit = (enabled && noise) ? (rand()%2) : 0;
   if (enabled && intena && (!sound_IRQ)) {
    sound_IRQ = 1;
    ext_IRQ();
   }
  }
 }
 if (app_data.filter) filter(buffer, 1056);
}

variable_types:
buffer
unsigned char *

variable_types: volume int

variable_types: noise int

variable_types: enabled int

variable_types: intena int

variable_types: period int

variable_types: pnt int

variable_types: cnt int

variable_types: rndbit int

variable_types: pos int


decompiled_code:
__int64 update_audio()
{
__int64 result; // rax
const void *v1; // rbp

result = app_data[7];
if ( (_DWORD)result )
{
result = get_audio_stream_buffer(stream);
v1 = (const void *)result;
if ( result )
{
audio_process((_BYTE *)result);
if ( sndlog )
fwrite(v1, 1uLL, 0x420uLL, sndlog);
result = free_audio_stream_buffer(stream);
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
const void *v1; // rbp

result = app_data[7];
if ( (_DWORD)result )
{
result = get_audio_stream_buffer(stream);
v1 = (const void *)result;
if ( result )
{
func1((_BYTE *)result);
if ( sndlog )
fwrite(v1, 1uLL, 0x420uLL, sndlog);
result = free_audio_stream_buffer(stream);
}
}
return result;
}

original_code:
void update_audio(void) {
 unsigned char *p;
 if (app_data.sound_en) {
  p = (unsigned char *)get_audio_stream_buffer(stream);
  if (p) {
   audio_process(p);
   if (sndlog) fwrite(p,1,1056,sndlog);
   free_audio_stream_buffer(stream);
  }
    }
}

canonical_original_code:
void update_audio() {
 unsigned char *p;
 if (app_data.sound_en) {
  p = (unsigned char *)get_audio_stream_buffer(stream);
  if (p) {
   audio_process(p);
   if (sndlog) fwrite(p,1,1056,sndlog);
   free_audio_stream_buffer(stream);
  }
    }
}

variable_types:
p
unsigned char *


decompiled_code:
void init_sound_stream()
{
signed int v0; // eax
__int64 v1; // r8
__int64 v2; // r8

if ( app_data[7] )
{
v0 = 255 * app_data[13];
v1 = 1374389535LL * v0;
if ( app_data[17] )
v2 = v1 >> 37;
else
v2 = v1 >> 38;
stream = play_audio_stream(1056LL, 8LL, 0LL, 44100LL, (unsigned int)(v2 - (v0 >> 31)), 128LL);
if ( !stream )
{
puts("Error creating audio stream!");
app_data[7] = 0;
}
flt_b = 0LL;
flt_a = 0LL;
flt_prv = 0;
}
}

canonical_decompiled_code:
void func3()
{
signed int v0; // eax
__int64 v1; // r8
__int64 v2; // r8

if ( app_data[7] )
{
v0 = 255 * app_data[13];
v1 = 1374389535LL * v0;
if ( app_data[17] )
v2 = v1 >> 37;
else
v2 = v1 >> 38;
stream = play_audio_stream(1056LL, 8LL, 0LL, 44100LL, (unsigned int)(v2 - (v0 >> 31)), 128LL);
if ( !stream )
{
puts("Error creating audio stream!");
app_data[7] = 0;
}
flt_b = 0LL;
flt_a = 0LL;
flt_prv = 0;
}
}

original_code:
void init_sound_stream(void){
 int vol;
 if (app_data.sound_en){
  if (app_data.filter)
   vol = (255*app_data.svolume)/100;
  else
   vol = (255*app_data.svolume)/200;
  stream = play_audio_stream(1056,8,0,44100,vol,128);
  if (!stream) {
   printf("Error creating audio stream!\n");
   app_data.sound_en=0;
  }
  flt_a = flt_b = 0.0;
  flt_prv = 0;
 }
}

canonical_original_code:
void init_sound_stream(){
 int vol;
 if (app_data.sound_en){
  if (app_data.filter)
   vol = (255*app_data.svolume)/100;
  else
   vol = (255*app_data.svolume)/200;
  stream = play_audio_stream(1056,8,0,44100,vol,128);
  if (!stream) {
   printf("Error creating audio stream!\n");
   app_data.sound_en=0;
  }
  flt_a = flt_b = 0.0;
  flt_prv = 0;
 }
}

variable_types: vol int


decompiled_code:
void init_audio()
{
_BYTE *v0; // rax

sound_IRQ = 0;
if ( app_data[12] | app_data[7] )
{
puts("Initializing sound system...");
if ( (unsigned int)install_sound(0xFFFFFFFFLL, 0LL, 0LL) )
{
printf(" ERROR: could not initialize sound card\n %s\n", (const char *)&allegro_error);
puts(" Sound system disabled");
app_data[7] = 0;
app_data[12] = 0;
sndlog = 0LL;
}
else
{
v0 = *(_BYTE **)(digi_driver + 8LL);
if ( v0 && *v0 )
{
puts(" Sound system initialized ok");
printf(" Sound driver [%s] detected\n", *(const char **)(digi_driver + 8LL));
set_volume(0xFFFFFFFFLL, 0xFFFFFFFFLL);
init_sound_stream();
sndlog = 0LL;
}
else
{
puts(" ERROR: could not initialize sound card");
puts(" Sound system disabled");
app_data[7] = 0;
app_data[12] = 0;
sndlog = 0LL;
}
}
}
else
{
sndlog = 0LL;
}
}

canonical_decompiled_code:
void func4()
{
_BYTE *v0; // rax

sound_IRQ = 0;
if ( app_data[12] | app_data[7] )
{
puts("Initializing sound system...");
if ( (unsigned int)install_sound(0xFFFFFFFFLL, 0LL, 0LL) )
{
printf(" ERROR: could not initialize sound card\n %s\n", (const char *)&allegro_error);
puts(" Sound system disabled");
app_data[7] = 0;
app_data[12] = 0;
sndlog = 0LL;
}
else
{
v0 = *(_BYTE **)(digi_driver + 8LL);
if ( v0 && *v0 )
{
puts(" Sound system initialized ok");
printf(" Sound driver [%s] detected\n", *(const char **)(digi_driver + 8LL));
set_volume(0xFFFFFFFFLL, 0xFFFFFFFFLL);
func3();
sndlog = 0LL;
}
else
{
puts(" ERROR: could not initialize sound card");
puts(" Sound system disabled");
app_data[7] = 0;
app_data[12] = 0;
sndlog = 0LL;
}
}
}
else
{
sndlog = 0LL;
}
}

original_code:
void init_audio(void) {
 int i;
 sound_IRQ=0;
 if ((app_data.sound_en) || (app_data.voice)) {
  printf("Initializing sound system...\n");
  i = install_sound(-1, 0, ((void *)0));
  if (i != 0) {
     printf("  ERROR: could not initialize sound card\n   %s\n",allegro_error);
     printf("  Sound system disabled\n");
     app_data.sound_en = 0;
     app_data.voice = 0;
  }
  else {
   if (digi_driver->name && (strlen(digi_driver->name)>0)){
    printf("  Sound system initialized ok\n");
    printf("  Sound driver [%s] detected\n",digi_driver->name);
    set_volume(-1,-1);
    init_sound_stream();
   } else {
    printf("  ERROR: could not initialize sound card\n");
    printf("  Sound system disabled\n");
    app_data.sound_en = 0;
    app_data.voice = 0;
   }
  }
 }
 sndlog = ((void *)0);
}

canonical_original_code:
void init_audio() {
 int i;
 sound_IRQ=0;
 if ((app_data.sound_en) || (app_data.voice)) {
  printf("Initializing sound system...\n");
  i = install_sound(-1, 0, ((void *)0));
  if (i != 0) {
     printf("  ERROR: could not initialize sound card\n   %s\n",allegro_error);
     printf("  Sound system disabled\n");
     app_data.sound_en = 0;
     app_data.voice = 0;
  }
  else {
   if (digi_driver->name && (strlen(digi_driver->name)>0)){
    printf("  Sound system initialized ok\n");
    printf("  Sound driver [%s] detected\n",digi_driver->name);
    set_volume(-1,-1);
    init_sound_stream();
   } else {
    printf("  ERROR: could not initialize sound card\n");
    printf("  Sound system disabled\n");
    app_data.sound_en = 0;
    app_data.voice = 0;
   }
  }
 }
 sndlog = ((void *)0);
}


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

result = app_data[7];
if ( (_DWORD)result )
{
if ( stream )
{
result = stop_audio_stream();
stream = 0LL;
}
}
return result;
}

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

result = app_data[7];
if ( (_DWORD)result )
{
if ( stream )
{
result = stop_audio_stream();
stream = 0LL;
}
}
return result;
}

original_code:
void mute_audio(void){
 if (app_data.sound_en && stream){
  stop_audio_stream(stream);
  stream=((void *)0);
 }
}

canonical_original_code:
void mute_audio(){
 if (app_data.sound_en && stream){
  stop_audio_stream(stream);
  stream=((void *)0);
 }
}


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

result = app_data[7];
if ( result && stream )
result = stop_audio_stream();
if ( sndlog )
result = fclose(sndlog);
app_data[7] = 0;
return result;
}

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

result = app_data[7];
if ( result && stream )
result = stop_audio_stream();
if ( sndlog )
result = fclose(sndlog);
app_data[7] = 0;
return result;
}

original_code:
void close_audio(void) {
 if (app_data.sound_en && stream) {
  stop_audio_stream(stream);
 }
 if (sndlog) fclose(sndlog);
 app_data.sound_en=0;
}

canonical_original_code:
void close_audio() {
 if (app_data.sound_en && stream) {
  stop_audio_stream(stream);
 }
 if (sndlog) fclose(sndlog);
 app_data.sound_en=0;
}