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

    rb_node *anchor   = NULL;   // We need to store the last 4 levels:
    rb_node *granpa   = NULL;   //
    rb_node *parent   = NULL;   //            anchor
    rb_node *node     = NULL;   //              |    <- comp_g
    void    *old_data = NULL;   //            granpa
    int      comp_g   = 0;      //              |    <- comp_p
    int      comp_p   = 0;      //            parent
    int      comp_n   = 0;      //              |    <- comp_n
    int      comp     = 0;      //             node

    // 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) {

            // Create a new node:
            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;
                comp        = 0;
            }

            // And attach it bellow "parent":
            if (parent == NULL)  { tree->root    = node; }
            else if (comp_n < 0) { parent->left  = node; }
            else                 { parent->right = node; }

        // Otherwise "node" is an interior node:
        } else {

            // Compare "data" with "node->data":
            comp = (tree->comp)(data, node->data);

            // If the data is already there: Update and remember "old_data"
            if (comp == 0) {
                old_data   = node->data;
                node->data = data;
            }

            // If "node" has two RED children: Make a color flip
            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:
        if (IS_RED(node) && IS_RED(parent)) {

            // Case 1: Single "granpa-parent" left rotation
            if (comp_p > 0 && comp_n > 0) {

                granpa->right = parent->left;
                granpa->color = RED;
                parent->left  = granpa;
                parent->color = BLACK;
                
                if  (anchor == NULL) { tree->root    = parent; }
                else if (comp_g < 0) { anchor->left  = parent; }
                else if (comp_g > 0) { anchor->right = parent; }
                granpa = anchor;
                comp_p = comp_g;

            // Case 2: Single "granpa-parent" right rotation
            } else if (comp_p < 0 && comp_n < 0) {

                granpa->left  = parent->right;
                granpa->color = RED;
                parent->right = granpa;
                parent->color = BLACK;
                
                if  (anchor == NULL) { tree->root    = parent; }
                else if (comp_g < 0) { anchor->left  = parent; }
                else if (comp_g > 0) { anchor->right = parent; }
                granpa = anchor;
                comp_p = comp_g;

            // Case 3: Double "granpa-parent-node" rotation
            } else {

                // Case 3.1: Left-Right
                if (comp_n < 0) {
                    granpa->right = node->left;
                    granpa->color = RED;
                    parent->left  = node->right;
                    node->left    = granpa;
                    node->right   = parent;
                    node->color   = BLACK;
                    if (comp > 0) { granpa = parent; }
                    parent = node;
                    node   = granpa;
                    if (comp > 0) { comp_n *= -1;      }
                    if (comp < 0) { comp_n  = -comp_p; }
                    
                // Case 3.2: Right-Left
                } else {
                    granpa->left  = node->right;
                    granpa->color = RED;
                    parent->right = node->left;
                    node->right   = granpa;
                    node->left    = parent;
                    node->color   = BLACK;
                    if (comp < 0) { granpa = parent; }
                    parent = node;
                    node   = granpa;
                    if (comp < 0) { comp_n *= -1;      }
                    if (comp > 0) { comp_n  = -comp_p; }
                }

                if  (anchor == NULL) { tree->root    = parent; }
                else if (comp_g < 0) { anchor->left  = parent; }
                else if (comp_g > 0) { anchor->right = parent; }
                granpa = anchor;
                comp_p = comp_g;
                comp  *= -1;
            }
        }

        // Advance one step:
        anchor = granpa;
        granpa = parent;
        parent = node;
        if      (comp < 0) { node = node->left;  }  // Data is smaller
        else if (comp > 0) { node = node->right; }  // Data is bigger
        else               { break;              }  // We are done!

        // And remember were you come from:
        comp_g = comp_p;
        comp_p = comp_n;
        comp_n = comp;
    }

    // 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
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
je 0x9c17
movq %rsi, %r14
testq %rsi, %rsi
je 0x9c36
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%rax), %r12
testq %r12, %r12
je 0x9a41
movq (%r12), %rsi
movq %r14, %rdi
movq 0x18(%rsp), %rax
callq *0x8(%rax)
testl %eax, %eax
jne 0x9a0f
movq (%r12), %rcx
movq %rcx, 0x20(%rsp)
movq %r14, (%r12)
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0x9a84
cmpb $0x1, 0x18(%rcx)
jne 0x9a84
movq 0x10(%r12), %rdx
testq %rdx, %rdx
je 0x9a84
cmpb $0x1, 0x18(%rdx)
jne 0x9a84
movb $0x1, 0x18(%r12)
xorl %esi, %esi
movb %sil, 0x18(%rcx)
movb %sil, 0x18(%rdx)
jmp 0x9a84
movl $0x20, %edi
callq 0x10d0
testq %rax, %rax
je 0x9bed
movq %rax, %r12
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r13, %r13
je 0x9a74
testl %ebx, %ebx
js 0x9a7e
movq %r12, 0x10(%r13)
jmp 0x9a82
movq 0x18(%rsp), %rax
movq %r12, (%rax)
jmp 0x9a82
movq %r12, 0x8(%r13)
xorl %eax, %eax
cmpb $0x1, 0x18(%r12)
jne 0x9ab6
testq %r13, %r13
je 0x9ab6
cmpb $0x1, 0x18(%r13)
jne 0x9ab6
testl %r15d, %r15d
jle 0x9ae8
testl %ebx, %ebx
jle 0x9ae8
movq 0x8(%r13), %rcx
movq %rcx, 0x10(%rbp)
movb $0x1, 0x18(%rbp)
movq %rbp, 0x8(%r13)
jmp 0x9b44
movq %rbp, 0x10(%rsp)
movq %r13, %rbp
movq %r12, %r13
movl %r15d, 0xc(%rsp)
movl %ebx, %r15d
movl %eax, %ebx
testl %eax, %eax
js 0x9adb
movl $0x10, %eax
jne 0x9ae0
jmp 0x9bf2
movl $0x8, %eax
addq %r13, %rax
jmp 0x99e7
testl %r15d, %ebx
js 0x9b34
movl %ebx, %ecx
negl %ecx
negl %r15d
testl %ebx, %ebx
js 0x9b83
movq 0x10(%r12), %rdx
movq %rdx, 0x8(%rbp)
movb $0x1, 0x18(%rbp)
movq 0x8(%r12), %rdx
movq %rdx, 0x10(%r13)
movq %rbp, 0x10(%r12)
movq %r13, 0x8(%r12)
movb $0x0, 0x18(%r12)
testl %eax, %eax
cmovnsq %rbp, %r13
cmovsl %ecx, %ebx
cmovlel %ebx, %r15d
jmp 0x9bb6
movq 0x10(%r13), %rcx
movq %rcx, 0x8(%rbp)
movb $0x1, 0x18(%rbp)
movq %rbp, 0x10(%r13)
movb $0x0, 0x18(%r13)
cmpq $0x0, 0x10(%rsp)
je 0x9b65
cmpl $0x0, 0xc(%rsp)
js 0x9b6f
je 0x9b78
movq 0x10(%rsp), %rcx
movq %r13, 0x10(%rcx)
jmp 0x9b78
movq 0x18(%rsp), %rcx
movq %r13, (%rcx)
jmp 0x9b78
movq 0x10(%rsp), %rcx
movq %r13, 0x8(%rcx)
movq %r13, %rbp
movq %r12, %r13
jmp 0x9ac6
movq 0x8(%r12), %rdx
movq %rdx, 0x10(%rbp)
movb $0x1, 0x18(%rbp)
movq 0x10(%r12), %rdx
movq %rdx, 0x8(%r13)
movq %rbp, 0x8(%r12)
movq %r13, 0x10(%r12)
movb $0x0, 0x18(%r12)
testl %eax, %eax
cmovleq %rbp, %r13
cmovgl %ecx, %ebx
cmovnsl %ebx, %r15d
movq 0x18(%rsp), %rcx
cmpq $0x0, 0x10(%rsp)
je 0x9be0
cmpl $0x0, 0xc(%rsp)
js 0x9bd7
je 0x9be3
movq 0x10(%rsp), %rcx
addq $0x10, %rcx
jmp 0x9be0
movq 0x10(%rsp), %rcx
addq $0x8, %rcx
movq %r12, (%rcx)
negl %eax
movq %r12, %rbp
jmp 0x9ac9
callq 0x1344
movq 0x18(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x9c03
movb $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4665(%rip), %rdi      # 0xe283
leaq 0x45e8(%rip), %rsi      # 0xe20d
leaq 0x4b3e(%rip), %rcx      # 0xe76a
movl $0x90c, %edx            # imm = 0x90C
callq 0x1080
leaq 0x46c5(%rip), %rdi      # 0xe302
leaq 0x45c9(%rip), %rsi      # 0xe20d
leaq 0x4b1f(%rip), %rcx      # 0xe76a
movl $0x90d, %edx            # imm = 0x90D
callq 0x1080