// File: 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
movq %rsp, %rbp
subq $0x4890, %rsp           # imm = 0x4890
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, %rsi
leaq -0x18(%rbp), %rdi
callq 0x33a0
leaq -0x4020(%rbp), %rdi
movq %rdi, -0x4870(%rbp)
xorl %esi, %esi
movl $0x4000, %edx           # imm = 0x4000
callq 0x20f0
movq -0x4870(%rbp), %rdx
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x74a0
jmp 0x727e
movq $0x0, -0x4038(%rbp)
cmpq $0x8, -0x4038(%rbp)
jae 0x747d
movq -0x4038(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x4040(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x34b0
movq %rax, -0x4848(%rbp)
movq $0x0, -0x4850(%rbp)
cmpq $0x100, -0x4850(%rbp) # imm = 0x100
jae 0x734d
movq -0x4848(%rbp), %rcx
movq -0x4850(%rbp), %rax
movq %rcx, -0x4840(%rbp,%rax,8)
movq -0x4038(%rbp), %rcx
leaq -0x4020(%rbp), %rax
shlq $0xb, %rcx
addq %rcx, %rax
movq -0x4850(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq $0x3, %rax
addq -0x4848(%rbp), %rax
movq %rax, -0x4848(%rbp)
movq -0x4850(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x4850(%rbp)
jmp 0x72c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4028(%rbp)
movl %eax, -0x402c(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x34e0
jmp 0x748f
movq -0x4848(%rbp), %rax
movq %rax, -0x4878(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x34b0
movq %rax, %rcx
movq -0x4878(%rbp), %rax
movq -0x10(%rbp), %rdx
shlq $0x3, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0x7380
jmp 0x739f
leaq 0x40e1(%rip), %rdi      # 0xb468
leaq 0x40fb(%rip), %rsi      # 0xb489
movl $0x32, %edx
leaq 0x40fa(%rip), %rcx      # 0xb494
callq 0x2110
movq $0x0, -0x4858(%rbp)
movq -0x4858(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x7427
movq -0x8(%rbp), %rax
movq -0x4858(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x4860(%rbp)
movq -0x4860(%rbp), %rax
movq -0x4040(%rbp), %rcx
shrq %cl, %rax
andq $0xff, %rax
movq %rax, -0x4868(%rbp)
movq -0x4860(%rbp), %rcx
movq -0x4868(%rbp), %rdx
movq -0x4840(%rbp,%rdx,8), %rax
movq %rax, %rsi
addq $0x8, %rsi
movq %rsi, -0x4840(%rbp,%rdx,8)
movq %rcx, (%rax)
movq -0x4858(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x4858(%rbp)
jmp 0x73aa
leaq -0x18(%rbp), %rdi
movq %rdi, -0x4888(%rbp)
callq 0x34b0
movq -0x4888(%rbp), %rdi
movq %rax, -0x4880(%rbp)
callq 0x34b0
movq -0x4880(%rbp), %rdi
movq -0x10(%rbp), %rcx
leaq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rdx
callq 0x7540
jmp 0x7464
jmp 0x7466
movq -0x4038(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x4038(%rbp)
jmp 0x7289
leaq -0x18(%rbp), %rdi
callq 0x34e0
addq $0x4890, %rsp           # imm = 0x4890
popq %rbp
retq
movq -0x4028(%rbp), %rdi
callq 0x22c0
nopl (%rax,%rax)