// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: recover_utg_by_coverage(ma_ug_t**, asg_t*, ma_sub_t*, ma_hit_t_alloc*, R_to_U*, trans_chain*)
void recover_utg_by_coverage(ma_ug_t **ptg, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, trans_chain* t_ch)
{
    if(asm_opt.recover_atg_cov_min == -1) return;
    if(asm_opt.recover_atg_cov_max == -1) return;
    if(asm_opt.recover_atg_cov_min > asm_opt.recover_atg_cov_max) return;
    ma_ug_t *atg = NULL;
    atg = ma_ug_gen_primary(read_g, ALTER_LABLE);
    asg_t* nsg = atg->g;
    uint32_t v, n_vtx = nsg->n_seq, k, j, rId, available_reads = 0, keep_atg = 0, tn, is_Unitig;
    ma_utg_t* u = NULL;
    ma_hit_t *h;

    
    ///print_untig_by_read(atg, "SRR11606870.634978", -1, NULL, NULL, "debug");
    long long R_bases = 0, C_bases = 0, C_bases_primary = 0, C_bases_alter = 0;
    long long total_C_bases = 0, total_C_bases_alter = 0;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        u = &(atg->u.a[v]);
        if(u->m == 0) continue;
        total_C_bases = total_C_bases_alter = available_reads = 0;
        
        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            C_bases = C_bases_primary = C_bases_alter = 0;
            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(read_g->seq[tn].c == ALTER_LABLE)
                {
                    C_bases_alter += Get_qe((*h)) - Get_qs((*h));
                }
                else
                {
                    C_bases_primary += Get_qe((*h)) - Get_qs((*h));
                }
            }

            C_bases = C_bases_primary + C_bases_alter;
            total_C_bases += C_bases; 

            C_bases = C_bases/R_bases;
            if(C_bases >= asm_opt.recover_atg_cov_min && C_bases <= asm_opt.recover_atg_cov_max)
            {
                if(C_bases_alter >= (C_bases_primary + C_bases_alter) * ALTER_COV_THRES) available_reads++;
                total_C_bases_alter += C_bases_alter;
            }
        }


        if(((available_reads < (u->n * 0.8)) && (total_C_bases_alter < (total_C_bases * 0.8))) 
           || available_reads == 0)
        {
            asg_seq_del(nsg, v);

            if(u->m!=0)
            {
                u->m = u->n = 0;
                free(u->a);
                u->a = NULL;
            }
        }
        else
        {
            ///print_utg_coverage(atg, coverage_cut, v, sources);
            ///fprintf(stderr, "rId: %u\n", rId);
            ///fprintf(stderr, "%.*s\t%lld\n", (int)Get_NAME_LENGTH(R_INF, rId), Get_NAME(R_INF, rId), C_bases);
            keep_atg++;
        }
    }

    if(keep_atg > 0)
    {
        asg_cleanup(nsg);
        asg_symm(nsg);
        append_utg(*ptg, atg, t_ch);

        n_vtx = read_g->n_seq;
        for (v = 0; v < n_vtx; v++)
        {
            read_g->seq[v].c = ALTER_LABLE;
        }
        
        nsg = (*ptg)->g;
        n_vtx = nsg->n_seq;
        for (v = 0; v < n_vtx; ++v) 
        {
            if(nsg->seq[v].del) continue;
            u = &((*ptg)->u.a[v]);
            if(u->m == 0) continue;
            for (k = 0; k < u->n; k++)
            {
                rId = u->a[k]>>33;
                read_g->seq[rId].c = nsg->seq[v].c;
            }
        }


        n_vtx = read_g->n_seq;
        for (v = 0; v < n_vtx; v++)
        {
            if(read_g->seq[v].c == ALTER_LABLE)
            {
                asg_seq_drop(read_g, v);
            }
        }
    }

    ma_ug_destroy(atg);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x70(%rsp)
leaq 0x199792(%rip), %rax    # 0x28a758
movl 0x13c(%rax), %eax
cmpl $-0x1, %eax
je 0xf158b
leaq 0x19977c(%rip), %rdx    # 0x28a758
movl 0x140(%rdx), %edx
cmpl $-0x1, %edx
je 0xf158b
cmpl %edx, %eax
jg 0xf158b
movq %r8, %r13
movq %rsi, %rbx
movq %rdi, %r14
movq %rcx, 0x8(%rsp)
movq %r9, 0x58(%rsp)
movq %rsi, %rdi
movl $0x1, %esi
callq 0xb1a83
movq %rax, 0x18(%rsp)
movq 0x18(%rax), %r15
movl 0x14(%r15), %esi
andq $0x7fffffff, %rsi       # imm = 0x7FFFFFFF
je 0xf1570
movq %r14, 0x50(%rsp)
movsd 0x134476(%rip), %xmm4   # 0x2254b0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movsd 0x1344b0(%rip), %xmm5   # 0x2254f8
xorl %edi, %edi
movl $0x0, (%rsp)
movq %r13, 0x48(%rsp)
movq 0x8(%rsp), %rdx
movq %rsi, 0x10(%rsp)
movq 0x20(%r15), %rax
movl (%rax,%rdi,8), %ecx
testl %ecx, %ecx
js 0xf13a1
movl %ecx, 0x2c(%rsp)
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rdi,%rdi,4), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xf13a1
leaq (%rax,%rcx,8), %rax
movl 0x10(%rax), %ecx
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
movq %rdi, 0x20(%rsp)
movq %rax, 0x60(%rsp)
je 0xf125b
movq 0x18(%rax), %rax
movq %rax, 0x80(%rsp)
leaq 0x199696(%rip), %rax    # 0x28a758
movslq 0x13c(%rax), %rcx
movq %rcx, 0x78(%rsp)
movslq 0x140(%rax), %rax
movq %rax, 0x68(%rsp)
xorl %edi, %edi
movq $0x0, 0x38(%rsp)
xorl %esi, %esi
movl $0x0, 0x4(%rsp)
movq 0x80(%rsp), %rax
movq (%rax,%rdi,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rdx,%rax,8), %ecx
testq %rcx, %rcx
je 0xf11c8
leaq (%rdx,%rax,8), %rdx
movq (%rdx), %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r9
xorl %r10d, %r10d
xorl %r8d, %r8d
xorl %ecx, %ecx
cmpb $0x1, 0x20(%rdx,%r10)
jne 0xf11b9
movl 0xc(%rdx,%r10), %ebp
movq 0x20(%rbx), %r11
cmpl $0x0, (%r11,%rbp,8)
movl %ebp, %r12d
js 0xf1166
movl %r12d, %ebp
cmpl $0x0, (%r11,%rbp,8)
js 0xf11b9
movl 0x8(%rdx,%r10), %r12d
subl (%rdx,%r10), %r12d
cmpb $0x1, 0x4(%r11,%rbp,8)
jne 0xf11a5
addq %r12, %r8
jmp 0xf11b9
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %rbp, (%r13)
jbe 0xf118f
movq 0x8(%r13), %r13
movl (%r13,%rbp,4), %ebp
cmpl $-0x1, %ebp
je 0xf118f
movl %ebp, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
shrl $0x1f, %ebp
jmp 0xf1191
xorl %ebp, %ebp
cmpl $-0x1, %r12d
sete %r13b
orb %bpl, %r13b
je 0xf11aa
movq 0x48(%rsp), %r13
jmp 0xf11b9
addq %r12, %rcx
jmp 0xf11b9
movl %r12d, %r13d
cmpl $0x0, (%r11,%r13,8)
movq 0x48(%rsp), %r13
jns 0xf1146
addq $0x28, %r10
cmpq %r10, %r9
jne 0xf1127
jmp 0xf11cd
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x70(%rsp), %rdx
movl 0x4(%rdx,%rax,4), %r9d
movl (%rdx,%rax,4), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
subl %eax, %r9d
addq %r8, %rcx
movq %rcx, %rax
cqto
idivq %r9
cmpq 0x78(%rsp), %rax
jl 0xf121f
cmpq 0x68(%rsp), %rax
jg 0xf121f
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd %xmm5, %xmm1
ucomisd %xmm1, %xmm0
sbbl $-0x1, 0x4(%rsp)
addq %r8, 0x38(%rsp)
addq %rcx, %rsi
incq %rdi
cmpq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rdx
jne 0xf10ef
xorps %xmm1, %xmm1
cvtsi2sdq 0x38(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
mulsd %xmm4, %xmm0
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %ecx
jmp 0xf1265
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
xorps %xmm3, %xmm3
cvtsi2sdq 0x40(%rsp), %xmm3
mulsd %xmm4, %xmm3
ucomisd %xmm2, %xmm3
movl %ecx, %r8d
movl 0x2c(%rsp), %ecx
jbe 0xf1390
ucomisd %xmm1, %xmm0
movq 0x30(%rsp), %rax
ja 0xf12a2
testl %r8d, %r8d
jne 0xf139e
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax,%rdi,8)
leaq (%rdi,%rdi), %rax
xorl %ecx, %ecx
leaq (%rcx,%rax), %rdx
movq 0x28(%r15), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r14, %rsi
je 0xf133d
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%r15), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
orb $-0x80, 0xf(%rdi,%r9,8)
movl 0x8(%rdi,%r9,8), %r9d
xorq $0x1, %r9
movq 0x28(%r15), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r14, %r9
je 0xf1335
movq 0x8(%r15), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebp
xorl %edx, %ebp
cmpl $0x1, %ebp
jne 0xf132c
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xf131a
incq %r8
cmpq %rsi, %r8
jne 0xf12d8
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xf12b1
movq 0x60(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0xf137f
xorl %r12d, %r12d
movq %r12, 0xc(%rax)
movq 0x18(%rax), %rdi
movq %rax, %rbp
callq 0x2220
movsd 0x134185(%rip), %xmm5   # 0x2254f8
movsd 0x134135(%rip), %xmm4   # 0x2254b0
movq %r12, 0x18(%rbp)
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
jmp 0xf13a1
testl %r8d, %r8d
movq 0x30(%rsp), %rax
je 0xf12a2
incl (%rsp)
incq %rdi
cmpq %rsi, %rdi
jne 0xf1060
cmpl $0x0, (%rsp)
movq 0x50(%rsp), %r14
je 0xf1570
movq %r15, %rdi
callq 0xa02fa
movq %r15, %rdi
callq 0xa0333
movq %r15, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r15)
movq (%r14), %rdi
movq 0x18(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0xf0da7
movl 0x14(%rbx), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
je 0xf1409
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movb $0x1, 0x4(%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0xf13fc
movq (%r14), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0xf1472
movq 0x20(%rdx), %rdx
xorl %esi, %esi
cmpl $0x0, (%rdx,%rsi,8)
js 0xf146a
movq 0x10(%rax), %rdi
leaq (%rsi,%rsi,4), %r8
cmpl $0x0, 0xc(%rdi,%r8,8)
je 0xf146a
leaq (%rdi,%r8,8), %r8
movl 0x10(%r8), %edi
testq %rdi, %rdi
je 0xf146a
movq 0x18(%r8), %r8
movq 0x20(%rbx), %r9
xorl %r10d, %r10d
movq (%r8,%r10,8), %r11
movb 0x4(%rdx,%rsi,8), %bpl
shrq $0x21, %r11
movb %bpl, 0x4(%r9,%r11,8)
incq %r10
cmpq %r10, %rdi
jne 0xf1450
incq %rsi
cmpq %rcx, %rsi
jne 0xf1422
movl 0x14(%rbx), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
je 0xf1570
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x20(%rbx), %rsi
cmpb $0x1, 0x4(%rsi,%rcx,8)
jne 0xf1564
leaq (%rcx,%rcx), %rsi
xorl %edi, %edi
leaq (%rdi,%rsi), %r8
movq 0x28(%rbx), %r9
movq (%r9,%r8,8), %r10
movq %r10, %r9
andq %rdx, %r9
je 0xf1554
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
shlq $0x3, %r10
addq 0x8(%rbx), %r10
xorl %r11d, %r11d
leaq (%r11,%r11,2), %r14
movl 0xc(%r10,%r14,8), %ebp
testl %ebp, %ebp
js 0xf1548
leaq (%r10,%r14,8), %r15
movq 0x20(%rbx), %r12
movl 0x8(%r15), %r14d
movl %r14d, %r13d
shrl %r13d
cmpb $0x1, 0x4(%r12,%r13,8)
je 0xf1548
orl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, 0xc(%r15)
xorq $0x1, %r14
movq 0x28(%rbx), %r15
movq (%r15,%r14,8), %r15
movq %r15, %r14
andq %rdx, %r14
je 0xf1548
movq 0x8(%rbx), %r12
shrq $0x20, %r15
shlq $0x3, %r14
leaq (%r14,%r14,2), %r14
leaq (%r15,%r15,2), %r15
leaq (%r12,%r15,8), %r15
addq $0xc, %r15
xorl %r12d, %r12d
movl -0x4(%r15,%r12), %ebp
xorl %r8d, %ebp
cmpl $0x1, %ebp
jne 0xf153f
orb $-0x80, 0x3(%r15,%r12)
addq $0x18, %r12
cmpq %r12, %r14
jne 0xf152c
incq %r11
cmpq %r9, %r11
jne 0xf14c8
leaq 0x1(%rdi), %r8
testq %rdi, %rdi
movq %r8, %rdi
je 0xf149d
incq %rcx
cmpq %rax, %rcx
jne 0xf1488
movq 0x18(%rsp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0004
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq