// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/kdtree.c
// Function: vl_kdforest_query_recursively
vl_uindex
vl_kdforest_query_recursively (VlKDForestSearcher * searcher,
                               VlKDTree * tree,
                               vl_uindex nodeIndex,
                               VlKDForestNeighbor * neighbors,
                               vl_size numNeighbors,
                               vl_size * numAddedNeighbors,
                               double dist,
                               void const * query)
{

  VlKDTreeNode const * node = tree->nodes + nodeIndex ;
  vl_uindex i = node->splitDimension ;
  vl_index nextChild, saveChild ;
  double delta, saveDist ;
  double x ;
  double x1 = node->lowerBound ;
  double x2 = node->splitThreshold ;
  double x3 = node->upperBound ;
  VlKDForestSearchState * searchState ;

  searcher->searchNumRecursions ++ ;

  switch (searcher->forest->dataType) {
    case VL_TYPE_FLOAT :
      x = ((float const*) query)[i] ;
      break ;
    case VL_TYPE_DOUBLE :
      x = ((double const*) query)[i] ;
      break ;
    default :
      abort() ;
  }

  /* base case: this is a leaf node */
  if (node->lowerChild < 0) {

    vl_index begin = - node->lowerChild - 1 ;
    vl_index end   = - node->upperChild - 1 ;
    vl_index iter ;

    for (iter = begin ;
         iter < end &&
         (searcher->forest->searchMaxNumComparisons == 0 ||
          searcher->searchNumComparisons < searcher->forest->searchMaxNumComparisons) ;
         ++ iter) {

      vl_index di = tree->dataIndex [iter].index ;

      /* multiple KDTrees share the database points and we must avoid
       * adding the same point twice */
      if (searcher->searchIdBook[di] == searcher->searchId) continue ;
      searcher->searchIdBook[di] = searcher->searchId ;

      /* compare the query to this point */
      switch (searcher->forest->dataType) {
        case VL_TYPE_FLOAT:
          dist = ((VlFloatVectorComparisonFunction)searcher->forest->distanceFunction)
                 (searcher->forest->dimension,
                  ((float const *)query),
                  ((float const*)searcher->forest->data) + di * searcher->forest->dimension) ;
          break ;
        case VL_TYPE_DOUBLE:
          dist = ((VlDoubleVectorComparisonFunction)searcher->forest->distanceFunction)
                 (searcher->forest->dimension,
                  ((double const *)query),
                  ((double const*)searcher->forest->data) + di * searcher->forest->dimension) ;
          break ;
        default:
          abort() ;
      }
      searcher->searchNumComparisons += 1 ;

      /* see if it should be added to the result set */
      if (*numAddedNeighbors < numNeighbors) {
        VlKDForestNeighbor * newNeighbor = neighbors + *numAddedNeighbors ;
        newNeighbor->index = di ;
        newNeighbor->distance = dist ;
        vl_kdforest_neighbor_heap_push (neighbors, numAddedNeighbors) ;
      } else {
        VlKDForestNeighbor * largestNeighbor = neighbors + 0 ;
        if (largestNeighbor->distance > dist) {
          largestNeighbor->index = di ;
          largestNeighbor->distance = dist ;
          vl_kdforest_neighbor_heap_update (neighbors, *numAddedNeighbors, 0) ;
        }
      }
    } /* next data point */


    return nodeIndex ;
  }

#if 0
  assert (x1 <= x2 && x2 <= x3) ;
  assert (node->lowerChild >= 0) ;
  assert (node->upperChild >= 0) ;
#endif

  /*
   *   x1  x2 x3
   * x (---|---]
   *   (--x|---]
   *   (---|x--]
   *   (---|---] x
   */

  delta = x - x2 ;
  saveDist = dist + delta*delta ;

  if (x <= x2) {
    nextChild = node->lowerChild ;
    saveChild = node->upperChild ;
    if (x <= x1) {
      delta = x - x1 ;
      saveDist -= delta*delta ;
    }
  } else {
    nextChild = node->upperChild ;
    saveChild = node->lowerChild ;
    if (x > x3) {
      delta = x - x3 ;
      saveDist -= delta*delta ;
    }
  }

  if (*numAddedNeighbors < numNeighbors || neighbors[0].distance > saveDist) {
    searchState = searcher->searchHeapArray + searcher->searchHeapNumNodes ;
    searchState->tree = tree ;
    searchState->nodeIndex = saveChild ;
    searchState->distanceLowerBound = saveDist ;
    vl_kdforest_search_heap_push (searcher->searchHeapArray ,
                                  &searcher->searchHeapNumNodes) ;
  }

  return vl_kdforest_query_recursively (searcher,
                                        tree,
                                        nextChild,
                                        neighbors,
                                        numNeighbors,
                                        numAddedNeighbors,
                                        dist,
                                        query) ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x18(%rsp)
movq %r9, %rbx
movq %r8, %r15
movq %rcx, (%rsp)
movq %rsi, %r12
movq %rdi, %r13
leaq 0x40(%rdi), %r14
movq (%r12), %rax
imulq $0x38, %rdx, %rcx
movl 0x18(%rax,%rcx), %edi
movsd 0x28(%rax,%rcx), %xmm1
movsd 0x20(%rax,%rcx), %xmm4
movsd 0x30(%rax,%rcx), %xmm2
incq 0x30(%r13)
movq 0x20(%r13), %rsi
movl 0x10(%rsi), %esi
cmpl $0x2, %esi
je 0x1a931
cmpl $0x1, %esi
jne 0x1ab29
movq 0x60(%rsp), %rsi
cvtss2sd (%rsi,%rdi,4), %xmm3
jmp 0x1a93b
movq 0x60(%rsp), %rsi
movsd (%rsi,%rdi,8), %xmm3
addq %rcx, %rax
movq 0x8(%rax), %rbp
testq %rbp, %rbp
js 0x1a9d7
movaps %xmm3, %xmm0
subsd %xmm4, %xmm0
mulsd %xmm0, %xmm0
addsd 0x18(%rsp), %xmm0
ucomisd %xmm3, %xmm4
movq 0x10(%rax), %rdx
jae 0x1a97d
ucomisd %xmm2, %xmm3
jbe 0x1a978
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm3
subsd %xmm3, %xmm0
movq %rbp, %rax
jmp 0x1a995
ucomisd %xmm3, %xmm1
jb 0x1a98f
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
subsd %xmm3, %xmm0
movq %rdx, %rax
movq %rbp, %rdx
cmpq %r15, (%rbx)
jb 0x1a9ac
movq (%rsp), %rcx
movsd (%rcx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a8ee
imulq $0x18, 0x40(%r13), %rcx
movq 0x18(%r13), %rdi
movq %r12, (%rdi,%rcx)
movq %rax, 0x8(%rdi,%rcx)
movsd %xmm0, 0x10(%rdi,%rcx)
movq %r14, %rsi
movq %rdx, %rbp
callq 0x1ab2e
movq %rbp, %rdx
jmp 0x1a8ee
notq %rbp
movq 0x10(%rax), %rax
notq %rax
movq %rax, 0x18(%rsp)
movq %rbp, %r14
shlq $0x4, %r14
movq %r15, 0x20(%rsp)
movq %r12, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
cmpq 0x18(%rsp), %rbp
jge 0x1ab17
movq 0x20(%r13), %rax
movq 0xe0(%rax), %rcx
testq %rcx, %rcx
je 0x1aa21
cmpq %rcx, 0x28(%r13)
jae 0x1ab17
movq %rbx, %r15
movq 0x18(%r12), %rcx
movq (%rcx,%r14), %rbx
movq 0x10(%r13), %rcx
movq 0x48(%r13), %rsi
cmpq %rsi, (%rcx,%rbx,8)
je 0x1ab08
movq %rsi, (%rcx,%rbx,8)
movl 0x10(%rax), %ecx
cmpl $0x2, %ecx
je 0x1aa74
cmpl $0x1, %ecx
jne 0x1ab29
movq (%rax), %rdi
movq %rdi, %rdx
imulq %rbx, %rdx
shlq $0x2, %rdx
addq 0x18(%rax), %rdx
movq 0x60(%rsp), %rsi
callq *0x30(%rax)
cvtss2sd %xmm0, %xmm0
jmp 0x1aa8e
movq (%rax), %rdi
movq %rdi, %rdx
imulq %rbx, %rdx
shlq $0x3, %rdx
addq 0x18(%rax), %rdx
movq 0x60(%rsp), %rsi
callq *0x30(%rax)
incq 0x28(%r13)
movq (%r15), %rax
cmpq 0x20(%rsp), %rax
jae 0x1aac0
shlq $0x4, %rax
movq (%rsp), %rdi
movq %rbx, 0x8(%rdi,%rax)
movsd %xmm0, (%rdi,%rax)
movq (%r15), %rsi
callq 0x1b14c
incq (%r15)
movq 0x10(%rsp), %r12
jmp 0x1ab03
movq (%rsp), %rax
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %rdx
jbe 0x1ab08
movq (%rsp), %r12
movq %rbx, 0x8(%r12)
movsd %xmm0, (%r12)
movq (%r15), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1b191
movq %r12, %rdi
movq 0x10(%rsp), %r12
xorl %esi, %esi
callq 0x1b14c
movq 0x8(%rsp), %rdx
incq %rbp
addq $0x10, %r14
movq %r15, %rbx
jmp 0x1a9fc
movq %rdx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0