// File: /workspace/github_c_makefile_star2_O3/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 0x8a90
movq %rsi, %r14
testq %rsi, %rsi
je 0x8aaf
movq %rdi, %r15
cmpq %r14, %rdi
je 0x87cd
movq 0x8(%r15), %rdi
callq 0x79b8
testq %rax, %rax
je 0x87de
movq %rax, %rbx
movq (%r15), %rcx
testq %rcx, %rcx
je 0x87e0
movq (%r14), %rax
testq %rax, %rax
je 0x87e0
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x87ed
movq %rdx, %r15
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
jne 0x87af
movq %rcx, 0x10(%rdx)
movq 0x8(%r15), %rdx
movq %r15, %rcx
testq %rdx, %rdx
jne 0x87a9
jmp 0x87f0
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x7a12
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rcx, %r15
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x881d
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x87ff
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0x87f9
jmp 0x8820
movq %rax, %r14
movq 0x8(%rbx), %r12
xorl %r13d, %r13d
movq (%r15), %rdi
movq (%r14), %rsi
callq *%r12
testl %eax, %eax
js 0x888f
je 0x88e4
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x8a2a
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x89be
movq %rcx, %rax
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x886b
cmpq %r14, %rdx
jne 0x885a
addq $0x10, %rcx
testq %rdx, %rdx
jne 0x89b7
movq %r14, (%rcx)
movq 0x8(%rax), %rcx
movq %rax, %r14
testq %rcx, %rcx
jne 0x8854
movq %rax, %r14
jmp 0x89be
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x89d5
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x89be
movq %rcx, %rax
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x88c0
cmpq %r15, %rdx
jne 0x88af
addq $0x10, %rcx
testq %rdx, %rdx
jne 0x89b7
movq %r15, (%rcx)
movq 0x8(%rax), %rcx
movq %rax, %r15
testq %rcx, %rcx
jne 0x88a9
movq %rax, %r15
jmp 0x89be
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x8a86
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %rcx
testq %rcx, %rcx
je 0x8952
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x895f
movq %rdx, %r15
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x893b
cmpq %rcx, %rsi
jne 0x892a
testq %rsi, %rsi
jne 0x8957
movq %rcx, 0x10(%rdx)
movq 0x8(%r15), %rdx
movq %r15, %rcx
testq %rdx, %rdx
jne 0x8924
jmp 0x8962
xorl %r15d, %r15d
jmp 0x8962
movq $0x0, 0x10(%rdx)
movq %rcx, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x8a7e
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x89af
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x898f
cmpq %r14, %rsi
jne 0x897e
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x89b4
movq %r14, (%rcx)
movq 0x8(%rdx), %rcx
movq %rdx, %r14
testq %rcx, %rcx
jne 0x8978
movq %rax, %r13
movq %rdx, %r14
jmp 0x89be
movq %rax, %r13
jmp 0x89be
movq %rax, %r13
movq $0x0, (%rcx)
testq %r15, %r15
jne 0x8827
testq %r15, %r15
jne 0x8a2d
testq %r14, %r14
je 0x87e0
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x87e0
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x8a21
movq %rdx, %rcx
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8a02
cmpq %rax, %rsi
jne 0x89f1
testq %rsi, %rsi
jne 0x8a19
movq %rax, 0x10(%rdx)
movq 0x8(%rcx), %rdx
movq %rcx, %rax
testq %rdx, %rdx
jne 0x89eb
jmp 0x8a24
movq $0x0, 0x10(%rdx)
movq %rax, %rcx
movq 0x10(%rcx), %rax
jmp 0x89d9
xorl %r14d, %r14d
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x89cc
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x8a75
movq %rdx, %rcx
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8a56
cmpq %rax, %rsi
jne 0x8a45
testq %rsi, %rsi
jne 0x8a6d
movq %rax, 0x10(%rdx)
movq 0x8(%rcx), %rdx
movq %rcx, %rax
testq %rdx, %rdx
jne 0x8a3f
jmp 0x8a78
movq $0x0, 0x10(%rdx)
movq %rax, %rcx
movq 0x10(%rcx), %rax
jmp 0x8a31
xorl %r14d, %r14d
jmp 0x89c7
callq 0x1248
jmp 0x87e0
leaq 0x5a7f(%rip), %rdi      # 0xe516
leaq 0x576f(%rip), %rsi      # 0xe20d
leaq 0x5ac8(%rip), %rcx      # 0xe56d
movl $0x424, %edx            # imm = 0x424
callq 0x1080
leaq 0x5aa8(%rip), %rdi      # 0xe55e
leaq 0x5750(%rip), %rsi      # 0xe20d
leaq 0x5aa9(%rip), %rcx      # 0xe56d
movl $0x425, %edx            # imm = 0x425
callq 0x1080