// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
leaq -0x8b92(%rip), %rdi     # 0x11e0
callq 0x14240
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x15b10
cmpl $0x0, %eax
jne 0x9da5
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x18(%rbp), %rdi
callq 0x14e20
cmpl $0x0, %eax
jne 0x9dbf
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
imull $0xa, -0x8(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9e57
movl $0x4, %edi
callq 0x10a0
movq %rax, -0x20(%rbp)
callq 0x10d0
cltd
idivl -0x8(%rbp)
movq -0x20(%rbp), %rax
movl %edx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14670
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x9e13
movq -0x28(%rbp), %rdi
callq 0x10e0
movq -0x18(%rbp), %rdi
callq 0x15b10
cmpl $0x0, %eax
jne 0x9e2d
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x18(%rbp), %rdi
callq 0x14e20
cmpl $0x1, %eax
jne 0x9e47
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9e49
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x9dc6
movq -0x18(%rbp), %rdi
callq 0x14370
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9e77
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9e79
cmpq $0x0, -0x28(%rbp)
je 0x9f09
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x9ec4
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
jl 0x9ec2
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9f04
movq -0x18(%rbp), %rdi
callq 0x14f40
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9ee4
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
je 0x9f02
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9f09
jmp 0x9e79
movq -0x18(%rbp), %rdi
callq 0x14f40
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9f29
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9f2b
cmpq $0x0, -0x28(%rbp)
je 0x9fbb
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14fb0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x9f76
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
jg 0x9f74
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9fb6
movq -0x18(%rbp), %rdi
callq 0x14370
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9f96
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
je 0x9fb4
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0x9fbb
jmp 0x9f2b
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
imull $0x5, -0x8(%rbp), %ecx
cmpl %ecx, %eax
jge 0xa048
movl $0x4, %edi
callq 0x10a0
movq %rax, -0x20(%rbp)
callq 0x10d0
cltd
idivl -0x8(%rbp)
movq -0x20(%rbp), %rax
movl %edx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x150f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa00d
movq -0x28(%rbp), %rdi
callq 0x10e0
jmp 0xa01e
movq -0x20(%rbp), %rdi
callq 0x10e0
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x15b10
cmpl $0x0, %eax
jne 0xa038
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0xa03a
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x9fc2
movq -0x18(%rbp), %rdi
callq 0x14370
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa0e5
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa0a0
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
jl 0xa09e
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0xa0e0
movq -0x18(%rbp), %rdi
callq 0x14f40
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa0c0
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
je 0xa0de
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0xa0e5
jmp 0xa055
movq -0x18(%rbp), %rdi
callq 0x14f40
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa178
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14fb0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa139
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
jg 0xa137
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0xa173
movq -0x18(%rbp), %rdi
callq 0x14370
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa156
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11e0
cmpl $0x0, %eax
je 0xa171
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
jmp 0xa178
jmp 0xa0f2
movq -0x18(%rbp), %rdi
movq 0xfe3d(%rip), %rsi      # 0x19fc0
callq 0x15330
movq -0x18(%rbp), %rdi
callq 0x15b10
cmpl $0x0, %eax
jne 0xa19f
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x18(%rbp), %rdi
callq 0x14e20
cmpl $0x0, %eax
jne 0xa1b6
movl $0x1, -0x4(%rbp)
jmp 0xa1c6
movq -0x18(%rbp), %rdi
callq 0x10e0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop