// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rsi, 0x78(%rsp)
movq 0xa8(%rdx), %rbx
movq 0x150(%rbx), %r8
xorl %r9d, %r9d
testq %r8, %r8
je 0xcb718
leaq 0x1cb03f(%rip), %r14    # 0x296230
movsd 0x1663b7(%rip), %xmm2   # 0x2315b0
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, 0x70(%rsp)
movq %rdi, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %rdx, 0x80(%rsp)
movq 0x10(%rdi), %r10
leaq (%r9,%r9,4), %rcx
movl 0x10(%r10,%rcx,8), %eax
testq %rax, %rax
je 0xcb46c
leaq (%r10,%rcx,8), %rcx
movq 0x28(%r14), %rdx
movq %rdx, 0x10(%rsp)
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movq 0x10(%rsp), %r11
testb $0x8, (%r11,%rsi)
je 0xcb264
incq %rdx
cmpq %rdx, %rax
jne 0xcb243
jmp 0xcb46c
movq 0x160(%rbx), %rax
movq (%rax,%r9,8), %rax
testl %eax, %eax
je 0xcb46c
movq %r9, 0x18(%rsp)
movq %r8, 0x48(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
addq 0x148(%rbx), %rdx
movl %eax, %eax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movl $0x0, 0x40(%rsp)
movq %r10, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rax, 0x68(%rsp)
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
movl 0x14(%rdx,%rcx), %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%r10,%rax,8), %r11d
testq %r11, %r11
je 0xcb3ae
addq %rdx, %rcx
leaq (%r10,%rax,8), %rax
movq 0x78(%rsp), %rdx
movq 0x20(%rdx), %rbx
movq 0x18(%rax), %rax
movl 0xc(%rcx), %r12d
movl 0x10(%rcx), %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%rax,%r13,8), %rcx
movq %rcx, %r8
shrq $0x21, %r8
movl (%rbx,%r8,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq (%r15,%rsi), %rdx
cmpq %r14, %rdx
cmovaeq %r14, %rdx
cmpq %r12, %r15
movq %r12, %rdi
cmovaq %r15, %rdi
movq %rdx, %r9
subq %rdi, %r9
movl $0x0, %r10d
cmovaeq %r9, %r10
cmpl $0x1, %ebp
jne 0xcb350
cmpq %rdi, %rdx
jbe 0xcb3ae
cmpq %rdi, %rdx
jbe 0xcb39d
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
movl $0x1, %ebp
ucomisd %xmm0, %xmm1
jae 0xcb39d
movq 0x10(%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, 0x40(%rsp)
movl %ecx, %ecx
addq %rcx, %r15
incq %r13
cmpq %r13, %r11
jne 0xcb30c
movq 0x68(%rsp), %rax
incq %rax
cmpq 0x50(%rsp), %rax
movq 0x60(%rsp), %r10
movq 0x58(%rsp), %rdx
jne 0xcb2c5
cmpl $0x0, 0x40(%rsp)
je 0xcb447
movq 0x30(%rsp), %r12
cmpq 0x70(%rsp), %r12
movq 0x28(%rsp), %rbx
leaq 0x1cae48(%rip), %r14    # 0x296230
movq 0x38(%rsp), %r15
jne 0xcb41f
leaq (%r12,%r12), %rcx
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x70(%rsp)
leaq (,%rcx,8), %rsi
movq %r15, %rdi
callq 0x2370
movsd 0x166194(%rip), %xmm2   # 0x2315b0
movq %rax, %r15
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %rax
notl %eax
shlq $0x20, %rax
orq %r9, %rax
movq %rax, (%r15,%r12,8)
incq %r12
movq 0x150(%rbx), %r8
jmp 0xcb46c
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rbx
leaq 0x1cadd8(%rip), %r14    # 0x296230
movq 0x38(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x48(%rsp), %r8
movq 0x18(%rsp), %r9
incl %r9d
cmpq %r9, %r8
ja 0xcb21a
testq %r12, %r12
je 0xcb6b0
xorl %ecx, %ecx
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %rcx, 0x18(%rsp)
movl (%r15,%rcx,8), %ecx
movq 0x10(%rdi), %rdx
movq 0x160(%rbx), %rax
movq %rcx, 0x48(%rsp)
movq (%rax,%rcx,8), %rax
movl $0x0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
testl %eax, %eax
je 0xcb609
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
shlq $0x4, %rsi
movq 0x28(%rsp), %rcx
addq 0x148(%rcx), %rsi
leaq 0x1cad4c(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, %eax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rax, 0x68(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl 0x14(%rsi,%rax), %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rdx,%rcx,8), %ebp
testq %rbp, %rbp
je 0xcb5ec
addq %rsi, %rax
leaq (%rdx,%rcx,8), %rcx
movq 0x78(%rsp), %rdx
movq 0x20(%rdx), %rdi
movq 0x18(%rcx), %rbx
movl 0xc(%rax), %r8d
movl 0x10(%rax), %ecx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %eax, %eax
movq (%rbx,%r13,8), %rdx
movq %rdx, %r11
shrq $0x21, %r11
movl (%rdi,%r11,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq (%r15,%rsi), %r10
cmpq %rcx, %r10
cmovaeq %rcx, %r10
cmpq %r8, %r15
movq %r8, %r14
cmovaq %r15, %r14
movq %r10, %r12
subq %r14, %r12
movl $0x0, %r9d
cmovaeq %r12, %r9
cmpl $0x1, %eax
jne 0xcb599
cmpq %r14, %r10
jbe 0xcb5ec
cmpq %r14, %r10
jbe 0xcb5db
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
movl $0x1, %eax
ucomisd %xmm0, %xmm1
jae 0xcb5db
movq 0x40(%rsp), %rsi
movzbl (%rsi,%r11), %esi
movl %esi, %r9d
andl $0x1, %r9d
addl %r9d, 0x8(%rsp)
btl $0x1, %esi
adcl $0x0, 0x10(%rsp)
movl %edx, %edx
addq %rdx, %r15
incq %r13
cmpq %r13, %rbp
jne 0xcb555
movq 0x68(%rsp), %rax
incq %rax
cmpq 0x50(%rsp), %rax
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rsi
jne 0xcb510
movq 0x48(%rsp), %rax
leaq (%rax,%rax,4), %rax
leaq (%rdx,%rax,8), %rax
movl 0x10(%rsp), %ecx
cmpl %ecx, 0x8(%rsp)
movb $0x2, %cl
sbbb $0x0, %cl
movl 0x10(%rax), %edx
testl %edx, %edx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rbx
leaq 0x1cabf7(%rip), %r14    # 0x296230
movq 0x38(%rsp), %r15
movq 0x80(%rsp), %r12
je 0xcb691
movq 0x28(%r14), %rsi
xorl %r11d, %r11d
movq 0x18(%rax), %r8
movq (%r8,%r11,8), %r8
shrq $0x21, %r8
movb (%rsi,%r8), %r9b
testb $0x8, %r9b
jne 0xcb686
movq 0xa8(%r12), %r10
movq 0x10(%r10), %r10
cmpb $0x0, (%r10,%r8)
je 0xcb686
orb %cl, %r9b
movb %r9b, (%rsi,%r8)
movq 0x28(%r14), %rsi
movl 0x10(%rax), %edx
incq %r11
movl %edx, %r8d
cmpq %r8, %r11
jb 0xcb64f
movq 0x18(%rsp), %rcx
incq %rcx
movl %ecx, %eax
movq 0x30(%rsp), %r12
cmpq %rax, %r12
ja 0xcb483
movq 0x150(%rbx), %r8
testq %r8, %r8
je 0xcb71b
xorl %eax, %eax
movl $0x1, %ecx
movq 0x10(%rdi), %rdx
leaq (%rax,%rax,4), %rsi
cmpl $0x0, 0x10(%rdx,%rsi,8)
je 0xcb70f
leaq (%rdx,%rsi,8), %rdx
xorl %esi, %esi
movq 0x28(%r14), %r11
movq 0x18(%rdx), %r8
movq (%r8,%rsi,8), %r8
shrq $0x21, %r8
movb (%r11,%r8), %r9b
movl %r9d, %r10d
andb $0x2, %r10b
testb $0x1, %r9b
movzbl %r10b, %r9d
cmovnel %ecx, %r9d
movb %r9b, (%r11,%r8)
incq %rsi
movl 0x10(%rdx), %r8d
cmpq %r8, %rsi
jb 0xcb6d1
movq 0x150(%rbx), %r8
incl %eax
cmpq %rax, %r8
ja 0xcb6bc
jmp 0xcb71b
xorl %r15d, %r15d
movq %r15, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220