// File: /workspace/github_c_makefile_star2_O3/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 0xbdf0
movq %rsi, %r14
testq %rsi, %rsi
je 0xbe0f
movq %rdi, %rbx
movq (%rdi), %r13
testq %r13, %r13
je 0xbdbd
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 0xbcf3
je 0xbd36
movq 0x10(%r13), %rax
testq %rax, %rax
je 0xbd36
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jle 0xbce5
movq 0x10(%r13), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%r13)
movq %r13, 0x8(%rax)
cmpq $0x0, 0x10(%rax)
movq %rax, %r13
je 0xbd39
movq %r13, 0x10(%r12)
leaq 0x10(%r13), %rax
movq %r13, %r12
jmp 0xbd2e
movq 0x8(%r13), %rax
testq %rax, %rax
je 0xbd36
movq (%rax), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
testl %eax, %eax
jns 0xbd23
movq 0x8(%r13), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rax)
cmpq $0x0, 0x8(%rax)
movq %rax, %r13
je 0xbd39
movq %r13, 0x8(%r15)
leaq 0x8(%r13), %rax
movq %r13, %r15
movq (%rax), %r13
jmp 0xbca1
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)
movq (%rbx), %rcx
testl %eax, %eax
jg 0xbddf
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0xbdbd
movq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movq 0x8(%rax), %rsi
leaq 0x8(%rsp), %rdx
testq %rsi, %rsi
je 0xbdc4
movq %rsi, %rdi
movq 0x10(%rsi), %rsi
movq %rsi, 0x8(%rax)
movq %rax, 0x10(%rdi)
cmpq $0x0, 0x8(%rdi)
je 0xbdc1
movq %rdi, 0x8(%rdx)
movq 0x8(%rdi), %rax
movq 0x8(%rax), %rsi
movq %rdi, %rdx
jmp 0xbd91
xorl %eax, %eax
jmp 0xbde2
movq %rdi, %rax
movq 0x10(%rax), %rsi
movq %rsi, 0x8(%rdx)
movq 0x10(%rsp), %rdx
movq %rdx, 0x10(%rax)
movq %rax, (%rbx)
movq %rcx, 0x8(%rax)
movq %rax, %rcx
movq (%rcx), %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x248c(%rip), %rdi      # 0xe283
leaq 0x240f(%rip), %rsi      # 0xe20d
leaq 0x2ed9(%rip), %rcx      # 0xecde
movl $0x1378, %edx           # imm = 0x1378
callq 0x1080
leaq 0x24ec(%rip), %rdi      # 0xe302
leaq 0x23f0(%rip), %rsi      # 0xe20d
leaq 0x2eba(%rip), %rcx      # 0xecde
movl $0x1379, %edx           # imm = 0x1379
callq 0x1080