// File: /workspace/github_c_makefile_star2_O2/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 0x841a
movq %rsi, %r14
testq %rsi, %rsi
je 0x8439
movq %rdi, %r15
cmpq %r14, %rdi
je 0x83dc
movq 0x8(%r15), %rdi
callq 0x73c0
testq %rax, %rax
je 0x83ed
movq %rax, %rbx
movq (%r15), %r15
testq %r15, %r15
je 0x83ef
movq (%r14), %r12
testq %r12, %r12
je 0x83ef
movq 0x8(%r15), %rax
movq %rax, %rcx
testq %rax, %rax
je 0x81c3
movq %rcx, %rdx
movq 0x10(%rcx), %rcx
testq %rcx, %rcx
jne 0x8198
movq %r15, 0x10(%rdx)
movq %rax, %r15
jmp 0x818c
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x81b0
movq %r12, 0x10(%rcx)
movq %rax, %r12
movq 0x8(%r12), %rax
testq %rax, %rax
jne 0x81ad
xorl %r13d, %r13d
pushq $0x18
popq %r14
testq %r15, %r15
je 0x838b
testq %r12, %r12
je 0x838b
movq (%r15), %rdi
movq (%r12), %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0x8240
je 0x8281
movq 0x10(%r12), %rax
movl $0x0, %r12d
testq %rax, %rax
je 0x81d4
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x82fd
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x822b
cmpq %rax, %rsi
jne 0x821a
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x82f9
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0x820a
movq 0x10(%r15), %rax
movl $0x0, %r15d
testq %rax, %rax
je 0x81d4
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x82eb
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8270
cmpq %rax, %rsi
jne 0x825f
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x82f3
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0x824f
movq %r14, %rdi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x83fc
movq %rax, %r13
movq (%r15), %rax
movq %rax, (%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r13)
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x82e6
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x830a
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x82d8
cmpq %r15, %rdx
jne 0x82c7
testq %rdx, %rdx
jne 0x8305
movq %r15, 0x10(%rcx)
movq %rax, %r15
jmp 0x82bb
xorl %r15d, %r15d
jmp 0x830a
movq %rax, %r15
jmp 0x81d4
movq %rax, %r15
movq %r12, %rax
andq $0x0, (%rcx)
movq %rax, %r12
jmp 0x81d4
andq $0x0, 0x10(%rcx)
movq 0x10(%r12), %rax
movl $0x0, %r12d
testq %rax, %rax
je 0x81d4
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x82fd
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x833b
cmpq %rax, %rsi
jne 0x832a
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x82f9
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0x831e
movq 0x10(%r15), %rax
xorl %r15d, %r15d
testq %rax, %rax
je 0x838b
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8388
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8375
cmpq %rax, %rsi
jne 0x8364
testq %rsi, %rsi
jne 0x8383
movq %rax, 0x10(%rdx)
movq %rcx, %rax
jmp 0x8358
andq $0x0, 0x10(%rdx)
movq %rax, %r15
testq %r15, %r15
jne 0x834c
jmp 0x83d5
movq 0x10(%r12), %rax
movl $0x0, %r12d
testq %rax, %rax
je 0x83d5
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x83d2
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x83bf
cmpq %rax, %rsi
jne 0x83ae
testq %rsi, %rsi
jne 0x83cd
movq %rax, 0x10(%rdx)
movq %rcx, %rax
jmp 0x83a2
andq $0x0, 0x10(%rdx)
movq %rax, %r12
testq %r12, %r12
jne 0x8392
jmp 0x83ef
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x742a
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x8bf5(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x4eaa(%rip), %rdi      # 0xd2b7
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x83ef
leaq 0x50f5(%rip), %rdi      # 0xd516
leaq 0x4de5(%rip), %rsi      # 0xd20d
leaq 0x513e(%rip), %rcx      # 0xd56d
movl $0x424, %edx            # imm = 0x424
callq 0x1080
leaq 0x511e(%rip), %rdi      # 0xd55e
leaq 0x4dc6(%rip), %rsi      # 0xd20d
leaq 0x511f(%rip), %rcx      # 0xd56d
movl $0x425, %edx            # imm = 0x425
callq 0x1080