// File: /workspace/github_cpp_makefile_star2_O2/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 0x7e98
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 0x7574
movq %rax, (%r12)
cmpq %rbx, %rax
jae 0x7e87
movq %rax, %r14
movq %rbx, %r12
subq 0x10(%rsp), %r12
cmpq $0x4001, %r12           # imm = 0x4001
jb 0x7d49
sarq $0x5, %r12
movabsq $0x7ffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFF
movq %r12, %rax
shrq %rax
cmpq %r15, %rax
jbe 0x7dcf
movq %r15, %rbp
shlq $0x5, %rbp
cmpq %rcx, %r15
pushq $-0x1
popq %rdi
cmovbeq %rbp, %rdi
callq 0x2030
movq %rax, %r12
testq %r15, %r15
je 0x7d00
movq %r12, %r13
movq %r13, %rdi
callq 0x39d2
addq $0x20, %r13
addq $-0x20, %rbp
jne 0x7cee
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %rbp
cmpq %rbx, %r14
jae 0x7e53
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7574
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 0x8066
jmp 0x7d0a
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x280, %rdi            # imm = 0x280
callq 0x39d2
addq $0x20, %r15
cmpq $0x4000, %r15           # imm = 0x4000
jne 0x7d4c
leaq 0x280(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
cmpq %rbx, %r14
jae 0x7db3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7574
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 0x7e99
jmp 0x7d7b
movq 0x8(%rsp), %rdx
leaq 0x280(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7fd0
jmp 0x7e87
pushq $0x2
popq %rsi
movq %r12, %rax
cqto
idivq %rsi
movq %rax, %r13
shlq $0x5, %r13
cmpq %rcx, %rax
pushq $-0x1
popq %rdi
cmovbeq %r13, %rdi
callq 0x2030
movq %rax, %r15
incq %r12
cmpq $0x3, %r12
jb 0x7e11
movq %r15, %r12
movq %r12, %rdi
callq 0x39d2
addq $0x20, %r12
addq $-0x20, %r13
jne 0x7dff
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
cmpq %rbx, %r14
jae 0x7e6d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7574
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 0x7e99
jmp 0x7e1b
movq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x81ac
movq %r12, %rdi
jmp 0x7e82
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x7fd0
movq %r15, %rdi
callq 0x21d0
addq $0x4288, %rsp           # imm = 0x4288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq