// File: 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:
subq $0x78, %rsp
movl 0x80(%rsp), %eax
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
movl %r8d, 0x50(%rsp)
movl %r9d, 0x4c(%rsp)
cmpl $0x0, 0x54(%rsp)
jne 0x17112
movl 0x4c(%rsp), %eax
imull 0x50(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x58(%rsp), %rdi
movq 0x58(%rsp), %rsi
movslq 0x2c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x58(%rsp), %rdx
movslq 0x2c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x80(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movq 0x58(%rsp), %rcx
movslq 0x50(%rsp), %r8
xorl %eax, %eax
subq %r8, %rax
shlq $0x5, %rax
addq %rax, %rcx
callq 0x16cf0
jmp 0x17406
movl 0x50(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
leaq 0x77(%rsp), %rdi
callq 0xcd80
movb %al, %dl
movl $0x1, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movslq 0x50(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl $0x1, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x172a1
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rsi
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x60(%rsp), %rdx
leaq 0x77(%rsp), %rdi
callq 0xcd80
movb %al, %dl
movl $0x1, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x30(%rsp), %eax
jne 0x17230
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x1721c
movq 0x40(%rsp), %rsi
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x40(%rsp), %rdi
movslq 0x50(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rdi
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x8710
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x171cd
movq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x17277
movq 0x40(%rsp), %rdi
movq 0x40(%rsp), %rcx
movslq 0x34(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movslq 0x50(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movq 0x40(%rsp), %r8
movslq 0x50(%rsp), %rdx
xorl %eax, %eax
subq %rdx, %rax
shlq $0x5, %rax
addq %rax, %r8
leaq 0x34(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x177e0
jmp 0x17279
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x50(%rsp), %ecx
movq 0x38(%rsp), %rax
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x1715f
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x80(%rsp)
je 0x173ad
cmpl $0x0, 0x30(%rsp)
je 0x17349
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x17328
movq 0x40(%rsp), %rsi
movslq 0x14(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x40(%rsp), %rdi
movslq 0x50(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rdi
movslq 0x14(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x8710
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x172d9
movq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x50(%rsp), %eax
imull 0x4c(%rsp), %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
jmp 0x1735a
movl 0x50(%rsp), %eax
imull 0x4c(%rsp), %eax
addl 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x40(%rsp), %rdi
movq 0x40(%rsp), %rsi
movslq 0x34(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x40(%rsp), %rdx
movslq 0x34(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x80(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movq 0x40(%rsp), %rcx
movslq 0x50(%rsp), %r8
xorl %eax, %eax
subq %r8, %rax
shlq $0x5, %rax
addq %rax, %rcx
callq 0x16cf0
jmp 0x17406
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x17404
movq 0x40(%rsp), %rsi
movslq 0xc(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x40(%rsp), %rdi
movslq 0x50(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rdi
movslq 0xc(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x8710
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x173b5
jmp 0x17406
addq $0x78, %rsp
retq
nopl (%rax,%rax)