// File: /workspace/github_c_makefile_star2_O1/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: bs_tree_intersection
bs_tree *bs_tree_intersection(const bs_tree *tree_1, const bs_tree *tree_2) {

    bs_tree *tree = NULL;
    bs_node *node = NULL;
    bs_node *pred_1;
    bs_node *node_1;
    bs_node *pred_2;
    bs_node *node_2;
    int      comp;

    // Sanity check:
    assert(tree_1 != NULL);
    assert(tree_2 != NULL);

    // Special case: Both trees are the same
    if (tree_1 == tree_2) { return bs_tree_copy(tree_1); }

    // Create a new tree:
    tree = new_bs_tree(tree_1->comp);
    if (tree == NULL) { return NULL; }

    // Special case: Some of them is empty
    if (bs_tree_is_empty(tree_1) == YES) { return tree; }
    if (bs_tree_is_empty(tree_2) == YES) { return tree; }

    // Go to the smallest element of tree_1 while right-threading your path:
    node_1 = tree_1->root;
    if (node_1 != NULL) {
        while (node_1->left != NULL) {
            pred_1 = node_1->left;
            while (pred_1->right != NULL) { pred_1 = pred_1->right; }
            pred_1->right = node_1;
            node_1        = node_1->left;
        }
    }

    // Go to the smallest element of tree_2 while right-threading your path:
    node_2 = tree_2->root;
    if (node_2 != NULL) {
        while (node_2->left != NULL) {
            pred_2 = node_2->left;
            while (pred_2->right != NULL) { pred_2 = pred_2->right; }
            pred_2->right = node_2;
            node_2        = node_2->left;
        }
    }

    // Until we have exhausted at least one of the trees:
    while (node_1 != NULL && node_2 != NULL) {

        // compare both nodes:
        comp = (tree->comp)(node_1->data, node_2->data);

        if (comp < 0) {

            // advance node_1 //////////////////////////////////////////////////
            node_1 = node_1->right;
            if (node_1 != NULL) {
                while (node_1->left != NULL) {
                    pred_1 = node_1->left;
                    while (pred_1->right != NULL && pred_1->right != node_1) {
                        pred_1 = pred_1->right;
                    }
                    if (pred_1->right == NULL) {
                        pred_1->right = node_1;
                        node_1        = node_1->left;
                    } else {
                        pred_1->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

        } else if (comp > 0) {

            // advance node_2 //////////////////////////////////////////////////
            node_2 = node_2->right;
            if (node_2 != NULL) {
                while (node_2->left != NULL) {
                    pred_2 = node_2->left;
                    while (pred_2->right != NULL && pred_2->right != node_2) {
                        pred_2 = pred_2->right;
                    }
                    if (pred_2->right == NULL) {
                        pred_2->right = node_2;
                        node_2        = node_2->left;
                    } else {
                        pred_2->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

        } else {

            // insert node_1->data in tree /////////////////////////////////////
            if (node == NULL)  {
                tree->root = (bs_node *) malloc(sizeof(bs_node));
                node = tree->root;
            } else {
                node->right = (bs_node *) malloc(sizeof(bs_node));
                node = node->right;
            }
            if (node == NULL) {
                fprintf(stderr, "ERROR: Unable to allocate bs_node\n");
                return tree;
            } else {
                node->data  = node_1->data;
                node->left  = NULL;
                node->right = NULL;
            }
            ////////////////////////////////////////////////////////////////////

            // advance node_1 //////////////////////////////////////////////////
            node_1 = node_1->right;
            if (node_1 != NULL) {
                while (node_1->left != NULL) {
                    pred_1 = node_1->left;
                    while (pred_1->right != NULL && pred_1->right != node_1) {
                        pred_1 = pred_1->right;
                    }
                    if (pred_1->right == NULL) {
                        pred_1->right = node_1;
                        node_1        = node_1->left;
                    } else {
                        pred_1->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

            // advance node_2 //////////////////////////////////////////////////
            node_2 = node_2->right;
            if (node_2 != NULL) {
                while (node_2->left != NULL) {
                    pred_2 = node_2->left;
                    while (pred_2->right != NULL && pred_2->right != node_2) {
                        pred_2 = pred_2->right;
                    }
                    if (pred_2->right == NULL) {
                        pred_2->right = node_2;
                        node_2        = node_2->left;
                    } else {
                        pred_2->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////
        }
    }

    // We need to traverse tree_1 completely to restore the modified pointers:
    while (node_1 != NULL) {

        // advance node_1 //////////////////////////////////////////////////////
        node_1 = node_1->right;
        if (node_1 != NULL) {
            while (node_1->left != NULL) {
                pred_1 = node_1->left;
                while (pred_1->right != NULL && pred_1->right != node_1) {
                    pred_1 = pred_1->right;
                }
                if (pred_1->right == NULL) {
                    pred_1->right = node_1;
                    node_1        = node_1->left;
                } else {
                    pred_1->right = NULL;
                    break;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
    }

    // We need to traverse tree_2 completely to restore the modified pointers:
    while (node_2 != NULL) {

        // advance node_2 //////////////////////////////////////////////////////
        node_2 = node_2->right;
        if (node_2 != NULL) {
            while (node_2->left != NULL) {
                pred_2 = node_2->left;
                while (pred_2->right != NULL && pred_2->right != node_2) {
                    pred_2 = pred_2->right;
                }
                if (pred_2->right == NULL) {
                    pred_2->right = node_2;
                    node_2        = node_2->left;
                } else {
                    pred_2->right = NULL;
                    break;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
    }

    // Return the resulting tree:
    return tree;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
je 0x89c9
movq %rsi, %r14
testq %rsi, %rsi
je 0x89e8
movq %rdi, %r15
cmpq %r14, %rdi
je 0x86e4
movq 0x8(%r15), %rdi
callq 0x78a4
testq %rax, %rax
je 0x86f5
movq %rax, %rbx
movq (%r15), %rcx
testq %rcx, %rcx
je 0x89b5
movq (%r14), %rax
testq %rax, %rax
je 0x89b5
testq %rcx, %rcx
je 0x86fc
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x8701
movq %rdx, %r15
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
jne 0x86c6
movq %rcx, 0x10(%rdx)
movq 0x8(%r15), %rdx
movq %r15, %rcx
testq %rdx, %rdx
jne 0x86c0
jmp 0x8704
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x78fe
xorl %ebx, %ebx
jmp 0x89b5
xorl %r15d, %r15d
jmp 0x8704
movq %rcx, %r15
testq %rax, %rax
je 0x8736
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x873b
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x8718
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0x8712
jmp 0x873e
xorl %r14d, %r14d
jmp 0x873e
movq %rax, %r14
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x8959
movq 0x8(%rbx), %r12
xorl %r13d, %r13d
movq (%r15), %rdi
movq (%r14), %rsi
callq *%r12
testl %eax, %eax
js 0x87c1
je 0x8816
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x88e5
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x88f9
movq %rcx, %rax
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x879d
cmpq %r14, %rdx
jne 0x878c
addq $0x10, %rcx
testq %rdx, %rdx
jne 0x88f2
movq %r14, (%rcx)
movq 0x8(%rax), %rcx
movq %rax, %r14
testq %rcx, %rcx
jne 0x8786
movq %rax, %r14
jmp 0x88f9
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x8884
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x88f9
movq %rcx, %rax
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x87f2
cmpq %r15, %rdx
jne 0x87e1
addq $0x10, %rcx
testq %rdx, %rdx
jne 0x88f2
movq %r15, (%rcx)
movq 0x8(%rax), %rcx
movq %rax, %r15
testq %rcx, %rcx
jne 0x87db
movq %rax, %r15
jmp 0x88f9
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x89c2
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %rcx
testq %rcx, %rcx
je 0x8889
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x8896
movq %rdx, %r15
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x886d
cmpq %rcx, %rsi
jne 0x885c
testq %rsi, %rsi
jne 0x888e
movq %rcx, 0x10(%rdx)
movq 0x8(%r15), %rdx
movq %r15, %rcx
testq %rdx, %rdx
jne 0x8856
jmp 0x8899
xorl %r15d, %r15d
jmp 0x88f9
xorl %r15d, %r15d
jmp 0x8899
movq $0x0, 0x10(%rdx)
movq %rcx, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x88e2
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x88ea
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x88c2
cmpq %r14, %rsi
jne 0x88b1
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x88ef
movq %r14, (%rcx)
movq 0x8(%rdx), %rcx
movq %rdx, %r14
testq %rcx, %rcx
jne 0x88ab
movq %rax, %r13
movq %rdx, %r14
jmp 0x88f9
movq %rax, %r13
xorl %r14d, %r14d
jmp 0x88f9
movq %rax, %r13
jmp 0x88f9
movq %rax, %r13
movq $0x0, (%rcx)
testq %r15, %r15
je 0x8959
testq %r14, %r14
jne 0x8759
jmp 0x8959
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x8949
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8956
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x8932
cmpq %rax, %rdx
jne 0x8921
testq %rdx, %rdx
jne 0x894e
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0x891b
jmp 0x8959
xorl %r15d, %r15d
jmp 0x8959
movq $0x0, 0x10(%rcx)
movq %rax, %r15
testq %r15, %r15
jne 0x8909
jmp 0x89b0
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x89a0
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x89ad
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x8989
cmpq %rax, %rdx
jne 0x8978
testq %rdx, %rdx
jne 0x89a5
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0x8972
jmp 0x89b0
xorl %r14d, %r14d
jmp 0x89b0
movq $0x0, 0x10(%rcx)
movq %rax, %r14
testq %r14, %r14
jne 0x8960
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
callq 0x1248
jmp 0x89b5
leaq 0x5b46(%rip), %rdi      # 0xe516
leaq 0x5836(%rip), %rsi      # 0xe20d
leaq 0x5b8f(%rip), %rcx      # 0xe56d
movl $0x424, %edx            # imm = 0x424
callq 0x1080
leaq 0x5b6f(%rip), %rdi      # 0xe55e
leaq 0x5817(%rip), %rsi      # 0xe20d
leaq 0x5b70(%rip), %rcx      # 0xe56d
movl $0x425, %edx            # imm = 0x425
callq 0x1080