// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_hap_similarity(unsigned int*, unsigned int, unsigned int, ma_hit_t_alloc*, asg_t*, R_to_U*, double*, double*)
void get_hap_similarity(uint32_t* list, uint32_t Len, uint32_t target_uId, 
ma_hit_t_alloc* reverse_sources, asg_t *read_g, R_to_U* ruIndex, 
double* Match, double* Total)
{
    #define CUTOFF_THRES 100
    uint32_t i, j, qn, tn, is_Unitig, uId, min_count = 0, max_count = 0, cutoff = 0;;
    for (i = 0; i < Len; i++)
    {
        if(cutoff > CUTOFF_THRES)
        {
            max_count = 0;
            min_count = Len;
        }
        qn = list[i]>>1;
        if(reverse_sources[qn].length > 0) min_count++;
        for (j = 0; j < reverse_sources[qn].length; j++)
        {
            tn = Get_tn(reverse_sources[qn].buffer[j]);
            if(read_g->seq[tn].del == 1)
            {
                get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
            }


            get_R_to_U(ruIndex, tn, &uId, &is_Unitig);
            if(uId!=(uint32_t)-1 && is_Unitig == 1 && uId == target_uId)
            {
                max_count++;
                break;
            }
        }

        //means no match
        if(j == reverse_sources[qn].length)
        {
            cutoff++;
        }
        else
        {
            cutoff = 0;
        }
    }

    (*Match) = max_count;
    (*Total) = min_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r8, -0x18(%rsp)
movq %rdi, -0x8(%rsp)
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdi
testl %esi, %esi
je 0xec181
movl %esi, %eax
movq %rax, -0x10(%rsp)
xorl %r8d, %r8d
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %rcx, -0x20(%rsp)
movl %esi, -0x28(%rsp)
cmpl $0x65, %r12d
cmovael %esi, %ebp
cmovael %r8d, %r11d
movq -0x8(%rsp), %rax
movl (%rax,%r14,4), %eax
shrl %eax
leaq (%rax,%rax,2), %rdi
movl 0xc(%rcx,%rdi,8), %ebx
xorl %eax, %eax
testq %rbx, %rbx
setne %r10b
je 0xec130
movl %r11d, -0x24(%rsp)
leaq (%rcx,%rdi,8), %rdi
movq (%rdi), %r8
movq -0x18(%rsp), %rsi
movq 0x20(%rsi), %rdi
movq (%r9), %r11
addq $0xc, %r8
xorl %r13d, %r13d
movl (%r8), %r15d
cmpl $0x0, (%rdi,%r15,8)
js 0xec0f5
cmpq %r15, %r11
jbe 0xec10e
movq 0x8(%r9), %rsi
movl (%rsi,%r15,4), %r15d
cmpl $-0x1, %r15d
je 0xec10e
movl %r15d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
testl %r15d, %r15d
setns %r15b
cmpl %edx, %esi
setne %sil
orb %r15b, %sil
jne 0xec10e
jmp 0xec135
cmpq %r15, %r11
jbe 0xec10e
movq 0x8(%r9), %rsi
movslq (%rsi,%r15,4), %r15
testq %r15, %r15
js 0xec10e
cmpl $0x0, (%rdi,%r15,8)
jns 0xec0c5
incq %r13
addq $0x28, %r8
cmpq %r13, %rbx
jne 0xec0bb
movl %ebx, %r13d
movq -0x20(%rsp), %rcx
movl -0x28(%rsp), %esi
xorl %r8d, %r8d
movl -0x24(%rsp), %r11d
jmp 0xec149
xorl %r13d, %r13d
jmp 0xec149
movl -0x24(%rsp), %r11d
incl %r11d
movq -0x20(%rsp), %rcx
movl -0x28(%rsp), %esi
xorl %r8d, %r8d
movb %r10b, %al
addl %eax, %ebp
incl %r12d
cmpl %ebx, %r13d
cmovnel %r8d, %r12d
incq %r14
cmpq -0x10(%rsp), %r14
jne 0xec06f
movl %r11d, %eax
cvtsi2sd %rax, %xmm1
movl %ebp, %eax
cvtsi2sd %rax, %xmm0
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdi
jmp 0xec189
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm1, (%rdi)
movsd %xmm0, (%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq