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

    int i;
    bs_tree *tree  = new_bs_tree(MyComp);
    MyData  *data  = NULL;
    MyData  *found = NULL;

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

    // It is empty:
    if (bs_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 = bs_tree_insert(tree, data);

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

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

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

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

    // ...and backwards.
    found = bs_tree_max(tree);
    if (found == NULL) { return FAIL; }
    while (found != NULL) {
        data  = found;
        found = bs_tree_prev(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) <= 0) { return FAIL; }
        } else { 
            found = bs_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 = bs_tree_remove(tree, data);

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

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

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

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

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

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

    // It is empty:
    if (bs_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 -0xb0a(%rip), %rdi      # 0x121c
callq 0x73c0
movq %rax, %r14
movq %rax, %rdi
callq 0x8d4d
pushq $0x1
popq %rbx
testl %eax, %eax
je 0x1f5a
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
je 0x1f5a
imull $0xa, %r15d, %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x1db3
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x754a
movq %rax, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x1f5a
movq %r14, %rdi
callq 0x780e
cmpl $0x1, %eax
jne 0x1d62
jmp 0x1f5a
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x1f5a
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x7a2e
testq %rax, %rax
je 0x1de7
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jl 0x1dc7
jmp 0x1f5a
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x1f5a
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x1f5a
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x1f5a
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x7962
testq %rax, %rax
je 0x1e38
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x1e18
jmp 0x1f5a
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x1f5a
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x1f5a
leal (%r15,%r15,4), %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x1ea6
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7af8
movq %rax, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
jne 0x1e66
jmp 0x1f5a
movq %r14, %rdi
callq 0x78de
movq %rax, %r15
testq %r15, %r15
je 0x1ee9
movq %r14, %rdi
movq %r15, %rsi
callq 0x7a2e
testq %rax, %rax
je 0x1ed5
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jl 0x1eb1
jmp 0x1f5a
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x1f5a
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x1f5a
movq %r14, %rdi
callq 0x7920
movq %rax, %r15
testq %r15, %r15
je 0x1f29
movq %r14, %rdi
movq %r15, %rsi
callq 0x7962
testq %rax, %rax
je 0x1f15
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jg 0x1ef4
jmp 0x1f5a
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x1f5a
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x1f5a
movq 0xf090(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0x7cdf
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x1f5a
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
je 0x1f5a
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