// File: /workspace/github_c_makefile_star2_O3/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 0xbfd1
movq %rsi, %rbx
testq %rsi, %rsi
je 0xbff0
movq %rdi, %r14
movq (%rdi), %rbp
testq %rbp, %rbp
je 0xbf6f
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 0xbec2
je 0xbf07
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0xbf07
movq (%rax), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
jle 0xbeb5
movq 0x10(%rbp), %r15
movq 0x8(%r15), %rax
movq %rax, 0x10(%rbp)
movq %rbp, 0x8(%r15)
cmpq $0x0, 0x10(%r15)
movq %r15, %rbp
je 0xbf0a
movq %rbp, 0x10(%r13)
leaq 0x10(%rbp), %rax
movq %rbp, %r13
jmp 0xbeff
movq 0x8(%rbp), %rax
testq %rax, %rax
je 0xbf07
movq (%rax), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
jns 0xbef3
movq 0x8(%rbp), %r15
movq 0x10(%r15), %rax
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%r15)
cmpq $0x0, 0x8(%r15)
movq %r15, %rbp
je 0xbf0a
movq %rbp, 0x8(%r12)
leaq 0x8(%rbp), %rax
movq %rbp, %r12
movq (%rax), %rbp
jmp 0xbe6f
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)
movq (%r15), %rsi
movq %rbx, %rdi
callq *0x8(%r14)
testl %eax, %eax
je 0xbf8d
movl %eax, %ebp
movl $0x18, %edi
callq 0x10d0
movq %rax, (%r14)
testq %rax, %rax
je 0xbfbd
movq %rbx, (%rax)
testl %ebp, %ebp
jle 0xbf98
movq %r15, 0x8(%rax)
movq 0x10(%r15), %rcx
movq %rcx, 0x10(%rax)
movq $0x0, 0x10(%r15)
jmp 0xbfac
movl $0x18, %edi
callq 0x10d0
movq %rax, (%r14)
testq %rax, %rax
je 0xbfca
movq %rbx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
jmp 0xbfac
movq (%r14), %rcx
movq (%rcx), %rax
movq %rbx, (%rcx)
jmp 0xbfae
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 0xbfac
callq 0x14be
jmp 0xbfac
leaq 0x22ab(%rip), %rdi      # 0xe283
leaq 0x222e(%rip), %rsi      # 0xe20d
leaq 0x2b9b(%rip), %rcx      # 0xeb81
movl $0x125f, %edx           # imm = 0x125F
callq 0x1080
leaq 0x230b(%rip), %rdi      # 0xe302
leaq 0x220f(%rip), %rsi      # 0xe20d
leaq 0x2b7c(%rip), %rcx      # 0xeb81
movl $0x1260, %edx           # imm = 0x1260
callq 0x1080