// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, -0x10(%rsp)
movq %rdx, -0x18(%rsp)
leaq 0x1bf199(%rip), %rdx    # 0x253758
xorl %eax, %eax
cmpl $0x0, 0x13c(%rdx)
js 0x947b7
cmpl $0x0, 0x140(%rdx)
js 0x947b7
cmpl $0x0, 0xc(%rdi)
je 0x947b5
xorl %eax, %eax
movl 0x10(%rdi), %r12d
cmpq %r12, %rax
jae 0x94606
movq 0x18(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r9,%rcx)
incq %rax
jmp 0x945e7
movslq 0x13c(%rdx), %rax
movq %rax, -0x28(%rsp)
movslq 0x140(%rdx), %rax
movq %rax, -0x30(%rsp)
xorl %ecx, %ecx
movsd 0x14ef18(%rip), %xmm0   # 0x1e3540
xorl %r10d, %r10d
movq $0x0, -0x40(%rsp)
movl $0x0, -0x44(%rsp)
movq %rdi, -0x20(%rsp)
cmpq %r12, %rcx
je 0x94749
movq %r10, -0x8(%rsp)
movq 0x18(%rdi), %rax
movq %rcx, -0x38(%rsp)
movq (%rax,%rcx,8), %rcx
shrq $0x21, %rcx
imulq $0xc, %rcx, %rdx
movq -0x18(%rsp), %rdi
movl 0x4(%rdi,%rdx), %eax
movl (%rdi,%rdx), %edx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %edx
subl %edx, %eax
imulq $0x18, %rcx, %rcx
movq -0x10(%rsp), %rdi
leaq (%rdi,%rcx), %rdx
movl 0xc(%rdi,%rcx), %ecx
imulq $0x28, %rcx, %rbx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r11d, %r11d
cmpq %r13, %rbx
je 0x946f1
movq (%rdx), %r14
cmpb $0x1, 0x20(%r14,%r13)
jne 0x946eb
movl 0xc(%r14,%r13), %r10d
movq 0x20(%rsi), %rcx
cmpl $0x0, (%rcx,%r10,8)
js 0x946cc
movl 0x8(%r14,%r13), %ecx
subl (%r14,%r13), %ecx
cmpb $0x0, (%r9,%r10)
je 0x946e8
addq %rcx, %r15
jmp 0x946eb
cmpq %r10, (%r8)
jbe 0x946eb
movq 0x8(%r8), %rbp
movslq (%rbp,%r10,4), %r10
testq %r10, %r10
js 0x946eb
cmpl $0x0, (%rcx,%r10,8)
jns 0x946b7
jmp 0x946eb
addq %rcx, %r11
addq $0x28, %r13
jmp 0x94697
movl %eax, %ecx
addq %r15, %r11
movq %r11, %rax
cqto
idivq %rcx
cmpq -0x28(%rsp), %rax
jl 0x9472f
cmpq -0x30(%rsp), %rax
movq -0x38(%rsp), %rcx
jg 0x94734
cvtsi2sd %r15, %xmm1
cvtsi2sd %r11, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
sbbl $-0x1, -0x44(%rsp)
addq %r15, -0x40(%rsp)
jmp 0x94734
movq -0x38(%rsp), %rcx
movq -0x8(%rsp), %r10
addq %r11, %r10
incq %rcx
movq -0x20(%rsp), %rdi
jmp 0x94641
xorl %eax, %eax
movl -0x44(%rsp), %edx
movl %r12d, %ecx
cmpq %rcx, %rax
jae 0x94771
movq 0x18(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r9,%rcx)
incq %rax
movl 0x10(%rdi), %r12d
jmp 0x9474f
movl %edx, %eax
cvtsi2sd %rax, %xmm0
cvtsi2sd %rcx, %xmm1
mulsd 0x14ed7b(%rip), %xmm1   # 0x1e3500
ucomisd %xmm0, %xmm1
jbe 0x947ad
cvtsi2sdq -0x40(%rsp), %xmm0
cvtsi2sd %r10, %xmm1
mulsd 0x14ed61(%rip), %xmm1   # 0x1e3500
xorl %eax, %eax
ucomisd %xmm0, %xmm1
ja 0x947b7
testl %edx, %edx
jne 0x947b1
jmp 0x947b7
testl %edx, %edx
je 0x947b5
movb $0x1, %al
jmp 0x947b7
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq