// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %r15
movq %rdi, %r13
movq %rsi, (%rsp)
movq (%rsi), %rax
imulq $0x38, %r15, %rcx
movl 0x18(%rax,%rcx), %edx
movsd 0x28(%rax,%rcx), %xmm1
movsd 0x20(%rax,%rcx), %xmm4
movsd 0x30(%rax,%rcx), %xmm2
incq 0x30(%r13)
movq 0x20(%r13), %rdi
movl 0x10(%rdi), %edi
cmpl $0x2, %edi
je 0x1d964
cmpl $0x1, %edi
jne 0x1db49
movq 0x60(%rsp), %rdi
xorps %xmm3, %xmm3
cvtss2sd (%rdi,%rdx,4), %xmm3
jmp 0x1d96e
movq 0x60(%rsp), %rdi
movsd (%rdi,%rdx,8), %xmm3
addq %rcx, %rax
movq 0x8(%rax), %rbp
testq %rbp, %rbp
js 0x1da0e
movaps %xmm3, %xmm0
subsd %xmm4, %xmm0
mulsd %xmm0, %xmm0
addsd 0x8(%rsp), %xmm0
ucomisd %xmm3, %xmm4
movq 0x10(%rax), %r15
jae 0x1d9b0
ucomisd %xmm2, %xmm3
jbe 0x1d9ab
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm3
subsd %xmm3, %xmm0
movq %rbp, %rax
jmp 0x1d9c8
ucomisd %xmm3, %xmm1
jb 0x1d9c2
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
subsd %xmm3, %xmm0
movq %r15, %rax
movq %rbp, %r15
cmpq %r14, (%rbx)
jb 0x1d9dd
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1d91f
movq 0x18(%r13), %rdi
movq 0x40(%r13), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rsi, (%rdi,%rcx,8)
movq %rax, 0x8(%rdi,%rcx,8)
movsd %xmm0, 0x10(%rdi,%rcx,8)
movq 0x40(%r13), %rsi
callq 0x1e255
movq (%rsp), %rsi
incq 0x40(%r13)
jmp 0x1d91f
movq %r14, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %rbp
jle 0x1db37
notq 0x8(%rsp)
notq %rbp
movq %rbp, %r14
shlq $0x4, %r14
movq %r12, 0x10(%rsp)
movq 0x18(%rsp), %r12
movq 0x20(%r13), %rax
movq 0xe0(%rax), %rcx
testq %rcx, %rcx
je 0x1da5d
cmpq %rcx, 0x28(%r13)
jae 0x1db37
movq 0x18(%rsi), %rcx
movq (%rcx,%r14), %rbx
movq 0x10(%r13), %rcx
movq 0x48(%r13), %rdx
cmpq %rdx, (%rcx,%rbx,8)
je 0x1db25
movq %rdx, (%rcx,%rbx,8)
movl 0x10(%rax), %ecx
cmpl $0x2, %ecx
je 0x1daac
cmpl $0x1, %ecx
jne 0x1db49
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 0x1dac6
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 (%r12), %rax
cmpq 0x20(%rsp), %rax
jae 0x1daf7
shlq $0x4, %rax
movq 0x10(%rsp), %rdi
movq %rbx, 0x8(%rdi,%rax)
movsd %xmm0, (%rdi,%rax)
movq (%r12), %rsi
callq 0x1e13b
incq (%r12)
jmp 0x1db21
movq 0x10(%rsp), %rdi
movsd (%rdi), %xmm1
ucomisd %xmm0, %xmm1
movq (%rsp), %rsi
jbe 0x1db25
movq %rbx, 0x8(%rdi)
movsd %xmm0, (%rdi)
movq 0x18(%rsp), %rax
movq (%rax), %rsi
xorl %edx, %edx
callq 0x1e180
movq (%rsp), %rsi
incq %rbp
addq $0x10, %r14
cmpq 0x8(%rsp), %rbp
jl 0x1da43
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0