// File: /workspace/github_c_makefile_star2_O1/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: sp_tree_union
sp_tree *sp_tree_union(sp_tree *tree_1, sp_tree *tree_2) {

    sp_tree *tree;
    void    *data_1;
    void    *data_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 sp_tree_copy(tree_1); }

    // Create a new tree:
    tree = new_sp_tree(tree_1->comp);
    if (tree == NULL) { return NULL; }

    // Get the smallest element of each tree:
    data_1 = sp_tree_min(tree_1);
    data_2 = sp_tree_min(tree_2);

    // Until we have exhausted at least one of the trees:
    while (data_1 != NULL && data_2 != NULL) {

        // compare both nodes:
        comp = (tree->comp)(data_1, data_2);

        if (comp < 0) {

            // insert data_1 in tree:
            sp_tree_insert_max(tree, data_1);

            // advance data_1:
            data_1 = sp_tree_next(tree_1, data_1);

        } else if (comp > 0) {

            // insert data_2 in tree:
            sp_tree_insert_max(tree, data_2);

            // advance data_2:
            data_2 = sp_tree_next(tree_2, data_2);

        } else {

            // insert data_1 in tree:
            sp_tree_insert_max(tree, data_1);

            // advance data_1:
            data_1 = sp_tree_next(tree_1, data_1);

            // advance data_2:
            data_2 = sp_tree_next(tree_2, data_2);

        }
    }

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

        // insert data_1 in tree:
        sp_tree_insert_max(tree, data_1);

        // advance data_1:
        data_1 = sp_tree_next(tree_1, data_1);
    }

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

        // insert data_2 in tree:
        sp_tree_insert_max(tree, data_2);

        // advance data_2:
        data_2 = sp_tree_next(tree_2, data_2);
    }

    // Return the resulting tree:
    return tree;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
je 0xc975
movq %rsi, %rbx
testq %rsi, %rsi
je 0xc994
movq %rdi, %r15
cmpq %rbx, %rdi
je 0xc954
movq 0x8(%r15), %rdi
callq 0xb972
testq %rax, %rax
je 0xc965
movq %rax, %r14
movq %r15, %rdi
callq 0xba3b
movq %rax, %r13
movq %rbx, %rdi
callq 0xba3b
movq %rax, %r12
testq %r13, %r13
setne %al
testq %r12, %r12
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc90c
movq %r13, %rdi
movq %r12, %rsi
callq *0x8(%r14)
movq %r14, %rdi
testl %eax, %eax
js 0xc8c6
je 0xc8de
movq %r12, %rsi
callq 0xbac9
jmp 0xc8f4
movq %r13, %rsi
callq 0xbac9
movq %r15, %rdi
movq %r13, %rsi
callq 0xbbdf
movq %rax, %r13
jmp 0xc902
movq %r13, %rsi
callq 0xbac9
movq %r15, %rdi
movq %r13, %rsi
callq 0xbbdf
movq %rax, %r13
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbbdf
movq %rax, %r12
testq %r13, %r13
je 0xc90c
testq %r12, %r12
jne 0xc8a9
testq %r13, %r13
je 0xc92f
movq %r14, %rdi
movq %r13, %rsi
callq 0xbac9
movq %r15, %rdi
movq %r13, %rsi
callq 0xbbdf
movq %rax, %r13
testq %rax, %rax
jne 0xc911
testq %r12, %r12
je 0xc968
movq %r14, %rdi
movq %r12, %rsi
callq 0xbac9
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbbdf
movq %rax, %r12
testq %rax, %rax
jne 0xc934
jmp 0xc968
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xb9cc
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1b9a(%rip), %rdi      # 0xe516
leaq 0x188a(%rip), %rsi      # 0xe20d
leaq 0x242b(%rip), %rcx      # 0xedb5
movl $0x1445, %edx           # imm = 0x1445
callq 0x1080
leaq 0x1bc3(%rip), %rdi      # 0xe55e
leaq 0x186b(%rip), %rsi      # 0xe20d
leaq 0x240c(%rip), %rcx      # 0xedb5
movl $0x1446, %edx           # imm = 0x1446
callq 0x1080