// File: /workspace/github_cpp_makefile_star2_O3/Baobaobear[P]sort/./sortlib.hpp
// Function: void baobao::internal::tim_sort_buffer<true, baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, unsigned long, std::less<baobao::TestClass>)
void tim_sort_buffer(RandomAccessIterator beg, RandomAccessIterator end, size_t bufsize, Comp compare)
{
    typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
    if (end - beg > 1)
    {
        RandomAccessIterator run_stack[78]; // ln(2^32)/ln(4/3) = 77.1
        RandomAccessIterator* stack_top = run_stack + 1;
        run_stack[0] = beg;
        run_stack[1] = beg = tim_sort_create_run(beg, end, compare);

        if (end > beg && (end - run_stack[0]) * sizeof(value_type) > merge_sort_stack_buffer_size)
        {
            if (bufsize >= (size_t)(end - run_stack[0]) / 2)
            {
                value_type* buf = safecopy ? new value_type[(end - run_stack[0]) / 2]
                    : (value_type*)malloc((end - run_stack[0]) / 2 * sizeof(value_type));
                while (beg < end)
                {
                    RandomAccessIterator run = tim_sort_create_run(beg, end, compare);
                    *++stack_top = run;
                    beg = run;

                    //tim_sort_stack_merge<T, RandomAccessIterator, Comp>(buf, run_stack, stack_top, end);
                    tim_sort_stack_merge_13<safecopy>(buf, run_stack, stack_top, end, compare);
                }

                tim_sort_force_stack_merge<safecopy>(buf, run_stack, stack_top, compare);
                if (safecopy)
                    delete[] buf;
                else
                    free(buf);
            }
            else
            {
                value_type* buf = safecopy ? new value_type[bufsize]
                    : (value_type*)malloc(bufsize * sizeof(value_type));
                while (beg < end)
                {
                    RandomAccessIterator run = tim_sort_create_run(beg, end, compare);
                    *++stack_top = run;
                    beg = run;

                    //tim_sort_stack_merge<T, RandomAccessIterator, Comp>(buf, run_stack, stack_top, end);
                    tim_sort_stack_merge_with_buffer_13<safecopy>(buf, bufsize, run_stack, stack_top, end, compare);
                }

                tim_sort_force_stack_merge_with_buffer<safecopy>(buf, bufsize, run_stack, stack_top, compare);
                if (safecopy)
                    delete[] buf;
                else
                    free(buf);
            }
        }
        else if (end > beg)
        {
            typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
            value_type buf[merge_sort_stack_buffer_size / sizeof(value_type)];
            while (beg < end)
            {
                RandomAccessIterator run = tim_sort_create_run(beg, end, compare);
                *++stack_top = run;
                beg = run;

                //tim_sort_stack_merge<T, RandomAccessIterator, Comp>(buf, run_stack, stack_top, end);
                tim_sort_stack_merge_13<safecopy>(buf, run_stack, stack_top, end, compare);
            }

            tim_sort_force_stack_merge<safecopy>(buf, run_stack, stack_top, compare);
        }
    }
}

// Assembly:
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x21, %rax
jl 0xa04d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4288, %rsp           # imm = 0x4288
movq %rdx, %r15
movq %rsi, %rbx
leaq 0x18(%rsp), %r12
movq %r12, 0x8(%rsp)
movq %rdi, -0x8(%r12)
callq 0x9307
movq %rax, (%r12)
cmpq %rbx, %rax
jae 0xa03c
movq %rax, %r14
movq %rbx, %r13
subq 0x10(%rsp), %r13
cmpq $0x4001, %r13           # imm = 0x4001
jb 0x9f2f
sarq $0x5, %r13
movabsq $0x7ffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFF
movq %r13, %rcx
shrq %rcx
cmpq %r15, %rcx
jbe 0x9fa2
movq %r15, %r13
shlq $0x5, %r13
cmpq %rax, %r15
movq $-0x1, %rdi
cmovbeq %r13, %rdi
callq 0x2030
movq %rax, %r12
testq %r15, %r15
je 0x9ecf
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20d0
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %rbp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9307
movq %rax, %r14
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r14, 0x8(%rax)
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq 0xa282
cmpq %rbx, %r14
jb 0x9ed9
movq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0xa422
movq %r12, %rdi
jmp 0xa037
leaq 0x280(%rsp), %r15
movl $0x4000, %edx           # imm = 0x4000
movq %r15, %rdi
xorl %esi, %esi
callq 0x20d0
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9307
movq %rax, %r14
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r14, 0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0xa04e
cmpq %rbx, %r14
jb 0x9f50
movq 0x8(%rsp), %rdx
leaq 0x280(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa1de
jmp 0xa03c
movq %r13, %rcx
shrq $0x3f, %rcx
addq %r13, %rcx
sarq %rcx
movq %rcx, %r12
shlq $0x5, %r12
cmpq %rax, %rcx
movq $-0x1, %rdi
cmovbeq %r12, %rdi
callq 0x2030
movq %rax, %r15
incq %r13
cmpq $0x3, %r13
jb 0x9fe2
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20d0
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9307
movq %rax, %r14
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r14, 0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0xa04e
cmpq %rbx, %r14
jb 0x9fec
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0xa1de
movq %r15, %rdi
callq 0x21c0
addq $0x4288, %rsp           # imm = 0x4288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq