// 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>::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 %r8, %r13
movq %rcx, %r12
movq %rsi, %r15
movq %rdi, %r14
movq %rdx, %rbp
subq %rsi, %rbp
movq %rbp, %xmm1
punpckldq 0x1925(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x192d(%rip), %xmm1     # 0x5030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rdx, 0x8(%rsp)
jb 0x3724
sqrtsd %xmm0, %xmm0
jmp 0x3729
callq 0x20e0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x18d7(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
cmpq $0x1f, %rbx
ja 0x38be
shrq %rbp
addq %r15, %rbp
movq (%r14), %rax
movl 0x10(%r14), %ecx
movq (%rax,%rcx,8), %rcx
movss (%rcx,%r15,4), %xmm0
ucomiss (%rcx,%rbp,4), %xmm0
jbe 0x379c
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x379c
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, %rdx
jne 0x377a
movss (%rcx,%rbp,4), %xmm0
movq 0x8(%rsp), %rbx
ucomiss -0x4(%rcx,%rbx,4), %xmm0
jbe 0x37da
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x37da
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movss (%rdi,%rbp,4), %xmm0
movss -0x4(%rdi,%rbx,4), %xmm1
movss %xmm1, (%rdi,%rbp,4)
movss %xmm0, -0x4(%rdi,%rbx,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x37b8
movss (%rcx,%r15,4), %xmm0
ucomiss (%rcx,%rbp,4), %xmm0
jbe 0x3813
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x3813
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movss (%rsi,%r15,4), %xmm0
movss (%rsi,%rbp,4), %xmm1
movss %xmm1, (%rsi,%r15,4)
movss %xmm0, (%rsi,%rbp,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x37f1
leaq 0x1(%r15), %r12
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0x3846
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movss 0x4(%rsi,%r15,4), %xmm0
movss (%rsi,%rbp,4), %xmm1
movss %xmm1, 0x4(%rsi,%r15,4)
movss %xmm0, (%rsi,%rbp,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x3822
addq $0x2, %r15
addq $-0x2, %rbx
movq %rbx, %rax
subq %r15, %rax
incq %rax
movq %rax, %rcx
andq $-0x2, %rcx
movabsq $0x3ffffffffffffffd, %rdx # imm = 0x3FFFFFFFFFFFFFFD
cmpq %rdx, %rax
leaq 0x8(,%rcx,4), %rax
movq $-0x1, %r13
cmovbeq %rax, %r13
movq %r13, %rdi
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 0x3f50
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2150
movq %r13, %rdi
jmp 0x3bc2
movq (%r12), %rax
testq %rax, %rax
js 0x38d1
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
jmp 0x38e9
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
testq %rbp, %rbp
js 0x38f8
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
jmp 0x3912
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 0x3925
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
jmp 0x393f
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 0x16ed(%rip), %xmm0     # 0x5040
cvttss2si %xmm0, %r12
sarq $0x3f, %rcx
andq %rcx, %r12
orq %rax, %r12
cmpq $0x20, %r12
jb 0x39de
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 %r13, %rcx
leaq 0x10(%rcx), %r12
cmovaq %rcx, %r12
testq %r12, %r12
js 0x399b
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
jmp 0x39b6
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
sarq $0x3f, %rcx
subss 0x166d(%rip), %xmm0     # 0x5040
cvttss2si %xmm0, %rbx
andq %rcx, %rbx
orq %rax, %rbx
cmpq $0x2, %rbx
jb 0x3a60
movq %rbx, %rax
shrq %rax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x3a58
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 0x3a09
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 0x3a30
incq %rsi
cmpq %rax, %rsi
jne 0x39f3
leaq (%rbx,%r15), %r13
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x3550
movq %r14, 0x10(%rsp)
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x3b02
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 0x3afa
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 0x3ad9
incq %r9
cmpq %rax, %r9
jne 0x3ab6
subq %rbx, %rbp
leaq (%r12,%r15), %r13
addq %r12, %r15
incq %r15
cmpq $0x80, %rbp
jbe 0x3b45
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 0x3bda
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 0x3f50
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2150
movq %r12, %rdi
callq 0x2150
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop