// File: /workspace/github_c_makefile_star2_O1/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: bs_tree_union
bs_tree *bs_tree_union(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; }

    // 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) {

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

        } else if (comp > 0) {

            // insert node_2->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_2->data;
                node->left  = NULL;
                node->right = 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;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

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

    // Insert all remaining data from tree_1:
    while (node_1 != NULL) {

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

    // Insert all remaining data from tree_2:
    while (node_2 != NULL) {

        // insert node_2->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_2->data;
            node->left  = NULL;
            node->right = 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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x8625
movq %rsi, %r14
testq %rsi, %rsi
je 0x8644
movq %rdi, %r15
cmpq %r14, %rdi
je 0x823e
movq 0x8(%r15), %rdi
callq 0x78a4
testq %rax, %rax
je 0x8254
movq %rax, %rbx
movq (%r15), %rax
testq %rax, %rax
je 0x825b
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8260
movq %rcx, %r12
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x821f
movq %rax, 0x10(%rcx)
movq 0x8(%r12), %rcx
movq %r12, %rax
testq %rcx, %rcx
jne 0x8219
jmp 0x8263
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x78fe
xorl %ebx, %ebx
jmp 0x85f0
xorl %r12d, %r12d
jmp 0x8263
movq %rax, %r12
movq (%r14), %rax
testq %rax, %rax
je 0x8298
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x829d
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x827a
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0x8274
jmp 0x82a0
xorl %r15d, %r15d
jmp 0x82a0
movq %rax, %r15
testq %r12, %r12
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x84c3
movq 0x8(%rbx), %r13
xorl %r14d, %r14d
movq (%r12), %rdi
movq (%r15), %rsi
callq *%r13
testl %eax, %eax
js 0x8357
movl %eax, %ebp
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%r14), %rcx
testq %r14, %r14
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testl %ebp, %ebp
je 0x83e0
testq %rax, %rax
je 0x8610
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x849a
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x84a9
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8336
cmpq %r15, %rsi
jne 0x8325
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x84a2
movq %r15, (%rcx)
movq 0x8(%rdx), %rcx
movq %rdx, %r15
testq %rcx, %rcx
jne 0x831f
jmp 0x8492
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%r14), %rcx
testq %r14, %r14
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x8617
movq (%r12), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x8439
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0x84a9
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x83b9
cmpq %r12, %rsi
jne 0x83a8
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x84a2
movq %r12, (%rcx)
movq 0x8(%rdx), %rcx
movq %rdx, %r12
testq %rcx, %rcx
jne 0x83a2
movq %rax, %r14
movq %rdx, %r12
jmp 0x84ac
testq %rax, %rax
je 0x861e
movq (%r12), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0x8441
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x844e
movq %rdx, %r12
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x8421
cmpq %rcx, %rsi
jne 0x8410
testq %rsi, %rsi
jne 0x8446
movq %rcx, 0x10(%rdx)
movq 0x8(%r12), %rdx
movq %r12, %rcx
testq %rdx, %rdx
jne 0x840a
jmp 0x8451
movq %rax, %r14
xorl %r12d, %r12d
jmp 0x84ac
xorl %r12d, %r12d
jmp 0x8451
movq $0x0, 0x10(%rdx)
movq %rcx, %r12
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x849a
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x84a9
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x847a
cmpq %r15, %rsi
jne 0x8469
addq $0x10, %rcx
testq %rsi, %rsi
jne 0x84a2
movq %r15, (%rcx)
movq 0x8(%rdx), %rcx
movq %rdx, %r15
testq %rcx, %rcx
jne 0x8463
movq %rax, %r14
movq %rdx, %r15
jmp 0x84ac
movq %rax, %r14
xorl %r15d, %r15d
jmp 0x84ac
movq $0x0, (%rcx)
movq %rax, %r14
testq %r12, %r12
je 0x8553
testq %r15, %r15
jne 0x82bb
jmp 0x8553
xorl %r14d, %r14d
jmp 0x8553
movq %r14, %r13
movl $0x18, %edi
callq 0x10d0
movq %rax, %r14
leaq 0x10(%r13), %rax
testq %r13, %r13
cmoveq %rbx, %rax
movq %r14, (%rax)
testq %r14, %r14
je 0x8602
movq (%r12), %rax
movq %rax, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x8543
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8550
movq %rcx, %r12
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x852b
cmpq %rax, %rdx
jne 0x851a
testq %rdx, %rdx
jne 0x8548
movq %rax, 0x10(%rcx)
movq 0x8(%r12), %rcx
movq %r12, %rax
testq %rcx, %rcx
jne 0x8514
jmp 0x8553
xorl %r12d, %r12d
jmp 0x8553
movq $0x0, 0x10(%rcx)
movq %rax, %r12
testq %r12, %r12
jne 0x84cb
jmp 0x85e7
movq %r14, %r12
movl $0x18, %edi
callq 0x10d0
movq %rax, %r14
leaq 0x10(%r12), %rax
testq %r12, %r12
cmoveq %rbx, %rax
movq %r14, (%rax)
testq %r14, %r14
je 0x8609
movq (%r15), %rax
movq %rax, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x85d7
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x85e4
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x85c0
cmpq %rax, %rdx
jne 0x85af
testq %rdx, %rdx
jne 0x85dc
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0x85a9
jmp 0x85e7
xorl %r15d, %r15d
jmp 0x85e7
movq $0x0, 0x10(%rcx)
movq %rax, %r15
testq %r15, %r15
jne 0x8561
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11f4
jmp 0x85f0
callq 0x1210
jmp 0x85f0
callq 0x11bc
jmp 0x85f0
callq 0x122c
jmp 0x85f0
callq 0x11d8
jmp 0x85f0
leaq 0x5eea(%rip), %rdi      # 0xe516
leaq 0x5bda(%rip), %rsi      # 0xe20d
leaq 0x5eeb(%rip), %rcx      # 0xe525
movl $0x305, %edx            # imm = 0x305
callq 0x1080
leaq 0x5f13(%rip), %rdi      # 0xe55e
leaq 0x5bbb(%rip), %rsi      # 0xe20d
leaq 0x5ecc(%rip), %rcx      # 0xe525
movl $0x306, %edx            # imm = 0x306
callq 0x1080