// File: /workspace/github_cpp_makefile_star2_O3/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 0xeb8ca
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, -0x44(%rsp)
xorl %ebx, %ebx
movq %rdx, -0x38(%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 0xeb8a8
movq %rdi, -0x18(%rsp)
leaq (%rsi,%rax,8), %rax
movq %rax, -0x8(%rsp)
xorl %r8d, %r8d
movl %r15d, -0x3c(%rsp)
cmpl %r9d, %r15d
setb %al
cmpl %r11d, %r15d
seta %sil
orb %al, %sil
jne 0xeb887
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 0xeb887
movl %ebx, -0x40(%rsp)
leaq (%rdx,%rax,8), %rax
movq (%rax), %rdi
movq -0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rcx), %r14
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %rbx
xorl %esi, %esi
movl 0xc(%rdi,%rsi), %r11d
cmpl $0x0, (%rax,%r11,8)
js 0xeb850
movb $0x1, %r13b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %r11, %r14
jbe 0xeb83f
movq 0x8(%rcx), %rdx
movl (%rdx,%r11,4), %r11d
cmpl $-0x1, %r11d
je 0xeb83f
movl %r11d, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
shrl $0x1f, %r11d
movl %r11d, %r13d
cmpl %r10d, %ebp
setne %dl
xorb $0x1, %r13b
orb %dl, %r13b
jne 0xeb86a
jmp 0xeb875
cmpq %r11, %r14
jbe 0xeb86a
movq 0x8(%rcx), %r13
movslq (%r13,%r11,4), %r11
testq %r11, %r11
js 0xeb86a
cmpl $0x0, (%rax,%r11,8)
jns 0xeb814
addq $0x28, %rsi
cmpq %rsi, %rbx
jne 0xeb808
jmp 0xeb879
incl -0x44(%rsp)
movq -0x38(%rsp), %rdx
movl 0x38(%rsp), %r11d
movl -0x40(%rsp), %ebx
incq %r8
incl %r15d
cmpq %r12, %r8
jne 0xeb7a4
movl -0x3c(%rsp), %r15d
addl %r12d, %r15d
movq -0x30(%rsp), %rsi
movq -0x18(%rsp), %rdi
incq %rdi
movl %edi, %eax
cmpq %rax, -0x20(%rsp)
ja 0xeb772
movl -0x44(%rsp), %eax
cvtsi2ss %rax, %xmm1
movl %ebx, %eax
cvtsi2ss %rax, %xmm2
jmp 0xeb8d0
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