// File: /workspace/github_cpp_makefile_star2_O3/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)
movl %esi, %r13d
movq 0x10(%rdi), %r11
movl %esi, %esi
leaq (%rsi,%rsi,4), %rax
cmpl $0x0, 0xc(%r11,%rax,8)
je 0xcba33
movq 0x18(%rdi), %r10
movq 0x20(%r10), %r10
cmpl $0x0, (%r10,%rsi,8)
js 0xcba33
movq %r8, -0x10(%rsp)
movq 0x38(%rsp), %r10
leaq (%r11,%rax,8), %r8
cmpl $0x0, 0x10(%r8)
je 0xcb7a4
xorl %r11d, %r11d
movq 0x18(%r8), %rsi
movq (%rsi,%r11,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r10,%rsi)
incq %r11
movl 0x10(%r8), %esi
cmpq %rsi, %r11
jb 0xcb787
addl %r13d, %r13d
movb $0x1, %r8b
xorl %r11d, %r11d
movq 0x18(%rdi), %rbx
shlq $0x3, %r11
addq 0x28(%rbx), %r11
movq (%r11,%r13,8), %r14
testl %r14d, %r14d
je 0xcb82e
movq %r14, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r11
shlq $0x3, %r11
addq 0x8(%rbx), %r11
movl %r14d, %ebx
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rsi
cmpl $0x0, 0xc(%r11,%rsi,8)
js 0xcb826
leaq (%r11,%rsi,8), %r15
movq 0x10(%rdi), %rsi
movl 0x8(%r15), %r15d
shrl %r15d
leaq (%r15,%r15,4), %r15
cmpl $0x0, 0x10(%rsi,%r15,8)
je 0xcb826
leaq (%rsi,%r15,8), %r15
xorl %r12d, %r12d
movq 0x18(%r15), %rsi
movq (%rsi,%r12,8), %rsi
shrq $0x21, %rsi
movb $0x2, (%r10,%rsi)
incq %r12
movl 0x10(%r15), %esi
cmpq %rsi, %r12
jb 0xcb809
incq %r14
cmpq %rbx, %r14
jne 0xcb7db
movl $0x1, %r11d
testb $0x1, %r8b
movl $0x0, %r8d
jne 0xcb7ad
movq 0x10(%rdi), %rsi
movl 0x10(%rsi,%rax,8), %ecx
movq %rcx, -0x28(%rsp)
testq %rcx, %rcx
je 0xcb992
movq %r13, -0x30(%rsp)
leaq (%rsi,%rax,8), %rax
movq %rax, -0x20(%rsp)
movq 0x18(%rax), %rax
movq %rax, -0x18(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rbp, %rcx
movq -0x18(%rsp), %rax
movq (%rax,%r13,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rbp
movq -0x10(%rsp), %rsi
movl 0xc(%rsi,%rbp,8), %eax
testq %rax, %rax
je 0xcb916
leaq (%rsi,%rbp,8), %r8
movq (%r8), %r14
shlq $0x3, %rax
leaq (%rax,%rax,4), %r8
xorl %eax, %eax
cmpb $0x1, 0x20(%r14,%rax)
jne 0xcb90d
movl 0xc(%r14,%rax), %ebx
movq 0x20(%rdx), %r11
cmpl $0x0, (%r11,%rbx,8)
js 0xcb8e6
movzbl (%r10,%rbx), %r11d
cmpl $0x2, %r11d
je 0xcb901
cmpl $0x1, %r11d
jne 0xcb90d
movl 0x8(%r14,%rax), %esi
subl (%r14,%rax), %esi
addq %rsi, %r15
jmp 0xcb90d
cmpq %rbx, (%r9)
jbe 0xcb90d
movq 0x8(%r9), %rsi
movslq (%rsi,%rbx,4), %rbx
testq %rbx, %rbx
js 0xcb90d
cmpl $0x0, (%r11,%rbx,8)
jns 0xcb8c7
jmp 0xcb90d
movl 0x8(%r14,%rax), %esi
subl (%r14,%rax), %esi
addq %rsi, %r12
addq $0x28, %rax
cmpq %rax, %r8
jne 0xcb8af
movq -0x8(%rsp), %rsi
movl 0x4(%rsi,%rbp,4), %eax
movl (%rsi,%rbp,4), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
subl %esi, %eax
movq %rcx, %rbp
addq %rax, %rbp
incq %r13
cmpq -0x28(%rsp), %r13
jne 0xcb87c
cvtsi2sd %r12, %xmm0
cvtsi2sd %r15, %xmm1
mulsd 0x165c2d(%rip), %xmm1   # 0x231580
xorl %eax, %eax
ucomisd %xmm0, %xmm1
cmovaeq %r12, %rax
addq %r15, %rax
cmpq $0x0, -0x28(%rsp)
je 0xcb998
xorl %ecx, %ecx
movq -0x30(%rsp), %r13
movq -0x20(%rsp), %rsi
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 0xcb974
jmp 0xcb99d
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0xcb99d
movq -0x30(%rsp), %r13
movb $0x1, %cl
xorl %edx, %edx
movq 0x18(%rdi), %r8
shlq $0x3, %rdx
addq 0x28(%r8), %rdx
movq (%rdx,%r13,8), %r9
testl %r9d, %r9d
je 0xcba14
movq %r9, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%r8), %rdx
movl %r9d, %r8d
xorl %r9d, %r9d
movq 0x10(%rdi), %r11
leaq (%r9,%r9,2), %rsi
movl 0x8(%rdx,%rsi,8), %esi
shrl %esi
leaq (%rsi,%rsi,4), %rbx
cmpl $0x0, 0x10(%r11,%rbx,8)
je 0xcba0c
leaq (%r11,%rbx,8), %rbx
xorl %r14d, %r14d
movq 0x18(%rbx), %rsi
movq (%rsi,%r14,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r10,%rsi)
incq %r14
movl 0x10(%rbx), %esi
cmpq %rsi, %r14
jb 0xcb9f0
incq %r9
cmpq %r8, %r9
jne 0xcb9cf
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xcb9a1
testq %rbp, %rbp
je 0xcba33
cqto
idivq %rbp
jmp 0xcba35
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq