// File: /workspace/github_cpp_makefile_star2_O2/cdalitz[P]kdtree-cpp/kdtree.cpp
// Function: Kdtree::KdTree::neighbor_search(std::vector<double, std::allocator<double>> const&, Kdtree::kdtree_node*, unsigned long, std::priority_queue<Kdtree::nn4heap, std::vector<Kdtree::nn4heap, std::allocator<Kdtree::nn4heap>>, Kdtree::compare_nn4heap>*)
bool KdTree::neighbor_search(const CoordPoint& point, kdtree_node* node,
                             size_t k, SearchQueue* neighborheap) {
  double curdist, dist;

  curdist = distance->distance(point, node->point);
  if (!(searchpredicate && !(*searchpredicate)(allnodes[node->dataindex]))) {
    if (neighborheap->size() < k) {
      neighborheap->push(nn4heap(node->dataindex, curdist));
    } else if (curdist < neighborheap->top().distance) {
      neighborheap->pop();
      neighborheap->push(nn4heap(node->dataindex, curdist));
    }
  }
  // first search on side closer to point
  if (point[node->cutdim] < node->point[node->cutdim]) {
    if (node->loson)
      if (neighbor_search(point, node->loson, k, neighborheap)) return true;
  } else {
    if (node->hison)
      if (neighbor_search(point, node->hison, k, neighborheap)) return true;
  }
  // second search on farther side, if necessary
  if (neighborheap->size() < k) {
    dist = std::numeric_limits<double>::max();
  } else {
    dist = neighborheap->top().distance;
  }
  if (point[node->cutdim] < node->point[node->cutdim]) {
    if (node->hison && bounds_overlap_ball(point, dist, node->hison))
      if (neighbor_search(point, node->hison, k, neighborheap)) return true;
  } else {
    if (node->loson && bounds_overlap_ball(point, dist, node->loson))
      if (neighbor_search(point, node->loson, k, neighborheap)) return true;
  }

  if (neighborheap->size() == k) dist = neighborheap->top().distance;
  return ball_within_bounds(point, dist, node);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %r8, %r13
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x38(%rdi), %rdi
addq $0x10, %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x40(%r15), %rdi
testq %rdi, %rdi
je 0x3c01
imulq $0x28, (%rbx), %rsi
addq 0x48(%r15), %rsi
movq (%rdi), %rax
movsd %xmm0, 0x8(%rsp)
callq *0x10(%rax)
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x3c59
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jae 0x3c22
movq (%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
jmp 0x3c4c
movsd 0x8(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3c59
movq %r13, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x4386
movq (%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsi)
movq %r13, %rdi
callq 0x4370
movq (%r14), %rax
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
movsd (%rdx,%rcx,8), %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x3c76
movq 0x28(%rbx), %rdx
jmp 0x3c7a
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x3c9c
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movq %r13, %r8
callq 0x3bb0
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x3d6e
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jae 0x3cba
movsd 0x2668(%rip), %xmm0     # 0x6320
jmp 0x3cbf
movsd 0x8(%rax), %xmm0
movq (%r14), %rax
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
movsd (%rdx,%rcx,8), %xmm1
ucomisd (%rax,%rcx,8), %xmm1
jbe 0x3d00
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x3d47
movq %r15, %rdi
movq %r14, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x3f50
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x3d47
movq 0x30(%rbx), %rdx
jmp 0x3d28
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x3d47
movq %r15, %rdi
movq %r14, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x3f50
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x3d47
movq 0x28(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movq %r13, %r8
callq 0x3bb0
movsd 0x8(%rsp), %xmm0
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x3d6e
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jne 0x3d60
movsd 0x8(%rax), %xmm0
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4056
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq