// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: if_primary_unitig(ma_utg_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, R_to_U*, unsigned char*)
uint8_t if_primary_unitig(ma_utg_t* u, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, uint8_t* r_flag)
{
    if(asm_opt.recover_atg_cov_min < 0 || asm_opt.recover_atg_cov_max < 0) return 0;
    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_primary = 0;
    uint32_t available_reads = 0, k, j, rId, tn, is_Unitig;
    ma_hit_t *h;
    if(u->m == 0) return 0;
    total_C_bases = total_C_bases_primary = available_reads = 0;
    for (k = 0; k < u->n; k++)
    {
        rId = u->a[k]>>33;
        r_flag[rId] = 1;
    }

    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(r_flag[tn])
            {
                C_bases_primary += Get_qe((*h)) - Get_qs((*h));
            }
            else
            {
                C_bases_alter += Get_qe((*h)) - Get_qs((*h));
            }
        }

        C_bases = C_bases_primary + C_bases_alter;
        total_C_bases += C_bases; 
        ///if(C_bases_primary < C_bases * ALTER_COV_THRES) continue;

        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_primary >= (C_bases_primary + C_bases_alter) * ALTER_COV_THRES) available_reads++;
            total_C_bases_primary += C_bases_primary;
        }
    }

    for (k = 0; k < u->n; k++)
    {
        rId = u->a[k]>>33;
        r_flag[rId] = 0;
    }

    ///fprintf(stderr, "available_reads: %u, u->n: %u\n", available_reads, u->n);

    //if(available_reads < (u->n * 0.8) || available_reads == 0)
    if(((available_reads < (u->n * 0.8)) && (total_C_bases_primary < (total_C_bases * 0.8))) 
       || available_reads == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, -0x8(%rsp)
leaq 0x1c577e(%rip), %rdx    # 0x295758
xorl %eax, %eax
cmpl $0x0, 0x13c(%rdx)
js 0xd01fc
cmpl $0x0, 0x140(%rdx)
js 0xd01fc
cmpl $0x0, 0xc(%rdi)
je 0xd01fa
cmpl $0x0, 0x10(%rdi)
je 0xd01fa
xorl %eax, %eax
movq 0x18(%rdi), %r10
movq (%r10,%rax,8), %r10
shrq $0x21, %r10
movb $0x1, (%r9,%r10)
incq %rax
movl 0x10(%rdi), %r14d
cmpq %r14, %rax
jb 0xd000c
testq %r14, %r14
je 0xd01fa
movq $0x0, -0x40(%rsp)
movq %rdi, -0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rax, -0x10(%rsp)
movslq 0x13c(%rdx), %rax
movq %rax, -0x18(%rsp)
movslq 0x140(%rdx), %rax
movq %rax, -0x20(%rsp)
movsd 0x16158f(%rip), %xmm0   # 0x2315f8
xorl %ebx, %ebx
movq $0x0, -0x38(%rsp)
xorl %edi, %edi
movq %rcx, -0x30(%rsp)
movq -0x10(%rsp), %rax
movq (%rax,%rbx,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rcx,%rax,8), %r10d
testq %r10, %r10
je 0xd0105
leaq (%rcx,%rax,8), %rdx
movq (%rdx), %rdx
shlq $0x3, %r10
leaq (%r10,%r10,4), %r12
xorl %r13d, %r13d
xorl %r11d, %r11d
xorl %r15d, %r15d
cmpb $0x1, 0x20(%rdx,%r13)
jne 0xd00fa
movl 0xc(%rdx,%r13), %r10d
movq 0x20(%rsi), %rcx
cmpl $0x0, (%rcx,%r10,8)
js 0xd00db
movl 0x8(%rdx,%r13), %ecx
subl (%rdx,%r13), %ecx
cmpb $0x0, (%r9,%r10)
je 0xd00f7
addq %rcx, %r15
jmp 0xd00fa
cmpq %r10, (%r8)
jbe 0xd00fa
movq 0x8(%r8), %rbp
movslq (%rbp,%r10,4), %r10
testq %r10, %r10
js 0xd00fa
cmpl $0x0, (%rcx,%r10,8)
jns 0xd00c6
jmp 0xd00fa
addq %rcx, %r11
addq $0x28, %r13
cmpq %r13, %r12
jne 0xd00ae
jmp 0xd010b
xorl %r15d, %r15d
xorl %r11d, %r11d
movq -0x8(%rsp), %rdx
movl 0x4(%rdx,%rax,4), %ecx
movl (%rdx,%rax,4), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
subl %eax, %ecx
addq %r15, %r11
movq %r11, %rax
cqto
idivq %rcx
cmpq -0x18(%rsp), %rax
jl 0xd016a
cmpq -0x20(%rsp), %rax
movq -0x30(%rsp), %rcx
jg 0xd016f
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
movq -0x40(%rsp), %rax
sbbl $-0x1, %eax
movq %rax, -0x40(%rsp)
addq %r15, -0x38(%rsp)
jmp 0xd016f
movq -0x30(%rsp), %rcx
addq %r11, %rdi
incq %rbx
cmpq %r14, %rbx
jne 0xd007b
testq %r14, %r14
movq -0x40(%rsp), %rsi
je 0xd01f2
xorps %xmm0, %xmm0
cvtsi2sdq -0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
mulsd 0x16140e(%rip), %xmm1   # 0x2315b0
xorl %eax, %eax
movq -0x28(%rsp), %rdx
movq 0x18(%rdx), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r9,%rcx)
incq %rax
movl 0x10(%rdx), %ecx
cmpq %rcx, %rax
jb 0xd01a9
movl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd 0x1613d9(%rip), %xmm2   # 0x2315b0
movl %esi, %eax
cvtsi2sd %rax, %xmm3
ucomisd %xmm3, %xmm2
jbe 0xd01f2
xorl %eax, %eax
ucomisd %xmm0, %xmm1
ja 0xd01fc
testl %esi, %esi
jne 0xd01f6
jmp 0xd01fc
testl %esi, %esi
je 0xd01fa
movb $0x1, %al
jmp 0xd01fc
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq