// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
pushq $0x68
popq %rdi
callq 0x2100
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x50(%rax)
leaq 0x50(%rax), %rbp
leaq 0x38(%rax), %rdi
movq %r14, %rsi
callq 0x2e52
leaq 0x18(%r14), %rsi
movq %rbp, %rdi
callq 0x2e52
movq %r12, %rax
xorl %edx, %edx
divq 0x60(%r14)
movq %rdx, 0x8(%rbx)
movq %r15, %rax
subq %r13, %rax
cmpq $0x1, %rax
ja 0x38a3
movq %r13, (%rbx)
imulq $0x28, %r13, %rsi
addq 0x48(%r14), %rsi
movq %rbx, %rdi
addq $0x10, %rdi
callq 0x2e52
jmp 0x399a
movq %rdx, %rcx
movq %r12, (%rsp)
leaq (%r15,%r13), %rbp
shrq %rbp
movq 0x48(%r14), %rax
imulq $0x28, %r13, %rdi
addq %rax, %rdi
imulq $0x28, %rbp, %r12
leaq (%rax,%r12), %rsi
imulq $0x28, %r15, %rdx
addq %rax, %rdx
callq 0x4335
movq 0x48(%r14), %rsi
addq %r12, %rsi
movq %rbx, %rdi
addq $0x10, %rdi
callq 0x2e52
movq 0x48(%r14), %rcx
movq 0x8(%rbx), %rax
movq (%rcx,%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
movq %rbp, (%rbx)
cmpq %r13, %rbp
movsd %xmm0, 0x10(%rsp)
jne 0x3908
movq (%rsp), %r12
jmp 0x394a
movq 0x18(%r14), %rcx
movsd (%rcx,%rax,8), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rcx,%rax,8)
movq (%rsp), %r12
leaq 0x1(%r12), %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x380e
movq %rax, 0x28(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%r14), %rcx
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq %r15, %rcx
subq %rbp, %rcx
cmpq $0x1, %rcx
jbe 0x399a
movq (%r14), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %r12
incq %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x380e
movq %rax, 0x30(%rbx)
movq 0x8(%rbx), %rax
movq (%r14), %rcx
movsd (%rsp), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq