// File: /workspace/github_c_makefile_star2_O2/CarlosLunaMota[P]BinaryTrees/main.c
// Function: rb_tree_random_test
int rb_tree_random_test(int max_size) {

    int i;
    rb_tree *tree  = new_rb_tree(MyComp);
    MyData  *data  = NULL;
    MyData  *found = NULL;

    // It is a rb_tree:
    if (is_rb_tree(tree) == NO) { return FAIL; }

    // It is empty:
    if (rb_tree_is_empty(tree) == NO) { return FAIL; }

    // Insert elements randomly in the tree:
    for (i=0; i<max_size*10; i++) {
        
        data = (MyData *) malloc(sizeof(MyData));
        data->key = rand() % max_size;

        // Insert it:
        found = rb_tree_insert(tree, data);

        // If it was already there:
        if (found != NULL) { free(found); }

        // It is a rb_tree:
        if (is_rb_tree(tree) == NO)        { return FAIL; }

        // It is not empty:
        if (rb_tree_is_empty(tree) == YES) { return FAIL; }
    }

    // Check if everything is correctly sorted forward...
    found = rb_tree_min(tree);
    if (found == NULL) { return FAIL; }
    while (found != NULL) {
        data  = found;
        found = rb_tree_next(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) >= 0) { return FAIL; }
        } else { 
            found = rb_tree_max(tree);
            if (found == NULL)            { return FAIL; }
            if (MyComp(data, found) != 0) { return FAIL; }
            break;
        }
    }

    // ...and backwards.
    found = rb_tree_max(tree);
    if (found == NULL) { return FAIL; }
    while (found != NULL) {
        data  = found;
        found = rb_tree_prev(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) <= 0) { return FAIL; }
        } else { 
            found = rb_tree_min(tree);
            if (found == NULL)            { return FAIL; }
            if (MyComp(data, found) != 0) { return FAIL; }
            break; 
        }
    }

    // Remove elements randomly from the tree:
    for (i=0; i<max_size*5; i++) {
        
        data = (MyData *) malloc(sizeof(MyData));
        data->key = rand() % max_size;

        // Remove element
        found = rb_tree_remove(tree, data);

        // If it was there:
        if (found != NULL) { free(found); }

        // It is a rb_tree:
        if (is_rb_tree(tree) == NO) { return FAIL; }
    }

    // Check if everything is correctly sorted forward...
    found = rb_tree_min(tree);
    while (found != NULL) {
        data  = found;
        found = rb_tree_next(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) >= 0) { return FAIL; }
        } else { 
            found = rb_tree_max(tree);
            if (found == NULL)            { return FAIL; }
            if (MyComp(data, found) != 0) { return FAIL; }
            break;
        }
    }

    // ...and backwards.
    found = rb_tree_max(tree);
    while (found != NULL) {
        data  = found;
        found = rb_tree_prev(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) <= 0) { return FAIL; }
        } else { 
            found = rb_tree_min(tree);
            if (found == NULL)            { return FAIL; }
            if (MyComp(data, found) != 0) { return FAIL; }
            break; 
        }
    }

    // Now, clean everything!
    rb_tree_remove_all(tree, free);

    // It is a rb_tree:
    if (is_rb_tree(tree) == NO) { return FAIL; }

    // It is empty:
    if (rb_tree_is_empty(tree) == NO) { return FAIL; }

    free(tree);
    
    return PASS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %r15d
leaq -0x2af0(%rip), %rdi     # 0x121c
callq 0x9037
movq %rax, %r14
movq %rax, %rdi
callq 0xad71
pushq $0x1
popq %rbx
testl %eax, %eax
je 0x3f40
movq %r14, %rdi
callq 0x9736
testl %eax, %eax
je 0x3f40
imull $0xa, %r15d, %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x3d99
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x92e8
movq %rax, %rdi
callq 0x1120
movq %r14, %rdi
callq 0xad71
testl %eax, %eax
je 0x3f40
movq %r14, %rdi
callq 0x9736
cmpl $0x1, %eax
jne 0x3d48
jmp 0x3f40
movq %r14, %rdi
callq 0x9806
testq %rax, %rax
je 0x3f40
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x9956
testq %rax, %rax
je 0x3dcd
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jl 0x3dad
jmp 0x3f40
movq %r14, %rdi
callq 0x9848
testq %rax, %rax
je 0x3f40
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x3f40
movq %r14, %rdi
callq 0x9848
testq %rax, %rax
je 0x3f40
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x988a
testq %rax, %rax
je 0x3e1e
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x3dfe
jmp 0x3f40
movq %r14, %rdi
callq 0x9806
testq %rax, %rax
je 0x3f40
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x3f40
leal (%r15,%r15,4), %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x3e8c
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x9a20
movq %rax, %rdi
callq 0x1120
movq %r14, %rdi
callq 0xad71
testl %eax, %eax
jne 0x3e4c
jmp 0x3f40
movq %r14, %rdi
callq 0x9806
movq %rax, %r15
testq %r15, %r15
je 0x3ecf
movq %r14, %rdi
movq %r15, %rsi
callq 0x9956
testq %rax, %rax
je 0x3ebb
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jl 0x3e97
jmp 0x3f40
movq %r14, %rdi
callq 0x9848
testq %rax, %rax
je 0x3f40
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x3f40
movq %r14, %rdi
callq 0x9848
movq %rax, %r15
testq %r15, %r15
je 0x3f0f
movq %r14, %rdi
movq %r15, %rsi
callq 0x988a
testq %rax, %rax
je 0x3efb
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jg 0x3eda
jmp 0x3f40
movq %r14, %rdi
callq 0x9806
testq %rax, %rax
je 0x3f40
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x3f40
movq 0xd0aa(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0xa1af
movq %r14, %rdi
callq 0xad71
testl %eax, %eax
je 0x3f40
movq %r14, %rdi
callq 0x9736
testl %eax, %eax
je 0x3f40
movq %r14, %rdi
callq 0x1120
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq