// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
movq %rsi, %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq $0x10, %rax
jge 0xb982
movq %rbp, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbb9f
movl $0x1, %ecx
movl %ecx, %r13d
movl %ecx, %edx
imull %edx, %edx
leal (,%r13,2), %ecx
cmpq %rdx, %rax
jg 0xb987
decq %rax
movl %r13d, %ecx
cqto
idivq %rcx
movq %rax, %r15
leal 0x1(%rax), %ebx
movslq %ebx, %rax
shlq $0x5, %rax
shlq $0x5, %rcx
addq %rbp, %rcx
addq %rax, %rcx
movq %rbp, %rdi
movq %r14, %rsi
movq %rcx, %rdx
callq 0xbc7e
movq %rax, %r12
subq %rbp, %r12
shrq $0x5, %r12
leal (%r15,%r13), %eax
incl %eax
movl %eax, 0x18(%rsp)
cmpl %r12d, %eax
setle 0x17(%rsp)
jg 0xb9f4
movq %rbp, 0x20(%rsp)
movl %r13d, %r15d
jmp 0xba29
cmpl $0x4, %r12d
jge 0xba13
movq %rbp, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbda8
movl %r13d, %r15d
shrl %r13d
cmpl %r12d, %r15d
jg 0xba13
movq %rbp, 0x20(%rsp)
xorl %r13d, %r13d
movl %r15d, %ebx
movl %r13d, %eax
shlq $0x5, %rax
movslq %ebx, %rbp
shlq $0x5, %rbp
addq 0x20(%rsp), %rbp
addq %rax, %rbp
cmpl %r12d, 0x18(%rsp)
jg 0xba5b
movq %rbp, %rdi
movq %r14, %rsi
movl %r15d, %edx
movq 0x38(%rsp), %rcx
movl 0x1c(%rsp), %r8d
jmp 0xba69
movq %rbp, %rdi
movq %r14, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xbf0f
movq %r14, 0x30(%rsp)
subq %rbp, %r14
sarq $0x5, %r14
leal (%r15,%r15), %ecx
movslq %ecx, %rax
cmpq %rax, %r14
jle 0xbb71
movl %ebx, %eax
shrl $0x3, %eax
imull %ebx, %eax
movl %eax, 0x2c(%rsp)
movl %ebx, %eax
shrl %eax
movl %eax, 0x28(%rsp)
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x857f(%rip), %xmm1     # 0x14030
movsd 0x857f(%rip), %xmm2     # 0x14038
movl %r15d, %eax
movl %ecx, %r15d
movl %r13d, %r8d
movb 0x17(%rsp), %cl
cmpl %r12d, 0x18(%rsp)
jle 0xbb1b
cmpl $0x5, %ebx
jl 0xbae0
movb $0x1, %cl
movl 0x28(%rsp), %r8d
cmpl %r15d, 0x2c(%rsp)
jge 0xbb1b
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x40(%rsp), %xmm0
mulsd 0x8532(%rip), %xmm0     # 0x14028
movl $0x1, %ecx
cmpl $0x2, %ebx
jl 0xbb10
ucomisd %xmm1, %xmm0
jbe 0xbb10
addl %ecx, %ecx
mulsd %xmm2, %xmm0
cmpl %ebx, %ecx
jl 0xbb00
shll $0x2, %eax
cltd
idivl %ecx
movl %eax, %r8d
xorl %ecx, %ecx
cmpl 0x1c(%rsp), %r8d
movq 0x38(%rsp), %rax
movl $0x0, %edx
cmovgq %rdx, %rax
testb %cl, %cl
cmoveq %rdx, %rax
movq %rax, (%rsp)
movzbl %cl, %r9d
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movl %r15d, %ecx
callq 0xc57c
movsd 0x84df(%rip), %xmm2     # 0x14038
movsd 0x84cf(%rip), %xmm1     # 0x14030
leal (%r15,%r15), %ecx
movslq %ecx, %rax
cmpq %rax, %r14
jg 0xbab9
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xbb9f
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcba0