// File: EduBic[P]TSP-MetaheuristicSolvers/./neighborimprovement.h
// Function: FirstImprovement::execute(TSP const&, TSPSolution const&, move&)
double execute(const TSP& tsp , const TSPSolution& currSol, TSPMove& move) {
        //cout << endl << endl << "-------------- find neighbor --------------";


        double bestCostVariation = tsp.infinite;

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

            int h = currSol.sequence[a-1];
            int i = currSol.sequence[a];

            for ( uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++ ) {
                int j = currSol.sequence[b];
                int l = currSol.sequence[b+1];

                // incremental evaluation --> bestCostVariation (instead of best cost)
                double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
                                            + tsp.cost[h][j] + tsp.cost[i][l];

                if (neighCostVariation < bestCostVariation ) {
                    // neighbor better of precedent neighbors
                    bestCostVariation = neighCostVariation;
                    move.from = a;
                    move.to = b;

                    double currentBestValueFound = currSol.evaluateObjectiveFunction(tsp);

                    // on first improvement exit
                    //cout << endl << "Move from " << a << " to " << b;
                    //cout << "\tNeighbor Value = " << currentBestValueFound + bestCostVariation << " - CurrentBestValue = " << currentBestValueFound;
                    if (currentBestValueFound + bestCostVariation < currentBestValueFound) {
                        //cout << "\tIt's an improvement, chose it!" << endl;
                        //cout << "------------------------" << endl;
                        return bestCostVariation;
                    }
                }
            }
        }

        //cout << endl << "------------------------------" << endl;
        return bestCostVariation;
    }

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movq 0x70(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x28(%rsp), %rax
subq $0x2, %rcx
cmpq %rcx, %rax
jae 0xda4a
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, 0x20(%rsp)
movq 0x68(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x20(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0xda38
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, 0x8(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x44(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x8810
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x50(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x44(%rsp), %rsi
callq 0x8810
movsd 0x10(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movslq 0x4c(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x8810
movsd 0x18(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xda26
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 0xda24
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
jmp 0xda59
jmp 0xda26
jmp 0xda28
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xd887
jmp 0xda3a
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0xd823
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
addq $0x88, %rsp
retq
nopw (%rax,%rax)