// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: clean_hap(hc_links*, bubble_type*, ma_ug_t const*)
min_cut_t* clean_hap(hc_links* link, bubble_type* bub, const ma_ug_t *ug)
{
    double index_time = yak_realtime();
    min_cut_t* x; CALLOC(x, 1);
    kvec_t_u64_warp buff;
    kv_init(buff.a);
    init_min_cut_t(x, link, (const bubble_type*)bub, ug);

    // get_Connected_Components(&x);
    // check_connective(&x, link);
    // print_rGraph(&x);

    long long i;
    uint64_t k, uID;
    
    ///for (i = 0; (uint64_t)i < x.n; i++)
    for (i = x->n - 1; i >= 0; i--)
    {
        uID = x->order.a[i] & x->uID_mode;
        ///fprintf(stderr, "uID: %lu, f.n: %lu\n", uID, (uint64_t)link->a.a[uID].f.n);
        if(link->a.a[uID].f.n == 0) continue;
        for (k = 0; k < link->a.a[uID].f.n; k++)
        {
            if(link->a.a[uID].f.a[k].del) continue;
            if(x->utgVis.a[link->a.a[uID].f.a[k].uID] == 0) break;
        }
        ///fprintf(stderr, "k: %lu\n", k);
        if(k == link->a.a[uID].f.n) continue;
        reset_min_cut_t(x, link);
        ///fprintf(stderr, "reset\n");
        get_s_t(x, link, uID, &(x->src), &(x->dest), &buff);
        ///fprintf(stderr, "x.src: %lu, x.dest: %lu\n", x.src, x.dest);
        ///Note: should only consider edges betweem bubbles, ignore edges to homo untigs

        /*******************************for debug************************************/
        ///if(!select_large_node(ug, &x, x.src, x.dest, 10, 0)) continue; 
        ///if(uID != 26818) continue;
        //if(uID != 45179) continue;
        ///memset(x.bmerVis.a, 1, x.bmerVis.n);
        get_bmer_unitgs(x, bub, x->b_mer, x->src);
        x->bmerVis.a[x->src] = x->bmerVis.a[x->dest] = 1;
        /*******************************for debug************************************/

        graph_cut(x->src, x->dest, x);
        ///fprintf(stderr, "graph_cut\n");
        x->utgVis.a[uID] = 1;
        ///exit(0);
    }

    reset_min_cut_t(x, link);

    fprintf(stderr, "[M::%s::%.3f] # edges: %lu, # cutted edges: %lu\n", 
                                    __func__, yak_realtime()-index_time, x->n_e, x->c_e);
    update_link_by_min_cut_t(x, link);
    ///destory_min_cut_t(x);
    kv_destroy(buff.a);
    return x;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %rbx
callq 0x1365ef
movsd %xmm0, 0x8(%rsp)
pushq $0x1
popq %rdi
movl $0x180, %esi            # imm = 0x180
callq 0x2120
movq %rax, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, 0x18(%rsp)
movq %r15, %rdx
callq 0x147138
movq 0x148(%r14), %r15
decq %r15
js 0x161d3
leaq 0x150(%r14), %rax
movq %rax, 0x10(%rsp)
movq %r14, %r13
addq $0x158, %r13            # imm = 0x158
movb $0x1, %r12b
movq 0x28(%r14), %rax
movq 0x138(%r14), %rbp
andq (%rax,%r15,8), %rbp
movq 0x10(%rbx), %rcx
imulq $0x30, %rbp, %rdx
movq 0x18(%rcx,%rdx), %rax
testq %rax, %rax
je 0x161bc
addq %rdx, %rcx
addq $0x18, %rcx
movq 0x10(%rcx), %rcx
addq $0x18, %rcx
cmpb $0x0, (%rcx)
js 0x1613b
movq 0xd8(%r14), %rdx
movl -0x10(%rcx), %esi
cmpb $0x0, (%rdx,%rsi)
je 0x16146
addq $0x28, %rcx
decq %rax
jne 0x16126
jmp 0x161bc
testq %rax, %rax
je 0x161bc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1475f3
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
leaq 0x20(%rsp), %r9
callq 0x147b18
movslq 0x178(%r14), %rdx
movq 0x150(%r14), %rcx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x149448
movq 0xf0(%r14), %rax
movq 0x158(%r14), %rsi
movb %r12b, (%rax,%rsi)
movq 0x150(%r14), %rdi
movb %r12b, (%rax,%rdi)
movq %r14, %rdx
callq 0x1483e7
movq 0xd8(%r14), %rax
movb %r12b, (%rax,%rbp)
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0x160f2
movq 0x30(%rsp), %r15
jmp 0x161d6
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1475f3
movq 0x27edf8(%rip), %rax    # 0x294fe0
movq (%rax), %r12
callq 0x1365ef
subsd 0x8(%rsp), %xmm0
movq 0x160(%r14), %rcx
movq 0x168(%r14), %r8
leaq 0x225953(%rip), %rsi    # 0x23bb5e
leaq 0x22597d(%rip), %rdx    # 0x23bb8f
movq %r12, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1476bd
movq %r15, %rdi
callq 0x2220
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq