// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/kdtree.c
// Function: vl_kdtree_build_recursively
static void
vl_kdtree_build_recursively
(VlKDForest * forest,
 VlKDTree * tree, vl_uindex nodeIndex,
 vl_uindex dataBegin, vl_uindex dataEnd,
 unsigned int depth)
{
  vl_uindex d, i, medianIndex, splitIndex ;
  VlKDTreeNode * node = tree->nodes + nodeIndex ;
  VlKDTreeSplitDimension * splitDimension ;

  /* base case: there is only one data point */
  if (dataEnd - dataBegin <= 1) {
    if (tree->depth < depth) tree->depth = depth ;
    node->lowerChild = - dataBegin - 1;
    node->upperChild = - dataEnd - 1 ;
    return ;
  }

  /* compute the dimension with largest variance > 0 */
  forest->splitHeapNumNodes = 0 ;
  for (d = 0 ; d < forest->dimension ; ++ d) {
    double mean = 0 ; /* unnormalized */
    double secondMoment = 0 ;
    double variance = 0 ;
    vl_size numSamples = VL_KDTREE_VARIANCE_EST_NUM_SAMPLES;
    vl_bool useAllData = VL_FALSE;

    if(dataEnd - dataBegin <= VL_KDTREE_VARIANCE_EST_NUM_SAMPLES) {
      useAllData = VL_TRUE;
      numSamples = dataEnd - dataBegin;
    }

    for (i = 0; i < numSamples ; ++ i) {
      vl_uint32 sampleIndex;
      vl_index di;
      double datum ;

      if(useAllData == VL_TRUE) {
        sampleIndex = (vl_uint32)i;
      } else {
        sampleIndex = (vl_rand_uint32(forest->rand) % VL_KDTREE_VARIANCE_EST_NUM_SAMPLES);
      }
      sampleIndex += dataBegin;

      di = tree->dataIndex[sampleIndex].index ;

      switch(forest->dataType) {
        case VL_TYPE_FLOAT: datum = ((float const*)forest->data)
          [di * forest->dimension + d] ;
          break ;
        case VL_TYPE_DOUBLE: datum = ((double const*)forest->data)
          [di * forest->dimension + d] ;
          break ;
        default:
          abort() ;
      }
      mean += datum ;
      secondMoment += datum * datum ;
    }

    mean /= numSamples ;
    secondMoment /= numSamples ;
    variance = secondMoment - mean * mean ;

    if (variance <= 0) continue ;

    /* keep splitHeapSize most varying dimensions */
    if (forest->splitHeapNumNodes < forest->splitHeapSize) {
      VlKDTreeSplitDimension * splitDimension
        = forest->splitHeapArray + forest->splitHeapNumNodes ;
      splitDimension->dimension = (unsigned int)d ;
      splitDimension->mean = mean ;
      splitDimension->variance = variance ;
      vl_kdtree_split_heap_push (forest->splitHeapArray, &forest->splitHeapNumNodes) ;
    } else {
      VlKDTreeSplitDimension * splitDimension = forest->splitHeapArray + 0 ;
      if (splitDimension->variance < variance) {
        splitDimension->dimension = (unsigned int)d ;
        splitDimension->mean = mean ;
        splitDimension->variance = variance ;
        vl_kdtree_split_heap_update (forest->splitHeapArray, forest->splitHeapNumNodes, 0) ;
      }
    }
  }

  /* additional base case: the maximum variance is equal to 0 (overlapping points) */
  if (forest->splitHeapNumNodes == 0) {
    node->lowerChild = - dataBegin - 1 ;
    node->upperChild = - dataEnd - 1 ;
    return ;
  }

  /* toss a dice to decide the splitting dimension (variance > 0) */
  splitDimension = forest->splitHeapArray
  + (vl_rand_uint32(forest->rand) % VL_MIN(forest->splitHeapSize, forest->splitHeapNumNodes)) ;

  node->splitDimension = splitDimension->dimension ;

  /* sort data along largest variance dimension */
  for (i = dataBegin ; i < dataEnd ; ++ i) {
    vl_index di = tree->dataIndex[i].index ;
    double datum ;
    switch (forest->dataType) {
      case VL_TYPE_FLOAT: datum = ((float const*)forest->data)
        [di * forest->dimension + splitDimension->dimension] ;
        break ;
      case VL_TYPE_DOUBLE: datum = ((double const*)forest->data)
        [di * forest->dimension + splitDimension->dimension] ;
        break ;
      default:
        abort() ;
    }
    tree->dataIndex [i] .value = datum ;
  }
  qsort (tree->dataIndex + dataBegin,
         dataEnd - dataBegin,
         sizeof (VlKDTreeDataIndexEntry),
         vl_kdtree_compare_index_entries) ;

  /* determine split threshold */
  switch (forest->thresholdingMethod) {
    case VL_KDTREE_MEAN :
      node->splitThreshold = splitDimension->mean ;
      for (splitIndex = dataBegin ;
           splitIndex < dataEnd && tree->dataIndex[splitIndex].value <= node->splitThreshold ;
           ++ splitIndex) ;
      splitIndex -= 1 ;
      /* If the mean does not provide a proper partition, fall back to
       * median. This usually happens if all points have the same
       * value and the zero variance test fails for numerical accuracy
       * reasons. In this case, also due to numerical accuracy, the
       * mean value can be smaller, equal, or larger than all
       * points. */
      if (dataBegin <= splitIndex && splitIndex + 1 < dataEnd) break ;

    case VL_KDTREE_MEDIAN :
      medianIndex = (dataBegin + dataEnd - 1) / 2 ;
      splitIndex = medianIndex ;
      node -> splitThreshold = tree->dataIndex[medianIndex].value ;
      break ;

    default:
      abort() ;
  }

  /* divide subparts */
  node->lowerChild = vl_kdtree_node_new (tree, nodeIndex) ;
  vl_kdtree_build_recursively (forest, tree, node->lowerChild, dataBegin, splitIndex + 1, depth + 1) ;

  node->upperChild = vl_kdtree_node_new (tree, nodeIndex) ;
  vl_kdtree_build_recursively (forest, tree, node->upperChild, splitIndex + 1, dataEnd, depth + 1) ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %r14d
movq %r8, %rbp
movq %rcx, %r13
movq %rsi, %r8
movq %rdi, %r15
leaq 0x50(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq -0x1(%rbp), %rax
movq %rax, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
movq %rbp, 0x40(%rsp)
imulq $0x38, %rdx, %rax
addq (%r8), %rax
movq %rax, (%rsp)
movq %rbp, %rbx
subq %r13, %rbx
cmpq $0x1, %rbx
jbe 0x1a7dc
movq %rdx, 0x48(%rsp)
movl %r14d, 0x2c(%rsp)
andq $0x0, 0xc8(%r15)
cmpq $0x401, %rbx            # imm = 0x401
movl $0x400, %ebp            # imm = 0x400
cmovbq %rbx, %rbp
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %r14
cmpq (%r15), %r12
jae 0x1a5f9
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x1a419
cmpq $0x400, %rbx            # imm = 0x400
ja 0x1a393
movl %r14d, %eax
jmp 0x1a3be
movq 0x8(%r15), %rdi
movsd %xmm1, 0x60(%rsp)
movsd %xmm2, 0x58(%rsp)
callq 0x9360
movsd 0x58(%rsp), %xmm2
movsd 0x60(%rsp), %xmm1
movq 0x8(%rsp), %r8
andl $0x3ff, %eax            # imm = 0x3FF
addl %r13d, %eax
movq 0x18(%r8), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rax
movl 0x10(%r15), %ecx
cmpl $0x2, %ecx
je 0x1a3f3
cmpl $0x1, %ecx
jne 0x1a807
imulq (%r15), %rax
shlq $0x2, %rax
addq 0x18(%r15), %rax
cvtss2sd (%rax,%r12,4), %xmm0
jmp 0x1a405
imulq (%r15), %rax
shlq $0x3, %rax
addq 0x18(%r15), %rax
movsd (%rax,%r12,8), %xmm0
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm2
incq %r14
jmp 0x1a37c
movsd 0x50(%rsp), %xmm0
divsd %xmm0, %xmm1
divsd %xmm0, %xmm2
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
subsd %xmm0, %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
movq 0x30(%rsp), %r14
jae 0x1a5f1
movq 0xc8(%r15), %rsi
cmpq 0xd0(%r15), %rsi
jae 0x1a485
imulq $0x18, %rsi, %rax
movl %r12d, (%r14,%rax)
movsd %xmm1, 0x8(%r14,%rax)
movsd %xmm2, 0x10(%r14,%rax)
movq %r14, %rdi
callq 0x1b0ec
movq 0x8(%rsp), %r8
incq 0xc8(%r15)
jmp 0x1a5f1
ucomisd 0x60(%r15), %xmm2
jbe 0x1a5f1
movl %r12d, 0x50(%r15)
movsd %xmm1, 0x58(%r15)
movsd %xmm2, 0x60(%r15)
xorl %edx, %edx
leaq 0x1(,%rdx,2), %rax
cmpq %rsi, %rax
jae 0x1a5e2
leaq 0x2(,%rdx,2), %rcx
cmpq %rsi, %rcx
jae 0x1a58c
imulq $0x18, %rax, %rdi
movsd 0x10(%r14,%rdi), %xmm0
imulq $0x18, %rcx, %r8
movsd 0x10(%r14,%r8), %xmm1
imulq $0x18, %rdx, %rdx
ucomisd %xmm0, %xmm1
movsd 0x10(%r14,%rdx), %xmm2
jbe 0x1a53c
ucomisd %xmm0, %xmm2
jbe 0x1a5e2
movq 0x10(%r14,%rdx), %rcx
movq %rcx, 0x20(%rsp)
movups (%r14,%rdx), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%r14,%rdi), %rcx
movq %rcx, 0x10(%r14,%rdx)
movups (%r14,%rdi), %xmm0
movups %xmm0, (%r14,%rdx)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%r14,%rdi)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%r14,%rdi)
movq %rax, %rdx
jmp 0x1a4a3
ucomisd %xmm1, %xmm2
jbe 0x1a5e2
movq 0x10(%r14,%rdx), %rax
movq %rax, 0x20(%rsp)
movups (%r14,%rdx), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%r14,%r8), %rax
movq %rax, 0x10(%r14,%rdx)
movups (%r14,%r8), %xmm0
movups %xmm0, (%r14,%rdx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r14,%r8)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%r14,%r8)
movq %rcx, %rdx
jmp 0x1a4a3
imulq $0x18, %rdx, %rcx
movsd 0x10(%r14,%rcx), %xmm0
imulq $0x18, %rax, %rax
ucomisd 0x10(%r14,%rax), %xmm0
jbe 0x1a5e2
movq 0x10(%r14,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movups (%r14,%rcx), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x10(%r14,%rax), %rdx
movq %rdx, 0x10(%r14,%rcx)
movups (%r14,%rax), %xmm0
movups %xmm0, (%r14,%rcx)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%r14,%rax)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%r14,%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x1b0ec
movq 0x8(%rsp), %r8
incq %r12
jmp 0x1a368
cmpq $0x0, 0xc8(%r15)
movq 0x40(%rsp), %rbp
je 0x1a7e6
movq 0x8(%r15), %rdi
callq 0x9360
movl %eax, %eax
movq 0xc8(%r15), %rcx
movq 0xd0(%r15), %rdx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
xorl %edx, %edx
divq %rcx
imulq $0x18, %rdx, %rax
movq %r14, %rcx
addq %rax, %r14
movl (%rcx,%rax), %eax
movq (%rsp), %rcx
movl %eax, 0x18(%rcx)
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rdi
movq %r13, %r12
shlq $0x4, %r12
leaq (%rdi,%r12), %rcx
addq $0x8, %rcx
movq %r13, %rdx
cmpq %rbp, %rdx
jae 0x1a6ae
movq -0x8(%rcx), %rsi
movl 0x10(%r15), %r8d
cmpl $0x2, %r8d
je 0x1a690
cmpl $0x1, %r8d
jne 0x1a807
imulq (%r15), %rsi
shlq $0x2, %rsi
addq 0x18(%r15), %rsi
cvtss2sd (%rsi,%rax,4), %xmm0
jmp 0x1a6a1
imulq (%r15), %rsi
shlq $0x3, %rsi
addq 0x18(%r15), %rsi
movsd (%rsi,%rax,8), %xmm0
movsd %xmm0, (%rcx)
incq %rdx
addq $0x10, %rcx
jmp 0x1a660
addq %r12, %rdi
movq %rbx, %rsi
pushq $0x10
popq %rdx
leaq 0xa0c(%rip), %rcx       # 0x1b0ca
callq 0x9190
movq 0x8(%rsp), %rsi
movl 0x48(%r15), %eax
testl %eax, %eax
je 0x1a71d
cmpl $0x1, %eax
jne 0x1a807
movsd 0x8(%r14), %xmm0
movq (%rsp), %rax
movsd %xmm0, 0x20(%rax)
addq $0x8, %r12
movq %r13, %rbx
cmpq %rbp, %rbx
jae 0x1a71d
movq 0x18(%rsi), %rax
ucomisd (%rax,%r12), %xmm0
jb 0x1a709
incq %rbx
addq $0x10, %r12
jmp 0x1a6ef
cmpq %rbp, %rbx
setb %al
leaq -0x1(%rbx), %rcx
cmpq %r13, %rcx
setae %cl
testb %cl, %al
jne 0x1a746
movq 0x38(%rsp), %rax
leaq (%rax,%r13), %rbx
shrq %rbx
movq 0x18(%rsi), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movq (%rsp), %rax
movsd %xmm0, 0x20(%rax)
incq %rbx
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%rsi)
imulq $0x38, %rdx, %rcx
movq 0x48(%rsp), %r12
movq %r12, (%rax,%rcx)
andq $0x0, 0x20(%rax,%rcx)
andl $0x0, 0x18(%rax,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax,%rcx)
movq (%rsp), %rax
movq %rdx, 0x8(%rax)
movl 0x2c(%rsp), %r14d
incl %r14d
movq %r15, %rdi
movq %r13, %rcx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x1a2df
movq 0x8(%rsp), %r8
movq (%r8), %rax
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
imulq $0x38, %rcx, %rdx
movq %r12, (%rax,%rdx)
andq $0x0, 0x20(%rax,%rdx)
andl $0x0, 0x18(%rax,%rdx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax,%rdx)
movq (%rsp), %rax
movq %rcx, 0x10(%rax)
movq %rcx, %rdx
movq %rbx, %r13
jmp 0x1a318
cmpl %r14d, 0x20(%r8)
jae 0x1a7e6
movl %r14d, 0x20(%r8)
notq %r13
movq (%rsp), %rax
movq %r13, 0x8(%rax)
notq %rbp
movq %rbp, 0x10(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0