loop

decompiled_code:
__int64 __fastcall loop(int a1, __int64 a2)
{
__int64 i; // rbx
unsigned int v3; // er14
__int64 j; // rbx
int v6; // eax
int v7; // ebp
int v8; // er12
__int64 k; // rbx
__int64 v10[11]; // [rsp+0h] [rbp-58h]

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
fwrite("\n(use ctrl-c to stop)\n\n", 1uLL, 0x17uLL, _bss_start);
fwrite("ADC ", 1uLL, 0xDuLL, _bss_start);
for ( i = 0LL; i != 6; ++i )
{
if ( *(_DWORD *)(a2 + 4 * i) )
fprintf(_bss_start, " %d", (unsigned int)(i + 2));
}
fputc(10, _bss_start);
v3 = abort_read;
if ( !abort_read )
{
while ( 1 )
{
for ( j = 0LL; j != 6; ++j )
{
if ( *(_DWORD *)(a2 + 4 * j) )
{
v6 = open_adc((int)j + 2);
v7 = v6;
if ( v6 < 0 )
break;
v8 = read_adc(v6);
*((_DWORD *)v10 + j) = v8;
close(v7);
if ( v8 == -100000 )
break;
}
}
if ( a1 > 9 )
{
if ( a1 > 49 || (v3 & 7) == 0 )
{
LABEL_14:
fprintf(_bss_start, "\rRead %8d: ", v3);
for ( k = 0LL; k != 24; k += 4LL )
{
if ( *(_DWORD *)(a2 + k) )
fprintf(_bss_start, " %4d ", *(unsigned int *)((char *)v10 + k));
}
fflush(_bss_start);
goto LABEL_22;
}
}
else if ( (v3 & 0x1F) == 0 )
{
goto LABEL_14;
}
LABEL_22:
++v3;
msleep(a1);
if ( abort_read )
goto LABEL_7;
}
}
v3 = 0;
LABEL_7:
fwrite("\n\n", 1uLL, 2uLL, _bss_start);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, __int64 a2)
{
__int64 i; // rbx
unsigned int v3; // er14
__int64 j; // rbx
int v6; // eax
int v7; // ebp
int v8; // er12
__int64 k; // rbx
__int64 v10[11]; // [rsp+0h] [rbp-58h]

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
fwrite("\n(use ctrl-c to stop)\n\n", 1uLL, 0x17uLL, _bss_start);
fwrite("ADC ", 1uLL, 0xDuLL, _bss_start);
for ( i = 0LL; i != 6; ++i )
{
if ( *(_DWORD *)(a2 + 4 * i) )
fprintf(_bss_start, " %d", (unsigned int)(i + 2));
}
fputc(10, _bss_start);
v3 = abort_read;
if ( !abort_read )
{
while ( 1 )
{
for ( j = 0LL; j != 6; ++j )
{
if ( *(_DWORD *)(a2 + 4 * j) )
{
v6 = func11((int)j + 2);
v7 = v6;
if ( v6 < 0 )
break;
v8 = func10(v6);
*((_DWORD *)v10 + j) = v8;
close(v7);
if ( v8 == -100000 )
break;
}
}
if ( a1 > 9 )
{
if ( a1 > 49 || (v3 & 7) == 0 )
{
LABEL_14:
fprintf(_bss_start, "\rRead %8d: ", v3);
for ( k = 0LL; k != 24; k += 4LL )
{
if ( *(_DWORD *)(a2 + k) )
fprintf(_bss_start, " %4d ", *(unsigned int *)((char *)v10 + k));
}
fflush(_bss_start);
goto LABEL_22;
}
}
else if ( (v3 & 0x1F) == 0 )
{
goto LABEL_14;
}
LABEL_22:
++v3;
func13(a1);
if ( abort_read )
goto LABEL_7;
}
}
v3 = 0;
LABEL_7:
fwrite("\n\n", 1uLL, 2uLL, _bss_start);
return v3;
}

original_code:
int loop(int delay_ms, int *list)
{
    int fd, count, i, update;
    int val[6];
    count = 0;
    memset(val, 0, sizeof(val));
    fprintf(stdout, "\n(use ctrl-c to stop)\n\n");
    fprintf(stdout, "ADC          ");
    for (i = 0; i < 6; i++) {
        if (list[i])
            fprintf(stdout, "      %d", i + 2);
    }
    fprintf(stdout, "\n");
    while (!abort_read) {
        for (i = 0; i < 6; i++) {
            if (!list[i])
                continue;
            fd = open_adc(i + 2);
            if (fd < 0)
                break;;
            val[i] = read_adc(fd);
            close(fd);
            if (val[i] == -100000)
                break;
        }
        update = 0;
        if (delay_ms < 10) {
            if ((count & 0x1f) == 0)
                update = 1;
        }
        else if (delay_ms < 50) {
            if ((count & 0x07) == 0)
                update = 1;
        }
        else {
            update = 1;
        }
        if (update) {
            fprintf(stdout, "\rRead %8d: ", count);
            for (i = 0; i < 6; i++) {
                if (list[i])
                    fprintf(stdout, " %4d  ", val[i]);
            }
            fflush(stdout);
        }
        count++;
        msleep(delay_ms);
    }
    fprintf(stdout, "\n\n");
    return count;
}

canonical_original_code:
int loop(int delay_ms, int *list)
{
    int fd; int count; int i; int update;
    int val[6];
    count = 0;
    memset(val, 0, sizeof(val));
    fprintf(stdout, "\n(use ctrl-c to stop)\n\n");
    fprintf(stdout, "ADC          ");
    for (i = 0; i < 6; i++) {
        if (list[i])
            fprintf(stdout, "      %d", i + 2);
    }
    fprintf(stdout, "\n");
    while (!abort_read) {
        for (i = 0; i < 6; i++) {
            if (!list[i])
                continue;
            fd = open_adc(i + 2);
            if (fd < 0)
                break;;
            val[i] = read_adc(fd);
            close(fd);
            if (val[i] == -100000)
                break;
        }
        update = 0;
        if (delay_ms < 10) {
            if ((count & 0x1f) == 0)
                update = 1;
        }
        else if (delay_ms < 50) {
            if ((count & 0x07) == 0)
                update = 1;
        }
        else {
            update = 1;
        }
        if (update) {
            fprintf(stdout, "\rRead %8d: ", count);
            for (i = 0; i < 6; i++) {
                if (list[i])
                    fprintf(stdout, " %4d  ", val[i]);
            }
            fflush(stdout);
        }
        count++;
        msleep(delay_ms);
    }
    fprintf(stdout, "\n\n");
    return count;
}

variable_types:
list
int *

variable_types: i int

variable_types: update int

variable_types:
val
int [6]


main

canonical_name: main

repo: scottellis/polladc

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
int v5; // eax
const char **v6; // r12
const char **v7; // r13
unsigned int v8; // eax
int v9; // ebx
int result; // eax
__int64 v11[4]; // [rsp+0h] [rbp-68h] BYREF
struct timeval v12; // [rsp+20h] [rbp-48h] BYREF
struct timeval tv; // [rsp+30h] [rbp-38h] BYREF

register_sig_handler();
v3 = 50;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d:h");
if ( v4 == -1 )
break;
if ( v4 != 100 )
usage(*argv);
v5 = atoi(optarg);
v3 = v5;
if ( v5 <= 0 )
{
printf("Invalid delay %d. Must be greater then zero.\n", (unsigned int)v5);
usage(*argv);
}
}
if ( optind == argc )
{
puts("A list of ADCs is required");
usage(*argv);
}
v11[0] = 0LL;
v11[1] = 0LL;
v11[2] = 0LL;
if ( optind < argc )
{
v6 = &argv[optind];
v7 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v8 = atoi(*v6);
if ( v8 - 2 > 5 )
{
printf("adc %d is out of range\n", v8);
usage(*argv);
}
if ( *((_DWORD *)v11 + (int)(v8 - 2)) )
{
printf("adc %d listed more then once\n", v8);
usage(*argv);
}
*((_DWORD *)v11 + (int)(v8 - 2)) = 1;
++v6;
}
while ( v6 != v7 );
}
if ( gettimeofday(&tv, 0LL) < 0 )
{
perror("gettimeofday: start");
result = 1;
}
else
{
v9 = loop(v3, (__int64)v11);
if ( gettimeofday(&v12, 0LL) < 0 )
perror("gettimeofday: end");
else
show_elapsed((__int64)&tv, &v12, v9);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
int v5; // eax
const char **v6; // r12
const char **v7; // r13
unsigned int v8; // eax
int v9; // ebx
int result; // eax
__int64 v11[4]; // [rsp+0h] [rbp-68h] BYREF
struct timeval v12; // [rsp+20h] [rbp-48h] BYREF
struct timeval tv; // [rsp+30h] [rbp-38h] BYREF

func12();
v3 = 50;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d:h");
if ( v4 == -1 )
break;
if ( v4 != 100 )
func8(*argv);
v5 = atoi(optarg);
v3 = v5;
if ( v5 <= 0 )
{
printf("Invalid delay %d. Must be greater then zero.\n", (unsigned int)v5);
func8(*argv);
}
}
if ( optind == argc )
{
puts("A list of ADCs is required");
func8(*argv);
}
v11[0] = 0LL;
v11[1] = 0LL;
v11[2] = 0LL;
if ( optind < argc )
{
v6 = &argv[optind];
v7 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v8 = atoi(*v6);
if ( v8 - 2 > 5 )
{
printf("adc %d is out of range\n", v8);
func8(*argv);
}
if ( *((_DWORD *)v11 + (int)(v8 - 2)) )
{
printf("adc %d listed more then once\n", v8);
func8(*argv);
}
*((_DWORD *)v11 + (int)(v8 - 2)) = 1;
++v6;
}
while ( v6 != v7 );
}
if ( gettimeofday(&tv, 0LL) < 0 )
{
perror("gettimeofday: start");
result = 1;
}
else
{
v9 = func14(v3, (__int64)v11);
if ( gettimeofday(&v12, 0LL) < 0 )
perror("gettimeofday: end");
else
func9((__int64)&tv, &v12, v9);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
    int opt, delay_ms, adc, i;
    struct timeval start, end;
    int adc_list[6];
    register_sig_handler();
    delay_ms = 50;
    while ((opt = getopt(argc, argv, "d:h")) != -1) {
        switch (opt) {
        case 'd':
            delay_ms = atoi(optarg);
            if (delay_ms < 1) {
                printf("Invalid delay %d. Must be greater then zero.\n", delay_ms);
                usage(argv[0]);
            }
   break;
        case 'h':
        default:
            usage(argv[0]);
            break;
        }
    }
    if (optind == argc) {
        printf("A list of ADCs is required\n");
        usage(argv[0]);
    }
    memset(adc_list, 0, sizeof(adc_list));
    for (i = optind; i < argc; i++) {
        adc = atoi(argv[i]);
        if (adc < 2 || adc > 7) {
            printf("adc %d is out of range\n", adc);
            usage(argv[0]);
        }
        adc -= 2;
        if (adc_list[adc]) {
            printf("adc %d listed more then once\n", adc + 2);
            usage(argv[0]);
        }
        adc_list[adc] = 1;
    }
    if (gettimeofday(&start, ((void *)0)) < 0) {
        perror("gettimeofday: start");
        return 1;
    }
    int count = loop(delay_ms, adc_list);
    if (gettimeofday(&end, ((void *)0)) < 0)
        perror("gettimeofday: end");
    else
        show_elapsed(&start, &end, count);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int opt; int delay_ms; int adc; int i;
    struct timeval start; struct timeval end;
    int adc_list[6];
    register_sig_handler();
    delay_ms = 50;
    while ((opt = getopt(argc, argv, "d:h")) != -1) {
        switch (opt) {
        case 'd':
            delay_ms = atoi(optarg);
            if (delay_ms < 1) {
                printf("Invalid delay %d. Must be greater then zero.\n", delay_ms);
                usage(argv[0]);
            }
   break;
        case 'h':
        default:
            usage(argv[0]);
            break;
        }
    }
    if (optind == argc) {
        printf("A list of ADCs is required\n");
        usage(argv[0]);
    }
    memset(adc_list, 0, sizeof(adc_list));
    for (i = optind; i < argc; i++) {
        adc = atoi(argv[i]);
        if (adc < 2 || adc > 7) {
            printf("adc %d is out of range\n", adc);
            usage(argv[0]);
        }
        adc -= 2;
        if (adc_list[adc]) {
            printf("adc %d listed more then once\n", adc + 2);
            usage(argv[0]);
        }
        adc_list[adc] = 1;
    }
    if (gettimeofday(&start, ((void *)0)) < 0) {
        perror("gettimeofday: start");
        return 1;
    }
    int count = loop(delay_ms, adc_list);
    if (gettimeofday(&end, ((void *)0)) < 0)
        perror("gettimeofday: end");
    else
        show_elapsed(&start, &end, count);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: adc int

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

variable_types: count int

return_type: int


repo: scottgw/quicksilver

decompiled_code:
int __fastcall join_executor(__int64 a1)
{
return pthread_join(*(_QWORD *)(a1 + 24), 0LL);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
return pthread_join(*(_QWORD *)(a1 + 24), 0LL);
}

original_code:
static
void
join_executor(void* elem, void* user)
{
  executor_t exec = (executor_t)elem;
  pthread_join(exec->thread, ((void *)0));
}

canonical_original_code:
static void join_executor(void *elem, void *user)
{
  struct executor *exec = (executor_t)elem;
  pthread_join(exec->thread, ((void *)0));
}

variable_types:
elem
void *

variable_types:
user
void *

variable_types: struct executor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct list { void **data; int start; int end; int size; } *work; struct processor { struct task *task; struct executor *executor; int id; } *current_proc; unsigned long thread; } *

return_type: void

user_defined_types: struct executor { struct task *task; struct list *work; struct processor *current_proc; unsigned long thread; }

user_defined_types: struct task { void *base; struct ucontext_t ctx; enum task_state state; struct task *next; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }


decompiled_code:
void *__fastcall executor_run(void *a1)
{
ucontext_t ucp; // [rsp+20h] [rbp-3D8h] BYREF

getcontext(&ucp);
*(_QWORD *)(*(_QWORD *)a1 + 16LL) = &ucp;
task_set_func(*(_QWORD *)a1, executor_loop, a1);
task_run(*(_QWORD *)a1);
puts("end exec loop");
notifier_done = 1;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
ucontext_t ucp; // [rsp+20h] [rbp-3D8h] BYREF

getcontext(&ucp);
*(_QWORD *)(*(_QWORD *)a1 + 16LL) = &ucp;
task_set_func(*(_QWORD *)a1, executor_loop, a1);
task_run(*(_QWORD *)a1);
puts("end exec loop");
notifier_done = 1;
return 0LL;
}

original_code:
static
void*
executor_run(void* data)
{
  executor_t exec = data;
  ucontext_t loop_ctx;
  int volatile flag = 0;
  getcontext(&loop_ctx);
  if (flag == 0)
    {
      flag = 1;
      exec->task->ctx.uc_link = &loop_ctx;
      task_set_func(exec->task, executor_loop, exec);
      task_run(exec->task);
    }
  printf("end exec loop\n");
  notifier_done = 1;
  return ((void *)0);
}

canonical_original_code:
static void *executor_run(void *data)
{
  struct executor *exec = data;
  struct ucontext_t loop_ctx;
  int flag = 0;
  getcontext(&loop_ctx);
  if (flag == 0)
    {
      flag = 1;
      exec->task->ctx.uc_link = &loop_ctx;
      task_set_func(exec->task, executor_loop, exec);
      task_run(exec->task);
    }
  printf("end exec loop\n");
  notifier_done = 1;
  return ((void *)0);
}

variable_types:
data
void *

variable_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

variable_types: flag int

return_type: void *

user_defined_types: struct list { void **data; int start; int end; int size; }

user_defined_types: struct processor { struct task *task; struct executor *executor; int id; }

user_defined_types: enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 }


