// File: /workspace/github_c_makefile_star2_O3/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 0x871b
movq %rsi, %r14
testq %rsi, %rsi
je 0x873a
movq %rdi, %r15
cmpq %r14, %rdi
je 0x8334
movq 0x8(%r15), %rdi
callq 0x79b8
testq %rax, %rax
je 0x834a
movq (%r15), %rsi
testq %rsi, %rsi
je 0x8352
movq 0x8(%rsi), %rcx
testq %rcx, %rcx
je 0x8357
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x8316
movq %rsi, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rsi
testq %rcx, %rcx
jne 0x8310
jmp 0x835a
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7a12
xorl %r13d, %r13d
jmp 0x86ec
xorl %r15d, %r15d
jmp 0x835a
movq %rsi, %r15
movq (%r14), %rsi
testq %rsi, %rsi
je 0x8390
movq 0x8(%rsi), %rcx
testq %rcx, %rcx
je 0x839d
movq %rcx, %r12
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x8371
movq %rsi, 0x10(%rcx)
movq 0x8(%r12), %rcx
movq %r12, %rsi
testq %rcx, %rcx
jne 0x836b
jmp 0x83a0
movq %rax, %r13
xorl %ebx, %ebx
xorl %r12d, %r12d
jmp 0x85ab
movq %rsi, %r12
testq %r15, %r15
je 0x85a6
movq %rax, %r13
movq 0x8(%rax), %rax
movq %rax, (%rsp)
xorl %r14d, %r14d
movq (%r15), %rdi
movq (%r12), %rsi
callq *(%rsp)
testl %eax, %eax
js 0x8459
movl %eax, %ebp
movl $0x18, %edi
callq 0x10d0
movq %rax, %rbx
leaq 0x10(%r14), %rax
testq %r14, %r14
cmoveq %r13, %rax
movq %rbx, (%rax)
testl %ebp, %ebp
je 0x84df
testq %rbx, %rbx
je 0x8706
movq (%r12), %rax
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x86fe
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x8598
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x8438
cmpq %r12, %rdx
jne 0x8427
addq $0x10, %rax
testq %rdx, %rdx
jne 0x8591
movq %r12, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r12
testq %rax, %rax
jne 0x8421
jmp 0x858c
movl $0x18, %edi
callq 0x10d0
movq %rax, %rbx
leaq 0x10(%r14), %rax
testq %r14, %r14
cmoveq %r13, %rax
movq %rbx, (%rax)
testq %rbx, %rbx
je 0x870d
movq (%r15), %rax
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x8655
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x8598
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x84bb
cmpq %r15, %rdx
jne 0x84aa
addq $0x10, %rax
testq %rdx, %rdx
jne 0x8591
movq %r15, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r15
testq %rax, %rax
jne 0x84a4
movq %rcx, %r15
jmp 0x8598
testq %rbx, %rbx
je 0x8714
movq (%r15), %rax
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x8535
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8542
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x851e
cmpq %rax, %rdx
jne 0x850d
testq %rdx, %rdx
jne 0x853a
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0x8507
jmp 0x8545
xorl %r15d, %r15d
jmp 0x8545
movq $0x0, 0x10(%rcx)
movq %rax, %r15
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x8395
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x8598
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x8574
cmpq %r12, %rdx
jne 0x8563
addq $0x10, %rax
testq %rdx, %rdx
jne 0x8591
movq %r12, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r12
testq %rax, %rax
jne 0x855d
movq %rcx, %r12
jmp 0x8598
movq $0x0, (%rax)
movq %rbx, %r14
testq %r15, %r15
jne 0x83b7
jmp 0x85ab
movq %rax, %r13
xorl %ebx, %ebx
testq %r15, %r15
je 0x864c
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%rbx), %rcx
testq %rbx, %rbx
cmoveq %r13, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x8642
movq %rax, %rbx
movq (%r15), %rax
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x864c
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x8629
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x860a
cmpq %rax, %rdx
jne 0x85f9
testq %rdx, %rdx
jne 0x8621
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0x85f3
jmp 0x862c
movq $0x0, 0x10(%rcx)
movq %rax, %r15
movl $0x18, %edi
callq 0x10d0
movq %rax, 0x10(%rbx)
movq %rax, %rbx
testq %rax, %rax
jne 0x85d4
callq 0x1210
jmp 0x86ec
testq %r12, %r12
je 0x86ec
movl $0x18, %edi
callq 0x10d0
movq %rax, %r15
leaq 0x10(%rbx), %rax
testq %rbx, %rbx
cmoveq %r13, %rax
movq %r15, (%rax)
testq %r15, %r15
je 0x86e7
movq (%r12), %rax
movq %rax, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x86ec
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x86ce
movq %rcx, %r12
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0x86ae
cmpq %rax, %rdx
jne 0x869d
testq %rdx, %rdx
jne 0x86c6
movq %rax, 0x10(%rcx)
movq 0x8(%r12), %rcx
movq %r12, %rax
testq %rcx, %rcx
jne 0x8697
jmp 0x86d1
movq $0x0, 0x10(%rcx)
movq %rax, %r12
movl $0x18, %edi
callq 0x10d0
movq %rax, 0x10(%r15)
movq %rax, %r15
testq %rax, %rax
jne 0x8675
callq 0x122c
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x85b4
callq 0x11bc
jmp 0x86ec
callq 0x11f4
jmp 0x86ec
callq 0x11d8
jmp 0x86ec
leaq 0x5df4(%rip), %rdi      # 0xe516
leaq 0x5ae4(%rip), %rsi      # 0xe20d
leaq 0x5df5(%rip), %rcx      # 0xe525
movl $0x305, %edx            # imm = 0x305
callq 0x1080
leaq 0x5e1d(%rip), %rdi      # 0xe55e
leaq 0x5ac5(%rip), %rsi      # 0xe20d
leaq 0x5dd6(%rip), %rcx      # 0xe525
movl $0x306, %edx            # imm = 0x306
callq 0x1080