// File: /workspace/github_c_makefile_star2_O1/zpole[P]IICV/vl/kdtree.c
// Function: vl_kdforestsearcher_query
vl_size
vl_kdforestsearcher_query (VlKDForestSearcher * self,
                           VlKDForestNeighbor * neighbors,
                           vl_size numNeighbors,
                           void const * query)
{

  vl_uindex i, ti ;
  vl_bool exactSearch = self->forest->searchMaxNumComparisons == 0 ;

  VlKDForestSearchState * searchState  ;
  vl_size numAddedNeighbors = 0 ;

  assert (neighbors) ;
  assert (numNeighbors > 0) ;
  assert (query) ;

  /* this number is used to differentiate a query from the next */
  self -> searchId += 1 ;
  self -> searchNumRecursions = 0 ;

  self->searchNumComparisons = 0 ;
  self->searchNumSimplifications = 0 ;

  /* put the root node into the search heap */
  self->searchHeapNumNodes = 0 ;
  for (ti = 0 ; ti < self->forest->numTrees ; ++ ti) {
    searchState = self->searchHeapArray + self->searchHeapNumNodes ;
    searchState -> tree = self->forest->trees[ti] ;
    searchState -> nodeIndex = 0 ;
    searchState -> distanceLowerBound = 0 ;

    vl_kdforest_search_heap_push (self->searchHeapArray, &self->searchHeapNumNodes) ;
  }

  /* branch and bound */
  while (exactSearch || self->searchNumComparisons < self->forest->searchMaxNumComparisons)
  {
    /* pop the next optimal search node */
    VlKDForestSearchState * searchState ;

    /* break if search space completed */
    if (self->searchHeapNumNodes == 0) {
      break ;
    }
    searchState = self->searchHeapArray +
                  vl_kdforest_search_heap_pop (self->searchHeapArray, &self->searchHeapNumNodes) ;
    /* break if no better solution may exist */
    if (numAddedNeighbors == numNeighbors &&
        neighbors[0].distance < searchState->distanceLowerBound) {
      self->searchNumSimplifications ++ ;
      break ;
    }
    vl_kdforest_query_recursively (self,
                                   searchState->tree,
                                   searchState->nodeIndex,
                                   neighbors,
                                   numNeighbors,
                                   &numAddedNeighbors,
                                   searchState->distanceLowerBound,
                                   query) ;
  }

  /* sort neighbors by increasing distance */
  for (i = numAddedNeighbors ; i < numNeighbors ; ++ i) {
    neighbors[i].index = -1 ;
    neighbors[i].distance = VL_NAN_F ;
  }

  while (numAddedNeighbors) {
    vl_kdforest_neighbor_heap_pop (neighbors, &numAddedNeighbors) ;
  }

  return self->searchNumComparisons ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rdi, %r12
movq 0x20(%rdi), %rax
movq 0xe0(%rax), %rbp
movq $0x0, 0x8(%rsp)
incq 0x48(%rdi)
movq %rsi, %rbx
leaq 0x28(%rdi), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
movq 0x20(%rdi), %rax
cmpq $0x0, 0x40(%rax)
je 0x1dcd2
xorl %r13d, %r13d
movq 0x18(%r12), %rdi
movq 0x40(%r12), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x38(%rax), %rax
movq (%rax,%r13,8), %rax
movq %rax, (%rdi,%rcx,8)
movups %xmm0, 0x8(%rdi,%rcx,8)
movq 0x40(%r12), %rsi
callq 0x1e255
xorps %xmm0, %xmm0
incq 0x40(%r12)
incq %r13
movq 0x20(%r12), %rax
cmpq 0x40(%rax), %r13
jb 0x1dbe1
jmp 0x1dcd2
movq 0x18(%r12), %r13
decq %rax
movq %rax, 0x40(%r12)
movq 0x10(%r13), %rcx
movq %rcx, 0x20(%rsp)
movups (%r13), %xmm0
movaps %xmm0, 0x10(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x10(%r13,%rax,8), %rcx
movq %rcx, 0x10(%r13)
movups (%r13,%rax,8), %xmm0
movups %xmm0, (%r13)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%r13,%rax,8)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%r13,%rax,8)
movq 0x40(%r12), %rsi
cmpq $0x2, %rsi
jb 0x1dc89
movq %r13, %rdi
xorl %edx, %edx
callq 0x1e2b5
movq 0x40(%r12), %rax
leaq (%rax,%rax,2), %rax
leaq (,%rax,8), %rax
addq %r13, %rax
cmpq %r14, 0x8(%rsp)
jne 0x1dcaf
movsd 0x10(%rax), %xmm0
ucomisd (%rbx), %xmm0
ja 0x1dcfa
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movsd 0x10(%rax), %xmm0
movq %r15, (%rsp)
leaq 0x8(%rsp), %r9
movq %r12, %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x1d8f8
testq %rbp, %rbp
je 0x1dcea
movq 0x20(%r12), %rax
movq 0x28(%r12), %rcx
cmpq 0xe0(%rax), %rcx
jae 0x1dcff
movq 0x40(%r12), %rax
testq %rax, %rax
jne 0x1dc25
jmp 0x1dcff
incq 0x38(%r12)
movq 0x8(%rsp), %rax
subq %rax, %r14
jbe 0x1dd32
shlq $0x4, %rax
addq %rbx, %rax
addq $0x8, %rax
movabsq $0x7ff8000000000000, %rcx # imm = 0x7FF8000000000000
movq $-0x1, (%rax)
movq %rcx, -0x8(%rax)
addq $0x10, %rax
decq %r14
jne 0x1dd1e
movq 0x8(%rsp), %r15
testq %r15, %r15
je 0x1dd86
leaq -0x1(%r15), %r14
shlq $0x4, %r15
leaq (%r15,%rbx), %r12
addq $-0x10, %r12
xorl %r15d, %r15d
movups (%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rbx)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r12)
cmpq $0x2, %r14
jb 0x1dd7c
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1e180
addq $-0x10, %r12
addq $-0x1, %r14
jb 0x1dd4f
movq %r15, 0x8(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq