// File: /workspace/github_c_makefile_star2_O0/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: rb_tree_insert_max
void *rb_tree_insert_max(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->right = 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 left rotation
        if (IS_RED(node) && IS_RED(parent)) {
            granpa->right = parent->left;
            granpa->color = RED;
            parent->left  = granpa;
            parent->color = BLACK;
            if  (anchor == NULL) { tree->root    = parent; }
            else                 { anchor->right = parent; }
            granpa = anchor;
        }

        // Advance one step or exit:
        if (inserted == YES) { break; }
        anchor = granpa;
        granpa = parent;
        parent = node;
        node   = node->right;        
    }

    // 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 $0x40, %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)
cmpq $0x0, -0x8(%rbp)
je 0x100e8
jmp 0x10107
leaq 0x6183(%rip), %rdi      # 0x16272
leaq 0x6106(%rip), %rsi      # 0x161fc
movl $0xa05, %edx            # imm = 0xA05
leaq 0x66db(%rip), %rcx      # 0x167dd
callq 0x1060
cmpq $0x0, -0x10(%rbp)
je 0x10110
jmp 0x1012f
leaq 0x61da(%rip), %rdi      # 0x162f1
leaq 0x60de(%rip), %rsi      # 0x161fc
movl $0xa06, %edx            # imm = 0xA06
leaq 0x66b3(%rip), %rcx      # 0x167dd
callq 0x1060
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x10209
cmpq $0x0, -0x28(%rbp)
je 0x10181
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rcx
movq (%rcx), %rsi
callq *%rax
cmpl $0x0, %eax
jne 0x10181
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10310
movl $0x20, %edi
callq 0x10a0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x101b3
movq 0x9e5b(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x65e7(%rip), %rsi      # 0x1678e
movb $0x0, %al
callq 0x1080
jmp 0x10310
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 $0x1, -0x3c(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x101f9
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10205
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x10207
jmp 0x10263
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x10261
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10261
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x10261
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10261
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 0x10263
cmpq $0x0, -0x30(%rbp)
je 0x102df
movq -0x30(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x102df
cmpq $0x0, -0x28(%rbp)
je 0x102df
movq -0x28(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x102df
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 0x102cb
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x102d7
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpl $0x1, -0x3c(%rbp)
jne 0x102e7
jmp 0x10310
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x1013a
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x10325
movq -0x8(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x18(%rax)
movq -0x38(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop