// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdi, 0x40(%rsp)
leaq 0x19fddf(%rip), %rax    # 0x253758
movl 0x13c(%rax), %eax
cmpl $-0x1, %eax
je 0xb3c4f
leaq 0x19fdc9(%rip), %rcx    # 0x253758
movl 0x140(%rcx), %ecx
cmpl $-0x1, %ecx
je 0xb3c4f
cmpl %ecx, %eax
jg 0xb3c4f
movq %r8, %r13
movq %rsi, %rbx
movq %r9, 0x38(%rsp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x781bd
movq %rax, 0x10(%rsp)
movq 0x18(%rax), %r14
movl 0x14(%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r15d, %r15d
movsd 0x12fb26(%rip), %xmm2   # 0x1e3500
movsd 0x12fb5e(%rip), %xmm3   # 0x1e3540
movl $0x0, 0x4(%rsp)
movq %r14, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
cmpq %rcx, %r15
je 0xb3c00
movq 0x20(%r14), %rax
cmpl $0x0, (%rax,%r15,8)
js 0xb3bf8
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdx
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%rdx,%rax)
je 0xb3bf8
movq %r15, 0x20(%rsp)
addq %rax, %rdx
movl 0x10(%rdx), %eax
movq %rax, 0x30(%rsp)
leaq 0x19fd1d(%rip), %rax    # 0x253758
movslq 0x13c(%rax), %rcx
movq %rcx, 0x60(%rsp)
movslq 0x140(%rax), %rax
movq %rax, 0x48(%rsp)
xorl %r15d, %r15d
movl $0x0, (%rsp)
xorl %r8d, %r8d
movq $0x0, 0x28(%rsp)
cmpq 0x30(%rsp), %r15
je 0xb3b5d
movq %rdx, %r12
movq 0x18(%rdx), %rax
movq (%rax,%r15,8), %rcx
shrq $0x21, %rcx
imulq $0xc, %rcx, %rdx
movq 0x50(%rsp), %rsi
movl 0x4(%rsi,%rdx), %eax
movl (%rsi,%rdx), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
subl %edx, %eax
imulq $0x18, %rcx, %rcx
movq 0x58(%rsp), %rsi
leaq (%rsi,%rcx), %rdx
movl 0xc(%rsi,%rcx), %ecx
imulq $0x28, %rcx, %r14
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r11d, %r11d
cmpq %rsi, %r14
je 0xb3b14
movq (%rdx), %r9
cmpb $0x1, 0x20(%r9,%rsi)
jne 0xb3b0e
movl 0xc(%r9,%rsi), %r10d
movq 0x20(%rbx), %rdi
cmpl $0x0, (%rdi,%r10,8)
js 0xb3aee
movl 0x8(%r9,%rsi), %ebp
subl (%r9,%rsi), %ebp
cmpb $0x1, 0x4(%rdi,%r10,8)
jne 0xb3b0b
addq %rbp, %r11
jmp 0xb3b0e
cmpq %r10, (%r13)
jbe 0xb3b0e
movq 0x8(%r13), %rbp
movslq (%rbp,%r10,4), %r10
testq %r10, %r10
js 0xb3b0e
cmpl $0x0, (%rdi,%r10,8)
jns 0xb3ad8
jmp 0xb3b0e
addq %rbp, %rcx
addq $0x28, %rsi
jmp 0xb3ab8
movl %eax, %esi
addq %r11, %rcx
movq %rcx, %rax
cqto
idivq %rsi
cmpq 0x60(%rsp), %rax
jl 0xb3b4f
cmpq 0x48(%rsp), %rax
movq %r12, %rdx
jg 0xb3b52
cvtsi2sd %r11, %xmm0
cvtsi2sd %rcx, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
sbbl $-0x1, (%rsp)
addq %r11, 0x28(%rsp)
jmp 0xb3b52
movq %r12, %rdx
addq %rcx, %r8
incq %r15
jmp 0xb3a69
movl (%rsp), %ecx
movl %ecx, %eax
cvtsi2sd %rax, %xmm0
cvtsi2sdq 0x30(%rsp), %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0xb3be1
cvtsi2sdq 0x28(%rsp), %xmm0
cvtsi2sd %r8, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r15
ja 0xb3b9e
cmpl $0x0, (%rsp)
jne 0xb3bef
movq %r14, %rdi
movl %r15d, %esi
movq %rdx, %r12
callq 0x6f8da
cmpl $0x0, 0xc(%r12)
je 0xb3bca
andq $0x0, 0xc(%r12)
movq 0x18(%r12), %rdi
callq 0x2220
andq $0x0, 0x18(%r12)
movq 0x8(%rsp), %rcx
movsd 0x12f929(%rip), %xmm2   # 0x1e3500
movsd 0x12f961(%rip), %xmm3   # 0x1e3540
jmp 0xb3bf8
testl %ecx, %ecx
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r15
je 0xb3b9e
incl 0x4(%rsp)
movq 0x8(%rsp), %rcx
incq %r15
jmp 0xb39f4
cmpl $0x0, 0x4(%rsp)
je 0xb3cf1
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
movq 0x40(%rsp), %r14
movq (%r14), %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0xb3691
movl 0x14(%rbx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xb3c5e
movq 0x20(%rbx), %rdx
movb $0x1, 0x4(%rdx,%rcx,8)
incq %rcx
jmp 0xb3c3c
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rax
movq 0x18(%rax), %rcx
movl 0x14(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rdx, %rsi
je 0xb3cc2
movq 0x20(%rcx), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0xb3cbd
movq 0x10(%rax), %r8
imulq $0x28, %rsi, %r9
cmpl $0x0, 0xc(%r8,%r9)
je 0xb3cbd
addq %r9, %r8
movl 0x10(%r8), %r9d
xorl %r10d, %r10d
cmpq %r10, %r9
je 0xb3cbd
movq 0x18(%r8), %r11
movq (%r11,%r10,8), %r11
movb 0x4(%rdi,%rsi,8), %bpl
movq 0x20(%rbx), %r14
shrq $0x21, %r11
movb %bpl, 0x4(%r14,%r11,8)
incq %r10
jmp 0xb3c99
incq %rsi
jmp 0xb3c70
movl 0x14(%rbx), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %r14d, %r14d
cmpq %r14, %r15
je 0xb3cf1
movq 0x20(%rbx), %rax
cmpb $0x1, 0x4(%rax,%r14,8)
jne 0xb3cec
movq %rbx, %rdi
movl %r14d, %esi
callq 0x80870
incq %r14
jmp 0xb3cd0
movq 0x10(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6921d