// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
testq %rdi, %rdi
je 0x9582
movq %rsi, %r8
testq %rsi, %rsi
je 0x95a1
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r8, 0x20(%rsp)
movq (%rax), %r13
testq %r13, %r13
je 0x9394
movq (%r13), %rsi
movq %r8, %rdi
movq 0x10(%rsp), %rax
callq *0x8(%rax)
movq 0x20(%rsp), %r8
testl %eax, %eax
jne 0x9365
movq (%r13), %rcx
movq %rcx, 0x18(%rsp)
movq %r8, (%r13)
movq 0x8(%r13), %rcx
testq %rcx, %rcx
je 0x93dc
cmpb $0x1, 0x18(%rcx)
jne 0x93dc
movq 0x10(%r13), %rdx
testq %rdx, %rdx
je 0x93dc
cmpb $0x1, 0x18(%rdx)
jne 0x93dc
movb $0x1, 0x18(%r13)
xorl %esi, %esi
movb %sil, 0x18(%rcx)
movb %sil, 0x18(%rdx)
jmp 0x93dc
pushq $0x20
popq %rdi
callq 0x10d0
testq %rax, %rax
je 0x9541
movq %rax, %r13
movq 0x20(%rsp), %r8
movq %r8, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r12, %r12
je 0x93cb
testl %ebx, %ebx
js 0x93d5
movq %r13, 0x10(%r12)
jmp 0x93da
movq 0x10(%rsp), %rax
movq %r13, (%rax)
jmp 0x93da
movq %r13, 0x8(%r12)
xorl %eax, %eax
cmpb $0x1, 0x18(%r13)
jne 0x9434
testq %r12, %r12
je 0x9434
cmpb $0x1, 0x18(%r12)
jne 0x9434
testl %ebp, %ebp
jle 0x945d
testl %ebx, %ebx
jle 0x945d
movq 0x8(%r12), %rcx
movq %rcx, 0x10(%r14)
movb $0x1, 0x18(%r14)
movq %r14, 0x8(%r12)
movb $0x0, 0x18(%r12)
testq %r15, %r15
je 0x94cc
cmpl $0x0, 0xc(%rsp)
js 0x94d9
je 0x94c2
movq %r12, 0x10(%r15)
jmp 0x94dd
movq %r14, %r15
movq %r12, %r14
movq %r13, %r12
movl %ebp, 0xc(%rsp)
movl %ebx, %ebp
movl %eax, %ebx
pushq $0x8
popq %rax
testl %ebx, %ebx
js 0x9455
pushq $0x10
popq %rax
je 0x955d
addq %r12, %rax
jmp 0x9338
testl %ebp, %ebx
js 0x9497
movl %ebx, %ecx
negl %ecx
negl %ebp
testl %ebx, %ebx
js 0x94e8
movq 0x10(%r13), %rdx
movq %rdx, 0x8(%r14)
movb $0x1, 0x18(%r14)
movq 0x8(%r13), %rdx
movq %rdx, 0x10(%r12)
movq %r14, 0x10(%r13)
movq %r12, 0x8(%r13)
testl %eax, %eax
cmovnsq %r14, %r12
cmovsl %ecx, %ebx
cmovlel %ebx, %ebp
jmp 0x9512
movq 0x10(%r12), %rcx
movq %rcx, 0x8(%r14)
movb $0x1, 0x18(%r14)
movq %r14, 0x10(%r12)
movb $0x0, 0x18(%r12)
testq %r15, %r15
je 0x94cc
cmpl $0x0, 0xc(%rsp)
js 0x94d9
jne 0x942b
movl $0x0, 0xc(%rsp)
jmp 0x94dd
movq 0x10(%rsp), %rcx
movq %r12, (%rcx)
xorl %r15d, %r15d
jmp 0x94dd
movq %r12, 0x8(%r15)
movq %r12, %r14
movq %r13, %r12
jmp 0x9441
movq 0x8(%r13), %rdx
movq %rdx, 0x10(%r14)
movb $0x1, 0x18(%r14)
movq 0x10(%r13), %rdx
movq %rdx, 0x8(%r12)
movq %r14, 0x8(%r13)
movq %r12, 0x10(%r13)
testl %eax, %eax
cmovleq %r14, %r12
cmovgl %ecx, %ebx
cmovnsl %ebx, %ebp
movb $0x0, 0x18(%r13)
movq 0x10(%rsp), %rcx
testq %r15, %r15
je 0x9534
cmpl $0x0, 0xc(%rsp)
js 0x9530
je 0x9537
leaq 0x10(%r15), %rcx
jmp 0x9534
leaq 0x8(%r15), %rcx
movq %r13, (%rcx)
negl %eax
movq %r13, %r14
jmp 0x9443
movq 0x7ab0(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x4240(%rip), %rdi      # 0xd792
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
movq 0x10(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x956e
movb $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3cfa(%rip), %rdi      # 0xd283
leaq 0x3c7d(%rip), %rsi      # 0xd20d
leaq 0x41d3(%rip), %rcx      # 0xd76a
movl $0x90c, %edx            # imm = 0x90C
callq 0x1080
leaq 0x3d5a(%rip), %rdi      # 0xd302
leaq 0x3c5e(%rip), %rsi      # 0xd20d
leaq 0x41b4(%rip), %rcx      # 0xd76a
movl $0x90d, %edx            # imm = 0x90D
callq 0x1080