// File: /workspace/github_c_makefile_star2_O3/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 0x9d15
movq %rsi, %r15
testq %rsi, %rsi
je 0x9d34
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq (%rax), %r12
testq %r12, %r12
je 0x9b03
movq (%r12), %rsi
movq %r15, %rdi
movq 0x18(%rsp), %rax
callq *0x8(%rax)
testl %eax, %eax
jne 0x9ad1
movq (%r12), %rcx
movq %rcx, 0x20(%rsp)
movq %r15, (%r12)
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0x9b40
cmpb $0x1, 0x18(%rcx)
jne 0x9b40
movq 0x10(%r12), %rdx
testq %rdx, %rdx
je 0x9b40
cmpb $0x1, 0x18(%rdx)
jne 0x9b40
movb $0x1, 0x18(%r12)
xorl %esi, %esi
movb %sil, 0x18(%rcx)
movb %sil, 0x18(%rdx)
jmp 0x9b40
movl $0x20, %edi
callq 0x10d0
testq %rax, %rax
je 0x9ce9
movq %rax, %r12
movq %r15, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x1, 0x18(%rax)
testq %r13, %r13
je 0x9cda
testl %ebx, %ebx
js 0x9b3a
movq %r12, 0x10(%r13)
jmp 0x9b3e
movq %r12, 0x8(%r13)
xorl %eax, %eax
cmpb $0x1, 0x18(%r12)
jne 0x9b9d
testq %r13, %r13
je 0x9b9d
cmpb $0x1, 0x18(%r13)
jne 0x9b9d
testl %r14d, %r14d
jle 0x9bcf
testl %ebx, %ebx
jle 0x9bcf
movq 0x8(%r13), %rcx
movq %rcx, 0x10(%rbp)
movb $0x1, 0x18(%rbp)
movq %rbp, 0x8(%r13)
movb $0x0, 0x18(%r13)
cmpq $0x0, 0x10(%rsp)
je 0x9c49
cmpl $0x0, 0xc(%rsp)
js 0x9c5c
je 0x9c3f
movq 0x10(%rsp), %rcx
movq %r13, 0x10(%rcx)
jmp 0x9c65
movq %rbp, 0x10(%rsp)
movq %r13, %rbp
movq %r12, %r13
movl %r14d, 0xc(%rsp)
movl %ebx, %r14d
movl %eax, %ebx
testl %eax, %eax
js 0x9bc2
movl $0x10, %eax
jne 0x9bc7
jmp 0x9cee
movl $0x8, %eax
addq %r13, %rax
jmp 0x9aa9
testl %r14d, %ebx
js 0x9c15
movl %ebx, %ecx
negl %ecx
negl %r14d
testl %ebx, %ebx
js 0x9c70
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)
testl %eax, %eax
cmovnsq %rbp, %r13
cmovsl %ecx, %ebx
cmovlel %ebx, %r14d
jmp 0x9c9d
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 0x9c49
cmpl $0x0, 0xc(%rsp)
js 0x9c5c
jne 0x9b8f
movl $0x0, 0xc(%rsp)
jmp 0x9c65
movq 0x18(%rsp), %rcx
movq %r13, (%rcx)
movq $0x0, 0x10(%rsp)
jmp 0x9c65
movq 0x10(%rsp), %rcx
movq %r13, 0x8(%rcx)
movq %r13, %rbp
movq %r12, %r13
jmp 0x9bad
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)
testl %eax, %eax
cmovleq %rbp, %r13
cmovgl %ecx, %ebx
cmovnsl %ebx, %r14d
movb $0x0, 0x18(%r12)
movq 0x18(%rsp), %rcx
cmpq $0x0, 0x10(%rsp)
je 0x9ccd
cmpl $0x0, 0xc(%rsp)
js 0x9cc4
je 0x9cd0
movq 0x10(%rsp), %rcx
addq $0x10, %rcx
jmp 0x9ccd
movq 0x10(%rsp), %rcx
addq $0x8, %rcx
movq %r12, (%rcx)
negl %eax
movq %r12, %rbp
jmp 0x9bb0
movq 0x18(%rsp), %rax
movq %r12, (%rax)
movq 0x20(%rsp), %rax
jmp 0x9d00
callq 0x1344
movq 0x18(%rsp), %rax
movq (%rax), %r12
testq %r12, %r12
movq 0x20(%rsp), %rax
je 0x9d06
movb $0x0, 0x18(%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4567(%rip), %rdi      # 0xe283
leaq 0x44ea(%rip), %rsi      # 0xe20d
leaq 0x4a40(%rip), %rcx      # 0xe76a
movl $0x90c, %edx            # imm = 0x90C
callq 0x1080
leaq 0x45c7(%rip), %rdi      # 0xe302
leaq 0x44cb(%rip), %rsi      # 0xe20d
leaq 0x4a21(%rip), %rcx      # 0xe76a
movl $0x90d, %edx            # imm = 0x90D
callq 0x1080