// File: /workspace/github_cpp_makefile_star2_O1/EduBic[P]TSP-MetaheuristicSolvers/TabuSearchSolver.cpp
// Function: TabuSearchSolver::findFirstBestNeighbor(TSP const&, TSPSolution const&, move&)
double TabuSearchSolver::findFirstBestNeighbor(const TSP& tsp , const TSPSolution& currSol, TSPMove& move) {
    double bestCostVariation = tsp.infinite;

    // N.B. intial and final position are fixed (initial/final node remains 0)

    // Slice all the current solution vector
    for (uint a = 1 ; a < currSol.sequence.size() - 2; a++) {

        // prev node
        int h = currSol.sequence[a-1];
        // choose a starting node
        int i = currSol.sequence[a];

        for (uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++) {

            int j = currSol.sequence[b];        // choose a finishing node
            int l = currSol.sequence[b+1];      // prev node

            // prof. implementation it discards other moves
            /*if ( ( (currIter - tabuList[i] <= tabuLength) &&
                    (currIter - tabuList[j] <= tabuLength)) ) continue */

            double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
                                        + tsp.cost[h][j] + tsp.cost[i][l];


            if (isTabuMove(a, b) && !satisfiedAspirationCriteria(neighCostVariation)) {
                // DEBUG
                //std::cout << "\t" << "discard move: " << a << ", " << b << "\t";
            }
            else {

                // DEBUG
                /*if (isTabuMove(a, b) && satisfiedAspirationCriteria(neighCostVariation)) {
                    std::cout << "\t" << "in tabu but satisfied AC: " << a << ", " << b << "\t";
                }*/

                if (neighCostVariation < bestCostVariation) {
                    bestCostVariation = neighCostVariation;
                    move.from = a;
                    move.to = b;

                    double currentBestValueFound = currSol.evaluateObjectiveFunction(tsp);

                    // on first improvement exit
                    if (currentBestValueFound + bestCostVariation < currentBestValueFound) {
                        return bestCostVariation;
                    }

                    //std::cout << "\t" << "better move: " << a << ", " << b << "\t";
                }
                else {
                    //std::cout << "\t" << "no better move: " << a << ", " << b << "\t";
                }
            }
        }
    }

    return bestCostVariation;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x30(%rsp)
movq %rdx, (%rsp)
movq %rdi, %r14
movq %rsi, 0x10(%rsp)
movsd 0x20(%rsi), %xmm1
movl $0x1, %esi
movl %esi, %eax
movq (%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
addq $-0x2, %rdx
cmpq %rax, %rdx
jbe 0x6e58
leal -0x1(%rsi), %edx
movslq (%rcx,%rdx,4), %rdx
movslq (%rcx,%rax,4), %rcx
leal 0x1(%rsi), %eax
leaq (%rdx,%rdx,2), %rdx
movq %rdx, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x38(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, %r13d
movl %r13d, %r15d
movq (%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rbx
subq %rax, %rbx
sarq $0x2, %rbx
decq %rbx
cmpq %r15, %rbx
jbe 0x6e3f
movl %r13d, %ebp
movsd %xmm1, 0x18(%rsp)
movslq (%rax,%r15,4), %rcx
leal 0x1(%rbp), %r13d
movslq (%rax,%r13,4), %rax
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x40(%rsp), %r8
movq (%rdx,%r8,8), %r8
movq 0x48(%rsp), %r9
movsd (%r8,%r9,8), %xmm0
xorpd 0x15e9(%rip), %xmm0     # 0x82f0
leaq (%rcx,%rcx,2), %r9
movq (%rdx,%r9,8), %r9
subsd (%r9,%rax,8), %xmm0
addsd (%r8,%rcx,8), %xmm0
movq 0x38(%rsp), %rcx
movq (%rdx,%rcx,8), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, 0x50(%rsp)
movq %r14, %rdi
movq %rsi, %r12
movl %ebp, %edx
callq 0x6e72
testb %al, %al
je 0x6d82
movapd 0x50(%rsp), %xmm2
movsd 0x78(%r14), %xmm0
addsd 0x1594(%rip), %xmm0     # 0x82e8
movb $0x1, %al
cmpb $0x1, 0x80(%r14)
movsd 0x18(%rsp), %xmm1
jne 0x6e36
ucomisd %xmm2, %xmm0
movq %r12, %rsi
jbe 0x6d79
ucomisd %xmm2, %xmm1
ja 0x6d99
movapd %xmm1, %xmm2
jmp 0x6e28
movb $0x1, %al
movsd 0x18(%rsp), %xmm0
movapd 0x50(%rsp), %xmm2
ucomisd %xmm2, %xmm0
movq %r12, %rsi
jbe 0x6ded
movq 0x30(%rsp), %rax
movl %esi, (%rax)
movl %ebp, 0x4(%rax)
movq (%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
decq %rcx
je 0x6df3
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rdx
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
movslq (%rax,%r10,4), %r11
incq %r10
movl %r10d, %r8d
movslq (%rax,%r8,4), %r9
leaq (%r11,%r11,2), %r11
movq (%rdx,%r11,8), %r11
addsd (%r11,%r9,8), %xmm0
cmpq %r8, %rcx
ja 0x6dca
jmp 0x6df7
movapd %xmm0, %xmm2
jmp 0x6e28
xorpd %xmm0, %xmm0
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm0
cmpnltsd %xmm0, %xmm1
movapd 0x20(%rsp), %xmm0
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
ja 0x6e26
movb $0x1, %al
jmp 0x6e28
xorl %eax, %eax
movapd %xmm2, %xmm1
testb %al, %al
jne 0x6cac
jmp 0x6e43
movapd %xmm1, %xmm2
movq %r12, %rsi
jmp 0x6e28
movapd %xmm1, %xmm2
movl 0xc(%rsp), %eax
movl %eax, %esi
movapd %xmm2, %xmm1
cmpq %r15, %rbx
jbe 0x6c5f
jmp 0x6e5e
movapd %xmm1, 0x20(%rsp)
movaps 0x20(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq