oops

decompiled_code:
void __fastcall __noreturn oops(const char *a1)
{
printk("%s\n", a1);
_disable();
__halt();
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
printk("%s\n", a1);
_disable();
__halt();
}

original_code:
void oops(char *msg)
{
 printk("%s\n", msg);
 asm("cli\n\thlt\n"::);
}


repo: cloudsec/wos

original_code:
int creat_kthread(unsigned int eip)
{
        struct task_struct *tsk;
        int pid;
        pid = get_pid();
        if (pid == -1) {
                printk("Get pid failed.\n");
                return -1;
        }
        printk("pid: %d\n", pid);
        tsk = (struct task_struct *)alloc_page(2);
        if (!tsk) {
                printk("Alloc tsk page failed.\n");
                return -1;
        }
        printk("Alloc tsk page at: 0x%x\n", tsk);
        tsk->tss.prev_task_link = 0;
        tsk->tss.esp0 = tsk + (4*1024) * 2;
        tsk->tss.ss0 = 0x10;
        tsk->tss.esp1 = 0;
        tsk->tss.ss1 = 0;
        tsk->tss.esp2 = 0;
        tsk->tss.ss2 = 0;
        tsk->tss.cr3 = pg_dir;
        tsk->tss.eip = eip;
        tsk->tss.eflags = 0x200;
        tsk->tss.eax = 0;
        tsk->tss.ebx = 0;
        tsk->tss.ecx = 0;
        tsk->tss.edx = 0;
        tsk->tss.esp = (unsigned int)alloc_page(1) + (4*1024);
        tsk->tss.ebp = 0;
        tsk->tss.esi = 0;
        tsk->tss.edi = 0;
        tsk->tss.es = 0x10;
        tsk->tss.cs = 0x08;
        tsk->tss.ss = 0x10;
        tsk->tss.ds = 0x10;
        tsk->tss.fs = 0x10;
        tsk->tss.gs = 0x10;
        tsk->tss.ldt_sel = ((4 + 2*pid + 1) << 3);
        tsk->tss.io_map = 0x80000000;
        tsk->pid = pid;
        tsk->tss_sel = ((4 + 2*pid) << 3);
        tsk->ldt_sel = ((4 + 2*pid + 1) << 3);
        tsk->state = 0;
        tsk->counter = 15;
        tsk->priority = 200;
        set_tss_desc(new_gdt, (unsigned int)&(tsk->tss), 0x68, 0x89, (4 + 2*pid));
        set_ldt_desc(new_gdt, (unsigned int)&(tsk->ldt), 0x40, 0x82, (4 + 2*pid + 1));
        set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0x9a, 1);
        set_gdt_desc(tsk->ldt, 0x0, 0xffffffff, 0x92, 2);
        list_add_tail(&(tsk->list), &task_list_head);
 return 0;
}

variable_types:
eip
unsigned int

variable_types: struct task_struct { struct tss_struct { unsigned int prev_task_link; unsigned int esp0; unsigned int ss0; unsigned int esp1; unsigned int ss1; unsigned int esp2; unsigned int ss2; unsigned int cr3; unsigned int eip; unsigned int eflags; unsigned int eax; unsigned int ecx; unsigned int edx; unsigned int ebx; unsigned int esp; unsigned int ebp; unsigned int esi; unsigned int edi; unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds; unsigned int fs; unsigned int gs; unsigned int ldt_sel; unsigned int io_map; } tss; unsigned int tss_sel; unsigned int ldt_sel; struct gdt_desc { unsigned int a; unsigned int b; } ldt[3]; int pid; int state; int counter; int priority; struct list_head { struct list_head *prev; } list; } *

variable_types: pid int

return_type: int

user_defined_types: struct task_struct { struct tss_struct tss; unsigned int tss_sel; unsigned int ldt_sel; struct gdt_desc ldt[3]; int pid; int state; int counter; int priority; struct list_head list; }


decompiled_code:
__int64 do_exit()
{
unsigned int v0; // ebp
_QWORD *v1; // rdi
__int64 result; // rax
_QWORD *v3; // [rsp+10h] [rbp-8h]

v0 = *(_DWORD *)(current + 136);
printk("Pid %d exiting.\n", v0);
if ( !v0 )
oops("Init process try to exit.\n");
_disable();
v1 = (_QWORD *)task_list_head[1];
if ( v1 == task_list_head )
{
_enable();
printk("Del pid %d from task list failed.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
list_del_isra_0((__int64)v1, v3);
_enable();
printk("Del pid %d from task list ok.\n", v0);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebp
_QWORD *v1; // rdi
__int64 result; // rax
_QWORD *v3; // [rsp+10h] [rbp-8h]

v0 = *(_DWORD *)(current + 136);
printk("Pid %d exiting.\n", v0);
if ( !v0 )
oops("Init process try to exit.\n");
_disable();
v1 = (_QWORD *)task_list_head[1];
if ( v1 == task_list_head )
{
_enable();
printk("Del pid %d from task list failed.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
func0((__int64)v1, v3);
_enable();
printk("Del pid %d from task list ok.\n", v0);
result = 0LL;
}
return result;
}

original_code:
int do_exit(void)
{
        struct task_struct *tsk = (void *)0;
        struct list_head *p = (void *)0;
 int pid;
 pid = current->pid;
 printk("Pid %d exiting.\n", pid);
 if (pid == 0)
  oops("Init process try to exit.\n");
 asm("cli\n"::);
        for (p = (&task_list_head)->next; p != (&task_list_head); p = p->next) {
                tsk = ({ (struct task_struct *)((char * )p - (char *)&(((struct task_struct *)0x0)->list)); });;
                if (tsk) {
   list_del(&p);
   asm("sti\n"::);
   printk("Del pid %d from task list ok.\n", pid);
   return 0;
                }
        }
 asm("sti\n"::);
 printk("Del pid %d from task list failed.\n", pid);
 return -1;
}

canonical_original_code:
int do_exit()
{
        struct task_struct *tsk = (void *)0;
        struct list_head *p = (void *)0;
 int pid;
 pid = current->pid;
 printk("Pid %d exiting.\n", pid);
 if (pid == 0)
  oops("Init process try to exit.\n");
 asm("cli\n"::);
        for (p = (&task_list_head)->next; p != (&task_list_head); p = p->next) {
                tsk = ({ (struct task_struct *)((char * )p - (char *)&(((struct task_struct *)0x0)->list)); });;
                if (tsk) {
   list_del(&p);
   asm("sti\n"::);
   printk("Del pid %d from task list ok.\n", pid);
   return 0;
                }
        }
 asm("sti\n"::);
 printk("Del pid %d from task list failed.\n", pid);
 return -1;
}

variable_types: struct list_head { struct list_head *prev; } *

user_defined_types: struct tss_struct { unsigned int prev_task_link; unsigned int esp0; unsigned int ss0; unsigned int esp1; unsigned int ss1; unsigned int esp2; unsigned int ss2; unsigned int cr3; unsigned int eip; unsigned int eflags; unsigned int eax; unsigned int ecx; unsigned int edx; unsigned int ebx; unsigned int esp; unsigned int ebp; unsigned int esi; unsigned int edi; unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds; unsigned int fs; unsigned int gs; unsigned int ldt_sel; unsigned int io_map; }

user_defined_types: struct gdt_desc { unsigned int a; unsigned int b; }


printk

decompiled_code:
__int64 __fastcall printk(char *a1)
{
unsigned int v2; // [rsp+10h] [rbp-8h] BYREF

return vfprintf(a1, &v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
unsigned int v2; // [rsp+10h] [rbp-8h] BYREF

return func0(a1, &v2);
}

original_code:
int printk(char *format, ...)
{
 char* arg;
 (arg = (char *)&format + sizeof(format));
 return vfprintf(format, arg);
}

canonical_original_code:
int printk(char *format, ...)
{
 char *arg;
 (arg = (char *)&format + sizeof(format));
 return vfprintf(format, arg);
}

variable_types:
format
char *

variable_types:
arg
char *


repo: cmather/c-skeleton

decompiled_code:
int say_hello()
{
return printf("Hello World");
}

canonical_decompiled_code:
int func6()
{
return printf("Hello World");
}

original_code:
void say_hello()
{
  printf("Hello World");
}

return_type: void


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

if ( a1 == 30 )
return 32LL;
if ( a1 > 30 )
{
result = 64LL;
if ( a1 != 90 )
{
LABEL_8:
fprintf(stderr, "ERROR: unknown particle data format! (format = %ld)\n", a1);
return 0LL;
}
}
else
{
result = 16LL;
if ( a1 != 10 )
{
if ( a1 == 20 )
return 24LL;
goto LABEL_8;
}
}
return result;
}

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

if ( a1 == 30 )
return 32LL;
if ( a1 > 30 )
{
result = 64LL;
if ( a1 != 90 )
{
LABEL_8:
fprintf(stderr, "ERROR: unknown particle data format! (format = %ld)\n", a1);
return 0LL;
}
}
else
{
result = 16LL;
if ( a1 != 10 )
{
if ( a1 == 20 )
return 24LL;
goto LABEL_8;
}
}
return result;
}

original_code:
static inline size_t
pair_sizeof_data( const int64_t pair_format )
{
    switch ( pair_format ) {
        case PAIR_DATA_ID:
            return sizeof( PAIR_ID );
        case PAIR_DATA_SEP:
            return sizeof( PAIR_SEP );
        case PAIR_DATA_PROJ:
            return sizeof( PAIR_PROJ );
        case PAIR_DATA_ALL:
            return sizeof( PAIR_ALL );
    }
    fprintf( stderr, "ERROR: unknown particle data format!  (format = %" "l" "d" ")\n",
             pair_format );
    return 0;
}

canonical_original_code:
static inline unsigned long pair_sizeof_data(long pair_format)
{
    switch ( pair_format ) {
        case PAIR_DATA_ID:
            return sizeof( PAIR_ID );
        case PAIR_DATA_SEP:
            return sizeof( PAIR_SEP );
        case PAIR_DATA_PROJ:
            return sizeof( PAIR_PROJ );
        case PAIR_DATA_ALL:
            return sizeof( PAIR_ALL );
    }
    fprintf( stderr, "ERROR: unknown particle data format!  (format = %" "l" "d" ")\n",
             pair_format );
    return 0;
}

return_type: unsigned long


xcalloc

repo: cmccabe/consensus-fun

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

result = calloc(a1, a2);
if ( !result )
xcalloc_cold();
return result;
}

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

result = calloc(a1, a2);
if ( !result )
func1();
return result;
}

original_code:
void *xcalloc(size_t nmemb, size_t size)
{
 void *ret = calloc(nmemb, size);
 if (!ret)
  abort();
 return ret;
}

canonical_original_code:
void *xcalloc(unsigned long nmemb, unsigned long size)
{
 void *ret = calloc(nmemb, size);
 if (!ret)
  abort();
 return ret;
}

variable_types:
nmemb
unsigned long

variable_types:
size
unsigned long

variable_types:
ret
void *

return_type: void *


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1, int a2)
{
fprintf(
stderr,
"%s: a consensus protocol demo.\n"
"options:\n"
"-h: this help message\n"
"-n <num-nodes> set the number of acceptors (default: %d)\n"
"-v enable verbose debug messages\n",
a1,
5LL);
exit(a2);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1, int a2)
{
fprintf(
stderr,
"%s: a consensus protocol demo.\n"
"options:\n"
"-h: this help message\n"
"-n <num-nodes> set the number of acceptors (default: %d)\n"
"-v enable verbose debug messages\n",
a1,
5LL);
exit(a2);
}

original_code:
static void usage(const char *argv0, int retcode)
{
 fprintf(stderr, "%s: a consensus protocol demo.\noptions:\n-h:                     this help message\n-n <num-nodes>          set the number of acceptors (default: %d)\n-v                      enable verbose debug messages\n", argv0, 5);
 exit(retcode);
}

canonical_original_code:
static void usage(char *argv0, int retcode)
{
 fprintf(stderr, "%s: a consensus protocol demo.\noptions:\n-h:                     this help message\n-n <num-nodes>          set the number of acceptors (default: %d)\n-v                      enable verbose debug messages\n", argv0, 5);
 exit(retcode);
}

variable_types: retcode int


decompiled_code:
int __fastcall parse_argv(int argc, char **argv)
{
int result; // eax

g_num_nodes = 5;
g_verbose = 0;
while ( 1 )
{
result = getopt(argc, argv, "hn:v");
if ( result == -1 )
break;
switch ( result )
{
case 'n':
g_num_nodes = atoi(optarg);
break;
case 'v':
g_verbose = 1;
break;
case 'h':
usage(*argv, 0);
default:
fwrite("failed to parse argument.\n\n", 1uLL, 0x1BuLL, stderr);
usage(*argv, 1);
}
}
if ( g_num_nodes <= 2 )
{
fprintf(stderr, "num_nodes = %d, but we can't run with num_nodes < 3.\n\n", (unsigned int)g_num_nodes);
usage(*argv, 1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int argc, char **argv)
{
int result; // eax

g_num_nodes = 5;
g_verbose = 0;
while ( 1 )
{
result = getopt(argc, argv, "hn:v");
if ( result == -1 )
break;
switch ( result )
{
case 'n':
g_num_nodes = atoi(optarg);
break;
case 'v':
g_verbose = 1;
break;
case 'h':
func1(*argv, 0);
default:
fwrite("failed to parse argument.\n\n", 1uLL, 0x1BuLL, stderr);
func1(*argv, 1);
}
}
if ( g_num_nodes <= 2 )
{
fprintf(stderr, "num_nodes = %d, but we can't run with num_nodes < 3.\n\n", (unsigned int)g_num_nodes);
func1(*argv, 1);
}
return result;
}

original_code:
static void parse_argv(int argc, char **argv)
{
 int c;
 g_num_nodes = 5;
 g_verbose = 0;
 while ((c = getopt(argc, argv, "hn:v")) != -1) {
  switch (c) {
  case 'h':
   usage(argv[0], 0);
   break;
  case 'n':
   g_num_nodes = atoi(optarg);
   break;
  case 'v':
   g_verbose = 1;
   break;
  default:
   fprintf(stderr, "failed to parse argument.\n\n");
   usage(argv[0], 1);
   break;
  }
 }
 if (g_num_nodes < 3) {
  fprintf(stderr, "num_nodes = %d, but we can't run with "
    "num_nodes < 3.\n\n", g_num_nodes);
  usage(argv[0], 1);
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int


decompiled_code:
__int64 check_leaders()
{
unsigned int *v0; // rbp
unsigned int v1; // er12
__int64 v2; // rsi
unsigned int *v3; // rax
char *v4; // rcx
unsigned int v5; // edx
int v6; // ebx
__int64 v7; // rcx
__int64 v8; // rdx

if ( g_num_nodes <= 0 )
{
v2 = 0xFFFFFFFFLL;
LABEL_12:
v1 = 0;
printf("successfully elected node %d as leader.\n", v2);
return v1;
}
v0 = (unsigned int *)&unk_BEC;
v1 = 0;
v2 = 0xFFFFFFFFLL;
v3 = (unsigned int *)&unk_BEC;
v4 = (char *)&unk_CFC + 272 * (unsigned int)(g_num_nodes - 1);
do
{
while ( 1 )
{
v5 = *v3;
if ( (_DWORD)v2 != -1 )
break;
v3 += 68;
v2 = v5;
if ( v3 == (unsigned int *)v4 )
goto LABEL_8;
}
if ( v5 != (_DWORD)v2 )
v1 = 1;
v3 += 68;
}
while ( v3 != (unsigned int *)v4 );
LABEL_8:
v6 = 0;
if ( !v1 )
goto LABEL_12;
do
{
v7 = *v0;
v8 = (unsigned int)v6++;
v0 += 68;
fprintf(stderr, "%d: leader = %d\n", v8, v7);
}
while ( g_num_nodes > v6 );
return v1;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int *v0; // rbp
unsigned int v1; // er12
__int64 v2; // rsi
unsigned int *v3; // rax
char *v4; // rcx
unsigned int v5; // edx
int v6; // ebx
__int64 v7; // rcx
__int64 v8; // rdx

if ( g_num_nodes <= 0 )
{
v2 = 0xFFFFFFFFLL;
LABEL_12:
v1 = 0;
printf("successfully elected node %d as leader.\n", v2);
return v1;
}
v0 = (unsigned int *)&unk_BEC;
v1 = 0;
v2 = 0xFFFFFFFFLL;
v3 = (unsigned int *)&unk_BEC;
v4 = (char *)&unk_CFC + 272 * (unsigned int)(g_num_nodes - 1);
do
{
while ( 1 )
{
v5 = *v3;
if ( (_DWORD)v2 != -1 )
break;
v3 += 68;
v2 = v5;
if ( v3 == (unsigned int *)v4 )
goto LABEL_8;
}
if ( v5 != (_DWORD)v2 )
v1 = 1;
v3 += 68;
}
while ( v3 != (unsigned int *)v4 );
LABEL_8:
v6 = 0;
if ( !v1 )
goto LABEL_12;
do
{
v7 = *v0;
v8 = (unsigned int)v6++;
v0 += 68;
fprintf(stderr, "%d: leader = %d\n", v8, v7);
}
while ( g_num_nodes > v6 );
return v1;
}

original_code:
static int check_leaders(void)
{
 int i, leader, conflict;
 leader = -1;
 conflict = 0;
 for (i = 0; i < g_num_nodes; ++i) {
  if (leader == -1)
   leader = g_node_data[i].leader;
  else {
   if (leader != g_node_data[i].leader) {
    conflict = 1;
   }
  }
 }
 if (!conflict) {
  printf("successfully elected node %d as leader.\n", leader);
  return 0;
 }
 for (i = 0; i < g_num_nodes; ++i) {
  fprintf(stderr, "%d: leader = %d\n", i, g_node_data[i].leader);
 }
 return 1;
}

canonical_original_code:
static int check_leaders()
{
 int i; int leader; int conflict;
 leader = -1;
 conflict = 0;
 for (i = 0; i < g_num_nodes; ++i) {
  if (leader == -1)
   leader = g_node_data[i].leader;
  else {
   if (leader != g_node_data[i].leader) {
    conflict = 1;
   }
  }
 }
 if (!conflict) {
  printf("successfully elected node %d as leader.\n", leader);
  return 0;
 }
 for (i = 0; i < g_num_nodes; ++i) {
  fprintf(stderr, "%d: leader = %d\n", i, g_node_data[i].leader);
 }
 return 1;
}

variable_types: i int

variable_types: leader int

variable_types: conflict int


decompiled_code:
__int64 __fastcall run_tpc(int a1)
{
int v1; // ebp
unsigned int *v2; // rbx
__int64 *v3; // r14
unsigned int v4; // er12
__int64 v5; // rax
__int64 v6; // rbx
__int64 v7; // rdi
__int64 v8; // rbx
__int64 v9; // rdi
char v11[56]; // [rsp+0h] [rbp-38h] BYREF

if ( sem_init(&g_sem_accept_leader, 0, 0) )
goto run_tpc;
if ( pthread_mutex_init(&g_start_lock, 0LL) )
goto run_tpc;
v1 = pthread_cond_init(&g_start_cond, 0LL);
if ( v1 )
goto run_tpc;
g_start = 0;
memset(g_nodes, 0, sizeof(g_nodes));
memset(&g_node_data, 0, 0x8800uLL);
if ( g_num_nodes <= 0 )
goto run_tpc;
v2 = (unsigned int *)&g_node_data;
v3 = g_nodes;
v4 = 0;
do
{
snprintf(v11, 0x10uLL, "node_%3d", v4);
*v2 = v4;
v2[67] = -1;
v2[66] = -1;
v5 = worker_start(v11, tpc_handle_msg, 0LL, v2);
*v3 = v5;
if ( !v5 )
{
fprintf(stderr, "failed to allocate node %d\n", v4);
abort();
}
++v4;
v2 += 68;
++v3;
}
while ( g_num_nodes > (int)v4 );
if ( g_num_nodes <= 2 )
run_tpc:
run_tpc_cold();
send_do_propose(2);
if ( a1 )
{
send_do_propose(0);
send_do_propose(1);
}
pthread_mutex_lock(&g_start_lock);
g_start = 1;
pthread_cond_broadcast(&g_start_cond);
pthread_mutex_unlock(&g_start_lock);
if ( g_num_nodes > 0 )
{
do
{
while ( sem_wait(&g_sem_accept_leader) == -1 && *_errno_location() == 4 )
;
++v1;
}
while ( g_num_nodes > v1 );
if ( g_num_nodes > 0 )
{
v6 = 0LL;
do
{
v7 = g_nodes[v6++];
worker_stop(v7);
}
while ( g_num_nodes > (int)v6 );
if ( g_num_nodes > 0 )
{
v8 = 0LL;
do
{
v9 = g_nodes[v8++];
worker_join(v9);
}
while ( g_num_nodes > (int)v8 );
}
}
}
pthread_cond_destroy(&g_start_cond);
g_start = 0;
pthread_mutex_destroy(&g_start_lock);
sem_destroy(&g_sem_accept_leader);
return check_leaders();
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
int v1; // ebp
unsigned int *v2; // rbx
__int64 *v3; // r14
unsigned int v4; // er12
__int64 v5; // rax
__int64 v6; // rbx
__int64 v7; // rdi
__int64 v8; // rbx
__int64 v9; // rdi
char v11[56]; // [rsp+0h] [rbp-38h] BYREF

if ( sem_init(&g_sem_accept_leader, 0, 0) )
goto run_tpc;
if ( pthread_mutex_init(&g_start_lock, 0LL) )
goto run_tpc;
v1 = pthread_cond_init(&g_start_cond, 0LL);
if ( v1 )
goto run_tpc;
g_start = 0;
memset(g_nodes, 0, sizeof(g_nodes));
memset(&g_node_data, 0, 0x8800uLL);
if ( g_num_nodes <= 0 )
goto run_tpc;
v2 = (unsigned int *)&g_node_data;
v3 = g_nodes;
v4 = 0;
do
{
snprintf(v11, 0x10uLL, "node_%3d", v4);
*v2 = v4;
v2[67] = -1;
v2[66] = -1;
v5 = worker_start(v11, tpc_handle_msg, 0LL, v2);
*v3 = v5;
if ( !v5 )
{
fprintf(stderr, "failed to allocate node %d\n", v4);
abort();
}
++v4;
v2 += 68;
++v3;
}
while ( g_num_nodes > (int)v4 );
if ( g_num_nodes <= 2 )
run_tpc:
func7();
func3(2);
if ( a1 )
{
func3(0);
func3(1);
}
pthread_mutex_lock(&g_start_lock);
g_start = 1;
pthread_cond_broadcast(&g_start_cond);
pthread_mutex_unlock(&g_start_lock);
if ( g_num_nodes > 0 )
{
do
{
while ( sem_wait(&g_sem_accept_leader) == -1 && *_errno_location() == 4 )
;
++v1;
}
while ( g_num_nodes > v1 );
if ( g_num_nodes > 0 )
{
v6 = 0LL;
do
{
v7 = g_nodes[v6++];
worker_stop(v7);
}
while ( g_num_nodes > (int)v6 );
if ( g_num_nodes > 0 )
{
v8 = 0LL;
do
{
v9 = g_nodes[v8++];
worker_join(v9);
}
while ( g_num_nodes > (int)v8 );
}
}
}
pthread_cond_destroy(&g_start_cond);
g_start = 0;
pthread_mutex_destroy(&g_start_lock);
sem_destroy(&g_sem_accept_leader);
return func5();
}

original_code:
static int run_tpc(int duelling_proposers)
{
 int i;
 if (sem_init(&g_sem_accept_leader, 0, 0))
  abort();
 if (pthread_mutex_init(&g_start_lock, 0))
  abort();
 if (pthread_cond_init(&g_start_cond, 0))
  abort();
 g_start = 0;
 memset(g_nodes, 0, sizeof(g_nodes));
 memset(g_node_data, 0, sizeof(g_node_data));
 for (i = 0; i < g_num_nodes; ++i) {
  char name[16];
  snprintf(name, 16, "node_%3d", i);
  g_node_data[i].id = i;
  g_node_data[i].leader = -1;
  g_node_data[i].coord = -1;
  g_nodes[i] = worker_start(name, tpc_handle_msg,
    ((void *)0), &g_node_data[i]);
  if (!g_nodes[i]) {
   fprintf(stderr, "failed to allocate node %d\n", i);
   abort();
  }
 }
 if (g_num_nodes < 3)
  abort();
 send_do_propose(2);
 if (duelling_proposers) {
  send_do_propose(0);
  send_do_propose(1);
 }
 pthread_mutex_lock(&g_start_lock);
 g_start = 1;
 pthread_cond_broadcast(&g_start_cond);
 pthread_mutex_unlock(&g_start_lock);
 for (i = 0; i < g_num_nodes; ++i) {
  (__extension__ ({ long int __result; do __result = (long int) (sem_wait(&g_sem_accept_leader)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
 }
 for (i = 0; i < g_num_nodes; ++i) {
  worker_stop(g_nodes[i]);
 }
 for (i = 0; i < g_num_nodes; ++i) {
  worker_join(g_nodes[i]);
 }
 pthread_cond_destroy(&g_start_cond);
 g_start = 0;
 pthread_mutex_destroy(&g_start_lock);
 sem_destroy(&g_sem_accept_leader);
 return check_leaders();
}

canonical_original_code:
static int run_tpc(int duelling_proposers)
{
 int i;
 if (sem_init(&g_sem_accept_leader, 0, 0))
  abort();
 if (pthread_mutex_init(&g_start_lock, 0))
  abort();
 if (pthread_cond_init(&g_start_cond, 0))
  abort();
 g_start = 0;
 memset(g_nodes, 0, sizeof(g_nodes));
 memset(g_node_data, 0, sizeof(g_node_data));
 for (i = 0; i < g_num_nodes; ++i) {
  char name[16];
  snprintf(name, 16, "node_%3d", i);
  g_node_data[i].id = i;
  g_node_data[i].leader = -1;
  g_node_data[i].coord = -1;
  g_nodes[i] = worker_start(name, tpc_handle_msg,
    ((void *)0), &g_node_data[i]);
  if (!g_nodes[i]) {
   fprintf(stderr, "failed to allocate node %d\n", i);
   abort();
  }
 }
 if (g_num_nodes < 3)
  abort();
 send_do_propose(2);
 if (duelling_proposers) {
  send_do_propose(0);
  send_do_propose(1);
 }
 pthread_mutex_lock(&g_start_lock);
 g_start = 1;
 pthread_cond_broadcast(&g_start_cond);
 pthread_mutex_unlock(&g_start_lock);
 for (i = 0; i < g_num_nodes; ++i) {
  (__extension__ ({ long __result; do __result = (long) (sem_wait(&g_sem_accept_leader)); while (__result == -1L && (*__errno_location ()) == 4); __result; }));
 }
 for (i = 0; i < g_num_nodes; ++i) {
  worker_stop(g_nodes[i]);
 }
 for (i = 0; i < g_num_nodes; ++i) {
  worker_join(g_nodes[i]);
 }
 pthread_cond_destroy(&g_start_cond);
 g_start = 0;
 pthread_mutex_destroy(&g_start_lock);
 sem_destroy(&g_sem_accept_leader);
 return check_leaders();
}

variable_types:
name
char [16]


repo: cmoresid/dirapp

decompiled_code:
void __fastcall __noreturn err_quit(const char *a1)
{
fprintf(stderr, "Error: %s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(stderr, "Error: %s\n", a1);
exit(1);
}

original_code:
void err_quit(const char* error)
{
 fprintf(stderr, "Error: %s\n", error);
 exit(1);
}

canonical_original_code:
void err_quit(char *error)
{
 fprintf(stderr, "Error: %s\n", error);
 exit(1);
}

variable_types:
error
char *


decompiled_code:
__int64 __fastcall read_byte(int a1)
{
bool v1; // zf
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = recv(a1, buf, 1uLL, 0) == 0;
result = (unsigned __int8)buf[0];
if ( v1 )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
bool v1; // zf
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = recv(a1, buf, 1uLL, 0) == 0;
result = (unsigned __int8)buf[0];
if ( v1 )
result = 0LL;
return result;
}

original_code:
byte read_byte(int socketfd)
{
 size_t nbytes;
 byte buff[1];
 if ((nbytes = recv(socketfd, buff, 1, 0)) <= 0) {
  return 0;
 } else {
  return buff[0];
 }
}

canonical_original_code:
unsigned char read_byte(int socketfd)
{
 unsigned long nbytes;
 unsigned char buff[1];
 if ((nbytes = recv(socketfd, buff, 1, 0)) <= 0) {
  return 0;
 } else {
  return buff[0];
 }
}

variable_types: socketfd int

variable_types:
nbytes
unsigned long

variable_types:
buff
unsigned char [1]

return_type: unsigned char


decompiled_code:
__int64 __fastcall read_string(int fd, void *buf, int a3)
{
unsigned __int8 v4; // al
__int64 v5; // r14
ssize_t v6; // r8
__int64 result; // rax

v4 = read_byte(fd);
v5 = v4;
if ( !v4 || a3 <= (unsigned __int64)v4 )
return 0xFFFFFFFFLL;
v6 = recv(fd, buf, v4, 256);
result = 0LL;
if ( v6 )
{
result = (unsigned __int8)v5;
*((_BYTE *)buf + v5) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, int a3)
{
unsigned __int8 v4; // al
__int64 v5; // r14
ssize_t v6; // r8
__int64 result; // rax

v4 = func1(fd);
v5 = v4;
if ( !v4 || a3 <= (unsigned __int64)v4 )
return 0xFFFFFFFFLL;
v6 = recv(fd, buf, v4, 256);
result = 0LL;
if ( v6 )
{
result = (unsigned __int8)v5;
*((_BYTE *)buf + v5) = 0;
}
return result;
}

original_code:
int read_string(int socketfd, byte* buff, int buff_size)
{
 size_t nbytes;
 size_t len;
 if ((len = read_byte(socketfd)) <= 0) {
  return -1;
 }
 if (len >= buff_size) {
  return -1;
 }
 if ((nbytes = recv(socketfd, buff, len, MSG_WAITALL)) <= 0) {
  if (nbytes == 0) {
   return 0;
  } else {
   return -1;
  }
 } else {
  buff[len] = '\0';
  return len;
 }
}

canonical_original_code:
int read_string(int socketfd, unsigned char *buff, int buff_size)
{
 unsigned long nbytes;
 unsigned long len;
 if ((len = read_byte(socketfd)) <= 0) {
  return -1;
 }
 if (len >= buff_size) {
  return -1;
 }
 if ((nbytes = recv(socketfd, buff, len, MSG_WAITALL)) <= 0) {
  if (nbytes == 0) {
   return 0;
  } else {
   return -1;
  }
 } else {
  buff[len] = '\0';
  return len;
 }
}

variable_types:
buff
unsigned char *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall send_byte(int a1, char a2)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a2;
return 2 * (unsigned int)(send(a1, buf, 1uLL, 0) > 0) - 1;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, char a2)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a2;
return 2 * (unsigned int)(send(a1, buf, 1uLL, 0) > 0) - 1;
}

original_code:
int send_byte(int socketfd, byte b)
{
 byte buff[1];
 buff[0] = b;
 if (send(socketfd, buff, 1, 0) <= 0) {
  return -1;
 }
 return 1;
}

canonical_original_code:
int send_byte(int socketfd, unsigned char b)
{
 unsigned char buff[1];
 buff[0] = b;
 if (send(socketfd, buff, 1, 0) <= 0) {
  return -1;
 }
 return 1;
}

variable_types:
b
unsigned char


decompiled_code:
__int64 __fastcall send_string(int fd, char *src)
{
unsigned __int8 v2; // al
size_t v3; // r15
unsigned int v4; // er12
void *v5; // rsp
char v7[32]; // [rsp+0h] [rbp-20h] BYREF

v2 = strlen(src);
v3 = v2;
v4 = v2;
v5 = alloca((v2 + 15) & 0x1F0);
if ( (unsigned int)send_byte(fd, v2) != 1 || (strncpy(v7, src, v3), send(fd, v7, v3, 0) <= 0) )
v4 = -1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, char *src)
{
unsigned __int8 v2; // al
size_t v3; // r15
unsigned int v4; // er12
void *v5; // rsp
char v7[32]; // [rsp+0h] [rbp-20h] BYREF

v2 = strlen(src);
v3 = v2;
v4 = v2;
v5 = alloca((v2 + 15) & 0x1F0);
if ( (unsigned int)func3(fd, v2) != 1 || (strncpy(v7, src, v3), send(fd, v7, v3, 0) <= 0) )
v4 = -1;
return v4;
}

original_code:
int send_string(int socketfd, const char* str)
{
 byte len;
 len = strlen(str);
 byte byte_str[len];
 if (send_byte(socketfd, len) != 1) {
  return -1;
 }
 strncpy(byte_str, str, len);
 if (send(socketfd, byte_str, len, 0) <= 0) {
  return -1;
 }
 return len;
}

canonical_original_code:
int send_string(int socketfd, char *str)
{
 unsigned char len;
 len = strlen(str);
 unsigned char byte_str[-1];
 if (send_byte(socketfd, len) != 1) {
  return -1;
 }
 strncpy(byte_str, str, len);
 if (send(socketfd, byte_str, len, 0) <= 0) {
  return -1;
 }
 return len;
}

variable_types:
str
char *

variable_types:
len
unsigned char


decompiled_code:
void __fastcall free_mempool(void **a1)
{
free(*a1);
}

canonical_decompiled_code:
void __fastcall func0(void **a1)
{
free(*a1);
}

original_code:
void free_mempool(struct mempool* mp)
{
 free(mp->memblock);
}

canonical_original_code:
void free_mempool(struct mempool *mp)
{
 free(mp->memblock);
}

variable_types: struct mempool { void *memblock; struct memunit { struct memunit *next; struct memunit *prev; } *allocated_memblock; struct memunit *free_memblock; unsigned long unit_size; unsigned long memblock_size; } *

user_defined_types: struct mempool { void *memblock; struct memunit *allocated_memblock; struct memunit *free_memblock; unsigned long unit_size; unsigned long memblock_size; }


original_code:
void mempool_free(struct mempool* mp, void* p)
{
 if (mp->memblock < p && p < (void*)((char*)mp->memblock + mp->memblock_size)) {
  struct memunit* cur_unit = (struct memunit*)((char*)p - sizeof(struct memunit));
  mp->allocated_memblock = cur_unit->next;
  if (mp->allocated_memblock != ((void *)0)) {
   mp->allocated_memblock->prev = ((void *)0);
  }
  cur_unit->next = mp->free_memblock;
  if (mp->free_memblock != ((void *)0)) {
   mp->free_memblock->prev = cur_unit;
  }
  mp->free_memblock = cur_unit;
 } else {
  free(p);
 }
}

canonical_original_code:
void mempool_free(struct mempool *mp, void *p)
{
 if (mp->memblock < p && p < (void *)((char *)mp->memblock + mp->memblock_size)) {
  struct memunit *cur_unit = (struct memunit*)((char*)p - sizeof(struct memunit));
  mp->allocated_memblock = cur_unit->next;
  if (mp->allocated_memblock != ((void *)0)) {
   mp->allocated_memblock->prev = ((void *)0);
  }
  cur_unit->next = mp->free_memblock;
  if (mp->free_memblock != ((void *)0)) {
   mp->free_memblock->prev = cur_unit;
  }
  mp->free_memblock = cur_unit;
 } else {
  free(p);
 }
}

variable_types:
p
void *

variable_types: struct memunit { struct memunit *next; struct memunit *prev; } *


repo: cmwdotme/rcfsutil

decompiled_code:
char *__fastcall print_timestamp(unsigned int a1)
{
struct tm *v1; // rax
char *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = gmtime(&timer);
v2 = asctime(v1);
strcpy(timestamp, v2);
*(_BYTE *)(strlen(timestamp) + 2111) = 0;
return timestamp;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned int a1)
{
struct tm *v1; // rax
char *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = gmtime(&timer);
v2 = asctime(v1);
strcpy(timestamp, v2);
*(_BYTE *)(strlen(timestamp) + 2111) = 0;
return timestamp;
}

original_code:
char *print_timestamp(unsigned int tvalue)
{
  struct tm *tm;
  time_t time = tvalue;
  tm = gmtime(&time);
  sprintf(timestamp, "%s", asctime(tm));
  timestamp[strlen(timestamp) - 1] = '\0';
  return timestamp;
}

canonical_original_code:
char *print_timestamp(unsigned int tvalue)
{
  struct tm *tm;
  long time = tvalue;
  tm = gmtime(&time);
  sprintf(timestamp, "%s", asctime(tm));
  timestamp[strlen(timestamp) - 1] = '\0';
  return timestamp;
}

variable_types:
tvalue
unsigned int

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: time long

return_type: char *


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

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

original_code:
static inline u16 get_unaligned_le16(const u8 *p)
{
 return p[0] | p[1] << 8;
}

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

variable_types:
p
unsigned char *

return_type: unsigned short


MouseMotion

repo: cnlohr/bridgesim

decompiled_code:
__int64 __fastcall MouseMotion(unsigned int a1, double a2, double a3)
{
float v3; // xmm0_4
float v4; // xmm1_4

v3 = a2;
v4 = a3;
return WindowHandleMouseMove(guiwindow, a1, v3, v4);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, double a2, double a3)
{
float v3; // xmm0_4
float v4; // xmm1_4

v3 = a2;
v4 = a3;
return WindowHandleMouseMove(guiwindow, a1, v3, v4);
}

original_code:
void MouseMotion( double x, double y, int mask )
{
 WindowHandleMouseMove( guiwindow, x, y, mask );
}

canonical_original_code:
void MouseMotion(double x, double y, int mask)
{
 WindowHandleMouseMove( guiwindow, x, y, mask );
}

variable_types: x double

variable_types: y double

variable_types: mask int


MouseClick

decompiled_code:
__int64 __fastcall MouseClick(unsigned int a1, unsigned int a2, double a3, double a4)
{
float v4; // xmm0_4
float v5; // xmm1_4

v4 = a3;
v5 = a4;
return WindowHandleMouseClick(guiwindow, a1, a2, v4, v5);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2, double a3, double a4)
{
float v4; // xmm0_4
float v5; // xmm1_4

v4 = a3;
v5 = a4;
return WindowHandleMouseClick(guiwindow, a1, a2, v4, v5);
}

original_code:
void MouseClick( double x, double y, int button, int state )
{
 WindowHandleMouseClick( guiwindow, x, y, button, state );
}

canonical_original_code:
void MouseClick(double x, double y, int button, int state)
{
 WindowHandleMouseClick( guiwindow, x, y, button, state );
}

variable_types: button int

variable_types: state int


HandleKeyboard

decompiled_code:
__int64 __fastcall HandleKeyboard(unsigned int a1, unsigned int a2)
{
return WindowHandleKeyboard(guiwindow, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2)
{
return WindowHandleKeyboard(guiwindow, a1, a2);
}

original_code:
void HandleKeyboard( int c, int down )
{
 WindowHandleKeyboard( guiwindow, c, down );
}

canonical_original_code:
void HandleKeyboard(int c, int down)
{
 WindowHandleKeyboard( guiwindow, c, down );
}

variable_types: c int

variable_types: down int


UpdatedDisplay

decompiled_code:
__int64 UpdatedDisplay()
{
_DWORD *v0; // rdi
char s[55]; // [rsp+1h] [rbp-37h] BYREF

CheckCommonAssets();
glClearColor(0.2, 0.2, 0.2, 0.0);
glClear(16640LL);
glEnable(3042LL);
glBlendFunc(770LL, 771LL);
glEnable(2929LL);
glDepthFunc(513LL);
v0 = guiwindow;
*guiwindow = (int)ScreenW;
v0[1] = (int)*(float *)&ScreenH;
WindowRender();
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(0.0, ScreenW, 0.0, *(float *)&ScreenH, -1.0, 1.0);
glDepthFunc(519LL);
glScalef(1.0, -1.0);
glTranslatef(0.0, COERCE_DOUBLE(ScreenH ^ (unsigned __int64)LC7));
glMatrixMode(5888LL);
glLoadIdentity();
sprintf(s, "FPS: %d", fpscount);
glRasterPos2f(80.0, 50.0);
glutBitmapString(&glutBitmap9By15, s);
ActivateTexture(*((_QWORD *)guiwindow + 3));
DrawSquare(0.0, 0.0, ScreenW, *(float *)&ScreenH);
DeactivateTexture(*((_QWORD *)guiwindow + 3));
glLoadIdentity();
return glFlush();
}

canonical_decompiled_code:
__int64 func3()
{
_DWORD *v0; // rdi
char s[55]; // [rsp+1h] [rbp-37h] BYREF

CheckCommonAssets();
glClearColor(0.2, 0.2, 0.2, 0.0);
glClear(16640LL);
glEnable(3042LL);
glBlendFunc(770LL, 771LL);
glEnable(2929LL);
glDepthFunc(513LL);
v0 = guiwindow;
*guiwindow = (int)ScreenW;
v0[1] = (int)*(float *)&ScreenH;
WindowRender();
glMatrixMode(5889LL);
glLoadIdentity();
glOrtho(0.0, ScreenW, 0.0, *(float *)&ScreenH, -1.0, 1.0);
glDepthFunc(519LL);
glScalef(1.0, -1.0);
glTranslatef(0.0, COERCE_DOUBLE(ScreenH ^ (unsigned __int64)LC7));
glMatrixMode(5888LL);
glLoadIdentity();
sprintf(s, "FPS: %d", fpscount);
glRasterPos2f(80.0, 50.0);
glutBitmapString(&glutBitmap9By15, s);
ActivateTexture(*((_QWORD *)guiwindow + 3));
DrawSquare(0.0, 0.0, ScreenW, *(float *)&ScreenH);
DeactivateTexture(*((_QWORD *)guiwindow + 3));
glLoadIdentity();
return glFlush();
}

original_code:
void UpdatedDisplay(void)
{
 CheckCommonAssets();
 glClearColor( .2, .2, .2, 0 );
 glClear(0x00004000 | 0x00000100);
 glEnable (0x0BE2);
 glBlendFunc(0x0302, 0x0303);
 glEnable( 0x0B71 );
 glDepthFunc(0x0201);
 guiwindow->width = ScreenW;
 guiwindow->height = ScreenH;
 WindowRender( guiwindow );
 glMatrixMode(0x1701);
 glLoadIdentity();
 glOrtho(0, ScreenW, 0, ScreenH, -1, 1);
 glDepthFunc(0x0207);
 glScalef(1, -1, 1);
 glTranslatef(0, -ScreenH, 0);
 glMatrixMode(0x1700);
 glLoadIdentity();
 char st[15];
 sprintf( st, "FPS: %d", fpscount );
 glRasterPos2f(80, 50);
 glutBitmapString( ((void *) &glutBitmap9By15), st );
 ActivateTexture( guiwindow->torender );
 DrawSquare(0,0,ScreenW,ScreenH );
 DeactivateTexture( guiwindow->torender );
 glLoadIdentity();
 glFlush();
}

canonical_original_code:
void UpdatedDisplay()
{
 CheckCommonAssets();
 glClearColor( .2, .2, .2, 0 );
 glClear(0x00004000 | 0x00000100);
 glEnable (0x0BE2);
 glBlendFunc(0x0302, 0x0303);
 glEnable( 0x0B71 );
 glDepthFunc(0x0201);
 guiwindow->width = ScreenW;
 guiwindow->height = ScreenH;
 WindowRender( guiwindow );
 glMatrixMode(0x1701);
 glLoadIdentity();
 glOrtho(0, ScreenW, 0, ScreenH, -1, 1);
 glDepthFunc(0x0207);
 glScalef(1, -1, 1);
 glTranslatef(0, -ScreenH, 0);
 glMatrixMode(0x1700);
 glLoadIdentity();
 char st[15];
 sprintf( st, "FPS: %d", fpscount );
 glRasterPos2f(80, 50);
 glutBitmapString( ((void *) &glutBitmap9By15), st );
 ActivateTexture( guiwindow->torender );
 DrawSquare(0,0,ScreenW,ScreenH );
 DeactivateTexture( guiwindow->torender );
 glLoadIdentity();
 glFlush();
}

variable_types:
st
char [15]


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
__int64 v4; // rax

StartGlutMain(argc, argv, "test", 640LL, 480LL);
SetupCommonAssets();
v3 = (_DWORD *)CreateGUIWindow();
guiwindow = v3;
*(_QWORD *)v3 = 0x12C0000012CLL;
v4 = CreateButton(v3, "Hi!");
WindowAddElement(guiwindow, v4);
SetMouseWarpMode(0LL);
DoGlutLoop();
return 0;
}

original_code:
int main(int argc, char **argv)
{
 StartGlutMain( argc, argv, "test", 640, 480 );
 SetupCommonAssets();
 guiwindow = CreateGUIWindow( );
 guiwindow->width = 300;
 guiwindow->height = 300;
 struct GUIBase * b;
 WindowAddElement( guiwindow, b = CreateButton( guiwindow, "Hi!" ) );
 SetMouseWarpMode( 0 );
 DoGlutLoop();
}

canonical_original_code:
int main(int argc, char **argv)
{
 StartGlutMain( argc, argv, "test", 640, 480 );
 SetupCommonAssets();
 guiwindow = CreateGUIWindow( );
 guiwindow->width = 300;
 guiwindow->height = 300;
 struct GUIBase *b;
 WindowAddElement( guiwindow, b = CreateButton( guiwindow, "Hi!" ) );
 SetMouseWarpMode( 0 );
 DoGlutLoop();
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct GUIBase { float x; float w; float color[4]; int depressed; int clicked; int focused; struct GUIWindow { int width; int height; int focusid; struct RFBuffer { int width; int use_depth_buffer; enum TextureType { TTUNDEFINED=0, TTGRAYSCALE=1, TTGRAYALPHA=2, TTRGB=3, TTRGBA=4, TTRGBA16=5, TTRGBA32=6 } mtt; unsigned int renderbuffer; unsigned int outputbuffer; int outextures; } *renderbuffer; struct Texture { enum TextureType { TTUNDEFINED=0, TTGRAYSCALE=1, TTGRAYALPHA=2, TTRGB=3, TTRGBA=4, TTRGBA16=5, TTRGBA32=6 } format; unsigned int type; unsigned int texture; int width; int slot; unsigned char *rawdata; } *torender; struct GUIBase *elements[2048]; } *wp; void (*Prerender)(struct GUIBase *b); void (*Render)(struct GUIBase *b); int (*CheckClick)(struct GUIBase *b, float screenx, float screeny, int button, int down); void (*HandleMove)(struct GUIBase *b, float screenx, float screeny, int buttonmask); void (*HandleKeyboard)(struct GUIBase *b, char c, int down, int focused); void (*DestroyMe)(struct GUIBase *b); void (*ProcessClick)(struct GUIBase *b); } *

user_defined_types: struct GUIBase { float x; float w; float color[4]; int depressed; int clicked; int focused; struct GUIWindow *wp; void (*Prerender)(struct GUIBase *b); void (*Render)(struct GUIBase *b); int (*CheckClick)(struct GUIBase *b, float screenx, float screeny, int button, int down); void (*HandleMove)(struct GUIBase *b, float screenx, float screeny, int buttonmask); void (*HandleKeyboard)(struct GUIBase *b, char c, int down, int focused); void (*DestroyMe)(struct GUIBase *b); void (*ProcessClick)(struct GUIBase *b); }

user_defined_types: struct GUIWindow { int width; int height; int focusid; struct RFBuffer *renderbuffer; struct Texture *torender; struct GUIBase *elements[2048]; }

user_defined_types: struct RFBuffer { int width; int use_depth_buffer; enum TextureType mtt; unsigned int renderbuffer; unsigned int outputbuffer; int outextures; }

user_defined_types: struct Texture { enum TextureType format; unsigned int type; unsigned int texture; int width; int slot; unsigned char *rawdata; }


idle

decompiled_code:
// attributes: thunk
__int64 idle()
{
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 func0()
{
return glutPostRedisplay();
}

original_code:
void idle()
{
 glutPostRedisplay();
}

canonical_original_code:
void idle()
{
 glutPostRedisplay();
}


reshape

decompiled_code:
__int64 __fastcall reshape(int a1, int a2)
{
ScreenW = (float)a1;
ScreenH = (float)a2;
return glViewport(0LL, 0LL, (unsigned int)a1, (unsigned int)a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
ScreenW = (float)a1;
ScreenH = (float)a2;
return glViewport(0LL, 0LL, (unsigned int)a1, (unsigned int)a2);
}

original_code:
void reshape(int w, int h)
{
 ScreenW = w;
 ScreenH = h;
 glViewport(0, 0, w, h);
}

variable_types: w int

variable_types: h int


display

decompiled_code:
_QWORD *display()
{
double v0; // xmm0_8
float v1; // xmm2_4
double v2; // xmm1_8
_QWORD *result; // rax
double v4; // xmm1_8
double v5; // xmm1_8
int v6; // esi

v0 = OGGetAbsoluteTime() - StartTime;
v1 = ScreenW;
v2 = v0 - *(double *)&LastTime_0;
TotalTime = *(_QWORD *)&v0;
result = &DeltaTime;
LastTime_0 = *(_QWORD *)&v0;
DeltaTime = *(_QWORD *)&v2;
if ( ScreenW <= 0.0 || ScreenH <= 0.0 )
{
v4 = *(double *)&LC1;
if ( v0 < 1.0 )
return result;
}
else
{
v4 = *(double *)&LC1;
}
v5 = v4 + LastFPSTime;
if ( v0 > v5 )
{
LastFPSTime = v5;
fpscount = framecountsincefps;
printf("FPS: %d\n", framecountsincefps);
v1 = ScreenW;
v6 = 1;
}
else
{
v6 = framecountsincefps + 1;
}
framecountsincefps = v6;
RenderW = LODWORD(v1);
RenderH = LODWORD(ScreenH);
UpdatedDisplay();
return (_QWORD *)glFlush();
}

canonical_decompiled_code:
_QWORD *func2()
{
double v0; // xmm0_8
float v1; // xmm2_4
double v2; // xmm1_8
_QWORD *result; // rax
double v4; // xmm1_8
double v5; // xmm1_8
int v6; // esi

v0 = OGGetAbsoluteTime() - StartTime;
v1 = ScreenW;
v2 = v0 - *(double *)&LastTime_0;
TotalTime = *(_QWORD *)&v0;
result = &DeltaTime;
LastTime_0 = *(_QWORD *)&v0;
DeltaTime = *(_QWORD *)&v2;
if ( ScreenW <= 0.0 || ScreenH <= 0.0 )
{
v4 = *(double *)&LC1;
if ( v0 < 1.0 )
return result;
}
else
{
v4 = *(double *)&LC1;
}
v5 = v4 + LastFPSTime;
if ( v0 > v5 )
{
LastFPSTime = v5;
fpscount = framecountsincefps;
printf("FPS: %d\n", framecountsincefps);
v1 = ScreenW;
v6 = 1;
}
else
{
v6 = framecountsincefps + 1;
}
framecountsincefps = v6;
RenderW = LODWORD(v1);
RenderH = LODWORD(ScreenH);
UpdatedDisplay();
return (_QWORD *)glFlush();
}

original_code:
void display(void)
{
 static double LastTime;
 TotalTime = OGGetAbsoluteTime() - StartTime;
 DeltaTime = TotalTime - LastTime;
 LastTime = TotalTime;
 if( ( ScreenW <= 0 || ScreenH <= 0 ) && TotalTime < 1.0 ) return;
 if( LastFPSTime + 1 < TotalTime )
 {
  LastFPSTime += 1;
  fpscount = framecountsincefps;
  printf( "FPS: %d\n", fpscount );
  framecountsincefps = 0;
 }
 framecountsincefps++;
 RenderW = ScreenW;
 RenderH = ScreenH;
 UpdatedDisplay();
 glFlush();
}

canonical_original_code:
void display()
{
 static double LastTime;
 TotalTime = OGGetAbsoluteTime() - StartTime;
 DeltaTime = TotalTime - LastTime;
 LastTime = TotalTime;
 if( ( ScreenW <= 0 || ScreenH <= 0 ) && TotalTime < 1.0 ) return;
 if( LastFPSTime + 1 < TotalTime )
 {
  LastFPSTime += 1;
  fpscount = framecountsincefps;
  printf( "FPS: %d\n", fpscount );
  framecountsincefps = 0;
 }
 framecountsincefps++;
 RenderW = ScreenW;
 RenderH = ScreenH;
 UpdatedDisplay();
 glFlush();
}

variable_types: LastTime double


myupfunc

decompiled_code:
__int64 __fastcall myupfunc(unsigned __int8 a1)
{
return HandleKeyboard(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
return HandleKeyboard(a1, 0LL);
}

original_code:
void myupfunc( unsigned char key, int x, int y )
{
 HandleKeyboard( key, 0 );
}

canonical_original_code:
void myupfunc(unsigned char key, int x, int y)
{
 HandleKeyboard( key, 0 );
}

variable_types:
key
unsigned char

variable_types: x int

variable_types: y int


mykeyfunc

decompiled_code:
__int64 __fastcall mykeyfunc(unsigned __int8 a1)
{
return HandleKeyboard(a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
return HandleKeyboard(a1, 1LL);
}

original_code:
void mykeyfunc( unsigned char key, int x, int y )
{
 HandleKeyboard( key, 1 );
}

canonical_original_code:
void mykeyfunc(unsigned char key, int x, int y)
{
 HandleKeyboard( key, 1 );
}


myspecialfunc

decompiled_code:
__int64 __fastcall myspecialfunc(__int64 a1)
{
return HandleKeyboard(a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return HandleKeyboard(a1, 1LL);
}

original_code:
void myspecialfunc( int key, int x, int y )
{
 HandleKeyboard( key, 1 );
}

canonical_original_code:
void myspecialfunc(int key, int x, int y)
{
 HandleKeyboard( key, 1 );
}

variable_types: key int


myspecialupfunc

decompiled_code:
__int64 __fastcall myspecialupfunc(__int64 a1)
{
return HandleKeyboard(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return HandleKeyboard(a1, 0LL);
}

original_code:
void myspecialupfunc( int key, int x, int y )
{
 HandleKeyboard( key, 0 );
}

canonical_original_code:
void myspecialupfunc(int key, int x, int y)
{
 HandleKeyboard( key, 0 );
}


mousepassive

decompiled_code:
__int64 __fastcall mousepassive(__int64 a1, __int64 a2)
{
__int64 v2; // rdx

if ( domousewarp )
{
absoluteMouseMotionX = (double)((int)a1 - (int)(float)(RenderW * 0.5)) + absoluteMouseMotionX;
absoluteMouseMotionY = (double)((int)a2 - (int)(float)(RenderH * 0.5)) + absoluteMouseMotionY;
glutWarpPointer();
v2 = (unsigned int)mousebuttonmask;
a2 = (unsigned int)(a2 - (int)(float)(RenderH * 0.5));
a1 = (unsigned int)(a1 - (int)(float)(0.5 * RenderW));
}
else
{
v2 = (unsigned int)mousebuttonmask;
currentMouseY = (double)(int)a2;
currentMouseX = (double)(int)a1;
}
return MouseMotion(a1, a2, v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
__int64 v2; // rdx

if ( domousewarp )
{
absoluteMouseMotionX = (double)((int)a1 - (int)(float)(RenderW * 0.5)) + absoluteMouseMotionX;
absoluteMouseMotionY = (double)((int)a2 - (int)(float)(RenderH * 0.5)) + absoluteMouseMotionY;
glutWarpPointer();
v2 = (unsigned int)mousebuttonmask;
a2 = (unsigned int)(a2 - (int)(float)(RenderH * 0.5));
a1 = (unsigned int)(a1 - (int)(float)(0.5 * RenderW));
}
else
{
v2 = (unsigned int)mousebuttonmask;
currentMouseY = (double)(int)a2;
currentMouseX = (double)(int)a1;
}
return MouseMotion(a1, a2, v2);
}

original_code:
void mousepassive(int x, int y)
{
 if( domousewarp )
 {
  absoluteMouseMotionX += x - (int)(RenderW/2);
  absoluteMouseMotionY += y - (int)(RenderH/2);
  glutWarpPointer( (int)(RenderW/2), (int)(RenderH/2) );
  MouseMotion( x - (int)(RenderW/2), y - (int)(RenderH/2), mousebuttonmask );
 }
 else
 {
  currentMouseY = y;
  currentMouseX = x;
  MouseMotion( x, y, mousebuttonmask );
 }
}


mousemotion

decompiled_code:
// attributes: thunk
__int64 __fastcall mousemotion(__int64 a1, __int64 a2)
{
return mousepassive(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
return func7(a1, a2);
}

original_code:
void mousemotion(int x, int y)
{
 mousepassive( x, y );
}


mousefunc

decompiled_code:
__int64 __fastcall mousefunc(char a1, int a2, int a3, int a4)
{
int v4; // eax
__int64 result; // rax

v4 = mousebuttonmask & ~(1 << a1);
if ( a2 )
v4 = (1 << a1) | mousebuttonmask;
mousebuttonmask = v4;
if ( domousewarp )
result = MouseClick(absoluteMouseMotionX, absoluteMouseMotionY);
else
result = MouseClick((double)a3, (double)a4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1, int a2, int a3, int a4)
{
int v4; // eax
__int64 result; // rax

v4 = mousebuttonmask & ~(1 << a1);
if ( a2 )
v4 = (1 << a1) | mousebuttonmask;
mousebuttonmask = v4;
if ( domousewarp )
result = MouseClick(absoluteMouseMotionX, absoluteMouseMotionY);
else
result = MouseClick((double)a3, (double)a4);
return result;
}

original_code:
void mousefunc( int button, int state, int x, int y )
{
 if( state )
 {
  mousebuttonmask |= 1<<button;
 }
 else
 {
  mousebuttonmask &= ~(1<<button);
 }
 if( domousewarp )
 {
  MouseClick( absoluteMouseMotionX, absoluteMouseMotionY, button, state );
 }
 else
 {
  double dx = x;
  double dy = y;
  MouseClick( dx, dy, button, state );
 }
}

canonical_original_code:
void mousefunc(int button, int state, int x, int y)
{
 if( state )
 {
  mousebuttonmask |= 1<<button;
 }
 else
 {
  mousebuttonmask &= ~(1<<button);
 }
 if( domousewarp )
 {
  MouseClick( absoluteMouseMotionX, absoluteMouseMotionY, button, state );
 }
 else
 {
  double dx = x;
  double dy = y;
  MouseClick( dx, dy, button, state );
 }
}

variable_types: dx double

variable_types: dy double


StartGlutMain

decompiled_code:
__int64 __fastcall StartGlutMain(int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 result; // rax
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
glutInitWindowSize(a4, a5);
glutInit(v6, a2);
glutCreateWindow("Test");
if ( (unsigned int)glewInit() )
{
fwrite("Error: GLEW Fault.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
StartTime = OGGetAbsoluteTime();
glutSetKeyRepeat(0LL);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(mykeyfunc);
glutKeyboardUpFunc(myupfunc);
glutSpecialFunc(myspecialfunc);
glutSpecialUpFunc(myspecialupfunc);
glutMotionFunc(mousemotion);
glutPassiveMotionFunc(mousepassive);
result = glutMouseFunc(mousefunc);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 result; // rax
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
glutInitWindowSize(a4, a5);
glutInit(v6, a2);
glutCreateWindow("Test");
if ( (unsigned int)glewInit() )
{
fwrite("Error: GLEW Fault.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
StartTime = OGGetAbsoluteTime();
glutSetKeyRepeat(0LL);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(mykeyfunc);
glutKeyboardUpFunc(myupfunc);
glutSpecialFunc(myspecialfunc);
glutSpecialUpFunc(myspecialupfunc);
glutMotionFunc(mousemotion);
glutPassiveMotionFunc(mousepassive);
result = glutMouseFunc(mousefunc);
}
return result;
}

original_code:
int StartGlutMain(int argc, char **argv, const char * title, int initsizeX, int initsizeY)
{
 int i;
 glutInitWindowSize(initsizeX,initsizeY);
 glutInit( &argc, argv );
 glutCreateWindow( "Test" );
 if( glewInit() != 0 )
 {
  fprintf( stderr, "Error: GLEW Fault.\n" );
  return 0;
 }
 StartTime = OGGetAbsoluteTime();
 glutSetKeyRepeat( 0x0000 );
 glutDisplayFunc( display );
 glutReshapeFunc( reshape );
 glutIdleFunc( idle );
 glutKeyboardFunc( mykeyfunc );
 glutKeyboardUpFunc( myupfunc );
 glutSpecialFunc( myspecialfunc );
 glutSpecialUpFunc( myspecialupfunc );
 glutMotionFunc( mousemotion );
 glutPassiveMotionFunc( mousepassive );
 glutMouseFunc( mousefunc );
}

canonical_original_code:
int StartGlutMain(int argc, char **argv, char *title, int initsizeX, int initsizeY)
{
 int i;
 glutInitWindowSize(initsizeX,initsizeY);
 glutInit( &argc, argv );
 glutCreateWindow( "Test" );
 if( glewInit() != 0 )
 {
  fprintf( stderr, "Error: GLEW Fault.\n" );
  return 0;
 }
 StartTime = OGGetAbsoluteTime();
 glutSetKeyRepeat( 0x0000 );
 glutDisplayFunc( display );
 glutReshapeFunc( reshape );
 glutIdleFunc( idle );
 glutKeyboardFunc( mykeyfunc );
 glutKeyboardUpFunc( myupfunc );
 glutSpecialFunc( myspecialfunc );
 glutSpecialUpFunc( myspecialupfunc );
 glutMotionFunc( mousemotion );
 glutPassiveMotionFunc( mousepassive );
 glutMouseFunc( mousefunc );
}

variable_types:
title
char *

variable_types: initsizeX int

variable_types: initsizeY int

variable_types: i int


DoGlutLoop

decompiled_code:
// attributes: thunk
__int64 DoGlutLoop()
{
return glutMainLoop();
}

canonical_decompiled_code:
__int64 func11()
{
return glutMainLoop();
}

original_code:
void DoGlutLoop()
{
 glutMainLoop();
}

canonical_original_code:
void DoGlutLoop()
{
 glutMainLoop();
}


SetMouseWarpMode

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

domousewarp = a1;
if ( a1 )
result = glutSetCursor(101LL);
else
result = glutSetCursor(100LL);
return result;
}

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

domousewarp = a1;
if ( a1 )
result = glutSetCursor(101LL);
else
result = glutSetCursor(100LL);
return result;
}

original_code:
void SetMouseWarpMode( int dowarp )
{
 domousewarp = dowarp;
 if( dowarp )
 {
  glutSetCursor( 0x0065 );
 }
 else
 {
  glutSetCursor( 0x0064 );
 }
}

canonical_original_code:
void SetMouseWarpMode(int dowarp)
{
 domousewarp = dowarp;
 if( dowarp )
 {
  glutSetCursor( 0x0065 );
 }
 else
 {
  glutSetCursor( 0x0064 );
 }
}

variable_types: dowarp int


OGSleep

decompiled_code:
unsigned int __fastcall OGSleep(unsigned int seconds)
{
return sleep(seconds);
}

canonical_decompiled_code:
unsigned int __fastcall func0(unsigned int seconds)
{
return sleep(seconds);
}

original_code:
void OGSleep( int is )
{
 sleep( is );
}

canonical_original_code:
void OGSleep(int is)
{
 sleep( is );
}

variable_types: is int


OGUSleep

decompiled_code:
int __fastcall OGUSleep(__useconds_t useconds)
{
return usleep(useconds);
}

canonical_decompiled_code:
int __fastcall func1(__useconds_t useconds)
{
return usleep(useconds);
}

original_code:
void OGUSleep( int ius )
{
 usleep( ius );
}

canonical_original_code:
void OGUSleep(int ius)
{
 usleep( ius );
}

variable_types: ius int


OGGetAbsoluteTime

decompiled_code:
double OGGetAbsoluteTime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

canonical_decompiled_code:
double func2()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double OGGetAbsoluteTime()
{
 struct timeval tv;
 gettimeofday( &tv, 0 );
 return ((double)tv.tv_usec)/1000000. + (tv.tv_sec);
}

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

return_type: double


OGGetFileTime

decompiled_code:
double __fastcall OGGetFileTime(const char *a1)
{
double result; // xmm0_8
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
result = *(double *)&LC1;
else
result = (double)SLODWORD(v2.st_mtim.tv_sec);
return result;
}

canonical_decompiled_code:
double __fastcall func3(const char *a1)
{
double result; // xmm0_8
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
result = *(double *)&LC1;
else
result = (double)SLODWORD(v2.st_mtim.tv_sec);
return result;
}

original_code:
double OGGetFileTime( const char * file )
{
 struct stat buff;
 int r = stat( file, &buff );
 if( r < 0 )
 {
  return -1;
 }
 return buff.st_mtim.tv_sec;
}

canonical_original_code:
double OGGetFileTime(char *file)
{
 struct stat buff;
 int r = stat( file, &buff );
 if( r < 0 )
 {
  return -1;
 }
 return buff.st_mtim.tv_sec;
}

variable_types:
file
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: r int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


OGJoinThread

decompiled_code:
void *__fastcall OGJoinThread(void *ptr)
{
void *thread_return; // [rsp+0h] [rbp-10h] BYREF

if ( !ptr )
return 0LL;
pthread_join(*(_QWORD *)ptr, &thread_return);
free(ptr);
return thread_return;
}

canonical_decompiled_code:
void *__fastcall func4(void *ptr)
{
void *thread_return; // [rsp+0h] [rbp-10h] BYREF

if ( !ptr )
return 0LL;
pthread_join(*(_QWORD *)ptr, &thread_return);
free(ptr);
return thread_return;
}

original_code:
void * OGJoinThread( og_thread_t ot )
{
 void * retval;
 if( !ot )
 {
  return 0;
 }
 pthread_join( *(pthread_t*)ot, &retval );
 free( ot );
 return retval;
}

canonical_original_code:
void *OGJoinThread(void *ot)
{
 void *retval;
 if( !ot )
 {
  return 0;
 }
 pthread_join( *(unsigned long *)ot, &retval );
 free( ot );
 return retval;
}

variable_types:
ot
void *

variable_types:
retval
void *


OGCancelThread

decompiled_code:
void __fastcall OGCancelThread(void *ptr)
{
if ( ptr )
{
pthread_cancel(*(_QWORD *)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
if ( ptr )
{
pthread_cancel(*(_QWORD *)ptr);
free(ptr);
}
}

original_code:
void OGCancelThread( og_thread_t ot )
{
 if( !ot )
 {
  return;
 }
 pthread_cancel( *(pthread_t*)ot );
 free( ot );
}

canonical_original_code:
void OGCancelThread(void *ot)
{
 if( !ot )
 {
  return;
 }
 pthread_cancel( *(unsigned long *)ot );
 free( ot );
}


OGCreateMutex

decompiled_code:
pthread_mutex_t *OGCreateMutex()
{
pthread_mutex_t *v0; // r12
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(v0, &attr);
return v0;
}

canonical_decompiled_code:
pthread_mutex_t *func6()
{
pthread_mutex_t *v0; // r12
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(v0, &attr);
return v0;
}

original_code:
og_mutex_t OGCreateMutex()
{
 pthread_mutexattr_t mta;
 og_mutex_t r = malloc( sizeof( pthread_mutex_t ) );
 pthread_mutexattr_init(&mta);
 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init( (pthread_mutex_t *)r, &mta );
 return r;
}

canonical_original_code:
void *OGCreateMutex()
{
 union pthread_mutexattr_t mta;
 void *r = malloc( sizeof( pthread_mutex_t ) );
 pthread_mutexattr_init(&mta);
 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init( (union pthread_mutex_t *)r, &mta );
 return r;
}

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }

variable_types:
r
void *

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


OGLockMutex

decompiled_code:
int __fastcall OGLockMutex(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_lock(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func7(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_lock(a1);
return result;
}

original_code:
void OGLockMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_lock( (pthread_mutex_t*)om );
}

canonical_original_code:
void OGLockMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_lock( (union pthread_mutex_t *)om );
}

variable_types:
om
void *


OGUnlockMutex

decompiled_code:
int __fastcall OGUnlockMutex(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func8(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

original_code:
void OGUnlockMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_unlock( (pthread_mutex_t*)om );
}

canonical_original_code:
void OGUnlockMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_unlock( (union pthread_mutex_t *)om );
}


OGDeleteMutex

decompiled_code:
void __fastcall OGDeleteMutex(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
free(ptr);
}
}

original_code:
void OGDeleteMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_destroy( (pthread_mutex_t*)om );
 free( om );
}

canonical_original_code:
void OGDeleteMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_destroy( (union pthread_mutex_t *)om );
 free( om );
}


OGCreateSema

decompiled_code:
sem_t *OGCreateSema()
{
sem_t *v0; // r12

v0 = (sem_t *)malloc(0x20uLL);
sem_init(v0, 0, 0);
return v0;
}

canonical_decompiled_code:
sem_t *func10()
{
sem_t *v0; // r12

v0 = (sem_t *)malloc(0x20uLL);
sem_init(v0, 0, 0);
return v0;
}

original_code:
og_sema_t OGCreateSema()
{
 sem_t * sem = malloc( sizeof( sem_t ) );
 sem_init( sem, 0, 0 );
 return (og_sema_t)sem;
}

canonical_original_code:
void *OGCreateSema()
{
 union sem_t *sem = malloc( sizeof( sem_t ) );
 sem_init( sem, 0, 0 );
 return (void *)sem;
}

variable_types: union sem_t { char __size[32]; long __align; } *


OGLockSema

decompiled_code:
// attributes: thunk
int OGLockSema(sem_t *sem)
{
return sem_wait(sem);
}

canonical_decompiled_code:
int func11(sem_t *sem)
{
return sem_wait(sem);
}

original_code:
void OGLockSema( og_sema_t os )
{
 sem_wait( os );
}

canonical_original_code:
void OGLockSema(void *os)
{
 sem_wait( os );
}

variable_types:
os
void *


OGUnlockSema

decompiled_code:
// attributes: thunk
int OGUnlockSema(sem_t *sem)
{
return sem_post(sem);
}

canonical_decompiled_code:
int func12(sem_t *sem)
{
return sem_post(sem);
}

original_code:
void OGUnlockSema( og_sema_t os )
{
 sem_post( os );
}

canonical_original_code:
void OGUnlockSema(void *os)
{
 sem_post( os );
}


OGDeleteSema

decompiled_code:
void __fastcall OGDeleteSema(void *ptr)
{
sem_destroy((sem_t *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func13(void *ptr)
{
sem_destroy((sem_t *)ptr);
free(ptr);
}

original_code:
void OGDeleteSema( og_sema_t os )
{
 sem_destroy( os );
 free(os);
}

canonical_original_code:
void OGDeleteSema(void *os)
{
 sem_destroy( os );
 free(os);
}


CalculateTangentSpace

original_code:
float * CalculateTangentSpace( int Triangles, int VertexCount, int * Indices, float * verts, float * normals, float * texs )
{
 int i;
 float * tans = 0;
 if( normals && texs && verts )
 {
  tans = malloc( VertexCount * sizeof( float ) * 4 );
  memset( tans, 0, VertexCount * sizeof( float ) * 4 );
  float * tan1 = malloc( VertexCount * sizeof( float ) * 3 );
  float * tan2 = malloc( VertexCount * sizeof( float ) * 3 );
  memset( tan1, 0, VertexCount * sizeof( float ) * 3 );
  memset( tan2, 0, VertexCount * sizeof( float ) * 3 );
  for( i = 0; i < Triangles; i++ )
  {
   int v1 = Indices[i*3+0];
   int v2 = Indices[i*3+1];
   int v3 = Indices[i*3+2];
   float * t1 = &texs[v1*3];
   float * t2 = &texs[v2*3];
   float * t3 = &texs[v3*3];
   float * p1 = &verts[v1*3];
   float * p2 = &verts[v2*3];
   float * p3 = &verts[v3*3];
   float vec1[3];
   float vec2[3];
   float tex1[3];
   float tex2[3];
   sub3d( vec1, p2, p1 );
   sub3d( vec2, p3, p1 );
   sub3d( tex1, t2, t1 );
   sub3d( tex2, t3, t1 );
         float r = 1.0f / ( tex1[0] * tex2[1] - tex2[0] * tex1[1] );
   float sdir[3] = {
    (tex2[1] * vec1[0] - tex1[1] * vec2[0]) * r,
    (tex2[1] * vec1[1] - tex1[1] * vec2[1]) * r,
                (tex2[1] * vec1[2] - tex1[1] * vec2[2]) * r };
   float tdir[3] = {
    (tex1[0] * vec2[0] - tex2[0] * vec1[0]) * r,
    (tex1[0] * vec2[1] - tex2[0] * vec1[1]) * r,
                (tex1[0] * vec2[2] - tex2[0] * vec1[2]) * r };
   add3d( &tan1[v1*3], &tan1[v1*3], sdir );
   add3d( &tan1[v2*3], &tan1[v2*3], sdir );
   add3d( &tan1[v3*3], &tan1[v3*3], sdir );
   add3d( &tan2[v1*3], &tan2[v1*3], sdir );
   add3d( &tan2[v2*3], &tan2[v2*3], sdir );
   add3d( &tan2[v3*3], &tan2[v3*3], sdir );
  }
  for (i = 0; i < VertexCount; i++)
  {
      const float * n = &normals[i*3];
      const float * t = &tan1[i*3];
   float tmp[3];
   float tdn[3];
   float thisdot = dot3d( n, t );
   scale3d( tdn, n, thisdot );
   sub3d( tmp, t, tdn );
   normalize3d( &tans[i*4], tmp );
   cross3d( tmp, n, t );
   tans[i*4+3] = (dot3d( tmp, &tan2[i*3] ) < 0)?-1:1;
  }
 }
 return tans;
}

canonical_original_code:
float *CalculateTangentSpace(int Triangles, int VertexCount, int *Indices, float *verts, float *normals, float *texs)
{
 int i;
 float *tans = 0;
 if( normals && texs && verts )
 {
  tans = malloc( VertexCount * sizeof( float ) * 4 );
  memset( tans, 0, VertexCount * sizeof( float ) * 4 );
  float *tan1 = malloc( VertexCount * sizeof( float ) * 3 );
  float *tan2 = malloc( VertexCount * sizeof( float ) * 3 );
  memset( tan1, 0, VertexCount * sizeof( float ) * 3 );
  memset( tan2, 0, VertexCount * sizeof( float ) * 3 );
  for( i = 0; i < Triangles; i++ )
  {
   int v1 = Indices[i*3+0];
   int v2 = Indices[i*3+1];
   int v3 = Indices[i*3+2];
   float *t1 = &texs[v1*3];
   float *t2 = &texs[v2*3];
   float *t3 = &texs[v3*3];
   float *p1 = &verts[v1*3];
   float *p2 = &verts[v2*3];
   float *p3 = &verts[v3*3];
   float vec1[3];
   float vec2[3];
   float tex1[3];
   float tex2[3];
   sub3d( vec1, p2, p1 );
   sub3d( vec2, p3, p1 );
   sub3d( tex1, t2, t1 );
   sub3d( tex2, t3, t1 );
         float r = 1.0f / ( tex1[0] * tex2[1] - tex2[0] * tex1[1] );
   float sdir[3] = {
    (tex2[1] * vec1[0] - tex1[1] * vec2[0]) * r,
    (tex2[1] * vec1[1] - tex1[1] * vec2[1]) * r,
                (tex2[1] * vec1[2] - tex1[1] * vec2[2]) * r };
   float tdir[3] = {
    (tex1[0] * vec2[0] - tex2[0] * vec1[0]) * r,
    (tex1[0] * vec2[1] - tex2[0] * vec1[1]) * r,
                (tex1[0] * vec2[2] - tex2[0] * vec1[2]) * r };
   add3d( &tan1[v1*3], &tan1[v1*3], sdir );
   add3d( &tan1[v2*3], &tan1[v2*3], sdir );
   add3d( &tan1[v3*3], &tan1[v3*3], sdir );
   add3d( &tan2[v1*3], &tan2[v1*3], sdir );
   add3d( &tan2[v2*3], &tan2[v2*3], sdir );
   add3d( &tan2[v3*3], &tan2[v3*3], sdir );
  }
  for (i = 0; i < VertexCount; i++)
  {
      float *n = &normals[i*3];
      float *t = &tan1[i*3];
   float tmp[3];
   float tdn[3];
   float thisdot = dot3d( n, t );
   scale3d( tdn, n, thisdot );
   sub3d( tmp, t, tdn );
   normalize3d( &tans[i*4], tmp );
   cross3d( tmp, n, t );
   tans[i*4+3] = (dot3d( tmp, &tan2[i*3] ) < 0)?-1:1;
  }
 }
 return tans;
}

variable_types: Triangles int

variable_types: VertexCount int

variable_types:
Indices
int *

variable_types:
verts
float *

variable_types:
normals
float *

variable_types:
texs
float *

variable_types:
tans
float *

variable_types: r float

variable_types:
sdir
float [3]

variable_types:
tdir
float [3]

variable_types:
n
float *

variable_types:
t
float *

variable_types:
tmp
float [3]

variable_types:
tdn
float [3]

variable_types: thisdot float

return_type: float *


repo: cobbpg/pindurti

original_code:
void lcd_command(BYTE cmd) {
 if (emu->stop_cnt < lcd->next_w) return;
 if ((cmd & 0xfe) == 0x00) {
  lcd->w_len = cmd & 1;
 } else
 if ((cmd & 0xfe) == 0x02) {
  lcd->on = cmd & 1;
 } else
 if ((cmd & 0xfc) == 0x04) {
  lcd->up_dn = cmd & 1;
  lcd->cnt_sel = (cmd >> 1) & 1;
 } else
 if ((cmd & 0xf8) == 0x10) {
  lcd->amp = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x08) {
  lcd->amp2 = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x18) {
  lcd->test = (cmd >> 2) & 1;
  lcd->ctr = 0x3f;
 } else
 if ((cmd & 0xe0) == 0x20) {
  lcd->y = cmd & 0x1f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0x40) {
  lcd->z = cmd & 0x3f;
 } else
 if ((cmd & 0xc0) == 0x80) {
  lcd->x = cmd & 0x3f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0xc0) {
  lcd->ctr = cmd & 0x3f;
 }
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

canonical_original_code:
void lcd_command(unsigned char cmd) {
 if (emu->stop_cnt < lcd->next_w) return;
 if ((cmd & 0xfe) == 0x00) {
  lcd->w_len = cmd & 1;
 } else
 if ((cmd & 0xfe) == 0x02) {
  lcd->on = cmd & 1;
 } else
 if ((cmd & 0xfc) == 0x04) {
  lcd->up_dn = cmd & 1;
  lcd->cnt_sel = (cmd >> 1) & 1;
 } else
 if ((cmd & 0xf8) == 0x10) {
  lcd->amp = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x08) {
  lcd->amp2 = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x18) {
  lcd->test = (cmd >> 2) & 1;
  lcd->ctr = 0x3f;
 } else
 if ((cmd & 0xe0) == 0x20) {
  lcd->y = cmd & 0x1f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0x40) {
  lcd->z = cmd & 0x3f;
 } else
 if ((cmd & 0xc0) == 0x80) {
  lcd->x = cmd & 0x3f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0xc0) {
  lcd->ctr = cmd & 0x3f;
 }
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

variable_types:
cmd
unsigned char


original_code:
BYTE lcd_status() {
 return ((emu->stop_cnt < lcd->next_w) << 7) | (lcd->w_len << 6) |
  (lcd->on << 5) | (lcd->cnt_sel << 1) | (lcd->up_dn);
}

canonical_original_code:
unsigned char lcd_status() {
 return ((emu->stop_cnt < lcd->next_w) << 7) | (lcd->w_len << 6) |
  (lcd->on << 5) | (lcd->cnt_sel << 1) | (lcd->up_dn);
}


original_code:
void lcd_write(BYTE data) {
 if (emu->stop_cnt < lcd->next_w) return;
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i;
 for (i = 0; i < wl; i++) {
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  lcd->lcnt[adr] = emu->stop_cnt;
  lcd->dat[adr] = (data >> (wl - i - 1)) & 1;
  adr++;
 }
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

canonical_original_code:
void lcd_write(unsigned char data) {
 if (emu->stop_cnt < lcd->next_w) return;
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i;
 for (i = 0; i < wl; i++) {
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  lcd->lcnt[adr] = emu->stop_cnt;
  lcd->dat[adr] = (data >> (wl - i - 1)) & 1;
  adr++;
 }
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

variable_types:
data
unsigned char

variable_types: wl int

variable_types: cn int

variable_types: adr int

variable_types: i int


original_code:
BYTE lcd_read() {
 if (lcd->dummy) { lcd->dummy = 0; return 0; }
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i, v;
 v = 0;
 for (i = 0; i < wl; i++) v |= lcd->dat[adr + i] << (wl - i - 1);
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 return v;
}

canonical_original_code:
unsigned char lcd_read() {
 if (lcd->dummy) { lcd->dummy = 0; return 0; }
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i; int v;
 v = 0;
 for (i = 0; i < wl; i++) v |= lcd->dat[adr + i] << (wl - i - 1);
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 return v;
}

variable_types: v int


original_code:
void lcd_reset() {
 int i;
 lcd->x = lcd->y = lcd->z = lcd->on = lcd->test = lcd->amp = lcd->amp2 = lcd->next_w = 0;
 lcd->up_dn = lcd->cnt_sel = lcd->w_len = lcd->dummy = 1;
 for (i = 0; i < 120 * 64; i++) {
  lcd->dat[i] = lcd->wcnt[i] = lcd->lcnt[i] = 0;
  lcd->scr[i] = 0;
 }
 lcd->ctr = 18;
 lcd->tmin = 25;
 lcd->tjit = 22;
 lcd->sinc = 0.15 * 65535;
 lcd->sdec = 0.07 * 65535;
 lcd->cwht = 0.05 * 65535;
 lcd->cblk = 1.00 * 65535;
 lcd->cmul = 0.05 * 65535;
 lcd->amul = 0.15 * 65535;
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
}


original_code:
void lcd_update() {
 int i, adr;
 int tmp;
 for (i = 0; i < 120 * 64; i++) {
  adr = (i + lcd->z * 120) % (120 * 64);
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  tmp = (lcd->wcnt[adr] << 8) / emu->stop_period;
  tmp = (tmp * lcd->swht + (256 - tmp) * lcd->sblk) >> 8;
  if (lcd->scr[i] > tmp) {
   lcd->scr[i] -= lcd->sdec;
   if (lcd->scr[i] < tmp) lcd->scr[i] = tmp;
  } else
  if (lcd->scr[i] < tmp) {
   lcd->scr[i] += lcd->sinc;
   if (lcd->scr[i] > tmp) lcd->scr[i] = tmp;
  }
  if (lcd->scr[i] < 0) lcd->scr[i] = 0;
  if (lcd->scr[i] > 65535) lcd->scr[i] = 65535;
  lcd->wcnt[adr] = lcd->lcnt[adr] = 0;
 }
 if (lcd->next_w > 0) lcd->next_w -= emu->stop_period;
}

canonical_original_code:
void lcd_update() {
 int i; int adr;
 int tmp;
 for (i = 0; i < 120 * 64; i++) {
  adr = (i + lcd->z * 120) % (120 * 64);
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  tmp = (lcd->wcnt[adr] << 8) / emu->stop_period;
  tmp = (tmp * lcd->swht + (256 - tmp) * lcd->sblk) >> 8;
  if (lcd->scr[i] > tmp) {
   lcd->scr[i] -= lcd->sdec;
   if (lcd->scr[i] < tmp) lcd->scr[i] = tmp;
  } else
  if (lcd->scr[i] < tmp) {
   lcd->scr[i] += lcd->sinc;
   if (lcd->scr[i] > tmp) lcd->scr[i] = tmp;
  }
  if (lcd->scr[i] < 0) lcd->scr[i] = 0;
  if (lcd->scr[i] > 65535) lcd->scr[i] = 65535;
  lcd->wcnt[adr] = lcd->lcnt[adr] = 0;
 }
 if (lcd->next_w > 0) lcd->next_w -= emu->stop_period;
}

variable_types: tmp int


decompiled_code:
void *__fastcall trap_allocate_next_begin(int a1)
{
__int64 v2; // rbp
void *v3; // rdi
void *result; // rax

v2 = SLODWORD(debug_log[75]);
v3 = (void *)debug_log[v2];
if ( v3 )
{
free(v3);
v2 = SLODWORD(debug_log[75]);
}
result = malloc(a1);
debug_log[v2] = (__int64)result;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
__int64 v2; // rbp
void *v3; // rdi
void *result; // rax

v2 = SLODWORD(debug_log[75]);
v3 = (void *)debug_log[v2];
if ( v3 )
{
free(v3);
v2 = SLODWORD(debug_log[75]);
}
result = malloc(a1);
debug_log[v2] = (__int64)result;
return result;
}

original_code:
void trap_allocate_next_begin(int l) {
 if (debug_log.list[debug_log.next] != ((void *)0)) free(debug_log.list[debug_log.next]);
 debug_log.list[debug_log.next] = malloc(l);
}

variable_types: l int


decompiled_code:
__int64 trap_allocate_next_end()
{
__int64 v0; // rbp
__int64 result; // rax

v0 = SLODWORD(debug_log[75]);
*((_DWORD *)&debug_log[50] + v0) = strlen((const char *)debug_log[v0]);
result = (unsigned int)(50 * (((int)v0 + 1) / 50));
LODWORD(debug_log[75]) = ((int)v0 + 1) % 50;
return result;
}

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

v0 = SLODWORD(debug_log[75]);
*((_DWORD *)&debug_log[50] + v0) = strlen((const char *)debug_log[v0]);
result = (unsigned int)(50 * (((int)v0 + 1) / 50));
LODWORD(debug_log[75]) = ((int)v0 + 1) % 50;
return result;
}

original_code:
void trap_allocate_next_end() {
 debug_log.llen[debug_log.next] = strlen(debug_log.list[debug_log.next]);
 debug_log.next = (debug_log.next + 1) % 50;
}


decompiled_code:
__int64 trap_dump_register_contents()
{
trap_allocate_next_begin(134);
sprintf(
(char *)debug_log[SLODWORD(debug_log[75])],
"pc=%04x sp=%04x af=%04x bc=%04x de=%04x hl=%04x\r\n"
"ix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\n"
"r=%02x i=%02x im=%d halt=%d\r\n",
*(unsigned __int16 *)(z80 + 14LL),
*(unsigned __int16 *)(z80 + 12LL),
(unsigned __int16)*z80,
*(unsigned __int16 *)(z80 + 2LL),
*(unsigned __int16 *)(z80 + 4LL),
*(unsigned __int16 *)(z80 + 6LL),
*(unsigned __int16 *)(z80 + 8LL),
*(unsigned __int16 *)(z80 + 10LL),
*(unsigned __int16 *)(z80 + 16LL),
*(unsigned __int16 *)(z80 + 18LL),
*(unsigned __int16 *)(z80 + 20LL),
*(unsigned __int16 *)(z80 + 22LL),
*(unsigned __int8 *)(z80 + 26LL),
*(unsigned __int8 *)(z80 + 27LL),
*(unsigned __int8 *)(z80 + 28LL),
*(unsigned __int8 *)(z80 + 29LL));
return trap_allocate_next_end();
}

original_code:
void trap_dump_register_contents() {
 static const char *dformat = "pc=%04x sp=%04x af=%04x  bc=%04x  de=%04x  hl=%04x\r\nix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\nr=%02x i=%02x im=%d halt=%d\r\n";
 trap_allocate_next_begin(strlen(dformat));
 sprintf(debug_log.list[debug_log.next], dformat,
  z80->pc, z80->sp.w, z80->af.w, z80->bc.w, z80->de.w, z80->hl.w, z80->ix.w, z80->iy.w, z80->afs, z80->bcs, z80->des, z80->hls,
  z80->r, z80->i, z80->im, z80->hlt);
 trap_allocate_next_end();
}

canonical_original_code:
void trap_dump_register_contents() {
 static char *dformat = "pc=%04x sp=%04x af=%04x  bc=%04x  de=%04x  hl=%04x\r\nix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\nr=%02x i=%02x im=%d halt=%d\r\n";
 trap_allocate_next_begin(strlen(dformat));
 sprintf(debug_log.list[debug_log.next], dformat,
  z80->pc, z80->sp.w, z80->af.w, z80->bc.w, z80->de.w, z80->hl.w, z80->ix.w, z80->iy.w, z80->afs, z80->bcs, z80->des, z80->hls,
  z80->r, z80->i, z80->im, z80->hlt);
 trap_allocate_next_end();
}

variable_types:
dformat
char *


decompiled_code:
__int64 trap_printf()
{
int v0; // ebp
int v1; // ebx
char v2; // al
__int64 v3; // rdx
int v4; // eax
__int64 v6; // rdi
int v7; // er12
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rdi
char v11[2040]; // [rsp+0h] [rbp-7F8h] BYREF

v0 = 0;
v1 = *(unsigned __int16 *)(z80 + 6LL);
while ( 1 )
{
while ( 1 )
{
v2 = z80_acc((unsigned __int16)v1);
if ( v2 != 92 )
break;
v6 = (unsigned __int16)(v1 + 1);
v1 += 2;
switch ( (unsigned __int8)z80_acc(v6) )
{
case '!':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v11[v0] = 13;
v11[v0 + 1] = 10;
goto LABEL_12;
case 'A':
if ( v0 > 1995 )
goto LABEL_7;
v9 = (unsigned __int16)*z80;
goto LABEL_17;
case 'B':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 2LL);
goto LABEL_17;
case 'C':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 18LL);
goto LABEL_17;
case 'D':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 4LL);
goto LABEL_17;
case 'E':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 20LL);
goto LABEL_17;
case 'F':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 16LL);
goto LABEL_17;
case 'H':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 6LL);
goto LABEL_17;
case 'L':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 22LL);
goto LABEL_17;
case 'P':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 14LL);
goto LABEL_17;
case 'S':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 12LL);
goto LABEL_17;
case 'X':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 8LL);
goto LABEL_17;
case 'Y':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 10LL);
LABEL_17:
v7 = v0 + 3;
sprintf(&v11[v0], "%04x", v9);
goto LABEL_12;
case '\\':
v11[v0] = 92;
goto LABEL_7;
case 'a':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = (unsigned __int8)*z80;
break;
case 'b':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 2LL);
break;
case 'c':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 3LL);
break;
case 'd':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 4LL);
break;
case 'e':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 5LL);
break;
case 'f':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 1LL);
break;
case 'h':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 6LL);
break;
case 'i':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 27LL);
break;
case 'l':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 7LL);
break;
case 'm':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 2LL);
goto LABEL_61;
case 'n':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 4LL);
goto LABEL_61;
case 'o':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 8LL);
goto LABEL_61;
case 'q':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 10LL);
LABEL_61:
v8 = (unsigned __int8)z80_acc(v10);
break;
case 'r':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 26LL);
break;
case 'w':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 11LL);
break;
case 'x':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 9LL);
break;
case 'y':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 8LL);
break;
case 'z':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 10LL);
break;
default:
goto LABEL_7;
}
sprintf(&v11[v0], "%02x", v8);
LABEL_12:
v0 = v7 + 1;
}
v3 = v0;
v11[v0] = v2;
if ( !v2 )
break;
++v1;
LABEL_7:
if ( ++v0 == 2000 )
{
v3 = 2000LL;
break;
}
}
v11[v3] = 0;
v4 = strlen(v11);
trap_allocate_next_begin(v4);
strcpy((char *)debug_log[SLODWORD(debug_log[75])], v11);
return trap_allocate_next_end();
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // ebp
int v1; // ebx
char v2; // al
__int64 v3; // rdx
int v4; // eax
__int64 v6; // rdi
int v7; // er12
__int64 v8; // rdx
__int64 v9; // rdx
__int64 v10; // rdi
char v11[2040]; // [rsp+0h] [rbp-7F8h] BYREF

v0 = 0;
v1 = *(unsigned __int16 *)(z80 + 6LL);
while ( 1 )
{
while ( 1 )
{
v2 = z80_acc((unsigned __int16)v1);
if ( v2 != 92 )
break;
v6 = (unsigned __int16)(v1 + 1);
v1 += 2;
switch ( (unsigned __int8)z80_acc(v6) )
{
case '!':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v11[v0] = 13;
v11[v0 + 1] = 10;
goto LABEL_12;
case 'A':
if ( v0 > 1995 )
goto LABEL_7;
v9 = (unsigned __int16)*z80;
goto LABEL_17;
case 'B':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 2LL);
goto LABEL_17;
case 'C':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 18LL);
goto LABEL_17;
case 'D':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 4LL);
goto LABEL_17;
case 'E':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 20LL);
goto LABEL_17;
case 'F':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 16LL);
goto LABEL_17;
case 'H':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 6LL);
goto LABEL_17;
case 'L':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 22LL);
goto LABEL_17;
case 'P':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 14LL);
goto LABEL_17;
case 'S':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 12LL);
goto LABEL_17;
case 'X':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 8LL);
goto LABEL_17;
case 'Y':
if ( v0 > 1995 )
goto LABEL_7;
v9 = *(unsigned __int16 *)(z80 + 10LL);
LABEL_17:
v7 = v0 + 3;
sprintf(&v11[v0], "%04x", v9);
goto LABEL_12;
case '\\':
v11[v0] = 92;
goto LABEL_7;
case 'a':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = (unsigned __int8)*z80;
break;
case 'b':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 2LL);
break;
case 'c':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 3LL);
break;
case 'd':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 4LL);
break;
case 'e':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 5LL);
break;
case 'f':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 1LL);
break;
case 'h':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 6LL);
break;
case 'i':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 27LL);
break;
case 'l':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 7LL);
break;
case 'm':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 2LL);
goto LABEL_61;
case 'n':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 4LL);
goto LABEL_61;
case 'o':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 8LL);
goto LABEL_61;
case 'q':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v10 = *(unsigned __int16 *)(z80 + 10LL);
LABEL_61:
v8 = (unsigned __int8)z80_acc(v10);
break;
case 'r':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 26LL);
break;
case 'w':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 11LL);
break;
case 'x':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 9LL);
break;
case 'y':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 8LL);
break;
case 'z':
if ( v0 > 1997 )
goto LABEL_7;
v7 = v0 + 1;
v8 = *(unsigned __int8 *)(z80 + 10LL);
break;
default:
goto LABEL_7;
}
sprintf(&v11[v0], "%02x", v8);
LABEL_12:
v0 = v7 + 1;
}
v3 = v0;
v11[v0] = v2;
if ( !v2 )
break;
++v1;
LABEL_7:
if ( ++v0 == 2000 )
{
v3 = 2000LL;
break;
}
}
v11[v3] = 0;
v4 = strlen(v11);
func0(v4);
strcpy((char *)debug_log[SLODWORD(debug_log[75])], v11);
return func1();
}

variable_types:
buf
char [-1]

variable_types:
ptr
unsigned short

variable_types:
c
unsigned char


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

result = 0LL;
LODWORD(debug_log[75]) = 0;
memset(debug_log, 0, 0x258uLL);
return result;
}

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

result = 0LL;
LODWORD(debug_log[75]) = 0;
memset(debug_log, 0, 0x258uLL);
return result;
}

original_code:
void debug_trap_init() {
 int i;
 for (i = 0; i < 50; i++) {
  debug_log.list[i] = ((void *)0);
  debug_log.llen[i] = 0;
 }
 debug_log.next = 0;
}


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

if ( !a1 )
return trap_dump_register_contents();
if ( a1 == 1 )
result = trap_printf();
return result;
}

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

if ( !a1 )
return func2();
if ( a1 == 1 )
result = func3();
return result;
}

original_code:
void execute_trap(BYTE id) {
 switch (id) {
 case 0x00: trap_dump_register_contents(); break;
 case 0x01: trap_printf(); break;
 }
}

canonical_original_code:
void execute_trap(unsigned char id) {
 switch (id) {
 case 0x00: trap_dump_register_contents(); break;
 case 0x01: trap_printf(); break;
 }
}

variable_types:
id
unsigned char


original_code:
void insert_hex(char *pos, int len) {
 memmove(pos + 5, pos + 1, 16);
 pos[0] = '$';
 pos[1] = '%';
 pos[2] = '0';
 pos[3] = '0' + len;
 pos[4] = 'x';
}

variable_types:
pos
char *

variable_types: len int