// File: /workspace/github_c_makefile_star2_O0/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: rb_tree_remove_min
void *rb_tree_remove_min(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;   //   node  sister   //

    // 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->right: Double rotation
                        if  (granpa == NULL) { tree->root    = sister->left; }
                        else                 { granpa->left  = 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->right is RED:
                    else {

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

        // 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->left)){

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

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

    // Erase "parent", which should be RED:
    old_data = parent->data;
    if (granpa == NULL) { tree->root   = parent->right; }
    else                { granpa->left = 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x117cd
jmp 0x117ec
leaq 0x4a9e(%rip), %rdi      # 0x16272
leaq 0x4a21(%rip), %rsi      # 0x161fc
movl $0xbff, %edx            # imm = 0xBFF
leaq 0x5154(%rip), %rcx      # 0x1693b
callq 0x1060
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1180b
movq $0x0, -0x8(%rbp)
jmp 0x11b89
jmp 0x1180d
cmpq $0x0, -0x30(%rbp)
je 0x11b30
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x11838
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x0, %eax
jne 0x11a07
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x11858
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x0, %eax
jne 0x11a07
cmpq $0x0, -0x20(%rbp)
jne 0x1186c
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
jmp 0x11a05
cmpq $0x0, -0x28(%rbp)
jne 0x11888
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x11a03
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x118a4
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x0, %eax
jne 0x118dd
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x118c0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x0, %eax
jne 0x118dd
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x11a01
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1198b
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x1198b
cmpq $0x0, -0x18(%rbp)
jne 0x11919
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x11929
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x119ff
cmpq $0x0, -0x18(%rbp)
jne 0x1199f
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x119ab
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movb $0x0, 0x18(%rax)
jmp 0x11a01
jmp 0x11a03
jmp 0x11a05
jmp 0x11a07
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x11a23
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
je 0x11a47
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x11b03
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x11b03
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x11a8d
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x11a8d
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x11b01
cmpq $0x0, -0x20(%rbp)
jne 0x11aa5
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x11ab5
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x11b03
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1180d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x11b53
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x11b63
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rdi
callq 0x10e0
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x11b81
movq -0x10(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x18(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)