// File: /workspace/github_cpp_makefile_star2_O2/cdalitz[P]kdtree-cpp/kdtree.cpp
// Function: Kdtree::KdTree::k_nearest_neighbors(std::vector<double, std::allocator<double>> const&, unsigned long, std::vector<Kdtree::KdNode, std::allocator<Kdtree::KdNode>>*, Kdtree::KdNodePredicate*)
void KdTree::k_nearest_neighbors(const CoordPoint& point, size_t k,
                                 KdNodeVector* result,
                                 KdNodePredicate* pred /*=NULL*/) {
  size_t i;
  KdNode temp;
  searchpredicate = pred;

  result->clear();
  if (k < 1) return;
  if (point.size() != dimension)
    throw std::invalid_argument(
        "kdtree::k_nearest_neighbors(): point must be of same dimension as "
        "kdtree");

  // collect result of k values in neighborheap
  //std::priority_queue<nn4heap, std::vector<nn4heap>, compare_nn4heap>*
  //neighborheap = new std::priority_queue<nn4heap, std::vector<nn4heap>, compare_nn4heap>();
  SearchQueue* neighborheap = new SearchQueue();
  if (k > allnodes.size()) {
    // when more neighbors asked than nodes in tree, return everything
    k = allnodes.size();
    for (i = 0; i < k; i++) {
      if (!(searchpredicate && !(*searchpredicate)(allnodes[i])))
        neighborheap->push(
            nn4heap(i, distance->distance(allnodes[i].point, point)));
    }
  } else {
    neighbor_search(point, root, k, neighborheap);
  }

  // copy over result sorted by distance
  // (we must revert the vector for ascending order)
  while (!neighborheap->empty()) {
    i = neighborheap->top().dataindex;
    neighborheap->pop();
    result->push_back(allnodes[i]);
  }
  // beware that less than k results might have been returned
  k = result->size();
  for (i = 0; i < k / 2; i++) {
    temp = (*result)[i];
    (*result)[i] = (*result)[k - 1 - i];
    (*result)[k - 1 - i] = temp;
  }
  delete neighborheap;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %r8, 0x40(%rdi)
movq %rcx, %rdi
callq 0x2dda
testq %rbp, %rbp
je 0x3b3a
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x3, %rax
cmpq 0x60(%r15), %rax
jne 0x3b53
movq %r14, 0x8(%rsp)
pushq $0x20
popq %rdi
callq 0x2100
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x50(%r15), %rax
subq 0x48(%r15), %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
cmpq %rbp, %rax
jae 0x3a8b
movq %rax, %r13
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x3aa2
movq 0x40(%r15), %rdi
movq 0x48(%r15), %rsi
testq %rdi, %rdi
je 0x3a58
addq %rbp, %rsi
movq (%rdi), %rax
callq *0x10(%rax)
testb %al, %al
je 0x3a82
movq 0x48(%r15), %rsi
movq 0x38(%r15), %rdi
addq %rbp, %rsi
movq (%rdi), %rax
movq 0x8(%rsp), %rdx
callq *0x10(%rax)
movq %r12, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4370
incq %r12
addq $0x28, %rbp
jmp 0x3a35
movq 0x68(%r15), %rdx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rcx
movq %r14, %r8
callq 0x3bb0
movq (%r14), %rax
cmpq 0x8(%r14), %rax
je 0x3ac8
movq (%rax), %r12
movq %r14, %rdi
callq 0x4386
imulq $0x28, %r12, %rsi
addq 0x48(%r15), %rsi
movq %rbx, %rdi
callq 0x439e
jmp 0x3aa2
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %rax, %r15
imulq $0x28, %rax, %r13
shrq %r15
addq $-0x28, %r13
xorl %ebp, %ebp
leaq 0x20(%rsp), %r12
subq $0x1, %r15
jb 0x3b27
movq (%rbx), %rsi
addq %rbp, %rsi
movq %r12, %rdi
callq 0x43ca
movq (%rbx), %rdi
leaq (%rdi,%r13), %rsi
addq %rbp, %rdi
callq 0x43ca
movq (%rbx), %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x43ca
addq $0x28, %rbp
addq $-0x28, %r13
jmp 0x3aec
movq %r14, %rdi
callq 0x46f6
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x2110
leaq 0x20(%rsp), %rdi
callq 0x2fac
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x2080
movq %rax, %r14
leaq 0x281e(%rip), %rsi      # 0x6383
movq %rax, %rdi
callq 0x2190
movq 0x646c(%rip), %rsi      # 0x9fe0
movq 0x6455(%rip), %rdx      # 0x9fd0
movq %r14, %rdi
callq 0x2170
jmp 0x3b9a
movq %rax, %rbx
movq %r14, %rdi
callq 0x20c0
jmp 0x3b9d
jmp 0x3b9a
jmp 0x3b9a
jmp 0x3b9a
jmp 0x3b9a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2fac
movq %rbx, %rdi
callq 0x2180
nop