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

    sp_node *old_root;
    void    *old_data = NULL;

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

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

        // Splay the data to the root:
        splay(tree, data);

        // If it is here: remove it!
        if ((tree->comp)(tree->root->data, data) == 0) {
            old_root = tree->root;
            old_data = tree->root->data;
            if (tree->root->right == NULL) { tree->root = tree->root->left; }
            else {
                tree->root = tree->root->right;
                splay_left(tree);
                tree->root->left = old_root->left;
            }
            free(old_root);
        }
    }

    // Return
    return old_data;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
testq %rdi, %rdi
je 0xc645
movq %rsi, %r14
testq %rsi, %rsi
je 0xc664
movq %rdi, %rbx
movq (%rdi), %r13
testq %r13, %r13
je 0xc5b3
xorps %xmm0, %xmm0
leaq 0x8(%rsp), %r15
movups %xmm0, 0x8(%r15)
movq %r15, %r12
movq (%r13), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
js 0xc53a
je 0xc57d
movq 0x10(%r13), %rax
testq %rax, %rax
je 0xc57d
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jle 0xc52c
movq 0x10(%r13), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%r13)
movq %r13, 0x8(%rax)
cmpq $0x0, 0x10(%rax)
movq %rax, %r13
je 0xc580
movq %r13, 0x10(%r12)
leaq 0x10(%r13), %rax
movq %r13, %r12
jmp 0xc575
movq 0x8(%r13), %rax
testq %rax, %rax
je 0xc57d
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jns 0xc56a
movq 0x8(%r13), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rax)
cmpq $0x0, 0x8(%rax)
movq %rax, %r13
je 0xc580
movq %r13, 0x8(%r15)
leaq 0x8(%r13), %rax
movq %r13, %r15
movq (%rax), %r13
jmp 0xc4e8
movq %r13, %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%r12)
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r15)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, (%rbx)
movq (%rax), %rdi
movq %r14, %rsi
callq *0x8(%rbx)
testl %eax, %eax
je 0xc5b8
xorl %r14d, %r14d
jmp 0xc634
movq (%rbx), %rdi
movq (%rdi), %r14
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0xc607
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movq 0x8(%rax), %rdx
leaq 0x8(%rsp), %rcx
testq %rdx, %rdx
je 0xc613
movq %rdx, %rsi
movq 0x10(%rdx), %rdx
movq %rdx, 0x8(%rax)
movq %rax, 0x10(%rsi)
cmpq $0x0, 0x8(%rsi)
je 0xc610
movq %rsi, 0x8(%rcx)
movq 0x8(%rsi), %rax
movq 0x8(%rax), %rdx
movq %rsi, %rcx
jmp 0xc5db
movq 0x8(%rdi), %rax
movq %rax, (%rbx)
jmp 0xc62f
movq %rsi, %rax
movq 0x10(%rax), %rdx
movq %rdx, 0x8(%rcx)
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, (%rbx)
movq 0x8(%rdi), %rcx
movq %rcx, 0x8(%rax)
callq 0x1120
movq %r14, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1c37(%rip), %rdi      # 0xe283
leaq 0x1bba(%rip), %rsi      # 0xe20d
leaq 0x26b0(%rip), %rcx      # 0xed0a
movl $0x13a8, %edx           # imm = 0x13A8
callq 0x1080
leaq 0x1c97(%rip), %rdi      # 0xe302
leaq 0x1b9b(%rip), %rsi      # 0xe20d
leaq 0x2691(%rip), %rcx      # 0xed0a
movl $0x13a9, %edx           # imm = 0x13A9
callq 0x1080