// File: /workspace/github_c_makefile_star2_O0/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: bs_tree_insert
void *bs_tree_insert(bs_tree *tree, void *data) {

    bs_node *node;
    bs_node *new_node;
    void    *old_data;
    int      comp;

    // Sanity Checks:
    assert(tree != NULL);
    assert(data != NULL);

    // Avoid the trivial case: empty tree
    node = NULL;
    if (tree->root != NULL) {

        // Search for the correct place to insert data:
        node = tree->root;
        for (;;) {

            // Compare data:
            comp = (tree->comp)(data, node->data);

            // Data is smaller:
            if (comp < 0) {
                if (node->left == NULL) { break; }
                else { node = node->left; }
            }

            // Data is bigger:
            else if (comp > 0) {
                if (node->right == NULL) { break; }
                else { node = node->right; }
            }

            // Data is already there: overwrite it!
            else {
                old_data   = node->data;
                node->data = data;
                return old_data;
            }
        }
    }

    // Insert the new node here:
    new_node = (bs_node *) malloc(sizeof(bs_node));
    if (new_node == NULL) {
        fprintf(stderr, "ERROR: Unable to allocate bs_node\n");
    } else {
        new_node->data  = data;
        new_node->left  = NULL;
        new_node->right = NULL;
    }

    if (node == NULL)  { tree->root  = new_node; }
    else if (comp < 0) { node->left  = new_node; }
    else               { node->right = new_node; }

    // Data was not here!
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xc899
jmp 0xc8b8
leaq 0x99d2(%rip), %rdi      # 0x16272
leaq 0x9955(%rip), %rsi      # 0x161fc
movl $0xc2, %edx
leaq 0x9a16(%rip), %rcx      # 0x162c9
callq 0x1060
cmpq $0x0, -0x18(%rbp)
je 0xc8c1
jmp 0xc8e0
leaq 0x9a29(%rip), %rdi      # 0x162f1
leaq 0x992d(%rip), %rsi      # 0x161fc
movl $0xc3, %edx
leaq 0x99ee(%rip), %rcx      # 0x162c9
callq 0x1060
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0xc987
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rcx
movq (%rcx), %rsi
callq *%rax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0xc93a
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xc92c
jmp 0xc985
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0xc980
cmpl $0x0, -0x34(%rbp)
jle 0xc95b
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xc94d
jmp 0xc985
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0xc97e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xca17
jmp 0xc980
jmp 0xc901
jmp 0xc987
movl $0x18, %edi
callq 0x10a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xc9b6
movq 0xd655(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x98f9(%rip), %rsi      # 0x162a6
movb $0x0, %al
callq 0x1080
jmp 0xc9d9
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
cmpq $0x0, -0x20(%rbp)
jne 0xc9ed
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0xca0f
cmpl $0x0, -0x34(%rbp)
jge 0xca01
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0xca0d
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0xca0f
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)