// File: /workspace/github_cpp_makefile_star2_O2/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 0xfffac
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
cmpq $0x1, %rdx
jne 0xfffb0
movq (%r14), %rax
jmp 0x100319
pushq $-0x1
popq %rax
retq
movq %rdi, %rbx
movq 0x98(%rdi), %rcx
pushq $0x2
popq %r12
cmpq 0xa0(%rdi), %rcx
jne 0xffff5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0xa0(%rbx)
movq 0xa8(%rbx), %rdi
callq 0x2370
movq %rax, 0xa8(%rbx)
movq 0x98(%rbx), %rcx
jmp 0xffffc
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 0x100049
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0xb8(%rbx)
movq 0xc0(%rbx), %rdi
callq 0x2370
movq %rax, 0xc0(%rbx)
movq 0xb0(%rbx), %rcx
jmp 0x100050
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 0x10009d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0xe8(%rbx)
movq 0xf0(%rbx), %rdi
callq 0x2370
movq %rax, 0xf0(%rbx)
movq 0xe0(%rbx), %rcx
jmp 0x1000a4
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 0x1000f5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0x108(%rbx)
movq 0x110(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x110(%rbx)
movq 0x100(%rbx), %rcx
jmp 0x1000fc
movq 0x110(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x100(%rbx)
andl $0x0, (%rax,%rcx,4)
movq 0x118(%rbx), %rcx
cmpq 0x120(%rbx), %rcx
jne 0x10014d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0x120(%rbx)
movq 0x128(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x128(%rbx)
movq 0x118(%rbx), %rcx
jmp 0x100154
movq 0x128(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x118(%rbx)
andq $0x0, (%rax,%rcx,8)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rax
cmpq %rax, 0x8(%rbx)
jae 0x1001bf
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
incq %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
imulq $0x18, %rax, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0x1001c3
movq 0x10(%rbx), %rax
imulq $0x18, %rcx, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax,%rcx)
andq $0x0, 0x10(%rax,%rcx)
xorl %r13d, %r13d
cmpq %r15, %r13
je 0x10030f
movq (%r14,%r13,8), %rbp
movq 0x10(%rbx), %rcx
imulq $0x18, %rbp, %rdx
leaq (%rcx,%rdx), %rax
movq (%rcx,%rdx), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
subq $0x1, %rdx
jb 0x100218
movq 0x10(%rax), %rdi
cmpb $0x0, 0x18(%rdi,%rsi)
js 0x100212
addsd (%rdi,%rsi), %xmm0
addq $0x28, %rsi
jmp 0x1001fc
movq (%rbx), %rdx
imulq $0x18, %rdx, %rsi
movq (%rcx,%rsi), %rax
cmpq 0x8(%rcx,%rsi), %rax
movsd %xmm0, (%rsp)
jne 0x100269
leaq (%rax,%rax), %rdx
testq %rax, %rax
cmoveq %r12, %rdx
leaq (%rcx,%rsi), %rax
movq %rdx, 0x8(%rcx,%rsi)
movq 0x10(%rax), %rdi
imulq $0x28, %rdx, %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq (%rbx), %rdx
movq 0x10(%rbx), %rcx
imulq $0x18, %rdx, %rsi
movq %rax, 0x10(%rcx,%rsi)
movq (%rcx,%rsi), %rax
imulq $0x18, %rdx, %rdx
movq 0x10(%rcx,%rdx), %rsi
leaq 0x1(%rax), %rdi
movq %rdi, (%rcx,%rdx)
imulq $0x28, %rax, %rax
movsd %xmm0, (%rsi,%rax)
movl %ebp, 0x8(%rsi,%rax)
movb $0x0, 0x18(%rsi,%rax)
movl (%rbx), %ebp
movq 0x10(%rbx), %rcx
movq (%r14,%r13,8), %rdx
imulq $0x18, %rdx, %rsi
movq (%rcx,%rsi), %rax
cmpq 0x8(%rcx,%rsi), %rax
jne 0x1002e4
leaq (%rax,%rax), %rdx
testq %rax, %rax
cmoveq %r12, %rdx
movq %rdx, 0x8(%rcx,%rsi)
imulq $0x18, (%r14,%r13,8), %rax
movq 0x10(%rcx,%rax), %rdi
imulq $0x28, 0x8(%rcx,%rax), %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq 0x10(%rbx), %rcx
movq (%r14,%r13,8), %rdx
imulq $0x18, %rdx, %rsi
movq %rax, 0x10(%rcx,%rsi)
movq (%rcx,%rsi), %rax
imulq $0x18, %rdx, %rdx
movq 0x10(%rcx,%rdx), %rsi
leaq 0x1(%rax), %rdi
movq %rdi, (%rcx,%rdx)
imulq $0x28, %rax, %rax
movsd %xmm0, (%rsi,%rax)
movl %ebp, 0x8(%rsi,%rax)
movb $0x0, 0x18(%rsi,%rax)
incq %r13
jmp 0x1001d9
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