// File: /workspace/github_c_makefile_star2_O2/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 %rbx
pushq %rax
testq %rdi, %rdi
je 0xb4c2
movq %rsi, %r14
testq %rsi, %rsi
je 0xb4e1
movq %rdi, %rbx
cmpq $0x0, (%rdi)
je 0xb41c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb500
movq (%rbx), %r15
testq %r15, %r15
je 0xb438
movq (%r15), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
movl %eax, %ebp
testl %eax, %eax
jne 0xb43b
movq (%rbx), %rcx
movq (%rcx), %rax
movq %r14, (%rcx)
jmp 0xb478
pushq $0x18
popq %rdi
callq 0x10d0
movq %rax, (%rbx)
testq %rax, %rax
je 0xb4a4
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
jmp 0xb476
pushq $0x1
popq %rbp
pushq $0x18
popq %rdi
callq 0x10d0
movq %rax, (%rbx)
testq %rax, %rax
je 0xb483
movq %r14, (%rax)
testl %ebp, %ebp
jle 0xb465
movq %r15, 0x8(%rax)
movq 0x10(%r15), %rcx
movq %rcx, 0x10(%rax)
andq $0x0, 0x10(%r15)
jmp 0xb476
movq %r15, 0x10(%rax)
movq 0x8(%r15), %rcx
movq %rcx, 0x8(%rax)
andq $0x0, 0x8(%r15)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x5b6e(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x2715(%rip), %rdi      # 0xdba9
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
movq %r15, (%rbx)
jmp 0xb476
movq 0x5b4d(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x26f4(%rip), %rdi      # 0xdba9
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0xb476
leaq 0x1dba(%rip), %rdi      # 0xd283
leaq 0x1d3d(%rip), %rsi      # 0xd20d
leaq 0x26aa(%rip), %rcx      # 0xdb81
movl $0x125f, %edx           # imm = 0x125F
callq 0x1080
leaq 0x1e1a(%rip), %rdi      # 0xd302
leaq 0x1d1e(%rip), %rsi      # 0xd20d
leaq 0x268b(%rip), %rcx      # 0xdb81
movl $0x1260, %edx           # imm = 0x1260
callq 0x1080