// File: /workspace/github_cpp_makefile_star2_O2/cfiutak1[P]HPyC-KD-Tree/./utils/filewriter/../../src/kdtree/../quickselect/AdaptiveCacheAwareBlockquickselect.hpp
// Function: hpyc::AdaptiveCacheAwareBlockquickselect<float, 64ul, 64ul>::nth_element(unsigned long, unsigned long, unsigned long)
void nth_element(std::size_t begin, std::size_t end, std::size_t n) {
        std::size_t offset = 0;

        while (true) {
            if (begin == end) { return; }

            // If we're looking for the smallest item in the array, find it and move it to the beginning.
            if (n == 0) {
                std::size_t min_node_pos = begin;

                for (std::size_t i = begin + 1; i < end; ++i) {
                    if (this->cellAt(i) < this->cellAt(min_node_pos)) {
                        min_node_pos = i;
                    }
                }

                this->swap(begin, min_node_pos);

                return;
            }

            // If we're looking for the largest item in the array, find it and move it to the last item's position.
            else if (n == end - begin - 1) {
                std::size_t max_node_pos = begin;

                for (std::size_t i = begin + 1; i < end; ++i) {
                    if (this->cellAt(i) > this->cellAt(max_node_pos)) {
                        max_node_pos = i;
                    }
                }

                this->swap(end - 1, max_node_pos);

                return;
            }

            std::size_t pivot = selectPivotAndPartitionArray(begin, end, n, offset);

            if (pivot - begin == n) { return; }

            bool n_is_smaller = n < static_cast<std::size_t>(pivot - begin);

            // If the nth_element is smaller than the pivot's position, set the new search range to [begin, pivot).
            end -= ((end - pivot) & (0 - (n_is_smaller)));

            // If the nth_element is larger than the pivot's position, set the new search range to (pivot, end) and adjust the offset.
            n -= ((pivot - begin + 1) & (0 - (!n_is_smaller)));
            offset += ((pivot - begin + 1) & (0 - (!n_is_smaller)));
            offset &= (ItemsPerCacheLine - 1);
            begin += ((pivot - begin + 1) & (0 - (!n_is_smaller)));
        }
    }

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x8(%rsp), %r12
movq %rcx, (%r12)
xorl %r13d, %r13d
cmpq %rbx, %r15
je 0x3227
testq %rcx, %rcx
je 0x31b0
movq %r15, %rax
notq %rax
addq %rbx, %rax
cmpq %rax, %rcx
je 0x31e8
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x3280
movq %rax, %rdx
subq %r15, %rdx
movq 0x8(%rsp), %rcx
cmpq %rcx, %rdx
je 0x3227
leaq 0x1(%rdx), %rsi
cmpq %rdx, %rcx
cmovbq %rax, %rbx
movl $0x0, %eax
cmovaeq %rsi, %rax
subq %rax, %rcx
movq %rcx, 0x8(%rsp)
addl %eax, %r13d
andl $0xf, %r13d
addq %rax, %r15
jmp 0x3140
movq (%r14), %rax
movl 0x10(%r14), %ecx
leaq 0x1(%r15), %rsi
movq %r15, %rdx
cmpq %rbx, %rsi
jae 0x31e0
movq (%rax,%rcx,8), %rdi
movss (%rdi,%rdx,4), %xmm0
ucomiss (%rdi,%rsi,4), %xmm0
movq %rsi, %rdi
ja 0x31d8
movq %rdx, %rdi
incq %rsi
movq %rdi, %rdx
jmp 0x31be
movq %r14, %rdi
movq %r15, %rsi
jmp 0x3222
movq (%r14), %rax
movl 0x10(%r14), %ecx
leaq 0x1(%r15), %rdx
cmpq %rbx, %rdx
jae 0x3216
movq (%rax,%rcx,8), %rsi
movss (%rsi,%rdx,4), %xmm0
ucomiss (%rsi,%r15,4), %xmm0
movq %rdx, %rsi
ja 0x320e
movq %r15, %rsi
incq %rdx
movq %rsi, %r15
jmp 0x31f3
decq %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3250
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop