// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movsd 0x20(%rsi), %xmm0
movsd %xmm0, (%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
pushq $0x1
popq %r13
movl %r13d, %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
addq $-0x2, %rsi
cmpq %rdx, %rsi
jbe 0x5e48
leal -0x1(%r13), %esi
movslq (%rax,%rsi,4), %rsi
movslq (%rax,%rdx,4), %rdi
leal 0x1(%r13), %edx
imulq $0x18, %rsi, %rsi
movq %rsi, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
imulq $0x18, %rdi, %rsi
movq %rsi, 0x30(%rsp)
movl %edx, 0xc(%rsp)
movl %edx, %ebx
movl %ebx, %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
decq %rsi
cmpq %rdx, %rsi
jbe 0x5e3c
movl %ebx, %ebp
movslq (%rax,%rdx,4), %rcx
leal 0x1(%rbp), %ebx
movslq (%rax,%rbx,4), %rax
movq 0x8(%r15), %rdx
movq 0x38(%rsp), %rsi
movq (%rdx,%rsi), %rsi
movq 0x40(%rsp), %rdi
movsd (%rsi,%rdi,8), %xmm0
xorpd 0x1524(%rip), %xmm0     # 0x72b0
imulq $0x18, %rcx, %rdi
movq (%rdx,%rdi), %rdi
subsd (%rdi,%rax,8), %xmm0
addsd (%rsi,%rcx,8), %xmm0
movq 0x30(%rsp), %rcx
movq (%rdx,%rcx), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, 0x10(%rsp)
movq %r12, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x5e60
testb %al, %al
je 0x5df0
movapd 0x10(%rsp), %xmm1
movsd 0x78(%r12), %xmm0
addsd 0x14c8(%rip), %xmm0     # 0x72a0
cmpb $0x1, 0x80(%r12)
jne 0x5e30
ucomisd %xmm1, %xmm0
jbe 0x5e30
movsd (%rsp), %xmm0
jmp 0x5dfb
movsd (%rsp), %xmm0
movapd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5e30
movq 0x28(%rsp), %rax
movl %r13d, (%rax)
movl %ebp, 0x4(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x476c
movapd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm2, (%rsp)
ja 0x5e4d
movq (%r14), %rax
movq 0x8(%r14), %rcx
jmp 0x5d48
movl 0xc(%rsp), %edx
movl %edx, %r13d
jmp 0x5d01
movsd (%rsp), %xmm2
movapd %xmm2, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq