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

    rb_node *granpa   = NULL;   //      granpa      //
    rb_node *parent   = NULL;   //        |         //
    rb_node *sister   = NULL;   //      parent      //
    rb_node *node     = NULL;   //      /    \      // 
    void    *old_data = NULL;   //   sister node    //

    // Sanity Check:
    assert(tree != 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)) {

                        // Sister == parent->left: Single rotation
                        if  (granpa == NULL) { tree->root    = 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  (granpa == NULL) { tree->root    = 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;
                    }
                }
            }
        }

        // 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 (IS_RED(node->right)){

                // Move and compare again for free!
                granpa = parent;
                parent = node;
                node   = parent->right;
                sister = parent->left;
            }
            
            // Case 2.2: We are moving to the BLACK node
            else {
                // we are moving to the right: Single right-rotation
                if  (parent == NULL) { tree->root    = 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;
            }
        }

        // ...and finally move!
        granpa = parent;
        parent = node;
        node   = parent->right;
        sister = parent->left;
    }

    // Erase "parent", which should be RED:
    old_data = parent->data;
    if (granpa == NULL) { tree->root    = parent->left; }
    else                { granpa->right = parent->left; }
    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 %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0xa190
movq %rdi, %rbx
movq (%rdi), %rax
testq %rax, %rax
je 0xa182
xorl %edx, %edx
movb $0x1, %cl
xorl %edi, %edi
xorl %esi, %esi
testq %rax, %rax
je 0xa15a
movq 0x8(%rax), %r8
testq %r8, %r8
je 0xa023
cmpb $0x0, 0x18(%r8)
jne 0xa0e7
movq 0x10(%rax), %r8
testq %r8, %r8
je 0xa037
cmpb $0x0, 0x18(%r8)
jne 0xa0e7
testq %rdi, %rdi
je 0xa0a2
testq %rsi, %rsi
je 0xa0db
movq 0x8(%rsi), %r8
testq %r8, %r8
je 0xa057
movb 0x18(%r8), %r9b
testb %r9b, %r9b
jne 0xa070
movq 0x10(%rsi), %r9
testq %r9, %r9
je 0xa0a9
cmpb $0x0, 0x18(%r9)
je 0xa0a9
testq %r8, %r8
je 0xa0b1
movb 0x18(%r8), %r9b
cmpb $0x1, %r9b
jne 0xa0b1
leaq 0x10(%rdx), %r8
testq %rdx, %rdx
cmoveq %rbx, %r8
movq %rsi, (%r8)
movq 0x10(%rsi), %rdx
movq %rdx, 0x8(%rdi)
movq %rdi, 0x10(%rsi)
movb %cl, 0x18(%rax)
movb %cl, 0x18(%rsi)
movb $0x0, 0x18(%rdi)
movq 0x8(%rsi), %rsi
xorl %edx, %edx
jmp 0xa0e4
movb $0x1, %dl
movq %rax, %rsi
jmp 0xa0e4
movb %cl, 0x18(%rax)
movb %cl, 0x18(%rsi)
jmp 0xa0df
leaq 0x10(%rdx), %r8
testq %rdx, %rdx
movq 0x10(%rsi), %rdx
cmoveq %rbx, %r8
movq %rdx, (%r8)
movq 0x10(%rdx), %r8
movq %r8, 0x8(%rdi)
movq %rdi, 0x10(%rdx)
movq 0x8(%rdx), %r8
movq %r8, 0x10(%rsi)
movq %rsi, 0x8(%rdx)
movb $0x1, 0x18(%rax)
xorl %edx, %edx
movq %rdi, %rsi
movb %dl, 0x18(%rsi)
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xa101
cmpb $0x1, 0x18(%rdx)
jne 0xa101
movq 0x10(%rax), %r8
testq %r8, %r8
jne 0xa111
jmp 0xa12c
movq 0x10(%rax), %r8
testq %r8, %r8
je 0xa124
cmpb $0x1, 0x18(%r8)
jne 0xa124
cmpb $0x1, 0x18(%r8)
jne 0xa12c
movq 0x8(%r8), %rsi
movq %rax, %rdx
movq %r8, %rax
jmp 0xa14e
movq %rdx, %rsi
movq %rdi, %rdx
jmp 0xa14e
leaq 0x10(%rdi), %rsi
testq %rdi, %rdi
cmoveq %rbx, %rsi
movq %rdx, (%rsi)
movq 0x10(%rdx), %rsi
movq %rsi, 0x8(%rax)
movq %rax, 0x10(%rdx)
movb $0x1, 0x18(%rax)
movb $0x0, 0x18(%rdx)
movq %rax, %rdi
movq 0x10(%rax), %rax
jmp 0xa006
movq (%rdi), %r14
movq 0x8(%rdi), %rax
leaq 0x10(%rdx), %rcx
testq %rdx, %rdx
cmoveq %rbx, %rcx
movq %rax, (%rcx)
callq 0x1120
movq (%rbx), %rax
testq %rax, %rax
je 0xa185
movb $0x0, 0x18(%rax)
jmp 0xa185
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x30ec(%rip), %rdi      # 0xd283
leaq 0x306f(%rip), %rsi      # 0xd20d
leaq 0x37be(%rip), %rcx      # 0xd963
movl $0xc8d, %edx            # imm = 0xC8D
callq 0x1080