// File: /workspace/github_cpp_makefile_star2_O2/Baobaobear[P]sort/./sortlib.hpp
// Function: std::pair<baobao::TestClass*, baobao::TestClass*> baobao::internal::find_swap_bound<baobao::TestClass*, std::less<baobao::TestClass>>(baobao::TestClass*, baobao::TestClass*, baobao::TestClass*, std::less<baobao::TestClass>)
std::pair<RandomAccessIterator, RandomAccessIterator> find_swap_bound(RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
    if (end - beg < find_swap_bound_optimize_threshold)
    {
        RandomAccessIterator pivot_r = mid + (end - mid) / 2 + 1;
        RandomAccessIterator pivot_l = std::upper_bound(beg, mid, *pivot_r, compare);

        pivot_r = std::lower_bound(pivot_r, end, *pivot_l, compare);

        return std::make_pair(pivot_l, pivot_r);
    }
    else
    {
        typedef typename std::iterator_traits<RandomAccessIterator>::difference_type diff_type;

        RandomAccessIterator pivot_l = beg;
        RandomAccessIterator pivot_l_map = std::lower_bound(mid, end, *pivot_l, compare);

        if (pivot_l_map - mid > (end - mid) / 2 || mid - pivot_l <= pivot_l_map - mid)
        {
            return std::make_pair(pivot_l, pivot_l_map);
        }

        RandomAccessIterator pivot_r = std::lower_bound(beg, mid, *(end - 1), compare);
        RandomAccessIterator pivot_r_map = std::lower_bound(mid, end, *pivot_r, compare);

        if (mid - pivot_r > (mid - beg) / 2 || mid - pivot_r >= pivot_r_map - mid)
        {
            return std::make_pair(pivot_r, pivot_r_map);
        }

        do
        {
            RandomAccessIterator pivot_m = pivot_l + (pivot_r - pivot_l) / 2;
            pivot_m = std::lower_bound(pivot_l, pivot_r, *pivot_m, compare);
            if (pivot_m == pivot_l)
            {
                pivot_m = std::upper_bound(pivot_l, pivot_r, *pivot_m, compare);
                if (pivot_m == pivot_r)
                {
                    {
                        diff_type pivot_m_diff = (mid - pivot_l) - (pivot_l_map - mid);
                        if (pivot_l + pivot_m_diff <= pivot_r)
                        {
                            return std::make_pair(pivot_l + pivot_m_diff, pivot_l_map);
                        }
                    }
                    {
                        diff_type pivot_m_diff = (mid - pivot_r) - (pivot_r_map - mid);
                        if (pivot_l_map - pivot_m_diff <= pivot_r_map)
                        {
                            return std::make_pair(pivot_r, pivot_r_map + pivot_m_diff);
                        }
                    }
                    return std::make_pair(pivot_l, pivot_l_map);
                }
            }
            RandomAccessIterator pivot_m_map = std::lower_bound(pivot_l_map, pivot_r_map, *pivot_m, compare);
            diff_type pivot_m_diff = (mid - pivot_m) - (pivot_m_map - mid);
            if (pivot_m_diff > 0)
            {
                pivot_l = pivot_m;
                pivot_l_map = pivot_m_map;
            }
            else if (pivot_m_diff < 0)
            {
                pivot_r = pivot_m;
                pivot_r_map = pivot_m_map;
            }
            else
            {
                return std::make_pair(pivot_m, pivot_m_map);
            }
        } while (true);

        return std::make_pair(pivot_l, pivot_l_map);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, %rax
subq %rdi, %rax
cmpq $0x1fff, %rax           # imm = 0x1FFF
jg 0x66ce
movq %r13, %rax
subq %rbx, %rax
sarq $0x5, %rax
pushq $0x2
popq %rcx
cqto
idivq %rcx
shlq $0x5, %rax
leaq (%rbx,%rax), %r15
addq $0x20, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6997
movq %rax, %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x69cd
movq %rax, %rdx
jmp 0x67f9
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x69cd
movq %rax, %r15
movq %rax, %rcx
subq %rbx, %rcx
sarq $0x5, %rcx
movq %r13, %rax
subq %rbx, %rax
sarq $0x5, %rax
pushq $0x2
popq %rsi
cqto
idivq %rsi
cmpq %rax, %rcx
jg 0x67f3
movq %rbx, %rbp
subq %r14, %rbp
sarq $0x5, %rbp
cmpq %rcx, %rbp
jle 0x67f3
leaq -0x20(%r13), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x69cd
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x69cd
movq %rax, %r13
movq %rbx, %rcx
subq %r12, %rcx
movq %rcx, %rsi
sarq $0x5, %rsi
movq %rbp, %rax
cqto
pushq $0x2
popq %rdi
idivq %rdi
cmpq %rax, %rsi
setg %al
movq %r13, %rdx
subq %rbx, %rdx
cmpq %rdx, %rcx
setge %cl
orb %al, %cl
jne 0x680b
movq %r15, %rdx
movq %r14, %rbp
movq %rbp, %r14
movq %rdx, %r15
movq %r12, %rax
subq %rbp, %rax
sarq $0x5, %rax
cqto
pushq $0x2
popq %rcx
idivq %rcx
shlq $0x5, %rax
leaq (%rax,%rbp), %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x69cd
movq %rax, %rbp
cmpq %r14, %rax
jne 0x67bb
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x6997
movq %rax, %rbp
cmpq %r12, %rax
je 0x6813
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x69cd
movq %rax, %rdx
movq %rbx, %rax
subq %rbp, %rax
sarq $0x5, %rax
movq %rdx, %rcx
subq %rbx, %rcx
sarq $0x5, %rcx
subq %rcx, %rax
jg 0x6772
movq %rbp, %r12
movq %rdx, %r13
js 0x676c
jmp 0x67f9
movq %r14, %rbp
movq %r15, %rdx
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rbp
movq %r13, %rdx
jmp 0x67f9
movq %rbx, %rbp
subq %r14, %rbp
shrq $0x5, %rbp
movq %r15, %rax
subq %rbx, %rax
shrq $0x5, %rax
subq %rax, %rbp
shlq $0x5, %rbp
addq %r14, %rbp
cmpq %r12, %rbp
jbe 0x67f6
movq %r13, %rax
subq %rbx, %rax
subq %r12, %rbx
shrq $0x5, %rbx
shrq $0x5, %rax
subq %rax, %rbx
shlq $0x5, %rbx
movq %r15, %rax
subq %rbx, %rax
addq %r13, %rbx
cmpq %r13, %rax
cmovbeq %r12, %r14
cmovaq %r15, %rbx
movq %r14, %rbp
movq %rbx, %rdx
jmp 0x67f9