// File: Baobaobear[P]sort/./sortlib.hpp
// Function: void baobao::internal::radix_sort_msd_in_place<baobao::TestClass*, baobao::internal::radix_get_index<baobao::TestClass, unsigned int>, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, baobao::internal::radix_get_index<baobao::TestClass, unsigned int>, unsigned int, std::less<baobao::TestClass>)
void radix_sort_msd_in_place(RandomAccessIterator beg, RandomAccessIterator end, RadixIndexFunc get_index, uint32_t offset, Comp compare)
{
    if (end - beg < qsort_insertion_sort_threshold)
    {
        internal::q_insert_sort(beg, end, compare);
        return;
    }
    uint32_t counter[256] = { 0 }, not_empty_count = 0;
    RandomAccessIterator _split_iter[257], *split_iter = &_split_iter[1];
    for (RandomAccessIterator i = beg; i < end; ++i)
    {
        uint32_t index = (get_index(*i) >> offset) & 0xff;
        counter[index]++;
    }
    _split_iter[0] = beg;
    for (size_t i = 0; i < 256; ++i)
    {
        not_empty_count += counter[i] > 0;
        split_iter[i] = split_iter[i - 1] + counter[i];
    }
    if (not_empty_count > 1)
    {
        RandomAccessIterator sort_iter[256];
        memcpy(sort_iter, _split_iter, sizeof(sort_iter));
        for (size_t i = 0; i < 256; ++i)
        {
            if (split_iter[i] - split_iter[i - 1] > 0)
            {
                for (RandomAccessIterator start_i = sort_iter[i]; start_i < split_iter[i]; ++start_i)
                {
                    if (start_i < sort_iter[i]) start_i = sort_iter[i];

                    RandomAccessIterator cur = start_i;
                    while (true)
                    {
                        uint32_t index = (get_index(*cur) >> offset) & 0xff;
                        if (i == index)
                        {
                            sort_iter[index]++;
                            break;
                        }

                        std::swap(*cur, *sort_iter[index]);
                        sort_iter[index]++;
                    }
                }
                if (split_iter[i] - split_iter[i - 1] > 1)
                {
                    if (offset >= 8)
                    {
                        radix_sort_msd_in_place(split_iter[i - 1], split_iter[i], get_index, offset - 8, compare);
                    }
                    else
                    {
                        internal::quick_sort(split_iter[i - 1], split_iter[i], compare);
                    }
                }
            }
        }
    }
    else
    {
        if (offset >= 8)
        {
            radix_sort_msd_in_place(beg, end, get_index, offset - 8, compare);
        }
        else
        {
            internal::quick_sort(beg, end, compare);
        }
    }
}

// Assembly:
subq $0x1488, %rsp           # imm = 0x1488
movq %rdi, 0x1478(%rsp)
movq %rsi, 0x1470(%rsp)
movl %edx, 0x146c(%rsp)
movq 0x1470(%rsp), %rax
movq 0x1478(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
cmpq $0x28, %rax
jge 0x14d65
movq 0x1478(%rsp), %rdi
movq 0x1470(%rsp), %rsi
callq 0x5b10
jmp 0x15108
leaq 0x1060(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x20f0
movl $0x0, 0x105c(%rsp)
leaq 0x850(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x848(%rsp)
movq 0x1478(%rsp), %rax
movq %rax, 0x840(%rsp)
movq 0x840(%rsp), %rax
cmpq 0x1470(%rsp), %rax
jae 0x14e12
movq 0x840(%rsp), %rsi
leaq 0x1487(%rsp), %rdi
callq 0x14cf0
movl 0x146c(%rsp), %ecx
shrl %cl, %eax
andl $0xff, %eax
movl %eax, 0x83c(%rsp)
movl 0x83c(%rsp), %eax
movl 0x1060(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0x1060(%rsp,%rax,4)
movq 0x840(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x840(%rsp)
jmp 0x14da8
movq 0x1478(%rsp), %rax
movq %rax, 0x850(%rsp)
movq $0x0, 0x830(%rsp)
cmpq $0x100, 0x830(%rsp) # imm = 0x100
jae 0x14ec1
movq 0x830(%rsp), %rax
cmpl $0x0, 0x1060(%rsp,%rax,4)
seta %al
andb $0x1, %al
movzbl %al, %eax
addl 0x105c(%rsp), %eax
movl %eax, 0x105c(%rsp)
movq 0x848(%rsp), %rax
movq 0x830(%rsp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rdx
movq 0x830(%rsp), %rax
movl 0x1060(%rsp,%rax,4), %eax
shlq $0x5, %rax
addq %rax, %rdx
movq 0x848(%rsp), %rax
movq 0x830(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x830(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x830(%rsp)
jmp 0x14e2e
cmpl $0x1, 0x105c(%rsp)
jbe 0x150c6
leaq 0x30(%rsp), %rdi
leaq 0x850(%rsp), %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x2180
movq $0x0, 0x28(%rsp)
cmpq $0x100, 0x28(%rsp)      # imm = 0x100
jae 0x150c4
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x848(%rsp), %rcx
movq 0x28(%rsp), %rdx
subq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
subq %rcx, %rax
sarq $0x5, %rax
cmpq $0x0, %rax
jle 0x150af
movq 0x28(%rsp), %rax
movq 0x30(%rsp,%rax,8), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq 0x848(%rsp), %rcx
movq 0x28(%rsp), %rdx
cmpq (%rcx,%rdx,8), %rax
jae 0x1500c
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq 0x30(%rsp,%rcx,8), %rax
jae 0x14f80
movq 0x28(%rsp), %rax
movq 0x30(%rsp,%rax,8), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rsi
leaq 0x1487(%rsp), %rdi
callq 0x14cf0
movl 0x146c(%rsp), %ecx
shrl %cl, %eax
andl $0xff, %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movl 0x14(%rsp), %ecx
cmpq %rcx, %rax
jne 0x14fd0
movl 0x14(%rsp), %eax
movq 0x30(%rsp,%rax,8), %rcx
addq $0x20, %rcx
movq %rcx, 0x30(%rsp,%rax,8)
jmp 0x14ff7
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %eax
movq 0x30(%rsp,%rax,8), %rsi
callq 0x8660
movl 0x14(%rsp), %eax
movq 0x30(%rsp,%rax,8), %rcx
addq $0x20, %rcx
movq %rcx, 0x30(%rsp,%rax,8)
jmp 0x14f8a
jmp 0x14ff9
movq 0x20(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x20(%rsp)
jmp 0x14f44
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x848(%rsp), %rcx
movq 0x28(%rsp), %rdx
subq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
subq %rcx, %rax
sarq $0x5, %rax
cmpq $0x1, %rax
jle 0x150ad
cmpl $0x8, 0x146c(%rsp)
jb 0x15080
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rdi
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movl 0x146c(%rsp), %edx
subl $0x8, %edx
callq 0x14d10
jmp 0x150ab
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rdi
movq 0x848(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xfe40
jmp 0x150ad
jmp 0x150af
jmp 0x150b1
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x14eef
jmp 0x15108
cmpl $0x8, 0x146c(%rsp)
jb 0x150f1
movq 0x1478(%rsp), %rdi
movq 0x1470(%rsp), %rsi
movl 0x146c(%rsp), %edx
subl $0x8, %edx
callq 0x14d10
jmp 0x15106
movq 0x1478(%rsp), %rdi
movq 0x1470(%rsp), %rsi
callq 0xfe40
jmp 0x15108
addq $0x1488, %rsp           # imm = 0x1488
retq