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

    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;
            __builtin_prefetch(queue_ptrs[index] + 1);
        }

        // 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 0x3afa
movq (%rbx,%rbp,8), %rax
xorl %ecx, %ecx
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x3af5
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 0x3acd
incq %rbp
jmp 0x3ac2
xorl %eax, %eax
movq %rbx, %rsi
cmpq $0x8, %rax
je 0x3ba9
xorl %ecx, %ecx
cmpq $0x100, %rcx            # imm = 0x100
je 0x3b2b
movq (%r13,%rcx,8), %rdx
incq %rcx
testq %rdx, %rdx
je 0x3b0b
cmpq %r15, %rdx
jne 0x3b38
movq %rsi, %rcx
jmp 0x3b97
movq %rsi, %rcx
testq %r15, %r15
je 0x3b97
jmp 0x3bda
movq %r12, %rcx
xorl %edx, %edx
cmpq $0x100, %rdx            # imm = 0x100
je 0x3b59
movq %rcx, 0x10(%rsp,%rdx,8)
movq (%r13,%rdx,8), %rdi
leaq (%rcx,%rdi,8), %rcx
incq %rdx
jmp 0x3b3d
leaq (,%rax,8), %rcx
xorl %edx, %edx
cmpq %rdx, %r15
je 0x3b91
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)
prefetcht0 0x10(%r9)
incq %rdx
jmp 0x3b63
movq %r12, %rcx
movq %rsi, %r12
incq %rax
addq $0x800, %r13            # imm = 0x800
movq %rcx, %rsi
jmp 0x3aff
testq %r15, %r15
je 0x3bbe
cmpq %rbx, %rsi
je 0x3bbe
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 0x18d8(%rip), %rdi      # 0x54b9
leaq 0x1909(%rip), %rsi      # 0x54f1
leaq 0x18e0(%rip), %rcx      # 0x54cf
pushq $0x2f
popq %rdx
callq 0x20f0
nop