// File: /workspace/github_cpp_makefile_star2_O1/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 0xea7d3
movl %r13d, %r14d
incl %r14d
subl %r14d, %ebp
xorl %esi, %esi
movl %ebp, %r13d
movl %r14d, %ebp
jmp 0xea7dc
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
movl $0x1, %eax
addl %edx, %ecx
je 0xeaa35
movl 0xb8(%rsp), %edx
cvtsi2ss %rdx, %xmm2
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm1
mulss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
jae 0xeaa35
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 0xea606
addq $0x10, %rsp
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), %r13
movq %r13, %rcx
movq %r12, %r14
movq %r12, %r8
movq 0xb0(%rsp), %r12
movq %r12, %r9
pushq %rax
pushq %r10
callq 0xea606
addq $0x10, %rsp
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 %r13, %rcx
movq %r14, %r8
movq %r12, %r15
movq %r12, %r9
pushq %rax
pushq %r10
callq 0xea606
addq $0x10, %rsp
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 %r13, %rcx
movq %r14, %r8
movq %r15, %r9
pushq %r12
pushq %rbx
callq 0xea606
addq $0x10, %rsp
leaq 0x50(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm3
movapd %xmm0, %xmm4
divsd %xmm3, %xmm4
leaq 0x40(%rsp), %rax
movsd (%rax), %xmm2
leaq 0x38(%rsp), %rax
movsd (%rax), %xmm5
movapd %xmm2, %xmm1
divsd %xmm5, %xmm1
cmpnlesd %xmm4, %xmm1
andpd %xmm1, %xmm5
movapd %xmm1, %xmm4
andnpd %xmm3, %xmm4
orpd %xmm5, %xmm4
xorpd %xmm3, %xmm3
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %rax
leaq 0x30(%rsp), %rcx
movsd (%rcx), %xmm6
leaq 0x28(%rsp), %rcx
movsd (%rcx), %xmm7
movapd %xmm6, %xmm8
divsd %xmm7, %xmm8
movsd (%rbx), %xmm5
movsd (%r12), %xmm9
movapd %xmm5, %xmm4
divsd %xmm9, %xmm4
movl %eax, %eax
cvtsi2sd %rax, %xmm10
cmpnlesd %xmm8, %xmm4
andpd %xmm4, %xmm9
movapd %xmm4, %xmm8
andnpd %xmm7, %xmm8
orpd %xmm9, %xmm8
addsd %xmm10, %xmm8
cvttsd2si %xmm8, %rax
testl %eax, %eax
je 0xeaa30
andpd %xmm4, %xmm5
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %rcx
andnpd %xmm6, %xmm4
movl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
orpd %xmm5, %xmm4
addsd %xmm4, %xmm0
cvttsd2si %xmm0, %rcx
movl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x10(%rsp), %xmm1
xorl %eax, %eax
ucomiss %xmm1, %xmm0
setbe %al
jmp 0xeaa35
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq