// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: unitig_simi(unsigned int, unsigned int, ma_ug_t*, ma_hit_t_alloc*, R_to_U*, unsigned char*)
uint32_t unitig_simi(uint32_t x, uint32_t y, ma_ug_t* ug, ma_hit_t_alloc* reverse_sources, 
R_to_U* ruIndex, uint8_t* is_r_het)
{
    uint32_t k, j, uId, tn, is_Unitig, rId, ref_unitig, min_count, max_count, n_het, n_hom;
    ma_utg_t *nsu_x = NULL, *nsu_y = NULL, *nsu_query = NULL;
    nsu_x = &(ug->u.a[x]);
    nsu_y = &(ug->u.a[y]);

    if(nsu_x->n == 0 || nsu_y->n == 0) return UNAVAILABLE;

    if(nsu_x->n <= nsu_y->n)
    {
        nsu_query = nsu_x;
        ref_unitig = y;
    }
    else
    {
        nsu_query = nsu_y;
        ref_unitig = x;
    }
     
    min_count = max_count = n_het = n_hom = 0;
    for (k = 0; k < nsu_query->n; k++)
    {
        rId = nsu_query->a[k]>>33;
        if(reverse_sources[rId].length >= 0) min_count++;
        if((is_r_het[rId] & C_HET) || (is_r_het[rId] & P_HET)) n_het++;
        n_hom++;

        for (j = 0; j < reverse_sources[rId].length; j++)
        {
            tn = Get_tn(reverse_sources[rId].buffer[j]);
            get_R_to_U(ruIndex, tn, &uId, &is_Unitig);
            if(uId==(uint32_t)-1) continue;
            ///contained read
            if(is_Unitig == 0) get_R_to_U(ruIndex, uId, &uId, &is_Unitig);
            if(uId == (uint32_t)-1 || is_Unitig == 0) continue;
            if(uId == ref_unitig)
            {
                max_count++;
                break;
            }
        }
    }
    
    if(min_count == 0) return UNAVAILABLE;
    if(max_count > min_count*asm_opt.purge_simi_thres && n_het >= n_hom*HET_HOM_RATE) return PLOID;
	return NON_PLOID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r9, -0x8(%rsp)
movq 0x10(%rdx), %rdx
movl %edi, %eax
leaq (%rax,%rax,4), %r10
movl 0x10(%rdx,%r10,8), %r11d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %r11d, %r11d
je 0xd60d4
movl %esi, %r9d
leaq (%r9,%r9,4), %r9
leaq (%rdx,%r9,8), %rbx
movl 0x10(%rbx), %r9d
testl %r9d, %r9d
je 0xd60d4
cmpl %r9d, %r11d
cmoval %edi, %esi
leaq (%rdx,%r10,8), %rdi
cmovaq %rbx, %rdi
movl 0x10(%rdi), %r9d
testq %r9, %r9
je 0xd60d4
movq 0x18(%rdi), %rdi
xorl %r10d, %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0x0, -0xc(%rsp)
movq (%rdi,%r10,8), %rdx
shrq $0x21, %rdx
leaq (%rdx,%rdx,2), %rbx
movq -0x8(%rsp), %r14
movb (%r14,%rdx), %dl
andb $0x3, %dl
cmpb $0x1, %dl
sbbl $-0x1, %eax
movl 0xc(%rcx,%rbx,8), %edx
testq %rdx, %rdx
je 0xd6077
leaq (%rcx,%rbx,8), %rbx
movq (%rbx), %r14
movq (%r8), %r15
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %r12
xorl %r13d, %r13d
movl 0xc(%r14,%r13), %edx
cmpq %rdx, %r15
jbe 0xd606e
movq 0x8(%r8), %rbx
movl (%rbx,%rdx,4), %ebp
cmpq %r11, %rbp
je 0xd606e
movl %ebp, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
testl %ebp, %ebp
js 0xd6064
cmpq %rdx, %r15
jbe 0xd606e
movl (%rbx,%rdx,4), %ebx
cmpl $-0x1, %ebx
je 0xd606e
movl %ebx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shrl $0x1f, %ebx
jmp 0xd6066
movb $0x1, %bl
testb %bl, %bl
je 0xd606e
cmpl %esi, %edx
je 0xd6085
addq $0x28, %r13
cmpq %r13, %r12
jne 0xd6028
incq %r10
cmpq %r9, %r10
jne 0xd5fec
jmp 0xd608b
incl -0xc(%rsp)
jmp 0xd6077
movl -0xc(%rsp), %ecx
cvtsi2ss %rcx, %xmm0
cvtsi2ss %r9, %xmm1
leaq 0x1bf6b8(%rip), %rcx    # 0x295758
mulss 0x16c(%rcx), %xmm1
ucomiss %xmm1, %xmm0
jbe 0xd60cf
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
mulsd 0x15b4d9(%rip), %xmm1   # 0x2315a0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
jae 0xd60d4
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq