// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, -0x48(%rsp)
movq %rdx, -0x38(%rsp)
movq 0x10(%rdi), %rcx
movl %esi, %eax
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xbf225
movq 0x38(%rsp), %r11
leaq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x10(%rcx)
je 0xbee72
xorl %edx, %edx
movq 0x18(%rcx), %r10
movq (%r10,%rdx,8), %r10
shrq $0x21, %r10
movb $0x1, (%r11,%r10)
incq %rdx
movl 0x10(%rcx), %r10d
cmpq %r10, %rdx
jb 0xbee55
addl %esi, %esi
movb $0x1, %cl
xorl %edx, %edx
movq 0x18(%rdi), %r10
shlq $0x3, %rdx
addq 0x28(%r10), %rdx
movq (%rdx,%rsi,8), %rbx
testl %ebx, %ebx
je 0xbeeec
movq %rbx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%r10), %rdx
movl %ebx, %r14d
xorl %r12d, %r12d
movq 0x10(%rdi), %rbx
leaq (%r12,%r12,2), %r10
movl 0x8(%rdx,%r10,8), %r10d
shrl %r10d
leaq (%r10,%r10,4), %r10
cmpl $0x0, 0x10(%rbx,%r10,8)
je 0xbeee4
leaq (%rbx,%r10,8), %r15
xorl %ebx, %ebx
movq 0x18(%r15), %r10
movq (%r10,%rbx,8), %r10
shrq $0x21, %r10
movb $0x2, (%r11,%r10)
incq %rbx
movl 0x10(%r15), %r10d
cmpq %r10, %rbx
jb 0xbeec7
incq %r12
cmpq %r14, %r12
jne 0xbeea5
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xbee78
movq 0x10(%rdi), %rcx
movl 0x10(%rcx,%rax,8), %edx
movl %edx, -0x5c(%rsp)
testl %edx, %edx
je 0xbf19a
movq %r9, -0x8(%rsp)
movq %rsi, -0x40(%rsp)
leaq (%rcx,%rax,8), %r9
xorl %ecx, %ecx
movsd 0x171ce5(%rip), %xmm0   # 0x230c10
movsd 0x176e35(%rip), %xmm1   # 0x235d68
movsd 0x172645(%rip), %xmm2   # 0x231580
movq %r8, -0x28(%rsp)
movq %r9, -0x30(%rsp)
movq 0x18(%r9), %rax
movq %rcx, -0x10(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x20(%rsp)
leaq (%rax,%rax,2), %rsi
movl 0xc(%r8,%rsi,8), %ecx
testq %rcx, %rcx
movq %rsi, -0x18(%rsp)
je 0xbf06d
leaq (%r8,%rsi,8), %rax
movq (%rax), %rax
movq -0x48(%rsp), %rdx
leaq (%rdx,%rsi,4), %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r14
xorl %r9d, %r9d
movq $0x0, -0x50(%rsp)
movq $0x0, -0x58(%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %esi, %esi
xorl %r15d, %r15d
movq -0x38(%rsp), %r8
cmpb $0x1, 0x20(%rax,%r9)
jne 0xbf05e
movl 0xc(%rax,%r9), %edx
movq 0x20(%r8), %r10
cmpl $0x0, (%r10,%rdx,8)
js 0xbf00d
movzbl (%r11,%rdx), %edx
cmpl $0x2, %edx
je 0xbf02f
cmpl $0x1, %edx
jne 0xbf05e
movl (%rax,%r9), %edx
movl 0x8(%rax,%r9), %r10d
movl %r10d, %r12d
subl %edx, %r12d
addq %r12, -0x58(%rsp)
movl (%r13), %r12d
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andl %ebx, %r12d
cmpl %edx, %r12d
sbbq $-0x1, %r15
cmpl 0x4(%r13), %r10d
sbbq $-0x1, %rsi
jmp 0xbf05e
movq -0x8(%rsp), %r12
cmpq %rdx, (%r12)
jbe 0xbf05e
movq 0x8(%r12), %r12
movslq (%r12,%rdx,4), %rdx
testq %rdx, %rdx
js 0xbf05e
cmpl $0x0, (%r10,%rdx,8)
jns 0xbefc9
jmp 0xbf05e
movl (%rax,%r9), %edx
movl 0x8(%rax,%r9), %r10d
movl %r10d, %r12d
subl %edx, %r12d
addq %r12, -0x50(%rsp)
movl (%r13), %r12d
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andl %ebx, %r12d
cmpl %edx, %r12d
sbbq $-0x1, %rcx
cmpl 0x4(%r13), %r10d
sbbq $-0x1, %rbp
addq $0x28, %r9
cmpq %r9, %r14
jne 0xbefad
jmp 0xbf088
xorl %r15d, %r15d
xorl %esi, %esi
xorl %ecx, %ecx
xorl %ebp, %ebp
movq $0x0, -0x58(%rsp)
movq $0x0, -0x50(%rsp)
cmpq %rbp, %rcx
movq %rsi, %rax
cmovlq %r15, %rax
movq -0x50(%rsp), %r10
xorps %xmm3, %xmm3
cvtsi2sd %r10, %xmm3
movq -0x58(%rsp), %rbx
xorps %xmm4, %xmm4
cvtsi2sd %rbx, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm3, %xmm4
movq -0x28(%rsp), %r8
movq -0x30(%rsp), %r9
jae 0xbf114
addq %rbp, %rsi
addq %rcx, %r15
cmpq %rbp, %rcx
cmovlq %rsi, %r15
xorps %xmm3, %xmm3
cvtsi2sd %r15, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %rax, %xmm4
mulsd %xmm1, %xmm4
movl $0x1, %esi
ucomisd %xmm3, %xmm4
jb 0xbf136
cmpq %rbp, %rcx
movq %rbp, %rdx
cmovlq %rcx, %rdx
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
cmovgq %rcx, %rbp
xorps %xmm4, %xmm4
cvtsi2sd %rbp, %xmm4
mulsd %xmm2, %xmm4
ucomisd %xmm3, %xmm4
jb 0xbf136
movq -0x48(%rsp), %rax
movq -0x18(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %esi
movl (%rax,%rcx,4), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
subl %eax, %esi
je 0xbf144
addq %rbx, %r10
movq %r10, %rax
movl %esi, %ecx
cqto
idivq %rcx
cmpq 0x48(%rsp), %rax
jg 0xbf15a
movq 0x40(%rsp), %rax
movq -0x20(%rsp), %rcx
orb $0x1, (%rax,%rcx)
movl 0x10(%r9), %eax
movl %eax, -0x5c(%rsp)
movq -0x10(%rsp), %rcx
incq %rcx
movl -0x5c(%rsp), %eax
cmpq %rax, %rcx
jb 0xbef45
cmpl $0x0, -0x5c(%rsp)
movq -0x40(%rsp), %rsi
je 0xbf19a
xorl %eax, %eax
movq 0x18(%r9), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r11,%rcx)
incq %rax
movl 0x10(%r9), %ecx
cmpq %rcx, %rax
jb 0xbf17d
movb $0x1, %al
xorl %ecx, %ecx
movq 0x18(%rdi), %rdx
shlq $0x3, %rcx
addq 0x28(%rdx), %rcx
movq (%rcx,%rsi,8), %r8
testl %r8d, %r8d
je 0xbf213
movq %r8, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%rdx), %rcx
movl %r8d, %edx
xorl %ebx, %ebx
movq 0x10(%rdi), %r8
leaq (%rbx,%rbx,2), %r9
movl 0x8(%rcx,%r9,8), %r9d
shrl %r9d
leaq (%r9,%r9,4), %r9
cmpl $0x0, 0x10(%r8,%r9,8)
je 0xbf20b
leaq (%r8,%r9,8), %r8
xorl %r9d, %r9d
movq 0x18(%r8), %r10
movq (%r10,%r9,8), %r10
shrq $0x21, %r10
movb $0x0, (%r11,%r10)
incq %r9
movl 0x10(%r8), %r10d
cmpq %r10, %r9
jb 0xbf1ee
incq %rbx
cmpq %rdx, %rbx
jne 0xbf1cb
movl $0x1, %ecx
testb $0x1, %al
movl $0x0, %eax
jne 0xbf19e
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq