// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xb8(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
cmpq $0x100, %rax            # imm = 0x100
jge 0xe9af
movq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq 0x18(%rsp), %rax
shlq $0x5, %rcx
addq %rcx, %rax
addq $0x20, %rax
movq %rax, 0xa0(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0xf0e0
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0xf130
movq %rax, 0xa0(%rsp)
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
movq 0xb8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x88(%rsp), %rdx
callq 0xf130
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jg 0xea61
movq 0xb0(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq 0x80(%rsp), %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %rcx, %rax
jg 0xea8b
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
addq $-0x20, %rdx
callq 0xf130
movq %rax, 0x70(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0xf130
movq %rax, 0x60(%rsp)
movq 0xb0(%rsp), %rax
movq 0x70(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq %rax, 0x8(%rsp)
movq 0xb0(%rsp), %rax
movq 0xb8(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jg 0xeb44
movq 0xb0(%rsp), %rax
movq 0x70(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq 0x60(%rsp), %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %rcx, %rax
jl 0xeb68
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
jmp 0xeb6a
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
movq 0x70(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq (%rsp), %rax
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0xf130
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
cmpq 0x88(%rsp), %rax
jne 0xed32
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0xf0e0
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
cmpq 0x70(%rsp), %rax
jne 0xed30
movq 0xb0(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq 0x80(%rsp), %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x88(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq 0x70(%rsp), %rax
ja 0xec89
movq 0x88(%rsp), %rdi
movq 0x40(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq 0x80(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
movq 0xb0(%rsp), %rax
movq 0x70(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq 0x60(%rsp), %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x80(%rsp), %rax
xorl %ecx, %ecx
subq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq 0x60(%rsp), %rax
ja 0xed06
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x38(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
jmp 0xed32
movq 0x80(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0xf130
movq %rax, 0x30(%rsp)
movq 0xb0(%rsp), %rax
movq 0x50(%rsp), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movq 0x30(%rsp), %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
sarq $0x5, %rcx
subq %rcx, %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jle 0xeda2
movq 0x50(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0xede3
cmpq $0x0, 0x20(%rsp)
jge 0xedc0
movq 0x50(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0xede1
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
jmp 0xee16
jmp 0xede3
jmp 0xede5
movb $0x1, %al
testb $0x1, %al
jne 0xeb6a
jmp 0xedf1
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xf180
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rdx
addq $0xd8, %rsp
retq
nop