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

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

            // insert node_2->data in tree /////////////////////////////////////
            data = node_2->data;
            data = rb_tree_insert_max(tree, data);
            assert(data == 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;
                    }
                }
            }
            ////////////////////////////////////////////////////////////////////

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

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

    // Insert all remaining data from tree_2:
    while (node_2 != NULL) {

        // insert node_2->data in tree /////////////////////////////////////////
        data = node_2->data;
        data = rb_tree_insert_max(tree, data);
        assert(data == 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 0xa4d2
movq %rsi, %r14
testq %rsi, %rsi
je 0xa4f1
movq %rdi, %r15
cmpq %r14, %rdi
je 0xa27e
movq 0x8(%r15), %rdi
callq 0x9037
testq %rax, %rax
je 0xa28b
movq %rax, %rbx
movq (%r15), %r15
testq %r15, %r15
je 0xa292
movq 0x8(%r15), %rax
testq %rax, %rax
je 0xa295
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0xa269
movq %r15, 0x10(%rcx)
movq %rax, %r15
jmp 0xa25d
movq %r15, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x90a3
xorl %ebx, %ebx
jmp 0xa4c9
xorl %r15d, %r15d
movq (%r14), %r14
testq %r14, %r14
je 0xa2be
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xa2c1
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0xa2a9
movq %r14, 0x10(%rcx)
movq %rax, %r14
jmp 0xa29d
xorl %r14d, %r14d
testq %r15, %r15
je 0xa470
testq %r14, %r14
je 0xa470
movq (%r15), %rdi
movq (%r14), %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0xa336
je 0xa382
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x9172
movq 0x10(%r14), %rax
movl $0x0, %r14d
testq %rax, %rax
je 0xa2c1
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xa3d8
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xa321
cmpq %rax, %rsi
jne 0xa310
addq $0x10, %rcx
testq %rsi, %rsi
jne 0xa3d4
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0xa300
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x9172
movq 0x10(%r15), %rax
movl $0x0, %r15d
testq %rax, %rax
je 0xa2c1
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xa3c6
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xa371
cmpq %rax, %rsi
jne 0xa360
addq $0x10, %rcx
testq %rsi, %rsi
jne 0xa3ce
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0xa354
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x9172
movq 0x10(%r15), %r15
testq %r15, %r15
je 0xa3c1
movq 0x8(%r15), %rax
testq %rax, %rax
je 0xa3e5
movq %rax, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
je 0xa3b3
cmpq %r15, %rdx
jne 0xa3a2
testq %rdx, %rdx
jne 0xa3e0
movq %r15, 0x10(%rcx)
movq %rax, %r15
jmp 0xa396
xorl %r15d, %r15d
jmp 0xa3e5
movq %rax, %r15
jmp 0xa2c1
movq %rax, %r15
movq %r14, %rax
andq $0x0, (%rcx)
movq %rax, %r14
jmp 0xa2c1
andq $0x0, 0x10(%rcx)
movq 0x10(%r14), %rax
movl $0x0, %r14d
testq %rax, %rax
je 0xa2c1
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xa3d8
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xa415
cmpq %rax, %rsi
jne 0xa404
addq $0x10, %rcx
testq %rsi, %rsi
jne 0xa3d4
movq %rax, (%rcx)
movq %rdx, %rax
jmp 0xa3f8
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x9172
movq 0x10(%r15), %rax
xorl %r15d, %r15d
testq %rax, %rax
je 0xa470
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xa46d
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xa45a
cmpq %rax, %rsi
jne 0xa449
testq %rsi, %rsi
jne 0xa468
movq %rax, 0x10(%rdx)
movq %rcx, %rax
jmp 0xa43d
andq $0x0, 0x10(%rdx)
movq %rax, %r15
testq %r15, %r15
jne 0xa426
jmp 0xa4c4
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x9172
movq 0x10(%r14), %rax
movl $0x0, %r14d
testq %rax, %rax
je 0xa4c4
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xa4c1
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0xa4ae
cmpq %rax, %rsi
jne 0xa49d
testq %rsi, %rsi
jne 0xa4bc
movq %rax, 0x10(%rdx)
movq %rcx, %rax
jmp 0xa491
andq $0x0, 0x10(%rdx)
movq %rax, %r14
testq %r14, %r14
jne 0xa477
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq
leaq 0x303d(%rip), %rdi      # 0xd516
leaq 0x2d2d(%rip), %rsi      # 0xd20d
leaq 0x34d5(%rip), %rcx      # 0xd9bc
movl $0xd58, %edx            # imm = 0xD58
callq 0x1080
leaq 0x3066(%rip), %rdi      # 0xd55e
leaq 0x2d0e(%rip), %rsi      # 0xd20d
leaq 0x34b6(%rip), %rcx      # 0xd9bc
movl $0xd59, %edx            # imm = 0xD59
callq 0x1080