// 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>::staticBlockPartition(unsigned long, unsigned long, unsigned long)
inline std::size_t staticBlockPartition(std::size_t pivot, std::size_t unpartitioned_range_begin, std::size_t unpartitioned_range_end) {
        constexpr std::size_t BLOCK_PARTITION_THRESHOLD = BlockSize * 2;

        std::size_t last_item = unpartitioned_range_end - 1;

        alignas(64) std::size_t left_buffer[BlockSize];
        alignas(64) std::size_t right_buffer[BlockSize];
        std::size_t left_buffer_size = 0;
        std::size_t right_buffer_size = 0;

        std::size_t num_mutual_swaps = 0;

        std::size_t num_iters = 0;

        /*
         * Partitioning phase
         */
        while (static_cast<std::size_t>(last_item - unpartitioned_range_begin + 1) > BLOCK_PARTITION_THRESHOLD) {
            ++num_iters;
            // If both buffers are empty, process the next left and right blocks, recording any indices which are not
            // correctly partitioned.
            if (left_buffer_size == 0 && right_buffer_size == 0) {
                for (std::size_t i = 0; i < BlockSize; ) {
                    for (std::size_t j = 0; j < ItemsPerCacheLine; ++j) {
                        left_buffer[left_buffer_size] = i;
                        right_buffer[right_buffer_size] = i;

                        left_buffer_size += this->cellAt(pivot) < this->cellAt(unpartitioned_range_begin + i);
                        right_buffer_size += this->cellAt(last_item - i) < this->cellAt(pivot);
                        ++i;
                    }
                }
            }

            // If only the left bucket is empty, fill it.
            else if (left_buffer_size == 0) {
                for (std::size_t i = 0; i < BlockSize; ) {
                    for (std::size_t j = 0; j < ItemsPerCacheLine; ++j) {
                        left_buffer[left_buffer_size] = i;
                        left_buffer_size += this->cellAt(pivot) < this->cellAt(unpartitioned_range_begin + i);
                        ++i;
                    }
                }
            }

            // If only the right bucket is empty, fill it.
            else if (right_buffer_size == 0) {
                for (std::size_t i = 0; i < BlockSize; ) {
                    for (std::size_t j = 0; j < ItemsPerCacheLine; ++j) {
                        right_buffer[right_buffer_size] = i;
                        right_buffer_size += this->cellAt(last_item - i) < this->cellAt(pivot);
                        ++i;
                    }
                }
            }

            // The number of items to swap is the size of the smaller bucket
            num_mutual_swaps = std::min(left_buffer_size, right_buffer_size);

            // Perform a mutual exchange of misplaced values.
            for (std::size_t i = 0; i < num_mutual_swaps; ++i) {
                this->swap(
                    unpartitioned_range_begin + left_buffer[left_buffer_size - 1 - i],
                    last_item - right_buffer[right_buffer_size - 1 - i]
                );
            }

            left_buffer_size -= num_mutual_swaps;
            right_buffer_size -= num_mutual_swaps;

            // If left_buffer_size == 0, add BlockSize to unpartitioned_range_begin
            unpartitioned_range_begin += (0 - (left_buffer_size == 0)) & BlockSize;
            // If right_buffer_size == 0, subtract BlockSize from last_item
            last_item -= (0 - (right_buffer_size == 0)) & BlockSize;
        }

        /*
         * Cleanup phase for final iteration of main loop
         */
        // If left_buffer_size != 0, add BlockSize to begin
        unpartitioned_range_begin += (0 - (left_buffer_size != 0)) & BlockSize;

        // If right_buffer_size != 0, subtract BlockSize from last
        last_item -= (0 - (right_buffer_size != 0)) & BlockSize;

        // If there are remaining elements in the left buffer, move them to the right end of the comp() partition.
        // The elements closer to the chunk center are moved first, then the elements at the edges of the array.
        for (std::size_t i = 0; i < left_buffer_size; ++i) {
            this->swap(
                unpartitioned_range_begin - i - 1,
                unpartitioned_range_begin - BlockSize + left_buffer[left_buffer_size - 1 - i]
            );
        }

        // Ditto for the left buffer and the !comp() partition.
        for (std::size_t i = 0; i < right_buffer_size; ++i) {
            this->swap(
                last_item + 1 + i,
                last_item + BlockSize - right_buffer[right_buffer_size - 1 - i]
            );
        }

        unpartitioned_range_begin -= left_buffer_size;
        last_item += right_buffer_size;

        auto ret = dynamicBlockPartition(pivot, unpartitioned_range_begin, last_item, left_buffer, right_buffer);

        return ret;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
andq $-0x40, %rsp
subq $0x480, %rsp            # imm = 0x480
movq %rsi, 0x30(%rsp)
leaq -0x1(%rcx), %r8
subq %rdx, %rcx
addq $-0x81, %rcx
cmpq $-0x82, %rcx
movq %rdi, 0x28(%rsp)
ja 0x3e28
movq (%rdi), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rdi), %rcx
movl 0x10(%rdi), %r9d
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %r10, %rax
orq %r11, %rax
je 0x3ca4
testq %r11, %r11
je 0x3d20
testq %r10, %r10
jne 0x3d70
movq 0x38(%rsp), %rax
movq (%rax,%r9,8), %rax
movq 0x30(%rsp), %rsi
movss (%rax,%rsi,4), %xmm0
leaq (%rax,%r8,4), %rbx
xorl %r14d, %r14d
leaq (,%r14,4), %rax
movq %rbx, %r15
subq %rax, %r15
xorl %r12d, %r12d
movq %r10, %rax
movq %r14, 0x40(%rsp,%r10,8)
xorl %r10d, %r10d
ucomiss (%r15,%r12,4), %xmm0
seta %r10b
addq %rax, %r10
incq %r14
decq %r12
cmpq $-0x10, %r12
jne 0x3c72
leaq -0x1(%r14), %rax
cmpq $0x3e, %rax
jbe 0x3c61
jmp 0x3d70
movq 0x38(%rsp), %rax
movq (%rax,%r9,8), %rax
movq 0x30(%rsp), %rsi
movss (%rax,%rsi,4), %xmm0
leaq (%rax,%rdx,4), %rbx
leaq (%rax,%r8,4), %r14
xorl %r15d, %r15d
leaq (,%r15,4), %rax
movq %r14, %r12
subq %rax, %r12
xorl %r13d, %r13d
movq %r11, %rax
movq %r10, %rsi
movq %r15, 0x240(%rsp,%r11,8)
movq %r15, 0x40(%rsp,%r10,8)
movss (%rbx,%r15,4), %xmm1
xorl %r11d, %r11d
ucomiss %xmm0, %xmm1
seta %r11b
addq %rax, %r11
xorl %r10d, %r10d
ucomiss (%r12,%r13,4), %xmm0
seta %r10b
addq %rsi, %r10
incq %r15
decq %r13
cmpq $-0x10, %r13
jne 0x3cd3
leaq -0x1(%r15), %rax
cmpq $0x3e, %rax
jbe 0x3cc2
jmp 0x3d70
movq 0x38(%rsp), %rax
movq (%rax,%r9,8), %rax
movq 0x30(%rsp), %rsi
movss (%rax,%rsi,4), %xmm0
leaq (%rax,%rdx,4), %rbx
xorl %r14d, %r14d
movl $0x10, %r15d
movq %r11, %rax
movq %r14, 0x240(%rsp,%r11,8)
movss (%rbx,%r14,4), %xmm1
xorl %r11d, %r11d
ucomiss %xmm0, %xmm1
seta %r11b
addq %rax, %r11
incq %r14
decq %r15
jne 0x3d40
leaq -0x1(%r14), %rax
cmpq $0x3e, %rax
jbe 0x3d3a
cmpq %r11, %r10
movq %r11, %r15
cmovbq %r10, %r15
testq %r15, %r15
je 0x3dde
movq 0x28(%rsp), %rax
movq (%rax), %rbx
xorl %r14d, %r14d
testq %rcx, %rcx
je 0x3dd6
movq %r14, %rax
notq %rax
leaq (%r11,%rax), %rsi
addq %r10, %rax
movq %r8, %r12
subq 0x40(%rsp,%rax,8), %r12
movq 0x240(%rsp,%rsi,8), %r13
xorl %eax, %eax
movq (%rbx,%rax,8), %rsi
leaq (%rsi,%rdx,4), %rdi
movss (%rdi,%r13,4), %xmm0
movss (%rsi,%r12,4), %xmm1
movss %xmm1, (%rdi,%r13,4)
movss %xmm0, (%rsi,%r12,4)
incq %rax
cmpq %rax, %rcx
jne 0x3dae
incq %r14
cmpq %r15, %r14
jne 0x3d8a
movq %r11, %rbx
subq %r15, %rbx
xorl %eax, %eax
cmpq %r10, %r11
setbe %al
movq %r10, %r14
subq %r15, %r14
shll $0x6, %eax
addq %rax, %rdx
xorl %eax, %eax
cmpq %r11, %r10
seta %al
shll $0x6, %eax
addq %rax, %r8
addq $-0x40, %r8
movq %r8, %rax
subq %rdx, %rax
addq $-0x80, %rax
movq %r14, %r10
movq %rbx, %r11
cmpq $-0x81, %rax
jb 0x3c2d
jmp 0x3e2d
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %eax, %eax
testq %rbx, %rbx
setne %al
shll $0x6, %eax
addq %rdx, %rax
xorl %ecx, %ecx
testq %r14, %r14
sete %cl
shll $0x6, %ecx
testq %rbx, %rbx
je 0x3eb3
movq 0x28(%rsp), %rsi
movq (%rsi), %rdx
movq 0x8(%rsi), %r9
xorl %r10d, %r10d
leaq (,%rax,4), %r11
testq %r9, %r9
je 0x3eab
movq %r10, %r15
notq %r15
leaq (%rbx,%r15), %rsi
movq 0x240(%rsp,%rsi,8), %r12
xorl %r13d, %r13d
movq (%rdx,%r13,8), %rsi
addq %r11, %rsi
movss (%rsi,%r15,4), %xmm0
movss -0x100(%rsi,%r12,4), %xmm1
movss %xmm1, (%rsi,%r15,4)
movss %xmm0, -0x100(%rsi,%r12,4)
incq %r13
cmpq %r13, %r9
jne 0x3e7c
incq %r10
cmpq %rbx, %r10
jne 0x3e62
addq %r8, %rcx
addq $-0x40, %rcx
testq %r14, %r14
je 0x3f1c
leaq 0x40(%rcx), %rdx
movq 0x28(%rsp), %rsi
movq (%rsi), %r8
movq 0x8(%rsi), %r9
xorl %r10d, %r10d
testq %r9, %r9
je 0x3f14
movq %r10, %rsi
notq %rsi
addq %r14, %rsi
movq %rdx, %r11
subq 0x40(%rsp,%rsi,8), %r11
xorl %esi, %esi
movq (%r8,%rsi,8), %rdi
leaq (%rdi,%rcx,4), %r15
movss 0x4(%r15,%r10,4), %xmm0
movss (%rdi,%r11,4), %xmm1
movss %xmm1, 0x4(%r15,%r10,4)
movss %xmm0, (%rdi,%r11,4)
incq %rsi
cmpq %rsi, %r9
jne 0x3eea
incq %r10
cmpq %r14, %r10
jne 0x3ed2
subq %rbx, %rax
addq %r14, %rcx
leaq 0x240(%rsp), %r8
leaq 0x40(%rsp), %r9
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0x3f50
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq