// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
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)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq $0x0, 0x60(%rax)
movq %rsi, 0x38(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x30(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r12, (%rsp)
movl 0x14(%rbx), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, 0x58(%rsp)
je 0xd5d72
xorl %edx, %edx
movl $0x0, 0xc(%rsp)
movq 0x20(%rbx), %rax
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
movq %rdx, 0x20(%rsp)
js 0xd5d54
movq 0x28(%rbx), %rcx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rsi
andq %r13, %rsi
je 0xd5d54
movq 0x8(%rbx), %rax
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xd5954
testl %r8d, %r8d
jle 0xd5d54
movl 0x20(%rsp), %edx
xorq $0x1, %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rsi
andq %r13, %rsi
je 0xd5a2d
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xd59a9
cmpl $0x1, %r8d
jne 0xd5a2d
xorl %edi, %edi
cmpl $0x0, (%rdx,%rdi)
js 0xd59d8
movl -0x4(%rdx,%rdi), %r8d
movl %r8d, 0x14(%rsp)
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xd59c8
movl 0x14(%rsp), %edx
xorq $0x1, %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rcx
andq %r13, %rcx
je 0xd5a2d
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xd5a11
cmpl $0x1, %esi
je 0xd5d54
movq $0x0, 0xb8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
leaq 0x14(%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 0x1053a7
addq $0x20, %rsp
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0xd5b14
movq 0x10(%r12), %rdx
movq 0xc8(%rsp), %rsi
leaq 0x1c0790(%rip), %rax    # 0x296230
movq 0x28(%rax), %rdi
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
movl (%rsi,%r8,4), %r9d
shrl %r9d
leaq (%r9,%r9,4), %r10
movl 0x10(%rdx,%r10,8), %r9d
testq %r9, %r9
je 0xd5b05
leaq (%rdx,%r10,8), %r10
movq 0x18(%r10), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %r14
shrq $0x21, %r14
movzbl (%rdi,%r14), %ebp
testl %ebp, %ebp
je 0xd5afd
cmpl $0x5, %ebp
je 0xd5afd
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpb 0x120(%rsp), %bpl
setne %r14b
sete %r15b
addl %r15d, %eax
addl %r14d, %ecx
incq %r11
cmpq %r11, %r9
jne 0xd5acb
incq %r8
movl %r8d, %r9d
cmpq %r9, 0x18(%rsp)
ja 0xd5aab
jmp 0xd5b18
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
cvtsi2ss %rdx, %xmm0
addl %ecx, %eax
cvtsi2ss %rax, %xmm2
mulss 0x28(%rsp), %xmm2
ucomiss %xmm0, %xmm2
setae %al
cmpq $0x0, 0x18(%rsp)
sete %cl
orb %al, %cl
jne 0xd5d54
xorl %r15d, %r15d
movss %xmm2, 0x10(%rsp)
movq 0x10(%r12), %rax
movq 0xc8(%rsp), %rcx
movl (%rcx,%r15,4), %r14d
movl %r14d, %ebp
shrl %ebp
leaq (,%rbp,4), %rdx
addq %rbp, %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0xd5c33
leaq (%rax,%rdx,8), %r12
movq 0x18(%r12), %rsi
leaq 0x1c06a6(%rip), %rax    # 0x296230
movq 0x28(%rax), %rdi
xorl %r8d, %r8d
xorl %eax, %eax
xorl %edx, %edx
movq (%rsi,%r8,8), %r9
shrq $0x21, %r9
movzbl (%rdi,%r9), %r9d
testl %r9d, %r9d
je 0xd5bc9
cmpl $0x5, %r9d
je 0xd5bc9
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpb 0x120(%rsp), %r9b
setne %r10b
sete %r11b
addl %r11d, %edx
addl %r10d, %eax
incq %r8
cmpq %r8, %rcx
jne 0xd5b95
cmpl 0x128(%rsp), %eax
jbe 0xd5c2f
movl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
ucomiss %xmm0, %xmm2
jae 0xd5c2f
addl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd5c2f
movq %r12, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xcffc4
testb %al, %al
je 0xd5c46
movq (%rsp), %r12
movss 0x10(%rsp), %xmm2
jmp 0xd5c33
movq (%rsp), %r12
incl %r15d
cmpq %r15, 0x18(%rsp)
ja 0xd5b4c
jmp 0xd5d54
cmpl $0x0, 0xc(%r12)
je 0xd5c83
movl $0x0, 0x10(%r12)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq 0x18(%r12), %rdi
callq 0x2220
movq $0x0, 0x18(%r12)
movq 0xc8(%rsp), %rax
movl (%rax,%r15,4), %r14d
movl %r14d, %ebp
shrl %ebp
movq 0x20(%rbx), %rax
orl $0x80000000, (%rax,%rbp,8) # imm = 0x80000000
andl $-0x2, %r14d
movb $0x1, %al
xorl %ecx, %ecx
movq (%rsp), %r12
movss 0x10(%rsp), %xmm2
orq %r14, %rcx
movq 0x28(%rbx), %rdx
movq (%rdx,%rcx,8), %rsi
movq %rsi, %rdx
andq %r13, %rdx
je 0xd5d2c
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
shlq $0x3, %rsi
addq 0x8(%rbx), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r8
orb $-0x80, 0xf(%rsi,%r8,8)
movl 0x8(%rsi,%r8,8), %r8d
xorq $0x1, %r8
movq 0x28(%rbx), %r9
movq (%r9,%r8,8), %r9
movq %r9, %r8
andq %r13, %r8
je 0xd5d24
movq 0x8(%rbx), %r10
shrq $0x20, %r9
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
leaq (%r9,%r9,2), %r9
leaq (%r10,%r9,8), %r9
addq $0xc, %r9
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %r11d
xorl %ecx, %r11d
cmpl $0x1, %r11d
jne 0xd5d1b
orb $-0x80, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %r8
jne 0xd5d07
incq %rdi
cmpq %rdx, %rdi
jne 0xd5cc5
movl $0x1, %ecx
testb $0x1, %al
movl $0x0, %eax
jne 0xd5ca0
incl 0xc(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0xd5c33
movq 0x20(%rsp), %rdx
incq %rdx
cmpq 0x58(%rsp), %rdx
jne 0xd5908
cmpl $0x0, 0xc(%rsp)
jne 0xd58ea
movq 0xc8(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0a7d
movq 0x30(%rsp), %rdi
callq 0x2220
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq