// File: /workspace/github_cpp_makefile_star2_O1/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 0x1bb9e8(%rip), %rdx    # 0x28a758
xorl %eax, %eax
cmpl $0x0, 0x13c(%rdx)
js 0xcefd4
cmpl $0x0, 0x140(%rdx)
js 0xcefd4
cmpl $0x0, 0xc(%rdi)
je 0xcefd2
movl 0x10(%rdi), %eax
testl %eax, %eax
je 0xcedbc
xorl %r10d, %r10d
movq 0x18(%rdi), %rax
movq (%rax,%r10,8), %rax
shrq $0x21, %rax
movb $0x1, (%r9,%rax)
incq %r10
movl 0x10(%rdi), %eax
cmpq %rax, %r10
jb 0xceda0
testl %eax, %eax
je 0xcef6e
movq %rdi, -0x30(%rsp)
movq 0x18(%rdi), %rdi
movq %rdi, -0x10(%rsp)
movslq 0x13c(%rdx), %rdi
movq %rdi, -0x18(%rsp)
movslq 0x140(%rdx), %rdx
movq %rdx, -0x28(%rsp)
movl %eax, %eax
movq %rax, -0x20(%rsp)
xorl %ebp, %ebp
movsd 0x1566fd(%rip), %xmm0   # 0x2254f8
movl $0x0, -0x44(%rsp)
movq $0x0, -0x40(%rsp)
xorl %edi, %edi
movq %rcx, -0x38(%rsp)
movq -0x10(%rsp), %rax
movq (%rax,%rbp,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rcx,%rax,8), %r10d
testq %r10, %r10
je 0xceed6
leaq (%rcx,%rax,8), %rdx
movq (%rdx), %rdx
shlq $0x3, %r10
leaq (%r10,%r10,4), %r15
xorl %r13d, %r13d
xorl %r11d, %r11d
xorl %r14d, %r14d
cmpb $0x1, 0x20(%rdx,%r13)
jne 0xceec7
movl 0xc(%rdx,%r13), %ecx
movq 0x20(%rsi), %rbx
cmpl $0x0, (%rbx,%rcx,8)
movl %ecx, %r10d
js 0xcee83
movl %r10d, %r10d
cmpl $0x0, (%rbx,%r10,8)
js 0xceec7
movl 0x8(%rdx,%r13), %ecx
subl (%rdx,%r13), %ecx
cmpb $0x0, (%r9,%r10)
je 0xceec4
addq %rcx, %r14
jmp 0xceec7
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpq %rcx, (%r8)
jbe 0xceeaa
movq 0x8(%r8), %r12
movl (%r12,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0xceeaa
movl %ecx, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shrl $0x1f, %ecx
jmp 0xceeac
xorl %ecx, %ecx
cmpl $-0x1, %r10d
sete %r12b
orb %cl, %r12b
jne 0xceec7
movl %r10d, %ecx
cmpl $0x0, (%rbx,%rcx,8)
jns 0xcee64
jmp 0xceec7
addq %rcx, %r11
addq $0x28, %r13
cmpq %r13, %r15
jne 0xcee4a
jmp 0xceedc
xorl %r14d, %r14d
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 %r14, %r11
movq %r11, %rax
cqto
idivq %rcx
cmpq -0x18(%rsp), %rax
jl 0xcef33
cmpq -0x28(%rsp), %rax
movq -0x38(%rsp), %rcx
jg 0xcef38
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
sbbl $-0x1, -0x44(%rsp)
addq %r14, -0x40(%rsp)
jmp 0xcef38
movq -0x38(%rsp), %rcx
addq %r11, %rdi
incq %rbp
cmpq -0x20(%rsp), %rbp
jne 0xcee13
xorps %xmm1, %xmm1
cvtsi2sdq -0x40(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd 0x15654d(%rip), %xmm0   # 0x2254b0
movq -0x30(%rsp), %rdi
movl -0x44(%rsp), %edx
jmp 0xcef78
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movl 0x10(%rdi), %eax
testl %eax, %eax
je 0xcef9d
xorl %ecx, %ecx
movq 0x18(%rdi), %rax
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movb $0x0, (%r9,%rax)
incq %rcx
movl 0x10(%rdi), %eax
cmpq %rax, %rcx
jb 0xcef81
movl %edx, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl %eax, %eax
cvtsi2sd %rax, %xmm3
mulsd 0x1564fa(%rip), %xmm3   # 0x2254b0
ucomisd %xmm2, %xmm3
jbe 0xcefca
xorl %eax, %eax
ucomisd %xmm1, %xmm0
ja 0xcefd4
testl %edx, %edx
jne 0xcefce
jmp 0xcefd4
testl %edx, %edx
je 0xcefd2
movb $0x1, %al
jmp 0xcefd4
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq