// File: 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 %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x3f47
jmp 0x4159
cmpq $0x0, -0x20(%rbp)
jne 0x3fcb
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x3fb5
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4200
movq -0x60(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x64(%rbp)
movq -0x30(%rbp), %rsi
callq 0x4200
movss -0x64(%rbp), %xmm1
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x3fa5
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x3fa7
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3f62
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x4230
jmp 0x4159
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
subq -0x10(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x405d
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x4043
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x4200
movq -0x60(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x68(%rbp)
movq -0x40(%rbp), %rsi
callq 0x4200
movss -0x68(%rbp), %xmm0
ucomiss (%rax), %xmm0
jbe 0x4033
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x4035
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3ff4
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
subq $0x1, %rsi
movq -0x40(%rbp), %rdx
callq 0x4230
jmp 0x4159
jmp 0x405f
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %r8
leaq -0x20(%rbp), %rcx
callq 0x42b0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq -0x10(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x408f
jmp 0x4159
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
subq -0x10(%rbp), %rcx
cmpq %rcx, %rax
setb %al
andb $0x1, %al
movb %al, -0x51(%rbp)
movq -0x18(%rbp), %rcx
subq -0x50(%rbp), %rcx
movb -0x51(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
xorl %eax, %eax
subl %edx, %eax
cltq
andq %rax, %rcx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x50(%rbp), %rcx
subq -0x10(%rbp), %rcx
addq $0x1, %rcx
movb -0x51(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
xorl %eax, %eax
subl %edx, %eax
cltq
andq %rax, %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x50(%rbp), %rax
subq -0x10(%rbp), %rax
addq $0x1, %rax
movb -0x51(%rbp), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %edx
xorl %ecx, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
andq $0xf, %rax
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rax
subq -0x10(%rbp), %rax
addq $0x1, %rax
movb -0x51(%rbp), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %edx
xorl %ecx, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
addq -0x10(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x3f38
addq $0x70, %rsp
popq %rbp
retq
nop