decompiled_code:
__int64 __fastcall tree_find_arg(__int64 a1, unsigned int (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // r12
__int64 v6; // r14
__int64 v7; // rbp

v4 = a1 + 8;
if ( (unsigned int)list_is_empty(a1 + 8) )
return 0LL;
v5 = list_first_link(v4);
v6 = list_last_link(v4);
if ( v5 == v6 )
return 0LL;
while ( 1 )
{
v7 = v5 - 40;
if ( a2(v5 - 40, a3) )
break;
v7 = tree_find_arg(v5 - 40, a2, a3);
if ( v7 )
break;
v5 = list_next(v5);
if ( v6 == v5 )
return 0LL;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // r12
__int64 v6; // r14
__int64 v7; // rbp

v4 = a1 + 8;
if ( (unsigned int)list_is_empty(a1 + 8) )
return 0LL;
v5 = list_first_link(v4);
v6 = list_last_link(v4);
if ( v5 == v6 )
return 0LL;
while ( 1 )
{
v7 = v5 - 40;
if ( a2(v5 - 40, a3) )
break;
v7 = func3(v5 - 40, a2, a3);
if ( v7 )
break;
v5 = list_next(v5);
if ( v6 == v5 )
return 0LL;
}
return v7;
}

original_code:
struct tree_link *
tree_find_arg(struct tree_link *tree,
 tree_link_arg_predicate_t predicate, void *arg)
{
 struct list_link *iter, *enditer;
 struct tree_link *item;
 if(list_is_empty(&tree->children))
  return ((ptr_t)0x0);
 iter = list_first_link(&tree->children);
 enditer = list_last_link(&tree->children);
 for(; iter != enditer; iter = list_next(iter))
 {
  item = ((struct tree_link*)((size_t)iter - ( (size_t) &((struct tree_link*)0x0)->listLink )));
  if (predicate(item, arg))
  {
   return item;
  }
  if((item = tree_find_arg(item, predicate, arg)) != ((ptr_t)0x0))
   return item;
 }
 return ((ptr_t)0x0);
}

canonical_original_code:
struct tree_link *tree_find_arg(struct tree_link *tree, int (*predicate)(struct tree_link *link, void *arg), void *arg)
{
 struct list_link *iter; struct list_link *enditer;
 struct tree_link *item;
 if(list_is_empty(&tree->children))
  return ((void *)0x0);
 iter = list_first_link(&tree->children);
 enditer = list_last_link(&tree->children);
 for(; iter != enditer; iter = list_next(iter))
 {
  item = ((struct tree_link *)((unsigned long)iter - ( (unsigned long) &((struct tree_link *)0x0)->listLink )));
  if (predicate(item, arg))
  {
   return item;
  }
  if((item = tree_find_arg(item, predicate, arg)) != ((void *)0x0))
   return item;
 }
 return ((void *)0x0);
}

variable_types:
tree
struct tree_link *

variable_types: int (*)(struct tree_link *link, void *arg)

variable_types: struct list_link { unsigned int poison; struct list { struct list_link l; } *list; struct list_link *next; } *

return_type: struct tree_link { struct tree_link *par; struct list { struct list_link { unsigned int poison; struct list *list; struct list_link *next; } l; } children; struct list_link listLink; } *


repo: Siriuscoder/nanokernel

decompiled_code:
__int64 __fastcall tree_foreach(__int64 a1, void (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 v3; // r13
__int64 result; // rax
__int64 v6; // rbp
__int64 v7; // r14

v3 = a1 + 8;
result = list_is_empty(a1 + 8);
if ( !(_DWORD)result )
{
v6 = list_first_link(v3);
result = list_last_link(v3);
v7 = result;
if ( v6 != result )
{
do
{
tree_foreach(v6 - 40, a2, a3);
a2(v6 - 40, a3);
result = list_next(v6);
v6 = result;
}
while ( v7 != result );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, void (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 v3; // r13
__int64 result; // rax
__int64 v6; // rbp
__int64 v7; // r14

v3 = a1 + 8;
result = list_is_empty(a1 + 8);
if ( !(_DWORD)result )
{
v6 = list_first_link(v3);
result = list_last_link(v3);
v7 = result;
if ( v6 != result )
{
do
{
func4(v6 - 40, a2, a3);
a2(v6 - 40, a3);
result = list_next(v6);
v6 = result;
}
while ( v7 != result );
}
}
return result;
}

original_code:
void
tree_foreach(struct tree_link *tree,
 tree_link_arg_predicate_t predicate, void *arg)
{
 struct list_link *iter, *enditer;
 struct tree_link *item;
 if(!list_is_empty(&tree->children))
 {
  iter = list_first_link(&tree->children);
  enditer = list_last_link(&tree->children);
  for(; iter != enditer; iter = list_next(iter))
  {
   item = ((struct tree_link*)((size_t)iter - ( (size_t) &((struct tree_link*)0x0)->listLink )));
   tree_foreach(item, predicate, arg);
   predicate(item, arg);
  }
 }
}

canonical_original_code:
void tree_foreach(struct tree_link *tree, int (*predicate)(struct tree_link *link, void *arg), void *arg)
{
 struct list_link *iter; struct list_link *enditer;
 struct tree_link *item;
 if(!list_is_empty(&tree->children))
 {
  iter = list_first_link(&tree->children);
  enditer = list_last_link(&tree->children);
  for(; iter != enditer; iter = list_next(iter))
  {
   item = ((struct tree_link *)((unsigned long)iter - ( (unsigned long) &((struct tree_link *)0x0)->listLink )));
   tree_foreach(item, predicate, arg);
   predicate(item, arg);
  }
 }
}

variable_types: struct tree_link { struct tree_link *par; struct list { struct list_link { unsigned int poison; struct list *list; struct list_link *next; } l; } children; struct list_link listLink; } *

variable_types:
arg
void *

return_type: void

user_defined_types: struct tree_link { struct tree_link *par; struct list children; struct list_link listLink; }


decompiled_code:
_BOOL8 k_heap_init()
{
k_memset(&memPool, 0LL, 520LL);
qword_458 = (__int64)malloc(0x2000000uLL);
qword_450 = qword_458;
qword_460 = qword_458 + 0x2000000;
return qword_458 != 0;
}

original_code:
bool
k_heap_init()
{
 int i;
 k_memset(&memPool, 0, sizeof(MemPool_t));
 memPool.phisicalMemBeginPtr = memPool.phisicalMemPtr =
  malloc((0x1000 * 256 * 32));
 memPool.phisicalMemPtrMax = memPool.phisicalMemBeginPtr +
  (0x1000 * 256 * 32);
 if(memPool.phisicalMemBeginPtr == ((ptr_t)0x0))
  return 0;
 return 1;
}

canonical_original_code:
int k_heap_init()
{
 int i;
 k_memset(&memPool, 0, sizeof(MemPool_t));
 memPool.phisicalMemBeginPtr = memPool.phisicalMemPtr =
  malloc((0x1000 * 256 * 32));
 memPool.phisicalMemPtrMax = memPool.phisicalMemBeginPtr +
  (0x1000 * 256 * 32);
 if(memPool.phisicalMemBeginPtr == ((void *)0x0))
  return 0;
 return 1;
}

variable_types: i int

return_type: int


decompiled_code:
__int64 __fastcall k_free(__int64 a1)
{
return k_pool_release(&memPool, a1 + ~(unsigned __int64)*(unsigned __int8 *)(a1 - 1));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return k_pool_release(&memPool, a1 + ~(unsigned __int64)*(unsigned __int8 *)(a1 - 1));
}

original_code:
void
k_free(ptr_t block)
{
 byte dividerSize = *(((byte *)block)-1);
 byte *rawBlock = ((byte *)block)-dividerSize-1;
 k_pool_release(&memPool, rawBlock);
}

canonical_original_code:
void k_free(void *block)
{
 unsigned char dividerSize = *(((byte *)block)-1);
 unsigned char *rawBlock = ((byte *)block)-dividerSize-1;
 k_pool_release(&memPool, rawBlock);
}

variable_types:
block
void *

variable_types:
dividerSize
unsigned char

variable_types:
rawBlock
unsigned char *


decompiled_code:
__int64 __fastcall get_memory_info(_QWORD *a1)
{
unsigned int v1; // edx
__int64 v2; // rcx
__int64 result; // rax

k_memset(a1, 0LL, 32LL);
v1 = qword_450;
v2 = 0LL;
*a1 = (unsigned int)(qword_460 - qword_450);
a1[1] = v1;
a1[2] = (unsigned int)qword_458 - v1;
do
{
result = (unsigned int)dword_358[v2];
if ( (_DWORD)result )
{
result = (unsigned int)((_DWORD)result << v2);
a1[3] += result;
}
++v2;
}
while ( v2 != 31 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
unsigned int v1; // edx
__int64 v2; // rcx
__int64 result; // rax

k_memset(a1, 0LL, 32LL);
v1 = qword_450;
v2 = 0LL;
*a1 = (unsigned int)(qword_460 - qword_450);
a1[1] = v1;
a1[2] = (unsigned int)qword_458 - v1;
do
{
result = (unsigned int)dword_358[v2];
if ( (_DWORD)result )
{
result = (unsigned int)((_DWORD)result << v2);
a1[3] += result;
}
++v2;
}
while ( v2 != 31 );
return result;
}

original_code:
void
get_memory_info(memInfo_t *info)
{
 int i;
    k_memset(info, 0, sizeof(memInfo_t));
 info->totalSize = (uint32_t)(memPool.phisicalMemPtrMax -
  memPool.phisicalMemBeginPtr);
 info->heapAddress = (uint32_t)memPool.phisicalMemBeginPtr;
 info->heapCached = (uint32_t)(memPool.phisicalMemPtr -
        memPool.phisicalMemBeginPtr);
    for(i = 0; i < 31; i++)
 {
  if(memPool.numAllocatedBlocks[i] != 0)
   info->memoryUsed += (memPool.numAllocatedBlocks[i] * (1<<i));
 }
}

canonical_original_code:
void get_memory_info(struct memInfo_t *info)
{
 int i;
    k_memset(info, 0, sizeof(memInfo_t));
 info->totalSize = (unsigned int)(memPool.phisicalMemPtrMax -
  memPool.phisicalMemBeginPtr);
 info->heapAddress = (unsigned int)memPool.phisicalMemBeginPtr;
 info->heapCached = (unsigned int)(memPool.phisicalMemPtr -
        memPool.phisicalMemBeginPtr);
    for(i = 0; i < 31; i++)
 {
  if(memPool.numAllocatedBlocks[i] != 0)
   info->memoryUsed += (memPool.numAllocatedBlocks[i] * (1<<i));
 }
}

variable_types: struct memInfo_t { unsigned long totalSize; unsigned long heapAddress; unsigned long heapCached; unsigned long memoryUsed; } *


decompiled_code:
__int64 k_print_memory_usage_info()
{
__int64 v0; // rbx
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

v0 = 0LL;
get_memory_info(v4);
k_print("Memory usage:\n");
k_print("Heap address: 0x%08x\n", v4[1]);
k_print("Memory cached: %d/%d bytes\n", v4[2], v4[0]);
k_print("Memory used: %d bytes\n", v4[3]);
k_print("Allocated blocks:\n");
do
{
while ( 1 )
{
v1 = (unsigned int)dword_358[v0];
if ( (_DWORD)v1 )
break;
if ( ++v0 == 31 )
return k_print("\n");
}
v2 = (unsigned int)(1 << v0++);
k_print("|%dx(%d)|", v1, v2);
}
while ( v0 != 31 );
return k_print("\n");
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rbx
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

v0 = 0LL;
func6(v4);
k_print("Memory usage:\n");
k_print("Heap address: 0x%08x\n", v4[1]);
k_print("Memory cached: %d/%d bytes\n", v4[2], v4[0]);
k_print("Memory used: %d bytes\n", v4[3]);
k_print("Allocated blocks:\n");
do
{
while ( 1 )
{
v1 = (unsigned int)dword_358[v0];
if ( (_DWORD)v1 )
break;
if ( ++v0 == 31 )
return k_print("\n");
}
v2 = (unsigned int)(1 << v0++);
k_print("|%dx(%d)|", v1, v2);
}
while ( v0 != 31 );
return k_print("\n");
}

original_code:
void
k_print_memory_usage_info()
{
 memInfo_t memInfo;
 int i;
 get_memory_info(&memInfo);
 k_print("Memory usage:\n");
 k_print("Heap address: 0x%08x\n", memInfo.heapAddress);
 k_print("Memory cached: %d/%d bytes\n", memInfo.heapCached, memInfo.totalSize);
 k_print("Memory used: %d bytes\n", memInfo.memoryUsed);
 k_print("Allocated blocks:\n");
 for(i = 0; i < 31; i++)
 {
  if(memPool.numAllocatedBlocks[i] != 0)
  k_print("|%dx(%d)|", memPool.numAllocatedBlocks[i], (1<<i));
 }
 k_print("\n");
}

canonical_original_code:
void k_print_memory_usage_info()
{
 struct memInfo_t memInfo;
 int i;
 get_memory_info(&memInfo);
 k_print("Memory usage:\n");
 k_print("Heap address: 0x%08x\n", memInfo.heapAddress);
 k_print("Memory cached: %d/%d bytes\n", memInfo.heapCached, memInfo.totalSize);
 k_print("Memory used: %d bytes\n", memInfo.memoryUsed);
 k_print("Allocated blocks:\n");
 for(i = 0; i < 31; i++)
 {
  if(memPool.numAllocatedBlocks[i] != 0)
  k_print("|%dx(%d)|", memPool.numAllocatedBlocks[i], (1<<i));
 }
 k_print("\n");
}

variable_types: struct memInfo_t { unsigned long totalSize; unsigned long heapAddress; unsigned long heapCached; unsigned long memoryUsed; }


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

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

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

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

original_code:
int
k_print(const char *format, ...)
{
 int ret;
 va_list args;
 __builtin_va_start(args,format);
 ret = vfprintf(stdout, format, args);
 __builtin_va_end(args);
 return ret;
}

canonical_original_code:
int k_print(char *format, ...)
{
 int ret;
 __builtin_va_list args;
 __builtin_va_start(args,format);
 ret = vfprintf(stdout, format, args);
 __builtin_va_end(args);
 return ret;
}

variable_types:
format
char *

variable_types: ret int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax

if ( (unsigned int)k_heap_init(argc, argv, envp) )
{
printf("%s TEST OK\n", "k_heap_init");
if ( (unsigned int)kmalloc_test() )
{
printf("%s TEST OK\n", "kmalloc_test");
if ( (unsigned int)run_vfs_test() )
{
printf("%s TEST OK\n", "run_vfs_test");
k_print_memory_usage_info();
result = 0;
}
else
{
printf("%s TEST FAILED at %s:%d\n", "run_vfs_test", "main.c", 38LL);
result = 1;
}
}
else
{
printf("%s TEST FAILED at %s:%d\n", "kmalloc_test", "main.c", 37LL);
result = 1;
}
}
else
{
printf("%s TEST FAILED at %s:%d\n", "k_heap_init", "main.c", 36LL);
result = 1;
}
return result;
}

original_code:
int
main(int argc, char** argv)
{
    if(!k_heap_init()) { printf("%s TEST FAILED at %s:%d\n", "k_heap_init", "main.c", 36); return 1; } else { printf("%s TEST OK\n", "k_heap_init"); };
 if(!kmalloc_test()) { printf("%s TEST FAILED at %s:%d\n", "kmalloc_test", "main.c", 37); return 1; } else { printf("%s TEST OK\n", "kmalloc_test"); };
    if(!run_vfs_test()) { printf("%s TEST FAILED at %s:%d\n", "run_vfs_test", "main.c", 38); return 1; } else { printf("%s TEST OK\n", "run_vfs_test"); };
    k_print_memory_usage_info();
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    if(!k_heap_init()) { printf("%s TEST FAILED at %s:%d\n", "k_heap_init", "main.c", 36); return 1; } else { printf("%s TEST OK\n", "k_heap_init"); };
 if(!kmalloc_test()) { printf("%s TEST FAILED at %s:%d\n", "kmalloc_test", "main.c", 37); return 1; } else { printf("%s TEST OK\n", "kmalloc_test"); };
    if(!run_vfs_test()) { printf("%s TEST FAILED at %s:%d\n", "run_vfs_test", "main.c", 38); return 1; } else { printf("%s TEST OK\n", "run_vfs_test"); };
    k_print_memory_usage_info();
    return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 kmalloc_test()
{
__int64 v0; // r13
char *v1; // r12
char *v2; // rbx
__int64 v3; // rax
char *v5; // rbx
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r13
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rdi
__int64 v12[20]; // [rsp+0h] [rbp-C8h] BYREF
char v13[40]; // [rsp+A0h] [rbp-28h] BYREF

v0 = 10LL;
v1 = (char *)v12;
v2 = (char *)v12;
do
{
v3 = k_malloc(v0);
*(_QWORD *)v2 = v3;
if ( !v3 )
return 0LL;
v2 += 8;
v0 *= 2LL;
}
while ( v2 != v13 );
v5 = (char *)v12;
k_print_memory_usage_info();
do
{
v6 = *(_QWORD *)v5;
v5 += 8;
k_free(v6);
}
while ( v5 != v13 );
v7 = 0LL;
v8 = 10LL;
k_print_memory_usage_info();
do
{
while ( (v7 & 1) != 0 )
{
v9 = k_malloc_aligned(v8, 4LL);
v12[v7] = v9;
if ( (v9 & 3) != 0 )
return 0LL;
++v7;
v8 *= 2LL;
if ( v7 == 20 )
goto LABEL_13;
}
v10 = k_malloc_aligned(v8, 8LL);
v12[v7] = v10;
if ( (v10 & 7) != 0 )
return 0LL;
++v7;
v8 *= 2LL;
}
while ( v7 != 20 );
LABEL_13:
k_print_memory_usage_info();
do
{
v11 = *(_QWORD *)v1;
v1 += 8;
k_free(v11);
}
while ( v1 != v13 );
k_print_memory_usage_info();
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r13
char *v1; // r12
char *v2; // rbx
__int64 v3; // rax
char *v5; // rbx
__int64 v6; // rdi
__int64 v7; // rbx
__int64 v8; // r13
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rdi
__int64 v12[20]; // [rsp+0h] [rbp-C8h] BYREF
char v13[40]; // [rsp+A0h] [rbp-28h] BYREF

v0 = 10LL;
v1 = (char *)v12;
v2 = (char *)v12;
do
{
v3 = k_malloc(v0);
*(_QWORD *)v2 = v3;
if ( !v3 )
return 0LL;
v2 += 8;
v0 *= 2LL;
}
while ( v2 != v13 );
v5 = (char *)v12;
k_print_memory_usage_info();
do
{
v6 = *(_QWORD *)v5;
v5 += 8;
k_free(v6);
}
while ( v5 != v13 );
v7 = 0LL;
v8 = 10LL;
k_print_memory_usage_info();
do
{
while ( (v7 & 1) != 0 )
{
v9 = k_malloc_aligned(v8, 4LL);
v12[v7] = v9;
if ( (v9 & 3) != 0 )
return 0LL;
++v7;
v8 *= 2LL;
if ( v7 == 20 )
goto LABEL_13;
}
v10 = k_malloc_aligned(v8, 8LL);
v12[v7] = v10;
if ( (v10 & 7) != 0 )
return 0LL;
++v7;
v8 *= 2LL;
}
while ( v7 != 20 );
LABEL_13:
k_print_memory_usage_info();
do
{
v11 = *(_QWORD *)v1;
v1 += 8;
k_free(v11);
}
while ( v1 != v13 );
k_print_memory_usage_info();
return 1LL;
}

original_code:
bool kmalloc_test()
{
 char *ptrs[20];
 int i;
 size_t acSize = 10;
 for(i = 0; i < (sizeof(ptrs)/sizeof(char*)); i++, acSize *= 2)
 {
  ptrs[i] = k_malloc(acSize);
  if(!ptrs[i])
   return 0;
 }
 k_print_memory_usage_info();
 for(i = 0; i < (sizeof(ptrs)/sizeof(char*)); i++, acSize *= 2)
  k_free(ptrs[i]);
 k_print_memory_usage_info();
 acSize = 10;
 for(i = 0; i < (sizeof(ptrs)/sizeof(char*)); i++, acSize *= 2)
 {
  if(i % 2)
  {
   ptrs[i] = k_malloc_aligned(acSize, 4);
   if((((size_t)ptrs[i]) % 4) != 0)
    return 0;
  }
  else
  {
   ptrs[i] = k_malloc_aligned(acSize, 8);
   if((((size_t)ptrs[i]) % 8) != 0)
    return 0;
  }
 }
 k_print_memory_usage_info();
 for(i = 0; i < (sizeof(ptrs)/sizeof(char*)); i++, acSize *= 2)
  k_free(ptrs[i]);
 k_print_memory_usage_info();
 return 1;
}

canonical_original_code:
int kmalloc_test()
{
 char *ptrs[20];
 int i;
 unsigned long acSize = 10;
 for(i = 0; i < (sizeof(ptrs)/sizeof(char *)); i++, acSize *= 2)
 {
  ptrs[i] = k_malloc(acSize);
  if(!ptrs[i])
   return 0;
 }
 k_print_memory_usage_info();
 for(i = 0; i < (sizeof(ptrs)/sizeof(char *)); i++, acSize *= 2)
  k_free(ptrs[i]);
 k_print_memory_usage_info();
 acSize = 10;
 for(i = 0; i < (sizeof(ptrs)/sizeof(char *)); i++, acSize *= 2)
 {
  if(i % 2)
  {
   ptrs[i] = k_malloc_aligned(acSize, 4);
   if((((unsigned long)ptrs[i]) % 4) != 0)
    return 0;
  }
  else
  {
   ptrs[i] = k_malloc_aligned(acSize, 8);
   if((((unsigned long)ptrs[i]) % 8) != 0)
    return 0;
  }
 }
 k_print_memory_usage_info();
 for(i = 0; i < (sizeof(ptrs)/sizeof(char *)); i++, acSize *= 2)
  k_free(ptrs[i]);
 k_print_memory_usage_info();
 return 1;
}

variable_types:
ptrs
char *[20]

variable_types:
acSize
unsigned long


extractSubString

decompiled_code:
__int64 __fastcall extractSubString(__int64 a1, _QWORD *a2, _QWORD *a3, _QWORD *a4, _QWORD *a5, __int64 *a6)
{
__int64 result; // rax

*a2 = strsub(a1, 0LL, 3LL);
*a3 = strsub(a1, 4LL, 5LL);
*a4 = strsub(a1, 6LL, 6LL);
*a5 = strsub(a1, 7LL, 8LL);
result = strsub(a1, 9LL, 18LL);
*a6 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2, _QWORD *a3, _QWORD *a4, _QWORD *a5, __int64 *a6)
{
__int64 result; // rax

*a2 = strsub(a1, 0LL, 3LL);
*a3 = strsub(a1, 4LL, 5LL);
*a4 = strsub(a1, 6LL, 6LL);
*a5 = strsub(a1, 7LL, 8LL);
result = strsub(a1, 9LL, 18LL);
*a6 = result;
return result;
}

original_code:
void extractSubString(char * bufferReader,
                      char ** year,
                      char ** week,
                      char ** day,
                      char ** hour,
                      char ** event)
{
    *year = strsub(bufferReader, YEAR_OFFSET, YEAR_OFFSET+YEAR_SIZE-1);
    *week = strsub(bufferReader, WEEK_OFFSET, WEEK_OFFSET+WEEK_SIZE-1);
    *day = strsub(bufferReader, DAY_OFFSET, DAY_OFFSET+DAY_SIZE-1);
    *hour = strsub(bufferReader, HOUR_OFFSET, HOUR_OFFSET+HOUR_SIZE-1);
    *event = strsub(bufferReader, EVENT_OFFSET, EVENT_OFFSET+EVENT_SIZE-1);
}

canonical_original_code:
void extractSubString(char *bufferReader, char **year, char **week, char **day, char **hour, char **event)
{
    *year = strsub(bufferReader, YEAR_OFFSET, YEAR_OFFSET+YEAR_SIZE-1);
    *week = strsub(bufferReader, WEEK_OFFSET, WEEK_OFFSET+WEEK_SIZE-1);
    *day = strsub(bufferReader, DAY_OFFSET, DAY_OFFSET+DAY_SIZE-1);
    *hour = strsub(bufferReader, HOUR_OFFSET, HOUR_OFFSET+HOUR_SIZE-1);
    *event = strsub(bufferReader, EVENT_OFFSET, EVENT_OFFSET+EVENT_SIZE-1);
}

variable_types:
bufferReader
char *

variable_types:
year
char **

variable_types:
week
char **

variable_types:
day
char **

variable_types:
hour
char **

variable_types:
event
char **


listeContigueDesEvent

decompiled_code:
char *__fastcall listeContigueDesEvent(__int64 a1, __int64 a2)
{
__int64 v2; // r13
char *v3; // r15
int v4; // er14
__int64 v5; // rbx
char *v6; // r12
char *v8; // rdi

v2 = a1;
v3 = (char *)malloc(0x100uLL);
if ( a1 )
{
v4 = 0;
do
{
while ( 1 )
{
v5 = *(_QWORD *)(v2 + 16);
if ( v5 )
break;
LABEL_7:
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
return v3;
}
v6 = &v3[v4];
do
{
while ( !(unsigned int)matchPattern(*(_QWORD *)(v5 + 16), a2) )
{
v5 = *(_QWORD *)(v5 + 24);
if ( !v5 )
goto LABEL_7;
}
v4 += 7;
strncpy(v6, *(const char **)v2, 4uLL);
strncpy(v6 + 4, *(const char **)(v2 + 8), 2uLL);
v8 = v6 + 6;
v6 += 7;
strncpy(v8, *(const char **)v5, 1uLL);
v5 = *(_QWORD *)(v5 + 24);
}
while ( v5 );
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
}
return v3;
}

original_code:
char * listeContigueDesEvent(calendar_t calendar,
                             char * chaineRecherche)
{
    char * listeContigue;
    weekCal_t * currentWeek = calendar;
    eventCal_t * currentEvent = ((void *)0);
    int i = 0;
    listeContigue = (char *)malloc(sizeof(char)*256);
    while (currentWeek != ((void *)0))
    {
        currentEvent = currentWeek->eventList;
        while (currentEvent != ((void *)0))
        {
            while (currentEvent != ((void *)0) && !matchPattern(currentEvent->event, chaineRecherche))
            {
                currentEvent = currentEvent->nextEvent;
            }
            if (currentEvent != ((void *)0))
            {
                strncpy(listeContigue+i,currentWeek->year, YEAR_SIZE);
                strncpy(listeContigue+i+WEEK_OFFSET,currentWeek->week, WEEK_SIZE);
                strncpy(listeContigue+i+DAY_OFFSET,currentEvent->day, DAY_SIZE);
                i += YEAR_SIZE+WEEK_SIZE+DAY_SIZE;
                currentEvent = currentEvent->nextEvent;
            }
        }
        currentWeek = currentWeek->nextWeek;
    }
    return listeContigue;
}

canonical_original_code:
char *listeContigueDesEvent(struct weekCal *calendar, char *chaineRecherche)
{
    char *listeContigue;
    struct weekCal *currentWeek = calendar;
    struct eventCal *currentEvent = ((void *)0);
    int i = 0;
    listeContigue = (char *)malloc(sizeof(char)*256);
    while (currentWeek != ((void *)0))
    {
        currentEvent = currentWeek->eventList;
        while (currentEvent != ((void *)0))
        {
            while (currentEvent != ((void *)0) && !matchPattern(currentEvent->event, chaineRecherche))
            {
                currentEvent = currentEvent->nextEvent;
            }
            if (currentEvent != ((void *)0))
            {
                strncpy(listeContigue+i,currentWeek->year, YEAR_SIZE);
                strncpy(listeContigue+i+WEEK_OFFSET,currentWeek->week, WEEK_SIZE);
                strncpy(listeContigue+i+DAY_OFFSET,currentEvent->day, DAY_SIZE);
                i += YEAR_SIZE+WEEK_SIZE+DAY_SIZE;
                currentEvent = currentEvent->nextEvent;
            }
        }
        currentWeek = currentWeek->nextWeek;
    }
    return listeContigue;
}

variable_types: struct weekCal { char *year; char *week; struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *eventList; struct weekCal *nextWeek; } *

variable_types:
chaineRecherche
char *

variable_types:
listeContigue
char *

variable_types: i int

return_type: char *

user_defined_types: struct weekCal { char *year; char *week; struct eventCal *eventList; struct weekCal *nextWeek; }


deleteWeekEventCal

decompiled_code:
__int64 __fastcall deleteWeekEventCal(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
_QWORD *v8; // rax
_QWORD *v9; // rbp
__int64 result; // rax
_QWORD *v11; // rax
_QWORD *v12; // r12

v8 = (_QWORD *)searchWeek(a1, a2, a3);
if ( !v8 )
return 1LL;
v9 = v8;
if ( !*v8 )
return 1LL;
if ( !(unsigned int)isSameYearWeek(*v8, a2, a3) )
return 1LL;
v11 = (_QWORD *)searchEvent(*v9 + 16LL, a4, a5);
v12 = v11;
if ( !v11 || !*v11 || !(unsigned int)isSameDayHour(*v11, a4, a5) )
return 1LL;
deleteEvent(v12);
result = 0LL;
if ( !*(_QWORD *)(*v9 + 16LL) )
{
deleteWeek(v9);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
_QWORD *v8; // rax
_QWORD *v9; // rbp
__int64 result; // rax
_QWORD *v11; // rax
_QWORD *v12; // r12

v8 = (_QWORD *)searchWeek(a1, a2, a3);
if ( !v8 )
return 1LL;
v9 = v8;
if ( !*v8 )
return 1LL;
if ( !(unsigned int)isSameYearWeek(*v8, a2, a3) )
return 1LL;
v11 = (_QWORD *)searchEvent(*v9 + 16LL, a4, a5);
v12 = v11;
if ( !v11 || !*v11 || !(unsigned int)isSameDayHour(*v11, a4, a5) )
return 1LL;
deleteEvent(v12);
result = 0LL;
if ( !*(_QWORD *)(*v9 + 16LL) )
{
deleteWeek(v9);
result = 0LL;
}
return result;
}

original_code:
int deleteWeekEventCal(calendar_t * calendar,
                       char * year,
                       char * week,
                       char * day,
                       char * hour)
{
    weekCal_t ** prevWeek = ((void *)0);
    eventCal_t ** prevEvent = ((void *)0);
    int error = TRUE;
    prevWeek = searchWeek(calendar, year, week);
    if (prevWeek != ((void *)0) && *prevWeek != ((void *)0) && isSameYearWeek(*prevWeek,year,week))
    {
        prevEvent = searchEvent(&(*prevWeek)->eventList, day, hour);
        if (prevEvent != ((void *)0) && *prevEvent != ((void *)0) && isSameDayHour(*prevEvent,day,hour))
        {
            deleteEvent(prevEvent);
            error = FALSE;
            if ((*prevWeek)->eventList == ((void *)0))
            {
                deleteWeek(prevWeek);
            }
        }
    }
    return error;
}

canonical_original_code:
int deleteWeekEventCal(struct weekCal **calendar, char *year, char *week, char *day, char *hour)
{
    struct weekCal **prevWeek = ((void *)0);
    struct eventCal **prevEvent = ((void *)0);
    int error = TRUE;
    prevWeek = searchWeek(calendar, year, week);
    if (prevWeek != ((void *)0) && *prevWeek != ((void *)0) && isSameYearWeek(*prevWeek,year,week))
    {
        prevEvent = searchEvent(&(*prevWeek)->eventList, day, hour);
        if (prevEvent != ((void *)0) && *prevEvent != ((void *)0) && isSameDayHour(*prevEvent,day,hour))
        {
            deleteEvent(prevEvent);
            error = FALSE;
            if ((*prevWeek)->eventList == ((void *)0))
            {
                deleteWeek(prevWeek);
            }
        }
    }
    return error;
}

variable_types: struct weekCal { char *year; char *week; struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *eventList; struct weekCal *nextWeek; } **

variable_types:
year
char *

variable_types:
week
char *

variable_types:
day
char *

variable_types:
hour
char *

variable_types: struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } **

variable_types: error int


convertWeekCalToWeekCalBil

original_code:
weekCalBil_t * convertWeekCalToWeekCalBil(calendar_t calendar)
{
    weekCal_t * currentWeek = calendar;
    weekCalBil_t * prevWeekBil;
    weekCalBil_t * listeBilatere = newWeekCalBil("\0", "\0", ((void *)0));
    listeBilatere->prevWeek = listeBilatere;
    listeBilatere->nextWeek = listeBilatere;
    prevWeekBil = listeBilatere;
    while (currentWeek != ((void *)0))
    {
        weekCalBil_t * newWeekCal = newWeekCalBil(
            currentWeek->year,
            currentWeek->week,
            currentWeek->eventList
        );
        newWeekCal->prevWeek = prevWeekBil;
        newWeekCal->nextWeek = prevWeekBil->nextWeek;
        prevWeekBil->nextWeek = newWeekCal;
        newWeekCal->nextWeek->prevWeek = newWeekCal;
        prevWeekBil = newWeekCal;
        currentWeek = currentWeek->nextWeek;
    }
    return listeBilatere;
}

canonical_original_code:
struct weekCalBil *convertWeekCalToWeekCalBil(struct weekCal *calendar)
{
    struct weekCal *currentWeek = calendar;
    struct weekCalBil *prevWeekBil;
    struct weekCalBil *listeBilatere = newWeekCalBil("\0", "\0", ((void *)0));
    listeBilatere->prevWeek = listeBilatere;
    listeBilatere->nextWeek = listeBilatere;
    prevWeekBil = listeBilatere;
    while (currentWeek != ((void *)0))
    {
        struct weekCalBil *newWeekCal = newWeekCalBil(
            currentWeek->year,
            currentWeek->week,
            currentWeek->eventList
        );
        newWeekCal->prevWeek = prevWeekBil;
        newWeekCal->nextWeek = prevWeekBil->nextWeek;
        prevWeekBil->nextWeek = newWeekCal;
        newWeekCal->nextWeek->prevWeek = newWeekCal;
        prevWeekBil = newWeekCal;
        currentWeek = currentWeek->nextWeek;
    }
    return listeBilatere;
}

variable_types: struct weekCal { char *year; char *week; struct eventCal *eventList; struct weekCal *nextWeek; } *

variable_types: struct weekCalBil { char *year; char *week; struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *eventList; struct weekCalBil *prevWeek; struct weekCalBil *nextWeek; } *

user_defined_types: struct weekCalBil { char *year; char *week; struct eventCal *eventList; struct weekCalBil *prevWeek; struct weekCalBil *nextWeek; }


freeCalendar

decompiled_code:
__int64 __fastcall freeCalendar(__int64 *a1)
{
__int64 v2; // rdi
__int64 result; // rax

v2 = *a1;
if ( v2 )
{
do
{
while ( *(_QWORD *)(v2 + 16) )
{
deleteEvent(v2 + 16);
v2 = *a1;
}
result = deleteWeek(a1);
v2 = *a1;
}
while ( *a1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1)
{
__int64 v2; // rdi
__int64 result; // rax

v2 = *a1;
if ( v2 )
{
do
{
while ( *(_QWORD *)(v2 + 16) )
{
deleteEvent(v2 + 16);
v2 = *a1;
}
result = deleteWeek(a1);
v2 = *a1;
}
while ( *a1 );
}
return result;
}

original_code:
void freeCalendar(calendar_t * calendar)
{
    while (*calendar != ((void *)0))
    {
        while ((*calendar)->eventList != ((void *)0))
        {
            deleteEvent(&(*calendar)->eventList);
        }
        deleteWeek(calendar);
    }
}

canonical_original_code:
void freeCalendar(struct weekCal **calendar)
{
    while (*calendar != ((void *)0))
    {
        while ((*calendar)->eventList != ((void *)0))
        {
            deleteEvent(&(*calendar)->eventList);
        }
        deleteWeek(calendar);
    }
}

user_defined_types: struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; }


printCalendar

decompiled_code:
int __fastcall printCalendar(__int64 a1)
{
__int64 v1; // rbp
int result; // eax
__int64 i; // rbx

v1 = a1;
puts("\n** Calendar **\n");
if ( !a1 )
return puts("Calendrier Vide =(");
do
{
result = printf("%s - semaine %s\n", *(const char **)v1, *(const char **)(v1 + 8));
for ( i = *(_QWORD *)(v1 + 16); i; i = *(_QWORD *)(i + 24) )
result = printf(
" Jour %s - Heure %s - %s\n",
*(const char **)i,
*(const char **)(i + 8),
*(const char **)(i + 16));
v1 = *(_QWORD *)(v1 + 24);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
__int64 v1; // rbp
int result; // eax
__int64 i; // rbx

v1 = a1;
puts("\n** Calendar **\n");
if ( !a1 )
return puts("Calendrier Vide =(");
do
{
result = printf("%s - semaine %s\n", *(const char **)v1, *(const char **)(v1 + 8));
for ( i = *(_QWORD *)(v1 + 16); i; i = *(_QWORD *)(i + 24) )
result = printf(
" Jour %s - Heure %s - %s\n",
*(const char **)i,
*(const char **)(i + 8),
*(const char **)(i + 16));
v1 = *(_QWORD *)(v1 + 24);
}
while ( v1 );
return result;
}

original_code:
void printCalendar(calendar_t calendar)
{
    weekCal_t * currentWeek;
    eventCal_t * currentEvent;
    currentWeek = calendar;
    printf("\n** Calendar **\n\n");
    if (currentWeek == ((void *)0))
    {
        printf("Calendrier Vide =(\n");
    }
    while (currentWeek != ((void *)0))
    {
        printf("%s - semaine %s\n", currentWeek->year, currentWeek->week);
        currentEvent = currentWeek->eventList;
        while (currentEvent != ((void *)0))
        {
            printf(
                "    Jour %s - Heure %s - %s\n",
                currentEvent->day,
                currentEvent->hour,
                currentEvent->event
            );
            currentEvent = currentEvent->nextEvent;
        }
        currentWeek = currentWeek->nextWeek;
    }
}

canonical_original_code:
void printCalendar(struct weekCal *calendar)
{
    struct weekCal *currentWeek;
    struct eventCal *currentEvent;
    currentWeek = calendar;
    printf("\n** Calendar **\n\n");
    if (currentWeek == ((void *)0))
    {
        printf("Calendrier Vide =(\n");
    }
    while (currentWeek != ((void *)0))
    {
        printf("%s - semaine %s\n", currentWeek->year, currentWeek->week);
        currentEvent = currentWeek->eventList;
        while (currentEvent != ((void *)0))
        {
            printf(
                "    Jour %s - Heure %s - %s\n",
                currentEvent->day,
                currentEvent->hour,
                currentEvent->event
            );
            currentEvent = currentEvent->nextEvent;
        }
        currentWeek = currentWeek->nextWeek;
    }
}

variable_types: struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *


matchPattern

decompiled_code:
_BOOL8 __fastcall matchPattern(__int64 a1, const char *a2)
{
int v4; // edi
unsigned __int64 v5; // r8
unsigned int v6; // eax
__int64 v7; // rdx
char v8; // si
char v9; // cl
bool v10; // al

v4 = 0;
v5 = (int)strnlen(a2, 0xBuLL);
if ( v5 <= 0xA )
{
while ( v5 )
{
v6 = 0;
v7 = 0LL;
do
{
v8 = *(_BYTE *)(a1 + v4 + v6);
v9 = a2[v7];
v7 = ++v6;
if ( v6 >= v5 )
{
v10 = v8 != v9;
goto LABEL_7;
}
}
while ( v8 == v9 );
v10 = 1;
LABEL_7:
if ( (unsigned int)++v4 > 9 || !v10 )
return v5 == v7;
}
}
return v5 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, const char *a2)
{
int v4; // edi
unsigned __int64 v5; // r8
unsigned int v6; // eax
__int64 v7; // rdx
char v8; // si
char v9; // cl
bool v10; // al

v4 = 0;
v5 = (int)strnlen(a2, 0xBuLL);
if ( v5 <= 0xA )
{
while ( v5 )
{
v6 = 0;
v7 = 0LL;
do
{
v8 = *(_BYTE *)(a1 + v4 + v6);
v9 = a2[v7];
v7 = ++v6;
if ( v6 >= v5 )
{
v10 = v8 != v9;
goto LABEL_7;
}
}
while ( v8 == v9 );
v10 = 1;
LABEL_7:
if ( (unsigned int)++v4 > 9 || !v10 )
return v5 == v7;
}
}
return v5 == 0;
}

original_code:
int matchPattern(char str[10],
                 char * pattern)
{
    unsigned int i = 0;
    unsigned int j = 0;
    size_t size = strnlen(pattern, 11);
    BOOL match = (size > 10);
    while(i<10 && !match)
    {
        j=0;
        match=TRUE;
        while(j < size && match)
        {
            match = (str[i+j] == pattern[j]);
            ++j;
        }
        ++i;
    }
    return (j == size);
}

canonical_original_code:
int matchPattern(char str[10], char *pattern)
{
    unsigned int i = 0;
    unsigned int j = 0;
    unsigned long size = strnlen(pattern, 11);
    unsigned int match = (size > 10);
    while(i<10 && !match)
    {
        j=0;
        match=TRUE;
        while(j < size && match)
        {
            match = (str[i+j] == pattern[j]);
            ++j;
        }
        ++i;
    }
    return (j == size);
}

variable_types:
str
char [10]

variable_types:
pattern
char *

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
size
unsigned long

variable_types:
match
unsigned int


isSameYearWeek

decompiled_code:
_BOOL8 __fastcall isSameYearWeek(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
_BOOL8 result; // rax

v4 = strcmp(*a1, a2);
result = 0LL;
if ( !v4 )
result = strcmp(a1[1], a3) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
_BOOL8 result; // rax

v4 = strcmp(*a1, a2);
result = 0LL;
if ( !v4 )
result = strcmp(a1[1], a3) == 0;
return result;
}

original_code:
int isSameYearWeek(weekCal_t * weekCal,
                   char * year,
                   char * week)
{
    return strcmp(weekCal->year, year) == 0 && strcmp(weekCal->week, week) == 0;
}

canonical_original_code:
int isSameYearWeek(struct weekCal *weekCal, char *year, char *week)
{
    return strcmp(weekCal->year, year) == 0 && strcmp(weekCal->week, week) == 0;
}

variable_types: struct weekCal { char *year; char *week; struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *eventList; struct weekCal *nextWeek; } *


isInferiorYearWeek

decompiled_code:
__int64 __fastcall isInferiorYearWeek(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
__int64 result; // rax

v4 = strcmp(*a1, a2);
result = 1LL;
if ( v4 >= 0 )
{
result = 0LL;
if ( !v4 )
result = (unsigned int)strcmp(a1[1], a3) >> 31;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
__int64 result; // rax

v4 = strcmp(*a1, a2);
result = 1LL;
if ( v4 >= 0 )
{
result = 0LL;
if ( !v4 )
result = (unsigned int)strcmp(a1[1], a3) >> 31;
}
return result;
}

original_code:
int isInferiorYearWeek(weekCal_t * weekCal,
                   char * year,
                   char * week)
{
    int isInferior = 0;
    int compYear = strcmp(weekCal->year, year);
    if (compYear < 0) isInferior=1;
    else if(compYear == 0)
        isInferior = (strcmp(weekCal->week, week) < 0);
    return isInferior;
}

canonical_original_code:
int isInferiorYearWeek(struct weekCal *weekCal, char *year, char *week)
{
    int isInferior = 0;
    int compYear = strcmp(weekCal->year, year);
    if (compYear < 0) isInferior=1;
    else if(compYear == 0)
        isInferior = (strcmp(weekCal->week, week) < 0);
    return isInferior;
}

variable_types: isInferior int

variable_types: compYear int


isSameDayHour

decompiled_code:
_BOOL8 __fastcall isSameDayHour(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
_BOOL8 result; // rax

v4 = strcmp(*a1, a2);
result = 0LL;
if ( !v4 )
result = strcmp(a1[1], a3) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
_BOOL8 result; // rax

v4 = strcmp(*a1, a2);
result = 0LL;
if ( !v4 )
result = strcmp(a1[1], a3) == 0;
return result;
}

original_code:
int isSameDayHour(eventCal_t * eventCal,
                  char * day,
                  char * hour)
{
    return strcmp(eventCal->day, day) == 0 && strcmp(eventCal->hour, hour) == 0;
}

canonical_original_code:
int isSameDayHour(struct eventCal *eventCal, char *day, char *hour)
{
    return strcmp(eventCal->day, day) == 0 && strcmp(eventCal->hour, hour) == 0;
}


isInferiorDayHour

decompiled_code:
__int64 __fastcall isInferiorDayHour(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
__int64 result; // rax

v4 = strcmp(*a1, a2);
result = 1LL;
if ( v4 >= 0 )
{
result = 0LL;
if ( !v4 )
result = (unsigned int)strcmp(a1[1], a3) >> 31;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char **a1, const char *a2, const char *a3)
{
int v4; // er8
__int64 result; // rax

v4 = strcmp(*a1, a2);
result = 1LL;
if ( v4 >= 0 )
{
result = 0LL;
if ( !v4 )
result = (unsigned int)strcmp(a1[1], a3) >> 31;
}
return result;
}

original_code:
int isInferiorDayHour(eventCal_t * eventCal,
                  char * day,
                  char * hour)
{
    int isInferior = 0;
    int compDay = strcmp(eventCal->day, day);
    if (compDay < 0) isInferior=1;
    else if(compDay == 0)
        isInferior = (strcmp(eventCal->hour, hour) < 0);
    return isInferior;
}

canonical_original_code:
int isInferiorDayHour(struct eventCal *eventCal, char *day, char *hour)
{
    int isInferior = 0;
    int compDay = strcmp(eventCal->day, day);
    if (compDay < 0) isInferior=1;
    else if(compDay == 0)
        isInferior = (strcmp(eventCal->hour, hour) < 0);
    return isInferior;
}

variable_types: compDay int


freeWeekCal

decompiled_code:
void __fastcall freeWeekCal(void ***a1)
{
free(**a1);
free((*a1)[1]);
free(*a1);
}

canonical_decompiled_code:
void __fastcall func4(void ***a1)
{
free(**a1);
free((*a1)[1]);
free(*a1);
}

original_code:
void freeWeekCal(weekCal_t ** weekCal)
{
    free((*weekCal)->year);
    free((*weekCal)->week);
    free(*weekCal);
}

canonical_original_code:
void freeWeekCal(struct weekCal **weekCal)
{
    free((*weekCal)->year);
    free((*weekCal)->week);
    free(*weekCal);
}

variable_types: struct weekCal { char *year; char *week; struct eventCal { char *day; char *hour; char *event; struct eventCal *nextEvent; } *eventList; struct weekCal *nextWeek; } **


deleteWeek

decompiled_code:
void __fastcall deleteWeek(void ***a1)
{
void **v1; // [rsp+8h] [rbp-10h] BYREF

v1 = *a1;
*a1 = (void **)v1[3];
freeWeekCal(&v1);
}

canonical_decompiled_code:
void __fastcall func5(void ***a1)
{
void **v1; // [rsp+8h] [rbp-10h] BYREF

v1 = *a1;
*a1 = (void **)v1[3];
func4(&v1);
}

original_code:
void deleteWeek(weekCal_t ** adrPrev)
{
    weekCal_t * weekToDelete = *adrPrev;
    *adrPrev = weekToDelete->nextWeek;
    freeWeekCal(&weekToDelete);
}

canonical_original_code:
void deleteWeek(struct weekCal **adrPrev)
{
    struct weekCal *weekToDelete = *adrPrev;
    *adrPrev = weekToDelete->nextWeek;
    freeWeekCal(&weekToDelete);
}


freeEventCal

decompiled_code:
void __fastcall freeEventCal(void ***a1)
{
free(**a1);
free((*a1)[1]);
free((*a1)[2]);
free(*a1);
}

original_code:
void freeEventCal(eventCal_t ** eventCal)
{
    free((*eventCal)->day);
    free((*eventCal)->hour);
    free((*eventCal)->event);
    free(*eventCal);
}

canonical_original_code:
void freeEventCal(struct eventCal **eventCal)
{
    free((*eventCal)->day);
    free((*eventCal)->hour);
    free((*eventCal)->event);
    free(*eventCal);
}


deleteEvent

decompiled_code:
void __fastcall deleteEvent(void ***a1)
{
void **v1; // [rsp+8h] [rbp-10h] BYREF

v1 = *a1;
*a1 = (void **)v1[3];
freeEventCal(&v1);
}

canonical_decompiled_code:
void __fastcall func7(void ***a1)
{
void **v1; // [rsp+8h] [rbp-10h] BYREF

v1 = *a1;
*a1 = (void **)v1[3];
func6(&v1);
}

original_code:
void deleteEvent(eventCal_t ** adrPrev)
{
    eventCal_t * eventToDelete = *adrPrev;
    *adrPrev = eventToDelete->nextEvent;
    freeEventCal(&eventToDelete);
}

canonical_original_code:
void deleteEvent(struct eventCal **adrPrev)
{
    struct eventCal *eventToDelete = *adrPrev;
    *adrPrev = eventToDelete->nextEvent;
    freeEventCal(&eventToDelete);
}


isQueueEmpty

decompiled_code:
_BOOL8 __fastcall isQueueEmpty(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == 0;
}

original_code:
int isQueueEmpty(queue_t * queue)
{
 return queue->nbElement == 0;
}

canonical_original_code:
int isQueueEmpty(struct queue_t *queue)
{
 return queue->nbElement == 0;
}

variable_types: struct queue_t { struct node { char item; struct node *sibling; struct node *child; } **items; unsigned int size; unsigned int indexStart; unsigned int indexEnd; unsigned int nbElement; } *

user_defined_types: struct queue_t { struct node **items; unsigned int size; unsigned int indexStart; unsigned int indexEnd; unsigned int nbElement; }


popQueue

original_code:
queueItem_t * popQueue(queue_t * queue)
{
 queueItem_t * item = ((void *)0);
 if (! isQueueEmpty(queue))
 {
  item = &queue->items[queue->indexStart];
  if ((queue->indexStart+1) % queue->size == 0)
  {
   queue->indexStart = 0;
        }
  else
  {
   queue->indexStart++;
  }
        queue->nbElement--;
 }
 return item;
}

canonical_original_code:
struct node **popQueue(struct queue_t *queue)
{
 struct node **item = ((void *)0);
 if (! isQueueEmpty(queue))
 {
  item = &queue->items[queue->indexStart];
  if ((queue->indexStart+1) % queue->size == 0)
  {
   queue->indexStart = 0;
        }
  else
  {
   queue->indexStart++;
  }
        queue->nbElement--;
 }
 return item;
}

variable_types: struct queue_t { struct node **items; unsigned int size; unsigned int indexStart; unsigned int indexEnd; unsigned int nbElement; } *

variable_types: struct node { char item; struct node *sibling; struct node *child; } **


isQueueFull

decompiled_code:
_BOOL8 __fastcall isQueueFull(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == *(_DWORD *)(a1 + 8);
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == *(_DWORD *)(a1 + 8);
}

original_code:
int isQueueFull(queue_t * queue)
{
 return queue->nbElement == queue->size;
}

canonical_original_code:
int isQueueFull(struct queue_t *queue)
{
 return queue->nbElement == queue->size;
}

user_defined_types: struct node { char item; struct node *sibling; struct node *child; }


pushQueue

decompiled_code:
__int64 __fastcall pushQueue(__int64 a1, __int64 a2)
{
unsigned int v2; // ecx
unsigned int v3; // ecx
int v4; // edx
__int64 result; // rax

if ( isQueueFull(a1) )
return 1LL;
v2 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v2) = a2;
v3 = v2 + 1;
v4 = v3 % *(_DWORD *)(a1 + 8);
result = 0LL;
if ( v4 )
v4 = v3;
++*(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 16) = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
unsigned int v2; // ecx
unsigned int v3; // ecx
int v4; // edx
__int64 result; // rax

if ( func2(a1) )
return 1LL;
v2 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v2) = a2;
v3 = v2 + 1;
v4 = v3 % *(_DWORD *)(a1 + 8);
result = 0LL;
if ( v4 )
v4 = v3;
++*(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 16) = v4;
return result;
}

original_code:
int pushQueue(queue_t * queue, queueItem_t item)
{
 int error = 1;
 if (! isQueueFull(queue))
 {
  queue->items[queue->indexEnd] = item;
  if ((queue->indexEnd+1) % queue->size == 0)
  {
   queue->indexEnd = 0;
        }
  else
  {
   queue->indexEnd++;
  }
        queue->nbElement++;
  error = 0;
 }
 return error;
}

canonical_original_code:
int pushQueue(struct queue_t *queue, struct node *item)
{
 int error = 1;
 if (! isQueueFull(queue))
 {
  queue->items[queue->indexEnd] = item;
  if ((queue->indexEnd+1) % queue->size == 0)
  {
   queue->indexEnd = 0;
        }
  else
  {
   queue->indexEnd++;
  }
        queue->nbElement++;
  error = 0;
 }
 return error;
}

variable_types:
item
struct node *

variable_types: error int


printQueue

decompiled_code:
int __fastcall printQueue(__int64 a1)
{
unsigned int v1; // ebx
int v2; // er12
const char *v3; // rsi
char *v5; // rax
__int64 v6; // rdx

v1 = *(_DWORD *)(a1 + 12);
v2 = *(_DWORD *)(a1 + 20);
puts("\n*** File ***\n");
printf("indexStart : %d\n", *(unsigned int *)(a1 + 12));
printf("indexEnd : %d\n", *(unsigned int *)(a1 + 16));
printf("size : %d\n", *(unsigned int *)(a1 + 8));
printf("nbElement : %d\n", *(unsigned int *)(a1 + 20));
v3 = "TRUE";
if ( !isQueueFull(a1) )
v3 = "FALSE";
printf("isQueueFull : %s\n", v3);
putchar(10);
if ( isQueueEmpty(a1) )
return puts("File vide");
printf("File : |");
for ( ; v2; --v2 )
{
v5 = *(char **)(*(_QWORD *)a1 + 8LL * v1);
v6 = v1++;
printf(" %d (%d) |", (unsigned int)*v5, v6);
if ( !(v1 % *(_DWORD *)(a1 + 8)) )
v1 = 0;
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
unsigned int v1; // ebx
int v2; // er12
const char *v3; // rsi
char *v5; // rax
__int64 v6; // rdx

v1 = *(_DWORD *)(a1 + 12);
v2 = *(_DWORD *)(a1 + 20);
puts("\n*** File ***\n");
printf("indexStart : %d\n", *(unsigned int *)(a1 + 12));
printf("indexEnd : %d\n", *(unsigned int *)(a1 + 16));
printf("size : %d\n", *(unsigned int *)(a1 + 8));
printf("nbElement : %d\n", *(unsigned int *)(a1 + 20));
v3 = "TRUE";
if ( !func2(a1) )
v3 = "FALSE";
printf("isQueueFull : %s\n", v3);
putchar(10);
if ( func0(a1) )
return puts("File vide");
printf("File : |");
for ( ; v2; --v2 )
{
v5 = *(char **)(*(_QWORD *)a1 + 8LL * v1);
v6 = v1++;
printf(" %d (%d) |", (unsigned int)*v5, v6);
if ( !(v1 % *(_DWORD *)(a1 + 8)) )
v1 = 0;
}
return putchar(10);
}

original_code:
void printQueue(queue_t * queue)
{
 unsigned int index = queue->indexStart;
    unsigned int nbElementNotPrinted = queue->nbElement;
    printf("\n*** File ***\n\n");
    printf("indexStart : %d\n", queue->indexStart);
    printf("indexEnd : %d\n", queue->indexEnd);
    printf("size : %d\n", queue->size);
    printf("nbElement : %d\n", queue->nbElement);
    printf("isQueueFull : %s\n", (isQueueFull(queue)?"TRUE":"FALSE"));
    printf("\n");
    if (isQueueEmpty(queue))
    {
        printf("File vide\n");
    }
    else
    {
        printf("File : |");
        while (nbElementNotPrinted > 0)
        {
            printf(" %d (%d) |", queue->items[index]->item, index);
            if ((index+1) % queue->size == 0)
            {
                index = 0;
            }
            else
            {
                index++;
            }
            nbElementNotPrinted--;
        }
        printf("\n");
    }
}

canonical_original_code:
void printQueue(struct queue_t *queue)
{
 unsigned int index = queue->indexStart;
    unsigned int nbElementNotPrinted = queue->nbElement;
    printf("\n*** File ***\n\n");
    printf("indexStart : %d\n", queue->indexStart);
    printf("indexEnd : %d\n", queue->indexEnd);
    printf("size : %d\n", queue->size);
    printf("nbElement : %d\n", queue->nbElement);
    printf("isQueueFull : %s\n", (isQueueFull(queue)?"TRUE":"FALSE"));
    printf("\n");
    if (isQueueEmpty(queue))
    {
        printf("File vide\n");
    }
    else
    {
        printf("File : |");
        while (nbElementNotPrinted > 0)
        {
            printf(" %d (%d) |", queue->items[index]->item, index);
            if ((index+1) % queue->size == 0)
            {
                index = 0;
            }
            else
            {
                index++;
            }
            nbElementNotPrinted--;
        }
        printf("\n");
    }
}

variable_types:
index
unsigned int

variable_types:
nbElementNotPrinted
unsigned int


freeQueue

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

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

original_code:
void freeQueue(queue_t * queue)
{
    free(queue->items);
    free(queue);
}

canonical_original_code:
void freeQueue(struct queue_t *queue)
{
    free(queue->items);
    free(queue);
}


isStackEmpty

decompiled_code:
_BOOL8 __fastcall isStackEmpty(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 == -1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 == -1;
}

original_code:
int isStackEmpty(stack_t stack)
{
    return stack.topIndex==-1;
}

canonical_original_code:
int isStackEmpty(struct stack_t stack)
{
    return stack.topIndex==-1;
}

variable_types: struct stack_t { struct node { char item; struct node *sibling; struct node *child; } **head; unsigned long capacity; int topIndex; }

user_defined_types: struct stack_t { struct node **head; unsigned long capacity; int topIndex; }


isStackFull

decompiled_code:
_BOOL8 __fastcall isStackFull(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 + 1 >= a8;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 + 1 >= a8;
}

original_code:
int isStackFull(stack_t stack)
{
    return stack.topIndex+1>=(int)stack.capacity;
}

canonical_original_code:
int isStackFull(struct stack_t stack)
{
    return stack.topIndex+1>=(int)stack.capacity;
}


popStack

original_code:
stack_item_t * popStack(stack_t * stack)
{
    stack_item_t * item = ((void *)0);
    if(!isStackEmpty(*stack))
    {
        item = stack->head+stack->topIndex;
        --stack->topIndex;
    }
    return item;
}

canonical_original_code:
struct node **popStack(struct stack_t *stack)
{
    struct node **item = ((void *)0);
    if(!isStackEmpty(*stack))
    {
        item = stack->head+stack->topIndex;
        --stack->topIndex;
    }
    return item;
}

variable_types: struct stack_t { struct node **head; unsigned long capacity; int topIndex; } *

return_type: struct node { char item; struct node *sibling; struct node *child; } **


pushStack

original_code:
int pushStack(stack_t * stack, stack_item_t item)
{
    int exit;
    if(isStackFull(*stack))
    {
        exit=FAIL;
    }
    else
    {
        ++stack->topIndex;
        *(stack->head+stack->topIndex)=item;
        exit=SUCCESS;
    }
    return exit;
}

canonical_original_code:
int pushStack(struct stack_t *stack, struct node *item)
{
    int exit;
    if(isStackFull(*stack))
    {
        exit=FAIL;
    }
    else
    {
        ++stack->topIndex;
        *(stack->head+stack->topIndex)=item;
        exit=SUCCESS;
    }
    return exit;
}

variable_types: struct stack_t { struct node { char item; struct node *sibling; struct node *child; } **head; unsigned long capacity; int topIndex; } *

variable_types: exit int


printStack

decompiled_code:
int __fastcall printStack(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char **a7, unsigned int a8, int a9)
{
int result; // eax
int i; // ebx
char *v12; // rax

result = printf("%d items of %d in the stack\n", (unsigned int)(a9 + 1), a8);
if ( a9 >= 0 )
{
for ( i = 0; i <= a9; result = printf("Item #%d: %d\n", (unsigned int)i, (unsigned int)*v12) )
{
v12 = *a7;
++i;
++a7;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char **a7, unsigned int a8, int a9)
{
int result; // eax
int i; // ebx
char *v12; // rax

result = printf("%d items of %d in the stack\n", (unsigned int)(a9 + 1), a8);
if ( a9 >= 0 )
{
for ( i = 0; i <= a9; result = printf("Item #%d: %d\n", (unsigned int)i, (unsigned int)*v12) )
{
v12 = *a7;
++i;
++a7;
}
}
return result;
}

original_code:
void printStack(stack_t stack)
{
    int i;
    printf("%d items of %d in the stack\n", stack.topIndex+1, (int)stack.capacity);
    for(i=0;i<=stack.topIndex;++i)
    {
        printf("Item #%d: %d\n",i+1,(*(stack.head+i))->item);
    }
}

canonical_original_code:
void printStack(struct stack_t stack)
{
    int i;
    printf("%d items of %d in the stack\n", stack.topIndex+1, (int)stack.capacity);
    for(i=0;i<=stack.topIndex;++i)
    {
        printf("Item #%d: %d\n",i+1,(*(stack.head+i))->item);
    }
}

variable_types: i int


freeStack

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

original_code:
void freeStack(stack_t * stack)
{
    free(stack->head);
    free(stack);
}

canonical_original_code:
void freeStack(struct stack_t *stack)
{
    free(stack->head);
    free(stack);
}


createDict

decompiled_code:
// attributes: thunk
__int64 createDict()
{
return buildTree();
}

canonical_decompiled_code:
__int64 func0()
{
return buildTree();
}

original_code:
Dict createDict(char * string)
{
    return buildTree(string);
}

canonical_original_code:
struct node *createDict(char *string)
{
    return buildTree(string);
}

variable_types:
string
char *

return_type: struct node { char item; struct node *sibling; struct node *child; } *


createEmptyDict

decompiled_code:
__int64 createEmptyDict()
{
return createNode();
}

canonical_decompiled_code:
__int64 func1()
{
return createNode();
}

original_code:
Dict createEmptyDict()
{
    return createNode();
}

canonical_original_code:
struct node *createEmptyDict()
{
    return createNode();
}


characterAreInferior

decompiled_code:
_BOOL8 __fastcall characterAreInferior(char a1, char a2)
{
const __int32_t **v2; // rax

v2 = _ctype_toupper_loc();
return (*v2)[a1] < (*v2)[a2];
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(char a1, char a2)
{
const __int32_t **v2; // rax

v2 = _ctype_toupper_loc();
return (*v2)[a1] < (*v2)[a2];
}

original_code:
int characterAreInferior(char one, char two)
{
    return toupper(one) < toupper(two);
}

variable_types: one char

variable_types: two char


characterAreSuperior

decompiled_code:
_BOOL8 __fastcall characterAreSuperior(char a1, char a2)
{
const __int32_t **v2; // rax

v2 = _ctype_toupper_loc();
return (*v2)[a1] > (*v2)[a2];
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(char a1, char a2)
{
const __int32_t **v2; // rax

v2 = _ctype_toupper_loc();
return (*v2)[a1] > (*v2)[a2];
}

original_code:
int characterAreSuperior(char one, char two)
{
    return toupper(one) > toupper(two);
}


insertWord

decompiled_code:
__int64 __fastcall insertWord(__int64 *a1, const char *a2)
{
__int64 result; // rax
__int64 v3; // rbx
__int64 *v4; // rbp
unsigned int v5; // er12
unsigned int v6; // er13
char v7; // r15
__int64 v8; // rax
size_t v9; // [rsp+8h] [rbp-40h]

v9 = strlen(a2);
result = *a1;
v3 = *(_QWORD *)(*a1 + 16);
if ( v9 )
{
v4 = (__int64 *)(result + 16);
v5 = 0;
result = 0LL;
do
{
v6 = a2[result];
if ( !v3 )
goto LABEL_8;
while ( 1 )
{
v7 = *(_BYTE *)v3;
if ( !characterAreInferior(*(_BYTE *)v3, v6) )
break;
v4 = (__int64 *)(v3 + 8);
v3 = *(_QWORD *)(v3 + 8);
if ( !v3 )
goto LABEL_8;
}
if ( characterAreSuperior(v7, v6) )
{
LABEL_8:
v8 = initNode(v6);
*(_QWORD *)(v8 + 8) = v3;
*(_QWORD *)(v8 + 16) = 0LL;
*v4 = v8;
}
if ( (_DWORD)v9 - 1 == v5 )
*(_BYTE *)*v4 = (*_ctype_toupper_loc())[*(char *)*v4];
v3 = *(_QWORD *)(*v4 + 16);
v4 = (__int64 *)(*v4 + 16);
result = ++v5;
}
while ( v9 > v5 );
}
return result;
}

original_code:
void insertWord(Dict * dico, char * word)
{
    unsigned int index = 0;
    size_t wordSize = strlen(word);
    unsigned int lastIndex = (unsigned int)wordSize-1;
    char currentCharacter;
    node_t ** prevNode = &(*dico)->child;
    node_t * currentNode = *prevNode;
    while (index < wordSize)
    {
        currentCharacter = word[index];
        while (currentNode != ((void *)0) && characterAreInferior(currentNode->item, currentCharacter))
        {
            prevNode = &currentNode->sibling;
            currentNode = *prevNode;
        }
        if (currentNode == ((void *)0) || characterAreSuperior(currentNode->item, currentCharacter))
        {
            node_t * newNode = initNode(currentCharacter);
            newNode->sibling = currentNode;
            newNode->child = ((void *)0);
            *prevNode = newNode;
        }
        if (index == lastIndex)
        {
            (*prevNode)->item = toupper((*prevNode)->item);
        }
        prevNode = &(*prevNode)->child;
        currentNode = *prevNode;
        index++;
    }
}

canonical_original_code:
void insertWord(struct node **dico, char *word)
{
    unsigned int index = 0;
    unsigned long wordSize = strlen(word);
    unsigned int lastIndex = (unsigned int)wordSize-1;
    char currentCharacter;
    struct node **prevNode = &(*dico)->child;
    struct node *currentNode = *prevNode;
    while (index < wordSize)
    {
        currentCharacter = word[index];
        while (currentNode != ((void *)0) && characterAreInferior(currentNode->item, currentCharacter))
        {
            prevNode = &currentNode->sibling;
            currentNode = *prevNode;
        }
        if (currentNode == ((void *)0) || characterAreSuperior(currentNode->item, currentCharacter))
        {
            struct node *newNode = initNode(currentCharacter);
            newNode->sibling = currentNode;
            newNode->child = ((void *)0);
            *prevNode = newNode;
        }
        if (index == lastIndex)
        {
            (*prevNode)->item = toupper((*prevNode)->item);
        }
        prevNode = &(*prevNode)->child;
        currentNode = *prevNode;
        index++;
    }
}

variable_types:
word
char *

variable_types:
wordSize
unsigned long

variable_types:
lastIndex
unsigned int

variable_types: currentCharacter char

variable_types: struct node { char item; struct node *sibling; struct node *child; } *


printDict

original_code:
void printDict(Dict dict)
{
    int i;
    int exit = 0;
    stack_t * stack = createStack(100);
    node_t * node = dict;
    printf(">>> DICTIONNARY <<<\n\n");
    while(!exit)
    {
        while(node)
        {
            pushStack(stack, node);
            node = node->child;
        }
        if(!isStackEmpty(*stack))
        {
            node = *popStack(stack);
            if(((*__ctype_b_loc ())[(int) ((node->item))] & (unsigned short int) _ISupper))
            {
                for(i = 1; i <= stack->topIndex; ++i)
                {
                    printf("%c", tolower(stack->head[i]->item));
                }
                printf("%c\n", tolower(node->item));
            }
            node = node->sibling;
        }
        else exit = 1;
    }
    freeStack(stack);
}

canonical_original_code:
void printDict(struct node *dict)
{
    int i;
    int exit = 0;
    struct stack_t *stack = createStack(100);
    struct node *node = dict;
    printf(">>> DICTIONNARY <<<\n\n");
    while(!exit)
    {
        while(node)
        {
            pushStack(stack, node);
            node = node->child;
        }
        if(!isStackEmpty(*stack))
        {
            node = *popStack(stack);
            if(((*__ctype_b_loc ())[(int) ((node->item))] & (unsigned short) _ISupper))
            {
                for(i = 1; i <= stack->topIndex; ++i)
                {
                    printf("%c", tolower(stack->head[i]->item));
                }
                printf("%c\n", tolower(node->item));
            }
            node = node->sibling;
        }
        else exit = 1;
    }
    freeStack(stack);
}


freeDict

decompiled_code:
// attributes: thunk
__int64 freeDict()
{
return freeTree();
}

canonical_decompiled_code:
__int64 func6()
{
return freeTree();
}

original_code:
void freeDict(Dict dict)
{
    freeTree(dict);
}

canonical_original_code:
void freeDict(struct node *dict)
{
    freeTree(dict);
}


createNode

decompiled_code:
_QWORD *createNode()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
if ( result )
{
result[2] = 0LL;
result[1] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
if ( result )
{
result[2] = 0LL;
result[1] = 0LL;
}
return result;
}

original_code:
node_t * createNode()
{
    node_t * node = malloc(sizeof(node_t));
    if(node)
    {
        node->child = 0;
        node->sibling = 0;
    }
    return node;
}

canonical_original_code:
struct node *createNode()
{
    struct node *node = malloc(sizeof(node_t));
    if(node)
    {
        node->child = 0;
        node->sibling = 0;
    }
    return node;
}


printTree

decompiled_code:
__int64 __fastcall printTree(char *a1)
{
__int64 v2; // rbp
__int64 v3; // rax
char *v4; // rbx
__int64 v5; // rax
__int64 v6; // rsi

v2 = createQueue(100LL);
puts(">>> PRINT TREE <<<\n");
LABEL_3:
while ( a1 )
{
printf("%c ", (unsigned int)*a1);
pushQueue(v2, a1);
a1 = (char *)*((_QWORD *)a1 + 1);
}
while ( !(unsigned int)isQueueEmpty(v2) )
{
v3 = popQueue(v2);
v4 = *(char **)v3;
v5 = *(_QWORD *)(*(_QWORD *)v3 + 16LL);
if ( v5 )
{
v6 = (unsigned int)*v4;
if ( *(_QWORD *)(v5 + 8) )
printf("\nChildren of %c: \n", v6);
else
printf("\nChild of %c: \n", v6);
a1 = (char *)*((_QWORD *)v4 + 2);
goto LABEL_3;
}
}
putchar(10);
return freeQueue(v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
__int64 v2; // rbp
__int64 v3; // rax
char *v4; // rbx
__int64 v5; // rax
__int64 v6; // rsi

v2 = createQueue(100LL);
puts(">>> PRINT TREE <<<\n");
LABEL_3:
while ( a1 )
{
printf("%c ", (unsigned int)*a1);
pushQueue(v2, a1);
a1 = (char *)*((_QWORD *)a1 + 1);
}
while ( !(unsigned int)isQueueEmpty(v2) )
{
v3 = popQueue(v2);
v4 = *(char **)v3;
v5 = *(_QWORD *)(*(_QWORD *)v3 + 16LL);
if ( v5 )
{
v6 = (unsigned int)*v4;
if ( *(_QWORD *)(v5 + 8) )
printf("\nChildren of %c: \n", v6);
else
printf("\nChild of %c: \n", v6);
a1 = (char *)*((_QWORD *)v4 + 2);
goto LABEL_3;
}
}
putchar(10);
return freeQueue(v2);
}

original_code:
void printTree(node_t * node)
{
    int exit = 0;
    queue_t * queue = createQueue(100);
    printf(">>> PRINT TREE <<<\n\n");
    while(!exit)
    {
        while(node)
        {
            printf("%c ", node->item);
            pushQueue(queue, node);
            node = node->sibling;
        }
        if(!isQueueEmpty(queue))
        {
            node = *popQueue(queue);
            if(node->child)
            {
                if(node->child->sibling)
                    printf("\nChildren of %c: \n", node->item);
                else
                    printf("\nChild of %c: \n", node->item);
            }
            node = node->child;
        }
        else exit = 1;
    }
    printf("\n");
    freeQueue(queue);
}

canonical_original_code:
void printTree(struct node *node)
{
    int exit = 0;
    struct queue_t *queue = createQueue(100);
    printf(">>> PRINT TREE <<<\n\n");
    while(!exit)
    {
        while(node)
        {
            printf("%c ", node->item);
            pushQueue(queue, node);
            node = node->sibling;
        }
        if(!isQueueEmpty(queue))
        {
            node = *popQueue(queue);
            if(node->child)
            {
                if(node->child->sibling)
                    printf("\nChildren of %c: \n", node->item);
                else
                    printf("\nChild of %c: \n", node->item);
            }
            node = node->child;
        }
        else exit = 1;
    }
    printf("\n");
    freeQueue(queue);
}

variable_types: struct queue_t { struct node { char item; struct node *sibling; struct node *child; } **items; unsigned int size; unsigned int indexStart; unsigned int indexEnd; unsigned int nbElement; } *


freeTree

decompiled_code:
__int64 __fastcall freeTree(__int64 a1, __int64 a2)
{
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rbp
void **v6; // rax

v3 = 100LL;
v5 = createStack(100LL);
while ( 1 )
{
while ( a1 )
{
a2 = a1;
v3 = v5;
pushStack(v5, a1);
a1 = *(_QWORD *)(a1 + 16);
}
if ( (unsigned int)isStackEmpty(v3, a2, v4) )
break;
v6 = (void **)popStack(v5);
v3 = (__int64)*v6;
a1 = *((_QWORD *)*v6 + 1);
free(*v6);
}
return freeStack(v5);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v3; // rdi
__int64 v4; // rdx
__int64 v5; // rbp
void **v6; // rax

v3 = 100LL;
v5 = createStack(100LL);
while ( 1 )
{
while ( a1 )
{
a2 = a1;
v3 = v5;
pushStack(v5, a1);
a1 = *(_QWORD *)(a1 + 16);
}
if ( (unsigned int)isStackEmpty(v3, a2, v4) )
break;
v6 = (void **)popStack(v5);
v3 = (__int64)*v6;
a1 = *((_QWORD *)*v6 + 1);
free(*v6);
}
return freeStack(v5);
}

original_code:
void freeTree(node_t * node)
{
    int exit = 0;
    stack_t * stack = createStack(100);
    node_t * tmp_node;
    while(!exit)
    {
        while(node)
        {
            pushStack(stack, node);
            node = node->child;
        }
        if(!isStackEmpty(*stack))
        {
            node = *popStack(stack);
            tmp_node = node;
            node = node->sibling;
            free(tmp_node);
        }
        else exit = 1;
    }
    freeStack(stack);
}

canonical_original_code:
void freeTree(struct node *node)
{
    int exit = 0;
    struct stack_t *stack = createStack(100);
    struct node *tmp_node;
    while(!exit)
    {
        while(node)
        {
            pushStack(stack, node);
            node = node->child;
        }
        if(!isStackEmpty(*stack))
        {
            node = *popStack(stack);
            tmp_node = node;
            node = node->sibling;
            free(tmp_node);
        }
        else exit = 1;
    }
    freeStack(stack);
}


main

repo: Sitwon/forloop

canonical_decompiled_code:
__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
func4(a1, a2, a3);
func5();
func6();
func7();
func8();
return 0LL;
}

original_code:
int main (int argc, char **argv) {
 classic_for();
 simple_while();
 clever_while();
 cleverer_while();
 reversed_cleverer_while();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 classic_for();
 simple_while();
 clever_while();
 cleverer_while();
 reversed_cleverer_while();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
RunSub(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func1(argc, argv, envp);
return 0;
}

original_code:
int main(int argc, char** argv)
{
  RunSub();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  RunSub();
  return 0;
}


decompiled_code:
int __fastcall Console_ColorBasedOnValue(unsigned int a1, int a2, unsigned int a3)
{
if ( ((3 * a3) >> 2) - a2 < a1 )
return printf("\x1B[32m");
if ( a3 / 3 - a2 >= a1 )
return printf("\x1B[31m");
return printf("\x1B[33m");
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, int a2, unsigned int a3)
{
if ( ((3 * a3) >> 2) - a2 < a1 )
return printf("\x1B[32m");
if ( a3 / 3 - a2 >= a1 )
return printf("\x1B[31m");
return printf("\x1B[33m");
}

original_code:
void Console_ColorBasedOnValue(u32 val, u32 min, u32 max)
{
  if(val > (3*max/4 - min))
    printf("\033[32m" );
  else if(val > (max/3 -min) )
    printf("\033[33m" );
  else
    printf("\033[31m");
  return;
}

canonical_original_code:
void Console_ColorBasedOnValue(unsigned int val, unsigned int min, unsigned int max)
{
  if(val > (3*max/4 - min))
    printf("\033[32m" );
  else if(val > (max/3 -min) )
    printf("\033[33m" );
  else
    printf("\033[31m");
  return;
}

variable_types:
val
unsigned int

variable_types:
min
unsigned int

variable_types:
max
unsigned int


decompiled_code:
void Console_IncreaseTickSpeed()
{
--tickSecs;
}

canonical_decompiled_code:
void func2()
{
--tickSecs;
}

original_code:
void Console_IncreaseTickSpeed()
{
  tickSecs--;
}


decompiled_code:
__int64 Console_GetTickSpeed()
{
return (unsigned int)tickSecs;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)tickSecs;
}

original_code:
u32 Console_GetTickSpeed()
{
  return tickSecs;
}

canonical_original_code:
unsigned int Console_GetTickSpeed()
{
  return tickSecs;
}

return_type: unsigned int


decompiled_code:
int __fastcall Console_ReadLine(_BYTE *a1)
{
_BYTE *v1; // rbx
int result; // eax

v1 = a1;
while ( 1 )
{
result = fgetc(stdin);
if ( result == 10 )
break;
*v1++ = result;
if ( v1 == a1 + 1024 )
return result;
}
*v1 = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func4(_BYTE *a1)
{
_BYTE *v1; // rbx
int result; // eax

v1 = a1;
while ( 1 )
{
result = fgetc(stdin);
if ( result == 10 )
break;
*v1++ = result;
if ( v1 == a1 + 1024 )
return result;
}
*v1 = 0;
return result;
}

original_code:
void Console_ReadLine(char* toBuffer)
{
  int readC;
  s32 i;
  for(i = 0; i < 1024; i++)
  {
    switch((readC = fgetc(stdin)))
    {
    case '\n': toBuffer[i] = 0; return;
    default: toBuffer[i] = readC; break;
    }
  }
}

canonical_original_code:
void Console_ReadLine(char *toBuffer)
{
  int readC;
  int i;
  for(i = 0; i < 1024; i++)
  {
    switch((readC = fgetc(stdin)))
    {
    case '\n': toBuffer[i] = 0; return;
    default: toBuffer[i] = readC; break;
    }
  }
}

variable_types:
toBuffer
char *

variable_types: readC int


BinomialDistribution

decompiled_code:
__int64 __fastcall BinomialDistribution(int a1, int a2)
{
unsigned int v2; // kr00_4
unsigned __int8 v3; // al
int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // esi
__int64 i; // rax
int v8; // ebx

v2 = a2 - a1;
v3 = rand();
v4 = 1;
v5 = 0;
v6 = v3;
for ( i = 0LL; ; v4 = BINOMIAL_BINS[i] )
{
v5 += v4;
v8 = i;
if ( v6 < v5 )
break;
if ( ++i == 9 )
{
v8 = 9;
return a1 + v2 / 9 * v8 + rand() % (v2 / 9);
}
}
return a1 + v2 / 9 * v8 + rand() % (v2 / 9);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
unsigned int v2; // kr00_4
unsigned __int8 v3; // al
int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // esi
__int64 i; // rax
int v8; // ebx

v2 = a2 - a1;
v3 = rand();
v4 = 1;
v5 = 0;
v6 = v3;
for ( i = 0LL; ; v4 = BINOMIAL_BINS[i] )
{
v5 += v4;
v8 = i;
if ( v6 < v5 )
break;
if ( ++i == 9 )
{
v8 = 9;
return a1 + v2 / 9 * v8 + rand() % (v2 / 9);
}
}
return a1 + v2 / 9 * v8 + rand() % (v2 / 9);
}

original_code:
u32 BinomialDistribution(u32 min, u32 max)
{
  u32 binSize = (max - min) / 9;
  u32 binPick = rand() % BINOMIAL_TOTAL;
  u32 i, acc = 0;
  for(i = 0; i < 9; i++)
  {
    acc += BINOMIAL_BINS[i];
    if(binPick < acc)
    {
      break;
    }
  }
  return (rand() % binSize) + (i * binSize) + min;
}

canonical_original_code:
unsigned int BinomialDistribution(unsigned int min, unsigned int max)
{
  unsigned int binSize = (max - min) / 9;
  unsigned int binPick = rand() % BINOMIAL_TOTAL;
  unsigned int i; unsigned int acc = 0;
  for(i = 0; i < 9; i++)
  {
    acc += BINOMIAL_BINS[i];
    if(binPick < acc)
    {
      break;
    }
  }
  return (rand() % binSize) + (i * binSize) + min;
}

variable_types:
binSize
unsigned int

variable_types:
binPick
unsigned int

variable_types:
i
unsigned int

variable_types:
acc
unsigned int


teardown

repo: SoftlySplinter/c-armok

decompiled_code:
void teardown()
{
free(input);
free(world);
free(workshops);
fort = 0LL;
}

canonical_decompiled_code:
void func0()
{
free(input);
free(world);
free(workshops);
fort = 0LL;
}

original_code:
void teardown() {
  free(input);
  free(world);
  free(workshops);
  fort = ((void *)0);
}


move

decompiled_code:
size_t __fastcall move(__int64 a1, int a2)
{
int v2; // eax
bool v3; // zf
size_t result; // rax

v2 = -1;
if ( a2 != 60 )
v2 = a2 == 62;
v3 = *(_DWORD *)(a1 + 4) + v2 == 0;
result = (unsigned int)(*(_DWORD *)(a1 + 4) + v2);
*(_DWORD *)(a1 + 4) = result;
if ( v3 )
{
fwrite("A dwarf went insane and tried to swim in lava\n", 1uLL, 0x2EuLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
result = *(unsigned int *)(a1 + 4);
}
if ( rock_pos == (_DWORD)result )
{
result = fwrite("A dwarf was hammered by stone\n", 1uLL, 0x1EuLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func1(__int64 a1, int a2)
{
int v2; // eax
bool v3; // zf
size_t result; // rax

v2 = -1;
if ( a2 != 60 )
v2 = a2 == 62;
v3 = *(_DWORD *)(a1 + 4) + v2 == 0;
result = (unsigned int)(*(_DWORD *)(a1 + 4) + v2);
*(_DWORD *)(a1 + 4) = result;
if ( v3 )
{
fwrite("A dwarf went insane and tried to swim in lava\n", 1uLL, 0x2EuLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
result = *(unsigned int *)(a1 + 4);
}
if ( rock_pos == (_DWORD)result )
{
result = fwrite("A dwarf was hammered by stone\n", 1uLL, 0x1EuLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
return result;
}

original_code:
void move(dwarf *dwarf, int direction) {
  int amount = 0;
  switch(direction) {
    case '<':
      amount = -1;
      break;
    case '>':
      amount = 1;
      break;
  }
  dwarf->pos = dwarf->pos + amount;
  if(dwarf->pos == 0) {
    fprintf(stderr, "A dwarf went insane and tried to swim in lava\n");
    dwarf->dead = 1;
  }
  if(dwarf->pos == rock_pos) {
    fprintf(stderr, "A dwarf was hammered by stone\n");
    dwarf->dead = 1;
  }
}

canonical_original_code:
void move(struct _dwarf *dwarf, int direction) {
  int amount = 0;
  switch(direction) {
    case '<':
      amount = -1;
      break;
    case '>':
      amount = 1;
      break;
  }
  dwarf->pos = dwarf->pos + amount;
  if(dwarf->pos == 0) {
    fprintf(stderr, "A dwarf went insane and tried to swim in lava\n");
    dwarf->dead = 1;
  }
  if(dwarf->pos == rock_pos) {
    fprintf(stderr, "A dwarf was hammered by stone\n");
    dwarf->dead = 1;
  }
}

variable_types: struct _dwarf { int id; int pos; char *instructions; int inst_offset; int dead; int rocks; } *

variable_types: direction int

variable_types: amount int


mine

decompiled_code:
__int64 __fastcall mine(__int64 a1)
{
__int64 v1; // rax
_DWORD *v2; // rdx
__int64 result; // rax

v1 = *(int *)(a1 + 4);
if ( (_DWORD)v1 == rock_pos - 1 )
++rock_pos;
v2 = (char *)world + 4 * v1 + 4;
result = (unsigned int)*v2;
if ( (int)result > 0 )
{
result = (unsigned int)(result - 1);
*v2 = result;
++*(_DWORD *)(a1 + 24);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rax
_DWORD *v2; // rdx
__int64 result; // rax

v1 = *(int *)(a1 + 4);
if ( (_DWORD)v1 == rock_pos - 1 )
++rock_pos;
v2 = (char *)world + 4 * v1 + 4;
result = (unsigned int)*v2;
if ( (int)result > 0 )
{
result = (unsigned int)(result - 1);
*v2 = result;
++*(_DWORD *)(a1 + 24);
}
return result;
}

original_code:
void mine(dwarf *dwarf) {
  if(dwarf->pos == rock_pos - 1) {
    rock_pos++;
  }
  if(world[dwarf->pos + 1] > 0) {
    world[dwarf->pos + 1]--;
    dwarf->rocks++;
  }
}

canonical_original_code:
void mine(struct _dwarf *dwarf) {
  if(dwarf->pos == rock_pos - 1) {
    rock_pos++;
  }
  if(world[dwarf->pos + 1] > 0) {
    world[dwarf->pos + 1]--;
    dwarf->rocks++;
  }
}

user_defined_types: struct _dwarf { int id; int pos; char *instructions; int inst_offset; int dead; int rocks; }


decompiled_code:
int __fastcall work_trader(__int64 a1)
{
int result; // eax
int v2; // eax

if ( *(_DWORD *)(a1 + 24) )
{
v2 = encode();
result = printf("%lc", v2);
*(_DWORD *)(a1 + 24) = 0;
}
else if ( input && *((_DWORD *)input + input_off) )
{
result = decode();
++input_off;
*(_DWORD *)(a1 + 24) = result;
}
else
{
result = fwrite("Elves stabbed a dwarf in the back\n", 1uLL, 0x22uLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
int v2; // eax

if ( *(_DWORD *)(a1 + 24) )
{
v2 = encode();
result = printf("%lc", v2);
*(_DWORD *)(a1 + 24) = 0;
}
else if ( input && *((_DWORD *)input + input_off) )
{
result = decode();
++input_off;
*(_DWORD *)(a1 + 24) = result;
}
else
{
result = fwrite("Elves stabbed a dwarf in the back\n", 1uLL, 0x22uLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
return result;
}

original_code:
void work_trader(dwarf *dwarf) {
  if(dwarf->rocks == 0) {
    if(input == ((void *)0) || *(input + input_off) == 0x0000) {
      fprintf(stderr, "Elves stabbed a dwarf in the back\n");
      dwarf->dead = 1;
    } else {
      wchar_t i = decode(*(input + input_off));
      dwarf->rocks = i;
      input_off++;
    }
  } else {
    printf("%lc", encode(dwarf->rocks));
    dwarf->rocks = 0;
  }
}

canonical_original_code:
void work_trader(struct _dwarf *dwarf) {
  if(dwarf->rocks == 0) {
    if(input == ((void *)0) || *(input + input_off) == 0x0000) {
      fprintf(stderr, "Elves stabbed a dwarf in the back\n");
      dwarf->dead = 1;
    } else {
      int i = decode(*(input + input_off));
      dwarf->rocks = i;
      input_off++;
    }
  } else {
    printf("%lc", encode(dwarf->rocks));
    dwarf->rocks = 0;
  }
}

variable_types: i int


work

decompiled_code:
void __fastcall work(int *a1)
{
unsigned int *v1; // rdx
unsigned int v2; // eax
unsigned int v3; // er8

v1 = (unsigned int *)((char *)workshops + 4 * a1[1]);
v2 = *v1;
if ( *v1 == 2 )
{
work_manager((__int64)a1);
}
else if ( v2 == 3 )
{
work_appraiser((__int64)a1);
}
else if ( v2 == 1 )
{
work_trader((__int64)a1);
}
else
{
v3 = a1[6];
if ( v3 )
{
if ( v3 - 1 > 2 )
{
fprintf(stderr, "Unknown workshop %d\n", v3);
}
else
{
*v1 = v3;
a1[6] = 0;
}
}
else
{
fwrite("A dwarf when stark raving mad\n", 1uLL, 0x1EuLL, stderr);
a1[5] = 1;
}
}
}

canonical_decompiled_code:
void __fastcall func6(int *a1)
{
unsigned int *v1; // rdx
unsigned int v2; // eax
unsigned int v3; // er8

v1 = (unsigned int *)((char *)workshops + 4 * a1[1]);
v2 = *v1;
if ( *v1 == 2 )
{
func4((__int64)a1);
}
else if ( v2 == 3 )
{
func5((__int64)a1);
}
else if ( v2 == 1 )
{
func3((__int64)a1);
}
else
{
v3 = a1[6];
if ( v3 )
{
if ( v3 - 1 > 2 )
{
fprintf(stderr, "Unknown workshop %d\n", v3);
}
else
{
*v1 = v3;
a1[6] = 0;
}
}
else
{
fwrite("A dwarf when stark raving mad\n", 1uLL, 0x1EuLL, stderr);
a1[5] = 1;
}
}
}

original_code:
void work(dwarf *dwarf) {
  switch(workshops[dwarf->pos]) {
  case 1:
    work_trader(dwarf);
    break;
  case 2:
    work_manager(dwarf);
    break;
  case 3:
    work_appraiser(dwarf);
    break;
  default:
    switch(dwarf->rocks) {
      case 0:
        fprintf(stderr, "A dwarf when stark raving mad\n");
        dwarf->dead = 1;
        break;
      case 1:
      case 2:
      case 3:
        workshops[dwarf->pos] = dwarf->rocks;
        dwarf->rocks = 0;
        break;
      default:
        fprintf(stderr, "Unknown workshop %d\n", dwarf->rocks);
    }
  }
}

canonical_original_code:
void work(struct _dwarf *dwarf) {
  switch(workshops[dwarf->pos]) {
  case 1:
    work_trader(dwarf);
    break;
  case 2:
    work_manager(dwarf);
    break;
  case 3:
    work_appraiser(dwarf);
    break;
  default:
    switch(dwarf->rocks) {
      case 0:
        fprintf(stderr, "A dwarf when stark raving mad\n");
        dwarf->dead = 1;
        break;
      case 1:
      case 2:
      case 3:
        workshops[dwarf->pos] = dwarf->rocks;
        dwarf->rocks = 0;
        break;
      default:
        fprintf(stderr, "Unknown workshop %d\n", dwarf->rocks);
    }
  }
}


decompiled_code:
void __fastcall do_step(__int64 a1)
{
const char *v1; // rbx
size_t v2; // rax
__int64 v3; // rdx
const char *v4; // rbx
int v5; // esi
int v6; // eax
__int64 v7; // rdx

if ( !*(_DWORD *)(a1 + 20) )
{
v1 = *(const char **)(a1 + 8);
v2 = strlen(v1);
v3 = *(int *)(a1 + 16);
if ( v2 <= (int)v3 + step_count )
{
fwrite("A dwarf was struck by melancholy\n", 1uLL, 0x21uLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
else
{
v4 = &v1[v3];
v5 = v4[step_count];
if ( v4[step_count] == 109 )
{
mine(a1);
}
else if ( v4[step_count] > 109 )
{
if ( (_BYTE)v5 == 119 )
work((int *)a1);
}
else if ( (_BYTE)v5 == 100 )
{
v6 = *(_DWORD *)(a1 + 24);
if ( v6 > 0 )
{
v7 = *(int *)(a1 + 4);
*(_DWORD *)(a1 + 24) = v6 - 1;
++*((_DWORD *)world + v7 - 1);
}
}
else if ( (char)v5 <= 100 && (v5 & 0xFD) == 60 )
{
move(a1, v5);
}
}
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
const char *v1; // rbx
size_t v2; // rax
__int64 v3; // rdx
const char *v4; // rbx
int v5; // esi
int v6; // eax
__int64 v7; // rdx

if ( !*(_DWORD *)(a1 + 20) )
{
v1 = *(const char **)(a1 + 8);
v2 = strlen(v1);
v3 = *(int *)(a1 + 16);
if ( v2 <= (int)v3 + step_count )
{
fwrite("A dwarf was struck by melancholy\n", 1uLL, 0x21uLL, stderr);
*(_DWORD *)(a1 + 20) = 1;
}
else
{
v4 = &v1[v3];
v5 = v4[step_count];
if ( v4[step_count] == 109 )
{
func2(a1);
}
else if ( v4[step_count] > 109 )
{
if ( (_BYTE)v5 == 119 )
func6((int *)a1);
}
else if ( (_BYTE)v5 == 100 )
{
v6 = *(_DWORD *)(a1 + 24);
if ( v6 > 0 )
{
v7 = *(int *)(a1 + 4);
*(_DWORD *)(a1 + 24) = v6 - 1;
++*((_DWORD *)world + v7 - 1);
}
}
else if ( (char)v5 <= 100 && (v5 & 0xFD) == 60 )
{
func1(a1, v5);
}
}
}
}

original_code:
void do_step(dwarf *dwarf) {
  if(!dwarf->dead) {
    if(strlen(dwarf->instructions) <= dwarf->inst_offset + step_count) {
      fprintf(stderr, "A dwarf was struck by melancholy\n");
      dwarf->dead = 1;
    } else {
      char token = *(dwarf->instructions + dwarf->inst_offset + step_count);
      switch(token) {
      case '<':
      case '>':
        move(dwarf, token);
        break;
      case 'm':
        mine(dwarf);
        break;
      case 'd':
        if(dwarf->rocks > 0) {
          dwarf->rocks--;
          world[dwarf->pos - 1]++;
        }
        break;
      case 'w':
        work(dwarf);
        break;
      }
    }
  }
}

canonical_original_code:
void do_step(struct _dwarf *dwarf) {
  if(!dwarf->dead) {
    if(strlen(dwarf->instructions) <= dwarf->inst_offset + step_count) {
      fprintf(stderr, "A dwarf was struck by melancholy\n");
      dwarf->dead = 1;
    } else {
      char token = *(dwarf->instructions + dwarf->inst_offset + step_count);
      switch(token) {
      case '<':
      case '>':
        move(dwarf, token);
        break;
      case 'm':
        mine(dwarf);
        break;
      case 'd':
        if(dwarf->rocks > 0) {
          dwarf->rocks--;
          world[dwarf->pos - 1]++;
        }
        break;
      case 'w':
        work(dwarf);
        break;
      }
    }
  }
}

variable_types: token char


step

decompiled_code:
__int64 step()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rdi

result = fort;
if ( *(_DWORD *)fort + *(_DWORD *)(fort + 4) <= 0 )
{
++step_count;
}
else
{
v1 = 0LL;
do
{
v2 = *(_QWORD *)(*(_QWORD *)(result + 16) + 8 * v1++);
do_step(v2);
result = fort;
}
while ( *(_DWORD *)fort + *(_DWORD *)(fort + 4) > (int)v1 );
++step_count;
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rdi

result = fort;
if ( *(_DWORD *)fort + *(_DWORD *)(fort + 4) <= 0 )
{
++step_count;
}
else
{
v1 = 0LL;
do
{
v2 = *(_QWORD *)(*(_QWORD *)(result + 16) + 8 * v1++);
func7(v2);
result = fort;
}
while ( *(_DWORD *)fort + *(_DWORD *)(fort + 4) > (int)v1 );
++step_count;
}
return result;
}

original_code:
void step() {
  for(int i = 0; i < fort->dwarf_size + fort->sub_size; i++) {
    do_step(fort->dwarves[i]);
  }
  step_count++;
}


decompiled_code:
void step_inc()
{
++step_count;
}

canonical_decompiled_code:
void func9()
{
++step_count;
}

original_code:
void step_inc() {
  step_count++;
}

canonical_original_code:
void step_inc() {
  step_count++;
}


count

decompiled_code:
__int64 __fastcall count(char *a1, char a2)
{
unsigned int v2; // er8
char v3; // al

v2 = 0;
do
{
v3 = *a1++;
v2 += v3 == a2;
}
while ( v3 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, char a2)
{
unsigned int v2; // er8
char v3; // al

v2 = 0;
do
{
v3 = *a1++;
v2 += v3 == a2;
}
while ( v3 );
return v2;
}

original_code:
int count(char *tokens, char search) {
  char token;
  int count = 0;
  do {
    token = *tokens;
    if(token == search) count++;
    tokens++;
  } while(token != '\0');
  return count;
}

variable_types:
tokens
char *

variable_types: search char

variable_types: count int


original_code:
int inst_len(char *tokens, int offset) {
  int len = 0;
  int count = 1 + offset;
  int comment = 0;
  int prev_comment = 0;
  char token = *(tokens + count);
  int slen = strlen(tokens);
  while((token != '+' || comment) &&
        (token != '-' || comment) &&
        token != '\0') {
    int temp_prev_comment = prev_comment;
    prev_comment = 0;
    if(token == '!') {
      if(temp_prev_comment) {
        comment = !(comment);
      } else {
        prev_comment = 1;
      }
    }
    if(!comment) {
      switch (token) {
        case 'm':
        case 'w':
        case 'd':
        case '<':
        case '>':
          len++;
      }
    }
    count++;
    token = *(tokens + count);
  }
  return len;
}

variable_types: offset int

variable_types: len int

variable_types: comment int

variable_types: slen int


decompiled_code:
void __fastcall free_fort(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *(int *)ptr > 0 )
{
v2 = 0LL;
do
{
free(*(void **)(*(_QWORD *)(*((_QWORD *)ptr + 2) + 8 * v2) + 8LL));
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2++);
free(v3);
}
while ( *(_DWORD *)ptr > (int)v2 );
}
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *(int *)ptr > 0 )
{
v2 = 0LL;
do
{
free(*(void **)(*(_QWORD *)(*((_QWORD *)ptr + 2) + 8 * v2) + 8LL));
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2++);
free(v3);
}
while ( *(_DWORD *)ptr > (int)v2 );
}
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void free_fort(fortress* fortress) {
  for(int i = 0; i < fortress->dwarf_size; i++) {
    free(fortress->dwarves[i]->instructions);
    free(fortress->dwarves[i]);
  }
  free(fortress->dwarves);
  free(fortress);
}

canonical_original_code:
void free_fort(struct _fortress *fortress) {
  for(int i = 0; i < fortress->dwarf_size; i++) {
    free(fortress->dwarves[i]->instructions);
    free(fortress->dwarves[i]);
  }
  free(fortress->dwarves);
  free(fortress);
}

variable_types: struct _fortress { int dwarf_size; int sub_size; int alive; struct _dwarf { int id; int pos; char *instructions; int inst_offset; int dead; int rocks; } **dwarves; } *

user_defined_types: struct _fortress { int dwarf_size; int sub_size; int alive; struct _dwarf **dwarves; }


encode

decompiled_code:
__int64 __fastcall encode(int a1)
{
return (unsigned int)codepage[a1];
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)codepage[a1];
}

original_code:
wchar_t encode(int character) {
  return codepage[character];
}

canonical_original_code:
int encode(int character) {
  return codepage[character];
}

variable_types: character int


decode

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

for ( result = 0LL; result != 256; ++result )
{
if ( codepage[result] == a1 )
return (unsigned int)result;
}
return 0xFFFFFFFFLL;
}

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

for ( result = 0LL; result != 256; ++result )
{
if ( codepage[result] == a1 )
return (unsigned int)result;
}
return 0xFFFFFFFFLL;
}

original_code:
int decode(wchar_t str) {
  for(int i = 0; i < 256; i++) {
    if(codepage[i] == str) {
      return i;
    }
  }
  return -1;
}

canonical_original_code:
int decode(int str) {
  for(int i = 0; i < 256; i++) {
    if(codepage[i] == str) {
      return i;
    }
  }
  return -1;
}

variable_types: str int


decompiled_code:
__int64 __fastcall syntactically_correct(int a1)
{
int v1; // eax
const unsigned __int16 *v2; // rcx
__int64 result; // rax
__int64 v4; // rdx

v1 = prev_comment;
prev_comment = 0;
if ( (_BYTE)a1 )
{
if ( (_BYTE)a1 != 33 )
{
v2 = *_ctype_b_loc();
result = 1LL;
if ( (v2[(char)a1] & 0x2000) != 0 )
return result;
result = (unsigned int)syn_comment;
if ( !syn_comment )
{
if ( (_BYTE)a1 == 43 )
{
result = 1LL;
++syn_dwarf;
return result;
}
if ( (_BYTE)a1 == 45 )
{
++syn_sub;
return 1LL;
}
if ( !__PAIR64__(syn_dwarf, syn_sub)
|| (unsigned __int8)(a1 - 60) > 0x3Bu
|| (v4 = 0x802010000000005LL, !_bittest64(&v4, (unsigned int)(a1 - 60))) )
{
syntax_error = (__int64)"Illegal token";
return result;
}
}
return 1LL;
}
if ( v1 )
syn_comment = syn_comment == 0;
else
prev_comment = 1;
result = 1LL;
}
else
{
if ( syn_dwarf <= 0 )
syntax_error = (__int64)"No dwarves";
result = syn_dwarf > 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // eax
const unsigned __int16 *v2; // rcx
__int64 result; // rax
__int64 v4; // rdx

v1 = prev_comment;
prev_comment = 0;
if ( (_BYTE)a1 )
{
if ( (_BYTE)a1 != 33 )
{
v2 = *_ctype_b_loc();
result = 1LL;
if ( (v2[(char)a1] & 0x2000) != 0 )
return result;
result = (unsigned int)syn_comment;
if ( !syn_comment )
{
if ( (_BYTE)a1 == 43 )
{
result = 1LL;
++syn_dwarf;
return result;
}
if ( (_BYTE)a1 == 45 )
{
++syn_sub;
return 1LL;
}
if ( !__PAIR64__(syn_dwarf, syn_sub)
|| (unsigned __int8)(a1 - 60) > 0x3Bu
|| (v4 = 0x802010000000005LL, !_bittest64(&v4, (unsigned int)(a1 - 60))) )
{
syntax_error = (__int64)"Illegal token";
return result;
}
}
return 1LL;
}
if ( v1 )
syn_comment = syn_comment == 0;
else
prev_comment = 1;
result = 1LL;
}
else
{
if ( syn_dwarf <= 0 )
syntax_error = (__int64)"No dwarves";
result = syn_dwarf > 0;
}
return result;
}

original_code:
int syntactically_correct(char token) {
  int temp_prev_comment = prev_comment;
  prev_comment = 0;
  switch(token) {
  case '\0':
    if(!(_syn_dwarf > 0)) {
      syntax_error = "No dwarves";
    }
    return _syn_dwarf > 0;
  case '!':
    if(temp_prev_comment) {
      _syn_comment = !(_syn_comment);
    } else {
      prev_comment = 1;
    }
    return 1;
  }
  if(((*__ctype_b_loc ())[(int) ((token))] & (unsigned short int) _ISspace) || _syn_comment) {
    return 1;
  }
  switch(token) {
  case '-':
    _syn_sub++;
    return 1;
  case '+':
    _syn_dwarf++;
    return 1;
  }
  if(_syn_dwarf || _syn_sub) {
    switch(token) {
    case 'm':
    case 'd':
    case 'w':
    case '<':
    case '>':
      return 1;
    }
  }
  syntax_error = "Illegal token";
  return 0;
}

canonical_original_code:
int syntactically_correct(char token) {
  int temp_prev_comment = prev_comment;
  prev_comment = 0;
  switch(token) {
  case '\0':
    if(!(_syn_dwarf > 0)) {
      syntax_error = "No dwarves";
    }
    return _syn_dwarf > 0;
  case '!':
    if(temp_prev_comment) {
      _syn_comment = !(_syn_comment);
    } else {
      prev_comment = 1;
    }
    return 1;
  }
  if(((*__ctype_b_loc ())[(int) ((token))] & (unsigned short) _ISspace) || _syn_comment) {
    return 1;
  }
  switch(token) {
  case '-':
    _syn_sub++;
    return 1;
  case '+':
    _syn_dwarf++;
    return 1;
  }
  if(_syn_dwarf || _syn_sub) {
    switch(token) {
    case 'm':
    case 'd':
    case 'w':
    case '<':
    case '>':
      return 1;
    }
  }
  syntax_error = "Illegal token";
  return 0;
}


decompiled_code:
__int64 __fastcall syntax_check(char *a1)
{
unsigned int v1; // er14
unsigned int v2; // er13
unsigned int v3; // er12
char i; // bp

syn_comment = 0;
prev_comment = 0;
syn_dwarf = 0;
syn_sub = 0;
v1 = 1;
v2 = 1;
v3 = 1;
do
{
for ( i = *a1++; i == 10; ++a1 )
{
i = *a1;
++v2;
v3 = 1;
}
if ( !(unsigned int)syntactically_correct(i) )
{
v1 = 0;
fprintf(stderr, "file.df:%d:%d: error: %s\n", v3, v2, (const char *)syntax_error);
}
++v3;
}
while ( i );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
unsigned int v1; // er14
unsigned int v2; // er13
unsigned int v3; // er12
char i; // bp

syn_comment = 0;
prev_comment = 0;
syn_dwarf = 0;
syn_sub = 0;
v1 = 1;
v2 = 1;
v3 = 1;
do
{
for ( i = *a1++; i == 10; ++a1 )
{
i = *a1;
++v2;
v3 = 1;
}
if ( !(unsigned int)func0(i) )
{
v1 = 0;
fprintf(stderr, "file.df:%d:%d: error: %s\n", v3, v2, (const char *)syntax_error);
}
++v3;
}
while ( i );
return v1;
}

original_code:
int syntax_check(char *tokens) {
  _syn_comment = 0;
  prev_comment = 0;
  _syn_dwarf = 0;
  _syn_sub = 0;
  char token;
  int line = 1;
  int byte = 1;
  int res;
  int end_res = 1;
  do {
    token = *tokens;
    switch(token) {
      case '\n':
        line++;
        byte = 1;
        break;
      default:
        res = syntactically_correct(token);
        if(!res) {
          fprintf(stderr, "file.df:%d:%d: error: %s\n", byte, line, syntax_error);
          end_res = 0;
        }
        byte++;
        break;
    }
    tokens++;
  } while(token != '\0');
  return end_res;
}

variable_types: line int

variable_types: byte int

variable_types: res int


main

repo: SoftlySplinter/softshell

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *file[21]; // [rsp+0h] [rbp-A8h] BYREF

signal(2, (__sighandler_t)1);
while ( 1 )
{
if ( (int)prompt_and_parse(file) <= 0 )
{
puts("Goodbye and thanks for all the fish");
exit(0);
}
if ( !fork() )
break;
wait(0LL);
}
execvp(file[0], file);
printf("%s: not found\n", file[0]);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *file[21]; // [rsp+0h] [rbp-A8h] BYREF

signal(2, (__sighandler_t)1);
while ( 1 )
{
if ( (int)func7(file) <= 0 )
{
puts("Goodbye and thanks for all the fish");
exit(0);
}
if ( !fork() )
break;
wait(0LL);
}
execvp(file[0], file);
printf("%s: not found\n", file[0]);
return 1;
}

original_code:
int main(int argc, char** argv) {
 signal(2, ((__sighandler_t) 1));
 char* args[20];
 while(prompt_and_parse(args)>0) {
  if(fork() == 0) {
   execvp(args[0], args);
   printf("%s: not found\n",args[0]);
   return 1;
  } else {
   wait(0);
  }
 }
 printf("Goodbye and thanks for all the fish\n");
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 signal(2, ((void (*)(int)) 1));
 char *args[20];
 while(prompt_and_parse(args)>0) {
  if(fork() == 0) {
   execvp(args[0], args);
   printf("%s: not found\n",args[0]);
   return 1;
  } else {
   wait(0);
  }
 }
 printf("Goodbye and thanks for all the fish\n");
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
args
char *[20]


decompiled_code:
__int64 __fastcall prompt_and_parse(char **a1)
{
_QWORD *v2; // rbx
char *v3; // rax

printf("$ ");
if ( !(unsigned int)gets() || strstr(line_0, "exit") )
return 0xFFFFFFFFLL;
v2 = a1 + 1;
*a1 = strtok(line_0, " \t");
do
{
++v2;
v3 = strtok(0LL, " \t");
*(v2 - 1) = v3;
}
while ( v3 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char **a1)
{
_QWORD *v2; // rbx
char *v3; // rax

printf("$ ");
if ( !(unsigned int)gets() || strstr(line_0, "exit") )
return 0xFFFFFFFFLL;
v2 = a1 + 1;
*a1 = strtok(line_0, " \t");
do
{
++v2;
v3 = strtok(0LL, " \t");
*(v2 - 1) = v3;
}
while ( v3 );
return 1LL;
}

original_code:
int prompt_and_parse(char **args) {
 static char line[100];
 printf("$ ");
 if(gets(line) == ((void *)0))
  return -1;
 if(strstr(line, "exit"))
  return -1;
 *args++ = strtok(line, " \t");
 while(*args++ = strtok(((void *)0), " \t"))
  ;
 return 1;
}

variable_types:
args
char **

variable_types:
line
char [100]