// File: /workspace/github_cpp_makefile_star2_O1/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 0xb1a83
movq %rax, %rbx
movq 0x18(%rax), %r12
movl 0x14(%r12), %r15d
addl %r15d, %r15d
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %r13
movapd %xmm0, 0x50(%r13)
movapd %xmm0, 0x40(%r13)
movapd %xmm0, 0x30(%r13)
movapd %xmm0, 0x20(%r13)
movapd %xmm0, 0x10(%r13)
movapd %xmm0, (%r13)
movq $0x0, 0x60(%r13)
movl $0x1c, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, (%r13)
movq %r12, %rdi
movq %r13, %rsi
callq 0xbf22d
movq %rax, 0x50(%rsp)
movl $0x1, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x60(%rsp)
movq %rbx, 0x48(%rsp)
movq 0x18(%rbx), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0xbebfd
movq 0x20(%rax), %rcx
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0xbebe8
movb $0x0, 0x4(%rcx,%rsi,8)
movq 0x48(%rsp), %rdi
movq 0x10(%rdi), %rdi
movl (%rdi,%rdx,8), %r8d
movl %r8d, -0xc(%rdi,%rdx,8)
incq %rsi
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x5, %rdx
cmpq %rdi, %rsi
jb 0xbebcb
xorl %r11d, %r11d
testl %r15d, %r15d
je 0xbf067
movl %r15d, %ebx
leaq 0x90(%rsp), %r15
movb $0x1, %r12b
xorl %ebp, %ebp
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x20(%rdi), %rax
movl %ebp, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xbecff
movq 0x28(%rdi), %rax
testl $0xfffffffe, (%rax,%rbp,8) # imm = 0xFFFFFFFE
je 0xbecff
movq 0x60(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0xbecff
movq %r11, 0x38(%rsp)
movl %r11d, 0x30(%rsp)
movl %r11d, 0x28(%rsp)
movq %r11, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %r11d, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %ebp, %edx
movq 0x50(%rsp), %rcx
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
xorl %r11d, %r11d
testq %rax, %rax
je 0xbecff
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0xbece4
movq 0xa8(%rsp), %rcx
movq 0xd8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %rbp
je 0xbecdc
cmpl (%rcx), %edi
je 0xbecdc
movq %rdi, %r8
xorq $0x1, %r8
movq 0x60(%rsp), %r9
movb %r12b, (%r9,%r8)
movb %r12b, (%r9,%rdi)
incq %rsi
cmpq %rsi, %rax
jne 0xbecbc
movq 0x60(%rsp), %rcx
movb $0x2, (%rcx,%rbp)
movq 0xa8(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rcx,%rax)
incq %rbp
cmpq %rbx, %rbp
jne 0xbec19
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %edx, %edx
xorl %r11d, %r11d
movq %rbx, 0x58(%rsp)
movq 0x60(%rsp), %rax
cmpb $0x2, (%rax,%r15)
jne 0xbf05b
movq %rdx, %r13
movq %r11, %rbx
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
movq %rbp, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
testq %rax, %rax
je 0xbee7c
cmpq $0x0, 0xc8(%rsp)
movq %rbx, %r11
movq %r15, 0x68(%rsp)
je 0xbee8c
xorl %edi, %edi
xorl %r12d, %r12d
xorl %esi, %esi
movq 0x58(%rsp), %rbx
movq %r13, %rdx
movq 0xd8(%rsp), %rax
movl (%rax,%r12,4), %eax
cmpq %rax, %r15
je 0xbee3a
movq 0xa8(%rsp), %rcx
cmpl (%rcx), %eax
je 0xbee3a
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
shrl %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %r13d
cmpq %rdx, %rdi
jne 0xbee22
leaq (%rdx,%rdx), %r15
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %r15
movq %rsi, 0x70(%rsp)
leaq (,%r15,4), %rsi
movq %rdi, %rbx
movq %r11, %rdi
callq 0x2370
movq 0x70(%rsp), %rsi
movq %rbx, %rdi
movq 0x58(%rsp), %rbx
movq %rax, %r11
movq %r15, %rdx
movq 0x68(%rsp), %r15
addq %r13, %rsi
movq 0xd8(%rsp), %rax
movl (%rax,%r12,4), %eax
shrl %eax
movl %eax, (%r11,%rdi,4)
incq %rdi
incq %r12
cmpq 0xc8(%rsp), %r12
jb 0xbedac
movq %rsi, %xmm0
punpckldq 0x1663e8(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1663f0(%rip), %xmm0   # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x165d5c(%rip), %xmm1   # 0x224bd0
movapd %xmm1, 0x70(%rsp)
jmp 0xbeea0
movq %rbx, %r11
movq 0x58(%rsp), %rbx
movq %r13, %rdx
jmp 0xbf05b
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
xorl %edi, %edi
movq 0x58(%rsp), %rbx
movq %r13, %rdx
testq %rdi, %rdi
je 0xbf05b
movq %rdx, 0x80(%rsp)
xorl %r13d, %r13d
movq %rdi, 0x88(%rsp)
xorl %r12d, %r12d
movl (%r11,%r13,4), %eax
leal (%r12,%rax,2), %r15d
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x28(%rdi), %rax
testl $0xfffffffe, (%rax,%r15,8) # imm = 0xFFFFFFFE
je 0xbf024
movq %rbp, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq %r11, %rbx
callq 0xbf39f
movq %rbx, %r11
testq %rax, %rax
je 0xbf024
movq 0xc8(%rsp), %rcx
testq %rcx, %rcx
je 0xbef93
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 0xbef82
cmpl (%rdx), %r9d
je 0xbef82
movq 0x48(%rsp), %r10
movq 0x10(%r10), %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 0xbef59
cmpq $0x3, %rdi
setae %cl
jmp 0xbef97
xorl %eax, %eax
xorl %ecx, %ecx
movq %rax, %xmm0
punpckldq 0x16629c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1662a4(%rip), %xmm0   # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd 0x70(%rsp), %xmm1
setae %dl
cmpq $0x4, %rax
setae %al
orb %cl, %al
orb %dl, %al
testb $0x1, %al
jne 0xbf024
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
movq %rbp, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r15d, %edx
movq 0x50(%rsp), %rcx
leaq 0x90(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
movq %rbx, %r11
leaq 0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
je 0xbeebf
incq %r13
movq 0x88(%rsp), %rdi
cmpq %rdi, %r13
jne 0xbeebc
movq 0x58(%rsp), %rbx
movq 0x68(%rsp), %r15
movq 0x80(%rsp), %rdx
incq %r15
cmpq %rbx, %r15
jne 0xbed1a
movq %r11, 0x50(%rsp)
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdx
testl $0x7fffffff, 0x14(%rdx) # imm = 0x7FFFFFFF
je 0xbf1b4
xorl %ebx, %ebx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%rbx,8)
js 0xbf197
cmpb $0x1, 0x4(%rdx,%rbx,8)
jne 0xbf197
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rdx
leaq (%rbx,%rbx,4), %rsi
cmpl $0x0, 0xc(%rdx,%rsi,8)
je 0xbf197
leaq (%rdx,%rsi,8), %rdx
cmpl $0x0, 0x10(%rdx)
je 0xbf197
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
xorl %r8d, %r8d
leaq (%r8,%rdi), %r9
movq 0x28(%r14), %r10
movq (%r10,%r9,8), %r11
movq %r11, %r10
andq %rcx, %r10
je 0xbf178
shrq $0x20, %r11
leaq (%r11,%r11,2), %r11
shlq $0x3, %r11
addq 0x8(%r14), %r11
xorl %r15d, %r15d
leaq (%r15,%r15,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 0xbf170
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), %eax
xorl %r9d, %eax
cmpl $0x1, %eax
jne 0xbf167
orb $-0x80, 0x3(%r13,%rbp)
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0xbf154
incq %r15
cmpq %r10, %r15
jne 0xbf10e
leaq 0x1(%r8), %rax
testq %r8, %r8
movq %rax, %r8
je 0xbf0e7
incq %rsi
movl 0x10(%rdx), %eax
cmpq %rax, %rsi
jb 0xbf0ca
incq %rbx
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rbx
jb 0xbf089
movq %r14, %rdi
callq 0xa02fa
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 0x60(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0xa0004
movq 0x50(%rsp), %rdi
callq 0x2220
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq