// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x15429
jmp 0x15448
leaq 0x10d5(%rip), %rdi      # 0x16505
leaq 0xdc5(%rip), %rsi       # 0x161fc
movl $0x1445, %edx           # imm = 0x1445
leaq 0x196e(%rip), %rcx      # 0x16db1
callq 0x1060
cmpq $0x0, -0x18(%rbp)
je 0x15451
jmp 0x15470
leaq 0x10f5(%rip), %rdi      # 0x1654d
leaq 0xd9d(%rip), %rsi       # 0x161fc
movl $0x1446, %edx           # imm = 0x1446
leaq 0x1946(%rip), %rcx      # 0x16db1
callq 0x1060
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x1548c
movq -0x10(%rbp), %rdi
callq 0x142d0
movq %rax, -0x8(%rbp)
jmp 0x155df
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x14240
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x154b1
movq $0x0, -0x8(%rbp)
jmp 0x155df
movq -0x10(%rbp), %rdi
callq 0x14370
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x14370
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x28(%rbp)
movb %al, -0x35(%rbp)
je 0x154e2
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
testb $0x1, %al
jne 0x154ee
jmp 0x15585
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq *%rax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x15529
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x143e0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
jmp 0x15580
cmpl $0x0, -0x34(%rbp)
jle 0x1554f
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x143e0
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x14530
movq %rax, -0x30(%rbp)
jmp 0x1557e
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x143e0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x14530
movq %rax, -0x30(%rbp)
jmp 0x15580
jmp 0x154cb
jmp 0x15587
cmpq $0x0, -0x28(%rbp)
je 0x155ae
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x143e0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
jmp 0x15587
jmp 0x155b0
cmpq $0x0, -0x30(%rbp)
je 0x155d7
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x143e0
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x14530
movq %rax, -0x30(%rbp)
jmp 0x155b0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)