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

    if (nblock == 0) {
        int l = nblock2*lblock;
        if (havebuf) grail_MergeLeft(arr, arr + l, arr + l + llast, arr - lblock, compare);
        else grail_MergeWithoutBuffer(arr, arr + l, arr + l + llast, 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) {
            if (havebuf) grail_swapN(prest - lblock, prest, lrest);
            prest = pidx;
            lrest = lblock;
        }
        else {
            if (havebuf) {
                grail_SmartMergeWithBuffer(prest, &lrest, &frest, prest + lrest + lblock, prest - lblock, compare);
            }
            else {
                grail_SmartMergeWithoutBuffer(prest, &lrest, &frest, prest + lrest + lblock, compare);
            }

        }
    }
    prest = pidx - lrest;
    if (llast) {
        if (frest) {
            if (havebuf) grail_swapN(prest - lblock, prest, lrest);
            prest = pidx;
            lrest = lblock*nblock2;
            frest = 0;
        }
        else {
            lrest += lblock*nblock2;
        }
        if (havebuf) grail_MergeLeft(prest, prest + lrest, prest + lrest + llast, prest - lblock, compare);
        else grail_MergeWithoutBuffer(prest, prest + lrest, prest + lrest + llast, compare);
    }
    else {
        if (havebuf) grail_swapN(prest, prest - lblock, lrest);
    }
}

// Assembly:
subq $0x68, %rsp
movb %r9b, %al
movl 0x78(%rsp), %r9d
movl 0x70(%rsp), %r9d
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movl %ecx, 0x44(%rsp)
movl %r8d, 0x40(%rsp)
andb $0x1, %al
movb %al, 0x3f(%rsp)
cmpl $0x0, 0x44(%rsp)
jne 0x174eb
movl 0x70(%rsp), %eax
imull 0x40(%rsp), %eax
movl %eax, 0x1c(%rsp)
testb $0x1, 0x3f(%rsp)
je 0x174ae
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x48(%rsp), %rdx
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x78(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movq 0x48(%rsp), %rcx
movslq 0x40(%rsp), %r8
xorl %eax, %eax
subq %r8, %rax
shlq $0x5, %rax
addq %rax, %rcx
callq 0x16df0
jmp 0x174e6
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x48(%rsp), %rdx
movslq 0x1c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x78(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
callq 0x167d0
jmp 0x177d8
movl 0x40(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x67(%rsp), %rdi
callq 0xcd80
movb %al, %dl
movl $0x1, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movslq 0x40(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl $0x1, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0x1768e
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rsi
movslq 0x14(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x50(%rsp), %rdx
leaq 0x67(%rsp), %rdi
callq 0xcd80
movb %al, %dl
movl $0x1, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0x175e1
testb $0x1, 0x3f(%rsp)
je 0x175ca
movq 0x30(%rsp), %rdi
movslq 0x40(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq 0x30(%rsp), %rsi
movslq 0x24(%rsp), %rdx
callq 0x16ff0
movq 0x28(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x17664
testb $0x1, 0x3f(%rsp)
je 0x17631
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rcx
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movslq 0x40(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movq 0x30(%rsp), %r8
movslq 0x40(%rsp), %rdx
xorl %eax, %eax
subq %rdx, %rax
shlq $0x5, %rax
addq %rax, %r8
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x179e0
jmp 0x17662
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rcx
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movslq 0x40(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rcx
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x17b70
jmp 0x17664
jmp 0x17666
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movl 0x40(%rsp), %ecx
movq 0x28(%rsp), %rax
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
jmp 0x17538
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x78(%rsp)
je 0x177aa
cmpl $0x0, 0x20(%rsp)
je 0x17708
testb $0x1, 0x3f(%rsp)
je 0x176e7
movq 0x30(%rsp), %rdi
movslq 0x40(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq 0x30(%rsp), %rsi
movslq 0x24(%rsp), %rdx
callq 0x16ff0
movq 0x28(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x40(%rsp), %eax
imull 0x70(%rsp), %eax
movl %eax, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
jmp 0x17719
movl 0x40(%rsp), %eax
imull 0x70(%rsp), %eax
addl 0x24(%rsp), %eax
movl %eax, 0x24(%rsp)
testb $0x1, 0x3f(%rsp)
je 0x17770
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rsi
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x30(%rsp), %rdx
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x78(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movq 0x30(%rsp), %rcx
movslq 0x40(%rsp), %r8
xorl %eax, %eax
subq %r8, %rax
shlq $0x5, %rax
addq %rax, %rcx
callq 0x16df0
jmp 0x177a8
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rsi
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x30(%rsp), %rdx
movslq 0x24(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movslq 0x78(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdx
callq 0x167d0
jmp 0x177d8
testb $0x1, 0x3f(%rsp)
je 0x177d6
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rsi
movslq 0x40(%rsp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x5, %rax
addq %rax, %rsi
movslq 0x24(%rsp), %rdx
callq 0x16ff0
jmp 0x177d8
addq $0x68, %rsp
retq
nopl (%rax)