// File: /workspace/github_c_makefile_star2_O3/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 0x9a1a
movq %rsi, %r14
testq %rsi, %rsi
je 0x9a39
movq %rdi, %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %eax, %eax
movq %rdi, %rcx
movq %rax, %r13
movq (%rcx), %rbp
testq %rbp, %rbp
je 0x996b
movq 0x8(%rbp), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x99a4
cmpb $0x1, 0x18(%rcx)
movq %rbp, %rax
jne 0x99a4
movq 0x10(%rbp), %rdx
movq %rbp, %rax
testq %rdx, %rdx
je 0x99a4
cmpb $0x1, 0x18(%rdx)
movq %rbp, %rax
jne 0x99a4
movb $0x1, 0x18(%rbp)
xorl %eax, %eax
movb %al, 0x18(%rcx)
movb %al, 0x18(%rdx)
movq %rbp, %rax
jmp 0x99a4
testq %r13, %r13
je 0x997a
movq (%r13), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
movl $0x20, %edi
callq 0x10d0
testq %rax, %rax
je 0x9a13
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r13, %r13
je 0x99fb
movq %rax, 0x10(%r13)
testq %r13, %r13
je 0x99de
cmpb $0x1, 0x18(%rax)
jne 0x99de
cmpb $0x1, 0x18(%r13)
jne 0x99de
movq 0x8(%r13), %rcx
movq %rcx, 0x10(%r12)
movb $0x1, 0x18(%r12)
movq %r12, 0x8(%r13)
movb $0x0, 0x18(%r13)
leaq 0x10(%r15), %rcx
testq %r15, %r15
cmoveq %rbx, %rcx
movq %r13, (%rcx)
jmp 0x99e1
movq %r12, %r15
leaq 0x10(%rax), %rcx
movq %r13, %r12
testq %rbp, %rbp
jne 0x9925
movq (%rbx), %rax
testq %rax, %rax
jne 0x99fe
jmp 0x9a02
movq %rax, (%rbx)
movb $0x0, 0x18(%rax)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1328
jmp 0x99f1
leaq 0x4862(%rip), %rdi      # 0xe283
leaq 0x47e5(%rip), %rsi      # 0xe20d
leaq 0x4db2(%rip), %rcx      # 0xe7e1
movl $0xa05, %edx            # imm = 0xA05
callq 0x1080
leaq 0x48c2(%rip), %rdi      # 0xe302
leaq 0x47c6(%rip), %rsi      # 0xe20d
leaq 0x4d93(%rip), %rcx      # 0xe7e1
movl $0xa06, %edx            # imm = 0xA06
callq 0x1080