// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
leaq 0x2ac22e(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x13c(%rax)
jl 0x1185e3
leaq 0x2ac21e(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x140(%rax)
jge 0x1185ec
movb $0x0, -0x1(%rbp)
jmp 0x118960
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x118636
movb $0x0, -0x1(%rbp)
jmp 0x118960
movl $0x0, -0x6c(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x11868c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movl -0x78(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x118654
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1188cd
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x78(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x28(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x11882f
movq -0x28(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x74(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0x11875b
jmp 0x118821
movq -0x88(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1187b8
movq -0x30(%rbp), %rdi
movl -0x7c(%rbp), %esi
leaq -0x7c(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x7c(%rbp)
je 0x1187b4
cmpl $0x1, -0x80(%rbp)
je 0x1187b4
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1187b6
jmp 0x118821
jmp 0x1187b8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1187d0
jmp 0x118821
movq -0x38(%rbp), %rax
movl -0x7c(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x1187ff
movq -0x88(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x88(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x11881f
movq -0x88(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x88(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x118821
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x118707
movq -0x50(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
cqto
idivq -0x40(%rbp)
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
leaq 0x2abf98(%rip), %rcx    # 0x3c47f8
movslq 0x13c(%rcx), %rcx
cmpq %rcx, %rax
jl 0x1188bd
movq -0x48(%rbp), %rax
leaq 0x2abf81(%rip), %rcx    # 0x3c47f8
movslq 0x140(%rcx), %rcx
cmpq %rcx, %rax
jg 0x1188bd
cvtsi2sdq -0x50(%rbp), %xmm0
movq -0x50(%rbp), %rax
addq -0x58(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x2699ca(%rip), %xmm2   # 0x382268
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1188b1
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x50(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x1188bf
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x118693
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x11890c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movl -0x78(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1188d4
movl -0x6c(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x269908(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118950
cvtsi2sdq -0x68(%rbp), %xmm1
cvtsi2sdq -0x60(%rbp), %xmm0
movsd 0x2698ea(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x118956
cmpl $0x0, -0x6c(%rbp)
jne 0x11895c
movb $0x0, -0x1(%rbp)
jmp 0x118960
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)