// File: /workspace/github_c_makefile_star2_O2/CarlosLunaMota[P]BinaryTrees/main.c
// Function: bs_tree_sequential_test
int bs_tree_sequential_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 sequentially in the tree:
    for (i=0; i<max_size; i++) {
        
        data = (MyData *) malloc(sizeof(MyData));
        data->key = i;
        found = bs_tree_insert(tree, data);

        // It was not already there:
        if (found != NULL) { return FAIL; }

        // 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 all elements are there:
    data = (MyData *) malloc(sizeof(MyData));
    for (i=0; i<max_size; i++) {
        data->key = i;
        found = bs_tree_search(tree, data);
        if (found == NULL)            { return FAIL; }
        if (MyComp(data, found) != 0) { return FAIL; }
    }
    free(data);

    // Check if everything is correctly sorted forward...
    found = bs_tree_min(tree);
    if (found == NULL)   { return FAIL; }
    if (found->key != 0) { 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; }
    if (found->key != max_size-1) { 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; 
        }
    }

    // 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; }

    // Insert elements sequentially in the tree:
    for (i=max_size; i>0; i--) {
        
        data = (MyData *) malloc(sizeof(MyData));
        data->key = i;
        found = bs_tree_insert(tree, data);

        // It was not already there:
        if (found != NULL) { return FAIL; }

        // 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 all elements are there:
    data = (MyData *) malloc(sizeof(MyData));
    for (i=max_size; i>0; i--) {
        data->key = i;
        found = bs_tree_search(tree, data);
        if (found == NULL)            { return FAIL; }
        if (MyComp(data, found) != 0) { return FAIL; }
    }
    free(data);

    // Check if everything is correctly sorted forward...
    found = bs_tree_min(tree);
    if (found == NULL)   { return FAIL; }
    if (found->key != 1) { 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; }
    if (found->key != max_size) { 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; 
        }
    }

    // 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; }

    // Insert elements sequentially in the tree:
    for (i=1; i<=max_size; i++) {
        
        data = (MyData *) malloc(sizeof(MyData));
        data->key = i;
        found = bs_tree_insert(tree, data);

        // It was not already there:
        if (found != NULL) { return FAIL; }

        // 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; }

        data = (MyData *) malloc(sizeof(MyData));
        data->key = -i;
        found = bs_tree_insert(tree, data);

        // It was not already there:
        if (found != NULL) { return FAIL; }

        // 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 all elements are there:
    data = (MyData *) malloc(sizeof(MyData));
    for (i=1; i<=max_size; i++) {
        data->key = i;
        found = bs_tree_search(tree, data);
        if (found == NULL)            { return FAIL; }
        if (MyComp(data, found) != 0) { return FAIL; }
        data->key = -i;
        found = bs_tree_search(tree, data);
        if (found == NULL)            { return FAIL; }
        if (MyComp(data, found) != 0) { return FAIL; }
    }
    free(data);

    // Check if everything is correctly sorted forward...
    found = bs_tree_min(tree);
    if (found == NULL)           { return FAIL; }
    if (found->key != -max_size) { 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; }
    if (found->key != max_size) { 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; 
        }
    }

    // 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 -0x35(%rip), %rdi       # 0x121c
callq 0x73c0
movq %rax, %r14
movq %rax, %rdi
callq 0x8d4d
pushq $0x1
popq %rbx
testl %eax, %eax
je 0x12f1
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
je 0x12f1
xorl %ebx, %ebx
testl %r15d, %r15d
movl $0x0, %ebp
cmovgl %r15d, %ebp
pushq $0x4
popq %r12
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
cmpl %ebx, %ebp
je 0x12ca
movl %ebx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x754a
testq %rax, %rax
jne 0x12ee
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
incl %ebx
cmpl $0x1, %eax
jne 0x128a
jmp 0x12ee
xorl %ebx, %ebx
cmpl %ebx, %ebp
je 0x1302
movl %ebx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x783d
testq %rax, %rax
je 0x12ee
movl (%r13), %ecx
incl %ebx
cmpl (%rax), %ecx
je 0x12cc
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
pushq $0x1
popq %rbx
je 0x12f1
movq %rax, %r12
cmpl $0x0, (%rax)
jne 0x12f1
jmp 0x132f
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jge 0x12f1
movq %r14, %rdi
movq %r12, %rsi
callq 0x7a2e
testq %rax, %rax
jne 0x1324
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12f1
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12f1
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12f1
movq %rax, %r12
leal -0x1(%r15), %eax
cmpl %eax, (%r12)
jne 0x12f1
movq %r14, %rdi
movq %r12, %rsi
callq 0x7962
testq %rax, %rax
je 0x138e
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x136e
jmp 0x12f1
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x12f1
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12f1
movq 0xfc0e(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0x7cdf
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
je 0x12ee
pushq $0x4
popq %r12
movl %r15d, %ebx
movq %r12, %rdi
callq 0x10d0
movq %rax, %r13
testl %ebx, %ebx
jle 0x142c
movl %ebx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x754a
testq %rax, %rax
jne 0x12ee
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
decl %ebx
cmpl $0x1, %eax
jne 0x13e1
jmp 0x12ee
movl %r15d, %ebx
testl %ebx, %ebx
jle 0x145a
movl %ebx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x783d
testq %rax, %rax
je 0x12ee
movl (%r13), %ecx
decl %ebx
cmpl (%rax), %ecx
je 0x142f
jmp 0x12ee
movq %r13, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x12ee
movq %rax, %r12
cmpl $0x1, (%rax)
jne 0x12ee
movq %r14, %rdi
movq %r12, %rsi
callq 0x7a2e
testq %rax, %rax
je 0x149f
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jl 0x147f
jmp 0x12ee
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12ee
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12ee
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12ee
movq %rax, %r12
cmpl %r15d, (%rax)
jne 0x12ee
movq %r14, %rdi
movq %r12, %rsi
callq 0x7962
testq %rax, %rax
je 0x14f9
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x14d9
jmp 0x12ee
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x12ee
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12ee
movq 0xfaa3(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0x7cdf
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
pushq $0x1
popq %rbx
je 0x12f1
negl %ebp
pushq $0x1
popq %rax
pushq $-0x1
popq %rcx
movl %ecx, 0x4(%rsp)
movl %eax, %r12d
leal (%r12,%rbp), %ebx
pushq $0x4
popq %rdi
callq 0x10d0
movq %rax, %r13
cmpl $0x1, %ebx
je 0x15f7
movl %r12d, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x754a
testq %rax, %rax
jne 0x12ee
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
cmpl $0x1, %eax
je 0x12ee
pushq $0x4
popq %rdi
callq 0x10d0
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x754a
testq %rax, %rax
jne 0x12ee
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12ee
movq %r14, %rdi
callq 0x780e
incl %r12d
decl 0x4(%rsp)
cmpl $0x1, %eax
jne 0x1557
jmp 0x12ee
pushq $0x1
popq %rax
pushq $-0x1
popq %rcx
movl %ecx, 0x4(%rsp)
movl %eax, %ebx
movl %eax, %r12d
leal (%r12,%rbp), %eax
cmpl $0x1, %eax
je 0x1663
movl %r12d, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x783d
testq %rax, %rax
je 0x12f1
movl (%r13), %ecx
cmpl (%rax), %ecx
jne 0x12f1
movl 0x4(%rsp), %eax
movl %eax, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x783d
testq %rax, %rax
je 0x12f1
movl (%r13), %ecx
incl %r12d
decl 0x4(%rsp)
cmpl (%rax), %ecx
je 0x1606
jmp 0x12f1
movq %r13, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x12f1
movq %rax, %r12
movl (%rax), %eax
addl %r15d, %eax
jne 0x12f1
movq %r14, %rdi
movq %r12, %rsi
callq 0x7a2e
testq %rax, %rax
je 0x16aa
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jl 0x168a
jmp 0x12f1
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12f1
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12f1
movq %r14, %rdi
callq 0x7920
testq %rax, %rax
je 0x12f1
movq %rax, %r12
cmpl %r15d, (%rax)
jne 0x12f1
movq %r14, %rdi
movq %r12, %rsi
callq 0x7962
testq %rax, %rax
je 0x1704
movl (%r12), %ecx
movq %rax, %r12
cmpl (%rax), %ecx
jg 0x16e4
jmp 0x12f1
movq %r14, %rdi
callq 0x78de
testq %rax, %rax
je 0x12f1
movl (%r12), %ecx
cmpl (%rax), %ecx
jne 0x12f1
movq 0xf898(%rip), %rsi      # 0x10fc0
movq %r14, %rdi
callq 0x7cdf
movq %r14, %rdi
callq 0x8d4d
testl %eax, %eax
je 0x12f1
movq %r14, %rdi
callq 0x780e
testl %eax, %eax
je 0x12f1
movq %r14, %rdi
callq 0x1120
xorl %ebx, %ebx
jmp 0x12f1