// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movl %r8d, %ebp
movq %rdx, %rbx
movl 0x80(%rsp), %eax
testl %ecx, %ecx
je 0xa3de
movq %rsi, %r12
movq %rdi, %rsi
movl %r9d, 0x10(%rsp)
movl %eax, 0x14(%rsp)
leaq 0x8(%rsp), %rax
movl %ebp, (%rax)
movl (%rdi), %eax
xorl %edx, %edx
cmpl (%r12), %eax
setge %dl
leaq 0xc(%rsp), %rax
movl %edx, (%rax)
movslq %ebp, %rdx
movq %rdx, %rax
negq %rax
movq %rax, 0x28(%rsp)
movslq %ecx, %rax
movq %rax, 0x40(%rsp)
shlq $0x5, %rdx
leaq (%rbx,%rdx), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r14
movl %ebp, 0x24(%rsp)
movq %r12, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
leaq (%rbx,%rdx), %r13
movl 0x8(%rsp), %eax
movslq %eax, %rcx
shlq $0x5, %rcx
movq %r13, %rdi
subq %rcx, %rdi
cmpq 0x40(%rsp), %r14
jge 0xa396
movq %rdx, %r15
movq %r14, %rdx
shlq $0x5, %rdx
movl (%rsi,%rdx), %edx
xorl %esi, %esi
cmpl (%r12), %edx
setge %sil
cmpl %esi, 0xc(%rsp)
jne 0xa350
subq %rcx, %rbx
movq 0x18(%rsp), %r12
subq %rcx, %r12
xorl %ebp, %ebp
cltq
cmpq %rax, %rbp
jge 0xa371
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4164
incq %rbp
movl 0x8(%rsp), %eax
addq $0x20, %rbx
addq $0x20, %r12
jmp 0xa32d
leaq (%r15,%r13), %rcx
movq 0x28(%rsp), %r8
shlq $0x5, %r8
addq %rdi, %r8
leaq 0x8(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0xa6d1
jmp 0xa37e
movl 0x24(%rsp), %ebp
movl %ebp, 0x8(%rsp)
movq 0x30(%rsp), %r12
incq %r14
addq %r15, 0x18(%rsp)
movq %r13, %rbx
movq 0x38(%rsp), %rsi
movq %r15, %rdx
jmp 0xa2e3
movl 0x14(%rsp), %edx
testl %edx, %edx
je 0xa42f
cmpl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %r15
je 0xa460
subq %rcx, %rbx
subq %rcx, %r15
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0xa419
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4164
incq %r14
movl 0x8(%rsp), %eax
addq $0x20, %rbx
addq $0x20, %r15
jmp 0xa3bb
imull %ebp, %r9d
movslq %r9d, %rsi
shlq $0x5, %rsi
addq %rbx, %rsi
movslq %eax, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movslq %ebp, %rax
shlq $0x5, %rax
movq %rbx, %rcx
subq %rax, %rcx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0cf
movl 0x10(%rsp), %ecx
imull %ebp, %ecx
movl %ecx, 0x8(%rsp)
andl $0x0, 0xc(%rsp)
movl 0x14(%rsp), %edx
jmp 0xa470
subq %rcx, %rbx
movq 0x18(%rsp), %r15
subq %rcx, %r15
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0xa498
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4164
incq %r14
movl 0x8(%rsp), %eax
addq $0x20, %rbx
addq $0x20, %r15
jmp 0xa43d
movl 0x10(%rsp), %ecx
imull %ebp, %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rsp)
movq %rdi, %r13
movslq %ecx, %rsi
shlq $0x5, %rsi
addq %r13, %rsi
movslq %edx, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movq 0x28(%rsp), %rcx
shlq $0x5, %rcx
addq %r13, %rcx
movq %r13, %rdi
callq 0xa0cf
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq