// File: /workspace/github_cpp_makefile_star2_O2/travisdowns[P]sort-bench/radix4.cpp
// Function: radix_sort4(unsigned long*, unsigned long)
void radix_sort4(uint64_t *a, size_t count)
{
    std::unique_ptr<uint64_t[]> queue_area(new uint64_t[count]);

    freq_array_type freqs = {};
    count_frequency(a, count, freqs);
    
    for (size_t pass = 0; pass < RADIX_LEVELS; pass++) {
        uint64_t shift = pass * RADIX_BITS;

        // array of pointers to the current position in each queue, which we set up based on the
        // known final sizes of each queue (i.e., "tighly packed")
        uint64_t * queue_ptrs[RADIX_SIZE], * next = queue_area.get();
        for (size_t i = 0; i < RADIX_SIZE; i++) {
            queue_ptrs[i] = next;
            next += freqs[pass][i];
        }

        // sanity check: final pointer should point exactly to the end of the temp area
        assert(next == queue_area.get() + count);

        // copy each element into the appropriate queue based on the current RADIX_BITS sized
        // "digit" within it
        for (size_t i = 0; i < count; i++) {
            size_t value = a[i];
            size_t index = (value >> shift) & RADIX_MASK;
            *queue_ptrs[index]++ = value;
        }

        // copy all the queues back over top of the original array in order
        // since the queues are tighly packed and already in the right order
        // we can accomplish this with a single linear copy operation
        std::copy(queue_area.get(), queue_area.get() + count, a);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4818, %rsp           # imm = 0x4818
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rax
shrq $0x3d, %rax
leaq (,%rsi,8), %rcx
xorl %r15d, %r15d
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
movq %rcx, (%rsp)
orq %rcx, %rdi
callq 0x2030
movq %rax, %r12
movq %rax, 0x8(%rsp)
leaq 0x810(%rsp), %r13
movl $0x4000, %edx           # imm = 0x4000
movq %r13, %rdi
xorl %esi, %esi
callq 0x20e0
cmpq %rbx, %r15
je 0x36fb
movq (%r14,%r15,8), %rax
xorl %ecx, %ecx
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x36f6
movzbl %al, %edx
leaq (%rsp,%rdx,8), %rdx
addq $0x810, %rdx            # imm = 0x810
incq (%rcx,%rdx)
shrq $0x8, %rax
addq $0x800, %rcx            # imm = 0x800
jmp 0x36ce
incq %r15
jmp 0x36c3
leaq (%r12,%rbx,8), %rbp
xorl %r15d, %r15d
cmpq $0x8, %r15
je 0x3781
movq %r12, %rax
xorl %ecx, %ecx
cmpq $0x100, %rcx            # imm = 0x100
je 0x3729
movq %rax, 0x10(%rsp,%rcx,8)
movq (%r13,%rcx,8), %rdx
leaq (%rax,%rdx,8), %rax
incq %rcx
jmp 0x370d
cmpq %rbp, %rax
jne 0x379d
leaq (,%r15,8), %rcx
xorl %eax, %eax
cmpq %rax, %rbx
je 0x3761
movq (%r14,%rax,8), %rdx
movq %rdx, %rsi
shrq %cl, %rsi
movzbl %sil, %esi
movq 0x10(%rsp,%rsi,8), %rdi
leaq 0x8(%rdi), %r8
movq %r8, 0x10(%rsp,%rsi,8)
movq %rdx, (%rdi)
incq %rax
jmp 0x3738
testq %rbx, %rbx
je 0x3775
movq %r14, %rdi
movq %r12, %rsi
movq (%rsp), %rdx
callq 0x2130
incq %r15
addq $0x800, %r13            # imm = 0x800
jmp 0x3702
leaq 0x8(%rsp), %rdi
callq 0x294a
addq $0x4818, %rsp           # imm = 0x4818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1cc4(%rip), %rdi      # 0x5468
leaq 0x1cde(%rip), %rsi      # 0x5489
leaq 0x1ce2(%rip), %rcx      # 0x5494
pushq $0x32
popq %rdx
callq 0x20f0
nop