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

    rb_tree *tree = NULL;
    rb_node *pred_1;
    rb_node *node_1;
    rb_node *pred_2;
    rb_node *node_2;
    void    *data;
    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 rb_tree_copy(tree_1); }

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

    // Special case: Some of them is empty
    if (rb_tree_is_empty(tree_1) == YES) { return tree; }
    if (rb_tree_is_empty(tree_2) == YES) { return tree; }

    // Go to the smallest element of tree_1 while right-threading your path:
    node_1 = tree_1->root;
    if (node_1 != NULL) {
        while (node_1->left != NULL) {
            pred_1 = node_1->left;
            while (pred_1->right != NULL) { pred_1 = pred_1->right; }
            pred_1->right = node_1;
            node_1        = node_1->left;
        }
    }

    // Go to the smallest element of tree_2 while right-threading your path:
    node_2 = tree_2->root;
    if (node_2 != NULL) {
        while (node_2->left != NULL) {
            pred_2 = node_2->left;
            while (pred_2->right != NULL) { pred_2 = pred_2->right; }
            pred_2->right = node_2;
            node_2        = node_2->left;
        }
    }

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

        // compare both nodes:
        comp = (tree->comp)(node_1->data, node_2->data);

        if (comp < 0) {

            // advance node_1 //////////////////////////////////////////////////
            node_1 = node_1->right;
            if (node_1 != NULL) {
                while (node_1->left != NULL) {
                    pred_1 = node_1->left;
                    while (pred_1->right != NULL && pred_1->right != node_1) {
                        pred_1 = pred_1->right;
                    }
                    if (pred_1->right == NULL) {
                        pred_1->right = node_1;
                        node_1        = node_1->left;
                    } else {
                        pred_1->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

        } else if (comp > 0) {

            // advance node_2 //////////////////////////////////////////////////
            node_2 = node_2->right;
            if (node_2 != NULL) {
                while (node_2->left != NULL) {
                    pred_2 = node_2->left;
                    while (pred_2->right != NULL && pred_2->right != node_2) {
                        pred_2 = pred_2->right;
                    }
                    if (pred_2->right == NULL) {
                        pred_2->right = node_2;
                        node_2        = node_2->left;
                    } else {
                        pred_2->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

        } else {

            // insert node_1->data in tree /////////////////////////////////////
            data = node_1->data;
            data = rb_tree_insert_max(tree, data);
            assert(data == NULL);
            ////////////////////////////////////////////////////////////////////

            // advance node_1 //////////////////////////////////////////////////
            node_1 = node_1->right;
            if (node_1 != NULL) {
                while (node_1->left != NULL) {
                    pred_1 = node_1->left;
                    while (pred_1->right != NULL && pred_1->right != node_1) {
                        pred_1 = pred_1->right;
                    }
                    if (pred_1->right == NULL) {
                        pred_1->right = node_1;
                        node_1        = node_1->left;
                    } else {
                        pred_1->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

            // advance node_2 //////////////////////////////////////////////////
            node_2 = node_2->right;
            if (node_2 != NULL) {
                while (node_2->left != NULL) {
                    pred_2 = node_2->left;
                    while (pred_2->right != NULL && pred_2->right != node_2) {
                        pred_2 = pred_2->right;
                    }
                    if (pred_2->right == NULL) {
                        pred_2->right = node_2;
                        node_2        = node_2->left;
                    } else {
                        pred_2->right = NULL;
                        break;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////
        }
    }

    // We need to traverse tree_1 completely to restore the modified pointers:
    while (node_1 != NULL) {

        // advance node_1 //////////////////////////////////////////////////////
        node_1 = node_1->right;
        if (node_1 != NULL) {
            while (node_1->left != NULL) {
                pred_1 = node_1->left;
                while (pred_1->right != NULL && pred_1->right != node_1) {
                    pred_1 = pred_1->right;
                }
                if (pred_1->right == NULL) {
                    pred_1->right = node_1;
                    node_1        = node_1->left;
                } else {
                    pred_1->right = NULL;
                    break;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
    }

    // We need to traverse tree_2 completely to restore the modified pointers:
    while (node_2 != NULL) {

        // advance node_2 //////////////////////////////////////////////////////
        node_2 = node_2->right;
        if (node_2 != NULL) {
            while (node_2->left != NULL) {
                pred_2 = node_2->left;
                while (pred_2->right != NULL && pred_2->right != node_2) {
                    pred_2 = pred_2->right;
                }
                if (pred_2->right == NULL) {
                    pred_2->right = node_2;
                    node_2        = node_2->left;
                } else {
                    pred_2->right = NULL;
                    break;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
    }

    // Return the resulting tree:
    return tree;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
testq %rdi, %rdi
je 0xaef2
movq %rsi, %r14
testq %rsi, %rsi
je 0xaf11
movq %rdi, %r15
cmpq %r14, %rdi
je 0xac57
movq 0x8(%r15), %rdi
callq 0x96f9
testq %rax, %rax
je 0xac64
movq %rax, %rbx
movq (%r15), %rcx
testq %rcx, %rcx
je 0xaee9
movq (%r14), %rax
testq %rax, %rax
je 0xaee9
testq %rcx, %rcx
je 0xac6b
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0xac70
movq %rdx, %r15
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
jne 0xac39
movq %rcx, 0x10(%rdx)
movq 0x8(%r15), %rdx
movq %r15, %rcx
testq %rdx, %rdx
jne 0xac33
jmp 0xac73
movq %r15, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x9753
xorl %ebx, %ebx
jmp 0xaee9
xorl %r15d, %r15d
jmp 0xac73
movq %rcx, %r15
testq %rax, %rax
je 0xaca5
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xacaa
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0xac87
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0xac81
jmp 0xacad
xorl %r14d, %r14d
jmp 0xacad
movq %rax, %r14
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0xae8d
movq (%r15), %rdi
movq (%r14), %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0xad26
je 0xad7b
movq 0x10(%r14), %r14
testq %r14, %r14
je 0xae21
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xae2d
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xad05
cmpq %r14, %rdx
jne 0xacf4
addq $0x10, %rax
testq %rdx, %rdx
jne 0xae26
movq %r14, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r14
testq %rax, %rax
jne 0xacee
jmp 0xae1c
movq 0x10(%r15), %r15
testq %r15, %r15
je 0xadc6
movq 0x8(%r15), %rax
testq %rax, %rax
je 0xae2d
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xad57
cmpq %r15, %rdx
jne 0xad46
addq $0x10, %rax
testq %rdx, %rdx
jne 0xae26
movq %r15, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r15
testq %rax, %rax
jne 0xad40
movq %rcx, %r15
jmp 0xae2d
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x9835
movq 0x10(%r15), %rax
testq %rax, %rax
je 0xadcb
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xadd8
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xadaf
cmpq %rax, %rdx
jne 0xad9e
testq %rdx, %rdx
jne 0xadd0
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0xad98
jmp 0xaddb
xorl %r15d, %r15d
jmp 0xae2d
xorl %r15d, %r15d
jmp 0xaddb
movq $0x0, 0x10(%rcx)
movq %rax, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
je 0xae21
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xae2d
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xae04
cmpq %r14, %rdx
jne 0xadf3
addq $0x10, %rax
testq %rdx, %rdx
jne 0xae26
movq %r14, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r14
testq %rax, %rax
jne 0xaded
movq %rcx, %r14
jmp 0xae2d
xorl %r14d, %r14d
jmp 0xae2d
movq $0x0, (%rax)
testq %r15, %r15
je 0xae8d
testq %r14, %r14
jne 0xacc1
jmp 0xae8d
movq 0x10(%r15), %rax
testq %rax, %rax
je 0xae7d
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xae8a
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xae66
cmpq %rax, %rdx
jne 0xae55
testq %rdx, %rdx
jne 0xae82
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0xae4f
jmp 0xae8d
xorl %r15d, %r15d
jmp 0xae8d
movq $0x0, 0x10(%rcx)
movq %rax, %r15
testq %r15, %r15
jne 0xae3d
jmp 0xaee4
movq 0x10(%r14), %rax
testq %rax, %rax
je 0xaed4
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xaee1
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xaebd
cmpq %rax, %rdx
jne 0xaeac
testq %rdx, %rdx
jne 0xaed9
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0xaea6
jmp 0xaee4
xorl %r14d, %r14d
jmp 0xaee4
movq $0x0, 0x10(%rcx)
movq %rax, %r14
testq %r14, %r14
jne 0xae94
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq
leaq 0x361d(%rip), %rdi      # 0xe516
leaq 0x330d(%rip), %rsi      # 0xe20d
leaq 0x3aee(%rip), %rcx      # 0xe9f5
movl $0xe34, %edx            # imm = 0xE34
callq 0x1080
leaq 0x3646(%rip), %rdi      # 0xe55e
leaq 0x32ee(%rip), %rsi      # 0xe20d
leaq 0x3acf(%rip), %rcx      # 0xe9f5
movl $0xe35, %edx            # imm = 0xE35
callq 0x1080