// File: /workspace/github_c_makefile_star2_O1/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: sp_tree_insert
void *sp_tree_insert(sp_tree *tree, void *data) {

    sp_node *old_root;
    void    *old_data;
    int      comp = 1;

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

    // Trivial case: Empty tree
    if (tree->root == NULL) {
        tree->root = (sp_node *) malloc(sizeof(sp_node));
        if (tree->root == NULL) {
            fprintf(stderr, "ERROR: Unable to allocate sp_node\n");
        } else {
            tree->root->data  = data;
            tree->root->left  = NULL;
            tree->root->right = NULL;
        }
        return NULL;
    }

    // General case: Splay data to the root
    splay(tree, data);
    old_root = tree->root;

    // Compare the current root with data:
    if (old_root != NULL) { comp = (tree->comp)(data, old_root->data); }

    // If data is in the tree: overwrite it!
    if (comp == 0) {
        old_data = tree->root->data;
        tree->root->data = data;
        return old_data;
    }

    // Otherwise insert a new node as a root and link the previous root to it:
    tree->root = (sp_node *) malloc(sizeof(sp_node));
    if (tree->root == NULL) {
        fprintf(stderr, "ERROR: Unable to allocate sp_node\n");
        tree->root = old_root;
    } else {
        tree->root->data  = data;
        if (comp > 0) {
            tree->root->left  = old_root;
            tree->root->right = old_root->right;
            old_root->right   = NULL;
        } else {
            tree->root->right = old_root;
            tree->root->left  = old_root->left;
            old_root->left    = NULL;
        }
    }

    // Data was not here!
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0xbf63
movq %rsi, %rbx
testq %rsi, %rsi
je 0xbf82
movq %rdi, %r14
movq (%rdi), %rbp
testq %rbp, %rbp
je 0xbec5
xorps %xmm0, %xmm0
movq %rsp, %r12
movups %xmm0, 0x8(%r12)
movq %r12, %r13
movq (%rbp), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
js 0xbe44
je 0xbe89
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0xbe89
movq (%rax), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
jle 0xbe37
movq 0x10(%rbp), %r15
movq 0x8(%r15), %rax
movq %rax, 0x10(%rbp)
movq %rbp, 0x8(%r15)
cmpq $0x0, 0x10(%r15)
movq %r15, %rbp
je 0xbe8c
movq %rbp, 0x10(%r13)
leaq 0x10(%rbp), %rax
movq %rbp, %r13
jmp 0xbe81
movq 0x8(%rbp), %rax
testq %rax, %rax
je 0xbe89
movq (%rax), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
jns 0xbe75
movq 0x8(%rbp), %r15
movq 0x10(%r15), %rax
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%r15)
cmpq $0x0, 0x8(%r15)
movq %r15, %rbp
je 0xbe8c
movq %rbp, 0x8(%r12)
leaq 0x8(%rbp), %rax
movq %rbp, %r12
movq (%rax), %rbp
jmp 0xbdf1
movq %rbp, %r15
movq 0x8(%r15), %rax
movq %rax, 0x10(%r13)
movq 0x10(%r15), %rax
movq %rax, 0x8(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r15)
movq %r15, (%r14)
testq %r15, %r15
je 0xbee7
movq (%r15), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
movl %eax, %ebp
jmp 0xbeec
movl $0x18, %edi
callq 0x10d0
movq %rax, (%r14)
testq %rax, %rax
je 0xbf5c
movq %rbx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
jmp 0xbf3e
movl $0x1, %ebp
testl %ebp, %ebp
je 0xbf1f
movl $0x18, %edi
callq 0x10d0
movq %rax, (%r14)
testq %rax, %rax
je 0xbf4f
movq %rbx, (%rax)
testl %ebp, %ebp
jle 0xbf2a
movq %r15, 0x8(%rax)
movq 0x10(%r15), %rcx
movq %rcx, 0x10(%rax)
movq $0x0, 0x10(%r15)
jmp 0xbf3e
movq (%r14), %rcx
movq (%rcx), %rax
movq %rbx, (%rcx)
jmp 0xbf40
movq %r15, 0x10(%rax)
movq 0x8(%r15), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x8(%r15)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movq %r14, %rsi
callq 0x148d
jmp 0xbf3e
callq 0x14be
jmp 0xbf3e
leaq 0x2319(%rip), %rdi      # 0xe283
leaq 0x229c(%rip), %rsi      # 0xe20d
leaq 0x2c09(%rip), %rcx      # 0xeb81
movl $0x125f, %edx           # imm = 0x125F
callq 0x1080
leaq 0x2379(%rip), %rdi      # 0xe302
leaq 0x227d(%rip), %rsi      # 0xe20d
leaq 0x2bea(%rip), %rcx      # 0xeb81
movl $0x1260, %edx           # imm = 0x1260
callq 0x1080