// File: /workspace/github_cpp_makefile_star2_O1/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 0x40ce
movq (%rbx), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0x48(%r15), %rsi
movq (%rdi), %rax
movsd %xmm0, 0x8(%rsp)
callq *0x10(%rax)
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x4126
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jae 0x40ef
movq (%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
jmp 0x4119
movsd 0x8(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4126
movq %r13, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x49fc
movq (%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsi)
movq %r13, %rdi
callq 0x4964
movq (%r14), %rax
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
movsd (%rdx,%rcx,8), %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x4143
movq 0x28(%rbx), %rdx
jmp 0x4147
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x4169
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movq %r13, %r8
callq 0x4076
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x423b
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jae 0x4187
movsd 0x315b(%rip), %xmm0     # 0x72e0
jmp 0x418c
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 0x41cd
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x4214
movq %r15, %rdi
movq %r14, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x4434
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x4214
movq 0x30(%rbx), %rdx
jmp 0x41f5
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x4214
movq %r15, %rdi
movq %r14, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x4434
movsd 0x8(%rsp), %xmm0
testb %al, %al
je 0x4214
movq 0x28(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movq %r13, %r8
callq 0x4076
movsd 0x8(%rsp), %xmm0
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x423b
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r12, %rcx
jne 0x422d
movsd 0x8(%rax), %xmm0
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4554
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop