// File: /workspace/github_c_makefile_star2_O1/CarlosLunaMota[P]BinaryTrees/BinaryTrees.c
// Function: bs_tree_copy
bs_tree *bs_tree_copy(const bs_tree *tree) {

    bs_tree *new_tree = NULL;
    bs_node *new_node = NULL;
    bs_node *pred;
    bs_node *node;
    
    // Sanity check:
    assert(tree != NULL);
    
    // Create a new tree:
    new_tree = new_bs_tree(tree->comp);
    if (new_tree == NULL) { return NULL; }

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

    // Insert all data from tree into new_tree:
    while (node != NULL) {

        // insert node->data in new_tree ///////////////////////////////////////
        if (new_node == NULL)  {
            new_tree->root = (bs_node *) malloc(sizeof(bs_node));
            new_node = new_tree->root;
        } else {
            new_node->right = (bs_node *) malloc(sizeof(bs_node));
            new_node = new_node->right;
        }
        if (new_node == NULL) {
            fprintf(stderr, "ERROR: Unable to allocate bs_node\n");
            return new_tree;
        } else {
            new_node->data  = node->data;
            new_node->left  = NULL;
            new_node->right = NULL;
        }
        ////////////////////////////////////////////////////////////////////////

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

    // Return the resulting tree:
    return new_tree;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
testq %rdi, %rdi
je 0x7a05
movq %rdi, %r14
movq 0x8(%rdi), %rdi
callq 0x78a4
testq %rax, %rax
je 0x7955
movq %rax, %rbx
movq (%r14), %rax
testq %rax, %rax
je 0x795c
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x7961
movq %rcx, %r14
movq %rcx, %rdx
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x7937
movq %rax, 0x10(%rcx)
movq 0x8(%r14), %rcx
movq %r14, %rax
testq %rcx, %rcx
jne 0x7931
jmp 0x7964
xorl %ebx, %ebx
jmp 0x79f5
xorl %r14d, %r14d
jmp 0x7964
movq %rax, %r14
testq %r14, %r14
je 0x79f5
xorl %eax, %eax
movq %rax, %r15
movl $0x18, %edi
callq 0x10d0
leaq 0x10(%r15), %rcx
testq %r15, %r15
cmoveq %rbx, %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x79fe
movq (%r14), %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x10(%r14), %rcx
testq %rcx, %rcx
je 0x79dc
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x79e9
movq %rdx, %r14
movq %rdx, %rsi
movq %rsi, %rdx
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x79c5
cmpq %rcx, %rsi
jne 0x79b4
testq %rsi, %rsi
jne 0x79e1
movq %rcx, 0x10(%rdx)
movq 0x8(%r14), %rdx
movq %r14, %rcx
testq %rdx, %rdx
jne 0x79ae
jmp 0x79ec
xorl %r14d, %r14d
jmp 0x79ec
movq $0x0, 0x10(%rdx)
movq %rcx, %r14
testq %r14, %r14
jne 0x796f
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq
callq 0x114c
jmp 0x79f5
leaq 0x6877(%rip), %rdi      # 0xe283
leaq 0x67fa(%rip), %rsi      # 0xe20d
leaq 0x6876(%rip), %rcx      # 0xe290
movl $0x76, %edx
callq 0x1080