// File: /workspace/github_cpp_makefile_star2_O2/travisdowns[P]sort-bench/radix6.cpp
// Function: radix_sort6(unsigned long*, unsigned long)
void radix_sort6(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);

    uint64_t *from = a, *to = queue_area.get();
    
    for (size_t pass = 0; pass < RADIX_LEVELS; pass++) {

        if (is_trivial(freqs[pass], count)) {
            // this pass would do nothing, just skip it
            continue;
        }

        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 = to;
        for (size_t i = 0; i < RADIX_SIZE; i++) {
            queue_ptrs[i] = next;
            next += freqs[pass][i];
        }

        // 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 = from[i];
            size_t index = (value >> shift) & RADIX_MASK;
            *queue_ptrs[index]++ = value;
        }

        // swap from and to areas
        std::swap(from, to);
    }

    // because of the last swap, the "from" area has the sorted payload: if it's
    // not the original array "a", do a final copy
    if (from != a) {
        std::copy(from, from + count, a);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4818, %rsp           # imm = 0x4818
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rax
shrq $0x3d, %rax
leaq (,%rsi,8), %r14
xorl %ebp, %ebp
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %r14, %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 %r15, %rbp
je 0x396e
movq (%rbx,%rbp,8), %rax
xorl %ecx, %ecx
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x3969
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 0x3941
incq %rbp
jmp 0x3936
xorl %eax, %eax
movq %rbx, %rsi
cmpq $0x8, %rax
je 0x3a18
xorl %ecx, %ecx
cmpq $0x100, %rcx            # imm = 0x100
je 0x399f
movq (%r13,%rcx,8), %rdx
incq %rcx
testq %rdx, %rdx
je 0x397f
cmpq %r15, %rdx
jne 0x39ac
movq %rsi, %rcx
jmp 0x3a06
movq %rsi, %rcx
testq %r15, %r15
je 0x3a06
jmp 0x3a49
movq %r12, %rcx
xorl %edx, %edx
cmpq $0x100, %rdx            # imm = 0x100
je 0x39cd
movq %rcx, 0x10(%rsp,%rdx,8)
movq (%r13,%rdx,8), %rdi
leaq (%rcx,%rdi,8), %rcx
incq %rdx
jmp 0x39b1
leaq (,%rax,8), %rcx
xorl %edx, %edx
cmpq %rdx, %r15
je 0x3a00
movq (%rsi,%rdx,8), %rdi
movq %rdi, %r8
shrq %cl, %r8
movzbl %r8b, %r8d
movq 0x10(%rsp,%r8,8), %r9
leaq 0x8(%r9), %r10
movq %r10, 0x10(%rsp,%r8,8)
movq %rdi, (%r9)
incq %rdx
jmp 0x39d7
movq %r12, %rcx
movq %rsi, %r12
incq %rax
addq $0x800, %r13            # imm = 0x800
movq %rcx, %rsi
jmp 0x3973
testq %r15, %r15
je 0x3a2d
cmpq %rbx, %rsi
je 0x3a2d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x21e0
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 0x1a69(%rip), %rdi      # 0x54b9
leaq 0x1a6d(%rip), %rsi      # 0x54c4
leaq 0x1a71(%rip), %rcx      # 0x54cf
pushq $0x2d
popq %rdx
callq 0x20f0
nop