// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: add_mul_convex(min_cut_t*, unsigned long*, unsigned long)
uint64_t add_mul_convex(min_cut_t* x, uint64_t* a, uint64_t n)
{
    if(n == 0) return (uint64_t)-1;
    if(n == 1) return a[0];
    kv_push(uint8_t, x->rGraphSet, 0);
    kv_push(uint8_t, x->rGraphVis, 0);
    kv_push(uint8_t, x->bmerVis, 0);
    kv_push(uint32_t, x->parent, 0);
    kv_push(double, x->p_weight, 0);
    kv_resize(hc_edge_warp, x->rGraph, x->rGraph.n+1); 
    kv_init(x->rGraph.a[x->rGraph.n]);
    uint64_t i, k;
    hc_edge t;
    for (i = 0; i < n; i++)
    {
        ///t.uID = a[i]; t.del = t.enzyme = t.weight = 0;
        t.uID = a[i]; t.del = t.weight = 0;
        for (k = 0; k < x->rGraph.a[a[i]].n; k++)
        {
            if(x->rGraph.a[a[i]].a[k].del) continue;
            t.weight += x->rGraph.a[a[i]].a[k].weight;
        }
        kv_push(hc_edge, x->rGraph.a[x->rGraph.n], t);
        t.uID = x->rGraph.n;
        kv_push(hc_edge, x->rGraph.a[a[i]], t);
    }
    
    x->rGraph.n++;
    return x->rGraph.n - 1;
}

void get_s_t(min_cut_t* x, hc_links* link, uint64_t uID, uint64_t* src, uint64_t* dest, kvec_t_u64_warp* buff)
{
    buff->a.n = 0; (*src) = (*dest) = (uint64_t)-1;
    if(link->a.a[uID].f.n == 0) return;
    (*src) = uID; 

    uint64_t i, n;
    for (i = 0, n = 0; i < link->a.a[uID].f.n; i++)
    {
        if(link->a.a[uID].f.a[i].del) continue;
        kv_push(uint64_t, buff->a, link->a.a[uID].f.a[i].uID);
        (*dest) = link->a.a[uID].f.a[i].uID;
        n++;
    }
    if(n == 1 || n == 0) return;
    (*dest) = add_mul_convex(x, buff->a.a, buff->a.n);
}

uint64_t bfs_flow(uint64_t src, uint64_t dest, min_cut_t* x, kvec_t_u64_warp* buff)
{
    uint64_t *p = NULL, v, u, i;
    if(dest != (uint64_t)-1) memset(x->rGraphVis.a, 0, x->rGraphVis.n);

    kdq_push(uint64_t, x->q, src); 
    if(buff) kv_push(uint64_t, buff->a, src);

    x->rGraphVis.a[src] = 1;
    x->parent.a[src] = (uint32_t)-1;

    while (1)
    {
        p = kdq_shift(uint64_t, x->q);
        if(!p) break;
        v = *p;
        if(v == dest) return 1;
        for (i = 0; i < x->rGraph.a[v].n; i++)
        {
            if(x->rGraph.a[v].a[i].del) continue;
            if(x->rGraph.a[v].a[i].weight == 0) continue;
            u = x->rGraph.a[v].a[i].uID;
            if(x->rGraphVis.a[u]) continue;
            if(!x->bmerVis.a[u]) continue;

            x->parent.a[u] = v;
            x->p_weight.a[u] = x->rGraph.a[v].a[i].weight;

            kdq_push(uint64_t, x->q, u); 
            if(buff) kv_push(uint64_t, buff->a, u);
            ///set u or v to be 1? doesn't matter
            x->rGraphVis.a[u] = 1;
        }
    }

    return 0;
}

// Assembly:
testq %rdx, %rdx
je 0x141b22
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
cmpq $0x1, %rdx
jne 0x141b2a
movq (%r14), %rax
jmp 0x141ee3
movq $-0x1, %rax
retq
movq %rdi, %rbx
movq 0xa0(%rdi), %rax
cmpq %rax, 0x98(%rdi)
jne 0x141b67
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xa0(%rbx)
movq 0xa8(%rbx), %rdi
callq 0x2370
movq %rax, 0xa8(%rbx)
movq 0x98(%rbx), %rax
movq 0xa8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x98(%rbx)
movb $0x0, (%rcx,%rax)
movq 0xb8(%rbx), %rax
cmpq %rax, 0xb0(%rbx)
jne 0x141bbe
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xb8(%rbx)
movq 0xc0(%rbx), %rdi
callq 0x2370
movq %rax, 0xc0(%rbx)
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xb0(%rbx)
movb $0x0, (%rcx,%rax)
movq 0xe8(%rbx), %rax
cmpq %rax, 0xe0(%rbx)
jne 0x141c15
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xe8(%rbx)
movq 0xf0(%rbx), %rdi
callq 0x2370
movq %rax, 0xf0(%rbx)
movq 0xe0(%rbx), %rax
movq 0xf0(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xe0(%rbx)
movb $0x0, (%rcx,%rax)
movq 0x108(%rbx), %rax
cmpq %rax, 0x100(%rbx)
jne 0x141c70
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x108(%rbx)
movq 0x110(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x110(%rbx)
movq 0x100(%rbx), %rax
movq 0x110(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x100(%rbx)
movl $0x0, (%rcx,%rax,4)
movq 0x120(%rbx), %rax
cmpq %rax, 0x118(%rbx)
jne 0x141cce
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x120(%rbx)
movq 0x128(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x128(%rbx)
movq 0x118(%rbx), %rax
movq 0x128(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x118(%rbx)
movq $0x0, (%rcx,%rax,8)
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, 0x8(%rbx)
jae 0x141d4e
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rax,%rax,2), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx,%rax,8)
movq $0x0, 0x10(%rcx,%rax,8)
testq %r15, %r15
je 0x141ed9
xorl %r12d, %r12d
movl $0x2, %r13d
movq (%r14,%r12,8), %rbp
movq 0x10(%rbx), %rax
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq (%rax,%rdx,8), %rcx
testq %rcx, %rcx
je 0x141dba
leaq (%rax,%rdx,8), %rdx
movq 0x10(%rdx), %rdx
xorpd %xmm0, %xmm0
cmpb $0x0, 0x18(%rdx)
js 0x141daf
addsd (%rdx), %xmm0
addq $0x28, %rdx
decq %rcx
jne 0x141da5
jmp 0x141dbe
xorpd %xmm0, %xmm0
movq (%rbx), %rcx
leaq (%rcx,%rcx,2), %rdx
movq 0x8(%rax,%rdx,8), %rcx
cmpq %rcx, (%rax,%rdx,8)
movsd %xmm0, (%rsp)
jne 0x141e12
leaq (%rax,%rdx,8), %rax
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r13, %rdx
movq %rdx, 0x8(%rax)
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq (%rcx,%rcx,2), %rcx
movq %rax, 0x10(%rdx,%rcx,8)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rdx
movq 0x10(%rcx,%rax,8), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rcx,%rax,8)
leaq (%rdx,%rdx,4), %rax
movsd %xmm0, (%rsi,%rax,8)
movl %ebp, 0x8(%rsi,%rax,8)
movb $0x0, 0x18(%rsi,%rax,8)
movl (%rbx), %ebp
movq 0x10(%rbx), %rax
movq (%r14,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %rdx
cmpq %rdx, (%rax,%rcx,8)
jne 0x141e9e
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r13, %rsi
movq %rsi, 0x8(%rax,%rcx,8)
movq (%r14,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %rdx
movq 0x10(%rax,%rcx,8), %rdi
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq 0x10(%rbx), %rcx
movq (%r14,%r12,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq %rax, 0x10(%rcx,%rdx,8)
movq 0x10(%rbx), %rax
movq (%r14,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x10(%rax,%rcx,8), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rax,%rcx,8)
leaq (%rdx,%rdx,4), %rax
movsd %xmm0, (%rsi,%rax,8)
movl %ebp, 0x8(%rsi,%rax,8)
movb $0x0, 0x18(%rsi,%rax,8)
incq %r12
cmpq %r15, %r12
jne 0x141d7d
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq