// File: /workspace/github_cpp_makefile_star2_O2/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 %r8, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
movl %esi, %r13d
movq 0x10(%rdi), %rsi
movl %r13d, %r8d
imulq $0x28, %r8, %rax
cmpl $0x0, 0xc(%rsi,%rax)
je 0x8fee2
movq 0x18(%rdi), %r10
movq 0x20(%r10), %r10
cmpl $0x0, (%r10,%r8,8)
js 0x8fee2
movq 0x38(%rsp), %r10
addq %rax, %rsi
xorl %r8d, %r8d
movl 0x10(%rsi), %r11d
cmpq %r11, %r8
jae 0x8fca9
movq 0x18(%rsi), %r11
movq (%r11,%r8,8), %r11
shrq $0x21, %r11
movb $0x1, (%r10,%r11)
incq %r8
jmp 0x8fc8a
addl %r13d, %r13d
xorl %esi, %esi
cmpq $0x2, %rsi
je 0x8fd26
movq 0x18(%rdi), %rbx
movq 0x28(%rbx), %r8
movq %rsi, %r11
orq %r13, %r11
movq (%r8,%r11,8), %r11
movl %r11d, %r8d
shrq $0x20, %r11
imulq $0x18, %r11, %r11
addq 0x8(%rbx), %r11
xorl %ebx, %ebx
cmpq %r8, %rbx
je 0x8fd21
imulq $0x18, %rbx, %r14
cmpl $0x0, 0xc(%r11,%r14)
js 0x8fd1c
addq %r11, %r14
movl 0x8(%r14), %r14d
shrl %r14d
imulq $0x28, %r14, %r14
addq 0x10(%rdi), %r14
xorl %r15d, %r15d
movl 0x10(%r14), %r12d
cmpq %r12, %r15
jae 0x8fd1c
movq 0x18(%r14), %r12
movq (%r12,%r15,8), %r12
shrq $0x21, %r12
movb $0x2, (%r10,%r12)
incq %r15
jmp 0x8fcfd
incq %rbx
jmp 0x8fcd7
incq %rsi
jmp 0x8fcae
movq 0x10(%rdi), %rsi
leaq (%rsi,%rax), %r8
movl 0x10(%rsi,%rax), %esi
xorl %ecx, %ecx
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %eax, %eax
movq %r13, -0x20(%rsp)
movq %r8, -0x28(%rsp)
movq %rsi, -0x30(%rsp)
cmpq %rsi, %rcx
je 0x8fe26
movq %r15, -0x8(%rsp)
movq 0x18(%r8), %rsi
movq (%rsi,%rcx,8), %rsi
shrq $0x21, %rsi
imulq $0xc, %rsi, %r8
movq -0x18(%rsp), %r11
movl 0x4(%r11,%r8), %r13d
movl (%r11,%r8), %r8d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r8d
subl %r8d, %r13d
imulq $0x18, %rsi, %rsi
movq -0x10(%rsp), %r8
leaq (%r8,%rsi), %rbp
movl 0xc(%r8,%rsi), %esi
imulq $0x28, %rsi, %r11
xorl %esi, %esi
cmpq %rsi, %r11
je 0x8fe07
movq (%rbp), %r8
cmpb $0x1, 0x20(%r8,%rsi)
jne 0x8fe01
movl 0xc(%r8,%rsi), %r14d
movq 0x20(%rdx), %r15
cmpl $0x0, (%r15,%r14,8)
js 0x8fdda
movzbl (%r10,%r14), %r14d
cmpl $0x2, %r14d
je 0x8fdf5
cmpl $0x1, %r14d
jne 0x8fe01
movl 0x8(%r8,%rsi), %r14d
subl (%r8,%rsi), %r14d
addq %r14, %rbx
jmp 0x8fe01
cmpq %r14, (%r9)
jbe 0x8fe01
movq 0x8(%r9), %r12
movslq (%r12,%r14,4), %r14
testq %r14, %r14
js 0x8fe01
cmpl $0x0, (%r15,%r14,8)
jns 0x8fdbb
jmp 0x8fe01
movl 0x8(%r8,%rsi), %r14d
subl (%r8,%rsi), %r14d
addq %r14, %rax
addq $0x28, %rsi
jmp 0x8fd9a
movq -0x8(%rsp), %r15
addq %r13, %r15
incq %rcx
movq -0x20(%rsp), %r13
movq -0x28(%rsp), %r8
movq -0x30(%rsp), %rsi
jmp 0x8fd4a
cvtsi2sd %rax, %xmm0
cvtsi2sd %rbx, %xmm1
mulsd 0x153698(%rip), %xmm1   # 0x1e34d0
xorl %ecx, %ecx
ucomisd %xmm0, %xmm1
cmovbq %rcx, %rax
movl %esi, %edx
cmpq %rdx, %rcx
jae 0x8fe63
movq 0x18(%r8), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r10,%rdx)
incq %rcx
movl 0x10(%r8), %esi
jmp 0x8fe42
xorl %ecx, %ecx
cmpq $0x2, %rcx
je 0x8fed3
movq 0x18(%rdi), %r8
movq 0x28(%r8), %rdx
movq %rcx, %rsi
orq %r13, %rsi
movq (%rdx,%rsi,8), %rsi
movl %esi, %edx
shrq $0x20, %rsi
imulq $0x18, %rsi, %rsi
addq 0x8(%r8), %rsi
xorl %r8d, %r8d
cmpq %rdx, %r8
je 0x8fece
imulq $0x18, %r8, %r9
movl 0x8(%rsi,%r9), %r9d
shrl %r9d
imulq $0x28, %r9, %r9
addq 0x10(%rdi), %r9
xorl %r11d, %r11d
movl 0x10(%r9), %r14d
cmpq %r14, %r11
jae 0x8fec9
movq 0x18(%r9), %r14
movq (%r14,%r11,8), %r14
shrq $0x21, %r14
movb $0x0, (%r10,%r14)
incq %r11
jmp 0x8feaa
incq %r8
jmp 0x8fe8e
incq %rcx
jmp 0x8fe65
testq %r15, %r15
je 0x8fee2
addq %rbx, %rax
cqto
idivq %r15
jmp 0x8fee4
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq