// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movl %r9d, 0x20(%rsp)
movl %r8d, %ebp
movq %rdx, %rax
movq %rdx, 0x50(%rsp)
movq %rsi, 0x30(%rsp)
subq %rsi, %rax
sarq $0x5, %rax
leal (%rcx,%rcx), %edx
movl %edx, 0x24(%rsp)
movslq %edx, %rsi
cqto
idivq %rsi
movq %rax, %rbx
movq %rdx, %rax
negq %rax
movq %rcx, 0x88(%rsp)
cmpl %ecx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %ecx, 0xc(%rsp)
movl $0x0, %ecx
cmovleq %rax, %rcx
movq %rcx, 0x48(%rsp)
movq 0xe0(%rsp), %r12
movslq %r8d, %rax
movq %rax, 0x18(%rsp)
testq %r12, %r12
movq %rdi, 0x10(%rsp)
je 0x9b4d
movq 0x18(%rsp), %rcx
movq %rcx, %rax
shlq $0x5, %rax
movq 0x30(%rsp), %r14
subq %rax, %r14
testl %ecx, %ecx
movl $0x0, %r15d
cmovgl %ebp, %r15d
subq $0x1, %r15
jb 0x9b4d
movq %r12, %rdi
movq %r14, %rsi
callq 0x4164
movq 0x10(%rsp), %rdi
addq $0x20, %r12
addq $0x20, %r14
jmp 0x9b2d
movslq %ebx, %rax
movq %rax, 0x90(%rsp)
movl %eax, %r8d
leaq 0x20(%rdi), %rax
movq %rax, 0x80(%rsp)
movq 0x18(%rsp), %r13
shlq $0x5, %r13
movq 0x30(%rsp), %r14
leaq (%r14,%r13), %rax
movq %rax, 0x78(%rsp)
xorl %r9d, %r9d
xorl %edx, %edx
movl 0xc(%rsp), %ebx
movq %r8, 0x58(%rsp)
cmpq 0x90(%rsp), %r9
jg 0x9e25
testl %ebx, %ebx
sete %al
cmpq %r8, %r9
sete %cl
testb %cl, %al
jne 0x9e25
movq %rdx, 0x98(%rsp)
movslq %edx, %r15
shlq $0x5, %r15
addq 0x78(%rsp), %r15
movl 0x24(%rsp), %ecx
movl %ecx, %eax
imull %r9d, %eax
movslq %eax, %r12
shlq $0x5, %r12
addq %r14, %r12
xorl %esi, %esi
movq %r9, 0x60(%rsp)
cmpq %r8, %r9
sete %sil
movl %ecx, %eax
cmovel %ebx, %eax
cltd
idivl %ebp
movq %rax, 0x38(%rsp)
movslq %eax, %rbx
movq %rbx, %rax
shlq $0x5, %rax
shll $0x5, %esi
addq %rdi, %rsi
addq %rax, %rsi
callq 0x93cf
movq %r15, %r9
movq 0x10(%rsp), %r11
movq 0x88(%rsp), %rax
cltd
idivl %ebp
movl %eax, %r8d
movq 0x80(%rsp), %r10
pushq $0x1
popq %rdx
cmpq %rbx, %rdx
jge 0x9d34
leaq -0x1(%rdx), %rsi
movq %r9, %rax
movq %r10, %rcx
movq %rdx, 0xa0(%rsp)
movq %rsi, 0x28(%rsp)
movl %esi, %r15d
movslq %r15d, %r14
cmpq %rbx, %rdx
jge 0x9c8a
movl %r15d, %esi
imull %ebp, %esi
movslq %esi, %rsi
shlq $0x5, %rsi
movl (%rax), %edi
movl (%r12,%rsi), %esi
cmpl %esi, %edi
jge 0x9c70
movl %edx, %r15d
jmp 0x9c7e
jg 0x9c7e
shlq $0x5, %r14
movl (%rcx), %esi
cmpl (%r11,%r14), %esi
jl 0x9c6b
incq %rdx
addq $0x20, %rcx
addq %r13, %rax
jmp 0x9c4c
movl %r15d, %eax
movq 0x28(%rsp), %rdi
cmpq %rax, %rdi
je 0x9d1d
movq %r10, 0x68(%rsp)
movq %r9, 0x70(%rsp)
movq 0x18(%rsp), %rdx
imulq %rdx, %rdi
shlq $0x5, %rdi
addq %r12, %rdi
movl %r15d, %eax
imull %ebp, %eax
movslq %eax, %rsi
shlq $0x5, %rsi
addq %r12, %rsi
movl %r8d, 0x44(%rsp)
callq 0xa23a
movq 0x28(%rsp), %rdi
shlq $0x5, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
shlq $0x5, %r14
addq %rax, %r14
movq %r14, %rsi
callq 0xa092
movl 0x44(%rsp), %r8d
movl %r8d, %eax
cmpq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
je 0x9d08
cmpl %r15d, %r8d
jne 0x9d0e
xorl %eax, %r8d
xorl %r15d, %r8d
movq 0x10(%rsp), %r11
movq 0x70(%rsp), %r9
movq 0x68(%rsp), %r10
movq 0xa0(%rsp), %rdx
incq %rdx
addq $0x20, %r10
addq %r13, %r9
jmp 0x9c29
movl %r8d, %r14d
xorl %r10d, %r10d
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
movq 0x38(%rsp), %rdi
jne 0x9d9a
movl 0xc(%rsp), %ebx
movl %ebx, %eax
cltd
idivl %ebp
movl $0x0, %eax
testl %edx, %edx
je 0x9db1
movl %edi, %eax
imull %ebp, %eax
movslq %eax, %rcx
shlq $0x5, %rcx
addq %r12, %rcx
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
leal -0x1(%rdi), %esi
imull %ebp, %esi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x9da2
movslq %esi, %r8
shlq $0x5, %r8
movl (%rcx), %r9d
cmpl (%r12,%r8), %r9d
jge 0x9da7
incq %rdi
subl %ebp, %esi
jmp 0x9d7e
xorl %eax, %eax
movl 0xc(%rsp), %ebx
jmp 0x9db1
movl %edx, %r10d
jmp 0x9dac
movl %edx, %r10d
movl %edi, %eax
movq 0x38(%rsp), %rdi
movslq %r14d, %rsi
shlq $0x5, %rsi
addq %r11, %rsi
subl %eax, %edi
cmpq $0x0, 0xe0(%rsp)
movq %rdi, %rcx
je 0x9de4
subq $0x8, %rsp
movq %r11, %rdi
movq %r12, %rdx
movl %ebp, %r8d
movl %eax, %r9d
pushq %r10
callq 0xa263
jmp 0x9dfb
movzbl 0x20(%rsp), %r9d
movq %r11, %rdi
movq %r12, %rdx
movl %ebp, %r8d
pushq %r10
pushq %rax
callq 0xa4a7
popq %rax
popq %rcx
movq 0x60(%rsp), %r9
incq %r9
movq 0x98(%rsp), %rdx
addl 0x24(%rsp), %edx
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %r8
jmp 0x9b8c
cmpq $0x0, 0xe0(%rsp)
je 0x9e9b
negq 0x18(%rsp)
movq 0x48(%rsp), %rcx
shlq $0x5, %rcx
movq 0x50(%rsp), %rax
leaq (%rcx,%rax), %rbx
addq $-0x20, %rbx
negq %r13
cmpq %r14, %rbx
jb 0x9e65
leaq (%rbx,%r13), %rsi
movq %rbx, %rdi
callq 0x4164
addq $-0x20, %rbx
jmp 0x9e4e
movq 0x18(%rsp), %rax
shlq $0x5, %rax
addq %rax, %r14
xorl %ebx, %ebx
testl %ebp, %ebp
cmovgl %ebp, %ebx
movq 0xe0(%rsp), %r15
subq $0x1, %rbx
jb 0x9ed2
movq %r14, %rdi
movq %r15, %rsi
callq 0x4164
addq $0x20, %r14
addq $0x20, %r15
jmp 0x9e80
cmpb $0x0, 0x20(%rsp)
je 0x9ed2
movq 0x48(%rsp), %rcx
shlq $0x5, %rcx
movq 0x50(%rsp), %rax
leaq (%rcx,%rax), %rbx
addq $-0x20, %rbx
negq %r13
cmpq %r14, %rbx
jb 0x9ed2
leaq (%rbx,%r13), %rsi
movq %rbx, %rdi
callq 0xa092
addq $-0x20, %rbx
jmp 0x9ebb
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq