// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: set_trio_flag_by_cov(ma_ug_t*, asg_t*, hap_cov_t*)
void set_trio_flag_by_cov(ma_ug_t *ug, asg_t *read_g, hap_cov_t *cov)
{
    kvec_t(uint64_t) idx; kv_init(idx);
    uint32_t i, k, j, qn, tn, s[2], flag, n, found = 0;
    uint64_t offset, r_beg, r_end, ovlp;
    ma_utg_t *u = NULL, *w = NULL;
    u_trans_t *a = NULL;
    kv_u_trans_t *ta = &(cov->t_ch->k_trans);

    for (i = 0, idx.n = 0; i < ta->idx.n; i++)
    {
        qn = i;
        u = &(ug->u.a[qn]);
        for (k = 0; k < u->n; k++)
        {
            if((R_INF.trio_flag[u->a[k]>>33]&SET_TRIO)==0) break;
        }

        if(k >= u->n) continue; ///whole unitig is primary

        a = u_trans_a(*ta, qn);
        n = u_trans_n(*ta, qn);
        for (k = 0, s[0] = s[1] = 0; k < n; k++)
        {
            tn = a[k].tn;
            w = &(ug->u.a[tn]);
            for (j = found = 0, offset = 0; j < w->n; j++)
            {

                r_beg = offset; r_end = offset + read_g->seq[w->a[j]>>33].len;
                offset += (uint32_t)w->a[j];
                ovlp = ((MIN(r_end, a[k].te) > MAX(r_beg, a[k].ts))?
                                        MIN(r_end, a[k].te) - MAX(r_beg, a[k].ts):0);
                if(found == 1 && ovlp == 0) break;
                if(ovlp == 0) continue;
                found = 1;
                if(ovlp <= (read_g->seq[w->a[j]>>33].len)*0.8) continue;

                if((R_INF.trio_flag[w->a[j]>>33]&FATHER)||(R_INF.trio_flag[w->a[j]>>33]&MOTHER))
                {
                    s[0]++;
                }
                
                if(R_INF.trio_flag[w->a[j]>>33]&SET_TRIO)
                {
                    s[1]++;
                }
            }
        }

        if(s[1] > 0)
        {
            kv_push(uint64_t, idx, (uint64_t)((uint32_t)-1 - s[0]) << 32 | (qn));
        }
    }

    for (i = 0; i < idx.n; i++)
    {
        qn = (uint32_t)idx.a[i];
        u = &(ug->u.a[qn]);
        a = u_trans_a(*ta, qn);
        n = u_trans_n(*ta, qn);
        for (k = 0, s[0] = s[1] = 0; k < n; k++)
        {
            tn = a[k].tn;
            w = &(ug->u.a[tn]);
            for (j = found = 0, offset = 0; j < w->n; j++)
            {

                r_beg = offset; r_end = offset + read_g->seq[w->a[j]>>33].len;
                offset += (uint32_t)w->a[j];
                ovlp = ((MIN(r_end, a[k].te) > MAX(r_beg, a[k].ts))?
                                        MIN(r_end, a[k].te) - MAX(r_beg, a[k].ts):0);
                if(found == 1 && ovlp == 0) break;
                if(ovlp == 0) continue;
                found = 1;
                if(ovlp <= (read_g->seq[w->a[j]>>33].len)*0.8) continue;

                if(R_INF.trio_flag[w->a[j]>>33]&FATHER)
                {
                    s[0]++;
                }

                if(R_INF.trio_flag[w->a[j]>>33]&MOTHER)
                {
                    s[1]++;
                }
            }
        }

        if(s[0] >= s[1])
        {
            flag = MOTHER;
        }
        else
        {
            flag = FATHER;
        }
        for (k = 0; k < u->n; k++)
        {
            if(R_INF.trio_flag[u->a[k]>>33]&SET_TRIO) continue;
            if(cov->t_ch->ir_het[u->a[k]>>33] == N_HET) continue;
            R_INF.trio_flag[u->a[k]>>33] |= flag;
        }
    }


    for (i = 0, idx.n = 0; i < ta->idx.n; i++)
    {
        qn = i;
        u = &(ug->u.a[qn]);
        for (k = 0; k < u->n; k++)
        {
            if(R_INF.trio_flag[u->a[k]>>33]&FATHER)
            {
                R_INF.trio_flag[u->a[k]>>33] = FATHER;
            }
            else if(R_INF.trio_flag[u->a[k]>>33]&MOTHER)
            {
                R_INF.trio_flag[u->a[k]>>33] = MOTHER;
            }
            else
            {
                R_INF.trio_flag[u->a[k]>>33] = AMBIGU;
            }
        }
    }
    kv_destroy(idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, %r14
movq 0xa8(%rdx), %r15
xorl %r12d, %r12d
cmpq $0x0, 0x150(%r15)
movq %rdi, 0x60(%rsp)
movq %r15, 0x20(%rsp)
je 0xca1a7
leaq 0x1c130a(%rip), %r13    # 0x28b230
movsd 0x15b582(%rip), %xmm2   # 0x2254b0
xorl %edi, %edi
movq $0x0, 0x10(%rsp)
xorl %r10d, %r10d
movq 0x10(%r14), %rbx
leaq (%r12,%r12,4), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testl %eax, %eax
setne %sil
je 0xca195
leaq (%rbx,%rcx,8), %rdx
movq 0x28(%r13), %rcx
movq 0x18(%rdx), %rdx
movq (%rdx), %r8
shrq $0x21, %r8
testb $0x8, (%rcx,%r8)
je 0xc9f97
leal -0x1(%rax), %esi
xorl %r9d, %r9d
cmpl %r9d, %esi
je 0xca195
movq 0x8(%rdx,%r9,8), %r8
shrq $0x21, %r8
incq %r9
testb $0x8, (%rcx,%r8)
jne 0xc9f74
cmpl %eax, %r9d
setb %sil
testb %sil, %sil
je 0xca195
movq 0x160(%r15), %rax
movq (%rax,%r12,8), %rax
testl %eax, %eax
je 0xca13f
movq %r10, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rdi, 0x18(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
addq 0x148(%r15), %rdx
movq 0x28(%r13), %rcx
movq %rcx, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movq %rbx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rax, 0x50(%rsp)
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
movl 0x14(%rdx,%rcx), %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rbx,%rax,8), %r11d
testq %r11, %r11
je 0xca0ed
addq %rdx, %rcx
leaq (%rbx,%rax,8), %rax
movq 0x68(%rsp), %rdx
movq 0x20(%rdx), %rbx
movq 0x18(%rax), %rax
movl 0xc(%rcx), %r13d
movl 0x10(%rcx), %r14d
xorl %edi, %edi
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%rax,%rdi,8), %rcx
movq %rcx, %r8
shrq $0x21, %r8
movl (%rbx,%r8,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq (%r15,%rdx), %rsi
cmpq %r14, %rsi
cmovaeq %r14, %rsi
cmpq %r13, %r15
movq %r13, %r12
cmovaq %r15, %r12
movq %rsi, %r9
subq %r12, %r9
movl $0x0, %r10d
cmovaeq %r9, %r10
cmpl $0x1, %ebp
jne 0xca08f
cmpq %r12, %rsi
jbe 0xca0ed
cmpq %r12, %rsi
jbe 0xca0dc
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm1
movl $0x1, %ebp
ucomisd %xmm0, %xmm1
jae 0xca0dc
movq 0x58(%rsp), %rdx
movzbl (%rdx,%r8), %edx
movl %edx, %esi
andb $0x3, %sil
cmpb $0x1, %sil
movq 0x8(%rsp), %rsi
sbbl $-0x1, %esi
movq %rsi, 0x8(%rsp)
btl $0x3, %edx
adcl $0x0, 0x4(%rsp)
movl %ecx, %ecx
addq %rcx, %r15
incq %rdi
cmpl %edi, %r11d
jne 0xca04b
movq 0x50(%rsp), %rax
incq %rax
cmpq 0x38(%rsp), %rax
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %rdx
jne 0xca005
cmpl $0x0, 0x4(%rsp)
sete %al
movq 0x8(%rsp), %rbp
notl %ebp
shlq $0x20, %rbp
movq 0x60(%rsp), %r14
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %r12
leaq 0x1c10f8(%rip), %r13    # 0x28b230
movq 0x28(%rsp), %r10
jmp 0xca14b
movb $0x1, %al
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
testb %al, %al
jne 0xca195
cmpq %r10, 0x10(%rsp)
jne 0xca181
leaq (%r10,%r10), %rbx
testq %r10, %r10
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
callq 0x2370
movsd 0x15b335(%rip), %xmm2   # 0x2254b0
movq %rax, %rdi
movq %rbx, %r10
orq %r12, %rbp
movq 0x10(%rsp), %rax
movq %rbp, (%rdi,%rax,8)
incq %rax
movq %rax, 0x10(%rsp)
incl %r12d
cmpq %r12, 0x150(%r15)
ja 0xc9f3c
jmp 0xca1b2
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
cmpq $0x0, 0x10(%rsp)
je 0xca3d9
xorl %ecx, %ecx
movsd 0x15b2e8(%rip), %xmm0   # 0x2254b0
movq %rdi, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
movl (%rdi,%rcx,8), %ecx
movq 0x10(%r14), %rdx
movq 0x160(%r15), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rax
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
testl %eax, %eax
je 0xca349
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
shlq $0x4, %rsi
movq 0x20(%rsp), %rcx
addq 0x148(%rcx), %rsi
leaq 0x1c100d(%rip), %rcx    # 0x28b230
movq 0x28(%rcx), %rcx
movq %rcx, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rax, 0x50(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl 0x14(%rsi,%rax), %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rdx,%rcx,8), %ebx
testq %rbx, %rbx
je 0xca32c
addq %rsi, %rax
leaq (%rdx,%rcx,8), %rcx
movq 0x68(%rsp), %rdx
movq 0x20(%rdx), %r12
movq 0x18(%rcx), %r14
movl 0xc(%rax), %r9d
movl 0x10(%rax), %edi
xorl %r15d, %r15d
xorl %eax, %eax
xorl %ecx, %ecx
movq (%r14,%r15,8), %rsi
movq %rsi, %r13
shrq $0x21, %r13
movl (%r12,%r13,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
leaq (%rax,%r8), %r11
cmpq %rdi, %r11
cmovaeq %rdi, %r11
cmpq %r9, %rax
movq %r9, %rdx
cmovaq %rax, %rdx
movq %r11, %rbp
subq %rdx, %rbp
movl $0x0, %r10d
cmovaeq %rbp, %r10
cmpl $0x1, %ecx
jne 0xca2d8
cmpq %rdx, %r11
jbe 0xca32c
cmpq %rdx, %r11
jbe 0xca31b
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
mulsd %xmm0, %xmm2
movl $0x1, %ecx
ucomisd %xmm1, %xmm2
jae 0xca31b
movq 0x58(%rsp), %rdx
movzbl (%rdx,%r13), %edx
movl %edx, %r8d
andl $0x1, %r8d
addl %r8d, 0x8(%rsp)
btl $0x1, %edx
adcl $0x0, 0x4(%rsp)
movl %esi, %edx
addq %rdx, %rax
incq %r15
cmpl %r15d, %ebx
jne 0xca293
movq 0x50(%rsp), %rax
incq %rax
cmpq 0x38(%rsp), %rax
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rsi
jne 0xca24f
movq 0x28(%rsp), %rax
leaq (%rax,%rax,4), %rax
leaq (%rdx,%rax,8), %rax
movl 0x4(%rsp), %ecx
cmpl %ecx, 0x8(%rsp)
movb $0x2, %cl
sbbb $0x0, %cl
cmpl $0x0, 0x10(%rax)
movq 0x60(%rsp), %r14
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %rdi
movq 0x70(%rsp), %r11
leaq 0x1c0eae(%rip), %rbx    # 0x28b230
je 0xca3c4
xorl %edx, %edx
movq 0x28(%rbx), %rsi
movq 0x18(%rax), %r8
movq (%r8,%rdx,8), %r10
shrq $0x21, %r10
movb (%rsi,%r10), %r8b
testb $0x8, %r8b
jne 0xca3b9
movq 0xa8(%r11), %r9
movq 0x10(%r9), %r9
cmpb $0x0, (%r9,%r10)
je 0xca3b9
orb %cl, %r8b
movb %r8b, (%rsi,%r10)
incq %rdx
movl 0x10(%rax), %esi
cmpq %rsi, %rdx
jb 0xca386
movq 0x30(%rsp), %rcx
incq %rcx
movl %ecx, %eax
cmpq %rax, 0x10(%rsp)
ja 0xca1cd
cmpq $0x0, 0x150(%r15)
je 0xca449
xorl %eax, %eax
leaq 0x1c0e44(%rip), %rcx    # 0x28b230
movl $0x1, %edx
movq 0x10(%r14), %rsi
leaq (%rax,%rax,4), %r8
cmpl $0x0, 0x10(%rsi,%r8,8)
je 0xca43e
leaq (%rsi,%r8,8), %rsi
xorl %ebx, %ebx
movq 0x28(%rcx), %r8
movq 0x18(%rsi), %r9
movq (%r9,%rbx,8), %r9
shrq $0x21, %r9
movb (%r8,%r9), %r10b
movl %r10d, %r11d
andb $0x2, %r11b
testb $0x1, %r10b
movzbl %r11b, %r10d
cmovnel %edx, %r10d
movb %r10b, (%r8,%r9)
incq %rbx
movl 0x10(%rsi), %r8d
cmpq %r8, %rbx
jb 0xca407
incl %eax
cmpq %rax, 0x150(%r15)
ja 0xca3f1
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220