// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_haplotype_rate(ma_ug_t*, asg_t*, ma_hit_t_alloc*, R_to_U*, buf_t*, unsigned int, unsigned int, unsigned int, float)
int get_haplotype_rate(ma_ug_t *ug, asg_t *read_g, ma_hit_t_alloc* reverse_sources,
R_to_U* ruIndex, buf_t* b_0, uint32_t beg, uint32_t end, uint32_t query_cId, float match_rate)
{
    uint32_t i, j, k, num, match_num, offset, uId, qn, rId, is_Unitig, cId;
    ma_utg_t* reads;

    offset = match_num = num = 0;
    for (i = 0; i < b_0->b.n; i++)
    {
        uId = b_0->b.a[i]>>1;
        ///if(IsMerge(ug->u, uId)>0) continue;
        reads = &(ug->u.a[uId]);
        for (j = 0; j < reads->n; j++, offset++)
        {

            if(offset < beg || offset > end) continue;
            qn = reads->a[j]>>33;
            if(reverse_sources[qn].length > 0) num++;
            for (k = 0; k < reverse_sources[qn].length; k++)
            {
                rId = Get_tn(reverse_sources[qn].buffer[k]);
                ///there are three cases: 
                ///1. read at primary contigs, get_R_to_U() return its corresponding contig Id  
                ///2. read at alternative contigs, get_R_to_U() return (uint32_t)-1
                ///3. read has bee deleted, get_R_to_U() return the id of read that contains it 
                if(read_g->seq[rId].del == 1)
                {
                    ///get the id of read that contains it 
                    get_R_to_U(ruIndex, rId, &rId, &is_Unitig);
                    if(rId == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[rId].del == 1) continue;
                }

                ///here rId is the id of the read coming from the different haplotype
                ///cId is the id of the corresponding contig (note here is the contig, instead of untig)
                get_R_to_U(ruIndex, rId, &cId, &is_Unitig);
                if(is_Unitig == 0) continue;
                ///if the read is at alternative contigs, cId might be (uint32_t)-1
                
                if(cId == query_cId)
                {
                    match_num++;
                    break;
                } 
                
            }
        }
    } 

    if(match_num >= num*match_rate) return 1;
    return 0; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, -0x10(%rsp)
movq 0x38(%r8), %rax
movq %rax, -0x20(%rsp)
testq %rax, %rax
je 0xea061
movl 0x40(%rsp), %r10d
movl 0x38(%rsp), %r11d
movq 0x48(%r8), %rax
movq %rax, -0x28(%rsp)
movq 0x10(%rdi), %rsi
xorl %edi, %edi
xorl %r15d, %r15d
movl $0x0, -0x40(%rsp)
xorl %ebx, %ebx
movl %r9d, -0x3c(%rsp)
movq %rsi, -0x30(%rsp)
movq -0x28(%rsp), %rax
movl (%rax,%rdi,4), %eax
shrl %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rsi,%rax,8), %r12d
testq %r12, %r12
je 0xea03f
movq %rdi, -0x18(%rsp)
leaq (%rsi,%rax,8), %rax
movq %rax, -0x8(%rsp)
xorl %r8d, %r8d
movl %r15d, -0x34(%rsp)
cmpl %r9d, %r15d
setb %al
cmpl %r11d, %r15d
seta %sil
orb %al, %sil
jne 0xea01e
movq -0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r8,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rdx,%rax,8), %esi
xorl %edi, %edi
testq %rsi, %rsi
setne %dil
addl %edi, %ebx
testq %rsi, %rsi
je 0xea01e
movl %ebx, -0x38(%rsp)
movq %rdx, %r9
leaq (%rdx,%rax,8), %rax
movq (%rax), %rdi
movq -0x10(%rsp), %rax
movq 0x20(%rax), %rax
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %r14
xorl %ebx, %ebx
movl 0xc(%rdi,%rbx), %r11d
cmpl $0x0, (%rax,%r11,8)
movl %r11d, %esi
js 0xe9fb5
movl %esi, %r11d
movb $0x1, %sil
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpq %r11, (%rcx)
jbe 0xe9fa3
movq 0x8(%rcx), %rbp
movl (%rbp,%r11,4), %r11d
cmpl $-0x1, %r11d
je 0xe9fa3
movl %r11d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
shrl $0x1f, %r11d
movl %r11d, %esi
cmpl %r10d, %r13d
setne %r11b
xorb $0x1, %sil
orb %r11b, %sil
jne 0xe9ffa
jmp 0xea009
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %r11, (%rcx)
jbe 0xe9fdd
movq 0x8(%rcx), %r13
movl (%r13,%r11,4), %r11d
cmpl $-0x1, %r11d
je 0xe9fdd
movl %r11d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
shrl $0x1f, %r11d
jmp 0xe9fe0
xorl %r11d, %r11d
cmpl $-0x1, %esi
sete %r13b
orb %r11b, %r13b
jne 0xe9ffa
movl %esi, %r11d
cmpl $0x0, (%rax,%r11,8)
jns 0xe9f72
addq $0x28, %rbx
cmpq %rbx, %r14
jne 0xe9f63
jmp 0xea00d
incl -0x40(%rsp)
movq %r9, %rdx
movl -0x3c(%rsp), %r9d
movl 0x38(%rsp), %r11d
movl -0x38(%rsp), %ebx
incq %r8
incl %r15d
cmpq %r12, %r8
jne 0xe9eff
movl -0x34(%rsp), %r15d
addl %r12d, %r15d
movq -0x30(%rsp), %rsi
movq -0x18(%rsp), %rdi
incq %rdi
movl %edi, %eax
cmpq %rax, -0x20(%rsp)
ja 0xe9ecd
movl -0x40(%rsp), %eax
cvtsi2ss %rax, %xmm1
movl %ebx, %eax
cvtsi2ss %rax, %xmm2
jmp 0xea067
xorps %xmm2, %xmm2
xorps %xmm1, %xmm1
mulss %xmm0, %xmm2
xorl %eax, %eax
ucomiss %xmm2, %xmm1
setae %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq