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


repo: scottgw/quicksilver

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)(); }

return_type: void

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

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


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 *

return_type: void *

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


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]


main

canonical_name: main

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

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

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

sigemptyset(&set);
sigaddset(&set, 14);
pthread_sigmask(0, &set, 0LL);
v4.it_interval.tv_sec = 0LL;
v4.it_interval.tv_usec = 0LL;
v4.it_value.tv_sec = 1LL;
v4.it_value.tv_usec = 0LL;
setitimer(ITIMER_REAL, &v4, 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; }

return_type: int

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


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)
{
int v1; // ebp
int i; // ebx
__int64 v3; // rdi

if ( a1 <= 1 )
return 1LL;
v1 = 0;
for ( i = a1; i > 1; i -= 2 )
{
v3 = (unsigned int)(i - 1);
v1 += fib(v3);
}
return (unsigned int)(v1 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
int v1; // ebp
int i; // ebx
__int64 v3; // rdi

if ( a1 <= 1 )
return 1LL;
v1 = 0;
for ( i = a1; i > 1; i -= 2 )
{
v3 = (unsigned int)(i - 1);
v1 += func9(v3);
}
return (unsigned int)(v1 + 1);
}

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

variable_types: n int


notifier

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

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

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

func10((__int64)a1);
sigemptyset(&v2);
sigaddset(&v2, 14);
pthread_sigmask(1, &v2, 0LL);
pthread_sigmask(0, &v2, &oldmask);
sigsuspend(&oldmask);
pthread_sigmask(1, &v2, 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);
}


fibY

decompiled_code:
__int64 __fastcall fibY(__int64 a1, int a2)
{
int v2; // ebp
int v3; // ebx
__int64 v4; // rsi

v2 = 0;
v3 = a2;
maybe_yield(a1, (unsigned int)a2);
if ( a2 > 1 )
{
do
{
v4 = (unsigned int)(v3 - 1);
v3 -= 2;
v2 += fibY(a1, v4);
maybe_yield(a1, (unsigned int)v3);
}
while ( v3 > 1 );
}
return (unsigned int)(v2 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
int v2; // ebp
int v3; // ebx
__int64 v4; // rsi

v2 = 0;
v3 = a2;
maybe_yield(a1, (unsigned int)a2);
if ( a2 > 1 )
{
do
{
v4 = (unsigned int)(v3 - 1);
v3 -= 2;
v2 += func0(a1, v4);
maybe_yield(a1, (unsigned int)v3);
}
while ( v3 > 1 );
}
return (unsigned int)(v2 + 1);
}

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

variable_types: i int

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

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

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

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:
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 *

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

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

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

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:
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)
{
int v2; // ebp
int i; // ebx
__int64 v4; // rsi

if ( a2 <= 1 )
return 1LL;
v2 = 0;
for ( i = a2; i > 1; i -= 2 )
{
v4 = (unsigned int)(i - 1);
v2 += fib(a1, v4);
}
return (unsigned int)(v2 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
int v2; // ebp
int i; // ebx
__int64 v4; // rsi

if ( a2 <= 1 )
return 1LL;
v2 = 0;
for ( i = a2; i > 1; i -= 2 )
{
v4 = (unsigned int)(i - 1);
v2 += func3(a1, v4);
}
return (unsigned int)(v2 + 1);
}

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; // rbp
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


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


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; // r12
int v1; // edx
_DWORD *result; // rax

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

canonical_decompiled_code:
_DWORD *func1()
{
_DWORD *v0; // r12
int v1; // edx
_DWORD *result; // rax

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

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


decompiled_code:
__int64 __fastcall sysex_to_data(__int64 a1, __int64 a2, unsigned __int8 a3)
{
__int64 v5; // rax
unsigned __int8 v6; // dl
unsigned int v7; // er8
unsigned __int8 v8; // cl
__int64 v9; // r9
char v10; // cl

if ( !a3 )
return 0LL;
v5 = 0LL;
v6 = 0;
v7 = 0;
do
{
while ( (v5 & 7) == 0 )
{
v6 = *(_BYTE *)(a1 + v5++);
if ( a3 <= (unsigned __int8)v5 )
return v7;
}
v8 = v6;
v9 = (unsigned __int8)v7;
v6 >>= 1;
++v7;
v10 = *(_BYTE *)(a1 + v5++) | (v8 << 7);
*(_BYTE *)(a2 + v9) = v10;
}
while ( a3 > (unsigned __int8)v5 );
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, unsigned __int8 a3)
{
__int64 v5; // rax
unsigned __int8 v6; // dl
unsigned int v7; // er8
unsigned __int8 v8; // cl
__int64 v9; // r9
char v10; // cl

if ( !a3 )
return 0LL;
v5 = 0LL;
v6 = 0;
v7 = 0;
do
{
while ( (v5 & 7) == 0 )
{
v6 = *(_BYTE *)(a1 + v5++);
if ( a3 <= (unsigned __int8)v5 )
return v7;
}
v8 = v6;
v9 = (unsigned __int8)v7;
v6 >>= 1;
++v7;
v10 = *(_BYTE *)(a1 + v5++) | (v8 << 7);
*(_BYTE *)(a2 + v9) = v10;
}
while ( a3 > (unsigned __int8)v5 );
return v7;
}

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; // er8
__int64 result; // rax
int v3; // er8

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; // er8
__int64 result; // rax
int v3; // er8

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:
char *__fastcall type_get_lexeme(int a1)
{
__int64 v1; // rdi
char *result; // rax

v1 = (unsigned int)(a1 + 1);
result = (char *)&unk_24D;
if ( (unsigned int)v1 <= 4 )
result = CSWTCH_7[v1];
return result;
}

canonical_decompiled_code:
char *__fastcall func1(int a1)
{
__int64 v1; // rdi
char *result; // rax

v1 = (unsigned int)(a1 + 1);
result = (char *)&unk_24D;
if ( (unsigned int)v1 <= 4 )
result = CSWTCH_7[v1];
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:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__int64 v4; // rax
__int64 v5; // rbx
__int64 *v6; // rbp
const char *v7; // rbx
const char *v8; // rsi
unsigned int v9; // ebx
unsigned int v11; // ebx
__int64 v12; // rdi
const char *v14; // rax
char v15[56]; // [rsp+0h] [rbp-38h] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__int64 v4; // rax
__int64 v5; // rbx
__int64 *v6; // rbp
const char *v7; // rbx
const char *v8; // rsi
unsigned int v9; // ebx
unsigned int v11; // ebx
__int64 v12; // rdi
const char *v14; // rax
char v15[56]; // [rsp+0h] [rbp-38h] BYREF

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

variable_types: i int

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:
__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)
{
__int64 result; // rax
int v3; // edx

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

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

if ( buffer_current_type != a1 && buffer_current_type >= 0 )
func8();
result = buffer_current_fill;
buffer_current_type = a1;
v3 = buffer_current_fill + 1;
*((_BYTE *)&buffer + buffer_current_fill) = a2;
buffer_current_fill = v3;
if ( v3 == 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; // si
char *v2; // rbx

v1 = *a1;
if ( *a1 )
{
v2 = a1;
do
{
++v2;
lcd_enqueue(72, v1);
v1 = *v2;
}
while ( *v2 );
}
return lcd_flush();
}

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

v1 = *a1;
if ( *a1 )
{
v2 = a1;
do
{
++v2;
func9(72, v1);
v1 = *v2;
}
while ( *v2 );
}
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()
{
unsigned int v0; // er12
int v1; // ebp
__int16 v2; // bx
int result; // eax
__int16 v4; // [rsp+Eh] [rbp-1Ah]

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

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

v0 = 0;
v1 = 100;
do
{
v2 = rand();
if ( (int)usb_control_msg() < 0 )
return fwrite("USB request failed!", 1uLL, 0x13uLL, stderr);
v0 += v4 != v2;
--v1;
}
while ( v1 );
if ( v0 )
result = fprintf(stderr, "ERROR: %d out of %d echo transfers failed!\n", v0, 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: 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";
v2 = "CTRL0";
if ( (result & 2) == 0 )
v1 = "Firmware version %d.%d\n" + 23;
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";
v2 = "CTRL0";
if ( (result & 2) == 0 )
v1 = "Firmware version %d.%d\n" + 23;
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; // rsi
const char *v2; // r8

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

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

result = func15(136LL);
if ( result != -1 )
{
v1 = "on";
v2 = "off";
if ( (result & 2) != 0 )
v2 = "on";
if ( (result & 1) == 0 )
v1 = "off";
result = printf("Keys: 0:%s 1:%s\n", v1, v2);
}
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:
__int64 handle_signals()
{
__int64 result; // rax
sigset_t v1; // [rsp+0h] [rbp-128h] BYREF
struct sigaction v2; // [rsp+80h] [rbp-A8h] BYREF

if ( sigfillset(&v1) == -1
|| pthread_sigmask(2, &v1, 0LL) == -1
|| (memset(&v2, 0, sizeof(v2)), sigfillset(&v2.sa_mask) == -1)
|| (v2.sa_handler = (__sighandler_t)(closed + 1), sigaction(1, &v2, 0LL) == -1)
|| sigaction(3, &v2, 0LL) == -1
|| sigaction(13, &v2, 0LL) == -1
|| (v2.sa_handler = (__sighandler_t)sig_handler, sigaction(2, &v2, 0LL) == -1)
|| sigaction(15, &v2, 0LL) == -1
|| sigaction(7, &v2, 0LL) == -1
|| sigaction(8, &v2, 0LL) == -1
|| sigaction(4, &v2, 0LL) == -1
|| sigaction(11, &v2, 0LL) == -1
|| sigaction(31, &v2, 0LL) == -1
|| sigaction(24, &v2, 0LL) == -1
|| sigaction(25, &v2, 0LL) == -1
|| sigemptyset(&v1) == -1 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)-(pthread_sigmask(2, &v1, 0LL) == -1);
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
sigset_t v1; // [rsp+0h] [rbp-128h] BYREF
struct sigaction v2; // [rsp+80h] [rbp-A8h] BYREF

if ( sigfillset(&v1) == -1
|| pthread_sigmask(2, &v1, 0LL) == -1
|| (memset(&v2, 0, sizeof(v2)), sigfillset(&v2.sa_mask) == -1)
|| (v2.sa_handler = (__sighandler_t)(closed + 1), sigaction(1, &v2, 0LL) == -1)
|| sigaction(3, &v2, 0LL) == -1
|| sigaction(13, &v2, 0LL) == -1
|| (v2.sa_handler = (__sighandler_t)sig_handler, sigaction(2, &v2, 0LL) == -1)
|| sigaction(15, &v2, 0LL) == -1
|| sigaction(7, &v2, 0LL) == -1
|| sigaction(8, &v2, 0LL) == -1
|| sigaction(4, &v2, 0LL) == -1
|| sigaction(11, &v2, 0LL) == -1
|| sigaction(31, &v2, 0LL) == -1
|| sigaction(24, &v2, 0LL) == -1
|| sigaction(25, &v2, 0LL) == -1
|| sigemptyset(&v1) == -1 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)-(pthread_sigmask(2, &v1, 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)(); }

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

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


decompiled_code:
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 func4(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 func0()
{
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 func2()
{
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, ...)
{
struct timeval v2; // [rsp+0h] [rbp-928h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-910h] 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(&v2, 0LL);
localtime_r(&v2.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,
v2.tv_usec / 1000,
s);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
struct timeval v2; // [rsp+0h] [rbp-928h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-910h] 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(&v2, 0LL);
localtime_r(&v2.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,
v2.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
struct timeval v5; // [rsp+0h] [rbp-928h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-910h] 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(&v5, 0LL);
localtime_r(&v5.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,
v5.tv_usec / 1000,
s,
v3);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
int *v1; // rbx
char *v2; // rax
char *v3; // rax
struct timeval v5; // [rsp+0h] [rbp-928h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-910h] 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(&v5, 0LL);
localtime_r(&v5.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,
v5.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; }


decompiled_code:
ssize_t __fastcall print_sig_desc(int a1)
{
ssize_t result; // rax

switch ( a1 )
{
case 1:
result = write(1, "\nSIGHUP: hangup\n", 0x11uLL);
break;
case 2:
result = write(1, "\nSIGINT: interrupt (from keyboard)\n", 0x24uLL);
break;
case 3:
result = write(1, "\nSIGQUIT: quit (from keyboard)\n", 0x20uLL);
break;
case 4:
result = write(1, "\nSIGILL: illegal instruction\n", 0x1EuLL);
break;
case 5:
result = write(1, "\nSIGTRAP: trace/breakpoint trap\n", 0x21uLL);
break;
case 6:
result = write(1, "\nSIGABRT: call to abort\n", 0x19uLL);
break;
case 7:
result = write(1, "\nSIGBUS: access to undefined portion of a memory object\n", 0x39uLL);
break;
case 8:
result = write(1, "\nSIGFPE: erroneous arithmetic operation\n", 0x29uLL);
break;
case 9:
result = write(1, "\nSIGKILL: kill; synthetic only\n", 0x20uLL);
break;
case 10:
result = write(1, "\nSIGUSR1: user signal 1; synthetic only\n", 0x29uLL);
break;
case 11:
result = write(1, "\nSIGSEGV: invalid memory reference\n", 0x24uLL);
break;
case 12:
result = write(1, "\nSIGUSR2: user signal 2; synthetic only\n", 0x29uLL);
break;
case 13:
result = write(1, "\nSIGPIPE: write on a pipe with no reader\n", 0x2AuLL);
break;
case 14:
result = write(1, "\nSIGALRM: alarm clock expired\n", 0x1FuLL);
break;
case 15:
result = write(1, "\nSIGTERM: termination; synthetic only\n", 0x27uLL);
break;
case 17:
result = write(1, "\nSIGCHLD: child process terminated or stopped\n", 0x2FuLL);
break;
case 18:
result = write(1, "\nSIGCONT: continue executing (from keyboard)\n", 0x2EuLL);
break;
case 19:
result = write(1, "\nSIGSTOP: stop executing (from keyboard)\n", 0x2AuLL);
break;
case 20:
result = write(1, "\nSIGTSTP: terminal stop signal (from keyboard)\n", 0x30uLL);
break;
case 21:
result = write(1, "\nSIGTTIN: background process attempting read\n", 0x2EuLL);
break;
case 22:
result = write(1, "\nSIGTTOU: background process attemping write\n", 0x2EuLL);
break;
case 23:
result = write(1, "\nSIGURG: out-of-band data available at socket\n", 0x2FuLL);
break;
case 24:
result = write(1, "\nSIGXCPU: CPU-time limit exceeded\n", 0x23uLL);
break;
case 25:
result = write(1, "\nSIGXFSZ: file-size limit exceeded\n", 0x24uLL);
break;
case 26:
result = write(1, "\nSIGVTALRM: virtual timer expired\n", 0x23uLL);
break;
case 27:
result = write(1, "\nSIGPROF: profiling timer expired\n", 0x23uLL);
break;
case 29:
result = write(1, "\nSIGPOLL: pollable event\n", 0x1AuLL);
break;
case 31:
result = write(1, "\nSIGSYS: bad system call\n", 0x1AuLL);
break;
default:
result = write(1, "\nUnknown signal\n", 0x10uLL);
break;
}
return result;
}

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

switch ( a1 )
{
case 1:
result = write(1, "\nSIGHUP: hangup\n", 0x11uLL);
break;
case 2:
result = write(1, "\nSIGINT: interrupt (from keyboard)\n", 0x24uLL);
break;
case 3:
result = write(1, "\nSIGQUIT: quit (from keyboard)\n", 0x20uLL);
break;
case 4:
result = write(1, "\nSIGILL: illegal instruction\n", 0x1EuLL);
break;
case 5:
result = write(1, "\nSIGTRAP: trace/breakpoint trap\n", 0x21uLL);
break;
case 6:
result = write(1, "\nSIGABRT: call to abort\n", 0x19uLL);
break;
case 7:
result = write(1, "\nSIGBUS: access to undefined portion of a memory object\n", 0x39uLL);
break;
case 8:
result = write(1, "\nSIGFPE: erroneous arithmetic operation\n", 0x29uLL);
break;
case 9:
result = write(1, "\nSIGKILL: kill; synthetic only\n", 0x20uLL);
break;
case 10:
result = write(1, "\nSIGUSR1: user signal 1; synthetic only\n", 0x29uLL);
break;
case 11:
result = write(1, "\nSIGSEGV: invalid memory reference\n", 0x24uLL);
break;
case 12:
result = write(1, "\nSIGUSR2: user signal 2; synthetic only\n", 0x29uLL);
break;
case 13:
result = write(1, "\nSIGPIPE: write on a pipe with no reader\n", 0x2AuLL);
break;
case 14:
result = write(1, "\nSIGALRM: alarm clock expired\n", 0x1FuLL);
break;
case 15:
result = write(1, "\nSIGTERM: termination; synthetic only\n", 0x27uLL);
break;
case 17:
result = write(1, "\nSIGCHLD: child process terminated or stopped\n", 0x2FuLL);
break;
case 18:
result = write(1, "\nSIGCONT: continue executing (from keyboard)\n", 0x2EuLL);
break;
case 19:
result = write(1, "\nSIGSTOP: stop executing (from keyboard)\n", 0x2AuLL);
break;
case 20:
result = write(1, "\nSIGTSTP: terminal stop signal (from keyboard)\n", 0x30uLL);
break;
case 21:
result = write(1, "\nSIGTTIN: background process attempting read\n", 0x2EuLL);
break;
case 22:
result = write(1, "\nSIGTTOU: background process attemping write\n", 0x2EuLL);
break;
case 23:
result = write(1, "\nSIGURG: out-of-band data available at socket\n", 0x2FuLL);
break;
case 24:
result = write(1, "\nSIGXCPU: CPU-time limit exceeded\n", 0x23uLL);
break;
case 25:
result = write(1, "\nSIGXFSZ: file-size limit exceeded\n", 0x24uLL);
break;
case 26:
result = write(1, "\nSIGVTALRM: virtual timer expired\n", 0x23uLL);
break;
case 27:
result = write(1, "\nSIGPROF: profiling timer expired\n", 0x23uLL);
break;
case 29:
result = write(1, "\nSIGPOLL: pollable event\n", 0x1AuLL);
break;
case 31:
result = write(1, "\nSIGSYS: bad system call\n", 0x1AuLL);
break;
default:
result = write(1, "\nUnknown signal\n", 0x10uLL);
break;
}
return result;
}

original_code:
void print_sig_desc(int sig)
{
  switch (sig) {
  case 7:
    write(1, "\nSIGBUS:  access to undefined portion of a memory object\n", 57);
    break;
  case 8:
    write(1, "\nSIGFPE:  erroneous arithmetic operation\n", 41);
    break;
  case 4:
    write(1, "\nSIGILL:  illegal instruction\n", 30);
    break;
  case 13:
    write(1, "\nSIGPIPE:  write on a pipe with no reader\n", 42);
    break;
  case 11:
    write(1, "\nSIGSEGV:  invalid memory reference\n", 36);
    break;
  case 31:
    write(1, "\nSIGSYS:  bad system call\n", 26);
    break;
  case 24:
    write(1, "\nSIGXCPU:  CPU-time limit exceeded\n", 35);
    break;
  case 25:
    write(1, "\nSIGXFSZ:  file-size limit exceeded\n", 36);
    break;
  case 6:
    write(1, "\nSIGABRT:  call to abort\n", 25);
    break;
  case 1:
    write(1, "\nSIGHUP:  hangup\n", 17);
    break;
  case 2:
    write(1, "\nSIGINT:  interrupt (from keyboard)\n", 36);
    break;
  case 9:
    write(1, "\nSIGKILL:  kill; synthetic only\n", 32);
    break;
  case 3:
    write(1, "\nSIGQUIT:  quit (from keyboard)\n", 32);
    break;
  case 15:
    write(1, "\nSIGTERM:  termination; synthetic only\n", 39);
    break;
  case 10:
    write(1, "\nSIGUSR1:  user signal 1; synthetic only\n", 41);
    break;
  case 12:
    write(1, "\nSIGUSR2:  user signal 2; synthetic only\n", 41);
    break;
  case 17:
    write(1, "\nSIGCHLD:  child process terminated or stopped\n", 47);
    break;
  case 18:
    write(1, "\nSIGCONT:  continue executing (from keyboard)\n", 46);
    break;
  case 19:
    write(1, "\nSIGSTOP:  stop executing (from keyboard)\n", 42);
    break;
  case 20:
    write(1, "\nSIGTSTP:  terminal stop signal (from keyboard)\n", 48);
    break;
  case 21:
    write(1, "\nSIGTTIN:  background process attempting read\n", 46);
    break;
  case 22:
    write(1, "\nSIGTTOU:  background process attemping write\n", 46);
    break;
  case 14:
    write(1, "\nSIGALRM:  alarm clock expired\n", 31);
    break;
  case 26:
    write(1, "\nSIGVTALRM:  virtual timer expired\n", 35);
    break;
  case 27:
    write(1, "\nSIGPROF:  profiling timer expired\n", 35);
    break;
  case 29:
    write(1, "\nSIGPOLL:  pollable event\n", 26);
    break;
  case 5:
    write(1, "\nSIGTRAP:  trace/breakpoint trap\n", 33);
    break;
  case 23:
    write(1, "\nSIGURG:  out-of-band data available at socket\n", 47);
    break;
  default:
    write(1, "\nUnknown signal\n", 16);
  }
}


repo: scubjwu/mempool

decompiled_code:
int __fastcall cal_run_time(int a1, int a2, int a3, int a4)
{
return printf("time coast: %lf\n", (double)a4 / 1000000.0 - (double)a2 / 1000000.0 + (double)(a3 - a1));
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, int a4)
{
return printf("time coast: %lf\n", (double)a4 / 1000000.0 - (double)a2 / 1000000.0 + (double)(a3 - a1));
}

original_code:
static void cal_run_time(struct timeval start, struct timeval end)
{
 double time_slot;
 time_slot = end.tv_sec - start.tv_sec + ((double)end.tv_usec/1000000 - (double)start.tv_usec/1000000);
 printf("time coast: %lf\n", time_slot);
 return;
}

variable_types:
end
struct timeval


decompiled_code:
int __fastcall thread_test(void *arg)
{
char *v1; // rbx
pthread_t *v2; // rbp
pthread_t v3; // rdi
_BYTE v5[800]; // [rsp+0h] [rbp-348h] BYREF
char v6[40]; // [rsp+320h] [rbp-28h] BYREF

printf("============>%s\n", "thread_test");
v1 = v5;
v2 = (pthread_t *)v5;
do
{
if ( pthread_create(v2, 0LL, run_thread, arg) < 0 )
{
perror("pthread_create");
exit(1);
}
++v2;
}
while ( v2 != (pthread_t *)v6 );
do
{
v3 = *(_QWORD *)v1;
v1 += 8;
pthread_join(v3, 0LL);
}
while ( v1 != v6 );
return printf("%s<============\n\n", "thread_test");
}

canonical_decompiled_code:
int __fastcall func4(void *arg)
{
char *v1; // rbx
pthread_t *v2; // rbp
pthread_t v3; // rdi
_BYTE v5[800]; // [rsp+0h] [rbp-348h] BYREF
char v6[40]; // [rsp+320h] [rbp-28h] BYREF

printf("============>%s\n", "thread_test");
v1 = v5;
v2 = (pthread_t *)v5;
do
{
if ( pthread_create(v2, 0LL, run_thread, arg) < 0 )
{
perror("pthread_create");
exit(1);
}
++v2;
}
while ( v2 != (pthread_t *)v6 );
do
{
v3 = *(_QWORD *)v1;
v1 += 8;
pthread_join(v3, 0LL);
}
while ( v1 != v6 );
return printf("%s<============\n\n", "thread_test");
}

original_code:
static void thread_test(mempool pool)
{
 printf("============>%s\n", __FUNCTION__);
 pthread_t tid[100];
 int i;
 for(i=0; i<100; i++){
  if(pthread_create(&tid[i], 0, run_thread, (void *)pool) < 0){
   perror("pthread_create");
   exit(1);
  }
 }
 for(i=0; i<100; i++)
  pthread_join(tid[i], ((void *)0));
 printf("%s<============\n\n", __FUNCTION__);
}

canonical_original_code:
static void thread_test(struct mempool_class *pool)
{
 printf("============>%s\n", __FUNCTION__);
 unsigned long tid[100];
 int i;
 for(i=0; i<100; i++){
  if(pthread_create(&tid[i], 0, run_thread, (void *)pool) < 0){
   perror("pthread_create");
   exit(1);
  }
 }
 for(i=0; i<100; i++)
  pthread_join(tid[i], ((void *)0));
 printf("%s<============\n\n", __FUNCTION__);
}

variable_types: struct mempool_class { void *m_pool; struct mempool_func { void *(*chunk_alloc)(void *m_pool, unsigned long); void (*chunk_free)(void *m_pool, void *chunk); void (*pool_free)(void *m_pool); } m_func; } *

variable_types:
tid
unsigned long [100]

variable_types: i int

user_defined_types: struct mempool_class { void *m_pool; struct mempool_func m_func; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // ebx
__int64 v5; // r14
__int64 v6; // r13
unsigned int v7; // er12
int v8; // eax
__int64 *v9; // rbx
char v11[53]; // [rsp+3h] [rbp-35h] BYREF

v4 = argc;
if ( argc > 1 )
{
do
{
if ( *argv[1] == 45 )
break;
--v4;
++argv;
}
while ( v4 != 1 );
}
v5 = 0LL;
v6 = 0LL;
v7 = -1;
while ( 1 )
{
v8 = getopt(v4, (char *const *)argv, "n:s:b:r:");
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'r':
v7 = atoi(optarg);
break;
case 's':
v5 = atoi(optarg);
break;
case 'b':
v6 = atoi(optarg);
break;
default:
puts("wrong argv!");
exit(1);
}
}
if ( v7 == -1 )
{
puts("need round num for stress test(-r)!");
if ( v5 )
goto LABEL_21;
goto LABEL_20;
}
if ( !v5 )
{
LABEL_20:
puts("need pool size(-s)!");
LABEL_21:
if ( v6 )
goto LABEL_22;
goto LABEL_24;
}
if ( !v6 )
{
LABEL_24:
puts("need base size for stress test(-b)!");
LABEL_22:
exit(1);
}
printf("test pram:\n#pool size: %d\n#round num:%d\t#base size:%d\n\n", v5, v7, v6);
v9 = (__int64 *)mempool_init(v5, 1LL, 0LL, 0LL, 0LL, 0LL);
if ( !v9 )
{
puts("failed on pool create!");
goto LABEL_22;
}
puts("done pool create!");
strcpy(v11, "hello world!");
sanity_test(v9, v11);
stress_test(v9, v7, v6);
thread_test(v9);
sim_test(v9, v7, v6);
((void (__fastcall *)(__int64))v9[3])(*v9);
puts("done pool distroy!");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // ebx
__int64 v5; // r14
__int64 v6; // r13
unsigned int v7; // er12
int v8; // eax
__int64 *v9; // rbx
char v11[53]; // [rsp+3h] [rbp-35h] BYREF

v4 = argc;
if ( argc > 1 )
{
do
{
if ( *argv[1] == 45 )
break;
--v4;
++argv;
}
while ( v4 != 1 );
}
v5 = 0LL;
v6 = 0LL;
v7 = -1;
while ( 1 )
{
v8 = getopt(v4, (char *const *)argv, "n:s:b:r:");
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'r':
v7 = atoi(optarg);
break;
case 's':
v5 = atoi(optarg);
break;
case 'b':
v6 = atoi(optarg);
break;
default:
puts("wrong argv!");
exit(1);
}
}
if ( v7 == -1 )
{
puts("need round num for stress test(-r)!");
if ( v5 )
goto LABEL_21;
goto LABEL_20;
}
if ( !v5 )
{
LABEL_20:
puts("need pool size(-s)!");
LABEL_21:
if ( v6 )
goto LABEL_22;
goto LABEL_24;
}
if ( !v6 )
{
LABEL_24:
puts("need base size for stress test(-b)!");
LABEL_22:
exit(1);
}
printf("test pram:\n#pool size: %d\n#round num:%d\t#base size:%d\n\n", v5, v7, v6);
v9 = (__int64 *)mempool_init(v5, 1LL, 0LL, 0LL, 0LL, 0LL);
if ( !v9 )
{
puts("failed on pool create!");
goto LABEL_22;
}
puts("done pool create!");
strcpy(v11, "hello world!");
func2(v9, v11);
func3(v9, v7, v6);
func4(v9);
func1(v9, v7, v6);
((void (__fastcall *)(__int64))v9[3])(*v9);
puts("done pool distroy!");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 extern char *optarg;
 int opt;
 _Bool test_p = 1;
 int roundnum = -1;
 size_t size_base = 0;
 size_t pool_size = 0;
 while(argc > 1 && (*argv[1] != '-')){
  argv++;
  argc--;
 }
 while((-1) != (opt = getopt(argc, argv, "n:s:b:r:")))
  switch(opt){
  case 's':
   pool_size = (size_t)atoi(optarg);
   break;
  case 'b':
   size_base = (size_t)atoi(optarg);
   break;
  case 'r':
   roundnum = atoi(optarg);
   break;
  default:
   printf("wrong argv!\n");
   exit(1);
  }
 if(roundnum == -1){
  printf("need round num for stress test(-r)!\n");
  test_p = 0;
 }
 if(pool_size == 0){
  printf("need pool size(-s)!\n");
  test_p = 0;
 }
 if(size_base == 0){
  printf("need base size for stress test(-b)!\n");
  test_p = 0;
 }
 if(!test_p)
  exit(1);
 printf("test pram:\n#pool size: %d\n#round num:%d\t#base size:%d\n\n",
  pool_size, roundnum, size_base);
 mempool test_pool = ((void *)0);
 test_pool = mempool_init(pool_size, 1, LINK_POOL, ((void *)0), ((void *)0), ((void *)0));
 if(!test_pool){
  printf("failed on pool create!\n");
  exit(1);
 }
 printf("done pool create!\n");
 char test_str[] = "hello world!";
 sanity_test(test_pool, test_str);
 stress_test(test_pool, roundnum, size_base);
 thread_test(test_pool);
 sim_test(test_pool, roundnum, size_base);
 test_pool->m_func.pool_free(test_pool->m_pool);
 printf("done pool distroy!\n");
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 extern char *optarg;
 int opt;
 _Bool test_p = 1;
 int roundnum = -1;
 unsigned long size_base = 0;
 unsigned long pool_size = 0;
 while(argc > 1 && (*argv[1] != '-')){
  argv++;
  argc--;
 }
 while((-1) != (opt = getopt(argc, argv, "n:s:b:r:")))
  switch(opt){
  case 's':
   pool_size = (unsigned long)atoi(optarg);
   break;
  case 'b':
   size_base = (unsigned long)atoi(optarg);
   break;
  case 'r':
   roundnum = atoi(optarg);
   break;
  default:
   printf("wrong argv!\n");
   exit(1);
  }
 if(roundnum == -1){
  printf("need round num for stress test(-r)!\n");
  test_p = 0;
 }
 if(pool_size == 0){
  printf("need pool size(-s)!\n");
  test_p = 0;
 }
 if(size_base == 0){
  printf("need base size for stress test(-b)!\n");
  test_p = 0;
 }
 if(!test_p)
  exit(1);
 printf("test pram:\n#pool size: %d\n#round num:%d\t#base size:%d\n\n",
  pool_size, roundnum, size_base);
 struct mempool_class *test_pool = ((void *)0);
 test_pool = mempool_init(pool_size, 1, LINK_POOL, ((void *)0), ((void *)0), ((void *)0));
 if(!test_pool){
  printf("failed on pool create!\n");
  exit(1);
 }
 printf("done pool create!\n");
 char test_str[] = "hello world!";
 sanity_test(test_pool, test_str);
 stress_test(test_pool, roundnum, size_base);
 thread_test(test_pool);
 sim_test(test_pool, roundnum, size_base);
 test_pool->m_func.pool_free(test_pool->m_pool);
 printf("done pool distroy!\n");
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
optarg
char *

variable_types: opt int

variable_types: roundnum int

user_defined_types: struct mempool_func { void *(*chunk_alloc)(void *m_pool, unsigned long); void (*chunk_free)(void *m_pool, void *chunk); void (*pool_free)(void *m_pool); }


decompiled_code:
void __fastcall chain_lock_free(void *ptr)
{
if ( pthread_spin_destroy(*(pthread_spinlock_t **)ptr) )
{
perror("pthread_spin_destroy");
exit(1);
}
munmap(*(void **)ptr, 4uLL);
shm_unlink(*((const char **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( pthread_spin_destroy(*(pthread_spinlock_t **)ptr) )
{
perror("pthread_spin_destroy");
exit(1);
}
munmap(*(void **)ptr, 4uLL);
shm_unlink(*((const char **)ptr + 1));
free(ptr);
}

original_code:
static void chain_lock_free(lock_entity lock_t)
{
 if(pthread_spin_destroy((pthread_spinlock_t *)lock_t->lock_addr)){
  perror("pthread_spin_destroy");
  exit(1);
 }
 munmap(lock_t->lock_addr, sizeof(pthread_spinlock_t));
 shm_unlink(lock_t->lock_name);
 free(lock_t);
 lock_t = ((void *)0);
}

canonical_original_code:
static void chain_lock_free(struct mem_lock_t *lock_t)
{
 if(pthread_spin_destroy((int *)lock_t->lock_addr)){
  perror("pthread_spin_destroy");
  exit(1);
 }
 munmap(lock_t->lock_addr, sizeof(pthread_spinlock_t));
 shm_unlink(lock_t->lock_name);
 free(lock_t);
 lock_t = ((void *)0);
}

variable_types: struct mem_lock_t { void *lock_addr; char *lock_name; } *


decompiled_code:
void __fastcall pool_lock_free(void *ptr)
{
if ( pthread_mutex_destroy(*(pthread_mutex_t **)ptr) )
{
perror("pthread_mutex_destroy");
exit(1);
}
munmap(*(void **)ptr, 0x28uLL);
shm_unlink(*((const char **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( pthread_mutex_destroy(*(pthread_mutex_t **)ptr) )
{
perror("pthread_mutex_destroy");
exit(1);
}
munmap(*(void **)ptr, 0x28uLL);
shm_unlink(*((const char **)ptr + 1));
free(ptr);
}

original_code:
static void pool_lock_free(lock_entity lock_t)
{
 if(pthread_mutex_destroy((pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_destroy");
  exit(1);
 }
 munmap(lock_t->lock_addr, sizeof(pthread_mutex_t));
 shm_unlink(lock_t->lock_name);
 free(lock_t);
 lock_t = ((void *)0);
}

canonical_original_code:
static void pool_lock_free(struct mem_lock_t *lock_t)
{
 if(pthread_mutex_destroy((union pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_destroy");
  exit(1);
 }
 munmap(lock_t->lock_addr, sizeof(pthread_mutex_t));
 shm_unlink(lock_t->lock_name);
 free(lock_t);
 lock_t = ((void *)0);
}

user_defined_types: struct mem_lock_t { void *lock_addr; char *lock_name; }

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

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

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


decompiled_code:
int __fastcall chain_unlock(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_unlock(*a1);
if ( result )
{
perror("pthread_spin_unlock");
pthread_exit(0LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_unlock(*a1);
if ( result )
{
perror("pthread_spin_unlock");
pthread_exit(0LL);
}
return result;
}

original_code:
static void chain_unlock(lock_entity lock_t)
{
 if(pthread_spin_unlock((pthread_spinlock_t *)lock_t->lock_addr)){
  perror("pthread_spin_unlock");
  pthread_exit(((void *)0));
 }
}

canonical_original_code:
static void chain_unlock(struct mem_lock_t *lock_t)
{
 if(pthread_spin_unlock((int *)lock_t->lock_addr)){
  perror("pthread_spin_unlock");
  pthread_exit(((void *)0));
 }
}


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

result = pthread_mutex_unlock(*a1);
if ( result )
{
perror("pthread_mutex_unlock");
pthread_exit(0LL);
}
return result;
}

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

result = pthread_mutex_unlock(*a1);
if ( result )
{
perror("pthread_mutex_unlock");
pthread_exit(0LL);
}
return result;
}

original_code:
static void pool_unlock(lock_entity lock_t)
{
 if(pthread_mutex_unlock((pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_unlock");
  pthread_exit(((void *)0));
 }
}

canonical_original_code:
static void pool_unlock(struct mem_lock_t *lock_t)
{
 if(pthread_mutex_unlock((union pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_unlock");
  pthread_exit(((void *)0));
 }
}


decompiled_code:
int __fastcall chain_lock(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_lock(*a1);
if ( result )
{
perror("pthread_spin_lock");
pthread_exit(0LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_lock(*a1);
if ( result )
{
perror("pthread_spin_lock");
pthread_exit(0LL);
}
return result;
}

original_code:
static void chain_lock(lock_entity lock_t)
{
 if(pthread_spin_lock((pthread_spinlock_t *)lock_t->lock_addr)){
  perror("pthread_spin_lock");
  pthread_exit(((void *)0));
 }
}

canonical_original_code:
static void chain_lock(struct mem_lock_t *lock_t)
{
 if(pthread_spin_lock((int *)lock_t->lock_addr)){
  perror("pthread_spin_lock");
  pthread_exit(((void *)0));
 }
}


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

result = pthread_mutex_lock(*a1);
if ( result )
{
perror("pthread_mutex_lock");
pthread_exit(0LL);
}
return result;
}

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

result = pthread_mutex_lock(*a1);
if ( result )
{
perror("pthread_mutex_lock");
pthread_exit(0LL);
}
return result;
}

original_code:
static void pool_lock(lock_entity lock_t)
{
 if(pthread_mutex_lock((pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_lock");
  pthread_exit(((void *)0));
 }
}

canonical_original_code:
static void pool_lock(struct mem_lock_t *lock_t)
{
 if(pthread_mutex_lock((union pthread_mutex_t *)lock_t->lock_addr)){
  perror("pthread_mutex_lock");
  pthread_exit(((void *)0));
 }
}


decompiled_code:
int __fastcall chain_lock_init(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_init(*a1, 1);
if ( result )
{
perror("pthread_spin_init");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(pthread_spinlock_t **a1)
{
int result; // eax

result = pthread_spin_init(*a1, 1);
if ( result )
{
perror("pthread_spin_init");
exit(1);
}
return result;
}

original_code:
static void chain_lock_init(lock_entity lock_t)
{
 if(pthread_spin_init((pthread_spinlock_t *)lock_t->lock_addr, PTHREAD_PROCESS_SHARED)){
  perror("pthread_spin_init");
  exit(1);
 }
}

canonical_original_code:
static void chain_lock_init(struct mem_lock_t *lock_t)
{
 if(pthread_spin_init((int *)lock_t->lock_addr, PTHREAD_PROCESS_SHARED)){
  perror("pthread_spin_init");
  exit(1);
 }
}


decompiled_code:
int __fastcall pool_lock_init(pthread_mutex_t **a1)
{
int result; // eax
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

if ( pthread_mutexattr_init(&attr) )
{
perror("pthread_mutexattr_init");
exit(1);
}
if ( pthread_mutexattr_setpshared(&attr, 1) )
{
perror("pthread_mutexattr_setpshared");
exit(1);
}
result = pthread_mutex_init(*a1, &attr);
if ( result )
{
perror("pthread_mutex_init");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(pthread_mutex_t **a1)
{
int result; // eax
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

if ( pthread_mutexattr_init(&attr) )
{
perror("pthread_mutexattr_init");
exit(1);
}
if ( pthread_mutexattr_setpshared(&attr, 1) )
{
perror("pthread_mutexattr_setpshared");
exit(1);
}
result = pthread_mutex_init(*a1, &attr);
if ( result )
{
perror("pthread_mutex_init");
exit(1);
}
return result;
}

original_code:
static void pool_lock_init(lock_entity lock_t)
{
 pthread_mutexattr_t p_attr;
 if(pthread_mutexattr_init(&p_attr)){
  perror("pthread_mutexattr_init");
  exit(1);
 }
 if(pthread_mutexattr_setpshared(&p_attr, PTHREAD_PROCESS_SHARED)){
  perror("pthread_mutexattr_setpshared");
  exit(1);
 }
 if(pthread_mutex_init((pthread_mutex_t *)lock_t->lock_addr, &p_attr)){
  perror("pthread_mutex_init");
  exit(1);
 }
}

canonical_original_code:
static void pool_lock_init(struct mem_lock_t *lock_t)
{
 union pthread_mutexattr_t p_attr;
 if(pthread_mutexattr_init(&p_attr)){
  perror("pthread_mutexattr_init");
  exit(1);
 }
 if(pthread_mutexattr_setpshared(&p_attr, PTHREAD_PROCESS_SHARED)){
  perror("pthread_mutexattr_setpshared");
  exit(1);
 }
 if(pthread_mutex_init((union pthread_mutex_t *)lock_t->lock_addr, &p_attr)){
  perror("pthread_mutex_init");
  exit(1);
 }
}

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


decompiled_code:
void *__fastcall lock_entity_init(__int64 a1, _QWORD *a2, unsigned int a3)
{
void *v4; // rax
void *v5; // r12
char *v6; // rax
int v7; // eax
int v8; // ebp
int v9; // er8
size_t v10; // rsi
void *result; // rax

if ( !a1 )
{
puts("should init lock name");
exit(1);
}
if ( a3 > 2 )
{
puts("invalid parameter");
exit(1);
}
v4 = malloc(0x10uLL);
*a2 = v4;
v5 = v4;
v6 = (char *)calloc(0x80uLL, 1uLL);
*((_QWORD *)v5 + 1) = v6;
sprintf(v6, "%s.%d", (const char *)(a1 + 4), a3);
v7 = shm_open(*((const char **)v5 + 1), 194, 0x1A4u);
v8 = v7;
if ( v7 == -1 )
{
perror("shm_open");
exit(1);
}
if ( a3 - 1 > 1 )
{
if ( !ftruncate(v7, 40LL) )
{
v9 = v8;
v10 = 40LL;
goto LABEL_7;
}
LABEL_15:
perror("ftruncate");
exit(1);
}
if ( ftruncate(v7, 4LL) )
goto LABEL_15;
v9 = v8;
v10 = 4LL;
LABEL_7:
result = mmap(0LL, v10, 3, 1, v9, 0LL);
*(_QWORD *)v5 = result;
if ( !result )
{
perror("mmap");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, _QWORD *a2, unsigned int a3)
{
void *v4; // rax
void *v5; // r12
char *v6; // rax
int v7; // eax
int v8; // ebp
int v9; // er8
size_t v10; // rsi
void *result; // rax

if ( !a1 )
{
puts("should init lock name");
exit(1);
}
if ( a3 > 2 )
{
puts("invalid parameter");
exit(1);
}
v4 = malloc(0x10uLL);
*a2 = v4;
v5 = v4;
v6 = (char *)calloc(0x80uLL, 1uLL);
*((_QWORD *)v5 + 1) = v6;
sprintf(v6, "%s.%d", (const char *)(a1 + 4), a3);
v7 = shm_open(*((const char **)v5 + 1), 194, 0x1A4u);
v8 = v7;
if ( v7 == -1 )
{
perror("shm_open");
exit(1);
}
if ( a3 - 1 > 1 )
{
if ( !ftruncate(v7, 40LL) )
{
v9 = v8;
v10 = 40LL;
goto LABEL_7;
}
LABEL_15:
perror("ftruncate");
exit(1);
}
if ( ftruncate(v7, 4LL) )
goto LABEL_15;
v9 = v8;
v10 = 4LL;
LABEL_7:
result = mmap(0LL, v10, 3, 1, v9, 0LL);
*(_QWORD *)v5 = result;
if ( !result )
{
perror("mmap");
exit(1);
}
return result;
}

original_code:
static void lock_entity_init(char *lock_name, void **lock_t, int nnum)
{
 if(lock_name == ((void *)0)){
  printf("should init lock name\n");
  exit(1);
 }
 if(nnum < 0 || nnum >= 3){
  printf("invalid parameter\n");
  exit(1);
 }
 *lock_t = (lock_entity)malloc(sizeof(struct mem_lock_t));
 lock_entity p = *lock_t;
 p->lock_name = (char *)calloc(128, sizeof(char));
 sprintf(p->lock_name, "%s.%d", lock_name+4, nnum);
 int lock_fd;
 lock_fd = shm_open(p->lock_name, 0100|02|0200, 0644);
 if(lock_fd == -1){
  perror("shm_open");
  exit(1);
 }
 switch(nnum){
 case 0:
  if(ftruncate(lock_fd, sizeof(pthread_mutex_t))){
   perror("ftruncate");
   exit(1);
  }
  p->lock_addr = mmap(((void *)0), sizeof(pthread_mutex_t), 0x1|0x2,
        0x01, lock_fd, 0);
  if(p->lock_addr == ((void *)0)){
   perror("mmap");
   exit(1);
  }
  break;
 case 1:
 case 2:
  if(ftruncate(lock_fd, sizeof(pthread_spinlock_t))){
   perror("ftruncate");
   exit(1);
  }
  p->lock_addr = mmap(((void *)0), sizeof(pthread_spinlock_t), 0x1|0x2,
        0x01, lock_fd, 0);
  if(p->lock_addr == ((void *)0)){
   perror("mmap");
   exit(1);
  }
  break;
 default:
  break;
 }
}

canonical_original_code:
static void lock_entity_init(char *lock_name, void **lock_t, int nnum)
{
 if(lock_name == ((void *)0)){
  printf("should init lock name\n");
  exit(1);
 }
 if(nnum < 0 || nnum >= 3){
  printf("invalid parameter\n");
  exit(1);
 }
 *lock_t = (struct mem_lock_t *)malloc(sizeof(struct mem_lock_t));
 struct mem_lock_t *p = *lock_t;
 p->lock_name = (char *)calloc(128, sizeof(char));
 sprintf(p->lock_name, "%s.%d", lock_name+4, nnum);
 int lock_fd;
 lock_fd = shm_open(p->lock_name, 0100|02|0200, 0644);
 if(lock_fd == -1){
  perror("shm_open");
  exit(1);
 }
 switch(nnum){
 case 0:
  if(ftruncate(lock_fd, sizeof(pthread_mutex_t))){
   perror("ftruncate");
   exit(1);
  }
  p->lock_addr = mmap(((void *)0), sizeof(pthread_mutex_t), 0x1|0x2,
        0x01, lock_fd, 0);
  if(p->lock_addr == ((void *)0)){
   perror("mmap");
   exit(1);
  }
  break;
 case 1:
 case 2:
  if(ftruncate(lock_fd, sizeof(pthread_spinlock_t))){
   perror("ftruncate");
   exit(1);
  }
  p->lock_addr = mmap(((void *)0), sizeof(pthread_spinlock_t), 0x1|0x2,
        0x01, lock_fd, 0);
  if(p->lock_addr == ((void *)0)){
   perror("mmap");
   exit(1);
  }
  break;
 default:
  break;
 }
}

variable_types: nnum int