// File: /workspace/github_cpp_makefile_star2_O3/Baobaobear[P]sort/./grailsort.hpp
// Function: void Mrrl::grail_commonSort<baobao::TestClass*, baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, baobao::TestClass*, int, std::less<baobao::TestClass>)
void grail_commonSort(RandomAccessIterator beg, RandomAccessIterator end, RandomAccessBufferIterator extbuf, int LExtBuf, Comp compare) {
    int lblock, nkeys, findkeys;
    bool havebuf;

    if (end - beg < 16) {
        grail_SortIns(beg, end, compare);
        return;
    }

    lblock = 1;
    while (lblock*lblock < end - beg) lblock *= 2;
    nkeys = (int)((end - beg - 1) / lblock + 1);
    findkeys = (int)(grail_FindKeys(beg, end, beg + nkeys + lblock, compare) - beg);
    havebuf = true;
    if (findkeys < nkeys + lblock) {
        if (findkeys < 4) {
            grail_LazyStableSort(beg, end, compare);
            return;
        }
        nkeys = lblock;
        while (nkeys > findkeys) nkeys /= 2;
        havebuf = false;
        lblock = 0;
    }
    RandomAccessIterator ptr = beg + lblock + nkeys;
    int cbuf = havebuf ? lblock : nkeys;
    if (havebuf) grail_BuildBlocks(ptr, end, cbuf, extbuf, LExtBuf, compare);
    else grail_BuildBlocks(ptr, end, cbuf, (RandomAccessBufferIterator)NULL, 0, compare);

    // 2*cbuf are built
    while (end - ptr > (cbuf *= 2)) {
        int lb = lblock;
        bool chavebuf = havebuf;
        if (!havebuf) {
            if (nkeys > 4 && nkeys / 8 * nkeys >= cbuf) {
                lb = nkeys / 2;
                chavebuf = true;
            }
            else {
                int nk = 1;
                double s = (double)cbuf * findkeys / 2;
                while (nk < nkeys && s > 0.9) {
                    nk *= 2; s /= 8;
                }
                lb = (2 * cbuf) / nk;
            }
        }
        else {
#if 0
            if (LExtBuf != 0) {
                while (lb > LExtBuf && lb*lb > 2 * cbuf) lb /= 2;  // set size of block close to sqrt(new_block_length)
            }
#endif
        }
        grail_CombineBlocks(beg, ptr, end, cbuf, lb, chavebuf, (RandomAccessBufferIterator)(chavebuf && lb <= LExtBuf ? extbuf : NULL), compare);
    }
    grail_SortIns(beg, ptr, compare);
    grail_MergeWithoutBuffer(beg, ptr, end, compare);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, 0x30(%rsp)
movq %rdi, %r15
movq %rsi, %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq $0x10, %rax
jge 0xb780
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb99a
movl $0x1, %r13d
movl %r13d, %r12d
movl %r13d, %ecx
imull %ecx, %ecx
leal (%r12,%r12), %r13d
cmpq %rcx, %rax
ja 0xb786
movl %r12d, %r14d
decl %eax
xorl %edx, %edx
divl %r12d
movq %rax, 0x10(%rsp)
leal 0x1(%rax), %ebp
movslq %ebp, %rax
shlq $0x5, %rax
shlq $0x5, %r14
addq %r15, %r14
addq %rax, %r14
movq %r15, %rdi
movq %rsi, %rbx
movq %r14, %rdx
callq 0xba77
movq %r15, %rcx
movq %rax, %r15
movq %rcx, 0x18(%rsp)
subq %rcx, %r15
shrq $0x5, %r15
movq 0x10(%rsp), %rax
addl %r12d, %eax
incl %eax
movl %eax, 0x2c(%rsp)
cmpl %r15d, %eax
jle 0xb84a
cmpl $0x4, %r15d
jge 0xb80f
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbba6
movl %r12d, %ebp
shrl %r12d
cmpl %r15d, %ebp
jg 0xb80f
movslq %ebp, %r14
shlq $0x5, %r14
addq 0x18(%rsp), %r14
xorl %r12d, %r12d
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xbd0d
movq %r14, %rcx
leal (,%rbp,2), %r13d
movl %ebp, %eax
jmp 0xb868
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movq 0x30(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0xbd0d
movl %r12d, %eax
movq %r14, %rcx
movq %rbx, 0x10(%rsp)
movq %rbx, %r14
movq %rcx, 0x38(%rsp)
subq %rcx, %r14
sarq $0x5, %r14
movslq %r13d, %rcx
cmpq %rcx, %r14
jle 0xb967
movl %ebp, %ecx
shrl $0x3, %ecx
imull %ebp, %ecx
movl %ecx, 0x28(%rsp)
movl %ebp, %ecx
shrl %ecx
movl %ecx, 0x24(%rsp)
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x8781(%rip), %xmm1     # 0x14030
movsd 0x8781(%rip), %xmm2     # 0x14038
movl %r13d, %ebx
movl %r12d, %r8d
cmpl %r15d, 0x2c(%rsp)
jle 0xb8d4
cmpl $0x5, %ebp
jl 0xb8eb
movl 0x24(%rsp), %r8d
cmpl %ebx, 0x28(%rsp)
jl 0xb8eb
cmpl 0xc(%rsp), %r8d
movq 0x30(%rsp), %rcx
movl $0x0, %eax
cmovgq %rax, %rcx
movb $0x1, %al
jmp 0xb927
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x40(%rsp), %xmm0
mulsd 0x8728(%rip), %xmm0     # 0x14028
movl $0x1, %ecx
cmpl $0x2, %ebp
jl 0xb91a
ucomisd %xmm1, %xmm0
jbe 0xb91a
addl %ecx, %ecx
mulsd %xmm2, %xmm0
cmpl %ebp, %ecx
jl 0xb90a
shll $0x2, %eax
cltd
idivl %ecx
movl %eax, %r8d
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, (%rsp)
movzbl %al, %r9d
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl %ebx, %ecx
callq 0xc387
leal (%rbx,%rbx), %r13d
movslq %r13d, %rcx
movl %ebx, %eax
cmpq %rcx, %r14
movsd 0x86d7(%rip), %xmm1     # 0x14030
movsd 0x86d7(%rip), %xmm2     # 0x14038
jg 0xb8b7
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %r14
movq %r14, %rsi
callq 0xb99a
movq %rbx, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc9ca