// File: /workspace/github_cpp_makefile_star2_O2/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 %r8, -0x8(%rsp)
movq %rcx, -0x10(%rsp)
movq 0x10(%rdi), %rax
movl %esi, %ecx
imulq $0x28, %rcx, %r11
cmpl $0x0, 0xc(%rax,%r11)
je 0x7c615
movq 0x38(%rsp), %r10
addq %r11, %rax
xorl %ecx, %ecx
movl 0x10(%rax), %r8d
cmpq %r8, %rcx
jae 0x7c418
movq 0x18(%rax), %r8
movq (%r8,%rcx,8), %r8
shrq $0x21, %r8
movb $0x1, (%r10,%r8)
incq %rcx
jmp 0x7c3f9
movq 0x40(%rsp), %rax
movq %r8, (%rax)
addl %esi, %esi
xorl %eax, %eax
cmpq $0x2, %rax
je 0x7c492
movq 0x18(%rdi), %rcx
movq 0x28(%rcx), %r8
movq %rax, %rbx
orq %rsi, %rbx
movq (%r8,%rbx,8), %r8
movl %r8d, %ebx
shrq $0x20, %r8
imulq $0x18, %r8, %r14
addq 0x8(%rcx), %r14
xorl %r8d, %r8d
cmpq %rbx, %r8
je 0x7c48d
imulq $0x18, %r8, %rcx
movl 0x8(%r14,%rcx), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
addq 0x10(%rdi), %rcx
xorl %r15d, %r15d
movl 0x10(%rcx), %r12d
cmpq %r12, %r15
jae 0x7c488
movq 0x18(%rcx), %r12
movq (%r12,%r15,8), %r12
shrq $0x21, %r12
movb $0x2, (%r10,%r12)
incq %r15
jmp 0x7c469
incq %r8
jmp 0x7c44e
incq %rax
jmp 0x7c424
addq 0x10(%rdi), %r11
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %eax, %eax
movq %r11, -0x18(%rsp)
movq %rsi, -0x20(%rsp)
movl 0x10(%r11), %r8d
cmpq %r8, %r15
jae 0x7c579
movq %r12, %rcx
movq 0x18(%r11), %rsi
movq (%rsi,%r15,8), %rsi
shrq $0x21, %rsi
imulq $0xc, %rsi, %r8
movq -0x10(%rsp), %r11
movl 0x4(%r11,%r8), %r12d
movl (%r11,%r8), %r8d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r8d
subl %r8d, %r12d
imulq $0x18, %rsi, %r13
addq -0x8(%rsp), %r13
xorl %ebp, %ebp
xorl %ebx, %ebx
movl 0xc(%r13), %esi
cmpq %rsi, %rbx
jae 0x7c561
movq (%r13), %r8
cmpb $0x1, 0x20(%r8,%rbp)
jne 0x7c558
movl 0xc(%r8,%rbp), %esi
movq 0x20(%rdx), %r14
cmpl $0x0, (%r14,%rsi,8)
js 0x7c53f
movzbl (%r10,%rsi), %r14d
testl %r14d, %r14d
je 0x7c558
cmpl $0x2, %r14d
jne 0x7c531
movq 0x40(%rsp), %r11
incq (%r11)
movb $0x3, (%r10,%rsi)
movl 0x8(%r8,%rbp), %esi
subl (%r8,%rbp), %esi
addq %rsi, %rax
jmp 0x7c558
cmpq %rsi, (%r9)
jbe 0x7c558
movq 0x8(%r9), %r11
movslq (%r11,%rsi,4), %rsi
testq %rsi, %rsi
js 0x7c558
cmpl $0x0, (%r14,%rsi,8)
jns 0x7c514
incq %rbx
addq $0x28, %rbp
jmp 0x7c4ef
addq %r12, %rcx
movq %rcx, %r12
incq %r15
movq -0x20(%rsp), %rsi
movq -0x18(%rsp), %r11
jmp 0x7c4a8
xorl %ecx, %ecx
movl %r8d, %edx
cmpq %rdx, %rcx
jae 0x7c59d
movq 0x18(%r11), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r10,%rdx)
incq %rcx
movl 0x10(%r11), %r8d
jmp 0x7c57b
xorl %ecx, %ecx
cmpq $0x2, %rcx
je 0x7c60e
movq 0x18(%rdi), %r9
movq 0x28(%r9), %rdx
movq %rcx, %r8
orq %rsi, %r8
movq (%rdx,%r8,8), %r8
movl %r8d, %edx
shrq $0x20, %r8
imulq $0x18, %r8, %r8
addq 0x8(%r9), %r8
xorl %r14d, %r14d
cmpq %rdx, %r14
je 0x7c609
imulq $0x18, %r14, %r9
movl 0x8(%r8,%r9), %r9d
shrl %r9d
imulq $0x28, %r9, %r9
addq 0x10(%rdi), %r9
xorl %r11d, %r11d
movl 0x10(%r9), %ebx
cmpq %rbx, %r11
jae 0x7c604
movq 0x18(%r9), %rbx
movq (%rbx,%r11,8), %rbx
shrq $0x21, %rbx
movb $0x0, (%r10,%rbx)
incq %r11
jmp 0x7c5e5
incq %r14
jmp 0x7c5c9
incq %rcx
jmp 0x7c59f
cqto
idivq %r12
jmp 0x7c617
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq