// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
movl %edi, %r14d
leaq -0x2ba2(%rip), %rdi     # 0x166c
callq 0x96f9
movq %rax, %rbx
movq %rax, %rdi
callq 0xb5c1
testl %eax, %eax
je 0x445d
movq %rbx, %rdi
callq 0x9db6
testl %eax, %eax
je 0x445d
testl %r14d, %r14d
jle 0x42ab
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 0x9996
testq %rax, %rax
je 0x4285
movq %rax, %rdi
callq 0x1120
movq %rbx, %rdi
callq 0xb5c1
testl %eax, %eax
je 0x4462
movq %rbx, %rdi
callq 0x9db6
cmpl $0x1, %eax
je 0x4462
decl %r12d
jne 0x4254
movq %rbx, %rdi
callq 0x9e7c
testq %rax, %rax
je 0x445d
movq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9fc4
testq %rax, %rax
je 0x42de
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jl 0x42bf
jmp 0x445d
movq %rbx, %rdi
callq 0x9ebe
testq %rax, %rax
je 0x445d
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x445d
movq %rbx, %rdi
callq 0x9ebe
testq %rax, %rax
je 0x445d
movq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9f00
testq %rax, %rax
je 0x432d
movl (%r15), %ecx
movq %rax, %r15
cmpl (%rax), %ecx
jg 0x430e
jmp 0x445d
movq %rbx, %rdi
callq 0x9e7c
testq %rax, %rax
je 0x445d
movl (%r15), %ecx
cmpl (%rax), %ecx
jne 0x445d
testl %r14d, %r14d
jle 0x43a2
leal (%r14,%r14,4), %eax
cmpl $0x2, %eax
movl $0x1, %ebp
cmovgel %eax, %ebp
movl $0x4, %edi
callq 0x10d0
movq %rax, %r15
callq 0x1110
cltd
idivl %r14d
movl %edx, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa07c
testq %rax, %rax
je 0x438e
movq %rax, %rdi
callq 0x1120
movq %rbx, %rdi
callq 0xb5c1
testl %eax, %eax
je 0x445d
decl %ebp
jne 0x435d
movq %rbx, %rdi
callq 0x9e7c
movq %rax, %r14
testq %r14, %r14
je 0x43e5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9fc4
testq %rax, %rax
je 0x43d1
movl (%r14), %ecx
movq %rax, %r14
cmpl (%rax), %ecx
jl 0x43ad
jmp 0x445d
movq %rbx, %rdi
callq 0x9ebe
testq %rax, %rax
je 0x445d
movl (%r14), %ecx
cmpl (%rax), %ecx
jne 0x445d
movq %rbx, %rdi
callq 0x9ebe
movq %rax, %r14
testq %r14, %r14
je 0x4425
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9f00
testq %rax, %rax
je 0x4411
movl (%r14), %ecx
movq %rax, %r14
cmpl (%rax), %ecx
jg 0x43f0
jmp 0x445d
movq %rbx, %rdi
callq 0x9e7c
testq %rax, %rax
je 0x445d
movl (%r14), %ecx
cmpl (%rax), %ecx
jne 0x445d
movq 0xdb94(%rip), %rsi      # 0x11fc0
movq %rbx, %rdi
callq 0xa7f4
movq %rbx, %rdi
callq 0xb5c1
testl %eax, %eax
je 0x445d
movq %rbx, %rdi
callq 0x9db6
movl $0x1, %ebp
testl %eax, %eax
je 0x4462
movq %rbx, %rdi
callq 0x1120
xorl %ebp, %ebp
jmp 0x4462
movl $0x1, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq