// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq %r15, %r12
movq 0x8(%rsp), %rax
subq %rax, %r12
movq %r12, %rbp
shrq %rbp
addq %rax, %rbp
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x4205
movq 0x8(%rbx), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss (%rdi,%rbp,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x41ef
movq 0x10(%rbx), %rsi
cmpq %rsi, 0x20(%rbx)
je 0x42e2
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x450a
cmpq $0x1, %r12
je 0x4312
cmpq $0x2, %r12
je 0x4321
movq 0x10(%rsp), %rsi
movss (%rsi,%r13,4), %xmm0
movq 0x8(%r14), %rax
movq 0x18(%r14), %rcx
movq (%rax,%r13,8), %rax
movss (%rax,%rbp,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 0x42a8
leaq 0x1(%rbp), %rdx
movl %eax, %r13d
movq %r14, %rdi
movq %r15, %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 %rbp, %r15
jbe 0x41c8
jmp 0x4312
movl %eax, %r13d
movq %r14, %rdi
movq 0x8(%rsp), %rdx
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
ja 0x4312
incq %rbp
movq %rbp, 0x8(%rsp)
jmp 0x41c8
movq 0x28(%rbx), %rdi
shlq $0x4, %rsi
addq %rdi, %rsi
leaq 0x3(%rsp), %rdx
callq 0x45e6
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r15, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x4388
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x434b
movq 0x8(%rbx), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss -0x4(%rdi,%rbp,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x4334
movq 0x20(%rbx), %rax
cmpq 0x10(%rbx), %rax
jne 0x436e
movq %rbx, %rdi
movq %rbp, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x458c
movq %rbx, %rdi
movq %rbp, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x450a
nop