// File: /workspace/github_cpp_makefile_star2_O3/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 0x147758
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
cmpq $0x1, %rdx
jne 0x147760
movq (%r14), %rax
jmp 0x147b09
movq $-0x1, %rax
retq
movq %rdi, %rbx
movq 0x98(%rdi), %rcx
cmpq 0xa0(%rdi), %rcx
jne 0x1477a6
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xa0(%rbx)
movq 0xa8(%rbx), %rdi
callq 0x2370
movq %rax, 0xa8(%rbx)
movq 0x98(%rbx), %rcx
jmp 0x1477ad
movq 0xa8(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x98(%rbx)
movb $0x0, (%rax,%rcx)
movq 0xb0(%rbx), %rcx
cmpq 0xb8(%rbx), %rcx
jne 0x1477ff
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xb8(%rbx)
movq 0xc0(%rbx), %rdi
callq 0x2370
movq %rax, 0xc0(%rbx)
movq 0xb0(%rbx), %rcx
jmp 0x147806
movq 0xc0(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0xb0(%rbx)
movb $0x0, (%rax,%rcx)
movq 0xe0(%rbx), %rcx
cmpq 0xe8(%rbx), %rcx
jne 0x147858
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xe8(%rbx)
movq 0xf0(%rbx), %rdi
callq 0x2370
movq %rax, 0xf0(%rbx)
movq 0xe0(%rbx), %rcx
jmp 0x14785f
movq 0xf0(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0xe0(%rbx)
movb $0x0, (%rax,%rcx)
movq 0x100(%rbx), %rcx
cmpq 0x108(%rbx), %rcx
jne 0x1478b5
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x108(%rbx)
movq 0x110(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x110(%rbx)
movq 0x100(%rbx), %rcx
jmp 0x1478bc
movq 0x110(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x100(%rbx)
movl $0x0, (%rax,%rcx,4)
movq 0x118(%rbx), %rcx
cmpq 0x120(%rbx), %rcx
jne 0x147915
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x120(%rbx)
movq 0x128(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x128(%rbx)
movq 0x118(%rbx), %rcx
jmp 0x14791c
movq 0x128(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x118(%rbx)
movq $0x0, (%rax,%rcx,8)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rax
cmpq %rax, 0x8(%rbx)
jae 0x147993
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0x147997
movq 0x10(%rbx), %rax
leaq (%rcx,%rcx,2), %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax,%rcx,8)
movq $0x0, 0x10(%rax,%rcx,8)
xorl %r12d, %r12d
movl $0x2, %r13d
movq (%r14,%r12,8), %rbp
movq 0x10(%rbx), %rcx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq (%rcx,%rdx,8), %rax
testq %rax, %rax
je 0x1479f3
leaq (%rcx,%rdx,8), %rdx
movq 0x10(%rdx), %rdx
xorpd %xmm0, %xmm0
cmpb $0x0, 0x18(%rdx)
js 0x1479e8
addsd (%rdx), %xmm0
addq $0x28, %rdx
decq %rax
jne 0x1479de
jmp 0x1479f7
xorpd %xmm0, %xmm0
movq (%rbx), %rdx
leaq (%rdx,%rdx,2), %rsi
movq (%rcx,%rsi,8), %rax
cmpq 0x8(%rcx,%rsi,8), %rax
movsd %xmm0, (%rsp)
jne 0x147a4b
leaq (%rcx,%rsi,8), %rcx
leaq (%rax,%rax), %rdx
testq %rax, %rax
cmoveq %r13, %rdx
movq %rdx, 0x8(%rcx)
movq 0x10(%rcx), %rdi
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq (%rbx), %rdx
movq 0x10(%rbx), %rcx
leaq (%rdx,%rdx,2), %rsi
movq %rax, 0x10(%rcx,%rsi,8)
movq (%rcx,%rsi,8), %rax
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rcx,%rdx,8), %rsi
leaq 0x1(%rax), %rdi
movq %rdi, (%rcx,%rdx,8)
leaq (%rax,%rax,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), %rcx
movq (%r14,%r12,8), %rdx
leaq (%rdx,%rdx,2), %rsi
movq (%rcx,%rsi,8), %rax
cmpq 0x8(%rcx,%rsi,8), %rax
jne 0x147ad0
leaq (%rax,%rax), %rdx
testq %rax, %rax
cmoveq %r13, %rdx
movq %rdx, 0x8(%rcx,%rsi,8)
movq (%r14,%r12,8), %rax
leaq (%rax,%rax,2), %rax
movq 0x8(%rcx,%rax,8), %rdx
movq 0x10(%rcx,%rax,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), %rsi
movq %rax, 0x10(%rcx,%rsi,8)
movq (%rcx,%rsi,8), %rax
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rcx,%rdx,8), %rsi
leaq 0x1(%rax), %rdi
movq %rdi, (%rcx,%rdx,8)
leaq (%rax,%rax,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 0x1479b6
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