// File: /workspace/github_cpp_makefile_star2_O1/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, -0x40(%rsp)
movq %rdx, -0x38(%rsp)
movq 0x10(%rdi), %rcx
movl %esi, %eax
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xbe266
movq 0x38(%rsp), %r11
leaq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x10(%rcx)
je 0xbde59
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 0xbde3c
addl %esi, %esi
xorl %ecx, %ecx
movq 0x18(%rdi), %r10
leaq (,%rcx,8), %rdx
addq 0x28(%r10), %rdx
movq (%rdx,%rsi,8), %rbx
testl %ebx, %ebx
je 0xbded6
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 0xbdece
leaq (%rbx,%r10,8), %r15
xorl %r10d, %r10d
movq 0x18(%r15), %rbx
movq (%rbx,%r10,8), %rbx
shrq $0x21, %rbx
movb $0x2, (%r11,%rbx)
incq %r10
movl 0x10(%r15), %ebx
cmpq %rbx, %r10
jb 0xbdeb1
incq %r12
cmpq %r14, %r12
jne 0xbde8e
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xbde5d
movq 0x10(%rdi), %rcx
leaq (%rcx,%rax,8), %r10
cmpl $0x0, 0x10(%r10)
je 0xbe1b5
xorl %ecx, %ecx
movsd 0x166ccd(%rip), %xmm0   # 0x224bd0
movsd 0x16bc65(%rip), %xmm1   # 0x229b70
movsd 0x16756d(%rip), %xmm2   # 0x225480
movq %r9, -0x48(%rsp)
movq %r8, -0x28(%rsp)
movq %rsi, -0x20(%rsp)
movq %r10, -0x30(%rsp)
movq 0x18(%r10), %rax
movq %rcx, -0x8(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x10(%rsp)
leaq (%rax,%rax,2), %rsi
movl 0xc(%r8,%rsi,8), %ecx
testq %rcx, %rcx
movq %rsi, -0x18(%rsp)
je 0xbe0a8
leaq (%r8,%rsi,8), %rax
movq (%rax), %rax
movq -0x40(%rsp), %rdx
leaq (%rdx,%rsi,4), %rsi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r13
xorl %ebx, %ebx
movq $0x0, -0x50(%rsp)
movq $0x0, -0x58(%rsp)
xorl %ebp, %ebp
movq $0x0, -0x60(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
movq -0x38(%rsp), %r8
cmpb $0x1, 0x20(%rax,%rbx)
jne 0xbe099
movl 0xc(%rax,%rbx), %r10d
movq 0x20(%r8), %rdx
cmpl $0x0, (%rdx,%r10,8)
movl %r10d, %r14d
js 0xbe046
movl %r14d, %r10d
cmpl $0x0, (%rdx,%r10,8)
js 0xbe099
movzbl (%r11,%r10), %edx
testl %edx, %edx
je 0xbe099
cmpl $0x1, %edx
jne 0xbe00b
movl (%rax,%rbx), %r10d
movl 0x8(%rax,%rbx), %r14d
movl %r14d, %r12d
subl %r10d, %r12d
addq %r12, -0x58(%rsp)
movl (%rsi), %r12d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %r12d
cmpl %r10d, %r12d
movq -0x48(%rsp), %r9
sbbq $-0x1, %rcx
cmpl 0x4(%rsi), %r14d
sbbq $-0x1, %r15
cmpb $0x2, %dl
jne 0xbe099
movl (%rax,%rbx), %edx
movl 0x8(%rax,%rbx), %r10d
movl %r10d, %r14d
subl %edx, %r14d
addq %r14, -0x50(%rsp)
movl (%rsi), %r14d
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
andl %r12d, %r14d
cmpl %edx, %r14d
sbbq $-0x1, -0x60(%rsp)
cmpl 0x4(%rsi), %r10d
sbbq $-0x1, %rbp
jmp 0xbe099
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq %r10, (%r9)
jbe 0xbe06f
movq 0x8(%r9), %r12
movl (%r12,%r10,4), %r10d
cmpl $-0x1, %r10d
je 0xbe06f
movl %r10d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
shrl $0x1f, %r10d
jmp 0xbe072
xorl %r10d, %r10d
cmpl $-0x1, %r14d
sete %r12b
orb %r10b, %r12b
je 0xbe086
movq -0x48(%rsp), %r9
jmp 0xbe099
movl %r14d, %r10d
cmpl $0x0, (%rdx,%r10,8)
movq -0x48(%rsp), %r9
jns 0xbdfb7
addq $0x28, %rbx
cmpq %rbx, %r13
jne 0xbdf95
jmp 0xbe0ca
xorl %ecx, %ecx
xorl %r15d, %r15d
movq $0x0, -0x60(%rsp)
xorl %ebp, %ebp
movq $0x0, -0x58(%rsp)
movq $0x0, -0x50(%rsp)
movq -0x60(%rsp), %r14
cmpq %rbp, %r14
movq %r15, %rax
cmovlq %rcx, %rax
movq -0x50(%rsp), %rsi
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
movq -0x58(%rsp), %rbx
xorps %xmm4, %xmm4
cvtsi2sd %rbx, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm3, %xmm4
movq -0x28(%rsp), %r8
movq -0x30(%rsp), %r10
jae 0xbe15b
addq %rbp, %r15
addq %r14, %rcx
cmpq %rbp, %r14
cmovlq %r15, %rcx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %rax, %xmm4
mulsd %xmm1, %xmm4
movl $0x1, %ecx
ucomisd %xmm3, %xmm4
jb 0xbe17b
cmpq %rbp, %r14
movq %rbp, %rdx
cmovlq %r14, %rdx
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
cmovgq %r14, %rbp
xorps %xmm4, %xmm4
cvtsi2sd %rbp, %xmm4
mulsd %xmm2, %xmm4
ucomisd %xmm3, %xmm4
jb 0xbe17b
addq %rbx, %rsi
movq -0x40(%rsp), %rax
movq -0x18(%rsp), %rdx
movl 0x4(%rax,%rdx,4), %ecx
movl (%rax,%rdx,4), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
subl %eax, %ecx
movq %rsi, %rax
testl %ecx, %ecx
je 0xbe18d
movl %ecx, %ecx
cqto
idivq %rcx
cmpq 0x48(%rsp), %rax
jg 0xbe19b
movq 0x40(%rsp), %rax
movq -0x10(%rsp), %rcx
orb $0x1, (%rax,%rcx)
movq -0x8(%rsp), %rcx
incq %rcx
movl 0x10(%r10), %eax
cmpq %rax, %rcx
movq -0x20(%rsp), %rsi
jb 0xbdf27
cmpl $0x0, 0x10(%r10)
je 0xbe1db
xorl %eax, %eax
movq 0x18(%r10), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r11,%rcx)
incq %rax
movl 0x10(%r10), %ecx
cmpq %rcx, %rax
jb 0xbe1be
xorl %eax, %eax
movq 0x18(%rdi), %rdx
leaq (,%rax,8), %rcx
addq 0x28(%rdx), %rcx
movq (%rcx,%rsi,8), %r8
testl %r8d, %r8d
je 0xbe256
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 0xbe24e
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 0xbe231
incq %rbx
cmpq %rdx, %rbx
jne 0xbe20e
leaq 0x1(%rax), %rcx
testq %rax, %rax
movq %rcx, %rax
je 0xbe1dd
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq