enableGC

decompiled_code:
void __noreturn enableGC()
{
fwrite(
"Attempting to enable garbage collection, but yourObjective-C runtime was built without garbage collectionsupport\n",
1uLL,
0x71uLL,
stderr);
abort();
}

canonical_decompiled_code:
void __noreturn func3()
{
fwrite(
"Attempting to enable garbage collection, but yourObjective-C runtime was built without garbage collectionsupport\n",
1uLL,
0x71uLL,
stderr);
abort();
}

original_code:
__attribute__ ((visibility("hidden"))) void enableGC(BOOL exclusive)
{
 fprintf(stderr, "Attempting to enable garbage collection, but your"
   "Objective-C runtime was built without garbage collection"
   "support\n");
 abort();
}

canonical_original_code:
void enableGC(unsigned char exclusive)
{
 fprintf(stderr, "Attempting to enable garbage collection, but your"
   "Objective-C runtime was built without garbage collection"
   "support\n");
 abort();
}

variable_types:
exclusive
unsigned char


decompiled_code:
void __fastcall unlock_queue(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

original_code:
inline static void unlock_queue(dispatch_queue_t queue)
{
 queue->spinlock = 0;
}

canonical_original_code:
inline static void unlock_queue(struct dispatch_queue *queue)
{
 queue->spinlock = 0;
}

variable_types: struct dispatch_queue { int refcount; int spinlock; unsigned int producer; unsigned int consumer; 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; } 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; } conditionVariable; struct { void (*function)(void *); void *data; } ring_buffer[32]; } *

return_type: void

user_defined_types: struct dispatch_queue { int refcount; int spinlock; unsigned int producer; unsigned int consumer; union pthread_mutex_t mutex; union pthread_cond_t conditionVariable; struct { void (*function)(void *); void *data; } ring_buffer[32]; }

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


decompiled_code:
__int64 __fastcall read_from_queue(__int64 a1, _QWORD *a2, _QWORD *a3)
{
int v4; // eax
pthread_mutex_t *v5; // r13
__int64 v6; // rax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(a1 + 8) == v4 )
{
v5 = (pthread_mutex_t *)(a1 + 16);
do
{
pthread_mutex_lock(v5);
if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(a1 + 12) )
pthread_cond_wait((pthread_cond_t *)(a1 + 56), v5);
pthread_mutex_unlock(v5);
v4 = *(_DWORD *)(a1 + 12);
}
while ( *(_DWORD *)(a1 + 8) == v4 );
}
v6 = a1 + 16LL * (v4 & 0x1F);
*a2 = *(_QWORD *)(v6 + 104);
result = *(_QWORD *)(v6 + 112);
*a3 = result;
_InterlockedAdd((volatile signed __int32 *)(a1 + 12), 1u);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _QWORD *a2, _QWORD *a3)
{
int v4; // eax
pthread_mutex_t *v5; // r13
__int64 v6; // rax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(a1 + 8) == v4 )
{
v5 = (pthread_mutex_t *)(a1 + 16);
do
{
pthread_mutex_lock(v5);
if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(a1 + 12) )
pthread_cond_wait((pthread_cond_t *)(a1 + 56), v5);
pthread_mutex_unlock(v5);
v4 = *(_DWORD *)(a1 + 12);
}
while ( *(_DWORD *)(a1 + 8) == v4 );
}
v6 = a1 + 16LL * (v4 & 0x1F);
*a2 = *(_QWORD *)(v6 + 104);
result = *(_QWORD *)(v6 + 112);
*a3 = result;
_InterlockedAdd((volatile signed __int32 *)(a1 + 12), 1u);
return result;
}

original_code:
static inline void read_from_queue(dispatch_queue_t queue,
  dispatch_function_t *function, void **data)
{
 while (((queue->producer - queue->consumer) == 0))
 {
  pthread_mutex_lock(&queue->mutex);
  if (((queue->producer - queue->consumer) == 0))
  {
   pthread_cond_wait(&queue->conditionVariable, &queue->mutex);
  }
  pthread_mutex_unlock(&queue->mutex);
 }
 unsigned int idx = ((queue->consumer) & (32 - 1));
 *function = queue->ring_buffer[idx].function;
 *data = queue->ring_buffer[idx].data;
 __sync_fetch_and_add(&queue->consumer, 1);
}

canonical_original_code:
static inline void read_from_queue(struct dispatch_queue *queue, void (**function)(void *), void **data)
{
 while (((queue->producer - queue->consumer) == 0))
 {
  pthread_mutex_lock(&queue->mutex);
  if (((queue->producer - queue->consumer) == 0))
  {
   pthread_cond_wait(&queue->conditionVariable, &queue->mutex);
  }
  pthread_mutex_unlock(&queue->mutex);
 }
 unsigned int idx = ((queue->consumer) & (32 - 1));
 *function = queue->ring_buffer[idx].function;
 *data = queue->ring_buffer[idx].data;
 __sync_fetch_and_add(&queue->consumer, 1);
}

variable_types:
function
void (**)(void *)

variable_types:
data
void **

variable_types:
idx
unsigned int

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


runloop

decompiled_code:
void *__fastcall runloop(void *a1)
{
__int64 v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3[4]; // [rsp+8h] [rbp-20h] BYREF

while ( *(int *)a1 > 0 )
{
read_from_queue((__int64)a1, v3, &v2);
((void (__fastcall *)(__int64))v3[0])(v2);
}
pthread_cond_destroy((pthread_cond_t *)((char *)a1 + 56));
pthread_mutex_destroy((pthread_mutex_t *)((char *)a1 + 16));
free(a1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1)
{
__int64 v2; // [rsp+0h] [rbp-28h] BYREF
__int64 v3[4]; // [rsp+8h] [rbp-20h] BYREF

while ( *(int *)a1 > 0 )
{
func2((__int64)a1, v3, &v2);
((void (__fastcall *)(__int64))v3[0])(v2);
}
pthread_cond_destroy((pthread_cond_t *)((char *)a1 + 56));
pthread_mutex_destroy((pthread_mutex_t *)((char *)a1 + 16));
free(a1);
return 0LL;
}

original_code:
static void *runloop(void *q)
{
 dispatch_queue_t queue = q;
 dispatch_function_t function;
 void *data;
 while (queue->refcount > 0)
 {
  read_from_queue(queue, &function, &data);
  function(data);
 }
 pthread_cond_destroy(&queue->conditionVariable);
 pthread_mutex_destroy(&queue->mutex);
 free(queue);
 return ((void *)0);
}

canonical_original_code:
static void *runloop(void *q)
{
 struct dispatch_queue *queue = q;
 void (*function)(void *);
 void *data;
 while (queue->refcount > 0)
 {
  read_from_queue(queue, &function, &data);
  function(data);
 }
 pthread_cond_destroy(&queue->conditionVariable);
 pthread_mutex_destroy(&queue->mutex);
 free(queue);
 return ((void *)0);
}

variable_types:
q
void *

variable_types:
function
void (*)(void *)

variable_types:
data
void *

return_type: void *


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

while ( 1 )
{
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)(a1 + 4), 1, 0);
if ( !(_DWORD)result )
break;
sched_yield();
}
return result;
}

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

while ( 1 )
{
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)(a1 + 4), 1, 0);
if ( !(_DWORD)result )
break;
sched_yield();
}
return result;
}

original_code:
inline static void lock_queue(dispatch_queue_t queue)
{
 while(!__sync_bool_compare_and_swap(&queue->spinlock, 0, 1))
 {
  sched_yield();
 }
}

canonical_original_code:
inline static void lock_queue(struct dispatch_queue *queue)
{
 while(!__sync_bool_compare_and_swap(&queue->spinlock, 0, 1))
 {
  sched_yield();
 }
}


decompiled_code:
void __fastcall insert_into_queue(__int64 a1, __int64 a2, __int64 a3)
{
int i; // edx
__int64 v5; // rdx
int v6; // ebp

lock_queue(a1);
for ( i = *(_DWORD *)(a1 + 8); *(_DWORD *)(a1 + 12) - i == -32; i = *(_DWORD *)(a1 + 8) )
sched_yield();
v5 = a1 + 16LL * (i & 0x1F);
*(_QWORD *)(v5 + 104) = a2;
*(_QWORD *)(v5 + 112) = a3;
_InterlockedAdd((volatile signed __int32 *)(a1 + 8), 1u);
v6 = *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 12);
unlock_queue(a1);
if ( v6 == 1 )
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
pthread_cond_signal((pthread_cond_t *)(a1 + 56));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, __int64 a3)
{
int i; // edx
__int64 v5; // rdx
int v6; // ebp

func4(a1);
for ( i = *(_DWORD *)(a1 + 8); *(_DWORD *)(a1 + 12) - i == -32; i = *(_DWORD *)(a1 + 8) )
sched_yield();
v5 = a1 + 16LL * (i & 0x1F);
*(_QWORD *)(v5 + 104) = a2;
*(_QWORD *)(v5 + 112) = a3;
_InterlockedAdd((volatile signed __int32 *)(a1 + 8), 1u);
v6 = *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 12);
func0(a1);
if ( v6 == 1 )
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
pthread_cond_signal((pthread_cond_t *)(a1 + 56));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}
}

original_code:
inline static void insert_into_queue(dispatch_queue_t queue,
  dispatch_function_t function,
  void *data)
{
 lock_queue(queue);
 while (((32 - (queue->producer - queue->consumer)) == 0))
 {
  sched_yield();
 }
 unsigned int idx = ((queue->producer) & (32 - 1));
 queue->ring_buffer[idx].function = function;
 queue->ring_buffer[idx].data = data;
 __sync_fetch_and_add(&queue->producer, 1);
 unsigned int space = queue->producer - queue->consumer;
 unlock_queue(queue);
 if (space == 1)
 {
  pthread_mutex_lock(&queue->mutex);
  pthread_cond_signal(&queue->conditionVariable);
  pthread_mutex_unlock(&queue->mutex);
 }
}

canonical_original_code:
inline static void insert_into_queue(struct dispatch_queue *queue, void (*function)(void *), void *data)
{
 lock_queue(queue);
 while (((32 - (queue->producer - queue->consumer)) == 0))
 {
  sched_yield();
 }
 unsigned int idx = ((queue->producer) & (32 - 1));
 queue->ring_buffer[idx].function = function;
 queue->ring_buffer[idx].data = data;
 __sync_fetch_and_add(&queue->producer, 1);
 unsigned int space = queue->producer - queue->consumer;
 unlock_queue(queue);
 if (space == 1)
 {
  pthread_mutex_lock(&queue->mutex);
  pthread_cond_signal(&queue->conditionVariable);
  pthread_mutex_unlock(&queue->mutex);
 }
}

variable_types:
space
unsigned int


decompiled_code:
char *toy_dispatch_queue_create()
{
char *v0; // rbx
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)calloc(1uLL, 0x268uLL);
*(_DWORD *)v0 = 1;
pthread_cond_init((pthread_cond_t *)(v0 + 56), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
pthread_create(newthread, 0LL, runloop, v0);
pthread_detach(newthread[0]);
return v0;
}

canonical_decompiled_code:
char *func6()
{
char *v0; // rbx
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)calloc(1uLL, 0x268uLL);
*(_DWORD *)v0 = 1;
pthread_cond_init((pthread_cond_t *)(v0 + 56), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
pthread_create(newthread, 0LL, runloop, v0);
pthread_detach(newthread[0]);
return v0;
}

original_code:
dispatch_queue_t toy_dispatch_queue_create(const char *label,
  void *attr)
{
 dispatch_queue_t queue = calloc(1, sizeof(struct dispatch_queue));
 queue->refcount = 1;
 pthread_cond_init(&queue->conditionVariable, ((void *)0));
 pthread_mutex_init(&queue->mutex, ((void *)0));
 pthread_t thread;
 pthread_create(&thread, ((void *)0), runloop, queue);
 pthread_detach(thread);
 return queue;
}

canonical_original_code:
struct dispatch_queue *toy_dispatch_queue_create(char *label, void *attr)
{
 struct dispatch_queue *queue = calloc(1, sizeof(struct dispatch_queue));
 queue->refcount = 1;
 pthread_cond_init(&queue->conditionVariable, ((void *)0));
 pthread_mutex_init(&queue->mutex, ((void *)0));
 unsigned long thread;
 pthread_create(&thread, ((void *)0), runloop, queue);
 pthread_detach(thread);
 return queue;
}

variable_types:
label
char *

variable_types:
attr
void *

variable_types:
thread
unsigned long

return_type: struct dispatch_queue { int refcount; int spinlock; unsigned int producer; unsigned int consumer; 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; } 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; } conditionVariable; struct { void (*function)(void *); void *data; } ring_buffer[32]; } *


decompiled_code:
void __fastcall toy_dispatch_async_f(__int64 a1, __int64 a2, __int64 a3)
{
insert_into_queue(a1, a3, a2);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
func5(a1, a3, a2);
}

original_code:
void toy_dispatch_async_f(dispatch_queue_t queue, void *context,
  dispatch_function_t work)
{
 insert_into_queue(queue, work, context);
}

canonical_original_code:
void toy_dispatch_async_f(struct dispatch_queue *queue, void *context, void (*work)(void *))
{
 insert_into_queue(queue, work, context);
}

variable_types:
context
void *

variable_types:
work
void (*)(void *)


decompiled_code:
void __fastcall toy_dispatch_release(__int64 a1)
{
insert_into_queue(a1, (__int64)release, a1);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
func5(a1, (__int64)release, a1);
}

original_code:
void toy_dispatch_release(dispatch_queue_t queue)
{
 insert_into_queue(queue, release, queue);
}

canonical_original_code:
void toy_dispatch_release(struct dispatch_queue *queue)
{
 insert_into_queue(queue, release, queue);
}


SparseArrayFind

decompiled_code:
__int64 __fastcall SparseArrayFind(__int64 a1, int *a2)
{
int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // esi
__int64 result; // rax
int v8; // er13
__int64 v9; // rbx
unsigned __int64 v10; // r14
__int64 v11; // rdi

v4 = *(_DWORD *)(a1 + 4);
v5 = ((unsigned int)*a2 & *(_DWORD *)a1) >> v4;
v6 = *(_DWORD *)a1 >> v4;
if ( v4 )
{
result = 0LL;
if ( v5 < v6 )
{
v8 = ~(*(_DWORD *)a1 >> 8);
v9 = 8LL * v5;
v10 = 8 * (v5 + (unsigned __int64)(v6 - 1 - v5) + 1);
do
{
v11 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + v9);
if ( v11 )
{
result = SparseArrayFind(v11, a2);
if ( result )
return result;
}
else
{
*a2 = v8 & (*a2 + (1 << *(_DWORD *)(a1 + 4)));
}
v9 += 8LL;
}
while ( v9 != v10 );
result = 0LL;
}
}
else if ( v5 > v6 )
{
result = 0LL;
}
else
{
do
{
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5);
if ( result )
break;
++*a2;
++v5;
}
while ( v6 >= v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int *a2)
{
int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // esi
__int64 result; // rax
int v8; // er13
__int64 v9; // rbx
unsigned __int64 v10; // r14
__int64 v11; // rdi

v4 = *(_DWORD *)(a1 + 4);
v5 = ((unsigned int)*a2 & *(_DWORD *)a1) >> v4;
v6 = *(_DWORD *)a1 >> v4;
if ( v4 )
{
result = 0LL;
if ( v5 < v6 )
{
v8 = ~(*(_DWORD *)a1 >> 8);
v9 = 8LL * v5;
v10 = 8 * (v5 + (unsigned __int64)(v6 - 1 - v5) + 1);
do
{
v11 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + v9);
if ( v11 )
{
result = func0(v11, a2);
if ( result )
return result;
}
else
{
*a2 = v8 & (*a2 + (1 << *(_DWORD *)(a1 + 4)));
}
v9 += 8LL;
}
while ( v9 != v10 );
result = 0LL;
}
}
else if ( v5 > v6 )
{
result = 0LL;
}
else
{
do
{
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5);
if ( result )
break;
++*a2;
++v5;
}
while ( v6 >= v5 );
}
return result;
}

original_code:
static void *SparseArrayFind(SparseArray * sarray, uint32_t * index)
{
 uint32_t j = (((*index) & sarray->mask) >> sarray->shift);
 uint32_t max = (sarray->mask >> sarray->shift);
 if (sarray->shift == 0)
 {
  while (j<=max)
  {
   if (sarray->data[j] != ((void*)0))
   {
    return sarray->data[j];
   }
   (*index)++;
   j++;
  }
 }
 else while (j<max)
 {
  uint32_t zeromask = ~(sarray->mask >> 8);
  while (j<max)
  {
   if (sarray->data[j] != ((void*)0))
   {
    void * ret = SparseArrayFind(sarray->data[j], index);
    if (ret != ((void*)0))
    {
     return ret;
    }
   }
   else
   {
    (*index) += 1<<sarray->shift;
    *index &= zeromask;
   }
   j++;
  }
 }
 return ((void*)0);
}

canonical_original_code:
static void *SparseArrayFind(struct SparseArray *sarray, unsigned int *index)
{
 unsigned int j = (((*index) & sarray->mask) >> sarray->shift);
 unsigned int max = (sarray->mask >> sarray->shift);
 if (sarray->shift == 0)
 {
  while (j<=max)
  {
   if (sarray->data[j] != ((void *)0))
   {
    return sarray->data[j];
   }
   (*index)++;
   j++;
  }
 }
 else while (j<max)
 {
  unsigned int zeromask = ~(sarray->mask >> 8);
  while (j<max)
  {
   if (sarray->data[j] != ((void *)0))
   {
    void *ret = SparseArrayFind(sarray->data[j], index);
    if (ret != ((void *)0))
    {
     return ret;
    }
   }
   else
   {
    (*index) += 1<<sarray->shift;
    *index &= zeromask;
   }
   j++;
  }
 }
 return ((void *)0);
}

variable_types: struct SparseArray { unsigned int mask; unsigned int shift; unsigned int refCount; void **data; } *

variable_types:
index
unsigned int *

variable_types:
j
unsigned int

variable_types:
max
unsigned int

variable_types:
zeromask
unsigned int

variable_types:
ret
void *


decompiled_code:
unsigned __int64 __fastcall init_pointers(__int64 a1)
{
int v1; // ebp
unsigned __int64 result; // rax

v1 = *(_DWORD *)(a1 + 4);
result = (unsigned __int64)calloc((unsigned int)((*(_DWORD *)a1 >> v1) + 1), 8uLL);
*(_QWORD *)(a1 + 16) = result;
if ( v1 )
{
LODWORD(result) = 0;
do
{
*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (unsigned int)result) = &EmptyArray;
result = (unsigned int)(result + 1);
}
while ( *(_DWORD *)a1 >> *(_DWORD *)(a1 + 4) >= (unsigned int)result );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1)
{
int v1; // ebp
unsigned __int64 result; // rax

v1 = *(_DWORD *)(a1 + 4);
result = (unsigned __int64)calloc((unsigned int)((*(_DWORD *)a1 >> v1) + 1), 8uLL);
*(_QWORD *)(a1 + 16) = result;
if ( v1 )
{
LODWORD(result) = 0;
do
{
*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (unsigned int)result) = &EmptyArray;
result = (unsigned int)(result + 1);
}
while ( *(_DWORD *)a1 >> *(_DWORD *)(a1 + 4) >= (unsigned int)result );
}
return result;
}

original_code:
static void init_pointers(SparseArray * sarray)
{
 sarray->data = calloc(((sarray->mask >> sarray->shift) + 1), sizeof(void*));
 if(sarray->shift != 0)
 {
  for(unsigned i=0 ; i<=(sarray->mask >> sarray->shift) ; i++)
  {
   sarray->data[i] = &EmptyArray;
  }
 }
}

canonical_original_code:
static void init_pointers(struct SparseArray *sarray)
{
 sarray->data = calloc(((sarray->mask >> sarray->shift) + 1), sizeof(void *));
 if(sarray->shift != 0)
 {
  for(unsigned int i = 0; i<=(sarray->mask >> sarray->shift) ; i++)
  {
   sarray->data[i] = &EmptyArray;
  }
 }
}

variable_types:
i
unsigned int

user_defined_types: struct SparseArray { unsigned int mask; unsigned int shift; unsigned int refCount; void **data; }


SparseArrayNew

decompiled_code:
_DWORD *SparseArrayNew()
{
return SparseArrayNewWithDepth(32);
}

canonical_decompiled_code:
_DWORD *func2()
{
return SparseArrayNewWithDepth(32);
}

original_code:
__attribute__ ((visibility("hidden"))) SparseArray *SparseArrayNew()
{
 return SparseArrayNewWithDepth(32);
}

canonical_original_code:
struct SparseArray *SparseArrayNew()
{
 return SparseArrayNewWithDepth(32);
}

return_type: struct SparseArray { unsigned int mask; unsigned int shift; unsigned int refCount; void **data; } *


SparseArrayNext

decompiled_code:
__int64 __fastcall SparseArrayNext(__int64 a1, int *a2)
{
++*a2;
return SparseArrayFind(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int *a2)
{
++*a2;
return func0(a1, a2);
}

original_code:
__attribute__ ((visibility("hidden"))) void *SparseArrayNext(SparseArray * sarray, uint32_t * idx)
{
 (*idx)++;
 return SparseArrayFind(sarray, idx);
}

canonical_original_code:
void *SparseArrayNext(struct SparseArray *sarray, unsigned int *idx)
{
 (*idx)++;
 return SparseArrayFind(sarray, idx);
}

variable_types:
idx
unsigned int *


SparseArrayDestroy

decompiled_code:
void __fastcall SparseArrayDestroy(volatile signed __int32 *a1)
{
int v1; // ecx
unsigned __int32 v2; // eax
__int64 v3; // r12
__int64 v4; // rbp

if ( a1 != (volatile signed __int32 *)&EmptyArray && _InterlockedExchangeAdd(a1 + 2, 0xFFFFFFFF) == 1 )
{
v1 = *((_DWORD *)a1 + 1);
if ( v1 )
{
v2 = (unsigned __int32)*a1 >> v1;
if ( v2 != -1 )
{
v3 = 8LL * v2 + 8;
v4 = 0LL;
do
{
SparseArrayDestroy(*(_QWORD *)(*((_QWORD *)a1 + 2) + v4));
v4 += 8LL;
}
while ( v4 != v3 );
}
}
free(*((void **)a1 + 2));
free((void *)a1);
}
}

canonical_decompiled_code:
void __fastcall func4(volatile signed __int32 *a1)
{
int v1; // ecx
unsigned __int32 v2; // eax
__int64 v3; // r12
__int64 v4; // rbp

if ( a1 != (volatile signed __int32 *)&EmptyArray && _InterlockedExchangeAdd(a1 + 2, 0xFFFFFFFF) == 1 )
{
v1 = *((_DWORD *)a1 + 1);
if ( v1 )
{
v2 = (unsigned __int32)*a1 >> v1;
if ( v2 != -1 )
{
v3 = 8LL * v2 + 8;
v4 = 0LL;
do
{
func4(*(_QWORD *)(*((_QWORD *)a1 + 2) + v4));
v4 += 8LL;
}
while ( v4 != v3 );
}
}
free(*((void **)a1 + 2));
free((void *)a1);
}
}

original_code:
__attribute__ ((visibility("hidden"))) void SparseArrayDestroy(SparseArray * sarray)
{
 if (sarray == &EmptyArray ||
  (__sync_sub_and_fetch(&sarray->refCount, 1) > 0))
  {
  return;
 }
 if(sarray->shift > 0)
 {
  uint32_t max = (sarray->mask >> sarray->shift) + 1;
  for(uint32_t i=0 ; i<max ; i++)
  {
   SparseArrayDestroy((SparseArray*)sarray->data[i]);
  }
 }
 free(sarray->data);
 free(sarray);
}

canonical_original_code:
void SparseArrayDestroy(struct SparseArray *sarray)
{
 if (sarray == &EmptyArray ||
  (__sync_sub_and_fetch(&sarray->refCount, 1) > 0))
  {
  return;
 }
 if(sarray->shift > 0)
 {
  unsigned int max = (sarray->mask >> sarray->shift) + 1;
  for(unsigned int i = 0; i<max ; i++)
  {
   SparseArrayDestroy((struct SparseArray *)sarray->data[i]);
  }
 }
 free(sarray->data);
 free(sarray);
}


SparseArrayInsert

decompiled_code:
__int64 __fastcall SparseArrayInsert(int *a1, unsigned int a2, __int64 a3)
{
unsigned int v3; // er13
__int64 v4; // r14
int **v5; // r15
int *v6; // r12
__int64 result; // rax
unsigned int v8; // eax
unsigned int v10; // [rsp+Ch] [rbp-3Ch]

v3 = a1[1];
if ( v3 )
{
v10 = *a1;
v4 = (a2 & *a1) >> v3;
v5 = (int **)(*((_QWORD *)a1 + 2) + 8 * v4);
v6 = *v5;
if ( *v5 == (int *)&EmptyArray )
{
v6 = (int *)calloc(1uLL, 0x18uLL);
v6[2] = 1;
v8 = v3 - 8;
if ( v3 <= 8 )
v8 = 0;
v6[1] = v8;
*v6 = v10 >> 8;
init_pointers((__int64)v6);
*(_QWORD *)(*((_QWORD *)a1 + 2) + 8 * v4) = v6;
}
else if ( (unsigned int)v6[2] > 1 )
{
*v5 = SparseArrayCopy(*v5);
SparseArrayDestroy(v6);
v6 = *(int **)(*((_QWORD *)a1 + 2) + 8 * v4);
}
result = SparseArrayInsert(v6, a2, a3);
}
else
{
result = *((_QWORD *)a1 + 2);
*(_QWORD *)(result + 8LL * (*a1 & a2)) = a3;
}
return result;
}

original_code:
__attribute__ ((visibility("hidden"))) void SparseArrayInsert(SparseArray * sarray, uint32_t index, void *value)
{
 if (sarray->shift > 0)
 {
  uint32_t i = ((index & sarray->mask) >> sarray->shift);
  SparseArray *child = sarray->data[i];
  if(&EmptyArray == child)
  {
   SparseArray * newsarray = calloc(1, sizeof(SparseArray));
   newsarray->refCount = 1;
   if (8 >= sarray->shift)
   {
    newsarray->shift = 0;
   }
   else
   {
    newsarray->shift = sarray->shift - 8;
   }
   newsarray->mask = sarray->mask >> 8;
   init_pointers(newsarray);
   sarray->data[i] = newsarray;
   child = newsarray;
  }
  else if (child->refCount > 1)
  {
   sarray->data[i] = SparseArrayCopy(child);
   SparseArrayDestroy(child);
   child = sarray->data[i];
  }
  SparseArrayInsert(child, index, value);
 }
 else
 {
  sarray->data[index & sarray->mask] = value;
 }
}

canonical_original_code:
void SparseArrayInsert(struct SparseArray *sarray, unsigned int index, void *value)
{
 if (sarray->shift > 0)
 {
  unsigned int i = ((index & sarray->mask) >> sarray->shift);
  struct SparseArray *child = sarray->data[i];
  if(&EmptyArray == child)
  {
   struct SparseArray *newsarray = calloc(1, sizeof(SparseArray));
   newsarray->refCount = 1;
   if (8 >= sarray->shift)
   {
    newsarray->shift = 0;
   }
   else
   {
    newsarray->shift = sarray->shift - 8;
   }
   newsarray->mask = sarray->mask >> 8;
   init_pointers(newsarray);
   sarray->data[i] = newsarray;
   child = newsarray;
  }
  else if (child->refCount > 1)
  {
   sarray->data[i] = SparseArrayCopy(child);
   SparseArrayDestroy(child);
   child = sarray->data[i];
  }
  SparseArrayInsert(child, index, value);
 }
 else
 {
  sarray->data[index & sarray->mask] = value;
 }
}

variable_types:
index
unsigned int

variable_types:
value
void *


decompiled_code:
void *__fastcall objc_atomic_malloc(size_t a1)
{
return malloc(a1);
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
return malloc(a1);
}

original_code:
void *objc_atomic_malloc(size_t size)
{
 return malloc(size);
}

canonical_original_code:
void *objc_atomic_malloc(unsigned long size)
{
 return malloc(size);
}

variable_types:
size
unsigned long


decompiled_code:
void *__fastcall objc_valloc(size_t a1)
{
return valloc(a1);
}

canonical_decompiled_code:
void *__fastcall func2(size_t a1)
{
return valloc(a1);
}

original_code:
void *objc_valloc(size_t size)
{
 return valloc(size);
}

canonical_original_code:
void *objc_valloc(unsigned long size)
{
 return valloc(size);
}


decompiled_code:
void *__fastcall objc_realloc(void *a1, size_t a2)
{
return realloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func3(void *a1, size_t a2)
{
return realloc(a1, a2);
}

original_code:
void *objc_realloc(void *mem, size_t size)
{
 return realloc(mem, size);
}

canonical_original_code:
void *objc_realloc(void *mem, unsigned long size)
{
 return realloc(mem, size);
}

variable_types:
mem
void *


decompiled_code:
void *__fastcall objc_calloc(size_t a1, size_t a2)
{
return calloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func4(size_t a1, size_t a2)
{
return calloc(a1, a2);
}

original_code:
void * objc_calloc(size_t nelem, size_t size)
{
 return calloc(nelem, size);
}

canonical_original_code:
void *objc_calloc(unsigned long nelem, unsigned long size)
{
 return calloc(nelem, size);
}

variable_types:
nelem
unsigned long


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

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

original_code:
void objc_free(void *mem)
{
 free(mem);
}


decompiled_code:
int __fastcall objc_release_lock(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}

canonical_decompiled_code:
int __fastcall func0(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}

original_code:
__attribute__((unused)) static void objc_release_lock(void *x)
{
 mutex_t *lock = *(mutex_t**)x;
 pthread_mutex_unlock(lock);
}

canonical_original_code:
static void objc_release_lock(void *x)
{
 union pthread_mutex_t *lock = *(mutex_t**)x;
 pthread_mutex_unlock(lock);
}

variable_types:
x
void *

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


decompiled_code:
__int64 __fastcall objc_collect_garbage_data(__int64 a1, __int64 a2)
{
pthread_mutex_t *v3; // [rsp+8h] [rbp-20h] BYREF

if ( !garbage_queue )
{
v3 = &runtime_mutex;
pthread_mutex_lock(&runtime_mutex);
if ( !garbage_queue )
garbage_queue = toy_dispatch_queue_create("ObjC deferred free queue", 0LL);
objc_release_lock(&v3);
}
return toy_dispatch_async_f(garbage_queue, a2, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
pthread_mutex_t *v3; // [rsp+8h] [rbp-20h] BYREF

if ( !garbage_queue )
{
v3 = &runtime_mutex;
pthread_mutex_lock(&runtime_mutex);
if ( !garbage_queue )
garbage_queue = toy_dispatch_queue_create("ObjC deferred free queue", 0LL);
func0(&v3);
}
return toy_dispatch_async_f(garbage_queue, a2, a1);
}

original_code:
__attribute__ ((visibility("hidden"))) void objc_collect_garbage_data(void(*cleanup)(void*), void *garbage)
{
 if (0 == garbage_queue)
 {
  __attribute__((cleanup(objc_release_lock))) __attribute__((unused)) mutex_t *lock_pointer = &runtime_mutex; pthread_mutex_lock(&runtime_mutex);
  if (0 == garbage_queue)
  {
   garbage_queue = toy_dispatch_queue_create("ObjC deferred free queue", 0);
  }
 }
 toy_dispatch_async_f(garbage_queue, garbage, cleanup);
}

canonical_original_code:
void objc_collect_garbage_data(void (*cleanup)(void *), void *garbage)
{
 if (0 == garbage_queue)
 {
  union pthread_mutex_t *lock_pointer = &runtime_mutex; pthread_mutex_lock(&runtime_mutex);
  if (0 == garbage_queue)
  {
   garbage_queue = toy_dispatch_queue_create("ObjC deferred free queue", 0);
  }
 }
 toy_dispatch_async_f(garbage_queue, garbage, cleanup);
}

variable_types:
cleanup
void (*)(void *)

variable_types:
garbage
void *


canonical_decompiled_code:
int __fastcall func1(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}


decompiled_code:
__int64 __fastcall alloc_buffer(__int64 a1)
{
int v1; // ebp
_QWORD *v2; // r12
__int64 v3; // r12
pthread_mutex_t *v5; // [rsp+8h] [rbp-20h] BYREF

v5 = &trampoline_lock;
pthread_mutex_lock(&trampoline_lock);
if ( !offset || (unsigned __int64)(a1 + offset) > 0xFFF )
{
v1 = mkstemp(tmpPattern);
unlink(tmpPattern);
ftruncate(v1, 4096LL);
v2 = mmap(0LL, 0x1000uLL, 2, 1, v1, 0LL);
executeBuffer = (__int64)mmap(0LL, 0x1000uLL, 5, 1, v1, 0LL);
*v2 = writeBuffer;
writeBuffer = (__int64)v2;
offset = 8LL;
}
v3 = writeBuffer + offset;
offset += a1;
objc_release_lock(&v5);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // ebp
_QWORD *v2; // r12
__int64 v3; // r12
pthread_mutex_t *v5; // [rsp+8h] [rbp-20h] BYREF

v5 = &trampoline_lock;
pthread_mutex_lock(&trampoline_lock);
if ( !offset || (unsigned __int64)(a1 + offset) > 0xFFF )
{
v1 = mkstemp(tmpPattern);
unlink(tmpPattern);
ftruncate(v1, 4096LL);
v2 = mmap(0LL, 0x1000uLL, 2, 1, v1, 0LL);
executeBuffer = (__int64)mmap(0LL, 0x1000uLL, 5, 1, v1, 0LL);
*v2 = writeBuffer;
writeBuffer = (__int64)v2;
offset = 8LL;
}
v3 = writeBuffer + offset;
offset += a1;
func1(&v5);
return v3;
}

original_code:
static struct wx_buffer alloc_buffer(size_t size)
{
 __attribute__((cleanup(objc_release_lock))) __attribute__((unused)) mutex_t *lock_pointer = &trampoline_lock; pthread_mutex_lock(&trampoline_lock);
 if ((0 == offset) || (offset + size >= 4096))
 {
  int fd = mkstemp(tmpPattern);
  unlink(tmpPattern);
  ftruncate(fd, 4096);
  void *w = mmap(((void *)0), 4096, 0x2, 0x01, fd, 0);
  executeBuffer = mmap(((void *)0), 4096, 0x1|0x4, 0x01, fd, 0);
  *((void**)w) = writeBuffer;
  writeBuffer = w;
  offset = sizeof(void*);
 }
 struct wx_buffer b = { writeBuffer + offset, executeBuffer + offset };
 offset += size;
 return b;
}

canonical_original_code:
static struct wx_buffer alloc_buffer(unsigned long size)
{
 union pthread_mutex_t *lock_pointer = &trampoline_lock; pthread_mutex_lock(&trampoline_lock);
 if ((0 == offset) || (offset + size >= 4096))
 {
  int fd = mkstemp(tmpPattern);
  unlink(tmpPattern);
  ftruncate(fd, 4096);
  void *w = mmap(((void *)0), 4096, 0x2, 0x01, fd, 0);
  executeBuffer = mmap(((void *)0), 4096, 0x1|0x4, 0x01, fd, 0);
  *((void **)w) = writeBuffer;
  writeBuffer = w;
  offset = sizeof(void *);
 }
 struct wx_buffer b = { writeBuffer + offset, executeBuffer + offset };
 offset += size;
 return b;
}

variable_types: fd int

variable_types:
w
void *

variable_types:
b
struct wx_buffer { void *w; void *x; }

return_type: struct wx_buffer { void *w; void *x; }


decompiled_code:
int init_trampolines()
{
char *v0; // rdx
int result; // eax

init_recursive_mutex(&trampoline_lock);
v0 = getenv("TMPDIR");
if ( !v0 )
v0 = "/tmp/";
result = asprintf(&tmpPattern, "%s/objc_trampolinesXXXXXXXXXXX", v0);
if ( result < 0 )
abort();
return result;
}

canonical_decompiled_code:
int func3()
{
char *v0; // rdx
int result; // eax

func0(&trampoline_lock);
v0 = getenv("TMPDIR");
if ( !v0 )
v0 = "/tmp/";
result = asprintf(&tmpPattern, "%s/objc_trampolinesXXXXXXXXXXX", v0);
if ( result < 0 )
abort();
return result;
}

original_code:
__attribute__ ((visibility("hidden"))) void init_trampolines(void)
{
 init_recursive_mutex(&(trampoline_lock));
 char *tmp = getenv("TMPDIR");
 if (((void *)0) == tmp)
 {
  tmp = "/tmp/";
 }
 if (0 > asprintf(&tmpPattern, "%s/objc_trampolinesXXXXXXXXXXX", tmp))
 {
  abort();
 }
}

canonical_original_code:
void init_trampolines()
{
 init_recursive_mutex(&(trampoline_lock));
 char *tmp = getenv("TMPDIR");
 if (((void *)0) == tmp)
 {
  tmp = "/tmp/";
 }
 if (0 > asprintf(&tmpPattern, "%s/objc_trampolinesXXXXXXXXXXX", tmp))
 {
  abort();
 }
}

variable_types:
tmp
char *


decompiled_code:
char *block_copyIMPTypeEncoding_np()
{
const char *v0; // rax
char *v1; // rax
char *v2; // r13
char *v3; // rbx
const unsigned __int16 *v4; // r12
_BYTE *v5; // rbp
size_t v6; // rax
char v7; // al

v0 = (const char *)block_getType_np();
v1 = strdup(v0);
v2 = v1;
if ( v1 )
{
v3 = &v1[lengthOfTypeEncoding(v1)];
v4 = *_ctype_b_loc();
while ( (v4[*v3] & 0x800) != 0 )
++v3;
if ( *v3 != 64 )
_assert_fail("'@' == *replace", "block_to_imp.c", 0x95u, "block_copyIMPTypeEncoding_np");
v5 = v3 + 1;
if ( v3[1] != 63 )
_assert_fail("'?' == *replace", "block_to_imp.c", 0x97u, "block_copyIMPTypeEncoding_np");
v6 = strlen(v3 + 1);
memmove(v3 + 1, v3 + 2, v6);
v7 = v3[1];
if ( (v4[v7] & 0x800) != 0 )
{
do
v7 = *++v5;
while ( (v4[(char)*v5] & 0x800) != 0 );
}
if ( v7 == 64 )
{
*v5 = 58;
}
else
{
free(v2);
v2 = 0LL;
}
}
return v2;
}

canonical_decompiled_code:
char *func6()
{
const char *v0; // rax
char *v1; // rax
char *v2; // r13
char *v3; // rbx
const unsigned __int16 *v4; // r12
_BYTE *v5; // rbp
size_t v6; // rax
char v7; // al

v0 = (const char *)block_getType_np();
v1 = strdup(v0);
v2 = v1;
if ( v1 )
{
v3 = &v1[lengthOfTypeEncoding(v1)];
v4 = *_ctype_b_loc();
while ( (v4[*v3] & 0x800) != 0 )
++v3;
if ( *v3 != 64 )
_assert_fail("'@' == *replace", "block_to_imp.c", 0x95u, "block_copyIMPTypeEncoding_np");
v5 = v3 + 1;
if ( v3[1] != 63 )
_assert_fail("'?' == *replace", "block_to_imp.c", 0x97u, "block_copyIMPTypeEncoding_np");
v6 = strlen(v3 + 1);
memmove(v3 + 1, v3 + 2, v6);
v7 = v3[1];
if ( (v4[v7] & 0x800) != 0 )
{
do
v7 = *++v5;
while ( (v4[(char)*v5] & 0x800) != 0 );
}
if ( v7 == 64 )
{
*v5 = 58;
}
else
{
free(v2);
v2 = 0LL;
}
}
return v2;
}

original_code:
char *block_copyIMPTypeEncoding_np(void*block)
{
 char *buffer = strdup(block_getType_np(block));
 if (((void *)0) == buffer) { return ((void *)0); }
 char *replace = buffer;
 replace += lengthOfTypeEncoding(replace);
 while (((*__ctype_b_loc ())[(int) ((*replace))] & (unsigned short int) _ISdigit)) { replace++; }
 ((void) sizeof (('@' == *replace) ? 1 : 0), __extension__ ({ if ('@' == *replace) ; else __assert_fail ("'@' == *replace", "block_to_imp.c", 149, __extension__ __PRETTY_FUNCTION__); }));
 replace++;
 ((void) sizeof (('?' == *replace) ? 1 : 0), __extension__ ({ if ('?' == *replace) ; else __assert_fail ("'?' == *replace", "block_to_imp.c", 151, __extension__ __PRETTY_FUNCTION__); }));
 memmove(replace, replace+1, strlen(replace));
 while (((*__ctype_b_loc ())[(int) ((*replace))] & (unsigned short int) _ISdigit)) { replace++; }
 if ('@' != *replace)
 {
  free(buffer);
  return ((void *)0);
 }
 *replace = ':';
 return buffer;
}

canonical_original_code:
char *block_copyIMPTypeEncoding_np(void *block)
{
 char *buffer = strdup(block_getType_np(block));
 if (((void *)0) == buffer) { return ((void *)0); }
 char *replace = buffer;
 replace += lengthOfTypeEncoding(replace);
 while (((*__ctype_b_loc ())[(int) ((*replace))] & (unsigned short) _ISdigit)) { replace++; }
 ((void) sizeof (('@' == *replace) ? 1 : 0), __extension__ ({ if ('@' == *replace) ; else __assert_fail ("'@' == *replace", "block_to_imp.c", 149, __extension__ __PRETTY_FUNCTION__); }));
 replace++;
 ((void) sizeof (('?' == *replace) ? 1 : 0), __extension__ ({ if ('?' == *replace) ; else __assert_fail ("'?' == *replace", "block_to_imp.c", 151, __extension__ __PRETTY_FUNCTION__); }));
 memmove(replace, replace+1, strlen(replace));
 while (((*__ctype_b_loc ())[(int) ((*replace))] & (unsigned short) _ISdigit)) { replace++; }
 if ('@' != *replace)
 {
  free(buffer);
  return ((void *)0);
 }
 *replace = ':';
 return buffer;
}

variable_types:
block
void *

variable_types:
buffer
char *

variable_types:
replace
char *

return_type: char *


endsWith

decompiled_code:
_BOOL8 __fastcall endsWith(const char *a1, const char *a2)
{
char *v2; // rbp
_BOOL8 result; // rax
size_t v4; // rbx

if ( !a1 )
return 0LL;
v2 = strstr(a1, a2);
result = 0LL;
if ( v2 )
{
v4 = strlen(a2);
result = v4 == strlen(v2);
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char *a1, const char *a2)
{
char *v2; // rbp
_BOOL8 result; // rax
size_t v4; // rbx

if ( !a1 )
return 0LL;
v2 = strstr(a1, a2);
result = 0LL;
if ( v2 )
{
v4 = strlen(a2);
result = v4 == strlen(v2);
}
return result;
}

original_code:
static BOOL endsWith(const char *string, const char *suffix)
{
 if (((void *)0) == string) { return ((BOOL)0); }
 char *interior = strstr(string, suffix);
 return (interior && (strlen(suffix) == strlen(interior)));
}

canonical_original_code:
static unsigned char endsWith(char *string, char *suffix)
{
 if (((void *)0) == string) { return ((unsigned char)0); }
 char *interior = strstr(string, suffix);
 return (interior && (strlen(suffix) == strlen(interior)));
}

variable_types:
string
char *

variable_types:
suffix
char *

variable_types:
interior
char *

return_type: unsigned char


canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}


repo: qnet-cpp-dreamcast-dev/glink

decompiled_code:
void __fastcall set_glink_platform(int a1)
{
platform = a1;
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
platform = a1;
}

original_code:
void set_glink_platform(GLINK_PLATFORM p) {
 platform = p;
}

canonical_original_code:
void set_glink_platform(enum GLINK_PLATFORM p) {
 platform = p;
}

variable_types: enum GLINK_PLATFORM { PC=0, DREAMCAST=1 }


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

result = (unsigned int)platform;
if ( !platform )
return glink_pc_setup();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func29();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void glink_screen_setup(unsigned int w, unsigned int h) {
 if (platform == PC) {
  glink_pc_setup(w, h);
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

variable_types:
w
unsigned int

variable_types:
h
unsigned int


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

result = (unsigned int)platform;
if ( !platform )
return glink_pc_shutdown();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func30();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void glink_shutdown() {
 if (platform == PC) {
  glink_pc_shutdown();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}


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

result = (unsigned int)platform;
if ( !platform )
return pc_pump_events();
if ( platform != 1 )
exit(0);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func20();
if ( platform != 1 )
exit(0);
return result;
}

original_code:
void glink_pump_events() {
 if (platform == PC) {
  pc_pump_events();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(0);
 }
}


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

result = (unsigned int)platform;
if ( !platform )
return pc_get_key_state();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func21();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
unsigned char* glink_get_key_state() {
 if (platform == PC) {
  return pc_get_key_state();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

canonical_original_code:
unsigned char *glink_get_key_state() {
 if (platform == PC) {
  return pc_get_key_state();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

return_type: unsigned char *


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

result = (unsigned int)platform;
if ( !platform )
return pc_check_for_exit();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func22();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
int check_for_exit() {
 if (platform == PC) {
  return pc_check_for_exit();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

return_type: int


decompiled_code:
__int64 glink_get_ticks()
{
return pc_get_ticks();
}

canonical_decompiled_code:
__int64 func13()
{
return func23();
}

original_code:
unsigned int glink_get_ticks() {
 return pc_get_ticks();
}

return_type: unsigned int


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

result = (unsigned int)platform;
if ( !platform )
return pc_render_rect();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func25();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void render_rect(const int x, const int y, const unsigned int w, const unsigned int h, const unsigned int color) {
 if (platform == PC) {
  pc_render_rect(x, y, w, h, color);
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

canonical_original_code:
void render_rect(int x, int y, unsigned int w, unsigned int h, unsigned int color) {
 if (platform == PC) {
  pc_render_rect(x, y, w, h, color);
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

variable_types: x int

variable_types: y int

variable_types:
color
unsigned int


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

result = (unsigned int)platform;
if ( !platform )
return pc_render_rect_r();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func26();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void render_rect_r(const glink_rect r, const unsigned int color) {
 if (platform == PC) {
  pc_render_rect_r(r, color);
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

canonical_original_code:
void render_rect_r(struct _glink_rect r, unsigned int color) {
 if (platform == PC) {
  pc_render_rect_r(r, color);
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}

variable_types: struct _glink_rect { int x; int y; unsigned int w; unsigned int h; }


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

result = (unsigned int)platform;
if ( !platform )
return pc_blank_video_buffer();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func27();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void blank_video_buffer() {
 if (platform == PC) {
  pc_blank_video_buffer();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}


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

result = (unsigned int)platform;
if ( !platform )
return pc_update_video();
if ( platform != 1 )
exit(1);
return result;
}

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

result = (unsigned int)platform;
if ( !platform )
return func28();
if ( platform != 1 )
exit(1);
return result;
}

original_code:
void update_video() {
 if (platform == PC) {
  pc_update_video();
 }
 else if (platform == DREAMCAST) {
 }
 else {
  exit(1);
 }
}


decompiled_code:
__int64 pc_pump_events()
{
return SDL_PumpEvents();
}

canonical_decompiled_code:
__int64 func20()
{
return SDL_PumpEvents();
}

original_code:
void pc_pump_events() {
 SDL_PumpEvents();
}


decompiled_code:
__int64 *pc_get_key_state()
{
__int64 *v0; // rax

v0 = (__int64 *)SDL_GetKeyState();
keystates = *v0;
qword_404197 = *(__int64 *)((char *)v0 + 247);
qmemcpy(
(void *)((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v0 - ((char *)&keystates - ((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL))),
8LL
* (((unsigned int)((unsigned int)&keystates - ((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL)) + 255) >> 3));
return &keystates;
}

canonical_decompiled_code:
__int64 *func21()
{
__int64 *v0; // rax

v0 = (__int64 *)SDL_GetKeyState();
keystates = *v0;
qword_404197 = *(__int64 *)((char *)v0 + 247);
qmemcpy(
(void *)((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v0 - ((char *)&keystates - ((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL))),
8LL
* (((unsigned int)((unsigned int)&keystates - ((unsigned __int64)&unk_4040A8 & 0xFFFFFFFFFFFFFFF8LL)) + 255) >> 3));
return &keystates;
}

original_code:
unsigned char* pc_get_key_state() {
 Uint8* ks = SDL_GetKeyState(((void *)0));
 memcpy(keystates, ks, sizeof(unsigned char) * 0xFF);
 return keystates;
}

canonical_original_code:
unsigned char *pc_get_key_state() {
 unsigned char *ks = SDL_GetKeyState(((void *)0));
 memcpy(keystates, ks, sizeof(unsigned char) * 0xFF);
 return keystates;
}

variable_types:
ks
unsigned char *


decompiled_code:
_BOOL8 pc_check_for_exit()
{
return *((_BYTE *)pc_get_key_state() + 27) != 0;
}

original_code:
int pc_check_for_exit() {
 unsigned char* ks = pc_get_key_state();
 if (ks[0x1B])
  return 1;
 else
  return 0;
}

canonical_original_code:
int pc_check_for_exit() {
 unsigned char *ks = pc_get_key_state();
 if (ks[0x1B])
  return 1;
 else
  return 0;
}


decompiled_code:
__int64 pc_get_ticks()
{
return SDL_GetTicks();
}

canonical_decompiled_code:
__int64 func23()
{
return SDL_GetTicks();
}

original_code:
unsigned int pc_get_ticks() {
 return SDL_GetTicks();
}


decompiled_code:
__int64 pc_render_rect()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

canonical_decompiled_code:
__int64 func25()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

original_code:
void pc_render_rect(const int x, const int y, const int w, const int h, const unsigned int color) {
 SDL_Rect r;
 r.x = x;
 r.y = y;
 r.w = w;
 r.h = h;
 SDL_FillRect(SDL_GetVideoSurface(), &r, color);
}

canonical_original_code:
void pc_render_rect(int x, int y, int w, int h, unsigned int color) {
 struct SDL_Rect r;
 r.x = x;
 r.y = y;
 r.w = w;
 r.h = h;
 SDL_FillRect(SDL_GetVideoSurface(), &r, color);
}

variable_types: w int

variable_types: h int

variable_types: struct SDL_Rect { short x; unsigned short w; }


decompiled_code:
__int64 pc_render_rect_r()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

canonical_decompiled_code:
__int64 func26()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

original_code:
void pc_render_rect_r(const glink_rect r, const unsigned int color) {
 SDL_Rect out_r;
 out_r.x = r.x;
 out_r.y = r.y;
 out_r.w = r.w;
 out_r.h = r.h;
 SDL_FillRect(SDL_GetVideoSurface(), &out_r, color);
}

canonical_original_code:
void pc_render_rect_r(struct _glink_rect r, unsigned int color) {
 struct SDL_Rect out_r;
 out_r.x = r.x;
 out_r.y = r.y;
 out_r.w = r.w;
 out_r.h = r.h;
 SDL_FillRect(SDL_GetVideoSurface(), &out_r, color);
}

user_defined_types: struct _glink_rect { int x; int y; unsigned int w; unsigned int h; }

user_defined_types: struct SDL_Rect { short x; unsigned short w; }


decompiled_code:
__int64 pc_blank_video_buffer()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

canonical_decompiled_code:
__int64 func27()
{
SDL_GetVideoSurface();
return SDL_FillRect();
}

original_code:
void pc_blank_video_buffer() {
 SDL_FillRect(SDL_GetVideoSurface(), ((void *)0), 0x0);
}


decompiled_code:
__int64 pc_update_video()
{
SDL_GetVideoSurface();
return SDL_Flip();
}

canonical_decompiled_code:
__int64 func28()
{
SDL_GetVideoSurface();
return SDL_Flip();
}

original_code:
void pc_update_video() {
 SDL_Flip(SDL_GetVideoSurface());
}


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

screen = SDL_SetVideoMode();
result = 0LL;
memset(&keystates, 0, 0xF8uLL);
*((_DWORD *)&keystates + 62) = 0;
*(_WORD *)((char *)&qword_404197 + 5) = 0;
HIBYTE(qword_404197) = 0;
return result;
}

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

screen = SDL_SetVideoMode();
result = 0LL;
memset(&keystates, 0, 0xF8uLL);
*((_DWORD *)&keystates + 62) = 0;
*(_WORD *)((char *)&qword_404197 + 5) = 0;
HIBYTE(qword_404197) = 0;
return result;
}

original_code:
void glink_pc_setup(unsigned int w, unsigned int h) {
 screen = SDL_SetVideoMode(w, h, 32, 0x00000000);
 memset(keystates, 0x00, sizeof(unsigned char) * 0xFF);
}


decompiled_code:
__int64 glink_pc_shutdown()
{
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func30()
{
return SDL_Quit();
}

original_code:
void glink_pc_shutdown() {
 SDL_Quit();
}


decompiled_code:
__int64 __fastcall rect_screen_collision(_DWORD *a1)
{
__int64 result; // rax

result = (unsigned int)(*a1 + a1[2]);
if ( (unsigned int)result > 0x500 )
*a1 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func32(_DWORD *a1)
{
__int64 result; // rax

result = (unsigned int)(*a1 + a1[2]);
if ( (unsigned int)result > 0x500 )
*a1 = 0;
return result;
}

original_code:
void rect_screen_collision(glink_rect* r) {
 if (r->x + r->w > 1280) {
  r->x = 0;
 }
}

canonical_original_code:
void rect_screen_collision(struct _glink_rect *r) {
 if (r->x + r->w > 1280) {
  r->x = 0;
 }
}

variable_types: struct _glink_rect { int x; int y; unsigned int w; unsigned int h; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
__int64 v6; // [rsp+8h] [rbp-20h]

set_glink_platform(0);
glink_screen_setup(1280LL, 960LL);
glink_rect_init(&v5, 250, 300, 128, 128);
while ( 1 )
{
glink_pump_events();
if ( (int)check_for_exit() > 0 )
break;
if ( *(_BYTE *)(glink_get_key_state() + 103) )
{
v3 = 0;
LABEL_6:
fwrite(&unk_402004, 1uLL, 3uLL, _bss_start);
move_rect(&v5, 1, 0);
rect_screen_collision(&v5);
blank_video_buffer();
render_rect_r(v5, v6, 16711680LL);
update_video();
if ( v3 )
goto LABEL_10;
}
else
{
move_rect(&v5, 1, 0);
rect_screen_collision(&v5);
blank_video_buffer();
render_rect_r(v5, v6, 16711680LL);
update_video();
}
}
if ( *(_BYTE *)(glink_get_key_state() + 103) )
{
v3 = 1;
goto LABEL_6;
}
move_rect(&v5, 1, 0);
rect_screen_collision(&v5);
blank_video_buffer();
render_rect_r(v5, v6, 16711680LL);
update_video();
LABEL_10:
glink_shutdown();
return 0;
}

original_code:
int main(int argv, char** args) {
 set_glink_platform(PC);
 glink_screen_setup(1280, 960);
 int done = 0;
 glink_rect r;
 glink_rect_init(&r, 250, 300, 128, 128);
 glink_event e;
 while (!done) {
  glink_pump_events();
  if (check_for_exit() > 0) {
   done = 1;
  }
  unsigned char* ks = glink_get_key_state();
  if (ks[0x67])
   fprintf(stdout, "hi\n");
  move_rect(&r, 1, 0);
  rect_screen_collision(&r);
  blank_video_buffer();
  render_rect_r(r, 0xFF0000);
  update_video();
 }
 glink_shutdown();
}

canonical_original_code:
int main(int argv, char **args) {
 set_glink_platform(PC);
 glink_screen_setup(1280, 960);
 int done = 0;
 struct _glink_rect r;
 glink_rect_init(&r, 250, 300, 128, 128);
 struct _glink_event e;
 while (!done) {
  glink_pump_events();
  if (check_for_exit() > 0) {
   done = 1;
  }
  unsigned char *ks = glink_get_key_state();
  if (ks[0x67])
   fprintf(stdout, "hi\n");
  move_rect(&r, 1, 0);
  rect_screen_collision(&r);
  blank_video_buffer();
  render_rect_r(r, 0xFF0000);
  update_video();
 }
 glink_shutdown();
}

variable_types: argv int

variable_types:
args
char **

variable_types: done int

variable_types: struct _glink_event { unsigned char type; struct _glink_quit { unsigned char type; } quit; }

user_defined_types: struct _glink_event { unsigned char type; struct _glink_quit quit; }


repo: qsn/audiotools

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

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

original_code:
void free_click(struct click *click)
{
 if (click != ((void *)0)) {
  free(click->buf);
  free(click);
  click = ((void *)0);
 }
}

variable_types: struct click { float *buf; unsigned int size; } *


original_code:
ssize_t write_wave_header(int fd, unsigned long srate, size_t wave_size)
{
 struct wave_header h;
 uint32_t wsize = (uint32_t) wave_size * (16 / 8);
 h.chunkid = 0x46464952;
 h.chunksize = 44 + wsize;
 h.format = 0x45564157;
 h.fmtchunkid = 0x20746d66;
 h.fmtchunksize = 16;
 h.audiofmt = 1;
 h.nchannels = 1;
 h.srate = (uint32_t) srate;
 h.brate = h.srate * h.nchannels * 16 / 8;
 h.balign = h.nchannels * 16 / 8;
 h.bps = 16;
 h.datachunkid = 0x61746164;
 h.datachunksize = wsize;
 return write(fd, &h, 44);
}

canonical_original_code:
long write_wave_header(int fd, unsigned long srate, unsigned long wave_size)
{
 struct wave_header h;
 unsigned int wsize = (uint32_t) wave_size * (16 / 8);
 h.chunkid = 0x46464952;
 h.chunksize = 44 + wsize;
 h.format = 0x45564157;
 h.fmtchunkid = 0x20746d66;
 h.fmtchunksize = 16;
 h.audiofmt = 1;
 h.nchannels = 1;
 h.srate = (unsigned int) srate;
 h.brate = h.srate * h.nchannels * 16 / 8;
 h.balign = h.nchannels * 16 / 8;
 h.bps = 16;
 h.datachunkid = 0x61746164;
 h.datachunksize = wsize;
 return write(fd, &h, 44);
}

variable_types: fd int

variable_types:
srate
unsigned long

variable_types: struct wave_header { unsigned int chunkid; unsigned int chunksize; unsigned int format; unsigned int fmtchunkid; unsigned int fmtchunksize; unsigned short audiofmt; unsigned short nchannels; unsigned int srate; unsigned int brate; unsigned short balign; unsigned short bps; unsigned int datachunkid; unsigned int datachunksize; }

variable_types:
wsize
unsigned int

return_type: long


decompiled_code:
__int64 __fastcall write_wave_samples(int fd, unsigned __int64 a2, __int64 a3)
{
_WORD *v4; // rbp
unsigned __int16 v5; // r14
unsigned int v6; // ebx
__int64 v7; // rdx
__int64 result; // rax
__int64 v9; // rax

v4 = malloc(0x800uLL);
v5 = 1024;
v6 = 0;
v7 = 0LL;
if ( a2 )
{
while ( 1 )
{
if ( v5 )
{
v9 = 0LL;
if ( a2 != v7 )
{
do
{
v4[v9] = (int)(float)(*(float *)(a3 + 4LL * (v6 + (unsigned int)v9)) * 32768.0);
if ( v5 - 1 == v9 )
break;
++v9;
}
while ( v9 != a2 - v7 );
}
}
if ( v5 + v6 > a2 )
v5 = a2 - v6;
if ( write(fd, v4, 2LL * v5) < 0 )
break;
v6 += v5;
v7 = v6;
if ( v6 >= a2 )
goto LABEL_2;
}
perror("write failed");
free(v4);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_2:
free(v4);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, unsigned __int64 a2, __int64 a3)
{
_WORD *v4; // rbp
unsigned __int16 v5; // r14
unsigned int v6; // ebx
__int64 v7; // rdx
__int64 result; // rax
__int64 v9; // rax

v4 = malloc(0x800uLL);
v5 = 1024;
v6 = 0;
v7 = 0LL;
if ( a2 )
{
while ( 1 )
{
if ( v5 )
{
v9 = 0LL;
if ( a2 != v7 )
{
do
{
v4[v9] = (int)(float)(*(float *)(a3 + 4LL * (v6 + (unsigned int)v9)) * 32768.0);
if ( v5 - 1 == v9 )
break;
++v9;
}
while ( v9 != a2 - v7 );
}
}
if ( v5 + v6 > a2 )
v5 = a2 - v6;
if ( write(fd, v4, 2LL * v5) < 0 )
break;
v6 += v5;
v7 = v6;
if ( v6 >= a2 )
goto LABEL_2;
}
perror("write failed");
free(v4);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_2:
free(v4);
result = 0LL;
}
return result;
}

original_code:
int write_wave_samples(int fd, size_t wave_size, char *buf)
{
 unsigned int i, offset;
 unsigned short blen = 1024;
 uint16_t *tmp = malloc(blen * sizeof(uint16_t));
 jack_default_audio_sample_t *samples = (jack_default_audio_sample_t *) buf;
 for (offset = 0; offset < wave_size; offset += blen) {
  for (i = 0; i < blen && i < (wave_size - offset); i++)
   tmp[i] = (uint16_t) (32768 * samples[i + offset]);
  if (wave_size < (offset + blen))
   blen = (unsigned short) (wave_size - offset);
  if (write(fd, tmp, blen * sizeof(uint16_t)) < 0) {
   perror("write failed");
   free(tmp);
   return -1;
  }
 }
 free(tmp);
 return 0;
}

canonical_original_code:
int write_wave_samples(int fd, unsigned long wave_size, char *buf)
{
 unsigned int i; unsigned int offset;
 unsigned short blen = 1024;
 unsigned short *tmp = malloc(blen * sizeof(uint16_t));
 float *samples = (jack_default_audio_sample_t *) buf;
 for (offset = 0; offset < wave_size; offset += blen) {
  for (i = 0; i < blen && i < (wave_size - offset); i++)
   tmp[i] = (unsigned short) (32768 * samples[i + offset]);
  if (wave_size < (offset + blen))
   blen = (unsigned short) (wave_size - offset);
  if (write(fd, tmp, blen * sizeof(unsigned short)) < 0) {
   perror("write failed");
   free(tmp);
   return -1;
  }
 }
 free(tmp);
 return 0;
}

variable_types:
buf
char *

variable_types:
i
unsigned int

variable_types:
offset
unsigned int

variable_types:
blen
unsigned short

variable_types:
tmp
unsigned short *

variable_types:
samples
float *


mopReadRC

decompiled_code:
void mopReadRC()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void
mopReadRC()
{
}

canonical_original_code:
void mopReadRC()
{
}


mopReadDL

decompiled_code:
void mopReadDL()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void
mopReadDL()
{
}

canonical_original_code:
void mopReadDL()
{
}


Loop

original_code:
void
Loop()
{
 u_char *buf, *bp, *ep;
 int cc;
 fd_set fds, listeners;
 int bufsize = 1100, maxfd =0;
 struct if_info *ii;
 if (iflist == 0) {
  syslog(3, "no interfaces");
  exit(0);
 }
  buf = (u_char *) malloc((unsigned) bufsize);
 if (buf == 0) {
  syslog(3, "malloc: %m");
  exit(0);
 }
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 for (ii = iflist; ii; ii = ii->next) {
  if (ii->fd != -1) {
   ((void) (((&fds)->__fds_bits)[((ii->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ii->fd) % (8 * (int) sizeof (__fd_mask)))))));
   if (ii->fd > maxfd)
    maxfd = ii->fd;
         }
 }
 while (1) {
  listeners = fds;
  if (select(maxfd + 1, &listeners, (fd_set *) 0,
   (fd_set *) 0, (struct timeval *) 0) < 0) {
   syslog(3, "select: %m");
   exit(0);
  }
  for (ii = iflist; ii; ii = ii->next) {
   if (ii->fd != -1) {
    if (!((((&listeners)->__fds_bits)[((ii->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ii->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
     continue;
   }
 again:
   cc = read(ii->fd, (char *) buf, bufsize);
   if (cc < 0 && (*__errno_location ()) == 4)
    goto again;
   bp = buf;
   ep = bp + cc;
if(bp < ep)
 {
 mopProcess(ii,buf);
 }
}
}
}

canonical_original_code:
void Loop()
{
 unsigned char *buf; unsigned char *bp; unsigned char *ep;
 int cc;
 struct fd_set fds; struct fd_set listeners;
 int bufsize = 1100; int maxfd = 0;
 struct if_info *ii;
 if (iflist == 0) {
  syslog(3, "no interfaces");
  exit(0);
 }
  buf = (unsigned char *) malloc((unsigned int) bufsize);
 if (buf == 0) {
  syslog(3, "malloc: %m");
  exit(0);
 }
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 for (ii = iflist; ii; ii = ii->next) {
  if (ii->fd != -1) {
   ((void) (((&fds)->__fds_bits)[((ii->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ii->fd) % (8 * (int) sizeof (__fd_mask)))))));
   if (ii->fd > maxfd)
    maxfd = ii->fd;
         }
 }
 while (1) {
  listeners = fds;
  if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
   (struct fd_set *) 0, (struct timeval *) 0) < 0) {
   syslog(3, "select: %m");
   exit(0);
  }
  for (ii = iflist; ii; ii = ii->next) {
   if (ii->fd != -1) {
    if (!((((&listeners)->__fds_bits)[((ii->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ii->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
     continue;
   }
 again:
   cc = read(ii->fd, (char *) buf, bufsize);
   if (cc < 0 && (*__errno_location ()) == 4)
    goto again;
   bp = buf;
   ep = bp + cc;
if(bp < ep)
 {
 mopProcess(ii,buf);
 }
}
}
}

variable_types:
buf
unsigned char *

variable_types:
bp
unsigned char *

variable_types:
ep
unsigned char *

variable_types: cc int

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

variable_types:
listeners
struct fd_set { long __fds_bits[-1]; }

variable_types: bufsize int

variable_types: maxfd int

variable_types: struct if_info { int fd; int trans; unsigned char eaddr[6]; char if_name[32]; int (*iopen)(char *, int, unsigned short, int); int (*write)(int, unsigned char *, int, int); void (*read)(); struct if_info *next; } *

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


deviceInitAll

decompiled_code:
int deviceInitAll()
{
int v0; // eax
int v1; // ebp
unsigned __int64 v2; // rdx
char *v3; // r12
char *v4; // rbx
unsigned int v6; // [rsp+0h] [rbp-168h] BYREF
_QWORD *v7; // [rsp+8h] [rbp-160h]
_QWORD src[43]; // [rsp+10h] [rbp-158h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
syslog(3, "deviceInitAll: old socket: %m");
exit(1);
}
v1 = v0;
v6 = 320;
v7 = src;
if ( ioctl(v0, 0x8912uLL, &v6) < 0 || v6 <= 0x27 )
{
syslog(3, "deviceInitAll: old SIOCGIFCONF: %m");
exit(1);
}
v2 = (int)v6 / 0x28uLL;
if ( (int)v2 > 0 )
{
v3 = (char *)&src[5 * (unsigned int)(v2 - 1) + 5];
v4 = (char *)src;
do
{
if ( ioctl(v1, 0x8913uLL, v4) >= 0 && (v4[16] & 0x18) == 0 )
deviceInitOne(v4);
v4 += 40;
}
while ( v4 != v3 );
}
return close(v1);
}

canonical_decompiled_code:
int func3()
{
int v0; // eax
int v1; // ebp
unsigned __int64 v2; // rdx
char *v3; // r12
char *v4; // rbx
unsigned int v6; // [rsp+0h] [rbp-168h] BYREF
_QWORD *v7; // [rsp+8h] [rbp-160h]
_QWORD src[43]; // [rsp+10h] [rbp-158h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
syslog(3, "deviceInitAll: old socket: %m");
exit(1);
}
v1 = v0;
v6 = 320;
v7 = src;
if ( ioctl(v0, 0x8912uLL, &v6) < 0 || v6 <= 0x27 )
{
syslog(3, "deviceInitAll: old SIOCGIFCONF: %m");
exit(1);
}
v2 = (int)v6 / 0x28uLL;
if ( (int)v2 > 0 )
{
v3 = (char *)&src[5 * (unsigned int)(v2 - 1) + 5];
v4 = (char *)src;
do
{
if ( ioctl(v1, 0x8913uLL, v4) >= 0 && (v4[16] & 0x18) == 0 )
func2(v4);
v4 += 40;
}
while ( v4 != v3 );
}
return close(v1);
}

original_code:
void
deviceInitAll()
{
 int fd;
 int n;
 struct ifreq ibuf[8], *ifrp;
 struct ifconf ifc;
 if ((fd = socket(2, SOCK_DGRAM, 0)) < 0) {
  syslog(3, "deviceInitAll: old socket: %m");
  exit(1);
 }
 ifc.ifc_len = sizeof ibuf;
 ifc.ifc_ifcu.ifcu_buf = (caddr_t)ibuf;
 if (ioctl(fd, 0x8912, (char *)&ifc) < 0 ||
     ifc.ifc_len < sizeof(struct ifreq)) {
  syslog(3, "deviceInitAll: old SIOCGIFCONF: %m");
  exit(1);
 }
 ifrp = ibuf;
 n = ifc.ifc_len / sizeof(*ifrp);
 for (; --n >= 0; ++ifrp) {
  if (ioctl(fd, 0x8913, (char *)ifrp) < 0) {
   continue;
  }
  if (
      ifrp->ifr_ifru.ifru_flags & IFF_LOOPBACK ||
      ifrp->ifr_ifru.ifru_flags & IFF_POINTOPOINT)
   continue;
  deviceInitOne(ifrp->ifr_ifrn.ifrn_name);
 }
 (void) close(fd);
}

canonical_original_code:
void deviceInitAll()
{
 int fd;
 int n;
 struct ifreq ibuf[8]; struct ifreq *ifrp;
 struct ifconf ifc;
 if ((fd = socket(2, SOCK_DGRAM, 0)) < 0) {
  syslog(3, "deviceInitAll: old socket: %m");
  exit(1);
 }
 ifc.ifc_len = sizeof ibuf;
 ifc.ifc_ifcu.ifcu_buf = (char *)ibuf;
 if (ioctl(fd, 0x8912, (char *)&ifc) < 0 ||
     ifc.ifc_len < sizeof(struct ifreq)) {
  syslog(3, "deviceInitAll: old SIOCGIFCONF: %m");
  exit(1);
 }
 ifrp = ibuf;
 n = ifc.ifc_len / sizeof(*ifrp);
 for (; --n >= 0; ++ifrp) {
  if (ioctl(fd, 0x8913, (char *)ifrp) < 0) {
   continue;
  }
  if (
      ifrp->ifr_ifru.ifru_flags & IFF_LOOPBACK ||
      ifrp->ifr_ifru.ifru_flags & IFF_POINTOPOINT)
   continue;
  deviceInitOne(ifrp->ifr_ifrn.ifrn_name);
 }
 (void) close(fd);
}

variable_types: fd int

variable_types: n int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } [8]

variable_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } *ifcu_req; } ifc_ifcu; }

user_defined_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


repo: quad/voot

decompiled_code:
void frontend_cleanup()
{
input_handler_poll = 0;
free(prog_name);
}

canonical_decompiled_code:
void func0()
{
input_handler_poll = 0;
free(prog_name);
}

original_code:
static void frontend_cleanup(void)
{
    input_handler_poll = FALSE;
    free(prog_name);
}

canonical_original_code:
static void frontend_cleanup()
{
    input_handler_poll = FALSE;
    free(prog_name);
}


decompiled_code:
int frontend_printf(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
vprintf(a1, arg);
return putchar(10);
}

canonical_decompiled_code:
int func2(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
vprintf(a1, arg);
return putchar(10);
}

original_code:
static void frontend_printf(const char *format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vprintf(format, args);
    __builtin_va_end(args);
    printf("\n");
}

canonical_original_code:
static void frontend_printf(char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    vprintf(format, args);
    __builtin_va_end(args);
    printf("\n");
}

variable_types:
format
char *


decompiled_code:
int logger_callback(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
printf("%s: [npc|%s] ", (const char *)prog_name, npc_log_level_desc[a1]);
vprintf(a2, arg);
return putchar(10);
}

canonical_decompiled_code:
int func4(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
printf("%s: [npc|%s] ", (const char *)prog_name, npc_log_level_desc[a1]);
vprintf(a2, arg);
return putchar(10);
}

original_code:
void logger_callback(npc_log_level severity, const char *format, ...)
{
    va_list args;
    printf("%s: [npc|%s] ", prog_name, npc_log_level_desc[severity]);
    __builtin_va_start(args,format);
    vprintf(format, args);
    __builtin_va_end(args);
    printf("\n");
}

canonical_original_code:
void logger_callback(enum npc_log_level severity, char *format, ...)
{
    __builtin_va_list args;
    printf("%s: [npc|%s] ", prog_name, npc_log_level_desc[severity]);
    __builtin_va_start(args,format);
    vprintf(format, args);
    __builtin_va_end(args);
    printf("\n");
}

variable_types: enum npc_log_level { LOG_EMERG=0, LOG_ALERT=1, LOG_CRIT=2, LOG_ERR=3, LOG_WARNING=4, LOG_NOTICE=5, LOG_INFO=6, LOG_DEBUG=7 }


decompiled_code:
int display_start_banner()
{
puts(
"Console Netplay VOOT Client (npclient), built May 23 2025 at 14:07:08\n"
"Copyright (C) 2001-2002, Scott Robinson. All Rights Reserved.");
return puts(
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n");
}

canonical_decompiled_code:
int func7()
{
puts(
"Console Netplay VOOT Client (npclient), built May 23 2025 at 14:07:08\n"
"Copyright (C) 2001-2002, Scott Robinson. All Rights Reserved.");
return puts(
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n");
}

original_code:
static void display_start_banner(void)
{
    printf(banner_text);
    printf(gpl_text);
}

canonical_original_code:
static void display_start_banner()
{
    printf(banner_text);
    printf(gpl_text);
}


decompiled_code:
int display_exit_banner()
{
return puts("npclient exit!");
}

canonical_decompiled_code:
int func8()
{
return puts("npclient exit!");
}

original_code:
static void display_exit_banner(void)
{
    printf(exit_text);
}

canonical_original_code:
static void display_exit_banner()
{
    printf(exit_text);
}


decompiled_code:
int __fastcall frontend_init(const char *a1)
{
prog_name = strdup(a1);
input_handler_poll = 1;
return pthread_create(&input_poll_thread, 0LL, input_poll, 0LL);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
prog_name = strdup(a1);
input_handler_poll = 1;
return pthread_create(&input_poll_thread, 0LL, input_poll, 0LL);
}

original_code:
static void frontend_init(char *pname)
{
    prog_name = strdup(pname);
    input_handler_poll = TRUE;
    pthread_create(&input_poll_thread, ((void *)0), input_poll, ((void *)0));
}

variable_types:
pname
char *


decompiled_code:
void *__fastcall input_poll(void *a1)
{
char *v1; // rax

if ( !input_handler_poll )
return 0LL;
do
{
v1 = (char *)readline("npclient> ");
input_handler(v1);
}
while ( input_handler_poll );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func12(void *a1)
{
char *v1; // rax

if ( !input_handler_poll )
return 0LL;
do
{
v1 = (char *)readline("npclient> ");
func11(v1);
}
while ( input_handler_poll );
return 0LL;
}

original_code:
void* input_poll(void *arg)
{
    while(input_handler_poll)
        input_handler(readline("npclient> "));
    return ((void *)0);
}

canonical_original_code:
void *input_poll(void *arg)
{
    while(input_handler_poll)
        input_handler(readline("npclient> "));
    return ((void *)0);
}

variable_types:
arg
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
_DWORD *v4; // rbx
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

display_start_banner();
frontend_init(*argv);
v6[0] = (__int64)logger_callback;
v6[1] = (__int64)packet_callback;
npc_init(v6);
parse_options(argc, (char **)argv);
while ( 1 )
{
v3 = npc_get_event();
v4 = (_DWORD *)v3;
if ( !v3 )
break;
npc_handle_command(v3);
if ( *v4 == 10 )
{
free(v4);
break;
}
free(v4);
}
frontend_cleanup();
display_exit_banner();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
_DWORD *v4; // rbx
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

func7();
func9(*argv);
v6[0] = (__int64)logger_callback;
v6[1] = (__int64)packet_callback;
npc_init(v6);
func10(argc, (char **)argv);
while ( 1 )
{
v3 = npc_get_event();
v4 = (_DWORD *)v3;
if ( !v3 )
break;
npc_handle_command(v3);
if ( *v4 == 10 )
{
free(v4);
break;
}
free(v4);
}
func0();
func8();
return 0;
}

original_code:
int main(int argc, char **argv) {
 argc--;
 if (argc != 3) {
  printf("usage: bin2c <input> <identifier> <output>\n");
  return 0;
 }
 convert(argv[1], argv[2], argv[3]);
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall voot_check_packet_advsize(__int64 a1, unsigned int a2)
{
__int64 result; // rax

result = 0LL;
if ( a2 > 2 )
{
result = (unsigned int)(unsigned __int16)_bswap_16(*(_WORD *)(a1 + 1)) + 3;
if ( (unsigned int)result > a2 )
result = a2;
}
return result;
}

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

result = 0LL;
if ( a2 > 2 )
{
result = (unsigned int)(unsigned __int16)func0(*(_WORD *)(a1 + 1)) + 3;
if ( (unsigned int)result > a2 )
result = a2;
}
return result;
}

original_code:
uint32 voot_check_packet_advsize(voot_packet *packet, uint32 size)
{
    uint32 advsize;
    if (size < sizeof(voot_packet_header))
        return 0;
    return ((advsize = (sizeof(voot_packet_header) + ntohs(packet->header.size))) > size) ? size : advsize;
}

canonical_original_code:
unsigned int voot_check_packet_advsize(struct voot_packet *packet, unsigned int size)
{
    unsigned int advsize;
    if (size < sizeof(voot_packet_header))
        return 0;
    return ((advsize = (sizeof(voot_packet_header) + ntohs(packet->header.size))) > size) ? size : advsize;
}

variable_types: struct voot_packet { struct voot_packet_header { unsigned char type; unsigned short size; } header; unsigned char buffer[1024]; } *

variable_types:
size
unsigned int

variable_types:
advsize
unsigned int

user_defined_types: struct voot_packet { struct voot_packet_header header; unsigned char buffer[1024]; }


decompiled_code:
void *__fastcall voot_parse_buffer(void *src, size_t n)
{
void *v2; // rbp

v2 = 0LL;
if ( voot_check_packet((__int64)src, n) )
{
v2 = malloc(0x403uLL);
memcpy(v2, src, (unsigned int)n);
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func3(void *src, size_t n)
{
void *v2; // rbp

v2 = 0LL;
if ( func1((__int64)src, n) )
{
v2 = malloc(0x403uLL);
memcpy(v2, src, (unsigned int)n);
}
return v2;
}

original_code:
voot_packet* voot_parse_buffer(unsigned char *buffer, unsigned int buffer_size)
{
    voot_packet *packet;
    if (!voot_check_packet((voot_packet *) buffer, buffer_size))
        return ((void *)0);
    packet = malloc(sizeof(voot_packet));
    memcpy(packet, buffer, buffer_size);
    return packet;
}

canonical_original_code:
struct voot_packet *voot_parse_buffer(unsigned char *buffer, unsigned int buffer_size)
{
    struct voot_packet *packet;
    if (!voot_check_packet((struct voot_packet *) buffer, buffer_size))
        return ((void *)0);
    packet = malloc(sizeof(voot_packet));
    memcpy(packet, buffer, buffer_size);
    return packet;
}

variable_types:
buffer
unsigned char *

return_type: struct voot_packet { struct voot_packet_header { unsigned char type; unsigned short size; } header; unsigned char buffer[1024]; } *


decompiled_code:
__int64 __fastcall voot_socket_verify_packet(int fd)
{
int v1; // ebx
_BYTE v3[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = 0;
if ( (unsigned int)recv(fd, v3, 3uLL, 2) > 2 )
{
v1 = (unsigned __int16)_bswap_16(*(__int16 *)&v3[1]) + 3;
if ( v1 > (int)recv(fd, v3, v1, 2) )
v1 = 0;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd)
{
int v1; // ebx
_BYTE v3[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = 0;
if ( (unsigned int)recv(fd, v3, 3uLL, 2) > 2 )
{
v1 = (unsigned __int16)func0(*(__int16 *)&v3[1]) + 3;
if ( v1 > (int)recv(fd, v3, v1, 2) )
v1 = 0;
}
return (unsigned int)v1;
}

original_code:
uint32 voot_socket_verify_packet(int32 socket)
{
    voot_packet packet;
    int32 packet_header_size;
    int32 full_packet_size;
    int32 predict_packet_size;
    packet_header_size = recv(socket, &packet, sizeof(voot_packet_header), MSG_PEEK);
    if (packet_header_size < sizeof(voot_packet_header))
        return 0;
    predict_packet_size = sizeof(voot_packet_header) + ntohs(packet.header.size);
    full_packet_size = recv(socket, &packet, predict_packet_size, MSG_PEEK);
    if (full_packet_size >= predict_packet_size)
        return predict_packet_size;
    else
        return 0;
}

canonical_original_code:
unsigned int voot_socket_verify_packet(int socket)
{
    struct voot_packet packet;
    int packet_header_size;
    int full_packet_size;
    int predict_packet_size;
    packet_header_size = recv(socket, &packet, sizeof(voot_packet_header), MSG_PEEK);
    if (packet_header_size < sizeof(voot_packet_header))
        return 0;
    predict_packet_size = sizeof(voot_packet_header) + ntohs(packet.header.size);
    full_packet_size = recv(socket, &packet, predict_packet_size, MSG_PEEK);
    if (full_packet_size >= predict_packet_size)
        return predict_packet_size;
    else
        return 0;
}

variable_types: socket int

variable_types: struct voot_packet { struct voot_packet_header { unsigned char type; unsigned short size; } header; unsigned char buffer[1024]; }


decompiled_code:
void *__fastcall voot_parse_socket(int fd)
{
int v1; // eax
void *v2; // rdx
size_t v3; // rax
_BYTE v5[1512]; // [rsp+0h] [rbp-5E8h] BYREF

v1 = voot_socket_verify_packet(fd);
v2 = 0LL;
if ( v1 )
{
v3 = recv(fd, v5, v1, 0);
v2 = voot_parse_buffer(v5, v3);
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func5(int fd)
{
int v1; // eax
void *v2; // rdx
size_t v3; // rax
_BYTE v5[1512]; // [rsp+0h] [rbp-5E8h] BYREF

v1 = func4(fd);
v2 = 0LL;
if ( v1 )
{
v3 = recv(fd, v5, v1, 0);
v2 = func3(v5, v3);
}
return v2;
}

original_code:
voot_packet* voot_parse_socket(int32 socket)
{
    unsigned char data[1500];
    int32 packet_size;
    int32 rx;
    packet_size = voot_socket_verify_packet(socket);
    if (packet_size)
        rx = recv(socket, data, packet_size, 0);
    else
        return ((void *)0);
    return voot_parse_buffer(data, rx);
}

canonical_original_code:
struct voot_packet *voot_parse_socket(int socket)
{
    unsigned char data[1500];
    int packet_size;
    int rx;
    packet_size = voot_socket_verify_packet(socket);
    if (packet_size)
        rx = recv(socket, data, packet_size, 0);
    else
        return ((void *)0);
    return voot_parse_buffer(data, rx);
}

variable_types:
data
unsigned char [1500]

variable_types: rx int

user_defined_types: struct voot_packet_header { unsigned char type; unsigned short size; }


decompiled_code:
ssize_t __fastcall voot_send_packet(int fd, void *buf, size_t n)
{
ssize_t result; // rax
unsigned int v4; // ebx
_BOOL4 v5; // edx

result = 0LL;
if ( fd >= 0 )
{
v4 = n;
v5 = voot_check_packet((__int64)buf, n);
result = 0LL;
if ( v5 )
result = send(fd, buf, v4, 0);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func6(int fd, void *buf, size_t n)
{
ssize_t result; // rax
unsigned int v4; // ebx
_BOOL4 v5; // edx

result = 0LL;
if ( fd >= 0 )
{
v4 = n;
v5 = func1((__int64)buf, n);
result = 0LL;
if ( v5 )
result = send(fd, buf, v4, 0);
}
return result;
}

original_code:
int32 voot_send_packet(int32 socket, voot_packet *packet, uint32 size)
{
    if (socket < 0)
        return FALSE;
    else if (!voot_check_packet(packet, size))
        return FALSE;
    return send(socket, packet, size, 0);
}

canonical_original_code:
int voot_send_packet(int socket, struct voot_packet *packet, unsigned int size)
{
    if (socket < 0)
        return FALSE;
    else if (!voot_check_packet(packet, size))
        return FALSE;
    return send(socket, packet, size, 0);
}


decompiled_code:
__int64 __fastcall voot_send_command(int fd, char a2)
{
char *v2; // rbx
unsigned int v3; // eax
unsigned int v4; // ebp

v2 = (char *)malloc(0x403uLL);
*v2 = 99;
*(_WORD *)(v2 + 1) = _bswap_16(1);
v2[3] = a2;
v3 = voot_check_packet_advsize((__int64)v2, 0x403u);
v4 = voot_send_packet(fd, v2, v3);
free(v2);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, char a2)
{
char *v2; // rbx
unsigned int v3; // eax
unsigned int v4; // ebp

v2 = (char *)malloc(0x403uLL);
*v2 = 99;
*(_WORD *)(v2 + 1) = func0(1);
v2[3] = a2;
v3 = func2((__int64)v2, 0x403u);
v4 = func6(fd, v2, v3);
free(v2);
return v4;
}

original_code:
int32 voot_send_command(int32 socket, uint8 command)
{
    int32 retval;
    voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = 'c';
    packet->header.size = htons(1);
    packet->buffer[0] = command;
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

canonical_original_code:
int voot_send_command(int socket, unsigned char command)
{
    int retval;
    struct voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = 'c';
    packet->header.size = htons(1);
    packet->buffer[0] = command;
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

variable_types:
command
unsigned char

variable_types: retval int


decompiled_code:
__int64 __fastcall voot_send_command_opt(int fd, char a2, int a3)
{
char *v4; // rbx
unsigned int v5; // eax
unsigned int v6; // ebp

v4 = (char *)malloc(0x403uLL);
*v4 = 99;
*(_WORD *)(v4 + 1) = _bswap_16(8);
v4[3] = a2;
*(_DWORD *)(v4 + 7) = a3;
v5 = voot_check_packet_advsize((__int64)v4, 0x403u);
v6 = voot_send_packet(fd, v4, v5);
free(v4);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, char a2, int a3)
{
char *v4; // rbx
unsigned int v5; // eax
unsigned int v6; // ebp

v4 = (char *)malloc(0x403uLL);
*v4 = 99;
*(_WORD *)(v4 + 1) = func0(8);
v4[3] = a2;
*(_DWORD *)(v4 + 7) = a3;
v5 = func2((__int64)v4, 0x403u);
v6 = func6(fd, v4, v5);
free(v4);
return v6;
}

original_code:
int32 voot_send_command_opt(int32 socket, uint8 command, uint32 option)
{
    int32 retval;
    voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = 'c';
    packet->header.size = htons(8);
    packet->buffer[0] = command;
    ((uint32 *) packet->buffer)[1] = option;
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

canonical_original_code:
int voot_send_command_opt(int socket, unsigned char command, unsigned int option)
{
    int retval;
    struct voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = 'c';
    packet->header.size = htons(8);
    packet->buffer[0] = command;
    ((unsigned int *) packet->buffer)[1] = option;
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

variable_types:
option
unsigned int


decompiled_code:
__int64 __fastcall voot_send_data(int fd, char a2, const void *a3, unsigned int a4)
{
char *v6; // rbx
unsigned int v7; // eax
unsigned int v8; // ebp

v6 = (char *)malloc(0x403uLL);
*v6 = a2;
*(_WORD *)(v6 + 1) = _bswap_16(a4);
memcpy(v6 + 3, a3, a4);
v7 = voot_check_packet_advsize((__int64)v6, 0x403u);
v8 = voot_send_packet(fd, v6, v7);
free(v6);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, char a2, const void *a3, unsigned int a4)
{
char *v6; // rbx
unsigned int v7; // eax
unsigned int v8; // ebp

v6 = (char *)malloc(0x403uLL);
*v6 = a2;
*(_WORD *)(v6 + 1) = func0(a4);
memcpy(v6 + 3, a3, a4);
v7 = func2((__int64)v6, 0x403u);
v8 = func6(fd, v6, v7);
free(v6);
return v8;
}

original_code:
int32 voot_send_data(int32 socket, uint8 packet_type, const uint8 *data, uint32 data_size)
{
    int32 retval;
    voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = packet_type;
    packet->header.size = htons(data_size);
    memcpy(packet->buffer, data, data_size);
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

canonical_original_code:
int voot_send_data(int socket, unsigned char packet_type, unsigned char *data, unsigned int data_size)
{
    int retval;
    struct voot_packet *packet;
    packet = malloc(sizeof(voot_packet));
    packet->header.type = packet_type;
    packet->header.size = htons(data_size);
    memcpy(packet->buffer, data, data_size);
    retval = voot_send_packet(socket, packet, voot_check_packet_advsize(packet, sizeof(voot_packet)));
    free(packet);
    return retval;
}

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall voot_dump_buffer(int fd, int a2, char *a3, unsigned int a4)
{
char *v6; // rbx
unsigned int v7; // ebp

voot_send_command_opt(fd, 68, a2);
if ( a4 > 0x3FE )
{
v6 = a3;
v7 = 0;
while ( (unsigned int)voot_send_data(fd, 68, v6, 0x3FFu) )
{
++v7;
v6 += 1023;
if ( v7 >= a4 / 0x3FF )
goto LABEL_7;
}
fwrite("Error sending packet in main dump loop! Abort!\n", 1uLL, 0x2FuLL, stderr);
}
LABEL_7:
if ( a4 % 0x3FF )
voot_send_data(fd, 68, &a3[a4 - (unsigned __int64)(a4 % 0x3FF)], a4 % 0x3FF);
return voot_send_command(fd, 100);
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd, int a2, char *a3, unsigned int a4)
{
char *v6; // rbx
unsigned int v7; // ebp

func8(fd, 68, a2);
if ( a4 > 0x3FE )
{
v6 = a3;
v7 = 0;
while ( (unsigned int)func9(fd, 68, v6, 0x3FFu) )
{
++v7;
v6 += 1023;
if ( v7 >= a4 / 0x3FF )
goto LABEL_7;
}
fwrite("Error sending packet in main dump loop! Abort!\n", 1uLL, 0x2FuLL, stderr);
}
LABEL_7:
if ( a4 % 0x3FF )
func9(fd, 68, &a3[a4 - (unsigned __int64)(a4 % 0x3FF)], a4 % 0x3FF);
return func7(fd, 100);
}

original_code:
void voot_dump_buffer(int32 socket, uint32 address, const uint8 *in_data, uint32 in_data_length)
{
    uint32 index, remain, segment_size;
    voot_packet *sizer;
    segment_size = sizeof(sizer->buffer) - 1;
    voot_send_command_opt(socket, 'D', address);
    for (index = 0; index < (in_data_length / segment_size); index++)
    {
        const uint8 *in_data_segment;
        in_data_segment = in_data + (segment_size * index);
        if (!voot_send_data(socket, 'D', in_data_segment, segment_size))
        {
            fprintf(stderr, "Error sending packet in main dump loop! Abort!\n");
            break;
        }
    }
    remain = in_data_length % segment_size;
    if (remain)
        voot_send_data(socket, 'D', (in_data + in_data_length) - remain, remain);
    voot_send_command(socket, 'd');
}

canonical_original_code:
void voot_dump_buffer(int socket, unsigned int address, unsigned char *in_data, unsigned int in_data_length)
{
    unsigned int index; unsigned int remain; unsigned int segment_size;
    struct voot_packet *sizer;
    segment_size = sizeof(sizer->buffer) - 1;
    voot_send_command_opt(socket, 'D', address);
    for (index = 0; index < (in_data_length / segment_size); index++)
    {
        unsigned char *in_data_segment;
        in_data_segment = in_data + (segment_size * index);
        if (!voot_send_data(socket, 'D', in_data_segment, segment_size))
        {
            fprintf(stderr, "Error sending packet in main dump loop! Abort!\n");
            break;
        }
    }
    remain = in_data_length % segment_size;
    if (remain)
        voot_send_data(socket, 'D', (in_data + in_data_length) - remain, remain);
    voot_send_command(socket, 'd');
}

variable_types:
address
unsigned int

variable_types:
index
unsigned int

variable_types:
remain
unsigned int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 4 )
convert(argv[1], argv[2], argv[3]);
else
puts("usage: bin2c <input> <identifier> <output>");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 4 )
func7(argv[1], argv[2], argv[3]);
else
puts("usage: bin2c <input> <identifier> <output>");
return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 argc--;
 if (argc != 3) {
  printf("usage: bin2c <input> <identifier> <output>\n");
  return 0;
 }
 convert(argv[1], argv[2], argv[3]);
}

variable_types: argc int


sigHandlerIO

decompiled_code:
void sigHandlerIO()
{
int v0; // eax
_BYTE v1[72]; // [rsp-48h] [rbp-48h] BYREF

if ( queuebuffer )
{
while ( 1 )
{
v0 = read(uartfd, v1, 0x40uLL);
if ( v0 <= 0 )
break;
qb_push(queuebuffer, v1, v0);
}
}
}

canonical_decompiled_code:
void func1()
{
int v0; // eax
_BYTE v1[72]; // [rsp-48h] [rbp-48h] BYREF

if ( queuebuffer )
{
while ( 1 )
{
v0 = read(uartfd, v1, 0x40uLL);
if ( v0 <= 0 )
break;
qb_push(queuebuffer, v1, v0);
}
}
}

original_code:
void sigHandlerIO(int signumber) {
 if (queuebuffer) {
  char buffer[64];
  int bytes;
  while ((bytes = read(uartfd, buffer, 64)) > 0)
   qb_push(queuebuffer, buffer, bytes);
 }
}

variable_types: signumber int

variable_types:
buffer
char [64]

variable_types: bytes int