// File: /workspace/github_cpp_makefile_star2_O3/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 %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 0x3bd2
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 0x3ba4
incq %rax
cmpq %r15, %rax
jne 0x3b9e
xorl %eax, %eax
movq %r12, %rdx
movq %rbx, %rsi
movq %rsi, %rdi
xorl %ecx, %ecx
movq (%r13,%rcx,8), %rsi
testq %rsi, %rsi
jne 0x3c02
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x3bdf
movq %rdi, %rsi
testq %r15, %r15
je 0x3c69
jmp 0x3caa
cmpq %r15, %rsi
jne 0x3c0c
movq %rdi, %rsi
jmp 0x3c69
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 0x3c11
testq %r15, %r15
je 0x3c63
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)
prefetcht0 0x10(%r10)
incq %rsi
cmpq %rsi, %r15
jne 0x3c39
movq %rdx, %rsi
movq %rdi, %rdx
incq %rax
addq $0x800, %r13            # imm = 0x800
cmpq $0x8, %rax
jne 0x3bda
testq %r15, %r15
je 0x3c92
cmpq %rbx, %rsi
je 0x3c92
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 0x1838(%rip), %rdi      # 0x54e9
leaq 0x1869(%rip), %rsi      # 0x5521
leaq 0x1840(%rip), %rcx      # 0x54ff
movl $0x2f, %edx
callq 0x20c0
nopl (%rax)