// File: 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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movl %ecx, 0x84(%rsp)
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
cmpq $0x10, %rax
jge 0x151cd
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x15590
jmp 0x1557f
movl $0x1, 0x80(%rsp)
movl 0x80(%rsp), %eax
imull 0x80(%rsp), %eax
cltq
movq 0x90(%rsp), %rcx
movq 0x98(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %rcx, %rax
jge 0x15217
movl 0x80(%rsp), %eax
shll %eax
movl %eax, 0x80(%rsp)
jmp 0x151d8
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
subq $0x1, %rax
movslq 0x80(%rsp), %rcx
cqto
idivq %rcx
addq $0x1, %rax
movl %eax, 0x7c(%rsp)
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movslq 0x7c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x80(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
callq 0x15690
movq 0x98(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl %eax, 0x78(%rsp)
movb $0x1, 0x77(%rsp)
movl 0x78(%rsp), %eax
movl 0x7c(%rsp), %ecx
addl 0x80(%rsp), %ecx
cmpl %ecx, %eax
jge 0x15302
cmpl $0x4, 0x78(%rsp)
jge 0x152cb
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x15810
jmp 0x1557f
movl 0x80(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x78(%rsp), %eax
jle 0x152f2
movl 0x7c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x7c(%rsp)
jmp 0x152d6
movb $0x0, 0x77(%rsp)
movl $0x0, 0x80(%rsp)
movq 0x98(%rsp), %rax
movslq 0x80(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
testb $0x1, 0x77(%rsp)
je 0x1533e
movl 0x80(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x15346
movl 0x7c(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x64(%rsp)
testb $0x1, 0x77(%rsp)
je 0x1537d
movq 0x68(%rsp), %rdi
movq 0x90(%rsp), %rsi
movl 0x64(%rsp), %edx
movq 0x88(%rsp), %rcx
movl 0x84(%rsp), %r8d
callq 0x15980
jmp 0x1539a
movq 0x68(%rsp), %rdi
movq 0x90(%rsp), %rsi
movl 0x64(%rsp), %edx
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x15980
jmp 0x1539c
movq 0x90(%rsp), %rax
movq 0x68(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl 0x64(%rsp), %ecx
shll %ecx
movl %ecx, 0x64(%rsp)
movslq %ecx, %rcx
cmpq %rcx, %rax
jle 0x15553
movl 0x80(%rsp), %eax
movl %eax, 0x5c(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
movb %al, 0x5b(%rsp)
testb $0x1, 0x77(%rsp)
jne 0x154b0
cmpl $0x4, 0x7c(%rsp)
jle 0x1541e
movl 0x7c(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
imull 0x7c(%rsp), %eax
cmpl 0x64(%rsp), %eax
jl 0x1541e
movl 0x7c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x5c(%rsp)
movb $0x1, 0x5b(%rsp)
jmp 0x154ae
movl $0x1, 0x54(%rsp)
cvtsi2sdl 0x64(%rsp), %xmm0
cvtsi2sdl 0x78(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x11bea(%rip), %xmm1    # 0x27028
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x54(%rsp), %ecx
xorl %eax, %eax
cmpl 0x7c(%rsp), %ecx
movb %al, 0x3f(%rsp)
jge 0x15471
movsd 0x48(%rsp), %xmm0
movsd 0x11bca(%rip), %xmm1    # 0x27030
ucomisd %xmm1, %xmm0
seta %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x1547b
jmp 0x1549f
movl 0x54(%rsp), %eax
shll %eax
movl %eax, 0x54(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x11ba5(%rip), %xmm1    # 0x27038
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x15448
movl 0x64(%rsp), %eax
shll %eax
cltd
idivl 0x54(%rsp)
movl %eax, 0x5c(%rsp)
jmp 0x154b2
jmp 0x154b2
movq 0x98(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x38(%rsp)
movb 0x5b(%rsp), %al
movb %al, 0x3e(%rsp)
testb $0x1, 0x5b(%rsp)
je 0x15511
movl 0x5c(%rsp), %eax
cmpl 0x84(%rsp), %eax
jg 0x15511
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x1551a
xorl %eax, %eax
movq %rax, 0x10(%rsp)
jmp 0x1551a
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %ecx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
movb 0x3e(%rsp), %al
movq 0x10(%rsp), %r10
movq %rsp, %r9
movq %r10, (%r9)
movzbl %al, %r9d
andl $0x1, %r9d
callq 0x161d0
jmp 0x1539c
movq 0x98(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x15590
movq 0x98(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x90(%rsp), %rdx
callq 0x167d0
addq $0xa8, %rsp
retq
nopw (%rax,%rax)