// File: /workspace/github_cpp_makefile_star2_O3/Baobaobear[P]sort/./grailsort.hpp
// Function: void Mrrl::grail_CombineBlocks<baobao::TestClass*, baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, baobao::TestClass*, int, int, bool, baobao::TestClass*, std::less<baobao::TestClass>)
void grail_CombineBlocks(RandomAccessIterator keys, RandomAccessIterator beg, RandomAccessIterator end, int LL, int lblock, bool havebuf, RandomAccessBufferIterator xbuf, Comp compare) {
    RandomAccessIterator arr1;

    int M = (int)((end - beg) / (2 * LL));
    int lrest = (int)((end - beg) % (2 * LL));
    if (lrest <= LL) {
        end -= lrest;
        lrest = 0;
    }
    if (xbuf)
    {
        //memcpy(xbuf, beg - lblock, lblock * sizeof(value_type)); // TODO
        for (int i = 0; i < lblock; ++i)
        {
            *(xbuf + i) = *(beg - lblock + i);
        }
    }
    for (int b = 0; b <= M; b++) {
        if (b == M && lrest == 0) break;
        arr1 = beg + b * 2 * LL;
        int NBlk = (b == M ? lrest : 2 * LL) / lblock;
        grail_SortIns(keys, keys + NBlk + (b == M ? 1 : 0), compare);
        int midkey = LL / lblock;
        for (int u = 1; u < NBlk; u++) {
            int p = u - 1;
            for (int v = u; v < NBlk; v++) {
                if (compare(*(arr1 + v*lblock), *(arr1 + p*lblock)) ||
                    (grail_lesseq(*(arr1 + v*lblock), *(arr1 + p*lblock), compare) && compare(*(keys + v), *(keys + p))))
                    p = v;
            }
            if (p != u - 1) {
                grail_swapN(arr1 + (u - 1)*lblock, arr1 + p*lblock, lblock);
                grail_swap1(keys + (u - 1), keys + p);
                if (midkey == u - 1 || midkey == p) midkey ^= (u - 1) ^ p;
            }
        }
        int nbl2 = 0, llast = 0;
        if (b == M) llast = lrest%lblock;
        if (llast != 0) {
            while (nbl2 < NBlk && compare(*(arr1 + NBlk*lblock), *(arr1 + (NBlk - nbl2 - 1)*lblock))) nbl2++;
        }
        if (xbuf) grail_MergeBuffersLeftWithXBuf(keys, keys + midkey, arr1, NBlk - nbl2, lblock, nbl2, llast, compare);
        else grail_MergeBuffersLeft(keys, keys + midkey, arr1, NBlk - nbl2, lblock, havebuf, nbl2, llast, compare);
    }
    if (xbuf) {
        for (RandomAccessIterator p = end; --p >= beg;) *p = *(p - lblock);
        //memcpy(beg - lblock, xbuf, lblock * sizeof(value_type)); // TODO
        for (int i = 0; i < lblock; ++i)
        {
            *(beg - lblock + i) = *(xbuf + i);
        }
    }
    else if (havebuf) while (--end >= beg) grail_swap1(end, end - lblock);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, %ebp
movq %rsi, %r14
movq %rdi, %r13
movq 0x100(%rsp), %rbx
movq %rdx, 0x78(%rsp)
movq %rdx, %rax
subq %rsi, %rax
sarq $0x5, %rax
leal (%rcx,%rcx), %r11d
movslq %r11d, %rsi
cqto
idivq %rsi
movq %rdx, %rsi
negq %rsi
xorl %r10d, %r10d
movq %rcx, 0xa0(%rsp)
cmpl %ecx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %ecx, 0x8(%rsp)
cmovleq %rsi, %r10
testq %rbx, %rbx
sete %dl
testl %r8d, %r8d
setle %cl
orb %dl, %cl
jne 0xc454
movl %ebp, %r15d
movq %r15, %rdx
shlq $0x5, %rdx
movq %r14, %rcx
subq %rdx, %rcx
xorl %edx, %edx
movq %rcx, %rsi
movq %rbx, %rdi
movq %rdx, %r8
shlq $0x5, %r8
movl (%rcx,%r8), %r9d
movl %r9d, (%rbx,%r8)
movl 0x4(%rcx,%r8), %r9d
movl %r9d, 0x4(%rbx,%r8)
movl $0x2, %r8d
movl (%rsi,%r8,4), %r9d
movl %r9d, (%rdi,%r8,4)
incq %r8
cmpq $0x8, %r8
jne 0xc433
incq %rdx
addq $0x20, %rdi
addq $0x20, %rsi
cmpq %r15, %rdx
jne 0xc414
shlq $0x5, %r10
movq %r10, 0x70(%rsp)
testl %eax, %eax
movl 0x8(%rsp), %edx
js 0xc83a
movslq %ebp, %r15
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
incl %eax
movq %rax, 0x98(%rsp)
movq %r15, 0xb8(%rsp)
shlq $0x5, %r15
leaq (%r14,%r15), %rax
movq %rax, 0x90(%rsp)
leaq 0x20(%r13), %rax
movq %rax, 0x88(%rsp)
movq %r15, %rax
negq %rax
movq %rax, 0x80(%rsp)
xorl %esi, %esi
xorl %r8d, %r8d
movq %r14, 0x40(%rsp)
movq %r13, 0xc0(%rsp)
movl %r11d, 0x38(%rsp)
movq %rdi, 0x50(%rsp)
testl %edx, %edx
sete %al
cmpq %rdi, %r8
sete %cl
testb %cl, %al
jne 0xc83a
movq %rsi, 0xb0(%rsp)
movq %r14, %rcx
movslq %esi, %r14
shlq $0x5, %r14
movl %r11d, %eax
imull %r8d, %eax
movslq %eax, %r12
shlq $0x5, %r12
addq %rcx, %r12
xorl %esi, %esi
movq %r8, 0x58(%rsp)
cmpq %rdi, %r8
sete %sil
movl %r11d, %eax
cmovel %edx, %eax
cltd
idivl %ebp
movq %rax, 0x48(%rsp)
movslq %eax, %rbx
movq %rbx, %rax
shlq $0x5, %rax
shll $0x5, %esi
addq %r13, %rsi
addq %rax, %rsi
movq %r13, %rdi
callq 0xb99a
movq 0xa0(%rsp), %rax
cltd
idivl %ebp
movl %eax, %r8d
cmpl $0x2, %ebx
movq %r14, 0xa8(%rsp)
jl 0xc730
movq 0x90(%rsp), %rax
leaq (%rax,%r14), %rdi
movl $0x1, %r14d
movq %r13, %rsi
movq 0x88(%rsp), %r10
leaq -0x1(%r14), %rax
movq %r10, 0x30(%rsp)
movq %rdi, %r11
movq %r14, %rdx
movq %rax, 0x68(%rsp)
movl %eax, %r9d
movl %r9d, %eax
imull %ebp, %eax
cltq
shlq $0x5, %rax
movl (%r11), %ecx
movl (%r12,%rax), %eax
cmpl %eax, %ecx
jge 0xc5a8
movl %edx, %r9d
jmp 0xc5bb
jg 0xc5bb
movslq %r9d, %rax
shlq $0x5, %rax
movl (%r10), %ecx
cmpl (%r13,%rax), %ecx
jl 0xc5a3
incq %rdx
addq %r15, %r11
addq $0x20, %r10
cmpq %rdx, %rbx
jne 0xc58c
movl %r9d, %eax
movq 0x68(%rsp), %r10
cmpq %rax, %r10
je 0xc714
movq %rdi, 0x60(%rsp)
movl %r8d, 0x3c(%rsp)
movq 0xb8(%rsp), %rdx
imulq %rdx, %r10
shlq $0x5, %r10
addq %r12, %r10
movl %r9d, %eax
imull %ebp, %eax
movslq %eax, %r11
shlq $0x5, %r11
addq %r12, %r11
movl (%r10), %ecx
movl 0x4(%r10), %r13d
movq 0x18(%r10), %rax
movq %rax, 0x20(%rsp)
movups 0x8(%r10), %xmm0
movaps %xmm0, 0x10(%rsp)
movl (%r11), %eax
movl %eax, (%r10)
movl 0x4(%r11), %eax
movl %eax, 0x4(%r10)
leaq 0x8(%r11), %r8
movl $0x2, %eax
movl (%r11,%rax,4), %edi
movl %edi, (%r10,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0xc639
addq $0x20, %r10
movl %ecx, (%r11)
movl %r13d, 0x4(%r11)
addq $0x20, %r11
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r8)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r8)
decq %rdx
jne 0xc608
movq 0x68(%rsp), %r11
movq %r11, %rax
shlq $0x5, %rax
movslq %r9d, %rdi
shlq $0x5, %rdi
movq 0xc0(%rsp), %r13
leaq (%rdi,%r13), %rcx
movl (%r13,%rax), %edx
movl 0x4(%r13,%rax), %r8d
movq 0x18(%r13,%rax), %r10
movq %r10, 0x20(%rsp)
movups 0x8(%r13,%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x8(%r13,%rdi), %r10
movl -0x8(%r10), %edi
movl %edi, (%r13,%rax)
movl -0x4(%r10), %edi
movl %edi, 0x4(%r13,%rax)
movl $0x2, %eax
movl (%rcx,%rax,4), %edi
movl %edi, (%rsi,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0xc6ca
movl %edx, (%rcx)
movl %r8d, 0x4(%rcx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r10)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r10)
movl 0x3c(%rsp), %r8d
movl %r8d, %eax
cmpq %rax, %r11
setne %al
cmpl %r9d, %r8d
setne %cl
testb %cl, %al
jne 0xc70f
xorl %r11d, %r8d
xorl %r9d, %r8d
movq 0x60(%rsp), %rdi
movq 0x30(%rsp), %r10
incq %r14
addq %r15, %rdi
addq $0x20, %r10
addq $0x20, %rsi
cmpq %rbx, %r14
jne 0xc575
xorl %r10d, %r10d
movq 0x58(%rsp), %rax
cmpq 0x50(%rsp), %rax
jne 0xc7a6
movl 0x8(%rsp), %eax
cltd
idivl %ebp
testl %edx, %edx
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rcx
je 0xc7bc
movl %edx, %eax
testl %ecx, %ecx
jle 0xc7be
movl %ecx, %edx
imull %ebp, %edx
movslq %edx, %rdx
shlq $0x5, %rdx
movl (%r12,%rdx), %esi
decq %rbx
imulq %r15, %rbx
movq 0xa8(%rsp), %rdi
addq %r14, %rdi
addq %rbx, %rdi
xorl %r10d, %r10d
movq 0x100(%rsp), %rbx
movq 0x80(%rsp), %rdx
cmpl (%rdi), %esi
jge 0xc7c6
incq %r10
addq %rdx, %rdi
cmpq %r10, %rcx
jne 0xc792
movl %ecx, %r10d
jmp 0xc7c6
xorl %eax, %eax
movq 0x40(%rsp), %r14
movq 0x100(%rsp), %rbx
movq 0x48(%rsp), %rcx
jmp 0xc7c6
xorl %eax, %eax
movq 0x100(%rsp), %rbx
movslq %r8d, %rsi
shlq $0x5, %rsi
addq %r13, %rsi
subl %r10d, %ecx
testq %rbx, %rbx
je 0xc7f0
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rdx
movl %ebp, %r8d
movl %r10d, %r9d
pushq %rax
callq 0xcf36
jmp 0xc807
movzbl 0xc(%rsp), %r9d
movq %r13, %rdi
movq %r12, %rdx
movl %ebp, %r8d
pushq %rax
pushq %r10
callq 0xd1db
addq $0x10, %rsp
movq 0x58(%rsp), %r8
incq %r8
movl 0x38(%rsp), %r11d
movq 0xb0(%rsp), %rsi
addl %r11d, %esi
cmpq 0x98(%rsp), %r8
movl 0x8(%rsp), %edx
movq 0x50(%rsp), %rdi
jne 0xc4cc
movq 0x78(%rsp), %rsi
movq 0x70(%rsp), %r8
leaq (%rsi,%r8), %rax
testq %rbx, %rbx
je 0xc90f
leaq -0x20(%rax), %rdi
cmpq %r14, %rdi
jb 0xc8af
movslq %ebp, %rcx
movq %rcx, %rdx
negq %rdx
addq %r8, %rsi
addq $-0x18, %rsi
shlq $0x5, %rcx
negq %rcx
shlq $0x5, %rdx
movq %rax, %r8
movq %rdi, %rax
movl (%rdi,%rdx), %edi
movl %edi, -0x20(%r8)
movl 0x4(%rax,%rdx), %edi
movl %edi, -0x1c(%r8)
movl $0x6, %edi
movq %rsi, %r8
movl (%r8,%rcx), %r9d
movl %r9d, (%r8)
addq $0x4, %r8
decq %rdi
jne 0xc892
leaq -0x20(%rax), %rdi
addq $-0x20, %rsi
cmpq %r14, %rdi
jae 0xc875
testl %ebp, %ebp
jle 0xc9b8
movl %ebp, %eax
movq %rax, %rcx
shlq $0x5, %rcx
subq %rcx, %r14
xorl %ecx, %ecx
movq %rbx, %rdx
movq %r14, %rsi
movq %rcx, %rdi
shlq $0x5, %rdi
movl (%rbx,%rdi), %r8d
movl %r8d, (%r14,%rdi)
movl 0x4(%rbx,%rdi), %r8d
movl %r8d, 0x4(%r14,%rdi)
movl $0x2, %edi
movl (%rdx,%rdi,4), %r8d
movl %r8d, (%rsi,%rdi,4)
incq %rdi
cmpq $0x8, %rdi
jne 0xc8e9
incq %rcx
addq $0x20, %rsi
addq $0x20, %rdx
cmpq %rax, %rcx
jne 0xc8cb
jmp 0xc9b8
cmpb $0x0, 0xc(%rsp)
je 0xc9b8
leaq -0x20(%rax), %rdi
cmpq %r14, %rdi
jb 0xc9b8
movslq %ebp, %rcx
movq %rcx, %rdx
negq %rdx
addq %r8, %rsi
addq $-0x18, %rsi
shlq $0x5, %rcx
negq %rcx
shlq $0x5, %rdx
movq %rax, %r11
movq %rdi, %rax
addq %rdx, %rdi
movl -0x20(%r11), %r8d
movl -0x1c(%r11), %r9d
movq -0x8(%r11), %r10
movq %r10, 0x20(%rsp)
movups -0x18(%r11), %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x8(%rax,%rdx), %r10
movl -0x8(%r10), %ebx
movl %ebx, -0x20(%r11)
movl -0x4(%r10), %ebx
movl %ebx, -0x1c(%r11)
movl $0x6, %r11d
movq %rsi, %rbx
movl (%rbx,%rcx), %ebp
movl %ebp, (%rbx)
addq $0x4, %rbx
decq %r11
jne 0xc984
movl %r8d, (%rdi)
movl %r9d, 0x4(%rdi)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%r10)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r10)
leaq -0x20(%rax), %rdi
addq $-0x20, %rsi
cmpq %r14, %rdi
jae 0xc942
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq