// File: /workspace/github_c_makefile_star2_O1/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 0x9d78
movq %rsi, %r14
testq %rsi, %rsi
je 0x9d97
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 0x9cc9
movq 0x8(%rbp), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x9d07
cmpb $0x1, 0x18(%rcx)
movq %rbp, %rax
jne 0x9d07
movq 0x10(%rbp), %rdx
movq %rbp, %rax
testq %rdx, %rdx
je 0x9d07
cmpb $0x1, 0x18(%rdx)
movq %rbp, %rax
jne 0x9d07
movb $0x1, 0x18(%rbp)
xorl %eax, %eax
movb %al, 0x18(%rcx)
movb %al, 0x18(%rdx)
movq %rbp, %rax
jmp 0x9d07
testq %r13, %r13
je 0x9cd8
movq (%r13), %rsi
movq %r14, %rdi
callq *0x8(%rbx)
movl $0x20, %edi
callq 0x10d0
testq %rax, %rax
je 0x9d71
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r13, %r13
je 0x9d04
movq %rax, 0x8(%r13)
jmp 0x9d07
movq %rax, (%rbx)
testq %r13, %r13
je 0x9d41
cmpb $0x1, 0x18(%rax)
jne 0x9d41
cmpb $0x1, 0x18(%r13)
jne 0x9d41
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 0x9d44
movq %r12, %r15
leaq 0x8(%rax), %rcx
movq %r13, %r12
testq %rbp, %rbp
jne 0x9c83
movq (%rbx), %rax
testq %rax, %rax
je 0x9d60
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 0x9d54
leaq 0x4504(%rip), %rdi      # 0xe283
leaq 0x4487(%rip), %rsi      # 0xe20d
leaq 0x4a28(%rip), %rcx      # 0xe7b5
movl $0x9ae, %edx            # imm = 0x9AE
callq 0x1080
leaq 0x4564(%rip), %rdi      # 0xe302
leaq 0x4468(%rip), %rsi      # 0xe20d
leaq 0x4a09(%rip), %rcx      # 0xe7b5
movl $0x9af, %edx            # imm = 0x9AF
callq 0x1080