// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %r9d, %r14d
movq %rcx, %r12
movq %rsi, %r9
movq %rdi, %r15
movq %rsi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
imulq $0x38, %rdx, %rax
addq (%r9), %rax
movq %rax, (%rsp)
movq %r8, %rbx
subq %r12, %rbx
cmpq $0x1, %rbx
jbe 0x1d7f5
movl %r14d, 0x1c(%rsp)
movq $0x0, 0xc8(%r15)
cmpq $0x0, (%r15)
je 0x1d5da
cmpq $0x401, %rbx            # imm = 0x401
movl $0x400, %ebp            # imm = 0x400
cmovbq %rbx, %rbp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x50(%r15), %rax
movq %rax, 0x20(%rsp)
cmpq $0x1, %rbp
adcq $0x0, %rbp
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
xorpd %xmm2, %xmm2
cmpq $0x400, %rbx            # imm = 0x400
ja 0x1d4b8
movl %r14d, %eax
jmp 0x1d4e3
movq 0x8(%r15), %rdi
movsd %xmm1, 0x40(%rsp)
movsd %xmm2, 0x38(%rsp)
callq 0x9340
movsd 0x38(%rsp), %xmm2
movsd 0x40(%rsp), %xmm1
movq 0x8(%rsp), %r9
andl $0x3ff, %eax            # imm = 0x3FF
addl %r12d, %eax
movq 0x18(%r9), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rax
movl 0x10(%r15), %ecx
cmpl $0x2, %ecx
je 0x1d51b
cmpl $0x1, %ecx
jne 0x1d839
imulq (%r15), %rax
shlq $0x2, %rax
addq 0x18(%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
jmp 0x1d52d
imulq (%r15), %rax
shlq $0x3, %rax
addq 0x18(%r15), %rax
movsd (%rax,%r13,8), %xmm0
addsd %xmm0, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
incq %r14
cmpq %r14, %rbp
jne 0x1d4aa
movsd 0x30(%rsp), %xmm0
divsd %xmm0, %xmm2
divsd %xmm0, %xmm1
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x1d5ce
movq 0xc8(%r15), %rsi
cmpq 0xd0(%r15), %rsi
jae 0x1d5a5
leaq (%rsi,%rsi,2), %rax
movq 0x20(%rsp), %rdi
movl %r13d, (%rdi,%rax,8)
movsd %xmm2, 0x8(%rdi,%rax,8)
movsd %xmm1, 0x10(%rdi,%rax,8)
callq 0x1dfb1
movq 0x8(%rsp), %r9
incq 0xc8(%r15)
jmp 0x1d5ce
ucomisd 0x60(%r15), %xmm1
jbe 0x1d5ce
movl %r13d, 0x50(%r15)
movsd %xmm2, 0x58(%r15)
movsd %xmm1, 0x60(%r15)
movq 0x20(%rsp), %rdi
xorl %edx, %edx
callq 0x1e011
movq 0x8(%rsp), %r9
incq %r13
cmpq (%r15), %r13
jb 0x1d49f
cmpq $0x0, 0xc8(%r15)
je 0x1d813
movq 0x8(%r15), %rdi
callq 0x9340
movq 0x8(%rsp), %r9
movl %eax, %eax
movq 0xc8(%r15), %rcx
movq 0xd0(%r15), %rdx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
xorl %edx, %edx
divq %rcx
leaq (%rdx,%rdx,2), %r13
movl 0x50(%r15,%r13,8), %eax
movq (%rsp), %rcx
movl %eax, 0x18(%rcx)
movq 0x10(%rsp), %rbp
cmpq %r12, %rbp
jbe 0x1d68f
movl 0x10(%r15), %ecx
leal -0x1(%rcx), %edx
cmpl $0x1, %edx
ja 0x1d839
movq 0x18(%r9), %rsi
movq (%r15), %rdx
movq %r12, %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
addq $0x8, %rsi
movq %rbx, %rdi
movq -0x8(%rsi), %r8
imulq %rdx, %r8
cmpl $0x1, %ecx
jne 0x1d674
shlq $0x2, %r8
addq 0x18(%r15), %r8
xorps %xmm0, %xmm0
cvtss2sd (%r8,%rax,4), %xmm0
jmp 0x1d682
shlq $0x3, %r8
addq 0x18(%r15), %r8
movsd (%r8,%rax,8), %xmm0
movsd %xmm0, (%rsi)
addq $0x10, %rsi
decq %rdi
jne 0x1d654
movq %r12, %r14
shlq $0x4, %r14
movq 0x18(%r9), %rdi
addq %r14, %rdi
movl $0x10, %edx
movq %rbx, %rsi
leaq 0x8e1(%rip), %rcx       # 0x1df8d
callq 0x9190
movq 0x8(%rsp), %rsi
movl 0x48(%r15), %eax
testl %eax, %eax
je 0x1d737
cmpl $0x1, %eax
jne 0x1d839
leaq (%r15,%r13,8), %rax
addq $0x50, %rax
movsd 0x8(%rax), %xmm0
movq (%rsp), %rax
movsd %xmm0, 0x20(%rax)
cmpq %r12, %rbp
setbe %al
ja 0x1d6ea
movq %r12, %rbx
jmp 0x1d72a
movq 0x18(%rsi), %rcx
ucomisd 0x8(%rcx,%r14), %xmm0
movq %r12, %rbx
jb 0x1d72a
leaq 0x1(%r12), %rbx
addq %r14, %rcx
addq $0x18, %rcx
movq %rbx, %rax
cmpq %rbx, %rbp
je 0x1d721
ucomisd (%rcx), %xmm0
leaq 0x1(%rax), %rbx
leaq 0x10(%rcx), %rcx
jae 0x1d706
decq %rbx
jmp 0x1d724
movq %rbp, %rbx
cmpq %rbp, %rax
setae %al
testb %al, %al
jne 0x1d737
leaq -0x1(%rbx), %rax
cmpq %r12, %rax
jae 0x1d75e
leaq (%r12,%rbp), %rbx
decq %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 0x28(%rsp), %r13
movq %r13, (%rax,%rcx)
xorl %edi, %edi
movq %rdi, 0x20(%rax,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax,%rcx)
movl %edi, 0x18(%rax,%rcx)
movq (%rsp), %rbp
movq %rdx, 0x8(%rbp)
movl 0x1c(%rsp), %r14d
incl %r14d
movq %r15, %rdi
movq %r12, %rcx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x1d40f
movq 0x8(%rsp), %r9
movq (%r9), %rax
movq 0x8(%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r9)
imulq $0x38, %rcx, %rdx
movq %r13, (%rax,%rdx)
xorl %esi, %esi
movq %rsi, 0x20(%rax,%rdx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax,%rdx)
movl %esi, 0x18(%rax,%rdx)
movq %rcx, 0x10(%rbp)
movq 0x10(%rsp), %r8
movq %rcx, %rdx
movq %rbx, %r12
jmp 0x1d433
cmpl %r14d, 0x20(%r9)
jae 0x1d7ff
movl %r14d, 0x20(%r9)
notq %r12
movq (%rsp), %rax
movq %r12, 0x8(%rax)
notq %r8
movq %r8, 0x10(%rax)
jmp 0x1d82a
notq %r12
movq (%rsp), %rcx
movq %r12, 0x8(%rcx)
movq 0x10(%rsp), %rax
notq %rax
movq %rax, 0x10(%rcx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0