// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r10
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
decq %r10
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r10, %rax
subq %r12, %rax
addq $-0x80, %rax
cmpq $-0x82, %rax
movq %r10, 0x38(%rsp)
ja 0x3877
movq %rbx, %rax
orq %r13, %rax
je 0x373e
testq %r13, %r13
je 0x37ba
testq %rbx, %rbx
je 0x381d
cmpq %r13, %rbx
movq %r13, %r14
cmovbq %rbx, %r14
movq %r14, 0x20(%rsp)
negq %r14
leaq 0x38(%rsp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, 0x30(%rsp)
leaq 0x238(%rsp), %rax
leaq (%rax,%r13,8), %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x36fd
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %rsi
addq %r12, %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rax
subq (%rax,%r15,8), %rdx
movq 0x10(%rsp), %rdi
callq 0x3250
decq %r15
jmp 0x36cf
movq %r13, %rax
movq 0x20(%rsp), %rsi
subq %rsi, %rax
xorl %ecx, %ecx
cmpq %rbx, %r13
setbe %cl
movq %rbx, %rdx
subq %rsi, %rdx
shll $0x6, %ecx
addq %rcx, %r12
xorl %ecx, %ecx
cmpq %r13, %rbx
seta %cl
shll $0x6, %ecx
movq 0x38(%rsp), %r10
addq %rcx, %r10
addq $-0x40, %r10
movq %rax, %r13
movq %rdx, %rbx
jmp 0x3662
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rcx
movq 0x18(%rsp), %rax
movss (%rcx,%rax,4), %xmm0
leaq (%rcx,%r12,4), %rax
leaq (%rcx,%r10,4), %rcx
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %edx, %edx
cmpq $0x3f, %rdx
ja 0x369b
leaq 0x10(%rdx), %rsi
xorl %edi, %edi
cmpq $-0x10, %rdi
je 0x37b1
movq %rdx, 0x240(%rsp,%r13,8)
movq %rdx, 0x40(%rsp,%rbx,8)
movss (%rax,%rdx,4), %xmm1
xorl %r8d, %r8d
ucomiss %xmm0, %xmm1
seta %r8b
addq %r8, %r13
xorl %r8d, %r8d
ucomiss (%rcx,%rdi,4), %xmm0
seta %r8b
addq %r8, %rbx
incq %rdx
decq %rdi
jmp 0x3776
addq $-0x40, %rcx
movq %rsi, %rdx
jmp 0x3766
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x18(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
leaq (%rax,%r12,4), %rax
xorl %r13d, %r13d
xorl %ecx, %ecx
cmpq $0x3f, %rcx
ja 0x369b
leaq 0x10(%rcx), %rdx
xorl %esi, %esi
cmpq $0x10, %rsi
je 0x3814
leaq (%rcx,%rsi), %rdi
movq %rdi, 0x240(%rsp,%r13,8)
movss (%rax,%rsi,4), %xmm1
xorl %edi, %edi
ucomiss %xmm0, %xmm1
seta %dil
addq %rdi, %r13
incq %rsi
jmp 0x37ec
addq $0x40, %rax
movq %rdx, %rcx
jmp 0x37dc
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x18(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
leaq (%rax,%r10,4), %rax
xorl %ebx, %ebx
xorl %ecx, %ecx
cmpq $0x3f, %rcx
ja 0x369b
leaq 0x10(%rcx), %rdx
xorl %esi, %esi
cmpq $-0x10, %rsi
je 0x386e
movq %rcx, 0x40(%rsp,%rbx,8)
xorl %edi, %edi
ucomiss (%rax,%rsi,4), %xmm0
seta %dil
addq %rdi, %rbx
incq %rcx
decq %rsi
jmp 0x384e
addq $-0x40, %rax
movq %rdx, %rcx
jmp 0x383e
xorl %eax, %eax
movq %r13, %r15
subq $0x1, %r15
setae %al
shll $0x6, %eax
leaq (%r12,%rax), %rcx
movq %rcx, 0x30(%rsp)
xorl %ecx, %ecx
testq %rbx, %rbx
sete %cl
shll $0x6, %ecx
addq $-0x40, %rcx
movq %rcx, 0x28(%rsp)
leaq (%r12,%rax), %r14
addq $-0x40, %r14
addq %rax, %r12
decq %r12
cmpq $-0x1, %r15
je 0x38d7
movq 0x240(%rsp,%r15,8), %rdx
addq %r14, %rdx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3250
decq %r15
decq %r12
jmp 0x38b1
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x20(%rsp)
leaq (%rcx,%rax), %r14
addq $0x40, %r14
leaq -0x1(%rbx), %r12
leaq (%rcx,%rax), %r15
incq %r15
cmpq $-0x1, %r12
je 0x3920
movq %r14, %rdx
subq 0x40(%rsp,%r12,8), %rdx
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x3250
decq %r12
incq %r15
jmp 0x38fd
movq 0x30(%rsp), %rdx
subq %r13, %rdx
movq 0x20(%rsp), %rcx
addq %rbx, %rcx
leaq 0x240(%rsp), %r8
leaq 0x40(%rsp), %r9
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x395c
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop