// File: /workspace/github_cpp_makefile_star2_O3/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, -0x18(%rsp)
movq 0x10(%rdi), %rcx
movl %esi, %eax
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xb74ee
movq %r9, %r13
movq 0x38(%rsp), %r10
leaq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x10(%rcx)
je 0xb74f5
xorl %r11d, %r11d
movq 0x18(%rcx), %r9
movq (%r9,%r11,8), %r9
shrq $0x21, %r9
movb $0x1, (%r10,%r9)
incq %r11
movl 0x10(%rcx), %ebx
cmpq %rbx, %r11
jb 0xb74cd
movq %r8, %r9
jmp 0xb74fa
xorl %eax, %eax
jmp 0xb777e
movq %r8, %r9
xorl %ebx, %ebx
movq 0x40(%rsp), %rcx
movq %rbx, (%rcx)
addl %esi, %esi
movb $0x1, %cl
xorl %r8d, %r8d
movq 0x18(%rdi), %r11
shlq $0x3, %r8
addq 0x28(%r11), %r8
movq (%r8,%rsi,8), %rbx
testl %ebx, %ebx
je 0xb757e
movq %rbx, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%r11), %r8
movl %ebx, %r12d
xorl %r11d, %r11d
movq 0x10(%rdi), %rbx
leaq (%r11,%r11,2), %r14
movl 0x8(%r8,%r14,8), %r14d
shrl %r14d
leaq (%r14,%r14,4), %r14
cmpl $0x0, 0x10(%rbx,%r14,8)
je 0xb7576
leaq (%rbx,%r14,8), %rbx
xorl %r14d, %r14d
movq 0x18(%rbx), %r15
movq (%r15,%r14,8), %r15
shrq $0x21, %r15
movb $0x2, (%r10,%r15)
incq %r14
movl 0x10(%rbx), %r15d
cmpq %r15, %r14
jb 0xb7559
incq %r11
cmpq %r12, %r11
jne 0xb7536
movl $0x1, %r8d
testb $0x1, %cl
movl $0x0, %ecx
jne 0xb7509
movq 0x10(%rdi), %rcx
movl 0x10(%rcx,%rax,8), %r8d
testl %r8d, %r8d
je 0xb76dd
movq %rsi, -0x30(%rsp)
leaq (%rcx,%rax,8), %rbx
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r11d, %r11d
movq %r9, -0x20(%rsp)
movq %rbx, -0x28(%rsp)
movq 0x18(%rbx), %rcx
movq (%rcx,%r15,8), %rcx
shrq $0x21, %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x18(%rsp), %rsi
movl 0x4(%rsi,%rcx,4), %r14d
movl (%rsi,%rcx,4), %ebp
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ebp
movl %ebp, -0x34(%rsp)
movl 0xc(%r9,%rcx,8), %ebp
testl %ebp, %ebp
je 0xb7699
movq %r14, -0x10(%rsp)
movq %r11, -0x8(%rsp)
leaq (%r9,%rcx,8), %r11
xorl %r14d, %r14d
xorl %r8d, %r8d
movq (%r11), %rcx
cmpb $0x1, 0x20(%rcx,%r14)
jne 0xb7673
movl 0xc(%rcx,%r14), %esi
movq 0x20(%rdx), %rbx
cmpl $0x0, (%rbx,%rsi,8)
js 0xb764f
movzbl (%r10,%rsi), %ebx
testl %ebx, %ebx
je 0xb7673
cmpl $0x2, %ebx
jne 0xb7641
movq 0x40(%rsp), %r9
incq (%r9)
movb $0x3, (%r10,%rsi)
movl 0xc(%r11), %ebp
movl 0x8(%rcx,%r14), %esi
subl (%rcx,%r14), %esi
addq %rsi, %rax
jmp 0xb7673
cmpq %rsi, (%r13)
jbe 0xb7673
movq %r13, %r9
movq 0x8(%r13), %r12
movslq (%r12,%rsi,4), %rsi
testq %rsi, %rsi
js 0xb7670
cmpl $0x0, (%rbx,%rsi,8)
movq %r9, %r13
jns 0xb7622
jmp 0xb7673
movq %r9, %r13
incq %r8
movl %ebp, %ecx
addq $0x28, %r14
cmpq %rcx, %r8
jb 0xb7608
movq -0x28(%rsp), %rbx
movl 0x10(%rbx), %r8d
movq -0x20(%rsp), %r9
movq -0x8(%rsp), %r11
movq -0x10(%rsp), %r14
subl -0x34(%rsp), %r14d
addq %r14, %r11
incq %r15
movl %r8d, %ecx
cmpq %rcx, %r15
jb 0xb75bf
testl %r8d, %r8d
je 0xb76e4
xorl %ecx, %ecx
movq -0x30(%rsp), %rsi
movq 0x18(%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r10,%rdx)
incq %rcx
movl 0x10(%rbx), %edx
cmpq %rdx, %rcx
jb 0xb76bc
movq %r11, %r15
jmp 0xb76ec
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0xb76ec
movq %r11, %r15
movq -0x30(%rsp), %rsi
movb $0x1, %cl
xorl %edx, %edx
movq 0x18(%rdi), %r9
shlq $0x3, %rdx
addq 0x28(%r9), %rdx
movq (%rdx,%rsi,8), %r8
testl %r8d, %r8d
je 0xb7766
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%r9), %rdx
movl %r8d, %r14d
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 0xb775e
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 0xb7741
incq %r8
cmpq %r14, %r8
jne 0xb771e
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xb76f0
cqto
idivq %r15
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq