// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x68, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
callq 0x86b0
jmp 0x6eb4
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
addq $0x38, %rdi
callq 0x3d70
movq -0x98(%rbp), %rsi
addq $0x18, %rsi
movq -0x40(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d70
movq -0x98(%rbp), %rcx
movq -0x10(%rbp), %rax
xorl %edx, %edx
divq 0x60(%rcx)
movq -0x40(%rbp), %rax
movq %rdx, 0x8(%rax)
movq -0x20(%rbp), %rax
subq -0x18(%rbp), %rax
cmpq $0x1, %rax
ja 0x6f68
movq -0x98(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
addq $0x48, %rdi
movq -0x18(%rbp), %rsi
callq 0x3b30
movq %rax, %rsi
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x3d70
jmp 0x71e0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x68, %esi
callq 0x2120
jmp 0x71ed
movq -0x98(%rbp), %rdi
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
shrq %rax
movq %rax, -0x28(%rbp)
addq $0x48, %rdi
callq 0x5900
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x87c0
movq -0x98(%rbp), %rdi
movq %rax, -0x58(%rbp)
addq $0x48, %rdi
callq 0x5900
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x87c0
movq -0x98(%rbp), %rdi
movq %rax, -0x68(%rbp)
addq $0x48, %rdi
callq 0x5900
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x87c0
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8800
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x8710
movq -0x98(%rbp), %rdi
addq $0x48, %rdi
movq -0x28(%rbp), %rsi
callq 0x3b30
movq %rax, %rsi
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x3d70
movq -0x98(%rbp), %rdi
addq $0x48, %rdi
movq -0x28(%rbp), %rsi
callq 0x3b30
movq %rax, %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
subq -0x18(%rbp), %rax
cmpq $0x0, %rax
jbe 0x712a
movq -0x98(%rbp), %rdi
addq $0x18, %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movq -0x98(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
addq $0x18, %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0xa8(%rbp), %xmm0
movq -0x98(%rbp), %rdi
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rsi
addq $0x1, %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x6e70
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
addq $0x18, %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
cmpq $0x1, %rax
jbe 0x71de
movq -0x98(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movq -0x98(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0xb8(%rbp), %xmm0
movq -0x98(%rbp), %rdi
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rsi
addq $0x1, %rsi
movq -0x28(%rbp), %rdx
addq $0x1, %rdx
movq -0x20(%rbp), %rcx
callq 0x6e70
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x38e0
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x71e0
movq -0x40(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2190
nopw %cs:(%rax,%rax)