doPthreadPool

repo: labanyd/threadpool

decompiled_code:
void __fastcall __noreturn doPthreadPool(void *a1)
{
pthread_mutex_t *v1; // rbp
int v2; // eax
__int64 v3; // rdx
void (__fastcall *v4)(__int64); // r13
__int64 v5; // r14
__int64 v6; // [rsp+0h] [rbp-C8h]
int v7; // [rsp+Ch] [rbp-BCh]
__pthread_unwind_buf_t buf; // [rsp+10h] [rbp-B8h] BYREF
__int64 v9; // [rsp+80h] [rbp-48h] BYREF
int v10; // [rsp+88h] [rbp-40h]

v6 = *(_QWORD *)a1;
v7 = *((_DWORD *)a1 + 2);
v9 = *(_QWORD *)a1;
v10 = v7;
pthread_setcanceltype(1, 0LL);
if ( !_sigsetjmp((struct __jmp_buf_tag *)&buf, 0) )
{
_pthread_register_cancel(&buf);
v1 = (pthread_mutex_t *)(v6 + 8);
while ( 1 )
{
pthread_mutex_lock(v1);
while ( 1 )
{
v2 = *(_DWORD *)(v6 + 120);
if ( v2 )
break;
*(_DWORD *)(*(_QWORD *)(v6 + 96) + 4LL * v7) = 1;
pthread_cond_wait((pthread_cond_t *)(v6 + 48), v1);
}
v3 = *(_QWORD *)(v6 + 104);
v4 = *(void (__fastcall **)(__int64))v3;
v5 = *(_QWORD *)(v3 + 8);
*(_QWORD *)(v6 + 104) = *(_QWORD *)(v3 + 16);
*(_DWORD *)(v6 + 120) = v2 - 1;
pthread_mutex_unlock(v1);
*(_DWORD *)(*(_QWORD *)(v6 + 96) + 4LL * v7) = 2;
pthread_testcancel();
v4(v5);
pthread_testcancel();
}
}
CleanUp(&v9);
_pthread_unwind_next(&buf);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
pthread_mutex_t *v1; // rbp
int v2; // eax
__int64 v3; // rdx
void (__fastcall *v4)(__int64); // r13
__int64 v5; // r14
__int64 v6; // [rsp+0h] [rbp-C8h]
int v7; // [rsp+Ch] [rbp-BCh]
__pthread_unwind_buf_t buf; // [rsp+10h] [rbp-B8h] BYREF
__int64 v9; // [rsp+80h] [rbp-48h] BYREF
int v10; // [rsp+88h] [rbp-40h]

v6 = *(_QWORD *)a1;
v7 = *((_DWORD *)a1 + 2);
v9 = *(_QWORD *)a1;
v10 = v7;
pthread_setcanceltype(1, 0LL);
if ( !_sigsetjmp((struct __jmp_buf_tag *)&buf, 0) )
{
_pthread_register_cancel(&buf);
v1 = (pthread_mutex_t *)(v6 + 8);
while ( 1 )
{
pthread_mutex_lock(v1);
while ( 1 )
{
v2 = *(_DWORD *)(v6 + 120);
if ( v2 )
break;
*(_DWORD *)(*(_QWORD *)(v6 + 96) + 4LL * v7) = 1;
pthread_cond_wait((pthread_cond_t *)(v6 + 48), v1);
}
v3 = *(_QWORD *)(v6 + 104);
v4 = *(void (__fastcall **)(__int64))v3;
v5 = *(_QWORD *)(v3 + 8);
*(_QWORD *)(v6 + 104) = *(_QWORD *)(v3 + 16);
*(_DWORD *)(v6 + 120) = v2 - 1;
pthread_mutex_unlock(v1);
*(_DWORD *)(*(_QWORD *)(v6 + 96) + 4LL * v7) = 2;
pthread_testcancel();
v4(v5);
pthread_testcancel();
}
}
func0(&v9);
_pthread_unwind_next(&buf);
}

original_code:
void *
doPthreadPool(void *arg)
{
 Func fun;
 void *argument;
 PthreadPool *pool = ((PthreadStatus *)arg)->m_pool;
 int seq = ((PthreadStatus *)arg)->m_iThread;
 PthreadStatus pStatus = {pool, seq};
 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
 do { __pthread_unwind_buf_t __cancel_buf; void (*__cancel_routine) (void *) = (CleanUp); void *__cancel_arg = ((void*)&pStatus); int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); if (__builtin_expect ((__not_first_call), 0)) { __cancel_routine (__cancel_arg); __pthread_unwind_next (&__cancel_buf); } __pthread_register_cancel (&__cancel_buf); do {;
 while (1)
 {
  pthread_mutex_lock(&pool->m_mutex);
  while (pool->m_iTaskNum == 0)
  {
   *(pool->m_pStatus + seq) = THREAD_WAITTING;
   pthread_cond_wait(&pool->m_cond, &pool->m_mutex);
  }
  fun = pool->m_pTaskHead->m_pFunc;
  argument = pool->m_pTaskHead->m_pArg;
  pool->m_pTaskHead = pool->m_pTaskHead->next;
  pool->m_iTaskNum--;
  pthread_mutex_unlock(&pool->m_mutex);
  *(pool->m_pStatus + seq) = THREAD_RUNNING;
  pthread_testcancel();
  (*fun)(argument);
  pthread_testcancel();
 }
 do { } while (0); } while (0); __pthread_unregister_cancel (&__cancel_buf); if (0) __cancel_routine (__cancel_arg); } while (0);
}

canonical_original_code:
void *doPthreadPool(void *arg)
{
 void (*fun)(void *arg);
 void *argument;
 struct pthreadpool_t *pool = ((PthreadStatus *)arg)->m_pool;
 int seq = ((PthreadStatus *)arg)->m_iThread;
 struct PthreadStatus pStatus = {pool, seq};
 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
 do { struct __pthread_unwind_buf_t __cancel_buf; void (*__cancel_routine)(void *) = (CleanUp); void *__cancel_arg = ((void*)&pStatus); int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) __cancel_buf.__cancel_jmp_buf, 0); if (__builtin_expect ((__not_first_call), 0)) { __cancel_routine (__cancel_arg); __pthread_unwind_next (&__cancel_buf); } __pthread_register_cancel (&__cancel_buf); do {;
 while (1)
 {
  pthread_mutex_lock(&pool->m_mutex);
  while (pool->m_iTaskNum == 0)
  {
   *(pool->m_pStatus + seq) = THREAD_WAITTING;
   pthread_cond_wait(&pool->m_cond, &pool->m_mutex);
  }
  fun = pool->m_pTaskHead->m_pFunc;
  argument = pool->m_pTaskHead->m_pArg;
  pool->m_pTaskHead = pool->m_pTaskHead->next;
  pool->m_iTaskNum--;
  pthread_mutex_unlock(&pool->m_mutex);
  *(pool->m_pStatus + seq) = THREAD_RUNNING;
  pthread_testcancel();
  (*fun)(argument);
  pthread_testcancel();
 }
 do { } while (0); } while (0); __pthread_unregister_cancel (&__cancel_buf); if (0) __cancel_routine (__cancel_arg); } while (0);
}

variable_types:
arg
void *

variable_types:
fun
void (*)(void *arg)

variable_types:
argument
void *

variable_types: seq int

variable_types: struct PthreadStatus { struct pthreadpool_t { unsigned long *m_pth; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } m_cond; int *m_pStatus; struct task_element_t { void (*m_pFunc)(void *arg); void *m_pArg; struct task_element_t *next; } *m_pTaskHead; struct task_element_t *m_pTaskTail; int m_iTaskNum; int m_iPthreadNum; } *m_pool; int m_iThread; }

variable_types: struct __pthread_unwind_buf_t { struct { long __cancel_jmp_buf[8]; int __mask_was_saved; } __cancel_jmp_buf[1]; void *__pad[4]; }

return_type: void *

user_defined_types: struct PthreadStatus { struct pthreadpool_t *m_pool; int m_iThread; }

user_defined_types: struct pthreadpool_t { unsigned long *m_pth; union pthread_mutex_t m_mutex; union pthread_cond_t m_cond; int *m_pStatus; struct task_element_t *m_pTaskHead; struct task_element_t *m_pTaskTail; int m_iTaskNum; int m_iPthreadNum; }

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

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long 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; }


PthreadPoolInit

decompiled_code:
__int64 __fastcall PthreadPoolInit(int a1)
{
char *v1; // rax
__int64 v2; // rbp
void *v3; // rbx
void *v4; // r12
__int64 v5; // rbx
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+8h] [rbp-30h]

v1 = (char *)malloc(0x80uLL);
v2 = (__int64)v1;
if ( v1 )
{
*((_QWORD *)v1 + 13) = 0LL;
*((_QWORD *)v1 + 14) = 0LL;
*((_DWORD *)v1 + 30) = 0;
pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL);
pthread_cond_init((pthread_cond_t *)(v2 + 48), 0LL);
v3 = malloc(8LL * a1);
*(_QWORD *)v2 = v3;
if ( v3 )
{
*(_DWORD *)(v2 + 124) = a1;
v4 = malloc(4LL * a1);
*(_QWORD *)(v2 + 96) = v4;
if ( v4 )
{
v5 = 0LL;
if ( a1 > 0 )
{
while ( 1 )
{
v7 = v2;
v8 = v5;
if ( pthread_create((pthread_t *)(*(_QWORD *)v2 + 8 * v5), 0LL, (void *(*)(void *))doPthreadPool, &v7) )
break;
*(_DWORD *)(*(_QWORD *)(v2 + 96) + 4 * v5) = 0;
usleep(0x3E8u);
if ( ++v5 == a1 )
return v2;
}
free(*(void **)(v2 + 96));
free(*(void **)v2);
free((void *)v2);
v2 = 0LL;
}
}
else
{
free(v3);
free((void *)v2);
v2 = 0LL;
}
}
else
{
free((void *)v2);
v2 = 0LL;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
char *v1; // rax
__int64 v2; // rbp
void *v3; // rbx
void *v4; // r12
__int64 v5; // rbx
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+8h] [rbp-30h]

v1 = (char *)malloc(0x80uLL);
v2 = (__int64)v1;
if ( v1 )
{
*((_QWORD *)v1 + 13) = 0LL;
*((_QWORD *)v1 + 14) = 0LL;
*((_DWORD *)v1 + 30) = 0;
pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL);
pthread_cond_init((pthread_cond_t *)(v2 + 48), 0LL);
v3 = malloc(8LL * a1);
*(_QWORD *)v2 = v3;
if ( v3 )
{
*(_DWORD *)(v2 + 124) = a1;
v4 = malloc(4LL * a1);
*(_QWORD *)(v2 + 96) = v4;
if ( v4 )
{
v5 = 0LL;
if ( a1 > 0 )
{
while ( 1 )
{
v7 = v2;
v8 = v5;
if ( pthread_create((pthread_t *)(*(_QWORD *)v2 + 8 * v5), 0LL, (void *(*)(void *))doPthreadPool, &v7) )
break;
*(_DWORD *)(*(_QWORD *)(v2 + 96) + 4 * v5) = 0;
usleep(0x3E8u);
if ( ++v5 == a1 )
return v2;
}
free(*(void **)(v2 + 96));
free(*(void **)v2);
free((void *)v2);
v2 = 0LL;
}
}
else
{
free(v3);
free((void *)v2);
v2 = 0LL;
}
}
else
{
free((void *)v2);
v2 = 0LL;
}
}
return v2;
}

original_code:
PthreadPool *
PthreadPoolInit(int pthreadNums)
{
 PthreadPool *pool = (PthreadPool *) malloc (sizeof(PthreadPool));
 if (pool == ((void *)0))
 {
  return ((void *)0);
 }
 pool->m_pTaskHead = ((void *)0);
 pool->m_pTaskTail = ((void *)0);
 pool->m_iTaskNum = 0;
 pthread_mutex_init(&pool->m_mutex, ((void *)0));
 pthread_cond_init(&pool->m_cond, ((void *)0));
 pool->m_pth = (pthread_t *) malloc (sizeof(pthread_t) * pthreadNums);
 if (((void *)0) == pool->m_pth)
 {
  free(pool);
  return ((void *)0);
 }
 pool->m_iPthreadNum = pthreadNums;
 pool->m_pStatus = (int *) malloc (sizeof(int) * pthreadNums);
 if (((void *)0) == pool->m_pStatus)
 {
  free(pool->m_pth);
  free(pool);
  return ((void *)0);
 }
 int i , ret;
 PthreadStatus pst;
 for (i = 0; i < pthreadNums; i++)
 {
  pst.m_pool = pool;
  pst.m_iThread = i;
  ret = pthread_create(pool->m_pth + i, ((void *)0), doPthreadPool,
   (void *)&pst);
  if (0 != ret)
  {
   free(pool->m_pStatus);
   free(pool->m_pth);
   free(pool);
   return ((void *)0);
  }
  *(pool->m_pStatus + i) = THREAD_INIT;
  usleep(1000);
 }
 return pool;
}

canonical_original_code:
struct pthreadpool_t *PthreadPoolInit(int pthreadNums)
{
 struct pthreadpool_t *pool = (PthreadPool *) malloc (sizeof(PthreadPool));
 if (pool == ((void *)0))
 {
  return ((void *)0);
 }
 pool->m_pTaskHead = ((void *)0);
 pool->m_pTaskTail = ((void *)0);
 pool->m_iTaskNum = 0;
 pthread_mutex_init(&pool->m_mutex, ((void *)0));
 pthread_cond_init(&pool->m_cond, ((void *)0));
 pool->m_pth = (unsigned long *) malloc (sizeof(pthread_t) * pthreadNums);
 if (((void *)0) == pool->m_pth)
 {
  free(pool);
  return ((void *)0);
 }
 pool->m_iPthreadNum = pthreadNums;
 pool->m_pStatus = (int *) malloc (sizeof(int) * pthreadNums);
 if (((void *)0) == pool->m_pStatus)
 {
  free(pool->m_pth);
  free(pool);
  return ((void *)0);
 }
 int i; int ret;
 struct PthreadStatus pst;
 for (i = 0; i < pthreadNums; i++)
 {
  pst.m_pool = pool;
  pst.m_iThread = i;
  ret = pthread_create(pool->m_pth + i, ((void *)0), doPthreadPool,
   (void *)&pst);
  if (0 != ret)
  {
   free(pool->m_pStatus);
   free(pool->m_pth);
   free(pool);
   return ((void *)0);
  }
  *(pool->m_pStatus + i) = THREAD_INIT;
  usleep(1000);
 }
 return pool;
}

variable_types: pthreadNums int

variable_types: struct pthreadpool_t { unsigned long *m_pth; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } m_cond; int *m_pStatus; struct task_element_t { void (*m_pFunc)(void *arg); void *m_pArg; struct task_element_t *next; } *m_pTaskHead; struct task_element_t *m_pTaskTail; int m_iTaskNum; int m_iPthreadNum; } *

variable_types: i int

variable_types: ret int


FreeTaskQueue

original_code:
void
FreeTaskQueue(PthreadPool *pool)
{
 TaskElement *ele = pool->m_pTaskHead;
 while (ele)
 {
  pool->m_pTaskHead = pool->m_pTaskHead->next;
  free(ele);
  ele = pool->m_pTaskHead;
 }
}

canonical_original_code:
void FreeTaskQueue(struct pthreadpool_t *pool)
{
 struct task_element_t *ele = pool->m_pTaskHead;
 while (ele)
 {
  pool->m_pTaskHead = pool->m_pTaskHead->next;
  free(ele);
  ele = pool->m_pTaskHead;
 }
}

variable_types: struct task_element_t { void (*m_pFunc)(void *arg); void *m_pArg; struct task_element_t *next; } *

return_type: void

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

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


PthreadPoolDestroy

decompiled_code:
__int64 __fastcall PthreadPoolDestroy(void *ptr)
{
int v1; // er12
__int64 v2; // r12
__int64 v3; // rbx

if ( !ptr )
return 4294967293LL;
v1 = *((_DWORD *)ptr + 31);
if ( v1 > 0 )
{
v2 = 8LL * (unsigned int)v1;
v3 = 0LL;
do
{
pthread_cancel(*(_QWORD *)(*(_QWORD *)ptr + v3));
pthread_join(*(_QWORD *)(*(_QWORD *)ptr + v3), 0LL);
v3 += 8LL;
}
while ( v3 != v2 );
}
free(*(void **)ptr);
free(*((void **)ptr + 12));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 8));
pthread_cond_destroy((pthread_cond_t *)ptr + 1);
FreeTaskQueue((__int64)ptr);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *ptr)
{
int v1; // er12
__int64 v2; // r12
__int64 v3; // rbx

if ( !ptr )
return 4294967293LL;
v1 = *((_DWORD *)ptr + 31);
if ( v1 > 0 )
{
v2 = 8LL * (unsigned int)v1;
v3 = 0LL;
do
{
pthread_cancel(*(_QWORD *)(*(_QWORD *)ptr + v3));
pthread_join(*(_QWORD *)(*(_QWORD *)ptr + v3), 0LL);
v3 += 8LL;
}
while ( v3 != v2 );
}
free(*(void **)ptr);
free(*((void **)ptr + 12));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 8));
pthread_cond_destroy((pthread_cond_t *)ptr + 1);
func3((__int64)ptr);
free(ptr);
return 0LL;
}

original_code:
int
PthreadPoolDestroy(PthreadPool *pool)
{
 if (((void *)0) == (pool)) { return (PTHREAD_POOL_NULL); };
 int i = 0;
 int seq = pool->m_iPthreadNum;
 for (; i < seq; i++)
 {
  pthread_cancel(*(pool->m_pth + i));
  pthread_join(*(pool->m_pth + i), 0);
 }
 free(pool->m_pth);
 free(pool->m_pStatus);
 pthread_mutex_destroy(&pool->m_mutex);
 pthread_cond_destroy(&pool->m_cond);
 FreeTaskQueue(pool);
 free(pool);
 return SUCCESS;
}

canonical_original_code:
int PthreadPoolDestroy(struct pthreadpool_t *pool)
{
 if (((void *)0) == (pool)) { return (PTHREAD_POOL_NULL); };
 int i = 0;
 int seq = pool->m_iPthreadNum;
 for (; i < seq; i++)
 {
  pthread_cancel(*(pool->m_pth + i));
  pthread_join(*(pool->m_pth + i), 0);
 }
 free(pool->m_pth);
 free(pool->m_pStatus);
 pthread_mutex_destroy(&pool->m_mutex);
 pthread_cond_destroy(&pool->m_cond);
 FreeTaskQueue(pool);
 free(pool);
 return SUCCESS;
}

return_type: int

user_defined_types: struct task_element_t { void (*m_pFunc)(void *arg); void *m_pArg; struct task_element_t *next; }


PthreadPoolAddTask

decompiled_code:
__int64 __fastcall PthreadPoolAddTask(__int64 a1, __int64 a2, __int64 a3)
{
pthread_mutex_t *v4; // r12
_QWORD *v5; // rax
__int64 result; // rax

if ( !a1 )
return 4294967293LL;
if ( !a2 )
return 4294967292LL;
v4 = (pthread_mutex_t *)(a1 + 8);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
v5 = malloc(0x18uLL);
if ( v5 )
{
*v5 = a2;
v5[1] = a3;
v5[2] = 0LL;
if ( *(_QWORD *)(a1 + 104) )
*(_QWORD *)(*(_QWORD *)(a1 + 112) + 16LL) = v5;
else
*(_QWORD *)(a1 + 104) = v5;
*(_QWORD *)(a1 + 112) = v5;
++*(_DWORD *)(a1 + 120);
pthread_cond_broadcast((pthread_cond_t *)(a1 + 48));
pthread_mutex_unlock(v4);
result = 0LL;
}
else
{
pthread_mutex_unlock(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3)
{
pthread_mutex_t *v4; // r12
_QWORD *v5; // rax
__int64 result; // rax

if ( !a1 )
return 4294967293LL;
if ( !a2 )
return 4294967292LL;
v4 = (pthread_mutex_t *)(a1 + 8);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
v5 = malloc(0x18uLL);
if ( v5 )
{
*v5 = a2;
v5[1] = a3;
v5[2] = 0LL;
if ( *(_QWORD *)(a1 + 104) )
*(_QWORD *)(*(_QWORD *)(a1 + 112) + 16LL) = v5;
else
*(_QWORD *)(a1 + 104) = v5;
*(_QWORD *)(a1 + 112) = v5;
++*(_DWORD *)(a1 + 120);
pthread_cond_broadcast((pthread_cond_t *)(a1 + 48));
pthread_mutex_unlock(v4);
result = 0LL;
}
else
{
pthread_mutex_unlock(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
PthreadPoolAddTask(PthreadPool *pool, Func fun, void *arg)
{
 if (((void *)0) == (pool)) { return (PTHREAD_POOL_NULL); };
 if (((void *)0) == (fun)) { return (TASK_FUNCTION_NULL); };
 pthread_mutex_lock(&pool->m_mutex);
 TaskElement * ele = (TaskElement *) malloc (sizeof(TaskElement));
 if (((void *)0) == ele)
 {
  pthread_mutex_unlock(&pool->m_mutex);
  return MALLOC_FAIL;
 }
 ele->m_pFunc = fun;
 ele->m_pArg = arg;
 ele->next = ((void *)0);
 if (pool->m_pTaskHead == ((void *)0))
 {
  pool->m_pTaskHead = ele;
  pool->m_pTaskTail = ele;
 }
 else
 {
  pool->m_pTaskTail->next = ele;
  pool->m_pTaskTail = ele;
 }
 pool->m_iTaskNum++;
 pthread_cond_broadcast(&pool->m_cond);
 pthread_mutex_unlock(&pool->m_mutex);
 return SUCCESS;
}

canonical_original_code:
int PthreadPoolAddTask(struct pthreadpool_t *pool, void (*fun)(void *arg), void *arg)
{
 if (((void *)0) == (pool)) { return (PTHREAD_POOL_NULL); };
 if (((void *)0) == (fun)) { return (TASK_FUNCTION_NULL); };
 pthread_mutex_lock(&pool->m_mutex);
 struct task_element_t *ele = (TaskElement *) malloc (sizeof(TaskElement));
 if (((void *)0) == ele)
 {
  pthread_mutex_unlock(&pool->m_mutex);
  return MALLOC_FAIL;
 }
 ele->m_pFunc = fun;
 ele->m_pArg = arg;
 ele->next = ((void *)0);
 if (pool->m_pTaskHead == ((void *)0))
 {
  pool->m_pTaskHead = ele;
  pool->m_pTaskTail = ele;
 }
 else
 {
  pool->m_pTaskTail->next = ele;
  pool->m_pTaskTail = ele;
 }
 pool->m_iTaskNum++;
 pthread_cond_broadcast(&pool->m_cond);
 pthread_mutex_unlock(&pool->m_mutex);
 return SUCCESS;
}


repo: lagodiuk/raytracing-render

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

if ( *(_QWORD *)(a1 + 16) )
release_kd_tree();
result = build_kd_tree(*(_QWORD *)a1, (unsigned int)(*(_DWORD *)(a1 + 12) + 1));
*(_QWORD *)(a1 + 16) = result;
return result;
}

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

if ( *(_QWORD *)(a1 + 16) )
release_kd_tree();
result = build_kd_tree(*(_QWORD *)a1, (unsigned int)(*(_DWORD *)(a1 + 12) + 1));
*(_QWORD *)(a1 + 16) = result;
return result;
}

original_code:
static inline void
rebuild_kd_tree(Scene * scene) {
    if(scene->kd_tree)
        release_kd_tree(scene->kd_tree);
    scene->kd_tree = build_kd_tree(scene->objects, scene->last_object_index + 1);
}

canonical_original_code:
static inline void rebuild_kd_tree(struct Scene *scene) {
    if(scene->kd_tree)
        release_kd_tree(scene->kd_tree);
    scene->kd_tree = build_kd_tree(scene->objects, scene->last_object_index + 1);
}

variable_types: struct Scene { struct Object3d { void *data; int (*intersect)(void *data, struct Point3d { double x; double y; double z; } vector_start, struct Vector3d { double x; double y; double z; } vector, struct Point3d *intersection_point); struct Color { unsigned char r; unsigned char g; unsigned char b; } (*get_color)(void *data, struct Point3d intersection_point); struct Vector3d (*get_normal_vector)(void *data, struct Point3d intersection_point); struct Material { double Ka; double Kd; double Ks; double Kr; double Kt; double p; } (*get_material)(void *data, struct Point3d intersection_point); struct Point3d (*get_min_boundary_point)(void *data); struct Point3d (*get_max_boundary_point)(void *data); void (*release_data)(void *data); } **objects; int objects_count; int last_object_index; struct KDTree { struct KDNode { enum Plane { XY=0, XZ=1, YZ=2, NONE=3 } plane; union Coord { double x; double y; double z; } coord; struct Object3d **objects; int objects_count; struct KDNode *l; struct KDNode *r; } *root; struct Voxel { double x_min; double y_min; double z_min; double x_max; double y_max; double z_max; } bounding_box; } *kd_tree; struct LightSource3d { struct Point3d location_world; struct Point3d location; struct Color color; } **light_sources; int light_sources_count; int last_light_source_index; struct Color background_color; double (*fog_density)(double distance, void *fog_parameters); void *fog_parameters; } *

user_defined_types: struct Scene { struct Object3d **objects; int objects_count; int last_object_index; struct KDTree *kd_tree; struct LightSource3d **light_sources; int light_sources_count; int last_light_source_index; struct Color background_color; double (*fog_density)(double distance, void *fog_parameters); void *fog_parameters; }

user_defined_types: struct KDTree { struct KDNode *root; struct Voxel bounding_box; }

user_defined_types: struct Object3d { void *data; int (*intersect)(void *data, struct Point3d vector_start, struct Vector3d vector, struct Point3d *intersection_point); struct Color (*get_color)(void *data, struct Point3d intersection_point); struct Vector3d (*get_normal_vector)(void *data, struct Point3d intersection_point); struct Material (*get_material)(void *data, struct Point3d intersection_point); struct Point3d (*get_min_boundary_point)(void *data); struct Point3d (*get_max_boundary_point)(void *data); void (*release_data)(void *data); }

user_defined_types: struct KDNode { enum Plane plane; union Coord coord; struct Object3d **objects; int objects_count; struct KDNode *l; struct KDNode *r; }


decompiled_code:
__int64 __fastcall add_object(__int64 *a1, __int64 a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *a1;
LODWORD(result) = *((_DWORD *)a1 + 3) + 1;
*((_DWORD *)a1 + 3) = result;
result = (int)result;
*(_QWORD *)(v2 + 8LL * (int)result) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 *a1, __int64 a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *a1;
LODWORD(result) = *((_DWORD *)a1 + 3) + 1;
*((_DWORD *)a1 + 3) = result;
result = (int)result;
*(_QWORD *)(v2 + 8LL * (int)result) = a2;
return result;
}

original_code:
void
add_object(Scene * const scene,
           Object3d * const object) {
    scene->objects[++scene->last_object_index] = object;
}

canonical_original_code:
void add_object(struct Scene *scene, struct Object3d *object) {
    scene->objects[++scene->last_object_index] = object;
}

variable_types:
object
struct Object3d *

user_defined_types: struct Voxel { double x_min; double y_min; double z_min; double x_max; double y_max; double z_max; }

user_defined_types: struct LightSource3d { struct Point3d location_world; struct Point3d location; struct Color color; }

user_defined_types: union Coord { double x; double y; double z; }

user_defined_types: struct Point3d { double x; double y; double z; }

user_defined_types: struct Vector3d { double x; double y; double z; }

user_defined_types: struct Color { unsigned char r; unsigned char g; unsigned char b; }

user_defined_types: struct Material { double Ka; double Kd; double Ks; double Kr; double Kt; double p; }


decompiled_code:
__int64 __fastcall prepare_scene(__int64 a1)
{
return rebuild_kd_tree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return func5(a1);
}

original_code:
void
prepare_scene(Scene * const scene) {
    rebuild_kd_tree(scene);
}

canonical_original_code:
void prepare_scene(struct Scene *scene) {
    rebuild_kd_tree(scene);
}


decompiled_code:
__int64 __fastcall add_light_source(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 24);
LODWORD(result) = *(_DWORD *)(a1 + 36) + 1;
*(_DWORD *)(a1 + 36) = result;
result = (int)result;
*(_QWORD *)(v2 + 8LL * (int)result) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 24);
LODWORD(result) = *(_DWORD *)(a1 + 36) + 1;
*(_DWORD *)(a1 + 36) = result;
result = (int)result;
*(_QWORD *)(v2 + 8LL * (int)result) = a2;
return result;
}

original_code:
void
add_light_source(Scene * const scene,
                 LightSource3d * const light_source) {
    scene->light_sources[++scene->last_light_source_index] = light_source;
}

canonical_original_code:
void add_light_source(struct Scene *scene, struct LightSource3d *light_source) {
    scene->light_sources[++scene->last_light_source_index] = light_source;
}


decompiled_code:
void __fastcall release_object3d(void *ptr)
{
(*((void (__fastcall **)(_QWORD))ptr + 7))(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
(*((void (__fastcall **)(_QWORD))ptr + 7))(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void
release_object3d(Object3d * obj) {
    obj->release_data(obj->data);
    free(obj);
}

canonical_original_code:
void release_object3d(struct Object3d *obj) {
    obj->release_data(obj->data);
    free(obj);
}

variable_types: struct Object3d { void *data; int (*intersect)(void *data, struct Point3d { double x; double y; double z; } vector_start, struct Vector3d { double x; double y; double z; } vector, struct Point3d *intersection_point); struct Color { unsigned char r; unsigned char g; unsigned char b; } (*get_color)(void *data, struct Point3d intersection_point); struct Vector3d (*get_normal_vector)(void *data, struct Point3d intersection_point); struct Material { double Ka; double Kd; double Ks; double Kr; double Kt; double p; } (*get_material)(void *data, struct Point3d intersection_point); struct Point3d (*get_min_boundary_point)(void *data); struct Point3d (*get_max_boundary_point)(void *data); void (*release_data)(void *data); } *


decompiled_code:
void __fastcall release_scene(void *ptr)
{
__int64 v2; // rbp
void *v3; // rdi
__int64 v4; // rbp
void *v5; // rdi
void *v6; // rdi

if ( *((int *)ptr + 2) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2);
if ( v3 )
release_object3d(v3);
++v2;
}
while ( *((_DWORD *)ptr + 2) > (int)v2 );
}
if ( *((int *)ptr + 8) > 0 )
{
v4 = 0LL;
do
{
v5 = *(void **)(*((_QWORD *)ptr + 3) + 8 * v4);
if ( v5 )
free(v5);
++v4;
}
while ( *((_DWORD *)ptr + 8) > (int)v4 );
}
free(*(void **)ptr);
free(*((void **)ptr + 3));
v6 = (void *)*((_QWORD *)ptr + 7);
if ( v6 )
free(v6);
if ( *((_QWORD *)ptr + 2) )
release_kd_tree();
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
__int64 v2; // rbp
void *v3; // rdi
__int64 v4; // rbp
void *v5; // rdi
void *v6; // rdi

if ( *((int *)ptr + 2) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2);
if ( v3 )
func9(v3);
++v2;
}
while ( *((_DWORD *)ptr + 2) > (int)v2 );
}
if ( *((int *)ptr + 8) > 0 )
{
v4 = 0LL;
do
{
v5 = *(void **)(*((_QWORD *)ptr + 3) + 8 * v4);
if ( v5 )
free(v5);
++v4;
}
while ( *((_DWORD *)ptr + 8) > (int)v4 );
}
free(*(void **)ptr);
free(*((void **)ptr + 3));
v6 = (void *)*((_QWORD *)ptr + 7);
if ( v6 )
free(v6);
if ( *((_QWORD *)ptr + 2) )
release_kd_tree();
free(ptr);
}

original_code:
void
release_scene(Scene * scene) {
    int i;
    for(i = 0; i < scene->objects_count; i++) {
        if(scene->objects[i]) {
            release_object3d(scene->objects[i]);
        }
    }
    for(i = 0; i < scene->light_sources_count; i++) {
        if(scene->light_sources[i]) {
            free(scene->light_sources[i]);
        }
    }
    free(scene->objects);
    free(scene->light_sources);
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    if(scene->kd_tree)
        release_kd_tree(scene->kd_tree);
    free(scene);
}

canonical_original_code:
void release_scene(struct Scene *scene) {
    int i;
    for(i = 0; i < scene->objects_count; i++) {
        if(scene->objects[i]) {
            release_object3d(scene->objects[i]);
        }
    }
    for(i = 0; i < scene->light_sources_count; i++) {
        if(scene->light_sources[i]) {
            free(scene->light_sources[i]);
        }
    }
    free(scene->objects);
    free(scene->light_sources);
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    if(scene->kd_tree)
        release_kd_tree(scene->kd_tree);
    free(scene);
}


decompiled_code:
double *__fastcall new_camera(double a1, double a2, double a3, double a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9, __int64 a10, __int128 a11, __int64 a12)
{
double v12; // rbx
double v13; // rbp
double v14; // r12
double v15; // r13
double v16; // r14
double v17; // r15
double *result; // rax
double cosx; // [rsp+20h] [rbp-48h] BYREF
double sinx[8]; // [rsp+28h] [rbp-40h] BYREF

sincos(a3, sinx, &cosx);
v12 = cosx;
v13 = sinx[0];
sincos(a2, sinx, &cosx);
v14 = cosx;
v15 = sinx[0];
sincos(a1, sinx, &cosx);
v16 = cosx;
v17 = sinx[0];
result = (double *)malloc(0x68uLL);
*(__m128i *)result = _mm_loadu_si128((const __m128i *)&a11);
*((_QWORD *)result + 2) = a12;
result[3] = a1;
result[4] = v17;
result[5] = v16;
result[6] = a2;
result[7] = v15;
result[8] = v14;
result[9] = a3;
result[10] = v13;
result[11] = v12;
result[12] = a4;
return result;
}

canonical_decompiled_code:
double *__fastcall func12(double a1, double a2, double a3, double a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9, __int64 a10, __int128 a11, __int64 a12)
{
double v12; // rbx
double v13; // rbp
double v14; // r12
double v15; // r13
double v16; // r14
double v17; // r15
double *result; // rax
double cosx; // [rsp+20h] [rbp-48h] BYREF
double sinx[8]; // [rsp+28h] [rbp-40h] BYREF

sincos(a3, sinx, &cosx);
v12 = cosx;
v13 = sinx[0];
sincos(a2, sinx, &cosx);
v14 = cosx;
v15 = sinx[0];
sincos(a1, sinx, &cosx);
v16 = cosx;
v17 = sinx[0];
result = (double *)malloc(0x68uLL);
*(__m128i *)result = _mm_loadu_si128((const __m128i *)&a11);
*((_QWORD *)result + 2) = a12;
result[3] = a1;
result[4] = v17;
result[5] = v16;
result[6] = a2;
result[7] = v15;
result[8] = v14;
result[9] = a3;
result[10] = v13;
result[11] = v12;
result[12] = a4;
return result;
}

original_code:
Camera *
new_camera(const Point3d camera_position,
           const Float al_x,
           const Float al_y,
           const Float al_z,
           const Float proj_plane_dist) {
    Camera * cam = malloc(sizeof(Camera));
    cam->camera_position = camera_position;
    cam->al_x = al_x;
    cam->sin_al_x = sin(al_x);
    cam->cos_al_x = cos(al_x);
    cam->al_y = al_y;
    cam->sin_al_y = sin(al_y);
    cam->cos_al_y = cos(al_y);
    cam->al_z = al_z;
    cam->sin_al_z = sin(al_z);
    cam->cos_al_z = cos(al_z);
    cam->proj_plane_dist = proj_plane_dist;
    return cam;
}

canonical_original_code:
struct Camera *new_camera(struct Point3d camera_position, double al_x, double al_y, double al_z, double proj_plane_dist) {
    struct Camera *cam = malloc(sizeof(Camera));
    cam->camera_position = camera_position;
    cam->al_x = al_x;
    cam->sin_al_x = sin(al_x);
    cam->cos_al_x = cos(al_x);
    cam->al_y = al_y;
    cam->sin_al_y = sin(al_y);
    cam->cos_al_y = cos(al_y);
    cam->al_z = al_z;
    cam->sin_al_z = sin(al_z);
    cam->cos_al_z = cos(al_z);
    cam->proj_plane_dist = proj_plane_dist;
    return cam;
}

variable_types: struct Camera { struct Point3d { double x; double y; double z; } camera_position; double al_x; double sin_al_x; double cos_al_x; double al_y; double sin_al_y; double cos_al_y; double al_z; double sin_al_z; double cos_al_z; double proj_plane_dist; } *

user_defined_types: struct Camera { struct Point3d camera_position; double al_x; double sin_al_x; double cos_al_x; double al_y; double sin_al_y; double cos_al_y; double al_z; double sin_al_z; double cos_al_z; double proj_plane_dist; }


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

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

original_code:
void
release_camera(Camera * const cam) {
    free(cam);
}

canonical_original_code:
void release_camera(struct Camera *cam) {
    free(cam);
}


original_code:
void
rotate_camera(Camera * const cam,
              const Float al_x,
              const Float al_y,
              const Float al_z) {
    if(fabs(al_x) > 1e-5) {
        cam->al_x += al_x;
        cam->sin_al_x = sin(cam->al_x);
        cam->cos_al_x = cos(cam->al_x);
    }
    if(fabs(al_y) > 1e-5) {
        cam->al_y += al_y;
        cam->sin_al_y = sin(cam->al_y);
        cam->cos_al_y = cos(cam->al_y);
    }
    if(fabs(al_z) > 1e-5) {
        cam->al_z += al_z;
        cam->sin_al_z = sin(cam->al_z);
        cam->cos_al_z = cos(cam->al_z);
    }
}

canonical_original_code:
void rotate_camera(struct Camera *cam, double al_x, double al_y, double al_z) {
    if(fabs(al_x) > 1e-5) {
        cam->al_x += al_x;
        cam->sin_al_x = sin(cam->al_x);
        cam->cos_al_x = cos(cam->al_x);
    }
    if(fabs(al_y) > 1e-5) {
        cam->al_y += al_y;
        cam->sin_al_y = sin(cam->al_y);
        cam->cos_al_y = cos(cam->al_y);
    }
    if(fabs(al_z) > 1e-5) {
        cam->al_z += al_z;
        cam->sin_al_z = sin(cam->al_z);
        cam->cos_al_z = cos(cam->al_z);
    }
}


decompiled_code:
double __fastcall exponential_fog_density(_QWORD *a1, double a2)
{
return 1.0 - exp(COERCE_DOUBLE(*a1 ^ LC0) * a2);
}

canonical_decompiled_code:
double __fastcall func0(_QWORD *a1, double a2)
{
return 1.0 - exp(COERCE_DOUBLE(*a1 ^ LC0) * a2);
}

original_code:
static inline Float
exponential_fog_density(const Float distance,
                        const void * fog_data) {
    Float * k = (Float *) fog_data;
    return 1 - exp(- (*k) * distance);
}

canonical_original_code:
static inline double exponential_fog_density(double distance, void *fog_data) {
    double *k = (Float *) fog_data;
    return 1 - exp(- (*k) * distance);
}

variable_types: distance double

variable_types:
k
double *

return_type: double


decompiled_code:
void __fastcall set_no_fog(__int64 a1)
{
void *v2; // rdi

v2 = *(void **)(a1 + 56);
if ( v2 )
free(v2);
*(_QWORD *)(a1 + 48) = 0LL;
}

original_code:
void
set_no_fog(Scene * const scene) {
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    scene->fog_density = ((void *)0);
}

canonical_original_code:
void set_no_fog(struct Scene *scene) {
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    scene->fog_density = ((void *)0);
}


decompiled_code:
void __fastcall set_exponential_fog(__int64 a1, double a2)
{
double *v3; // rbp
void *v4; // rdi

*(_QWORD *)(a1 + 48) = exponential_fog_density;
v3 = (double *)malloc(8uLL);
*v3 = a2;
v4 = *(void **)(a1 + 56);
if ( v4 )
free(v4);
*(_QWORD *)(a1 + 56) = v3;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, double a2)
{
double *v3; // rbp
void *v4; // rdi

*(_QWORD *)(a1 + 48) = exponential_fog_density;
v3 = (double *)malloc(8uLL);
*v3 = a2;
v4 = *(void **)(a1 + 56);
if ( v4 )
free(v4);
*(_QWORD *)(a1 + 56) = v3;
}

original_code:
void
set_exponential_fog(Scene * const scene,
                    const Float k) {
    scene->fog_density = exponential_fog_density;
    Float * k_p = malloc(sizeof(Float));
    *k_p = k;
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    scene->fog_parameters = k_p;
}

canonical_original_code:
void set_exponential_fog(struct Scene *scene, double k) {
    scene->fog_density = exponential_fog_density;
    double *k_p = malloc(sizeof(Float));
    *k_p = k;
    if(scene->fog_parameters) {
        free(scene->fog_parameters);
    }
    scene->fog_parameters = k_p;
}

variable_types: k double


decompiled_code:
double __fastcall dot_product(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

canonical_decompiled_code:
double __fastcall func2(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

original_code:
static inline Float
dot_product(const Vector3d v1,
            const Vector3d v2) {
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

canonical_original_code:
static inline double dot_product(struct Vector3d v1, struct Vector3d v2) {
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

variable_types: struct Vector3d { double x; double y; double z; }


decompiled_code:
double __fastcall sqr_module_vector(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
return dot_product(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a9, a10, a11);
}

canonical_decompiled_code:
double __fastcall func3(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
return func2(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a9, a10, a11);
}

original_code:
static inline Float
sqr_module_vector(const Vector3d v) {
    return dot_product(v, v);
}

canonical_original_code:
static inline double sqr_module_vector(struct Vector3d v) {
    return dot_product(v, v);
}


decompiled_code:
unsigned __int64 __fastcall get_sphere_color(__int64 a1)
{
return ((unsigned __int64)*(unsigned __int8 *)(a1 + 34) << 16) | *(unsigned __int16 *)(a1 + 32);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 a1)
{
return ((unsigned __int64)*(unsigned __int8 *)(a1 + 34) << 16) | *(unsigned __int16 *)(a1 + 32);
}

original_code:
static Color
get_sphere_color(const void * data,
                 const Point3d intersection_point) {
 const Sphere * sphere = data;
 return sphere->color;
}

canonical_original_code:
static struct Color get_sphere_color(void *data, struct Point3d intersection_point) {
 struct Sphere *sphere = data;
 return sphere->color;
}

variable_types:
data
void *

variable_types: struct Sphere { struct Point3d { double x; double y; double z; } center; double radius; struct Color { unsigned char r; unsigned char g; unsigned char b; } color; struct Material { double Ka; double Kd; double Ks; double Kr; double Kt; double p; } material; } *

user_defined_types: struct Sphere { struct Point3d center; double radius; struct Color color; struct Material material; }


decompiled_code:
double __fastcall module_vector(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v17; // xmm0_8
double result; // xmm0_8

v17 = sqr_module_vector(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
if ( v17 < 0.0 )
result = sqrt(v17);
else
result = sqrt(v17);
return result;
}

canonical_decompiled_code:
double __fastcall func7(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v17; // xmm0_8
double result; // xmm0_8

v17 = func3(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
if ( v17 < 0.0 )
result = sqrt(v17);
else
result = sqrt(v17);
return result;
}

original_code:
static inline Float
module_vector(const Vector3d v) {
    return sqrt(sqr_module_vector(v));
}

canonical_original_code:
static inline double module_vector(struct Vector3d v) {
    return sqrt(sqr_module_vector(v));
}


decompiled_code:
void __fastcall normalize_vector(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
double v9; // xmm0_8

v9 = module_vector(a2, a3, a4, a5, a6, a7, a8, a9, *a1, a1[1], a1[2]);
*a1 = *a1 / v9;
a1[1] = a1[1] / v9;
a1[2] = a1[2] / v9;
}

canonical_decompiled_code:
void __fastcall func8(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
double v9; // xmm0_8

v9 = func7(a2, a3, a4, a5, a6, a7, a8, a9, *a1, a1[1], a1[2]);
*a1 = *a1 / v9;
a1[1] = a1[1] / v9;
a1[2] = a1[2] / v9;
}

original_code:
static inline void
normalize_vector(Vector3d * const v) {
    const Float module = module_vector(*v);
    v->x = v->x / module;
    v->y = v->y / module;
    v->z = v->z / module;
}

canonical_original_code:
static inline void normalize_vector(struct Vector3d *v) {
    double module = module_vector(*v);
    v->x = v->x / module;
    v->y = v->y / module;
    v->z = v->z / module;
}

variable_types: struct Vector3d { double x; double y; double z; } *

variable_types: module double


original_code:
static Boolean
intersect_sphere(const void * data,
                 const Point3d vector_start,
                 const Vector3d vector,
                 Point3d * const intersection_point) {
    const Sphere * sphere = data;
    const Point3d center = sphere->center;
    const Float r = sphere->radius;
    const Float a = vector.x * vector.x
                    + vector.y * vector.y
                    + vector.z * vector.z;
    const Float b = 2 * (vector.x * (vector_start.x - center.x)
                         + vector.y * (vector_start.y - center.y)
                         + vector.z * (vector_start.z - center.z));
    const Float c = center.x * center.x
                    + center.y * center.y
                    + center.z * center.z
                    + vector_start.x * vector_start.x
                    + vector_start.y * vector_start.y
                    + vector_start.z * vector_start.z
                    - 2 * (vector_start.x * center.x
                           + vector_start.y * center.y
                           + vector_start.z * center.z)
                    - r * r;
    const Float D = b * b - 4 * a * c;
    if(D < 0)
        return 0;
    const Float sqrt_D = sqrt(D);
    const Float a_2 = 2 * a;
    const Float t1 = (-b + sqrt_D) / a_2;
    const Float t2 = (-b - sqrt_D) / a_2;
    const Float min_t = (t1 < t2) ? t1 : t2;
    const Float max_t = (t1 > t2) ? t1 : t2;
    const Float t = (min_t > 1e-5) ? min_t : max_t;
    if(t < 1e-5)
        return 0;
    *intersection_point = point3d(vector_start.x + t * vector.x,
                                  vector_start.y + t * vector.y,
                                  vector_start.z + t * vector.z);
    return 1;
}

canonical_original_code:
static int intersect_sphere(void *data, struct Point3d vector_start, struct Vector3d vector, struct Point3d *intersection_point) {
    struct Sphere *sphere = data;
    struct Point3d center = sphere->center;
    double r = sphere->radius;
    double a = vector.x * vector.x
                    + vector.y * vector.y
                    + vector.z * vector.z;
    double b = 2 * (vector.x * (vector_start.x - center.x)
                         + vector.y * (vector_start.y - center.y)
                         + vector.z * (vector_start.z - center.z));
    double c = center.x * center.x
                    + center.y * center.y
                    + center.z * center.z
                    + vector_start.x * vector_start.x
                    + vector_start.y * vector_start.y
                    + vector_start.z * vector_start.z
                    - 2 * (vector_start.x * center.x
                           + vector_start.y * center.y
                           + vector_start.z * center.z)
                    - r * r;
    double D = b * b - 4 * a * c;
    if(D < 0)
        return 0;
    double sqrt_D = sqrt(D);
    double a_2 = 2 * a;
    double t1 = (-b + sqrt_D) / a_2;
    double t2 = (-b - sqrt_D) / a_2;
    double min_t = (t1 < t2) ? t1 : t2;
    double max_t = (t1 > t2) ? t1 : t2;
    double t = (min_t > 1e-5) ? min_t : max_t;
    if(t < 1e-5)
        return 0;
    *intersection_point = point3d(vector_start.x + t * vector.x,
                                  vector_start.y + t * vector.y,
                                  vector_start.z + t * vector.z);
    return 1;
}

variable_types: struct Point3d { double x; double y; double z; }

variable_types: r double

variable_types: a double

variable_types: b double

variable_types: c double

variable_types: D double

variable_types: t double


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

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

original_code:
void
release_sphere_data(void * data) {
 Sphere * sphere = data;
 free(sphere);
}

canonical_original_code:
void release_sphere_data(void *data) {
 struct Sphere *sphere = data;
 free(sphere);
}


rgb

decompiled_code:
unsigned __int64 __fastcall rgb(char a1, char a2, unsigned __int8 a3)
{
unsigned __int16 v4; // [rsp+1h] [rbp-3h]

LOBYTE(v4) = a1;
HIBYTE(v4) = a2;
return ((unsigned __int64)a3 << 16) | v4;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(char a1, char a2, unsigned __int8 a3)
{
unsigned __int16 v4; // [rsp+1h] [rbp-3h]

LOBYTE(v4) = a1;
HIBYTE(v4) = a2;
return ((unsigned __int64)a3 << 16) | v4;
}

original_code:
static inline Color
rgb(Byte r,
    Byte g,
    Byte b) {
        Color c;
        c.r = r;
        c.g = g;
        c.b = b;
        return c;
}

canonical_original_code:
static inline struct Color rgb(unsigned char r, unsigned char g, unsigned char b) {
        struct Color c;
        c.r = r;
        c.g = g;
        c.b = b;
        return c;
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char

variable_types: struct Color { unsigned char r; unsigned char g; unsigned char b; }


decompiled_code:
unsigned __int64 __fastcall add_colors(int a1, unsigned int a2)
{
unsigned int v2; // edx
int v3; // eax
int v4; // esi
int v5; // edx
char v6; // di
unsigned __int64 v7; // rax

v2 = HIWORD(a2);
v3 = (unsigned __int8)a2 + (unsigned __int8)a1;
v4 = BYTE1(a1) + BYTE1(a2);
v5 = BYTE2(a1) + (unsigned __int8)v2;
v6 = -1;
if ( v5 > 255 )
LOBYTE(v5) = -1;
if ( v4 > 255 )
LOBYTE(v4) = -1;
if ( v3 <= 255 )
v6 = v3;
v7 = rgb(v6, v4, v5);
return (unsigned __int16)v7 | v7 & 0xFF0000;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(int a1, unsigned int a2)
{
unsigned int v2; // edx
int v3; // eax
int v4; // esi
int v5; // edx
char v6; // di
unsigned __int64 v7; // rax

v2 = HIWORD(a2);
v3 = (unsigned __int8)a2 + (unsigned __int8)a1;
v4 = BYTE1(a1) + BYTE1(a2);
v5 = BYTE2(a1) + (unsigned __int8)v2;
v6 = -1;
if ( v5 > 255 )
LOBYTE(v5) = -1;
if ( v4 > 255 )
LOBYTE(v4) = -1;
if ( v3 <= 255 )
v6 = v3;
v7 = func0(v6, v4, v5);
return (unsigned __int16)v7 | v7 & 0xFF0000;
}

original_code:
static inline Color
add_colors(Color c1,
           Color c2) {
 int r = (int) c1.r + c2.r;
 int g = (int) c1.g + c2.g;
 int b = (int) c1.b + c2.b;
 r = (r < 255) ? r : 255;
 g = (g < 255) ? g : 255;
 b = (b < 255) ? b : 255;
 return rgb((Byte) r, (Byte) g, (Byte) b);
}

canonical_original_code:
static inline struct Color add_colors(struct Color c1, struct Color c2) {
 int r = (int) c1.r + c2.r;
 int g = (int) c1.g + c2.g;
 int b = (int) c1.b + c2.b;
 r = (r < 255) ? r : 255;
 g = (g < 255) ? g : 255;
 b = (b < 255) ? b : 255;
 return rgb((unsigned char) r, (unsigned char) g, (unsigned char) b);
}

variable_types: r int

variable_types: g int

variable_types: b int

return_type: struct Color { unsigned char r; unsigned char g; unsigned char b; }


original_code:
void
render_scene(const Scene * const scene,
             const Camera * const camera,
             Canvas * canvas,
             const int num_threads) {
    const int w = canvas->w;
    const int h = canvas->h;
    const Float dx = w / 2.0;
    const Float dy = h / 2.0;
    const Float focus = camera->proj_plane_dist;
    omp_set_num_threads((num_threads < 2) ? 1 : num_threads);
    int i;
    int j;

#pragma omp parallel private(i, j)

#pragma omp for collapse(2) schedule(dynamic, 10)
    for(i = 0; i < w; i++) {
        for(j = 0; j < h; j++) {
            const Float x = i - dx;
            const Float y = j - dy;
            const Vector3d ray = vector3df(x, y, focus);
            const Color col = trace(scene, camera, ray);
            set_pixel(i, j, col, canvas);
        }
    }
    const int antialiasing = 1;
    if(antialiasing) {
        Canvas * edges = detect_edges_canvas(canvas, num_threads);

#pragma omp parallel private(i, j)

#pragma omp for collapse(2) schedule(dynamic, 10)
        for(i = 1; i < w - 1; i++) {
            for(j = 1; j < h - 1; j++) {
                Byte gray = get_pixel(i, j, edges).r;
                if(gray > 10) {
                    const Float x = i - dx;
                    const Float y = j - dy;
                    Color c = get_pixel(i, j, canvas);
                    const Float weight = 1.0 / 4;
                    c = mul_color(c, weight);
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y, focus)), weight));
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x, y + 0.5, focus)), weight));
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y + 0.5, focus)), weight));
                    set_pixel(i, j, c, canvas);
                }
            }
        }
        release_canvas(edges);
    }
}

canonical_original_code:
void render_scene(struct Scene *scene, struct Camera *camera, struct Canvas *canvas, int num_threads) {
    int w = canvas->w;
    int h = canvas->h;
    double dx = w / 2.0;
    double dy = h / 2.0;
    double focus = camera->proj_plane_dist;
    omp_set_num_threads((num_threads < 2) ? 1 : num_threads);
    int i;
    int j;

#pragma omp parallel private(i, j)

#pragma omp for collapse(2) schedule(dynamic, 10)
    for(i = 0; i < w; i++) {
        for(j = 0; j < h; j++) {
            double x = i - dx;
            double y = j - dy;
            struct Vector3d ray = vector3df(x, y, focus);
            struct Color col = trace(scene, camera, ray);
            set_pixel(i, j, col, canvas);
        }
    }
    int antialiasing = 1;
    if(antialiasing) {
        struct Canvas *edges = detect_edges_canvas(canvas, num_threads);

#pragma omp parallel private(i, j)

#pragma omp for collapse(2) schedule(dynamic, 10)
        for(i = 1; i < w - 1; i++) {
            for(j = 1; j < h - 1; j++) {
                unsigned char gray = get_pixel(i, j, edges).r;
                if(gray > 10) {
                    double x = i - dx;
                    double y = j - dy;
                    struct Color c = get_pixel(i, j, canvas);
                    double weight = 1.0 / 4;
                    c = mul_color(c, weight);
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y, focus)), weight));
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x, y + 0.5, focus)), weight));
                    c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y + 0.5, focus)), weight));
                    set_pixel(i, j, c, canvas);
                }
            }
        }
        release_canvas(edges);
    }
}

variable_types: struct Scene { struct Object3d { void *data; int (*intersect)(void *data, struct Point3d { double x; double y; double z; } vector_start, struct Vector3d { double x; double y; double z; } vector, struct Point3d *intersection_point); struct Color { unsigned char r; unsigned char g; unsigned char b; } (*get_color)(void *data, struct Point3d intersection_point); struct Vector3d (*get_normal_vector)(void *data, struct Point3d intersection_point); struct Material { double Ka; double Kd; double Ks; double Kr; double Kt; double p; } (*get_material)(void *data, struct Point3d intersection_point); struct Point3d (*get_min_boundary_point)(void *data); struct Point3d (*get_max_boundary_point)(void *data); void (*release_data)(void *data); } **objects; int objects_count; int last_object_index; struct KDTree { struct KDNode { enum Plane { XY=0, XZ=1, YZ=2, NONE=3 } plane; union Coord { double x; double y; double z; } coord; struct Object3d **objects; int objects_count; struct KDNode *l; struct KDNode *r; } *root; struct Voxel { double x_min; double y_min; double z_min; double x_max; double y_max; double z_max; } bounding_box; } *kd_tree; struct LightSource3d { struct Point3d location_world; struct Point3d location; struct Color color; } **light_sources; int light_sources_count; int last_light_source_index; struct Color background_color; double (*fog_density)(double distance, void *fog_parameters); void *fog_parameters; } *

variable_types: struct Camera { struct Point3d camera_position; double al_x; double sin_al_x; double cos_al_x; double al_y; double sin_al_y; double cos_al_y; double al_z; double sin_al_z; double cos_al_z; double proj_plane_dist; } *

variable_types: struct Canvas { int w; int h; struct Color *data; } *

variable_types: w int

variable_types: h int

variable_types: dx double

variable_types: dy double

variable_types: focus double

variable_types: i int

variable_types: j int

variable_types: x double

variable_types: y double

variable_types: antialiasing int

variable_types: struct Canvas { int w; int h; struct Color { unsigned char r; unsigned char g; unsigned char b; } *data; } *

variable_types:
gray
unsigned char

variable_types: weight double


isOption

decompiled_code:
_BOOL8 __fastcall isOption(_BYTE *a1)
{
return *a1 == 45;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(_BYTE *a1)
{
return *a1 == 45;
}

original_code:
int isOption(const char* arg){
 if(*arg=='-')
  return 1;
 else
  return 0;
}

canonical_original_code:
int isOption(char *arg){
 if(*arg=='-')
  return 1;
 else
  return 0;
}

variable_types:
arg
char *


getParam

decompiled_code:
const char *__fastcall getParam(char *s)
{
_BOOL4 v1; // eax
const char *v2; // rcx
size_t v4; // rax
int v5; // ebp
__int64 v6; // rax

v1 = isOption(s);
v2 = "NO_PARAM_INPUT";
if ( v1 )
{
v4 = strlen(s);
v5 = v4;
v2 = (const char *)malloc(v4 - 1);
if ( v5 > 1 )
{
v6 = 1LL;
do
{
v2[v6 - 1] = s[v6];
++v6;
}
while ( v6 != (unsigned int)(v5 - 2) + 2LL );
}
}
return v2;
}

canonical_decompiled_code:
const char *__fastcall func8(char *s)
{
_BOOL4 v1; // eax
const char *v2; // rcx
size_t v4; // rax
int v5; // ebp
__int64 v6; // rax

v1 = func7(s);
v2 = "NO_PARAM_INPUT";
if ( v1 )
{
v4 = strlen(s);
v5 = v4;
v2 = (const char *)malloc(v4 - 1);
if ( v5 > 1 )
{
v6 = 1LL;
do
{
v2[v6 - 1] = s[v6];
++v6;
}
while ( v6 != (unsigned int)(v5 - 2) + 2LL );
}
}
return v2;
}

original_code:
char*getParam(const char* param){
 if(isOption(param)==0){
  return "NO_PARAM_INPUT";
 }
 int paramLen=strlen(param);
 char* res=(char*)malloc(sizeof(char)*(strlen(param)-1));
 for(int i=1;i<paramLen;i++)
  *(res+(i-1))=*(param+i);
 return res;
}

canonical_original_code:
char *getParam(char *param){
 if(isOption(param)==0){
  return "NO_PARAM_INPUT";
 }
 int paramLen = strlen(param);
 char *res = (char*)malloc(sizeof(char)*(strlen(param)-1));
 for(int i = 1;i<paramLen;i++)
  *(res+(i-1))=*(param+i);
 return res;
}

variable_types:
param
char *

variable_types: paramLen int

variable_types:
res
char *

return_type: char *


wiringPiSetup

decompiled_code:
__int64 wiringPiSetup()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code: int wiringPiSetup(void) {return 0;}

canonical_original_code: int wiringPiSetup() {return 0;}


pinMode

decompiled_code:
void pinMode()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code: void pinMode (int a, int b) {a=b;}

canonical_original_code: void pinMode(int a, int b) {a=b;}

variable_types: a int

variable_types: b int


delayMicroseconds

decompiled_code:
void delayMicroseconds()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code: void delayMicroseconds(unsigned int a) {a=a;}

variable_types:
a
unsigned int


digitalWrite

decompiled_code:
void digitalWrite()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code: void digitalWrite(int a, int b) {a=b;}

canonical_original_code: void digitalWrite(int a, int b) {a=b;}


digitalRead

decompiled_code:
__int64 __fastcall digitalRead(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
return a1;
}

original_code: int digitalRead(int a) {return a;}

canonical_original_code: int digitalRead(int a) {return a;}


decodeByte

decompiled_code:
__int64 __fastcall decodeByte(__int16 a1)
{
int v1; // ecx
int v2; // eax
unsigned int v3; // esi

v1 = 7;
v2 = 0x8000;
v3 = 0;
do
{
v3 |= ((unsigned __int16)(v2 & a1) == v2) << v1--;
v2 >>= 2;
}
while ( v1 != -1 );
return v3;
}

original_code:
unsigned char decodeByte(unsigned short int word)
{
    unsigned char decodedChar=0;
    int j;
    int shift = 7;
    for(j=0x8000;j>0; j>>=2) {
     decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift;
  shift -=1;
    }
    return decodedChar;
}

canonical_original_code:
unsigned char decodeByte(unsigned short word)
{
    unsigned char decodedChar = 0;
    int j;
    int shift = 7;
    for(j=0x8000;j>0; j>>=2) {
     decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift;
  shift -=1;
    }
    return decodedChar;
}

variable_types:
word
unsigned short

variable_types:
decodedChar
unsigned char

variable_types: shift int

return_type: unsigned char


homeEasyDecode

original_code:
unsigned long int homeEasyDecode(BYTE_BUFFER *buffer)
{
    unsigned long int result = 0;
    unsigned short int word;
    unsigned char *byte = (unsigned char*)&word;
    unsigned int i;
    for(i=0; i<(buffer->size+1)/2; i++) {
        byte[1] = buffer->data[2*i];
        byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
        result += (decodeByte(word)) << ((3 - i)* 8);
    }
    return result;
}

canonical_original_code:
unsigned long homeEasyDecode(struct BYTE_BUFFER *buffer)
{
    unsigned long result = 0;
    unsigned short word;
    unsigned char *byte = (unsigned char*)&word;
    unsigned int i;
    for(i=0; i<(buffer->size+1)/2; i++) {
        byte[1] = buffer->data[2*i];
        byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
        result += (decodeByte(word)) << ((3 - i)* 8);
    }
    return result;
}

variable_types: struct BYTE_BUFFER { char *data; unsigned long size; } *

variable_types:
result
unsigned long

variable_types:
byte
unsigned char *

variable_types:
i
unsigned int

return_type: unsigned long


encodeByte

decompiled_code:
__int64 __fastcall encodeByte(char a1)
{
int v1; // ecx
int v2; // edx
unsigned int v3; // esi

v1 = 14;
v2 = 128;
v3 = 0;
do
{
v3 |= (((unsigned __int8)(v2 & a1) == v2) + 1) << v1;
v1 -= 2;
v2 >>= 1;
}
while ( v1 != -2 );
return v3;
}

original_code:
unsigned short int encodeByte(unsigned char byte)
{
    unsigned short int encodedChar=0;
    int j;
    int shift = 14;
    for(j=0x80;j>0; j>>=1) {
       encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
       shift -=2;
    }
    return encodedChar;
}

canonical_original_code:
unsigned short encodeByte(unsigned char byte)
{
    unsigned short encodedChar = 0;
    int j;
    int shift = 14;
    for(j=0x80;j>0; j>>=1) {
       encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
       shift -=2;
    }
    return encodedChar;
}

variable_types:
byte
unsigned char

variable_types:
encodedChar
unsigned short

variable_types: j int

return_type: unsigned short


homeEasyEncode

decompiled_code:
__int64 __fastcall homeEasyEncode(unsigned __int64 a1)
{
__int64 v1; // rdx
int i; // ebx
unsigned __int16 v3; // ax
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v5[0] = createByteBuffer();
v5[1] = v1;
for ( i = 24; i != -8; i -= 8 )
{
v3 = encodeByte(a1 >> i);
pushWord(v5, v3);
}
return v5[0];
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 a1)
{
__int64 v1; // rdx
int i; // ebx
unsigned __int16 v3; // ax
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v5[0] = createByteBuffer();
v5[1] = v1;
for ( i = 24; i != -8; i -= 8 )
{
v3 = func7(a1 >> i);
pushWord(v5, v3);
}
return v5[0];
}

original_code:
BYTE_BUFFER homeEasyEncode(unsigned long int frame)
{
    BYTE_BUFFER result = createByteBuffer();
    unsigned int i;
    for(i=0; i<4; i++) {
        pushWord(&result, encodeByte((frame >> ((3 - i) * 8)) & 0xff));
    }
    return result;
}

canonical_original_code:
struct BYTE_BUFFER homeEasyEncode(unsigned long frame)
{
    struct BYTE_BUFFER result = createByteBuffer();
    unsigned int i;
    for(i=0; i<4; i++) {
        pushWord(&result, encodeByte((frame >> ((3 - i) * 8)) & 0xff));
    }
    return result;
}

variable_types:
frame
unsigned long

variable_types: struct BYTE_BUFFER { char *data; unsigned long size; }


createHomeEasyCommand

original_code:
unsigned long int createHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on)
{
    unsigned long int command = id << 6;
    char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
    command |= (formatedSection == 'G' ? 0x20 : 0);
    command |= (on ? 0x10 : 0);
    command |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
    command |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
    return command;
}

canonical_original_code:
unsigned long createHomeEasyCommand(unsigned long id, char section, unsigned char nb, unsigned char on)
{
    unsigned long command = id << 6;
    char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
    command |= (formatedSection == 'G' ? 0x20 : 0);
    command |= (on ? 0x10 : 0);
    command |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
    command |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
    return command;
}

variable_types:
id
unsigned long

variable_types: section char

variable_types:
nb
unsigned char

variable_types:
on
unsigned char

variable_types:
command
unsigned long

variable_types: formatedSection char


getHomeEasyId

decompiled_code:
unsigned __int64 __fastcall getHomeEasyId(unsigned __int64 a1)
{
return a1 >> 6;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(unsigned __int64 a1)
{
return a1 >> 6;
}

original_code:
unsigned long int getHomeEasyId(unsigned long int frame)
{
    return frame >> 6;
}

canonical_original_code:
unsigned long getHomeEasyId(unsigned long frame)
{
    return frame >> 6;
}


getHomeEasyInfo

decompiled_code:
void __fastcall getHomeEasyInfo(unsigned __int64 a1, unsigned __int64 *a2, _BYTE *a3, _BYTE *a4, _BYTE *a5)
{
if ( a2 )
*a2 = getHomeEasyId(a1);
if ( a3 )
*a3 = (((unsigned __int8)a1 >> 4) ^ 1) & 1;
if ( !a4 )
{
if ( !a5 )
return;
if ( (a1 & 0x20) != 0 )
goto LABEL_8;
LABEL_10:
*a5 = (a1 & 3) + 1;
return;
}
if ( (a1 & 0x20) == 0 )
{
*a4 = (((unsigned __int8)a1 >> 2) & 3) + 65;
if ( !a5 )
return;
goto LABEL_10;
}
*a4 = 71;
if ( a5 )
LABEL_8:
*a5 = 0;
}

original_code:
void getHomeEasyInfo(unsigned long int frame, unsigned long int* id, unsigned char* onOff, unsigned char* section, unsigned char* number)
{
    unsigned char data = (unsigned char)(frame & 0x0000007f);
    if (id) {
        *id = getHomeEasyId(frame);
    }
    if (onOff) {
        *onOff = ((data & 0x10) == 0);
    }
    if (section) {
        if ((data & 0x20) == 0x20) {
            *section = 'G';
        } else {
            *section = ((data & 0x0f) >> 2) + 'A';
        }
    }
    if (number) {
        if ((data & 0x20) != 0x20) {
            *number = 1 + (data & 0x03);
        } else {
            *number=0;
        }
    }
}

canonical_original_code:
void getHomeEasyInfo(unsigned long frame, unsigned long *id, unsigned char *onOff, unsigned char *section, unsigned char *number)
{
    unsigned char data = (unsigned char)(frame & 0x0000007f);
    if (id) {
        *id = getHomeEasyId(frame);
    }
    if (onOff) {
        *onOff = ((data & 0x10) == 0);
    }
    if (section) {
        if ((data & 0x20) == 0x20) {
            *section = 'G';
        } else {
            *section = ((data & 0x0f) >> 2) + 'A';
        }
    }
    if (number) {
        if ((data & 0x20) != 0x20) {
            *number = 1 + (data & 0x03);
        } else {
            *number=0;
        }
    }
}

variable_types:
id
unsigned long *

variable_types:
onOff
unsigned char *

variable_types:
section
unsigned char *

variable_types:
number
unsigned char *

variable_types:
data
unsigned char


setHomeEasyTransmittorPin

decompiled_code:
void __fastcall setHomeEasyTransmittorPin(char a1)
{
homeEasyPinOut = a1;
}

canonical_decompiled_code:
void __fastcall func12(char a1)
{
homeEasyPinOut = a1;
}

original_code:
void setHomeEasyTransmittorPin(unsigned char pinNumber)
{
    homeEasyPinOut = pinNumber;
}

variable_types:
pinNumber
unsigned char


getHomeEasyTransmittorPin

decompiled_code:
__int64 getHomeEasyTransmittorPin()
{
return (unsigned __int8)homeEasyPinOut;
}

canonical_decompiled_code:
__int64 func13()
{
return (unsigned __int8)homeEasyPinOut;
}

original_code:
unsigned char getHomeEasyTransmittorPin()
{
    return homeEasyPinOut;
}


setHomeEasyReceptorPin

decompiled_code:
void __fastcall setHomeEasyReceptorPin(char a1)
{
homeEasyPinIn = a1;
}

canonical_decompiled_code:
void __fastcall func14(char a1)
{
homeEasyPinIn = a1;
}

original_code:
void setHomeEasyReceptorPin(unsigned char pinNumber)
{
    homeEasyPinIn = pinNumber;
}


getHomeEasyReceptorPin

decompiled_code:
__int64 getHomeEasyReceptorPin()
{
return (unsigned __int8)homeEasyPinIn;
}

canonical_decompiled_code:
__int64 func15()
{
return (unsigned __int8)homeEasyPinIn;
}

original_code:
unsigned char getHomeEasyReceptorPin()
{
    return homeEasyPinIn;
}


initIO

decompiled_code:
__int64 initIO()
{
unsigned int v0; // ebx

v0 = wiringPiSetup();
if ( v0 == -1 )
printf("GPIO setup failed %d\n", 0xFFFFFFFFLL);
return v0;
}

canonical_decompiled_code:
__int64 func16()
{
unsigned int v0; // ebx

v0 = func0();
if ( v0 == -1 )
printf("GPIO setup failed %d\n", 0xFFFFFFFFLL);
return v0;
}

original_code:
int initIO()
{
    int status;
    status = wiringPiSetup();
    if (status != -1) {
        pinMode(homeEasyPinIn, 0);
        pinMode(homeEasyPinOut, 1);
    } else {
        printf("GPIO setup failed %d\n", status);
    }
    return status;
}

variable_types: status int


sendHomeEasyBit

decompiled_code:
void sendHomeEasyBit()
{
;
}

canonical_decompiled_code:
void func17()
{
;
}

original_code:
void sendHomeEasyBit(unsigned char bit)
{
    digitalWrite(homeEasyPinOut, 1);
    delayMicroseconds(timings[bit][0]);
    digitalWrite(homeEasyPinOut, 0);
    delayMicroseconds(timings[bit][1]);
    digitalWrite(homeEasyPinOut, 1);
}

variable_types:
bit
unsigned char


sendHomeEasyByte

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

LODWORD(result) = 8;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
return result;
}

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

LODWORD(result) = 8;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
return result;
}

original_code:
void sendHomeEasyByte(unsigned char byte)
{
    int i;
    for(i=0x80;i>0; i>>=1) {
     sendHomeEasyBit((byte & i) == i);
    }
}

variable_types:
byte
unsigned char

variable_types: i int


sendHomeEasyBytes

decompiled_code:
void __fastcall sendHomeEasyBytes(__int64 a1, unsigned __int64 a2)
{
unsigned int i; // eax

if ( a2 )
{
for ( i = 0; i < a2; ++i )
;
}
}

canonical_decompiled_code:
void __fastcall func19(__int64 a1, unsigned __int64 a2)
{
unsigned int i; // eax

if ( a2 )
{
for ( i = 0; i < a2; ++i )
;
}
}

original_code:
void sendHomeEasyBytes(BYTE_BUFFER buffer)
{
    unsigned int i;
    for(i=0; i<buffer.size; i++) {
        sendHomeEasyByte(buffer.data[i]);
    }
}

canonical_original_code:
void sendHomeEasyBytes(struct BYTE_BUFFER buffer)
{
    unsigned int i;
    for(i=0; i<buffer.size; i++) {
        sendHomeEasyByte(buffer.data[i]);
    }
}

variable_types: struct BYTE_BUFFER { char *data; unsigned long size; }

variable_types:
i
unsigned int


sendFrame

decompiled_code:
__int64 __fastcall sendFrame(__int64 a1, unsigned __int64 a2, int a3)
{
int v4; // ebx

cpuMode(0LL);
if ( a3 )
{
v4 = 0;
do
{
sendHomeEasyBytes(a1, a2);
++v4;
}
while ( a3 != v4 );
}
return cpuMode(1LL);
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, unsigned __int64 a2, int a3)
{
int v4; // ebx

cpuMode(0LL);
if ( a3 )
{
v4 = 0;
do
{
func19(a1, a2);
++v4;
}
while ( a3 != v4 );
}
return cpuMode(1LL);
}

original_code:
void sendFrame(BYTE_BUFFER frame, unsigned int repeat)
{
    unsigned int i;
    cpuMode(0);
    sendHomeEasyBit(2);
    sendHomeEasyBit(3);
    for(i=0; i<repeat; i++) {
        sendHomeEasyBytes(frame);
        sendHomeEasyBit(4);
    }
    digitalWrite(homeEasyPinOut, 0);
    cpuMode(1);
}

canonical_original_code:
void sendFrame(struct BYTE_BUFFER frame, unsigned int repeat)
{
    unsigned int i;
    cpuMode(0);
    sendHomeEasyBit(2);
    sendHomeEasyBit(3);
    for(i=0; i<repeat; i++) {
        sendHomeEasyBytes(frame);
        sendHomeEasyBit(4);
    }
    digitalWrite(homeEasyPinOut, 0);
    cpuMode(1);
}

variable_types:
repeat
unsigned int

user_defined_types: struct BYTE_BUFFER { char *data; unsigned long size; }


sendHomeEasyCommand

decompiled_code:
__int64 __fastcall sendHomeEasyCommand(__int64 a1, char a2, char a3, char a4, unsigned __int8 a5)
{
__int64 v6; // rax
__int64 v7; // r12
unsigned __int64 v8; // rdx
unsigned __int64 v9; // rbp

v6 = createHomeEasyCommand(a1, a2, a3, a4);
v7 = homeEasyEncode(v6);
v9 = v8;
sendFrame(v7, v8, a5);
return destroyByteBuffer(v7, v9);
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, char a2, char a3, char a4, unsigned __int8 a5)
{
__int64 v6; // rax
__int64 v7; // r12
unsigned __int64 v8; // rdx
unsigned __int64 v9; // rbp

v6 = func9(a1, a2, a3, a4);
v7 = func8(v6);
v9 = v8;
func20(v7, v8, a5);
return destroyByteBuffer(v7, v9);
}

original_code:
void sendHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
{
    unsigned long int command;
    BYTE_BUFFER encoded;
    command = createHomeEasyCommand(id, section, nb, on);
    encoded = homeEasyEncode(command);
    sendFrame(encoded, repeat);
    destroyByteBuffer(encoded);
}

canonical_original_code:
void sendHomeEasyCommand(unsigned long id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
{
    unsigned long command;
    struct BYTE_BUFFER encoded;
    command = createHomeEasyCommand(id, section, nb, on);
    encoded = homeEasyEncode(command);
    sendFrame(encoded, repeat);
    destroyByteBuffer(encoded);
}

variable_types:
id
unsigned long

variable_types: section char

variable_types:
nb
unsigned char

variable_types:
repeat
unsigned char


frameSize

decompiled_code:
__int64 __fastcall frameSize(_BYTE *a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax

if ( a2 )
*a2 = 0;
result = 0LL;
if ( *a1 == 1 )
{
do
result = (unsigned int)(result + 1);
while ( a1[(unsigned int)result] );
if ( !a2 )
goto LABEL_5;
*a2 = result;
}
while ( !a1[(unsigned int)result] )
LABEL_5:
result = (unsigned int)(result + 1);
if ( a3 )
*a3 = result - *a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(_BYTE *a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax

if ( a2 )
*a2 = 0;
result = 0LL;
if ( *a1 == 1 )
{
do
result = (unsigned int)(result + 1);
while ( a1[(unsigned int)result] );
if ( !a2 )
goto LABEL_5;
*a2 = result;
}
while ( !a1[(unsigned int)result] )
LABEL_5:
result = (unsigned int)(result + 1);
if ( a3 )
*a3 = result - *a2;
return result;
}

original_code:
unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low)
{
    unsigned int i=0;
    if (high) *high = 0;
    if (data[i] == 1) {
        for(i=0; data[i]; i++) ;
        if (high) *high = i;
    }
    for(; !data[i]; i++) ;
    if (low) *low = i - *high;
    return i;
}

canonical_original_code:
unsigned int frameSize(unsigned char *data, unsigned int *high, unsigned int *low)
{
    unsigned int i = 0;
    if (high) *high = 0;
    if (data[i] == 1) {
        for(i=0; data[i]; i++) ;
        if (high) *high = i;
    }
    for(; !data[i]; i++) ;
    if (low) *low = i - *high;
    return i;
}

variable_types:
data
unsigned char *

variable_types:
high
unsigned int *

variable_types:
low
unsigned int *

return_type: unsigned int


testByteBuffer

decompiled_code:
__int64 testByteBuffer()
{
__int64 v0; // rdx
__int16 v2; // [rsp+Dh] [rbp-1Bh] BYREF
char v3; // [rsp+Fh] [rbp-19h]
__int64 v4; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v2 = -4147;
v3 = 18;
printf("\n=== %s ===\n", "testByteBuffer");
v4 = createByteBuffer();
v5 = v0;
pushByte(&v4, 171LL);
pushBytes(&v4, &v2, 3LL);
pushWord(&v4, 13398LL);
printf("Should: AB CD EF 12 34 56\nGet: ");
printfByteBuffer(v4, v5);
return destroyByteBuffer(v4, v5);
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rdx
__int16 v2; // [rsp+Dh] [rbp-1Bh] BYREF
char v3; // [rsp+Fh] [rbp-19h]
__int64 v4; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v2 = -4147;
v3 = 18;
printf("\n=== %s ===\n", "testByteBuffer");
v4 = createByteBuffer();
v5 = v0;
pushByte(&v4, 171LL);
pushBytes(&v4, &v2, 3LL);
pushWord(&v4, 13398LL);
printf("Should: AB CD EF 12 34 56\nGet: ");
printfByteBuffer(v4, v5);
return destroyByteBuffer(v4, v5);
}

original_code:
void testByteBuffer()
{
    BYTE_BUFFER buffer;
    char bytes[] = {0xcd, 0xef, 0x12};
    printf("\n=== %s ===\n", __FUNCTION__);
    buffer = createByteBuffer();
    pushByte(&buffer, 0xab);
    pushBytes(&buffer, bytes, 3);
    pushWord(&buffer, 0x3456);
    printf("Should: AB CD EF 12 34 56\nGet: ");
    printfByteBuffer(buffer);
    destroyByteBuffer(buffer);
}

canonical_original_code:
void testByteBuffer()
{
    struct BYTE_BUFFER buffer;
    char bytes[] = {0xcd, 0xef, 0x12};
    printf("\n=== %s ===\n", __FUNCTION__);
    buffer = createByteBuffer();
    pushByte(&buffer, 0xab);
    pushBytes(&buffer, bytes, 3);
    pushWord(&buffer, 0x3456);
    printf("Should: AB CD EF 12 34 56\nGet: ");
    printfByteBuffer(buffer);
    destroyByteBuffer(buffer);
}

variable_types:
bytes
char []


testPrintBits

decompiled_code:
__int64 testPrintBits()
{
__int64 v0; // rdx
__int64 v2; // [rsp+8h] [rbp-20h] BYREF
__int64 v3; // [rsp+10h] [rbp-18h] BYREF
__int64 v4; // [rsp+18h] [rbp-10h]

v2 = 0xF0DEBC9A78563412LL;
printf("\n=== %s ===\n", "testPrintBits");
v3 = createByteBuffer();
v4 = v0;
pushBytes(&v3, &v2, 8LL);
printfBitBuffer(v3, v4);
return destroyByteBuffer(v3, v4);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rdx
__int64 v2; // [rsp+8h] [rbp-20h] BYREF
__int64 v3; // [rsp+10h] [rbp-18h] BYREF
__int64 v4; // [rsp+18h] [rbp-10h]

v2 = 0xF0DEBC9A78563412LL;
printf("\n=== %s ===\n", "testPrintBits");
v3 = createByteBuffer();
v4 = v0;
pushBytes(&v3, &v2, 8LL);
printfBitBuffer(v3, v4);
return destroyByteBuffer(v3, v4);
}

original_code:
void testPrintBits()
{
    BYTE_BUFFER buffer;
    char bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
    printf("\n=== %s ===\n", __FUNCTION__);
    buffer = createByteBuffer();
    pushBytes(&buffer, bytes, 8);
    printfBitBuffer(buffer);
    destroyByteBuffer(buffer);
}

canonical_original_code:
void testPrintBits()
{
    struct BYTE_BUFFER buffer;
    char bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
    printf("\n=== %s ===\n", __FUNCTION__);
    buffer = createByteBuffer();
    pushBytes(&buffer, bytes, 8);
    printfBitBuffer(buffer);
    destroyByteBuffer(buffer);
}


testGetId

decompiled_code:
int testGetId()
{
__int64 v0; // rax

printf("\n=== %s ===\n", "testGetId");
v0 = getHomeEasyId(673776026LL);
return printf("Should 00A0A406\nGet %08X\n", v0);
}

canonical_decompiled_code:
int func2()
{
__int64 v0; // rax

printf("\n=== %s ===\n", "testGetId");
v0 = getHomeEasyId(673776026LL);
return printf("Should 00A0A406\nGet %08X\n", v0);
}

original_code:
void testGetId()
{
    unsigned long int id;
    unsigned long int frame = 0x2829019a;
    printf("\n=== %s ===\n", __FUNCTION__);
    id = getHomeEasyId(frame);
    printf("Should 00A0A406\nGet %08X\n", id);
}

canonical_original_code:
void testGetId()
{
    unsigned long id;
    unsigned long frame = 0x2829019a;
    printf("\n=== %s ===\n", __FUNCTION__);
    id = getHomeEasyId(frame);
    printf("Should 00A0A406\nGet %08X\n", id);
}

variable_types:
frame
unsigned long


testEncode

decompiled_code:
int testEncode()
{
unsigned __int16 v0; // ax

printf("\n=== %s ===\n", "testEncode");
v0 = encodeByte(40LL);
return printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", 40LL, v0);
}

canonical_decompiled_code:
int func3()
{
unsigned __int16 v0; // ax

printf("\n=== %s ===\n", "testEncode");
v0 = encodeByte(40LL);
return printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", 40LL, v0);
}

original_code:
void testEncode()
{
    unsigned char srcByte = 0x28;
    unsigned short int encWord;
    printf("\n=== %s ===\n", __FUNCTION__);
    encWord = encodeByte(srcByte);
    printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", srcByte, encWord);
}

canonical_original_code:
void testEncode()
{
    unsigned char srcByte = 0x28;
    unsigned short encWord;
    printf("\n=== %s ===\n", __FUNCTION__);
    encWord = encodeByte(srcByte);
    printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", srcByte, encWord);
}

variable_types:
srcByte
unsigned char

variable_types:
encWord
unsigned short


testDecode

decompiled_code:
int testDecode()
{
unsigned __int8 v0; // al

printf("\n=== %s ===\n", "testDecode");
v0 = decodeByte(22933LL);
return printf("Input: %04X\nShould: 0x28\nGet: %02X\n", 22933LL, v0);
}

canonical_decompiled_code:
int func4()
{
unsigned __int8 v0; // al

printf("\n=== %s ===\n", "testDecode");
v0 = decodeByte(22933LL);
return printf("Input: %04X\nShould: 0x28\nGet: %02X\n", 22933LL, v0);
}

original_code:
void testDecode()
{
    unsigned short int srcWord = 0x5995;
    unsigned char decByte;
    printf("\n=== %s ===\n", __FUNCTION__);
    decByte = decodeByte(srcWord);
    printf("Input: %04X\nShould: 0x28\nGet: %02X\n", srcWord, decByte);
}

canonical_original_code:
void testDecode()
{
    unsigned short srcWord = 0x5995;
    unsigned char decByte;
    printf("\n=== %s ===\n", __FUNCTION__);
    decByte = decodeByte(srcWord);
    printf("Input: %04X\nShould: 0x28\nGet: %02X\n", srcWord, decByte);
}

variable_types:
srcWord
unsigned short

variable_types:
decByte
unsigned char


testHomeEasyEncode

decompiled_code:
__int64 testHomeEasyEncode()
{
__int64 v0; // rbp
__int64 v1; // rdx
__int64 v2; // rbx

printf("\n=== %s ===\n", "testHomeEasyEncode");
printf("Input: %08X\n", 673776026LL);
printf("Should: 59 95 59 95 55 56 96 99\nGet: ");
v0 = homeEasyEncode(673776026LL);
v2 = v1;
printfByteBuffer(v0, v1);
return destroyByteBuffer(v0, v2);
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbp
__int64 v1; // rdx
__int64 v2; // rbx

printf("\n=== %s ===\n", "testHomeEasyEncode");
printf("Input: %08X\n", 673776026LL);
printf("Should: 59 95 59 95 55 56 96 99\nGet: ");
v0 = homeEasyEncode(673776026LL);
v2 = v1;
printfByteBuffer(v0, v1);
return destroyByteBuffer(v0, v2);
}

original_code:
void testHomeEasyEncode()
{
    BYTE_BUFFER source;
    BYTE_BUFFER encoded;
    unsigned long int frame = 0x2829019a;
    printf("\n=== %s ===\n", __FUNCTION__);
    printf("Input: %08X\n", frame);
    printf("Should: 59 95 59 95 55 56 96 99\nGet:    ");
    encoded = homeEasyEncode(frame);
    printfByteBuffer(encoded);
    destroyByteBuffer(encoded);
}

canonical_original_code:
void testHomeEasyEncode()
{
    struct BYTE_BUFFER source;
    struct BYTE_BUFFER encoded;
    unsigned long frame = 0x2829019a;
    printf("\n=== %s ===\n", __FUNCTION__);
    printf("Input: %08X\n", frame);
    printf("Should: 59 95 59 95 55 56 96 99\nGet:    ");
    encoded = homeEasyEncode(frame);
    printfByteBuffer(encoded);
    destroyByteBuffer(encoded);
}


testHomeEasyDecode

decompiled_code:
__int64 testHomeEasyDecode()
{
__int64 v0; // rdx
__int64 v1; // rax
__int64 v3; // [rsp+8h] [rbp-20h] BYREF
__int64 v4; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v3 = 0x559A565596599559LL;
printf("\n=== %s ===\n", "testHomeEasyDecode");
v4 = createByteBuffer();
v5 = v0;
pushBytes(&v4, &v3, 8LL);
printf("Input: ");
printfByteBuffer(v4, v5);
v1 = homeEasyDecode(&v4);
printf("Should: 282901B0\nGet: %08X\n", v1);
return destroyByteBuffer(v4, v5);
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rdx
__int64 v1; // rax
__int64 v3; // [rsp+8h] [rbp-20h] BYREF
__int64 v4; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v3 = 0x559A565596599559LL;
printf("\n=== %s ===\n", "testHomeEasyDecode");
v4 = createByteBuffer();
v5 = v0;
pushBytes(&v4, &v3, 8LL);
printf("Input: ");
printfByteBuffer(v4, v5);
v1 = homeEasyDecode(&v4);
printf("Should: 282901B0\nGet: %08X\n", v1);
return destroyByteBuffer(v4, v5);
}

original_code:
void testHomeEasyDecode()
{
    BYTE_BUFFER source;
    unsigned long int decoded;
    unsigned char bytes[] = {0x59, 0x95, 0x59, 0x96, 0x55, 0x56, 0x9A, 0x55};
    printf("\n=== %s ===\n", __FUNCTION__);
    source = createByteBuffer();
    pushBytes(&source, bytes, 8);
    printf("Input: ");
    printfByteBuffer(source);
    printf("Should: 282901B0\nGet:    %08X\n", homeEasyDecode(&source));
    destroyByteBuffer(source);
}

canonical_original_code:
void testHomeEasyDecode()
{
    struct BYTE_BUFFER source;
    unsigned long decoded;
    unsigned char bytes[] = {0x59, 0x95, 0x59, 0x96, 0x55, 0x56, 0x9A, 0x55};
    printf("\n=== %s ===\n", __FUNCTION__);
    source = createByteBuffer();
    pushBytes(&source, bytes, 8);
    printf("Input: ");
    printfByteBuffer(source);
    printf("Should: 282901B0\nGet:    %08X\n", homeEasyDecode(&source));
    destroyByteBuffer(source);
}

variable_types:
decoded
unsigned long

variable_types:
bytes
unsigned char []


testHomeEasyCommand

decompiled_code:
__int64 __fastcall testHomeEasyCommand(char a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v4; // r13
__int64 v5; // rbx
__int64 v6; // rbp
__int64 v7; // rdx
__int64 v8; // rbx

v4 = getHomeEasyId(673776026LL);
printf("\n=== %s ===\n", "testHomeEasyCommand");
v5 = createHomeEasyCommand(v4, (unsigned int)a1, a2, a3);
printf("%08X\n", v5);
v6 = homeEasyEncode(v5);
v8 = v7;
printfByteBuffer(v6, v7);
return destroyByteBuffer(v6, v8);
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v4; // r13
__int64 v5; // rbx
__int64 v6; // rbp
__int64 v7; // rdx
__int64 v8; // rbx

v4 = getHomeEasyId(673776026LL);
printf("\n=== %s ===\n", "testHomeEasyCommand");
v5 = createHomeEasyCommand(v4, (unsigned int)a1, a2, a3);
printf("%08X\n", v5);
v6 = homeEasyEncode(v5);
v8 = v7;
printfByteBuffer(v6, v7);
return destroyByteBuffer(v6, v8);
}

original_code:
void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
{
    unsigned long int command;
    BYTE_BUFFER encoded;
    unsigned long int id = getHomeEasyId(0x2829019a);
    printf("\n=== %s ===\n", __FUNCTION__);
    command = createHomeEasyCommand(id, section, num, onOff);
    printf("%08X\n", command);
    encoded = homeEasyEncode(command);
    printfByteBuffer(encoded);
    destroyByteBuffer(encoded);
}

canonical_original_code:
void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
{
    unsigned long command;
    struct BYTE_BUFFER encoded;
    unsigned long id = getHomeEasyId(0x2829019a);
    printf("\n=== %s ===\n", __FUNCTION__);
    command = createHomeEasyCommand(id, section, num, onOff);
    printf("%08X\n", command);
    encoded = homeEasyEncode(command);
    printfByteBuffer(encoded);
    destroyByteBuffer(encoded);
}

variable_types:
num
unsigned char

variable_types:
onOff
unsigned char

variable_types:
command
unsigned long

variable_types: struct BYTE_BUFFER { char *data; unsigned long size; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Test");
testByteBuffer("Test", argv);
testPrintBits();
testGetId();
testEncode();
testDecode();
testHomeEasyEncode();
testHomeEasyDecode();
testHomeEasyCommand(68, 4u, 0);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Test");
func0("Test", argv);
func1();
func2();
func3();
func4();
func5();
func6();
func7(68, 4u, 0);
return 0;
}

original_code:
int main()
{
    printf("Test\n");
    testByteBuffer();
    testPrintBits();
    testGetId();
    testEncode();
    testDecode();
    testHomeEasyEncode();
    testHomeEasyDecode();
    testHomeEasyCommand('D', 4, 0);
    return 0;
}


createByteBuffer

decompiled_code:
void *createByteBuffer()
{
return malloc(1uLL);
}

canonical_decompiled_code:
void *func0()
{
return malloc(1uLL);
}

original_code:
BYTE_BUFFER createByteBuffer()
{
    BYTE_BUFFER buffer;
    buffer.size = 0;
    buffer.data = (char*) malloc(1);
    return buffer;
}

canonical_original_code:
struct BYTE_BUFFER createByteBuffer()
{
    struct BYTE_BUFFER buffer;
    buffer.size = 0;
    buffer.data = (char *) malloc(1);
    return buffer;
}

return_type: struct BYTE_BUFFER { char *data; unsigned long size; }


destroyByteBuffer

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

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

original_code:
void destroyByteBuffer(BYTE_BUFFER buffer)
{
    free(buffer.data);
}

canonical_original_code:
void destroyByteBuffer(struct BYTE_BUFFER buffer)
{
    free(buffer.data);
}


printfBit

decompiled_code:
int __fastcall printfBit(char a1)
{
int v1; // ebp
int v2; // ebx
int result; // eax

v1 = 8;
v2 = 128;
do
{
result = fprintf(stdout, "%d", (unsigned __int8)(v2 & a1) == v2);
v2 >>= 1;
--v1;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func2(char a1)
{
int v1; // ebp
int v2; // ebx
int result; // eax

v1 = 8;
v2 = 128;
do
{
result = fprintf(stdout, "%d", (unsigned __int8)(v2 & a1) == v2);
v2 >>= 1;
--v1;
}
while ( v1 );
return result;
}

original_code:
void printfBit(unsigned char byte)
{
    int i;
    for(i=0x80; i>0; i>>=1) {
        fprintf(stdout, "%d", ((byte & i) == i));
    }
}

variable_types:
byte
unsigned char

variable_types: i int


printfBitBuffer

decompiled_code:
int __fastcall printfBitBuffer(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // ebx

LODWORD(v2) = printf("bytes: %lu\nbits: %lu\n", a2, 8 * a2);
if ( a2 )
{
v3 = 0;
v2 = 0LL;
do
{
printfBit(*(_BYTE *)(v2 + a1));
putchar(10);
v2 = ++v3;
}
while ( v3 < a2 );
}
return v2;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // ebx

LODWORD(v2) = printf("bytes: %lu\nbits: %lu\n", a2, 8 * a2);
if ( a2 )
{
v3 = 0;
v2 = 0LL;
do
{
func2(*(_BYTE *)(v2 + a1));
putchar(10);
v2 = ++v3;
}
while ( v3 < a2 );
}
return v2;
}

original_code:
void printfBitBuffer(BYTE_BUFFER buffer)
{
    printf("bytes: %lu\nbits: %lu\n", buffer.size, buffer.size*8);
    unsigned int x;
    for(x=0; x<buffer.size ; x++) {
        printfBit(buffer.data[x]);
        printf("\n");
    }
}

canonical_original_code:
void printfBitBuffer(struct BYTE_BUFFER buffer)
{
    printf("bytes: %lu\nbits: %lu\n", buffer.size, buffer.size*8);
    unsigned int x;
    for(x=0; x<buffer.size ; x++) {
        printfBit(buffer.data[x]);
        printf("\n");
    }
}

variable_types:
x
unsigned int


printfByteBuffer

decompiled_code:
int __fastcall printfByteBuffer(__int64 a1, unsigned __int64 a2)
{
unsigned int v2; // ebx
__int64 v3; // rax

if ( a2 )
{
v2 = 0;
v3 = 0LL;
do
{
fprintf(stdout, "%02X ", *(unsigned __int8 *)(v3 + a1));
v3 = ++v2;
}
while ( v2 < a2 );
}
return fputc(10, stdout);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, unsigned __int64 a2)
{
unsigned int v2; // ebx
__int64 v3; // rax

if ( a2 )
{
v2 = 0;
v3 = 0LL;
do
{
fprintf(stdout, "%02X ", *(unsigned __int8 *)(v3 + a1));
v3 = ++v2;
}
while ( v2 < a2 );
}
return fputc(10, stdout);
}

original_code:
void printfByteBuffer(BYTE_BUFFER buffer)
{
    unsigned int i;
    for(i=0; i<buffer.size; i++) {
        fprintf(stdout, "%02X ", (unsigned char)buffer.data[i]);
    }
    fprintf(stdout, "\n");
}

canonical_original_code:
void printfByteBuffer(struct BYTE_BUFFER buffer)
{
    unsigned int i;
    for(i=0; i<buffer.size; i++) {
        fprintf(stdout, "%02X ", (unsigned char)buffer.data[i]);
    }
    fprintf(stdout, "\n");
}

variable_types:
i
unsigned int


pushByte

decompiled_code:
char *__fastcall pushByte(__int64 a1, char a2)
{
size_t v3; // rsi
char *result; // rax

v3 = *(_QWORD *)(a1 + 8) + 1LL;
*(_QWORD *)(a1 + 8) = v3;
result = (char *)realloc(*(void **)a1, v3);
*(_QWORD *)a1 = result;
result[*(_QWORD *)(a1 + 8) - 1] = a2;
return result;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1, char a2)
{
size_t v3; // rsi
char *result; // rax

v3 = *(_QWORD *)(a1 + 8) + 1LL;
*(_QWORD *)(a1 + 8) = v3;
result = (char *)realloc(*(void **)a1, v3);
*(_QWORD *)a1 = result;
result[*(_QWORD *)(a1 + 8) - 1] = a2;
return result;
}

original_code:
void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
{
    buffer->size++;
    buffer->data = (char*)realloc(buffer->data, buffer->size);
    buffer->data[buffer->size-1] = byte;
}

canonical_original_code:
void pushByte(struct BYTE_BUFFER *buffer, unsigned char byte)
{
    buffer->size++;
    buffer->data = (char *)realloc(buffer->data, buffer->size);
    buffer->data[buffer->size-1] = byte;
}

variable_types: struct BYTE_BUFFER { char *data; unsigned long size; } *


pushWord

decompiled_code:
char *__fastcall pushWord(__int64 a1, __int16 a2)
{
pushByte(a1, SHIBYTE(a2));
return pushByte(a1, a2);
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1, __int16 a2)
{
func5(a1, SHIBYTE(a2));
return func5(a1, a2);
}

original_code:
void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
{
    unsigned char* data = (unsigned char*)&word;
    pushByte(buffer, data[1]);
    pushByte(buffer, data[0]);
}

canonical_original_code:
void pushWord(struct BYTE_BUFFER *buffer, unsigned short word)
{
    unsigned char *data = (unsigned char*)&word;
    pushByte(buffer, data[1]);
    pushByte(buffer, data[0]);
}

variable_types:
word
unsigned short

variable_types:
data
unsigned char *


pushBytes

decompiled_code:
void *__fastcall pushBytes(__int64 a1, const void *a2, unsigned int a3)
{
size_t v3; // rbp
char *v4; // rax
void *result; // rax

v3 = a3;
v4 = (char *)realloc(*(void **)a1, *(_QWORD *)(a1 + 8) + a3);
*(_QWORD *)a1 = v4;
result = memcpy(&v4[*(_QWORD *)(a1 + 8)], a2, v3);
*(_QWORD *)(a1 + 8) += v3;
return result;
}

canonical_decompiled_code:
void *__fastcall func7(__int64 a1, const void *a2, unsigned int a3)
{
size_t v3; // rbp
char *v4; // rax
void *result; // rax

v3 = a3;
v4 = (char *)realloc(*(void **)a1, *(_QWORD *)(a1 + 8) + a3);
*(_QWORD *)a1 = v4;
result = memcpy(&v4[*(_QWORD *)(a1 + 8)], a2, v3);
*(_QWORD *)(a1 + 8) += v3;
return result;
}

original_code:
void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
{
    buffer->data = (char*)realloc(buffer->data, buffer->size+len);
    memcpy(&buffer->data[buffer->size], byte, len);
    buffer->size += len;
}

canonical_original_code:
void pushBytes(struct BYTE_BUFFER *buffer, unsigned char *byte, unsigned int len)
{
    buffer->data = (char *)realloc(buffer->data, buffer->size+len);
    memcpy(&buffer->data[buffer->size], byte, len);
    buffer->size += len;
}

variable_types:
byte
unsigned char *

variable_types:
len
unsigned int


bitAt

decompiled_code:
__int64 __fastcall bitAt(__int64 a1, __int64 a2, unsigned __int64 a3)
{
return *(_BYTE *)((a3 >> 3) + a1) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, unsigned __int64 a3)
{
return *(_BYTE *)((a3 >> 3) + a1) & 1;
}

original_code:
unsigned int bitAt(BYTE_BUFFER buffer, unsigned long int n)
{
    unsigned char byte = buffer.data[n / 8];
    return (byte & (0x80 >> (n % 8)) != 0);
}

canonical_original_code:
unsigned int bitAt(struct BYTE_BUFFER buffer, unsigned long n)
{
    unsigned char byte = buffer.data[n / 8];
    return (byte & (0x80 >> (n % 8)) != 0);
}

variable_types:
n
unsigned long


showTime

decompiled_code:
struct timeval *__fastcall showTime(_QWORD *a1)
{
struct timeval *v1; // rbx
__suseconds_t v2; // rax
__suseconds_t v3; // rsi
__int64 v4; // r9

v1 = (struct timeval *)malloc(0x10uLL);
gettimeofday(v1, 0LL);
if ( a1 )
{
v2 = a1[1];
v3 = v1->tv_usec;
if ( v2 <= v3 )
{
v4 = v1->tv_sec - *a1;
}
else
{
v4 = v1->tv_sec - *a1 - 1;
v3 += 1000000LL;
}
fprintf(stderr, format, v4, (v3 - v2) / 1000, v4 % 1000);
}
return v1;
}

canonical_decompiled_code:
struct timeval *__fastcall func0(_QWORD *a1)
{
struct timeval *v1; // rbx
__suseconds_t v2; // rax
__suseconds_t v3; // rsi
__int64 v4; // r9

v1 = (struct timeval *)malloc(0x10uLL);
gettimeofday(v1, 0LL);
if ( a1 )
{
v2 = a1[1];
v3 = v1->tv_usec;
if ( v2 <= v3 )
{
v4 = v1->tv_sec - *a1;
}
else
{
v4 = v1->tv_sec - *a1 - 1;
v3 += 1000000LL;
}
fprintf(stderr, format, v4, (v3 - v2) / 1000, v4 % 1000);
}
return v1;
}

original_code:
struct timeval* showTime(struct timeval* start)
{
    struct timeval* stop;
    long tv_sec;
    long tv_usec;
    stop = (struct timeval*)malloc(sizeof(struct timeval));
    gettimeofday(stop, 0);
    if (!start) return stop;
    tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
    tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
    fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
    return stop;
}

canonical_original_code:
struct timeval *showTime(struct timeval *start)
{
    struct timeval *stop;
    long tv_sec;
    long tv_usec;
    stop = (struct timeval *)malloc(sizeof(struct timeval));
    gettimeofday(stop, 0);
    if (!start) return stop;
    tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
    tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
    fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
    return stop;
}

variable_types:
start
struct timeval *

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


cpuMode

decompiled_code:
void __fastcall cpuMode(int a1)
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 )
{
param.__sched_priority = 0;
if ( sched_setscheduler(0, 0, &param) == -1 )
perror("Failed to switch to normal scheduler.");
}
else
{
param.__sched_priority = sched_get_priority_max(2);
if ( sched_setscheduler(0, 2, &param) == -1 )
perror("Failed to switch to realtime scheduler.");
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 )
{
param.__sched_priority = 0;
if ( sched_setscheduler(0, 0, &param) == -1 )
perror("Failed to switch to normal scheduler.");
}
else
{
param.__sched_priority = sched_get_priority_max(2);
if ( sched_setscheduler(0, 2, &param) == -1 )
perror("Failed to switch to realtime scheduler.");
}
}

original_code:
void cpuMode(int mode)
{
    struct sched_param p;
    switch (mode) {
        case 0:
            p.sched_priority = sched_get_priority_max(2);
            if( sched_setscheduler( 0, 2, &p ) == -1 ) {
                perror("Failed to switch to realtime scheduler.");
            }
            break;
        case 1:
        default:
            p.sched_priority = 0;
            if( sched_setscheduler( 0, 0, &p ) == -1 ) {
                perror("Failed to switch to normal scheduler.");
            }
            break;
    }
}

variable_types: mode int

variable_types: struct sched_param { int sched_priority; }


repo: lantus/ROTT

original_code:
unsigned long PITCH_GetScale
   (
   int pitchoffset
   )
   {
   unsigned long scale;
   int octaveshift;
   int noteshift;
   int note;
   int detune;
   if ( pitchoffset == 0 )
      {
      return( PitchTable[ 0 ][ 0 ] );
      }
   noteshift = pitchoffset % 1200;
   if ( noteshift < 0 )
      {
      noteshift += 1200;
      }
   note = noteshift / 100;
   detune = ( noteshift % 100 ) / ( 100 / 25 );
   octaveshift = ( pitchoffset - noteshift ) / 1200;
   if ( detune < 0 )
      {
      detune += ( 100 / 25 );
      note--;
      if ( note < 0 )
         {
         note += 12;
         octaveshift--;
         }
      }
   scale = PitchTable[ note ][ detune ];
   if ( octaveshift < 0 )
      {
      scale >>= -octaveshift;
      }
   else
      {
      scale <<= octaveshift;
      }
   return( scale );
   }

canonical_original_code:
unsigned long PITCH_GetScale(int pitchoffset)
   {
   unsigned long scale;
   int octaveshift;
   int noteshift;
   int note;
   int detune;
   if ( pitchoffset == 0 )
      {
      return( PitchTable[ 0 ][ 0 ] );
      }
   noteshift = pitchoffset % 1200;
   if ( noteshift < 0 )
      {
      noteshift += 1200;
      }
   note = noteshift / 100;
   detune = ( noteshift % 100 ) / ( 100 / 25 );
   octaveshift = ( pitchoffset - noteshift ) / 1200;
   if ( detune < 0 )
      {
      detune += ( 100 / 25 );
      note--;
      if ( note < 0 )
         {
         note += 12;
         octaveshift--;
         }
      }
   scale = PitchTable[ note ][ detune ];
   if ( octaveshift < 0 )
      {
      scale >>= -octaveshift;
      }
   else
      {
      scale <<= octaveshift;
      }
   return( scale );
   }

variable_types: pitchoffset int

variable_types:
scale
unsigned long

variable_types: octaveshift int

variable_types: noteshift int

variable_types: note int

variable_types: detune int


decompiled_code:
void PITCH_LockEnd()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static void PITCH_LockEnd
   (
   void
   )
   {
   }

canonical_original_code:
static void PITCH_LockEnd()
   {
   }


decompiled_code:
__int64 PITCH_UnlockMemory()
{
DPMI_UnlockMemoryRegion(PITCH_GetScale, PITCH_LockEnd);
return DPMI_UnlockMemory(PitchTable, 2400LL);
}

canonical_decompiled_code:
__int64 func2()
{
DPMI_UnlockMemoryRegion(PITCH_GetScale, PITCH_LockEnd);
return DPMI_UnlockMemory(PitchTable, 2400LL);
}

original_code:
void PITCH_UnlockMemory
   (
   void
   )
   {
   DPMI_UnlockMemoryRegion( PITCH_GetScale, PITCH_LockEnd );
   ( DPMI_UnlockMemory( (void *) &( PitchTable ), sizeof( PitchTable ) ) );
   }

canonical_original_code:
void PITCH_UnlockMemory()
   {
   DPMI_UnlockMemoryRegion( PITCH_GetScale, PITCH_LockEnd );
   ( DPMI_UnlockMemory( (void *) &( PitchTable ), sizeof( PitchTable ) ) );
   }