// 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>::selectPivotAndPartitionArray(unsigned long, unsigned long, unsigned long const&, unsigned long)
inline std::size_t selectPivotAndPartitionArray(std::size_t begin, std::size_t end, const std::size_t& k, std::size_t offset) {
        constexpr std::size_t AdaptiveSampleThreshold = 2 * ItemsPerCacheLine;

        std::size_t chunk_size = end - begin;
        std::size_t sample_size = sqrt(chunk_size);

        // If the sample size is smaller than the threshold for adaptive sampling, fall back to median of 3 pivot selection
        // and use dynamic block partitioning.
        if (sample_size < AdaptiveSampleThreshold) {
            std::size_t pivot = medianOf3(begin, end);

            return dynamicBlockPartition(pivot, begin + 2, end - 2);
        }

        // The samples are moved to the beginning of the array, and of those samples, the pivot_pos-th largest sample is
        // chosen to be the pivot. The pivot position is determined by the size of k relative to the size of the chunk to partition.
        float pivot_ratio = float(k) / chunk_size;
        std::size_t pivot_pos = pivot_ratio * sample_size;

        // If the pivot position is large enough to make it viable, adjust the pivot position so that the pivot position is
        // the closest power of ItemsPerCacheLine, minus 1. The sample size is also adjusted to maintain the original
        // pivot position : sample size ratio.

        if (pivot_pos >= (AdaptiveSampleThreshold)) {
            // Round the pivot position to the closest power of ItemsPerCacheLine, minus 1
            if (((pivot_pos % ItemsPerCacheLine) >= (ItemsPerCacheLine / 2)) || (pivot_pos < ItemsPerCacheLine)) {
                pivot_pos += ((ItemsPerCacheLine - 1) - (pivot_pos % ItemsPerCacheLine));
            }

            else {
                pivot_pos -= ((pivot_pos % ItemsPerCacheLine) + 1);
            }

            // Account for the offset of the array chunk. For example, if the chunk begins at ItemsPerCacheLine + 3,
            // this will subtract 3 from the pivot position so that the unpartitioned range begins on the start of a cache
            // line.
            if (pivot_pos <= offset) { pivot_pos += (ItemsPerCacheLine - offset); }
            else { pivot_pos -= offset; }

            // Adjust the sample size based on changes to the pivot position.
            sample_size = std::ceil(float(pivot_pos) / pivot_ratio);
        }

        // Use the beginning and end of (sample size / 2) cache lines as the samples.
        for (std::size_t i = 0; i < (sample_size >> 1); ++i) {
            this->swap(begin + (i << 1), begin + (ItemsPerCacheLine * i));
            this->swap(begin + (i << 1) + 1, begin + (ItemsPerCacheLine * i) + (ItemsPerCacheLine - 1));
        }

        // If the sample size is odd, we'll just be lazy and use whatever value is already at begin + sample_size - 1. It's random enough.

        // Find the pivot_pos-th element of the sampled items.
        nth_element(begin, begin + sample_size, pivot_pos);

        // Swap all items in the sample slice that are !comp() to the pivot with items at the end of the chunk.
        for (std::size_t i = 0; i < this->num_dimensions; ++i) {
            std::swap_ranges(
                this->nodes[i] + (begin + pivot_pos + 1),
                this->nodes[i] + (begin + sample_size),
                this->nodes[i] + (end - (sample_size - pivot_pos) + 1)
            );
        }

        // Partition items between [pivot + 1, !comp() mini partition begin)
        if ((end - begin) - sample_size > (BlockSize << 1u)) {
            return staticBlockPartition(begin + pivot_pos, begin + pivot_pos + 1, end - (sample_size - pivot_pos) + 1);
        }

        else {
            return dynamicBlockPartition(begin + pivot_pos, begin + pivot_pos + 1, end - (sample_size - pivot_pos) + 1);
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdx, %rbp
subq %rsi, %rbp
movq %rbp, %xmm0
punpckldq 0x1968(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1970(%rip), %xmm0     # 0x5030
movq %rdi, %r14
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rsi
subsd 0x192a(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rsi
andq %rsi, %rbx
orq %rax, %rbx
cmpq $0x1f, %rbx
ja 0x37d8
shrq %rbp
addq %r15, %rbp
movq (%r14), %rax
movl 0x10(%r14), %ecx
movq (%rax,%rcx,8), %r8
movss (%r8,%rbp,4), %xmm0
movss (%r8,%r15,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3754
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x3754
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movss (%rdi,%r15,4), %xmm0
movss (%rdi,%rbp,4), %xmm1
movss %xmm1, (%rdi,%r15,4)
movss %xmm0, (%rdi,%rbp,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x372c
movss (%r8,%rbp,4), %xmm0
ucomiss -0x4(%r8,%rdx,4), %xmm0
movq 0x8(%r14), %rcx
jbe 0x3793
testq %rcx, %rcx
je 0x3b1f
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movss (%rdi,%rbp,4), %xmm0
movss -0x4(%rdi,%rdx,4), %xmm1
movss %xmm1, (%rdi,%rbp,4)
movss %xmm0, -0x4(%rdi,%rdx,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x376b
movss (%r8,%rbp,4), %xmm0
movss (%r8,%r15,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x38bd
testq %rcx, %rcx
je 0x3b28
xorl %edi, %edi
movq (%rax,%rdi,8), %rsi
movss (%rsi,%r15,4), %xmm0
movss (%rsi,%rbp,4), %xmm1
movss %xmm1, (%rsi,%r15,4)
movss %xmm0, (%rsi,%rbp,4)
incq %rdi
cmpq %rdi, %rcx
jne 0x37ad
leaq 0x1(%r15), %r12
jmp 0x38ca
movq (%rcx), %rax
testq %rax, %rax
js 0x37ea
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
jmp 0x3802
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
testq %rbp, %rbp
js 0x3811
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
jmp 0x382b
movq %rbp, %rax
shrq %rax
movl %ebp, %ecx
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
divss %xmm0, %xmm1
testq %rbx, %rbx
js 0x383e
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
jmp 0x3858
movq %rbx, %rax
shrq %rax
movl %ebx, %ecx
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x17d4(%rip), %xmm0     # 0x5040
cvttss2si %xmm0, %r12
sarq $0x3f, %rcx
andq %rcx, %r12
orq %rax, %r12
cmpq $0x20, %r12
movq %rdx, 0x8(%rsp)
jb 0x393c
movl %r12d, %eax
andl $0xf, %eax
xorl %ecx, %ecx
cmpq $0x8, %rax
sbbq %rcx, %rcx
orq $0xf, %rcx
xorq %rax, %rcx
addq %r12, %rcx
subq %r8, %rcx
leaq 0x10(%rcx), %r12
cmovaq %rcx, %r12
testq %r12, %r12
js 0x38f3
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
jmp 0x390e
leaq 0x1(%r15), %r12
testq %rcx, %rcx
je 0x3b2c
xorl %edi, %edi
movq (%rax,%rdi,8), %rsi
movss (%rsi,%r12,4), %xmm0
movss (%rsi,%rbp,4), %xmm1
movss %xmm1, (%rsi,%r12,4)
movss %xmm0, (%rsi,%rbp,4)
incq %rdi
cmpq %rdi, %rcx
jne 0x38cc
jmp 0x3b2c
movq %r12, %rax
shrq %rax
movl %r12d, %ecx
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
divss %xmm1, %xmm0
callq 0x20c0
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x1719(%rip), %xmm0     # 0x5040
cvttss2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
cmpq $0x2, %rbx
jb 0x39b8
movq %rbx, %rax
shrq %rax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x39b0
movq %rsi, %rdi
shlq $0x4, %rdi
addq %r15, %rdi
leaq (%r15,%rsi,2), %r8
xorl %r9d, %r9d
movq (%rcx,%r9,8), %r10
movss (%r10,%r8,4), %xmm0
movss (%r10,%rdi,4), %xmm1
movss %xmm1, (%r10,%r8,4)
movss %xmm0, (%r10,%rdi,4)
incq %r9
cmpq %r9, %rdx
jne 0x3961
xorl %r9d, %r9d
movq (%rcx,%r9,8), %r10
movss 0x4(%r10,%r8,4), %xmm0
movss 0x3c(%r10,%rdi,4), %xmm1
movss %xmm1, 0x4(%r10,%r8,4)
movss %xmm0, 0x3c(%r10,%rdi,4)
incq %r9
cmpq %r9, %rdx
jne 0x3988
incq %rsi
cmpq %rax, %rsi
jne 0x394b
leaq (%rbx,%r15), %r13
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x3514
movq %r14, 0x10(%rsp)
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x3a5a
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x8(%rsp), %rdx
leaq (,%rdx,4), %rdx
leaq (,%rbx,4), %rsi
subq %rsi, %rdx
leaq 0x4(,%r12,4), %r14
leaq (,%r15,4), %r8
xorl %r9d, %r9d
movq (%rcx,%r9,8), %r10
leaq (%r10,%r12,4), %rdi
leaq (%rdi,%r15,4), %rdi
addq $0x4, %rdi
leaq (%r10,%r13,4), %r11
cmpq %r11, %rdi
je 0x3a52
leaq (%r10,%rdx), %r11
addq %r8, %r10
movq %r14, %rdi
movss (%r10,%rdi), %xmm0
movss (%r11,%rdi), %xmm1
movss %xmm1, (%r10,%rdi)
movss %xmm0, (%r11,%rdi)
addq $0x4, %rdi
cmpq %rdi, %rsi
jne 0x3a31
incq %r9
cmpq %rax, %r9
jne 0x3a0e
subq %rbx, %rbp
leaq (%r12,%r15), %r13
addq %r12, %r15
incq %r15
cmpq $0x80, %rbp
jbe 0x3a9d
movq 0x8(%rsp), %rax
subq %rbx, %rax
leaq (%rax,%r12), %rcx
incq %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3bba
subq %rbx, %r12
movq 0x8(%rsp), %rcx
leaq (%r12,%rcx), %rax
leaq (%r12,%rcx), %r14
incq %r14
subq %r13, %rax
incq %rax
movq %rax, %rcx
andq $-0x2, %rcx
movabsq $0x3ffffffffffffffd, %rdx # imm = 0x3FFFFFFFFFFFFFFD
cmpq %rdx, %rax
leaq 0x8(,%rcx,4), %rax
movq $-0x1, %r12
cmovbeq %rax, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, %rbp
movq %r12, %rdi
callq 0x2030
movq %rax, %r12
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbp, %r8
movq %rax, %r9
callq 0x3f48
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2140
movq %r12, %rdi
jmp 0x3ba2
movss (%r8,%r15,4), %xmm1
ucomiss %xmm0, %xmm1
leaq 0x1(%r15), %r12
addq $0x2, %r15
addq $-0x2, %rdx
movq %rdx, %rax
subq %r15, %rax
incq %rax
movq %rax, %rcx
andq $-0x2, %rcx
movabsq $0x3ffffffffffffffd, %rsi # imm = 0x3FFFFFFFFFFFFFFD
cmpq %rsi, %rax
leaq 0x8(,%rcx,4), %rax
movq $-0x1, %r13
cmovbeq %rax, %r13
movq %r13, %rdi
movq %rdx, %rbx
callq 0x2030
movq %rax, %rbp
movq %r13, %rdi
callq 0x2030
movq %rax, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %rbp, %r8
movq %rax, %r9
callq 0x3f48
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2140
movq %r13, %rdi
callq 0x2140
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop