// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xfb93e
jmp 0xfc068
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfb97d
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x44(%rbp)
movq 0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xfb945
movl $0x0, -0xa8(%rbp)
cmpl $0x2, -0xa8(%rbp)
jae 0xfba94
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0xc(%rbp), %ecx
shll %ecx
addl -0xa8(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa4(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0xc(%rbp), %edx
shll %edx
addl -0xa8(%rbp), %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0xfba7e
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xb0(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfba6e
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x44(%rbp)
movq 0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xfba36
jmp 0xfba70
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xfb9f9
jmp 0xfba80
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0xfb987
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfbf12
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x44(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x28(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0xfbd32
movq -0x28(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0xfbb68
jmp 0xfbd24
movq -0xb8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xfbbc8
movq -0x30(%rbp), %rdi
movl -0x48(%rbp), %esi
leaq -0x48(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x48(%rbp)
je 0xfbbc1
cmpl $0x1, -0x4c(%rbp)
je 0xfbbc1
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xfbbc6
jmp 0xfbd24
jmp 0xfbbc8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xfbbe3
jmp 0xfbd24
movq 0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xfbbf8
jmp 0xfbd24
movq 0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0xfbc8d
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0xb8(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xb8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
ja 0xfbc62
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0xfbc8b
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0xfbc8d
movq 0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xfbd22
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0xb8(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xb8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
ja 0xfbcf7
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0xfbd20
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xfbd22
jmp 0xfbd24
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xfbb14
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0xfbd58
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0xfbd72
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x70(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x98(%rbp)
cvtsi2sdq -0x90(%rbp), %xmm1
cvtsi2sdq -0x88(%rbp), %xmm0
movsd 0x28328c(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0xfbddc
movq -0x88(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0xfbeda
cvtsi2sdq -0x98(%rbp), %xmm1
cvtsi2sdq -0xa0(%rbp), %xmm0
movsd 0x28aa52(%rip), %xmm2   # 0x386848
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0xfbec5
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0xfbe1b
movq -0x78(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0xfbe26
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0xfbe51
movq -0x78(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0xfbe5c
movq -0x80(%rbp), %rax
movq %rax, -0xd0(%rbp)
movsd -0xc8(%rbp), %xmm1
movq -0xd0(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x283198(%rip), %xmm2   # 0x37f010
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0xfbec5
movq -0x88(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0xfbed8
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x1, -0x58(%rbp)
jmp 0xfbeda
cmpq $0x0, -0x58(%rbp)
jle 0xfbef1
movq -0x60(%rbp), %rax
cqto
idivq -0x58(%rbp)
cmpq 0x20(%rbp), %rax
jg 0xfbf02
movq 0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
movzbl (%rax,%rcx), %edx
orl $0x1, %edx
movb %dl, (%rax,%rcx)
jmp 0xfbf04
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xfbab1
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfbf51
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x44(%rbp)
movq 0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xfbf19
movl $0x0, -0xa8(%rbp)
cmpl $0x2, -0xa8(%rbp)
jae 0xfc068
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0xc(%rbp), %ecx
shll %ecx
addl -0xa8(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa4(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0xc(%rbp), %edx
shll %edx
addl -0xa8(%rbp), %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0xfc052
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xb0(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfc042
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x44(%rbp)
movq 0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xfc00a
jmp 0xfc044
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xfbfcd
jmp 0xfc054
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0xfbf5b
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)