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

    sp_node *old_root;
    int      comp;

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

    // Trivial case: empty tree
    if (tree->root == NULL) { return NULL; }

    // Trivial case: null data == return minimum
    if (data == NULL) {
        splay_left(tree);
        return tree->root->data;
    }

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

    // Take a look at the current root:
    comp = (tree->comp)(tree->root->data, data);

    // If its smaller or equal we must find the successor in the right subtree:
    if (comp <= 0) {
        if (tree->root->right == NULL) { return NULL; }
        else {
            old_root   = tree->root;
            tree->root = tree->root->right;
            old_root->right = NULL;
            splay_left(tree);
            tree->root->left = old_root;
        }
    }

    // Predecessor is at root now:
    return tree->root->data;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
testq %rdi, %rdi
je 0xbd72
movq %rsi, %r14
testq %rsi, %rsi
je 0xbd91
movq %rdi, %rbx
movq (%rdi), %r13
testq %r13, %r13
je 0xbd3f
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 0xbc72
je 0xbcb5
movq 0x10(%r13), %rax
testq %rax, %rax
je 0xbcb5
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jle 0xbc64
movq 0x10(%r13), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%r13)
movq %r13, 0x8(%rax)
cmpq $0x0, 0x10(%rax)
movq %rax, %r13
je 0xbcb8
movq %r13, 0x10(%r12)
leaq 0x10(%r13), %rax
movq %r13, %r12
jmp 0xbcad
movq 0x8(%r13), %rax
testq %rax, %rax
je 0xbcb5
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jns 0xbca2
movq 0x8(%r13), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rax)
cmpq $0x0, 0x8(%rax)
movq %rax, %r13
je 0xbcb8
movq %r13, 0x8(%r15)
leaq 0x8(%r13), %rax
movq %r13, %r15
movq (%rax), %r13
jmp 0xbc20
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
jg 0xbd5e
movq (%rbx), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0xbd3f
movq %rcx, (%rbx)
movq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movq 0x8(%rcx), %rsi
leaq 0x8(%rsp), %rdx
testq %rsi, %rsi
je 0xbd46
movq %rsi, %rdi
movq 0x10(%rsi), %rsi
movq %rsi, 0x8(%rcx)
movq %rcx, 0x10(%rdi)
cmpq $0x0, 0x8(%rdi)
je 0xbd43
movq %rdi, 0x8(%rdx)
movq 0x8(%rdi), %rcx
movq 0x8(%rcx), %rsi
movq %rdi, %rdx
jmp 0xbd13
xorl %eax, %eax
jmp 0xbd64
movq %rdi, %rcx
movq 0x10(%rcx), %rsi
movq %rsi, 0x8(%rdx)
movq 0x10(%rsp), %rdx
movq %rdx, 0x10(%rcx)
movq %rcx, (%rbx)
movq %rax, 0x8(%rcx)
movq (%rbx), %rax
movq (%rax), %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x250a(%rip), %rdi      # 0xe283
leaq 0x248d(%rip), %rsi      # 0xe20d
leaq 0x2f57(%rip), %rcx      # 0xecde
movl $0x1378, %edx           # imm = 0x1378
callq 0x1080
leaq 0x256a(%rip), %rdi      # 0xe302
leaq 0x246e(%rip), %rsi      # 0xe20d
leaq 0x2f38(%rip), %rcx      # 0xecde
movl $0x1379, %edx           # imm = 0x1379
callq 0x1080