// File: /workspace/github_cpp_makefile_star2_O3/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), %r11d
testl %ecx, %ecx
je 0xd082
movq %rdi, %r10
movl %r8d, 0x4(%rsp)
movl (%rdi), %eax
xorl %edx, %edx
movq %rsi, 0x18(%rsp)
cmpl (%rsi), %eax
setge %dl
movl %edx, 0x8(%rsp)
movslq %r8d, %rbp
movq %rbp, %rbx
shlq $0x5, %rbx
leaq (%r15,%rbx), %r14
cmpl $0x2, %ecx
jl 0xd0bd
movl %r8d, 0xc(%rsp)
movl %r9d, 0x10(%rsp)
movl %r11d, 0x14(%rsp)
negq %rbp
movl %ecx, %eax
movq %rax, 0x20(%rsp)
addq $0x8, %r15
movl $0x1, %r13d
shlq $0x5, %rbp
movq 0x18(%rsp), %r11
movslq 0x4(%rsp), %rax
movq %rax, %rcx
shlq $0x5, %rcx
movq %r14, %rdi
subq %rcx, %rdi
movq %r13, %rdx
shlq $0x5, %rdx
movq %r10, %r12
movl (%r10,%rdx), %edx
xorl %esi, %esi
cmpl (%r11), %edx
setge %sil
cmpl %esi, 0x8(%rsp)
jne 0xd037
testl %eax, %eax
jle 0xd02d
leaq (%rdi,%rbp), %rdx
negq %rcx
xorl %esi, %esi
movq %rsi, %r8
shlq $0x5, %r8
movl (%rdi,%r8), %r9d
movl %r9d, (%rdx,%r8)
movl 0x4(%rdi,%r8), %r9d
movl %r9d, 0x4(%rdx,%r8)
movl $0x6, %r8d
movq %rcx, %r9
movl 0x8(%r14,%r9), %r10d
movl %r10d, (%r15,%r9)
addq $0x4, %r9
decq %r8
jne 0xd00f
incq %rsi
addq $0x20, %rcx
cmpq %rax, %rsi
jne 0xcfed
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
jmp 0xd053
leaq (%r14,%rbx), %rcx
leaq (%rdi,%rbp), %r8
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xd555
movq 0x18(%rsp), %r11
incq %r13
addq %rbx, %r14
addq %rbx, %r15
cmpq 0x20(%rsp), %r13
movq %r12, %r10
jne 0xcfb1
movl 0x4(%rsp), %edx
movslq %edx, %rbp
movl 0x14(%rsp), %r11d
movl 0x10(%rsp), %r9d
movl 0xc(%rsp), %r8d
jmp 0xd0c0
imull %r8d, %r9d
movslq %r9d, %rsi
shlq $0x5, %rsi
addq %r15, %rsi
movslq %r11d, %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 0xcbd9
movl %r8d, %edx
shlq $0x5, %rbp
movq %r14, %rax
subq %rbp, %rax
testl %r11d, %r11d
je 0xd141
cmpl $0x0, 0x8(%rsp)
je 0xd19b
testl %edx, %edx
jle 0xd12e
movq %rax, %rcx
subq %rbx, %rcx
movl %edx, %edx
xorl %esi, %esi
movq %rax, %rdi
movq %rcx, %r12
movq %rsi, %r15
shlq $0x5, %r15
movl (%rax,%r15), %r10d
movl %r10d, (%rcx,%r15)
movl 0x4(%rax,%r15), %r10d
movl %r10d, 0x4(%rcx,%r15)
movl $0x2, %r15d
movl (%rdi,%r15,4), %r10d
movl %r10d, (%r12,%r15,4)
incq %r15
cmpq $0x8, %r15
jne 0xd10d
incq %rsi
addq $0x20, %r12
addq $0x20, %rdi
cmpq %rdx, %rsi
jne 0xd0ee
imull %r8d, %r9d
movl %r9d, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0xd1aa
testl %edx, %edx
jle 0xd1cc
movq %rax, %rcx
subq %rbx, %rcx
movl %edx, %edx
xorl %esi, %esi
movq %rax, %rdi
movq %rcx, %r8
movq %rsi, %r9
shlq $0x5, %r9
movl (%rax,%r9), %r10d
movl %r10d, (%rcx,%r9)
movl 0x4(%rax,%r9), %r10d
movl %r10d, 0x4(%rcx,%r9)
movl $0x2, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
cmpq $0x8, %r9
jne 0xd178
incq %rsi
addq $0x20, %r8
addq $0x20, %rdi
cmpq %rdx, %rsi
jne 0xd159
jmp 0xd1cc
imull %r8d, %r9d
addl %edx, %r9d
movl %r9d, 0x4(%rsp)
movq %rax, %r14
movslq %r9d, %rsi
shlq $0x5, %rsi
addq %r14, %rsi
movslq %r11d, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movq %r14, %rcx
subq %rbx, %rcx
movq %r14, %rdi
callq 0xcbd9
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq