// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, -0x10(%rsp)
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %r11
testq %r11, %r11
je 0xb730c
movq $0x0, (%r11)
testq %r10, %r10
je 0xb7318
movq $0x0, (%r10)
cmpl $0x0, 0xc(%rdi)
je 0xb748b
movl 0x10(%rdi), %ebx
xorl %eax, %eax
testl %ebx, %ebx
je 0xb7460
movq 0x18(%rdi), %rdx
movq (%rdx,%rax,8), %rdx
cmpq $-0x1, %rdx
je 0xb7349
shrq $0x21, %rdx
movb $0x1, (%r9,%rdx)
movl 0x10(%rdi), %ebx
incq %rax
movl %ebx, %r15d
cmpq %r15, %rax
jb 0xb732f
testl %ebx, %ebx
je 0xb7465
movq 0x18(%rdi), %r12
xorl %r13d, %r13d
xorl %eax, %eax
xorl %r14d, %r14d
movq %r12, -0x18(%rsp)
movq (%r12,%r13,8), %rdx
cmpq $-0x1, %rdx
je 0xb741e
shrq $0x21, %rdx
leaq (%rdx,%rdx,2), %r10
movq -0x10(%rsp), %r11
movl 0x4(%r11,%r10,4), %edx
movl (%r11,%r10,4), %r11d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andl %ebp, %r11d
subl %r11d, %edx
addq %rdx, %r14
movl 0xc(%rcx,%r10,8), %edx
testq %rdx, %rdx
je 0xb741e
movq %r14, -0x8(%rsp)
movq %rcx, %r14
leaq (%rcx,%r10,8), %r10
movq (%r10), %r10
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %r11
xorl %ebp, %ebp
cmpb $0x1, 0x20(%r10,%rbp)
jne 0xb7408
movl 0xc(%r10,%rbp), %r12d
movq 0x20(%rsi), %rcx
cmpl $0x0, (%rcx,%r12,8)
js 0xb73ef
cmpb $0x1, (%r9,%r12)
jne 0xb7408
movl 0x8(%r10,%rbp), %ecx
subl (%r10,%rbp), %ecx
addq %rcx, %rax
jmp 0xb7408
cmpq %r12, (%r8)
jbe 0xb7408
movq 0x8(%r8), %rdx
movslq (%rdx,%r12,4), %r12
testq %r12, %r12
js 0xb7408
cmpl $0x0, (%rcx,%r12,8)
jns 0xb73da
addq $0x28, %rbp
cmpq %rbp, %r11
jne 0xb73c2
movq %r14, %rcx
movq -0x8(%rsp), %r14
movq -0x18(%rsp), %r12
incq %r13
cmpq %r15, %r13
jne 0xb736d
testl %ebx, %ebx
movq 0x40(%rsp), %r10
je 0xb746a
xorl %ecx, %ecx
movq 0x38(%rsp), %r11
movq 0x18(%rdi), %rdx
movq (%rdx,%rcx,8), %rdx
cmpq $-0x1, %rdx
je 0xb7454
shrq $0x21, %rdx
movb $0x0, (%r9,%rdx)
movl 0x10(%rdi), %ebx
incq %rcx
movl %ebx, %edx
cmpq %rdx, %rcx
jb 0xb743a
jmp 0xb746f
xorl %r14d, %r14d
jmp 0xb746f
xorl %eax, %eax
xorl %r14d, %r14d
movq 0x38(%rsp), %r11
testq %r11, %r11
je 0xb7477
movq %r14, (%r11)
testq %r10, %r10
je 0xb747f
movq %rax, (%r10)
testq %r14, %r14
je 0xb748b
cqto
idivq %r14
jmp 0xb748d
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq