// File: /workspace/github_c_makefile_star2_O2/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 0xbc0d
movq %rsi, %rbx
testq %rsi, %rsi
je 0xbc2c
movq %rdi, %r15
cmpq %rbx, %rdi
je 0xbbec
movq 0x8(%r15), %rdi
callq 0xb14a
testq %rax, %rax
je 0xbbfd
movq %rax, %r14
movq %r15, %rdi
callq 0xb21f
movq %rax, %r13
movq %rbx, %rdi
callq 0xb21f
movq %rax, %r12
testq %r13, %r13
je 0xbbc5
testq %r12, %r12
je 0xbbc5
movq %r13, %rdi
movq %r12, %rsi
callq *0x8(%r14)
movq %r14, %rdi
testl %eax, %eax
jns 0xbb7d
movq %r13, %rsi
callq 0xb261
movq %r15, %rdi
movq %r13, %rsi
callq 0xb322
movq %rax, %r13
jmp 0xbb4a
je 0xbb94
movq %r12, %rsi
callq 0xb261
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb322
jmp 0xbb47
movq %r13, %rsi
callq 0xb261
movq %r15, %rdi
movq %r13, %rsi
callq 0xb322
movq %rax, %r13
jmp 0xbb87
movq %r14, %rdi
movq %r13, %rsi
callq 0xb261
movq %r15, %rdi
movq %r13, %rsi
callq 0xb322
movq %rax, %r13
testq %r13, %r13
jne 0xbbac
jmp 0xbbe5
movq %r14, %rdi
movq %r12, %rsi
callq 0xb261
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb322
movq %rax, %r12
testq %r12, %r12
jne 0xbbcc
jmp 0xbc00
movq %r15, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xb1b6
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1902(%rip), %rdi      # 0xd516
leaq 0x15f2(%rip), %rsi      # 0xd20d
leaq 0x2193(%rip), %rcx      # 0xddb5
movl $0x1445, %edx           # imm = 0x1445
callq 0x1080
leaq 0x192b(%rip), %rdi      # 0xd55e
leaq 0x15d3(%rip), %rsi      # 0xd20d
leaq 0x2174(%rip), %rcx      # 0xddb5
movl $0x1446, %edx           # imm = 0x1446
callq 0x1080