original_code:
void *
rb_tree_remove(struct rb_tree *t, void *data)
{
    void *delete_data = ((void *)0);
    t->root = _node_remove(t->root, data, &delete_data, t->cmp_func);
    t->root->color = 0;
    return delete_data;
}

canonical_original_code:
void *rb_tree_remove(struct rb_tree *t, void *data)
{
    void *delete_data = ((void *)0);
    t->root = _node_remove(t->root, data, &delete_data, t->cmp_func);
    t->root->color = 0;
    return delete_data;
}

variable_types: struct rb_tree { struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *root; int (*cmp_func)(void *, void *); } *

variable_types:
data
void *

user_defined_types: struct rb_tree { struct tree_node *root; int (*cmp_func)(void *, void *); }


repo: leyafo/practice-algorithm

decompiled_code:
_BOOL8 __fastcall rb_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

original_code:
int
rb_tree_empty(struct rb_tree *t){
    return t == ((void *)0) || t->root == ((void *)0);
}

return_type: int

user_defined_types: struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; }


decompiled_code:
void __fastcall rb_tree_ascend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func13(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func0(*a1, 1, a2);
}

original_code:
void
rb_tree_ascend_iterate(struct rb_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root,1, iterate_func);
}

return_type: void


decompiled_code:
void __fastcall rb_tree_descend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func14(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func0(*a1, 0, a2);
}

original_code:
void
rb_tree_descend_iterate(struct rb_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root,0, iterate_func);
}


decompiled_code:
void __fastcall rb_tree_release(void **a1)
{
tree_iterate(*(_QWORD **)*a1, 2, 0LL);
*(_QWORD *)*a1 = 0LL;
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func15(void **a1)
{
func0(*(_QWORD **)*a1, 2, 0LL);
*(_QWORD *)*a1 = 0LL;
free(*a1);
*a1 = 0LL;
}

original_code:
void rb_tree_release(struct rb_tree **t){
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct rb_tree { struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *root; int (*cmp_func)(void *, void *); } **


decompiled_code:
__int64 __fastcall rb_tree_find_max(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 24);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 24);
}
return result;
}

original_code:
void *
rb_tree_find_max(struct rb_tree *t)
{
    if (t->root == ((void *)0))
        return ((void *)0);
    struct tree_node *n = t->root;
    while(n->right != ((void *)0)){
        n = n->right;
    }
    return n->data;
}

canonical_original_code:
void *rb_tree_find_max(struct rb_tree *t)
{
    if (t->root == ((void *)0))
        return ((void *)0);
    struct tree_node *n = t->root;
    while(n->right != ((void *)0)){
        n = n->right;
    }
    return n->data;
}

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *

return_type: void *


decompiled_code:
_BOOL8 __fastcall binary_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_QWORD *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0LL;
return result;
}

original_code:
int
binary_tree_empty(struct binary_tree *t) {
    if(t)
        return t->root == ((void *)0);
    return 1;
}

variable_types: struct binary_tree { struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list { struct list *next; void *data; } *equal_list; } *root; int (*cmp_func)(void *, void *); int count; } *

user_defined_types: struct binary_tree { struct tree_node *root; int (*cmp_func)(void *, void *); int count; }

user_defined_types: struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list *equal_list; }


decompiled_code:
void __fastcall binary_tree_descend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func4(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func2(*a1, 0, a2);
}

original_code:
void
binary_tree_descend_iterate(struct binary_tree *t, void (*iterate_func)(void *)) {
    _tree_iterate(t->root, 0, iterate_func);
}

user_defined_types: struct list { struct list *next; void *data; }


decompiled_code:
void __fastcall binary_tree_ascend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func5(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func2(*a1, 1, a2);
}

original_code:
void
binary_tree_ascend_iterate(struct binary_tree *t, void (*iterate_func)(void *)) {
    _tree_iterate(t->root, 1, iterate_func);
}


decompiled_code:
_BOOL8 __fastcall binary_tree_find(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD))(a1 + 8)) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1, __int64 a2)
{
return func1(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD))(a1 + 8)) != 0;
}

original_code:
int
binary_tree_find(struct binary_tree *t, void *data) {
    if(_tree_find(t->root, data, t->cmp_func))
        return 1;
    return 0;
}


decompiled_code:
void __fastcall binary_tree_release(void **a1)
{
tree_iterate(*(_QWORD **)*a1, 2, 0LL);
*((_DWORD *)*a1 + 4) = 0;
*(_QWORD *)*a1 = 0LL;
free(*a1);
*a1 = 0LL;
}

original_code:
void
binary_tree_release(struct binary_tree **t) {
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->count = 0;
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct binary_tree { struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list { struct list *next; void *data; } *equal_list; } *root; int (*cmp_func)(void *, void *); int count; } **


decompiled_code:
__int64 __fastcall binary_tree_count(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int
binary_tree_count(struct binary_tree *t) {
    return t->count;
}


original_code:
void
avl_tree_insert(struct avl_tree *t, void *data)
{
    struct tree_node **n = &t->root, *parent = ((void *)0);
    while(1) {
        if(*n == ((void *)0)) {
            *n = malloc(sizeof(struct tree_node));
            (*n)->parent = parent;
            (*n)->left = (*n)->right = ((void *)0);
            (*n)->height = 1;
            (*n)->data = data;
            break;
        }
        parent = *n;
        int cmp_result = (*t->cmp_func)((*n)->data,data);
        if( cmp_result > 0)
            n = &(*n)->left;
        else if(cmp_result < 0)
            n = &(*n)->right;
        else
            return;
    }
    ++t->count;
    _keep_balance(t, *n);
}

canonical_original_code:
void avl_tree_insert(struct avl_tree *t, void *data)
{
    struct tree_node **n = &t->root; struct tree_node *parent = ((void *)0);
    while(1) {
        if(*n == ((void *)0)) {
            *n = malloc(sizeof(struct tree_node));
            (*n)->parent = parent;
            (*n)->left = (*n)->right = ((void *)0);
            (*n)->height = 1;
            (*n)->data = data;
            break;
        }
        parent = *n;
        int cmp_result = (*t->cmp_func)((*n)->data,data);
        if( cmp_result > 0)
            n = &(*n)->left;
        else if(cmp_result < 0)
            n = &(*n)->right;
        else
            return;
    }
    ++t->count;
    _keep_balance(t, *n);
}

variable_types: struct avl_tree { struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *root; int count; int (*cmp_func)(void *, void *); } *

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } **

user_defined_types: struct avl_tree { struct tree_node *root; int count; int (*cmp_func)(void *, void *); }


original_code:
void *
avl_tree_remove(struct avl_tree *t, void *data)
{
    struct tree_node *erase_node = _tree_find(t->root, data, t->cmp_func);
    void * remove_data = ((void *)0);
    if(erase_node)
    {
        struct tree_node *free_node;
        if(erase_node->left == ((void *)0) && erase_node->right == ((void *)0)){
            _set_parent(erase_node->parent, erase_node, ((void *)0));
            if(t->root == erase_node)
                t->root = ((void *)0);
            free_node = erase_node;
            goto keep_balance;
        }
        struct tree_node *it = erase_node;
        if(it->right != ((void *)0)) {
            it = it->right;
            while(it->left)
                it = it->left;
            void * tmp = erase_node->data;
            erase_node->data = it->data;
            it->data = tmp;
            if(it->right){
                _set_parent(it->parent, it, it->right);
            } else {
                _set_parent(it->parent, it, ((void *)0));
            }
        } else {
            _set_parent(it->parent, it, it->left);
            if(t->root == it) {
                t->root = it->left;
            }
        }
        free_node = it;
    keep_balance:
        {
            _keep_balance(t, free_node);
            remove_data = free_node->data;
            free(free_node);
            --t->count;
        }
    }
    return remove_data;
}

canonical_original_code:
void *avl_tree_remove(struct avl_tree *t, void *data)
{
    struct tree_node *erase_node = _tree_find(t->root, data, t->cmp_func);
    void *remove_data = ((void *)0);
    if(erase_node)
    {
        struct tree_node *free_node;
        if(erase_node->left == ((void *)0) && erase_node->right == ((void *)0)){
            _set_parent(erase_node->parent, erase_node, ((void *)0));
            if(t->root == erase_node)
                t->root = ((void *)0);
            free_node = erase_node;
            goto keep_balance;
        }
        struct tree_node *it = erase_node;
        if(it->right != ((void *)0)) {
            it = it->right;
            while(it->left)
                it = it->left;
            void *tmp = erase_node->data;
            erase_node->data = it->data;
            it->data = tmp;
            if(it->right){
                _set_parent(it->parent, it, it->right);
            } else {
                _set_parent(it->parent, it, ((void *)0));
            }
        } else {
            _set_parent(it->parent, it, it->left);
            if(t->root == it) {
                t->root = it->left;
            }
        }
        free_node = it;
    keep_balance:
        {
            _keep_balance(t, free_node);
            remove_data = free_node->data;
            free(free_node);
            --t->count;
        }
    }
    return remove_data;
}

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *

variable_types:
tmp
void *


decompiled_code:
_BOOL8 __fastcall avl_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0LL;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(_QWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0LL;
else
result = 0LL;
return result;
}

original_code:
int
avl_tree_empty(struct avl_tree *t)
{
    if(!t)
        return 0;
    return t->root == ((void *)0);
}

user_defined_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; }


decompiled_code:
__int64 __fastcall avl_tree_count(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int
avl_tree_count(struct avl_tree *t)
{
    return t->count;
}


decompiled_code:
void __fastcall avl_tree_ascend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func12(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func7(*a1, 1, a2);
}

original_code:
void
avl_tree_ascend_iterate(struct avl_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root, 1, iterate_func);
}


decompiled_code:
void __fastcall avl_tree_descend_iterate(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func13(_QWORD **a1, void (__fastcall *a2)(_QWORD))
{
func7(*a1, 0, a2);
}

original_code:
void
avl_tree_descend_iterate(struct avl_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root, 0, iterate_func);
}


decompiled_code:
_BOOL8 __fastcall avl_tree_find(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD))(a1 + 16)) != 0;
}

original_code:
int
avl_tree_find(struct avl_tree *t, void *data)
{
    if(_tree_find(t->root, data, t->cmp_func))
        return 1;
    return 0;
}


decompiled_code:
void __fastcall avl_tree_release(void **a1)
{
tree_iterate(*(_QWORD **)*a1, 2, 0LL);
*((_DWORD *)*a1 + 2) = 0;
*(_QWORD *)*a1 = 0LL;
free(*a1);
*a1 = 0LL;
}

original_code:
void
avl_tree_release(struct avl_tree **t)
{
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->count = 0;
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct avl_tree { struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *root; int count; int (*cmp_func)(void *, void *); } **


decompiled_code:
__int64 __fastcall avl_tree_find_max(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 32);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 32);
}
return result;
}

original_code:
void *
avl_tree_find_max(struct avl_tree *t)
{
    struct tree_node *n = t->root;
    if(!n)
        return ((void *)0);
    while(n->right)
        n = n->right;
    return n->data;
}

canonical_original_code:
void *avl_tree_find_max(struct avl_tree *t)
{
    struct tree_node *n = t->root;
    if(!n)
        return ((void *)0);
    while(n->right)
        n = n->right;
    return n->data;
}


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

result = malloc(0x10uLL);
*(_DWORD *)result = 0;
result[1] = 0LL;
return result;
}

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

result = malloc(0x10uLL);
*(_DWORD *)result = 0;
result[1] = 0LL;
return result;
}

original_code:
struct stack*
stack_init(void) {
    struct stack *stk;
    stk = malloc(sizeof(struct stack));
    stk->count = 0;
    stk->head = ((void *)0);
    return stk;
}

canonical_original_code:
struct stack *stack_init() {
    struct stack *stk;
    stk = malloc(sizeof(struct stack));
    stk->count = 0;
    stk->head = ((void *)0);
    return stk;
}

variable_types: struct stack { int count; struct element { void *data; struct element *link; } *head; } *

user_defined_types: struct stack { int count; struct element *head; }


decompiled_code:
_BOOL8 __fastcall stack_empty(_DWORD *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_DWORD *a1)
{
return *a1 == 0;
}

original_code:
int
stack_empty(struct stack *stk) {
    return stk->count == 0;
}

user_defined_types: struct element { void *data; struct element *link; }


original_code:
void *
stack_pop(struct stack *stk) {
    void *data;
    struct element *e;
    e = stk->head;
    stk->head = e->link;
    stk->count--;
    data = e->data;
    free(e);
    return data;
}

canonical_original_code:
void *stack_pop(struct stack *stk) {
    void *data;
    struct element *e;
    e = stk->head;
    stk->head = e->link;
    stk->count--;
    data = e->data;
    free(e);
    return data;
}

variable_types: struct element { void *data; struct element *link; } *


decompiled_code:
void __fastcall stack_release(void **a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)*a1 + 1);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func3(void **a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)*a1 + 1);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
free(*a1);
*a1 = 0LL;
}

original_code:
void
stack_release(struct stack **stk) {
    struct element *e, *u;
    e = (*stk)->head;
    while(e) {
        u = e;
        e = e->link;
        free(u);
    }
    free(*stk);
    *stk = ((void *)0);
}

canonical_original_code:
void stack_release(struct stack **stk) {
    struct element *e; struct element *u;
    e = (*stk)->head;
    while(e) {
        u = e;
        e = e->link;
        free(u);
    }
    free(*stk);
    *stk = ((void *)0);
}

variable_types: struct stack { int count; struct element { void *data; struct element *link; } *head; } **


main

canonical_name: main

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

v3 = getenv("AWS_ACCESS_KEY_ID");
v4 = getenv("AWS_SECRET_KEY");
if ( argc <= 1 )
{
fwrite("Usage: s3test <bucket>\n", 1uLL, 0x17uLL, stderr);
result = 1;
}
else if ( v3 && v4 )
{
v5 = argv[1];
v6 = s3_init(v3, v4, "s3.amazonaws.com");
puts("Listing bucket root");
v7 = (_QWORD *)s3_list_bucket(v6, v5, 0LL);
v8 = v7;
if ( v7 )
{
for ( i = *v7; i; i = *(_QWORD *)(i + 32) )
printf("\tKey %s\n", *(const char **)i);
s3_bucket_entries_free(v8);
}
puts("Listing /foo/bar/");
v10 = (_QWORD *)s3_list_bucket(v6, v5, "foo/bar/");
v11 = v10;
if ( v10 )
{
for ( j = *v10; j; j = *(_QWORD *)(j + 32) )
printf("\tKey %s\n", *(const char **)j);
s3_bucket_entries_free(v11);
}
puts("Uploading foo.txt");
s3_put(v6, v5, "foo.txt", "text/plain", "foo bar gazonk", 14LL);
puts("Downloading foo.txt");
v13 = s3_string_init();
s3_get(v6, v5, "foo.txt", v13);
printf("Contents:\n%.*s\n", *(_DWORD *)(v13 + 8), *(const char **)v13);
s3_string_free(v13);
puts("Deleting foo.txt");
s3_delete(v6, v5, "foo.txt");
s3_free(v6);
result = 0;
}
else
{
fwrite("Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n", 1uLL, 0x48uLL, stderr);
result = 1;
}
return result;
}

original_code:
int main (int argc, char **argv) {
 struct S3 *s3;
 struct s3_string *out;
 struct s3_bucket_entry_head *bkt_entries;
 struct s3_bucket_entry *e;
 char *file_contents = "foo bar gazonk";
 char *s3_key_id = getenv("AWS_ACCESS_KEY_ID");
 char *s3_secret = getenv("AWS_SECRET_KEY");
 if (argc < 2) {
  fprintf(stderr, "Usage: s3test <bucket>\n");
  return 1;
 }
 if (s3_key_id == ((void *)0) || s3_secret == ((void *)0)) {
  fprintf(stderr, "Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n");
  return 1;
 }
 char *bucket = argv[1];
 s3 = s3_init(s3_key_id, s3_secret, "s3.amazonaws.com");
 printf("Listing bucket root\n");
 bkt_entries = s3_list_bucket(s3, bucket, ((void *)0));
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Listing /foo/bar/\n");
 bkt_entries = s3_list_bucket(s3, bucket, "foo/bar/");
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Uploading foo.txt\n");
 s3_put(s3, bucket, "foo.txt", "text/plain", file_contents, strlen(file_contents));
 printf("Downloading foo.txt\n");
 out = s3_string_init();
 s3_get(s3, bucket, "foo.txt", out);
 printf("Contents:\n%.*s\n", (int)out->len, out->ptr);
 s3_string_free(out);
 printf("Deleting foo.txt\n");
 s3_delete(s3, bucket, "foo.txt");
 s3_free(s3);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 struct S3 *s3;
 struct s3_string *out;
 struct s3_bucket_entry_head *bkt_entries;
 struct s3_bucket_entry *e;
 char *file_contents = "foo bar gazonk";
 char *s3_key_id = getenv("AWS_ACCESS_KEY_ID");
 char *s3_secret = getenv("AWS_SECRET_KEY");
 if (argc < 2) {
  fprintf(stderr, "Usage: s3test <bucket>\n");
  return 1;
 }
 if (s3_key_id == ((void *)0) || s3_secret == ((void *)0)) {
  fprintf(stderr, "Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n");
  return 1;
 }
 char *bucket = argv[1];
 s3 = s3_init(s3_key_id, s3_secret, "s3.amazonaws.com");
 printf("Listing bucket root\n");
 bkt_entries = s3_list_bucket(s3, bucket, ((void *)0));
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Listing /foo/bar/\n");
 bkt_entries = s3_list_bucket(s3, bucket, "foo/bar/");
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Uploading foo.txt\n");
 s3_put(s3, bucket, "foo.txt", "text/plain", file_contents, strlen(file_contents));
 printf("Downloading foo.txt\n");
 out = s3_string_init();
 s3_get(s3, bucket, "foo.txt", out);
 printf("Contents:\n%.*s\n", (int)out->len, out->ptr);
 s3_string_free(out);
 printf("Deleting foo.txt\n");
 s3_delete(s3, bucket, "foo.txt");
 s3_free(s3);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct S3 { char *secret; char *id; char *base_url; char *proxy; } *

variable_types: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; } *

variable_types: struct s3_bucket_entry_head { struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; } *tqh_first; struct s3_bucket_entry **tqh_last; } *

variable_types:
bucket
char *

user_defined_types: struct s3_bucket_entry_head { struct s3_bucket_entry *tqh_first; struct s3_bucket_entry **tqh_last; }


decompiled_code:
size_t __fastcall s3_string_curl_writefunc(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v5; // rbp
__int64 v6; // r13
char *v7; // rax

v5 = a3 * a2;
v6 = *(_QWORD *)(a4 + 8) + a3 * a2;
v7 = (char *)realloc(*(void **)a4, v6 + 1);
*(_QWORD *)a4 = v7;
if ( !v7 )
{
fwrite("realloc() failed\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
memcpy(&v7[*(_QWORD *)(a4 + 8)], src, v5);
*(_BYTE *)(*(_QWORD *)a4 + v6) = 0;
*(_QWORD *)(a4 + 8) = v6;
return v5;
}

canonical_decompiled_code:
size_t __fastcall func0(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v5; // rbp
__int64 v6; // r13
char *v7; // rax

v5 = a3 * a2;
v6 = *(_QWORD *)(a4 + 8) + a3 * a2;
v7 = (char *)realloc(*(void **)a4, v6 + 1);
*(_QWORD *)a4 = v7;
if ( !v7 )
{
fwrite("realloc() failed\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
memcpy(&v7[*(_QWORD *)(a4 + 8)], src, v5);
*(_BYTE *)(*(_QWORD *)a4 + v6) = 0;
*(_QWORD *)(a4 + 8) = v6;
return v5;
}

original_code:
size_t
s3_string_curl_writefunc(void *ptr, size_t len, size_t nmemb, struct s3_string *s) {
 size_t new_len = s->len + len *nmemb;
 s->ptr = realloc(s->ptr, new_len + 1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "realloc() failed\n");
  exit(1);
 }
 memcpy(s->ptr+s->len, ptr, len*nmemb);
 s->ptr[new_len] = '\0';
 s->len = new_len;
 return len*nmemb;
}

canonical_original_code:
unsigned long s3_string_curl_writefunc(void *ptr, unsigned long len, unsigned long nmemb, struct s3_string *s) {
 unsigned long new_len = s->len + len *nmemb;
 s->ptr = realloc(s->ptr, new_len + 1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "realloc() failed\n");
  exit(1);
 }
 memcpy(s->ptr+s->len, ptr, len*nmemb);
 s->ptr[new_len] = '\0';
 s->len = new_len;
 return len*nmemb;
}

variable_types:
ptr
void *

variable_types:
len
unsigned long

variable_types:
nmemb
unsigned long

return_type: unsigned long

user_defined_types: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; }


decompiled_code:
size_t __fastcall s3_string_curl_readfunc(void *a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 v6; // rsi
size_t v7; // rbx

v6 = a4[2];
v7 = a3 * a2;
if ( a4[1] - v6 <= v7 )
v7 = a4[1] - v6;
memcpy(a1, (const void *)(*a4 + v6), v7);
a4[2] += v7;
return v7;
}

original_code:
size_t
s3_string_curl_readfunc(void *ptr, size_t len, size_t nmemb, struct s3_string *s) {
 size_t left = s->len - s->uploaded;
 size_t max_chunk = len * nmemb;
 size_t retcode = left < max_chunk ? left : max_chunk;
 memcpy(ptr, s->ptr + s->uploaded, retcode);
 s->uploaded += retcode;
 return retcode;
}

canonical_original_code:
unsigned long s3_string_curl_readfunc(void *ptr, unsigned long len, unsigned long nmemb, struct s3_string *s) {
 unsigned long left = s->len - s->uploaded;
 unsigned long max_chunk = len * nmemb;
 unsigned long retcode = left < max_chunk ? left : max_chunk;
 memcpy(ptr, s->ptr + s->uploaded, retcode);
 s->uploaded += retcode;
 return retcode;
}

variable_types:
left
unsigned long

variable_types:
retcode
unsigned long


decompiled_code:
_QWORD *s3_string_init()
{
_QWORD *v0; // rbx
_BYTE *v1; // rax

v0 = malloc(0x18uLL);
v0[1] = 0LL;
v0[2] = 0LL;
v1 = malloc(1uLL);
*v0 = v1;
if ( !v1 )
{
fwrite("malloc() failed\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
*v1 = 0;
return v0;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // rbx
_BYTE *v1; // rax

v0 = malloc(0x18uLL);
v0[1] = 0LL;
v0[2] = 0LL;
v1 = malloc(1uLL);
*v0 = v1;
if ( !v1 )
{
fwrite("malloc() failed\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
*v1 = 0;
return v0;
}

original_code:
struct s3_string *
s3_string_init(void) {
 struct s3_string *s;
 s = malloc(sizeof (struct s3_string));
 s->len = 0;
 s->uploaded = 0;
 s->ptr = malloc(s->len+1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "malloc() failed\n");
  exit(1);
 }
 s->ptr[0] = '\0';
 return s;
}

canonical_original_code:
struct s3_string *s3_string_init() {
 struct s3_string *s;
 s = malloc(sizeof (struct s3_string));
 s->len = 0;
 s->uploaded = 0;
 s->ptr = malloc(s->len+1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "malloc() failed\n");
  exit(1);
 }
 s->ptr[0] = '\0';
 return s;
}

return_type: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; } *


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

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

original_code:
void
s3_string_free(struct s3_string *str) {
 free(str->ptr);
 free(str);
}


decompiled_code:
void __fastcall s3_free(void *ptr)
{
free(*((void **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*((void **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void
s3_free(struct S3 *s3) {
 free(s3->id);
 free(s3->secret);
 free(s3->base_url);
 free(s3);
}

user_defined_types: struct S3 { char *secret; char *id; char *base_url; char *proxy; }


decompiled_code:
char *s3_make_date()
{
char *v0; // rbx
struct tm *v1; // rax
struct tm v3; // [rsp+0h] [rbp-48h] BYREF
time_t timer; // [rsp+38h] [rbp-10h] BYREF

v0 = (char *)malloc(0x80uLL);
timer = time(0LL);
v1 = gmtime(&timer);
*(__m128i *)&v3.tm_sec = _mm_loadu_si128((const __m128i *)v1);
*(__m128i *)&v3.tm_mon = _mm_loadu_si128((const __m128i *)&v1->tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_loadu_si128((const __m128i *)&v1->tm_isdst);
v3.tm_zone = v1->tm_zone;
strftime(v0, 0x80uLL, "%a, %d %b %Y %H:%M:%S %Z", &v3);
return v0;
}

canonical_decompiled_code:
char *func3()
{
char *v0; // rbx
struct tm *v1; // rax
struct tm v3; // [rsp+0h] [rbp-48h] BYREF
time_t timer; // [rsp+38h] [rbp-10h] BYREF

v0 = (char *)malloc(0x80uLL);
timer = time(0LL);
v1 = gmtime(&timer);
*(__m128i *)&v3.tm_sec = _mm_loadu_si128((const __m128i *)v1);
*(__m128i *)&v3.tm_mon = _mm_loadu_si128((const __m128i *)&v1->tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_loadu_si128((const __m128i *)&v1->tm_isdst);
v3.tm_zone = v1->tm_zone;
strftime(v0, 0x80uLL, "%a, %d %b %Y %H:%M:%S %Z", &v3);
return v0;
}

original_code:
char *
s3_make_date(void) {
 char *date;
 time_t now;
 struct tm tm;
 date = malloc(128);
 now = time(0);
 tm = *gmtime(&now);
 strftime(date, 128, "%a, %d %b %Y %H:%M:%S %Z", &tm);
 return date;
}

canonical_original_code:
char *s3_make_date() {
 char *date;
 long now;
 struct tm tm;
 date = malloc(128);
 now = time(0);
 tm = *gmtime(&now);
 strftime(date, 128, "%a, %d %b %Y %H:%M:%S %Z", &tm);
 return date;
}

variable_types:
date
char *

variable_types: now long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

return_type: char *


decompiled_code:
void __fastcall s3_get(__int64 a1, const char *a2, const char *a3, __int64 a4)
{
char *v6; // rbx
void *v7; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v6 = s3_make_date();
asprintf(&ptr, "%s\n\n\n%s\n/%s/%s", "GET", v6, a2, a3);
asprintf((char **)&v7, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "GET", (__int64)v7, ptr, v6, a4, 0LL, 0LL, 0LL);
free(ptr);
free(v6);
free(v7);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, const char *a2, const char *a3, __int64 a4)
{
char *v6; // rbx
void *v7; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v6 = func3();
asprintf(&ptr, "%s\n\n\n%s\n/%s/%s", "GET", v6, a2, a3);
asprintf((char **)&v7, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "GET", (__int64)v7, ptr, v6, a4, 0LL, 0LL, 0LL);
free(ptr);
free(v6);
free(v7);
}

original_code:
void
s3_get(struct S3 *s3, const char *bucket, const char *key, struct s3_string *out) {
 const char *method = "GET";
 char *sign_data;
 char *date;
 char *url;
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 free(sign_data);
 free(date);
 free(url);
}

canonical_original_code:
void s3_get(struct S3 *s3, char *bucket, char *key, struct s3_string *out) {
 char *method = "GET";
 char *sign_data;
 char *date;
 char *url;
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 free(sign_data);
 free(date);
 free(url);
}

variable_types:
key
char *

variable_types:
method
char *

variable_types:
url
char *


decompiled_code:
void __fastcall s3_delete(__int64 a1, const char *a2, const char *a3)
{
__int64 v4; // rbp
char *v5; // rbx
void *v6; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v4 = s3_string_init();
v5 = s3_make_date();
asprintf(&ptr, "%s\n\n\n%s\n/%s/%s", "DELETE", v5, a2, a3);
asprintf((char **)&v6, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "DELETE", (__int64)v6, ptr, v5, v4, 0LL, 0LL, 0LL);
s3_string_free(v4);
free(v5);
free(v6);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, const char *a2, const char *a3)
{
__int64 v4; // rbp
char *v5; // rbx
void *v6; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v4 = s3_string_init();
v5 = func3();
asprintf(&ptr, "%s\n\n\n%s\n/%s/%s", "DELETE", v5, a2, a3);
asprintf((char **)&v6, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "DELETE", (__int64)v6, ptr, v5, v4, 0LL, 0LL, 0LL);
s3_string_free(v4);
free(v5);
free(v6);
free(ptr);
}

original_code:
void
s3_delete(struct S3 *s3, const char *bucket, const char *key) {
 char *sign_data;
 char *url;
 char *date;
 const char *method = "DELETE";
 struct s3_string *out;
 out = s3_string_init();
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 s3_string_free(out);
 free(date);
 free(url);
 free(sign_data);
}

canonical_original_code:
void s3_delete(struct S3 *s3, char *bucket, char *key) {
 char *sign_data;
 char *url;
 char *date;
 char *method = "DELETE";
 struct s3_string *out;
 out = s3_string_init();
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 s3_string_free(out);
 free(date);
 free(url);
 free(sign_data);
}


decompiled_code:
void __fastcall s3_put(__int64 a1, const char *a2, const char *a3, const char *a4, const void *a5, size_t a6)
{
__int64 v10; // rbx
void *v11; // rax
char *v12; // r12
char *v13; // rax
char *v14; // rbp
const char *v15; // r8
__int64 v16; // [rsp+18h] [rbp-50h]
char *v17; // [rsp+20h] [rbp-48h] BYREF
char *ptr; // [rsp+28h] [rbp-40h] BYREF

v10 = s3_string_init();
v16 = s3_string_init();
v11 = realloc(*(void **)v10, a6 + 1);
*(_QWORD *)v10 = v11;
memcpy(v11, a5, a6);
*(_QWORD *)(v10 + 8) = a6;
v12 = (char *)s3_md5_sum(a5, a6);
v13 = s3_make_date();
v14 = v13;
v15 = (const char *)&unk_6EA;
if ( a4 )
v15 = a4;
asprintf(&ptr, "%s\n%s\n%s\n%s\n/%s/%s", "PUT", v12, v15, v13, a2, a3);
asprintf(&v17, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "PUT", (__int64)v17, ptr, v14, v16, v10, v12, a4);
s3_string_free(v10);
s3_string_free(v16);
free(v17);
free(v12);
free(v14);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, const char *a2, const char *a3, const char *a4, const void *a5, size_t a6)
{
__int64 v10; // rbx
void *v11; // rax
char *v12; // r12
char *v13; // rax
char *v14; // rbp
const char *v15; // r8
__int64 v16; // [rsp+18h] [rbp-50h]
char *v17; // [rsp+20h] [rbp-48h] BYREF
char *ptr; // [rsp+28h] [rbp-40h] BYREF

v10 = s3_string_init();
v16 = s3_string_init();
v11 = realloc(*(void **)v10, a6 + 1);
*(_QWORD *)v10 = v11;
memcpy(v11, a5, a6);
*(_QWORD *)(v10 + 8) = a6;
v12 = (char *)s3_md5_sum(a5, a6);
v13 = func3();
v14 = v13;
v15 = (const char *)&unk_6EA;
if ( a4 )
v15 = a4;
asprintf(&ptr, "%s\n%s\n%s\n%s\n/%s/%s", "PUT", v12, v15, v13, a2, a3);
asprintf(&v17, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "PUT", (__int64)v17, ptr, v14, v16, v10, v12, a4);
s3_string_free(v10);
s3_string_free(v16);
free(v17);
free(v12);
free(v14);
free(ptr);
}

original_code:
void
s3_put(struct S3 *s3, const char *bucket, const char *key, const char *content_type, const char *data, size_t len) {
 const char *method = "PUT";
 char *sign_data;
 char *date;
 char *url;
 char *md5;
 struct s3_string *in, *out;
 in = s3_string_init();
 out = s3_string_init();
 in->ptr = realloc(in->ptr, len + 1);
 memcpy(in->ptr, data, len);
 in->len = len;
 md5 = s3_md5_sum(data, len);
 date = s3_make_date();
 asprintf(&sign_data, "%s\n%s\n%s\n%s\n/%s/%s", method, md5, content_type ? content_type : "", date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, in, md5, content_type);
 s3_string_free(in);
 s3_string_free(out);
 free(url);
 free(md5);
 free(date);
 free(sign_data);
}

canonical_original_code:
void s3_put(struct S3 *s3, char *bucket, char *key, char *content_type, char *data, unsigned long len) {
 char *method = "PUT";
 char *sign_data;
 char *date;
 char *url;
 char *md5;
 struct s3_string *in; struct s3_string *out;
 in = s3_string_init();
 out = s3_string_init();
 in->ptr = realloc(in->ptr, len + 1);
 memcpy(in->ptr, data, len);
 in->len = len;
 md5 = s3_md5_sum(data, len);
 date = s3_make_date();
 asprintf(&sign_data, "%s\n%s\n%s\n%s\n/%s/%s", method, md5, content_type ? content_type : "", date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, in, md5, content_type);
 s3_string_free(in);
 s3_string_free(out);
 free(url);
 free(md5);
 free(date);
 free(sign_data);
}

variable_types:
data
char *


decompiled_code:
void __fastcall s3_bucket_entry_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
free(ptr);
}

original_code:
void
s3_bucket_entry_free(struct s3_bucket_entry *entry) {
 if (entry->key)
  free(entry->key);
 if (entry->lastmod)
  free(entry->lastmod);
 if (entry->etag)
  free(entry->etag);
 free(entry);
}

variable_types: struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; } *


decompiled_code:
void __fastcall s3_bucket_entries_free(void *ptr)
{
_QWORD *v2; // rdi
__int64 v3; // rax

v2 = *(_QWORD **)ptr;
if ( v2 )
{
do
{
v3 = v2[4];
if ( v3 )
*(_QWORD *)(v3 + 40) = v2[5];
else
*((_QWORD *)ptr + 1) = v2[5];
*(_QWORD *)v2[5] = v2[4];
s3_bucket_entry_free(v2);
v2 = *(_QWORD **)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}

original_code:
void
s3_bucket_entries_free(struct s3_bucket_entry_head *entries) {
 struct s3_bucket_entry *e;
 while ((e = ((entries)->tqh_first)) != ((void *)0)) {
  do { if (((e)->list.tqe_next) != ((void *)0)) (e)->list.tqe_next->list.tqe_prev = (e)->list.tqe_prev; else (entries)->tqh_last = (e)->list.tqe_prev; *(e)->list.tqe_prev = (e)->list.tqe_next; } while ( 0);
  s3_bucket_entry_free(e);
 }
 free(entries);
}

user_defined_types: struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; }


zerr

repo: lgpasquale/qchextractor

decompiled_code:
unsigned int __fastcall zerr(unsigned int a1)
{
unsigned int result; // eax

result = fwrite("zpipe: ", 1uLL, 7uLL, stderr);
if ( a1 >= 0xFFFFFFFA )
{
result = a1 + 6;
switch ( a1 )
{
case 0xFFFFFFFA:
result = fwrite("zlib version mismatch!\n", 1uLL, 0x17uLL, stderr);
break;
case 0xFFFFFFFB:
return result;
case 0xFFFFFFFC:
result = fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
break;
case 0xFFFFFFFD:
result = fwrite("invalid or incomplete deflate data\n", 1uLL, 0x23uLL, stderr);
break;
case 0xFFFFFFFE:
result = fwrite("invalid compression level\n", 1uLL, 0x1AuLL, stderr);
break;
case 0xFFFFFFFF:
if ( ferror(stdin) )
fwrite("error reading stdin\n", 1uLL, 0x14uLL, stderr);
result = ferror(stdout);
if ( result )
result = fwrite("error writing stdout\n", 1uLL, 0x15uLL, stderr);
break;
}
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func9(unsigned int a1)
{
unsigned int result; // eax

result = fwrite("zpipe: ", 1uLL, 7uLL, stderr);
if ( a1 >= 0xFFFFFFFA )
{
result = a1 + 6;
switch ( a1 )
{
case 0xFFFFFFFA:
result = fwrite("zlib version mismatch!\n", 1uLL, 0x17uLL, stderr);
break;
case 0xFFFFFFFB:
return result;
case 0xFFFFFFFC:
result = fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
break;
case 0xFFFFFFFD:
result = fwrite("invalid or incomplete deflate data\n", 1uLL, 0x23uLL, stderr);
break;
case 0xFFFFFFFE:
result = fwrite("invalid compression level\n", 1uLL, 0x1AuLL, stderr);
break;
case 0xFFFFFFFF:
if ( ferror(stdin) )
fwrite("error reading stdin\n", 1uLL, 0x14uLL, stderr);
result = ferror(stdout);
if ( result )
result = fwrite("error writing stdout\n", 1uLL, 0x15uLL, stderr);
break;
}
}
return result;
}

original_code:
void zerr(int ret)
{
    fputs("zpipe: ", stderr);
    switch (ret) {
    case (-1):
        if (ferror(stdin))
            fputs("error reading stdin\n", stderr);
        if (ferror(stdout))
            fputs("error writing stdout\n", stderr);
        break;
    case (-2):
        fputs("invalid compression level\n", stderr);
        break;
    case (-3):
        fputs("invalid or incomplete deflate data\n", stderr);
        break;
    case (-4):
        fputs("out of memory\n", stderr);
        break;
    case (-6):
        fputs("zlib version mismatch!\n", stderr);
    }
}

variable_types: ret int


main

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

if ( argc == 1 )
{
v5 = def(stdin, stdout);
v3 = v5;
if ( !v5 )
return v3;
LABEL_7:
zerr(v5);
return v3;
}
if ( argc == 2 && !strcmp(argv[1], "-d") )
{
v5 = inf(stdin, stdout);
v3 = v5;
if ( !v5 )
return v3;
goto LABEL_7;
}
fwrite("zpipe usage: zpipe [-d] < source > dest\n", 1uLL, 0x28uLL, stderr);
return 1;
}

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

if ( argc == 1 )
{
v5 = func7(stdin, stdout);
v3 = v5;
if ( !v5 )
return v3;
LABEL_7:
func9(v5);
return v3;
}
if ( argc == 2 && !strcmp(argv[1], "-d") )
{
v5 = func8(stdin, stdout);
v3 = v5;
if ( !v5 )
return v3;
goto LABEL_7;
}
fwrite("zpipe usage: zpipe [-d] < source > dest\n", 1uLL, 0x28uLL, stderr);
return 1;
}

original_code:
int main(int argc, char **argv)
{
    int ret;
    ;
    ;
    if (argc == 1) {
        ret = def(stdin, stdout, (-1));
        if (ret != 0)
            zerr(ret);
        return ret;
    }
    else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
        ret = inf(stdin, stdout);
        if (ret != 0)
            zerr(ret);
        return ret;
    }
    else {
        fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
        return 1;
    }
}

variable_types: argc int

variable_types:
argv
char **


repo: lhh/cpglock

decompiled_code:
void *__fastcall do_alloc(size_t n)
{
void *v1; // rax

while ( 1 )
{
v1 = malloc(n);
if ( v1 )
break;
usleep(0x2710u);
}
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func0(size_t n)
{
void *v1; // rax

while ( 1 )
{
v1 = malloc(n);
if ( v1 )
break;
usleep(0x2710u);
}
return memset(v1, 0, n);
}

original_code:
void *
do_alloc(size_t n)
{
 void *p;
 do {
  p = malloc(n);
  if (!p) {
   usleep(10000);
  }
 } while(!p);
 memset(p, 0, n);
 return p;
}

canonical_original_code:
void *do_alloc(unsigned long n)
{
 void *p;
 do {
  p = malloc(n);
  if (!p) {
   usleep(10000);
  }
 } while(!p);
 memset(p, 0, n);
 return p;
}

variable_types:
n
unsigned long

variable_types:
p
void *


decompiled_code:
__int64 __fastcall select_retry(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
unsigned int v8; // ebx

do
v8 = select(nfds, readfds, writefds, exceptfds, timeout);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
unsigned int v8; // ebx

do
v8 = select(nfds, readfds, writefds, exceptfds, timeout);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

original_code:
int
select_retry(int fdmax, fd_set * rfds, fd_set * wfds, fd_set * xfds,
        struct timeval *timeout)
{
 int rv;
 while (1) {
  rv = select(fdmax, rfds, wfds, xfds, timeout);
  if ((rv == -1) && ((*__errno_location ()) == 4))
   continue;
  return rv;
 }
}

canonical_original_code:
int select_retry(int fdmax, struct fd_set *rfds, struct fd_set *wfds, struct fd_set *xfds, struct timeval *timeout)
{
 int rv;
 while (1) {
  rv = select(fdmax, rfds, wfds, xfds, timeout);
  if ((rv == -1) && ((*__errno_location ()) == 4))
   continue;
  return rv;
 }
}

variable_types: fdmax int

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

variable_types:
wfds
struct fd_set *

variable_types:
xfds
struct fd_set *

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

variable_types: rv int


decompiled_code:
size_t __fastcall write_retry(int fd, __int64 a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
int v5; // er14
__int64 v6; // r12
size_t v7; // rbp
int v8; // eax
int v9; // ebx
int v10; // eax
int v11; // eax
size_t v13; // [rsp+0h] [rbp-158h]
size_t v15; // [rsp+18h] [rbp-140h]
fd_set exceptfds; // [rsp+20h] [rbp-138h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-B8h] BYREF

v15 = a3;
if ( !a3 )
return 0LL;
v4 = a3;
v5 = fd / 64;
v6 = 1LL << (fd % 64);
v7 = 0LL;
v13 = 0LL;
while ( 1 )
{
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[v5] |= v6;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v5] |= v6;
v8 = select_retry(fd + 1, 0LL, &writefds, &exceptfds, a4);
v9 = v8;
if ( v8 == -1 )
break;
if ( !v8 )
{
*_errno_location() = 110;
return -1LL;
}
if ( (exceptfds.fds_bits[v5] & v6) != 0 || (v10 = write(fd, (const void *)(a2 + v13), v4)) == 0 && v9 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v10 == -1 )
{
v11 = *_errno_location();
if ( v11 != 11 && v11 != 4 )
return -1LL;
}
else
{
v7 += v10;
v4 -= v10;
}
v13 = v7;
if ( v7 >= v15 )
return v7;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func2(int fd, __int64 a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
int v5; // er14
__int64 v6; // r12
size_t v7; // rbp
int v8; // eax
int v9; // ebx
int v10; // eax
int v11; // eax
size_t v13; // [rsp+0h] [rbp-158h]
size_t v15; // [rsp+18h] [rbp-140h]
fd_set exceptfds; // [rsp+20h] [rbp-138h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-B8h] BYREF

v15 = a3;
if ( !a3 )
return 0LL;
v4 = a3;
v5 = fd / 64;
v6 = 1LL << (fd % 64);
v7 = 0LL;
v13 = 0LL;
while ( 1 )
{
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[v5] |= v6;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v5] |= v6;
v8 = func1(fd + 1, 0LL, &writefds, &exceptfds, a4);
v9 = v8;
if ( v8 == -1 )
break;
if ( !v8 )
{
*_errno_location() = 110;
return -1LL;
}
if ( (exceptfds.fds_bits[v5] & v6) != 0 || (v10 = write(fd, (const void *)(a2 + v13), v4)) == 0 && v9 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v10 == -1 )
{
v11 = *_errno_location();
if ( v11 != 11 && v11 != 4 )
return -1LL;
}
else
{
v7 += v10;
v4 -= v10;
}
v13 = v7;
if ( v7 >= v15 )
return v7;
}
return -1LL;
}

original_code:
ssize_t
write_retry(int fd, void *buf, size_t count, struct timeval * timeout)
{
 int n, rv = 0;
 ssize_t total = 0, remain = count;
 fd_set wfds, xfds;
 while (total < count) {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&wfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(fd + 1, ((void *)0), &wfds, &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = write(fd, (char *)buf + (off_t) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

canonical_original_code:
long write_retry(int fd, void *buf, unsigned long count, struct timeval *timeout)
{
 int n; int rv = 0;
 long total = 0; long remain = count;
 struct fd_set wfds; struct fd_set xfds;
 while (total < count) {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&wfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(fd + 1, ((void *)0), &wfds, &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = write(fd, (char *)buf + (long) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n int

variable_types: total long

variable_types: remain long

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

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

return_type: long

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


decompiled_code:
size_t __fastcall read_retry(int fd, void *s, size_t nbytes, struct timeval *a4)
{
size_t v4; // rbx
size_t v5; // r15
int v6; // er14
__int64 v7; // r12
size_t v8; // rbp
int v9; // eax
int v10; // ebx
int v11; // eax
int v12; // eax
size_t v14; // [rsp+0h] [rbp-158h]
size_t v16; // [rsp+18h] [rbp-140h]
fd_set exceptfds; // [rsp+20h] [rbp-138h] BYREF
fd_set readfds; // [rsp+A0h] [rbp-B8h] BYREF

v4 = nbytes;
v16 = nbytes;
v5 = nbytes;
memset(s, 0, nbytes);
if ( !v4 )
return 0LL;
v6 = fd / 64;
v7 = 1LL << (fd % 64);
v8 = 0LL;
v14 = 0LL;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v6] |= v7;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v6] |= v7;
v9 = select_retry(fd + 1, &readfds, 0LL, &exceptfds, a4);
v10 = v9;
if ( v9 == -1 )
break;
if ( !v9 )
{
*_errno_location() = 110;
return -1LL;
}
if ( (exceptfds.fds_bits[v6] & v7) != 0 || (v11 = read(fd, (char *)s + v14, v5)) == 0 && v10 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v11 == -1 )
{
v12 = *_errno_location();
if ( v12 != 11 && v12 != 4 )
return -1LL;
}
else
{
v8 += v11;
v5 -= v11;
}
v14 = v8;
if ( v8 >= v16 )
return v8;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func3(int fd, void *s, size_t nbytes, struct timeval *a4)
{
size_t v4; // rbx
size_t v5; // r15
int v6; // er14
__int64 v7; // r12
size_t v8; // rbp
int v9; // eax
int v10; // ebx
int v11; // eax
int v12; // eax
size_t v14; // [rsp+0h] [rbp-158h]
size_t v16; // [rsp+18h] [rbp-140h]
fd_set exceptfds; // [rsp+20h] [rbp-138h] BYREF
fd_set readfds; // [rsp+A0h] [rbp-B8h] BYREF

v4 = nbytes;
v16 = nbytes;
v5 = nbytes;
memset(s, 0, nbytes);
if ( !v4 )
return 0LL;
v6 = fd / 64;
v7 = 1LL << (fd % 64);
v8 = 0LL;
v14 = 0LL;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v6] |= v7;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v6] |= v7;
v9 = func1(fd + 1, &readfds, 0LL, &exceptfds, a4);
v10 = v9;
if ( v9 == -1 )
break;
if ( !v9 )
{
*_errno_location() = 110;
return -1LL;
}
if ( (exceptfds.fds_bits[v6] & v7) != 0 || (v11 = read(fd, (char *)s + v14, v5)) == 0 && v10 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v11 == -1 )
{
v12 = *_errno_location();
if ( v12 != 11 && v12 != 4 )
return -1LL;
}
else
{
v8 += v11;
v5 -= v11;
}
v14 = v8;
if ( v8 >= v16 )
return v8;
}
return -1LL;
}

original_code:
ssize_t
read_retry(int sockfd, void *buf, size_t count, struct timeval * timeout)
{
 int n, rv = 0;
 ssize_t total = 0, remain = count;
 fd_set rfds, xfds;
 memset(buf, 0, count);
 while (total < count) {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(sockfd + 1, &rfds, ((void *)0), &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = read(sockfd, (char *)buf + (off_t) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

canonical_original_code:
long read_retry(int sockfd, void *buf, unsigned long count, struct timeval *timeout)
{
 int n; int rv = 0;
 long total = 0; long remain = count;
 struct fd_set rfds; struct fd_set xfds;
 memset(buf, 0, count);
 while (total < count) {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(sockfd + 1, &rfds, ((void *)0), &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = read(sockfd, (char *)buf + (long) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

variable_types: sockfd int

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


decompiled_code:
__int64 __fastcall sock_listen(const char *a1)
{
int v1; // eax
unsigned int v2; // ebx
__mode_t v3; // ebp
struct sockaddr v5[9]; // [rsp+0h] [rbp-98h] BYREF

v1 = socket(1, 1, 0);
v2 = -1;
if ( v1 >= 0 )
{
v2 = v1;
v5[0].sa_family = 1;
snprintf(v5[0].sa_data, 0x6CuLL, "%s", a1);
unlink(v5[0].sa_data);
v3 = umask(0x3Fu);
if ( bind(v2, v5, 0x6Eu) < 0 )
{
umask(v3);
}
else
{
umask(v3);
if ( listen(v2, 128) >= 0 )
return v2;
}
close(v2);
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
int v1; // eax
unsigned int v2; // ebx
__mode_t v3; // ebp
struct sockaddr v5[9]; // [rsp+0h] [rbp-98h] BYREF

v1 = socket(1, 1, 0);
v2 = -1;
if ( v1 >= 0 )
{
v2 = v1;
v5[0].sa_family = 1;
snprintf(v5[0].sa_data, 0x6CuLL, "%s", a1);
unlink(v5[0].sa_data);
v3 = umask(0x3Fu);
if ( bind(v2, v5, 0x6Eu) < 0 )
{
umask(v3);
}
else
{
umask(v3);
if ( listen(v2, 128) >= 0 )
return v2;
}
close(v2);
v2 = -1;
}
return v2;
}

original_code:
int
sock_listen(const char *sockpath)
{
 int sock = -1;
       struct sockaddr_un su;
 mode_t om;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  goto fail;
 su.sun_family = 1;
 snprintf(su.sun_path, (sizeof(su.sun_path)), "%s", sockpath);
 unlink(su.sun_path);
 om = umask(077);
 if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
  umask(om);
  goto fail;
 }
 umask(om);
 if (listen(sock, 128) < 0)
  goto fail;
 return sock;
fail:
       if (sock >= 0)
  close(sock);
 return -1;
}

canonical_original_code:
int sock_listen(char *sockpath)
{
 int sock = -1;
       struct sockaddr_un su;
 unsigned int om;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  goto fail;
 su.sun_family = 1;
 snprintf(su.sun_path, (sizeof(su.sun_path)), "%s", sockpath);
 unlink(su.sun_path);
 om = umask(077);
 if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
  umask(om);
  goto fail;
 }
 umask(om);
 if (listen(sock, 128) < 0)
  goto fail;
 return sock;
fail:
       if (sock >= 0)
  close(sock);
 return -1;
}

variable_types:
sockpath
char *

variable_types: sock int

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

variable_types:
om
unsigned int

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


decompiled_code:
__int64 __fastcall sock_connect(const char *a1, int a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // eax
int v5; // eax
int v7; // ebp
__int64 v8; // r12
fd_set v9; // [rsp+0h] [rbp-1A8h] BYREF
fd_set readfds; // [rsp+80h] [rbp-128h] BYREF
struct sockaddr addr; // [rsp+100h] [rbp-A8h] BYREF
socklen_t optlen; // [rsp+178h] [rbp-30h] BYREF
char optval[4]; // [rsp+17Ch] [rbp-2Ch] BYREF
struct timeval timeout; // [rsp+180h] [rbp-28h] BYREF

timeout.tv_sec = a2;
timeout.tv_usec = 0LL;
v2 = socket(1, 1, 0);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
addr.sa_family = 1;
snprintf(addr.sa_data, 0x6CuLL, "%s", a1);
v4 = fcntl(v3, 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v3, 4, v4);
v5 = connect(v3, &addr, 0x6Eu);
if ( v5 < 0 )
{
if ( *_errno_location() != 115 )
{
close(v3);
return (unsigned int)-1;
}
}
else if ( !v5 )
{
return (unsigned int)v3;
}
memset(&readfds, 0, sizeof(readfds));
v7 = v3 >> 6;
v8 = 1LL << (v3 % 64);
readfds.fds_bits[v3 >> 6] |= v8;
*(__m128i *)v9.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
*(__m128i *)&v9.fds_bits[2] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
*(__m128i *)&v9.fds_bits[4] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
*(__m128i *)&v9.fds_bits[6] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
*(__m128i *)&v9.fds_bits[8] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)&v9.fds_bits[10] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
*(__m128i *)&v9.fds_bits[12] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
*(__m128i *)&v9.fds_bits[14] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
if ( select(v3 + 1, &readfds, &v9, 0LL, &timeout) )
{
if ( (readfds.fds_bits[v7] & v8) == 0 && (v9.fds_bits[v7] & v8) == 0
|| (optlen = 4, getsockopt(v3, 1, 4, optval, &optlen) < 0) )
{
close(v3);
v3 = -1;
}
}
else
{
close(v3);
*_errno_location() = 110;
v3 = -1;
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, int a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // eax
int v5; // eax
int v7; // ebp
__int64 v8; // r12
fd_set v9; // [rsp+0h] [rbp-1A8h] BYREF
fd_set readfds; // [rsp+80h] [rbp-128h] BYREF
struct sockaddr addr; // [rsp+100h] [rbp-A8h] BYREF
socklen_t optlen; // [rsp+178h] [rbp-30h] BYREF
char optval[4]; // [rsp+17Ch] [rbp-2Ch] BYREF
struct timeval timeout; // [rsp+180h] [rbp-28h] BYREF

timeout.tv_sec = a2;
timeout.tv_usec = 0LL;
v2 = socket(1, 1, 0);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
addr.sa_family = 1;
snprintf(addr.sa_data, 0x6CuLL, "%s", a1);
v4 = fcntl(v3, 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v3, 4, v4);
v5 = connect(v3, &addr, 0x6Eu);
if ( v5 < 0 )
{
if ( *_errno_location() != 115 )
{
close(v3);
return (unsigned int)-1;
}
}
else if ( !v5 )
{
return (unsigned int)v3;
}
memset(&readfds, 0, sizeof(readfds));
v7 = v3 >> 6;
v8 = 1LL << (v3 % 64);
readfds.fds_bits[v3 >> 6] |= v8;
*(__m128i *)v9.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
*(__m128i *)&v9.fds_bits[2] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
*(__m128i *)&v9.fds_bits[4] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
*(__m128i *)&v9.fds_bits[6] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
*(__m128i *)&v9.fds_bits[8] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)&v9.fds_bits[10] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
*(__m128i *)&v9.fds_bits[12] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
*(__m128i *)&v9.fds_bits[14] = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
if ( select(v3 + 1, &readfds, &v9, 0LL, &timeout) )
{
if ( (readfds.fds_bits[v7] & v8) == 0 && (v9.fds_bits[v7] & v8) == 0
|| (optlen = 4, getsockopt(v3, 1, 4, optval, &optlen) < 0) )
{
close(v3);
v3 = -1;
}
}
else
{
close(v3);
*_errno_location() = 110;
v3 = -1;
}
return (unsigned int)v3;
}

original_code:
int
sock_connect(const char *sockpath, int tout)
{
 struct timeval timeout = {tout, 0};
 int sock, flags, error, ret;
 socklen_t len;
 struct sockaddr_un sun;
 fd_set rfds, wfds;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  return -1;
 sun.sun_family = 1;
 snprintf(sun.sun_path, (sizeof(sun.sun_path)), "%s", sockpath);
 flags = fcntl(sock, 3, 0);
 fcntl(sock, 4, flags | 04000);
        ret = connect(sock, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0 && ((*__errno_location ()) != 115)) {
  close(sock);
  return -1;
 }
 if (ret == 0)
  goto done;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 wfds = rfds;
 ret = select(sock + 1, &rfds, &wfds, ((void *)0), &timeout);
 if (ret == 0) {
  close(sock);
  (*__errno_location ()) = 110;
  return -1;
 }
 if (((((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0) || ((((&wfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  len = sizeof (error);
  if (getsockopt(sock, 1, 4, &error, &len) < 0) {
   close(sock);
   return -1;
  }
 } else {
  close(sock);
  return -1;
 }
done:
 return sock;
}

canonical_original_code:
int sock_connect(char *sockpath, int tout)
{
 struct timeval timeout = {tout, 0};
 int sock; int flags; int error; int ret;
 unsigned int len;
 struct sockaddr_un sun;
 struct fd_set rfds; struct fd_set wfds;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  return -1;
 sun.sun_family = 1;
 snprintf(sun.sun_path, (sizeof(sun.sun_path)), "%s", sockpath);
 flags = fcntl(sock, 3, 0);
 fcntl(sock, 4, flags | 04000);
        ret = connect(sock, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0 && ((*__errno_location ()) != 115)) {
  close(sock);
  return -1;
 }
 if (ret == 0)
  goto done;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 wfds = rfds;
 ret = select(sock + 1, &rfds, &wfds, ((void *)0), &timeout);
 if (ret == 0) {
  close(sock);
  (*__errno_location ()) = 110;
  return -1;
 }
 if (((((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0) || ((((&wfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  len = sizeof (error);
  if (getsockopt(sock, 1, 4, &error, &len) < 0) {
   close(sock);
   return -1;
  }
 } else {
  close(sock);
  return -1;
 }
done:
 return sock;
}

variable_types: tout int

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

variable_types: flags int

variable_types: error int

variable_types: ret int

variable_types:
len
unsigned int

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


decompiled_code:
int __fastcall sock_accept(int fd)
{
int result; // eax

if ( fd < 0 )
{
*_errno_location() = 9;
result = -1;
}
else
{
while ( 1 )
{
result = accept(fd, 0LL, 0LL);
if ( result >= 0 )
break;
if ( *_errno_location() != 4 )
return -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd)
{
int result; // eax

if ( fd < 0 )
{
*_errno_location() = 9;
result = -1;
}
else
{
while ( 1 )
{
result = accept(fd, 0LL, 0LL);
if ( result >= 0 )
break;
if ( *_errno_location() != 4 )
return -1;
}
}
return result;
}

original_code:
int
sock_accept(int sockfd)
{
 int acceptfd;
 if (sockfd < 0) {
  (*__errno_location ()) = 9;
  return -1;
 }
 while ((acceptfd =
  accept(sockfd, (struct sockaddr *) ((void *)0), ((void *)0))) < 0) {
  if ((*__errno_location ()) == 4) {
   continue;
  }
  return -1;
 }
 return acceptfd;
}

variable_types: acceptfd int


hexdump

decompiled_code:
int __fastcall hexdump(unsigned __int8 *a1, __int64 a2)
{
__int64 v2; // rbx

printf("%d bytes @ %p \n", a2, a1);
if ( a2 )
{
printf(" %02x", *a1);
v2 = 1LL;
while ( a2 != v2 )
{
printf(" %02x", a1[v2++]);
if ( (v2 & 0xF) == 0 )
putchar(10);
}
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int8 *a1, __int64 a2)
{
__int64 v2; // rbx

printf("%d bytes @ %p \n", a2, a1);
if ( a2 )
{
printf(" %02x", *a1);
v2 = 1LL;
while ( a2 != v2 )
{
printf(" %02x", a1[v2++]);
if ( (v2 & 0xF) == 0 )
putchar(10);
}
}
return putchar(10);
}

original_code:
void
hexdump(const void *buf, size_t len)
{
 size_t x;
 printf("%d bytes @ %p \n", (int)len, buf);
 for (x = 0; x < len; x++) {
  printf(" %02x", (((char *)buf)[x])&0xff);
  if (((x+1) % 16) == 0)
   printf("\n");
 }
 printf("\n");
}

canonical_original_code:
void hexdump(void *buf, unsigned long len)
{
 unsigned long x;
 printf("%d bytes @ %p \n", (int)len, buf);
 for (x = 0; x < len; x++) {
  printf(" %02x", (((char *)buf)[x])&0xff);
  if (((x+1) % 16) == 0)
   printf("\n");
 }
 printf("\n");
}

variable_types:
buf
void *

variable_types:
len
unsigned long

variable_types:
x
unsigned long


inter

decompiled_code:
void inter()
{
running = 0;
}

canonical_decompiled_code:
void func0()
{
running = 0;
}

original_code:
static void
inter(int sig)
{
 running = 0;
}

variable_types: sig int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
int result; // eax
_BYTE v5[120]; // [rsp+0h] [rbp-98h] BYREF
__int64 v6[4]; // [rsp+78h] [rbp-20h] BYREF

if ( argc <= 1 )
{
printf("usage: %s lockname\n", *argv);
result = 1;
}
else
{
signal(2, (__sighandler_t)inter);
if ( (int)cpg_lock_init(v6) < 0 )
{
perror("cpg_lock_init");
result = 1;
}
else
{
v3 = running;
if ( running )
{
v3 = 0;
while ( 1 )
{
printf("lock ... ");
fflush(stdout);
if ( (int)cpg_lock(v6[0], argv[1], 0LL, v5) < 0 )
{
perror("cpg_lock");
return 1;
}
printf("unlock");
if ( (int)cpg_unlock(v6[0], v5) < 0 )
break;
++v3;
putchar(10);
if ( !running )
goto LABEL_8;
}
perror("cpg_unlock");
result = 1;
}
else
{
LABEL_8:
cpg_lock_fin(v6[0]);
printf("%s taken/released %d times\n", *argv, v3);
result = 0;
}
}
}
return result;
}

original_code:
int
main(int argc, char **argv)
{
 cpg_lock_handle_t h;
 struct cpg_lock l;
 int n = 0;
 if (argc < 2) {
  printf("usage: %s lockname\n", argv[0]);
  return 1;
 }
 signal(2, inter);
 if (cpg_lock_init(&h) < 0) {
  perror("cpg_lock_init");
  return 1;
 }
 while(running) {
  printf("lock ... ");
  fflush(stdout);
  if (cpg_lock(h, argv[1], 0, &l) < 0) {
   perror("cpg_lock");
   return 1;
  }
  printf("unlock");
  if (cpg_unlock(h, &l) < 0) {
   perror("cpg_unlock");
   return 1;
  }
  ++n;
  printf("\n");
 }
 cpg_lock_fin(h);
 printf("%s taken/released %d times\n", argv[0], n);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 void *h;
 struct cpg_lock l;
 int n = 0;
 if (argc < 2) {
  printf("usage: %s lockname\n", argv[0]);
  return 1;
 }
 signal(2, inter);
 if (cpg_lock_init(&h) < 0) {
  perror("cpg_lock_init");
  return 1;
 }
 while(running) {
  printf("lock ... ");
  fflush(stdout);
  if (cpg_lock(h, argv[1], 0, &l) < 0) {
   perror("cpg_lock");
   return 1;
  }
  printf("unlock");
  if (cpg_unlock(h, &l) < 0) {
   perror("cpg_unlock");
   return 1;
  }
  ++n;
  printf("\n");
 }
 cpg_lock_fin(h);
 printf("%s taken/released %d times\n", argv[0], n);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
h
void *

variable_types: struct cpg_lock { char resource[96]; int local_fd; int local_id; enum lock_state_t { LOCK_FREE=0, LOCK_PENDING=1, LOCK_HELD=2 } state; int owner_nodeid; int owner_pid; int owner_tid; }

variable_types: n int

user_defined_types: struct cpg_lock { char resource[96]; int local_fd; int local_id; enum lock_state_t state; int owner_nodeid; int owner_pid; int owner_tid; }


decompiled_code:
int setup_sigmask()
{
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigfillset(&v1);
sigdelset(&v1, 3);
sigdelset(&v1, 4);
sigdelset(&v1, 5);
sigdelset(&v1, 6);
sigdelset(&v1, 8);
sigdelset(&v1, 11);
sigdelset(&v1, 7);
return sigprocmask(0, &v1, 0LL);
}

canonical_decompiled_code:
int func1()
{
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

sigfillset(&v1);
sigdelset(&v1, 3);
sigdelset(&v1, 4);
sigdelset(&v1, 5);
sigdelset(&v1, 6);
sigdelset(&v1, 8);
sigdelset(&v1, 11);
sigdelset(&v1, 7);
return sigprocmask(0, &v1, 0LL);
}

original_code:
static int
setup_sigmask(void)
{
 sigset_t set;
 sigfillset(&set);
 sigdelset(&set, 3);
 sigdelset(&set, 4);
 sigdelset(&set, 5);
 sigdelset(&set, 6);
 sigdelset(&set, 8);
 sigdelset(&set, 11);
 sigdelset(&set, 7);
 return (sigprocmask(0, &set, ((void *)0)));
}

canonical_original_code:
static int setup_sigmask()
{
 struct __sigset_t set;
 sigfillset(&set);
 sigdelset(&set, 3);
 sigdelset(&set, 4);
 sigdelset(&set, 5);
 sigdelset(&set, 6);
 sigdelset(&set, 8);
 sigdelset(&set, 11);
 sigdelset(&set, 7);
 return (sigprocmask(0, &set, ((void *)0)));
}

variable_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
int __fastcall daemon_init(char *path)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( getuid() )
{
fwrite("daemon_init: Sorry, only root wants to run this.\n", 1uLL, 0x31uLL, stderr);
exit(1);
}
if ( check_process_running(path, v2) && getpid() != v2[0] )
{
fprintf(stderr, "daemon_init: Process \"%s\" already running.\n", path);
exit(1);
}
if ( setup_sigmask() < 0 )
{
fwrite("daemon_init: Unable to set signal mask.\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
if ( daemon(0, 0) )
{
fwrite("daemon_init: Unable to daemonize.\n", 1uLL, 0x22uLL, stderr);
exit(1);
}
update_pidfile(path);
result = nice(-1);
if ( result < 0 )
result = fwrite("daemon_init: Unable to renice.\n", 1uLL, 0x1FuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func4(char *path)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( getuid() )
{
fwrite("daemon_init: Sorry, only root wants to run this.\n", 1uLL, 0x31uLL, stderr);
exit(1);
}
if ( func3(path, v2) && getpid() != v2[0] )
{
fprintf(stderr, "daemon_init: Process \"%s\" already running.\n", path);
exit(1);
}
if ( func1() < 0 )
{
fwrite("daemon_init: Unable to set signal mask.\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
if ( daemon(0, 0) )
{
fwrite("daemon_init: Unable to daemonize.\n", 1uLL, 0x22uLL, stderr);
exit(1);
}
func0(path);
result = nice(-1);
if ( result < 0 )
result = fwrite("daemon_init: Unable to renice.\n", 1uLL, 0x1FuLL, stderr);
return result;
}

original_code:
void
daemon_init(char *prog)
{
 uid_t uid;
 pid_t pid;
 uid = getuid();
 if (uid) {
  fprintf(stderr,
   "daemon_init: Sorry, only root wants to run this.\n");
  exit(1);
 }
 if (check_process_running(prog, &pid) && (pid != getpid())) {
  fprintf(stderr,
   "daemon_init: Process \"%s\" already running.\n",
   prog);
  exit(1);
 }
 if (setup_sigmask() < 0) {
  fprintf(stderr, "daemon_init: Unable to set signal mask.\n");
  exit(1);
 }
 if (daemon(0, 0)) {
  fprintf(stderr, "daemon_init: Unable to daemonize.\n");
  exit(1);
 }
 update_pidfile(prog);
 if (nice(-1) < 0)
  fprintf(stderr, "daemon_init: Unable to renice.\n");
}

canonical_original_code:
void daemon_init(char *prog)
{
 unsigned int uid;
 int pid;
 uid = getuid();
 if (uid) {
  fprintf(stderr,
   "daemon_init: Sorry, only root wants to run this.\n");
  exit(1);
 }
 if (check_process_running(prog, &pid) && (pid != getpid())) {
  fprintf(stderr,
   "daemon_init: Process \"%s\" already running.\n",
   prog);
  exit(1);
 }
 if (setup_sigmask() < 0) {
  fprintf(stderr, "daemon_init: Unable to set signal mask.\n");
  exit(1);
 }
 if (daemon(0, 0)) {
  fprintf(stderr, "daemon_init: Unable to daemonize.\n");
  exit(1);
 }
 update_pidfile(prog);
 if (nice(-1) < 0)
  fprintf(stderr, "daemon_init: Unable to renice.\n");
}

variable_types:
prog
char *

variable_types:
uid
unsigned int

variable_types: pid int


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

if ( pid_filename )
{
unlink(&pid_filename);
result = 0LL;
memset(&pid_filename, 0, 0x1000uLL);
}
return result;
}

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

if ( pid_filename )
{
unlink(&pid_filename);
result = 0LL;
memset(&pid_filename, 0, 0x1000uLL);
}
return result;
}

original_code:
void
daemon_cleanup(void)
{
 if (!strlen(pid_filename))
  return;
 unlink(pid_filename);
 memset(pid_filename, 0, sizeof(pid_filename));
}

canonical_original_code:
void daemon_cleanup()
{
 if (!strlen(pid_filename))
  return;
 unlink(pid_filename);
 memset(pid_filename, 0, sizeof(pid_filename));
}


original_code:
static int
check_pending(struct pending_node **l,
       struct cpg_lock_msg *exp,
       struct cpg_lock_msg *ret)
{
 struct pending_node *p;
 int x;
 if (((x=0) == 0) && (*l != ((void *)0))) for (p = *l; (x == 0) || (p != *l); p = (void*)(&((*p)._list_head))->le_next, x++) {
  if (!strcmp(exp->resource, p->m.resource) &&
      exp->owner_tid == p->m.owner_tid) {
   do { if ((&((*p)._list_head)) == (&((**l)._list_head))) { *l = (void *)(&((**l)._list_head))->le_next; } if ((&((*p)._list_head)) == (&((**l)._list_head))) { (&((*p)._list_head))->le_next = ((void *)0); (&((*p)._list_head))->le_prev = ((void *)0); *l = ((void *)0); } else { (&((*p)._list_head))->le_next->le_prev = (&((*p)._list_head))->le_prev; (&((*p)._list_head))->le_prev->le_next = (&((*p)._list_head))->le_next; (&((*p)._list_head))->le_prev = ((void *)0); (&((*p)._list_head))->le_next = ((void *)0); } } while (0);
   memcpy(ret, &p->m, sizeof(*ret));
   free(p);
   return 0;
  }
 }
 return 1;
}

canonical_original_code:
static int check_pending(struct pending_node **l, struct cpg_lock_msg *exp, struct cpg_lock_msg *ret)
{
 struct pending_node *p;
 int x;
 if (((x=0) == 0) && (*l != ((void *)0))) for (p = *l; (x == 0) || (p != *l); p = (void *)(&((*p)._list_head))->le_next, x++) {
  if (!strcmp(exp->resource, p->m.resource) &&
      exp->owner_tid == p->m.owner_tid) {
   do { if ((&((*p)._list_head)) == (&((**l)._list_head))) { *l = (void *)(&((**l)._list_head))->le_next; } if ((&((*p)._list_head)) == (&((**l)._list_head))) { (&((*p)._list_head))->le_next = ((void *)0); (&((*p)._list_head))->le_prev = ((void *)0); *l = ((void *)0); } else { (&((*p)._list_head))->le_next->le_prev = (&((*p)._list_head))->le_prev; (&((*p)._list_head))->le_prev->le_next = (&((*p)._list_head))->le_next; (&((*p)._list_head))->le_prev = ((void *)0); (&((*p)._list_head))->le_next = ((void *)0); } } while (0);
   memcpy(ret, &p->m, sizeof(*ret));
   free(p);
   return 0;
  }
 }
 return 1;
}

variable_types: struct pending_node { struct list_entry { struct list_entry *le_next; } _list_head; struct cpg_lock_msg { char resource[96]; int request; unsigned int owner_nodeid; unsigned int owner_pid; unsigned int flags; unsigned int lockid; unsigned int owner_tid; char pad[8]; } m; } **

variable_types:
exp
struct cpg_lock_msg *

variable_types:
ret
struct cpg_lock_msg *

variable_types: x int

user_defined_types: struct pending_node { struct list_entry _list_head; struct cpg_lock_msg m; }


decompiled_code:
__int64 __fastcall cpg_lock_init(pthread_mutex_t **a1)
{
__int64 v1; // rax
pthread_mutex_t *v2; // rbx
int v3; // eax
__int64 result; // rax
int *v5; // rbp
int v6; // er12

v1 = do_alloc(64LL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (pthread_mutex_t *)v1;
v3 = sock_connect("/var/run/cpglockd.sk", 3LL);
v2[1].__owner = v3;
if ( v3 < 0 )
{
v5 = _errno_location();
v6 = *v5;
free(v2);
*v5 = v6;
result = 0xFFFFFFFFLL;
}
else
{
v2[1].__nusers = getpid();
pthread_mutex_init(v2, 0LL);
*a1 = v2;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(pthread_mutex_t **a1)
{
__int64 v1; // rax
pthread_mutex_t *v2; // rbx
int v3; // eax
__int64 result; // rax
int *v5; // rbp
int v6; // er12

v1 = do_alloc(64LL);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (pthread_mutex_t *)v1;
v3 = sock_connect("/var/run/cpglockd.sk", 3LL);
v2[1].__owner = v3;
if ( v3 < 0 )
{
v5 = _errno_location();
v6 = *v5;
free(v2);
*v5 = v6;
result = 0xFFFFFFFFLL;
}
else
{
v2[1].__nusers = getpid();
pthread_mutex_init(v2, 0LL);
*a1 = v2;
result = 0LL;
}
return result;
}

original_code:
int
cpg_lock_init(void **handle)
{
 struct cpg_lock_handle *h;
 int esv;
 h = do_alloc(sizeof (*h));
 if (!h)
  return -1;
 h->fd = sock_connect("/var/run/cpglockd.sk", 3);
 if (h->fd < 0) {
  esv = (*__errno_location ());
  free(h);
  (*__errno_location ()) = esv;
  return -1;
 }
 h->pid = getpid();
 pthread_mutex_init(&h->mutex, ((void *)0));
 *handle = (void *)h;
 return 0;
}

variable_types:
handle
void **

variable_types: struct cpg_lock_handle { 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; struct pending_node { struct list_entry { struct list_entry *le_next; } _list_head; struct cpg_lock_msg { char resource[96]; int request; unsigned int owner_nodeid; unsigned int owner_pid; unsigned int flags; unsigned int lockid; unsigned int owner_tid; char pad[8]; } m; } *pending; int fd; int pid; int seq; } *

variable_types: esv int

user_defined_types: struct cpg_lock_handle { union pthread_mutex_t mutex; struct pending_node *pending; int fd; int pid; int seq; }

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 cpg_lock(pthread_mutex_t *mutex, char *src, int a3, __int64 a4)
{
unsigned int v6; // er14
unsigned int v7; // ebp
int v9; // eax
__int32 v10; // eax
__int16 v11; // ax
__int16 v12; // ax
__int64 v13[2]; // [rsp+0h] [rbp-138h] BYREF
__m128i s1[8]; // [rsp+10h] [rbp-128h] BYREF
char dest[96]; // [rsp+90h] [rbp-A8h] BYREF
__int64 v16; // [rsp+F0h] [rbp-48h]
__int64 v17; // [rsp+F8h] [rbp-40h]
__int64 v18; // [rsp+100h] [rbp-38h]
__int64 v19; // [rsp+108h] [rbp-30h]

if ( mutex )
{
pthread_mutex_lock(mutex);
v6 = mutex[1].__nusers;
if ( v6 == getpid() )
{
if ( strlen(src) <= 0x5F )
{
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
memset(s1, 0, sizeof(s1));
strncpy(dest, src, 0x60uLL);
strncpy((char *)a4, src, 0x60uLL);
LODWORD(v17) = mutex[1].__nusers;
v9 = mutex[1].__kind + 1;
mutex[1].__kind = v9;
HIDWORD(v18) = v9;
LODWORD(v16) = 1;
HIDWORD(v17) = a3;
v7 = -1;
if ( write_retry((unsigned int)mutex[1].__owner, dest, 128LL, 0LL) >= 0 )
{
while ( (unsigned int)check_pending((__m128i **)&mutex[1], (__int64)dest, s1) )
{
v13[0] = 0LL;
v13[1] = random() & 0x3FFF;
if ( read_retry((unsigned int)mutex[1].__owner, s1, 128LL, v13) < 0 )
{
if ( *_errno_location() != 110 )
{
v7 = -1;
goto LABEL_4;
}
pthread_mutex_unlock(mutex);
v11 = random();
usleep(v11 & 0x3FFF);
pthread_mutex_lock(mutex);
}
else
{
if ( !strcmp((const char *)s1, dest) && s1[7].m128i_i32[1] == HIDWORD(v18) )
break;
add_pending((__int64)&mutex[1], s1);
pthread_mutex_unlock(mutex);
v12 = random();
usleep(v12 & 0x3FFF);
pthread_mutex_lock(mutex);
}
}
v10 = s1[6].m128i_i32[1];
v7 = -1;
if ( s1[6].m128i_i32[1] )
{
if ( s1[6].m128i_i32[0] == 2 )
{
*_errno_location() = 11;
}
else
{
v7 = -1;
if ( s1[6].m128i_i32[0] == 3 )
{
*(_DWORD *)(a4 + 104) = 2;
*(_DWORD *)(a4 + 108) = v10;
*(_DWORD *)(a4 + 112) = mutex[1].__nusers;
*(_DWORD *)(a4 + 100) = s1[7].m128i_i32[0];
v7 = 0;
}
}
}
}
}
else
{
*_errno_location() = 36;
v7 = -1;
}
}
else
{
*_errno_location() = 9;
v7 = -1;
}
LABEL_4:
pthread_mutex_unlock(mutex);
}
else
{
*_errno_location() = 22;
v7 = -1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(pthread_mutex_t *mutex, char *src, int a3, __int64 a4)
{
unsigned int v6; // er14
unsigned int v7; // ebp
int v9; // eax
__int32 v10; // eax
__int16 v11; // ax
__int16 v12; // ax
__int64 v13[2]; // [rsp+0h] [rbp-138h] BYREF
__m128i s1[8]; // [rsp+10h] [rbp-128h] BYREF
char dest[96]; // [rsp+90h] [rbp-A8h] BYREF
__int64 v16; // [rsp+F0h] [rbp-48h]
__int64 v17; // [rsp+F8h] [rbp-40h]
__int64 v18; // [rsp+100h] [rbp-38h]
__int64 v19; // [rsp+108h] [rbp-30h]

if ( mutex )
{
pthread_mutex_lock(mutex);
v6 = mutex[1].__nusers;
if ( v6 == getpid() )
{
if ( strlen(src) <= 0x5F )
{
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
memset(s1, 0, sizeof(s1));
strncpy(dest, src, 0x60uLL);
strncpy((char *)a4, src, 0x60uLL);
LODWORD(v17) = mutex[1].__nusers;
v9 = mutex[1].__kind + 1;
mutex[1].__kind = v9;
HIDWORD(v18) = v9;
LODWORD(v16) = 1;
HIDWORD(v17) = a3;
v7 = -1;
if ( write_retry((unsigned int)mutex[1].__owner, dest, 128LL, 0LL) >= 0 )
{
while ( (unsigned int)func0((__m128i **)&mutex[1], (__int64)dest, s1) )
{
v13[0] = 0LL;
v13[1] = random() & 0x3FFF;
if ( read_retry((unsigned int)mutex[1].__owner, s1, 128LL, v13) < 0 )
{
if ( *_errno_location() != 110 )
{
v7 = -1;
goto LABEL_4;
}
pthread_mutex_unlock(mutex);
v11 = random();
usleep(v11 & 0x3FFF);
pthread_mutex_lock(mutex);
}
else
{
if ( !strcmp((const char *)s1, dest) && s1[7].m128i_i32[1] == HIDWORD(v18) )
break;
add_pending((__int64)&mutex[1], s1);
pthread_mutex_unlock(mutex);
v12 = random();
usleep(v12 & 0x3FFF);
pthread_mutex_lock(mutex);
}
}
v10 = s1[6].m128i_i32[1];
v7 = -1;
if ( s1[6].m128i_i32[1] )
{
if ( s1[6].m128i_i32[0] == 2 )
{
*_errno_location() = 11;
}
else
{
v7 = -1;
if ( s1[6].m128i_i32[0] == 3 )
{
*(_DWORD *)(a4 + 104) = 2;
*(_DWORD *)(a4 + 108) = v10;
*(_DWORD *)(a4 + 112) = mutex[1].__nusers;
*(_DWORD *)(a4 + 100) = s1[7].m128i_i32[0];
v7 = 0;
}
}
}
}
}
else
{
*_errno_location() = 36;
v7 = -1;
}
}
else
{
*_errno_location() = 9;
v7 = -1;
}
LABEL_4:
pthread_mutex_unlock(mutex);
}
else
{
*_errno_location() = 22;
v7 = -1;
}
return v7;
}

original_code:
int
cpg_lock(void *handle, const char *resource, lock_flag_t flags, struct cpg_lock *lock)
{
 struct cpg_lock_handle *h = handle;
 struct cpg_lock_msg l, r;
 struct timeval tv;
 int ret = -1;
 if (!h) {
  (*__errno_location ()) = 22;
  return -1;
 }
 pthread_mutex_lock(&h->mutex);
 if (h->pid != (int)getpid()) {
  (*__errno_location ()) = 9;
  goto out;
 }
 if (strlen(resource) > sizeof(l.resource)-1) {
  (*__errno_location ()) = 36;
  goto out;
 }
 memset(&l, 0, sizeof(l));
 memset(&r, 0, sizeof(r));
 strncpy(l.resource, resource, sizeof(l.resource));
 strncpy(lock->resource, resource, sizeof(lock->resource));
 l.owner_pid = h->pid;
 ++h->seq;
 l.owner_tid = h->seq;
 l.request = MSG_LOCK;
 l.flags = (uint32_t)flags;
 if (write_retry(h->fd, &l, sizeof(l), 0) < 0)
  goto out;
 do {
  if (check_pending(&h->pending, &l, &r) == 0)
   break;
  tv.tv_sec = 0;
  tv.tv_usec = random() & 16383;
  if (read_retry(h->fd, &r, sizeof(r), &tv) < 0) {
   if ((*__errno_location ()) == 110) {
    pthread_mutex_unlock(&h->mutex);
    usleep(random() & 16383);
    pthread_mutex_lock(&h->mutex);
    continue;
   }
   goto out;
  }
  if (strcmp(r.resource, l.resource)) {
   do { } while (0);
   add_pending(&h->pending, &r);
   pthread_mutex_unlock(&h->mutex);
   usleep(random() & 16383);
   pthread_mutex_lock(&h->mutex);
   continue;
  }
  if (r.owner_tid != l.owner_tid) {
   do { } while (0);
   add_pending(&h->pending, &r);
   pthread_mutex_unlock(&h->mutex);
   usleep(random() & 16383);
   pthread_mutex_lock(&h->mutex);
   continue;
  }
  break;
 } while (1);
 if (r.owner_nodeid == 0)
  goto out;
 if (r.request == MSG_NAK) {
  (*__errno_location ()) = 11;
  ret = -1;
  goto out;
 }
 if (r.request != MSG_GRANT) {
  goto out;
 }
 lock->state = LOCK_HELD;
 lock->owner_nodeid = r.owner_nodeid;
 lock->owner_pid = h->pid;
 lock->local_id = r.lockid;
 ret = 0;
out:
 pthread_mutex_unlock(&h->mutex);
 return ret;
}

canonical_original_code:
int cpg_lock(void *handle, char *resource, enum lock_flag_t flags, struct cpg_lock *lock)
{
 struct cpg_lock_handle *h = handle;
 struct cpg_lock_msg l; struct cpg_lock_msg r;
 struct timeval tv;
 int ret = -1;
 if (!h) {
  (*__errno_location ()) = 22;
  return -1;
 }
 pthread_mutex_lock(&h->mutex);
 if (h->pid != (int)getpid()) {
  (*__errno_location ()) = 9;
  goto out;
 }
 if (strlen(resource) > sizeof(l.resource)-1) {
  (*__errno_location ()) = 36;
  goto out;
 }
 memset(&l, 0, sizeof(l));
 memset(&r, 0, sizeof(r));
 strncpy(l.resource, resource, sizeof(l.resource));
 strncpy(lock->resource, resource, sizeof(lock->resource));
 l.owner_pid = h->pid;
 ++h->seq;
 l.owner_tid = h->seq;
 l.request = MSG_LOCK;
 l.flags = (unsigned int)flags;
 if (write_retry(h->fd, &l, sizeof(l), 0) < 0)
  goto out;
 do {
  if (check_pending(&h->pending, &l, &r) == 0)
   break;
  tv.tv_sec = 0;
  tv.tv_usec = random() & 16383;
  if (read_retry(h->fd, &r, sizeof(r), &tv) < 0) {
   if ((*__errno_location ()) == 110) {
    pthread_mutex_unlock(&h->mutex);
    usleep(random() & 16383);
    pthread_mutex_lock(&h->mutex);
    continue;
   }
   goto out;
  }
  if (strcmp(r.resource, l.resource)) {
   do { } while (0);
   add_pending(&h->pending, &r);
   pthread_mutex_unlock(&h->mutex);
   usleep(random() & 16383);
   pthread_mutex_lock(&h->mutex);
   continue;
  }
  if (r.owner_tid != l.owner_tid) {
   do { } while (0);
   add_pending(&h->pending, &r);
   pthread_mutex_unlock(&h->mutex);
   usleep(random() & 16383);
   pthread_mutex_lock(&h->mutex);
   continue;
  }
  break;
 } while (1);
 if (r.owner_nodeid == 0)
  goto out;
 if (r.request == MSG_NAK) {
  (*__errno_location ()) = 11;
  ret = -1;
  goto out;
 }
 if (r.request != MSG_GRANT) {
  goto out;
 }
 lock->state = LOCK_HELD;
 lock->owner_nodeid = r.owner_nodeid;
 lock->owner_pid = h->pid;
 lock->local_id = r.lockid;
 ret = 0;
out:
 pthread_mutex_unlock(&h->mutex);
 return ret;
}

variable_types:
handle
void *

variable_types:
resource
char *

variable_types:
flags
enum lock_flag_t { FL_TRY=1 }

variable_types: struct cpg_lock { char resource[96]; int local_fd; int local_id; enum lock_state_t { LOCK_FREE=0, LOCK_PENDING=1, LOCK_HELD=2 } state; int owner_nodeid; int owner_pid; int owner_tid; } *

variable_types: struct cpg_lock_msg { char resource[96]; int request; unsigned int owner_nodeid; unsigned int owner_pid; unsigned int flags; unsigned int lockid; unsigned int owner_tid; char pad[8]; }

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

variable_types: ret int

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

user_defined_types: struct list_entry { struct list_entry *le_next; }


original_code:
int
cpg_unlock(void *handle, struct cpg_lock *lock)
{
 struct cpg_lock_handle *h = handle;
 struct cpg_lock_msg l;
 int ret = -1;
 if (!h) {
  (*__errno_location ()) = 22;
  goto out;
 }
 if (h->pid != (int)getpid()) {
  (*__errno_location ()) = 9;
  goto out;
 }
 if (lock->state != LOCK_HELD) {
  (*__errno_location ()) = 22;
  goto out;
 }
 if (!lock->local_id) {
  (*__errno_location ()) = 22;
  goto out;
 }
 strncpy(l.resource, lock->resource, sizeof(l.resource));
 l.request = MSG_UNLOCK;
 l.owner_nodeid = lock->owner_nodeid;
 l.owner_pid = lock->owner_pid;
 l.lockid = lock->local_id;
 l.owner_tid = 0;
 lock->state = LOCK_FREE;
 ret = write_retry(h->fd, &l, sizeof(l), 0);
out:
 return ret;
}

user_defined_types: struct cpg_lock_msg { char resource[96]; int request; unsigned int owner_nodeid; unsigned int owner_pid; unsigned int flags; unsigned int lockid; unsigned int owner_tid; char pad[8]; }

user_defined_types: enum lock_state_t { LOCK_FREE=0, LOCK_PENDING=1, LOCK_HELD=2 }


decompiled_code:
__int64 __fastcall cpg_lock_fin(pthread_mutex_t *mutex)
{
unsigned int v1; // ebp
__int64 result; // rax

if ( mutex )
{
pthread_mutex_lock(mutex);
v1 = mutex[1].__nusers;
if ( v1 == getpid() )
{
close(mutex[1].__owner);
pthread_mutex_destroy(mutex);
free(mutex);
result = 0LL;
}
else
{
*_errno_location() = 9;
result = 0xFFFFFFFFLL;
}
}
else
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(pthread_mutex_t *mutex)
{
unsigned int v1; // ebp
__int64 result; // rax

if ( mutex )
{
pthread_mutex_lock(mutex);
v1 = mutex[1].__nusers;
if ( v1 == getpid() )
{
close(mutex[1].__owner);
pthread_mutex_destroy(mutex);
free(mutex);
result = 0LL;
}
else
{
*_errno_location() = 9;
result = 0xFFFFFFFFLL;
}
}
else
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
cpg_lock_fin(void *handle)
{
 struct cpg_lock_handle *h = handle;
 if (!h) {
  (*__errno_location ()) = 22;
  return -1;
 }
 pthread_mutex_lock(&h->mutex);
 if (h->pid != (int)getpid()) {
  (*__errno_location ()) = 9;
  return -1;
 }
 close(h->fd);
 pthread_mutex_destroy(&h->mutex);
 free(h);
 return 0;
}


repo: libbybeer/graphstats

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

result = calloc(a2, 4uLL);
*(_QWORD *)a1 = result;
if ( !result )
{
fprintf(stderr, "Can't calloc memory for %s.\n", "bin->elts");
exit(1);
}
*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = 0;
return result;
}

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

result = calloc(a2, 4uLL);
*(_QWORD *)a1 = result;
if ( !result )
{
fprintf(stderr, "Can't calloc memory for %s.\n", "bin->elts");
exit(1);
}
*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = 0;
return result;
}

original_code:
void init_stretchy_bin(stretchy_bin_t *bin, uint32_t capacity){
  if (( (bin->elts) = (uint32_t *)calloc(capacity,sizeof(uint32_t))) == ((void *)0)){ fprintf( stderr, "Can't calloc memory for %s.\n", "bin->elts"); exit(1); };
  bin->capacity = capacity;
  bin->fill = 0;
}

canonical_original_code:
void init_stretchy_bin(struct stretchy_bin_t *bin, unsigned int capacity){
  if (( (bin->elts) = (unsigned int *)calloc(capacity,sizeof(unsigned int))) == ((void *)0)){ fprintf( stderr, "Can't calloc memory for %s.\n", "bin->elts"); exit(1); };
  bin->capacity = capacity;
  bin->fill = 0;
}

variable_types: struct stretchy_bin_t { unsigned int *elts; unsigned int capacity; unsigned int fill; } *

variable_types:
capacity
unsigned int


decompiled_code:
__int64 __fastcall find_in_stretchy_bin(_DWORD *a1, __int64 a2, int a3)
{
unsigned int v5; // esi
_DWORD *v6; // rdx
__int64 result; // rax
int v8; // edx

v5 = *(_DWORD *)(a2 + 12);
if ( v5 )
{
v6 = *(_DWORD **)a2;
result = 0LL;
while ( *v6 != a3 )
{
result = (unsigned int)(result + 1);
++v6;
if ( (_DWORD)result == v5 )
{
result = v5;
v8 = 0;
goto LABEL_8;
}
}
v8 = 1;
}
else
{
result = 0LL;
v8 = 0;
}
LABEL_8:
*a1 = result;
a1[1] = v8;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_DWORD *a1, __int64 a2, int a3)
{
unsigned int v5; // esi
_DWORD *v6; // rdx
__int64 result; // rax
int v8; // edx

v5 = *(_DWORD *)(a2 + 12);
if ( v5 )
{
v6 = *(_DWORD **)a2;
result = 0LL;
while ( *v6 != a3 )
{
result = (unsigned int)(result + 1);
++v6;
if ( (_DWORD)result == v5 )
{
result = v5;
v8 = 0;
goto LABEL_8;
}
}
v8 = 1;
}
else
{
result = 0LL;
v8 = 0;
}
LABEL_8:
*a1 = result;
a1[1] = v8;
return result;
}

original_code:
void find_in_stretchy_bin(int *ret,stretchy_bin_t *bin, uint32_t value){
  int i,j;
  j=0;
  for(i=0;i<bin->fill;i++){
    if(bin->elts[i]==value){
 j=1;
 break;
    }
  }
  ret[0] = i;
  ret[1] = j;
}

canonical_original_code:
void find_in_stretchy_bin(int *ret, struct stretchy_bin_t *bin, unsigned int value){
  int i; int j;
  j=0;
  for(i=0;i<bin->fill;i++){
    if(bin->elts[i]==value){
 j=1;
 break;
    }
  }
  ret[0] = i;
  ret[1] = j;
}

variable_types:
ret
int *

variable_types:
value
unsigned int

variable_types: i int

variable_types: j int

user_defined_types: struct stretchy_bin_t { unsigned int *elts; unsigned int capacity; unsigned int fill; }


decompiled_code:
void *__fastcall add_to_stretchy_bin(__int64 a1, int a2)
{
_DWORD *v2; // rdx
int v3; // eax
void *result; // rax
unsigned int v5; // eax

v2 = *(_DWORD **)a1;
v3 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(a1 + 12) = v3 + 1;
v2[v3] = a2;
result = (void *)*(unsigned int *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) == (_DWORD)result )
{
v5 = 2 * (_DWORD)result;
*(_DWORD *)(a1 + 8) = v5;
result = realloc(*(void **)a1, 4LL * v5);
*(_QWORD *)a1 = result;
if ( !result )
{
fprintf(stderr, "Can't realloc memory for %s.\n", "bin->elts");
exit(1);
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func10(__int64 a1, int a2)
{
_DWORD *v2; // rdx
int v3; // eax
void *result; // rax
unsigned int v5; // eax

v2 = *(_DWORD **)a1;
v3 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(a1 + 12) = v3 + 1;
v2[v3] = a2;
result = (void *)*(unsigned int *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) == (_DWORD)result )
{
v5 = 2 * (_DWORD)result;
*(_DWORD *)(a1 + 8) = v5;
result = realloc(*(void **)a1, 4LL * v5);
*(_QWORD *)a1 = result;
if ( !result )
{
fprintf(stderr, "Can't realloc memory for %s.\n", "bin->elts");
exit(1);
}
}
return result;
}

original_code:
void add_to_stretchy_bin(stretchy_bin_t *bin, uint32_t item){
  bin->elts[bin->fill++] = item;
  if (bin->fill==bin->capacity){
    bin->capacity *= 2;
    if (( (bin->elts) = (uint32_t *)realloc((bin->elts),bin->capacity*sizeof(uint32_t))) == ((void *)0)){ fprintf( stderr, "Can't realloc memory for %s.\n", "bin->elts"); exit(1); };
  }
}

canonical_original_code:
void add_to_stretchy_bin(struct stretchy_bin_t *bin, unsigned int item){
  bin->elts[bin->fill++] = item;
  if (bin->fill==bin->capacity){
    bin->capacity *= 2;
    if (( (bin->elts) = (unsigned int *)realloc((bin->elts),bin->capacity*sizeof(unsigned int))) == ((void *)0)){ fprintf( stderr, "Can't realloc memory for %s.\n", "bin->elts"); exit(1); };
  }
}

variable_types:
item
unsigned int


decompiled_code:
int __fastcall print_stretchy_bin(__int64 a1, const char *a2)
{
__int64 v2; // rbx

printf("Bin (%s) has capacity %d and %d items.\n", a2, *(unsigned int *)(a1 + 8), *(unsigned int *)(a1 + 12));
printf("In bin:");
if ( *(_DWORD *)(a1 + 12) )
{
v2 = 0LL;
do
printf(" %u", *(unsigned int *)(*(_QWORD *)a1 + 4 * v2++));
while ( *(_DWORD *)(a1 + 12) > (unsigned int)v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, const char *a2)
{
__int64 v2; // rbx

printf("Bin (%s) has capacity %d and %d items.\n", a2, *(unsigned int *)(a1 + 8), *(unsigned int *)(a1 + 12));
printf("In bin:");
if ( *(_DWORD *)(a1 + 12) )
{
v2 = 0LL;
do
printf(" %u", *(unsigned int *)(*(_QWORD *)a1 + 4 * v2++));
while ( *(_DWORD *)(a1 + 12) > (unsigned int)v2 );
}
return putchar(10);
}

original_code:
void print_stretchy_bin(stretchy_bin_t *bin,char *desc){
  int i;
  printf("Bin (%s) has capacity %d and %d items.\n",desc,bin->capacity,bin->fill);
  printf("In bin:");
  for (i=0;i<bin->fill;i++){
    printf(" %u",bin->elts[i]);
  }
  printf("\n");
}

canonical_original_code:
void print_stretchy_bin(struct stretchy_bin_t *bin, char *desc){
  int i;
  printf("Bin (%s) has capacity %d and %d items.\n",desc,bin->capacity,bin->fill);
  printf("In bin:");
  for (i=0;i<bin->fill;i++){
    printf(" %u",bin->elts[i]);
  }
  printf("\n");
}

variable_types:
desc
char *