// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, %ebp
movq %rdi, %r13
movq 0xf0(%rsp), %r11
movq %rdx, 0x68(%rsp)
movq %rdx, %rax
subq %rsi, %rax
sarq $0x5, %rax
leal (%rcx,%rcx), %r14d
movslq %r14d, %rdi
cqto
idivq %rdi
movq %rdx, %rdi
negq %rdi
xorl %ebx, %ebx
movq %rcx, 0x90(%rsp)
cmpl %ecx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %ecx, 0x8(%rsp)
cmovleq %rdi, %rbx
testq %r11, %r11
sete %dl
testl %r8d, %r8d
setle %cl
orb %dl, %cl
jne 0xc645
movl %ebp, %r15d
movq %r15, %rdx
shlq $0x5, %rdx
movq %rsi, %rcx
subq %rdx, %rcx
xorl %edx, %edx
movq %rcx, %r10
movq %r11, %rdi
movq %rdx, %r8
shlq $0x5, %r8
movl (%rcx,%r8), %r9d
movl %r9d, (%r11,%r8)
movl 0x4(%rcx,%r8), %r9d
movl %r9d, 0x4(%r11,%r8)
movl $0x2, %r8d
movl (%r10,%r8,4), %r9d
movl %r9d, (%rdi,%r8,4)
incq %r8
cmpq $0x8, %r8
jne 0xc624
incq %rdx
addq $0x20, %rdi
addq $0x20, %r10
cmpq %r15, %rdx
jne 0xc605
shlq $0x5, %rbx
movq %rbx, 0x60(%rsp)
testl %eax, %eax
movl 0x8(%rsp), %edx
js 0xca06
movslq %ebp, %r15
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
incl %eax
movq %rax, 0x88(%rsp)
movq %r15, 0xa0(%rsp)
shlq $0x5, %r15
leaq (%rsi,%r15), %rax
movq %rax, 0x80(%rsp)
leaq 0x20(%r13), %rax
movq %rax, 0x78(%rsp)
movq %r15, %rax
negq %rax
movq %rax, 0x70(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rsi, 0x40(%rsp)
movq %r13, 0xa8(%rsp)
movl %r14d, 0x38(%rsp)
movq %rdi, 0x48(%rsp)
testl %edx, %edx
sete %al
cmpq %rdi, %r9
sete %cl
testb %cl, %al
jne 0xca06
movq %r8, 0x98(%rsp)
movslq %r8d, %rax
shlq $0x5, %rax
movq %rax, 0x50(%rsp)
movl %r14d, %eax
imull %r9d, %eax
movslq %eax, %r12
shlq $0x5, %r12
addq %rsi, %r12
xorl %esi, %esi
movq %r9, 0x58(%rsp)
cmpq %rdi, %r9
sete %sil
movl %r14d, %eax
cmovel %edx, %eax
cltd
idivl %ebp
movl %eax, %r14d
movslq %eax, %rbx
movq %rbx, %rax
shlq $0x5, %rax
shll $0x5, %esi
addq %r13, %rsi
addq %rax, %rsi
movq %r13, %rdi
callq 0xbb9f
movq 0x90(%rsp), %rax
cltd
idivl %ebp
movl %eax, %edi
cmpl $0x2, %ebx
jl 0xc91b
movq 0x80(%rsp), %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %rbx
movl $0x1, %ecx
movq %r13, %rsi
movq 0x78(%rsp), %r10
leaq -0x1(%rcx), %rax
movq %r10, 0x28(%rsp)
movq %rbx, %r11
movq %rcx, %rdx
movq %rax, 0x30(%rsp)
movl %eax, %r9d
movl %r9d, %eax
imull %ebp, %eax
cltq
shlq $0x5, %rax
movl (%r11), %r8d
movl (%r12,%rax), %eax
cmpl %eax, %r8d
jge 0xc78d
movl %edx, %r9d
jmp 0xc7a0
jg 0xc7a0
movslq %r9d, %rax
shlq $0x5, %rax
movl (%r10), %r8d
cmpl (%r13,%rax), %r8d
jl 0xc788
incq %rdx
addq %r15, %r11
addq $0x20, %r10
cmpq %rdx, %r14
jne 0xc770
movl %r9d, %eax
movq 0x30(%rsp), %r10
cmpq %rax, %r10
je 0xc8ff
movq %rbx, 0xb0(%rsp)
movl %edi, 0x3c(%rsp)
movq 0xa0(%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), %ebx
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 0xc820
addq $0x20, %r10
movl %ebx, (%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 0xc7ef
movq 0x30(%rsp), %rax
shlq $0x5, %rax
movslq %r9d, %rdi
shlq $0x5, %rdi
movq 0xa8(%rsp), %r13
leaq (%rdi,%r13), %rdx
movl (%r13,%rax), %r8d
movl 0x4(%r13,%rax), %r10d
movq 0x18(%r13,%rax), %r11
movq %r11, 0x20(%rsp)
movups 0x8(%r13,%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x8(%r13,%rdi), %r11
movl -0x8(%r11), %edi
movl %edi, (%r13,%rax)
movl -0x4(%r11), %edi
movl %edi, 0x4(%r13,%rax)
movl $0x2, %eax
movl (%rdx,%rax,4), %edi
movl %edi, (%rsi,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0xc8ae
movl %r8d, (%rdx)
movl %r10d, 0x4(%rdx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r11)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r11)
movl 0x3c(%rsp), %edi
movl %edi, %eax
movq 0x30(%rsp), %r8
cmpq %rax, %r8
setne %al
cmpl %r9d, %edi
setne %dl
testb %dl, %al
movq 0xb0(%rsp), %rbx
jne 0xc8ff
xorl %r8d, %edi
xorl %r9d, %edi
movq 0x28(%rsp), %r10
incq %rcx
addq %r15, %rbx
addq $0x20, %r10
addq $0x20, %rsi
cmpq %r14, %rcx
jne 0xc759
xorl %r10d, %r10d
movl $0x0, %eax
movq 0x58(%rsp), %rcx
cmpq 0x48(%rsp), %rcx
jne 0xc938
movl 0x8(%rsp), %eax
cltd
idivl %ebp
movl %edx, %eax
testl %eax, %eax
movq 0x50(%rsp), %rsi
je 0xc981
testl %r14d, %r14d
jle 0xc981
movl %r14d, %ecx
imull %ebp, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
movl (%r12,%rcx), %ecx
leaq -0x1(%r14), %rdx
imulq %r15, %rdx
addq 0x40(%rsp), %rsi
addq %rdx, %rsi
xorl %r10d, %r10d
movq 0x70(%rsp), %rdx
cmpl (%rsi), %ecx
jge 0xc981
incq %r10
addq %rdx, %rsi
cmpq %r10, %r14
jne 0xc96f
movl %r14d, %r10d
movslq %edi, %rsi
shlq $0x5, %rsi
addq %r13, %rsi
subl %r10d, %r14d
cmpq $0x0, 0xf0(%rsp)
je 0xc9b4
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rdx
movl %r14d, %ecx
movl %ebp, %r8d
movl %r10d, %r9d
pushq %rax
callq 0xd111
jmp 0xc9ce
movzbl 0xc(%rsp), %r9d
movq %r13, %rdi
movq %r12, %rdx
movl %r14d, %ecx
movl %ebp, %r8d
pushq %rax
pushq %r10
callq 0xd3c3
addq $0x10, %rsp
movq 0x58(%rsp), %r9
incq %r9
movl 0x38(%rsp), %r14d
movq 0x98(%rsp), %r8
addl %r14d, %r8d
cmpq 0x88(%rsp), %r9
movq 0x40(%rsp), %rsi
movl 0x8(%rsp), %edx
movq 0x48(%rsp), %rdi
jne 0xc6b8
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
leaq (%r8,%r9), %rax
movq 0xf0(%rsp), %r11
testq %r11, %r11
je 0xcae4
leaq -0x20(%rax), %rdi
cmpq %rsi, %rdi
jb 0xca84
movslq %ebp, %rcx
movq %rcx, %rdx
negq %rdx
leaq (%r9,%r8), %r10
addq $-0x18, %r10
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 %r10, %r8
movl (%r8,%rcx), %r9d
movl %r9d, (%r8)
addq $0x4, %r8
decq %rdi
jne 0xca67
leaq -0x20(%rax), %rdi
addq $-0x20, %r10
cmpq %rsi, %rdi
jae 0xca4a
testl %ebp, %ebp
jle 0xcb8e
movl %ebp, %eax
movq %rax, %rcx
shlq $0x5, %rcx
subq %rcx, %rsi
xorl %ecx, %ecx
movq %r11, %rdx
movq %rsi, %r9
movq %rcx, %rdi
shlq $0x5, %rdi
movl (%r11,%rdi), %r8d
movl %r8d, (%rsi,%rdi)
movl 0x4(%r11,%rdi), %r8d
movl %r8d, 0x4(%rsi,%rdi)
movl $0x2, %edi
movl (%rdx,%rdi,4), %r8d
movl %r8d, (%r9,%rdi,4)
incq %rdi
cmpq $0x8, %rdi
jne 0xcabe
incq %rcx
addq $0x20, %r9
addq $0x20, %rdx
cmpq %rax, %rcx
jne 0xcaa0
jmp 0xcb8e
cmpb $0x0, 0xc(%rsp)
je 0xcb8e
leaq -0x20(%rax), %rdi
cmpq %rsi, %rdi
jb 0xcb8e
movslq %ebp, %rcx
movq %rcx, %rdx
negq %rdx
leaq (%r9,%r8), %r14
addq $-0x18, %r14
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 %r14, %rbx
movl (%rbx,%rcx), %ebp
movl %ebp, (%rbx)
addq $0x4, %rbx
decq %r11
jne 0xcb5a
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, %r14
cmpq %rsi, %rdi
jae 0xcb18
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq