// File: /workspace/github_c_makefile_star2_O2/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 0x80f7
movq %rsi, %r14
testq %rsi, %rsi
je 0x8116
movq %rdi, %r15
cmpq %r14, %rdi
je 0x7db4
movq 0x8(%r15), %rdi
callq 0x73c0
testq %rax, %rax
je 0x7dca
movq %rax, %rbx
movq (%r15), %r15
testq %r15, %r15
je 0x7dd1
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x7dd4
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x7d9f
movq %r15, 0x10(%rcx)
movq %rax, %r15
jmp 0x7d93
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x742a
xorl %ebx, %ebx
jmp 0x80e5
xorl %r15d, %r15d
movq (%r14), %r12
testq %r12, %r12
je 0x7dfe
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x7e01
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x7de9
movq %r12, 0x10(%rcx)
movq %rax, %r12
jmp 0x7ddc
xorl %r12d, %r12d
xorl %eax, %eax
pushq $0x18
popq %r14
movq %rax, %r13
testq %r15, %r15
je 0x8043
testq %r12, %r12
je 0x8043
movq (%r15), %rdi
movq (%r12), %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0x7eb5
movl %eax, %ebp
movq %r14, %rdi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testl %ebp, %ebp
je 0x7f22
testq %rax, %rax
je 0x80c9
movq (%r12), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r12), %rcx
movq %rax, %r13
movl $0x0, %r12d
testq %rcx, %rcx
je 0x7e0a
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x7eaa
movq %rsi, %rdi
movq %rdi, %rdx
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x7e95
cmpq %rcx, %rdi
jne 0x7e84
addq $0x10, %rdx
testq %rdi, %rdi
jne 0x7f7f
movq %rcx, (%rdx)
movq %rsi, %rcx
jmp 0x7e78
movq %rax, %r13
movq %rcx, %r12
jmp 0x7e0a
movq %r14, %rdi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x80c9
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %rcx
movl $0x0, %r15d
testq %rcx, %rcx
je 0x7e07
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x7f71
movq %rsi, %rdi
movq %rdi, %rdx
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x7f11
cmpq %rcx, %rdi
jne 0x7f00
addq $0x10, %rdx
testq %rdi, %rdi
jne 0x7f79
movq %rcx, (%rdx)
movq %rsi, %rcx
jmp 0x7ef4
testq %rax, %rax
je 0x80c9
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x7f6c
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x7f90
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x7f5e
cmpq %r15, %rsi
jne 0x7f4d
testq %rsi, %rsi
jne 0x7f8b
movq %r15, 0x10(%rdx)
movq %rcx, %r15
jmp 0x7f41
xorl %r15d, %r15d
jmp 0x7f90
movq %rcx, %r15
jmp 0x7e07
movq %rcx, %r15
movq %r12, %rcx
andq $0x0, (%rdx)
movq %rcx, %r12
jmp 0x7e07
andq $0x0, 0x10(%rdx)
movq 0x10(%r12), %rcx
movl $0x0, %r12d
testq %rcx, %rcx
je 0x7e07
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x7f83
movq %rsi, %rdi
movq %rdi, %rdx
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x7fc1
cmpq %rcx, %rdi
jne 0x7fb0
addq $0x10, %rdx
testq %rdi, %rdi
jne 0x7f7f
movq %rcx, (%rdx)
movq %rsi, %rcx
jmp 0x7fa4
pushq $0x18
popq %rdi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x80c9
movq (%r15), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r15), %rcx
xorl %r15d, %r15d
movq %rax, %r13
testq %rcx, %rcx
je 0x8043
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x803d
movq %rdx, %rdi
movq %rdi, %rsi
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x802a
cmpq %rcx, %rdi
jne 0x8019
testq %rdi, %rdi
jne 0x8038
movq %rcx, 0x10(%rsi)
movq %rdx, %rcx
jmp 0x800d
andq $0x0, 0x10(%rsi)
movq %rax, %r13
movq %rcx, %r15
testq %r15, %r15
jne 0x7fd2
pushq $0x18
popq %r14
testq %r12, %r12
je 0x80e5
movq %r14, %rdi
callq 0x10d0
leaq 0x10(%r13), %rcx
testq %r13, %r13
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x80c9
movq (%r12), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r12), %rcx
movq %rax, %r13
movl $0x0, %r12d
testq %rcx, %rcx
je 0x804c
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x80c1
movq %rdx, %rdi
movq %rdi, %rsi
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x80ae
cmpq %rcx, %rdi
jne 0x809d
testq %rdi, %rdi
jne 0x80bc
movq %rcx, 0x10(%rsi)
movq %rdx, %rcx
jmp 0x8091
andq $0x0, 0x10(%rsi)
movq %rax, %r13
movq %rcx, %r12
jmp 0x804c
movq 0x8f28(%rip), %rax      # 0x10ff8
movq (%rax), %rcx
leaq 0x51dd(%rip), %rdi      # 0xd2b7
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5418(%rip), %rdi      # 0xd516
leaq 0x5108(%rip), %rsi      # 0xd20d
leaq 0x5419(%rip), %rcx      # 0xd525
movl $0x305, %edx            # imm = 0x305
callq 0x1080
leaq 0x5441(%rip), %rdi      # 0xd55e
leaq 0x50e9(%rip), %rsi      # 0xd20d
leaq 0x53fa(%rip), %rcx      # 0xd525
movl $0x306, %edx            # imm = 0x306
callq 0x1080