// File: /workspace/github_c_makefile_star2_O3/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: rb_tree_diff
rb_tree *rb_tree_diff(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);

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

    // Special case: Both trees are the same
    if (tree_1 == tree_2) { return tree; }

    // Special case: tree_1 is empty
    if (rb_tree_is_empty(tree_1) == 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) {

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

        } 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 {

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

    // Insert all remaining data from tree_1:
    while (node_1 != NULL) {

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

    // 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 0xb2d2
movq %rsi, %r14
testq %rsi, %rsi
je 0xb2f1
movq %rdi, %r15
movq 0x8(%rdi), %rdi
callq 0x97c1
movq %rax, %rbx
testq %rax, %rax
sete %al
cmpq %r14, %r15
sete %cl
orb %al, %cl
jne 0xb2c1
movq (%r15), %rax
testq %rax, %rax
je 0xb2c1
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xb05b
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0xb03d
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0xb037
jmp 0xb05e
movq %rax, %r15
movq (%r14), %rax
testq %rax, %rax
je 0xb210
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xb097
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0xb079
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0xb073
jmp 0xb09a
movq %rax, %r14
movq (%r15), %rdi
movq (%r14), %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0xb0ff
je 0xb15f
movq 0x10(%r14), %r14
testq %r14, %r14
je 0xb210
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xb200
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xb0de
cmpq %r14, %rdx
jne 0xb0cd
addq $0x10, %rax
testq %rdx, %rdx
jne 0xb1f9
movq %r14, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r14
testq %rax, %rax
jne 0xb0c7
jmp 0xb1f4
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x98f7
movq 0x10(%r15), %r15
testq %r15, %r15
je 0xb270
movq 0x8(%r15), %rax
testq %rax, %rax
je 0xb200
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xb13b
cmpq %r15, %rdx
jne 0xb12a
addq $0x10, %rax
testq %rdx, %rdx
jne 0xb1f9
movq %r15, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r15
testq %rax, %rax
jne 0xb124
movq %rcx, %r15
jmp 0xb200
movq 0x10(%r15), %rax
testq %rax, %rax
je 0xb19f
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xb1ac
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xb188
cmpq %rax, %rdx
jne 0xb177
testq %rdx, %rdx
jne 0xb1a4
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0xb171
jmp 0xb1af
xorl %r15d, %r15d
jmp 0xb1af
movq $0x0, 0x10(%rcx)
movq %rax, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
je 0xb2ca
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xb200
movq %rax, %rcx
movq %rax, %rdx
movq %rdx, %rax
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xb1dc
cmpq %r14, %rdx
jne 0xb1cb
addq $0x10, %rax
testq %rdx, %rdx
jne 0xb1f9
movq %r14, (%rax)
movq 0x8(%rcx), %rax
movq %rcx, %r14
testq %rax, %rax
jne 0xb1c5
movq %rcx, %r14
jmp 0xb200
movq $0x0, (%rax)
testq %r15, %r15
jne 0xb09a
testq %r15, %r15
jne 0xb257
jmp 0xb26b
xorl %r14d, %r14d
jmp 0xb257
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xb254
movq %rcx, %r15
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xb235
cmpq %rax, %rdx
jne 0xb224
testq %rdx, %rdx
jne 0xb24c
movq %rax, 0x10(%rcx)
movq 0x8(%r15), %rcx
movq %r15, %rax
testq %rcx, %rcx
jne 0xb21e
jmp 0xb257
movq $0x0, 0x10(%rcx)
movq %rax, %r15
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x98f7
movq 0x10(%r15), %rax
testq %rax, %rax
jne 0xb215
testq %r14, %r14
je 0xb2c1
movq 0x10(%r14), %rax
testq %rax, %rax
je 0xb2c1
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xb2b8
movq %rdx, %rcx
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xb299
cmpq %rax, %rsi
jne 0xb288
testq %rsi, %rsi
jne 0xb2b0
movq %rax, 0x10(%rdx)
movq 0x8(%rcx), %rdx
movq %rcx, %rax
testq %rdx, %rdx
jne 0xb282
jmp 0xb2bb
movq $0x0, 0x10(%rdx)
movq %rax, %rcx
movq 0x10(%rcx), %rax
jmp 0xb274
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq
xorl %r14d, %r14d
jmp 0xb209
leaq 0x323d(%rip), %rdi      # 0xe516
leaq 0x2f2d(%rip), %rsi      # 0xe20d
leaq 0x374e(%rip), %rcx      # 0xea35
movl $0xefc, %edx            # imm = 0xEFC
callq 0x1080
leaq 0x3266(%rip), %rdi      # 0xe55e
leaq 0x2f0e(%rip), %rsi      # 0xe20d
leaq 0x372f(%rip), %rcx      # 0xea35
movl $0xefd, %edx            # imm = 0xEFD
callq 0x1080