// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq 0xe0(%rax), %rbp
andq $0x0, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
incq 0x48(%rdi)
movq %rdx, %r14
movq %rsi, %rbx
leaq 0x28(%rdi), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x40(%rdi), %r13
xorpd %xmm0, %xmm0
movupd %xmm0, 0x28(%rdi)
movupd %xmm0, 0x38(%rdi)
xorl %r12d, %r12d
cmpq 0x40(%rax), %r12
jae 0x1ac5d
movq 0x18(%r15), %rdi
imulq $0x18, 0x40(%r15), %rcx
movq 0x38(%rax), %rax
movq (%rax,%r12,8), %rax
movq %rax, (%rdi,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rdi,%rcx)
movq %r13, %rsi
callq 0x1ab2e
incq %r12
movq 0x20(%r15), %rax
jmp 0x1ac27
movq 0x38(%rsp), %r12
testq %rbp, %rbp
je 0x1ac7c
movq 0x20(%r15), %rax
movq 0x28(%r15), %rcx
cmpq 0xe0(%rax), %rcx
jae 0x1ae56
movq (%r13), %rcx
testq %rcx, %rcx
je 0x1ae56
movq 0x18(%r15), %rax
decq %rcx
movq %rcx, 0x40(%r15)
movq 0x10(%rax), %rdx
movq %rdx, 0x20(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
imulq $0x18, %rcx, %rcx
movq 0x10(%rax,%rcx), %rdx
movq %rdx, 0x10(%rax)
movups (%rax,%rcx), %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rax,%rcx)
movq 0x40(%r15), %rcx
cmpq $0x2, %rcx
jb 0x1ae12
xorl %edi, %edi
leaq 0x1(,%rdi,2), %rdx
cmpq %rcx, %rdx
jae 0x1ae12
leaq 0x2(,%rdi,2), %rsi
cmpq %rcx, %rsi
jae 0x1adc2
imulq $0x18, %rdx, %r8
movsd 0x10(%rax,%r8), %xmm0
imulq $0x18, %rsi, %r9
movsd 0x10(%rax,%r9), %xmm1
imulq $0x18, %rdi, %rdi
ucomisd %xmm0, %xmm1
movsd 0x10(%rax,%rdi), %xmm2
jbe 0x1ad74
ucomisd %xmm0, %xmm2
jbe 0x1ae12
movq 0x10(%rax,%rdi), %rsi
movq %rsi, 0x20(%rsp)
movups (%rax,%rdi), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%rax,%r8), %rsi
movq %rsi, 0x10(%rax,%rdi)
movups (%rax,%r8), %xmm0
movups %xmm0, (%rax,%rdi)
movq 0x20(%rsp), %rsi
movq %rsi, 0x10(%rax,%r8)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rax,%r8)
movq %rdx, %rdi
jmp 0x1acde
ucomisd %xmm1, %xmm2
jbe 0x1ae12
movq 0x10(%rax,%rdi), %rdx
movq %rdx, 0x20(%rsp)
movups (%rax,%rdi), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%rax,%r9), %rdx
movq %rdx, 0x10(%rax,%rdi)
movups (%rax,%r9), %xmm0
movups %xmm0, (%rax,%rdi)
movq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rax,%r9)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rax,%r9)
movq %rsi, %rdi
jmp 0x1acde
imulq $0x18, %rdi, %rsi
movsd 0x10(%rax,%rsi), %xmm0
imulq $0x18, %rdx, %rcx
ucomisd 0x10(%rax,%rcx), %xmm0
jbe 0x1ae12
movq 0x10(%rax,%rsi), %rdx
movq %rdx, 0x20(%rsp)
movups (%rax,%rsi), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%rax,%rcx), %rdx
movq %rdx, 0x10(%rax,%rsi)
movups (%rax,%rcx), %xmm0
movups %xmm0, (%rax,%rsi)
movq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rax,%rcx)
imulq $0x18, (%r13), %rcx
addq %rcx, %rax
cmpq %r14, 0x30(%rsp)
jne 0x1ae2e
movsd 0x10(%rax), %xmm0
ucomisd (%rbx), %xmm0
jbe 0x1ae33
jmp 0x1ae5d
movsd 0x10(%rax), %xmm0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, (%rsp)
movq %r15, %rdi
movq %rbx, %rcx
movq %r14, %r8
leaq 0x30(%rsp), %r9
callq 0x1a8c6
jmp 0x1ac62
movq 0x30(%rsp), %r15
jmp 0x1ae64
incq 0x38(%r15)
movq %r14, %r15
movq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rbx), %rcx
addq $0x8, %rcx
movabsq $0x7ff8000000000000, %rdx # imm = 0x7FF8000000000000
movq %r15, %rsi
cmpq %r14, %rsi
jae 0x1ae96
orq $-0x1, (%rcx)
movq %rdx, -0x8(%rcx)
incq %rsi
addq $0x10, %rcx
jmp 0x1ae80
decq %r15
leaq (%rax,%rbx), %r14
addq $-0x10, %r14
cmpq $-0x1, %r15
je 0x1aedd
movups (%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rbx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%r14)
cmpq $0x1, %r15
jbe 0x1aed4
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1b191
decq %r15
addq $-0x10, %r14
jmp 0x1aea1
movq 0x40(%rsp), %rax
movq (%rax), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq