// File: /workspace/github_cpp_makefile_star2_O1/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, -0x8(%rsp)
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
je 0xb630a
movq $0x0, (%rdx)
testq %rax, %rax
je 0xb6316
movq $0x0, (%rax)
cmpl $0x0, 0xc(%rdi)
je 0xb64b4
movl 0x10(%rdi), %edx
testl %edx, %edx
je 0xb634b
xorl %eax, %eax
movq 0x18(%rdi), %rdx
movq (%rdx,%rax,8), %rdx
cmpq $-0x1, %rdx
je 0xb6340
shrq $0x21, %rdx
movb $0x1, (%r9,%rdx)
incq %rax
movl 0x10(%rdi), %edx
cmpq %rdx, %rax
jb 0xb6329
testl %edx, %edx
je 0xb6460
movq 0x18(%rdi), %r14
movl %edx, %r15d
xorl %r12d, %r12d
xorl %eax, %eax
xorl %ebx, %ebx
movq %r14, -0x10(%rsp)
movq %r15, -0x18(%rsp)
movq (%r14,%r12,8), %rdx
cmpq $-0x1, %rdx
je 0xb6452
shrq $0x21, %rdx
leaq (%rdx,%rdx,2), %rdx
movq -0x8(%rsp), %r11
movl 0x4(%r11,%rdx,4), %r10d
movl (%r11,%rdx,4), %r11d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andl %ebp, %r11d
subl %r11d, %r10d
addq %r10, %rbx
movl 0xc(%rcx,%rdx,8), %r10d
testq %r10, %r10
je 0xb6452
movq %rcx, %r15
leaq (%rcx,%rdx,8), %rcx
movq (%rcx), %r13
shlq $0x3, %r10
leaq (%r10,%r10,4), %r10
xorl %r11d, %r11d
cmpb $0x1, 0x20(%r13,%r11)
jne 0xb6438
movl 0xc(%r13,%r11), %ecx
movq 0x20(%rsi), %rbp
cmpl $0x0, (%rbp,%rcx,8)
movl %ecx, %r14d
js 0xb63fb
movl %r14d, %ecx
cmpl $0x0, (%rbp,%rcx,8)
js 0xb6438
cmpb $0x1, (%r9,%rcx)
jne 0xb6438
movl 0x8(%r13,%r11), %ecx
subl (%r13,%r11), %ecx
addq %rcx, %rax
jmp 0xb6438
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq %rcx, (%r8)
jbe 0xb6421
movq 0x8(%r8), %rdx
movl (%rdx,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0xb6421
movl %ecx, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
shrl $0x1f, %ecx
jmp 0xb6423
xorl %ecx, %ecx
cmpl $-0x1, %r14d
sete %dl
orb %cl, %dl
jne 0xb6438
movl %r14d, %ecx
cmpl $0x0, (%rbp,%rcx,8)
jns 0xb63db
addq $0x28, %r11
cmpq %r11, %r10
jne 0xb63c0
movq %r15, %rcx
movq -0x10(%rsp), %r14
movq -0x18(%rsp), %r15
incq %r12
cmpq %r15, %r12
jne 0xb636b
jmp 0xb6464
xorl %ebx, %ebx
xorl %eax, %eax
cmpl $0x0, 0x10(%rdi)
je 0xb648e
xorl %ecx, %ecx
movq 0x18(%rdi), %rdx
movq (%rdx,%rcx,8), %rdx
cmpq $-0x1, %rdx
je 0xb6483
shrq $0x21, %rdx
movb $0x0, (%r9,%rdx)
incq %rcx
movl 0x10(%rdi), %edx
cmpq %rdx, %rcx
jb 0xb646c
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
je 0xb649b
movq %rbx, (%rcx)
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
je 0xb64a8
movq %rax, (%rcx)
testq %rbx, %rbx
je 0xb64b4
cqto
idivq %rbx
jmp 0xb64b6
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq