// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movl %r9d, 0xbc(%rsp)
movq 0xd8(%rsp), %rax
movq (%rax), %rax
imulq $0x38, 0xd0(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0xc0(%rsp), %rax
subq 0xc8(%rsp), %rax
cmpq $0x1, %rax
ja 0x2c6ff
movq 0xd8(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0xbc(%rsp), %eax
jae 0x2c6c2
movl 0xbc(%rsp), %ecx
movq 0xd8(%rsp), %rax
movl %ecx, 0x20(%rax)
xorl %eax, %eax
movl %eax, %ecx
subq 0xc8(%rsp), %rcx
subq $0x1, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x8(%rax)
xorl %eax, %eax
movl %eax, %ecx
subq 0xc0(%rsp), %rcx
subq $0x1, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2cf66
movq 0xe0(%rsp), %rax
movq $0x0, 0xc8(%rax)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0xe0(%rsp), %rcx
cmpq (%rcx), %rax
jae 0x2cae3
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movq $0x400, 0x68(%rsp)      # imm = 0x400
movl $0x0, 0x64(%rsp)
movq 0xc0(%rsp), %rax
subq 0xc8(%rsp), %rax
cmpq $0x400, %rax            # imm = 0x400
ja 0x2c79b
movl $0x1, 0x64(%rsp)
movq 0xc0(%rsp), %rax
subq 0xc8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
cmpq 0x68(%rsp), %rax
jae 0x2c8f6
cmpl $0x1, 0x64(%rsp)
jne 0x2c7cf
movq 0xa8(%rsp), %rax
movl %eax, 0x60(%rsp)
jmp 0x2c7e9
movq 0xe0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x93a0
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x60(%rsp)
movl 0xc8(%rsp), %ecx
movl 0x60(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x60(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x60(%rsp), %ecx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x58(%rsp)
movq 0xe0(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x2c(%rsp)
subl $0x1, %eax
je 0x2c838
jmp 0x2c82d
movl 0x2c(%rsp), %eax
subl $0x2, %eax
je 0x2c86e
jmp 0x2c8a0
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x58(%rsp), %rcx
movq 0xe0(%rsp), %rdx
imulq (%rdx), %rcx
addq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
jmp 0x2c8a5
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x58(%rsp), %rcx
movq 0xe0(%rsp), %rdx
imulq (%rdx), %rcx
addq 0xb0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
jmp 0x2c8a5
callq 0x90b0
movsd 0x50(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
movsd 0x78(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x2c7a7
movq 0x68(%rsp), %xmm3
movaps 0x2c85d(%rip), %xmm2    # 0x59160
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movapd 0x2c861(%rip), %xmm0    # 0x59170
subpd %xmm0, %xmm3
movaps %xmm3, %xmm1
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
addsd %xmm1, %xmm3
movsd 0x80(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x80(%rsp)
movq 0x68(%rsp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm1
movsd 0x80(%rsp), %xmm0
movsd 0x80(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x70(%rsp), %xmm0
jb 0x2c9aa
jmp 0x2caca
movq 0xe0(%rsp), %rax
movq 0xc8(%rax), %rax
movq 0xe0(%rsp), %rcx
cmpq 0xd0(%rcx), %rax
jae 0x2ca47
movq 0xe0(%rsp), %rax
addq $0x50, %rax
movq 0xe0(%rsp), %rcx
imulq $0x18, 0xc8(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0xb0(%rsp), %rax
movl %eax, %ecx
movq 0x48(%rsp), %rax
movl %ecx, (%rax)
movsd 0x80(%rsp), %xmm0
movq 0x48(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x70(%rsp), %xmm0
movq 0x48(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0xe0(%rsp), %rdi
addq $0x50, %rdi
movq 0xe0(%rsp), %rsi
addq $0xc8, %rsi
callq 0x2e060
jmp 0x2cac8
movq 0xe0(%rsp), %rax
addq $0x50, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movsd 0x10(%rax), %xmm1
movsd 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2cac6
movq 0xb0(%rsp), %rax
movl %eax, %ecx
movq 0x40(%rsp), %rax
movl %ecx, (%rax)
movsd 0x80(%rsp), %xmm0
movq 0x40(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x70(%rsp), %xmm0
movq 0x40(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0xe0(%rsp), %rdi
addq $0x50, %rdi
movq 0xe0(%rsp), %rax
movq 0xc8(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2e0a0
jmp 0x2cac8
jmp 0x2caca
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x2c71e
movq 0xe0(%rsp), %rax
cmpq $0x0, 0xc8(%rax)
jne 0x2cb32
xorl %eax, %eax
movl %eax, %ecx
subq 0xc8(%rsp), %rcx
subq $0x1, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x8(%rax)
xorl %eax, %eax
movl %eax, %ecx
subq 0xc0(%rsp), %rcx
subq $0x1, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2cf66
movq 0xe0(%rsp), %rax
addq $0x50, %rax
movq %rax, 0x18(%rsp)
movq 0xe0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x93a0
movl %eax, %eax
movq %rax, 0x20(%rsp)
movq 0xe0(%rsp), %rax
movq 0xd0(%rax), %rax
movq 0xe0(%rsp), %rcx
cmpq 0xc8(%rcx), %rax
jae 0x2cb91
movq 0xe0(%rsp), %rax
movq 0xd0(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x2cba5
movq 0xe0(%rsp), %rax
movq 0xc8(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movq 0x18(%rsp), %rax
imulq $0x18, %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movl (%rax), %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0xc8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
cmpq 0xc0(%rsp), %rax
jae 0x2ccfb
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0xa8(%rsp), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x38(%rsp)
movq 0xe0(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0xc(%rsp)
subl $0x1, %eax
je 0x2cc45
jmp 0x2cc3a
movl 0xc(%rsp), %eax
subl $0x2, %eax
je 0x2cc80
jmp 0x2ccb7
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq 0xe0(%rsp), %rdx
imulq (%rdx), %rcx
movq 0x88(%rsp), %rdx
movl (%rdx), %edx
addq %rdx, %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x2ccbc
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq 0xe0(%rsp), %rdx
imulq (%rdx), %rcx
movq 0x88(%rsp), %rdx
movl (%rdx), %edx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x2ccbc
callq 0x90b0
movsd 0x30(%rsp), %xmm0
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0xa8(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x2cbed
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0xc8(%rsp), %rax
movq %rax, %rcx
shlq $0x4, %rcx
addq %rcx, %rdi
movq 0xc0(%rsp), %rsi
subq %rax, %rsi
leaq 0x13b5(%rip), %rcx      # 0x2e0e0
movl $0x10, %edx
callq 0x91b0
movq 0xe0(%rsp), %rax
movl 0x48(%rax), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x2ce39
jmp 0x2cd4e
movl 0x8(%rsp), %eax
subl $0x1, %eax
jne 0x2ce97
jmp 0x2cd5d
movq 0x88(%rsp), %rax
movsd 0x8(%rax), %xmm0
movq 0x90(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movq 0xc8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rcx
xorl %eax, %eax
cmpq 0xc0(%rsp), %rcx
movb %al, 0x7(%rsp)
jae 0x2cdd7
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x98(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq 0x90(%rsp), %rax
movsd 0x20(%rax), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x2cde1
jmp 0x2cdf9
jmp 0x2cde3
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x2cd87
movq 0x98(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x98(%rsp)
movq 0xc8(%rsp), %rax
cmpq 0x98(%rsp), %rax
ja 0x2ce37
movq 0x98(%rsp), %rax
addq $0x1, %rax
cmpq 0xc0(%rsp), %rax
jae 0x2ce37
jmp 0x2ce9c
jmp 0x2ce39
movq 0xc8(%rsp), %rax
addq 0xc0(%rsp), %rax
subq $0x1, %rax
shrq %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0xa0(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
movq 0x90(%rsp), %rax
movsd %xmm0, 0x20(%rax)
jmp 0x2ce9c
callq 0x90b0
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x2cf70
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0xc8(%rsp), %rcx
movq 0x98(%rsp), %r8
addq $0x1, %r8
movl 0xbc(%rsp), %r9d
addl $0x1, %r9d
callq 0x2c630
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x2cf70
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rdx
movq 0x98(%rsp), %rcx
addq $0x1, %rcx
movq 0xc0(%rsp), %r8
movl 0xbc(%rsp), %r9d
addl $0x1, %r9d
callq 0x2c630
addq $0xe8, %rsp
retq
nop