// File: /workspace/github_cpp_makefile_star2_O1/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 0x1303c7
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 0x1414b7
movq 0x148(%r14), %r15
decq %r15
js 0x16a93
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 0x16a83
addq %rdx, %rcx
addq $0x18, %rcx
movq 0x10(%rcx), %rdx
addq $0x18, %rdx
xorl %ecx, %ecx
cmpb $0x0, (%rdx)
js 0x169fe
movq 0xd8(%r14), %rsi
movl -0x10(%rdx), %edi
cmpb $0x0, (%rsi,%rdi)
je 0x16a0d
incq %rcx
addq $0x28, %rdx
cmpq %rcx, %rax
jne 0x169e9
movq %rax, %rcx
cmpq %rax, %rcx
je 0x16a83
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1419c3
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
leaq 0x20(%rsp), %r9
callq 0x141ef2
movslq 0x178(%r14), %rdx
movq 0x150(%r14), %rcx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x142e61
movq 0xf0(%r14), %rax
movq 0x158(%r14), %rsi
movb %r12b, (%rax,%rsi)
movq 0x150(%r14), %rdi
movb %r12b, (%rax,%rdi)
movq %r14, %rdx
callq 0x142568
movq 0xd8(%r14), %rax
movb %r12b, (%rax,%rbp)
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0x169b3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1419c3
movq 0x27353b(%rip), %rax    # 0x289fe0
movq (%rax), %r15
callq 0x1303c7
subsd 0x8(%rsp), %xmm0
movq 0x160(%r14), %rcx
movq 0x168(%r14), %r8
leaq 0x218e46(%rip), %rsi    # 0x22f90e
leaq 0x218e70(%rip), %rdx    # 0x22f93f
movq %r15, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x141a8d
movq 0x30(%rsp), %rdi
callq 0x2220
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq