// File: /workspace/github_cpp_makefile_star2_O2/Manca[P]algorithms2/./assignment5.hpp
// Function: assignment5::TSP::solve()
float solve()
        {
            if (num_cities == 0)
                return 0.0;
            ////// array definition
            PROFILE("Allocation");
            int S_size = (int)std::pow(2, num_cities-1); // 2^24 (first city is always included)
            float **A = new float*[S_size];
            for (int i = 0; i < S_size; i++)
            {
                A[i] = new float[num_cities];         // 2^24 x 24
            }
            PROFILE_STOP();
            PROFILE("Initialization");
            // initialization
            for (int i = 0; i < S_size; i++)
            {
                for (int j = 0; j < num_cities;j++)
                    A[i][j] = (float)INT32_MAX;
            }
            A[0][0] = 0;    // first subset of S (subset 1), contains only starting vertex 0 (0...001)
            PROFILE_STOP();
            // algo
            for (int m = 1; m < num_cities; m++)    // we need to start from 1 to get all the cities that are connected to city 0 directly!
            {
                PROFILE("Processing m = " + std::to_string(m));
                int S = (1 << m) - 1;    // we begin with initial number of ones (m=2) (2^m - 1 - init size of the set)
                int S_no_j = 0;         // previous S that does not contain j

                while (S < (1 << (num_cities-1)))   // and generate all subsets with m ones in them until we hit 2^(num_cities-1)
                {
                    // let's find all the ones in S and use their positions as vertex numbers
                  //  std::cout << "Subset: " << S << std::endl << "------" << std::endl;
                    for (int j = 0; j < num_cities-1; j++)      // for each city, see if it's in S, and yes do the math! (j+1 actual city number!)
                    {
                        int sXORj = S ^ (1 << j);               // S xor 2^j (put 1 at j's position)
                        // extract last bit
                        bool isJinS = !((sXORj >> j) & 1);      // get the xor'ed bit and AND it with 1 to see if it's 1 or 0
                        if (isJinS)
                        {
                            // do work
                           // std::cout << "Processing city j: " << j+1 << std::endl;
                            float min = (float)INT32_MAX;

                            S_no_j = S ^ (1 << j);
                            std::vector<float> dists(num_cities, (float)INT32_MAX);   // distances
                            dists[0] = A[S_no_j][0] + _distances[0][j+1];

                            for (int k = 0; k < num_cities-1; k++)
                            {
                                if (k != j)
                                {
                                    // need to check if k is in S!
                                    int sXORk = S ^ (1 << k);  // S xor 2^k (put 1 at k's position)
                                    bool isKinS = !((sXORk >> k) & 1);
                                    if (isKinS)
                                    {
                                  //      std::cout << "S without j is: " << S_no_j << std::endl;
                                        dists[k+1] = A[S_no_j][k+1] + _distances[k+1][j+1];
                                    }
                                }
                            }
                            // find min
                            for (int i = 0; i < num_cities; i++)
                                if (dists[i] < min)
                                    min = dists[i];
                           // std::cout << "Minimum for subset: " << S << " is: " << min << std::endl << "-----" << std::endl;
                            A[S][j+1] = min;
                        }
                    }
                    // use Gosper Hack to get the next subset
                    S = next_subset(S);
                }
                PROFILE_STOP();
            }

            // finally, let's get the minimum of all minimums
            float min_min = (float)INT32_MAX;
            for (int j = 1; j < num_cities; j++)
            {
                float calc = A[S_size-1][j] + _distances[j][0];
                if (calc < min_min)
                    min_min = calc;
            }
            std::cout << "Minimum cost tour is: " << (int)min_min << std::endl;
            assert((int)min_min == 26442);
            // cleanup
            PROFILE("Cleanup")
            for (int i = 0; i < S_size; i++)
            {
                delete[] A[i];
            }
            delete[] A;
            PROFILE_STOP();

            return min_min;
        }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
cmpl $0x0, (%rdi)
je 0x7f31
movq %rdi, %r14
callq 0x725c
movq %rax, %rbx
leaq 0x623a(%rip), %rsi      # 0xdd87
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x2120
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x72a6
leaq 0x18(%rsp), %rdi
callq 0x2150
movl (%r14), %edi
decl %edi
movsd 0x5550(%rip), %xmm0     # 0xd0d0
callq 0x22f0
cvttsd2si %xmm0, %eax
cltq
leaq (,%rax,8), %rdi
xorl %ebx, %ebx
testl %eax, %eax
pushq $-0x1
popq %r12
cmovsq %r12, %rdi
movl $0x0, %r13d
movq %rax, 0x38(%rsp)
cmovgl %eax, %r13d
callq 0x2030
movq %rax, %rcx
movslq (%r14), %rax
leaq (,%rax,4), %r15
testq %rax, %rax
cmovsq %r12, %r15
movq %r13, %r12
movq %rcx, 0x10(%rsp)
cmpq %rbx, %r12
je 0x7beb
movq %r15, %rdi
callq 0x2030
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0x7bd0
callq 0x725c
movq %rax, %rdi
callq 0x72c8
callq 0x725c
movq %rax, %r15
leaq 0x618b(%rip), %rsi      # 0xdd92
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x2120
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x72a6
leaq 0x18(%rsp), %rdi
callq 0x2150
movl (%r14), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq 0x10(%rsp), %rdi
cmpq %r12, %rcx
je 0x7c5d
xorl %edx, %edx
cmpq %rdx, %rax
je 0x7c58
movq (%rdi,%rcx,8), %rsi
movl $0x4f000000, (%rsi,%rdx,4) # imm = 0x4F000000
incq %rdx
jmp 0x7c43
incq %rcx
jmp 0x7c3c
movq (%rdi), %rax
andl $0x0, (%rax)
callq 0x725c
movq %rax, %rdi
callq 0x72c8
pushq $0x1
popq %r15
movl %r15d, %ebp
movq %r12, 0x40(%rsp)
movslq (%r14), %rax
cmpl %eax, %ebp
jge 0x7e3b
callq 0x725c
movq %rax, %r12
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, 0x8(%rsp)
movl %ebp, %esi
callq 0x94b3
leaq 0x18(%rsp), %r13
movq %r13, %rdi
leaq 0x60f0(%rip), %rsi      # 0xdda1
movq %rbx, %rdx
callq 0x948f
movq %r12, %rdi
movq %r13, %rsi
callq 0x72a6
movq %r13, %rdi
callq 0x2150
leaq 0x48(%rsp), %rdi
callq 0x2150
pushq $-0x1
popq %rax
movl %eax, %r12d
movl 0x8(%rsp), %ecx
shll %cl, %r12d
notl %r12d
movl (%r14), %esi
leal -0x1(%rsi), %ecx
movl %r15d, %eax
shll %cl, %eax
cmpl %eax, %r12d
jge 0x7e1e
movslq %r12d, %r13
xorl %ebx, %ebx
movq %rbx, %rbp
leal -0x1(%rsi), %eax
cltq
cmpq %rax, %rbp
jge 0x7dfa
movl %r15d, %ebx
movl %ebp, %ecx
shll %cl, %ebx
btl %ebp, %r12d
jb 0x7d22
incq %rbp
jmp 0x7d02
xorl %r12d, %ebx
movslq %esi, %rsi
movl $0x4f000000, 0x48(%rsp) # imm = 0x4F000000
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x950c
movslq %ebx, %rax
movq 0x10(%rsp), %r10
movq (%r10,%rax,8), %rdx
movss (%rdx), %xmm0
movq 0x20(%r14), %rsi
movq (%rsi), %rax
addss 0x4(%rax,%rbp,4), %xmm0
movq 0x18(%rsp), %rax
movss %xmm0, (%rax)
movl (%r14), %ecx
cmpl $0x2, %ecx
movl %r15d, %edi
cmovgel %ecx, %edi
leaq 0x1(%rbp), %rbx
decl %edi
addq $0x18, %rsi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x7db4
cmpq %r8, %rbp
je 0x7dab
btl %r8d, %r12d
jae 0x7dab
movss 0x4(%rdx,%r8,4), %xmm0
movq (%rsi), %r9
addss 0x4(%r9,%rbp,4), %xmm0
movss %xmm0, 0x4(%rax,%r8,4)
incq %r8
addq $0x18, %rsi
jmp 0x7d83
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %edx, %edx
movss 0x5318(%rip), %xmm0     # 0xd0e0
cmpq %rdx, %rcx
je 0x7dde
movss (%rax,%rdx,4), %xmm1
minss %xmm0, %xmm1
incq %rdx
movaps %xmm1, %xmm0
jmp 0x7dc8
movq (%r10,%r13,8), %rax
movss %xmm0, 0x4(%rax,%rbp,4)
leaq 0x18(%rsp), %rdi
callq 0x91fc
movl (%r14), %esi
jmp 0x7cff
movl %r12d, %ecx
negl %ecx
andl %r12d, %ecx
leal (%rcx,%r12), %edi
xorl %edi, %r12d
sarl $0x2, %r12d
movl %r12d, %eax
cltd
idivl %ecx
movl %eax, %r12d
orl %edi, %r12d
jmp 0x7ce9
callq 0x725c
movq %rax, %rdi
callq 0x72c8
movl 0x8(%rsp), %ebp
incl %ebp
movq 0x40(%rsp), %r12
jmp 0x7c7c
movq 0x20(%r14), %rcx
addq $0x18, %rcx
movss 0x5295(%rip), %xmm1     # 0xd0e0
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %r8
cmpq %rax, %rdx
jge 0x7e7e
movq -0x8(%rdi,%r8,8), %rsi
movss (%rsi,%rdx,4), %xmm0
movq (%rcx), %rsi
addss (%rsi), %xmm0
minss %xmm1, %xmm0
incq %rdx
addq $0x18, %rcx
movaps %xmm0, %xmm1
jmp 0x7e58
movq 0xe153(%rip), %rdi      # 0x15fd8
leaq 0x5f25(%rip), %rsi      # 0xddb1
movss %xmm1, 0x8(%rsp)
callq 0x2230
cvttss2si 0x8(%rsp), %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x2370
movq %rax, %rdi
callq 0x2160
cmpl $0x674a, %ebp           # imm = 0x674A
jne 0x7f44
callq 0x725c
movq %rax, %r14
leaq 0x5f46(%rip), %rsi      # 0xde10
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x2120
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x72a6
leaq 0x18(%rsp), %rdi
callq 0x2150
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
cmpq %rbx, %r12
je 0x7f14
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x7f0f
callq 0x2270
movq 0x10(%rsp), %rax
incq %rbx
jmp 0x7ef7
movq %rax, %rdi
callq 0x2270
callq 0x725c
movq %rax, %rdi
callq 0x72c8
movss 0x8(%rsp), %xmm0
jmp 0x7f35
xorpd %xmm0, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e7d(%rip), %rdi      # 0xddc8
leaq 0x5e8c(%rip), %rsi      # 0xddde
leaq 0x5e97(%rip), %rcx      # 0xddf0
movl $0xa6, %edx
callq 0x21b0
jmp 0x7f6b
jmp 0x7f75
jmp 0x7f6b
jmp 0x7f75
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x7f91
movq %rax, %rbx
jmp 0x7f96
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2150
jmp 0x7f8c
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x2380