// 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>::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
movq %rcx, %rax
subq %rdx, %rax
movq %rax, -0x10(%rsp)
incq %rax
movq %rax, %r11
shrq %r11
cmpq $0x2, %rax
movq %rcx, -0x18(%rsp)
movq %r11, -0x28(%rsp)
jae 0x3f7e
xorl %r14d, %r14d
xorl %r11d, %r11d
jmp 0x3fe5
movq (%rdi), %rax
movl 0x10(%rdi), %r10d
movq (%rax,%r10,8), %rax
movss (%rax,%rsi,4), %xmm0
leaq (%rax,%rdx,4), %rbx
cmpq $0x1, %r11
movq %r11, %r15
adcq $0x0, %r15
leaq (%rax,%rcx,4), %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r10d, %r10d
movq %r13, (%r8,%r10,8)
movss (%rbx,%r13,4), %xmm1
xorl %r14d, %r14d
ucomiss %xmm0, %xmm1
seta %r14b
addq %r10, %r14
movq %r13, (%r9,%rbp,8)
xorl %r11d, %r11d
ucomiss (%r12), %xmm0
seta %r11b
addq %rbp, %r11
incq %r13
addq $-0x4, %r12
movq %r11, %rbp
movq %r14, %r10
cmpq %r13, %r15
jne 0x3fa9
movq %r9, -0x20(%rsp)
cmpq %r14, %r11
movq %r14, %r10
cmovbq %r11, %r10
testq %r10, %r10
je 0x404a
movq (%rdi), %r15
movq 0x8(%rdi), %r12
xorl %r13d, %r13d
testq %r12, %r12
je 0x4042
movq -0x18(%rsp), %rbx
movq -0x20(%rsp), %rax
subq (%rax,%r13,8), %rbx
movq (%r8,%r13,8), %rbp
xorl %ecx, %ecx
movq (%r15,%rcx,8), %r9
leaq (%r9,%rbp,4), %rax
movss (%rax,%rdx,4), %xmm0
movss (%r9,%rbx,4), %xmm1
movss %xmm1, (%rax,%rdx,4)
movss %xmm0, (%r9,%rbx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x401c
incq %r13
cmpq %r10, %r13
jne 0x4003
movq -0x28(%rsp), %r9
addq %rdx, %r9
testb $0x1, -0x10(%rsp)
movq %rdi, -0x28(%rsp)
movq %rsi, -0x8(%rsp)
jne 0x407e
movq (%rdi), %rax
movl 0x10(%rdi), %ecx
movq (%rax,%rcx,8), %rax
movss (%rax,%rsi,4), %xmm0
xorl %ebx, %ebx
ucomiss (%rax,%r9,4), %xmm0
seta %bl
jmp 0x4080
xorl %ebx, %ebx
addq %r9, %rbx
movq %r14, %r15
subq %r10, %r15
cmpq %r11, %r14
jbe 0x40e4
leaq (%r8,%r14,8), %rsi
movq -0x28(%rsp), %rax
movq (%rax), %r8
movq 0x8(%rax), %r12
xorl %r13d, %r13d
testq %r12, %r12
je 0x40dc
movq %r13, %rbp
notq %rbp
movq (%rsi,%rbp,8), %rdi
xorl %eax, %eax
movq (%r8,%rax,8), %rcx
leaq (%rcx,%rdi,4), %r9
leaq (%rcx,%rbx,4), %rcx
movss (%r9,%rdx,4), %xmm0
movss (%rcx,%rbp,4), %xmm1
movss %xmm1, (%r9,%rdx,4)
movss %xmm0, (%rcx,%rbp,4)
incq %rax
cmpq %rax, %r12
jne 0x40b2
incq %r13
cmpq %r15, %r13
jne 0x40a1
movq %r11, %rax
subq %r10, %rax
cmpq %r14, %r11
jbe 0x4155
movq -0x20(%rsp), %rcx
leaq (%rcx,%r11,8), %r8
movq -0x28(%rsp), %rcx
movq (%rcx), %r9
movq 0x8(%rcx), %rdx
xorl %r10d, %r10d
movq -0x18(%rsp), %r14
testq %rdx, %rdx
je 0x4146
movq %r10, %rcx
notq %rcx
movq %r14, %rdi
subq (%r8,%rcx,8), %rdi
xorl %ecx, %ecx
movq (%r9,%rcx,8), %rsi
leaq (%rsi,%rbx,4), %r11
movss (%rsi,%rdi,4), %xmm0
movss (%r11,%r10,4), %xmm1
movss %xmm1, (%rsi,%rdi,4)
movss %xmm0, (%r11,%r10,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x4120
incq %r10
cmpq %rax, %r10
jne 0x410c
movq -0x28(%rsp), %rcx
jmp 0x415e
movq -0x28(%rsp), %rcx
movq 0x8(%rcx), %rdx
notq %r15
addq %r15, %rax
addq %rbx, %rax
testq %rdx, %rdx
movq -0x8(%rsp), %r8
je 0x4198
movq (%rcx), %rcx
xorl %edi, %edi
movq (%rcx,%rdi,8), %rsi
movss (%rsi,%rax,4), %xmm0
movss (%rsi,%r8,4), %xmm1
movss %xmm1, (%rsi,%rax,4)
movss %xmm0, (%rsi,%r8,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x4176
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop