// File: /workspace/github_cpp_makefile_star2_O3/cfiutak1[P]HPyC-KD-Tree/./utils/filewriter/../../src/kdtree/KDTree.hpp
// Function: void hpyc::KDTree<float>::nearestNeighborsSearch<true>(float const*, unsigned long, unsigned long, unsigned long, hpyc::KNNQueue<float>&) const
void KDTree<ItemT>::nearestNeighborsSearch(const ItemT* query_point, std::size_t begin, std::size_t end, std::size_t depth, KNNQueue<ItemT>& nearest_neighbors) const {
    std::size_t range = end - begin;
    std::size_t traverser_index = begin + (range / 2);

    this->readPointAt(nearest_neighbors.getPotentialNeighbor(), traverser_index);

    // Optimization: This templatization is done to prevent unnecessarily checking if the KNN queue is full. Once
    // the queue is full, it will never be not full again, so we can cut down on some work by only checking the fullness
    // until it becomes full.
    // TODO - Can probably cut down on code reuse by making this template argument a function pointer instead of a boolean.
    if constexpr (CheckIfFull) {
        if (nearest_neighbors.full()) {
            nearest_neighbors.heapify();
            return this->nearestNeighborsSearch<false>(query_point, begin, end, depth, nearest_neighbors);
        }

        nearest_neighbors.registerAsNeighbor(traverser_index);

        // Base case - If there's two elements left, the 2nd element has already been tested. Thus, we simply register the
        // 1st element and stop recursing.
        if (range == 2) {
            this->readPointAt(nearest_neighbors.getPotentialNeighbor(), traverser_index - 1);

            if (nearest_neighbors.full()) {
                nearest_neighbors.registerAsNeighborIfCloser(traverser_index);
            }

            else {
                nearest_neighbors.registerAsNeighbor(traverser_index);
            }

            return;
        }
    }

    else {
        nearest_neighbors.registerAsNeighborIfCloser(traverser_index);

        // Base case - If there's two elements left, the 2nd element has already been tested. Thus, we simply register the
        // 1st element and stop recursing.
        if (range == 2) {
            this->readPointAt(nearest_neighbors.getPotentialNeighbor(), traverser_index - 1);

            nearest_neighbors.registerAsNeighborIfCloser(traverser_index);
            return;
        }
    }

    // Base case - If there's one element left, it has already been tested so stop recursing.
    if (range == 1) { return; }

    // TODO - These used to be floats. If there's very very small distances, this might behave differently if ItemT is a double instead.
    ItemT query_at_current_dimension = query_point[depth];
    ItemT traverser_at_current_dimension = this->nodes[depth][traverser_index];
    ItemT difference_at_current_dimension = traverser_at_current_dimension - query_at_current_dimension;
    ItemT distance_from_query_at_current_dimension = difference_at_current_dimension * difference_at_current_dimension;

    unsigned int new_depth = (depth + 1) & (-1 + (depth == this->num_dimensions - 1));

    // If the query is less than the traverser at the current dimension,
    //     1. Traverse down the left subtree.
    //     2. After fully traversing the left subtree, determine if the right subtree can possibly have a closer neighbor.
    //     3. If the right subtree is not viable, return. Otherwise, traverse the right subtree.
    if (query_at_current_dimension < traverser_at_current_dimension) {
        this->nearestNeighborsSearch<CheckIfFull>(query_point, begin, traverser_index, new_depth, nearest_neighbors);

        double farthest_neighbor_distance = nearest_neighbors.top().distance_from_queried_point;

        if (farthest_neighbor_distance < distance_from_query_at_current_dimension) { return; }

        this->nearestNeighborsSearch<CheckIfFull>(query_point, traverser_index + 1, end, new_depth, nearest_neighbors);
    }

    // If the query is greater than or equal to the traverser at the current dimension,
    //     1. Traverse down the right subtree.
    //     2. After fully traversing the right subtree, determine if the left subtree can possibly have a closer neighbor.
    //     3. If the left subtree is not viable, return. Otherwise, traverse the left subtree.
    else {
        this->nearestNeighborsSearch<CheckIfFull>(query_point, traverser_index + 1, end, new_depth, nearest_neighbors);

        double farthest_neighbor_distance = nearest_neighbors.top().distance_from_queried_point;

        if (farthest_neighbor_distance < distance_from_query_at_current_dimension) { return; }

        this->nearestNeighborsSearch<CheckIfFull>(query_point, begin, traverser_index, new_depth, nearest_neighbors);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %rbx
movq %r8, %r13
movq %rcx, %rbp
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq %rbp, %r15
movq 0x8(%rsp), %rax
subq %rax, %r15
movq %r15, %r12
shrq %r12
addq %rax, %r12
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x4206
movq 0x8(%rbx), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss (%rdi,%r12,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x41ef
movq 0x20(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x42e6
movq %rbx, %rdi
movq %r12, %rsi
callq 0x450e
cmpq $0x1, %r15
je 0x4316
cmpq $0x2, %r15
je 0x4325
movq 0x10(%rsp), %rsi
movss (%rsi,%r13,4), %xmm0
movq 0x8(%r14), %rax
movq 0x18(%r14), %rcx
movq (%rax,%r13,8), %rax
movss (%rax,%r12,4), %xmm1
decq %rcx
leal 0x1(%r13), %eax
cmpq %r13, %rcx
movl $0x0, %ecx
cmovel %ecx, %eax
ucomiss %xmm0, %xmm1
subss %xmm0, %xmm1
mulss %xmm1, %xmm1
movss %xmm1, 0x4(%rsp)
ja 0x42ab
leaq 0x1(%r12), %rdx
movl %eax, %r13d
movq %r14, %rdi
movq %rbp, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x41a4
movq 0x28(%rbx), %rax
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
ucomisd 0x8(%rax), %xmm0
movq %r12, %rbp
jbe 0x41c8
jmp 0x4316
movl %eax, %r13d
movq %r14, %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x41a4
movq 0x28(%rbx), %rax
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
incq %r12
ucomisd 0x8(%rax), %xmm0
movq %r12, 0x8(%rsp)
jbe 0x41c8
jmp 0x4316
movq 0x28(%rbx), %rdi
shlq $0x4, %rsi
addq %rdi, %rsi
leaq 0x3(%rsp), %rdx
callq 0x45ea
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x438c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x4350
movq 0x8(%rbx), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss -0x4(%rdi,%r12,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x4338
movq 0x20(%rbx), %rax
cmpq 0x10(%rbx), %rax
jne 0x4373
movq %rbx, %rdi
movq %r12, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4590
movq %rbx, %rdi
movq %r12, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x450e