// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_utg_cov(ma_ug_t*, unsigned int, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, unsigned char*)
uint64_t get_utg_cov(ma_ug_t *ug, uint32_t uID, asg_t* read_g, 
const ma_sub_t* coverage_cut, ma_hit_t_alloc* sources, R_to_U* ruIndex, uint8_t* r_flag)
{
    ma_utg_t *u = &(ug->u.a[uID]);
    uint32_t k, j, rId, tn, is_Unitig;
    long long R_bases = 0, C_bases = 0;
    long long cov_in, cov_out;
    uint32_t nv, i;
    asg_arc_t *av = NULL;
    ma_hit_t *h;
    if(u->m == 0 || ug->g->seq[uID].del) return 0;
    ///set
    for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 1;
    for (i = 0; i < 2; i++)
    {
        nv = asg_arc_n(ug->g, (uID<<1)+i);
        av = asg_arc_a(ug->g, (uID<<1)+i);
        for (j = 0; j < nv; j++)
        {
            if(av[j].del) continue;
            u = &(ug->u.a[av[j].v>>1]);
            for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 2;
        }
    }

    u = &(ug->u.a[uID]);
	cov_in = cov_out = R_bases = 0; 
    for (k = 0; k < u->n; k++)
    {
        rId = u->a[k]>>33;
		R_bases += (coverage_cut[rId].e - coverage_cut[rId].s);
        for (j = 0; j < (uint64_t)(sources[rId].length); j++)
        {
            h = &(sources[rId].buffer[j]);
            ///if(h->del) continue;
            if(h->el != 1) continue;
            tn = Get_tn((*h));
            if(read_g->seq[tn].del == 1)
            {
                ///get the id of read that contains it 
                get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
            }
            if(read_g->seq[tn].del == 1) continue;
            if(r_flag[tn] == 0) continue;
            if(r_flag[tn] == 1) cov_in += (Get_qe((*h)) - Get_qs((*h)));
            if(r_flag[tn] == 2) cov_out += (Get_qe((*h)) - Get_qs((*h)));
        }
    }
	C_bases = cov_in;
    if(cov_out <= (cov_in*0.2)) C_bases += cov_out;

    ///reset
    for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 0;

    for (i = 0; i < 2; i++)
    {
        nv = asg_arc_n(ug->g, (uID<<1)+i);
        av = asg_arc_a(ug->g, (uID<<1)+i);
        for (j = 0; j < nv; j++)
        {
            u = &(ug->u.a[av[j].v>>1]);
            for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 0;
        }
    }

	return R_bases == 0? 0 : C_bases/R_bases;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, -0x8(%rsp)
movq 0x10(%rdi), %rcx
movl %esi, %r10d
leaq (%r10,%r10,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xca78f
movq 0x18(%rdi), %r11
movq 0x20(%r11), %r11
cmpl $0x0, (%r11,%r10,8)
js 0xca78f
movq %r8, -0x10(%rsp)
movq 0x38(%rsp), %r10
leaq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x10(%rcx)
je 0xca4c8
xorl %r11d, %r11d
movq 0x18(%rcx), %r8
movq (%r8,%r11,8), %r8
shrq $0x21, %r8
movb $0x1, (%r10,%r8)
incq %r11
movl 0x10(%rcx), %r8d
cmpq %r8, %r11
jb 0xca4ab
addl %esi, %esi
xorl %r8d, %r8d
movq 0x18(%rdi), %rbx
leaq (,%r8,8), %rcx
addq 0x28(%rbx), %rcx
movq (%rcx,%rsi,8), %r11
testl %r11d, %r11d
je 0xca551
movq %r11, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%rbx), %rcx
movl %r11d, %r11d
xorl %r12d, %r12d
leaq (%r12,%r12,2), %rbx
cmpl $0x0, 0xc(%rcx,%rbx,8)
js 0xca549
leaq (%rcx,%rbx,8), %r14
movq 0x10(%rdi), %rbx
movl 0x8(%r14), %r14d
shrl %r14d
leaq (%r14,%r14,4), %r14
cmpl $0x0, 0x10(%rbx,%r14,8)
je 0xca549
leaq (%rbx,%r14,8), %rbx
xorl %r14d, %r14d
movq 0x18(%rbx), %r15
movq (%r15,%r14,8), %r15
shrq $0x21, %r15
movb $0x2, (%r10,%r15)
incq %r14
movl 0x10(%rbx), %r15d
cmpq %r15, %r14
jb 0xca52c
incq %r12
cmpq %r11, %r12
jne 0xca4ff
leaq 0x1(%r8), %rcx
testq %r8, %r8
movq %rcx, %r8
je 0xca4cd
movq 0x10(%rdi), %rcx
leaq (%rcx,%rax,8), %rax
movl 0x10(%rax), %ecx
movq %rcx, -0x18(%rsp)
testq %rcx, %rcx
movq %rsi, -0x28(%rsp)
movq %rax, -0x30(%rsp)
je 0xca699
movq 0x18(%rax), %rax
movq %rax, -0x20(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq $0x0, -0x38(%rsp)
xorl %r11d, %r11d
movq -0x20(%rsp), %rax
movq (%rax,%r13,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rbp
movq -0x10(%rsp), %rcx
movl 0xc(%rcx,%rbp,8), %eax
testq %rax, %rax
je 0xca671
leaq (%rcx,%rbp,8), %rcx
movq (%rcx), %r12
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
xorl %ebx, %ebx
cmpb $0x1, 0x20(%r12,%rbx)
jne 0xca664
movl 0xc(%r12,%rbx), %esi
movq 0x20(%rdx), %r8
cmpl $0x0, (%r8,%rsi,8)
movl %esi, %ecx
js 0xca629
movl %ecx, %ecx
cmpl $0x0, (%r8,%rcx,8)
js 0xca664
movzbl (%r10,%rcx), %ecx
testl %ecx, %ecx
je 0xca664
cmpl $0x1, %ecx
jne 0xca616
movl 0x8(%r12,%rbx), %esi
subl (%r12,%rbx), %esi
addq %rsi, -0x38(%rsp)
cmpb $0x2, %cl
jne 0xca664
movl 0x8(%r12,%rbx), %ecx
subl (%r12,%rbx), %ecx
addq %rcx, %r15
jmp 0xca664
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rsi, (%r9)
jbe 0xca64d
movq 0x8(%r9), %r14
movl (%r14,%rsi,4), %esi
cmpl $-0x1, %esi
je 0xca64d
movl %esi, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shrl $0x1f, %esi
jmp 0xca64f
xorl %esi, %esi
cmpl $-0x1, %ecx
sete %r14b
orb %sil, %r14b
jne 0xca664
movl %ecx, %esi
cmpl $0x0, (%r8,%rsi,8)
jns 0xca5f1
addq $0x28, %rbx
cmpq %rbx, %rax
jne 0xca5d3
movq -0x8(%rsp), %rcx
movl 0x4(%rcx,%rbp,4), %eax
movl (%rcx,%rbp,4), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
subl %ecx, %eax
addq %rax, %r11
incq %r13
cmpq -0x18(%rsp), %r13
jne 0xca59f
jmp 0xca6a8
xorl %r11d, %r11d
movq $0x0, -0x38(%rsp)
xorl %r15d, %r15d
cvtsi2sd %r15, %xmm0
movq -0x38(%rsp), %r8
cvtsi2sd %r8, %xmm1
mulsd 0x15adc1(%rip), %xmm1   # 0x225480
xorl %eax, %eax
ucomisd %xmm0, %xmm1
cmovaeq %r15, %rax
movq -0x30(%rsp), %rsi
cmpl $0x0, 0x10(%rsi)
movq -0x28(%rsp), %r15
je 0xca6f7
xorl %ecx, %ecx
movq 0x18(%rsi), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r10,%rdx)
incq %rcx
movl 0x10(%rsi), %edx
cmpq %rdx, %rcx
jb 0xca6db
addq %r8, %rax
xorl %ecx, %ecx
movq 0x18(%rdi), %rsi
leaq (,%rcx,8), %rdx
addq 0x28(%rsi), %rdx
movq (%rdx,%r15,8), %r8
testl %r8d, %r8d
je 0xca773
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%rsi), %rdx
movl %r8d, %esi
xorl %r8d, %r8d
movq 0x10(%rdi), %r9
leaq (%r8,%r8,2), %rbx
movl 0x8(%rdx,%rbx,8), %ebx
shrl %ebx
leaq (%rbx,%rbx,4), %rbx
cmpl $0x0, 0x10(%r9,%rbx,8)
je 0xca76b
leaq (%r9,%rbx,8), %r9
xorl %ebx, %ebx
movq 0x18(%r9), %r14
movq (%r14,%rbx,8), %r14
shrq $0x21, %r14
movb $0x0, (%r10,%r14)
incq %rbx
movl 0x10(%r9), %r14d
cmpq %r14, %rbx
jb 0xca74e
incq %r8
cmpq %rsi, %r8
jne 0xca72e
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xca6fc
testq %r11, %r11
je 0xca78f
cqto
idivq %r11
jmp 0xca791
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq