// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: hic_clean(asg_t*)
void hic_clean(asg_t* read_g)
{
    uint32_t n_vtx, v, u;
    uint64_t i, k, k_i, tLen, v_occ, u_occ, utg_occ;
    double bub_rate = 0.1;
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen_primary(read_g, PRIMARY_LABLE);
    n_vtx = ug->g->n_seq * 2;
    buf_t b; memset(&b, 0, sizeof(buf_t)); b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    ///for (i = 0, tLen = 1; i < ug->u.n; i++) tLen += ug->u.a[i].len;
    tLen = get_bub_pop_max_dist_advance(ug->g, &b);
    uint8_t* bs_flag = (uint8_t*)calloc(n_vtx, 1);
    kvec_t(uint32_t) ax;
    kv_init(ax);
    for (v = 0; v < ug->g->n_seq; ++v) 
    {
        if(ug->g->seq[v].del) continue;
        ug->g->seq[v].c = PRIMARY_LABLE;
        EvaluateLen(ug->u, v) = ug->u.a[v].n;
    }


    for (v = 0; v < n_vtx; ++v) 
    {
        if(ug->g->seq[v>>1].del) continue;
        if(asg_arc_n(ug->g, v) < 2) continue;
        if(bs_flag[v] != 0) continue;
        if(asg_bub_pop1_primary_trio(ug->g, NULL, v, tLen, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL))
        {
            //beg is v, end is b.S.a[0]
            //note b.b include end, does not include beg
            for (i = 0; i < b.b.n; i++)
            {
                if(b.b.a[i]==v || b.b.a[i]==b.S.a[0]) continue;
                bs_flag[b.b.a[i]] = bs_flag[b.b.a[i]^1] = 1;
            }
            bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 3;
        }
    }


    for (v = 0; v < n_vtx; ++v) 
    {
        if(bs_flag[v] !=2) continue;
        if(asg_bub_pop1_primary_trio(ug->g, NULL, v, tLen, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL))
        {
            //note b.b include end, does not include beg
            for (i = v_occ = ax.n = 0; i < b.b.n; i++)
            {
                if(b.b.a[i]==v || b.b.a[i]==b.S.a[0]) continue;
                v_occ += ug->u.a[b.b.a[i]>>1].n;
                kv_push(uint32_t, ax, b.b.a[i]>>1);
            }

            for (i = 0; i < ax.n; i++)
            {
                for (k = 0; k < 2; k++)
                {
                    u = (ax.a[i]<<1) + k;
                    if(asg_arc_n(ug->g, u) < 2) continue;
                    if(asg_bub_pop1_primary_trio(ug->g, NULL, u, tLen, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL))
                    {
                        for (k_i = u_occ = utg_occ = 0; k_i < b.b.n; k_i++)
                        {
                            if(b.b.a[k_i]==u || b.b.a[k_i]==b.S.a[0]) continue;
                            u_occ += ug->u.a[b.b.a[k_i]>>1].n;
                            utg_occ++;
                        }

                        if(u_occ >= v_occ*bub_rate) continue;
                        if(u_occ > 3) continue;
                        if(utg_occ > 2) continue;
                        asg_bub_pop1_primary_trio(ug->g, NULL, u, tLen, &b, (uint32_t)-1, (uint32_t)-1, 1, NULL, NULL, NULL, 0, 0, NULL);
                    }
                }
            }
        }
    }

    ma_utg_t* m = NULL;
    for (v = 0; v < ug->g->n_seq; ++v) 
    {
        if(ug->g->seq[v].del) continue;
        if(ug->g->seq[v].c != ALTER_LABLE) continue;
        m = &(ug->u.a[v]);
        if(m->m == 0) continue;
        for (k = 0; k < m->n; k++)
        {
            asg_seq_del(read_g, m->a[k]>>33);
        }
    }

    asg_cleanup(read_g);
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); free(bs_flag);
    ma_ug_destroy(ug);
    kv_destroy(ax);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movsd 0x281a11(%rip), %xmm0   # 0x37f018
movsd %xmm0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xc(%rbp)
leaq -0xc8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl -0xc(%rbp), %eax
movl %eax, %edi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0xc8(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xc8(%rbp), %rsi
callq 0xfde50
movq %rax, -0x38(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xd0(%rbp)
movq $0x0, -0xe0(%rbp)
movq $0x0, -0xe8(%rbp)
movq $0x0, -0xd8(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x60(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xfd734
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xfd6eb
jmp 0xfd729
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x10(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x10(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xfd6b7
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0xfd8c6
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xfd76a
jmp 0xfd8b8
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x2, %eax
jae 0xfd787
jmp 0xfd8b8
movq -0xd0(%rbp), %rax
movl -0x10(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xfd79f
jmp 0xfd8b8
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x10(%rbp), %edx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
cmpq $0x0, %rax
je 0xfd8b6
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xfd88f
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x10(%rbp), %eax
je 0xfd84e
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xb0(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xfd850
jmp 0xfd881
movq -0xd0(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0xd0(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xfd81b
movq -0xd0(%rbp), %rax
movl -0x10(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq -0xd0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x3, (%rax,%rcx)
jmp 0xfd8b8
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xfd73b
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0xfdcf1
movq -0xd0(%rbp), %rax
movl -0x10(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0xfd8f1
jmp 0xfdce3
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x10(%rbp), %edx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
cmpq $0x0, %rax
je 0xfdce1
movq $0x0, -0xe8(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xfda8b
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x10(%rbp), %eax
je 0xfd9b7
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xb0(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xfd9bc
jmp 0xfda7a
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0xe0(%rbp), %rax
jne 0xfda4c
cmpq $0x0, -0xe0(%rbp)
je 0xfda12
movq -0xe0(%rbp), %rax
shlq %rax
movq %rax, -0xf8(%rbp)
jmp 0xfda20
movl $0x2, %eax
movq %rax, -0xf8(%rbp)
jmp 0xfda20
movq -0xf8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, -0xd8(%rbp)
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
shrl %edx
movq -0xd8(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xe8(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0xfda7a
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xfd980
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0xe8(%rbp), %rax
jae 0xfdcdf
movq $0x0, -0x28(%rbp)
cmpq $0x2, -0x28(%rbp)
jae 0xfdccc
movq -0xd8(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shll %eax
movl %eax, %eax
addq -0x28(%rbp), %rax
movl %eax, -0x14(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x2, %eax
jae 0xfdaed
jmp 0xfdcbb
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x14(%rbp), %edx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
cmpq $0x0, %rax
je 0xfdcb9
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xfdbf1
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x14(%rbp), %eax
je 0xfdbac
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xb0(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xfdbae
jmp 0xfdbe3
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movq -0x30(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfdb79
movq -0x48(%rbp), %xmm0
movaps 0x284413(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x284417(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x40(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x58(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xfdc3d
jmp 0xfdcbb
cmpq $0x3, -0x48(%rbp)
jbe 0xfdc46
jmp 0xfdcbb
cmpq $0x2, -0x50(%rbp)
jbe 0xfdc4f
jmp 0xfdcbb
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x14(%rbp), %edx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x1, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
jmp 0xfdcbb
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xfdaac
jmp 0xfdcce
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xfda93
jmp 0xfdce1
jmp 0xfdce3
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xfd8cd
movq $0x0, -0xf0(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x60(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xfdddc
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xfdd3e
jmp 0xfddce
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
je 0xfdd59
jmp 0xfddce
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x10(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xfdd81
jmp 0xfddce
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0xf0(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0xfddcc
movq -0x8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %esi
callq 0xd9510
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xfdd89
jmp 0xfddce
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xfdd03
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0xc8(%rbp), %rdi
callq 0x2210
movq -0xb0(%rbp), %rdi
callq 0x2210
movq -0x98(%rbp), %rdi
callq 0x2210
movq -0x80(%rbp), %rdi
callq 0x2210
movq -0x68(%rbp), %rdi
callq 0x2210
movq -0xd0(%rbp), %rdi
callq 0x2210
movq -0x60(%rbp), %rdi
callq 0xcd6f0
movq -0xd8(%rbp), %rdi
callq 0x2210
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)