// File: /workspace/github_c_makefile_star2_O3/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 0x9e76
movq %rsi, %r14
testq %rsi, %rsi
je 0x9e95
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 0x9dc7
movq 0x8(%rbp), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x9e00
cmpb $0x1, 0x18(%rcx)
movq %rbp, %rax
jne 0x9e00
movq 0x10(%rbp), %rdx
movq %rbp, %rax
testq %rdx, %rdx
je 0x9e00
cmpb $0x1, 0x18(%rdx)
movq %rbp, %rax
jne 0x9e00
movb $0x1, 0x18(%rbp)
xorl %eax, %eax
movb %al, 0x18(%rcx)
movb %al, 0x18(%rdx)
movq %rbp, %rax
jmp 0x9e00
testq %r13, %r13
je 0x9dd6
movq (%r13), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
movl $0x20, %edi
callq 0x10d0
testq %rax, %rax
je 0x9e6f
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r13, %r13
je 0x9e57
movq %rax, 0x8(%r13)
testq %r13, %r13
je 0x9e3a
cmpb $0x1, 0x18(%rax)
jne 0x9e3a
cmpb $0x1, 0x18(%r13)
jne 0x9e3a
movq 0x10(%r13), %rcx
movq %rcx, 0x8(%r12)
movb $0x1, 0x18(%r12)
movq %r12, 0x10(%r13)
movb $0x0, 0x18(%r13)
leaq 0x8(%r15), %rcx
testq %r15, %r15
cmoveq %rbx, %rcx
movq %r13, (%rcx)
jmp 0x9e3d
movq %r12, %r15
leaq 0x8(%rax), %rcx
movq %r13, %r12
testq %rbp, %rbp
jne 0x9d81
movq (%rbx), %rax
testq %rax, %rax
jne 0x9e5a
jmp 0x9e5e
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 0x1360
jmp 0x9e4d
leaq 0x4406(%rip), %rdi      # 0xe283
leaq 0x4389(%rip), %rsi      # 0xe20d
leaq 0x492a(%rip), %rcx      # 0xe7b5
movl $0x9ae, %edx            # imm = 0x9AE
callq 0x1080
leaq 0x4466(%rip), %rdi      # 0xe302
leaq 0x436a(%rip), %rsi      # 0xe20d
leaq 0x490b(%rip), %rcx      # 0xe7b5
movl $0x9af, %edx            # imm = 0x9AF
callq 0x1080