decompiled_code:
__int64 __fastcall switch_to_next_processor(_QWORD *a1)
{
_QWORD *v1; // rbp
__int64 result; // rax

if ( (int)list_size(a1[1]) <= 0 )
_assert_fail("list_size(exec->work) > 0", "executor.c", 0x15u, "switch_to_next_processor");
v1 = (_QWORD *)list_take(a1[1]);
v1[1] = a1;
a1[2] = v1;
*(_QWORD *)(*v1 + 984LL) = *a1;
yield_to(*a1, *v1);
result = *(unsigned int *)(*v1 + 976LL);
if ( (_DWORD)result != 3 )
result = list_add(a1[1], v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
_QWORD *v1; // rbp
__int64 result; // rax

if ( (int)list_size(a1[1]) <= 0 )
_assert_fail("list_size(exec->work) > 0", "executor.c", 0x15u, "switch_to_next_processor");
v1 = (_QWORD *)list_take(a1[1]);
v1[1] = a1;
a1[2] = v1;
*(_QWORD *)(*v1 + 984LL) = *a1;
yield_to(*a1, *v1);
result = *(unsigned int *)(*v1 + 976LL);
if ( (_DWORD)result != 3 )
result = list_add(a1[1], v1);
return result;
}

original_code:
static
void
switch_to_next_processor(executor_t exec)
{
  ((void) sizeof ((list_size(exec->work) > 0) ? 1 : 0), __extension__ ({ if (list_size(exec->work) > 0) ; else __assert_fail ("list_size(exec->work) > 0", "executor.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  processor_t proc = list_take(exec->work);
  proc->executor = exec;
  exec->current_proc = proc;
  proc->task->next = exec->task;
  yield_to(exec->task, proc->task);
  if(proc->task->state != TASK_FINISHED)
    list_add(exec->work, proc);
}

canonical_original_code:
static void switch_to_next_processor(struct executor *exec)
{
  ((void) sizeof ((list_size(exec->work) > 0) ? 1 : 0), __extension__ ({ if (list_size(exec->work) > 0) ; else __assert_fail ("list_size(exec->work) > 0", "executor.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  struct processor *proc = list_take(exec->work);
  proc->executor = exec;
  exec->current_proc = proc;
  proc->task->next = exec->task;
  yield_to(exec->task, proc->task);
  if(proc->task->state != TASK_FINISHED)
    list_add(exec->work, proc);
}

variable_types: struct processor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct executor { struct task *task; struct list { void **data; int start; int end; int size; } *work; struct processor *current_proc; unsigned long thread; } *executor; int id; } *


decompiled_code:
__int64 __fastcall executor_loop(_QWORD *a1)
{
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v2 = 0;
puts("Executor running");
do
{
if ( (int)list_size(a1[1]) > 0 )
{
switch_to_next_processor(a1);
}
else
{
puts("no workers");
v2 = 1;
}
result = v2;
}
while ( !v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v2 = 0;
puts("Executor running");
do
{
if ( (int)list_size(a1[1]) > 0 )
{
func2(a1);
}
else
{
puts("no workers");
v2 = 1;
}
result = v2;
}
while ( !v2 );
return result;
}

original_code:
static
void
executor_loop(void* data)
{
  executor_t exec = (executor_t) data;
  int volatile done = 0;
  printf("Executor running\n");
  while(done == 0)
    {
      if (list_size(exec->work) > 0)
        {
          switch_to_next_processor(exec);
        }
      else
        {
          printf("no workers\n");
          done = 1;
        }
    }
}

canonical_original_code:
static void executor_loop(void *data)
{
  struct executor *exec = (executor_t) data;
  int done = 0;
  printf("Executor running\n");
  while(done == 0)
    {
      if (list_size(exec->work) > 0)
        {
          switch_to_next_processor(exec);
        }
      else
        {
          printf("no workers\n");
          done = 1;
        }
    }
}

variable_types: done int

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; }

user_defined_types: struct _libc_xmmreg { unsigned int element[4]; }


decompiled_code:
_QWORD *make_executor()
{
_QWORD *v0; // rbx

v0 = malloc(8uLL);
*v0 = task_make();
v0[2] = 0LL;
return v0;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *v0; // rbx

v0 = malloc(8uLL);
*v0 = task_make();
v0[2] = 0LL;
return v0;
}

original_code:
executor_t
make_executor()
{
  executor_t exec = malloc(sizeof(executor_t));
  exec->task = task_make();
  exec->current_proc = ((void *)0);
  return exec;
}

canonical_original_code:
struct executor *make_executor()
{
  struct executor *exec = malloc(sizeof(executor_t));
  exec->task = task_make();
  exec->current_proc = ((void *)0);
  return exec;
}

return_type: struct executor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct list { void **data; int start; int end; int size; } *work; struct processor { struct task *task; struct executor *executor; int id; } *current_proc; unsigned long thread; } *


decompiled_code:
__int64 join_executors()
{
return list_foreach(executors, join_executor, 0LL);
}

canonical_decompiled_code:
__int64 func5()
{
return list_foreach(executors, join_executor, 0LL);
}

original_code:
void
join_executors()
{
  list_foreach(executors, join_executor, ((void *)0));
}


decompiled_code:
int __fastcall create_executors(pthread_t a1, int a2)
{
__int64 v2; // rax
int i; // ebp
pthread_t *v4; // rbx

v2 = list_make();
executors = v2;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
v4 = make_executor();
v4[1] = a1;
list_add(executors, v4);
LODWORD(v2) = pthread_create(v4 + 3, 0LL, executor_run, v4);
}
}
return v2;
}

canonical_decompiled_code:
int __fastcall func6(pthread_t a1, int a2)
{
__int64 v2; // rax
int i; // ebp
pthread_t *v4; // rbx

v2 = list_make();
executors = v2;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
v4 = func4();
v4[1] = a1;
list_add(executors, v4);
LODWORD(v2) = pthread_create(v4 + 3, 0LL, executor_run, v4);
}
}
return v2;
}

original_code:
void
create_executors(list_t work, int n)
{
  executors = list_make();
  for(int i = 0; i < n; i++)
    {
      executor_t exec = make_executor();
      exec->work = work;
      list_add(executors, exec);
      pthread_create(&exec->thread, ((void *)0), executor_run, exec);
    }
}

canonical_original_code:
void create_executors(struct list *work, int n)
{
  executors = list_make();
  for(int i = 0; i < n; i++)
    {
      struct executor *exec = make_executor();
      exec->work = work;
      list_add(executors, exec);
      pthread_create(&exec->thread, ((void *)0), executor_run, exec);
    }
}

variable_types: struct list { void **data; int start; int end; int size; } *

variable_types: n int

variable_types: i int


decompiled_code:
_DWORD *list_make()
{
_DWORD *v0; // rbx

v0 = malloc(0x18uLL);
*(_QWORD *)v0 = malloc(0x2000uLL);
v0[2] = 0;
v0[3] = 0;
v0[4] = 0;
return v0;
}

original_code:
list_t
list_make()
{
  list_t list = (list_t)malloc(sizeof(struct list));
  list->data = malloc(1024*sizeof(void*));
  list->start = 0;
  list->end = 0;
  list->size = 0;
  return list;
}

canonical_original_code:
struct list *list_make()
{
  struct list *list = (list_t)malloc(sizeof(struct list));
  list->data = malloc(1024*sizeof(void *));
  list->start = 0;
  list->end = 0;
  list->size = 0;
  return list;
}

return_type: struct list { void **data; int start; int end; int size; } *


original_code:
void*
list_take(list_t list)
{
  void* data;
  ((void) sizeof ((list->size != 0) ? 1 : 0), __extension__ ({ if (list->size != 0) ; else __assert_fail ("list->size != 0", "list.c", 23, __extension__ __PRETTY_FUNCTION__); }));
  data = list->data[list->start];
  list->start = (list->start + 1) % 1024;
  list->size--;
  return data;
}

canonical_original_code:
void *list_take(struct list *list)
{
  void *data;
  ((void) sizeof ((list->size != 0) ? 1 : 0), __extension__ ({ if (list->size != 0) ; else __assert_fail ("list->size != 0", "list.c", 23, __extension__ __PRETTY_FUNCTION__); }));
  data = list->data[list->start];
  list->start = (list->start + 1) % 1024;
  list->size--;
  return data;
}


decompiled_code:
__int64 __fastcall list_foreach(__int64 a1, __int64 (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
__int64 result; // rax
int v5; // edx
__int64 v6; // rbx
unsigned __int64 v7; // r14

result = *(unsigned int *)(a1 + 12);
v5 = *(_DWORD *)(a1 + 8);
if ( (int)result > v5 )
{
v6 = 8LL * v5;
v7 = 8 * (v5 + (unsigned __int64)(unsigned int)(result - 1 - v5) + 1);
do
{
result = a2(*(_QWORD *)(*(_QWORD *)a1 + v6), a3);
v6 += 8LL;
}
while ( v6 != v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
__int64 result; // rax
int v5; // edx
__int64 v6; // rbx
unsigned __int64 v7; // r14

result = *(unsigned int *)(a1 + 12);
v5 = *(_DWORD *)(a1 + 8);
if ( (int)result > v5 )
{
v6 = 8LL * v5;
v7 = 8 * (v5 + (unsigned __int64)(unsigned int)(result - 1 - v5) + 1);
do
{
result = a2(*(_QWORD *)(*(_QWORD *)a1 + v6), a3);
v6 += 8LL;
}
while ( v6 != v7 );
}
return result;
}

original_code:
void
list_foreach(list_t list, void (*func)(void*, void*), void* user_data)
{
  int end = list->end;
  for(int i = list->start; i < end; i++)
    {
      func(list->data[i], user_data);
    }
}

canonical_original_code:
void list_foreach(struct list *list, void (*func)(void *, void *), void *user_data)
{
  int end = list->end;
  for(int i = list->start; i < end; i++)
    {
      func(list->data[i], user_data);
    }
}

variable_types:
func
void (*)(void *, void *)

variable_types: end int


original_code:
void
list_add(list_t list, void* data)
{
  *(list->data + list->end) = data;
  list->size++;
  list->end = (list->end + 1) % 1024;
}

canonical_original_code:
void list_add(struct list *list, void *data)
{
  *(list->data + list->end) = data;
  list->size++;
  list->end = (list->end + 1) % 1024;
}


decompiled_code:
__int64 __fastcall list_size(__int64 a1)
{
if ( !a1 )
_assert_fail("list != NULL", "list.c", 0x33u, "list_size");
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
if ( !a1 )
_assert_fail("list != NULL", "list.c", 0x33u, "list_size");
return *(unsigned int *)(a1 + 16);
}

original_code:
int
list_size(list_t list)
{
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  return list->size;
}

canonical_original_code:
int list_size(struct list *list)
{
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  return list->size;
}


decompiled_code:
void __fastcall list_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

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

original_code:
void
list_free(list_t list)
{
  free(list->data);
  free(list);
}

canonical_original_code:
void list_free(struct list *list)
{
  free(list->data);
  free(list);
}


decompiled_code:
void __noreturn usr_handler()
{
puts("USR1 Handled");
pthread_exit(0LL);
}

canonical_decompiled_code:
void __noreturn func8()
{
puts("USR1 Handled");
pthread_exit(0LL);
}

original_code:
void
usr_handler(int x, siginfo_t *info, void* data)
{
  printf("USR1 Handled\n");
  pthread_exit(((void *)0));
}

canonical_original_code:
void usr_handler(int x, struct siginfo_t *info, void *data)
{
  printf("USR1 Handled\n");
  pthread_exit(((void *)0));
}

variable_types: x int

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

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


original_code:
void
setup_handler(pthread_t *threads)
{
  struct sigaction alarm_action;
  sigset_t mask;
  sigemptyset(&mask);
  alarm_action.__sigaction_handler.sa_handler = timer_handler;
  alarm_action.sa_mask = mask;
  alarm_action.sa_flags = 0;
  sigaction_threads = threads;
  sigaction (14, &alarm_action, ((void *)0));
}

canonical_original_code:
void setup_handler(unsigned long *threads)
{
  struct sigaction alarm_action;
  struct __sigset_t mask;
  sigemptyset(&mask);
  alarm_action.__sigaction_handler.sa_handler = timer_handler;
  alarm_action.sa_mask = mask;
  alarm_action.sa_flags = 0;
  sigaction_threads = threads;
  sigaction (14, &alarm_action, ((void *)0));
}

variable_types:
threads
unsigned long *

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

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


original_code:
void
setup_usr_handler()
{
  struct sigaction usr_action;
  sigset_t mask;
  sigemptyset(&mask);
  usr_action.__sigaction_handler.sa_sigaction = usr_handler;
  usr_action.sa_mask = mask;
  usr_action.sa_flags = 0;
  sigaction (10, &usr_action, ((void *)0));
}

canonical_original_code:
void setup_usr_handler()
{
  struct sigaction usr_action;
  struct __sigset_t mask;
  sigemptyset(&mask);
  usr_action.__sigaction_handler.sa_sigaction = usr_handler;
  usr_action.sa_mask = mask;
  usr_action.sa_flags = 0;
  sigaction (10, &usr_action, ((void *)0));
}

variable_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: union sigval { int sival_int; void *sival_ptr; }


decompiled_code:
void *__fastcall thread_run(void *a1)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, 14);
pthread_sigmask(0, &v2, 0LL);
setup_usr_handler();
fib(45);
puts("After thread");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func13(void *a1)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, 14);
pthread_sigmask(0, &v2, 0LL);
func12();
func9(45);
puts("After thread");
return 0LL;
}

original_code:
void*
thread_run(void* ptr)
{
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(0, &mask, ((void *)0));
  setup_usr_handler();
  fib(45);
  printf("After thread\n");
  return ((void *)0);
}

canonical_original_code:
void *thread_run(void *ptr)
{
  struct __sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(0, &mask, ((void *)0));
  setup_usr_handler();
  fib(45);
  printf("After thread\n");
  return ((void *)0);
}

variable_types:
ptr
void *


decompiled_code:
int setup_threads()
{
pthread_t v1; // [rsp+8h] [rbp-20h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-18h] BYREF
pthread_t th[2]; // [rsp+18h] [rbp-10h] BYREF

pthread_create(&newthread, 0LL, thread_run, 0LL);
pthread_create(th, 0LL, thread_run, 0LL);
pthread_create(&v1, 0LL, notifier, &newthread);
pthread_join(newthread, 0LL);
pthread_join(th[0], 0LL);
pthread_join(v1, 0LL);
return puts("Done with threads");
}

canonical_decompiled_code:
int func14()
{
pthread_t v1; // [rsp+8h] [rbp-20h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-18h] BYREF
pthread_t th[2]; // [rsp+18h] [rbp-10h] BYREF

pthread_create(&newthread, 0LL, thread_run, 0LL);
pthread_create(th, 0LL, thread_run, 0LL);
pthread_create(&v1, 0LL, notifier, &newthread);
pthread_join(newthread, 0LL);
pthread_join(th[0], 0LL);
pthread_join(v1, 0LL);
return puts("Done with threads");
}

original_code:
void
setup_threads()
{
  pthread_t threads[2];
  pthread_t pause_thr;
  pthread_create(threads + 0, ((void *)0), thread_run, ((void *)0));
  pthread_create(threads + 1, ((void *)0), thread_run, ((void *)0));
  pthread_create(&pause_thr, ((void *)0), notifier, threads);
  pthread_join(threads[0], ((void *)0));
  pthread_join(threads[1], ((void *)0));
  pthread_join(pause_thr, ((void *)0));
  printf("Done with threads\n");
}

canonical_original_code:
void setup_threads()
{
  unsigned long threads[2];
  unsigned long pause_thr;
  pthread_create(threads + 0, ((void *)0), thread_run, ((void *)0));
  pthread_create(threads + 1, ((void *)0), thread_run, ((void *)0));
  pthread_create(&pause_thr, ((void *)0), notifier, threads);
  pthread_join(threads[0], ((void *)0));
  pthread_join(threads[1], ((void *)0));
  pthread_join(pause_thr, ((void *)0));
  printf("Done with threads\n");
}

variable_types:
threads
unsigned long [2]


decompiled_code:
void __noreturn timer_handler()
{
pthread_kill(*(_QWORD *)sigaction_threads, 10);
pthread_kill(*(_QWORD *)(sigaction_threads + 8), 10);
puts("Timer handled");
pthread_exit(0LL);
}

canonical_decompiled_code:
void __noreturn func7()
{
pthread_kill(*(_QWORD *)sigaction_threads, 10);
pthread_kill(*(_QWORD *)(sigaction_threads + 8), 10);
puts("Timer handled");
pthread_exit(0LL);
}

original_code:
void
timer_handler(int x)
{
  pthread_kill(sigaction_threads[0], 10);
  pthread_kill(sigaction_threads[1], 10);
  printf("Timer handled\n");
  pthread_exit(((void *)0));
}


fib

decompiled_code:
__int64 __fastcall fib(int a1)
{
__int64 result; // rax
int v2; // ebp

result = 1LL;
if ( a1 > 1 )
{
v2 = fib((unsigned int)(a1 - 1));
result = v2 + (unsigned int)fib((unsigned int)(a1 - 2));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
__int64 result; // rax
int v2; // ebp

result = 1LL;
if ( a1 > 1 )
{
v2 = func9((unsigned int)(a1 - 1));
result = v2 + (unsigned int)func9((unsigned int)(a1 - 2));
}
return result;
}

original_code:
int
fib(int n)
{
  if (n < 2)
    return 1;
  else
    return fib (n-1) + fib (n-2);
}


notifier

decompiled_code:
void *__fastcall notifier(void *a1)
{
__sigset_t v2; // [rsp+0h] [rbp-108h] BYREF
sigset_t set; // [rsp+80h] [rbp-88h] BYREF

setup_handler((__int64)a1);
sigemptyset(&set);
sigaddset(&set, 14);
pthread_sigmask(1, &set, 0LL);
pthread_sigmask(0, &set, &v2);
sigsuspend(&v2);
pthread_sigmask(1, &set, 0LL);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func11(void *a1)
{
__sigset_t v2; // [rsp+0h] [rbp-108h] BYREF
sigset_t set; // [rsp+80h] [rbp-88h] BYREF

func10((__int64)a1);
sigemptyset(&set);
sigaddset(&set, 14);
pthread_sigmask(1, &set, 0LL);
pthread_sigmask(0, &set, &v2);
sigsuspend(&v2);
pthread_sigmask(1, &set, 0LL);
return 0LL;
}

original_code:
void*
notifier(void* ptr)
{
  sigset_t mask, old_mask;
  pthread_t *threads = (pthread_t*) ptr;
  setup_handler(threads);
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(1, &mask, ((void *)0));
  pthread_sigmask(0, &mask, &old_mask);
  sigsuspend(&old_mask);
  pthread_sigmask(1, &mask, ((void *)0));
  return ((void *)0);
}

canonical_original_code:
void *notifier(void *ptr)
{
  struct __sigset_t mask; struct __sigset_t old_mask;
  unsigned long *threads = (pthread_t*) ptr;
  setup_handler(threads);
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(1, &mask, ((void *)0));
  pthread_sigmask(0, &mask, &old_mask);
  sigsuspend(&old_mask);
  pthread_sigmask(1, &mask, ((void *)0));
  return ((void *)0);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
sigset_t v4; // [rsp+0h] [rbp-A8h] BYREF
struct itimerval newa; // [rsp+80h] [rbp-28h] BYREF

sigemptyset(&v4);
sigaddset(&v4, 14);
pthread_sigmask(0, &v4, 0LL);
newa.it_interval.tv_sec = 0LL;
newa.it_interval.tv_usec = 0LL;
newa.it_value.tv_sec = 1LL;
newa.it_value.tv_usec = 0LL;
setitimer(ITIMER_REAL, &newa, 0LL);
setup_threads();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
sigset_t v4; // [rsp+0h] [rbp-A8h] BYREF
struct itimerval newa; // [rsp+80h] [rbp-28h] BYREF

sigemptyset(&v4);
sigaddset(&v4, 14);
pthread_sigmask(0, &v4, 0LL);
newa.it_interval.tv_sec = 0LL;
newa.it_interval.tv_usec = 0LL;
newa.it_value.tv_sec = 1LL;
newa.it_value.tv_usec = 0LL;
setitimer(ITIMER_REAL, &newa, 0LL);
func14();
return 0;
}

original_code:
int
main(int argc, char **argv)
{
  struct itimerval timer_param;
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(0, &mask, ((void *)0));
  timer_param.it_interval.tv_sec = 0;
  timer_param.it_interval.tv_usec = 0;
  timer_param.it_value.tv_sec = 1;
  timer_param.it_value.tv_usec = 0;
  setitimer(ITIMER_REAL,
            &timer_param,
            ((void *)0));
  setup_threads();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct itimerval timer_param;
  struct __sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, 14);
  pthread_sigmask(0, &mask, ((void *)0));
  timer_param.it_interval.tv_sec = 0;
  timer_param.it_interval.tv_usec = 0;
  timer_param.it_value.tv_sec = 1;
  timer_param.it_value.tv_usec = 0;
  setitimer(ITIMER_REAL,
            &timer_param,
            ((void *)0));
  setup_threads();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct itimerval { struct timeval { long tv_sec; long tv_usec; } it_interval; struct timeval it_value; }

user_defined_types: struct itimerval { struct timeval it_interval; struct timeval it_value; }


fibY

decompiled_code:
__int64 __fastcall fibY(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // er12

maybe_yield();
result = 1LL;
if ( a2 > 1 )
{
v3 = fibY(a1, (unsigned int)(a2 - 1));
result = v3 + (unsigned int)fibY(a1, (unsigned int)(a2 - 2));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // er12

maybe_yield();
result = 1LL;
if ( a2 > 1 )
{
v3 = func0(a1, (unsigned int)(a2 - 1));
result = v3 + (unsigned int)func0(a1, (unsigned int)(a2 - 2));
}
return result;
}

original_code:
int
fibY(processor_t proc, int i)
{
  maybe_yield(proc, i);
  if (i < 2)
    return 1;
  else
    return fibY(proc, i-1) + fibY(proc, i-2);
}

canonical_original_code:
int fibY(struct processor *proc, int i)
{
  maybe_yield(proc, i);
  if (i < 2)
    return 1;
  else
    return fibY(proc, i-1) + fibY(proc, i-2);
}

variable_types: struct processor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct executor { struct task *task; struct list { void **data; int start; int end; int size; } *work; struct processor *current_proc; unsigned long thread; } *executor; int id; } *


decompiled_code:
int __fastcall task2(__int64 a1)
{
unsigned int v1; // eax

puts("Thread2 on the go");
v1 = fibY(a1, 30);
printf("Result2: %d\n", v1);
return puts("Thread2 finished");
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
unsigned int v1; // eax

puts("Thread2 on the go");
v1 = func0(a1, 30);
printf("Result2: %d\n", v1);
return puts("Thread2 finished");
}

original_code:
void
task2(void* data)
{
  int x;
  printf("Thread2 on the go\n");
  x = fibY((processor_t) data, 30);
  printf("Result2: %d\n", x);
  printf("Thread2 finished\n");
}

canonical_original_code:
void task2(void *data)
{
  int x;
  printf("Thread2 on the go\n");
  x = fibY((struct processor *) data, 30);
  printf("Result2: %d\n", x);
  printf("Thread2 finished\n");
}

variable_types:
data
void *


decompiled_code:
int __fastcall task1(__int64 a1)
{
unsigned int v1; // eax

puts("Thread1 on the go");
v1 = fibY(a1, 30);
printf("Result1: %d\n", v1);
return puts("Thread1 finished");
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
unsigned int v1; // eax

puts("Thread1 on the go");
v1 = func0(a1, 30);
printf("Result1: %d\n", v1);
return puts("Thread1 finished");
}

original_code:
void
task1(void* data)
{
  int x;
  printf("Thread1 on the go\n");
  x = fibY((processor_t) data, 30);
  printf("Result1: %d\n", x);
  printf("Thread1 finished\n");
}

canonical_original_code:
void task1(void *data)
{
  int x;
  printf("Thread1 on the go\n");
  x = fibY((struct processor *) data, 30);
  printf("Result1: %d\n", x);
  printf("Thread1 finished\n");
}


fib

decompiled_code:
__int64 __fastcall fib(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebp

result = 1LL;
if ( a2 > 1 )
{
v3 = fib(a1, (unsigned int)(a2 - 1));
result = v3 + (unsigned int)fib(a1, (unsigned int)(a2 - 2));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebp

result = 1LL;
if ( a2 > 1 )
{
v3 = func3(a1, (unsigned int)(a2 - 1));
result = v3 + (unsigned int)func3(a1, (unsigned int)(a2 - 2));
}
return result;
}

original_code:
int
fib(processor_t proc, int i)
{
  false_yield(proc, i);
  if (i < 2)
    return 1;
  else
    return fib(proc, i-1) + fib(proc, i-2);
}

canonical_original_code:
int fib(struct processor *proc, int i)
{
  false_yield(proc, i);
  if (i < 2)
    return 1;
  else
    return fib(proc, i-1) + fib(proc, i-2);
}


main

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

v3 = list_make(argc, argv, envp);
proc1 = make_processor();
proc2 = make_processor();
reset_stack_to(task1, proc1);
reset_stack_to(task2, proc2);
list_add(v3, proc1);
list_add(v3, proc2);
create_executors(v3, 1LL);
v4 = create_notifier();
pthread_join(v4, 0LL);
join_executors();
puts("post join");
list_free(v3);
free_processor(proc1);
free_processor(proc2);
puts("end of test");
return 0;
}

original_code:
int
main(int argc, char **argv)
{
  list_t work = list_make();
  proc1 = make_processor();
  proc2 = make_processor();
  reset_stack_to (task1, proc1);
  reset_stack_to (task2, proc2);
  list_add(work, proc1);
  list_add(work, proc2);
  create_executors(work, 1);
  {
    pthread_t notifier = create_notifier();
    pthread_join(notifier, ((void *)0));
  }
  join_executors();
  printf("post join\n");
  list_free(work);
  free_processor(proc1);
  free_processor(proc2);
  printf("end of test\n");
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct list *work = list_make();
  proc1 = make_processor();
  proc2 = make_processor();
  reset_stack_to (task1, proc1);
  reset_stack_to (task2, proc2);
  list_add(work, proc1);
  list_add(work, proc2);
  create_executors(work, 1);
  {
    unsigned long notifier = create_notifier();
    pthread_join(notifier, ((void *)0));
  }
  join_executors();
  printf("post join\n");
  list_free(work);
  free_processor(proc1);
  free_processor(proc2);
  printf("end of test\n");
  return 0;
}

variable_types: struct list { void **data; int start; int end; int size; } *

variable_types:
notifier
unsigned long


decompiled_code:
char *task_make()
{
char *v0; // rbx

v0 = (char *)malloc(0x3E0uLL);
*(_QWORD *)v0 = malloc(0x4000uLL);
*((_DWORD *)v0 + 244) = 0;
getcontext((ucontext_t *)(v0 + 8));
*((_QWORD *)v0 + 3) = *(_QWORD *)v0 + 0x4000LL;
*((_QWORD *)v0 + 5) = 0x4000LL;
return v0;
}

original_code:
task_t
task_make()
{
  task_t task = (task_t) malloc(sizeof(struct task));
  task->base = malloc(4*1024*sizeof(int));
  task->state = TASK_UNINIT;
  getcontext(&task->ctx);
  task->ctx.uc_stack.ss_sp = task->base + 4*1024*sizeof(int);
  task->ctx.uc_stack.ss_size = 4*1024*sizeof(int);
  return task;
}

canonical_original_code:
struct task *task_make()
{
  struct task *task = (task_t) malloc(sizeof(struct task));
  task->base = malloc(4*1024*sizeof(int));
  task->state = TASK_UNINIT;
  getcontext(&task->ctx);
  task->ctx.uc_stack.ss_sp = task->base + 4*1024*sizeof(int);
  task->ctx.uc_stack.ss_size = 4*1024*sizeof(int);
  return task;
}

variable_types: struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *


decompiled_code:
void __fastcall task_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void
task_free(task_t task)
{
  free(task->base);
  free(task);
}

canonical_original_code:
void task_free(struct task *task)
{
  free(task->base);
  free(task);
}


decompiled_code:
void __fastcall task_set_func(__int64 a1, __int64 a2, __int64 a3)
{
if ( !a1 )
_assert_fail("task != NULL", "task.c", 0x2Au, "task_set_func");
makecontext((ucontext_t *)(a1 + 8), task_wrapper, 3, a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
if ( !a1 )
_assert_fail("task != NULL", "task.c", 0x2Au, "task_set_func");
makecontext((ucontext_t *)(a1 + 8), task_wrapper, 3, a1, a2, a3);
}

original_code:
void
task_set_func(task_t task, void (*f)(void*), void* data)
{
  ((void) sizeof ((task != ((void *)0)) ? 1 : 0), __extension__ ({ if (task != ((void *)0)) ; else __assert_fail ("task != NULL", "task.c", 42, __extension__ __PRETTY_FUNCTION__); }));
  makecontext(&task->ctx, (void (*)())task_wrapper, 3, task, f, data);
}

canonical_original_code:
void task_set_func(struct task *task, void (*f)(void *), void *data)
{
  ((void) sizeof ((task != ((void *)0)) ? 1 : 0), __extension__ ({ if (task != ((void *)0)) ; else __assert_fail ("task != NULL", "task.c", 42, __extension__ __PRETTY_FUNCTION__); }));
  makecontext(&task->ctx, (void (*)())task_wrapper, 3, task, f, data);
}

variable_types:
f
void (*)(void *)


decompiled_code:
int __fastcall task_run(__int64 a1)
{
*(_DWORD *)(a1 + 976) = 1;
return setcontext((const ucontext_t *)(a1 + 8));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
*(_DWORD *)(a1 + 976) = 1;
return setcontext((const ucontext_t *)(a1 + 8));
}

original_code:
void
task_run(task_t task)
{
  task->state = TASK_RUNNING;
  setcontext(&task->ctx);
}

canonical_original_code:
void task_run(struct task *task)
{
  task->state = TASK_RUNNING;
  setcontext(&task->ctx);
}


original_code:
static
void
task_wrapper(task_t task, void (*f)(void*), void* data)
{
  task->state = TASK_RUNNING;
  f(data);
  task->state = TASK_FINISHED;
  if (task->next != ((void *)0))
    task_run (task->next);
}

canonical_original_code:
static void task_wrapper(struct task *task, void (*f)(void *), void *data)
{
  task->state = TASK_RUNNING;
  f(data);
  task->state = TASK_FINISHED;
  if (task->next != ((void *)0))
    task_run (task->next);
}


decompiled_code:
int __fastcall yield_to(__int64 a1, __int64 a2)
{
if ( *(_DWORD *)(a1 + 976) != 1 )
_assert_fail("from_task->state == TASK_RUNNING", "task.c", 0x38u, "yield_to");
getcontext((ucontext_t *)(a1 + 8));
*(_DWORD *)(a1 + 976) = 2;
return task_run(a2);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2)
{
if ( *(_DWORD *)(a1 + 976) != 1 )
_assert_fail("from_task->state == TASK_RUNNING", "task.c", 0x38u, "yield_to");
getcontext((ucontext_t *)(a1 + 8));
*(_DWORD *)(a1 + 976) = 2;
return func3(a2);
}

original_code:
void
yield_to(task_t from_task, task_t to_task)
{
  ((void) sizeof ((from_task->state == TASK_RUNNING) ? 1 : 0), __extension__ ({ if (from_task->state == TASK_RUNNING) ; else __assert_fail ("from_task->state == TASK_RUNNING", "task.c", 56, __extension__ __PRETTY_FUNCTION__); }));
  volatile int flag = 0;
  getcontext(&from_task->ctx);
  if (flag == 0)
    {
      flag = 1;
      from_task->state = TASK_RUNNABLE;
      task_run(to_task);
    }
}

canonical_original_code:
void yield_to(struct task *from_task, struct task *to_task)
{
  ((void) sizeof ((from_task->state == TASK_RUNNING) ? 1 : 0), __extension__ ({ if (from_task->state == TASK_RUNNING) ; else __assert_fail ("from_task->state == TASK_RUNNING", "task.c", 56, __extension__ __PRETTY_FUNCTION__); }));
  int flag = 0;
  getcontext(&from_task->ctx);
  if (flag == 0)
    {
      flag = 1;
      from_task->state = TASK_RUNNABLE;
      task_run(to_task);
    }
}

variable_types: flag int


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

result = time_is_up;
if ( time_is_up == 1 )
{
time_is_up = 0;
result = yield_to(*(_QWORD *)a1, **(_QWORD **)(a1 + 8));
}
return result;
}

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

result = time_is_up;
if ( time_is_up == 1 )
{
time_is_up = 0;
result = yield_to(*(_QWORD *)a1, **(_QWORD **)(a1 + 8));
}
return result;
}

original_code:
void
maybe_yield(processor_t proc, int i)
{
  if (time_is_up == 1)
    {
      time_is_up = 0;
      yield_to(proc->task, proc->executor->task);
    }
}

canonical_original_code:
void maybe_yield(struct processor *proc, int i)
{
  if (time_is_up == 1)
    {
      time_is_up = 0;
      yield_to(proc->task, proc->executor->task);
    }
}

variable_types: i int


decompiled_code:
void __fastcall free_processor(void *ptr)
{
task_free(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
task_free(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void
free_processor(processor_t proc)
{
  task_free (proc->task);
  free (proc);
}

canonical_original_code:
void free_processor(struct processor *proc)
{
  task_free (proc->task);
  free (proc);
}


decompiled_code:
_DWORD *make_processor()
{
_DWORD *v0; // rbx
int v1; // eax

v0 = malloc(0x18uLL);
*(_QWORD *)v0 = task_make();
v1 = global_id++;
v0[4] = v1;
return v0;
}

canonical_decompiled_code:
_DWORD *func1()
{
_DWORD *v0; // rbx
int v1; // eax

v0 = malloc(0x18uLL);
*(_QWORD *)v0 = task_make();
v1 = global_id++;
v0[4] = v1;
return v0;
}

original_code:
processor_t
make_processor()
{
  processor_t proc = (processor_t) malloc(sizeof(struct processor));
  proc->task = task_make();
  proc->id = global_id++;
  return proc;
}

canonical_original_code:
struct processor *make_processor()
{
  struct processor *proc = (processor_t) malloc(sizeof(struct processor));
  proc->task = task_make();
  proc->id = global_id++;
  return proc;
}

return_type: struct processor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct executor { struct task *task; struct list { void **data; int start; int end; int size; } *work; struct processor *current_proc; unsigned long thread; } *executor; int id; } *


decompiled_code:
__int64 __fastcall reset_stack_to(__int64 a1, _QWORD *a2)
{
return task_set_func(*a2, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _QWORD *a2)
{
return task_set_func(*a2, a1, a2);
}

original_code:
void
reset_stack_to(void (*f)(void*), processor_t proc)
{
  task_set_func(proc->task, f, proc);
}

canonical_original_code:
void reset_stack_to(void (*f)(void *), struct processor *proc)
{
  task_set_func(proc->task, f, proc);
}


original_code:
uint8_t data_to_sysex(uint8_t *data, uint8_t *sysex, uint8_t len) {
  uint8_t retlen = 0;
  uint8_t cnt;
  uint8_t cnt7 = 0;
  sysex[0] = 0;
  for (cnt = 0; cnt < len; cnt++) {
    uint8_t c = data[cnt] & 0x7F;
    uint8_t msb = data[cnt] >> 7;
    sysex[0] |= msb << cnt7;
    sysex[1 + cnt7] = c;
    if (cnt7++ == 6) {
      sysex += 8;
      retlen += 8;
      sysex[0] = 0;
      cnt7 = 0;
    }
  }
  return retlen + cnt7 + (cnt7 != 0 ? 1 : 0);
}

canonical_original_code:
unsigned char data_to_sysex(unsigned char *data, unsigned char *sysex, unsigned char len) {
  unsigned char retlen = 0;
  unsigned char cnt;
  unsigned char cnt7 = 0;
  sysex[0] = 0;
  for (cnt = 0; cnt < len; cnt++) {
    unsigned char c = data[cnt] & 0x7F;
    unsigned char msb = data[cnt] >> 7;
    sysex[0] |= msb << cnt7;
    sysex[1 + cnt7] = c;
    if (cnt7++ == 6) {
      sysex += 8;
      retlen += 8;
      sysex[0] = 0;
      cnt7 = 0;
    }
  }
  return retlen + cnt7 + (cnt7 != 0 ? 1 : 0);
}

variable_types:
data
unsigned char *

variable_types:
sysex
unsigned char *

variable_types:
len
unsigned char

variable_types:
retlen
unsigned char

variable_types:
cnt
unsigned char

variable_types:
c
unsigned char

variable_types:
msb
unsigned char

return_type: unsigned char


original_code:
uint8_t sysex_to_data(uint8_t *sysex, uint8_t *data, uint8_t len) {
  uint8_t cnt;
  uint8_t cnt2 = 0;
  uint8_t bits = 0;
  for (cnt = 0; cnt < len; cnt++) {
    if ((cnt % 8) == 0) {
      bits = sysex[cnt];
    } else {
      data[cnt2++] = sysex[cnt] | ((bits & 1) << 7);
      bits >>= 1;
    }
  }
  return cnt2;
}

canonical_original_code:
unsigned char sysex_to_data(unsigned char *sysex, unsigned char *data, unsigned char len) {
  unsigned char cnt;
  unsigned char cnt2 = 0;
  unsigned char bits = 0;
  for (cnt = 0; cnt < len; cnt++) {
    if ((cnt % 8) == 0) {
      bits = sysex[cnt];
    } else {
      data[cnt2++] = sysex[cnt] | ((bits & 1) << 7);
      bits >>= 1;
    }
  }
  return cnt2;
}

variable_types:
bits
unsigned char


repo: scottmccain/Compiler

decompiled_code:
__int64 __fastcall type_get_from_lexeme(char *s1)
{
int v1; // edx
__int64 result; // rax
int v3; // edx

v1 = strcasecmp(s1, "Int");
result = 1LL;
if ( v1 )
{
v3 = strcasecmp(s1, "Bool");
result = 2LL;
if ( v3 )
result = strcasecmp(s1, "Char") == 0 ? 3 : 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
int v1; // edx
__int64 result; // rax
int v3; // edx

v1 = strcasecmp(s1, "Int");
result = 1LL;
if ( v1 )
{
v3 = strcasecmp(s1, "Bool");
result = 2LL;
if ( v3 )
result = strcasecmp(s1, "Char") == 0 ? 3 : 0;
}
return result;
}

original_code:
Type
type_get_from_lexeme(const char *lexeme)
{
    if (!strcasecmp (lexeme, "Int"))
        return INTEGER;
    else if (!strcasecmp (lexeme, "Bool"))
        return BOOLEAN;
    else if (!strcasecmp (lexeme, "Char"))
        return CHAR;
    else
        return VOID;
}

canonical_original_code:
enum TypeEnum type_get_from_lexeme(char *lexeme)
{
    if (!strcasecmp (lexeme, "Int"))
        return INTEGER;
    else if (!strcasecmp (lexeme, "Bool"))
        return BOOLEAN;
    else if (!strcasecmp (lexeme, "Char"))
        return CHAR;
    else
        return VOID;
}

variable_types:
lexeme
char *

return_type: enum TypeEnum { ERROR=-1, VOID=0, INTEGER=1, BOOLEAN=2, CHAR=3 }


decompiled_code:
const char *__fastcall type_get_lexeme(int a1)
{
const char *result; // rax

switch ( a1 )
{
case -1:
result = "error";
break;
case 0:
result = "void";
break;
case 1:
result = "int";
break;
case 2:
result = "bool";
break;
case 3:
result = "char";
break;
default:
result = (const char *)&unk_240;
break;
}
return result;
}

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

switch ( a1 )
{
case -1:
result = "error";
break;
case 0:
result = "void";
break;
case 1:
result = "int";
break;
case 2:
result = "bool";
break;
case 3:
result = "char";
break;
default:
result = (const char *)&unk_240;
break;
}
return result;
}

original_code:
char *
type_get_lexeme(Type type)
{
    switch (type) {
        case VOID:
            return "void";
        case INTEGER:
            return "int";
        case BOOLEAN:
            return "bool";
        case CHAR:
            return "char";
        case ERROR:
            return "error";
        default:
            return "";
    }
}

canonical_original_code:
char *type_get_lexeme(enum TypeEnum type)
{
    switch (type) {
        case VOID:
            return "void";
        case INTEGER:
            return "int";
        case BOOLEAN:
            return "bool";
        case CHAR:
            return "char";
        case ERROR:
            return "error";
        default:
            return "";
    }
}

variable_types: enum TypeEnum { ERROR=-1, VOID=0, INTEGER=1, BOOLEAN=2, CHAR=3 }

return_type: char *


decompiled_code:
__int64 lcd_send()
{
if ( (int)usb_control_msg() >= 0 )
return 0LL;
fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func7()
{
if ( (int)usb_control_msg() >= 0 )
return 0LL;
fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int lcd_send(int request, int value, int index) {
  if(usb_control_msg(handle, (0x02 << 5), request,
        value, index, ((void *)0), 0, 1000) < 0) {
    fprintf(stderr, "USB request failed!");
    return -1;
  }
  return 0;
}

variable_types: request int

variable_types: value int

variable_types: index int


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

result = (unsigned int)buffer_current_type;
if ( buffer_current_type != -1 )
{
result = lcd_send(
buffer_current_type | (unsigned int)(buffer_current_fill - 1),
(unsigned __int16)buffer,
(unsigned __int16)word_4040D2);
buffer_current_type = -1;
buffer_current_fill = 0;
}
return result;
}

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

result = (unsigned int)buffer_current_type;
if ( buffer_current_type != -1 )
{
result = func7(
buffer_current_type | (unsigned int)(buffer_current_fill - 1),
(unsigned __int16)buffer,
(unsigned __int16)word_4040D2);
buffer_current_type = -1;
buffer_current_fill = 0;
}
return result;
}

original_code:
void lcd_flush(void) {
  int request, value, index;
  if (buffer_current_type == -1)
    return;
  request = buffer_current_type | (buffer_current_fill - 1);
  value = buffer[0] | (buffer[1] << 8);
  index = buffer[2] | (buffer[3] << 8);
  lcd_send(request, value, index);
  buffer_current_type = -1;
  buffer_current_fill = 0;
}

canonical_original_code:
void lcd_flush() {
  int request; int value; int index;
  if (buffer_current_type == -1)
    return;
  request = buffer_current_type | (buffer_current_fill - 1);
  value = buffer[0] | (buffer[1] << 8);
  index = buffer[2] | (buffer[3] << 8);
  lcd_send(request, value, index);
  buffer_current_type = -1;
  buffer_current_fill = 0;
}


decompiled_code:
__int64 __fastcall lcd_enqueue(int a1, char a2)
{
int v2; // edx
__int64 result; // rax

if ( buffer_current_type != a1 && buffer_current_type >= 0 )
lcd_flush();
buffer_current_type = a1;
LODWORD(result) = buffer_current_fill++;
v2 = buffer_current_fill;
result = (int)result;
*((_BYTE *)&buffer + (int)result) = a2;
if ( v2 == 4 )
result = lcd_flush();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, char a2)
{
int v2; // edx
__int64 result; // rax

if ( buffer_current_type != a1 && buffer_current_type >= 0 )
func8();
buffer_current_type = a1;
LODWORD(result) = buffer_current_fill++;
v2 = buffer_current_fill;
result = (int)result;
*((_BYTE *)&buffer + (int)result) = a2;
if ( v2 == 4 )
result = func8();
return result;
}

original_code:
void lcd_enqueue(int command_type, int value) {
  if ((buffer_current_type >= 0) && (buffer_current_type != command_type))
    lcd_flush();
  buffer_current_type = command_type;
  buffer[buffer_current_fill++] = value;
  if (buffer_current_fill == 4)
    lcd_flush();
}


decompiled_code:
__int64 __fastcall lcd_command(unsigned __int8 a1, char a2)
{
return lcd_enqueue(a1 | 0x20u, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, char a2)
{
return func9(a1 | 0x20u, a2);
}

original_code:
void lcd_command(const unsigned char ctrl, const unsigned char cmd) {
  lcd_enqueue((1<<5) | ctrl, cmd);
}

canonical_original_code:
void lcd_command(unsigned char ctrl, unsigned char cmd) {
  lcd_enqueue((1<<5) | ctrl, cmd);
}

variable_types:
ctrl
unsigned char

variable_types:
cmd
unsigned char


decompiled_code:
__int64 lcd_clear()
{
lcd_command(0x18u, 1);
return lcd_command(0x18u, 3);
}


decompiled_code:
__int64 lcd_home()
{
return lcd_command(0x18u, 3);
}


decompiled_code:
__int64 __fastcall lcd_write(char *a1)
{
char *v1; // rbx
char i; // si

v1 = a1;
for ( i = *a1; *v1; i = *v1 )
{
++v1;
lcd_enqueue(72, i);
}
return lcd_flush();
}

canonical_decompiled_code:
__int64 __fastcall func13(char *a1)
{
char *v1; // rbx
char i; // si

v1 = a1;
for ( i = *a1; *v1; i = *v1 )
{
++v1;
func9(72, i);
}
return func8();
}

original_code:
void lcd_write(const char *data) {
  int ctrl = (1<<3);
  while(*data)
    lcd_enqueue((2<<5) | ctrl, *data++);
  lcd_flush();
}

canonical_original_code:
void lcd_write(char *data) {
  int ctrl = (1<<3);
  while(*data)
    lcd_enqueue((2<<5) | ctrl, *data++);
  lcd_flush();
}

variable_types:
data
char *

variable_types: ctrl int


decompiled_code:
int lcd_echo()
{
int v0; // ebp
unsigned int v1; // er12
__int16 v2; // bx
int result; // eax
__int16 v4; // [rsp+Eh] [rbp-1Ah]

v0 = 100;
v1 = 0;
do
{
v2 = rand();
if ( (int)usb_control_msg() < 0 )
return fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
v1 += v4 != v2;
--v0;
}
while ( v0 );
if ( v1 )
result = fprintf(stderr, "ERROR: %d out of %d echo transfers failed!\n", v1, 100LL);
else
result = puts("Echo test successful!");
return result;
}

canonical_decompiled_code:
int func14()
{
int v0; // ebp
unsigned int v1; // er12
__int16 v2; // bx
int result; // eax
__int16 v4; // [rsp+Eh] [rbp-1Ah]

v0 = 100;
v1 = 0;
do
{
v2 = rand();
if ( (int)usb_control_msg() < 0 )
return fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
v1 += v4 != v2;
--v0;
}
while ( v0 );
if ( v1 )
result = fprintf(stderr, "ERROR: %d out of %d echo transfers failed!\n", v1, 100LL);
else
result = puts("Echo test successful!");
return result;
}

original_code:
void lcd_echo(void) {
  int i, nBytes, errors=0;
  unsigned short val, ret;
  for(i=0;i<100;i++) {
    val = rand() & 0xffff;
    nBytes = usb_control_msg(handle,
    (0x02 << 5) | 0x00 | 0x80,
    (0<<5), val, 0, (char*)&ret, sizeof(ret), 1000);
    if(nBytes < 0) {
      fprintf(stderr, "USB request failed!");
      return;
    }
    if(val != ret)
      errors++;
  }
  if(errors)
    fprintf(stderr, "ERROR: %d out of %d echo transfers failed!\n",
     errors, 100);
  else
    printf("Echo test successful!\n");
}

canonical_original_code:
void lcd_echo() {
  int i; int nBytes; int errors = 0;
  unsigned short val; unsigned short ret;
  for(i=0;i<100;i++) {
    val = rand() & 0xffff;
    nBytes = usb_control_msg(handle,
    (0x02 << 5) | 0x00 | 0x80,
    (0<<5), val, 0, (char *)&ret, sizeof(ret), 1000);
    if(nBytes < 0) {
      fprintf(stderr, "USB request failed!");
      return;
    }
    if(val != ret)
      errors++;
  }
  if(errors)
    fprintf(stderr, "ERROR: %d out of %d echo transfers failed!\n",
     errors, 100);
  else
    printf("Echo test successful!\n");
}

variable_types: i int

variable_types: nBytes int

variable_types: errors int

variable_types:
val
unsigned short

variable_types:
ret
unsigned short


decompiled_code:
__int64 lcd_get()
{
unsigned __int8 v1; // [rsp+Eh] [rbp-Ah]
unsigned __int8 v2; // [rsp+Fh] [rbp-9h]

if ( (int)usb_control_msg() >= 0 )
return v1 + (v2 << 8);
fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func15()
{
unsigned __int8 v1; // [rsp+Eh] [rbp-Ah]
unsigned __int8 v2; // [rsp+Fh] [rbp-9h]

if ( (int)usb_control_msg() >= 0 )
return v1 + (v2 << 8);
fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int lcd_get(unsigned char cmd) {
  unsigned char buffer[2];
  int nBytes;
  nBytes = usb_control_msg(handle,
    (0x02 << 5) | 0x00 | 0x80,
    cmd, 0, 0, (char *)buffer, sizeof(buffer), 1000);
  if(nBytes < 0) {
    fprintf(stderr, "USB request failed!");
    return -1;
  }
  return buffer[0] + 256*buffer[1];
}

variable_types:
buffer
unsigned char [2]


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

result = lcd_get(128LL);
if ( result != -1 )
result = printf("Firmware version %d.%d\n", (unsigned __int8)result, (unsigned int)(result >> 8));
return result;
}

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

result = func15(128LL);
if ( result != -1 )
result = printf("Firmware version %d.%d\n", (unsigned __int8)result, (unsigned int)(result >> 8));
return result;
}

original_code:
void lcd_get_version(void) {
  int ver = lcd_get(((4<<5) | (0<<3)));
  if(ver != -1)
    printf("Firmware version %d.%d\n", ver&0xff, ver>>8);
}

canonical_original_code:
void lcd_get_version() {
  int ver = lcd_get(((4<<5) | (0<<3)));
  if(ver != -1)
    printf("Firmware version %d.%d\n", ver&0xff, ver>>8);
}

variable_types: ver int


decompiled_code:
int lcd_get_controller()
{
int result; // eax
const char *v1; // rdx
const char *v2; // rsi

result = lcd_get(144LL);
if ( result != -1 )
{
if ( result )
{
v1 = " CTRL1";
if ( (result & 2) == 0 )
v1 = "Firmware version %d.%d\n" + 23;
v2 = "CTRL0";
if ( (result & 1) == 0 )
v2 = "Firmware version %d.%d\n" + 23;
result = printf("Installed controllers: %s%s\n", v2, v1);
}
else
{
result = puts("No controllers installed!");
}
}
return result;
}

canonical_decompiled_code:
int func17()
{
int result; // eax
const char *v1; // rdx
const char *v2; // rsi

result = func15(144LL);
if ( result != -1 )
{
if ( result )
{
v1 = " CTRL1";
if ( (result & 2) == 0 )
v1 = "Firmware version %d.%d\n" + 23;
v2 = "CTRL0";
if ( (result & 1) == 0 )
v2 = "Firmware version %d.%d\n" + 23;
result = printf("Installed controllers: %s%s\n", v2, v1);
}
else
{
result = puts("No controllers installed!");
}
}
return result;
}

original_code:
void lcd_get_controller(void) {
  int ctrl = lcd_get(((4<<5) | (2<<3)));
  if(ctrl != -1) {
    if(ctrl)
      printf("Installed controllers: %s%s\n",
      (ctrl&1)?"CTRL0":"",
      (ctrl&2)?" CTRL1":"");
    else
      printf("No controllers installed!\n");
  }
}

canonical_original_code:
void lcd_get_controller() {
  int ctrl = lcd_get(((4<<5) | (2<<3)));
  if(ctrl != -1) {
    if(ctrl)
      printf("Installed controllers: %s%s\n",
      (ctrl&1)?"CTRL0":"",
      (ctrl&2)?" CTRL1":"");
    else
      printf("No controllers installed!\n");
  }
}


decompiled_code:
int lcd_get_keys()
{
int result; // eax
const char *v1; // rdx
const char *v2; // rsi

result = lcd_get(136LL);
if ( result != -1 )
{
v1 = "on";
if ( (result & 2) == 0 )
v1 = "off";
v2 = "on";
if ( (result & 1) == 0 )
v2 = "off";
result = printf("Keys: 0:%s 1:%s\n", v2, v1);
}
return result;
}

canonical_decompiled_code:
int func18()
{
int result; // eax
const char *v1; // rdx
const char *v2; // rsi

result = func15(136LL);
if ( result != -1 )
{
v1 = "on";
if ( (result & 2) == 0 )
v1 = "off";
v2 = "on";
if ( (result & 1) == 0 )
v2 = "off";
result = printf("Keys: 0:%s 1:%s\n", v2, v1);
}
return result;
}

original_code:
void lcd_get_keys(void) {
  int keymask = lcd_get(((4<<5) | (1<<3)));
  if(keymask != -1)
    printf("Keys: 0:%s 1:%s\n",
    (keymask&1)?"on":"off",
    (keymask&2)?"on":"off");
}

canonical_original_code:
void lcd_get_keys() {
  int keymask = lcd_get(((4<<5) | (1<<3)));
  if(keymask != -1)
    printf("Keys: 0:%s 1:%s\n",
    (keymask&1)?"on":"off",
    (keymask&2)?"on":"off");
}

variable_types: keymask int


decompiled_code:
size_t lcd_set()
{
size_t result; // rax

result = usb_control_msg();
if ( (result & 0x80000000) != 0LL )
result = fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return result;
}

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

result = usb_control_msg();
if ( (result & 0x80000000) != 0LL )
result = fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
return result;
}

original_code:
void lcd_set(unsigned char cmd, int value) {
  if(usb_control_msg(handle,
      (0x02 << 5), cmd, value, 0,
      ((void *)0), 0, 1000) < 0) {
    fprintf(stderr, "USB request failed!");
  }
}


decompiled_code:
size_t lcd_set_contrast()
{
return lcd_set();
}

canonical_decompiled_code:
size_t func20()
{
return func19();
}

original_code:
void lcd_set_contrast(int value) {
  lcd_set(((3<<5) | (0<<3)), value);
}


decompiled_code:
size_t lcd_set_brightness()
{
return lcd_set();
}

canonical_decompiled_code:
size_t func21()
{
return func19();
}

original_code:
void lcd_set_brightness(int value) {
  lcd_set(((3<<5) | (1<<3)), value);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbp
const char *v4; // rbx
const char *v5; // rsi
int j; // ebx
int k; // ebx
__int64 *v9; // rbx
const char *v10; // rax
char v11[56]; // [rsp+0h] [rbp-38h] BYREF

puts("-- LCD2USB test application --");
puts("-- (c) 2006 by Till Harbaum --");
puts("-- http://www.harbaum.org/till/lcd2usb --");
usb_init();
usb_find_busses();
usb_find_devices();
for ( i = usb_get_busses(); i; i = *(_QWORD *)i )
{
v9 = *(__int64 **)(i + 4120);
if ( v9 )
{
while ( *((_DWORD *)v9 + 1034) != -969931773 )
{
v9 = (__int64 *)*v9;
if ( !v9 )
goto LABEL_12;
}
argv = (const char **)(i + 16);
printf("Found LCD2USB device on bus %s device %s.\n", (const char *)(i + 16), (const char *)v9 + 16);
handle = usb_open();
if ( !handle )
{
v10 = (const char *)usb_strerror();
argv = (const char **)"Error: Cannot open USB device: %s\n";
fprintf(stderr, "Error: Cannot open USB device: %s\n", v10);
}
}
LABEL_12:
;
}
if ( !handle )
{
fwrite("Error: Could not find LCD2USB device\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
lcd_echo();
lcd_get_version();
lcd_get_controller();
lcd_get_keys();
lcd_set_contrast();
lcd_set_brightness();
lcd_clear(255LL, argv);
v4 = " The quick brown fox jumps over the lazy dogs back ... ";
do
{
v5 = v4;
strncpy(v11, v4, 0x10uLL);
v11[16] = 0;
lcd_home();
lcd_write(v11);
usleep(0x186A0u);
++v4;
}
while ( v4 != " " );
for ( j = 255; j != -1; --j )
{
lcd_set_brightness();
usleep(0x2710u);
}
lcd_clear(10000LL, v5);
lcd_write("Bye bye!!!");
for ( k = 0; k != 256; ++k )
{
lcd_set_brightness();
usleep(0x2710u);
}
usb_close();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbp
const char *v4; // rbx
const char *v5; // rsi
int j; // ebx
int k; // ebx
__int64 *v9; // rbx
const char *v10; // rax
char v11[56]; // [rsp+0h] [rbp-38h] BYREF

puts("-- LCD2USB test application --");
puts("-- (c) 2006 by Till Harbaum --");
puts("-- http://www.harbaum.org/till/lcd2usb --");
usb_init();
usb_find_busses();
usb_find_devices();
for ( i = usb_get_busses(); i; i = *(_QWORD *)i )
{
v9 = *(__int64 **)(i + 4120);
if ( v9 )
{
while ( *((_DWORD *)v9 + 1034) != -969931773 )
{
v9 = (__int64 *)*v9;
if ( !v9 )
goto LABEL_12;
}
argv = (const char **)(i + 16);
printf("Found LCD2USB device on bus %s device %s.\n", (const char *)(i + 16), (const char *)v9 + 16);
handle = usb_open();
if ( !handle )
{
v10 = (const char *)usb_strerror();
argv = (const char **)"Error: Cannot open USB device: %s\n";
fprintf(stderr, "Error: Cannot open USB device: %s\n", v10);
}
}
LABEL_12:
;
}
if ( !handle )
{
fwrite("Error: Could not find LCD2USB device\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
func14();
func16();
func17();
func18();
func20();
func21();
func11(255LL, argv);
v4 = " The quick brown fox jumps over the lazy dogs back ... ";
do
{
v5 = v4;
strncpy(v11, v4, 0x10uLL);
v11[16] = 0;
func12();
func13(v11);
usleep(0x186A0u);
++v4;
}
while ( v4 != " " );
for ( j = 255; j != -1; --j )
{
func21();
usleep(0x2710u);
}
func11(10000LL, v5);
func13("Bye bye!!!");
for ( k = 0; k != 256; ++k )
{
func21();
usleep(0x2710u);
}
usb_close();
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  struct usb_bus *bus;
  struct usb_device *dev;
  int i;
  printf("--      LCD2USB test application       --\n");
  printf("--      (c) 2006 by Till Harbaum       --\n");
  printf("-- http://www.harbaum.org/till/lcd2usb --\n");
  usb_init();
  usb_find_busses();
  usb_find_devices();
  for(bus = usb_get_busses(); bus; bus = bus->next) {
    for(dev = bus->devices; dev; dev = dev->next) {
      if((dev->descriptor.idVendor == 0x0403) &&
  (dev->descriptor.idProduct == 0xc630)) {
 printf("Found LCD2USB device on bus %s device %s.\n",
        bus->dirname, dev->filename);
 if(!(handle = usb_open(dev)))
   fprintf(stderr, "Error: Cannot open USB device: %s\n",
    usb_strerror());
 break;
      }
    }
  }
  if(!handle) {
    fprintf(stderr, "Error: Could not find LCD2USB device\n");
    exit(-1);
  }
  lcd_echo();
  lcd_get_version();
  lcd_get_controller();
  lcd_get_keys();
  lcd_set_contrast(200);
  lcd_set_brightness(255);
  lcd_clear();
  for(i=0;i<strlen(msg)-15;i++) {
    char tmp_str[17];
    strncpy(tmp_str, msg+i, 16);
    tmp_str[16] = 0;
    lcd_home();
    lcd_write(tmp_str);
    usleep(100*1000);
  }
  for(i=255;i>=0;i--) {
    lcd_set_brightness(i);
    usleep(10*1000);
  }
  lcd_clear();
  lcd_write("Bye bye!!!");
  for(i=0;i<=255;i++) {
    lcd_set_brightness(i);
    usleep(10*1000);
  }
  usb_close(handle);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *devices; unsigned int location; struct usb_device *root_dev; } *

variable_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device *devices; unsigned int location; struct usb_device *root_dev; } *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *

user_defined_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; }

user_defined_types: struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; int extralen; }

user_defined_types: struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }

user_defined_types: struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen; }


decompiled_code:
int handle_signals()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

result = sigfillset(&set);
if ( result != -1 )
{
result = pthread_sigmask(2, &set, 0LL);
if ( result != -1 )
{
memset(&v1, 0, sizeof(v1));
result = sigfillset(&v1.sa_mask);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)&loc_1;
result = sigaction(1, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(3, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(13, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)sig_handler;
result = sigaction(2, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(15, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(7, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(8, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(4, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(11, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(31, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(24, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(25, &v1, 0LL);
if ( result != -1 )
{
result = sigemptyset(&set);
if ( result != -1 )
result = -(pthread_sigmask(2, &set, 0LL) == -1);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-128h] BYREF
sigset_t set; // [rsp+A0h] [rbp-88h] BYREF

result = sigfillset(&set);
if ( result != -1 )
{
result = pthread_sigmask(2, &set, 0LL);
if ( result != -1 )
{
memset(&v1, 0, sizeof(v1));
result = sigfillset(&v1.sa_mask);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)&loc_1;
result = sigaction(1, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(3, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(13, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)sig_handler;
result = sigaction(2, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(15, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(7, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(8, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(4, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(11, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(31, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(24, &v1, 0LL);
if ( result != -1 )
{
result = sigaction(25, &v1, 0LL);
if ( result != -1 )
{
result = sigemptyset(&set);
if ( result != -1 )
result = -(pthread_sigmask(2, &set, 0LL) == -1);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

original_code:
static int handle_signals()
{
  sigset_t set;
  struct sigaction act;
  if(sigfillset(&set) == -1)
    return -1;
  if(pthread_sigmask(2, &set, ((void *)0)) == -1)
    return -1;
  memset(&act, 0, sizeof(act));
  if(sigfillset(&act.sa_mask) == -1)
    return -1;
  act.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
  if(sigaction(1, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(3, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(13, &act, ((void *)0)) == -1)
    return -1;
  act.__sigaction_handler.sa_handler = sig_handler;
  if(sigaction(2, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(15, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(7, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(8, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(4, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(11, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(31, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(24, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(25, &act, ((void *)0)) == -1)
    return -1;
  if(sigemptyset(&set) == -1)
    return -1;
  if(pthread_sigmask(2, &set, ((void *)0)) == -1)
    return -1;
  return 0;
}

canonical_original_code:
static int handle_signals()
{
  struct __sigset_t set;
  struct sigaction act;
  if(sigfillset(&set) == -1)
    return -1;
  if(pthread_sigmask(2, &set, ((void *)0)) == -1)
    return -1;
  memset(&act, 0, sizeof(act));
  if(sigfillset(&act.sa_mask) == -1)
    return -1;
  act.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
  if(sigaction(1, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(3, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(13, &act, ((void *)0)) == -1)
    return -1;
  act.__sigaction_handler.sa_handler = sig_handler;
  if(sigaction(2, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(15, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(7, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(8, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(4, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(11, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(31, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(24, &act, ((void *)0)) == -1)
    return -1;
  if(sigaction(25, &act, ((void *)0)) == -1)
    return -1;
  if(sigemptyset(&set) == -1)
    return -1;
  if(pthread_sigmask(2, &set, ((void *)0)) == -1)
    return -1;
  return 0;
}

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


decompiled_code:
void __fastcall __noreturn sig_handler(int a1)
{
print_sig_desc();
write(1, "Exiting...\n", 0xCuLL);
if ( a1 != 2 && a1 != 15 )
exit(1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(int a1)
{
print_sig_desc();
write(1, "Exiting...\n", 0xCuLL);
if ( a1 != 2 && a1 != 15 )
exit(1);
exit(0);
}

original_code:
static void sig_handler(int sig)
{
  print_sig_desc(sig);
  write(1, "Exiting...\n", 12);
  if ((sig == 2) || (sig == 15))
    _exit(0);
  else
    _exit(1);
}

variable_types: sig int


closed

decompiled_code:
__int64 closed()
{
puts("We've been disconnected!");
pthread_mutex_lock(&mutex);
is_connected = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
puts("We've been disconnected!");
pthread_mutex_lock(&mutex);
is_connected = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return 0LL;
}

original_code:
static int closed()
{
  printf("We've been disconnected!\n");
  pthread_mutex_lock(&mutex);
  is_connected = 0;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);
  return 0;
}


connected

decompiled_code:
__int64 connected()
{
puts("We're connected!");
pthread_mutex_lock(&mutex);
is_connected = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
puts("We're connected!");
pthread_mutex_lock(&mutex);
is_connected = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return 0LL;
}

original_code:
static int connected()
{
  printf("We're connected!\n");
  pthread_mutex_lock(&mutex);
  is_connected = 1;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);
  return 0;
}


logit

decompiled_code:
int logit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-920h] BYREF
struct timeval tv; // [rsp+20h] [rbp-908h] BYREF
struct tm tp; // [rsp+30h] [rbp-8F8h] BYREF
char s[2048]; // [rsp+70h] [rbp-8B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x800uLL, format, arg);
gettimeofday(&tv, 0LL);
localtime_r(&tv.tv_sec, &tp);
return printf(
"%02d:%02d:%02d.%03ld - %s\n",
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
tv.tv_usec / 1000,
s);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-920h] BYREF
struct timeval tv; // [rsp+20h] [rbp-908h] BYREF
struct tm tp; // [rsp+30h] [rbp-8F8h] BYREF
char s[2048]; // [rsp+70h] [rbp-8B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x800uLL, format, arg);
gettimeofday(&tv, 0LL);
localtime_r(&tv.tv_sec, &tp);
return printf(
"%02d:%02d:%02d.%03ld - %s\n",
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
tv.tv_usec / 1000,
s);
}

original_code:
void logit(const char *fmt, ...)
{
  char buf[2048];
  struct tm t;
  struct timeval tv;
  va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, sizeof(buf), fmt, ap);
  __builtin_va_end(ap);
  gettimeofday(&tv, 0);
  localtime_r((const time_t *)&tv.tv_sec, &t);
  printf("%02d:%02d:%02d.%03ld - %s\n", t.tm_hour, t.tm_min, t.tm_sec, tv.tv_usec / 1000, buf);
}

canonical_original_code:
void logit(char *fmt, ...)
{
  char buf[2048];
  struct tm t;
  struct timeval tv;
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, sizeof(buf), fmt, ap);
  __builtin_va_end(ap);
  gettimeofday(&tv, 0);
  localtime_r((long *)&tv.tv_sec, &t);
  printf("%02d:%02d:%02d.%03ld - %s\n", t.tm_hour, t.tm_min, t.tm_sec, tv.tv_usec / 1000, buf);
}

variable_types:
fmt
char *

variable_types:
buf
char [2048]

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: struct timeval { long tv_sec; long tv_usec; }


logerr

decompiled_code:
int logerr(char *format, ...)
{
int *v1; // rbx
char *v2; // rax
char *v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-920h] BYREF
struct timeval tv; // [rsp+20h] [rbp-908h] BYREF
struct tm tp; // [rsp+30h] [rbp-8F8h] BYREF
char s[2048]; // [rsp+70h] [rbp-8B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x800uLL, format, arg);
v1 = _errno_location();
v2 = strerror(*v1);
syslog(3, "%s %s", s, v2);
gettimeofday(&tv, 0LL);
localtime_r(&tv.tv_sec, &tp);
v3 = strerror(*v1);
return printf(
"%02d:%02d:%02d.%03ld - %s : %s\n",
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
tv.tv_usec / 1000,
s,
v3);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
int *v1; // rbx
char *v2; // rax
char *v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-920h] BYREF
struct timeval tv; // [rsp+20h] [rbp-908h] BYREF
struct tm tp; // [rsp+30h] [rbp-8F8h] BYREF
char s[2048]; // [rsp+70h] [rbp-8B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x800uLL, format, arg);
v1 = _errno_location();
v2 = strerror(*v1);
syslog(3, "%s %s", s, v2);
gettimeofday(&tv, 0LL);
localtime_r(&tv.tv_sec, &tp);
v3 = strerror(*v1);
return printf(
"%02d:%02d:%02d.%03ld - %s : %s\n",
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
tv.tv_usec / 1000,
s,
v3);
}

original_code:
void logerr(const char *fmt, ...)
{
  char buf[2048];
  struct tm t;
  struct timeval tv;
  va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, sizeof(buf), fmt, ap);
  __builtin_va_end(ap);
  syslog(3, "%s %s", buf, strerror((*__errno_location ())));
  gettimeofday(&tv, 0);
  localtime_r((const time_t *)&tv.tv_sec, &t);
  printf("%02d:%02d:%02d.%03ld - %s : %s\n", t.tm_hour, t.tm_min, t.tm_sec, tv.tv_usec / 1000, buf, strerror((*__errno_location ())));
}

canonical_original_code:
void logerr(char *fmt, ...)
{
  char buf[2048];
  struct tm t;
  struct timeval tv;
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, sizeof(buf), fmt, ap);
  __builtin_va_end(ap);
  syslog(3, "%s %s", buf, strerror((*__errno_location ())));
  gettimeofday(&tv, 0);
  localtime_r((long *)&tv.tv_sec, &t);
  printf("%02d:%02d:%02d.%03ld - %s : %s\n", t.tm_hour, t.tm_min, t.tm_sec, tv.tv_usec / 1000, buf, strerror((*__errno_location ())));
}

user_defined_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; }

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