// File: /workspace/github_c_makefile_star2_O0/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: sp_tree_intersection
sp_tree *sp_tree_intersection(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; }

    // Special case: Some of them is empty
    if (sp_tree_is_empty(tree_1) == YES) { return tree; }
    if (sp_tree_is_empty(tree_2) == YES) { return tree; }
    
    // 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) {

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

        } else if (comp > 0) {

            // 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);

        }
    }

    // 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 0x15609
jmp 0x15628
leaq 0xef5(%rip), %rdi       # 0x16505
leaq 0xbe5(%rip), %rsi       # 0x161fc
movl $0x149d, %edx           # imm = 0x149D
leaq 0x17bb(%rip), %rcx      # 0x16dde
callq 0x1060
cmpq $0x0, -0x18(%rbp)
je 0x15631
jmp 0x15650
leaq 0xf15(%rip), %rdi       # 0x1654d
leaq 0xbbd(%rip), %rsi       # 0x161fc
movl $0x149e, %edx           # imm = 0x149E
leaq 0x1793(%rip), %rcx      # 0x16dde
callq 0x1060
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x1566c
movq -0x10(%rbp), %rdi
callq 0x142d0
movq %rax, -0x8(%rbp)
jmp 0x15786
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x14240
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x15691
movq $0x0, -0x8(%rbp)
jmp 0x15786
movq -0x10(%rbp), %rdi
callq 0x14e20
cmpl $0x1, %eax
jne 0x156ac
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x15786
movq -0x18(%rbp), %rdi
callq 0x14e20
cmpl $0x1, %eax
jne 0x156c7
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x15786
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 0x156f8
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
testb $0x1, %al
jne 0x15701
jmp 0x1577e
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 0x1572f
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x14530
movq %rax, -0x28(%rbp)
jmp 0x15779
cmpl $0x0, -0x34(%rbp)
jle 0x15748
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x14530
movq %rax, -0x30(%rbp)
jmp 0x15777
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 0x15779
jmp 0x156e1
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq