// File: /workspace/github_c_makefile_star2_O1/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 0xa43a
movq %rsi, %r8
testq %rsi, %rsi
je 0xa459
movq %rdi, %rbx
movq (%rdi), %r12
xorl %r15d, %r15d
testq %r12, %r12
je 0xa402
xorl %edx, %edx
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %r8, 0x10(%rsp)
movq 0x8(%r12), %rsi
testq %rsi, %rsi
je 0xa0df
cmpb $0x0, 0x18(%rsi)
jne 0xa260
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0xa0f3
cmpb $0x0, 0x18(%rsi)
jne 0xa260
testq %r13, %r13
je 0xa14c
testq %rax, %rax
je 0xa252
movq 0x8(%rax), %rsi
testq %rsi, %rsi
je 0xa110
cmpb $0x0, 0x18(%rsi)
jne 0xa11f
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0xa16e
cmpb $0x0, 0x18(%rdi)
je 0xa16e
testq %rsi, %rsi
je 0xa156
cmpb $0x1, 0x18(%rsi)
jne 0xa156
testl %r15d, %r15d
js 0xa198
movq %rbx, %rsi
testq %rcx, %rcx
je 0xa206
testl %edx, %edx
js 0xa1ff
addq $0x10, %rcx
jmp 0xa203
movb $0x1, %al
movq %r12, %rcx
jmp 0xa25d
testl %r15d, %r15d
jle 0xa17d
testq %rcx, %rcx
je 0xa18f
movq 0x10(%rax), %rsi
testl %edx, %edx
js 0xa1b2
movq %rsi, 0x10(%rcx)
jmp 0xa1b6
movb $0x1, %cl
movb %cl, 0x18(%r12)
movb %cl, 0x18(%rax)
jmp 0xa258
movq %rbx, %rsi
testq %rcx, %rcx
je 0xa1db
testl %edx, %edx
js 0xa1d4
addq $0x10, %rcx
jmp 0xa1d8
movq 0x10(%rax), %rcx
movq %rcx, (%rbx)
jmp 0xa1b6
movq %rbx, %rdi
testq %rcx, %rcx
je 0xa233
testl %edx, %edx
js 0xa22c
addq $0x10, %rcx
jmp 0xa230
movq %rsi, 0x8(%rcx)
movq 0x10(%rax), %rcx
movq 0x10(%rcx), %rdx
movq %rdx, 0x8(%r13)
movq %r13, 0x10(%rcx)
movq 0x8(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq %rax, 0x8(%rcx)
jmp 0xa252
addq $0x8, %rcx
movq %rcx, %rsi
movq %rax, (%rsi)
movq 0x8(%rax), %rcx
movq %rcx, 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
jmp 0xa228
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
xorl %eax, %eax
jmp 0xa25d
addq $0x8, %rcx
movq %rcx, %rdi
movq %rsi, (%rdi)
movq 0x8(%rax), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, 0x10(%r13)
movq %r13, 0x8(%rcx)
movq 0x10(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq %rax, 0x10(%rcx)
movb $0x1, 0x18(%r12)
xorl %eax, %eax
movq %r13, %rcx
movb %al, 0x18(%rcx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r14, %r14
jne 0xa27b
movq (%r12), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
movq 0x10(%rsp), %r8
movl %eax, %ebp
testl %ebp, %ebp
jne 0xa28d
movq (%r12), %r14
movl $0x1, %ebp
movq %r12, 0x8(%rsp)
movq 0x8(%r12), %rax
testq %rax, %rax
je 0xa29d
cmpb $0x1, 0x18(%rax)
je 0xa2ad
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0xa30c
cmpb $0x1, 0x18(%rcx)
jne 0xa30c
testq %rax, %rax
sete %cl
testl %ebp, %ebp
setns %dl
orb %cl, %dl
jne 0xa2c2
cmpb $0x1, 0x18(%rax)
je 0xa2d6
testl %ebp, %ebp
jle 0xa314
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0xa314
cmpb $0x1, 0x18(%rcx)
jne 0xa314
movl %ebp, %eax
notl %eax
shrl $0x1f, %eax
movq 0x8(%r12,%rax,8), %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r14, %r14
jne 0xa2fb
movq (%r13), %rsi
movq %r8, %rdi
callq *0x8(%rbx)
movq 0x10(%rsp), %r8
testl %eax, %eax
je 0xa340
movq %r12, %rcx
movl %ebp, %r15d
movl %eax, %ebp
jmp 0xa3b7
movq %r13, %rcx
jmp 0xa3b4
testl %ebp, %ebp
js 0xa32b
movq %rbx, %rcx
testq %r13, %r13
je 0xa35d
testl %r15d, %r15d
js 0xa356
addq $0x10, %r13
jmp 0xa35a
testq %r13, %r13
je 0xa384
movq 0x10(%r12), %rax
testl %r15d, %r15d
js 0xa38e
movq %rax, 0x10(%r13)
jmp 0xa392
movq (%r13), %r14
movq %r12, %rcx
movq %r13, 0x8(%rsp)
movl %ebp, %r15d
movl $0x1, %ebp
jmp 0xa3b7
addq $0x8, %r13
movq %r13, %rcx
movq %rax, (%rcx)
movq 0x8(%r12), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x8(%r12)
movq %r12, 0x10(%rcx)
movb $0x1, 0x18(%r12)
movb $0x0, 0x18(%rcx)
movl $0x1, %r15d
jmp 0xa3b4
movq 0x10(%r12), %rax
movq %rax, (%rbx)
jmp 0xa392
movq %rax, 0x8(%r13)
movq 0x10(%r12), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x10(%r12)
movq %r12, 0x8(%rcx)
movb $0x1, 0x18(%r12)
movb $0x0, 0x18(%rcx)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %r12, %r13
movl %r15d, %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, %r15d
testq %r12, %r12
jne 0xa0cb
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xa41c
movq (%r13), %rax
movq %rax, (%rdx)
testq %rcx, %rcx
je 0xa407
movq 0x10(%r13), %rax
cmpq %r13, 0x8(%rcx)
je 0xa410
movq %rax, 0x10(%rcx)
jmp 0xa414
xorl %r14d, %r14d
jmp 0xa428
movq 0x10(%r13), %rax
movq %rax, (%rbx)
jmp 0xa414
movq %rax, 0x8(%rcx)
movq %r13, %rdi
callq 0x1120
movq (%rbx), %rax
testq %rax, %rax
je 0xa428
movb $0x0, 0x18(%rax)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e42(%rip), %rdi      # 0xe283
leaq 0x3dc5(%rip), %rsi      # 0xe20d
leaq 0x44c2(%rip), %rcx      # 0xe911
movl $0xb0b, %edx            # imm = 0xB0B
callq 0x1080
leaq 0x3ea2(%rip), %rdi      # 0xe302
leaq 0x3da6(%rip), %rsi      # 0xe20d
leaq 0x44a3(%rip), %rcx      # 0xe911
movl $0xb0c, %edx            # imm = 0xB0C
callq 0x1080