// File: canzarlab[P]Trajan/src/match.cpp
// Function: bipartite_aux(array2d&, std::vector<int, std::allocator<int>>&)
int bipartite_aux(array2d& D, vector<int>& sol)
{
    // complexity of bipartite is O(n2^m) so make sure m < n
    bool transposed = false;
    if (D.n < D.m)
    {
        D = D.transpose();
        transposed = true;
    }

    // dp table for the matching
    array2d bdp(D.n, 1 << D.m), pdp(D.n, 1 << D.m, -1);

    // compute the matching
    int weight = bipartite(D, bdp, pdp, 0, fm(D.m));

    // store the matching
    // note: -1 is unmatched
    sol.resize(D.n, -1);
    mask m = fm(D.m);
    for (int k = 0; k < D.n && m > 0; ++k)
    {
        sol[k] = pdp(k, m);
        if (pdp(k, m) != -1)
            m ^= in(pdp(k, m));
    }

    // transpose the matching back
    if (transposed)
    {
        vector<int> zol(D.m, -1);
        for (int i = 0; i < D.n; ++i)
            if (sol[i] != -1)
                zol[sol[i]] = i;
        swap(sol, zol);
    }
    return weight;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x4d7d
movq -0x8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x7a50
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x7b10
leaq -0x38(%rbp), %rdi
callq 0x7b50
movb $0x1, -0x11(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %esi
movb 0x4(%rax), %cl
movl $0x1, %edx
movl %edx, -0xc8(%rbp)
shll %cl, %edx
leaq -0x58(%rbp), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x7b70
movl -0xc8(%rbp), %edx
movq -0x8(%rbp), %rax
movl (%rax), %esi
movb 0x4(%rax), %cl
shll %cl, %edx
leaq -0x78(%rbp), %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x7b70
jmp 0x4dc2
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl 0x4(%rax), %edi
callq 0x7be0
movq %rax, -0xd0(%rbp)
jmp 0x4dde
movq -0xd0(%rbp), %r8
movq -0xd8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x78(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4b90
movl %eax, -0xdc(%rbp)
jmp 0x4e03
movl -0xdc(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movslq (%rax), %rsi
movl $0xffffffff, -0x8c(%rbp) # imm = 0xFFFFFFFF
leaq -0x8c(%rbp), %rdx
callq 0x7c00
jmp 0x4e32
movq -0x8(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x7be0
movq %rax, -0xe8(%rbp)
jmp 0x4e47
movq -0xe8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %ecx
movq -0x8(%rbp), %rdx
xorl %eax, %eax
cmpl (%rdx), %ecx
movb %al, -0xe9(%rbp)
jge 0x4e86
cmpq $0x0, -0x98(%rbp)
seta %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x4e95
jmp 0x4f8e
movl -0x9c(%rbp), %esi
movl -0x98(%rbp), %edx
leaq -0x78(%rbp), %rdi
callq 0x7820
movq %rax, -0xf8(%rbp)
jmp 0x4eb3
movq -0xf8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x104(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x9c(%rbp), %rsi
callq 0x7cb0
movl -0x104(%rbp), %ecx
movl %ecx, (%rax)
movl -0x9c(%rbp), %esi
movl -0x98(%rbp), %edx
leaq -0x78(%rbp), %rdi
callq 0x7820
movq %rax, -0x100(%rbp)
jmp 0x4ef8
movq -0x100(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x4f78
movl -0x9c(%rbp), %esi
movl -0x98(%rbp), %edx
leaq -0x78(%rbp), %rdi
callq 0x7820
movq %rax, -0x110(%rbp)
jmp 0x4f22
movq -0x110(%rbp), %rax
movl (%rax), %edi
callq 0x79f0
movq %rax, -0x118(%rbp)
jmp 0x4f39
movq -0x118(%rbp), %rax
xorq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x4f78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x50da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x50d1
jmp 0x4f7a
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x4e5f
testb $0x1, -0x11(%rbp)
je 0x50a4
movq -0x8(%rbp), %rax
movslq 0x4(%rax), %rax
movq %rax, -0x128(%rbp)
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
leaq -0xbd(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x7cd0
movq -0x128(%rbp), %rsi
movq -0x120(%rbp), %rcx
leaq -0xb8(%rbp), %rdi
leaq -0xbc(%rbp), %rdx
callq 0x7cf0
jmp 0x4fe7
leaq -0xbd(%rbp), %rdi
callq 0x7d70
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x5088
movq -0x10(%rbp), %rdi
movslq -0xc4(%rbp), %rsi
callq 0x7cb0
cmpl $-0x1, (%rax)
je 0x5072
movl -0xc4(%rbp), %eax
movl %eax, -0x12c(%rbp)
movq -0x10(%rbp), %rdi
movslq -0xc4(%rbp), %rsi
callq 0x7cb0
movslq (%rax), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x7cb0
movl -0x12c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x5072
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xbd(%rbp), %rdi
callq 0x7d70
jmp 0x50d1
jmp 0x5074
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x4ffd
movq -0x10(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7d90
leaq -0xb8(%rbp), %rdi
callq 0x7dc0
movl -0x88(%rbp), %eax
movl %eax, -0x130(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x7b50
leaq -0x58(%rbp), %rdi
callq 0x7b50
movl -0x130(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x7b50
leaq -0x58(%rbp), %rdi
callq 0x7b50
movq -0x80(%rbp), %rdi
callq 0x44e0
nopl (%rax)