// File: cfiutak1[P]HPyC-KD-Tree/./utils/filewriter/../../src/kdtree/KDTree.hpp
// Function: hpyc::KDTree<float>::buildTree(unsigned long, unsigned long, unsigned int)
void KDTree<ItemT>::buildTree(const std::size_t subarray_begin, const std::size_t subarray_end, const unsigned int depth) {
    std::size_t range = subarray_end - subarray_begin;

    // Base case - If there is one element left, it is already sorted and thus in its correct position.
    if (range == 1) { return; }

    // Base case - If there are two elements left, they will be in their correct positions if they are sorted.
    else if (range == 2) {
        if (this->nodes[depth][subarray_begin] > this->nodes[depth][subarray_begin + 1]) {
            this->swap(subarray_begin, subarray_begin + 1);
        }

        return;
    }

    else if (range == 3) {
        AdaptiveCacheAwareBlockquickselect<ItemT> qs(this->nodes, this->num_dimensions, depth);
        qs.sort3(subarray_begin);

        return;
    }

    // Partition the current subarray around the median at the current dimension.
    AdaptiveCacheAwareBlockquickselect<ItemT> qs(this->nodes, this->num_dimensions, depth);
    qs.nth_element(subarray_begin, subarray_end, (range / 2));

    unsigned int new_depth = (depth + 1) & (-1 + (depth == this->num_dimensions - 1));

    // Build left subtree (all elements left of the median)
    this->buildTree(subarray_begin, subarray_begin + (range / 2), new_depth);

    // Build right subtree (all elements right of the median)
    this->buildTree(subarray_begin + (range / 2) + 1, subarray_end, new_depth);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
subq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
jne 0x3cc7
jmp 0x3de3
cmpq $0x2, -0x28(%rbp)
jne 0x3d16
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x1c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movq -0x10(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
ucomiss 0x4(%rax,%rcx,4), %xmm0
jbe 0x3d11
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x1, %rdx
callq 0x3df0
jmp 0x3de3
cmpq $0x3, -0x28(%rbp)
jne 0x3d47
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %rdx
movl -0x1c(%rbp), %ecx
leaq -0x40(%rbp), %rdi
callq 0x3e70
movq -0x10(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x3ea0
jmp 0x3de3
jmp 0x3d49
jmp 0x3d4b
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %rdx
movl -0x1c(%rbp), %ecx
leaq -0x58(%rbp), %rdi
callq 0x3e70
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
shrq %rcx
leaq -0x58(%rbp), %rdi
callq 0x3f10
movq -0x68(%rbp), %rdi
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl -0x1c(%rbp), %ecx
movq 0x18(%rdi), %rdx
subq $0x1, %rdx
cmpq %rdx, %rcx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x28(%rbp), %rax
shrq %rax
addq %rax, %rdx
movl -0x5c(%rbp), %ecx
callq 0x3c90
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
shrq %rax
addq %rax, %rsi
addq $0x1, %rsi
movq -0x18(%rbp), %rdx
movl -0x5c(%rbp), %ecx
callq 0x3c90
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)