// File: /workspace/github_cpp_makefile_star2_O1/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
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)
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, 0x8(%rsp)
movl 0x14(%rbx), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, 0x58(%rsp)
je 0xd49c7
xorl %edx, %edx
movl $0x0, 0x10(%rsp)
movq 0x20(%rbx), %rax
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
movq %rdx, 0x20(%rsp)
js 0xd49a9
movq 0x28(%rbx), %rax
movq (%rax,%rdx,8), %rcx
movq %rcx, %rdx
andq %r13, %rdx
je 0xd49a9
movq 0x8(%rbx), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xd459e
testl %edi, %edi
jle 0xd49a9
movl 0x20(%rsp), %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rdx
andq %r13, %rdx
je 0xd4692
movq 0x8(%rbx), %rdi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
leaq (%rdi,%rsi,8), %rdi
addq $0xc, %rdi
leaq (,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl (%rdi,%r8), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x18, %r8
cmpq %r8, %rcx
jne 0xd45fb
cmpl $0x1, %r9d
jne 0xd4692
testq %rdx, %rdx
je 0xd4642
movq 0x8(%rbx), %rdx
leaq (%rdx,%rsi,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
cmpl $0x0, (%rdx,%rsi)
js 0xd4639
movl -0x4(%rdx,%rsi), %edi
movl %edi, 0x14(%rsp)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xd462b
movl 0x14(%rsp), %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
andq %r13, %rcx
je 0xd4692
movq 0x8(%rbx), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,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 0xd4676
cmpl $0x1, %esi
je 0xd49a9
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 0x10109f
addq $0x20, %rsp
movq 0xb8(%rsp), %rcx
testq %rcx, %rcx
je 0xd4775
movq 0x10(%r12), %rsi
movq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x1b6b2b(%rip), %rax    # 0x28b230
movq 0x28(%rax), %r8
xorl %r9d, %r9d
xorl %edx, %edx
xorl %eax, %eax
movq 0x18(%rsp), %rdi
movl (%rdi,%r9,4), %r10d
shrl %r10d
leaq (%r10,%r10,4), %r11
movl 0x10(%rsi,%r11,8), %r10d
testq %r10, %r10
je 0xd4768
leaq (%rsi,%r11,8), %r11
movq 0x18(%r11), %r11
xorl %r12d, %r12d
movq (%r11,%r12,8), %rbp
shrq $0x21, %rbp
movzbl (%r8,%rbp), %ebp
testl %ebp, %ebp
je 0xd4760
cmpl $0x5, %ebp
je 0xd4760
xorl %r15d, %r15d
xorl %edi, %edi
cmpb %r14b, %bpl
setne %r15b
sete %dil
addl %edi, %eax
addl %r15d, %edx
incq %r12
cmpq %r12, %r10
jne 0xd4735
incq %r9
movl %r9d, %edi
cmpq %rdi, %rcx
ja 0xd4710
jmp 0xd4779
xorl %eax, %eax
xorl %edx, %edx
movl %edx, %ecx
cvtsi2ss %rcx, %xmm0
addl %edx, %eax
cvtsi2ss %rax, %xmm2
mulss 0x28(%rsp), %xmm2
ucomiss %xmm0, %xmm2
jae 0xd49a4
cmpq $0x0, 0xb8(%rsp)
movq 0x8(%rsp), %r12
je 0xd49a9
xorl %ebp, %ebp
movss %xmm2, 0x18(%rsp)
movq 0x10(%r12), %rax
movq 0xc8(%rsp), %rcx
movl (%rcx,%rbp,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %r12
movl 0x10(%r12), %edx
movl $0x0, %ecx
movl $0x0, %eax
testq %rdx, %rdx
je 0xd482e
movq 0x18(%r12), %rsi
leaq 0x1b6a44(%rip), %rax    # 0x28b230
movq 0x28(%rax), %rdi
xorl %r8d, %r8d
xorl %eax, %eax
xorl %ecx, %ecx
movq (%rsi,%r8,8), %r9
shrq $0x21, %r9
movzbl (%rdi,%r9), %r9d
testl %r9d, %r9d
je 0xd4826
cmpl $0x5, %r9d
je 0xd4826
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpb %r14b, %r9b
setne %r10b
sete %r11b
addl %r11d, %ecx
addl %r10d, %eax
incq %r8
cmpq %r8, %rdx
jne 0xd47f7
cmpl 0x128(%rsp), %eax
jbe 0xd488d
movl %eax, %edx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
ucomiss %xmm0, %xmm2
jae 0xd488d
addl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd488d
movq %r12, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xced5a
testb %al, %al
je 0xd48a7
movq 0x8(%rsp), %r12
movss 0x18(%rsp), %xmm2
jmp 0xd4892
movq 0x8(%rsp), %r12
incl %ebp
cmpq %rbp, 0xb8(%rsp)
ja 0xd47b2
jmp 0xd49a9
cmpl $0x0, 0xc(%r12)
je 0xd48d3
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%r12)
movl $0x0, (%r12)
movq 0x18(%r12), %rdi
callq 0x2220
movq $0x0, 0x18(%r12)
movq 0xc8(%rsp), %rax
movl (%rax,%rbp,4), %eax
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
xorl %ecx, %ecx
movq 0x8(%rsp), %r12
movss 0x18(%rsp), %xmm2
leaq (%rcx,%rax), %rdx
movq 0x28(%rbx), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r13, %rsi
je 0xd498b
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%rbx), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
orb $-0x80, 0xf(%rdi,%r9,8)
movl 0x8(%rdi,%r9,8), %r9d
xorq $0x1, %r9
movq 0x28(%rbx), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r13, %r9
je 0xd4983
movq 0x8(%rbx), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %r15d
xorl %edx, %r15d
cmpl $0x1, %r15d
jne 0xd497a
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xd4966
incq %r8
cmpq %rsi, %r8
jne 0xd4924
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xd48fd
incl 0x10(%rsp)
jmp 0xd4892
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %rdx
incq %rdx
cmpq 0x58(%rsp), %rdx
jne 0xd4553
cmpl $0x0, 0x10(%rsp)
jne 0xd4535
movq 0xc8(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa02fa
movq 0x30(%rsp), %rdi
callq 0x2220
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq