// File: /workspace/github_cpp_makefile_star2_O2/estan[P]tsp/tsp.cpp
// Function: threeOpt(std::vector<unsigned short, std::allocator<unsigned short>>&, Matrix<unsigned int> const&, Matrix<unsigned short> const&, std::vector<unsigned short, std::allocator<unsigned short>>&, unsigned int&, unsigned int, std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>> const&)
inline void threeOpt(vector<uint16_t>& tour, const Matrix<uint32_t>& d,
        const Matrix<uint16_t>& neighbor, vector<uint16_t>& position,
        uint32_t& max, uint32_t min,
        const chrono::time_point<chrono::high_resolution_clock>& deadline) {
    const size_t N = d.rows(); // Number of cities.

    // Candidate edges PQ, RS, TU and their positions in tour.
    uint16_t P, Q, R, S, T, U;
    size_t P_i, Q_i, R_i, S_i, T_i, U_i;

    // AB, CD, EF is PQ, RS, TU, but in tour order.
    uint16_t A, B, C, D, E, F;
    size_t A_i, B_i, C_i, D_i, E_i, F_i;

    bool locallyOptimal = false;
    while (!locallyOptimal) {
        locallyOptimal = true;

        // For each edge PQ.
        for (size_t i = 0; i < N; ++i) {
            P_i = i;
            Q_i = (P_i + 1) % N;
            P = tour[P_i];
            Q = tour[Q_i];

            if (chrono::high_resolution_clock::now() > deadline)
                return; // Deadline has passed, return early.

            // For each edge RS (S j:th nearest neighbor of P).
            for (size_t j = 0; j < neighbor.cols(); ++j) {
                S_i = position[neighbor[P][j]];
                R_i = (S_i + N - 1) % N;
                R = tour[R_i];
                S = tour[S_i];

                if (P == R || R == Q) // RS same as, or follows, PQ.
                    continue; // Go to next edge RS.

                if (d[P][S] + 2 * min > d[P][Q] + 2 * max)
                    break; // Go to next edge PQ.

                if (d[P][S] + 2 * min > d[P][Q] + d[R][S] + max)
                    continue; // Go to next edge RS.

                // For each edge TU (U k:th nearest neighbor of P).
                for (size_t k = 0; k < neighbor.cols(); ++k) {
                    U_i = position[neighbor[P][k]];
                    T_i = (U_i + N - 1) % N;
                    T = tour[T_i];
                    U = tour[U_i];

                    if (U == S || // TU same as RS.
                        T == S || // TU follows RS.
                        U == R || // TU preceeds RS.
                        T == P || // TU same as PQ.
                        T == Q)   // TU follows PQ.
                        continue; // Go to next edge TU.

                    if (d[P][S] + d[Q][U] + min > d[P][Q] + d[R][S] + max)
                        break; // Go to next edge RS.

                    // Let AB, CD, EF be the edges PQ, RS, TU in tour order.
                    ordered(A, A_i, B, B_i, C, C_i, D, D_i, E, E_i, F, F_i,
                            P, P_i, Q, Q_i, R, R_i, S, S_i, T, T_i, U, U_i);

                    // Try exchanging AB, CD and EF for another edge triple.
                    // See 3opt_cases.png for an illustration.
                    bool changed = false;
                    uint32_t d_AB_CD_EF = d[A][B] + d[C][D] + d[E][F];
                    if (d[D][A] + d[F][B] + d[C][E] < d_AB_CD_EF) {
                        // Change AB, CD, EF to DA, FB, CE.
                        reverse(tour, F_i, A_i, position);
                        reverse(tour, D_i, E_i, position);
                        max = maximum(max, d[D][A], d[F][B], d[C][E]);
                        changed = true;
                    } else if (d[B][D] + d[E][A] + d[F][C] < d_AB_CD_EF) {
                        // Change AB, CD, EF to BD, EA, FC.
                        reverse(tour, F_i, A_i, position);
                        reverse(tour, B_i, C_i, position);
                        max = maximum(max, d[B][D], d[E][A], d[F][C]);
                        changed = true;
                    } else if (d[A][C] + d[B][E] + d[D][F] < d_AB_CD_EF) {
                        // Change AB, CD, EF to AC, BE, DF.
                        reverse(tour, B_i, C_i, position);
                        reverse(tour, D_i, E_i, position);
                        max = maximum(max, d[A][C], d[B][E], d[D][F]);
                        changed = true;
                    } else if (d[B][E] + d[D][A] + d[F][C] < d_AB_CD_EF) {
                        // Change AB, CD, EF to BE, DA, FC.
                        reverse(tour, A_i, F_i, position);
                        reverse(tour, B_i, C_i, position);
                        reverse(tour, D_i, E_i, position);
                        max = maximum(max, d[B][E], d[D][A], d[F][C]);
                        changed = true;
                    }

                    if (changed) {
                        locallyOptimal = false;
                        goto next_PQ; // Go to next edge PQ.
                    }
                }
            }
            next_PQ: continue;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r8, 0xd8(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x110(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0x18(%rsi), %r13
leaq -0x1(%r13), %r15
movq %r9, 0x118(%rsp)
leal (%r9,%r9), %eax
movq %rax, 0x120(%rsp)
movb $0x1, 0xe(%rsp)
movq $0x0, 0x30(%rsp)
movq %r13, 0x68(%rsp)
movq %r15, 0x10(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movq 0x30(%rsp), %rdx
cmpq %r13, %rdx
setae 0xe(%rsp)
jae 0x2caf
leaq 0x1(%rdx), %rcx
cmpq %r13, %rcx
movl $0x0, %esi
movq %rcx, 0x30(%rsp)
cmovneq %rcx, %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rdx, 0xe8(%rsp)
movzwl (%rax,%rdx,2), %ebx
movq %rsi, 0x98(%rsp)
movzwl (%rax,%rsi,2), %r14d
callq 0x1030
movq 0x1c0(%rsp), %rcx
cmpq %rax, (%rcx)
jl 0x2cc4
movq 0x110(%rsp), %rax
movq 0x20(%rax), %r9
movq %r9, %r12
imulq %rbx, %r12
addq %r12, %r12
addq (%rax), %r12
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x100(%rsp)
movq 0xd0(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rax), %rdx
movq %rdx, %rax
imulq %rbx, %rax
movq %rax, 0x90(%rsp)
leaq (%rcx,%rax,4), %rax
movq %rax, 0x138(%rsp)
movq 0xd8(%rsp), %rax
movl (%rax), %eax
movq %rax, 0xe0(%rsp)
addl %eax, %eax
movq %rax, 0x130(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rdx, %rax
imulq %r14, %rax
movq %rax, 0xf0(%rsp)
leaq (%rcx,%rax,4), %rax
movq %rax, 0x170(%rsp)
xorl %eax, %eax
movq 0xe8(%rsp), %rsi
movq %rbx, %r10
movq %r14, %r11
movq %r9, 0x150(%rsp)
movq %r12, 0x148(%rsp)
movq %rcx, 0xf8(%rsp)
movq %r14, 0x140(%rsp)
cmpq %r9, %rax
je 0x2575
movq %rax, 0x178(%rsp)
movzwl (%r12,%rax,2), %eax
movq 0x108(%rsp), %rcx
movzwl (%rcx,%rax,2), %r14d
leaq (%r15,%r14), %rax
xorl %edx, %edx
divq %r13
movq 0x100(%rsp), %rbx
movzwl (%rbx,%rdx,2), %eax
cmpw %ax, %r10w
je 0x2779
cmpw %r11w, %ax
je 0x2779
movq %rdx, %rbp
movq %r13, %r15
movzwl %ax, %edi
movzwl (%rbx,%r14,2), %r13d
movq 0x138(%rsp), %rcx
movl (%rcx,%r13,4), %edx
movq 0x120(%rsp), %rax
movq %rdx, 0xb0(%rsp)
addl %eax, %edx
movl (%rcx,%r11,4), %eax
movq 0x130(%rsp), %rcx
addl %eax, %ecx
cmpl %ecx, %edx
ja 0x2ab3
movq 0xa8(%rsp), %r8
imulq %rdi, %r8
movq 0xf8(%rsp), %rcx
movq %r8, 0xa0(%rsp)
leaq (%rcx,%r8,4), %rcx
movq %r13, 0x60(%rsp)
movl (%rcx,%r13,4), %r13d
movq 0xe0(%rsp), %rcx
addl %eax, %ecx
movl %r13d, %r8d
addl %r13d, %ecx
cmpl %ecx, %edx
movq %r15, %r13
movq 0x10(%rsp), %r15
jbe 0x2789
movq 0x178(%rsp), %rax
incq %rax
jmp 0x26a6
movq 0xb0(%rsp), %rcx
addl 0x118(%rsp), %ecx
movq %rcx, 0xb0(%rsp)
addl %eax, %r8d
addl 0xe0(%rsp), %r8d
movl %r8d, 0xcc(%rsp)
movq 0xa8(%rsp), %rax
imulq 0x60(%rsp), %rax
movq %rax, 0x128(%rsp)
xorl %eax, %eax
movq %rdi, 0x168(%rsp)
movq %rbp, 0x160(%rsp)
movq %r14, 0x158(%rsp)
cmpq %rax, %r9
je 0x2779
movq %rax, 0x180(%rsp)
movzwl (%r12,%rax,2), %eax
movq 0x108(%rsp), %rcx
movzwl (%rcx,%rax,2), %ecx
leaq (%r15,%rcx), %rax
xorl %edx, %edx
divq %r13
movq %rcx, 0xc0(%rsp)
movzwl (%rbx,%rcx,2), %r8d
cmpw %di, %r8w
movq 0x60(%rsp), %rcx
je 0x2aa3
cmpw %cx, %r8w
je 0x2aa3
movzwl (%rbx,%rdx,2), %eax
cmpw %cx, %ax
je 0x2aa3
cmpw %r10w, %ax
je 0x2aa3
cmpw %r11w, %ax
je 0x2aa3
movzwl %r8w, %r8d
movq 0x170(%rsp), %rcx
movq %r8, 0x20(%rsp)
movl (%rcx,%r8,4), %ecx
addl 0xb0(%rsp), %ecx
cmpl 0xcc(%rsp), %ecx
ja 0x2779
cmpq %rsi, %rbp
jae 0x28c7
movq 0xf0(%rsp), %r9
movq 0x90(%rsp), %rcx
movq 0xa0(%rsp), %r13
movq %r10, 0x50(%rsp)
movq %rdi, %rbx
movq %r11, 0x58(%rsp)
movq 0x60(%rsp), %r8
movq %rbp, 0x40(%rsp)
movq %r14, %r12
movq %rsi, 0x38(%rsp)
movq 0x98(%rsp), %r15
cmpq %rdx, %rsi
jb 0x295f
cmpq %rdx, %rsi
setb %cl
cmpq %rsi, %rbp
setb %r8b
cmpq %rbp, %rdx
jae 0x291d
orb %cl, %r8b
movq 0xf0(%rsp), %r9
movq 0x90(%rsp), %rcx
movq 0xa0(%rsp), %r13
movq %r10, 0x50(%rsp)
movq %rdi, %rbx
movq %r11, 0x58(%rsp)
movq 0x60(%rsp), %r8
movq %rbp, 0x40(%rsp)
movq %r14, %r12
movq %rsi, 0x38(%rsp)
movq 0x98(%rsp), %r15
jne 0x295f
movq 0x128(%rsp), %r9
movq 0xa0(%rsp), %rcx
movq 0x90(%rsp), %r13
movq %rdi, 0x50(%rsp)
movq %r10, %rbx
movq 0x60(%rsp), %r8
movq %r8, 0x58(%rsp)
movq %r11, %r8
movq %rsi, 0x40(%rsp)
movq 0x98(%rsp), %r12
movq %rbp, 0x38(%rsp)
movq %r14, %r15
movq %r12, 0x80(%rsp)
movq %r15, 0x88(%rsp)
movq %rdx, 0xb8(%rsp)
movq %r10, %rdi
movq %r8, %rbp
movq 0xf8(%rsp), %r8
leaq (%r8,%r13,4), %r15
leal (,%rbp,4), %esi
leaq (%r8,%rcx,4), %r11
movq 0x58(%rsp), %rcx
movl (%r11,%rcx,4), %r13d
addl (%rsi,%r15), %r13d
movzwl %ax, %r12d
movq 0xa8(%rsp), %r14
movq %r14, %rax
imulq %r12, %rax
leaq (%r8,%rax,4), %r10
movq 0x20(%rsp), %rdx
addl (%r10,%rdx,4), %r13d
leaq (%r8,%r9,4), %r9
movl (%r9,%rbx,4), %eax
movq %r14, %rcx
imulq %rdx, %rcx
leaq (%r8,%rcx,4), %rcx
movl (%rsi,%rcx), %esi
addl %eax, %esi
movq %r12, 0x48(%rsp)
addl (%r11,%r12,4), %esi
cmpl %r13d, %esi
jb 0x2ac5
movq %rbp, 0x78(%rsp)
imulq %rbp, %r14
leaq (%r8,%r14,4), %rsi
movq %rbx, 0x70(%rsp)
movl (%r10,%rbx,4), %r11d
movq 0x58(%rsp), %rdx
addl (%rsi,%rdx,4), %r11d
movq 0x50(%rsp), %rbp
movl (%rcx,%rbp,4), %r10d
addl %r10d, %r11d
cmpl %r13d, %r11d
jb 0x2b28
movq 0x48(%rsp), %rcx
movl (%rsi,%rcx,4), %ecx
movl (%r15,%rbp,4), %esi
addl %ecx, %esi
movq 0x20(%rsp), %r8
addl (%r9,%r8,4), %esi
cmpl %r13d, %esi
jb 0x2b84
addl %eax, %r10d
addl %ecx, %r10d
cmpl %r13d, %r10d
movq 0x68(%rsp), %r13
movq 0x10(%rsp), %r15
movq 0xe8(%rsp), %rsi
movq %rdi, %r10
movq 0x140(%rsp), %r11
movq 0x150(%rsp), %r9
movq 0x148(%rsp), %r12
movq 0x100(%rsp), %rbx
movq 0x158(%rsp), %r14
movq 0x160(%rsp), %rbp
movq 0x168(%rsp), %rdi
movq 0xb8(%rsp), %rax
jb 0x2be7
movq 0x180(%rsp), %rax
incq %rax
jmp 0x27e3
movq 0x30(%rsp), %rdx
movq %r15, %r13
movq 0x10(%rsp), %r15
jmp 0x257a
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq 0xc0(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x28(%rsp), %r14
movq %r14, %rcx
callq 0x3186
movq %r15, %rdi
movq 0x88(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq %r14, %rcx
callq 0x3186
movq 0x58(%rsp), %rax
movq %rbx, %rcx
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %rbx
movq 0x68(%rsp), %r13
movq 0x10(%rsp), %r15
movq 0x48(%rsp), %r8
jmp 0x2c5e
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0xc0(%rsp), %rsi
movq %rdx, %r15
movq 0x40(%rsp), %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rcx
callq 0x3186
movq %r14, %rdi
movq 0x80(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rcx
callq 0x3186
movq 0x78(%rsp), %rax
movq %r15, %rcx
movq 0x48(%rsp), %rdx
movq %rbp, %rsi
movq 0x70(%rsp), %rbp
movq 0x20(%rsp), %rbx
movq %rsi, %r8
jmp 0x2bdb
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq 0x80(%rsp), %rsi
movq %rdx, %rbx
movq 0x38(%rsp), %rdx
movq 0x28(%rsp), %r14
movq %r14, %rcx
callq 0x3186
movq %r15, %rdi
movq 0x88(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq %r14, %rcx
callq 0x3186
movq 0x70(%rsp), %rax
movq %rbp, %rcx
movq 0x78(%rsp), %rdx
movq 0x48(%rsp), %rbp
movq 0x20(%rsp), %r8
movq 0x68(%rsp), %r13
movq 0x10(%rsp), %r15
jmp 0x2c5e
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq 0x40(%rsp), %rsi
movq %rdx, %r12
movq 0xc0(%rsp), %rdx
movq 0x28(%rsp), %r14
movq %r14, %rcx
movq %rax, %rbx
callq 0x3186
movq %r15, %rdi
movq 0x80(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
callq 0x3186
movq %r15, %rdi
movq 0x10(%rsp), %r15
movq 0x88(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x3186
movq 0x78(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %r12, %rdx
movq 0x70(%rsp), %rbp
movq 0x20(%rsp), %rbx
movq 0x50(%rsp), %r8
movq 0xd0(%rsp), %rdi
movq (%rdi), %rsi
movq 0x20(%rdi), %rdi
imulq %rdi, %rax
imulq %rdi, %rdx
leaq (%rsi,%rdx,4), %rdx
imulq %rdi, %rbx
leaq (%rsi,%rbx,4), %rdi
movl (%rdx,%rbp,4), %edx
movl (%rdi,%r8,4), %edi
cmpl %edi, %edx
cmoval %edx, %edi
leaq (%rsi,%rax,4), %rax
movl (%rax,%rcx,4), %eax
movq 0xd8(%rsp), %rdx
movl (%rdx), %ecx
cmpl %edi, %eax
cmoval %eax, %edi
cmpl %edi, %ecx
cmoval %ecx, %edi
movl %edi, (%rdx)
jmp 0x256d
movl $0x0, %eax
movq %rax, 0x30(%rsp)
testb $0x1, 0xf(%rsp)
je 0x256d
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq