// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rsi, %r14
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %r8, 0x40(%rdi)
movq (%rcx), %rsi
movq %rcx, %rdi
callq 0x5bae
testq %r13, %r13
je 0x3fda
movq %rbx, 0x8(%rsp)
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x3, %rax
cmpq 0x60(%r15), %rax
jne 0x3fe9
movq %r14, %rbx
movl $0x20, %edi
callq 0x2110
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x48(%r15), %rax
movq 0x50(%r15), %rcx
movq %rcx, %rbp
subq %rax, %rbp
sarq $0x3, %rbp
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rbp
cmpq %r13, %rbp
jae 0x3ea5
cmpq %rax, %rcx
je 0x3eba
cmpq $0x1, %rbp
adcq $0x0, %rbp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x40(%r15), %rdi
movq 0x48(%r15), %rsi
testq %rdi, %rdi
je 0x3e6f
addq %r13, %rsi
movq (%rdi), %rax
callq *0x10(%rax)
testb %al, %al
je 0x3e97
movq 0x48(%r15), %rsi
movq 0x38(%r15), %rdi
addq %r13, %rsi
movq (%rdi), %rax
movq %rbx, %rdx
callq *0x10(%rax)
movq %r12, 0x38(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r14, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4942
incq %r12
addq $0x28, %r13
cmpq %r12, %rbp
jne 0x3e51
jmp 0x3eba
movq 0x68(%r15), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %r14, %r8
callq 0x4054
movq 0x8(%rsp), %r13
movq (%r14), %rdi
cmpq 0x8(%r14), %rdi
je 0x3ee9
movq (%rdi), %rbx
movq %r14, %rdi
callq 0x49d4
leaq (%rbx,%rbx,4), %rsi
shlq $0x3, %rsi
addq 0x48(%r15), %rsi
movq %r13, %rdi
callq 0x4a1c
jmp 0x3ebf
movq 0x8(%r13), %r12
subq (%r13), %r12
sarq $0x3, %r12
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r12
cmpq $0x2, %r12
jb 0x3fa5
leaq (%r12,%r12,4), %rax
shrq %r12
leaq -0x28(,%rax,8), %rbp
xorl %r15d, %r15d
movq 0x8(%rsp), %r13
movq (%r13), %rbx
leaq (%rbx,%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x33a0
movl 0x20(%rbx,%r15), %eax
leaq 0x28(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq 0x18(%rbx,%r15), %rax
movq %rax, (%rcx)
movq (%r13), %rbx
leaq (%rbx,%rbp), %rsi
leaq (%rbx,%r15), %rdi
callq 0x33a0
movl 0x20(%rbx,%rbp), %eax
movl %eax, 0x20(%rbx,%r15)
movq 0x18(%rbx,%rbp), %rax
movq %rax, 0x18(%rbx,%r15)
movq (%r13), %rbx
leaq (%rbx,%rbp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x33a0
leaq 0x28(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x20(%rbx,%rbp)
movq (%rcx), %rax
movq %rax, 0x18(%rbx,%rbp)
addq $-0x28, %rbp
addq $0x28, %r15
decq %r12
jne 0x3f24
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3fb6
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x2120
movl $0x20, %esi
movq %r14, %rdi
callq 0x2120
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3fda
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x2080
movq %rax, %r14
leaq 0x3356(%rip), %rsi      # 0x7353
movq %rax, %rdi
callq 0x21b0
movq 0x5fd4(%rip), %rsi      # 0x9fe0
movq 0x5fbd(%rip), %rdx      # 0x9fd0
movq %r14, %rdi
callq 0x2190
jmp 0x4032
movq %rax, %rbx
movq %r14, %rdi
callq 0x20d0
jmp 0x4035
jmp 0x4032
jmp 0x4032
jmp 0x4032
jmp 0x4032
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x404c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0