// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %r15
leaq 0x8(%rsp), %r12
movq %rcx, (%r12)
xorl %r13d, %r13d
cmpq %rbx, %r14
je 0x36c0
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x35ee
movq %r14, %rcx
notq %rcx
addq %rbx, %rcx
cmpq %rcx, %rax
je 0x365a
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x36ce
movq %rax, %rcx
subq %r14, %rcx
movq 0x8(%rsp), %rdx
cmpq %rdx, %rcx
je 0x35e4
leaq 0x1(%rcx), %rsi
cmpq %rcx, %rdx
cmovbq %rax, %rbx
movl $0x0, %eax
cmovaeq %rsi, %rax
movq %rdx, %rsi
subq %rax, %rsi
movq %rsi, 0x8(%rsp)
addl %eax, %r13d
andl $0xf, %r13d
addq %rax, %r14
cmpq %rdx, %rcx
jne 0x3572
jmp 0x36c0
leaq 0x1(%r14), %rcx
movq %r14, %rax
cmpq %rbx, %rcx
jae 0x3624
movq (%r15), %rax
movl 0x10(%r15), %edx
movq (%rax,%rdx,8), %rdx
movq %r14, %rsi
movss (%rdx,%rsi,4), %xmm0
ucomiss (%rdx,%rcx,4), %xmm0
movq %rcx, %rax
ja 0x3619
movq %rsi, %rax
incq %rcx
movq %rax, %rsi
cmpq %rcx, %rbx
jne 0x3608
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x36c0
movq (%r15), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss (%rdi,%r14,4), %xmm0
movss (%rdi,%rax,4), %xmm1
movss %xmm1, (%rdi,%r14,4)
movss %xmm0, (%rdi,%rax,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x3636
jmp 0x36c0
leaq 0x1(%r14), %rcx
cmpq %rbx, %rcx
jae 0x368d
movq (%r15), %rax
movl 0x10(%r15), %edx
movq (%rax,%rdx,8), %rdx
movss (%rdx,%rcx,4), %xmm0
ucomiss (%rdx,%r14,4), %xmm0
movq %rcx, %rax
ja 0x3680
movq %r14, %rax
incq %rcx
movq %rax, %r14
cmpq %rcx, %rbx
jne 0x366e
jmp 0x3690
movq %r14, %rax
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x36c0
movq (%r15), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movss -0x4(%rdi,%rbx,4), %xmm0
movss (%rdi,%rax,4), %xmm1
movss %xmm1, -0x4(%rdi,%rbx,4)
movss %xmm0, (%rdi,%rax,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x369e
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq