// File: /workspace/github_c_makefile_star2_O2/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: rb_tree_insert_min
void *rb_tree_insert_min(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->left = 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 right rotation
        if (IS_RED(node) && IS_RED(parent)) {            
            granpa->left  = parent->right;
            granpa->color = RED;
            parent->right = granpa;
            parent->color = BLACK;            
            if  (anchor == NULL) { tree->root   = parent; }
            else                 { anchor->left = parent; }
            granpa = anchor;
        }

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

    // 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 0x96f8
movq %rsi, %r14
testq %rsi, %rsi
je 0x9717
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 0x9638
movq 0x8(%r12), %rcx
movq %r12, %rax
testq %rcx, %rcx
je 0x9674
cmpb $0x1, 0x18(%rcx)
movq %r12, %rax
jne 0x9674
movq 0x10(%r12), %rdx
movq %r12, %rax
testq %rdx, %rdx
je 0x9674
cmpb $0x1, 0x18(%rdx)
movq %r12, %rax
jne 0x9674
movb $0x1, 0x18(%r12)
xorl %eax, %eax
movb %al, 0x18(%rcx)
movb %al, 0x18(%rdx)
movq %r12, %rax
jmp 0x9674
testq %rbp, %rbp
je 0x9647
movq (%rbp), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
pushq $0x20
popq %rdi
callq 0x10d0
testq %rax, %rax
je 0x96da
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %rbp, %rbp
je 0x9671
movq %rax, 0x8(%rbp)
jmp 0x9674
movq %rax, (%rbx)
testq %rbp, %rbp
je 0x96aa
cmpb $0x1, 0x18(%rax)
jne 0x96aa
cmpb $0x1, 0x18(%rbp)
jne 0x96aa
movq 0x10(%rbp), %rcx
movq %rcx, 0x8(%r13)
movb $0x1, 0x18(%r13)
movq %r13, 0x10(%rbp)
movb $0x0, 0x18(%rbp)
leaq 0x8(%r15), %rcx
testq %r15, %r15
cmoveq %rbx, %rcx
movq %rbp, (%rcx)
jmp 0x96ad
movq %r13, %r15
leaq 0x8(%rax), %rcx
movq %rbp, %r13
testq %r12, %r12
jne 0x95ee
movq (%rbx), %rax
testq %rax, %rax
je 0x96c9
movb $0x0, 0x18(%rax)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7917(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x40a7(%rip), %rdi      # 0xd792
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x96bd
leaq 0x3b84(%rip), %rdi      # 0xd283
leaq 0x3b07(%rip), %rsi      # 0xd20d
leaq 0x40a8(%rip), %rcx      # 0xd7b5
movl $0x9ae, %edx            # imm = 0x9AE
callq 0x1080
leaq 0x3be4(%rip), %rdi      # 0xd302
leaq 0x3ae8(%rip), %rsi      # 0xd20d
leaq 0x4089(%rip), %rcx      # 0xd7b5
movl $0x9af, %edx            # imm = 0x9AF
callq 0x1080