// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, 0x58(%rsp)
movq %rdi, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
movq 0xa8(%rdx), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x1c4a47(%rip), %r11    # 0x254230
movsd 0x153d0f(%rip), %xmm2   # 0x1e3500
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
movq $0x0, (%rsp)
movl %edi, %edi
movq 0x20(%rsp), %rax
cmpq %rdi, 0x150(%rax)
jbe 0x8fa0b
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdx
imulq $0x28, %rdi, %rcx
leaq (%rdx,%rcx), %rax
movq %rdx, 0x48(%rsp)
movl 0x10(%rdx,%rcx), %ecx
movq 0x28(%r11), %rdx
movq %rdx, 0x28(%rsp)
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x8fa04
movq 0x18(%rax), %rdx
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
incq %rsi
movq 0x28(%rsp), %r8
testb $0x8, (%r8,%rdx)
jne 0x8f83d
movq 0x20(%rsp), %rdx
movq 0x160(%rdx), %rax
movq %rdi, 0x8(%rsp)
movq (%rax,%rdi,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %rcx
addq 0x148(%rdx), %rcx
movq %rcx, 0x38(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
xorl %r10d, %r10d
xorl %r15d, %r15d
cmpq 0x40(%rsp), %rax
je 0x8f98f
movq %rax, 0x50(%rsp)
imulq $0x30, %rax, %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax), %r11
movl 0x14(%rcx,%rax), %eax
imulq $0x28, %rax, %rcx
movq 0x48(%rsp), %rdx
leaq (%rdx,%rcx), %rax
movl 0x10(%rdx,%rcx), %r14d
xorl %edi, %edi
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpq %rdi, %r14
je 0x8f97b
movq 0x58(%rsp), %rcx
movq 0x20(%rcx), %rdx
movq 0x18(%rax), %rcx
movq (%rcx,%rdi,8), %rcx
movq %rcx, %r8
shrq $0x21, %r8
movl (%rdx,%r8,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq (%rbx,%rsi), %rdx
movl 0xc(%r11), %r13d
movl 0x10(%r11), %r9d
cmpq %r9, %rdx
cmovbq %rdx, %r9
cmpq %r13, %rbx
cmovaq %rbx, %r13
movq %r9, %r12
subq %r13, %r12
movl $0x0, %edx
cmovaeq %r12, %rdx
cmpl $0x1, %ebp
jne 0x8f933
cmpq %r13, %r9
jbe 0x8f97b
cmpq %r13, %r9
jbe 0x8f96e
cvtsi2sd %rdx, %xmm0
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jae 0x8f96b
movq 0x28(%rsp), %rdx
movzbl (%rdx,%r8), %edx
movl %edx, %esi
andb $0x3, %sil
cmpb $0x1, %sil
sbbl $-0x1, %r15d
btl $0x3, %edx
adcl $0x0, %r10d
pushq $0x1
popq %rbp
movl %ecx, %ecx
addq %rcx, %rbx
incq %rdi
jmp 0x8f8d5
movq 0x50(%rsp), %rax
incq %rax
leaq 0x1c48a6(%rip), %r11    # 0x254230
jmp 0x8f89c
testl %r10d, %r10d
je 0x8f9d5
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0x8f9dc
leaq (%rax,%rax), %rbx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq (%rsp), %rdi
callq 0x2370
movsd 0x153b39(%rip), %xmm2   # 0x1e3500
leaq 0x1c4862(%rip), %r11    # 0x254230
movq %rbx, 0x18(%rsp)
jmp 0x8f9e0
movq 0x8(%rsp), %rdi
jmp 0x8fa04
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
notl %r15d
shlq $0x20, %r15
orq %rdi, %r15
movq %rax, (%rsp)
movq 0x10(%rsp), %rcx
movq %r15, (%rax,%rcx,8)
incq %rcx
movq %rcx, 0x10(%rsp)
incl %edi
jmp 0x8f804
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, 0x10(%rsp)
movq (%rsp), %rax
jbe 0x8fbc8
movq %rcx, 0x8(%rsp)
movl (%rax,%rcx,8), %edx
movq 0x30(%rsp), %rax
movq 0x10(%rax), %r9
movq 0x20(%rsp), %rcx
movq 0x160(%rcx), %rax
movq %rdx, 0x18(%rsp)
movq (%rax,%rdx,8), %rax
movl %eax, %edx
movq %rdx, 0x48(%rsp)
shrq $0x20, %rax
imulq $0x30, %rax, %rax
addq 0x148(%rcx), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%r11), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
xorl %r11d, %r11d
xorl %ebx, %ebx
movq %r9, 0x40(%rsp)
cmpq 0x48(%rsp), %rax
je 0x8fb5d
movq %rax, 0x50(%rsp)
imulq $0x30, %rax, %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movl 0x14(%rcx,%rax), %eax
imulq $0x28, %rax, %rax
leaq (%r9,%rax), %r14
movl 0x10(%r9,%rax), %esi
xorl %edi, %edi
xorl %r13d, %r13d
xorl %eax, %eax
cmpq %rdi, %rsi
je 0x8fb4b
movq 0x58(%rsp), %rcx
movq 0x20(%rcx), %rcx
movq 0x18(%r14), %rdx
movq (%rdx,%rdi,8), %r9
movq %r9, %r8
shrq $0x21, %r8
movl (%rcx,%r8,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rax,%rcx), %r12
movl 0xc(%rbp), %edx
movl 0x10(%rbp), %r10d
cmpq %r10, %r12
cmovbq %r12, %r10
cmpq %rdx, %rax
cmovaq %rax, %rdx
movq %r10, %r15
subq %rdx, %r15
movl $0x0, %r12d
cmovaeq %r15, %r12
cmpl $0x1, %r13d
jne 0x8fb08
cmpq %rdx, %r10
jbe 0x8fb4b
cmpq %rdx, %r10
jbe 0x8fb3d
cvtsi2sd %r12, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jae 0x8fb39
movq 0x28(%rsp), %rcx
movzbl (%rcx,%r8), %ecx
movl %ecx, %edx
andl $0x1, %edx
addl %edx, %ebx
btl $0x1, %ecx
adcl $0x0, %r11d
pushq $0x1
popq %r13
movl %r9d, %ecx
addq %rcx, %rax
incq %rdi
jmp 0x8faa9
movq 0x50(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r9
jmp 0x8fa74
imulq $0x28, 0x18(%rsp), %rax
addq %rax, %r9
cmpl %r11d, %ebx
movb $0x2, %al
sbbb $0x0, %al
xorl %ecx, %ecx
leaq 0x1c46ba(%rip), %r11    # 0x254230
movl 0x10(%r9), %edx
cmpq %rdx, %rcx
jae 0x8fbbc
movq 0x28(%r11), %rdx
movq 0x18(%r9), %rsi
movq (%rsi,%rcx,8), %rsi
shrq $0x21, %rsi
movb (%rdx,%rsi), %dil
testb $0x8, %dil
jne 0x8fbb7
movq 0x60(%rsp), %r8
movq 0xa8(%r8), %r8
movq 0x10(%r8), %r8
cmpb $0x0, (%r8,%rsi)
je 0x8fbb7
orb %al, %dil
movb %dil, (%rdx,%rsi)
incq %rcx
jmp 0x8fb76
movq 0x8(%rsp), %rcx
incl %ecx
jmp 0x8fa0d
xorl %eax, %eax
pushq $0x1
popq %rcx
movl %eax, %eax
movq 0x20(%rsp), %rdx
cmpq %rax, 0x150(%rdx)
jbe 0x8fc28
imulq $0x28, %rax, %rdx
movq 0x30(%rsp), %rsi
addq 0x10(%rsi), %rdx
xorl %esi, %esi
movl 0x10(%rdx), %edi
cmpq %rdi, %rsi
jae 0x8fc24
movq 0x28(%r11), %rdi
movq 0x18(%rdx), %r8
movq (%r8,%rsi,8), %r8
shrq $0x21, %r8
movb (%rdi,%r8), %r9b
movl %r9d, %r10d
andb $0x2, %r10b
testb $0x1, %r9b
movzbl %r10b, %r9d
cmovnel %ecx, %r9d
movb %r9b, (%rdi,%r8)
incq %rsi
jmp 0x8fbec
incl %eax
jmp 0x8fbcd
movq (%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220