// 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>::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), %rax
subq %rdx, %rcx
addq $-0x81, %rcx
cmpq $-0x82, %rcx
ja 0x3ea3
movq (%rdi), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rdi), %r9
movq %rdi, 0x28(%rsp)
movl 0x10(%rdi), %r10d
xorl %ecx, %ecx
xorl %r11d, %r11d
movq %rcx, %r8
orq %r11, %r8
je 0x3c2e
testq %r11, %r11
je 0x3cad
testq %rcx, %rcx
je 0x3d0b
movq %r11, %rbx
jmp 0x3d06
movq 0x38(%rsp), %rcx
movq (%rcx,%r10,8), %r8
movq 0x30(%rsp), %rcx
movss (%r8,%rcx,4), %xmm0
leaq (%r8,%rdx,4), %rcx
leaq (%r8,%rax,4), %r11
xorl %r8d, %r8d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r8, %r15
addq $0x10, %r8
xorl %r13d, %r13d
movq %r15, %r12
movq %rbx, %rsi
movq %r14, %rdi
movq %r12, 0x240(%rsp,%rbx,8)
movq %r12, 0x40(%rsp,%r14,8)
movss (%rcx,%r12,4), %xmm1
xorl %ebx, %ebx
ucomiss %xmm0, %xmm1
seta %bl
addq %rsi, %rbx
xorl %r14d, %r14d
ucomiss (%r11,%r13,4), %xmm0
seta %r14b
addq %rdi, %r14
incq %r12
decq %r13
cmpq $-0x10, %r13
jne 0x3c5f
addq $-0x40, %r11
cmpq $0x2f, %r15
jbe 0x3c52
jmp 0x3d64
movq 0x38(%rsp), %rsi
movq (%rsi,%r10,8), %rsi
movq 0x30(%rsp), %rdi
movss (%rsi,%rdi,4), %xmm0
leaq (%rsi,%rdx,4), %r11
xorl %r8d, %r8d
xorl %ebx, %ebx
movq %r8, %r14
addq $0x10, %r8
xorl %r15d, %r15d
movq %rbx, %rsi
leaq (%r14,%r15), %rdi
movq %rdi, 0x240(%rsp,%rbx,8)
movss (%r11,%r15,4), %xmm1
xorl %ebx, %ebx
ucomiss %xmm0, %xmm1
seta %bl
addq %rsi, %rbx
incq %r15
cmpq $0x10, %r15
jne 0x3cd3
addq $0x40, %r11
cmpq $0x2f, %r14
jbe 0x3cc9
movq %rcx, %r14
jmp 0x3d64
movq 0x38(%rsp), %rcx
movq (%rcx,%r10,8), %rcx
movq 0x30(%rsp), %rsi
movss (%rcx,%rsi,4), %xmm0
leaq (%rcx,%rax,4), %rcx
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rbx, %r8
addq $0x10, %rbx
xorl %r15d, %r15d
movq %r8, %r12
movq %r14, %rsi
movq %r12, 0x40(%rsp,%r14,8)
xorl %r14d, %r14d
ucomiss (%rcx,%r15,4), %xmm0
seta %r14b
addq %rsi, %r14
incq %r12
decq %r15
cmpq $-0x10, %r15
jne 0x3d34
addq $-0x40, %rcx
cmpq $0x2f, %r8
jbe 0x3d27
movq %r11, %rbx
cmpq %rbx, %r14
movq %rbx, %r15
cmovbq %r14, %r15
testq %r15, %r15
je 0x3dd3
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
xorl %r11d, %r11d
testq %r9, %r9
je 0x3dcb
movq %r11, %rsi
notq %rsi
leaq (%rbx,%rsi), %rdi
addq %r14, %rsi
movq %rax, %r8
subq 0x40(%rsp,%rsi,8), %r8
movq 0x240(%rsp,%rdi,8), %r12
xorl %r13d, %r13d
movq (%rcx,%r13,8), %rsi
leaq (%rsi,%rdx,4), %rdi
movss (%rdi,%r12,4), %xmm0
movss (%rsi,%r8,4), %xmm1
movss %xmm1, (%rdi,%r12,4)
movss %xmm0, (%rsi,%r8,4)
incq %r13
cmpq %r13, %r9
jne 0x3da3
incq %r11
cmpq %r15, %r11
jne 0x3d7e
movq %rbx, %r11
subq %r15, %r11
xorl %esi, %esi
cmpq %r14, %rbx
setbe %sil
movq %r14, %rcx
subq %r15, %rcx
shll $0x6, %esi
addq %rsi, %rdx
xorl %esi, %esi
cmpq %rbx, %r14
seta %sil
shll $0x6, %esi
addq %rsi, %rax
addq $-0x40, %rax
movq %rax, %rsi
subq %rdx, %rsi
addq $-0x80, %rsi
cmpq $-0x81, %rsi
jb 0x3c0c
xorl %esi, %esi
testq %r11, %r11
setne %sil
shll $0x6, %esi
addq %rsi, %rdx
xorl %r8d, %r8d
testq %rcx, %rcx
sete %r8b
shll $0x6, %r8d
testq %r11, %r11
je 0x3ea8
movq 0x28(%rsp), %rsi
movq (%rsi), %r9
movq 0x8(%rsi), %r10
xorl %esi, %esi
leaq (,%rdx,4), %rbx
testq %r10, %r10
je 0x3e99
movq %rsi, %r14
notq %r14
leaq (%r11,%r14), %rdi
movq 0x240(%rsp,%rdi,8), %r15
xorl %r12d, %r12d
movq (%r9,%r12,8), %rdi
addq %rbx, %rdi
movss (%rdi,%r14,4), %xmm0
movss -0x100(%rdi,%r15,4), %xmm1
movss %xmm1, (%rdi,%r14,4)
movss %xmm0, -0x100(%rdi,%r15,4)
incq %r12
cmpq %r12, %r10
jne 0x3e6a
incq %rsi
cmpq %r11, %rsi
jne 0x3e50
jmp 0x3eab
xorl %r11d, %r11d
jmp 0x3f19
xorl %r11d, %r11d
movq 0x28(%rsp), %rdi
addq %r8, %rax
addq $-0x40, %rax
testq %rcx, %rcx
je 0x3f19
leaq 0x40(%rax), %r8
movq (%rdi), %r9
movq 0x8(%rdi), %r10
xorl %esi, %esi
testq %r10, %r10
je 0x3f0a
movq %rsi, %rdi
notq %rdi
addq %rcx, %rdi
movq %r8, %rbx
subq 0x40(%rsp,%rdi,8), %rbx
xorl %r14d, %r14d
movq (%r9,%r14,8), %rdi
leaq (%rdi,%rax,4), %r15
movss 0x4(%r15,%rsi,4), %xmm0
movss (%rdi,%rbx,4), %xmm1
movss %xmm1, 0x4(%r15,%rsi,4)
movss %xmm0, (%rdi,%rbx,4)
incq %r14
cmpq %r14, %r10
jne 0x3ee2
incq %rsi
cmpq %rcx, %rsi
jne 0x3ec9
movq 0x28(%rsp), %rdi
jmp 0x3f1b
xorl %ecx, %ecx
addq %rax, %rcx
subq %r11, %rdx
leaq 0x240(%rsp), %r8
leaq 0x40(%rsp), %r9
movq 0x30(%rsp), %rsi
callq 0x3f48
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop