// File: /workspace/github_cpp_makefile_star2_O2/Baobaobear[P]sort/./sortlib.hpp
// Function: baobao::TestClass* baobao::internal::tim_sort_create_run<baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, std::less<baobao::TestClass>)
RandomAccessIterator tim_sort_create_run(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
    if (end - beg < timsort_last_run_threshold) // must bigger than timsort_min_run
    {
        insert_sort(beg, end, compare);
        return end;
    }

    bool ascending;
    RandomAccessIterator run_end = beg + 3;

    if (compare(*(beg + 1), *beg))
    {
        if (!compare(*(beg + 2), *beg))
        {
            std::swap(*beg, *(beg + 1));
            ascending = true;
        }
        else if (compare(*(beg + 2), *(beg + 1)))
        {
            ascending = false;
        }
        else
        {
            std::swap(*(beg + 2), *(beg + 1));
            ascending = false;
        }
    }
    else
    {
        if (compare(*(beg + 2), *beg))
        {
            std::swap(*beg, *(beg + 1));
            ascending = false;
        }
        else if (!compare(*(beg + 2), *(beg + 1)))
        {
            ascending = true;
        }
        else
        {
            std::swap(*(beg + 2), *(beg + 1));
            ascending = true;
        }
    }

    if (!ascending)
    {
        while (run_end < end && compare(*run_end, *(run_end - 1)))
        {
            ++run_end;
        }

        if (run_end - beg < timsort_min_run)
        {
            insert_sort_part_rev(beg, run_end, beg + timsort_min_run, compare);
            run_end = beg + timsort_min_run;
            while (run_end < end && compare(*run_end, *(run_end - timsort_insert_gap)))
            {
                unguarded_insert_single_rev(run_end, compare);
                ++run_end;
            }
        }

        // reverse
        typename std::iterator_traits<RandomAccessIterator>::value_type t;
        for (RandomAccessIterator b = beg, e = run_end - 1; b < e; ++b, --e)
        {
            t = *b; *b = *e; *e = t;
        }
    }
    else
    {
        while (run_end < end && !compare(*run_end, *(run_end - 1)))
        {
            ++run_end;
        }

        if (run_end - beg < timsort_min_run)
        {
            insert_sort_part(beg, run_end, beg + timsort_min_run, compare);
            run_end = beg + timsort_min_run;
            while (run_end < end && !compare(*run_end, *(run_end - timsort_insert_gap)))
            {
                unguarded_insert_single(run_end, compare);
                ++run_end;
            }
        }
    }

    return run_end;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x7ff, %rax            # imm = 0x7FF
jg 0x75ad
movq %r14, %rdi
movq %rbx, %rsi
callq 0x57c0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x20(%r14), %rsi
movl (%r14), %r8d
movl 0x20(%r14), %eax
leaq 0x40(%r14), %rcx
movl 0x40(%r14), %edx
movq %r14, %rdi
cmpl %r8d, %eax
jge 0x75d6
cmpl %r8d, %edx
jge 0x7645
movq %rcx, %rdi
cmpl %eax, %edx
jge 0x75db
jmp 0x75e0
cmpl %r8d, %edx
jge 0x763e
callq 0x4106
pushq $0x60
popq %rax
leaq (%r14,%rax), %rsi
cmpq %rbx, %rsi
jae 0x75fd
movl (%r14,%rax), %ecx
cmpl -0x20(%r14,%rax), %ecx
jge 0x75fd
addq $0x20, %rax
jmp 0x75e3
cmpq $0x3ff, %rax            # imm = 0x3FF
jg 0x76a0
leaq 0x400(%r14), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x7ab4
cmpq %rbx, %r15
jae 0x76a5
movl (%r15), %eax
cmpl -0x100(%r15), %eax
jge 0x76a5
movq %r15, %rdi
callq 0x7b41
addq $0x20, %r15
jmp 0x761b
movq %rcx, %rdi
cmpl %eax, %edx
jge 0x764a
callq 0x4106
pushq $0x60
popq %rax
leaq (%r14,%rax), %rsi
cmpq %rbx, %rsi
jae 0x7667
movl (%r14,%rax), %ecx
cmpl -0x20(%r14,%rax), %ecx
jl 0x7667
addq $0x20, %rax
jmp 0x764d
cmpq $0x3ff, %rax            # imm = 0x3FF
jg 0x76ed
leaq 0x400(%r14), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x5e3b
cmpq %rbx, %r15
jae 0x76f5
movl (%r15), %eax
cmpl -0x100(%r15), %eax
jl 0x76f5
movq %r15, %rdi
callq 0x7b9e
addq $0x20, %r15
jmp 0x7681
movq %rsi, %rbx
jmp 0x76a8
movq %r15, %rbx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x39d2
leaq -0x20(%rbx), %r12
cmpq %r12, %r14
jae 0x759e
movq %r15, %rdi
movq %r14, %rsi
callq 0x4164
movq %r14, %rdi
movq %r12, %rsi
callq 0x4164
movq %r12, %rdi
movq %r15, %rsi
callq 0x4164
addq $0x20, %r14
addq $-0x20, %r12
jmp 0x76b9
movq %rsi, %rbx
jmp 0x759e
movq %r15, %rbx
jmp 0x759e