// File: /workspace/github_c_makefile_star2_O2/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: rb_tree_remove
void *rb_tree_remove(rb_tree *tree, const void *data) {

    rb_node *granpa   = NULL;   // We need to store the last 3 levels //
    rb_node *parent   = NULL;   //                                    //
    rb_node *sister   = NULL;   //            granpa                  //
    rb_node *node     = NULL;   //              |                     //
    rb_node *old_node = NULL;   //            parent                  //
    void    *old_data = NULL;   //            /    \   <- comp_n      //
    int      comp_n   = 0;      //        sister  node                //
    int      comp     = 0;      //                / \  <- comp        //

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

    // Initialize the search at the root node:
    node = tree->root;
    if (node == NULL) { return NULL; }
    
    // Look for a leaf:
    while (node != NULL) {

        // At this point node is BLACK, if sister exists is BLACK and if parent 
        // exists is RED. We want to paint node RED and repair any violation.

        // Case 1: Node has two BLACK children
        if (IS_BLACK(node->left) && IS_BLACK(node->right)) {

            // Easy case: the node is the root node
            if (parent == NULL) { node->color = RED; }

            // General case:
            else {

                // Case 1.0: Node has no sister
                if (sister == NULL) {
                    node->color   = RED;
                    parent->color = BLACK;
                    
                // Case 1.1: Sister has 2 BLACK children
                } else if (IS_BLACK(sister->left) && IS_BLACK(sister->right)){
                    node->color   = RED;
                    sister->color = RED;
                    parent->color = BLACK;

                // Case 1.2: Sister has at least 1 RED children
                } else {

                    // If sister->left is RED:
                    if (IS_RED(sister->left)) {

                        // If sister == parent->right: Double rotation
                        if (comp < 0) {

                            if  (granpa == NULL) { tree->root    = sister->left; }
                            else if (comp_n < 0) { granpa->left  = sister->left; }
                            else                 { granpa->right = sister->left; }
                            granpa = sister->left;
                            
                            parent->right = granpa->left;
                            granpa->left  = parent;

                            sister->left  = granpa->right;
                            granpa->right = sister;
                            sister        = parent->right;
                            
                            node->color   = RED;
                            parent->color = BLACK;
                        }

                        // If sister == parent->left: Single rotation
                        else {

                            if  (granpa == NULL) { tree->root    = sister; }
                            else if (comp_n < 0) { granpa->left  = sister; }
                            else                 { granpa->right = sister; }
                            granpa = sister;
                            
                            parent->left  = granpa->right;
                            granpa->right = parent;
                            sister        = parent->left;
                            
                            node->color   = RED;
                            granpa->color = RED;
                            parent->color = BLACK;
                            granpa->left->color = BLACK;
                        }
                    }

                    // If sister->right is RED:
                    else {

                        // If sister == parent->left: Double rotation
                        if (comp > 0) {

                            if  (granpa == NULL) { tree->root    = sister->right; }
                            else if (comp_n < 0) { granpa->left  = sister->right; }
                            else                 { granpa->right = sister->right; }
                            granpa = sister->right;
                            
                            parent->left  = granpa->right;
                            granpa->right = parent;

                            sister->right = granpa->left;
                            granpa->left  = sister;
                            sister        = parent->left;
                            
                            node->color   = RED;
                            parent->color = BLACK;
                        }

                        // If sister == parent->right: Single rotation
                        else {

                            if  (granpa == NULL) { tree->root    = sister; }
                            else if (comp_n < 0) { granpa->left  = sister; }
                            else                 { granpa->right = sister; }
                            granpa = sister;
                            
                            parent->right = granpa->left;
                            granpa->left  = parent;
                            sister        = parent->right;

                            node->color   = RED;
                            granpa->color = RED;
                            parent->color = BLACK;
                            granpa->right->color = BLACK;
                        }
                    }
                }
            }
        }

        // We compare the data now because we need the information for "Case 2"

        // Compare data unless you already know where to go:
        comp_n = comp;
        comp   = (old_data == NULL) ? (tree->comp)(data, node->data) : (-1);

        // If we have found the node to remove: Remember it!
        if (comp == 0) {
            old_data = node->data;
            old_node = node;
            comp     = +1;   // ...and search the successor
        }
        
        // Case 2: Node has at least one RED children
        if (IS_RED(node->left) || IS_RED(node->right)) {

            // Again node is BLACK, if sister exists is BLACK and if parent
            // exists is RED. We paint node RED and repair any violation.

            // Case 2.1: We are moving to the RED node
            if ((comp < 0 && IS_RED(node->left )) ||
                (comp > 0 && IS_RED(node->right)) ){

                // Move and compare again for free!
                granpa = parent;
                parent = node;
                if (comp < 0) {
                    node   = parent->left;
                    sister = parent->right;
                } else if (comp > 0) {
                    node   = parent->right;
                    sister = parent->left;
                }
                comp_n = comp;
                comp   = (old_data == NULL) ? (tree->comp)(data, node->data) : (-1);
                if (comp == 0) {
                    old_data = node->data;
                    old_node = node;
                    comp     = +1;
                }
            }
            
            // Case 2.2: We are moving to the BLACK node
            else {
                // If we are moving to the left: Single left-rotation
                if (comp < 0) {
                    if  (parent == NULL) { tree->root    = node->right; }
                    else if (comp_n < 0) { parent->left  = node->right; }
                    else                 { parent->right = node->right; }
                    granpa        = parent;
                    parent        = node->right;
                    sister        = parent->right;
                    node->right   = parent->left;
                    parent->left  = node;
                                        
                    node->color   = RED;
                    parent->color = BLACK;

                    comp_n = -1;
                }

                // If we are moving to the right: Single right-rotation
                else {
                    if  (parent == NULL) { tree->root    = node->left; }
                    else if (comp_n < 0) { parent->left  = node->left; }
                    else                 { parent->right = node->left; }
                    granpa        = parent;
                    parent        = node->left;
                    sister        = parent->left;
                    node->left    = parent->right;
                    parent->right = node;
                                        
                    node->color   = RED;
                    parent->color = BLACK;

                    comp_n = 1;
                }                
            }
        }

        // ...and finally move!
        granpa = parent;
        parent = node;
        if (comp < 0) {
            node   = parent->left;
            sister = parent->right;
        } else if (comp > 0) {
            node   = parent->right;
            sister = parent->left;
        }
    }

    // Erase "parent", which should be RED:
    if (old_node != NULL) {
        old_node->data = parent->data;
        if      (granpa       == NULL)   { tree->root    = parent->right; }
        else if (granpa->left == parent) { granpa->left  = parent->right; }
        else                             { granpa->right = parent->right; }
        free(parent);
    }
    
    // Before leaving: Make sure that the root is BLACK!
    if (tree->root != NULL) { tree->root->color = BLACK; }

    // And return old_data (which will be NULL unless data was already here)
    return old_data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x9dda
movq %rsi, %r8
testq %rsi, %rsi
je 0x9df9
movq %rdi, %rbx
movq (%rdi), %r13
xorl %edx, %edx
testq %r13, %r13
je 0x9da1
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %eax, %eax
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r8, 0x8(%rsp)
testq %r13, %r13
je 0x9d7a
movq 0x8(%r13), %rsi
testq %rsi, %rsi
je 0x9a8b
cmpb $0x0, 0x18(%rsi)
jne 0x9c07
movq 0x10(%r13), %rsi
testq %rsi, %rsi
je 0x9a9e
cmpb $0x0, 0x18(%rsi)
jne 0x9c07
testq %r12, %r12
je 0x9afe
testq %rax, %rax
je 0x9bfa
movq 0x8(%rax), %rsi
testq %rsi, %rsi
je 0x9abe
movb 0x18(%rsi), %dil
testb %dil, %dil
jne 0x9ad6
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x9b08
cmpb $0x0, 0x18(%rdi)
je 0x9b08
testq %rsi, %rsi
je 0x9b16
movb 0x18(%rsi), %dil
cmpb $0x1, %dil
jne 0x9b16
testl %r14d, %r14d
js 0x9b3d
movq %rbx, %rsi
testq %rcx, %rcx
je 0x9b87
testl %edx, %edx
js 0x9b80
addq $0x10, %rcx
jmp 0x9b84
movb $0x1, %al
movq %r13, %rcx
jmp 0x9c04
movb $0x1, %cl
movb %cl, 0x18(%r13)
movb %cl, 0x18(%rax)
jmp 0x9bff
testl %r14d, %r14d
jle 0x9b2e
testq %rcx, %rcx
je 0x9b53
movq 0x10(%rax), %rsi
testl %edx, %edx
js 0x9b5c
movq %rsi, 0x10(%rcx)
jmp 0x9b60
testq %rcx, %rcx
je 0x9b7b
testl %edx, %edx
js 0x9bb1
movq %rax, 0x10(%rcx)
jmp 0x9bb9
testq %rcx, %rcx
je 0x9bac
testl %edx, %edx
js 0x9bd9
movq %rsi, 0x10(%rcx)
jmp 0x9be1
movq 0x10(%rax), %rsi
movq %rsi, (%rbx)
jmp 0x9b60
movq %rsi, 0x8(%rcx)
movq 0x10(%rsi), %rcx
movq %rcx, 0x8(%r12)
movq %r12, 0x10(%rsi)
movq 0x8(%rsi), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x8(%rsi)
jmp 0x9bfa
movq %rax, (%rbx)
jmp 0x9bb9
addq $0x8, %rcx
movq %rcx, %rsi
movq %rax, (%rsi)
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r12)
movq %r12, 0x10(%rax)
movb $0x1, %cl
movb %cl, 0x18(%r13)
movb %cl, 0x18(%rax)
movb $0x0, 0x18(%r12)
movq 0x8(%rax), %rcx
jmp 0x9bd5
movq %rsi, (%rbx)
jmp 0x9be1
movq %rax, 0x8(%rcx)
movq 0x8(%rax), %rsi
movq %rsi, 0x10(%r12)
movq %r12, 0x8(%rax)
movb $0x1, %cl
movb %cl, 0x18(%r13)
movb %cl, 0x18(%rax)
movb $0x0, 0x18(%r12)
movq 0x10(%rax), %rcx
xorl %eax, %eax
jmp 0x9c04
movq %rsi, 0x8(%rcx)
movq 0x8(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, 0x10(%r12)
movq %r12, 0x8(%rsi)
movq 0x10(%rsi), %rcx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rsi)
movb $0x1, 0x18(%r13)
xorl %eax, %eax
movq %r12, %rcx
movb %al, 0x18(%rcx)
testq %r15, %r15
je 0x9c11
pushq $-0x1
popq %rbp
jmp 0x9c37
movq (%r13), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
testl %eax, %eax
je 0x9c26
movl %eax, %ebp
xorl %r15d, %r15d
jmp 0x9c32
movq (%r13), %r15
movq %r13, 0x10(%rsp)
pushq $0x1
popq %rbp
movq 0x8(%rsp), %r8
leaq 0x8(%r13), %rcx
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x9c59
cmpb $0x1, 0x18(%rax)
jne 0x9c59
testl %ebp, %ebp
js 0x9c7e
movq 0x10(%r13), %rdx
testq %rdx, %rdx
jne 0x9c74
jmp 0x9c97
movq 0x10(%r13), %rdx
testq %rdx, %rdx
je 0x9d53
cmpb $0x1, 0x18(%rdx)
jne 0x9d53
testl %ebp, %ebp
js 0x9cc5
cmpb $0x1, 0x18(%rdx)
jne 0x9c97
leaq 0x10(%r13), %rcx
movq (%rcx), %r14
testq %r15, %r15
je 0x9ca8
movq %r13, %r12
movq %r14, %r13
movl %ebp, %r14d
pushq $-0x1
popq %rbp
jmp 0x9d53
testq %r12, %r12
je 0x9ce5
testl %r14d, %r14d
js 0x9cea
movq %rax, 0x10(%r12)
jmp 0x9cf2
movq (%r14), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
testl %eax, %eax
je 0x9d0e
movq %r13, %r12
movq %r14, %r13
xorl %r15d, %r15d
movl %ebp, %r14d
movl %eax, %ebp
jmp 0x9d22
testq %rax, %rax
je 0x9cd0
cmpb $0x1, 0x18(%rax)
je 0x9c7e
testq %r12, %r12
je 0x9d29
movq 0x10(%r13), %rax
testl %r14d, %r14d
js 0x9d32
movq %rax, 0x10(%r12)
jmp 0x9d37
movq %rax, (%rbx)
jmp 0x9cf2
movq %rax, 0x8(%r12)
movq (%rcx), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rax)
movb $0x1, 0x18(%r13)
movb $0x0, 0x18(%rax)
movq %rax, %r12
pushq $0x1
jmp 0x9d51
movq (%r14), %r15
movq %r13, %r12
movq %r14, %r13
movq %r14, 0x10(%rsp)
movl %ebp, %r14d
pushq $0x1
popq %rbp
movq 0x8(%rsp), %r8
jmp 0x9d53
movq 0x10(%r13), %rax
movq %rax, (%rbx)
jmp 0x9d37
movq %rax, 0x8(%r12)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%r13)
movq %r13, 0x8(%rax)
movb $0x1, 0x18(%r13)
movb $0x0, 0x18(%rax)
movq %rax, %r12
pushq $-0x1
popq %r14
movl %ebp, %eax
shrl $0x1f, %eax
movl %ebp, %edx
notl %edx
shrl $0x1f, %edx
movq 0x8(%r13,%rax,8), %rax
movq %r12, %rcx
movq %r13, %r12
movq 0x8(%r13,%rdx,8), %r13
movl %r14d, %edx
movl %ebp, %r14d
jmp 0x9a6f
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x9dbc
movq (%r12), %rax
movq %rax, (%rdx)
testq %rcx, %rcx
je 0x9da6
movq 0x10(%r12), %rax
cmpq %r12, 0x8(%rcx)
je 0x9db0
movq %rax, 0x10(%rcx)
jmp 0x9db4
xorl %r15d, %r15d
jmp 0x9dc8
movq 0x10(%r12), %rax
movq %rax, (%rbx)
jmp 0x9db4
movq %rax, 0x8(%rcx)
movq %r12, %rdi
callq 0x1120
movq (%rbx), %rax
testq %rax, %rax
je 0x9dc8
movb $0x0, 0x18(%rax)
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34a2(%rip), %rdi      # 0xd283
leaq 0x3425(%rip), %rsi      # 0xd20d
leaq 0x3b22(%rip), %rcx      # 0xd911
movl $0xb0b, %edx            # imm = 0xB0B
callq 0x1080
leaq 0x3502(%rip), %rdi      # 0xd302
leaq 0x3406(%rip), %rsi      # 0xd20d
leaq 0x3b03(%rip), %rcx      # 0xd911
movl $0xb0c, %edx            # imm = 0xB0C
callq 0x1080