// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x1, -0x2c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x14690
jmp 0x146af
leaq 0x1bdb(%rip), %rdi      # 0x16272
leaq 0x1b5e(%rip), %rsi      # 0x161fc
movl $0x125f, %edx           # imm = 0x125F
leaq 0x24d3(%rip), %rcx      # 0x16b7d
callq 0x1060
cmpq $0x0, -0x18(%rbp)
je 0x146b8
jmp 0x146d7
leaq 0x1c32(%rip), %rdi      # 0x162f1
leaq 0x1b36(%rip), %rsi      # 0x161fc
movl $0x1260, %edx           # imm = 0x1260
leaq 0x24ab(%rip), %rcx      # 0x16b7d
callq 0x1060
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x14752
movl $0x18, %edi
callq 0x10a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x14719
movq 0x58f2(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x2495(%rip), %rsi      # 0x16ba5
movb $0x0, %al
callq 0x1080
jmp 0x14745
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq $0x0, 0x10(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x14877
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x14890
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x14789
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rcx
movq (%rcx), %rsi
callq *%rax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x147b8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x14877
movl $0x18, %edi
callq 0x10a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x147fb
movq 0x581b(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x23be(%rip), %rsi      # 0x16ba5
movb $0x0, %al
callq 0x1080
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1486f
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x2c(%rbp)
jle 0x1483f
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x1486d
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x1486f
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)