// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1d6edb
movq $-0x1, -0x8(%rbp)
jmp 0x1d7738
cmpq $0x1, -0x20(%rbp)
jne 0x1d6ef2
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x1d7738
jmp 0x1d6ef4
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xa0(%rcx), %rax
jne 0x1d6f75
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x1d6f2e
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0x1d6f39
movl $0x2, %eax
movq %rax, -0x60(%rbp)
jmp 0x1d6f39
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
shlq $0x0, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x98(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x98(%rdx)
movb $0x0, (%rax,%rcx)
jmp 0x1d6f9f
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xb8(%rcx), %rax
jne 0x1d7020
movq -0x10(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
je 0x1d6fd9
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x1d6fe4
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x1d6fe4
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rsi
shlq $0x0, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0xb0(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0xb0(%rdx)
movb $0x0, (%rax,%rcx)
jmp 0x1d704a
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xe8(%rcx), %rax
jne 0x1d70cb
movq -0x10(%rbp), %rax
cmpq $0x0, 0xe8(%rax)
je 0x1d7084
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x1d708f
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x1d708f
movq -0x70(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rsi
shlq $0x0, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xf0(%rax)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0xe0(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0xe0(%rdx)
movb $0x0, (%rax,%rcx)
jmp 0x1d70f5
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x108(%rcx), %rax
jne 0x1d7176
movq -0x10(%rbp), %rax
cmpq $0x0, 0x108(%rax)
je 0x1d712f
movq -0x10(%rbp), %rax
movq 0x108(%rax), %rax
shlq %rax
movq %rax, -0x78(%rbp)
jmp 0x1d713a
movl $0x2, %eax
movq %rax, -0x78(%rbp)
jmp 0x1d713a
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x108(%rax)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x108(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x110(%rax)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x100(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x100(%rdx)
movl $0x0, (%rax,%rcx,4)
jmp 0x1d71a3
movq -0x10(%rbp), %rax
movq 0x118(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x120(%rcx), %rax
jne 0x1d7224
movq -0x10(%rbp), %rax
cmpq $0x0, 0x120(%rax)
je 0x1d71dd
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rax
shlq %rax
movq %rax, -0x80(%rbp)
jmp 0x1d71e8
movl $0x2, %eax
movq %rax, -0x80(%rbp)
jmp 0x1d71e8
movq -0x80(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x120(%rax)
movq -0x10(%rbp), %rax
movq 0x128(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x120(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x128(%rax)
movq -0x10(%rbp), %rax
movq 0x128(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x118(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x118(%rdx)
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x1d7252
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x1d7339
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
imulq $0x18, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1d733b
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq $0x0, 0x10(%rax)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x1d771b
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movb -0x40(%rbp), %al
andb $0x7f, %al
orb $0x0, %al
movb %al, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rsi
imulq $0x18, (%rdx,%rsi,8), %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1d745e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1d741b
jmp 0x1d744d
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1d73ca
jmp 0x1d7460
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x10(%rbp), %rdx
imulq $0x18, (%rdx), %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1d754a
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1d74d0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0x1d74de
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0x1d74de
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdx
imulq $0x18, (%rdx), %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
imulq $0x28, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdx
imulq $0x18, (%rdx), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
imulq $0x18, (%rax), %rax
addq %rax, %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0x58(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rsi
imulq $0x18, (%rdx,%rsi,8), %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1d76b2
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1d7624
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x1d7632
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x1d7632
movq -0x90(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rsi
imulq $0x18, (%rdx,%rsi,8), %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
imulq $0x28, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rsi
imulq $0x18, (%rdx,%rsi,8), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
imulq $0x18, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rdx
imulq $0x18, (%rax,%rdx,8), %rax
addq %rax, %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0x58(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
jmp 0x1d770a
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1d7393
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)