// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %ecx, 0x14(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdi, %r13
movq %rsi, %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq $0x10, %rax
jge 0x91ea
movq %r13, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x93cf
pushq $0x1
popq %rcx
movl %ecx, %r12d
movl %ecx, %edx
imull %edx, %edx
leal (%r12,%r12), %ecx
cmpq %rdx, %rax
ja 0x91ed
movl %r12d, %r14d
decl %eax
xorl %edx, %edx
divl %r12d
movq %rax, 0x8(%rsp)
leal 0x1(%rax), %ebp
movslq %ebp, %rax
shlq $0x5, %rax
shlq $0x5, %r14
addq %r13, %r14
addq %rax, %r14
movq %r13, %rdi
movq %rsi, %rbx
movq %r14, %rdx
callq 0x947e
movq %rax, %r15
subq %r13, %r15
shrq $0x5, %r15
movq 0x8(%rsp), %rax
addl %r12d, %eax
incl %eax
movl %eax, 0x1c(%rsp)
cmpl %r15d, %eax
jle 0x92a6
cmpl $0x4, %r15d
jge 0x926b
movq %r13, %rdi
movq %rbx, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x957b
movl %r12d, %ebp
shrl %r12d
cmpl %r15d, %ebp
jg 0x926b
movslq %ebp, %r14
shlq $0x5, %r14
movq %r13, 0x8(%rsp)
addq %r13, %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x965c
movq %r14, %rcx
movl %r12d, 0x10(%rsp)
xorl %r12d, %r12d
movl %ebp, %eax
jmp 0x92d1
movq %r13, 0x8(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movq 0x20(%rsp), %rcx
movl 0x14(%rsp), %r8d
callq 0x965c
movl %ebp, %eax
shrl %eax
movl %eax, 0x10(%rsp)
movl %r12d, %eax
movq %r14, %rcx
movq %rbx, 0x28(%rsp)
movq %rbx, %r14
movq %rcx, %r13
subq %rcx, %r14
sarq $0x5, %r14
movl %ebp, %ecx
shrl $0x3, %ecx
imull %ebp, %ecx
movl %ecx, 0x18(%rsp)
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x5d2e(%rip), %xmm1     # 0xf030
movsd 0x5d2e(%rip), %xmm2     # 0xf038
leal (%rax,%rax), %ebx
movslq %ebx, %rcx
cmpq %rcx, %r14
jle 0x93a1
movl %r12d, %r8d
cmpl %r15d, 0x1c(%rsp)
jle 0x9333
cmpl $0x5, %ebp
jl 0x934a
movl 0x10(%rsp), %r8d
cmpl %ebx, 0x18(%rsp)
jl 0x934a
cmpl 0x14(%rsp), %r8d
movq 0x20(%rsp), %rcx
movl $0x0, %eax
cmovgq %rax, %rcx
movb $0x1, %al
jmp 0x937e
cvtsi2sd %ebx, %xmm0
mulsd 0x30(%rsp), %xmm0
mulsd 0x5ccc(%rip), %xmm0     # 0xf028
pushq $0x1
popq %rcx
cmpl %ebp, %ecx
jge 0x9371
ucomisd %xmm1, %xmm0
jbe 0x9371
addl %ecx, %ecx
mulsd %xmm2, %xmm0
jmp 0x935f
shll $0x2, %eax
cltd
idivl %ecx
movl %eax, %r8d
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, (%rsp)
movzbl %al, %r9d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movl %ebx, %ecx
callq 0x9a8a
movl %ebx, %eax
jmp 0x92fa
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x93cf
movq %rbx, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9ee4