// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
cmpl $0x0, (%rdi)
je 0x99c9
movq %rdi, %r14
callq 0x88f8
movq %rax, %rbx
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x5a41(%rip), %rsi      # 0xee01
leaq 0x5a44(%rip), %rdx      # 0xee0b
leaq 0x8(%rsp), %rdi
callq 0xbb78
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x8942
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x93ed
callq 0x21f0
movl (%r14), %edi
decl %edi
movsd 0x4d26(%rip), %xmm0     # 0xe120
callq 0x22c0
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rsp)
movslq %eax, %rbp
leaq (,%rbp,8), %rdi
testl %ebp, %ebp
movq $-0x1, %r12
cmovsq %r12, %rdi
callq 0x2030
movq %rax, %rbx
testl %ebp, %ebp
jle 0x9454
movslq (%r14), %rax
leaq (,%rax,4), %r15
testq %rax, %rax
cmovsq %r12, %r15
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x2030
movq %rax, (%rbx,%r12,8)
incq %r12
cmpq %r12, %rbp
jne 0x9440
callq 0x88f8
movq %rax, %rdi
callq 0x898c
callq 0x88f8
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %r13, (%rdi)
leaq 0x5994(%rip), %rsi      # 0xee0c
leaq 0x599b(%rip), %rdx      # 0xee1a
callq 0xbb78
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x8942
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x94a0
callq 0x21f0
cmpl $0x0, 0x2c(%rsp)
jle 0x95e2
movl (%r14), %eax
leal 0x3(%rax), %ecx
movq %rax, %rdx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %ecx
shlq $0x4, %rcx
xorl %edx, %edx
movdqa 0x4c2e(%rip), %xmm1     # 0xe100
movdqa 0x4b96(%rip), %xmm2     # 0xe070
movdqa 0x4b9e(%rip), %xmm3     # 0xe080
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x4c1e(%rip), %xmm5     # 0xe110
testl %eax, %eax
jle 0x95d6
movq (%rbx,%rdx,8), %rsi
xorl %edi, %edi
movdqa %xmm2, %xmm6
movdqa %xmm1, %xmm7
movdqa %xmm6, %xmm8
pxor %xmm3, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm0, %xmm9
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
movd %xmm8, %r8d
notl %r8d
testb $0x1, %r8b
je 0x954c
movl $0x4f000000, (%rsi,%rdi) # imm = 0x4F000000
pxor %xmm4, %xmm8
pextrw $0x4, %xmm8, %r8d
testb $0x1, %r8b
je 0x9565
movl $0x4f000000, 0x4(%rsi,%rdi) # imm = 0x4F000000
movdqa %xmm7, %xmm8
pxor %xmm3, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm0, %xmm9
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
pxor %xmm4, %xmm8
pextrw $0x0, %xmm8, %r8d
testb $0x1, %r8b
je 0x95ad
movl $0x4f000000, 0x8(%rsi,%rdi) # imm = 0x4F000000
pextrw $0x4, %xmm8, %r8d
testb $0x1, %r8b
je 0x95c1
movl $0x4f000000, 0xc(%rsi,%rdi) # imm = 0x4F000000
paddq %xmm5, %xmm6
paddq %xmm5, %xmm7
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x9508
incq %rdx
cmpq %rbp, %rdx
jne 0x94f2
movq (%rbx), %rax
movl $0x0, (%rax)
callq 0x88f8
movq %rax, %rdi
callq 0x898c
cmpl $0x2, (%r14)
movq %rbp, 0x30(%rsp)
jl 0x98d7
movl $0x1, %r12d
callq 0x88f8
movq %rax, %r15
movl $0x1, %ebp
cmpl $0xa, %r12d
jb 0x9662
movl $0x4, %ebp
movl %r12d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x965b
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x9660
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x9662
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %ebp
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x962d
addl $-0x3, %ebp
jmp 0x9662
addl $-0x2, %ebp
jmp 0x9662
decl %ebp
movl %ebp, %esi
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movl $0x2d, %edx
callq 0x2250
movq 0x38(%rsp), %rdi
movl %ebp, %esi
movl %r12d, (%rsp)
movl %r12d, %edx
callq 0xaf49
movl $0xf, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x5774(%rip), %rcx      # 0xee1b
callq 0x2230
leaq 0x18(%rsp), %r12
movq %r12, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x96d4
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x96de
movupd (%rcx), %xmm0
movupd %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x8942
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x9712
callq 0x21f0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
movl (%rsp), %r15d
je 0x972a
callq 0x21f0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %r15d, %ecx
shll %cl, %ebp
notl %ebp
movl (%r14), %esi
leal -0x1(%rsi), %ecx
movl $0x1, %eax
shll %cl, %eax
cmpl %ebp, %eax
jle 0x9872
cmpl $0x2, %esi
jl 0x9849
movslq %ebp, %r12
xorl %r15d, %r15d
movl $0x1, %r13d
movl %r15d, %ecx
shll %cl, %r13d
btl %r15d, %ebp
jae 0x9838
xorl %ebp, %r13d
movslq %esi, %rsi
movl $0x4f000000, 0x38(%rsp) # imm = 0x4F000000
leaq 0x8(%rsp), %rdi
leaq 0x38(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0xadc0
movslq %r13d, %rax
movq (%rbx,%rax,8), %rcx
movss (%rcx), %xmm0
movq 0x20(%r14), %rdx
movq (%rdx), %rax
addss 0x4(%rax,%r15,4), %xmm0
movq 0x8(%rsp), %rdi
movss %xmm0, (%rdi)
movslq (%r14), %rax
cmpq $0x2, %rax
jl 0x97f6
leal -0x1(%rax), %esi
addq $0x18, %rdx
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x97ea
btl %r8d, %ebp
jae 0x97ea
movss 0x4(%rcx,%r8,4), %xmm0
movq (%rdx), %r9
addss 0x4(%r9,%r15,4), %xmm0
movss %xmm0, 0x4(%rdi,%r8,4)
incq %r8
addq $0x18, %rdx
cmpq %r8, %rsi
jne 0x97c7
movss 0x4932(%rip), %xmm0     # 0xe130
testl %eax, %eax
jle 0x9820
xorl %ecx, %ecx
movss 0x4924(%rip), %xmm1     # 0xe130
movss (%rdi,%rcx,4), %xmm0
minss %xmm1, %xmm0
incq %rcx
movaps %xmm0, %xmm1
cmpq %rcx, %rax
jne 0x980c
movq (%rbx,%r12,8), %rax
movss %xmm0, 0x4(%rax,%r15,4)
incq %r15
callq 0x21f0
movl (%r14), %esi
jmp 0x983b
incq %r15
leal -0x1(%rsi), %eax
cltq
cmpq %rax, %r15
jl 0x975a
movl %ebp, %ecx
negl %ecx
andl %ebp, %ecx
leal (%rcx,%rbp), %edi
xorl %edi, %ebp
sarl $0x2, %ebp
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %ebp
orl %edi, %ebp
leal -0x1(%rsi), %ecx
movl $0x1, %eax
shll %cl, %eax
cmpl %eax, %ebp
jl 0x974b
callq 0x88f8
movq %rax, %rdi
callq 0x898c
movl (%rsp), %r12d
incl %r12d
movl (%r14), %eax
cmpl %eax, %r12d
jl 0x960d
cmpl $0x2, %eax
jl 0x98d7
movq 0x30(%rsp), %rcx
movq -0x8(%rbx,%rcx,8), %rcx
movq 0x20(%r14), %rdx
addq $0x18, %rdx
movss 0x487f(%rip), %xmm0     # 0xe130
movl $0x1, %esi
movss (%rcx,%rsi,4), %xmm1
movq (%rdx), %rdi
addss (%rdi), %xmm1
minss %xmm0, %xmm1
incq %rsi
addq $0x18, %rdx
movaps %xmm1, %xmm0
cmpq %rsi, %rax
jne 0x98b6
jmp 0x98df
movd 0x4851(%rip), %xmm1     # 0xe130
movd %xmm1, (%rsp)
movq 0xb6ed(%rip), %r14      # 0x14fd8
leaq 0x5539(%rip), %rsi      # 0xee2b
movl $0x16, %edx
movq %r14, %rdi
callq 0x2240
cvttss2si (%rsp), %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x2340
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
cmpl $0x674a, %ebp           # imm = 0x674A
jne 0x99db
callq 0x88f8
movq %rax, %r14
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %r15
movq %r15, (%rdi)
leaq 0x552a(%rip), %rsi      # 0xee8a
leaq 0x552a(%rip), %rdx      # 0xee91
callq 0xbb78
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x8942
movq 0x30(%rsp), %r12
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x998d
callq 0x21f0
cmpl $0x0, 0x2c(%rsp)
jle 0x99ad
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x99a5
callq 0x2260
incq %r14
cmpq %r14, %r12
jne 0x9997
movq %rbx, %rdi
callq 0x2260
callq 0x88f8
movq %rax, %rdi
callq 0x898c
movss (%rsp), %xmm0
jmp 0x99cc
xorps %xmm0, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5460(%rip), %rdi      # 0xee42
leaq 0x546f(%rip), %rsi      # 0xee58
leaq 0x547a(%rip), %rcx      # 0xee6a
movl $0xa6, %edx
callq 0x2170
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
jne 0x9a49
jmp 0x9a4e
jmp 0x9a1e
jmp 0x9a0f
jmp 0x9a1e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
jne 0x9a49
jmp 0x9a4e
movq %rax, %rbx
jmp 0x9a4e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x9a3a
callq 0x21f0
jmp 0x9a3a
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x9a4e
callq 0x21f0
movq %rbx, %rdi
callq 0x2350