// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %r14
xorl %esi, %esi
callq 0xb2a6d
movq %rax, 0x48(%rsp)
movq 0x18(%rax), %rbx
movl 0x14(%rbx), %ebp
addl %ebp, %ebp
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %r12
movupd %xmm0, 0x58(%r12)
movupd %xmm0, 0x48(%r12)
movupd %xmm0, 0x38(%r12)
movupd %xmm0, 0x28(%r12)
movupd %xmm0, 0x18(%r12)
movupd %xmm0, 0x8(%r12)
movl $0x1c, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, (%r12)
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %r12
callq 0xc0162
movq %rax, 0x50(%rsp)
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r15
movl 0x14(%rbx), %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0xbfb38
movq 0x20(%r12), %rax
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rax,%rsi,8)
js 0xbfb24
movb $0x0, 0x4(%rax,%rsi,8)
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx,%rdx,8), %edi
movl %edi, -0xc(%rcx,%rdx,8)
movl 0x14(%r12), %ecx
incq %rsi
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x5, %rdx
cmpq %rdi, %rsi
jb 0xbfb04
xorl %r13d, %r13d
testq %rbp, %rbp
movq %r15, 0x60(%rsp)
je 0xbff7b
movb $0x1, %bl
xorl %r13d, %r13d
movq 0x20(%r12), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xbfc20
movq 0x28(%r12), %rax
testl $0xfffffffe, (%rax,%r13,8) # imm = 0xFFFFFFFE
je 0xbfc20
cmpb $0x0, (%r15,%r13)
jne 0xbfc20
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r12, %rdi
xorl %esi, %esi
movl %r13d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
testq %rax, %rax
je 0xbfc20
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
testq %rcx, %rcx
je 0xbfc10
movq 0xd8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r13
je 0xbfc08
cmpl (%rax), %edi
je 0xbfc08
movq %rdi, %r8
xorq $0x1, %r8
movb %bl, (%r15,%r8)
movb %bl, (%r15,%rdi)
incq %rsi
cmpq %rsi, %rcx
jne 0xbfbed
movb $0x2, (%r15,%r13)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r15,%rax)
incq %r13
cmpq %rbp, %r13
jne 0xbfb4e
testl %ebp, %ebp
movq 0x48(%rsp), %r11
je 0xbff82
xorl %r12d, %r12d
movq $0x0, 0x78(%rsp)
xorl %r13d, %r13d
movq %rbp, 0x68(%rsp)
cmpb $0x2, (%r15,%r12)
jne 0xbff5b
movq 0x18(%r11), %rdi
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r12d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
testq %rax, %rax
je 0xbff56
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0xbff56
movq 0xd8(%rsp), %rcx
movq $0x0, 0x58(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x48(%rsp), %r11
movq %r12, 0x70(%rsp)
movl (%rcx,%r15,4), %edx
cmpq %rdx, %r12
je 0xbfd6c
movq 0xa8(%rsp), %rsi
cmpl (%rsi), %edx
je 0xbfd6c
movq 0x10(%r11), %rsi
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl 0x10(%rsi,%rdx,8), %r12d
movq 0x58(%rsp), %rbp
cmpq 0x78(%rsp), %rbp
jne 0xbfd51
leaq (,%rbp,2), %rax
testq %rbp, %rbp
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x78(%rsp)
leaq (,%rax,4), %rsi
movq %r13, %rdi
callq 0x2370
movq 0x48(%rsp), %r11
movq %rax, %r13
movq 0xc8(%rsp), %rax
movq 0xd8(%rsp), %rcx
addq %r12, %rbx
movl (%rcx,%r15,4), %edx
shrl %edx
movl %edx, (%r13,%rbp,4)
incq %rbp
movq %rbp, 0x58(%rsp)
movq 0x70(%rsp), %r12
incq %r15
cmpq %rax, %r15
jb 0xbfcdc
movq %rbx, %xmm0
punpckldq 0x1714fb(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x171503(%rip), %xmm0   # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x170e6f(%rip), %xmm1   # 0x230c10
movapd %xmm1, 0x80(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0xbff69
movq %r13, %rbp
movq 0x18(%r11), %r13
xorl %r12d, %r12d
xorl %eax, %eax
movb $0x1, %bl
movl (%rbp,%r12,4), %ecx
leal (%rax,%rcx,2), %r15d
movq 0x28(%r13), %rax
testl $0xfffffffe, (%rax,%r15,8) # imm = 0xFFFFFFFE
je 0xbff21
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r13, %rdi
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
movq 0x48(%rsp), %r11
testq %rax, %rax
je 0xbff21
movq 0xc8(%rsp), %rcx
testq %rcx, %rcx
je 0xbfe8f
movq 0xa8(%rsp), %rdx
movq 0xd8(%rsp), %rsi
xorl %edi, %edi
xorl %eax, %eax
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
cmpl %r15d, %r9d
je 0xbfe7e
cmpl (%rdx), %r9d
je 0xbfe7e
movq 0x10(%r11), %r10
shrl %r9d
leaq (%r9,%r9,4), %r9
movl 0x10(%r10,%r9,8), %r9d
addq %r9, %rax
incq %rdi
incq %r8
cmpq %r8, %rcx
jne 0xbfe5a
cmpq $0x3, %rdi
setae %cl
jmp 0xbfe93
xorl %eax, %eax
xorl %ecx, %ecx
movq %rax, %xmm0
punpckldq 0x1713e0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1713e8(%rip), %xmm0   # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd 0x80(%rsp), %xmm1
setae %dl
cmpq $0x4, %rax
setae %al
orb %cl, %al
orb %dl, %al
testb $0x1, %al
jne 0xbff21
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r13, %rdi
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
movq 0x48(%rsp), %r11
movl $0x1, %eax
testb $0x1, %bl
movl $0x0, %ebx
jne 0xbfdc4
incq %r12
cmpq 0x58(%rsp), %r12
jne 0xbfdc0
movq 0x60(%rsp), %r15
movq %rbp, %r13
movq 0x68(%rsp), %rbp
movq 0x70(%rsp), %r12
jmp 0xbff5b
movq 0x48(%rsp), %r11
incq %r12
cmpq %rbp, %r12
jne 0xbfc4d
jmp 0xbff75
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rbp
jmp 0xbff5b
movq 0x18(%r11), %r12
jmp 0xbff85
movq 0x48(%rsp), %r11
jmp 0xbff85
xorl %r13d, %r13d
movl 0x14(%r12), %esi
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0xc00ec
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r13, 0x50(%rsp)
movq %r12, 0x80(%rsp)
movq 0x20(%r12), %rdx
cmpl $0x0, (%rdx,%rax,8)
js 0xc00d8
cmpb $0x1, 0x4(%rdx,%rax,8)
jne 0xc00d8
movq 0x10(%r11), %rdx
leaq (%rax,%rax,4), %rdi
cmpl $0x0, 0xc(%rdx,%rdi,8)
je 0xc00d8
leaq (%rdx,%rdi,8), %rdx
cmpl $0x0, 0x10(%rdx)
je 0xc00d8
xorl %esi, %esi
movq 0x18(%rdx), %rdi
movq (%rdi,%rsi,8), %rdi
shrq $0x21, %rdi
movq 0x20(%r14), %r8
orl $0x80000000, (%r8,%rdi,8) # imm = 0x80000000
addl %edi, %edi
movb $0x1, %r8b
xorl %r9d, %r9d
orq %rdi, %r9
movq 0x28(%r14), %r10
movq (%r10,%r9,8), %r11
movq %r11, %r10
andq %rcx, %r10
je 0xc0097
shrq $0x20, %r11
leaq (%r11,%r11,2), %r11
shlq $0x3, %r11
addq 0x8(%r14), %r11
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %r12
orb $-0x80, 0xf(%r11,%r12,8)
movl 0x8(%r11,%r12,8), %r12d
xorq $0x1, %r12
movq 0x28(%r14), %r13
movq (%r13,%r12,8), %r13
movq %r13, %r12
andq %rcx, %r12
je 0xc008f
movq 0x8(%r14), %rbp
shrq $0x20, %r13
shlq $0x3, %r12
leaq (%r12,%r12,2), %r12
leaq (%r13,%r13,2), %r13
leaq 0xc(,%r13,8), %r13
addq %rbp, %r13
xorl %ebp, %ebp
movl -0x4(%r13,%rbp), %r15d
xorl %r9d, %r15d
cmpl $0x1, %r15d
jne 0xc0086
orb $-0x80, 0x3(%r13,%rbp)
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0xc0072
incq %rbx
cmpq %r10, %rbx
jne 0xc002c
movl $0x1, %r9d
testb $0x1, %r8b
movl $0x0, %r8d
jne 0xc0007
incq %rsi
movl 0x10(%rdx), %edi
cmpq %rdi, %rsi
jb 0xbffe7
movq 0x80(%rsp), %r12
movl 0x14(%r12), %esi
movq 0x48(%rsp), %r11
movq 0x60(%rsp), %r15
movq 0x50(%rsp), %r13
incq %rax
movl %esi, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rax
jb 0xbffaa
movq %r14, %rdi
movq %r11, %rbx
callq 0xa0a7d
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 0xf0(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0761
movq %r13, %rdi
callq 0x2220
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq