// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_ug_coverage_aggressive(ma_ug_t*, unsigned int, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, unsigned char*, unsigned long*)
uint32_t get_ug_coverage_aggressive(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,
uint64_t *n_utg)
{
    ma_utg_t* u = &(ug->u.a[uID]);
    uint32_t k, j, rId, tn, is_Unitig;
    long long R_bases = 0, C_bases = 0;
    ma_hit_t *h;
    if(u->m == 0) return 0;

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

    (*n_utg) = u->n;
    uint32_t nv, i;
    asg_arc_t *av = NULL;
    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++)
            {
                rId = u->a[k]>>33;
                r_flag[rId] = 2;
            }
        }
    }
    

    u = &(ug->u.a[uID]);
    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->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] == 2)
            {
                (*n_utg)++;
                r_flag[tn] = 3;
            } 
            C_bases += (Get_qe((*h)) - Get_qs((*h)));
            ///if(uID == 35701) fprintf(stderr, "flag: %u, coverage: %d\n", r_flag[tn], (int)(Get_qe((*h)) - Get_qs((*h))));
        }
    }





    for (k = 0; k < u->n; k++)
    {
        rId = u->a[k]>>33;
        r_flag[rId] = 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++)
            {
                rId = u->a[k]>>33;
                r_flag[rId] = 0;
            }
        }
    }

    ///if(uID == 35701) fprintf(stderr, "C_bases: %lld, R_bases: %lld, (*n_utg): %lu\n", C_bases, R_bases, (*n_utg));
    return C_bases/R_bases;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, -0x10(%rsp)
movq 0x10(%rdi), %rcx
movl %esi, %eax
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xb66fa
movq 0x38(%rsp), %r10
leaq (%rcx,%rax,8), %rcx
movl 0x10(%rcx), %ebx
testl %ebx, %ebx
je 0xb6514
xorl %r11d, %r11d
movq 0x18(%rcx), %rbx
movq (%rbx,%r11,8), %rbx
shrq $0x21, %rbx
movb $0x1, (%r10,%rbx)
incq %r11
movl 0x10(%rcx), %ebx
cmpq %rbx, %r11
jb 0xb64f8
movq %r9, -0x30(%rsp)
movl %ebx, %ecx
movq 0x40(%rsp), %r9
movq %rcx, (%r9)
addl %esi, %esi
xorl %ecx, %ecx
movq 0x18(%rdi), %rbx
leaq (,%rcx,8), %r9
addq 0x28(%rbx), %r9
movq (%r9,%rsi,8), %r9
testl %r9d, %r9d
je 0xb659f
movq %r9, %r11
shrq $0x20, %r11
leaq (%r11,%r11,2), %r15
shlq $0x3, %r15
addq 0x8(%rbx), %r15
movl %r9d, %r12d
xorl %r9d, %r9d
movq 0x10(%rdi), %r11
leaq (%r9,%r9,2), %rbx
movl 0x8(%r15,%rbx,8), %ebx
shrl %ebx
leaq (%rbx,%rbx,4), %rbx
cmpl $0x0, 0x10(%r11,%rbx,8)
je 0xb6597
leaq (%r11,%rbx,8), %r11
xorl %ebx, %ebx
movq 0x18(%r11), %r14
movq (%r14,%rbx,8), %r14
shrq $0x21, %r14
movb $0x2, (%r10,%r14)
incq %rbx
movl 0x10(%r11), %r14d
cmpq %r14, %rbx
jb 0xb657a
incq %r9
cmpq %r12, %r9
jne 0xb6559
leaq 0x1(%rcx), %r9
testq %rcx, %rcx
movq %r9, %rcx
je 0xb6527
movq 0x10(%rdi), %rcx
leaq (%rcx,%rax,8), %r9
cmpl $0x0, 0x10(%r9)
movq %rsi, -0x28(%rsp)
je 0xb6701
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r12d, %r12d
movq -0x30(%rsp), %rbx
movq %r8, -0x18(%rsp)
movq %r9, -0x20(%rsp)
movq %r12, -0x8(%rsp)
movq 0x18(%r9), %rcx
movq (%rcx,%r15,8), %rcx
shrq $0x21, %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x10(%rsp), %rsi
movl 0x4(%rsi,%rcx,4), %r12d
movl (%rsi,%rcx,4), %r13d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r13d
cmpl $0x0, 0xc(%r8,%rcx,8)
je 0xb66d0
leaq (%r8,%rcx,8), %r11
xorl %ebp, %ebp
xorl %r14d, %r14d
movq (%r11), %r8
cmpb $0x1, 0x20(%r8,%rbp)
jne 0xb66bc
movl 0xc(%r8,%rbp), %r9d
movq 0x20(%rdx), %rcx
cmpl $0x0, (%rcx,%r9,8)
movl %r9d, %esi
js 0xb6671
movl %esi, %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0xb66bc
movzbl (%r10,%rsi), %ecx
testl %ecx, %ecx
je 0xb66bc
cmpl $0x2, %ecx
jne 0xb6663
movq 0x40(%rsp), %rcx
incq (%rcx)
movb $0x3, (%r10,%rsi)
movl 0x8(%r8,%rbp), %ecx
subl (%r8,%rbp), %ecx
addq %rcx, %rax
jmp 0xb66bc
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %r9, (%rbx)
jbe 0xb6698
movq 0x8(%rbx), %rbx
movl (%rbx,%r9,4), %r9d
cmpl $-0x1, %r9d
je 0xb6698
movl %r9d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
shrl $0x1f, %r9d
jmp 0xb669b
xorl %r9d, %r9d
cmpl $-0x1, %esi
sete %bl
orb %r9b, %bl
je 0xb66ad
movq -0x30(%rsp), %rbx
jmp 0xb66bc
movl %esi, %r9d
cmpl $0x0, (%rcx,%r9,8)
movq -0x30(%rsp), %rbx
jns 0xb6640
incq %r14
movl 0xc(%r11), %ecx
addq $0x28, %rbp
cmpq %rcx, %r14
jb 0xb661e
subl %r13d, %r12d
movq -0x8(%rsp), %rcx
addq %r12, %rcx
movq %rcx, %r12
incq %r15
movq -0x20(%rsp), %r9
movl 0x10(%r9), %ecx
cmpq %rcx, %r15
movq -0x18(%rsp), %r8
jb 0xb65de
jmp 0xb6706
xorl %eax, %eax
jmp 0xb67c2
xorl %r12d, %r12d
xorl %eax, %eax
cmpl $0x0, 0x10(%r9)
movq -0x28(%rsp), %r14
je 0xb6731
xorl %ecx, %ecx
movq 0x18(%r9), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r10,%rdx)
incq %rcx
movl 0x10(%r9), %edx
cmpq %rdx, %rcx
jb 0xb6714
xorl %ecx, %ecx
movq 0x18(%rdi), %rsi
leaq (,%rcx,8), %rdx
addq 0x28(%rsi), %rdx
movq (%rdx,%r14,8), %r8
testl %r8d, %r8d
je 0xb67ad
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%rsi), %rdx
movl %r8d, %esi
xorl %r8d, %r8d
movq 0x10(%rdi), %r9
leaq (%r8,%r8,2), %r11
movl 0x8(%rdx,%r11,8), %r11d
shrl %r11d
leaq (%r11,%r11,4), %r11
cmpl $0x0, 0x10(%r9,%r11,8)
je 0xb67a5
leaq (%r9,%r11,8), %r9
xorl %r11d, %r11d
movq 0x18(%r9), %rbx
movq (%rbx,%r11,8), %rbx
shrq $0x21, %rbx
movb $0x0, (%r10,%rbx)
incq %r11
movl 0x10(%r9), %ebx
cmpq %rbx, %r11
jb 0xb6788
incq %r8
cmpq %rsi, %r8
jne 0xb6765
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xb6733
cqto
idivq %r12
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq