// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
movl %edi, %r14d
leaq -0x4ccb(%rip), %rdi     # 0x166c
callq 0xb9f6
movq %rax, %rbx
movq %rax, %rdi
callq 0xcdca
testl %eax, %eax
je 0x658b
movq %rbx, %rdi
callq 0xc122
testl %eax, %eax
je 0x658b
testl %r14d, %r14d
jle 0x63d4
leal (%r14,%r14), %eax
leal (%rax,%rax,4), %eax
cmpl $0x2, %eax
movl $0x1, %r12d
cmovgel %eax, %r12d
movl $0x1, %ebp
movl $0x4, %edi
callq 0x10d0
movq %rax, %r15
callq 0x1110
cltd
idivl %r14d
movl %edx, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xbe2e
testq %rax, %rax
je 0x63ae
movq %rax, %rdi
callq 0x1120
movq %rbx, %rdi
callq 0xcdca
testl %eax, %eax
je 0x6590
movq %rbx, %rdi
callq 0xc122
cmpl $0x1, %eax
je 0x6590
decl %r12d
jne 0x637d
movq %rbx, %rdi
callq 0xbabf
testq %rax, %rax
je 0x658b
movq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xbc60
testq %rax, %rax
je 0x6407
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jl 0x63e8
jmp 0x658b
movq %rbx, %rdi
callq 0xc2b3
testq %rax, %rax
je 0x658b
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x658b
movq %rbx, %rdi
callq 0xc2b3
testq %rax, %rax
je 0x658b
movq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc341
testq %rax, %rax
je 0x6456
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jg 0x6437
jmp 0x658b
movq %rbx, %rdi
callq 0xbabf
testq %rax, %rax
je 0x658b
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x658b
testl %r14d, %r14d
jle 0x64d0
leal (%r14,%r14,4), %r12d
cmpl $0x2, %r12d
movl $0x1, %ebp
cmovll %ebp, %r12d
movl $0x4, %edi
callq 0x10d0
movq %rax, %r15
callq 0x1110
cltd
idivl %r14d
movl %edx, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc50f
testq %rax, %rax
cmovneq %rax, %r15
movq %r15, %rdi
callq 0x1120
movq %rbx, %rdi
callq 0xcdca
testl %eax, %eax
je 0x6590
decl %r12d
jne 0x6488
movq %rbx, %rdi
callq 0xbabf
movq %rax, %r14
testq %r14, %r14
je 0x6513
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbc60
testq %rax, %rax
je 0x64ff
movl (%r14), %ecx
movq %rax, %r14
cmpl (%rax), %ecx
jl 0x64db
jmp 0x658b
movq %rbx, %rdi
callq 0xc2b3
testq %rax, %rax
je 0x658b
movl (%r14), %ecx
cmpl (%rax), %ecx
jne 0x658b
movq %rbx, %rdi
callq 0xc2b3
movq %rax, %r14
testq %r14, %r14
je 0x6553
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc341
testq %rax, %rax
je 0x653f
movl (%r14), %ecx
movq %rax, %r14
cmpl (%rax), %ecx
jg 0x651e
jmp 0x658b
movq %rbx, %rdi
callq 0xbabf
testq %rax, %rax
je 0x658b
movl (%r14), %ecx
cmpl (%rax), %ecx
jne 0x658b
movq 0xba66(%rip), %rsi      # 0x11fc0
movq %rbx, %rdi
callq 0xc82a
movq %rbx, %rdi
callq 0xcdca
testl %eax, %eax
je 0x658b
movq %rbx, %rdi
callq 0xc122
movl $0x1, %ebp
testl %eax, %eax
je 0x6590
movq %rbx, %rdi
callq 0x1120
xorl %ebp, %ebp
jmp 0x6590
movl $0x1, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq