// File: 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 %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
addq $0x10, %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x68(%rbp), %rax
movsd %xmm0, -0x38(%rbp)
cmpq $0x0, 0x40(%rax)
je 0x76af
movq -0x68(%rbp), %rdi
movq 0x40(%rdi), %rax
movq %rax, -0x70(%rbp)
addq $0x48, %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x3b30
movq -0x70(%rbp), %rdi
movq %rax, %rsi
movq (%rdi), %rax
callq *0x10(%rax)
testb $0x1, %al
jne 0x76af
jmp 0x774a
movq -0x30(%rbp), %rdi
callq 0x8be0
cmpq -0x28(%rbp), %rax
jae 0x76ea
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movsd -0x38(%rbp), %xmm0
leaq -0x50(%rbp), %rdi
callq 0x88d0
movq -0x78(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x8880
jmp 0x7748
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x30(%rbp), %rdi
callq 0x8920
movsd -0x80(%rbp), %xmm1
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x7746
movq -0x30(%rbp), %rdi
callq 0x8940
movq -0x30(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movsd -0x38(%rbp), %xmm0
leaq -0x60(%rbp), %rdi
callq 0x88d0
movq -0x88(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8880
jmp 0x7748
jmp 0x774a
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x37f0
movsd (%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0x90(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x77c9
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x77c7
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x7630
testb $0x1, %al
jne 0x77bc
jmp 0x77c5
movb $0x1, -0x1(%rbp)
jmp 0x7966
jmp 0x77c7
jmp 0x7804
movq -0x20(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x7802
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x7630
testb $0x1, %al
jne 0x77f7
jmp 0x7800
movb $0x1, -0x1(%rbp)
jmp 0x7966
jmp 0x7802
jmp 0x7804
movq -0x30(%rbp), %rdi
callq 0x8be0
cmpq -0x28(%rbp), %rax
jae 0x781f
callq 0x8c00
movsd %xmm0, -0x40(%rbp)
jmp 0x7832
movq -0x30(%rbp), %rdi
callq 0x8920
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x37f0
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0x98(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x78d1
movq -0x20(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x78cf
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x40(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x7c60
testb $0x1, %al
jne 0x78a1
jmp 0x78cf
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x7630
testb $0x1, %al
jne 0x78c4
jmp 0x78cd
movb $0x1, -0x1(%rbp)
jmp 0x7966
jmp 0x78cf
jmp 0x7929
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x7927
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x40(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x7c60
testb $0x1, %al
jne 0x78fc
jmp 0x7927
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x7630
testb $0x1, %al
jne 0x791f
jmp 0x7925
movb $0x1, -0x1(%rbp)
jmp 0x7966
jmp 0x7927
jmp 0x7929
movq -0x30(%rbp), %rdi
callq 0x8be0
cmpq -0x28(%rbp), %rax
jne 0x794b
movq -0x30(%rbp), %rdi
callq 0x8920
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x40(%rbp), %xmm0
movq -0x20(%rbp), %rdx
callq 0x7fc0
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)