// File: ALSchwalm[P]cppEvolve/include/cppEvolve/Genome/Tree/Crossover.hpp
// Function: evolve::tree::Tree<int>* evolve::tree::crossover::singlePoint<int>(evolve::tree::Tree<int> const*, evolve::tree::Tree<int> const*)
Tree<T>* singlePoint(const Tree<T>* left, const Tree<T>* right) {
    auto tree = left->clone(); // Copy left

    auto treeDepth = right->getDepth();
    auto depth = utils::random_uint(treeDepth);

    auto sourceNode = right->root; // Crossover from the right to the left
    BaseNode<T>* prevSourceNode = nullptr;

    for (unsigned int i = 0; i < depth; ++i) {
        if (sourceNode->getChildren().empty()) {
            sourceNode = prevSourceNode; // Do not copy terminators
            break;
        }
        prevSourceNode = sourceNode;
        sourceNode = sourceNode->getChildren()[utils::random_uint(
            sourceNode->getChildren().size())];
    }

    auto currentNode = tree->root;
    BaseNode<T>* prevNode = nullptr;

    for (unsigned int i = 0; i < depth; ++i) {
        prevNode = currentNode;
        auto nextNodeLocation =
            utils::random_uint(currentNode->getChildren().size());
        currentNode = currentNode->getChildren()[nextNodeLocation];

        if (sourceNode->getDepth() == currentNode->getDepth()) {
            auto& children = prevNode->getChildren();
            delete currentNode;
            children[nextNodeLocation] = sourceNode->clone();
            break;
        } else if (currentNode->getChildren().empty()) {
            break; // This point should never be reached
        }
    }

    return tree;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xabd0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0xac50
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %edi
callq 0x2420
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq -0x28(%rbp), %rax
jae 0x349a
movq -0x30(%rbp), %rdi
callq 0xac70
movq %rax, %rdi
callq 0xac90
testb $0x1, %al
jne 0x3444
jmp 0x344e
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x349a
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xac70
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0xac70
movq %rax, %rdi
callq 0x7430
movq %rax, %rdi
callq 0x2420
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0xacd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3424
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpq -0x28(%rbp), %rax
jae 0x359a
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0xac70
movq %rax, %rdi
callq 0x7430
movq %rax, %rdi
callq 0x2420
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rdi
callq 0xac70
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0xacd0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0xacf0
movl %eax, -0x74(%rbp)
movq -0x48(%rbp), %rdi
callq 0xacf0
movl %eax, %ecx
movl -0x74(%rbp), %eax
cmpl %ecx, %eax
jne 0x356f
movq -0x50(%rbp), %rdi
callq 0xac70
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x3545
movq -0x80(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xacd0
movq -0x88(%rbp), %rcx
movq %rcx, (%rax)
jmp 0x359a
movq -0x48(%rbp), %rdi
callq 0xac70
movq %rax, %rdi
callq 0xac90
testb $0x1, %al
jne 0x3586
jmp 0x3588
jmp 0x359a
jmp 0x358a
jmp 0x358c
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x34b4
movq -0x18(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)