// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: set_ug_coverage_aggressive(ma_ug_t*, unsigned int, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, unsigned char*, unsigned char*, long long)
void set_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,
uint8_t* is_r_het, long long het_cov_thres)
{
    ma_utg_t *u = &(ug->u.a[uID]);
    uint32_t k, j, rId, tn, is_Unitig;
    long long R_bases = 0, C_bases = 0;
    long long cov_in_s, cov_in_e, cov_out_s, cov_out_e, cov_in, cov_out, try_cov, cen_cov;
    uint32_t nv, i;
    asg_arc_t *av = NULL;
    ma_hit_t *h;
    if(u->m == 0) return;

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

    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;
        cov_in = cov_out = 0;
        cov_in_s = cov_in_e = cov_out_s = cov_out_e = 0;
        for (j = 0; j < (uint64_t)(sources[rId].length); j++)
        {
            h = &(sources[rId].buffer[j]);
            ///if(h->del) continue;
            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] == 1)
            {
                cov_in += (Get_qe((*h)) - Get_qs((*h)));
                if(((Get_qs((*h)) <= coverage_cut[rId].s))) cov_in_s++;
                if(((Get_qe((*h)) >= coverage_cut[rId].e))) cov_in_e++;
            } 
            
            if(r_flag[tn] == 2)
            {
                cov_out += (Get_qe((*h)) - Get_qs((*h))); 
                if(((Get_qs((*h)) <= coverage_cut[rId].s))) cov_out_s++;
                if(((Get_qe((*h)) >= coverage_cut[rId].e))) cov_out_e++;
            }
        }

        if(cov_out_s >= cov_out_e) ///more out overlap from s
        {
            cen_cov = cov_in_e;
            try_cov = cov_in_s + cov_out_s;
        }
        else ///more out overlap from e
        {
            cen_cov = cov_in_s;
            try_cov = cov_in_e + cov_out_e;
        }

        if(cov_out <= (cov_in*0.1))
        {
            C_bases = cov_in + cov_out;
            R_bases = (coverage_cut[rId].e - coverage_cut[rId].s);
        }
        else if((try_cov <= cen_cov*1.1) && (MIN(cov_out_s, cov_out_e)<=((MAX(cov_out_s, cov_out_e))*0.2)))
        {
            C_bases = cov_in + cov_out;
            R_bases = (coverage_cut[rId].e - coverage_cut[rId].s);
        }
        else
        {
            C_bases = cen_cov;
            R_bases = 1;
        }

        if((R_bases <= 0) || ((C_bases/R_bases) <= het_cov_thres))
        {
            is_r_het[rId] |= C_HET;
        }
    }


    ///reset
    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;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r8, -0x28(%rsp)
movq %rcx, -0x30(%rsp)
movq %rdx, -0x8(%rsp)
movq 0x10(%rdi), %rax
movl %esi, %ecx
imulq $0x28, %rcx, %r14
cmpl $0x0, 0xc(%rax,%r14)
je 0x83806
movq %r9, -0x10(%rsp)
movq 0x38(%rsp), %r11
addq %r14, %rax
xorl %ecx, %ecx
movl 0x10(%rax), %edx
cmpq %rdx, %rcx
jae 0x834ff
movq 0x18(%rax), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x1, (%r11,%rdx)
incq %rcx
jmp 0x834e1
addl %esi, %esi
xorl %eax, %eax
cmpq $0x2, %rax
je 0x83570
movq 0x18(%rdi), %r8
movq 0x28(%r8), %rcx
movq %rax, %rdx
orq %rsi, %rdx
movq (%rcx,%rdx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r8), %rdx
xorl %ebx, %ebx
cmpq %rcx, %rbx
je 0x8356b
imulq $0x18, %rbx, %r8
movl 0x8(%rdx,%r8), %r8d
shrl %r8d
imulq $0x28, %r8, %r8
addq 0x10(%rdi), %r8
xorl %r10d, %r10d
movl 0x10(%r8), %r9d
cmpq %r9, %r10
jae 0x83566
movq 0x18(%r8), %r9
movq (%r9,%r10,8), %r9
shrq $0x21, %r9
movb $0x2, (%r11,%r9)
incq %r10
jmp 0x83547
incq %rbx
jmp 0x8352b
incq %rax
jmp 0x83503
addq 0x10(%rdi), %r14
xorl %edx, %edx
movsd 0x15f672(%rip), %xmm0   # 0x1e2bf0
movsd 0x1646e2(%rip), %xmm1   # 0x1e7c68
movsd 0x15ff42(%rip), %xmm2   # 0x1e34d0
movq %rsi, -0x38(%rsp)
movq %r14, -0x40(%rsp)
movl 0x10(%r14), %ecx
cmpq %rcx, %rdx
jae 0x83774
movq 0x18(%r14), %rax
movq %rdx, -0x18(%rsp)
movq (%rax,%rdx,8), %rsi
shrq $0x21, %rsi
imulq $0x18, %rsi, %rcx
movq -0x28(%rsp), %rdx
leaq (%rdx,%rcx), %rax
movl 0xc(%rdx,%rcx), %ecx
movq %rsi, -0x20(%rsp)
imulq $0xc, %rsi, %rdx
addq -0x30(%rsp), %rdx
imulq $0x28, %rcx, %r13
xorl %r12d, %r12d
xorl %esi, %esi
xorl %r8d, %r8d
xorl %ebx, %ebx
xorl %ecx, %ecx
movq $0x0, -0x48(%rsp)
xorl %r14d, %r14d
cmpq %r12, %r13
je 0x836b6
movq (%rax), %r15
cmpb $0x1, 0x20(%r15,%r12)
jne 0x836ad
movl 0xc(%r15,%r12), %r10d
movq -0x8(%rsp), %r9
movq 0x20(%r9), %r9
cmpl $0x0, (%r9,%r10,8)
js 0x8365f
movzbl (%r11,%r10), %r9d
cmpl $0x2, %r9d
je 0x83681
cmpl $0x1, %r9d
jne 0x836ad
movl (%r15,%r12), %r9d
movl 0x8(%r15,%r12), %r10d
movl %r10d, %r15d
subl %r9d, %r15d
addq %r15, -0x48(%rsp)
movl (%rdx), %ebp
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl %r15d, %ebp
cmpl %r9d, %ebp
sbbq $-0x1, %rsi
cmpl 0x4(%rdx), %r10d
sbbq $-0x1, %r8
jmp 0x836ad
movq -0x10(%rsp), %rbp
cmpq %r10, (%rbp)
jbe 0x836ad
movq 0x8(%rbp), %rbp
movslq (%rbp,%r10,4), %r10
testq %r10, %r10
js 0x836ad
cmpl $0x0, (%r9,%r10,8)
jns 0x8361e
jmp 0x836ad
movl (%r15,%r12), %r9d
movl 0x8(%r15,%r12), %r10d
movl %r10d, %r15d
subl %r9d, %r15d
addq %r15, %r14
movl (%rdx), %ebp
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl %r15d, %ebp
cmpl %r9d, %ebp
sbbq $-0x1, %rbx
cmpl 0x4(%rdx), %r10d
sbbq $-0x1, %rcx
addq $0x28, %r12
jmp 0x835f1
cmpq %rcx, %rbx
movq %r8, %rax
cmovlq %rsi, %rax
movq %r14, %r9
cvtsi2sd %r14, %xmm3
movq -0x48(%rsp), %r14
cvtsi2sd %r14, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm3, %xmm4
jae 0x8372b
addq %rcx, %r8
addq %rbx, %rsi
cmpq %rcx, %rbx
cmovlq %r8, %rsi
cvtsi2sd %rsi, %xmm3
cvtsi2sd %rax, %xmm4
mulsd %xmm1, %xmm4
ucomisd %xmm3, %xmm4
jae 0x83704
movl $0x1, %ecx
jmp 0x83741
cmpq %rcx, %rbx
movq %rcx, %rsi
cmovlq %rbx, %rsi
cvtsi2sd %rsi, %xmm3
cmovgq %rbx, %rcx
cvtsi2sd %rcx, %xmm4
mulsd %xmm2, %xmm4
ucomisd %xmm3, %xmm4
movl $0x1, %ecx
jb 0x83741
movl 0x4(%rdx), %ecx
movl (%rdx), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
subl %eax, %ecx
je 0x8374f
addq %r14, %r9
movq %r9, %rax
movl %ecx, %ecx
cqto
idivq %rcx
cmpq 0x48(%rsp), %rax
jg 0x8375d
movq 0x40(%rsp), %rax
movq -0x20(%rsp), %rcx
orb $0x1, (%rax,%rcx)
movq -0x18(%rsp), %rdx
incq %rdx
movq -0x38(%rsp), %rsi
movq -0x40(%rsp), %r14
jmp 0x83598
xorl %eax, %eax
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x83797
movq 0x18(%r14), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r11,%rcx)
incq %rax
movl 0x10(%r14), %ecx
jmp 0x83776
xorl %eax, %eax
cmpq $0x2, %rax
je 0x83806
movq 0x18(%rdi), %r8
movq 0x28(%r8), %rcx
movq %rax, %rdx
orq %rsi, %rdx
movq (%rcx,%rdx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r8), %rdx
xorl %ebx, %ebx
cmpq %rcx, %rbx
je 0x83801
imulq $0x18, %rbx, %r8
movl 0x8(%rdx,%r8), %r8d
shrl %r8d
imulq $0x28, %r8, %r8
addq 0x10(%rdi), %r8
xorl %r9d, %r9d
movl 0x10(%r8), %r10d
cmpq %r10, %r9
jae 0x837fc
movq 0x18(%r8), %r10
movq (%r10,%r9,8), %r10
shrq $0x21, %r10
movb $0x0, (%r11,%r10)
incq %r9
jmp 0x837dd
incq %rbx
jmp 0x837c1
incq %rax
jmp 0x83799
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq