// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, 0x8(%rsp)
cmpq %rdx, %rsi
jne 0x3539
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
xorl %r12d, %r12d
leaq 0x8(%rsp), %r13
testq %rcx, %rcx
je 0x35b5
movq %r14, %rax
notq %rax
addq %rbx, %rax
cmpq %rax, %rcx
je 0x3624
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x3694
movq %rax, %rdx
subq %r14, %rdx
movq 0x8(%rsp), %rcx
cmpq %rcx, %rdx
je 0x352b
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, %r12d
andl $0xf, %r12d
addq %rax, %r14
cmpq %rbx, %r14
jne 0x354a
jmp 0x352b
leaq 0x1(%r14), %rcx
movq %r14, %rax
cmpq %rbx, %rcx
jae 0x35eb
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 0x35e0
movq %rsi, %rax
incq %rcx
movq %rax, %rsi
cmpq %rcx, %rbx
jne 0x35cf
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x352b
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 0x35fd
jmp 0x352b
leaq 0x1(%r14), %rcx
cmpq %rbx, %rcx
jae 0x3657
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 0x364a
movq %r14, %rax
incq %rcx
movq %rax, %r14
cmpq %rcx, %rbx
jne 0x3638
jmp 0x365a
movq %r14, %rax
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x352b
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 0x366c
jmp 0x352b
nop