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

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

    // 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 0x10898
jmp 0x108b7
leaq 0x59d3(%rip), %rdi      # 0x16272
leaq 0x5956(%rip), %rsi      # 0x161fc
movl $0x9ae, %edx            # imm = 0x9AE
leaq 0x5eff(%rip), %rcx      # 0x167b1
callq 0x1060
cmpq $0x0, -0x10(%rbp)
je 0x108c0
jmp 0x108df
leaq 0x5a2a(%rip), %rdi      # 0x162f1
leaq 0x592e(%rip), %rsi      # 0x161fc
movl $0x9af, %edx            # imm = 0x9AF
leaq 0x5ed7(%rip), %rcx      # 0x167b1
callq 0x1060
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x109b9
cmpq $0x0, -0x28(%rbp)
je 0x10931
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 0x10931
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10ac0
movl $0x20, %edi
callq 0x10a0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x10963
movq 0x96ab(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x5e37(%rip), %rsi      # 0x1678e
movb $0x0, %al
callq 0x1080
jmp 0x10ac0
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 0x109a9
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x109b5
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x109b7
jmp 0x10a13
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x10a11
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10a11
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x10a11
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10a11
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 0x10a13
cmpq $0x0, -0x30(%rbp)
je 0x10a8f
movq -0x30(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10a8f
cmpq $0x0, -0x28(%rbp)
je 0x10a8f
movq -0x28(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x10a8f
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 0x10a7b
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x10a87
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpl $0x1, -0x3c(%rbp)
jne 0x10a97
jmp 0x10ac0
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 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x108ea
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x10ad5
movq -0x8(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x18(%rax)
movq -0x38(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop