// File: /workspace/github_cpp_makefile_star2_O1/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 0x5c20
testq %r13, %r13
je 0x3fe5
movq %rbx, 0x8(%rsp)
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x3, %rax
cmpq 0x60(%r15), %rax
jne 0x400b
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 0x3ec7
cmpq %rax, %rcx
je 0x3edc
cmpq $0x1, %rbp
adcq $0x0, %rbp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x40(%r15), %rdi
testq %rdi, %rdi
je 0x3e8d
movq 0x48(%r15), %rsi
addq %r13, %rsi
movq (%rdi), %rax
callq *0x10(%rax)
testb %al, %al
je 0x3eb9
movq 0x38(%r15), %rdi
movq 0x48(%r15), %rsi
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 0x4964
incq %r12
addq $0x28, %r13
cmpq %r12, %rbp
jne 0x3e73
jmp 0x3edc
movq 0x68(%r15), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %r14, %r8
callq 0x4076
movq 0x8(%rsp), %r13
movq (%r14), %rax
cmpq 0x8(%r14), %rax
je 0x3f0b
movq (%rax), %rbx
movq %r14, %rdi
callq 0x49fc
leaq (%rbx,%rbx,4), %rsi
shlq $0x3, %rsi
addq 0x48(%r15), %rsi
movq %r13, %rdi
callq 0x4a3c
jmp 0x3ee1
movq 0x8(%r13), %r12
subq (%r13), %r12
sarq $0x3, %r12
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r12
cmpq $0x2, %r12
jb 0x3fc4
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 0x339e
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 0x339e
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 0x339e
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 0x3f46
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3fd8
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 0x3ffc
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 0x3314(%rip), %rsi      # 0x7333
movq %rax, %rdi
callq 0x21b0
movq 0x5fb2(%rip), %rsi      # 0x9fe0
movq 0x5f9b(%rip), %rdx      # 0x9fd0
movq %r14, %rdi
callq 0x2190
jmp 0x4054
movq %rax, %rbx
movq %r14, %rdi
callq 0x20d0
jmp 0x4057
jmp 0x4054
jmp 0x4054
jmp 0x4054
jmp 0x4054
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x406e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0