// File: /workspace/github_cpp_makefile_star2_O1/cdalitz[P]kdtree-cpp/kdtree.cpp
// Function: Kdtree::KdTree::build_tree(unsigned long, unsigned long, unsigned long)
kdtree_node* KdTree::build_tree(size_t depth, size_t a, size_t b) {
  size_t m;
  double temp, cutval;
  kdtree_node* node = new kdtree_node();
  node->lobound = lobound;
  node->upbound = upbound;
  node->cutdim = depth % dimension;
  if (b - a <= 1) {
    node->dataindex = a;
    node->point = allnodes[a].point;
  } else {
    m = (a + b) / 2;
    std::nth_element(allnodes.begin() + a, allnodes.begin() + m,
                     allnodes.begin() + b, compare_dimension(node->cutdim));
    node->point = allnodes[m].point;
    cutval = allnodes[m].point[node->cutdim];
    node->dataindex = m;
    if (m - a > 0) {
      temp = upbound[node->cutdim];
      upbound[node->cutdim] = cutval;
      node->loson = build_tree(depth + 1, a, m);
      upbound[node->cutdim] = temp;
    }
    if (b - m > 1) {
      temp = lobound[node->cutdim];
      lobound[node->cutdim] = cutval;
      node->hison = build_tree(depth + 1, m + 1, b);
      lobound[node->cutdim] = temp;
    }
  }
  return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
movl $0x68, %edi
callq 0x2110
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x50(%rax)
movq $0x0, 0x60(%rax)
leaq 0x50(%rax), %rbp
leaq 0x38(%rax), %rdi
movq %r14, %rsi
callq 0x339e
leaq 0x18(%r14), %rsi
movq %rbp, %rdi
callq 0x339e
movq %r12, %rax
xorl %edx, %edx
divq 0x60(%r14)
movq %rdx, 0x8(%rbx)
movq %r15, %rax
subq %r13, %rax
cmpq $0x1, %rax
ja 0x3c65
movq %r13, (%rbx)
leaq (,%r13,4), %rsi
addq %r13, %rsi
shlq $0x3, %rsi
addq 0x48(%r14), %rsi
movq %rbx, %rdi
addq $0x10, %rdi
callq 0x339e
jmp 0x3db2
movq %r12, 0x20(%rsp)
leaq (%r15,%r13), %rbp
shrq %rbp
cmpq %r13, %r15
sete %al
movq %r15, %rcx
subq %rbp, %rcx
movq %rcx, 0x8(%rsp)
sete %cl
orb %al, %cl
jne 0x3cdf
movq %rdx, %r8
movq 0x48(%r14), %rdi
leaq (,%r15,8), %rax
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax), %rdx
leaq (%rbp,%rbp,4), %rcx
leaq (%rdi,%rcx,8), %rsi
leaq (,%r13,8), %rcx
leaq (%rcx,%rcx,4), %rcx
addq %rcx, %rdi
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
bsrq %rcx, %rcx
xorl $0x3f, %ecx
addl %ecx, %ecx
xorq $0x7e, %rcx
callq 0x4fe1
leaq (,%rbp,8), %rax
leaq (%rax,%rax,4), %r12
movq 0x48(%r14), %rsi
addq %r12, %rsi
movq %rbx, %rdi
addq $0x10, %rdi
callq 0x339e
movq 0x48(%r14), %rcx
movq 0x8(%rbx), %rax
movq (%rcx,%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
movq %rbp, (%rbx)
cmpq %r13, %rbp
movq 0x20(%rsp), %r12
je 0x3d66
movq 0x18(%r14), %rcx
movsd (%rcx,%rax,8), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, (%rcx,%rax,8)
leaq 0x1(%r12), %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movsd %xmm0, 0x18(%rsp)
callq 0x3bc0
movsd 0x18(%rsp), %xmm0
movq %rax, 0x28(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%r14), %rcx
movsd 0x10(%rsp), %xmm1
movsd %xmm1, (%rcx,%rax,8)
cmpq $0x1, 0x8(%rsp)
jbe 0x3db2
movq 0x8(%rbx), %rax
movq (%r14), %rcx
movsd (%rcx,%rax,8), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rcx,%rax,8)
incq %r12
incq %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x3bc0
movq %rax, 0x30(%rbx)
movq 0x8(%rbx), %rax
movq (%r14), %rcx
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq