// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x20(%rbp)
movl $0x1, %edi
movl $0x180, %esi            # imm = 0x180
callq 0x2140
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x1d62b0
movq -0x28(%rbp), %rax
movq 0x148(%rax), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jl 0x1d99ea
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
andq 0x138(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x60(%rbp), %rcx
addq %rcx, %rax
cmpq $0x0, 0x18(%rax)
jne 0x1d9867
jmp 0x1d99d9
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x30, -0x60(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
jae 0x1d98f9
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x60(%rbp), %rcx
addq %rcx, %rax
movq 0x28(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1d98b1
jmp 0x1d98e8
movq -0x28(%rbp), %rax
movq 0xd8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x30, -0x60(%rbp), %rdx
addq %rdx, %rcx
movq 0x28(%rcx), %rcx
imulq $0x28, -0x58(%rbp), %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1d98e6
jmp 0x1d98f9
jmp 0x1d98e8
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1d986f
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x30, -0x60(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
jne 0x1d9918
jmp 0x1d99d9
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1d6c50
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x28(%rbp), %rcx
addq $0x150, %rcx            # imm = 0x150
movq -0x28(%rbp), %r8
addq $0x158, %r8             # imm = 0x158
leaq -0x48(%rbp), %r9
callq 0x1d7750
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movslq 0x178(%rax), %rdx
movq -0x28(%rbp), %rax
movq 0x150(%rax), %rcx
callq 0x1d96d0
movq -0x28(%rbp), %rax
movq 0xf0(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x158(%rcx), %rcx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movq 0xf0(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x150(%rcx), %rcx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movq 0x150(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x158(%rax), %rsi
movq -0x28(%rbp), %rdx
callq 0x1d8480
movq -0x28(%rbp), %rax
movq 0xd8(%rax), %rax
movq -0x60(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1d9821
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1d6c50
movq 0x1ea5e2(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x1b7ca0
movq -0x68(%rbp), %rdi
subsd -0x20(%rbp), %xmm0
movq -0x28(%rbp), %rax
movq 0x160(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x168(%rax), %r8
leaq 0x1b2d4b(%rip), %rsi    # 0x38c77b
leaq 0x1b2d75(%rip), %rdx    # 0x38c7ac
movb $0x1, %al
callq 0x22a0
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1d6df0
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x28(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nop