// 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>::dynamicBlockPartition(unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long*)
inline std::size_t dynamicBlockPartition(std::size_t pivot, std::size_t unpartitioned_range_begin, std::size_t last_unpartitioned_item, std::size_t* left_buffer, std::size_t* right_buffer) {
        /*
         * Partitioning phase
         */
        // + 1 because last_unpartitioned_item is inclusive
        std::size_t remaining_unpartitioned_size = last_unpartitioned_item - unpartitioned_range_begin + 1;

        std::size_t left_buffer_size = 0;
        std::size_t right_buffer_size = 0;

        // Iterate over the array chunk, populating the left and right swap buffers.
        for (size_t i = 0; i < remaining_unpartitioned_size / 2; ++i) {
            left_buffer[left_buffer_size] = i;
            left_buffer_size += (this->cellAt(pivot) < this->cellAt(unpartitioned_range_begin + i));

            right_buffer[right_buffer_size] = i;
            right_buffer_size += (this->cellAt(last_unpartitioned_item - i) < this->cellAt(pivot));
        }

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

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

        /*
         * Cleanup phase
         */
        std::size_t initial_unpartitioned_range_begin = unpartitioned_range_begin;

        // Update unpartitioned_range_begin to be at the beginning of the !comp() partition. If the array chunk is odd, the middle
        // item has not been inspected. If this item compares with the pivot, the comp() partition's size is incremented by 1.
        unpartitioned_range_begin += (remaining_unpartitioned_size / 2);
        unpartitioned_range_begin += ((remaining_unpartitioned_size % 2) && (this->cellAt(unpartitioned_range_begin) < this->cellAt(pivot)));

        // 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 (size_t i = 0; i < left_buffer_size - num_mutual_swaps; ++i) {
            this->swap(
                initial_unpartitioned_range_begin + left_buffer[left_buffer_size - 1 - i],
                unpartitioned_range_begin - 1 - i
           );
        }

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

        left_buffer_size -= num_mutual_swaps;
        right_buffer_size -= num_mutual_swaps;

        // Move unpartitioned_range_begin to the left/right, depending on which (if any) of the buffers had remaining items.
        unpartitioned_range_begin -= left_buffer_size;
        unpartitioned_range_begin += right_buffer_size;

        // Swap the pivot with the last item in the comp() partition.
        this->swap(unpartitioned_range_begin - 1, pivot);

        return unpartitioned_range_begin - 1;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbp
movq %r8, %r14
movq %rcx, %rax
subq %rdx, %rax
movq %rax, 0x18(%rsp)
leaq 0x1(%rax), %r11
shrq %r11
movq (%rdi), %rax
movq %rdi, 0x8(%rsp)
movl 0x10(%rdi), %r10d
movq %rdx, 0x10(%rsp)
leaq (,%rdx,4), %rdx
xorl %r12d, %r12d
movq %rcx, 0x30(%rsp)
movq %rcx, %rbx
xorl %r13d, %r13d
xorl %edi, %edi
cmpq %rdi, %r11
je 0x39ef
movq %rdi, (%r14,%r12,8)
movq (%rax,%r10,8), %rcx
movss (%rcx,%rsi,4), %xmm0
leaq (%rcx,%rdx), %r9
movss (%r9,%rdi,4), %xmm1
xorl %r9d, %r9d
ucomiss %xmm0, %xmm1
seta %r9b
addq %r9, %r12
movq %rdi, (%rbp,%r13,8)
xorl %r9d, %r9d
ucomiss (%rcx,%rbx,4), %xmm0
seta %r9b
addq %r9, %r13
incq %rdi
decq %rbx
jmp 0x39ab
movq %rsi, 0x20(%rsp)
movq %r11, 0x28(%rsp)
cmpq %r12, %r13
movq %r12, %r15
cmovbq %r13, %r15
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x3a2c
movq (%r14,%rbx,8), %rsi
addq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdx
subq (%rbp,%rbx,8), %rdx
movq 0x8(%rsp), %rdi
callq 0x3250
incq %rbx
jmp 0x3a05
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rdx
testb $0x1, 0x18(%rsp)
movq %rbp, 0x40(%rsp)
jne 0x3a6a
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rcx
movq 0x20(%rsp), %rax
movss (%rcx,%rax,4), %xmm0
xorl %eax, %eax
ucomiss (%rcx,%rdx,4), %xmm0
seta %al
jmp 0x3a6c
xorl %eax, %eax
addq %rax, %rdx
movq %rdx, 0x18(%rsp)
movq %r12, %rcx
subq %r15, %rcx
movq %rcx, 0x38(%rsp)
movq %r15, %rbx
subq %r12, %rbx
addq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rbp
decq %rbp
leaq (%r14,%r12,8), %r12
addq $-0x8, %r12
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x3ac2
movq (%r12,%r14,8), %rsi
addq 0x10(%rsp), %rsi
leaq (%r14,%rbp), %rdx
movq 0x8(%rsp), %rdi
callq 0x3250
decq %r14
jmp 0x3aa1
movq %r13, %r12
subq %r15, %r12
movq 0x18(%rsp), %r14
movq %r14, %rbp
movq 0x20(%rsp), %rbx
cmpq %r13, %r15
je 0x3afe
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rax
subq -0x8(%rax,%r13,8), %rsi
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
callq 0x3250
decq %r13
incq %rbp
jmp 0x3ad5
movq 0x38(%rsp), %rax
notq %rax
addq %rax, %r12
addq %r14, %r12
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3250
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq