// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movl %r9d, 0x14(%rsp)
movq %rcx, %r13
imulq $0x38, %rdx, %rax
addq (%rsi), %rax
movq %r8, %rbx
subq %rcx, %rbx
cmpq $0x2, %rbx
jae 0x1d29e
movq %r13, %r12
jmp 0x1d817
movq %rdi, %r15
movq %rdx, 0x48(%rsp)
movq %rax, 0x8(%rsp)
leaq 0x50(%rdi), %rbp
leaq -0x1(%r8), %rax
movq %rax, 0x50(%rsp)
movq %rsi, 0x18(%rsp)
movq %r8, 0x40(%rsp)
movq %rbp, 0x58(%rsp)
movq $0x0, 0xc8(%r15)
cmpq $0x0, (%r15)
je 0x1d825
cmpq $0x401, %rbx            # imm = 0x401
movl $0x400, %r12d           # imm = 0x400
cmovbq %rbx, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movsd %xmm0, 0x60(%rsp)
xorl %r14d, %r14d
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
xorpd %xmm2, %xmm2
cmpq $0x400, %rbx            # imm = 0x400
ja 0x1d315
movl %ebp, %eax
jmp 0x1d340
movq 0x8(%r15), %rdi
movsd %xmm1, 0x70(%rsp)
movsd %xmm2, 0x68(%rsp)
callq 0x9340
movsd 0x68(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
movq 0x18(%rsp), %rsi
andl $0x3ff, %eax            # imm = 0x3FF
addl %r13d, %eax
movq 0x18(%rsi), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rax
movl 0x10(%r15), %ecx
cmpl $0x2, %ecx
je 0x1d378
cmpl $0x1, %ecx
jne 0x1d859
imulq (%r15), %rax
shlq $0x2, %rax
addq 0x18(%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r14,4), %xmm0
jmp 0x1d38a
imulq (%r15), %rax
shlq $0x3, %rax
addq 0x18(%r15), %rax
movsd (%rax,%r14,8), %xmm0
addsd %xmm0, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
incq %rbp
cmpq %rbp, %r12
jne 0x1d308
movsd 0x60(%rsp), %xmm0
divsd %xmm0, %xmm2
divsd %xmm0, %xmm1
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
movq 0x58(%rsp), %rbp
jae 0x1d597
movq 0xc8(%r15), %rax
cmpq 0xd0(%r15), %rax
jae 0x1d477
leaq (%rax,%rax,2), %rcx
movl %r14d, (%rbp,%rcx,8)
movsd %xmm2, 0x8(%rbp,%rcx,8)
movsd %xmm1, 0x10(%rbp,%rcx,8)
testq %rax, %rax
je 0x1d58b
leaq -0x1(%rax), %rcx
leaq (%rax,%rax,2), %r8
movq %rcx, %rax
shrq %rax
leaq (%rax,%rax,2), %rdx
movsd 0x10(%rbp,%rdx,8), %xmm0
ucomisd 0x10(%rbp,%r8,8), %xmm0
jbe 0x1d468
movq 0x10(%rbp,%r8,8), %rdi
movq %rdi, 0x30(%rsp)
movups (%rbp,%r8,8), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x10(%rbp,%rdx,8), %rdi
movq %rdi, 0x10(%rbp,%r8,8)
movups (%rbp,%rdx,8), %xmm0
movups %xmm0, (%rbp,%r8,8)
movq 0x30(%rsp), %rdi
movq %rdi, 0x10(%rbp,%rdx,8)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rbp,%rdx,8)
cmpq $0x1, %rcx
ja 0x1d401
movq 0xc8(%r15), %rax
incq %rax
jmp 0x1d590
ucomisd 0x60(%r15), %xmm1
jbe 0x1d597
movl %r14d, 0x50(%r15)
movsd %xmm2, 0x58(%r15)
movsd %xmm1, 0x60(%r15)
cmpq $0x2, %rax
jb 0x1d597
movl $0x1, %ecx
xorl %edx, %edx
xorl %r9d, %r9d
addq $0x2, %rdx
cmpq %rax, %rdx
jae 0x1d5a5
leaq (%rcx,%rcx,2), %rdi
movsd 0x10(%rbp,%rdi,8), %xmm0
leaq (%rdx,%rdx,2), %r8
movsd 0x10(%rbp,%r8,8), %xmm1
ucomisd %xmm0, %xmm1
leaq (%r9,%r9,2), %r9
movsd 0x10(%rbp,%r9,8), %xmm2
jbe 0x1d526
ucomisd %xmm0, %xmm2
jbe 0x1d597
movq 0x10(%rbp,%r9,8), %rdx
movq %rdx, 0x30(%rsp)
movups (%rbp,%r9,8), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x10(%rbp,%rdi,8), %rdx
movq %rdx, 0x10(%rbp,%r9,8)
movups (%rbp,%rdi,8), %xmm0
movups %xmm0, (%rbp,%r9,8)
movq 0x30(%rsp), %rdx
movq %rdx, 0x10(%rbp,%rdi,8)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rbp,%rdi,8)
jmp 0x1d571
ucomisd %xmm1, %xmm2
jbe 0x1d597
movq 0x10(%rbp,%r9,8), %rcx
movq %rcx, 0x30(%rsp)
movups (%rbp,%r9,8), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x10(%rbp,%r8,8), %rcx
movq %rcx, 0x10(%rbp,%r9,8)
movups (%rbp,%r8,8), %xmm0
movups %xmm0, (%rbp,%r9,8)
movq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rbp,%r8,8)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rbp,%r8,8)
movq %rdx, %rcx
movq %rcx, %r9
leaq (%rcx,%rcx), %rdx
leaq 0x1(,%rcx,2), %rcx
cmpq %rax, %rcx
jb 0x1d4a7
jmp 0x1d597
movl $0x1, %eax
movq %rax, 0xc8(%r15)
incq %r14
cmpq (%r15), %r14
jb 0x1d2fe
jmp 0x1d5fb
leaq (%r9,%r9,2), %rdx
movsd 0x10(%rbp,%rdx,8), %xmm0
leaq (%rcx,%rcx,2), %rax
ucomisd 0x10(%rbp,%rax,8), %xmm0
jbe 0x1d597
movq 0x10(%rbp,%rdx,8), %rcx
movq %rcx, 0x30(%rsp)
movups (%rbp,%rdx,8), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x10(%rbp,%rax,8), %rcx
movq %rcx, 0x10(%rbp,%rdx,8)
movups (%rbp,%rax,8), %xmm0
movups %xmm0, (%rbp,%rdx,8)
movq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rbp,%rax,8)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rbp,%rax,8)
jmp 0x1d597
cmpq $0x0, 0xc8(%r15)
je 0x1d82f
movq 0x8(%r15), %rdi
callq 0x9340
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), %r14
movl (%rbp,%r14,8), %r8d
movq 0x8(%rsp), %rax
movl %r8d, 0x18(%rax)
movq 0x40(%rsp), %rax
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %rdi
subq %r13, %rax
jbe 0x1d6ac
movl 0x10(%r15), %ecx
leal -0x1(%rcx), %edx
cmpl $0x1, %edx
ja 0x1d859
movq (%r15), %rdx
movq 0x18(%r15), %r9
leaq (%r9,%r8,8), %rsi
leaq (%r9,%r8,4), %r8
movq %r13, %r9
shlq $0x4, %r9
addq %rdi, %r9
addq $0x8, %r9
movq -0x8(%r9), %r10
imulq %rdx, %r10
cmpl $0x1, %ecx
jne 0x1d698
xorps %xmm0, %xmm0
cvtss2sd (%r8,%r10,4), %xmm0
jmp 0x1d69e
movsd (%rsi,%r10,8), %xmm0
movsd %xmm0, (%r9)
addq $0x10, %r9
decq %rax
jne 0x1d680
movq %r13, %r12
shlq $0x4, %r12
addq %r12, %rdi
movl $0x10, %edx
movq %rbx, %rsi
leaq 0xcb1(%rip), %rcx       # 0x1e376
callq 0x9190
movl 0x48(%r15), %eax
cmpl $0x1, %eax
je 0x1d6eb
testl %eax, %eax
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdi
jne 0x1d859
movq 0x18(%rsi), %rax
jmp 0x1d741
leaq (,%r14,8), %rax
addq %rbp, %rax
movsd 0x8(%rax), %xmm0
movq 0x8(%rsp), %rdi
movsd %xmm0, 0x20(%rdi)
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %rax
movq 0x40(%rsp), %rdx
cmpq %r13, %rdx
jbe 0x1d741
leaq (%rax,%r12), %rcx
addq $0x8, %rcx
movq %r13, %r12
ucomisd (%rcx), %xmm0
jb 0x1d737
incq %r12
addq $0x10, %rcx
cmpq %r12, %rdx
jne 0x1d723
jmp 0x1d741
leaq -0x1(%r12), %rcx
cmpq %r13, %rcx
jae 0x1d762
movq 0x50(%rsp), %rcx
leaq (%rcx,%r13), %r12
shrq %r12
movq %r12, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, 0x20(%rdi)
incq %r12
movq %rdi, 0x8(%rsp)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%rsi)
imulq $0x38, %rdx, %rcx
movq 0x48(%rsp), %rbx
movq %rbx, (%rax,%rcx)
xorl %r14d, %r14d
movq %r14, 0x20(%rax,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax,%rcx)
movl %r14d, 0x18(%rax,%rcx)
movq %rdx, 0x8(%rdi)
movl 0x14(%rsp), %r9d
incl %r9d
movq %r15, %rdi
movq %r13, %rcx
movq %r12, %r8
movl %r9d, 0x14(%rsp)
callq 0x1d26d
movq 0x18(%rsp), %rsi
movq (%rsi), %rcx
movq 0x8(%rsi), %rdx
leaq 0x1(%rdx), %rax
movq %rax, 0x8(%rsi)
imulq $0x38, %rdx, %rax
movq %rbx, (%rcx,%rax)
movq %r14, 0x20(%rcx,%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rcx,%rax)
movl %r14d, 0x18(%rcx,%rax)
movq 0x8(%rsp), %rcx
movq %rdx, 0x10(%rcx)
addq (%rsi), %rax
movq 0x40(%rsp), %r8
movq %r8, %rbx
subq %r12, %rbx
movq %rax, 0x8(%rsp)
movq %r12, %r13
movq %rdx, 0x48(%rsp)
cmpq $0x2, %rbx
jae 0x1d2c7
movl 0x14(%rsp), %ecx
cmpl %ecx, 0x20(%rsi)
jae 0x1d83c
movl %ecx, 0x20(%rsi)
jmp 0x1d83c
movq %r13, %r12
movq 0x8(%rsp), %rax
jmp 0x1d83c
movq %r13, %r12
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %r8
notq %r12
movq %r12, 0x8(%rax)
notq %r8
movq %r8, 0x10(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0