// File: /workspace/github_cpp_makefile_star2_O1/Baobaobear[P]sort/./grailsort.hpp
// Function: void Mrrl::grail_MergeBuffersLeftWithXBuf<baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, baobao::TestClass*, int, int, int, int, std::less<baobao::TestClass>)
void grail_MergeBuffersLeftWithXBuf(RandomAccessIterator keys, RandomAccessIterator midkey, RandomAccessIterator arr, int nblock, int lblock, int nblock2, int llast, Comp compare) {
    RandomAccessIterator prest, pidx;
    int lrest, frest;

    if (nblock == 0) {
        int l = nblock2*lblock;
        grail_MergeLeftWithXBuf(arr, arr + l, arr + l + llast, arr - lblock, compare);
        return;
    }

    lrest = lblock;
    frest = compare(*keys, *midkey) ? 0 : 1;
    pidx = arr + lblock;
    for (int cidx = 1; cidx < nblock;cidx++, pidx += lblock) {
        prest = pidx - lrest;
        int fnext = compare(*(keys + cidx), *midkey) ? 0 : 1;
        if (fnext == frest) {
            //memcpy(prest - lblock, prest, lrest * sizeof(value_type)); // TODO
            for (int i = 0; i < lrest; ++i)
            {
                *(prest - lblock + i) = *(prest + i);
            }
            prest = pidx;
            lrest = lblock;
        }
        else {
            grail_SmartMergeWithXBuf(prest, &lrest, &frest, prest + lrest + lblock, prest - lblock, compare);
        }
    }
    prest = pidx - lrest;
    if (llast) {
        if (frest) {
            //memcpy(prest - lblock, prest, lrest * sizeof(value_type)); // TODO
            for (int i = 0; i < lrest; ++i)
            {
                *(prest - lblock + i) = *(prest + i);
            }
            prest = pidx;
            lrest = lblock*nblock2;
            frest = 0;
        }
        else {
            lrest += lblock*nblock2;
        }
        grail_MergeLeftWithXBuf(prest, prest + lrest, prest + lrest + llast, prest - lblock, compare);
    }
    else {
        //memcpy(prest - lblock, prest, lrest * sizeof(value_type));
        for (int i = 0; i < lrest; ++i)
        {
            *(prest - lblock + i) = *(prest + i);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movl 0x60(%rsp), %eax
testl %ecx, %ecx
je 0xd2e8
movq %rdi, %r10
movl %eax, 0x14(%rsp)
movl %r8d, 0x4(%rsp)
movl (%rdi), %eax
xorl %edx, %edx
cmpl (%rsi), %eax
setge %dl
movl %edx, 0x8(%rsp)
movslq %r8d, %rax
movq %rax, %r14
shlq $0x5, %r14
leaq (%r15,%r14), %rbx
cmpl $0x2, %ecx
movl %r8d, 0xc(%rsp)
movl %r9d, 0x10(%rsp)
jl 0xd24b
negq %rax
movl %ecx, %ecx
movq %rcx, 0x20(%rsp)
addq $0x8, %r15
movl $0x1, %r13d
shlq $0x5, %rax
movq %rax, 0x18(%rsp)
movslq 0x4(%rsp), %rax
movq %rax, %rcx
shlq $0x5, %rcx
movq %rbx, %rdi
subq %rcx, %rdi
movq %r13, %rdx
shlq $0x5, %rdx
movq %r10, %r12
movl (%r10,%rdx), %edx
xorl %r9d, %r9d
movq %rsi, %rbp
cmpl (%rsi), %edx
setge %r9b
cmpl %r9d, 0x8(%rsp)
jne 0xd210
testl %eax, %eax
jle 0xd209
movq 0x18(%rsp), %rdx
addq %rdi, %rdx
negq %rcx
xorl %esi, %esi
movq %rsi, %r10
shlq $0x5, %r10
movl (%rdi,%r10), %r9d
movl %r9d, (%rdx,%r10)
movl 0x4(%rdi,%r10), %r9d
movl %r9d, 0x4(%rdx,%r10)
movl $0x6, %r11d
movq %rcx, %r9
movl 0x8(%rbx,%r9), %r10d
movl %r10d, (%r15,%r9)
addq $0x4, %r9
decq %r11
jne 0xd1eb
incq %rsi
addq $0x20, %rcx
cmpq %rax, %rsi
jne 0xd1c9
movl %r8d, 0x4(%rsp)
jmp 0xd231
leaq (%rbx,%r14), %rcx
movq 0x18(%rsp), %rax
leaq (%rdi,%rax), %r8
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xd73b
movl 0xc(%rsp), %r8d
incq %r13
addq %r14, %rbx
addq %r14, %r15
cmpq 0x20(%rsp), %r13
movq %rbp, %rsi
movq %r12, %r10
jne 0xd185
movslq 0x4(%rsp), %rax
movq %rax, %rdi
shlq $0x5, %rdi
movq %rbx, %rcx
subq %rdi, %rcx
movl 0x14(%rsp), %r11d
testl %r11d, %r11d
je 0xd323
cmpl $0x0, 0x8(%rsp)
je 0xd37e
testl %eax, %eax
jle 0xd2ce
movq %rcx, %rdx
subq %r14, %rdx
addq %r14, %rdi
movq %rbx, %rsi
subq %rdi, %rsi
xorl %edi, %edi
movq %rcx, %r8
movq %rdi, %r9
shlq $0x5, %r9
movl (%rcx,%r9), %r10d
movl %r10d, (%rdx,%r9)
movl 0x4(%rcx,%r9), %r10d
movl %r10d, 0x4(%rdx,%r9)
movl $0x2, %r9d
movl (%r8,%r9,4), %r10d
movl %r10d, (%rsi,%r9,4)
incq %r9
cmpq $0x8, %r9
jne 0xd2ad
incq %rdi
addq $0x20, %rsi
addq $0x20, %r8
cmpq %rax, %rdi
jne 0xd28e
movl 0x10(%rsp), %eax
imull 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0xd390
imull %r8d, %r9d
movslq %r9d, %rsi
shlq $0x5, %rsi
addq %r15, %rsi
movslq %eax, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movslq %r8d, %rax
shlq $0x5, %rax
movq %r15, %rcx
subq %rax, %rcx
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcdb4
testl %eax, %eax
jle 0xd3b4
movq %rcx, %rdx
subq %r14, %rdx
addq %r14, %rdi
subq %rdi, %rbx
xorl %esi, %esi
movq %rcx, %rdi
movq %rsi, %r8
shlq $0x5, %r8
movl (%rcx,%r8), %r9d
movl %r9d, (%rdx,%r8)
movl 0x4(%rcx,%r8), %r9d
movl %r9d, 0x4(%rdx,%r8)
movl $0x2, %r8d
movl (%rdi,%r8,4), %r9d
movl %r9d, (%rbx,%r8,4)
incq %r8
cmpq $0x8, %r8
jne 0xd35b
incq %rsi
addq $0x20, %rbx
addq $0x20, %rdi
cmpq %rax, %rsi
jne 0xd33c
jmp 0xd3b4
movl 0x10(%rsp), %edx
imull 0xc(%rsp), %edx
addl %eax, %edx
movl %edx, 0x4(%rsp)
movq %rcx, %rbx
movslq 0x4(%rsp), %rsi
shlq $0x5, %rsi
addq %rbx, %rsi
movslq %r11d, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movq %rbx, %rcx
subq %r14, %rcx
movq %rbx, %rdi
callq 0xcdb4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq