// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: force_trio_clean(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, R_to_U*, unsigned char, float, float, unsigned int)
void force_trio_clean(ma_ug_t* ug, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, R_to_U* ruIndex, 
uint8_t flag, float self_drop_rate, float contig_drop_rate, uint32_t min_occ)
{
    asg_t* nsg = ug->g;
    uint32_t beg, end, n_vtx = nsg->n_seq<<1, k, i, rId, tf_occ, tnf_occ, flag_occ, non_flag_occ, n_reduce = 1;
    long long nodeLen, baseLen, max_stop_nodeLen, max_stop_baseLen;
    buf_t b; memset(&b, 0, sizeof(buf_t));
    ma_utg_t *u = NULL;
    uint8_t* primary_flag = (uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t));

    while (n_reduce)
    {
        n_reduce = 0;
        n_vtx = nsg->n_seq;
        for (beg = 0; beg < n_vtx; ++beg) 
        {
            if(nsg->seq[beg>>1].del || asg_arc_n(nsg, beg) <= 0 || get_real_length(nsg, beg, NULL)<=0)
            {
                continue;
            }
            if(get_real_length(nsg, beg^1, NULL) == 1)///check if beg is the tig end
            {
                get_real_length(nsg, beg^1, &end);
                if(get_real_length(nsg, end^1, NULL) == 1) continue;
            }
            b.b.n = 0; tf_occ = tnf_occ = 0;
            get_unitig(nsg, ug, beg, &end, &nodeLen, &baseLen, &max_stop_nodeLen, &max_stop_baseLen, 1, &b);

            for (i = 0; i < b.b.n; i++)
            {
                u = &((ug)->u.a[b.b.a[i]>>1]);
                for (k = 0; k < u->n; k++)
                {
                    rId = u->a[k]>>33;
                    if(R_INF.trio_flag[rId] == AMBIGU) continue;
                    if(R_INF.trio_flag[rId] == DROP) continue;
                    if(R_INF.trio_flag[rId] == flag) tf_occ++;
                    if(R_INF.trio_flag[rId] != flag) tnf_occ++;
                }
            }
            if(tnf_occ <= ((tnf_occ+tf_occ)*contig_drop_rate)) continue;

            for (i = 0; i < b.b.n; i++)
            {
                flag_occ = non_flag_occ = 0;
                u = &((ug)->u.a[b.b.a[i]>>1]);
                for (k = 0; k < u->n; k++)
                {
                    rId = u->a[k]>>33;
                    if(R_INF.trio_flag[rId] == AMBIGU) continue;
                    if(R_INF.trio_flag[rId] == DROP) continue;
                    if(R_INF.trio_flag[rId] == flag) flag_occ++;
                    if(R_INF.trio_flag[rId] != flag) non_flag_occ++;
                }

                if(non_flag_occ <= min_occ) continue;
                if(non_flag_occ <= ((non_flag_occ+flag_occ)*self_drop_rate)) continue;
                if(non_flag_occ <= ((tf_occ+tnf_occ)*contig_drop_rate)) continue;
                if(if_primary_unitig(u, read_g, coverage_cut, sources, ruIndex, primary_flag))
                {
                    continue;
                }

                if(u->m != 0)
                {
                    u->circ = u->end = u->len = u->m = u->n = u->start = 0;
                    free(u->a);
                    u->a = NULL;
                }
                asg_seq_del(nsg, b.b.a[i]>>1);
                n_reduce++;
            }
            
        }
    }

    free(b.b.a);
    asg_cleanup(nsg);
    free(primary_flag);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movss %xmm1, 0x28(%rsp)
movss %xmm0, 0x2c(%rsp)
movq %r9, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %rbp
movb 0x120(%rsp), %r14b
movq 0x18(%rdi), %rbx
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rax
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rsi, 0x38(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x30(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movq %rbx, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0x98f60
movl 0x14(%rbx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x58(%rsp)
xorl %esi, %esi
movl $0x0, 0xc(%rsp)
cmpq 0x58(%rsp), %rsi
je 0x98c83
movq 0x20(%rbx), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x98f58
movq 0x28(%rbx), %rax
cmpl $0x0, (%rax,%rsi,8)
je 0x98f58
movq %rbx, %rdi
xorl %edx, %edx
movq %rsi, %r15
callq 0xc2e7a
testl %eax, %eax
jle 0x98f55
movq %r15, 0x10(%rsp)
movl %r15d, %r12d
xorl $0x1, %r12d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x98d2f
movq %rbx, %rdi
movl %r12d, %esi
leaq 0x1c(%rsp), %rdx
callq 0xc2e7a
movl 0x1c(%rsp), %esi
xorl $0x1, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x98d2f
movq 0x10(%rsp), %rsi
jmp 0x98f58
andq $0x0, 0xb8(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
leaq 0x80(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0xb8(%rsp), %rbx
movq 0xc8(%rsp), %rdx
leaq 0x1bb4a4(%rip), %rax    # 0x254230
movq 0x28(%rax), %rsi
xorl %edi, %edi
xorl %eax, %eax
xorl %ecx, %ecx
movl %edi, %edi
cmpq %rdi, %rbx
jbe 0x98df9
movq 0x10(%rbp), %r9
movl (%rdx,%rdi,4), %r8d
shrl %r8d
imulq $0x28, %r8, %r10
leaq (%r9,%r10), %r8
movl 0x10(%r9,%r10), %r9d
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x98df5
movq 0x18(%r8), %r11
movq (%r11,%r10,8), %r11
shrq $0x21, %r11
movzbl (%rsi,%r11), %r11d
testl %r11d, %r11d
je 0x98df0
cmpl $0x5, %r11d
je 0x98df0
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpb %r14b, %r11b
setne %r15b
sete %r12b
addl %r12d, %eax
addl %r15d, %ecx
incq %r10
jmp 0x98db8
incl %edi
jmp 0x98d96
movl %ecx, %edx
cvtsi2ss %rdx, %xmm0
addl %ecx, %eax
cvtsi2ss %rax, %xmm2
mulss 0x28(%rsp), %xmm2
ucomiss %xmm0, %xmm2
movq 0x10(%rsp), %rsi
jae 0x98f4e
xorl %r15d, %r15d
movss %xmm2, 0x18(%rsp)
movl %r15d, %r15d
cmpq %r15, %rbx
jbe 0x98f4e
movq 0x10(%rbp), %rax
movq 0xc8(%rsp), %rcx
movl (%rcx,%r15,4), %r12d
shrl %r12d
imulq $0x28, %r12, %rcx
leaq (%rax,%rcx), %r13
movl 0x10(%rax,%rcx), %edx
leaq 0x1bb3da(%rip), %rax    # 0x254230
movq 0x28(%rax), %r11
xorl %edi, %edi
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rdi, %rdx
je 0x98e9d
movq 0x18(%r13), %r8
movq (%r8,%rdi,8), %r8
shrq $0x21, %r8
movzbl (%r11,%r8), %r8d
testl %r8d, %r8d
je 0x98e98
cmpl $0x5, %r8d
je 0x98e98
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpb %r14b, %r8b
setne %r9b
sete %r10b
addl %r10d, %ecx
addl %r9d, %eax
incq %rdi
jmp 0x98e60
cmpl 0x128(%rsp), %eax
jbe 0x98f46
movl %eax, %edx
cvtsi2ss %rdx, %xmm0
ucomiss %xmm0, %xmm2
jae 0x98f46
addl %ecx, %eax
cvtsi2ss %rax, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x98f46
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x945a4
testb %al, %al
jne 0x98f3b
cmpl $0x0, 0xc(%r13)
je 0x98f22
andl $0x0, 0x10(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq 0x18(%r13), %rdi
callq 0x2220
andq $0x0, 0x18(%r13)
movq 0xc8(%rsp), %rax
movl (%rax,%r15,4), %r12d
shrl %r12d
movq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0x6f8da
incl 0xc(%rsp)
movq 0xb8(%rsp), %rbx
movq 0x10(%rsp), %rsi
movss 0x18(%rsp), %xmm2
incl %r15d
jmp 0x98e24
movq 0x20(%rsp), %rbx
jmp 0x98f58
movq %r15, %rsi
incq %rsi
jmp 0x98ca5
movq 0xc8(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x694eb
movq 0x30(%rsp), %rdi
callq 0x2220
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq