// File: /workspace/github_cpp_makefile_star2_O1/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 0x989a
movq %rdi, %r14
callq 0x88b8
movq %rax, %rbx
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x5a89(%rip), %rsi      # 0xee01
leaq 0x5a8c(%rip), %rdx      # 0xee0b
leaq 0x8(%rsp), %rdi
callq 0xbb3a
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x8904
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x93a5
callq 0x2200
movl (%r14), %edi
decl %edi
movsd 0x4d6e(%rip), %xmm0     # 0xe120
callq 0x22d0
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 0x940c
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 0x93f8
callq 0x88b8
movq %rax, %rdi
callq 0x894e
callq 0x88b8
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %r13, (%rdi)
leaq 0x59dc(%rip), %rsi      # 0xee0c
leaq 0x59e3(%rip), %rdx      # 0xee1a
callq 0xbb3a
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x8904
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9458
callq 0x2200
cmpl $0x0, 0x2c(%rsp)
jle 0x959a
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 0x4c76(%rip), %xmm1     # 0xe100
movdqa 0x4bde(%rip), %xmm2     # 0xe070
movdqa 0x4be6(%rip), %xmm3     # 0xe080
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x4c66(%rip), %xmm5     # 0xe110
testl %eax, %eax
jle 0x958e
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 0x9504
movl $0x4f000000, (%rsi,%rdi) # imm = 0x4F000000
pxor %xmm4, %xmm8
pextrw $0x4, %xmm8, %r8d
testb $0x1, %r8b
je 0x951d
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 0x9565
movl $0x4f000000, 0x8(%rsi,%rdi) # imm = 0x4F000000
pextrw $0x4, %xmm8, %r8d
testb $0x1, %r8b
je 0x9579
movl $0x4f000000, 0xc(%rsi,%rdi) # imm = 0x4F000000
paddq %xmm5, %xmm6
paddq %xmm5, %xmm7
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x94c0
incq %rdx
cmpq %rbp, %rdx
jne 0x94aa
movq %rbp, 0x30(%rsp)
movq (%rbx), %rax
movl $0x0, (%rax)
callq 0x88b8
movq %rax, %rdi
callq 0x894e
movl (%r14), %edx
cmpl $0x2, %edx
jl 0x9853
leaq 0x48(%rsp), %r15
movl $0x1, %eax
movl %eax, (%rsp)
callq 0x88b8
movl (%rsp), %r13d
movq %rax, %r12
movl $0x1, %ebp
cmpl $0xa, %r13d
jb 0x962a
movl $0x4, %ebp
movl %r13d, %esi
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %esi
jbe 0x9623
cmpl $0x3e7, %esi            # imm = 0x3E7
jbe 0x9628
cmpl $0x2710, %esi           # imm = 0x2710
jb 0x962a
movl %esi, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %ebp
cmpl $0x1869f, %esi          # imm = 0x1869F
movl %ecx, %esi
ja 0x95f2
addl $-0x3, %ebp
jmp 0x962a
addl $-0x2, %ebp
jmp 0x962a
decl %ebp
movl %ebp, %esi
movq %r15, 0x38(%rsp)
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movl $0x2d, %edx
callq 0x2260
movq 0x38(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xaf0c
movl $0xf, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x57b5(%rip), %rcx      # 0xee1b
callq 0x2240
leaq 0x18(%rsp), %r15
movq %r15, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x9693
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x969c
movupd (%rcx), %xmm0
movupd %xmm0, (%r15)
movq %r12, %rdi
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x8(%rsp), %rsi
callq 0x8904
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x96d0
callq 0x2200
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x96e4
callq 0x2200
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl (%rsp), %ecx
shll %cl, %ebp
notl %ebp
movb (%r14), %cl
decb %cl
movl $0x1, %eax
shll %cl, %eax
cmpl %ebp, %eax
jle 0x982c
movl (%r14), %eax
cmpl $0x2, %eax
jl 0x9801
movslq %ebp, %r12
xorl %r15d, %r15d
movl $0x1, %r13d
movl %r15d, %ecx
shll %cl, %r13d
btl %r15d, %ebp
jae 0x97ec
xorl %ebp, %r13d
movslq %eax, %rsi
movl $0x4f000000, 0x38(%rsp) # imm = 0x4F000000
leaq 0x8(%rsp), %rdi
leaq 0x38(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0xad90
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 0x97b2
leal -0x1(%rax), %esi
addq $0x18, %rdx
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x97a6
btl %r8d, %ebp
jae 0x97a6
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 0x9783
movss 0x4976(%rip), %xmm0     # 0xe130
testl %eax, %eax
jle 0x97dc
xorl %ecx, %ecx
movss 0x4968(%rip), %xmm1     # 0xe130
movss (%rdi,%rcx,4), %xmm0
minss %xmm1, %xmm0
incq %rcx
movaps %xmm0, %xmm1
cmpq %rcx, %rax
jne 0x97c8
movq (%rbx,%r12,8), %rax
movss %xmm0, 0x4(%rax,%r15,4)
callq 0x2200
incq %r15
movslq (%r14), %rax
movq %rax, %rcx
decq %rcx
cmpq %rcx, %r15
jl 0x9716
movl %ebp, %ecx
negl %ecx
andl %ebp, %ecx
leal (%rcx,%rbp), %esi
xorl %esi, %ebp
sarl $0x2, %ebp
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %ebp
orl %esi, %ebp
movb (%r14), %cl
decb %cl
movl $0x1, %eax
shll %cl, %eax
cmpl %eax, %ebp
jl 0x9704
callq 0x88b8
movq %rax, %rdi
callq 0x894e
movl (%rsp), %eax
incl %eax
movl (%r14), %edx
cmpl %edx, %eax
leaq 0x18(%rsp), %r13
leaq 0x48(%rsp), %r15
jl 0x95cb
cmpl $0x2, %edx
jl 0x98a2
movq 0x30(%rsp), %rax
movq -0x8(%rbx,%rax,8), %rax
movq 0x20(%r14), %rcx
movl %edx, %edx
addq $0x18, %rcx
movss 0x48bc(%rip), %xmm0     # 0xe130
movl $0x1, %esi
movss (%rax,%rsi,4), %xmm1
movq (%rcx), %rdi
addss (%rdi), %xmm1
minss %xmm0, %xmm1
incq %rsi
addq $0x18, %rcx
movaps %xmm1, %xmm0
cmpq %rsi, %rdx
jne 0x9879
jmp 0x98aa
xorps %xmm0, %xmm0
jmp 0x998d
movd 0x4886(%rip), %xmm1     # 0xe130
movd %xmm1, (%rsp)
movq 0xb722(%rip), %r14      # 0x14fd8
leaq 0x556e(%rip), %rsi      # 0xee2b
movl $0x16, %edx
movq %r14, %rdi
callq 0x2250
cvttss2si (%rsp), %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x2350
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
cmpl $0x674a, %ebp           # imm = 0x674A
jne 0x999c
callq 0x88b8
movq %rax, %r14
leaq 0x8(%rsp), %rdi
movq %r13, (%rdi)
leaq 0x5564(%rip), %rsi      # 0xee8a
leaq 0x5564(%rip), %rdx      # 0xee91
callq 0xbb3a
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x8904
movq 0x30(%rsp), %r15
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9953
callq 0x2200
cmpl $0x0, 0x2c(%rsp)
jle 0x9973
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x996b
callq 0x2270
incq %r14
cmpq %r14, %r15
jne 0x995d
movq %rbx, %rdi
callq 0x2270
callq 0x88b8
movq %rax, %rdi
callq 0x894e
movss (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x549f(%rip), %rdi      # 0xee42
leaq 0x54ae(%rip), %rsi      # 0xee58
leaq 0x54b9(%rip), %rcx      # 0xee6a
movl $0xa6, %edx
callq 0x2170
jmp 0x99c3
jmp 0x99d2
jmp 0x99c3
jmp 0x99d2
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
jne 0x9a02
jmp 0x9a07
movq %rax, %rbx
jmp 0x9a07
leaq 0x48(%rsp), %r12
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x99f8
callq 0x2200
jmp 0x99f8
leaq 0x48(%rsp), %r12
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r12, %rdi
je 0x9a07
callq 0x2200
movq %rbx, %rdi
callq 0x2360
nop