// File: /workspace/github_c_makefile_star2_O3/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 0xc9d4
movq %rsi, %rbx
testq %rsi, %rsi
je 0xc9f3
movq %rdi, %r15
cmpq %rbx, %rdi
je 0xc9b3
movq 0x8(%r15), %rdi
callq 0xb9f6
testq %rax, %rax
je 0xc9c4
movq %rax, %r14
movq %r15, %rdi
callq 0xbabf
movq %rax, %r13
movq %rbx, %rdi
callq 0xbabf
movq %rax, %r12
testq %r13, %r13
setne %al
testq %r12, %r12
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc96b
movq %r13, %rdi
movq %r12, %rsi
callq *0x8(%r14)
movq %r14, %rdi
testl %eax, %eax
js 0xc925
je 0xc93d
movq %r12, %rsi
callq 0xbb4d
jmp 0xc953
movq %r13, %rsi
callq 0xbb4d
movq %r15, %rdi
movq %r13, %rsi
callq 0xbc60
movq %rax, %r13
jmp 0xc961
movq %r13, %rsi
callq 0xbb4d
movq %r15, %rdi
movq %r13, %rsi
callq 0xbc60
movq %rax, %r13
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbc60
movq %rax, %r12
testq %r13, %r13
je 0xc96b
testq %r12, %r12
jne 0xc908
testq %r13, %r13
je 0xc98e
movq %r14, %rdi
movq %r13, %rsi
callq 0xbb4d
movq %r15, %rdi
movq %r13, %rsi
callq 0xbc60
movq %rax, %r13
testq %rax, %rax
jne 0xc970
testq %r12, %r12
je 0xc9c7
movq %r14, %rdi
movq %r12, %rsi
callq 0xbb4d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbc60
movq %rax, %r12
testq %rax, %rax
jne 0xc993
jmp 0xc9c7
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xba50
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1b3b(%rip), %rdi      # 0xe516
leaq 0x182b(%rip), %rsi      # 0xe20d
leaq 0x23cc(%rip), %rcx      # 0xedb5
movl $0x1445, %edx           # imm = 0x1445
callq 0x1080
leaq 0x1b64(%rip), %rdi      # 0xe55e
leaq 0x180c(%rip), %rsi      # 0xe20d
leaq 0x23ad(%rip), %rcx      # 0xedb5
movl $0x1446, %edx           # imm = 0x1446
callq 0x1080