// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
xorl %r12d, %r12d
movq %rdi, 0x60(%rsp)
xorl %esi, %esi
callq 0x781bd
movq %rax, %rbx
movq 0x18(%rax), %r14
movl 0x14(%r14), %r13d
addl %r13d, %r13d
xorpd %xmm0, %xmm0
leaq 0x78(%rsp), %r15
movupd %xmm0, 0x58(%r15)
movupd %xmm0, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
movupd %xmm0, 0x28(%r15)
movupd %xmm0, 0x18(%r15)
movupd %xmm0, 0x8(%r15)
pushq $0x1c
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x84975
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rsi
movq %r13, 0x58(%rsp)
movq %r13, %rdi
callq 0x2120
movq %rax, 0x40(%rsp)
pushq $0x2
popq %rax
movl 0x14(%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r12
jae 0x844d9
movq 0x20(%r14), %rcx
cmpl $0x0, (%rcx,%r12,8)
js 0x844d0
movb $0x0, 0x4(%rcx,%r12,8)
movq 0x10(%rbx), %rcx
movl (%rcx,%rax,8), %edx
movl %edx, -0xc(%rcx,%rax,8)
incq %r12
addq $0x5, %rax
jmp 0x844a5
xorl %r15d, %r15d
leaq 0x78(%rsp), %r12
pushq $-0x1
popq %rbp
movb $0x1, %r13b
cmpq 0x58(%rsp), %r15
je 0x845cd
movq 0x20(%r14), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x845c5
movq 0x28(%r14), %rax
testl $0xfffffffe, (%rax,%r15,8) # imm = 0xFFFFFFFE
je 0x845c5
movq 0x40(%rsp), %rax
cmpb $0x0, (%rax,%r15)
jne 0x845c5
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
movq %r12, %r8
movl %ebp, %r9d
callq 0x84a86
testq %rax, %rax
je 0x845c5
movq 0x90(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0xc0(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x845b1
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r15
je 0x845ac
cmpl (%rax), %edi
je 0x845ac
movq %rdi, %r8
xorq $0x1, %r8
movq 0x40(%rsp), %r9
movb %r13b, (%r9,%r8)
movb %r13b, (%r9,%rdi)
incq %rsi
jmp 0x84587
movq 0x40(%rsp), %rcx
movb $0x2, (%rcx,%r15)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rcx,%rax)
incq %r15
jmp 0x844e7
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq 0x58(%rsp), %r14
je 0x84898
movq 0x40(%rsp), %rax
cmpb $0x2, (%rax,%r14)
jne 0x84890
movq 0x18(%rbx), %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
xorl %esi, %esi
movl %r14d, %edx
movq 0x50(%rsp), %rcx
leaq 0x78(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
testq %rax, %rax
je 0x84890
movq %r14, 0x68(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, 0x48(%rsp)
cmpq 0xb0(%rsp), %r15
jae 0x846e2
movq 0xc0(%rsp), %rax
movl (%rax,%r15,4), %eax
cmpq %rax, 0x68(%rsp)
je 0x846da
movq 0x90(%rsp), %rcx
cmpl (%rcx), %eax
je 0x846da
movq 0x10(%rbx), %rcx
shrl %eax
imulq $0x28, %rax, %rdx
movl 0x10(%rcx,%rdx), %r12d
cmpq %r13, 0x48(%rsp)
jne 0x846c6
movq %r13, %rax
addq %r13, %r13
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r13
leaq (,%r13,4), %rsi
movq %rbp, %rdi
callq 0x2370
movq %rax, %rbp
movq 0xc0(%rsp), %rax
movl (%rax,%r15,4), %eax
shrl %eax
addq %r12, %r14
movq 0x48(%rsp), %rcx
movl %eax, (%rbp,%rcx,4)
incq %rcx
movq %rcx, 0x48(%rsp)
incq %r15
jmp 0x84652
movq %r14, %xmm0
punpckldq 0x15eb81(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x15eb89(%rip), %xmm0   # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x15e4e5(%rip), %xmm1   # 0x1e2bf0
movapd %xmm1, 0xe0(%rsp)
xorl %r14d, %r14d
movq %r13, 0x70(%rsp)
cmpq 0x48(%rsp), %r14
je 0x8488b
xorl %r13d, %r13d
cmpq $0x2, %r13
je 0x8487e
movl (%rbp,%r14,4), %eax
leal (%r13,%rax,2), %r12d
movq 0x18(%rbx), %r15
movq 0x28(%r15), %rax
testl $0xfffffffe, (%rax,%r12,8) # imm = 0xFFFFFFFE
je 0x84876
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
movl %r12d, %edx
movq 0x50(%rsp), %rcx
leaq 0x78(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
testq %rax, %rax
je 0x84876
movq 0x90(%rsp), %rdx
movq 0xb0(%rsp), %rsi
movq 0xc0(%rsp), %rdi
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %r8, %rsi
je 0x847ee
movl (%rdi,%r8,4), %r9d
cmpl %r12d, %r9d
je 0x847e9
cmpl (%rdx), %r9d
je 0x847e9
movq 0x10(%rbx), %r10
shrl %r9d
imulq $0x28, %r9, %r9
movl 0x10(%r10,%r9), %r9d
addq %r9, %rcx
incq %rax
incq %r8
jmp 0x847c0
movq %rcx, %xmm0
punpckldq 0x15ea75(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x15ea7d(%rip), %xmm0   # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd 0xe0(%rsp), %xmm1
setae %dl
cmpq $0x4, %rcx
setae %cl
cmpq $0x3, %rax
setae %al
orb %cl, %al
orb %dl, %al
jne 0x84876
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
orl $-0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movl %r12d, %edx
movq 0x50(%rsp), %rcx
leaq 0x78(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
incq %r13
jmp 0x8472a
incq %r14
movq 0x70(%rsp), %r13
jmp 0x8471c
movq 0x68(%rsp), %r14
incq %r14
jmp 0x845d5
movq 0x18(%rbx), %r14
xorl %r15d, %r15d
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r15
jae 0x84901
movq 0x20(%r14), %rax
cmpl $0x0, (%rax,%r15,8)
js 0x848fc
cmpb $0x1, 0x4(%rax,%r15,8)
jne 0x848fc
movq 0x10(%rbx), %r12
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%r12,%rax)
je 0x848fc
addq %rax, %r12
xorl %r13d, %r13d
movl 0x10(%r12), %eax
cmpq %rax, %r13
jae 0x848fc
movq 0x18(%r12), %rax
movq (%rax,%r13,8), %rsi
shrq $0x21, %rsi
movq 0x60(%rsp), %rdi
callq 0x6f8da
incq %r13
jmp 0x848d6
incq %r15
jmp 0x8489f
movq 0x60(%rsp), %rdi
callq 0x694eb
movq 0x78(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x6921d
movq %rbp, %rdi
callq 0x2220
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq