// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_ug_coverage(ma_utg_t*, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, unsigned char*, unsigned long*, unsigned long*)
uint32_t get_ug_coverage(ma_utg_t* u, asg_t* read_g, const ma_sub_t* coverage_cut, ma_hit_t_alloc* sources, R_to_U* ruIndex, uint8_t* r_flag, uint64_t *rR, uint64_t *rC)
{
    uint32_t k, j, rId, tn, is_Unitig;
    long long R_bases = 0, C_bases = 0;
    ma_hit_t *h;
    if(rR) (*rR) = 0; if(rC) (*rC) = 0;
    if(u->m == 0) return 0;

    for (k = 0; k < u->n; k++)
    {
        if(u->a[k] == (uint64_t)-1) continue;
        rId = u->a[k]>>33;
        r_flag[rId] = 1;
    }

    for (k = 0; k < u->n; k++)
    {
        if(u->a[k] == (uint64_t)-1) continue;
        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->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] != 1) continue;
            C_bases += (Get_qe((*h)) - Get_qs((*h)));
        }
    }


    for (k = 0; k < u->n; k++)
    {
        if(u->a[k] == (uint64_t)-1) continue;
        rId = u->a[k]>>33;
        r_flag[rId] = 0;
    }

    if(rR) (*rR) = R_bases; if(rC) (*rC) = C_bases;
    return R_bases == 0? 0:C_bases/R_bases;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, -0x10(%rsp)
movq %rdx, -0x18(%rsp)
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
je 0x7c266
andq $0x0, (%rcx)
testq %rax, %rax
je 0x7c26f
andq $0x0, (%rax)
cmpl $0x0, 0xc(%rdi)
je 0x7c3b8
xorl %eax, %eax
movl 0x10(%rdi), %r14d
cmpq %r14, %rax
jae 0x7c2a0
movq 0x18(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
cmpq $-0x1, %rcx
je 0x7c29b
shrq $0x21, %rcx
movb $0x1, (%r9,%rcx)
incq %rax
jmp 0x7c27b
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %eax, %eax
cmpq %r14, %r15
je 0x7c368
movq 0x18(%rdi), %rcx
movq (%rcx,%r15,8), %r10
cmpq $-0x1, %r10
je 0x7c360
movq %r11, -0x8(%rsp)
shrq $0x21, %r10
imulq $0xc, %r10, %r11
movq -0x18(%rsp), %rcx
movl 0x4(%rcx,%r11), %edx
movl (%rcx,%r11), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
subl %r11d, %edx
imulq $0x18, %r10, %r10
movq -0x10(%rsp), %rcx
leaq (%rcx,%r10), %r13
movl 0xc(%rcx,%r10), %r10d
imulq $0x28, %r10, %r10
xorl %r11d, %r11d
cmpq %r11, %r10
je 0x7c358
movq (%r13), %rbp
cmpb $0x1, 0x20(%rbp,%r11)
jne 0x7c352
movl 0xc(%rbp,%r11), %ebx
movq 0x20(%rsi), %r12
cmpl $0x0, (%r12,%rbx,8)
js 0x7c339
cmpb $0x1, (%r9,%rbx)
jne 0x7c352
movl 0x8(%rbp,%r11), %ecx
subl (%rbp,%r11), %ecx
addq %rcx, %rax
jmp 0x7c352
cmpq %rbx, (%r8)
jbe 0x7c352
movq 0x8(%r8), %rcx
movslq (%rcx,%rbx,4), %rbx
testq %rbx, %rbx
js 0x7c352
cmpl $0x0, (%r12,%rbx,8)
jns 0x7c323
addq $0x28, %r11
jmp 0x7c302
movq -0x8(%rsp), %r11
addq %rdx, %r11
incq %r15
jmp 0x7c2a8
xorl %ecx, %ecx
movl %r14d, %edx
cmpq %rdx, %rcx
jae 0x7c392
movq 0x18(%rdi), %rdx
movq (%rdx,%rcx,8), %rdx
cmpq $-0x1, %rdx
je 0x7c38d
shrq $0x21, %rdx
movb $0x0, (%r9,%rdx)
movl 0x10(%rdi), %r14d
incq %rcx
jmp 0x7c36a
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
je 0x7c39f
movq %r11, (%rcx)
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
je 0x7c3ac
movq %rax, (%rcx)
testq %r11, %r11
je 0x7c3b8
cqto
idivq %r11
jmp 0x7c3ba
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq