// File: 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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x20(%rsp), %rax
subq $0x2, %rcx
cmpq %rcx, %rax
jae 0xb054
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x50(%rsp)
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movl 0x48(%rsp), %eax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x18(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0xb042
movq 0x68(%rsp), %rdi
movl 0x48(%rsp), %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x44(%rsp)
movq 0x68(%rsp), %rdi
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x40(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x50(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x4c(%rsp), %rsi
callq 0x8810
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, (%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x44(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x8810
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x50(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x44(%rsp), %rsi
callq 0x8810
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x4c(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x8810
movsd 0x10(%rsp), %xmm0
movq 0x28(%rsp), %rdi
addsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x54(%rsp), %esi
movl 0x48(%rsp), %edx
callq 0xb080
testb $0x1, %al
jne 0xafa1
jmp 0xafb7
movq 0x28(%rsp), %rdi
movsd 0x38(%rsp), %xmm0
callq 0xb1d0
testb $0x1, %al
jne 0xafb7
jmp 0xb030
movsd 0x38(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb02c
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0x54(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, (%rax)
movl 0x48(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x5bf0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm1
addsd 0x58(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb02a
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
jmp 0xb063
jmp 0xb02e
jmp 0xb02e
jmp 0xb030
jmp 0xb032
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xae61
jmp 0xb044
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0xadfd
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)