pop

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

if ( !last )
return (unsigned int)-1;
v0 = *last;
delete_node(last);
return v0;
}

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

if ( !last )
return (unsigned int)-1;
v0 = *last;
func0(last);
return v0;
}

original_code:
int pop() {
 if (last == ((void *)0))
  return -1;
 int val = last->val;
 delete_node(last);
 return val;
}

variable_types: val int


clear

repo: dloghin/libdsa

decompiled_code:
__int64 clear()
{
_DWORD *v0; // rax

delete_list();
v0 = (_DWORD *)head;
*(_QWORD *)(head + 8LL) = 0LL;
last = v0;
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
_DWORD *v0; // rax

func1();
v0 = (_DWORD *)head;
*(_QWORD *)(head + 8LL) = 0LL;
last = v0;
return 0LL;
}

original_code:
int clear() {
 delete_list(head);
 head->next = ((void *)0);
 last = head;
 return SUCCESS;
}

return_type: int


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

result = "MinHeap";
if ( a1 != 1 )
{
result = "None";
if ( a1 == 2 )
result = "MaxHeap";
}
return result;
}

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

result = "MinHeap";
if ( a1 != 1 )
{
result = "None";
if ( a1 == 2 )
result = "MaxHeap";
}
return result;
}

original_code:
char* type2name(enum heap_type_t type)
{
 switch (type) {
 case MIN_HEAP:
  return "MinHeap";
 case MAX_HEAP:
  return "MaxHeap";
 default:
  return "None";
 }
}

canonical_original_code:
char *type2name(enum heap_type_t type)
{
 switch (type) {
 case MIN_HEAP:
  return "MinHeap";
 case MAX_HEAP:
  return "MaxHeap";
 default:
  return "None";
 }
}

variable_types: enum heap_type_t { MIN_HEAP=1, MAX_HEAP=2 }

return_type: char *


decompiled_code:
__int64 __fastcall alloc_int_heap(_QWORD *a1, int a2, int a3)
{
_QWORD *v4; // rbx
int v5; // ebp
void *v6; // rax
__int64 result; // rax

if ( *a1 )
return 4294967294LL;
v4 = malloc(0x20uLL);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = 8;
if ( a3 > 0 )
v5 = 1 << (mylog2((unsigned int)a3) + 1);
v6 = malloc(8LL * v5);
v4[3] = v6;
if ( v6 )
{
v4[1] = 0LL;
v4[2] = v5;
*(_DWORD *)v4 = a2;
*a1 = v4;
result = 0LL;
}
else
{
free(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, int a2, int a3)
{
_QWORD *v4; // rbx
int v5; // ebp
void *v6; // rax
__int64 result; // rax

if ( *a1 )
return 4294967294LL;
v4 = malloc(0x20uLL);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = 8;
if ( a3 > 0 )
v5 = 1 << (mylog2((unsigned int)a3) + 1);
v6 = malloc(8LL * v5);
v4[3] = v6;
if ( v6 )
{
v4[1] = 0LL;
v4[2] = v5;
*(_DWORD *)v4 = a2;
*a1 = v4;
result = 0LL;
}
else
{
free(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int alloc_int_heap(heap_int_t** heap, enum heap_type_t type, int size)
{
 int n;
 heap_int_t* h;
 if (*heap != ((void *)0))
  return ENULL;
 h = (heap_int_t*)malloc(sizeof(heap_int_t));
 if (!h)
  return EALLOC;
 if (size <= 0)
  n = 1 << 3;
 else
  n = 1 << (mylog2(size) + 1);
 h->data = (typeof(h->data))malloc(n * sizeof(h->data));
 if (!(h->data)) {
  free(h);
  return EALLOC;
 }
 h->size = 0;
 h->nalloc = n;
 h->type = type;
 *heap = h;
 return SUCCESS;
}

canonical_original_code:
int alloc_int_heap(struct _heap_int_t **heap, enum heap_type_t type, int size)
{
 int n;
 struct _heap_int_t *h;
 if (*heap != ((void *)0))
  return ENULL;
 h = (struct _heap_int_t *)malloc(sizeof(heap_int_t));
 if (!h)
  return EALLOC;
 if (size <= 0)
  n = 1 << 3;
 else
  n = 1 << (mylog2(size) + 1);
 h->data = (typeof(h->data))malloc(n * sizeof(h->data));
 if (!(h->data)) {
  free(h);
  return EALLOC;
 }
 h->size = 0;
 h->nalloc = n;
 h->type = type;
 *heap = h;
 return SUCCESS;
}

variable_types: struct _heap_int_t { enum heap_type_t { MIN_HEAP=1, MAX_HEAP=2 } type; unsigned long size; unsigned long nalloc; int *data; } **

variable_types: size int

variable_types: n int

user_defined_types: struct _heap_int_t { enum heap_type_t type; unsigned long size; unsigned long nalloc; int *data; }


decompiled_code:
void __fastcall destroy_int_heap(void **a1)
{
if ( a1 )
{
if ( *a1 )
{
free(*((void **)*a1 + 3));
free(*a1);
*a1 = 0LL;
}
}
}

original_code:
void destroy_int_heap(heap_int_t** heap)
{
 if (heap == ((void *)0))
  return;
 if (*heap == ((void *)0))
  return;
 free((*heap)->data);
 free(*heap);
 *heap = ((void *)0);
}

canonical_original_code:
void destroy_int_heap(struct _heap_int_t **heap)
{
 if (heap == ((void *)0))
  return;
 if (*heap == ((void *)0))
  return;
 free((*heap)->data);
 free(*heap);
 *heap = ((void *)0);
}

return_type: void

user_defined_types: enum heap_type_t { MIN_HEAP=1, MAX_HEAP=2 }


original_code:
int insert_int_heap(heap_int_t* heap, int val)
{
 if (heap == ((void *)0))
  return ENULL;
 if (heap->size == heap->nalloc)
  if (realloc_int_heap(heap) != SUCCESS)
   return EALLOC;
 int aux;
 int pos = heap->size;
 int parent = (pos-1)/2;
 heap->data[pos] = val;
 int cont = 1;
 switch (heap->type) {
 case MIN_HEAP:
  while (parent >= 0 && cont) {
   cont = 0;
   if (heap->data[pos] < heap->data[parent]) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[pos];
    heap->data[pos] = aux;
    pos = parent;
    parent = (parent-1)/2;
    cont = 1;
   }
  }
  break;
 case MAX_HEAP:
  while (parent >= 0 && cont) {
   cont = 0;
   if (heap->data[pos] > heap->data[parent]) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[pos];
    heap->data[pos] = aux;
    pos = parent;
    parent = (parent-1)/2;
    cont = 1;
   }
  }
  break;
 }
 return SUCCESS;
}

canonical_original_code:
int insert_int_heap(struct _heap_int_t *heap, int val)
{
 if (heap == ((void *)0))
  return ENULL;
 if (heap->size == heap->nalloc)
  if (realloc_int_heap(heap) != SUCCESS)
   return EALLOC;
 int aux;
 int pos = heap->size;
 int parent = (pos-1)/2;
 heap->data[pos] = val;
 int cont = 1;
 switch (heap->type) {
 case MIN_HEAP:
  while (parent >= 0 && cont) {
   cont = 0;
   if (heap->data[pos] < heap->data[parent]) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[pos];
    heap->data[pos] = aux;
    pos = parent;
    parent = (parent-1)/2;
    cont = 1;
   }
  }
  break;
 case MAX_HEAP:
  while (parent >= 0 && cont) {
   cont = 0;
   if (heap->data[pos] > heap->data[parent]) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[pos];
    heap->data[pos] = aux;
    pos = parent;
    parent = (parent-1)/2;
    cont = 1;
   }
  }
  break;
 }
 return SUCCESS;
}

variable_types: struct _heap_int_t { enum heap_type_t { MIN_HEAP=1, MAX_HEAP=2 } type; unsigned long size; unsigned long nalloc; int *data; } *

variable_types: aux int

variable_types: pos int

variable_types: parent int

variable_types: cont int


original_code:
int push_root_int_heap(heap_int_t* heap)
{
 int aux;
 int parent = 0;
 int child = 2*parent+1;
 int index = parent;
 switch (heap->type) {
 case MIN_HEAP:
  while (child < heap->size) {
   if (heap->data[index] > heap->data[child])
    index = child;
   if (child+1 < heap->size && (heap->data[index] > heap->data[child+1]))
    index = child+1;
   if (index != parent) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[index];
    heap->data[index] = aux;
    parent = index;
    child = 2*parent+1;
   }
   else
    break;
  }
  break;
 case MAX_HEAP:
  while (child < heap->size) {
   if (heap->data[index] < heap->data[child])
    index = child;
   if (child+1 < heap->size && (heap->data[index] < heap->data[child+1]))
    index = child+1;
   if (index != parent) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[index];
    heap->data[index] = aux;
    parent = index;
    child = 2*parent+1;
   }
   else
    break;
  }
  break;
 }
 return SUCCESS;
}

canonical_original_code:
int push_root_int_heap(struct _heap_int_t *heap)
{
 int aux;
 int parent = 0;
 int child = 2*parent+1;
 int index = parent;
 switch (heap->type) {
 case MIN_HEAP:
  while (child < heap->size) {
   if (heap->data[index] > heap->data[child])
    index = child;
   if (child+1 < heap->size && (heap->data[index] > heap->data[child+1]))
    index = child+1;
   if (index != parent) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[index];
    heap->data[index] = aux;
    parent = index;
    child = 2*parent+1;
   }
   else
    break;
  }
  break;
 case MAX_HEAP:
  while (child < heap->size) {
   if (heap->data[index] < heap->data[child])
    index = child;
   if (child+1 < heap->size && (heap->data[index] < heap->data[child+1]))
    index = child+1;
   if (index != parent) {
    aux = heap->data[parent];
    heap->data[parent] = heap->data[index];
    heap->data[index] = aux;
    parent = index;
    child = 2*parent+1;
   }
   else
    break;
  }
  break;
 }
 return SUCCESS;
}

variable_types: child int

variable_types: index int


original_code:
int heapify_int_heap(heap_int_t* heap, int index)
{
 if (heap == ((void *)0))
  return ENULL;
 if (heap->size <= index)
  return EBOUNDS;
 int aux;
 int idx_left;
 int idx_right;
 int pos = index;
 switch (heap->type) {
 case MIN_HEAP:
  while (pos >= 0) {
   idx_left = 2*index+1;
   idx_right = 2*index+2;
   if (idx_left < heap->size && heap->data[idx_left] < heap->data[pos])
    pos = idx_left;
   if (idx_right < heap->size && heap->data[idx_right] < heap->data[pos])
    pos = idx_right;
   if (pos != index) {
    aux = heap->data[index];
    heap->data[index] = heap->data[pos];
    heap->data[pos] = aux;
    index = pos;
   }
   else
    pos = -1;
  }
  break;
 case MAX_HEAP:
  while (pos >= 0) {
   idx_left = 2*index+1;
   idx_right = 2*index+2;
   if (idx_left < heap->size && heap->data[idx_left] > heap->data[pos])
    pos = idx_left;
   if (idx_right < heap->size && heap->data[idx_right] > heap->data[pos])
    pos = idx_right;
   if (pos != index) {
    aux = heap->data[index];
    heap->data[index] = heap->data[pos];
    heap->data[pos] = aux;
    index = pos;
   }
   else
    pos = -1;
  }
  break;
 }
 return SUCCESS;
}

canonical_original_code:
int heapify_int_heap(struct _heap_int_t *heap, int index)
{
 if (heap == ((void *)0))
  return ENULL;
 if (heap->size <= index)
  return EBOUNDS;
 int aux;
 int idx_left;
 int idx_right;
 int pos = index;
 switch (heap->type) {
 case MIN_HEAP:
  while (pos >= 0) {
   idx_left = 2*index+1;
   idx_right = 2*index+2;
   if (idx_left < heap->size && heap->data[idx_left] < heap->data[pos])
    pos = idx_left;
   if (idx_right < heap->size && heap->data[idx_right] < heap->data[pos])
    pos = idx_right;
   if (pos != index) {
    aux = heap->data[index];
    heap->data[index] = heap->data[pos];
    heap->data[pos] = aux;
    index = pos;
   }
   else
    pos = -1;
  }
  break;
 case MAX_HEAP:
  while (pos >= 0) {
   idx_left = 2*index+1;
   idx_right = 2*index+2;
   if (idx_left < heap->size && heap->data[idx_left] > heap->data[pos])
    pos = idx_left;
   if (idx_right < heap->size && heap->data[idx_right] > heap->data[pos])
    pos = idx_right;
   if (pos != index) {
    aux = heap->data[index];
    heap->data[index] = heap->data[pos];
    heap->data[pos] = aux;
    index = pos;
   }
   else
    pos = -1;
  }
  break;
 }
 return SUCCESS;
}


decompiled_code:
int __fastcall print_tree_int_heap(__int64 a1)
{
const char *v1; // rax
int v2; // ebp
int i; // er15
unsigned __int64 v4; // rbx
int v5; // er13

v1 = type2name(*(_DWORD *)a1);
printf("Heap: size=%d, allocated=%d, type=%s\n", *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), v1);
v2 = 0;
for ( i = 0; (int)mylog2(*(unsigned int *)(a1 + 8)) >= i; ++i )
{
if ( 1 << i > 0 )
{
v4 = v2;
v5 = v2 + (1 << i);
while ( *(_QWORD *)(a1 + 8) > v4 )
{
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4 * v4));
++v2;
++v4;
if ( v2 == v5 )
{
v2 = v5;
break;
}
}
}
putchar(10);
}
return puts("Heap end.");
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
const char *v1; // rax
int v2; // ebp
int i; // er15
unsigned __int64 v4; // rbx
int v5; // er13

v1 = func0(*(_DWORD *)a1);
printf("Heap: size=%d, allocated=%d, type=%s\n", *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), v1);
v2 = 0;
for ( i = 0; (int)mylog2(*(unsigned int *)(a1 + 8)) >= i; ++i )
{
if ( 1 << i > 0 )
{
v4 = v2;
v5 = v2 + (1 << i);
while ( *(_QWORD *)(a1 + 8) > v4 )
{
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4 * v4));
++v2;
++v4;
if ( v2 == v5 )
{
v2 = v5;
break;
}
}
}
putchar(10);
}
return puts("Heap end.");
}

original_code:
void print_tree_int_heap(heap_int_t* heap)
{
 int h, i, j;
 printf("Heap: size=%d, allocated=%d, type=%s\n", heap->size, heap->nalloc, type2name(heap->type));
 i = 0;
 for (h=0; h<=mylog2(heap->size); h++) {
  for (j=0; j<(1<<h) && i<heap->size; j++, i++)
   printf("%d ", heap->data[i]);
  printf("\n");
 }
 printf("Heap end.\n");
}

canonical_original_code:
void print_tree_int_heap(struct _heap_int_t *heap)
{
 int h; int i; int j;
 printf("Heap: size=%d, allocated=%d, type=%s\n", heap->size, heap->nalloc, type2name(heap->type));
 i = 0;
 for (h=0; h<=mylog2(heap->size); h++) {
  for (j=0; j<(1<<h) && i<heap->size; j++, i++)
   printf("%d ", heap->data[i]);
  printf("\n");
 }
 printf("Heap end.\n");
}

variable_types: h int

variable_types: i int

variable_types: j int


decompiled_code:
int __fastcall print_array_int_heap(__int64 a1)
{
const char *v1; // rax
unsigned __int64 v2; // rbx

v1 = type2name(*(_DWORD *)a1);
printf("Heap: size=%d, allocated=%d, type=%s\n", *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), v1);
if ( *(_QWORD *)(a1 + 8) )
{
v2 = 0LL;
do
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4 * v2++));
while ( *(_QWORD *)(a1 + 8) > v2 );
}
return puts("\nHeap end.");
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
const char *v1; // rax
unsigned __int64 v2; // rbx

v1 = func0(*(_DWORD *)a1);
printf("Heap: size=%d, allocated=%d, type=%s\n", *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), v1);
if ( *(_QWORD *)(a1 + 8) )
{
v2 = 0LL;
do
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4 * v2++));
while ( *(_QWORD *)(a1 + 8) > v2 );
}
return puts("\nHeap end.");
}

original_code:
void print_array_int_heap(heap_int_t* heap)
{
 int i;
 printf("Heap: size=%d, allocated=%d, type=%s\n", heap->size, heap->nalloc, type2name(heap->type));
 for (i=0; i<heap->size; i++)
  printf("%d ", heap->data[i]);
 printf("\nHeap end.\n");
}

canonical_original_code:
void print_array_int_heap(struct _heap_int_t *heap)
{
 int i;
 printf("Heap: size=%d, allocated=%d, type=%s\n", heap->size, heap->nalloc, type2name(heap->type));
 for (i=0; i<heap->size; i++)
  printf("%d ", heap->data[i]);
 printf("\nHeap end.\n");
}


decompiled_code:
__int64 __fastcall build_int_heap_copy(__int64 *a1, int a2, int a3, __int64 a4)
{
__int64 v6; // rdx
int v7; // ebx
__int64 result; // rax

alloc_int_heap(a1, a2, a3);
if ( !*a1 )
return 4294967294LL;
if ( a3 <= 0 )
{
*(_QWORD *)(*a1 + 8) = a3;
if ( a3 >= -1 )
heapify_int_heap(*a1, a3 / 2);
result = 0LL;
}
else
{
v6 = 0LL;
do
{
*(_DWORD *)(*(_QWORD *)(*a1 + 24) + v6) = *(_DWORD *)(a4 + v6);
v6 += 4LL;
}
while ( v6 != 4LL * (unsigned int)a3 );
*(_QWORD *)(*a1 + 8) = a3;
v7 = a3 / 2;
do
heapify_int_heap(*a1, v7--);
while ( v7 >= 0 );
result = 0LL;
}
return result;
}

original_code:
int build_int_heap_copy(heap_int_t** heap, enum heap_type_t type, int n, int* values)
{
 int i;
 alloc_int_heap(heap, type, n);
 if (*heap == ((void *)0))
  return ENULL;
 for (i=0; i<n; i++)
  (*heap)->data[i] = values[i];
 (*heap)->size = n;
 for (i = n/2; i >= 0; i--)
  heapify_int_heap(*heap, i);
 return SUCCESS;
}

canonical_original_code:
int build_int_heap_copy(struct _heap_int_t **heap, enum heap_type_t type, int n, int *values)
{
 int i;
 alloc_int_heap(heap, type, n);
 if (*heap == ((void *)0))
  return ENULL;
 for (i=0; i<n; i++)
  (*heap)->data[i] = values[i];
 (*heap)->size = n;
 for (i = n/2; i >= 0; i--)
  heapify_int_heap(*heap, i);
 return SUCCESS;
}

variable_types:
values
int *


decompiled_code:
__int64 __fastcall build_int_heap_attach(__int64 *a1, int a2, int a3, __int64 a4)
{
_QWORD *v6; // rax
int v7; // ebx

v6 = (_QWORD *)*a1;
if ( !*a1 )
{
v6 = malloc(0x20uLL);
if ( !v6 )
return 0xFFFFFFFFLL;
*a1 = (__int64)v6;
}
v6[1] = a3;
v6[2] = a3;
*(_DWORD *)v6 = a2;
v6[3] = a4;
v7 = a3 / 2;
if ( a3 < -1 )
return 0LL;
do
heapify_int_heap(*a1, v7--);
while ( v7 >= 0 );
return 0LL;
}

original_code:
int build_int_heap_attach(heap_int_t** heap, enum heap_type_t type, int n, int* values)
{
 int i;
 heap_int_t* h;
 if (*heap == ((void *)0)) {
  h = (heap_int_t*)malloc(sizeof(heap_int_t));
  if (!h)
   return EALLOC;
  *heap = h;
 }
 else
  h = *heap;
 h->size = n;
 h->nalloc = n;
 h->type = type;
 h->data = values;
 for (i = n/2; i >= 0; i--)
  heapify_int_heap(*heap, i);
 return SUCCESS;
}

canonical_original_code:
int build_int_heap_attach(struct _heap_int_t **heap, enum heap_type_t type, int n, int *values)
{
 int i;
 struct _heap_int_t *h;
 if (*heap == ((void *)0)) {
  h = (struct _heap_int_t *)malloc(sizeof(heap_int_t));
  if (!h)
   return EALLOC;
  *heap = h;
 }
 else
  h = *heap;
 h->size = n;
 h->nalloc = n;
 h->type = type;
 h->data = values;
 for (i = n/2; i >= 0; i--)
  heapify_int_heap(*heap, i);
 return SUCCESS;
}


decompiled_code:
void __fastcall heap_sort_int(int a1, __int64 a2)
{
__int64 v2; // rbx
unsigned __int64 v3; // rbp
int *v4; // rax
int v5; // edx
void *v6; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
build_int_heap_attach((__int64 *)&ptr, 2, a1, a2);
if ( a1 - 1 >= 0 )
{
v2 = 4LL * (a1 - 1);
v3 = 4 * (a1 - (unsigned __int64)(unsigned int)(a1 - 1)) - 8;
do
{
v4 = (int *)*((_QWORD *)ptr + 3);
v5 = *v4;
*v4 = v4[(unsigned __int64)v2 / 4];
*(_DWORD *)(*((_QWORD *)ptr + 3) + v2) = v5;
v6 = ptr;
--*((_QWORD *)ptr + 1);
push_root_int_heap((__int64)v6);
v2 -= 4LL;
}
while ( v2 != v3 );
}
free(ptr);
}

original_code:
void heap_sort_int(int n, int* array)
{
 int aux;
 int end_idx;
 heap_int_t* heap = ((void *)0);
 build_int_heap_attach(&heap, MAX_HEAP, n, array);
 for (end_idx=n-1; end_idx>=0; end_idx--) {
  aux = heap->data[0];
  heap->data[0] = heap->data[end_idx];
  heap->data[end_idx] = aux;
  heap->size--;
  push_root_int_heap(heap);
 }
 free(heap);
}

canonical_original_code:
void heap_sort_int(int n, int *array)
{
 int aux;
 int end_idx;
 struct _heap_int_t *heap = ((void *)0);
 build_int_heap_attach(&heap, MAX_HEAP, n, array);
 for (end_idx=n-1; end_idx>=0; end_idx--) {
  aux = heap->data[0];
  heap->data[0] = heap->data[end_idx];
  heap->data[end_idx] = aux;
  heap->size--;
  push_root_int_heap(heap);
 }
 free(heap);
}

variable_types:
array
int *


original_code:
int heapify_void_heap(heap_void_t* heap, int index)
{
 if (heap == ((void *)0))
  return EALLOC;
 if (heap->size <= index)
  return EBOUNDS;
 void* aux;
 int idx_left;
 int idx_right;
 int pos = index;
 while (pos >= 0) {
  idx_left = 2*index+1;
  idx_right = 2*index+2;
  if (idx_left < heap->size &&
    heap->compare(heap->data[idx_left], heap->data[pos]) > 0)
   pos = idx_left;
  if (idx_right < heap->size &&
    heap->compare(heap->data[idx_right], heap->data[pos]) > 0)
   pos = idx_right;
  if (pos != index) {
   aux = heap->data[index];
   heap->data[index] = heap->data[pos];
   heap->data[pos] = aux;
   index = pos;
  }
  else
   pos = -1;
 }
 return SUCCESS;
}

canonical_original_code:
int heapify_void_heap(struct _heap_void_t *heap, int index)
{
 if (heap == ((void *)0))
  return EALLOC;
 if (heap->size <= index)
  return EBOUNDS;
 void *aux;
 int idx_left;
 int idx_right;
 int pos = index;
 while (pos >= 0) {
  idx_left = 2*index+1;
  idx_right = 2*index+2;
  if (idx_left < heap->size &&
    heap->compare(heap->data[idx_left], heap->data[pos]) > 0)
   pos = idx_left;
  if (idx_right < heap->size &&
    heap->compare(heap->data[idx_right], heap->data[pos]) > 0)
   pos = idx_right;
  if (pos != index) {
   aux = heap->data[index];
   heap->data[index] = heap->data[pos];
   heap->data[pos] = aux;
   index = pos;
  }
  else
   pos = -1;
 }
 return SUCCESS;
}

variable_types: struct _heap_void_t { int (*compare)(void *a, void *b); unsigned long size; unsigned long nalloc; void **data; } *

variable_types:
aux
void *


original_code:
int push_root_void_heap(heap_void_t* heap)
{
 void* aux;
 int parent = 0;
 int child = 2*parent+1;
 int index = parent;
 while (child < heap->size) {
  if (heap->compare(heap->data[index], heap->data[child]) < 0)
   index = child;
  if (child+1 < heap->size &&
    heap->compare(heap->data[index], heap->data[child+1]) < 0)
   index = child+1;
  if (index != parent) {
   aux = heap->data[parent];
   heap->data[parent] = heap->data[index];
   heap->data[index] = aux;
   parent = index;
   child = 2*parent+1;
  }
  else
   break;
 }
 return SUCCESS;
}

canonical_original_code:
int push_root_void_heap(struct _heap_void_t *heap)
{
 void *aux;
 int parent = 0;
 int child = 2*parent+1;
 int index = parent;
 while (child < heap->size) {
  if (heap->compare(heap->data[index], heap->data[child]) < 0)
   index = child;
  if (child+1 < heap->size &&
    heap->compare(heap->data[index], heap->data[child+1]) < 0)
   index = child+1;
  if (index != parent) {
   aux = heap->data[parent];
   heap->data[parent] = heap->data[index];
   heap->data[index] = aux;
   parent = index;
   child = 2*parent+1;
  }
  else
   break;
 }
 return SUCCESS;
}

user_defined_types: struct _heap_void_t { int (*compare)(void *a, void *b); unsigned long size; unsigned long nalloc; void **data; }


decompiled_code:
__int64 __fastcall build_void_heap_attach(__int64 *a1, __int64 a2, int a3, __int64 a4)
{
_QWORD *v6; // rax
int v7; // ebx

if ( !a2 )
return 4294967294LL;
v6 = (_QWORD *)*a1;
if ( !*a1 )
{
v6 = malloc(0x20uLL);
if ( !v6 )
return 0xFFFFFFFFLL;
*a1 = (__int64)v6;
}
v6[1] = a3;
v6[2] = a3;
*v6 = a2;
v6[3] = a4;
v7 = a3 / 2;
if ( a3 < -1 )
return 0LL;
do
heapify_void_heap(*a1, v7--);
while ( v7 >= 0 );
return 0LL;
}

original_code:
int build_void_heap_attach(heap_void_t** heap, int (*comp)(const void* a, const void* b), int n, void** values)
{
 int i;
 heap_void_t* h;
 if (comp == ((void *)0))
  return ENULL;
 if (*heap == ((void *)0)) {
  h = (heap_void_t*)malloc(sizeof(heap_void_t));
  if (!h)
   return EALLOC;
   *heap = h;
 }
 else
  h = *heap;
 h->size = n;
 h->nalloc = n;
 h->compare = comp;
 h->data = values;
 for (i = n/2; i >= 0; i--)
  heapify_void_heap(*heap, i);
 return SUCCESS;
}

canonical_original_code:
int build_void_heap_attach(struct _heap_void_t **heap, int (*comp)(void *a, void *b), int n, void **values)
{
 int i;
 struct _heap_void_t *h;
 if (comp == ((void *)0))
  return ENULL;
 if (*heap == ((void *)0)) {
  h = (struct _heap_void_t *)malloc(sizeof(heap_void_t));
  if (!h)
   return EALLOC;
   *heap = h;
 }
 else
  h = *heap;
 h->size = n;
 h->nalloc = n;
 h->compare = comp;
 h->data = values;
 for (i = n/2; i >= 0; i--)
  heapify_void_heap(*heap, i);
 return SUCCESS;
}

variable_types: struct _heap_void_t { int (*compare)(void *a, void *b); unsigned long size; unsigned long nalloc; void **data; } **

variable_types:
comp
int (*)(void *a, void *b)

variable_types:
values
void **


decompiled_code:
void __fastcall heap_sort_void(int a1, __int64 a2, __int64 a3)
{
__int64 v3; // rbx
unsigned __int64 v4; // rbp
__int64 *v5; // rax
__int64 v6; // rdx
void *v7; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
build_void_heap_attach((__int64 *)&ptr, a3, a1, a2);
if ( a1 - 1 >= 0 )
{
v3 = 8LL * (a1 - 1);
v4 = 8 * (a1 - (unsigned __int64)(unsigned int)(a1 - 1)) - 16;
do
{
v5 = (__int64 *)*((_QWORD *)ptr + 3);
v6 = *v5;
*v5 = v5[(unsigned __int64)v3 / 8];
*(_QWORD *)(*((_QWORD *)ptr + 3) + v3) = v6;
v7 = ptr;
--*((_QWORD *)ptr + 1);
push_root_void_heap((__int64)v7);
v3 -= 8LL;
}
while ( v3 != v4 );
}
free(ptr);
}

original_code:
void heap_sort_void(int n, void** array, int (*comp)(const void* a, const void* b))
{
 void* aux;
 int end_idx;
 heap_void_t* heap = ((void *)0);
 build_void_heap_attach(&heap, comp, n, array);
 for (end_idx=n-1; end_idx>=0; end_idx--) {
  aux = heap->data[0];
  heap->data[0] = heap->data[end_idx];
  heap->data[end_idx] = aux;
  heap->size--;
  push_root_void_heap(heap);
 }
 free(heap);
}

canonical_original_code:
void heap_sort_void(int n, void **array, int (*comp)(void *a, void *b))
{
 void *aux;
 int end_idx;
 struct _heap_void_t *heap = ((void *)0);
 build_void_heap_attach(&heap, comp, n, array);
 for (end_idx=n-1; end_idx>=0; end_idx--) {
  aux = heap->data[0];
  heap->data[0] = heap->data[end_idx];
  heap->data[end_idx] = aux;
  heap->size--;
  push_root_void_heap(heap);
 }
 free(heap);
}

variable_types: n int

variable_types:
array
void **


decompiled_code:
__int64 __fastcall mylog2(int a1)
{
unsigned int v1; // edx
unsigned int i; // eax

v1 = 0;
for ( i = 0; i != 32; ++i )
{
if ( (a1 & 1) != 0 )
v1 = i;
a1 >>= 1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
unsigned int v1; // edx
unsigned int i; // eax

v1 = 0;
for ( i = 0; i != 32; ++i )
{
if ( (a1 & 1) != 0 )
v1 = i;
a1 >>= 1;
}
return v1;
}

original_code:
int mylog2(int n)
{
 int i;
 int log = 0;
 for (i=0; i<8*sizeof(n); i++) {
  if ((0x1 & n) == 0x1)
   log = i;
  n = n>>1;
 }
 return log;
}

variable_types: log int


original_code:
int partition_int(int* a, int n0, int n1)
{
 int pivot = a[n0];
 while (n0 < n1) {
  while (a[n0] < pivot)
   n0++;
  while (a[n1] > pivot)
   n1--;
  if (a[n0] == a[n1])
   n0++;
  else
   if (n0 < n1) {
    int aux = a[n0];
    a[n0] = a[n1];
    a[n1] = aux;
   }
 }
 return n1;
}

canonical_original_code:
int partition_int(int *a, int n0, int n1)
{
 int pivot = a[n0];
 while (n0 < n1) {
  while (a[n0] < pivot)
   n0++;
  while (a[n1] > pivot)
   n1--;
  if (a[n0] == a[n1])
   n0++;
  else
   if (n0 < n1) {
    int aux = a[n0];
    a[n0] = a[n1];
    a[n1] = aux;
   }
 }
 return n1;
}

variable_types:
a
int *

variable_types: pivot int

variable_types: aux int


decompiled_code:
__int64 __fastcall quick_select_bounds_int(__int64 a1, int a2, int a3, int a4)
{
int v4; // ebp
int v5; // ebx
int v8; // eax

if ( a4 < 0 )
return 0LL;
v4 = a2;
v5 = a3;
if ( a4 > a3 )
return 0LL;
if ( a3 <= a2 )
return 0LL;
do
{
v8 = partition_int(a1, v4, v5);
if ( a4 == v8 )
return *(unsigned int *)(a1 + 4LL * v8);
if ( a4 >= v8 )
v4 = v8 + 1;
else
v5 = v8 - 1;
}
while ( v4 < v5 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int a3, int a4)
{
int v4; // ebp
int v5; // ebx
int v8; // eax

if ( a4 < 0 )
return 0LL;
v4 = a2;
v5 = a3;
if ( a4 > a3 )
return 0LL;
if ( a3 <= a2 )
return 0LL;
do
{
v8 = func0(a1, v4, v5);
if ( a4 == v8 )
return *(unsigned int *)(a1 + 4LL * v8);
if ( a4 >= v8 )
v4 = v8 + 1;
else
v5 = v8 - 1;
}
while ( v4 < v5 );
return 0LL;
}

original_code:
int quick_select_bounds_int(int* a, int n0, int n1, int k)
{
 int m;
 if (k < 0 || k>n1)
  return 0;
 while (n0 < n1) {
  m = partition_int(a, n0, n1);
  if (m == k)
                 return a[m];
  else
   if (m > k)
                         n1 = m-1;
   else
    n0 = m+1;
 }
 return 0;
}

canonical_original_code:
int quick_select_bounds_int(int *a, int n0, int n1, int k)
{
 int m;
 if (k < 0 || k>n1)
  return 0;
 while (n0 < n1) {
  m = partition_int(a, n0, n1);
  if (m == k)
                 return a[m];
  else
   if (m > k)
                         n1 = m-1;
   else
    n0 = m+1;
 }
 return 0;
}

variable_types: k int

variable_types: m int


decompiled_code:
__int64 __fastcall quick_select_int(__int64 a1, int a2, int a3)
{
return quick_select_bounds_int(a1, 0, a2 - 1, a3 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, int a3)
{
return func2(a1, 0, a2 - 1, a3 - 1);
}

original_code:
int quick_select_int(int* a, int n, int k)
{
 return quick_select_bounds_int(a, 0, n-1, k-1);
}

canonical_original_code:
int quick_select_int(int *a, int n, int k)
{
 return quick_select_bounds_int(a, 0, n-1, k-1);
}


decompiled_code:
__int64 __fastcall bin_search_int(int a1, __int64 a2, int a3, int a4)
{
int v5; // esi
__int64 result; // rax
int v7; // ecx

v5 = a4;
if ( a3 > a4 )
return 0xFFFFFFFFLL;
while ( 1 )
{
result = (unsigned int)(a3 + ((v5 - a3) >> 1));
v7 = *(_DWORD *)(a2 + 4LL * (int)result);
if ( v7 == a1 )
break;
if ( v7 <= a1 )
a3 = result + 1;
else
v5 = result - 1;
if ( a3 > v5 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, int a3, int a4)
{
int v5; // esi
__int64 result; // rax
int v7; // ecx

v5 = a4;
if ( a3 > a4 )
return 0xFFFFFFFFLL;
while ( 1 )
{
result = (unsigned int)(a3 + ((v5 - a3) >> 1));
v7 = *(_DWORD *)(a2 + 4LL * (int)result);
if ( v7 == a1 )
break;
if ( v7 <= a1 )
a3 = result + 1;
else
v5 = result - 1;
if ( a3 > v5 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int bin_search_int(int val, int* vals, int n0, int n1) {
 int m;
 while (n0 <= n1) {
  m = n0 + ((n1 - n0)>>1);
  if (val == vals[m])
   return m;
  else
   if (val < vals[m])
    n1 = m-1;
   else
    n0 = m+1;
 }
 return -1;
}

canonical_original_code:
int bin_search_int(int val, int *vals, int n0, int n1) {
 int m;
 while (n0 <= n1) {
  m = n0 + ((n1 - n0)>>1);
  if (val == vals[m])
   return m;
  else
   if (val < vals[m])
    n1 = m-1;
   else
    n0 = m+1;
 }
 return -1;
}

variable_types: val int

variable_types:
vals
int *


decompiled_code:
__int64 __fastcall bin_search(__int64 a1, __int64 a2, int a3, int a4, unsigned int (__fastcall *a5)(__int64, _QWORD))
{
int v5; // ebp
int v6; // er13
int v8; // ebx
_QWORD *v9; // r12

v5 = a3;
v6 = a4;
if ( a3 <= a4 )
{
while ( 1 )
{
v8 = v5 + ((v6 - v5) >> 1);
v9 = (_QWORD *)(a2 + 8LL * v8);
if ( !a5(a1, *v9) )
break;
if ( (a5(a1, *v9) & 0x80000000) != 0 )
v6 = v8 - 1;
else
v5 = v8 + 1;
if ( v5 > v6 )
return (unsigned int)-1;
}
}
else
{
v8 = -1;
}
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3, int a4, unsigned int (__fastcall *a5)(__int64, _QWORD))
{
int v5; // ebp
int v6; // er13
int v8; // ebx
_QWORD *v9; // r12

v5 = a3;
v6 = a4;
if ( a3 <= a4 )
{
while ( 1 )
{
v8 = v5 + ((v6 - v5) >> 1);
v9 = (_QWORD *)(a2 + 8LL * v8);
if ( !a5(a1, *v9) )
break;
if ( (a5(a1, *v9) & 0x80000000) != 0 )
v6 = v8 - 1;
else
v5 = v8 + 1;
if ( v5 > v6 )
return (unsigned int)-1;
}
}
else
{
v8 = -1;
}
return (unsigned int)v8;
}

original_code:
int bin_search(void* val, void** vals, int n0, int n1, int (*comp)(void* a, void* b)) {
 int m;
 while (n0 <= n1) {
  m = n0 + ((n1 - n0)>>1);
  if (comp(val, vals[m]) == 0)
   return m;
  else
   if (comp(val, vals[m]) < 0)
    n1 = m-1;
   else
    n0 = m+1;
 }
 return -1;
}

canonical_original_code:
int bin_search(void *val, void **vals, int n0, int n1, int (*comp)(void *a, void *b)) {
 int m;
 while (n0 <= n1) {
  m = n0 + ((n1 - n0)>>1);
  if (comp(val, vals[m]) == 0)
   return m;
  else
   if (comp(val, vals[m]) < 0)
    n1 = m-1;
   else
    n0 = m+1;
 }
 return -1;
}

variable_types:
val
void *

variable_types:
vals
void **


asynctask

decompiled_code:
int __fastcall asynctask(void *(*start_routine)(void *), void *arg)
{
pthread_attr_t v3; // [rsp+0h] [rbp-58h] BYREF
pthread_t newthread[4]; // [rsp+38h] [rbp-20h] BYREF

pthread_attr_init(&v3);
pthread_attr_setdetachstate(&v3, 1);
return pthread_create(newthread, &v3, start_routine, arg);
}

canonical_decompiled_code:
int __fastcall func0(void *(*start_routine)(void *), void *arg)
{
pthread_attr_t v3; // [rsp+0h] [rbp-58h] BYREF
pthread_t newthread[4]; // [rsp+38h] [rbp-20h] BYREF

pthread_attr_init(&v3);
pthread_attr_setdetachstate(&v3, 1);
return pthread_create(newthread, &v3, start_routine, arg);
}

original_code:
int asynctask(void* (*task)(void* args), void* arg) {
 pthread_t th;
 pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 return pthread_create(&th, &attr, task, arg);
}

canonical_original_code:
int asynctask(void *(*task)(void *args), void *arg) {
 unsigned long th;
 union pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 return pthread_create(&th, &attr, task, arg);
}

variable_types:
task
void *(*)(void *args)

variable_types:
arg
void *

variable_types:
th
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }


keepaliveRenew

decompiled_code:
int keepaliveRenew()
{
return setitimer(ITIMER_REAL, &it_1, 0LL);
}

canonical_decompiled_code:
int func0()
{
return setitimer(ITIMER_REAL, &it_1, 0LL);
}

original_code:
void keepaliveRenew()
{
  static struct itimerval it =
  {
    {3, 700000},
    {3, 700000}};
  setitimer(ITIMER_REAL, &it, ((void *)0));
}

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

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


keepaliveSend

decompiled_code:
int __fastcall keepaliveSend(unsigned int a1)
{
if ( verbose_flag )
fwrite("\nKeepAliveSend: keeping the IR tower alive...", 1uLL, 0x2DuLL, stderr);
if ( (unsigned int)mywrite(a1, &keepaliveByte, 1LL) != 1 )
{
myperror("sending keepalive");
exit(-1);
}
return keepaliveRenew();
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1)
{
if ( verbose_flag )
fwrite("\nKeepAliveSend: keeping the IR tower alive...", 1uLL, 0x2DuLL, stderr);
if ( (unsigned int)mywrite(a1, &keepaliveByte, 1LL) != 1 )
{
myperror("sending keepalive");
exit(-1);
}
return func0();
}

original_code:
static void keepaliveSend(int fd)
{
  if (verbose_flag)
    fputs("\nKeepAliveSend: keeping the IR tower alive...",stderr);
  if (mywrite(fd, &keepaliveByte, 1) != 1) {
    myperror("sending keepalive");
    exit(-1);
  }
  keepaliveRenew();
}

variable_types: fd int


keepaliveHandler

decompiled_code:
void keepaliveHandler()
{
unsigned int v0; // eax

if ( verbose_flag )
fwrite("\nHandler: keeping the IR tower alive...\n", 1uLL, 0x28uLL, stderr);
v0 = rcxFD();
keepaliveSend(v0);
}

canonical_decompiled_code:
void func2()
{
unsigned int v0; // eax

if ( verbose_flag )
fwrite("\nHandler: keeping the IR tower alive...\n", 1uLL, 0x28uLL, stderr);
v0 = rcxFD();
func1(v0);
}

original_code:
static void keepaliveHandler(int arg)
{
  if (verbose_flag)
    fputs("\nHandler: keeping the IR tower alive...\n",stderr);
  keepaliveSend(rcxFD());
}

variable_types: arg int


keepaliveInit

decompiled_code:
int keepaliveInit()
{
unsigned int v0; // eax

if ( verbose_flag )
fwrite("KeepAlive Init...", 1uLL, 0x11uLL, stderr);
signal(14, (__sighandler_t)keepaliveHandler);
v0 = rcxFD();
return keepaliveSend(v0);
}

canonical_decompiled_code:
int func3()
{
unsigned int v0; // eax

if ( verbose_flag )
fwrite("KeepAlive Init...", 1uLL, 0x11uLL, stderr);
signal(14, (__sighandler_t)keepaliveHandler);
v0 = rcxFD();
return func1(v0);
}

original_code:
void keepaliveInit(void)
{
  if (verbose_flag) fputs("KeepAlive Init...", stderr);
  signal(14, keepaliveHandler);
  keepaliveSend(rcxFD());
}

canonical_original_code:
void keepaliveInit()
{
  if (verbose_flag) fputs("KeepAlive Init...", stderr);
  signal(14, keepaliveHandler);
  keepaliveSend(rcxFD());
}


keepaliveShutdown

decompiled_code:
__sighandler_t keepaliveShutdown()
{
setitimer(ITIMER_REAL, &it_0, 0LL);
return signal(14, 0LL);
}

canonical_decompiled_code:
__sighandler_t func4()
{
setitimer(ITIMER_REAL, &it_0, 0LL);
return signal(14, 0LL);
}

original_code:
void keepaliveShutdown()
{
  static const struct itimerval it =
  {
    {0, 0},
    {0, 0}};
  setitimer(ITIMER_REAL, &it, 0);
  signal(14, ((__sighandler_t) 0));
}

canonical_original_code:
void keepaliveShutdown()
{
  static struct itimerval it = {
    {0, 0},
    {0, 0}};
  setitimer(ITIMER_REAL, &it, 0);
  signal(14, ((void (*)(int)) 0));
}

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


decompiled_code:
__int64 __fastcall lx_write(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
unsigned int v4; // er14
__int16 *v5; // rbp
unsigned int v6; // er14
unsigned int v7; // ebp
__int64 v8; // rbp
unsigned int v9; // er13
__int16 buf[21]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = creat(a2, 0x1A0u);
v3 = v2;
if ( v2 >= 0 )
{
v4 = write(v2, "brickOS", 8uLL);
v5 = (__int16 *)a1;
if ( v4 == 8 )
{
do
{
buf[0] = _bswap_16(*v5);
v6 = write(v3, buf, 2uLL);
if ( v6 != 2 )
{
close(v3);
return v6;
}
++v5;
}
while ( v5 != (__int16 *)(a1 + 16) );
v7 = write(v3, *(const void **)(a1 + 16), *(unsigned __int16 *)(a1 + 6) + *(unsigned __int16 *)(a1 + 4));
if ( *(unsigned __int16 *)(a1 + 6) + *(unsigned __int16 *)(a1 + 4) == v7 )
{
v8 = 0LL;
if ( *(_WORD *)(a1 + 14) )
{
while ( 1 )
{
buf[0] = _bswap_16(*(_WORD *)(*(_QWORD *)(a1 + 24) + 2 * v8));
v9 = write(v3, buf, 2uLL);
if ( v9 != 2 )
break;
if ( *(unsigned __int16 *)(a1 + 14) <= (int)++v8 )
goto LABEL_9;
}
close(v3);
v3 = v9;
}
else
{
LABEL_9:
close(v3);
v3 = 0;
}
}
else
{
close(v3);
v3 = v7;
}
}
else
{
close(v3);
v3 = v4;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
unsigned int v4; // er14
__int16 *v5; // rbp
unsigned int v6; // er14
unsigned int v7; // ebp
__int64 v8; // rbp
unsigned int v9; // er13
__int16 buf[21]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = creat(a2, 0x1A0u);
v3 = v2;
if ( v2 >= 0 )
{
v4 = write(v2, "brickOS", 8uLL);
v5 = (__int16 *)a1;
if ( v4 == 8 )
{
do
{
buf[0] = func0(*v5);
v6 = write(v3, buf, 2uLL);
if ( v6 != 2 )
{
close(v3);
return v6;
}
++v5;
}
while ( v5 != (__int16 *)(a1 + 16) );
v7 = write(v3, *(const void **)(a1 + 16), *(unsigned __int16 *)(a1 + 6) + *(unsigned __int16 *)(a1 + 4));
if ( *(unsigned __int16 *)(a1 + 6) + *(unsigned __int16 *)(a1 + 4) == v7 )
{
v8 = 0LL;
if ( *(_WORD *)(a1 + 14) )
{
while ( 1 )
{
buf[0] = func0(*(_WORD *)(*(_QWORD *)(a1 + 24) + 2 * v8));
v9 = write(v3, buf, 2uLL);
if ( v9 != 2 )
break;
if ( *(unsigned __int16 *)(a1 + 14) <= (int)++v8 )
goto LABEL_9;
}
close(v3);
v3 = v9;
}
else
{
LABEL_9:
close(v3);
v3 = 0;
}
}
else
{
close(v3);
v3 = v7;
}
}
else
{
close(v3);
v3 = v4;
}
}
return v3;
}

original_code:
int lx_write(const lx_t *lx,const unsigned char *filename) {
  int i,rc,fd=creat(filename,0400 | 0200 | (0400 >> 3));
  unsigned short tmp;
  if(fd<0)
    return fd;
  if((rc=write(fd,"brickOS",8))!=(8)) { close(fd); return rc; };
  for(i=0; i<8; i++) {
    tmp=htons( ((unsigned short*)lx)[i] );
    if((rc=write(fd,&tmp,2))!=(2)) { close(fd); return rc; };
  }
  if((rc=write(fd,lx->text,lx->text_size + lx->data_size))!=(lx->text_size + lx->data_size)) { close(fd); return rc; };
  for(i=0; i<lx->num_relocs; i++) {
    tmp=htons( lx->reloc[i] );
    if((rc=write(fd,&tmp,2))!=(2)) { close(fd); return rc; };
  }
  close(fd);
  return 0;
}

canonical_original_code:
int lx_write(struct lx_t *lx, unsigned char *filename) {
  int i; int rc; int fd = creat(filename,0400 | 0200 | (0400 >> 3));
  unsigned short tmp;
  if(fd<0)
    return fd;
  if((rc=write(fd,"brickOS",8))!=(8)) { close(fd); return rc; };
  for(i=0; i<8; i++) {
    tmp=htons( ((unsigned short *)lx)[i] );
    if((rc=write(fd,&tmp,2))!=(2)) { close(fd); return rc; };
  }
  if((rc=write(fd,lx->text,lx->text_size + lx->data_size))!=(lx->text_size + lx->data_size)) { close(fd); return rc; };
  for(i=0; i<lx->num_relocs; i++) {
    tmp=htons( lx->reloc[i] );
    if((rc=write(fd,&tmp,2))!=(2)) { close(fd); return rc; };
  }
  close(fd);
  return 0;
}

variable_types: struct lx_t { unsigned short version; unsigned short base; unsigned short text_size; unsigned short data_size; unsigned short bss_size; unsigned short stack_size; unsigned short offset; unsigned short num_relocs; unsigned char *text; unsigned short *reloc; } *

variable_types:
filename
unsigned char *

variable_types: i int

variable_types: rc int

variable_types:
tmp
unsigned short


original_code:
int lx_read(lx_t *lx,const unsigned char *filename) {
  int i,rc,fd=open(filename,00);
  unsigned char buffer[8];
  unsigned short tmp;
  if(fd<0)
    return fd;
  if((rc=read(fd,buffer,8))!=(8)) { close(fd); return rc; };
  if(strcmp(buffer,"brickOS")) {
    close(fd);
    return -1;
  }
  for(i=0; i<8; i++) {
    if((rc=read(fd,&tmp,2))!=(2)) { close(fd); return rc; };
    ((unsigned short*)lx)[i]= ntohs(tmp);
  }
  if((lx->text=malloc(lx->text_size + lx->data_size))==0) {
    close(fd);
    return -1;
  }
  if((rc=read(fd,lx->text,lx->text_size + lx->data_size))!=(lx->text_size + lx->data_size)) { close(fd); return rc; };
  if(lx->num_relocs) {
    if((lx->reloc=malloc(sizeof(unsigned short)*lx->num_relocs))==0) {
      close(fd);
      return -1;
    }
    for(i=0; i<lx->num_relocs; i++) {
      if((rc=read(fd,&tmp,2))!=(2)) { close(fd); return rc; };
      lx->reloc[i]=ntohs(tmp);
    }
  }
  return 0;
}

canonical_original_code:
int lx_read(struct lx_t *lx, unsigned char *filename) {
  int i; int rc; int fd = open(filename,00);
  unsigned char buffer[8];
  unsigned short tmp;
  if(fd<0)
    return fd;
  if((rc=read(fd,buffer,8))!=(8)) { close(fd); return rc; };
  if(strcmp(buffer,"brickOS")) {
    close(fd);
    return -1;
  }
  for(i=0; i<8; i++) {
    if((rc=read(fd,&tmp,2))!=(2)) { close(fd); return rc; };
    ((unsigned short *)lx)[i]= ntohs(tmp);
  }
  if((lx->text=malloc(lx->text_size + lx->data_size))==0) {
    close(fd);
    return -1;
  }
  if((rc=read(fd,lx->text,lx->text_size + lx->data_size))!=(lx->text_size + lx->data_size)) { close(fd); return rc; };
  if(lx->num_relocs) {
    if((lx->reloc=malloc(sizeof(unsigned short)*lx->num_relocs))==0) {
      close(fd);
      return -1;
    }
    for(i=0; i<lx->num_relocs; i++) {
      if((rc=read(fd,&tmp,2))!=(2)) { close(fd); return rc; };
      lx->reloc[i]=ntohs(tmp);
    }
  }
  return 0;
}

variable_types:
buffer
unsigned char [8]

user_defined_types: struct lx_t { unsigned short version; unsigned short base; unsigned short text_size; unsigned short data_size; unsigned short bss_size; unsigned short stack_size; unsigned short offset; unsigned short num_relocs; unsigned char *text; unsigned short *reloc; }


original_code:
void lx_relocate(lx_t *lx,unsigned short base) {
  unsigned short diff=base-lx->base;
  int i;
  for(i=0; i<lx->num_relocs; i++) {
    unsigned char *ptr=lx->text+lx->reloc[i];
    unsigned short off =(ptr[0]<<8) | ptr[1];
    off+=diff;
    ptr[0]=off >> 8;
    ptr[1]=off & 0xff;
  }
  lx->base=base;
}

canonical_original_code:
void lx_relocate(struct lx_t *lx, unsigned short base) {
  unsigned short diff = base-lx->base;
  int i;
  for(i=0; i<lx->num_relocs; i++) {
    unsigned char *ptr = lx->text+lx->reloc[i];
    unsigned short off = (ptr[0]<<8) | ptr[1];
    off+=diff;
    ptr[0]=off >> 8;
    ptr[1]=off & 0xff;
  }
  lx->base=base;
}

variable_types:
base
unsigned short

variable_types:
diff
unsigned short

variable_types:
ptr
unsigned char *

variable_types:
off
unsigned short


rcxFD

decompiled_code:
__int64 rcxFD()
{
return (unsigned int)rcxFd;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)rcxFd;
}

original_code:
int rcxFD(void) {
  return rcxFd;
}

canonical_original_code:
int rcxFD() {
  return rcxFd;
}


myperror

decompiled_code:
void __fastcall myperror(const char *a1)
{
perror(a1);
}

canonical_decompiled_code:
void __fastcall func1(const char *a1)
{
perror(a1);
}

original_code:
void myperror(char *str) {
    perror(str);
}

variable_types:
str
char *


rcxInit

decompiled_code:
__int64 __fastcall rcxInit(char *file, int a2)
{
int v2; // ebp
speed_t v4; // ebx
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

if ( *file == 45 && !file[1] )
{
v2 = 0;
}
else
{
v2 = open(file, 130);
if ( v2 < 0 )
{
fprintf(stderr, "Error opening tty=%s, ", file);
perror("open");
return 0xFFFFFFFFLL;
}
}
if ( tty_usb )
{
LABEL_5:
rcxFd = v2;
return 0LL;
}
if ( isatty(v2) )
{
if ( !tty_usb )
{
*(_QWORD *)&v5.c_iflag = 0LL;
*(_QWORD *)&v5.c_cflag = 0LL;
*(_QWORD *)&v5.c_line = 0LL;
*(_QWORD *)&v5.c_cc[7] = 0LL;
*(_QWORD *)&v5.c_cc[15] = 0LL;
*(_QWORD *)&v5.c_cc[23] = 0LL;
*(_QWORD *)&v5.c_cc[31] = 0LL;
v5.c_ospeed = 0;
if ( a2 )
{
v5.c_cflag = 2224;
v4 = 12;
}
else
{
v5.c_cflag = 2992;
v4 = 11;
}
cfsetispeed(&v5, v4);
cfsetospeed(&v5, v4);
if ( tcsetattr(v2, 0, &v5) == -1 )
{
perror("tcsetattr");
exit(1);
}
}
goto LABEL_5;
}
close(v2);
fprintf(stderr, "%s: not a tty\n", file);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *file, int a2)
{
int v2; // ebp
speed_t v4; // ebx
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

if ( *file == 45 && !file[1] )
{
v2 = 0;
}
else
{
v2 = open(file, 130);
if ( v2 < 0 )
{
fprintf(stderr, "Error opening tty=%s, ", file);
perror("open");
return 0xFFFFFFFFLL;
}
}
if ( tty_usb )
{
LABEL_5:
rcxFd = v2;
return 0LL;
}
if ( isatty(v2) )
{
if ( !tty_usb )
{
*(_QWORD *)&v5.c_iflag = 0LL;
*(_QWORD *)&v5.c_cflag = 0LL;
*(_QWORD *)&v5.c_line = 0LL;
*(_QWORD *)&v5.c_cc[7] = 0LL;
*(_QWORD *)&v5.c_cc[15] = 0LL;
*(_QWORD *)&v5.c_cc[23] = 0LL;
*(_QWORD *)&v5.c_cc[31] = 0LL;
v5.c_ospeed = 0;
if ( a2 )
{
v5.c_cflag = 2224;
v4 = 12;
}
else
{
v5.c_cflag = 2992;
v4 = 11;
}
cfsetispeed(&v5, v4);
cfsetospeed(&v5, v4);
if ( tcsetattr(v2, 0, &v5) == -1 )
{
perror("tcsetattr");
exit(1);
}
}
goto LABEL_5;
}
close(v2);
fprintf(stderr, "%s: not a tty\n", file);
return 0xFFFFFFFFLL;
}

original_code:
int rcxInit(const char *tty, int highspeed)
{
  int fd;
    struct termios ios;
  if (*tty == '-' && !*(tty + 1))
    fd = 0;
  else if ((fd = open(tty, 02 | 0200)) < 0) {
    fprintf(stderr,"Error opening tty=%s, ",tty);
    perror("open");
    return -1;
  }
  if (tty_usb == 0 && !isatty(fd)) {
    close(fd);
    fprintf(stderr, "%s: not a tty\n", tty);
    return -1;
  }
  if (tty_usb == 0) {
  memset(&ios, 0, sizeof(ios));
  ios.c_cflag = 0000200 | 0004000 | 0000060 | (highspeed ? 0 : 0000400 | 0001000);
  cfsetispeed(&ios, highspeed ? 0000014 : 0000013);
  cfsetospeed(&ios, highspeed ? 0000014 : 0000013);
  if (tcsetattr(fd, 0, &ios) == -1) {
    perror("tcsetattr");
    exit(1);
     }
  }
  rcxFd=fd;
  return 0;
}

canonical_original_code:
int rcxInit(char *tty, int highspeed)
{
  int fd;
    struct termios ios;
  if (*tty == '-' && !*(tty + 1))
    fd = 0;
  else if ((fd = open(tty, 02 | 0200)) < 0) {
    fprintf(stderr,"Error opening tty=%s, ",tty);
    perror("open");
    return -1;
  }
  if (tty_usb == 0 && !isatty(fd)) {
    close(fd);
    fprintf(stderr, "%s: not a tty\n", tty);
    return -1;
  }
  if (tty_usb == 0) {
  memset(&ios, 0, sizeof(ios));
  ios.c_cflag = 0000200 | 0004000 | 0000060 | (highspeed ? 0 : 0000400 | 0001000);
  cfsetispeed(&ios, highspeed ? 0000014 : 0000013);
  cfsetospeed(&ios, highspeed ? 0000014 : 0000013);
  if (tcsetattr(fd, 0, &ios) == -1) {
    perror("tcsetattr");
    exit(1);
     }
  }
  rcxFd=fd;
  return 0;
}

variable_types:
tty
char *

variable_types: highspeed int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


rcxShutdown

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

result = close(rcxFd);
rcxFd = -1;
return result;
}

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

result = close(rcxFd);
rcxFd = -1;
return result;
}

original_code:
void rcxShutdown()
{
    close(rcxFd);
  rcxFd=-1;
}


decompiled_code:
int __fastcall lx_from_images(__int64 a1, unsigned __int16 *a2, unsigned __int16 a3)
{
unsigned __int16 v5; // r12
int v6; // ebx
unsigned __int8 v7; // r12
unsigned __int16 v8; // bp
__int64 v9; // rdx
__int64 v10; // rax
unsigned __int16 v11; // ax
unsigned __int16 v12; // cx
unsigned __int16 v13; // r8
unsigned __int16 v14; // r9
unsigned __int16 v15; // dx
void *v16; // rax
void *v17; // rdi
__int64 v18; // rax
unsigned int v19; // er12
__int64 v20; // rdx
__int16 v21; // bp
__int16 v22; // bx
unsigned __int8 v23; // dl
unsigned __int8 v24; // al
int v26; // [rsp+Ch] [rbp-3Ch]

v5 = a2[12];
v6 = (v5 - *a2) >> 8;
v7 = v5 - *a2;
v8 = a2[1] + a2[2];
if ( verbose_flag )
printf("diff_hi=0x%02x diff_lo=0x%02x\n", (unsigned __int8)v6, v7);
v9 = *(_QWORD *)a2;
LOWORD(v9) = 0;
v10 = *((_QWORD *)a2 + 3);
LOWORD(v10) = 0;
if ( v9 != v10 )
{
fwrite("image size mismatch\n", 1uLL, 0x14uLL, stderr);
exit(-1);
}
if ( a2[4] != a2[16] )
{
fwrite("starting offset mismatch\n", 1uLL, 0x19uLL, stderr);
exit(-1);
}
if ( !(_BYTE)v6 || !v7 )
{
fwrite("need images whose base offsets differ in high and low byte\n", 1uLL, 0x3BuLL, stderr);
exit(-1);
}
*(_WORD *)a1 = 0;
v11 = *a2;
*(_WORD *)(a1 + 2) = *a2;
v12 = a2[1];
*(_WORD *)(a1 + 4) = v12;
v13 = a2[2];
*(_WORD *)(a1 + 6) = v13;
v14 = a2[3];
*(_WORD *)(a1 + 8) = v14;
*(_WORD *)(a1 + 10) = a3;
v15 = a2[4];
*(_DWORD *)(a1 + 12) = v15;
if ( verbose_flag )
printf(
"base =0x%04x offset =0x%04x\ntext_size=0x%04x data_size=0x%04x bss_size=0x%04x stack_size=0x%04x\n",
v11,
v15,
v12,
v13,
v14,
a3);
v16 = malloc(0x8000uLL);
*(_QWORD *)(a1 + 24) = v16;
if ( !v16 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(-1);
}
v17 = malloc(v8);
*(_QWORD *)(a1 + 16) = v17;
if ( !v17 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(-1);
}
memcpy(v17, *((const void **)a2 + 2), v8);
LODWORD(v18) = v8;
v26 = v8;
if ( v8 )
{
v19 = 0;
do
{
v21 = *(unsigned __int8 *)(*((_QWORD *)a2 + 2) + (int)v19);
v18 = *((_QWORD *)a2 + 5);
v22 = *(unsigned __int8 *)(v18 + (int)v19);
if ( verbose_flag )
LODWORD(v18) = printf(
"0x%04x: %02x - %02x = %02x\n",
v19,
(unsigned __int8)v21,
(unsigned __int8)v22,
(unsigned __int8)(v22 - v21));
if ( (_BYTE)v21 != (_BYTE)v22 )
{
v23 = *(_BYTE *)(*((_QWORD *)a2 + 2) + (int)v19 + 1LL);
v24 = *(_BYTE *)(*((_QWORD *)a2 + 5) + (int)v19 + 1LL);
if ( v23 == v24 )
{
fprintf(stderr, "single byte difference at +0x%04x\n", v19);
exit(-1);
}
if ( (v23 | (unsigned __int16)(v21 << 8)) - *a2 != (v24 | (unsigned __int16)(v22 << 8)) - a2[12] )
{
fprintf(stderr, "relocation offset mismatch at +0x%04x\n", v19);
exit(-1);
}
if ( verbose_flag )
printf("reloc[%d]=0x%04x\n", *(unsigned __int16 *)(a1 + 14), v19);
v20 = *(_QWORD *)(a1 + 24);
LOWORD(v18) = *(_WORD *)(a1 + 14);
*(_WORD *)(a1 + 14) = v18 + 1;
LODWORD(v18) = (unsigned __int16)v18;
*(_WORD *)(v20 + 2LL * (unsigned __int16)v18) = v19++;
}
++v19;
}
while ( (int)v19 < v26 );
}
return v18;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, unsigned __int16 *a2, unsigned __int16 a3)
{
unsigned __int16 v5; // r12
int v6; // ebx
unsigned __int8 v7; // r12
unsigned __int16 v8; // bp
__int64 v9; // rdx
__int64 v10; // rax
unsigned __int16 v11; // ax
unsigned __int16 v12; // cx
unsigned __int16 v13; // r8
unsigned __int16 v14; // r9
unsigned __int16 v15; // dx
void *v16; // rax
void *v17; // rdi
__int64 v18; // rax
unsigned int v19; // er12
__int64 v20; // rdx
__int16 v21; // bp
__int16 v22; // bx
unsigned __int8 v23; // dl
unsigned __int8 v24; // al
int v26; // [rsp+Ch] [rbp-3Ch]

v5 = a2[12];
v6 = (v5 - *a2) >> 8;
v7 = v5 - *a2;
v8 = a2[1] + a2[2];
if ( verbose_flag )
printf("diff_hi=0x%02x diff_lo=0x%02x\n", (unsigned __int8)v6, v7);
v9 = *(_QWORD *)a2;
LOWORD(v9) = 0;
v10 = *((_QWORD *)a2 + 3);
LOWORD(v10) = 0;
if ( v9 != v10 )
{
fwrite("image size mismatch\n", 1uLL, 0x14uLL, stderr);
exit(-1);
}
if ( a2[4] != a2[16] )
{
fwrite("starting offset mismatch\n", 1uLL, 0x19uLL, stderr);
exit(-1);
}
if ( !(_BYTE)v6 || !v7 )
{
fwrite("need images whose base offsets differ in high and low byte\n", 1uLL, 0x3BuLL, stderr);
exit(-1);
}
*(_WORD *)a1 = 0;
v11 = *a2;
*(_WORD *)(a1 + 2) = *a2;
v12 = a2[1];
*(_WORD *)(a1 + 4) = v12;
v13 = a2[2];
*(_WORD *)(a1 + 6) = v13;
v14 = a2[3];
*(_WORD *)(a1 + 8) = v14;
*(_WORD *)(a1 + 10) = a3;
v15 = a2[4];
*(_DWORD *)(a1 + 12) = v15;
if ( verbose_flag )
printf(
"base =0x%04x offset =0x%04x\ntext_size=0x%04x data_size=0x%04x bss_size=0x%04x stack_size=0x%04x\n",
v11,
v15,
v12,
v13,
v14,
a3);
v16 = malloc(0x8000uLL);
*(_QWORD *)(a1 + 24) = v16;
if ( !v16 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(-1);
}
v17 = malloc(v8);
*(_QWORD *)(a1 + 16) = v17;
if ( !v17 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(-1);
}
memcpy(v17, *((const void **)a2 + 2), v8);
LODWORD(v18) = v8;
v26 = v8;
if ( v8 )
{
v19 = 0;
do
{
v21 = *(unsigned __int8 *)(*((_QWORD *)a2 + 2) + (int)v19);
v18 = *((_QWORD *)a2 + 5);
v22 = *(unsigned __int8 *)(v18 + (int)v19);
if ( verbose_flag )
LODWORD(v18) = printf(
"0x%04x: %02x - %02x = %02x\n",
v19,
(unsigned __int8)v21,
(unsigned __int8)v22,
(unsigned __int8)(v22 - v21));
if ( (_BYTE)v21 != (_BYTE)v22 )
{
v23 = *(_BYTE *)(*((_QWORD *)a2 + 2) + (int)v19 + 1LL);
v24 = *(_BYTE *)(*((_QWORD *)a2 + 5) + (int)v19 + 1LL);
if ( v23 == v24 )
{
fprintf(stderr, "single byte difference at +0x%04x\n", v19);
exit(-1);
}
if ( (v23 | (unsigned __int16)(v21 << 8)) - *a2 != (v24 | (unsigned __int16)(v22 << 8)) - a2[12] )
{
fprintf(stderr, "relocation offset mismatch at +0x%04x\n", v19);
exit(-1);
}
if ( verbose_flag )
printf("reloc[%d]=0x%04x\n", *(unsigned __int16 *)(a1 + 14), v19);
v20 = *(_QWORD *)(a1 + 24);
LOWORD(v18) = *(_WORD *)(a1 + 14);
*(_WORD *)(a1 + 14) = v18 + 1;
LODWORD(v18) = (unsigned __int16)v18;
*(_WORD *)(v20 + 2LL * (unsigned __int16)v18) = v19++;
}
++v19;
}
while ( (int)v19 < v26 );
}
return v18;
}

original_code:
void lx_from_images(lx_t *lx,image_t *img,unsigned short stack_size) {
  const unsigned char diff_hi=(img[1].base - img[0].base) >> 8;
  const unsigned char diff_lo=(img[1].base - img[0].base) & 0xff;
  const unsigned short size=img[0].text_size + img[0].data_size;
  int i;
  if(verbose_flag)
    printf("diff_hi=0x%02x diff_lo=0x%02x\n",diff_hi,diff_lo);
  if(img[0].text_size != img[1].text_size ||
     img[0].data_size != img[1].data_size ||
     img[0].bss_size != img[1].bss_size ) {
    fprintf(stderr,"image size mismatch\n");
    exit(-1);
  }
  if(img[0].offset != img[1].offset) {
    fprintf(stderr,"starting offset mismatch\n");
    exit(-1);
  }
  if(!diff_hi || !diff_lo) {
    fprintf(stderr,"need images whose base offsets differ in high and low byte\n");
    exit(-1);
  }
  lx->version =0;
  lx->base =img[0].base;
  lx->text_size =img[0].text_size;
  lx->data_size =img[0].data_size;
  lx->bss_size =img[0].bss_size;
  lx->stack_size=stack_size;
  lx->offset =img[0].offset;
  lx->num_relocs=0;
  if(verbose_flag)
    printf("base     =0x%04x offset   =0x%04x\n"
     "text_size=0x%04x data_size=0x%04x bss_size=0x%04x stack_size=0x%04x\n",
     lx->base,lx->offset,lx->text_size,lx->data_size,lx->bss_size,lx->stack_size);
  if((lx->reloc=malloc(16384*sizeof(unsigned short)))==((void *)0)) {
    fprintf(stderr,"out of memory\n");
    exit(-1);
  }
  if((lx->text=malloc(size))==((void *)0)) {
    fprintf(stderr,"out of memory\n");
    exit(-1);
  }
  memcpy(lx->text,img[0].text,size);
  for(i=0; i<size; i++) {
    unsigned char c0=img[0].text[i];
    unsigned char c1=img[1].text[i];
    if(verbose_flag)
      printf("0x%04x: %02x - %02x = %02x\n",i,c0,c1,0xff & (c1-c0));
    if(c0!=c1) {
      unsigned char l0=img[0].text[i+1];
      unsigned char l1=img[1].text[i+1];
      if(l0 == l1) {
  fprintf(stderr,"single byte difference at +0x%04x\n",i);
        exit(-1);
      } else {
  unsigned short addr0=((c0<<8) | l0) - img[0].base;
  unsigned short addr1=((c1<<8) | l1) - img[1].base;
  if(addr0!=addr1) {
    fprintf(stderr,"relocation offset mismatch at +0x%04x\n",i);
    exit(-1);
  }
  if(verbose_flag)
      printf("reloc[%d]=0x%04x\n",lx->num_relocs,i);
  lx->reloc[lx->num_relocs++]=(unsigned short)i++;
      }
    }
  }
}

canonical_original_code:
void lx_from_images(struct lx_t *lx, struct image_t *img, unsigned short stack_size) {
  unsigned char diff_hi = (img[1].base - img[0].base) >> 8;
  unsigned char diff_lo = (img[1].base - img[0].base) & 0xff;
  unsigned short size = img[0].text_size + img[0].data_size;
  int i;
  if(verbose_flag)
    printf("diff_hi=0x%02x diff_lo=0x%02x\n",diff_hi,diff_lo);
  if(img[0].text_size != img[1].text_size ||
     img[0].data_size != img[1].data_size ||
     img[0].bss_size != img[1].bss_size ) {
    fprintf(stderr,"image size mismatch\n");
    exit(-1);
  }
  if(img[0].offset != img[1].offset) {
    fprintf(stderr,"starting offset mismatch\n");
    exit(-1);
  }
  if(!diff_hi || !diff_lo) {
    fprintf(stderr,"need images whose base offsets differ in high and low byte\n");
    exit(-1);
  }
  lx->version =0;
  lx->base =img[0].base;
  lx->text_size =img[0].text_size;
  lx->data_size =img[0].data_size;
  lx->bss_size =img[0].bss_size;
  lx->stack_size=stack_size;
  lx->offset =img[0].offset;
  lx->num_relocs=0;
  if(verbose_flag)
    printf("base     =0x%04x offset   =0x%04x\n"
     "text_size=0x%04x data_size=0x%04x bss_size=0x%04x stack_size=0x%04x\n",
     lx->base,lx->offset,lx->text_size,lx->data_size,lx->bss_size,lx->stack_size);
  if((lx->reloc=malloc(16384*sizeof(unsigned short)))==((void *)0)) {
    fprintf(stderr,"out of memory\n");
    exit(-1);
  }
  if((lx->text=malloc(size))==((void *)0)) {
    fprintf(stderr,"out of memory\n");
    exit(-1);
  }
  memcpy(lx->text,img[0].text,size);
  for(i=0; i<size; i++) {
    unsigned char c0 = img[0].text[i];
    unsigned char c1 = img[1].text[i];
    if(verbose_flag)
      printf("0x%04x: %02x - %02x = %02x\n",i,c0,c1,0xff & (c1-c0));
    if(c0!=c1) {
      unsigned char l0 = img[0].text[i+1];
      unsigned char l1 = img[1].text[i+1];
      if(l0 == l1) {
  fprintf(stderr,"single byte difference at +0x%04x\n",i);
        exit(-1);
      } else {
  unsigned short addr0 = ((c0<<8) | l0) - img[0].base;
  unsigned short addr1 = ((c1<<8) | l1) - img[1].base;
  if(addr0!=addr1) {
    fprintf(stderr,"relocation offset mismatch at +0x%04x\n",i);
    exit(-1);
  }
  if(verbose_flag)
      printf("reloc[%d]=0x%04x\n",lx->num_relocs,i);
  lx->reloc[lx->num_relocs++]=(unsigned short)i++;
      }
    }
  }
}

variable_types: struct image_t { unsigned short base; unsigned short text_size; unsigned short data_size; unsigned short bss_size; unsigned short offset; unsigned char *text; } *

variable_types:
size
unsigned short


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r14
int v4; // ebp
int v5; // ebx
int v6; // eax
int v7; // eax
int v8; // er13
unsigned __int16 v10; // [rsp+8h] [rbp-80h] BYREF
int longind; // [rsp+Ch] [rbp-7Ch] BYREF
__int16 v12[2]; // [rsp+10h] [rbp-78h] BYREF
unsigned __int16 v13; // [rsp+14h] [rbp-74h]
unsigned __int16 v14; // [rsp+16h] [rbp-72h]
unsigned __int16 v15; // [rsp+18h] [rbp-70h]
unsigned __int16 v16; // [rsp+1Ah] [rbp-6Eh]
unsigned __int16 v17; // [rsp+1Ch] [rbp-6Ch]
unsigned __int16 v18; // [rsp+1Eh] [rbp-6Ah]
unsigned __int16 v19[12]; // [rsp+30h] [rbp-58h] BYREF
char v20[64]; // [rsp+48h] [rbp-40h] BYREF

v3 = 1024;
v4 = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "s:vd", &long_options, &longind);
if ( v5 == -1 )
break;
switch ( v5 )
{
case 's':
_isoc99_sscanf(optarg, "%x", &v10);
v3 = v10;
break;
case 'v':
verbose_flag = 1;
break;
case 'd':
v4 = 1;
break;
}
}
v6 = optind;
if ( argc - optind <= 2 )
{
fprintf(
stderr,
"usage: %s file.ds1 file.ds2 file.lx\n [-s<stacksize>] [-v] [-d]\n size in hex, please.\n",
*argv);
exit(1);
}
++optind;
image_load(v19, argv[v6]);
v7 = optind++;
image_load(v20, argv[v7]);
lx_from_images((__int64)v12, v19, v3);
v8 = lx_write(v12, argv[optind]);
if ( v8 )
{
fprintf(stderr, "error writing %s\n", argv[optind]);
v4 = v5;
}
else if ( v4 )
{
printf("version=%d\n", (unsigned __int16)v12[0]);
printf("base=0x%x\n", (unsigned __int16)v12[1]);
printf("text_size=%d\n", v13);
printf("data_size=%d\n", v14);
printf("bss_size=%d\n", v15);
printf("stack_size=%d\n", v16);
printf("start_offset=%d\n", v17);
printf("num_relocs=%d\n", v18);
printf("contiguous free memory required to download=%d\n", v15 + v13 + 2 * (unsigned int)v14);
v4 = v8;
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r14
int v4; // ebp
int v5; // ebx
int v6; // eax
int v7; // eax
int v8; // er13
unsigned __int16 v10; // [rsp+8h] [rbp-80h] BYREF
int longind; // [rsp+Ch] [rbp-7Ch] BYREF
__int16 v12[2]; // [rsp+10h] [rbp-78h] BYREF
unsigned __int16 v13; // [rsp+14h] [rbp-74h]
unsigned __int16 v14; // [rsp+16h] [rbp-72h]
unsigned __int16 v15; // [rsp+18h] [rbp-70h]
unsigned __int16 v16; // [rsp+1Ah] [rbp-6Eh]
unsigned __int16 v17; // [rsp+1Ch] [rbp-6Ch]
unsigned __int16 v18; // [rsp+1Eh] [rbp-6Ah]
unsigned __int16 v19[12]; // [rsp+30h] [rbp-58h] BYREF
char v20[64]; // [rsp+48h] [rbp-40h] BYREF

v3 = 1024;
v4 = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "s:vd", &long_options, &longind);
if ( v5 == -1 )
break;
switch ( v5 )
{
case 's':
_isoc99_sscanf(optarg, "%x", &v10);
v3 = v10;
break;
case 'v':
verbose_flag = 1;
break;
case 'd':
v4 = 1;
break;
}
}
v6 = optind;
if ( argc - optind <= 2 )
{
fprintf(
stderr,
"usage: %s file.ds1 file.ds2 file.lx\n [-s<stacksize>] [-v] [-d]\n size in hex, please.\n",
*argv);
exit(1);
}
++optind;
image_load(v19, argv[v6]);
v7 = optind++;
image_load(v20, argv[v7]);
func0((__int64)v12, v19, v3);
v8 = lx_write(v12, argv[optind]);
if ( v8 )
{
fprintf(stderr, "error writing %s\n", argv[optind]);
v4 = v5;
}
else if ( v4 )
{
printf("version=%d\n", (unsigned __int16)v12[0]);
printf("base=0x%x\n", (unsigned __int16)v12[1]);
printf("text_size=%d\n", v13);
printf("data_size=%d\n", v14);
printf("bss_size=%d\n", v15);
printf("stack_size=%d\n", v16);
printf("start_offset=%d\n", v17);
printf("num_relocs=%d\n", v18);
printf("contiguous free memory required to download=%d\n", v15 + v13 + 2 * (unsigned int)v14);
v4 = v8;
}
return v4;
}

original_code:
int main(int argc, char **argv) {
  image_t img[2];
  lx_t lx;
  int opt;
  int display_flag = 0;
  unsigned short stack_size=1024;
  int option_index;
  while((opt=getopt_long(argc, argv, "s:vd",
                        (struct option *)long_options, &option_index) )!=-1) {
    unsigned tmp;
    switch(opt) {
      case 's':
  sscanf(optarg,"%x",&tmp);
  stack_size=(unsigned short)tmp;
        break;
      case 'v':
  verbose_flag=1;
  break;
      case 'd':
  display_flag=1;
  break;
    }
  }
  if(argc-optind<3) {
    fprintf(stderr,"usage: %s file.ds1 file.ds2 file.lx\n"
             "       [-s<stacksize>] [-v] [-d]\n"
             "       size in hex, please.\n",
             argv[0]);
    exit(1);
  }
  image_load(img , argv[optind++]);
  image_load(img+1, argv[optind++]);
  lx_from_images(&lx,img,stack_size);
  if(lx_write(&lx, argv[optind])) {
    fprintf(stderr,"error writing %s\n",argv[optind]);
    return -1;
  }
  if (display_flag) {
    printf("version=%d\n", lx.version);
    printf("base=0x%x\n", lx.base);
    printf("text_size=%d\n", lx.text_size);
    printf("data_size=%d\n", lx.data_size);
    printf("bss_size=%d\n", lx.bss_size);
    printf("stack_size=%d\n", lx.stack_size);
    printf("start_offset=%d\n", lx.offset);
    printf("num_relocs=%d\n", lx.num_relocs);
    printf("contiguous free memory required to download=%d\n",
     lx.text_size + lx.data_size*2 + lx.bss_size);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  struct image_t img[2];
  struct lx_t lx;
  int opt;
  int display_flag = 0;
  unsigned short stack_size = 1024;
  int option_index;
  while((opt=getopt_long(argc, argv, "s:vd",
                        (struct option *)long_options, &option_index) )!=-1) {
    unsigned int tmp;
    switch(opt) {
      case 's':
  sscanf(optarg,"%x",&tmp);
  stack_size=(unsigned short)tmp;
        break;
      case 'v':
  verbose_flag=1;
  break;
      case 'd':
  display_flag=1;
  break;
    }
  }
  if(argc-optind<3) {
    fprintf(stderr,"usage: %s file.ds1 file.ds2 file.lx\n"
             "       [-s<stacksize>] [-v] [-d]\n"
             "       size in hex, please.\n",
             argv[0]);
    exit(1);
  }
  image_load(img , argv[optind++]);
  image_load(img+1, argv[optind++]);
  lx_from_images(&lx,img,stack_size);
  if(lx_write(&lx, argv[optind])) {
    fprintf(stderr,"error writing %s\n",argv[optind]);
    return -1;
  }
  if (display_flag) {
    printf("version=%d\n", lx.version);
    printf("base=0x%x\n", lx.base);
    printf("text_size=%d\n", lx.text_size);
    printf("data_size=%d\n", lx.data_size);
    printf("bss_size=%d\n", lx.bss_size);
    printf("stack_size=%d\n", lx.stack_size);
    printf("start_offset=%d\n", lx.offset);
    printf("num_relocs=%d\n", lx.num_relocs);
    printf("contiguous free memory required to download=%d\n",
     lx.text_size + lx.data_size*2 + lx.bss_size);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct image_t { unsigned short base; unsigned short text_size; unsigned short data_size; unsigned short bss_size; unsigned short offset; unsigned char *text; } [2]

variable_types: struct lx_t { unsigned short version; unsigned short base; unsigned short text_size; unsigned short data_size; unsigned short bss_size; unsigned short stack_size; unsigned short offset; unsigned short num_relocs; unsigned char *text; unsigned short *reloc; }

variable_types: opt int

variable_types:
tmp
unsigned int

user_defined_types: struct option { char *name; int has_arg; int *flag; int val; }


basename

decompiled_code:
const char *__fastcall basename(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

v2 = strrchr(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

v2 = strrchr(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

original_code:
char *basename (char *path)
{
    char *last = strrchr(path, '/');
    return (last) ? last + 1 : path;
}

canonical_original_code:
char *basename(char *path)
{
    char *last = strrchr(path, '/');
    return (last) ? last + 1 : path;
}

variable_types:
path
char *

variable_types:
last
char *


original_code:
int
srec_encode(srec_t *srec, char *line)
{
    int alen, count, sum = 0, pos;
    if (srec->type > 9)
 return -4;
    alen = ltab[srec->type];
    if (alen == 0)
 return -4;
    line += sprintf(line, "S%d", srec->type);
    if (srec->count > 32)
 return -6;
    count = srec->count + (alen / 2) + 1;
    line += sprintf(line, "%02X", count);
    sum += count;
    while (alen) {
 int value;
 alen -= 2;
 value = (srec->addr >> (alen * 4)) & 0xff;
 line += sprintf(line, "%02X", value);
 sum += value;
    }
    for (pos = 0; pos < srec->count; pos++) {
 line += sprintf(line, "%02X", srec->data[pos]);
 sum += srec->data[pos];
    }
    sprintf(line, "%02X\n", (~sum) & 0xff);
    return 0;
}

canonical_original_code:
int srec_encode(struct srec_t *srec, char *line)
{
    int alen; int count; int sum = 0; int pos;
    if (srec->type > 9)
 return -4;
    alen = ltab[srec->type];
    if (alen == 0)
 return -4;
    line += sprintf(line, "S%d", srec->type);
    if (srec->count > 32)
 return -6;
    count = srec->count + (alen / 2) + 1;
    line += sprintf(line, "%02X", count);
    sum += count;
    while (alen) {
 int value;
 alen -= 2;
 value = (srec->addr >> (alen * 4)) & 0xff;
 line += sprintf(line, "%02X", value);
 sum += value;
    }
    for (pos = 0; pos < srec->count; pos++) {
 line += sprintf(line, "%02X", srec->data[pos]);
 sum += srec->data[pos];
    }
    sprintf(line, "%02X\n", (~sum) & 0xff);
    return 0;
}

variable_types: struct srec_t { unsigned char type; unsigned long addr; unsigned char count; unsigned char data[32]; } *

variable_types:
line
char *

variable_types: alen int

variable_types: count int

variable_types: sum int

variable_types: pos int

variable_types: value int


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

switch ( a1 )
{
case -8:
result = "checksum error";
break;
case -7:
result = "length error";
break;
case -6:
result = "line too long";
break;
case -5:
result = "line too short";
break;
case -3:
result = "invalid character";
break;
case -2:
result = "invalid header";
break;
case -1:
result = "null string error";
break;
case 0:
result = "no error";
break;
default:
result = "unknown error";
break;
}
return result;
}

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

switch ( a1 )
{
case -8:
result = "checksum error";
break;
case -7:
result = "length error";
break;
case -6:
result = "line too long";
break;
case -5:
result = "line too short";
break;
case -3:
result = "invalid character";
break;
case -2:
result = "invalid header";
break;
case -1:
result = "null string error";
break;
case 0:
result = "no error";
break;
default:
result = "unknown error";
break;
}
return result;
}

original_code:
char *
srec_strerror (int error)
{
    switch (error) {
    case 0: return "no error";
    case -1: return "null string error";
    case -2: return "invalid header";
    case -3: return "invalid character";
    case -5: return "line too short";
    case -6: return "line too long";
    case -7: return "length error";
    case -8: return "checksum error";
    default: return "unknown error";
    }
}

canonical_original_code:
char *srec_strerror(int error)
{
    switch (error) {
    case 0: return "no error";
    case -1: return "null string error";
    case -2: return "invalid header";
    case -3: return "invalid character";
    case -5: return "line too short";
    case -6: return "line too long";
    case -7: return "length error";
    case -8: return "checksum error";
    default: return "unknown error";
    }
}

variable_types: error int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
unsigned int v4; // ebp
__int64 v5; // rbp
__int64 i; // rbx
unsigned __int16 v8; // [rsp+Eh] [rbp-405Ah] BYREF
char v9[16384]; // [rsp+10h] [rbp-4058h] BYREF
char v10[88]; // [rsp+4010h] [rbp-58h] BYREF

v3 = basename(*argv);
*argv = v3;
if ( argc != 2 )
{
fprintf(_bss_start, "usage: %s filename\n", v3);
exit(1);
}
build_image_name(v10, argv[1]);
v4 = srec_load(argv[1], v9, 0x4000, &v8);
printf("/* Image file generated from %s by %s. */\n", argv[1], *argv);
printf("/* See source for %s for license info. */\n", argv[1]);
putchar(10);
printf("int %s_len = %d;\n", v10, v4);
printf("unsigned char %s_image[] = {", v10);
if ( v4 )
{
v5 = v4 - 1;
for ( i = 0LL; ; ++i )
{
if ( (i & 0xF) == 0 )
printf("\n ");
printf("%3d,", (unsigned __int8)v9[i]);
if ( i == v5 )
break;
}
}
putchar(10);
puts("};");
printf("unsigned short %s_start = 0x%04x;\n", v10, v8);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
unsigned int v4; // ebp
__int64 v5; // rbp
__int64 i; // rbx
unsigned __int16 v8; // [rsp+Eh] [rbp-405Ah] BYREF
char v9[16384]; // [rsp+10h] [rbp-4058h] BYREF
char v10[88]; // [rsp+4010h] [rbp-58h] BYREF

v3 = func8(*argv);
*argv = v3;
if ( argc != 2 )
{
fprintf(_bss_start, "usage: %s filename\n", v3);
exit(1);
}
build_image_name(v10, argv[1]);
v4 = func7(argv[1], v9, 0x4000, &v8);
printf("/* Image file generated from %s by %s. */\n", argv[1], *argv);
printf("/* See source for %s for license info. */\n", argv[1]);
putchar(10);
printf("int %s_len = %d;\n", v10, v4);
printf("unsigned char %s_image[] = {", v10);
if ( v4 )
{
v5 = v4 - 1;
for ( i = 0LL; ; ++i )
{
if ( (i & 0xF) == 0 )
printf("\n ");
printf("%3d,", (unsigned __int8)v9[i]);
if ( i == v5 )
break;
}
}
putchar(10);
puts("};");
printf("unsigned short %s_start = 0x%04x;\n", v10, v8);
return 0;
}

original_code:
int main (int argc, char **argv)
{
    unsigned char image_name[64];
    unsigned char image[0x4000];
    unsigned short image_start;
    unsigned int image_len;
    int i;
    argv[0] = basename(argv[0]);
    if (argc != 2) {
 fprintf(stderr, "usage: %s filename\n", argv[0]);
 exit(1);
    }
    build_image_name(image_name, argv[1]);
    image_len = srec_load(argv[1], image, 0x4000, &image_start);
    printf("/* Image file generated from %s by %s. */\n", argv[1], argv[0]);
    printf("/* See source for %s for license info. */\n", argv[1]);
    printf("\n");
    printf("int %s_len = %d;\n", image_name, image_len);
    printf("unsigned char %s_image[] = {", image_name);
    for (i = 0; i < image_len; i++) {
 if (i % 16 == 0)
     printf("\n    ");
 printf("%3d,", image[i]);
    }
    printf("\n");
    printf("};\n");
    printf("unsigned short %s_start = 0x%04x;\n", image_name, image_start);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned char image_name[64];
    unsigned char image[16384];
    unsigned short image_start;
    unsigned int image_len;
    int i;
    argv[0] = basename(argv[0]);
    if (argc != 2) {
 fprintf(stderr, "usage: %s filename\n", argv[0]);
 exit(1);
    }
    build_image_name(image_name, argv[1]);
    image_len = srec_load(argv[1], image, 0x4000, &image_start);
    printf("/* Image file generated from %s by %s. */\n", argv[1], argv[0]);
    printf("/* See source for %s for license info. */\n", argv[1]);
    printf("\n");
    printf("int %s_len = %d;\n", image_name, image_len);
    printf("unsigned char %s_image[] = {", image_name);
    for (i = 0; i < image_len; i++) {
 if (i % 16 == 0)
     printf("\n    ");
 printf("%3d,", image[i]);
    }
    printf("\n");
    printf("};\n");
    printf("unsigned short %s_start = 0x%04x;\n", image_name, image_start);
    return 0;
}

variable_types:
image
unsigned char [16384]

variable_types: i int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx

v3 = 0;
while ( 1 )
{
switch ( getchar() )
{
case 27:
return 0;
case 32:
v3 = v3 == 0 ? 0x7F : 0;
goto LABEL_3;
case 50:
case 51:
v3 ^= 4u;
goto LABEL_3;
case 97:
v3 ^= 0x10u;
goto LABEL_3;
case 99:
case 120:
case 121:
v3 ^= 0x20u;
goto LABEL_3;
case 100:
v3 ^= 0x40u;
goto LABEL_3;
case 101:
v3 ^= 2u;
goto LABEL_3;
case 113:
v3 ^= 8u;
goto LABEL_3;
case 115:
case 119:
v3 ^= 1u;
LABEL_3:
show_it(v3);
break;
default:
continue;
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx

v3 = 0;
while ( 1 )
{
switch ( getchar() )
{
case 27:
return 0;
case 32:
v3 = v3 == 0 ? 0x7F : 0;
goto LABEL_3;
case 50:
case 51:
v3 ^= 4u;
goto LABEL_3;
case 97:
v3 ^= 0x10u;
goto LABEL_3;
case 99:
case 120:
case 121:
v3 ^= 0x20u;
goto LABEL_3;
case 100:
v3 ^= 0x40u;
goto LABEL_3;
case 101:
v3 ^= 2u;
goto LABEL_3;
case 113:
v3 ^= 8u;
goto LABEL_3;
case 115:
case 119:
v3 ^= 1u;
LABEL_3:
func7(v3);
break;
default:
continue;
}
}
}

original_code:
int main() {
 int value=0;
 int norefresh=0;
 while(1) {
  norefresh=0;
  switch(getchar()) {
  case '2':
  case '3':
   value^=4;
   break;
  case 'q':
   value^=8;
   break;
  case 'e':
   value^=2;
   break;
  case 'w':
  case 's':
   value^=1;
   break;
  case 'a':
   value^=16;
   break;
  case 'y':
  case 'x':
  case 'c':
   value^=32;
   break;
  case 'd':
   value^=64;
   break;
  case ' ':
   if(value)
    value=0;
   else
    value=0x7f;
   break;
  case 27:
   return 0;
  default:
   norefresh=1;
  }
  if(!norefresh)
   show_it(value);
 }
}

canonical_original_code:
int main() {
 int value = 0;
 int norefresh = 0;
 while(1) {
  norefresh=0;
  switch(getchar()) {
  case '2':
  case '3':
   value^=4;
   break;
  case 'q':
   value^=8;
   break;
  case 'e':
   value^=2;
   break;
  case 'w':
  case 's':
   value^=1;
   break;
  case 'a':
   value^=16;
   break;
  case 'y':
  case 'x':
  case 'c':
   value^=32;
   break;
  case 'd':
   value^=64;
   break;
  case ' ':
   if(value)
    value=0;
   else
    value=0x7f;
   break;
  case 27:
   return 0;
  default:
   norefresh=1;
  }
  if(!norefresh)
   show_it(value);
 }
}

variable_types: norefresh int


repo: dlowe/pdb

decompiled_code:
void sql_shutdown()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void sql_shutdown(void)
{
}

canonical_original_code:
static void sql_shutdown()
{
}


decompiled_code:
__int64 __fastcall sql_get_table_key(char *s1)
{
__int64 v1; // rbp
__int64 v2; // rbx

if ( partitioned_table_count <= 0 )
return 0LL;
v1 = 0LL;
while ( 1 )
{
lo(0LL, "sql_get_table_key(): %s == %s?", s1, *(const char **)(partitioned_tables + 16 * v1));
v2 = partitioned_tables + 16 * v1;
if ( !strcmp(s1, *(const char **)v2) )
break;
if ( partitioned_table_count <= (int)++v1 )
return 0LL;
}
return *(_QWORD *)(v2 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 v1; // rbp
__int64 v2; // rbx

if ( partitioned_table_count <= 0 )
return 0LL;
v1 = 0LL;
while ( 1 )
{
lo(0LL, "sql_get_table_key(): %s == %s?", s1, *(const char **)(partitioned_tables + 16 * v1));
v2 = partitioned_tables + 16 * v1;
if ( !strcmp(s1, *(const char **)v2) )
break;
if ( partitioned_table_count <= (int)++v1 )
return 0LL;
}
return *(_QWORD *)(v2 + 8);
}

original_code:
static char *sql_get_table_key(char *table)
{
    for (int i = 0; i < partitioned_table_count; ++i) {
        lo(LOG_DEBUG, "sql_get_table_key(): %s == %s?", table,
           partitioned_tables[i].name);
        if (strcmp(table, partitioned_tables[i].name) == 0) {
            return partitioned_tables[i].key;
        }
    }
    return ((void *)0);
}

variable_types:
table
char *


decompiled_code:
__int64 __fastcall sql_get_map_keys(char *a1)
{
char *v2; // rbx
size_t v3; // rax
char *v5; // rbp
char v6; // r13
const char *v7; // r12
const char *v8; // rbp
char *v9; // rax
const char *v10; // rbx
char *v11; // rbx
const char *v12; // rbx
const char *v13; // rbx
const char *v14; // rbx
__int64 v15; // rax

if ( !memcmp(a1, "update", 6uLL) )
{
v2 = a1 + 7;
v3 = strcspn(v2, " ");
if ( v3 )
{
v5 = &v2[v3];
v6 = v2[v3];
v2[v3] = 0;
v7 = (const char *)sql_get_table_key(v2);
*v5 = v6;
v8 = &v5[strspn(v5, " ")];
lo(0LL, "sql_get_map_keys: after finding table: %s", v8);
v9 = strstr(v8, "where");
if ( v9 )
{
v10 = &v9[strspn(v9, " ")];
lo(0LL, "sql_get_map_keys: after finding where: %s", v10);
v11 = strstr(v10, v7);
if ( v11 )
{
v12 = &v11[strlen(v7)];
v13 = &v12[strspn(v12, " ")];
lo(0LL, "sql_get_map_keys: after finding %s: %s", v7, v13);
if ( *v13 == 61 )
{
v14 = &v13[strspn(v13 + 1, " ") + 1];
lo(0LL, "sql_get_map_keys: after finding =: %s", v14);
v15 = atol(v14);
lo(0LL, "sql_get_map_keys: XX: %ld", v15);
}
}
}
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1)
{
char *v2; // rbx
size_t v3; // rax
char *v5; // rbp
char v6; // r13
const char *v7; // r12
const char *v8; // rbp
char *v9; // rax
const char *v10; // rbx
char *v11; // rbx
const char *v12; // rbx
const char *v13; // rbx
const char *v14; // rbx
__int64 v15; // rax

if ( !memcmp(a1, "update", 6uLL) )
{
v2 = a1 + 7;
v3 = strcspn(v2, " ");
if ( v3 )
{
v5 = &v2[v3];
v6 = v2[v3];
v2[v3] = 0;
v7 = (const char *)func1(v2);
*v5 = v6;
v8 = &v5[strspn(v5, " ")];
lo(0LL, "sql_get_map_keys: after finding table: %s", v8);
v9 = strstr(v8, "where");
if ( v9 )
{
v10 = &v9[strspn(v9, " ")];
lo(0LL, "sql_get_map_keys: after finding where: %s", v10);
v11 = strstr(v10, v7);
if ( v11 )
{
v12 = &v11[strlen(v7)];
v13 = &v12[strspn(v12, " ")];
lo(0LL, "sql_get_map_keys: after finding %s: %s", v7, v13);
if ( *v13 == 61 )
{
v14 = &v13[strspn(v13 + 1, " ") + 1];
lo(0LL, "sql_get_map_keys: after finding =: %s", v14);
v15 = atol(v14);
lo(0LL, "sql_get_map_keys: XX: %ld", v15);
}
}
}
}
}
return 0LL;
}

original_code:
long *sql_get_map_keys(char *sql)
{
    if (strncmp(sql, "update", 6) == 0) {
        char *p = sql + 7;
        if (strcspn(p, " ")) {
            size_t end_of_table = strcspn(p, " ");
            char save = p[end_of_table];
            p[end_of_table] = '\0';
            char *key = sql_get_table_key(p);
            p[end_of_table] = save;
            p += end_of_table;
            p += strspn(p, " ");
            lo(LOG_DEBUG, "sql_get_map_keys: after finding table: %s", p);
            char *w = strstr(p, "where");
            if (w != ((void *)0)) {
                p = w;
                p += strspn(p, " ");
                lo(LOG_DEBUG, "sql_get_map_keys: after finding where: %s", p);
                char *k = strstr(p, key);
                if (k != ((void *)0)) {
                    p = k + strlen(key);
                    p += strspn(p, " ");
                    lo(LOG_DEBUG, "sql_get_map_keys: after finding %s: %s",
                       key, p);
                    if (*p == '=') {
                        ++p;
                        p += strspn(p, " ");
                        lo(LOG_DEBUG, "sql_get_map_keys: after finding =: %s",
                           p);
                        long map_key = atol(p);
                        lo(LOG_DEBUG, "sql_get_map_keys: XX: %ld", map_key);
                    }
                }
            }
        }
    }
    return ((void *)0);
}

canonical_original_code:
long *sql_get_map_keys(char *sql)
{
    if (strncmp(sql, "update", 6) == 0) {
        char *p = sql + 7;
        if (strcspn(p, " ")) {
            unsigned long end_of_table = strcspn(p, " ");
            char save = p[end_of_table];
            p[end_of_table] = '\0';
            char *key = sql_get_table_key(p);
            p[end_of_table] = save;
            p += end_of_table;
            p += strspn(p, " ");
            lo(LOG_DEBUG, "sql_get_map_keys: after finding table: %s", p);
            char *w = strstr(p, "where");
            if (w != ((void *)0)) {
                p = w;
                p += strspn(p, " ");
                lo(LOG_DEBUG, "sql_get_map_keys: after finding where: %s", p);
                char *k = strstr(p, key);
                if (k != ((void *)0)) {
                    p = k + strlen(key);
                    p += strspn(p, " ");
                    lo(LOG_DEBUG, "sql_get_map_keys: after finding %s: %s",
                       key, p);
                    if (*p == '=') {
                        ++p;
                        p += strspn(p, " ");
                        lo(LOG_DEBUG, "sql_get_map_keys: after finding =: %s",
                           p);
                        long map_key = atol(p);
                        lo(LOG_DEBUG, "sql_get_map_keys: XX: %ld", map_key);
                    }
                }
            }
        }
    }
    return ((void *)0);
}

variable_types:
sql
char *

variable_types:
p
char *

variable_types: save char

variable_types:
key
char *

variable_types:
w
char *

variable_types:
k
char *

return_type: long *


decompiled_code:
__int64 __fastcall sql_get_table_type(char *s1)
{
__int64 v1; // rbx

if ( partitioned_table_count <= 0 )
return 0LL;
v1 = 0LL;
while ( 1 )
{
lo(0LL, "sql_get_table_type(): %s == %s?", s1, *(const char **)(partitioned_tables + 16 * v1));
if ( !strcmp(s1, *(const char **)(partitioned_tables + 16 * v1)) )
break;
if ( partitioned_table_count <= (int)++v1 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s1)
{
__int64 v1; // rbx

if ( partitioned_table_count <= 0 )
return 0LL;
v1 = 0LL;
while ( 1 )
{
lo(0LL, "sql_get_table_type(): %s == %s?", s1, *(const char **)(partitioned_tables + 16 * v1));
if ( !strcmp(s1, *(const char **)(partitioned_tables + 16 * v1)) )
break;
if ( partitioned_table_count <= (int)++v1 )
return 0LL;
}
return 1LL;
}

canonical_original_code:
enum sql_table_type sql_get_table_type(char *table)
{
    for (int i = 0; i < partitioned_table_count; ++i) {
        lo(LOG_DEBUG, "sql_get_table_type(): %s == %s?", table,
           partitioned_tables[i].name);
        if (strcmp(table, partitioned_tables[i].name) == 0) {
            return SQL_TABLE_TYPE_PARTITIONED;
        }
    }
    return SQL_TABLE_TYPE_MASTER;
}

return_type: enum sql_table_type { SQL_TABLE_TYPE_MASTER=0, SQL_TABLE_TYPE_PARTITIONED=1 }


decompiled_code:
_BOOL8 __fastcall sql_get_type(char *a1)
{
bool v2; // zf
_BOOL4 v3; // ecx
char *v4; // rbx
size_t v5; // rax
char *v7; // rbp
char v8; // r12
int v9; // eax

v2 = memcmp(a1, "update", 6uLL) == 0;
v3 = 1;
if ( v2 )
{
v4 = a1 + 7;
v5 = strcspn(v4, " ");
v3 = 1;
if ( v5 )
{
v7 = &v4[v5];
v8 = v4[v5];
v4[v5] = 0;
v9 = sql_get_table_type(v4);
*v7 = v8;
v3 = v9 != 0;
}
}
return v3;
}

original_code:
sql_type sql_get_type(char *sql)
{
    if (strncmp(sql, "update", 6) == 0) {
        char *p = sql + 7;
        if (strcspn(p, " ")) {
            size_t end_of_table = strcspn(p, " ");
            char save = p[end_of_table];
            p[end_of_table] = 0;
            sql_table_type table_type = sql_get_table_type(p);
            p[end_of_table] = save;
            switch (table_type) {
            case SQL_TABLE_TYPE_MASTER:
                return SQL_TYPE_MASTER;
            case SQL_TABLE_TYPE_PARTITIONED:
                return SQL_TYPE_PARTITIONED;
            };
        }
    }
    return SQL_TYPE_PARTITIONED;
}

canonical_original_code:
enum sql_type sql_get_type(char *sql)
{
    if (strncmp(sql, "update", 6) == 0) {
        char *p = sql + 7;
        if (strcspn(p, " ")) {
            unsigned long end_of_table = strcspn(p, " ");
            char save = p[end_of_table];
            p[end_of_table] = 0;
            enum sql_table_type table_type = sql_get_table_type(p);
            p[end_of_table] = save;
            switch (table_type) {
            case SQL_TABLE_TYPE_MASTER:
                return SQL_TYPE_MASTER;
            case SQL_TABLE_TYPE_PARTITIONED:
                return SQL_TYPE_PARTITIONED;
            };
        }
    }
    return SQL_TYPE_PARTITIONED;
}

variable_types: enum sql_table_type { SQL_TABLE_TYPE_MASTER=0, SQL_TABLE_TYPE_PARTITIONED=1 }

return_type: enum sql_type { SQL_TYPE_MASTER=0, SQL_TYPE_PARTITIONED=1 }


decompiled_code:
void log_close()
{
if ( l )
{
free(l);
l = 0LL;
}
if ( stream )
{
fclose(stream);
stream = 0LL;
}
}

canonical_decompiled_code:
void func0()
{
if ( l )
{
free(l);
l = 0LL;
}
if ( stream )
{
fclose(stream);
stream = 0LL;
}
}

original_code:
static void log_close(void)
{
    if (l.filename) {
        free(l.filename);
        l.filename = 0;
    }
    if (l.file) {
        fclose(l.file);
        l.file = 0;
    }
}

canonical_original_code:
static void log_close()
{
    if (l.filename) {
        free(l.filename);
        l.filename = 0;
    }
    if (l.file) {
        fclose(l.file);
        l.file = 0;
    }
}


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

v1 = strcasecmp(s1, "error");
result = 2LL;
if ( v1 )
{
v3 = strcasecmp(s1, "info");
result = 1LL;
if ( v3 )
result = strcasecmp(s1, "debug") != 0 ? 3 : 0;
}
return result;
}

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

v1 = strcasecmp(s1, "error");
result = 2LL;
if ( v1 )
{
v3 = strcasecmp(s1, "info");
result = 1LL;
if ( v3 )
result = strcasecmp(s1, "debug") != 0 ? 3 : 0;
}
return result;
}

original_code:
static log_level log_level_from_string(const char *string)
{
    if (strcasecmp(string, "error") == 0) {
        return LOG_ERROR;
    }
    if (strcasecmp(string, "info") == 0) {
        return LOG_INFO;
    }
    if (strcasecmp(string, "debug") == 0) {
        return LOG_DEBUG;
    }
    return LOG_NONE;
}

canonical_original_code:
static enum log_level log_level_from_string(char *string)
{
    if (strcasecmp(string, "error") == 0) {
        return LOG_ERROR;
    }
    if (strcasecmp(string, "info") == 0) {
        return LOG_INFO;
    }
    if (strcasecmp(string, "debug") == 0) {
        return LOG_DEBUG;
    }
    return LOG_NONE;
}

variable_types:
string
char *

return_type: enum log_level { LOG_DEBUG=0, LOG_INFO=1, LOG_ERROR=2, LOG_NONE=3 }


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

result = 1LL;
if ( (unsigned int)dword_4C0 <= 2 )
{
dword_4C4 = getpid();
fclose(stream);
stream = fopen(l, "a");
result = 1LL;
if ( !stream )
{
log_close();
result = 0LL;
}
}
return result;
}

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

result = 1LL;
if ( (unsigned int)dword_4C0 <= 2 )
{
dword_4C4 = getpid();
fclose(stream);
stream = fopen(l, "a");
result = 1LL;
if ( !stream )
{
func0();
result = 0LL;
}
}
return result;
}

original_code:
int log_reopen(void)
{
    if (l.level < LOG_NONE) {
        l.pid = getpid();
        fclose(l.file);
        l.file = fopen(l.filename, "a");
        if (!l.file) {
            log_close();
            return 0;
        }
    }
    return 1;
}

canonical_original_code:
int log_reopen()
{
    if (l.level < LOG_NONE) {
        l.pid = getpid();
        fclose(l.file);
        l.file = fopen(l.filename, "a");
        if (!l.file) {
            log_close();
            return 0;
        }
    }
    return 1;
}


lo

decompiled_code:
void lo(unsigned int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-D8h] BYREF

if ( dword_4C0 <= a1 && a1 <= 2 )
{
gettimeofday(&tv, 0LL);
va_start(arg, a2);
flockfile(stream);
fprintf(stream, "%d %10ld.%06ld %6d ", a1, tv.tv_sec, tv.tv_usec, (unsigned int)dword_4C4);
vfprintf(stream, a2, arg);
fputc(10, stream);
fflush(stream);
funlockfile(stream);
}
}

canonical_decompiled_code:
void func5(unsigned int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-D8h] BYREF

if ( dword_4C0 <= a1 && a1 <= 2 )
{
gettimeofday(&tv, 0LL);
va_start(arg, a2);
flockfile(stream);
fprintf(stream, "%d %10ld.%06ld %6d ", a1, tv.tv_sec, tv.tv_usec, (unsigned int)dword_4C4);
vfprintf(stream, a2, arg);
fputc(10, stream);
fflush(stream);
funlockfile(stream);
}
}

original_code:
void lo(log_level level, const char *format, ...)
{
    if (level < l.level) {
        return;
    }
    if (level >= LOG_NONE) {
        return;
    }
    struct timeval tv;
    gettimeofday(&tv, ((void *)0));
    va_list args;
    __builtin_va_start(args,format);
    flockfile(l.file);
    fprintf(l.file, "%d %10ld.%06ld %6d ", level, tv.tv_sec, (long)tv.tv_usec,
            l.pid);
    vfprintf(l.file, format, args);
    fprintf(l.file, "\n");
    fflush(l.file);
    funlockfile(l.file);
    __builtin_va_end(args);
}

canonical_original_code:
void lo(enum log_level level, char *format, ...)
{
    if (level < l.level) {
        return;
    }
    if (level >= LOG_NONE) {
        return;
    }
    struct timeval tv;
    gettimeofday(&tv, ((void *)0));
    __builtin_va_list args;
    __builtin_va_start(args,format);
    flockfile(l.file);
    fprintf(l.file, "%d %10ld.%06ld %6d ", level, tv.tv_sec, (long)tv.tv_usec,
            l.pid);
    vfprintf(l.file, format, args);
    fprintf(l.file, "\n");
    fflush(l.file);
    funlockfile(l.file);
    __builtin_va_end(args);
}

variable_types: enum log_level { LOG_DEBUG=0, LOG_INFO=1, LOG_ERROR=2, LOG_NONE=3 }

variable_types:
format
char *

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


decompiled_code:
__int64 __fastcall send_reply(unsigned int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64, int *))
{
int v4; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6[0] = 0;
do
v4 = a3(a1, a2, v6);
while ( v4 == 1 );
return (unsigned int)-(v4 == 0);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64, int *))
{
int v4; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v6[0] = 0;
do
v4 = a3(a1, a2, v6);
while ( v4 == 1 );
return (unsigned int)-(v4 == 0);
}

original_code:
static int send_reply(int fd, packet * p, packet_writer put_packet)
{
    int sent = 0;
    packet_status status;
    do {
        status = put_packet(fd, p, &sent);
    } while (status == PACKET_INCOMPLETE);
    if (status == PACKET_ERROR) {
        return -1;
    }
    return 0;
}

canonical_original_code:
static int send_reply(int fd, struct packet *p, enum packet_status (*put_packet)(int, struct packet *, int *))
{
    int sent = 0;
    enum packet_status status;
    do {
        status = put_packet(fd, p, &sent);
    } while (status == PACKET_INCOMPLETE);
    if (status == PACKET_ERROR) {
        return -1;
    }
    return 0;
}

variable_types: fd int

variable_types: struct packet { char *bytes; int allocated; int size; } *

variable_types: enum packet_status { PACKET_ERROR=0, PACKET_INCOMPLETE=1, PACKET_COMPLETE=2, PACKET_EOF=3 } (*)(int, struct packet *, int *)

variable_types: sent int


decompiled_code:
__int64 __fastcall read_command(unsigned int a1, _QWORD *a2, __int64 (__fastcall *a3)(_QWORD, _QWORD *))
{
unsigned int i; // eax

*a2 = 0LL;
for ( i = a3(a1, a2); ; i = a3(a1, a2) )
{
if ( i == 2 )
return 0LL;
if ( i <= 2 )
break;
if ( i == 3 )
return 0xFFFFFFFFLL;
LABEL_3:
;
}
if ( i )
goto LABEL_3;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, _QWORD *a2, __int64 (__fastcall *a3)(_QWORD, _QWORD *))
{
unsigned int i; // eax

*a2 = 0LL;
for ( i = a3(a1, a2); ; i = a3(a1, a2) )
{
if ( i == 2 )
return 0LL;
if ( i <= 2 )
break;
if ( i == 3 )
return 0xFFFFFFFFLL;
LABEL_3:
;
}
if ( i )
goto LABEL_3;
return 0xFFFFFFFFLL;
}

original_code:
static int read_command(int fd, packet * p, packet_reader get_packet)
{
    short read_complete = 0;
    p->bytes = 0;
    while (!read_complete) {
        switch (get_packet(fd, p)) {
        case PACKET_EOF:
        case PACKET_ERROR:
            return -1;
        case PACKET_INCOMPLETE:
            read_complete = 0;
            break;
        case PACKET_COMPLETE:
            read_complete = 1;
            break;
        };
    }
    return 0;
}

canonical_original_code:
static int read_command(int fd, struct packet *p, enum packet_status (*get_packet)(int, struct packet *))
{
    short read_complete = 0;
    p->bytes = 0;
    while (!read_complete) {
        switch (get_packet(fd, p)) {
        case PACKET_EOF:
        case PACKET_ERROR:
            return -1;
        case PACKET_INCOMPLETE:
            read_complete = 0;
            break;
        case PACKET_COMPLETE:
            read_complete = 1;
            break;
        };
    }
    return 0;
}

variable_types: enum packet_status { PACKET_ERROR=0, PACKET_INCOMPLETE=1, PACKET_COMPLETE=2, PACKET_EOF=3 } (*)(int, struct packet *)

user_defined_types: struct packet { char *bytes; int allocated; int size; }


decompiled_code:
__int64 __fastcall command_delegate_filter(unsigned __int16 a1)
{
return *(unsigned int *)(command_delegate_mask + 4LL * a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
return *(unsigned int *)(command_delegate_mask + 4LL * a1);
}

original_code:
static delegate_filter_result command_delegate_filter(delegate_id id)
{
    return command_delegate_mask[id];
}

canonical_original_code:
static enum delegate_filter_result command_delegate_filter(unsigned short id)
{
    return command_delegate_mask[id];
}

variable_types:
id
unsigned short

return_type: enum delegate_filter_result { DELEGATE_FILTER_USE=0, DELEGATE_FILTER_DONT_USE=1 }


decompiled_code:
__int64 command_delegate_all()
{
unsigned __int16 i; // bx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned __int16 i; // bx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = 0;
}
return result;
}

original_code:
static void command_delegate_all(void)
{
    for (delegate_id i = 0; i < delegate_get_count(); ++i) {
        command_delegate_mask[i] = DELEGATE_FILTER_USE;
    }
}

canonical_original_code:
static void command_delegate_all()
{
    for (unsigned short i = 0; i < delegate_get_count(); ++i) {
        command_delegate_mask[i] = DELEGATE_FILTER_USE;
    }
}

variable_types:
i
unsigned short


decompiled_code:
__int64 command_delegate_init()
{
unsigned __int16 v0; // ax
unsigned __int16 i; // bx
__int64 result; // rax

v0 = delegate_get_count();
command_delegate_mask = (__int64)malloc(2LL * v0);
for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned __int16 v0; // ax
unsigned __int16 i; // bx
__int64 result; // rax

v0 = delegate_get_count();
command_delegate_mask = (__int64)malloc(2LL * v0);
for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = 0;
}
return result;
}

original_code:
static void command_delegate_init(void)
{
    command_delegate_mask = malloc(sizeof(short) * delegate_get_count());
    for (delegate_id i = 0; i < delegate_get_count(); ++i) {
        command_delegate_mask[i] = DELEGATE_FILTER_USE;
    }
}

canonical_original_code:
static void command_delegate_init()
{
    command_delegate_mask = malloc(sizeof(short) * delegate_get_count());
    for (unsigned short i = 0; i < delegate_get_count(); ++i) {
        command_delegate_mask[i] = DELEGATE_FILTER_USE;
    }
}


decompiled_code:
__int64 command_delegate_master()
{
unsigned __int16 i; // bx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = (unsigned __int16)delegate_master_id() != i;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int16 i; // bx
__int64 result; // rax

for ( i = 0; ; ++i )
{
result = delegate_get_count();
if ( (unsigned __int16)result <= i )
break;
*(_DWORD *)(command_delegate_mask + 4LL * i) = (unsigned __int16)delegate_master_id() != i;
}
return result;
}

original_code:
static void command_delegate_master(void)
{
    for (delegate_id i = 0; i < delegate_get_count(); ++i) {
        if (i == delegate_master_id()) {
            command_delegate_mask[i] = DELEGATE_FILTER_USE;
        } else {
            command_delegate_mask[i] = DELEGATE_FILTER_DONT_USE;
        }
    }
}

canonical_original_code:
static void command_delegate_master()
{
    for (unsigned short i = 0; i < delegate_get_count(); ++i) {
        if (i == delegate_master_id()) {
            command_delegate_mask[i] = DELEGATE_FILTER_USE;
        } else {
            command_delegate_mask[i] = DELEGATE_FILTER_DONT_USE;
        }
    }
}