decompiled_code:
__int64 __fastcall tree_add_node(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // eax
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a2 + 56);
v3 = *(_DWORD *)(a1 + 56);
if ( v2 >= v3 )
{
if ( v2 <= v3 )
return 4294967294LL;
result = tree_add_node(*(_QWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
{
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)a2 = a1;
++*(_DWORD *)(*(_QWORD *)(a2 + 24) + 8LL);
tree_update_list((_QWORD *)a2);
tree_update_list(*(_QWORD **)(a2 + 40));
tree_update_list(*(_QWORD **)(a2 + 32));
}
else if ( (_DWORD)result )
{
return result;
}
goto LABEL_7;
}
result = tree_add_node(*(_QWORD *)(a1 + 8));
if ( (_DWORD)result == -1 )
{
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)a2 = a1;
++*(_DWORD *)(*(_QWORD *)(a2 + 24) + 8LL);
tree_update_list((_QWORD *)a2);
tree_update_list(*(_QWORD **)(a2 + 40));
tree_update_list(*(_QWORD **)(a2 + 32));
goto LABEL_7;
}
if ( !(_DWORD)result )
{
LABEL_7:
tree_depth(a1);
tree_balance(a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // eax
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a2 + 56);
v3 = *(_DWORD *)(a1 + 56);
if ( v2 >= v3 )
{
if ( v2 <= v3 )
return 4294967294LL;
result = func7(*(_QWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
{
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)a2 = a1;
++*(_DWORD *)(*(_QWORD *)(a2 + 24) + 8LL);
func6((_QWORD *)a2);
func6(*(_QWORD **)(a2 + 40));
func6(*(_QWORD **)(a2 + 32));
}
else if ( (_DWORD)result )
{
return result;
}
goto LABEL_7;
}
result = func7(*(_QWORD *)(a1 + 8));
if ( (_DWORD)result == -1 )
{
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)a2 = a1;
++*(_DWORD *)(*(_QWORD *)(a2 + 24) + 8LL);
func6((_QWORD *)a2);
func6(*(_QWORD **)(a2 + 40));
func6(*(_QWORD **)(a2 + 32));
goto LABEL_7;
}
if ( !(_DWORD)result )
{
LABEL_7:
func0(a1);
func3(a1);
result = 0LL;
}
return result;
}

original_code:
static int tree_add_node(struct tree* parent, struct tree* t)
{
        if (parent == ((void *)0) || t == ((void *)0))
                return -1;
        if (t->key < parent->key)
        {
                int s = tree_add_node(parent->left, t);
                switch (s) {
                case -1:
                        parent->left = t;
                        t->parent = parent;
                        t->root->nodes++;
                        tree_update_list(t);
                        tree_update_list(t->prev);
                        tree_update_list(t->next);
                case 0:
                        break;
                default:
                        return s;
                }
        }
        else if(t->key > parent->key)
        {
                int s = tree_add_node(parent->right, t);
                switch (s)
                {
                case -1:
                        parent->right = t;
                        t->parent = parent;
                        t->root->nodes++;
                        tree_update_list(t);
                        tree_update_list(t->prev);
                        tree_update_list(t->next);
                case 0:
                        break;
                default:
                        return s;
                }
        }
        else
                return -2;
        tree_depth(parent);
        tree_balance(parent);
        return 0;
}

canonical_original_code:
static int tree_add_node(struct tree *parent, struct tree *t)
{
        if (parent == ((void *)0) || t == ((void *)0))
                return -1;
        if (t->key < parent->key)
        {
                int s = tree_add_node(parent->left, t);
                switch (s) {
                case -1:
                        parent->left = t;
                        t->parent = parent;
                        t->root->nodes++;
                        tree_update_list(t);
                        tree_update_list(t->prev);
                        tree_update_list(t->next);
                case 0:
                        break;
                default:
                        return s;
                }
        }
        else if(t->key > parent->key)
        {
                int s = tree_add_node(parent->right, t);
                switch (s)
                {
                case -1:
                        parent->right = t;
                        t->parent = parent;
                        t->root->nodes++;
                        tree_update_list(t);
                        tree_update_list(t->prev);
                        tree_update_list(t->next);
                case 0:
                        break;
                default:
                        return s;
                }
        }
        else
                return -2;
        tree_depth(parent);
        tree_balance(parent);
        return 0;
}

variable_types:
t
struct tree *

variable_types: s int

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


repo: bemk/AVL

decompiled_code:
__int64 __fastcall tree_find_node(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

if ( !a2 )
return 0LL;
v2 = *(_DWORD *)(a2 + 56);
result = a2;
if ( v2 != (_DWORD)a1 )
{
if ( v2 <= (int)a1 )
result = tree_find_node(a1, *(_QWORD *)(a2 + 16));
else
result = tree_find_node(a1, *(_QWORD *)(a2 + 8));
}
return result;
}

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

if ( !a2 )
return 0LL;
v2 = *(_DWORD *)(a2 + 56);
result = a2;
if ( v2 != (_DWORD)a1 )
{
if ( v2 <= (int)a1 )
result = func8(a1, *(_QWORD *)(a2 + 16));
else
result = func8(a1, *(_QWORD *)(a2 + 8));
}
return result;
}

original_code:
static struct tree* tree_find_node(int key, struct tree* tree)
{
        if (tree == ((void *)0))
                return ((void *)0);
        if (tree->key == key)
                return tree;
        if (key < tree->key)
                return tree_find_node(key, tree->left);
        else
                return tree_find_node(key, tree->right);
}

canonical_original_code:
static struct tree *tree_find_node(int key, struct tree *tree)
{
        if (tree == ((void *)0))
                return ((void *)0);
        if (tree->key == key)
                return tree;
        if (key < tree->key)
                return tree_find_node(key, tree->left);
        else
                return tree_find_node(key, tree->right);
}

variable_types: key int

variable_types:
tree
struct tree *

return_type: struct tree { struct tree *parent; struct tree *left; struct tree *right; struct tree_root { struct tree *tree; unsigned int nodes; struct tree *(*add)(int key, void *data, struct tree_root *root); void *(*find)(int key, struct tree_root *root); int (*delete)(int, struct tree_root *, int (*dtor)(void *, void *), void *); int (*flush)(struct tree_root *, int (*dtor)(void *, void *), void *); union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *root; struct tree *next; struct tree *prev; int rdepth; int ldepth; int key; void *data; } *

user_defined_types: struct tree { struct tree *parent; struct tree *left; struct tree *right; struct tree_root *root; struct tree *next; struct tree *prev; int rdepth; int ldepth; int key; void *data; }

user_defined_types: struct tree_root { struct tree *tree; unsigned int nodes; struct tree *(*add)(int key, void *data, struct tree_root *root); void *(*find)(int key, struct tree_root *root); int (*delete)(int, struct tree_root *, int (*dtor)(void *, void *), void *); int (*flush)(struct tree_root *, int (*dtor)(void *, void *), void *); union pthread_mutex_t mutex; }

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

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


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

v2 = a2;
if ( a2 )
{
pthread_mutex_lock((pthread_mutex_t *)(a2 + 48));
v3 = tree_find_node(a1, *(_QWORD *)a2);
v2 = v3;
if ( v3 )
v2 = *(_QWORD *)(v3 + 64);
pthread_mutex_unlock((pthread_mutex_t *)(a2 + 48));
}
return v2;
}

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

v2 = a2;
if ( a2 )
{
pthread_mutex_lock((pthread_mutex_t *)(a2 + 48));
v3 = func8(a1, *(_QWORD *)a2);
v2 = v3;
if ( v3 )
v2 = *(_QWORD *)(v3 + 64);
pthread_mutex_unlock((pthread_mutex_t *)(a2 + 48));
}
return v2;
}

original_code:
static void* tree_find(int key, struct tree_root* t)
{
        if (t == ((void *)0))
                return ((void *)0);
        void* ret = ((void *)0);
        pthread_mutex_lock(&t->mutex);
        struct tree* node = tree_find_node(key, t->tree);
        if (node == ((void *)0))
                goto err;
        ret = node->data;
err:
        pthread_mutex_unlock(&t->mutex);
        return ret;
}

canonical_original_code:
static void *tree_find(int key, struct tree_root *t)
{
        if (t == ((void *)0))
                return ((void *)0);
        void *ret = ((void *)0);
        pthread_mutex_lock(&t->mutex);
        struct tree *node = tree_find_node(key, t->tree);
        if (node == ((void *)0))
                goto err;
        ret = node->data;
err:
        pthread_mutex_unlock(&t->mutex);
        return ret;
}

variable_types: struct tree_root { struct tree { struct tree *parent; struct tree *left; struct tree *right; struct tree_root *root; struct tree *next; struct tree *prev; int rdepth; int ldepth; int key; void *data; } *tree; unsigned int nodes; struct tree *(*add)(int key, void *data, struct tree_root *root); void *(*find)(int key, struct tree_root *root); int (*delete)(int, struct tree_root *, int (*dtor)(void *, void *), void *); int (*flush)(struct tree_root *, int (*dtor)(void *, void *), void *); union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *

variable_types:
ret
void *

variable_types: struct tree { struct tree *parent; struct tree *left; struct tree *right; struct tree_root { struct tree *tree; unsigned int nodes; struct tree *(*add)(int key, void *data, struct tree_root *root); void *(*find)(int key, struct tree_root *root); int (*delete)(int, struct tree_root *, int (*dtor)(void *, void *), void *); int (*flush)(struct tree_root *, int (*dtor)(void *, void *), void *); union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *root; struct tree *next; struct tree *prev; int rdepth; int ldepth; int key; void *data; } *

return_type: void *


decompiled_code:
__int64 __fastcall tree_add(__int64 a1, __int64 a2)
{
pthread_mutex_t *v3; // r12
__int64 v4; // rdi
unsigned int v5; // ebx

if ( !a1 || !a2 )
return (unsigned int)-1;
v3 = (pthread_mutex_t *)(a1 + 48);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
{
v5 = tree_add_node(v4, a2);
}
else
{
*(_QWORD *)a1 = a2;
v5 = 4;
}
pthread_mutex_unlock(v3);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
pthread_mutex_t *v3; // r12
__int64 v4; // rdi
unsigned int v5; // ebx

if ( !a1 || !a2 )
return (unsigned int)-1;
v3 = (pthread_mutex_t *)(a1 + 48);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
{
v5 = func7(v4, a2);
}
else
{
*(_QWORD *)a1 = a2;
v5 = 4;
}
pthread_mutex_unlock(v3);
return v5;
}

original_code:
static int tree_add(struct tree_root* root, struct tree* tree)
{
        if (root == ((void *)0) || tree == ((void *)0))
                return -1;
        int ret = 4;
        pthread_mutex_lock(&root->mutex);
        if (root->tree != ((void *)0))
        {
                ret = tree_add_node(root->tree, tree);
                goto success;
        }
        root->tree = tree;
success:
        pthread_mutex_unlock(&root->mutex);
        return ret;
}

canonical_original_code:
static int tree_add(struct tree_root *root, struct tree *tree)
{
        if (root == ((void *)0) || tree == ((void *)0))
                return -1;
        int ret = 4;
        pthread_mutex_lock(&root->mutex);
        if (root->tree != ((void *)0))
        {
                ret = tree_add_node(root->tree, tree);
                goto success;
        }
        root->tree = tree;
success:
        pthread_mutex_unlock(&root->mutex);
        return ret;
}

variable_types: ret int

return_type: int


cleanup

decompiled_code:
__int64 __fastcall cleanup(void *a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(void *a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
free(a1);
return 0LL;
}

original_code:
static int cleanup(void* ptr, void* args)
{
        if (ptr == ((void *)0))
                return -1;
        free(ptr);
        return 0;
}

canonical_original_code:
static int cleanup(void *ptr, void *args)
{
        if (ptr == ((void *)0))
                return -1;
        free(ptr);
        return 0;
}

variable_types:
ptr
void *

variable_types:
args
void *


decompiled_code:
__int64 __fastcall tree_flush(void *ptr, void (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
if ( !ptr )
return 0xFFFFFFFFLL;
pthread_mutex_lock((pthread_mutex_t *)((char *)ptr + 48));
tree_flush_node(*(void **)ptr, a2, a3);
pthread_mutex_unlock((pthread_mutex_t *)((char *)ptr + 48));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 48));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(void *ptr, void (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
if ( !ptr )
return 0xFFFFFFFFLL;
pthread_mutex_lock((pthread_mutex_t *)((char *)ptr + 48));
func12(*(void **)ptr, a2, a3);
pthread_mutex_unlock((pthread_mutex_t *)((char *)ptr + 48));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 48));
free(ptr);
return 0LL;
}

original_code:
int tree_flush(struct tree_root* root, int(*dtor)(void*,void*), void* dtor_args)
{
        if (root == ((void *)0))
                return -1;
        pthread_mutex_lock(&root->mutex);
        tree_flush_node(root->tree, dtor, dtor_args);
        pthread_mutex_unlock(&root->mutex);
        pthread_mutex_destroy(&root->mutex);
        memset(root, 0, sizeof(*root));
        free(root);
        return 0;
}

canonical_original_code:
int tree_flush(struct tree_root *root, int (*dtor)(void *, void *), void *dtor_args)
{
        if (root == ((void *)0))
                return -1;
        pthread_mutex_lock(&root->mutex);
        tree_flush_node(root->tree, dtor, dtor_args);
        pthread_mutex_unlock(&root->mutex);
        pthread_mutex_destroy(&root->mutex);
        memset(root, 0, sizeof(*root));
        free(root);
        return 0;
}

variable_types:
dtor
int (*)(void *, void *)


original_code:
static int
tree_delete_node(int key, struct tree* tree, int(*dtor)(void*, void*), void* dtor_args)
{
        if (tree == ((void *)0))
                return -1;
        struct tree* t = tree;
        if (tree->key != key)
        {
                if (key < tree->key)
                {
                        key = tree_delete_node(key, tree->left, dtor, dtor_args);
                        tree_depth(tree);
                        return key;
                }
                else
                {
                        key = tree_delete_node(key, tree->right, dtor, dtor_args);
                        tree_depth(tree);
                        return key;
                }
        }
        printf("Found the damn basterd!\n");
        if (t == ((void *)0))
                return -3;
        if (t->ldepth == 0 && t->rdepth == 0)
        {
                if (t->key < t->parent->key)
                        t->parent->left = ((void *)0);
                else
                        t->parent->right = ((void *)0);
        }
        else if (t->right == ((void *)0) && t->left != ((void *)0))
        {
                if (t->key < t->parent->key)
                        t->parent->left = t->left;
                else
                        t->parent->right = t->left;
                t->left->parent = t->parent;
        }
        else if (t->right != ((void *)0) && t->left == ((void *)0))
        {
                if (t->key < t->parent->key)
                        t->parent->left = t->right;
                else
                        t->parent->right = t->right;
                t->right->parent = t->parent;
        }
        else
        {
                struct tree* successor = tree_find_leftmost(t->right);
                if (successor == ((void *)0))
                        return -1;
                struct tree* walker = successor->parent;
                successor->parent->left = successor->right;
                if (successor->right != ((void *)0))
                        successor->right->parent = successor->parent;
                tree_depth(successor->parent);
                successor->parent = t->parent;
                if (t->right == successor)
                        successor->right = ((void *)0);
                else
                        successor->right = t->right;
                successor->left = t->left;
                if (successor->parent == ((void *)0))
                        successor->root->tree = successor;
                else if (successor->key < successor->parent->key)
                        successor->parent->left = successor;
                else
                        successor->parent->right = successor;
                if (successor->right != ((void *)0))
                        successor->right->parent = successor;
                successor->left->parent = successor;
                tree_depth(successor);
                tree_depth(successor->parent);
                if (walker != t)
                {
                        while(walker != ((void *)0) && walker != successor)
                        {
                                tree_depth(walker);
                                tree_balance(walker);
                                walker = walker->parent;
                        }
                }
        }
        tree_update_list(t->prev);
        tree_update_list(t->next);
        tree_depth(t->parent);
        tree_balance(t->parent);
        t->root->nodes--;
        if (dtor != ((void *)0))
                dtor(t->data, dtor_args);
        memset(t, 0, sizeof(*t));
        free(t);
        return 0;
}

canonical_original_code:
static int tree_delete_node(int key, struct tree *tree, int (*dtor)(void *, void *), void *dtor_args)
{
        if (tree == ((void *)0))
                return -1;
        struct tree *t = tree;
        if (tree->key != key)
        {
                if (key < tree->key)
                {
                        key = tree_delete_node(key, tree->left, dtor, dtor_args);
                        tree_depth(tree);
                        return key;
                }
                else
                {
                        key = tree_delete_node(key, tree->right, dtor, dtor_args);
                        tree_depth(tree);
                        return key;
                }
        }
        printf("Found the damn basterd!\n");
        if (t == ((void *)0))
                return -3;
        if (t->ldepth == 0 && t->rdepth == 0)
        {
                if (t->key < t->parent->key)
                        t->parent->left = ((void *)0);
                else
                        t->parent->right = ((void *)0);
        }
        else if (t->right == ((void *)0) && t->left != ((void *)0))
        {
                if (t->key < t->parent->key)
                        t->parent->left = t->left;
                else
                        t->parent->right = t->left;
                t->left->parent = t->parent;
        }
        else if (t->right != ((void *)0) && t->left == ((void *)0))
        {
                if (t->key < t->parent->key)
                        t->parent->left = t->right;
                else
                        t->parent->right = t->right;
                t->right->parent = t->parent;
        }
        else
        {
                struct tree *successor = tree_find_leftmost(t->right);
                if (successor == ((void *)0))
                        return -1;
                struct tree *walker = successor->parent;
                successor->parent->left = successor->right;
                if (successor->right != ((void *)0))
                        successor->right->parent = successor->parent;
                tree_depth(successor->parent);
                successor->parent = t->parent;
                if (t->right == successor)
                        successor->right = ((void *)0);
                else
                        successor->right = t->right;
                successor->left = t->left;
                if (successor->parent == ((void *)0))
                        successor->root->tree = successor;
                else if (successor->key < successor->parent->key)
                        successor->parent->left = successor;
                else
                        successor->parent->right = successor;
                if (successor->right != ((void *)0))
                        successor->right->parent = successor;
                successor->left->parent = successor;
                tree_depth(successor);
                tree_depth(successor->parent);
                if (walker != t)
                {
                        while(walker != ((void *)0) && walker != successor)
                        {
                                tree_depth(walker);
                                tree_balance(walker);
                                walker = walker->parent;
                        }
                }
        }
        tree_update_list(t->prev);
        tree_update_list(t->next);
        tree_depth(t->parent);
        tree_balance(t->parent);
        t->root->nodes--;
        if (dtor != ((void *)0))
                dtor(t->data, dtor_args);
        memset(t, 0, sizeof(*t));
        free(t);
        return 0;
}


decompiled_code:
__int64 __fastcall tree_delete(unsigned int a1, __int64 a2, void (__fastcall *a3)(__int64, __int64), __int64 a4)
{
unsigned int v6; // ebx

if ( !a2 )
return (unsigned int)-1;
pthread_mutex_lock((pthread_mutex_t *)(a2 + 48));
v6 = tree_delete_node(a1, *(__int64 **)a2, a3, a4);
pthread_mutex_unlock((pthread_mutex_t *)(a2 + 48));
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1, __int64 a2, void (__fastcall *a3)(__int64, __int64), __int64 a4)
{
unsigned int v6; // ebx

if ( !a2 )
return (unsigned int)-1;
pthread_mutex_lock((pthread_mutex_t *)(a2 + 48));
v6 = func14(a1, *(__int64 **)a2, a3, a4);
pthread_mutex_unlock((pthread_mutex_t *)(a2 + 48));
return v6;
}

original_code:
static int
tree_delete(int key, struct tree_root* root, int (*dtor)(void*,void*), void* dtor_args)
{
        if (root == ((void *)0))
                return -1;
        pthread_mutex_lock(&root->mutex);
        int ret = tree_delete_node(key, root->tree, dtor, dtor_args);
        pthread_mutex_unlock(&root->mutex);
        return ret;
}

canonical_original_code:
static int tree_delete(int key, struct tree_root *root, int (*dtor)(void *, void *), void *dtor_args)
{
        if (root == ((void *)0))
                return -1;
        pthread_mutex_lock(&root->mutex);
        int ret = tree_delete_node(key, root->tree, dtor, dtor_args);
        pthread_mutex_unlock(&root->mutex);
        return ret;
}


decompiled_code:
char *tree_new_avl()
{
char *v0; // rbx

v0 = (char *)malloc(0x58uLL);
if ( v0 )
memset(v0, 0, 0x58uLL);
*((_QWORD *)v0 + 2) = tree_new_node;
*((_QWORD *)v0 + 3) = tree_find;
*((_QWORD *)v0 + 4) = tree_delete;
*((_QWORD *)v0 + 5) = tree_flush;
pthread_mutex_init((pthread_mutex_t *)(v0 + 48), 0LL);
return v0;
}

canonical_decompiled_code:
char *func17()
{
char *v0; // rbx

v0 = (char *)malloc(0x58uLL);
if ( v0 )
memset(v0, 0, 0x58uLL);
*((_QWORD *)v0 + 2) = tree_new_node;
*((_QWORD *)v0 + 3) = tree_find;
*((_QWORD *)v0 + 4) = tree_delete;
*((_QWORD *)v0 + 5) = tree_flush;
pthread_mutex_init((pthread_mutex_t *)(v0 + 48), 0LL);
return v0;
}

original_code:
struct tree_root* tree_new_avl()
{
        struct tree_root* t = malloc(sizeof(*t));
        if (t != ((void *)0))
                memset(t, 0, sizeof(*t));
        t->add = tree_new_node;
        t->find = tree_find;
        t->delete = tree_delete;
        t->flush = tree_flush;
        pthread_mutex_init(&t->mutex, ((void *)0));
        return t;
}

canonical_original_code:
struct tree_root *tree_new_avl()
{
        struct tree_root *t = malloc(sizeof(*t));
        if (t != ((void *)0))
                memset(t, 0, sizeof(*t));
        t->add = tree_new_node;
        t->find = tree_find;
        t->delete = tree_delete;
        t->flush = tree_flush;
        pthread_mutex_init(&t->mutex, ((void *)0));
        return t;
}

return_type: struct tree_root { struct tree { struct tree *parent; struct tree *left; struct tree *right; struct tree_root *root; struct tree *next; struct tree *prev; int rdepth; int ldepth; int key; void *data; } *tree; unsigned int nodes; struct tree *(*add)(int key, void *data, struct tree_root *root); void *(*find)(int key, struct tree_root *root); int (*delete)(int, struct tree_root *, int (*dtor)(void *, void *), void *); int (*flush)(struct tree_root *, int (*dtor)(void *, void *), void *); union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *


decompiled_code:
__int64 __fastcall tree_dump(__int64 a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
puts("Dumping tree!");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
tree_dump_node(*(_QWORD *)a1);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
puts("\nDone dumping tree!");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
puts("Dumping tree!");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
func16(*(_QWORD *)a1);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
puts("\nDone dumping tree!");
return 0LL;
}

original_code:
int tree_dump(struct tree_root* root)
{
        if (root == ((void *)0))
                return -1;
        printf("Dumping tree!\n");
        pthread_mutex_lock(&root->mutex);
        tree_dump_node(root->tree);
        pthread_mutex_unlock(&root->mutex);
        printf("\nDone dumping tree!\n");
        return 0;
}

canonical_original_code:
int tree_dump(struct tree_root *root)
{
        if (root == ((void *)0))
                return -1;
        printf("Dumping tree!\n");
        pthread_mutex_lock(&root->mutex);
        tree_dump_node(root->tree);
        pthread_mutex_unlock(&root->mutex);
        printf("\nDone dumping tree!\n");
        return 0;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
__int64 v4; // rbp
int i; // ebx
__int64 v6; // r12
_QWORD *v7; // rbx
_QWORD *v8; // r13
unsigned int v9; // eax
__int64 j; // r12
_QWORD *v11; // r12
_QWORD *v12; // rbx
int result; // eax

v3 = tree_new_avl();
if ( v3 )
{
v4 = (__int64)v3;
for ( i = 0; i != 17; ++i )
{
tree_new_node(i, 0LL, (_QWORD *)v4);
tree_dump(v4);
}
tree_delete(8u, v4, (void (__fastcall *)(__int64, __int64))cleanup, 0LL);
tree_delete(9u, v4, (void (__fastcall *)(__int64, __int64))cleanup, 0LL);
tree_dump(v4);
do
{
tree_new_node(i, 0LL, (_QWORD *)v4);
tree_dump(v4);
++i;
}
while ( i != 33 );
v6 = tree_find_node(4LL, *(_QWORD *)v4);
v7 = tree_find_prev((_QWORD *)v6);
v8 = tree_find_next((_QWORD *)v6);
printf("ptr: %X - %X - %X\n", v7, v6, v8);
printf("key: %X - %X - %X\n", *((unsigned int *)v7 + 14), *(unsigned int *)(v6 + 56), *((unsigned int *)v8 + 14));
v9 = pthread_mutex_trylock((pthread_mutex_t *)(v4 + 48));
printf("%X\n", v9);
pthread_mutex_unlock((pthread_mutex_t *)(v4 + 48));
for ( j = tree_find(0, v4); j; j = (__int64)tree_find_next((_QWORD *)j) )
{
printf("%X ", *(unsigned int *)(j + 56));
v7 = (_QWORD *)j;
}
putchar(10);
do
{
printf("%X ", *((unsigned int *)v7 + 14));
v11 = v7;
v7 = tree_find_prev(v7);
}
while ( v7 );
putchar(10);
do
{
printf("%X ", *((unsigned int *)v11 + 14));
v12 = v11;
v11 = (_QWORD *)v11[4];
}
while ( v11 );
putchar(10);
do
{
printf("%X ", *((unsigned int *)v12 + 14));
v12 = (_QWORD *)v12[5];
}
while ( v12 );
putchar(10);
tree_flush((void *)v4, (void (__fastcall *)(_QWORD, __int64))cleanup, 0LL);
result = 0;
}
else
{
fwrite("An error occured, no memory!\n", 1uLL, 0x1DuLL, stderr);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
__int64 v4; // rbp
int i; // ebx
__int64 v6; // r12
_QWORD *v7; // rbx
_QWORD *v8; // r13
unsigned int v9; // eax
__int64 j; // r12
_QWORD *v11; // r12
_QWORD *v12; // rbx
int result; // eax

v3 = func17();
if ( v3 )
{
v4 = (__int64)v3;
for ( i = 0; i != 17; ++i )
{
tree_new_node(i, 0LL, (_QWORD *)v4);
func18(v4);
}
func15(8u, v4, (void (__fastcall *)(__int64, __int64))cleanup, 0LL);
func15(9u, v4, (void (__fastcall *)(__int64, __int64))cleanup, 0LL);
func18(v4);
do
{
tree_new_node(i, 0LL, (_QWORD *)v4);
func18(v4);
++i;
}
while ( i != 33 );
v6 = func8(4LL, *(_QWORD *)v4);
v7 = tree_find_prev((_QWORD *)v6);
v8 = tree_find_next((_QWORD *)v6);
printf("ptr: %X - %X - %X\n", v7, v6, v8);
printf("key: %X - %X - %X\n", *((unsigned int *)v7 + 14), *(unsigned int *)(v6 + 56), *((unsigned int *)v8 + 14));
v9 = pthread_mutex_trylock((pthread_mutex_t *)(v4 + 48));
printf("%X\n", v9);
pthread_mutex_unlock((pthread_mutex_t *)(v4 + 48));
for ( j = func9(0, v4); j; j = (__int64)tree_find_next((_QWORD *)j) )
{
printf("%X ", *(unsigned int *)(j + 56));
v7 = (_QWORD *)j;
}
putchar(10);
do
{
printf("%X ", *((unsigned int *)v7 + 14));
v11 = v7;
v7 = tree_find_prev(v7);
}
while ( v7 );
putchar(10);
do
{
printf("%X ", *((unsigned int *)v11 + 14));
v12 = v11;
v11 = (_QWORD *)v11[4];
}
while ( v11 );
putchar(10);
do
{
printf("%X ", *((unsigned int *)v12 + 14));
v12 = (_QWORD *)v12[5];
}
while ( v12 );
putchar(10);
func13((void *)v4, (void (__fastcall *)(_QWORD, __int64))cleanup, 0LL);
result = 0;
}
else
{
fwrite("An error occured, no memory!\n", 1uLL, 0x1DuLL, stderr);
result = -1;
}
return result;
}

original_code:
int main()
{
        struct tree_root* t = tree_new_avl();
        if (t == ((void *)0))
        {
                fprintf(stderr, "An error occured, no memory!\n");
                return -1;
        }
        int i = 0;
        for (; i <= 16; i++)
        {
                tree_new_node(i, ((void *)0), t);
                tree_dump(t);
        }
        tree_delete(8, t, &cleanup, ((void *)0));
        tree_delete(9, t, &cleanup, ((void *)0));
        tree_dump(t);
        for (; i <= 32; i++)
        {
                tree_new_node(i, ((void *)0), t);
                tree_dump(t);
        }
        struct tree* a = tree_find_node(4, t->tree);
        struct tree* b = tree_find_prev(a);
        struct tree* c = tree_find_next(a);
        printf("ptr: %X - %X - %X\n", b, a, c);
        printf("key: %X - %X - %X\n", b->key, a->key, c->key);
        i = pthread_mutex_trylock(&t->mutex);
        printf("%X\n", i);
        pthread_mutex_unlock(&t->mutex);
        a = tree_find(0, t);
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = tree_find_next(a);
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = tree_find_prev(a);
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = a->next;
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = a->prev;
        }
        printf("\n");
        tree_flush(t, cleanup, ((void *)0));
        t = ((void *)0);
        return 0;
}

canonical_original_code:
int main()
{
        struct tree_root *t = tree_new_avl();
        if (t == ((void *)0))
        {
                fprintf(stderr, "An error occured, no memory!\n");
                return -1;
        }
        int i = 0;
        for (; i <= 16; i++)
        {
                tree_new_node(i, ((void *)0), t);
                tree_dump(t);
        }
        tree_delete(8, t, &cleanup, ((void *)0));
        tree_delete(9, t, &cleanup, ((void *)0));
        tree_dump(t);
        for (; i <= 32; i++)
        {
                tree_new_node(i, ((void *)0), t);
                tree_dump(t);
        }
        struct tree *a = tree_find_node(4, t->tree);
        struct tree *b = tree_find_prev(a);
        struct tree *c = tree_find_next(a);
        printf("ptr: %X - %X - %X\n", b, a, c);
        printf("key: %X - %X - %X\n", b->key, a->key, c->key);
        i = pthread_mutex_trylock(&t->mutex);
        printf("%X\n", i);
        pthread_mutex_unlock(&t->mutex);
        a = tree_find(0, t);
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = tree_find_next(a);
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = tree_find_prev(a);
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = a->next;
        }
        printf("\n");
        a = b;
        while (a != ((void *)0))
        {
                printf("%X ", a->key);
                b = a;
                a = a->prev;
        }
        printf("\n");
        tree_flush(t, cleanup, ((void *)0));
        t = ((void *)0);
        return 0;
}

variable_types: i int


repo: bemk/xorll

original_code:
int main(int argc, char** argv)
{
        struct list* lst = list_init();
        struct list* lst2 = list_init();
        struct node* n1 = node_init(lst);
        struct node* n2 = node_init(lst);
        struct node* n3 = node_init(lst);
        memset(n1, 0, sizeof(*n1));
        memset(n2, 0, sizeof(*n2));
        memset(n3, 0, sizeof(*n3));
        n1->ptr = n2;
        n2->ptr = n1;
        n1->lst = lst;
        n2->lst = lst;
        n3->lst = lst;
        n1->data = 0xC0DEBABE;
        n2->data = 0xDEADBEEF;
        n3->data = 0xCAFEBEA8;
        lst->head = n1;
        lst->tail = n2;
        debug_list(lst);
        node_add_tail(lst, n3);
        debug_list(lst);
        list_copy_list(lst, lst2);
        debug_list(lst2);
        node_purge(n3, n2);
        debug_list(lst);
        list_clean(lst);
        return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
        struct list *lst = list_init();
        struct list *lst2 = list_init();
        struct node *n1 = node_init(lst);
        struct node *n2 = node_init(lst);
        struct node *n3 = node_init(lst);
        memset(n1, 0, sizeof(*n1));
        memset(n2, 0, sizeof(*n2));
        memset(n3, 0, sizeof(*n3));
        n1->ptr = n2;
        n2->ptr = n1;
        n1->lst = lst;
        n2->lst = lst;
        n3->lst = lst;
        n1->data = 0xC0DEBABE;
        n2->data = 0xDEADBEEF;
        n3->data = 0xCAFEBEA8;
        lst->head = n1;
        lst->tail = n2;
        debug_list(lst);
        node_add_tail(lst, n3);
        debug_list(lst);
        list_copy_list(lst, lst2);
        debug_list(lst2);
        node_purge(n3, n2);
        debug_list(lst);
        list_clean(lst);
        return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct list { struct node { void *ptr; int data; struct list *lst; } *head; struct node *tail; } *

variable_types: struct node { void *ptr; int data; struct list { struct node *head; struct node *tail; } *lst; } *


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

result = malloc(0x10uLL);
if ( !result )
{
fwrite("OUT OF MEMORY!\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*result = 0LL;
result[1] = 0LL;
return result;
}

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

result = malloc(0x10uLL);
if ( !result )
{
fwrite("OUT OF MEMORY!\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*result = 0LL;
result[1] = 0LL;
return result;
}

original_code:
struct list*
list_init()
{
        struct list* lst = malloc(sizeof(*lst));
        if (lst == ((void *)0))
        {
                fprintf(stderr, "OUT OF MEMORY!\n");
                exit(1);
        }
        memset(lst, 0, sizeof(*lst));
        return lst;
}

canonical_original_code:
struct list *list_init()
{
        struct list *lst = malloc(sizeof(*lst));
        if (lst == ((void *)0))
        {
                fprintf(stderr, "OUT OF MEMORY!\n");
                exit(1);
        }
        memset(lst, 0, sizeof(*lst));
        return lst;
}

return_type: struct list { struct node { void *ptr; int data; struct list *lst; } *head; struct node *tail; } *

user_defined_types: struct list { struct node *head; struct node *tail; }


decompiled_code:
__int64 __fastcall list_clean(void *ptr)
{
unsigned __int64 v2; // rdi

v2 = *(_QWORD *)ptr;
if ( v2 )
{
do
{
node_purge(v2, 0LL);
v2 = *(_QWORD *)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(void *ptr)
{
unsigned __int64 v2; // rdi

v2 = *(_QWORD *)ptr;
if ( v2 )
{
do
{
func8(v2, 0LL);
v2 = *(_QWORD *)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
return 0LL;
}

original_code:
int
list_clean(struct list* lst)
{
        while (lst->head != ((void *)0))
                node_purge(lst->head, ((void *)0));
        free(lst);
        return 0;
}

canonical_original_code:
int list_clean(struct list *lst)
{
        while (lst->head != ((void *)0))
                node_purge(lst->head, ((void *)0));
        free(lst);
        return 0;
}

user_defined_types: struct node { void *ptr; int data; struct list *lst; }


decompiled_code:
__int64 __fastcall node_add_tail(unsigned __int64 *a1, unsigned __int64 a2)
{
if ( a1[1] )
{
node_insert(a1[1], 0LL, a2);
}
else
{
if ( *a1 )
{
fwrite("List corrupted\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*a1 = a2;
a1[1] = a2;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int64 *a1, unsigned __int64 a2)
{
if ( a1[1] )
{
func10(a1[1], 0LL, a2);
}
else
{
if ( *a1 )
{
fwrite("List corrupted\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*a1 = a2;
a1[1] = a2;
}
return 0LL;
}

original_code:
int
node_add_tail(struct list* lst, struct node* this)
{
        if (lst->tail == ((void *)0))
        {
                if (lst->head != ((void *)0))
                {
                        fprintf(stderr, "List corrupted\n");
                        exit(1);
                }
                else
                {
                        lst->head = this;
                        lst->tail = this;
                        return 0;
                }
        }
        node_insert(lst->tail, ((void *)0), this);
        return 0;
}

canonical_original_code:
int node_add_tail(struct list *lst, struct node *this)
{
        if (lst->tail == ((void *)0))
        {
                if (lst->head != ((void *)0))
                {
                        fprintf(stderr, "List corrupted\n");
                        exit(1);
                }
                else
                {
                        lst->head = this;
                        lst->tail = this;
                        return 0;
                }
        }
        node_insert(lst->tail, ((void *)0), this);
        return 0;
}

variable_types:
this
struct node *


decompiled_code:
__int64 __fastcall node_add_head(unsigned __int64 *a1, unsigned __int64 a2)
{
if ( *a1 )
{
node_insert(0LL, *a1, a2);
}
else
{
if ( a1[1] )
{
fwrite("List corrupted\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*a1 = a2;
a1[1] = a2;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned __int64 *a1, unsigned __int64 a2)
{
if ( *a1 )
{
func10(0LL, *a1, a2);
}
else
{
if ( a1[1] )
{
fwrite("List corrupted\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*a1 = a2;
a1[1] = a2;
}
return 0LL;
}

original_code:
int node_add_head(struct list* lst, struct node* this)
{
        if (lst->head == ((void *)0))
        {
                if (lst->tail != ((void *)0))
                {
                        fprintf(stderr, "List corrupted\n");
                        exit(1);
                }
                else
                {
                        lst->head = this;
                        lst->tail = this;
                        return 0;
                }
        }
        node_insert(((void *)0), lst->head, this);
        return 0;
}

canonical_original_code:
int node_add_head(struct list *lst, struct node *this)
{
        if (lst->head == ((void *)0))
        {
                if (lst->tail != ((void *)0))
                {
                        fprintf(stderr, "List corrupted\n");
                        exit(1);
                }
                else
                {
                        lst->head = this;
                        lst->tail = this;
                        return 0;
                }
        }
        node_insert(((void *)0), lst->head, this);
        return 0;
}


decompiled_code:
__int64 __fastcall node_get_next(__int64 a1, _QWORD *a2)
{
return *a2 ^ a1;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, _QWORD *a2)
{
return *a2 ^ a1;
}

original_code:
struct node*
node_get_next(struct node* prev, struct node* this)
{
        ulong l_prev = (ulong)prev;
        ulong l_this = (ulong)this->ptr;
        ulong l_next = l_prev ^ l_this;
        return (struct node*)l_next;
}

canonical_original_code:
struct node *node_get_next(struct node *prev, struct node *this)
{
        unsigned long l_prev = (ulong)prev;
        unsigned long l_this = (ulong)this->ptr;
        unsigned long l_next = l_prev ^ l_this;
        return (struct node *)l_next;
}

variable_types:
prev
struct node *

return_type: struct node { void *ptr; int data; struct list { struct node *head; struct node *tail; } *lst; } *


decompiled_code:
int __fastcall debug_list(__int64 *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
__int64 v3; // r12
__int64 v4; // rax

v1 = *a1;
if ( *a1 )
{
v2 = 0;
v3 = 0LL;
while ( 1 )
{
fprintf(stdout, "%d) Current data %X\n", v2, *(unsigned int *)(v1 + 8));
v4 = node_get_next(v3, (_QWORD *)v1);
++v2;
v3 = v1;
if ( !v4 )
break;
v1 = v4;
}
}
return fputc(10, stdout);
}

canonical_decompiled_code:
int __fastcall func14(__int64 *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
__int64 v3; // r12
__int64 v4; // rax

v1 = *a1;
if ( *a1 )
{
v2 = 0;
v3 = 0LL;
while ( 1 )
{
fprintf(stdout, "%d) Current data %X\n", v2, *(unsigned int *)(v1 + 8));
v4 = func13(v3, (_QWORD *)v1);
++v2;
v3 = v1;
if ( !v4 )
break;
v1 = v4;
}
}
return fputc(10, stdout);
}

original_code:
void debug_list(struct list* lst)
{
        struct node* carriage = lst->head;
        struct node* last = ((void *)0);
        struct node* new;
        int i = 0;
        while (carriage != ((void *)0))
        {
                fprintf(stdout, "%d) Current data %X\n", i, carriage->data);
                new = node_get_next(last, carriage);
                last = carriage;
                carriage = new;
                i++;
        }
        fprintf(stdout, "\n");
}

canonical_original_code:
void debug_list(struct list *lst)
{
        struct node *carriage = lst->head;
        struct node *last = ((void *)0);
        struct node *new;
        int i = 0;
        while (carriage != ((void *)0))
        {
                fprintf(stdout, "%d) Current data %X\n", i, carriage->data);
                new = node_get_next(last, carriage);
                last = carriage;
                carriage = new;
                i++;
        }
        fprintf(stdout, "\n");
}

return_type: void


decompiled_code:
__int64 __fastcall read_n(int a1, unsigned __int64 *a2, __int64 a3, unsigned __int64 a4)
{
unsigned __int64 v5; // rsi
__int64 result; // rax
ssize_t v8; // rax
ssize_t v9; // rax
int v10; // eax

v5 = *a2;
result = 0LL;
if ( v5 <= a4 )
{
v8 = read(a1, (void *)(a3 + v5), a4 - v5);
if ( v8 == -1 )
{
v10 = *_errno_location();
if ( v10 == 4 || v10 == 11 )
result = 3LL;
else
result = 2LL;
}
else if ( v8 )
{
v9 = *a2 + v8;
*a2 = v9;
result = 3 * (unsigned int)(v9 != a4);
}
else
{
*_errno_location() = 0;
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned __int64 *a2, __int64 a3, unsigned __int64 a4)
{
unsigned __int64 v5; // rsi
__int64 result; // rax
ssize_t v8; // rax
ssize_t v9; // rax
int v10; // eax

v5 = *a2;
result = 0LL;
if ( v5 <= a4 )
{
v8 = read(a1, (void *)(a3 + v5), a4 - v5);
if ( v8 == -1 )
{
v10 = *_errno_location();
if ( v10 == 4 || v10 == 11 )
result = 3LL;
else
result = 2LL;
}
else if ( v8 )
{
v9 = *a2 + v8;
*a2 = v9;
result = 3 * (unsigned int)(v9 != a4);
}
else
{
*_errno_location() = 0;
result = 1LL;
}
}
return result;
}

original_code:
static enum read_status read_n(int fd, size_t* filled, void* buf, size_t size) {
  ssize_t ret;
  if (*filled > size)
    return READ_DONE;
  ret = read(fd, (char*) buf + *filled, size - *filled);
  if (ret == -1) {
    if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11 || (*__errno_location ()) == 11)
      return READ_AGAIN;
    return READ_ERROR;
  } else if (ret == 0) {
    (*__errno_location ()) = 0;
    return READ_EOF;
  }
  *filled += (size_t) ret;
  if (*filled == size)
    return READ_DONE;
  return READ_AGAIN;
}

canonical_original_code:
static enum read_status read_n(int fd, unsigned long *filled, void *buf, unsigned long size) {
  long ret;
  if (*filled > size)
    return READ_DONE;
  ret = read(fd, (char *) buf + *filled, size - *filled);
  if (ret == -1) {
    if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11 || (*__errno_location ()) == 11)
      return READ_AGAIN;
    return READ_ERROR;
  } else if (ret == 0) {
    (*__errno_location ()) = 0;
    return READ_EOF;
  }
  *filled += (unsigned long) ret;
  if (*filled == size)
    return READ_DONE;
  return READ_AGAIN;
}

variable_types: fd int

variable_types:
filled
unsigned long *

variable_types:
buf
void *

variable_types:
size
unsigned long

variable_types: ret long

return_type: enum read_status { READ_DONE=0, READ_EOF=1, READ_ERROR=2, READ_AGAIN=3 }


decompiled_code:
int __fastcall inspect_msg(__int64 a1)
{
__int64 v1; // rsi
int result; // eax
const char *v3; // rsi

fwrite("Mumble: ", 1uLL, 8uLL, stdout);
v1 = *(unsigned int *)(a1 + 8);
switch ( (int)v1 )
{
case 0:
result = printf("Init(%u, %u)\n", *(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 16));
break;
case 1:
result = printf("Shmem(%s)\n", (const char *)(a1 + 12));
break;
case 2:
result = printf(
"Blit(%u, %u, %u, %u)\n",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 16),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 24));
break;
case 3:
result = printf(
"Active(%u, %u, %u, %u)\n",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 16),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 24));
break;
case 4:
result = printf("Pid(%u)\n", *(unsigned int *)(a1 + 12));
break;
case 5:
result = printf("Fps(%f)\n", *(float *)(a1 + 12));
break;
case 6:
v3 = "true";
if ( !*(_BYTE *)(a1 + 12) )
v3 = "false";
result = printf("Interactive(%s)\n", v3);
break;
default:
result = printf("??? %u %d\n", v1, *(unsigned int *)(a1 + 4));
break;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
__int64 v1; // rsi
int result; // eax
const char *v3; // rsi

fwrite("Mumble: ", 1uLL, 8uLL, stdout);
v1 = *(unsigned int *)(a1 + 8);
switch ( (int)v1 )
{
case 0:
result = printf("Init(%u, %u)\n", *(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 16));
break;
case 1:
result = printf("Shmem(%s)\n", (const char *)(a1 + 12));
break;
case 2:
result = printf(
"Blit(%u, %u, %u, %u)\n",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 16),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 24));
break;
case 3:
result = printf(
"Active(%u, %u, %u, %u)\n",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 16),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 24));
break;
case 4:
result = printf("Pid(%u)\n", *(unsigned int *)(a1 + 12));
break;
case 5:
result = printf("Fps(%f)\n", *(float *)(a1 + 12));
break;
case 6:
v3 = "true";
if ( !*(_BYTE *)(a1 + 12) )
v3 = "false";
result = printf("Interactive(%s)\n", v3);
break;
default:
result = printf("??? %u %d\n", v1, *(unsigned int *)(a1 + 4));
break;
}
return result;
}

original_code:
static void inspect_msg(struct OverlayMsg* msg) {
  fputs("Mumble: ", stdout);
  switch (msg->omh.uiType) {
    case 0:
      printf("Init(%u, %u)\n", msg->body.omi.uiWidth, msg->body.omi.uiHeight);
      break;
    case 1:
      printf("Shmem(%s)\n", msg->body.oms.a_cName);
      break;
    case 2:
      printf("Blit(%u, %u, %u, %u)\n", msg->body.omb.x, msg->body.omb.y, msg->body.omb.w, msg->body.omb.h);
      break;
    case 3:
      printf("Active(%u, %u, %u, %u)\n", msg->body.oma.x, msg->body.oma.y, msg->body.oma.w, msg->body.oma.h);
      break;
    case 4:
      printf("Pid(%u)\n", msg->body.omp.pid);
      break;
    case 5:
      printf("Fps(%f)\n", (double) msg->body.omf.fps);
      break;
    case 6:
      printf("Interactive(%s)\n", msg->body.omin.state ? "true" : "false");
      break;
    default:
      printf("??? %u %d\n", msg->omh.uiType, msg->omh.iLength);
      break;
  }
}

canonical_original_code:
static void inspect_msg(struct OverlayMsg *msg) {
  fputs("Mumble: ", stdout);
  switch (msg->omh.uiType) {
    case 0:
      printf("Init(%u, %u)\n", msg->body.omi.uiWidth, msg->body.omi.uiHeight);
      break;
    case 1:
      printf("Shmem(%s)\n", msg->body.oms.a_cName);
      break;
    case 2:
      printf("Blit(%u, %u, %u, %u)\n", msg->body.omb.x, msg->body.omb.y, msg->body.omb.w, msg->body.omb.h);
      break;
    case 3:
      printf("Active(%u, %u, %u, %u)\n", msg->body.oma.x, msg->body.oma.y, msg->body.oma.w, msg->body.oma.h);
      break;
    case 4:
      printf("Pid(%u)\n", msg->body.omp.pid);
      break;
    case 5:
      printf("Fps(%f)\n", (double) msg->body.omf.fps);
      break;
    case 6:
      printf("Interactive(%s)\n", msg->body.omin.state ? "true" : "false");
      break;
    default:
      printf("??? %u %d\n", msg->omh.uiType, msg->omh.iLength);
      break;
  }
}

variable_types: struct OverlayMsg { struct OverlayMsgHeader { unsigned int uiMagic; int iLength; unsigned int uiType; } omh; union { struct OverlayMsgShmem { char a_cName[2048]; } oms; struct OverlayMsgInit { unsigned int uiWidth; unsigned int uiHeight; } omi; struct OverlayMsgBlit { unsigned int x; } omb; struct OverlayMsgActive { unsigned int x; } oma; struct OverlayMsgPid { unsigned int pid; } omp; struct OverlayMsgFps { float fps; } omf; struct OverlayMsgInteractive { char state; } omin; } body; } *

user_defined_types: struct OverlayMsg { struct OverlayMsgHeader omh; union { struct OverlayMsgShmem oms; struct OverlayMsgInit omi; struct OverlayMsgBlit omb; struct OverlayMsgActive oma; struct OverlayMsgPid omp; struct OverlayMsgFps omf; struct OverlayMsgInteractive omin; } body; }


decompiled_code:
void *__fastcall open_mumble_shm(size_t len, char *name)
{
int v2; // eax
int v3; // ebx
void *v4; // rbp
int *v5; // r12
int v6; // er13

v2 = shm_open(name, 0, 0x180u);
if ( v2 == -1 )
{
perror("shm_open");
v4 = 0LL;
}
else
{
v3 = v2;
v4 = mmap(0LL, len, 1, 1, v2, 0LL);
v5 = _errno_location();
v6 = *v5;
close(v3);
*v5 = v6;
if ( !v4 )
perror("mmap");
}
return v4;
}

canonical_decompiled_code:
void *__fastcall func2(size_t len, char *name)
{
int v2; // eax
int v3; // ebx
void *v4; // rbp
int *v5; // r12
int v6; // er13

v2 = shm_open(name, 0, 0x180u);
if ( v2 == -1 )
{
perror("shm_open");
v4 = 0LL;
}
else
{
v3 = v2;
v4 = mmap(0LL, len, 1, 1, v2, 0LL);
v5 = _errno_location();
v6 = *v5;
close(v3);
*v5 = v6;
if ( !v4 )
perror("mmap");
}
return v4;
}

original_code:
static void* open_mumble_shm(size_t mmap_size, const char* name) {
  int fd;
  void* ptr;
  int my_errno;
  fd = shm_open(name, 00, 0600);
  if (fd == -1) {
    perror("shm_open");
    return ((void *)0);
  }
  ptr = mmap(((void *)0), mmap_size, 0x1, 0x01, fd, 0);
  my_errno = (*__errno_location ());
  close(fd);
  (*__errno_location ()) = my_errno;
  if (ptr == 0) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}

canonical_original_code:
static void *open_mumble_shm(unsigned long mmap_size, char *name) {
  int fd;
  void *ptr;
  int my_errno;
  fd = shm_open(name, 00, 0600);
  if (fd == -1) {
    perror("shm_open");
    return ((void *)0);
  }
  ptr = mmap(((void *)0), mmap_size, 0x1, 0x01, fd, 0);
  my_errno = (*__errno_location ());
  close(fd);
  (*__errno_location ()) = my_errno;
  if (ptr == 0) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}

variable_types:
name
char *


decompiled_code:
__int64 __fastcall get_mumble_pipe_path(char *s, const char *a2)
{
size_t v2; // rax
__int64 result; // rax

strcpy(s, a2);
v2 = strlen(s);
if ( v2 + 19 > 0x6C )
{
*_errno_location() = 36;
result = 0xFFFFFFFFLL;
}
else
{
*(__m128i *)&s[v2] = _mm_load_si128((const __m128i *)&FILENAME_BIT_0);
strcpy(&s[v2 + 16], "pe");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s, const char *a2)
{
size_t v2; // rax
__int64 result; // rax

strcpy(s, a2);
v2 = strlen(s);
if ( v2 + 19 > 0x6C )
{
*_errno_location() = 36;
result = 0xFFFFFFFFLL;
}
else
{
*(__m128i *)&s[v2] = _mm_load_si128((const __m128i *)&FILENAME_BIT_0);
strcpy(&s[v2 + 16], "pe");
result = 0LL;
}
return result;
}

original_code:
static int get_mumble_pipe_path(char* buf, const char* home) {
  static const char FILENAME_BIT[] = "/" "MumbleOverlayPipe";
  size_t len;
  strcpy(buf, home);
  len = strlen(buf);
  if (len + sizeof(FILENAME_BIT) > (sizeof(((struct sockaddr_un*) 0)->sun_path))) {
    (*__errno_location ()) = 36;
    return -1;
  }
  strcpy(buf + len, FILENAME_BIT);
  return 0;
}

canonical_original_code:
static int get_mumble_pipe_path(char *buf, char *home) {
  static char FILENAME_BIT[] = "/" "MumbleOverlayPipe";
  unsigned long len;
  strcpy(buf, home);
  len = strlen(buf);
  if (len + sizeof(FILENAME_BIT) > (sizeof(((struct sockaddr_un *) 0)->sun_path))) {
    (*__errno_location ()) = 36;
    return -1;
  }
  strcpy(buf + len, FILENAME_BIT);
  return 0;
}

variable_types:
buf
char *

variable_types:
home
char *

variable_types:
len
unsigned long

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall open_unix_socket(const char *a1)
{
unsigned int v1; // ebx
int v2; // eax
struct sockaddr v4[8]; // [rsp+0h] [rbp-88h] BYREF

v4[0].sa_family = 1;
v1 = get_mumble_pipe_path(v4[0].sa_data, a1);
if ( v1 != -1 )
{
v2 = socket(1, 1, 0);
v1 = v2;
if ( v2 != -1 && connect(v2, v4, 0x6Eu) == -1 )
{
close(v1);
v1 = -1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
unsigned int v1; // ebx
int v2; // eax
struct sockaddr v4[8]; // [rsp+0h] [rbp-88h] BYREF

v4[0].sa_family = 1;
v1 = func4(v4[0].sa_data, a1);
if ( v1 != -1 )
{
v2 = socket(1, 1, 0);
v1 = v2;
if ( v2 != -1 && connect(v2, v4, 0x6Eu) == -1 )
{
close(v1);
v1 = -1;
}
}
return v1;
}

original_code:
static int open_unix_socket(const char* path) {
  int sock;
  struct sockaddr_un addr;
  addr.sun_family = 1;
  if (get_mumble_pipe_path(addr.sun_path, path) == -1)
    return -1;
  sock = socket(1, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  if (connect(sock, (struct sockaddr*) &addr, sizeof addr) == -1) {
    close(sock);
    return -1;
  }
  return sock;
}

canonical_original_code:
static int open_unix_socket(char *path) {
  int sock;
  struct sockaddr_un addr;
  addr.sun_family = 1;
  if (get_mumble_pipe_path(addr.sun_path, path) == -1)
    return -1;
  sock = socket(1, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  if (connect(sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
    close(sock);
    return -1;
  }
  return sock;
}

variable_types:
path
char *

variable_types: sock int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall send_mumble_init_msg(int fd, unsigned __int16 a2, unsigned __int16 a3)
{
unsigned __int64 i; // rbp
int v4; // eax
ssize_t v5; // rax
ssize_t v6; // rbx
int v8[526]; // [rsp+0h] [rbp-838h] BYREF

v8[0] = 5;
v8[2] = 0;
v8[1] = 8;
v8[3] = a2;
v8[4] = a3;
for ( i = 0LL; i <= 0x13; i += v6 )
{
v5 = write(fd, v8, 20 - i);
v6 = v5;
if ( v5 == -1 )
{
v4 = *_errno_location();
if ( v4 != 11 && v4 != 4 )
{
LABEL_7:
close(fd);
return 0xFFFFFFFFLL;
}
}
else if ( !v5 )
{
goto LABEL_7;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, unsigned __int16 a2, unsigned __int16 a3)
{
unsigned __int64 i; // rbp
int v4; // eax
ssize_t v5; // rax
ssize_t v6; // rbx
int v8[526]; // [rsp+0h] [rbp-838h] BYREF

v8[0] = 5;
v8[2] = 0;
v8[1] = 8;
v8[3] = a2;
v8[4] = a3;
for ( i = 0LL; i <= 0x13; i += v6 )
{
v5 = write(fd, v8, 20 - i);
v6 = v5;
if ( v5 == -1 )
{
v4 = *_errno_location();
if ( v4 != 11 && v4 != 4 )
{
LABEL_7:
close(fd);
return 0xFFFFFFFFLL;
}
}
else if ( !v5 )
{
goto LABEL_7;
}
}
return 0LL;
}

original_code:
static int send_mumble_init_msg(int sock, uint16_t width, uint16_t height) {
  size_t msg_written, msgsize;
  struct OverlayMsg msg;
  ssize_t ret;
  msg.omh.uiMagic = 0x00000005;
  msg.omh.uiType = 0;
  msg.omh.iLength = sizeof(struct OverlayMsgInit);
  msg.body.omi.uiWidth = width;
  msg.body.omi.uiHeight = height;
  msgsize = sizeof(struct OverlayMsgHeader) + sizeof(struct OverlayMsgInit);
  msg_written = 0;
  while (msg_written < msgsize) {
    ret = write(sock, &msg, msgsize - msg_written);
    if ((ret == -1 && ((*__errno_location ()) != 11 && (*__errno_location ()) != 4)) || ret == 0) {
      close(sock);
      return -1;
    }
    msg_written += (size_t) ret;
  }
  return 0;
}

canonical_original_code:
static int send_mumble_init_msg(int sock, unsigned short width, unsigned short height) {
  unsigned long msg_written; unsigned long msgsize;
  struct OverlayMsg msg;
  long ret;
  msg.omh.uiMagic = 0x00000005;
  msg.omh.uiType = 0;
  msg.omh.iLength = sizeof(struct OverlayMsgInit);
  msg.body.omi.uiWidth = width;
  msg.body.omi.uiHeight = height;
  msgsize = sizeof(struct OverlayMsgHeader) + sizeof(struct OverlayMsgInit);
  msg_written = 0;
  while (msg_written < msgsize) {
    ret = write(sock, &msg, msgsize - msg_written);
    if ((ret == -1 && ((*__errno_location ()) != 11 && (*__errno_location ()) != 4)) || ret == 0) {
      close(sock);
      return -1;
    }
    msg_written += (unsigned long) ret;
  }
  return 0;
}

variable_types:
width
unsigned short

variable_types:
height
unsigned short

variable_types:
msgsize
unsigned long

variable_types: struct OverlayMsg { struct OverlayMsgHeader { unsigned int uiMagic; int iLength; unsigned int uiType; } omh; union { struct OverlayMsgShmem { char a_cName[2048]; } oms; struct OverlayMsgInit { unsigned int uiWidth; unsigned int uiHeight; } omi; struct OverlayMsgBlit { unsigned int x; } omb; struct OverlayMsgActive { unsigned int x; } oma; struct OverlayMsgPid { unsigned int pid; } omp; struct OverlayMsgFps { float fps; } omf; struct OverlayMsgInteractive { char state; } omin; } body; }


decompiled_code:
__int64 __fastcall inotify_init_watch_creates(char *name)
{
int v1; // eax
unsigned int v2; // ebx

v1 = inotify_init();
v2 = v1;
if ( v1 == -1 )
{
perror("inotify_init");
}
else if ( inotify_add_watch(v1, name, 0x100u) == -1 )
{
perror("inotify_add_watch");
close(v2);
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *name)
{
int v1; // eax
unsigned int v2; // ebx

v1 = inotify_init();
v2 = v1;
if ( v1 == -1 )
{
perror("inotify_init");
}
else if ( inotify_add_watch(v1, name, 0x100u) == -1 )
{
perror("inotify_add_watch");
close(v2);
v2 = -1;
}
return v2;
}

original_code:
static int inotify_init_watch_creates(const char* dir) {
  int inotify;
  if ((inotify = inotify_init()) == -1) {
    perror("inotify_init");
    return -1;
  }
  if (inotify_add_watch(inotify, dir, 0x00000100) == -1) {
    perror("inotify_add_watch");
    close(inotify);
    return -1;
  }
  return inotify;
}

canonical_original_code:
static int inotify_init_watch_creates(char *dir) {
  int inotify;
  if ((inotify = inotify_init()) == -1) {
    perror("inotify_init");
    return -1;
  }
  if (inotify_add_watch(inotify, dir, 0x00000100) == -1) {
    perror("inotify_add_watch");
    close(inotify);
    return -1;
  }
  return inotify;
}

variable_types:
dir
char *

variable_types: inotify int


repo: benf/car

decompiled_code:
int __fastcall tx_cmd(unsigned __int8 a1, unsigned __int8 a2)
{
putc(170, stdout);
usleep(1u);
putc(a1, stdout);
usleep(1u);
putc(a2, stdout);
usleep(1u);
fflush(stdout);
fprintf(stderr, "%c: %x\n", a1, a2);
return usleep(5u);
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int8 a1, unsigned __int8 a2)
{
putc(170, stdout);
usleep(1u);
putc(a1, stdout);
usleep(1u);
putc(a2, stdout);
usleep(1u);
fflush(stdout);
fprintf(stderr, "%c: %x\n", a1, a2);
return usleep(5u);
}

original_code:
void tx_cmd(uint8_t cmd, uint8_t param) {
 putc(0xAA, stdout);
 usleep(1);
 putc(cmd, stdout);
 usleep(1);
 putc(param, stdout);
 usleep(1);
 fflush(stdout);
 fprintf(stderr, "%c: %x\n", cmd, param);
 usleep(5);
}

canonical_original_code:
void tx_cmd(unsigned char cmd, unsigned char param) {
 putc(0xAA, stdout);
 usleep(1);
 putc(cmd, stdout);
 usleep(1);
 putc(param, stdout);
 usleep(1);
 fflush(stdout);
 fprintf(stderr, "%c: %x\n", cmd, param);
 usleep(5);
}

variable_types:
cmd
unsigned char

variable_types:
param
unsigned char


getTimeNow

repo: bengioe/ekanz

decompiled_code:
double getTimeNow()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(1, &v1);
return (double)SLODWORD(v1.tv_nsec) * 0.000000001 + (double)SLODWORD(v1.tv_sec);
}

canonical_decompiled_code:
double func0()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(1, &v1);
return (double)SLODWORD(v1.tv_nsec) * 0.000000001 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double getTimeNow(){
    struct timespec t;
    clock_gettime(1, &t);;
    return t.tv_sec + (double) t.tv_nsec * 1e-9;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; }

return_type: double


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rbx
double v5; // r14
__int64 v6; // rbx
double v7; // xmm0_8

ek_init_type(argc, argv, envp);
v3 = ek_raw_readfile(argv[1]);
v4 = ek_parse_text(v3, "test.py");
ek_parse_print_ast(v4);
putchar(10);
v5 = getTimeNow();
v6 = ek_bc_compile_ast(v4);
ek_bc_print(v6);
ek_vm_run(v6);
v7 = getTimeNow();
printf("%f seconds\n", v7 - v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rbx
double v5; // r14
__int64 v6; // rbx
double v7; // xmm0_8

ek_init_type(argc, argv, envp);
v3 = ek_raw_readfile(argv[1]);
v4 = ek_parse_text(v3, "test.py");
ek_parse_print_ast(v4);
putchar(10);
v5 = func0();
v6 = ek_bc_compile_ast(v4);
ek_bc_print(v6);
ek_vm_run(v6);
v7 = func0();
printf("%f seconds\n", v7 - v5);
return 0;
}

original_code:
int main(int argc, char** argv){
  ek_init_type();
  if (0){
    ek_type* strType = ek_type_derive(ek_NoneType, "__getitem__", 11);
    strType = ek_type_derive(strType, "__len__", 7);
    return 0;
  }
  char* file_data = ek_raw_readfile(argv[1]);
  ek_ast_node* ast = ek_parse_text(file_data, "test.py");
  ek_parse_print_ast(ast);
  printf("\n");
  double t0 = getTimeNow();
  ek_bytecode* bc = ek_bc_compile_ast(ast);
  ek_bc_print(bc);
  ek_vm_run(bc);
  double t1 = getTimeNow();
  printf("%f seconds\n",t1-t0);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv){
  ek_init_type();
  if (0){
    struct ek_type *strType = ek_type_derive(ek_NoneType, "__getitem__", 11);
    strType = ek_type_derive(strType, "__len__", 7);
    return 0;
  }
  char *file_data = ek_raw_readfile(argv[1]);
  struct ek_ast_node *ast = ek_parse_text(file_data, "test.py");
  ek_parse_print_ast(ast);
  printf("\n");
  double t0 = getTimeNow();
  struct ek_bytecode *bc = ek_bc_compile_ast(ast);
  ek_bc_print(bc);
  ek_vm_run(bc);
  double t1 = getTimeNow();
  printf("%f seconds\n",t1-t0);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct ek_ast_node { char *tokstr; int toklen; int type; struct ek_ast_node *left; struct ek_ast_node *right; unsigned short local_scope_size; unsigned char isCompiledUnit; union x { struct var { unsigned int scoperef; int scopepos; struct ek_type { char *map; unsigned long map_size; unsigned long nentries; unsigned int extra_space; struct ek_type_entry { char *str; unsigned int strlen; unsigned int index; unsigned long xhash; unsigned long yhash; struct ek_type_entry *next; } *entry; } *predicted_type; } var; struct jit { unsigned char isLeafUnit; char *block; } jit; } x; } *

variable_types: struct ek_bytecode { char *data; int size; char *startptr; struct ek_bytecode *next; struct ek_basicblock { char *bcstart; char *bcend; struct ek_bblockversion { int nassumptions; int *types; void *code; } *bbv; } *startbb; struct ek_basicblock *bb; } *

user_defined_types: struct ek_type { char *map; unsigned long map_size; unsigned long nentries; unsigned int extra_space; struct ek_type_entry *entry; }

user_defined_types: struct ek_ast_node { char *tokstr; int toklen; int type; struct ek_ast_node *left; struct ek_ast_node *right; unsigned short local_scope_size; unsigned char isCompiledUnit; union x x; }

user_defined_types: struct ek_bytecode { char *data; int size; char *startptr; struct ek_bytecode *next; struct ek_basicblock *startbb; struct ek_basicblock *bb; }

user_defined_types: union x { struct var var; struct jit jit; }

user_defined_types: struct var { unsigned int scoperef; int scopepos; struct ek_type *predicted_type; }

user_defined_types: struct ek_basicblock { char *bcstart; char *bcend; struct ek_bblockversion *bbv; }


xhash

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

result = 0xCBF29CE484222325LL;
if ( a2 > 0 )
{
v3 = &a1[a2];
do
result = 0x100000001B3LL * (*a1++ ^ (unsigned __int64)result);
while ( v3 != a1 );
}
return result;
}

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

result = 0xCBF29CE484222325LL;
if ( a2 > 0 )
{
v3 = &a1[a2];
do
result = 0x100000001B3LL * (*a1++ ^ (unsigned __int64)result);
while ( v3 != a1 );
}
return result;
}

variable_types:
s
char *

variable_types: l long

variable_types:
h
unsigned long

return_type: unsigned long


yhash

decompiled_code:
__int64 __fastcall yhash(char *a1, __int64 a2)
{
char *v2; // rsi
__int64 result; // rax

if ( a2 <= 0 )
return 961748927LL;
v2 = &a1[a2];
result = 961748927LL;
do
result = 160481183 * (*a1++ + 32 * result);
while ( v2 != a1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, __int64 a2)
{
char *v2; // rsi
__int64 result; // rax

if ( a2 <= 0 )
return 961748927LL;
v2 = &a1[a2];
result = 961748927LL;
do
result = 160481183 * (*a1++ + 32 * result);
while ( v2 != a1 );
return result;
}

original_code:
unsigned long yhash(char* s, long l){
  unsigned long h = 961748927;
  while (l-->0){h = ((h<<5) + *s++) * 160481183;}
  return h;
}

canonical_original_code:
unsigned long yhash(char *s, long l){
  unsigned long h = 961748927;
  while (l-->0){h = ((h<<5) + *s++) * 160481183;}
  return h;
}


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

result = malloc(0x28uLL);
result[1] = 0LL;
*result = 0LL;
result[2] = 0LL;
result[4] = 0LL;
return result;
}

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

result = malloc(0x28uLL);
result[1] = 0LL;
*result = 0LL;
result[2] = 0LL;
result[4] = 0LL;
return result;
}

original_code:
ek_type* ek_type_new(){
  ek_type* t = malloc(sizeof(ek_type));
  t->map_size = 0;
  t->map = 0;
  t->nentries = 0;
  t->entry = ((void *)0);
  return t;
}

canonical_original_code:
struct ek_type *ek_type_new(){
  struct ek_type *t = malloc(sizeof(ek_type));
  t->map_size = 0;
  t->map = 0;
  t->nentries = 0;
  t->entry = ((void *)0);
  return t;
}

variable_types: struct ek_type { char *map; unsigned long map_size; unsigned long nentries; unsigned int extra_space; struct ek_type_entry { char *str; unsigned int strlen; unsigned int index; unsigned long xhash; unsigned long yhash; struct ek_type_entry *next; } *entry; } *


decompiled_code:
void ek_init_type()
{
if ( !ek_NoneType )
{
ek_NoneType = (__int64)ek_type_new();
ek_NoneType = (__int64)ek_type_derive((_QWORD *)ek_NoneType, "__str__", 7LL);
ek_NoneType = (__int64)ek_type_derive((_QWORD *)ek_NoneType, "__eq__", 6LL);
ek_None = (__int64)ek_type_instanciate(ek_NoneType);
ek_IntType = (__int64)ek_type_new();
ek_FuncType = (__int64)ek_type_new();
ek_CFuncType = (__int64)ek_type_new();
*(_DWORD *)(ek_CFuncType + 24) = 8;
ek_StrType = (__int64)ek_type_new();
ek_StrType = (__int64)ek_type_derive((_QWORD *)ek_StrType, "__str__", 7LL);
*(_DWORD *)(ek_StrType + 24) = 16;
}
}

canonical_decompiled_code:
void func3()
{
if ( !ek_NoneType )
{
ek_NoneType = (__int64)func2();
ek_NoneType = (__int64)ek_type_derive((_QWORD *)ek_NoneType, "__str__", 7LL);
ek_NoneType = (__int64)ek_type_derive((_QWORD *)ek_NoneType, "__eq__", 6LL);
ek_None = (__int64)ek_type_instanciate(ek_NoneType);
ek_IntType = (__int64)func2();
ek_FuncType = (__int64)func2();
ek_CFuncType = (__int64)func2();
*(_DWORD *)(ek_CFuncType + 24) = 8;
ek_StrType = (__int64)func2();
ek_StrType = (__int64)ek_type_derive((_QWORD *)ek_StrType, "__str__", 7LL);
*(_DWORD *)(ek_StrType + 24) = 16;
}
}

original_code:
void ek_init_type(){
  if (ek_NoneType == ((void *)0)){
    ek_NoneType = ek_type_new();
    ek_NoneType = ek_type_derive(ek_NoneType, "__str__", 7);
    ek_NoneType = ek_type_derive(ek_NoneType, "__eq__", 6);
    ek_None = ek_type_instanciate(ek_NoneType);
    ek_IntType = ek_type_new();
    ek_FuncType = ek_type_new();
    ek_CFuncType = ek_type_new();
    ek_CFuncType->extra_space = sizeof(void*);
    ek_StrType = ek_type_new();
    ek_StrType = ek_type_derive(ek_StrType, "__str__", 7);
    ek_StrType->extra_space = sizeof(char*) + sizeof(unsigned long);
  }
}

canonical_original_code:
void ek_init_type(){
  if (ek_NoneType == ((void *)0)){
    ek_NoneType = ek_type_new();
    ek_NoneType = ek_type_derive(ek_NoneType, "__str__", 7);
    ek_NoneType = ek_type_derive(ek_NoneType, "__eq__", 6);
    ek_None = ek_type_instanciate(ek_NoneType);
    ek_IntType = ek_type_new();
    ek_FuncType = ek_type_new();
    ek_CFuncType = ek_type_new();
    ek_CFuncType->extra_space = sizeof(void *);
    ek_StrType = ek_type_new();
    ek_StrType = ek_type_derive(ek_StrType, "__str__", 7);
    ek_StrType->extra_space = sizeof(char *) + sizeof(unsigned long);
  }
}


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

result = *a1;
a1[*(_QWORD *)(*a1 + 16) + 1 + a2] = a3;
return result;
}

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

result = *a1;
a1[*(_QWORD *)(*a1 + 16) + 1 + a2] = a3;
return result;
}

original_code:
void ek_obj_setextra(ekop o, uint64_t pos, ekop v){
  *(((ekop*)o->data) + o->type->nentries + pos) = v;
}

canonical_original_code:
void ek_obj_setextra(struct ek_obj *o, unsigned long pos, struct ek_obj *v){
  *(((struct ek_obj **)o->data) + o->type->nentries + pos) = v;
}

variable_types: struct ek_obj { struct ek_type { char *map; unsigned long map_size; unsigned long nentries; unsigned int extra_space; struct ek_type_entry { char *str; unsigned int strlen; unsigned int index; unsigned long xhash; unsigned long yhash; struct ek_type_entry *next; } *entry; } *type; char data[]; } *

variable_types:
pos
unsigned long

variable_types:
v
struct ek_obj *

user_defined_types: struct ek_obj { struct ek_type *type; char data[]; }


decompiled_code:
__int64 __fastcall ek_obj_getextra(__int64 a1, __int64 a2)
{
return *(_QWORD *)(a1 + 8 * (*(_QWORD *)(*(_QWORD *)a1 + 16LL) + a2) + 8);
}

original_code:
ekop ek_obj_getextra(ekop o, uint64_t pos){
  return *(((ekop*)o->data) + o->type->nentries + pos);
}

canonical_original_code:
struct ek_obj *ek_obj_getextra(struct ek_obj *o, unsigned long pos){
  return *(((struct ek_obj **)o->data) + o->type->nentries + pos);
}

variable_types:
o
struct ek_obj *

return_type: struct ek_obj { struct ek_type { char *map; unsigned long map_size; unsigned long nentries; unsigned int extra_space; struct ek_type_entry { char *str; unsigned int strlen; unsigned int index; unsigned long xhash; unsigned long yhash; struct ek_type_entry *next; } *entry; } *type; char data[]; } *


decompiled_code:
__int64 __fastcall ek_obj_setattr_str(__int64 a1, char *a2, __int64 a3)
{
__int64 v4; // rax
__int64 result; // rax

v4 = strlen(a2);
result = *(char *)(**(_QWORD **)a1 + (unsigned __int64)xhash(a2, v4) % *(_QWORD *)(*(_QWORD *)a1 + 8LL));
*(_QWORD *)(a1 + 8 * result + 8) = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char *a2, __int64 a3)
{
__int64 v4; // rax
__int64 result; // rax

v4 = strlen(a2);
result = *(char *)(**(_QWORD **)a1 + (unsigned __int64)func0(a2, v4) % *(_QWORD *)(*(_QWORD *)a1 + 8LL));
*(_QWORD *)(a1 + 8 * result + 8) = a3;
return result;
}

original_code:
void ek_obj_setattr_str(ekop o, char* s, ekop v){
  unsigned long hash = xhash(s, strlen(s)) % o->type->map_size;
  *((ekop*)o->data + o->type->map[hash]) = v;
}

canonical_original_code:
void ek_obj_setattr_str(struct ek_obj *o, char *s, struct ek_obj *v){
  unsigned long hash = xhash(s, strlen(s)) % o->type->map_size;
  *((struct ek_obj **)o->data + o->type->map[hash]) = v;
}

variable_types:
hash
unsigned long

user_defined_types: struct ek_type_entry { char *str; unsigned int strlen; unsigned int index; unsigned long xhash; unsigned long yhash; struct ek_type_entry *next; }


decompiled_code:
__int64 __fastcall ek_obj_getattr_strn(__int64 a1, char *a2, int a3)
{
return *(_QWORD *)(a1
+ 8LL
* *(char *)(**(_QWORD **)a1 + (unsigned __int64)xhash(a2, a3) % *(_QWORD *)(*(_QWORD *)a1 + 8LL))
+ 8);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, char *a2, int a3)
{
return *(_QWORD *)(a1
+ 8LL
* *(char *)(**(_QWORD **)a1 + (unsigned __int64)func0(a2, a3) % *(_QWORD *)(*(_QWORD *)a1 + 8LL))
+ 8);
}

original_code:
ekop ek_obj_getattr_strn(ekop o, char* s, int l){
  unsigned long hash = xhash(s, l) % o->type->map_size;
  return *((ekop*)o->data + o->type->map[hash]);
}

canonical_original_code:
struct ek_obj *ek_obj_getattr_strn(struct ek_obj *o, char *s, int l){
  unsigned long hash = xhash(s, l) % o->type->map_size;
  return *((struct ek_obj **)o->data + o->type->map[hash]);
}

variable_types: l int


decompiled_code:
void *__fastcall arraylist_remove(__int64 a1, int a2)
{
__int64 v2; // rdx
char *v3; // r12
void *v4; // r13
int v5; // er15

v2 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v2 < a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v2, (unsigned int)a2);
v3 = (char *)(*(_QWORD *)a1 + a2 * *(_DWORD *)(a1 + 8));
v4 = malloc(*(int *)(a1 + 8));
if ( !v4 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
v5 = *(_DWORD *)(a1 + 8);
memcpy(v4, v3, v5);
memmove(v3, &v3[v5], v5 * (*(_DWORD *)(a1 + 12) + ~a2));
--*(_DWORD *)(a1 + 12);
return v4;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1, int a2)
{
__int64 v2; // rdx
char *v3; // r12
void *v4; // r13
int v5; // er15

v2 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v2 < a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v2, (unsigned int)a2);
v3 = (char *)(*(_QWORD *)a1 + a2 * *(_DWORD *)(a1 + 8));
v4 = malloc(*(int *)(a1 + 8));
if ( !v4 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
v5 = *(_DWORD *)(a1 + 8);
memcpy(v4, v3, v5);
memmove(v3, &v3[v5], v5 * (*(_DWORD *)(a1 + 12) + ~a2));
--*(_DWORD *)(a1 + 12);
return v4;
}

original_code:
void* arraylist_remove(arraylist *list, int index){
 if(index>list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 void *position=list->array+index*list->element_size;
 void *element;
 if((element=malloc(list->element_size))==((void *)0)) fprintf(stderr,"Ran out of memory");
 memcpy(element,position,list->element_size);
 memmove(position,position+list->element_size,list->element_size*(list->size-index-1));
 --list->size;
 return element;
}

canonical_original_code:
void *arraylist_remove(struct arraylist *list, int index){
 if(index>list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 void *position = list->array+index*list->element_size;
 void *element;
 if((element=malloc(list->element_size))==((void *)0)) fprintf(stderr,"Ran out of memory");
 memcpy(element,position,list->element_size);
 memmove(position,position+list->element_size,list->element_size*(list->size-index-1));
 --list->size;
 return element;
}

variable_types: struct arraylist { void *array; int element_size; int size; int capacity; } *

variable_types: index int

variable_types:
position
void *

variable_types:
element
void *


decompiled_code:
void *__fastcall arraylist_removeEnd(__int64 a1)
{
return arraylist_remove(a1, *(_DWORD *)(a1 + 12) - 1);
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
return func0(a1, *(_DWORD *)(a1 + 12) - 1);
}

original_code:
void* arraylist_removeEnd(arraylist *list){
 return arraylist_remove(list,list->size-1);
}

canonical_original_code:
void *arraylist_removeEnd(struct arraylist *list){
 return arraylist_remove(list,list->size-1);
}

user_defined_types: struct arraylist { void *array; int element_size; int size; int capacity; }


decompiled_code:
__int64 __fastcall arraylist_get(__int64 a1, int a2)
{
__int64 v2; // rdx

v2 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v2 <= a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v2, (unsigned int)a2);
return *(_QWORD *)a1 + *(_DWORD *)(a1 + 8) * a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // rdx

v2 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v2 <= a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v2, (unsigned int)a2);
return *(_QWORD *)a1 + *(_DWORD *)(a1 + 8) * a2;
}

original_code:
void* arraylist_get(arraylist *list, int index){
 if(index>=list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 return list->array+index*list->element_size;
}

canonical_original_code:
void *arraylist_get(struct arraylist *list, int index){
 if(index>=list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 return list->array+index*list->element_size;
}


decompiled_code:
void *__fastcall arraylist_set(__int64 a1, int a2, const void *a3)
{
__int64 v5; // rdx
int v6; // edi
void *v7; // rbx
void *v8; // r13
size_t v9; // rbp

v5 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v5 <= a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v5, (unsigned int)a2);
v6 = *(_DWORD *)(a1 + 8);
v7 = (void *)(*(_QWORD *)a1 + v6 * a2);
v8 = malloc(v6);
if ( !v8 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
v9 = *(int *)(a1 + 8);
memcpy(v8, v7, v9);
memcpy(v7, a3, v9);
return v8;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, int a2, const void *a3)
{
__int64 v5; // rdx
int v6; // edi
void *v7; // rbx
void *v8; // r13
size_t v9; // rbp

v5 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v5 <= a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v5, (unsigned int)a2);
v6 = *(_DWORD *)(a1 + 8);
v7 = (void *)(*(_QWORD *)a1 + v6 * a2);
v8 = malloc(v6);
if ( !v8 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
v9 = *(int *)(a1 + 8);
memcpy(v8, v7, v9);
memcpy(v7, a3, v9);
return v8;
}

original_code:
void* arraylist_set(arraylist *list, int index, void* itemPtr){
 if(index>=list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 void *object;
 void *position=list->array+index*list->element_size;
 if((object=malloc(list->element_size))==((void *)0)){
  fprintf(stderr,"Ran out of memory");
 }
 memcpy(object,position,list->element_size);
 memcpy(position, itemPtr, list->element_size);
 return object;
}

canonical_original_code:
void *arraylist_set(struct arraylist *list, int index, void *itemPtr){
 if(index>=list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 void *object;
 void *position = list->array+index*list->element_size;
 if((object=malloc(list->element_size))==((void *)0)){
  fprintf(stderr,"Ran out of memory");
 }
 memcpy(object,position,list->element_size);
 memcpy(position, itemPtr, list->element_size);
 return object;
}

variable_types:
itemPtr
void *

variable_types:
object
void *


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

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

original_code:
void arraylist_free(arraylist *list){
 free(list->array);
 free(list);
}

canonical_original_code:
void arraylist_free(struct arraylist *list){
 free(list->array);
 free(list);
}


decompiled_code:
__int64 __fastcall arraylist_size(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
int arraylist_size(arraylist *list){
 return list->size;
}

canonical_original_code:
int arraylist_size(struct arraylist *list){
 return list->size;
}


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

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

original_code:
int arraylist_isEmpty(arraylist *list){
 return list->size==0;
}

canonical_original_code:
int arraylist_isEmpty(struct arraylist *list){
 return list->size==0;
}


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

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

original_code:
void arraylist_clear(arraylist *list){
 list->size=0;
}

canonical_original_code:
void arraylist_clear(struct arraylist *list){
 list->size=0;
}


decompiled_code:
void __fastcall arraylist_swap(__int64 a1, int a2, int a3)
{
const void *v4; // rax
void *v5; // rbp
void *v6; // rbx

v4 = (const void *)arraylist_get(a1, a3);
v5 = arraylist_set(a1, a2, v4);
v6 = arraylist_set(a1, a3, v5);
free(v5);
free(v6);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, int a2, int a3)
{
const void *v4; // rax
void *v5; // rbp
void *v6; // rbx

v4 = (const void *)func2(a1, a3);
v5 = func3(a1, a2, v4);
v6 = func3(a1, a3, v5);
free(v5);
free(v6);
}

original_code:
void arraylist_swap(arraylist *list, int firstIndex, int secondIndex){
 void* a=arraylist_set(list,firstIndex,arraylist_get(list,secondIndex));
 void* b=arraylist_set(list,secondIndex,a);
 free(a);
 free(b);
}

canonical_original_code:
void arraylist_swap(struct arraylist *list, int firstIndex, int secondIndex){
 void *a = arraylist_set(list,firstIndex,arraylist_get(list,secondIndex));
 void *b = arraylist_set(list,secondIndex,a);
 free(a);
 free(b);
}

variable_types: firstIndex int

variable_types: secondIndex int

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
void __fastcall arraylist_resize(__int64 a1)
{
void *v1; // rbp
int v2; // eax
void *v3; // rax

v1 = *(void **)a1;
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = 2 * v2;
v3 = malloc(*(_DWORD *)(a1 + 8) * 2 * v2);
*(_QWORD *)a1 = v3;
if ( !v3 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
memcpy(*(void **)a1, v1, *(_DWORD *)(a1 + 8) * *(_DWORD *)(a1 + 12));
free(v1);
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1)
{
void *v1; // rbp
int v2; // eax
void *v3; // rax

v1 = *(void **)a1;
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = 2 * v2;
v3 = malloc(*(_DWORD *)(a1 + 8) * 2 * v2);
*(_QWORD *)a1 = v3;
if ( !v3 )
fwrite("Ran out of memory", 1uLL, 0x11uLL, stderr);
memcpy(*(void **)a1, v1, *(_DWORD *)(a1 + 8) * *(_DWORD *)(a1 + 12));
free(v1);
}

original_code:
void arraylist_resize(arraylist *list){
 void *sparePointer=list->array;
 list->capacity*=2;
 if((list->array=malloc(list->capacity*list->element_size))==((void *)0)){
  fprintf(stderr,"Ran out of memory");
 }
 memcpy(list->array, sparePointer, list->size*list->element_size);
 free(sparePointer);
}

canonical_original_code:
void arraylist_resize(struct arraylist *list){
 void *sparePointer = list->array;
 list->capacity*=2;
 if((list->array=malloc(list->capacity*list->element_size))==((void *)0)){
  fprintf(stderr,"Ran out of memory");
 }
 memcpy(list->array, sparePointer, list->size*list->element_size);
 free(sparePointer);
}

variable_types:
sparePointer
void *


decompiled_code:
void *__fastcall arraylist_add(__int64 a1, int a2, const void *a3)
{
__int64 v4; // rdx
int v5; // er15
char *v6; // rbp
void *result; // rax

v4 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v4 < a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v4, (unsigned int)a2);
v5 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) == *(_DWORD *)(a1 + 16) )
arraylist_resize(a1);
v6 = (char *)(*(_QWORD *)a1 + v5 * a2);
memmove(&v6[v5], v6, v5 * (*(_DWORD *)(a1 + 12) - a2));
result = memcpy(v6, a3, v5);
++*(_DWORD *)(a1 + 12);
return result;
}

canonical_decompiled_code:
void *__fastcall func10(__int64 a1, int a2, const void *a3)
{
__int64 v4; // rdx
int v5; // er15
char *v6; // rbp
void *result; // rax

v4 = *(unsigned int *)(a1 + 12);
if ( a2 < 0 || (int)v4 < a2 )
fprintf(stderr, "your index is out of bounds. size:%d index %d\n", v4, (unsigned int)a2);
v5 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) == *(_DWORD *)(a1 + 16) )
func9(a1);
v6 = (char *)(*(_QWORD *)a1 + v5 * a2);
memmove(&v6[v5], v6, v5 * (*(_DWORD *)(a1 + 12) - a2));
result = memcpy(v6, a3, v5);
++*(_DWORD *)(a1 + 12);
return result;
}

original_code:
void arraylist_add(arraylist *list,int index, void* itemPtr){
 if(index>list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 int elem_size=list->element_size;
 if(list->size==list->capacity){
  arraylist_resize(list);
 }
 void *position=list->array+index*elem_size;
 memmove(position+elem_size,position, elem_size*(list->size-index));
 memcpy(position, itemPtr, elem_size);
 ++list->size;
}

canonical_original_code:
void arraylist_add(struct arraylist *list, int index, void *itemPtr){
 if(index>list->size||index<0)
  fprintf(stderr,"your index is out of bounds. size:%d index %d\n",list->size,index);
 int elem_size = list->element_size;
 if(list->size==list->capacity){
  arraylist_resize(list);
 }
 void *position = list->array+index*elem_size;
 memmove(position+elem_size,position, elem_size*(list->size-index));
 memcpy(position, itemPtr, elem_size);
 ++list->size;
}


decompiled_code:
void *__fastcall arraylist_addEnd(__int64 a1, const void *a2)
{
return arraylist_add(a1, *(_DWORD *)(a1 + 12), a2);
}

canonical_decompiled_code:
void *__fastcall func11(__int64 a1, const void *a2)
{
return func10(a1, *(_DWORD *)(a1 + 12), a2);
}

original_code:
void arraylist_addEnd(arraylist *list, void* itemPtr){
 arraylist_add(list,list->size,itemPtr);
}

canonical_original_code:
void arraylist_addEnd(struct arraylist *list, void *itemPtr){
 arraylist_add(list,list->size,itemPtr);
}


decompiled_code:
int __fastcall arraylist_print(__int64 a1, void (__fastcall *a2)(__int64))
{
int v2; // ebx
__int64 v3; // rax

putchar(91);
v2 = 0;
if ( *(int *)(a1 + 12) > 0 )
{
while ( 1 )
{
v3 = arraylist_get(a1, v2);
a2(v3);
if ( *(_DWORD *)(a1 + 12) <= ++v2 )
break;
if ( v2 )
putchar(44);
}
}
return puts("]");
}

original_code:
void arraylist_print(arraylist *list, void (*printelem)(void *)){
 int i;
 printf("[");
 for(i=0;i<list->size;++i){
  if(i!=0) printf(",");
  printelem(arraylist_get(list,i));
 }
 printf("]\n");
}

canonical_original_code:
void arraylist_print(struct arraylist *list, void (*printelem)(void *)){
 int i;
 printf("[");
 for(i=0;i<list->size;++i){
  if(i!=0) printf(",");
  printelem(arraylist_get(list,i));
 }
 printf("]\n");
}

variable_types:
printelem
void (*)(void *)

variable_types: i int


decompiled_code:
int __fastcall arraylist_printInt(unsigned int *a1)
{
return printf("%d", *a1);
}

canonical_decompiled_code:
int __fastcall func13(unsigned int *a1)
{
return printf("%d", *a1);
}

original_code:
void arraylist_printInt(void* i){
 int j=*((int*)i);
 printf("%d",j);
}

canonical_original_code:
void arraylist_printInt(void *i){
 int j = *((int*)i);
 printf("%d",j);
}

variable_types:
i
void *

variable_types: j int


repo: benkasminbullock/cfunctions

decompiled_code:
void *__fastcall malloc_or_exit(size_t a1, int a2)
{
void *result; // rax
const char *v3; // rdx

result = malloc(a1);
if ( result )
{
++n_mallocs;
}
else
{
error((int)"out of memory", a2, v3);
result = (void *)calloc_or_exit();
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1, int a2)
{
void *result; // rax
const char *v3; // rdx

result = malloc(a1);
if ( result )
{
++n_mallocs;
}
else
{
error((int)"out of memory", a2, v3);
result = (void *)func1();
}
return result;
}

original_code:
void *
malloc_or_exit (size_t s)
{
    void * v;
    v = malloc (s);
    if (! v) {
 error ("out of memory");
    }
    n_mallocs++;
    return v;
}

canonical_original_code:
void *malloc_or_exit(unsigned long s)
{
    void *v;
    v = malloc (s);
    if (! v) {
 error ("out of memory");
    }
    n_mallocs++;
    return v;
}

variable_types:
s
unsigned long

variable_types:
v
void *


decompiled_code:
void *__fastcall calloc_or_exit(size_t a1, size_t a2)
{
void *result; // rax
const char *v3; // rdx

result = calloc(a1, a2);
if ( result )
{
++n_mallocs;
}
else
{
error((int)"out of memory", a2, v3);
result = (void *)realloc_or_exit();
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1, size_t a2)
{
void *result; // rax
const char *v3; // rdx

result = calloc(a1, a2);
if ( result )
{
++n_mallocs;
}
else
{
error((int)"out of memory", a2, v3);
result = (void *)func2();
}
return result;
}

original_code:
void *
calloc_or_exit (size_t s, size_t size)
{
    void * v;
    v = calloc (s, size);
    if (! v) {
 error ("out of memory");
    }
    n_mallocs++;
    return v;
}

canonical_original_code:
void *calloc_or_exit(unsigned long s, unsigned long size)
{
    void *v;
    v = calloc (s, size);
    if (! v) {
 error ("out of memory");
    }
    n_mallocs++;
    return v;
}

variable_types:
size
unsigned long


decompiled_code:
void *__fastcall realloc_or_exit(void *a1, size_t a2)
{
void *result; // rax
const char *v3; // rdx

if ( !a1 )
++n_mallocs;
result = realloc(a1, a2);
if ( !result )
{
error((int)"out of memory", a2, v3);
result = (void *)fseek_or_exit();
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1, size_t a2)
{
void *result; // rax
const char *v3; // rdx

if ( !a1 )
++n_mallocs;
result = realloc(a1, a2);
if ( !result )
{
error((int)"out of memory", a2, v3);
result = (void *)func3();
}
return result;
}

original_code:
void *
realloc_or_exit (void * v, size_t s)
{
    if (! v) {
 n_mallocs++;
    }
    v = realloc (v, s);
    if (! v) {
 error ("out of memory");
    }
    return v;
}

canonical_original_code:
void *realloc_or_exit(void *v, unsigned long s)
{
    if (! v) {
 n_mallocs++;
    }
    v = realloc (v, s);
    if (! v) {
 error ("out of memory");
    }
    return v;
}


decompiled_code:
int __fastcall stat_or_exit(const char *errnum, struct stat *a2)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = stat(errnum, a2);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"stat failed on %s: %s", (int)errnum, v4);
result = fclose_or_exit();
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(const char *errnum, struct stat *a2)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = stat(errnum, a2);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"stat failed on %s: %s", (int)errnum, v4);
result = func7();
}
return result;
}

original_code:
void
stat_or_exit (const char * file_name, struct stat * buf)
{
    if (stat (file_name, buf) == -1) {
 error ("stat failed on %s: %s", file_name, strerror ((*__errno_location ())));
    }
}

canonical_original_code:
void stat_or_exit(char *file_name, struct stat *buf)
{
    if (stat (file_name, buf) == -1) {
 error ("stat failed on %s: %s", file_name, strerror ((*__errno_location ())));
    }
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall free_or_exit(void *a1)
{
__int64 result; // rax

if ( a1 )
{
free(a1);
--n_mallocs;
result = 0LL;
}
else
{
fprintf(stderr, "%s:%d: Attempt to free null pointer.\n", "sys-or-exit.c", 132LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(void *a1)
{
__int64 result; // rax

if ( a1 )
{
free(a1);
--n_mallocs;
result = 0LL;
}
else
{
fprintf(stderr, "%s:%d: Attempt to free null pointer.\n", "sys-or-exit.c", 132LL);
result = 1LL;
}
return result;
}

original_code:
or_exit_t
free_or_exit (void * memory)
{
    if (! memory) {
 fprintf (stderr, "%s:%d: Attempt to free null pointer.\n",
   "sys-or-exit.c", 132);
 return or_exit_null_pointer;
    }
    free (memory);
    n_mallocs--;
    return or_exit_ok;
}

canonical_original_code:
enum or_exit_t free_or_exit(void *memory)
{
    if (! memory) {
 fprintf (stderr, "%s:%d: Attempt to free null pointer.\n",
   "sys-or-exit.c", 132);
 return or_exit_null_pointer;
    }
    free (memory);
    n_mallocs--;
    return or_exit_ok;
}

variable_types:
memory
void *

return_type: enum or_exit_t { or_exit_ok=0, or_exit_null_pointer=1 }


decompiled_code:
char *__fastcall strdup_or_exit(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rsi
const char *v4; // rdx

result = strdup(a1);
if ( result )
{
++n_mallocs;
}
else
{
v2 = _errno_location();
v3 = strerror(*v2);
error((int)"strdup failed: %s", (int)v3, v4);
result = (char *)chmod_or_exit((int)"strdup failed: %s", v3);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func9(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rsi
const char *v4; // rdx

result = strdup(a1);
if ( result )
{
++n_mallocs;
}
else
{
v2 = _errno_location();
v3 = strerror(*v2);
error((int)"strdup failed: %s", (int)v3, v4);
result = (char *)func10((int)"strdup failed: %s", v3);
}
return result;
}

original_code:
char *
strdup_or_exit (const char * todup)
{
    char * dupped;
    dupped = strdup (todup);
    if (! dupped) {
 error ("strdup failed: %s", strerror ((*__errno_location ())));
    }
    n_mallocs++;
    return dupped;
}

canonical_original_code:
char *strdup_or_exit(char *todup)
{
    char *dupped;
    dupped = strdup (todup);
    if (! dupped) {
 error ("strdup failed: %s", strerror ((*__errno_location ())));
    }
    n_mallocs++;
    return dupped;
}

variable_types:
todup
char *

variable_types:
dupped
char *

return_type: char *


decompiled_code:
int __fastcall chmod_or_exit(const char *errnum, char *format)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = chmod(errnum, (__mode_t)format);
if ( result )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"Could not chmod (%s, %o): %s", (int)errnum, (const char *)(unsigned int)format, v4);
result = unlink_or_exit((int)"Could not chmod (%s, %o): %s");
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(const char *errnum, char *format)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = chmod(errnum, (__mode_t)format);
if ( result )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"Could not chmod (%s, %o): %s", (int)errnum, (const char *)(unsigned int)format, v4);
result = func11((int)"Could not chmod (%s, %o): %s");
}
return result;
}

original_code:
void
chmod_or_exit (const char * path, mode_t mode)
{
    int status;
    status = chmod (path, mode);
    if (status) {
 error ("Could not chmod (%s, %o): %s", path, mode, strerror ((*__errno_location ())));
    }
}

canonical_original_code:
void chmod_or_exit(char *path, unsigned int mode)
{
    int status;
    status = chmod (path, mode);
    if (status) {
 error ("Could not chmod (%s, %o): %s", path, mode, strerror ((*__errno_location ())));
    }
}

variable_types:
path
char *

variable_types:
mode
unsigned int

variable_types: status int