// File: /workspace/github_cpp_makefile_star2_O3/bobverity[P]MavericK/Hungarian.cpp
// Function: hungarian(std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, bool, std::basic_ofstream<char, std::char_traits<char>>&)
vector<int> hungarian(vector< vector<double> > &M, vector<int> &edgesLeft, vector<int> &edgesRight, vector<int> &blockedLeft, vector<int> &blockedRight, bool outputLog_on, ofstream &outputLog_fileStream) {
    int n = int(M.size());
    
    // define maximum number of reps in Hungarian algorithm before aborting
    int maxReps= int(1e6);
    
    // initialise assignment objects
    int numberAssigned;
    
    // search for solution until maxReps reached
    for (int rep=0; rep<maxReps; rep++) {
        
        // zero assignment objects
        for (int i=0; i<n; i++) {
            edgesLeft[i] = -1;
            edgesRight[i] = -1;
        }
        numberAssigned = 0;
        
        // subtract smallest element from all rows and columns
        double minRow;
		vector<double> minCol = M[0];
        for (int i=0; i<int(M.size()); i++) {
            minRow = *min_element(begin(M[i]),end(M[i]));
            for (int j=0; j<int(M[i].size()); j++) {
                M[i][j] -= minRow;
                if (M[i][j]<minCol[j])
                    minCol[j] = M[i][j];
            }
        }
        for (int i=0; i<int(M.size()); i++) {
            for (int j=0; j<int(M[i].size()); j++) {
                M[i][j] -= minCol[j];
            }
        }
        
        // generate an initial matching
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (M[i][j]==0 && edgesRight[j]<0) {
                    edgesLeft[i] = j;
                    edgesRight[j] = i;
                    numberAssigned++;
                    break;
                }
            }
        }
        
        // if this matching is perfect then we are done
        if (numberAssigned==n)
            return(edgesLeft);
        
        // continue augmenting paths until no more possible
        //vector<int> blockedLeft(n);
        //vector<int> blockedRight(n);
        bool continueAugmenting = true;
        while (continueAugmenting) {
            continueAugmenting = false;
            
            // search all unmatched nodes
            for (int i=0; i<n; i++) {
                if (edgesLeft[i]<0) {
                    
                    // attempt to find augmented path
                    blockedLeft = vector<int>(n);
                    blockedRight = vector<int>(n);
                    vector<int> path = augmentLeft(i, M, edgesRight, blockedLeft, blockedRight);
                    
                    // if successful then augment
                    if (path[0]>=0) {
                        continueAugmenting = true;
                        numberAssigned ++;
                        for (int j=0; j<int(path.size()/2); j++) {
                            edgesLeft[path[j*2+1]] = path[j*2];
                            edgesRight[path[j*2]] = path[j*2+1];
                        }
                        
                        // if best matching found then finish
                        if (numberAssigned==n) {
                            return(edgesLeft);
                        }
                    }
                }
            }
        }
        
        // find minimum value in cost matrix, looking at all elements in which neither the row or the column is part of the minimum vertex cover
        double minVal = -log(double(0));
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (blockedLeft[i]==1 && blockedRight[j]==0 && M[i][j]<minVal) {
                    minVal = M[i][j];
                }
            }
        }
        
        // add or subtract this value from cost matrix as required
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (blockedLeft[i]==1 && blockedRight[j]==0) {
                    M[i][j] -= minVal;
                }
                if (blockedLeft[i]==0 && blockedRight[j]==1) {
                    M[i][j] += minVal;
                }
            }
        }
        
        // at this point we have a new cost matrix and can repeat the process from the top
        
    } // rep loop
    
    cerrAndLog("\nError: failed to solve label switching problem. Hungarian algorithm failed to find best assignment within " + to_string((long long)maxReps) + string(" iterations given the following cost matrix:\n"), outputLog_on, outputLog_fileStream);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cerrAndLog(to_string((long long)M[i][j])+string(" "), outputLog_on, outputLog_fileStream);
        }
        cerrAndLog("\n", outputLog_on, outputLog_fileStream);
    }
    
    exit(1);
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r9, %r13
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x78(%rsp)
movq 0x8(%rsi), %rax
movq %rsi, 0x50(%rsp)
subq (%rsi), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movq %rax, 0x8(%rsp)
movslq %eax, %r14
movl %r14d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, 0xa0(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x6143
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
movl %esi, (%rax,%rdx,4)
movl %esi, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %r15
jne 0x6135
movq 0x50(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x58(%rsp), %rdi
callq 0x5d20
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
testl %ecx, %ecx
jle 0x627b
movq 0x58(%rsp), %rdx
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movq (%rax,%r9,8), %r8
movq 0x8(%rax,%r9,8), %r9
cmpq %r9, %r8
sete %r10b
leaq 0x8(%r8), %r11
cmpq %r9, %r11
sete %bl
orb %r10b, %bl
movq %r8, %r10
jne 0x61d4
movsd (%r8), %xmm0
movq %r8, %r10
movsd (%r11), %xmm1
ucomisd %xmm1, %xmm0
cmovaq %r11, %r10
minsd %xmm0, %xmm1
addq $0x8, %r11
movapd %xmm1, %xmm0
cmpq %r9, %r11
jne 0x61b6
subq %r8, %r9
shrq $0x3, %r9
testl %r9d, %r9d
jle 0x6219
movsd (%r10), %xmm0
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %r10d, %r10d
movsd (%r8,%r10,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r8,%r10,8)
movsd (%rdx,%r10,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x6211
movsd %xmm1, (%rdx,%r10,8)
incq %r10
cmpq %r10, %r9
jne 0x61ef
incq %rdi
cmpq %rsi, %rdi
jne 0x6188
testl %ecx, %ecx
jle 0x627b
movq 0x58(%rsp), %rdx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
leaq (%rsi,%rsi,2), %r8
movq (%rax,%r8,8), %rdi
movq 0x8(%rax,%r8,8), %r8
subq %rdi, %r8
shrq $0x3, %r8
testl %r8d, %r8d
jle 0x6273
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
movsd (%rdi,%r9,8), %xmm0
subsd (%rdx,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r9,8)
incq %r9
cmpq %r9, %r8
jne 0x6259
incq %rsi
cmpq %rcx, %rsi
jne 0x6236
cmpl $0x0, 0x8(%rsp)
jle 0x62d6
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
xorl %r12d, %r12d
leaq (%rdx,%rdx,2), %rsi
movq (%rax,%rsi,8), %r8
movq 0x40(%rsp), %rsi
movq (%rsi), %rsi
xorl %edi, %edi
movsd (%r8,%rdi,8), %xmm0
ucomisd 0x2fdf9(%rip), %xmm0    # 0x360a8
jne 0x62b9
jp 0x62b9
cmpl $0x0, (%rsi,%rdi,4)
js 0x62c3
incq %rdi
cmpq %rdi, %r15
jne 0x62a1
jmp 0x62cc
movl %edi, (%rcx,%rdx,4)
movl %edx, (%rsi,%rdi,4)
incl %r12d
incq %rdx
cmpq %r15, %rdx
jne 0x628f
jmp 0x62d9
xorl %r12d, %r12d
cmpl 0x8(%rsp), %r12d
je 0x692a
movb $0x1, %bl
movb %bl, 0x7(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x6464
movb $0x1, %bpl
xorl %ebx, %ebx
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%rbx,4)
jns 0x644f
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
leaq 0xa8(%rsp), %rdx
callq 0x6a48
movq 0x48(%rsp), %rcx
movq (%rcx), %rdi
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rcx)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x636c
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x636c
callq 0x3310
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
leaq 0xa8(%rsp), %rdx
callq 0x6a48
movq (%r13), %rdi
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%r13)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r13)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x63c6
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x63c6
callq 0x3310
leaq 0x20(%rsp), %rdi
movl %ebx, %esi
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movq %r13, %r9
callq 0x5dc8
movq 0x20(%rsp), %rdi
cmpl $0x0, (%rdi)
js 0x644a
movq 0x28(%rsp), %rax
subq %rdi, %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x6433
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x40(%rsp), %rdx
movq (%rdx), %rdx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %esi, %esi
movslq (%rdi,%rsi,8), %r8
movslq 0x4(%rdi,%rsi,8), %r9
movl %r8d, (%rcx,%r9,4)
movl 0x4(%rdi,%rsi,8), %r9d
movl %r9d, (%rdx,%r8,4)
incq %rsi
cmpq %rsi, %rax
jne 0x6415
incl %r12d
cmpl 0x8(%rsp), %r12d
je 0x6486
callq 0x3310
movb $0x1, %al
movl %eax, 0x1c(%rsp)
jmp 0x644f
callq 0x3310
incq %rbx
cmpq %r14, %rbx
setl %bpl
cmpq %r15, %rbx
jne 0x6302
jmp 0x646e
xorl %ebp, %ebp
movl $0x0, 0x1c(%rsp)
testb %bpl, %bpl
movl 0x1c(%rsp), %ebx
jne 0x65a0
testb $0x1, %bl
jne 0x62e6
jmp 0x64b9
movq 0x78(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x6ad4
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x64a4
callq 0x3310
movb $0x1, %bl
movq 0x8(%rsp), %rax
movl %eax, %r12d
testb $0x1, %bpl
je 0x647b
jmp 0x65a0
xorpd %xmm0, %xmm0
callq 0x3290
cmpl $0x0, 0x8(%rsp)
jle 0x659c
xorpd 0x2fb6b(%rip), %xmm0    # 0x36040
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq (%r13), %rcx
movq 0x50(%rsp), %rdx
movq (%rdx), %rdx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
leaq (%rsi,%rsi,2), %r8
leaq (%rdx,%r8,8), %r8
xorl %r9d, %r9d
cmpl $0x1, %edi
jne 0x6518
cmpl $0x0, (%rcx,%r9,4)
jne 0x6518
movq (%r8), %r10
movsd (%r10,%r9,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6518
movapd %xmm1, %xmm0
incq %r9
cmpq %r9, %r15
jne 0x64f9
incq %rsi
cmpq %r15, %rsi
jne 0x64eb
cmpl $0x0, 0x8(%rsp)
jle 0x659c
movq 0x48(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq (%r13), %rsi
leaq (%rcx,%rcx,2), %rdi
shlq $0x3, %rdi
movq 0x50(%rsp), %r8
addq (%r8), %rdi
xorl %r8d, %r8d
testl %edx, %edx
je 0x6572
cmpl $0x1, %edx
jne 0x658c
cmpl $0x0, (%rsi,%r8,4)
jne 0x658c
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x6586
cmpl $0x1, (%rsi,%r8,4)
jne 0x658c
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r9,%r8,8)
incq %r8
cmpq %r8, %r15
jne 0x6553
incq %rcx
cmpq %r15, %rcx
jne 0x6539
movb %bl, 0x7(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x65af
callq 0x3310
testb $0x1, 0x7(%rsp)
movq 0xa0(%rsp), %rax
jne 0x6948
incl %eax
cmpl $0xf4240, %eax          # imm = 0xF4240
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jne 0x6114
leaq 0x20(%rsp), %rbx
movl $0xf4240, %esi          # imm = 0xF4240
movq %rbx, %rdi
callq 0x6c6c
leaq 0x2fac3(%rip), %rsi     # 0x360b0
leaq 0x58(%rsp), %rdi
movq %rbx, %rdx
callq 0x6bfc
leaq 0xb8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x2fb0f(%rip), %rsi     # 0x3611c
leaq 0x2fb35(%rip), %rdx     # 0x36149
leaq 0xa8(%rsp), %rdi
callq 0x6d9c
leaq 0xe8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x6b54
movzbl 0x140(%rsp), %esi
leaq 0xe8(%rsp), %rdi
movl %esi, 0x10(%rsp)
movq 0x148(%rsp), %rdx
callq 0x1d808
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6672
callq 0x3310
movq 0xa8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6684
callq 0x3310
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6697
callq 0x3310
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x66aa
callq 0x3310
cmpl $0x0, 0x8(%rsp)
jle 0x6920
leaq 0x90(%rsp), %r12
movq 0x8(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
leaq (%rax,%rax,2), %rax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movq %r12, %rbx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq 0x48(%rsp), %rcx
movq (%rax,%rcx,8), %rax
cvttsd2si (%rax,%r14,8), %r15
movq %r15, %r12
negq %r12
cmovsq %r15, %r12
movl $0x1, %ebp
cmpq $0xa, %r12
jb 0x675d
movl $0x4, %ebp
movq %r12, %rcx
movabsq $0x346dc5d63886594b, %rsi # imm = 0x346DC5D63886594B
cmpq $0x63, %rcx
jbe 0x6756
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x675b
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x675d
movq %rcx, %rax
mulq %rsi
shrq $0xb, %rdx
addl $0x4, %ebp
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x6720
addl $-0x3, %ebp
jmp 0x675d
addl $-0x2, %ebp
jmp 0x675d
decl %ebp
shrq $0x3f, %r15
leal (%r15,%rbp), %esi
leaq 0x68(%rsp), %r13
movq %r13, 0x58(%rsp)
leaq 0x58(%rsp), %rdi
movl $0x2d, %edx
callq 0x33a0
addq 0x58(%rsp), %r15
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x6d20
leaq 0x30(%rsp), %r15
movq %r15, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x322dd(%rip), %rsi     # 0x38a83
leaq 0x322d7(%rip), %rdx     # 0x38a84
callq 0x6d9c
movq 0x58(%rsp), %rcx
movl $0xf, %edi
cmpq %r13, %rcx
movq %rbx, %r12
je 0x67c9
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %r8
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0x67f5
movl $0xf, %edi
cmpq %r15, %rsi
je 0x67f0
movq 0x30(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x6801
leaq 0x58(%rsp), %rdi
callq 0x30e0
jmp 0x680f
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3370
movq %r12, 0x80(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x6838
movq %rdx, 0x80(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x90(%rsp)
jmp 0x6842
movupd (%rcx), %xmm0
movupd %xmm0, (%r12)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x88(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x80(%rsp), %rdi
movl 0x10(%rsp), %esi
movq 0x148(%rsp), %rdx
callq 0x1d808
movq 0x80(%rsp), %rdi
cmpq %r12, %rdi
je 0x688d
callq 0x3310
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x689c
callq 0x3310
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x68ab
callq 0x3310
incq %r14
cmpq 0x8(%rsp), %r14
jne 0x66df
leaq 0xd8(%rsp), %rbx
movq %rbx, 0xc8(%rsp)
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
leaq 0x30047(%rip), %rsi     # 0x36922
leaq 0x30041(%rip), %rdx     # 0x36923
callq 0x6d9c
movq %r14, %rdi
movl 0x10(%rsp), %esi
movq 0x148(%rsp), %rdx
callq 0x1d808
movq 0xc8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x690d
callq 0x3310
movq 0x40(%rsp), %rax
incq %rax
cmpq 0x8(%rsp), %rax
jne 0x66ce
movl $0x1, %edi
callq 0x33d0
movq 0x78(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x6ad4
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x6948
callq 0x3310
movq 0x78(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq 0xc8(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x6a3b
jmp 0x6a40
movq %rax, %r15
movq 0x80(%rsp), %rdi
cmpq %r12, %rdi
je 0x6992
callq 0x3310
jmp 0x6992
movq %rax, %r15
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x69ab
callq 0x3310
jmp 0x69ab
movq %rax, %r15
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
jmp 0x6a0f
jmp 0x6a2e
movq %rax, %r15
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x69d7
callq 0x3310
jmp 0x69d7
movq %rax, %r15
movq 0xa8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x69ee
callq 0x3310
jmp 0x69ee
movq %rax, %r15
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a06
callq 0x3310
jmp 0x6a06
movq %rax, %r15
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x6a3b
jmp 0x6a40
movq %rax, %r15
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6a31
callq 0x3310
jmp 0x6a31
jmp 0x6a2e
jmp 0x6a2e
movq %rax, %r15
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x6a40
callq 0x3310
movq %r15, %rdi
callq 0x3580