// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: calculate_hap_similarity(Hap_Align*, unsigned int, unsigned int, unsigned int, kvec_t_u32_warp*, kvec_t_u32_warp*, ma_hit_t_alloc*, asg_t*, R_to_U*, float, unsigned int)
uint32_t calculate_hap_similarity(Hap_Align* p, uint32_t dir, uint32_t xCid, uint32_t yCid,
kvec_t_u32_warp* x_vecs, kvec_t_u32_warp* y_vecs, ma_hit_t_alloc* reverse_sources, 
asg_t *read_g, R_to_U* ruIndex, float Hap_rate, uint32_t seedOcc)
{
    uint32_t max_count = 0, min_count = 0;
    uint32_t xLen = x_vecs->a.n;
    uint32_t yLen = y_vecs->a.n;
    uint32_t xLeftBeg, xLeftLen, yLeftBeg, yLeftLen;
    uint32_t xRightBeg, xRightLen, yRightBeg, yRightLen;
    if(dir == 0)
    {
        xLeftBeg = 0; xLeftLen = p->q_pos; xRightBeg = p->q_pos; xRightLen = xLen - xRightBeg;
        yLeftBeg = 0; yLeftLen = p->t_pos; yRightBeg = p->t_pos; yRightLen = yLen - yRightBeg;
    }
    else
    {
        xLeftBeg = 0; xLeftLen = p->q_pos; xRightBeg = p->q_pos; xRightLen = xLen - xRightBeg;

        yLeftBeg = p->t_pos + 1; yLeftLen = yLen - yLeftBeg;
        yRightBeg = 0; yRightLen = p->t_pos + 1;
    }



    max_count = seedOcc;
    min_count = MIN(xLeftLen, yLeftLen) + MIN(xRightLen, yRightLen);
    if(min_count == 0) return NON_PLOID;
    if(max_count <= min_count*Hap_rate) return NON_PLOID;



    

    double xLeftMatch, xLeftTotal, yLeftMatch, yLeftTotal;
    double xRightMatch, xRightTotal, yRightMatch, yRightTotal;

    get_hap_similarity(x_vecs->a.a+xLeftBeg, xLeftLen, yCid, reverse_sources, read_g, ruIndex, 
    &xLeftMatch, &xLeftTotal);
    get_hap_similarity(y_vecs->a.a+yLeftBeg, yLeftLen, xCid, reverse_sources, read_g, ruIndex,
    &yLeftMatch, &yLeftTotal);

    get_hap_similarity(x_vecs->a.a+xRightBeg, xRightLen, yCid, reverse_sources, read_g, ruIndex, 
    &xRightMatch, &xRightTotal);
    get_hap_similarity(y_vecs->a.a+yRightBeg, yRightLen, xCid, reverse_sources, read_g, ruIndex,
    &yRightMatch, &yRightTotal);

    max_count = min_count = 0;
    if((xLeftMatch/xLeftTotal) >= (yLeftMatch/yLeftTotal))
    {
        max_count += xLeftMatch;
        min_count += xLeftTotal;
    }
    else
    {
        max_count += yLeftMatch;
        min_count += yLeftTotal;
    }

    if((xRightMatch/xRightTotal) >= (yRightMatch/yRightTotal))
    {
        max_count += xRightMatch;
        min_count += xRightTotal;
    }
    else
    {
        max_count += yRightMatch;
        min_count += yRightTotal;
    }

    if(min_count == 0) return NON_PLOID;
    if(max_count > min_count*Hap_rate) return PLOID;
	return NON_PLOID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r10d
movl %edx, 0x4(%rsp)
movl (%r8), %r15d
movl (%r9), %ebp
movl 0x4(%rdi), %r13d
testl %esi, %esi
je 0xad2b9
movl %r13d, %r14d
incl %r14d
subl %r14d, %ebp
xorl %esi, %esi
movl %ebp, %r13d
movl %r14d, %ebp
jmp 0xad2c2
subl %r13d, %ebp
xorl %r14d, %r14d
movq %r13, %rsi
movl (%rdi), %ebx
subl %ebx, %r15d
cmpl %r13d, %ebx
movl %r13d, %edx
cmovbl %ebx, %edx
cmpl %ebp, %r15d
movl %ebp, %ecx
cmovbl %r15d, %ecx
pushq $0x1
popq %rax
addl %edx, %ecx
je 0xad530
movl 0xb8(%rsp), %edx
cvtsi2ss %rdx, %xmm2
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm1
mulss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
jae 0xad530
movss %xmm0, 0x10(%rsp)
movq 0xa8(%rsp), %r12
movq 0xa0(%rsp), %rcx
movq 0x10(%r8), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x48(%rsp), %rax
movq %r9, 0x8(%rsp)
leaq 0x50(%rsp), %r11
movq %rsi, 0x20(%rsp)
movl %ebx, %esi
movl %r10d, %edx
movq %r12, %r8
movq 0xb0(%rsp), %r9
pushq %rax
pushq %r11
movl %r10d, 0x24(%rsp)
callq 0xad145
popq %rax
popq %rcx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
leaq (%rax,%r14,4), %rdi
leaq 0x38(%rsp), %rax
leaq 0x40(%rsp), %r10
movl %r13d, %esi
movl 0x4(%rsp), %edx
movq 0xa0(%rsp), %r14
movq %r14, %rcx
movq %r12, %r13
movq %r12, %r8
movq 0xb0(%rsp), %r12
movq %r12, %r9
pushq %rax
pushq %r10
callq 0xad145
popq %rax
popq %rcx
movq 0x18(%rsp), %rax
leaq (%rax,%rbx,4), %rdi
leaq 0x28(%rsp), %rax
leaq 0x30(%rsp), %r10
movl %r15d, %esi
movl 0x14(%rsp), %edx
movq %r14, %rcx
movq %r13, %r8
movq %r12, %r15
movq %r12, %r9
pushq %rax
pushq %r10
callq 0xad145
popq %rax
popq %rcx
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
leaq 0x58(%rsp), %r12
leaq 0x60(%rsp), %rbx
movl %ebp, %esi
movl 0x4(%rsp), %edx
movq %r14, %rcx
movq %r13, %r8
movq %r15, %r9
pushq %r12
pushq %rbx
callq 0xad145
addq $0x10, %rsp
leaq 0x50(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x40(%rsp), %rax
movsd (%rax), %xmm0
movapd %xmm1, %xmm3
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
leaq 0x38(%rsp), %rax
movsd (%rax), %xmm4
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm5
movapd %xmm5, %xmm2
unpcklpd %xmm4, %xmm2            # xmm2 = xmm2[0],xmm4[0]
divpd %xmm2, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
movapd %xmm2, %xmm6
cmpnlesd %xmm3, %xmm6
andpd %xmm6, %xmm4
andnpd %xmm5, %xmm6
orpd %xmm4, %xmm6
xorpd %xmm5, %xmm5
addsd %xmm5, %xmm6
cvttsd2si %xmm6, %rax
leaq 0x30(%rsp), %rcx
movsd (%rcx), %xmm6
movsd (%rbx), %xmm4
movapd %xmm6, %xmm8
unpcklpd %xmm4, %xmm8            # xmm8 = xmm8[0],xmm4[0]
movsd (%r12), %xmm9
leaq 0x28(%rsp), %rcx
movsd (%rcx), %xmm10
movapd %xmm10, %xmm7
unpcklpd %xmm9, %xmm7            # xmm7 = xmm7[0],xmm9[0]
divpd %xmm7, %xmm8
movapd %xmm8, %xmm7
unpckhpd %xmm8, %xmm7            # xmm7 = xmm7[1],xmm8[1]
movl %eax, %eax
cvtsi2sd %rax, %xmm11
movapd %xmm7, %xmm12
cmpnlesd %xmm8, %xmm12
andpd %xmm12, %xmm9
andnpd %xmm10, %xmm12
orpd %xmm9, %xmm12
addsd %xmm11, %xmm12
cvttsd2si %xmm12, %rax
testl %eax, %eax
je 0xad52d
cmpnlesd %xmm8, %xmm7
andpd %xmm7, %xmm4
cmpnlesd %xmm3, %xmm2
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
addsd %xmm5, %xmm2
cvttsd2si %xmm2, %rcx
andnpd %xmm6, %xmm7
movl %ecx, %ecx
cvtsi2sd %rcx, %xmm0
orpd %xmm4, %xmm7
addsd %xmm7, %xmm0
cvttsd2si %xmm0, %rcx
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
mulss 0x10(%rsp), %xmm1
xorl %eax, %eax
ucomiss %xmm1, %xmm0
setbe %al
jmp 0xad530
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq