// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x143be4
movl $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x40(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x60(%rbp)
movl -0x44(%rbp), %eax
subl -0x60(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x143c30
movl $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x40(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x44(%rbp), %eax
subl -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x54(%rbp), %eax
ja 0x143c49
movl -0x4c(%rbp), %eax
movl %eax, -0xac(%rbp)
jmp 0x143c52
movl -0x54(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x64(%rbp), %eax
ja 0x143c71
movl -0x5c(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0x143c7a
movl -0x64(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb0(%rbp), %eax
movl -0xb4(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x143c9d
movl $0x1, -0x4(%rbp)
jmp 0x143f1f
movl -0x38(%rbp), %eax
cvtsi2ss %rax, %xmm1
movl -0x3c(%rbp), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x143cc3
movl $0x1, -0x4(%rbp)
jmp 0x143f1f
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x48(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movl -0x4c(%rbp), %esi
movl -0x1c(%rbp), %edx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
leaq -0x70(%rbp), %r10
leaq -0x78(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x143980
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x50(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movl -0x54(%rbp), %esi
movl -0x18(%rbp), %edx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
leaq -0x80(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x143980
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x58(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movl -0x5c(%rbp), %esi
movl -0x1c(%rbp), %edx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x98(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x143980
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x60(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movl -0x64(%rbp), %esi
movl -0x18(%rbp), %edx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
leaq -0xa0(%rbp), %r10
leaq -0xa8(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x143980
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movsd -0x70(%rbp), %xmm0
divsd -0x78(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
divsd -0x88(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x143e19
movsd -0x70(%rbp), %xmm1
movl -0x38(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x38(%rbp)
movsd -0x78(%rbp), %xmm1
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
jmp 0x143e4e
movsd -0x80(%rbp), %xmm1
movl -0x38(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x38(%rbp)
movsd -0x88(%rbp), %xmm1
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
movsd -0x90(%rbp), %xmm0
divsd -0x98(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
divsd -0xa8(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x143eae
movsd -0x90(%rbp), %xmm1
movl -0x38(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x38(%rbp)
movsd -0x98(%rbp), %xmm1
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
jmp 0x143ee6
movsd -0xa0(%rbp), %xmm1
movl -0x38(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x38(%rbp)
movsd -0xa8(%rbp), %xmm1
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x143ef5
movl $0x1, -0x4(%rbp)
jmp 0x143f1f
movl -0x38(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x3c(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x34(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x143f18
movl $0x0, -0x4(%rbp)
jmp 0x143f1f
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)