// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x1038d
jmp 0x103ac
leaq 0x5ede(%rip), %rdi      # 0x16272
leaq 0x5e61(%rip), %rsi      # 0x161fc
movl $0x90c, %edx            # imm = 0x90C
leaq 0x63bf(%rip), %rcx      # 0x16766
callq 0x1060
cmpq $0x0, -0x10(%rbp)
je 0x103b5
jmp 0x103d4
leaq 0x5f35(%rip), %rdi      # 0x162f1
leaq 0x5e39(%rip), %rsi      # 0x161fc
movl $0x90d, %edx            # imm = 0x90D
leaq 0x6397(%rip), %rcx      # 0x16766
callq 0x1060
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x10489
movl $0x20, %edi
callq 0x10a0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1041c
movq 0x9bf2(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x637e(%rip), %rsi      # 0x1678e
movb $0x0, %al
callq 0x1080
jmp 0x1082e
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movl $0x0, -0x48(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x10462
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10484
cmpl $0x0, -0x44(%rbp)
jge 0x10476
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x10482
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x10484
jmp 0x10517
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rcx
movq (%rcx), %rsi
callq *%rax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x104bd
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x10515
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10515
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x10515
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10515
movq -0x30(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movb $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movb $0x0, 0x18(%rax)
jmp 0x10517
cmpq $0x0, -0x30(%rbp)
je 0x107d3
movq -0x30(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x107d3
cmpq $0x0, -0x28(%rbp)
je 0x107d3
movq -0x28(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x107d3
cmpl $0x0, -0x40(%rbp)
jle 0x105dc
cmpl $0x0, -0x44(%rbp)
jle 0x105dc
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x18(%rax)
cmpq $0x0, -0x18(%rbp)
jne 0x1059f
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x105c9
cmpl $0x0, -0x3c(%rbp)
jge 0x105b3
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x105c7
cmpl $0x0, -0x3c(%rbp)
jle 0x105c5
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x105c7
jmp 0x105c9
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x107d1
cmpl $0x0, -0x40(%rbp)
jge 0x10669
cmpl $0x0, -0x44(%rbp)
jge 0x10669
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x18(%rax)
cmpq $0x0, -0x18(%rbp)
jne 0x1062c
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10656
cmpl $0x0, -0x3c(%rbp)
jge 0x10640
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x10654
cmpl $0x0, -0x3c(%rbp)
jle 0x10652
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x10654
jmp 0x10656
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x107cf
cmpl $0x0, -0x44(%rbp)
jge 0x106f9
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x18(%rax)
cmpl $0x0, -0x48(%rbp)
jle 0x106c9
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x48(%rbp)
jle 0x106e6
imull $-0x1, -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x106f4
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1077c
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x18(%rax)
cmpl $0x0, -0x48(%rbp)
jge 0x1074f
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x1076c
imull $-0x1, -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x48(%rbp)
jle 0x1077a
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1077c
cmpq $0x0, -0x18(%rbp)
jne 0x10790
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x107ba
cmpl $0x0, -0x3c(%rbp)
jge 0x107a4
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x107b8
cmpl $0x0, -0x3c(%rbp)
jle 0x107b6
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x107b8
jmp 0x107ba
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
imull $-0x1, -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x107d1
jmp 0x107d3
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x107ff
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x10817
cmpl $0x0, -0x48(%rbp)
jle 0x10813
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x10815
jmp 0x1082e
jmp 0x10817
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x103df
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x10843
movq -0x8(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x18(%rax)
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)