// File: /workspace/github_c_makefile_star2_O3/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 0xa533
movq %rsi, %r8
testq %rsi, %rsi
je 0xa552
movq %rdi, %rbx
movq (%rdi), %r12
xorl %r14d, %r14d
testq %r12, %r12
je 0xa4fb
xorl %edx, %edx
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %r8, 0x8(%rsp)
movq 0x8(%r12), %rsi
testq %rsi, %rsi
je 0xa1e0
cmpb $0x0, 0x18(%rsi)
jne 0xa35b
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0xa1f4
cmpb $0x0, 0x18(%rsi)
jne 0xa35b
testq %r13, %r13
je 0xa254
testq %rax, %rax
je 0xa34d
movq 0x8(%rax), %rsi
testq %rsi, %rsi
je 0xa214
movb 0x18(%rsi), %dil
testb %dil, %dil
jne 0xa22c
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0xa25e
cmpb $0x0, 0x18(%rdi)
je 0xa25e
testq %rsi, %rsi
je 0xa26d
movb 0x18(%rsi), %dil
cmpb $0x1, %dil
jne 0xa26d
testl %r14d, %r14d
js 0xa294
movq %rbx, %rsi
testq %rcx, %rcx
je 0xa2dd
testl %edx, %edx
js 0xa2d6
addq $0x10, %rcx
jmp 0xa2da
movb $0x1, %al
movq %r12, %rcx
jmp 0xa358
movb $0x1, %cl
movb %cl, 0x18(%r12)
movb %cl, 0x18(%rax)
jmp 0xa353
testl %r14d, %r14d
jle 0xa285
testq %rcx, %rcx
je 0xa2aa
movq 0x10(%rax), %rsi
testl %edx, %edx
js 0xa2b3
movq %rsi, 0x10(%rcx)
jmp 0xa2b7
testq %rcx, %rcx
je 0xa2d1
testl %edx, %edx
js 0xa306
movq %rax, 0x10(%rcx)
jmp 0xa30e
testq %rcx, %rcx
je 0xa301
testl %edx, %edx
js 0xa32d
movq %rsi, 0x10(%rcx)
jmp 0xa335
movq 0x10(%rax), %rsi
movq %rsi, (%rbx)
jmp 0xa2b7
movq %rsi, 0x8(%rcx)
movq 0x10(%rsi), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rsi)
movq 0x8(%rsi), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x8(%rsi)
jmp 0xa34d
movq %rax, (%rbx)
jmp 0xa30e
addq $0x8, %rcx
movq %rcx, %rsi
movq %rax, (%rsi)
movq 0x10(%rax), %rcx
movq %rcx, 0x8(%r13)
movq %r13, 0x10(%rax)
movb $0x1, %cl
movb %cl, 0x18(%r12)
movb %cl, 0x18(%rax)
movb $0x0, 0x18(%r13)
movq 0x8(%rax), %rcx
jmp 0xa329
movq %rsi, (%rbx)
jmp 0xa335
movq %rax, 0x8(%rcx)
movq 0x8(%rax), %rsi
movq %rsi, 0x10(%r13)
movq %r13, 0x8(%rax)
movb $0x1, %cl
movb %cl, 0x18(%r12)
movb %cl, 0x18(%rax)
movb $0x0, 0x18(%r13)
movq 0x10(%rax), %rcx
xorl %eax, %eax
jmp 0xa358
movq %rsi, 0x8(%rcx)
movq 0x8(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, 0x10(%r13)
movq %r13, 0x8(%rsi)
movq 0x10(%rsi), %rcx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rsi)
movb $0x1, 0x18(%r12)
xorl %eax, %eax
movq %r13, %rcx
movb %al, 0x18(%rcx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r15, %r15
jne 0xa38d
movq (%r12), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
testl %eax, %eax
je 0xa37a
movl %eax, %ebp
xorl %r15d, %r15d
jmp 0xa388
movq (%r12), %r15
movl $0x1, %ebp
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %r8
leaq 0x8(%r12), %rax
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0xa3b2
cmpb $0x1, 0x18(%rcx)
jne 0xa3b2
testl %ebp, %ebp
js 0xa3d1
movq 0x10(%r12), %rdx
testq %rdx, %rdx
jne 0xa3c6
jmp 0xa3f1
movq 0x10(%r12), %rdx
testq %rdx, %rdx
je 0xa3e9
cmpb $0x1, 0x18(%rdx)
jne 0xa3e9
testl %ebp, %ebp
js 0xa41c
cmpb $0x1, 0x18(%rdx)
jne 0xa3f1
leaq 0x10(%r12), %rax
movq (%rax), %r13
testq %r15, %r15
je 0xa401
movq %r12, %rcx
movl %ebp, %r14d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xa4b0
movq %r13, %rcx
jmp 0xa4ad
testq %r13, %r13
je 0xa43c
testl %r14d, %r14d
js 0xa441
movq %rcx, 0x10(%r13)
jmp 0xa448
movq (%r13), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
testl %eax, %eax
je 0xa467
movq %r12, %rcx
xorl %r15d, %r15d
movl %ebp, %r14d
movl %eax, %ebp
jmp 0xa47b
testq %rcx, %rcx
je 0xa427
cmpb $0x1, 0x18(%rcx)
je 0xa3d1
testq %r13, %r13
je 0xa482
movq 0x10(%r12), %rcx
testl %r14d, %r14d
js 0xa48c
movq %rcx, 0x10(%r13)
jmp 0xa490
movq %rcx, (%rbx)
jmp 0xa448
movq %rcx, 0x8(%r13)
movq (%rax), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x8(%r12)
movq %r12, 0x10(%rcx)
movb $0x1, 0x18(%r12)
movb $0x0, 0x18(%rcx)
movl $0x1, %r14d
jmp 0xa4ad
movq (%r13), %r15
movq %r12, %rcx
movq %r13, 0x10(%rsp)
movl %ebp, %r14d
movl $0x1, %ebp
movq 0x8(%rsp), %r8
jmp 0xa4b0
movq 0x10(%r12), %rcx
movq %rcx, (%rbx)
jmp 0xa490
movq %rcx, 0x8(%r13)
movq 0x8(%rcx), %rax
movq %rax, 0x10(%r12)
movq %r12, 0x8(%rcx)
movb $0x1, 0x18(%r12)
movb $0x0, 0x18(%rcx)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %r12, %r13
movl %r14d, %edx
movl %ebp, %eax
shrl $0x1f, %eax
movl %ebp, %esi
notl %esi
shrl $0x1f, %esi
movq 0x8(%r13,%rsi,8), %r12
movq 0x8(%r13,%rax,8), %rax
movl %ebp, %r14d
testq %r12, %r12
jne 0xa1cc
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0xa515
movq (%r13), %rax
movq %rax, (%rdx)
testq %rcx, %rcx
je 0xa500
movq 0x10(%r13), %rax
cmpq %r13, 0x8(%rcx)
je 0xa509
movq %rax, 0x10(%rcx)
jmp 0xa50d
xorl %r15d, %r15d
jmp 0xa521
movq 0x10(%r13), %rax
movq %rax, (%rbx)
jmp 0xa50d
movq %rax, 0x8(%rcx)
movq %r13, %rdi
callq 0x1120
movq (%rbx), %rax
testq %rax, %rax
je 0xa521
movb $0x0, 0x18(%rax)
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d49(%rip), %rdi      # 0xe283
leaq 0x3ccc(%rip), %rsi      # 0xe20d
leaq 0x43c9(%rip), %rcx      # 0xe911
movl $0xb0b, %edx            # imm = 0xB0B
callq 0x1080
leaq 0x3da9(%rip), %rdi      # 0xe302
leaq 0x3cad(%rip), %rsi      # 0xe20d
leaq 0x43aa(%rip), %rcx      # 0xe911
movl $0xb0c, %edx            # imm = 0xB0C
callq 0x1080