// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4800, %rsp           # imm = 0x4800
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rax
shrq $0x3d, %rax
leaq (,%rsi,8), %r14
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %r14, %rdi
callq 0x2030
movq %rax, %r12
leaq 0x800(%rsp), %r13
movl $0x4000, %edx           # imm = 0x4000
movq %r13, %rdi
xorl %esi, %esi
callq 0x20b0
testq %r15, %r15
je 0x3a56
xorl %eax, %eax
movq (%rbx,%rax,8), %rcx
xorl %edx, %edx
movzbl %cl, %esi
leaq (%rsp,%rsi,8), %rsi
addq $0x800, %rsi            # imm = 0x800
incq (%rdx,%rsi)
shrq $0x8, %rcx
addq $0x800, %rdx            # imm = 0x800
cmpq $0x4000, %rdx           # imm = 0x4000
jne 0x3a28
incq %rax
cmpq %r15, %rax
jne 0x3a22
xorl %eax, %eax
movq %r12, %rdx
movq %rbx, %rsi
movq %rsi, %rdi
xorl %ecx, %ecx
movq (%r13,%rcx,8), %rsi
testq %rsi, %rsi
jne 0x3a86
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x3a63
movq %rdi, %rsi
testq %r15, %r15
je 0x3ae8
jmp 0x3b29
cmpq %r15, %rsi
jne 0x3a90
movq %rdi, %rsi
jmp 0x3ae8
xorl %ecx, %ecx
movq %rdx, %rsi
movq %rsi, (%rsp,%rcx,8)
movq (%r13,%rcx,8), %r8
leaq (%rsi,%r8,8), %rsi
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x3a95
testq %r15, %r15
je 0x3ae2
leaq (,%rax,8), %rcx
xorl %esi, %esi
movq (%rdi,%rsi,8), %r8
movq %r8, %r9
shrq %cl, %r9
movzbl %r9b, %r9d
movq (%rsp,%r9,8), %r10
leaq 0x8(%r10), %r11
movq %r11, (%rsp,%r9,8)
movq %r8, (%r10)
incq %rsi
cmpq %rsi, %r15
jne 0x3abd
movq %rdx, %rsi
movq %rdi, %rdx
incq %rax
addq $0x800, %r13            # imm = 0x800
cmpq $0x8, %rax
jne 0x3a5e
testq %r15, %r15
je 0x3b11
cmpq %rbx, %rsi
je 0x3b11
movq %rbx, %rdi
movq %r14, %rdx
callq 0x21b0
movq %r12, %rdi
addq $0x4800, %rsp           # imm = 0x4800
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x2160
leaq 0x19b9(%rip), %rdi      # 0x54e9
leaq 0x19bd(%rip), %rsi      # 0x54f4
leaq 0x19c1(%rip), %rcx      # 0x54ff
movl $0x2d, %edx
callq 0x20c0