// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, -0x10(%rsp)
movq %rdi, -0x30(%rsp)
movl 0x40(%rsp), %r10d
movl 0x38(%rsp), %ebx
movq %r8, -0x28(%rsp)
movq 0x38(%r8), %rax
movq %rax, -0x20(%rsp)
xorl %r13d, %r13d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x0, -0x40(%rsp)
xorl %r15d, %r15d
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, -0x20(%rsp)
jbe 0xacbf0
movq -0x28(%rsp), %rax
movq 0x48(%rax), %rax
movq %rdx, -0x18(%rsp)
movl (%rax,%rdx,4), %eax
shrl %eax
movq -0x30(%rsp), %rdx
movq 0x10(%rdx), %rdi
imulq $0x28, %rax, %rax
leaq (%rdi,%rax), %rdx
movq %rdx, -0x8(%rsp)
movl 0x10(%rdi,%rax), %edx
xorl %edi, %edi
movl %r15d, -0x3c(%rsp)
movq %rdx, -0x38(%rsp)
cmpq %rdx, %rdi
je 0xacbdc
cmpl %r9d, %r15d
setb %al
cmpl %ebx, %r15d
seta %r8b
orb %al, %r8b
jne 0xacbd1
movl %r9d, %edx
movq -0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rdi,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rax
movq -0x10(%rsp), %r9
leaq (%r9,%rax), %r8
movl 0xc(%r9,%rax), %eax
cmpq $0x1, %rax
sbbl $-0x1, %r13d
imulq $0x28, %rax, %r12
xorl %r14d, %r14d
cmpq %r14, %r12
je 0xacbc5
movq (%r8), %rax
movl 0xc(%rax,%r14), %r11d
movq 0x20(%rsi), %rbx
cmpl $0x0, (%rbx,%r11,8)
movq (%rcx), %rax
js 0xacb91
movb $0x1, %bl
cmpq %r11, %rax
jbe 0xacbac
movq 0x8(%rcx), %rax
movl (%rax,%r11,4), %r11d
movl %ebp, %eax
cmpl $-0x1, %r11d
je 0xacbae
movl %r11d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shrl $0x1f, %r11d
movl %r11d, %ebx
jmp 0xacbae
cmpq %r11, %rax
jbe 0xacbbb
movq 0x8(%rcx), %r9
movslq (%r9,%r11,4), %r11
testq %r11, %r11
js 0xacbbb
cmpl $0x0, (%rbx,%r11,8)
jns 0xacb69
jmp 0xacbbb
movl %ebp, %eax
cmpl %r10d, %eax
setne %al
xorb $0x1, %bl
orb %al, %bl
je 0xacbc1
addq $0x28, %r14
jmp 0xacb4e
incl -0x40(%rsp)
movl %edx, %r9d
movl 0x38(%rsp), %ebx
movq -0x38(%rsp), %rdx
incq %rdi
incl %r15d
jmp 0xacafa
movl -0x3c(%rsp), %r15d
addl %edx, %r15d
movq -0x18(%rsp), %rdx
incl %edx
jmp 0xacab4
movl -0x40(%rsp), %eax
cvtsi2ss %rax, %xmm1
movl %r13d, %eax
cvtsi2ss %rax, %xmm2
mulss %xmm0, %xmm2
xorl %eax, %eax
ucomiss %xmm2, %xmm1
setae %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq