// File: 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
andq $-0x40, %rsp
subq $0x500, %rsp            # imm = 0x500
movq %rdi, 0x4e8(%rsp)
movq %rsi, 0x4e0(%rsp)
movq %rdx, 0x4d8(%rsp)
movq %rcx, 0x4d0(%rsp)
movq 0x4e8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x80, 0x4c8(%rsp)
movq 0x4d0(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x4c0(%rsp)
movq $0x0, 0xb8(%rsp)
movq $0x0, 0xb0(%rsp)
movq $0x0, 0xa8(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0x4c0(%rsp), %rax
subq 0x4d8(%rsp), %rax
addq $0x1, %rax
cmpq $0x80, %rax
jbe 0x4eeb
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x4c01
cmpq $0x0, 0xb0(%rsp)
jne 0x4c01
movq $0x0, 0x98(%rsp)
cmpq $0x40, 0x98(%rsp)
jae 0x4bfc
movq $0x0, 0x90(%rsp)
cmpq $0x10, 0x90(%rsp)
jae 0x4bf7
movq 0x48(%rsp), %rdi
movq 0x98(%rsp), %rcx
movq 0xb8(%rsp), %rax
movq %rcx, 0x2c0(%rsp,%rax,8)
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0xc0(%rsp,%rax,8)
movq 0x4e0(%rsp), %rsi
callq 0x4200
movq 0x48(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x40(%rsp)
movq 0x4d8(%rsp), %rsi
addq 0x98(%rsp), %rsi
callq 0x4200
movss 0x40(%rsp), %xmm1
movq 0x48(%rsp), %rdi
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
addq 0xb8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x4c0(%rsp), %rsi
subq 0x98(%rsp), %rsi
callq 0x4200
movq 0x48(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x44(%rsp)
movq 0x4e0(%rsp), %rsi
callq 0x4200
movss 0x44(%rsp), %xmm1
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x4ad5
jmp 0x4aba
jmp 0x4dc2
cmpq $0x0, 0xb8(%rsp)
jne 0x4cf0
movq $0x0, 0x88(%rsp)
cmpq $0x40, 0x88(%rsp)
jae 0x4ceb
movq $0x0, 0x80(%rsp)
cmpq $0x10, 0x80(%rsp)
jae 0x4ce6
movq 0x48(%rsp), %rdi
movq 0x88(%rsp), %rcx
movq 0xb8(%rsp), %rax
movq %rcx, 0x2c0(%rsp,%rax,8)
movq 0x4e0(%rsp), %rsi
callq 0x4200
movq 0x48(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x4d8(%rsp), %rsi
addq 0x88(%rsp), %rsi
callq 0x4200
movss 0x3c(%rsp), %xmm1
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
addq 0xb8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x4c37
jmp 0x4c1c
jmp 0x4dc0
cmpq $0x0, 0xb0(%rsp)
jne 0x4dbe
movq $0x0, 0x78(%rsp)
cmpq $0x40, 0x78(%rsp)
jae 0x4dbc
movq $0x0, 0x70(%rsp)
cmpq $0x10, 0x70(%rsp)
jae 0x4db7
movq 0x48(%rsp), %rdi
movq 0x78(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0xc0(%rsp,%rax,8)
movq 0x4c0(%rsp), %rsi
subq 0x78(%rsp), %rsi
callq 0x4200
movq 0x48(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x4e0(%rsp), %rsi
callq 0x4200
movss 0x38(%rsp), %xmm1
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x4d1d
jmp 0x4d08
jmp 0x4dbe
jmp 0x4dc0
jmp 0x4dc2
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x53a0
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x68(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jae 0x4e56
movq 0x48(%rsp), %rdi
movq 0x4d8(%rsp), %rsi
movq 0xb8(%rsp), %rax
subq $0x1, %rax
subq 0x68(%rsp), %rax
addq 0x2c0(%rsp,%rax,8), %rsi
movq 0x4c0(%rsp), %rdx
movq 0xb0(%rsp), %rax
subq $0x1, %rax
subq 0x68(%rsp), %rax
subq 0xc0(%rsp,%rax,8), %rdx
callq 0x4230
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x4deb
movq 0xa8(%rsp), %rcx
movq 0xb8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0xb8(%rsp)
movq 0xa8(%rsp), %rcx
movq 0xb0(%rsp), %rax
subq %rcx, %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb8(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
subl %ecx, %eax
cltq
andq $0x40, %rax
addq 0x4d8(%rsp), %rax
movq %rax, 0x4d8(%rsp)
cmpq $0x0, 0xb0(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
subl %ecx, %eax
movslq %eax, %rcx
andq $0x40, %rcx
movq 0x4c0(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x4c0(%rsp)
jmp 0x4a5c
cmpq $0x0, 0xb8(%rsp)
setne %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
subl %ecx, %eax
cltq
andq $0x40, %rax
addq 0x4d8(%rsp), %rax
movq %rax, 0x4d8(%rsp)
cmpq $0x0, 0xb0(%rsp)
setne %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
subl %ecx, %eax
movslq %eax, %rcx
andq $0x40, %rcx
movq 0x4c0(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x4c0(%rsp)
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jae 0x4fad
movq 0x48(%rsp), %rdi
movq 0x4d8(%rsp), %rsi
subq 0x60(%rsp), %rsi
subq $0x1, %rsi
movq 0x4d8(%rsp), %rdx
subq $0x40, %rdx
movq 0xb8(%rsp), %rax
subq $0x1, %rax
subq 0x60(%rsp), %rax
addq 0x2c0(%rsp,%rax,8), %rdx
callq 0x4230
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x4f4e
movq $0x0, 0x58(%rsp)
movq 0x58(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jae 0x5015
movq 0x48(%rsp), %rdi
movq 0x4c0(%rsp), %rsi
addq $0x1, %rsi
addq 0x58(%rsp), %rsi
movq 0x4c0(%rsp), %rdx
addq $0x40, %rdx
movq 0xb0(%rsp), %rax
subq $0x1, %rax
subq 0x58(%rsp), %rax
subq 0xc0(%rsp,%rax,8), %rdx
callq 0x4230
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x4fb6
movq 0x48(%rsp), %rdi
movq 0xb8(%rsp), %rcx
movq 0x4d8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x4d8(%rsp)
movq 0xb0(%rsp), %rax
addq 0x4c0(%rsp), %rax
movq %rax, 0x4c0(%rsp)
movq 0x4e0(%rsp), %rsi
movq 0x4d8(%rsp), %rdx
movq 0x4c0(%rsp), %rcx
leaq 0x2c0(%rsp), %r8
leaq 0xc0(%rsp), %r9
callq 0x5090
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)