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

    int i;
    sp_tree *tree  = new_sp_tree(MyComp);
    MyData  *data  = NULL;
    MyData  *found = NULL;

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

    // It is empty:
    if (sp_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 = sp_tree_insert(tree, data);
        
        // If it was already there:
        if (found != NULL) { free(found); }

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

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

    // Check if everything is correctly sorted forward...
    found = sp_tree_min(tree);
    if (found == NULL) { return FAIL; }
    while (found != NULL) {
        data  = found;
        found = sp_tree_next(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) >= 0) { return FAIL; }
        } else { 
            found = sp_tree_max(tree);
            if (found == NULL)            { return FAIL; }
            if (MyComp(data, found) != 0) { return FAIL; }
            break;
        }
    }
    
    // ...and backwards.
    found = sp_tree_max(tree);
    if (found == NULL) { return FAIL; }
    while (found != NULL) {
        data  = found;
        found = sp_tree_prev(tree, data);
        if (found != NULL) {
            if (MyComp(data, found) <= 0) { return FAIL; }
        } else { 
            found = sp_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 = sp_tree_remove(tree, data);

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

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

    }

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

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

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

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

    // It is empty:
    if (sp_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 -0x4ad6(%rip), %rdi     # 0x121c
callq 0xb14a
movq %rax, %r14
movq %rax, %rdi
callq 0xbfba
pushq $0x1
popq %rbx
testl %eax, %eax
je 0x5f2d
movq %r14, %rdi
callq 0xb772
testl %eax, %eax
je 0x5f2d
imull $0xa, %r15d, %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x5d7f
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xb3ca
movq %rax, %rdi
callq 0x1120
movq %r14, %rdi
callq 0xbfba
testl %eax, %eax
je 0x5f2d
movq %r14, %rdi
callq 0xb772
cmpl $0x1, %eax
jne 0x5d2e
jmp 0x5f2d
movq %r14, %rdi
callq 0xb21f
testq %rax, %rax
je 0x5f2d
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xb322
testq %rax, %rax
je 0x5db3
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jl 0x5d93
jmp 0x5f2d
movq %r14, %rdi
callq 0xb826
testq %rax, %rax
je 0x5f2d
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x5f2d
movq %r14, %rdi
callq 0xb826
testq %rax, %rax
je 0x5f2d
movq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xb868
testq %rax, %rax
je 0x5e04
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x5de4
jmp 0x5f2d
movq %r14, %rdi
callq 0xb21f
testq %rax, %rax
je 0x5f2d
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x5f2d
leal (%r15,%r15,4), %eax
xorl %ebp, %ebp
testl %eax, %eax
cmovgl %eax, %ebp
incl %ebp
pushq $0x4
popq %r12
decl %ebp
je 0x5e79
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
callq 0x1110
cltd
idivl %r15d
movl %edx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xb910
testq %rax, %rax
cmovneq %rax, %r13
movq %r13, %rdi
callq 0x1120
movq %r14, %rdi
callq 0xbfba
testl %eax, %eax
jne 0x5e32
jmp 0x5f2d
movq %r14, %rdi
callq 0xb21f
movq %rax, %r15
testq %r15, %r15
je 0x5ebc
movq %r14, %rdi
movq %r15, %rsi
callq 0xb322
testq %rax, %rax
je 0x5ea8
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jl 0x5e84
jmp 0x5f2d
movq %r14, %rdi
callq 0xb826
testq %rax, %rax
je 0x5f2d
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x5f2d
movq %r14, %rdi
callq 0xb826
movq %rax, %r15
testq %r15, %r15
je 0x5efc
movq %r14, %rdi
movq %r15, %rsi
callq 0xb868
testq %rax, %rax
je 0x5ee8
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jg 0x5ec7
jmp 0x5f2d
movq %r14, %rdi
callq 0xb21f
testq %rax, %rax
je 0x5f2d
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x5f2d
movq 0xb0bd(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0xba82
movq %r14, %rdi
callq 0xbfba
testl %eax, %eax
je 0x5f2d
movq %r14, %rdi
callq 0xb772
testl %eax, %eax
je 0x5f2d
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