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

    rb_node *anchor   = NULL;
    rb_node *granpa   = NULL;
    rb_node *parent   = NULL;
    rb_node *node     = NULL;
    void    *old_data = NULL;
    int      inserted = NO;

    // Sanity Checks:
    assert(tree != NULL);
    assert(data != NULL);

    // Search for the correct place to insert data:
    node = tree->root;
    for (;;) {

        // If we reach a leaf we must insert "data" here:
        if (node == NULL) {

            // Perhaps "parent" already contains "data":
            if (parent != NULL && (tree->comp)(data, parent->data) == 0) {
                old_data   = node->data;
                node->data = data;
                break;

            // Otherwise: Create a new node 
            } else {            
                node = (rb_node *) malloc(sizeof(rb_node));
                if (node == NULL) {
                    fprintf(stderr, "ERROR: Unable to allocate rb_node\n");
                    break;
                } else {
                    node->data  = data;
                    node->left  = NULL;
                    node->right = NULL;
                    node->color = RED;
                    inserted    = YES;
                }
                if (parent == NULL)  { tree->root    = node; }
                else                 { parent->right = node; }
            }            
            
        // Otherwise: "node" may require a color flip
        } else if (IS_RED(node->left) && IS_RED(node->right)) {
            node->color        = RED;
            node->left->color  = BLACK;
            node->right->color = BLACK;
        }

        // Repair any violation of the RED property: Single left rotation
        if (IS_RED(node) && IS_RED(parent)) {
            granpa->right = parent->left;
            granpa->color = RED;
            parent->left  = granpa;
            parent->color = BLACK;
            if  (anchor == NULL) { tree->root    = parent; }
            else                 { anchor->right = parent; }
            granpa = anchor;
        }

        // Advance one step or exit:
        if (inserted == YES) { break; }
        anchor = granpa;
        granpa = parent;
        parent = node;
        node   = node->right;        
    }

    // 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
pushq %rax
testq %rdi, %rdi
je 0x92aa
movq %rsi, %r14
testq %rsi, %rsi
je 0x92c9
movq %rdi, %rbx
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %eax, %eax
movq %rdi, %rcx
movq %rax, %rbp
movq (%rcx), %r12
testq %r12, %r12
je 0x91ea
movq 0x8(%r12), %rcx
movq %r12, %rax
testq %rcx, %rcx
je 0x9226
cmpb $0x1, 0x18(%rcx)
movq %r12, %rax
jne 0x9226
movq 0x10(%r12), %rdx
movq %r12, %rax
testq %rdx, %rdx
je 0x9226
cmpb $0x1, 0x18(%rdx)
movq %r12, %rax
jne 0x9226
movb $0x1, 0x18(%r12)
xorl %eax, %eax
movb %al, 0x18(%rcx)
movb %al, 0x18(%rdx)
movq %r12, %rax
jmp 0x9226
testq %rbp, %rbp
je 0x91f9
movq (%rbp), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
pushq $0x20
popq %rdi
callq 0x10d0
testq %rax, %rax
je 0x928c
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %rbp, %rbp
je 0x9223
movq %rax, 0x10(%rbp)
jmp 0x9226
movq %rax, (%rbx)
testq %rbp, %rbp
je 0x925c
cmpb $0x1, 0x18(%rax)
jne 0x925c
cmpb $0x1, 0x18(%rbp)
jne 0x925c
movq 0x8(%rbp), %rcx
movq %rcx, 0x10(%r13)
movb $0x1, 0x18(%r13)
movq %r13, 0x8(%rbp)
movb $0x0, 0x18(%rbp)
leaq 0x10(%r15), %rcx
testq %r15, %r15
cmoveq %rbx, %rcx
movq %rbp, (%rcx)
jmp 0x925f
movq %r13, %r15
leaq 0x10(%rax), %rcx
movq %rbp, %r13
testq %r12, %r12
jne 0x91a0
movq (%rbx), %rax
testq %rax, %rax
je 0x927b
movb $0x0, 0x18(%rax)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7d65(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x44f5(%rip), %rdi      # 0xd792
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x926f
leaq 0x3fd2(%rip), %rdi      # 0xd283
leaq 0x3f55(%rip), %rsi      # 0xd20d
leaq 0x4522(%rip), %rcx      # 0xd7e1
movl $0xa05, %edx            # imm = 0xA05
callq 0x1080
leaq 0x4032(%rip), %rdi      # 0xd302
leaq 0x3f36(%rip), %rsi      # 0xd20d
leaq 0x4503(%rip), %rcx      # 0xd7e1
movl $0xa06, %edx            # imm = 0xA06
callq 0x1080