// File: /workspace/github_c_makefile_star2_O3/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 0xa8d7
movq %rdi, %rbx
movq (%rdi), %rcx
testq %rcx, %rcx
je 0xa8c9
xorl %esi, %esi
movb $0x1, %al
xorl %edi, %edi
xorl %edx, %edx
movq 0x8(%rcx), %r8
testq %r8, %r8
je 0xa76a
cmpb $0x0, 0x18(%r8)
jne 0xa82e
movq 0x10(%rcx), %r8
testq %r8, %r8
je 0xa77e
cmpb $0x0, 0x18(%r8)
jne 0xa82e
testq %rdi, %rdi
je 0xa7e9
testq %rsi, %rsi
je 0xa822
movq 0x8(%rsi), %r8
testq %r8, %r8
je 0xa79e
movb 0x18(%r8), %r9b
testb %r9b, %r9b
jne 0xa7b7
movq 0x10(%rsi), %r9
testq %r9, %r9
je 0xa7f0
cmpb $0x0, 0x18(%r9)
je 0xa7f0
testq %r8, %r8
je 0xa7f8
movb 0x18(%r8), %r9b
cmpb $0x1, %r9b
jne 0xa7f8
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 %al, 0x18(%rcx)
movb %al, 0x18(%rsi)
movb $0x0, 0x18(%rdi)
movq 0x8(%rsi), %rsi
xorl %edx, %edx
jmp 0xa82b
movb $0x1, %dl
movq %rcx, %rsi
jmp 0xa82b
movb %al, 0x18(%rcx)
movb %al, 0x18(%rsi)
jmp 0xa826
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(%rcx)
xorl %edx, %edx
movq %rdi, %rsi
movb %dl, 0x18(%rsi)
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0xa848
cmpb $0x1, 0x18(%rdx)
jne 0xa848
movq 0x10(%rcx), %r8
testq %r8, %r8
jne 0xa858
jmp 0xa873
movq 0x10(%rcx), %r8
testq %r8, %r8
je 0xa86b
cmpb $0x1, 0x18(%r8)
jne 0xa86b
cmpb $0x1, 0x18(%r8)
jne 0xa873
movq 0x8(%r8), %rsi
movq %rcx, %rdx
movq %r8, %rdi
jmp 0xa898
movq %rdx, %rsi
movq %rdi, %rdx
jmp 0xa895
leaq 0x10(%rdi), %rsi
testq %rdi, %rdi
cmoveq %rbx, %rsi
movq %rdx, (%rsi)
movq 0x10(%rdx), %rsi
movq %rsi, 0x8(%rcx)
movq %rcx, 0x10(%rdx)
movb $0x1, 0x18(%rcx)
movb $0x0, 0x18(%rdx)
movq %rcx, %rdi
movq 0x10(%rdi), %rcx
testq %rcx, %rcx
jne 0xa756
movq (%rdi), %r14
leaq 0x10(%rdx), %rax
testq %rdx, %rdx
cmoveq %rbx, %rax
movq %rsi, (%rax)
callq 0x1120
movq (%rbx), %rax
testq %rax, %rax
je 0xa8cc
movb $0x0, 0x18(%rax)
jmp 0xa8cc
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x39a5(%rip), %rdi      # 0xe283
leaq 0x3928(%rip), %rsi      # 0xe20d
leaq 0x4077(%rip), %rcx      # 0xe963
movl $0xc8d, %edx            # imm = 0xC8D
callq 0x1080