// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: calculate_pair_hap_similarity_advance(hap_candidates*, unsigned long*, unsigned int, unsigned int, ma_utg_t*, ma_utg_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, asg_t*, R_to_U*, ma_sub_t*, float, int, int, int, unsigned long, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*, hap_cov_t*, long long*, long long*, long long*, long long*, float*)
uint32_t calculate_pair_hap_similarity_advance(hap_candidates* hap_can, 
uint64_t* position_index, uint32_t xUid, uint32_t yUid, ma_utg_t* xReads, ma_utg_t* yReads,
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, asg_t *read_g, R_to_U* ruIndex, ma_sub_t *coverage_cut, 
float Hap_rate, int is_local, int max_hang, int min_ovlp, uint64_t cov_threshold, kvec_asg_arc_t_offset* u_buffer, 
kvec_t_i32_warp* tailIndex, kvec_t_i32_warp* prevIndex, hap_cov_t *cov, long long* r_x_pos_beg, long long* r_x_pos_end, 
long long* r_y_pos_beg, long long* r_y_pos_end, float *sim)
{
    uint32_t max_count = 0, min_count = 0, flag;
    uint32_t xLen = xReads->n, xIndex;
    uint32_t yLen = yReads->n, yIndex;
    uint32_t xLeftBeg, xLeftLen, yLeftBeg, yLeftLen;
    uint32_t xRightBeg, xRightLen, yRightBeg, yRightLen;
    double xLeftMatch = 0, xLeftTotal = 0, yLeftMatch = 0, yLeftTotal = 0;
    double xRightMatch = 0, xRightTotal = 0, yRightMatch = 0, yRightTotal = 0;
    asg_arc_t* arch = NULL;
    

    arch = &(hap_can->t);
    xIndex = (uint32_t)(position_index[arch->ul>>33]);
    yIndex = (uint32_t)(position_index[arch->v>>1]);
    
    if(hap_can->rev == 0)
    {
        xLeftBeg = 0; xLeftLen = xIndex; xRightBeg = xIndex; xRightLen = xLen - xRightBeg;
        yLeftBeg = 0; yLeftLen = yIndex; yRightBeg = yIndex; yRightLen = yLen - yRightBeg;
    }
    else
    {
        xLeftBeg = 0; xLeftLen = xIndex; xRightBeg = xIndex; xRightLen = xLen - xRightBeg;

        yLeftBeg = yIndex + 1; yLeftLen = yLen - yLeftBeg;
        yRightBeg = 0; yRightLen = yIndex + 1;
    }

    flag = Get_type(*hap_can);


    if(flag == XCY)
    {
        get_pair_hap_similarity(yReads->a, yLen, xUid, reverse_sources, read_g, ruIndex, 
        &yLeftMatch, &yLeftTotal);
        max_count = yLeftMatch;
        min_count = yLeftTotal;
    }
    else if(flag == YCX)
    {
        get_pair_hap_similarity(xReads->a, xLen, yUid, reverse_sources, read_g, ruIndex, 
        &xLeftMatch, &xLeftTotal);
        max_count = xLeftMatch;
        min_count = xLeftTotal;
    }
    else if(flag == X2Y)
    {
        get_pair_hap_similarity(yReads->a+yLeftBeg, yLeftLen, xUid, reverse_sources, read_g, ruIndex,
        &yLeftMatch, &yLeftTotal);
        get_pair_hap_similarity(xReads->a+xRightBeg, xRightLen, yUid, reverse_sources, read_g, ruIndex, 
        &xRightMatch, &xRightTotal);
        max_count = yLeftMatch + xRightMatch;
        min_count = yLeftTotal + xRightTotal;
    }
    else if(flag == Y2X)
    {
        get_pair_hap_similarity(xReads->a+xLeftBeg, xLeftLen, yUid, reverse_sources, read_g, ruIndex, 
        &xLeftMatch, &xLeftTotal);
        get_pair_hap_similarity(yReads->a+yRightBeg, yRightLen, xUid, reverse_sources, read_g, ruIndex,
        &yRightMatch, &yRightTotal);
        max_count = xLeftMatch + yRightMatch;
        min_count = xLeftTotal + yRightTotal;
    } else abort();

    hap_can->weight = hap_can->index_beg = 0;
    if(min_count == 0) return NON_PLOID;
    if((max_count > min_count*Hap_rate) || is_local)
    {
        long long r_x_interval_beg, r_x_interval_end, r_y_interval_beg, r_y_interval_end;
        uint64_t ploid_coverage = 0;

        ///for containment, don't need to do anything
        get_hap_alignment_boundary(xReads, yReads, flag, xLeftMatch, xLeftTotal, 
        yLeftMatch, yLeftTotal, xRightMatch, xRightTotal, yRightMatch, yRightTotal,
        xLeftBeg, xLeftLen, yLeftBeg, yLeftLen, xRightBeg, xRightLen, yRightBeg, yRightLen,
        xUid, yUid, Hap_rate, is_local, position_index, reverse_sources, read_g, ruIndex, 
        hap_can->rev, &r_x_interval_beg, &r_x_interval_end, &r_y_interval_beg, &r_y_interval_end);

        if(r_x_interval_beg < 0 || r_x_interval_end < 0 || r_y_interval_beg < 0 || r_y_interval_end < 0)
        {
            return NON_PLOID;
        }

        get_pair_hap_similarity(xReads->a + r_x_interval_beg, r_x_interval_end + 1 - r_x_interval_beg, 
        yUid, reverse_sources, read_g, ruIndex, &xLeftMatch, &xLeftTotal);
        if(xLeftMatch == 0 || xLeftTotal == 0 || (is_local == 0 && xLeftMatch <= xLeftTotal*Hap_rate))
        {
            return NON_PLOID;
        }
                
        
        hap_can->weight = xLeftMatch;
        hap_can->index_beg = xLeftTotal;
        hap_can->index_end = flag;
        hap_can->x_beg_pos = r_x_interval_beg;
        hap_can->x_end_pos = r_x_interval_end;
        hap_can->y_beg_pos = r_y_interval_beg;
        hap_can->y_end_pos = r_y_interval_end;
        hap_can->index_end = determine_hap_overlap_type_advance(hap_can, xReads, yReads,
        ruIndex, reverse_sources, coverage_cut, read_g, position_index, max_hang, min_ovlp, 
        xUid, yUid, u_buffer, tailIndex, prevIndex, r_x_pos_beg, r_x_pos_end, r_y_pos_beg, 
        r_y_pos_end);
        if(hap_can->index_end == XCY && yReads->len > (xReads->len*2)) return NON_PLOID;
        if(hap_can->index_end == YCX && xReads->len > (yReads->len*2)) return NON_PLOID;
        if(hap_can->index_end == (uint32_t)-1) return NON_PLOID;

        ploid_coverage = get_pair_purge_coverage(xReads, *r_x_pos_beg, *r_x_pos_end, 
        yReads, *r_y_pos_beg, *r_y_pos_end, hap_can->rev, read_g, cov);

        if(cov_threshold > 0 && ploid_coverage >= cov_threshold) return NON_PLOID;

        get_pair_hap_similarity_by_base(xReads, read_g, yUid, reverse_sources, ruIndex, 
        *r_x_pos_beg, *r_x_pos_end, &xLeftMatch, &xLeftTotal);
        (*sim) = (xLeftTotal== 0? 0:((double)xLeftMatch)/((double)xLeftTotal));

        // adjust_hap_overlaps_score(xReads, sim, &(hap_can->score), xUid, yUid, (*r_x_pos_beg), (*r_x_pos_end));

        if(xLeftMatch == 0 || xLeftTotal == 0 || (*sim) <= Hap_rate)
        {
            return NON_PLOID;
        }

        return PLOID;
    } 
	return NON_PLOID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %rbp
movq %r8, %rbx
movl 0x10(%r8), %eax
movl 0x10(%r9), %r10d
andq $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
movss %xmm0, 0x4(%rsp)
andq $0x0, 0x60(%rsp)
movl %ecx, %r9d
andq $0x0, 0x58(%rsp)
movq %rdx, 0x20(%rsp)
andq $0x0, 0xe0(%rsp)
andq $0x0, 0xd8(%rsp)
movq %rdi, %r12
andq $0x0, 0xd0(%rsp)
andq $0x0, 0xc8(%rsp)
movl 0x38(%rdi), %ecx
shrl %ecx
movl (%rsi,%rcx,8), %r13d
cmpb $0x0, 0x28(%rdi)
je 0xcc479
movl %r13d, %edi
incl %edi
movl %r10d, %r13d
subl %edi, %r13d
xorl %r15d, %r15d
movl %edi, %ecx
movq %rcx, 0x18(%rsp)
jmp 0xcc489
movl %r10d, %ecx
subl %r13d, %ecx
movq %rcx, 0x18(%rsp)
xorl %edi, %edi
movl %r13d, %r15d
movl 0x1c(%r12), %edx
cmpq $0x3, %rdx
ja 0xccbb5
movq 0x30(%r12), %rcx
shrq $0x21, %rcx
movq %rsi, 0x70(%rsp)
movq (%rsi,%rcx,8), %rsi
movq 0x138(%rsp), %r14
movq 0x130(%rsp), %r8
movq 0x128(%rsp), %rcx
movl %eax, %r11d
subl %esi, %r11d
movq %r11, 0x38(%rsp)
leaq 0x11f104(%rip), %r11    # 0x1eb5d8
movq %rdx, 0x68(%rsp)
movslq (%r11,%rdx,4), %rdx
addq %r11, %rdx
movq %rbx, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movq %r15, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
jmpq *%rdx
movl %esi, %r15d
movl %edi, %edi
shlq $0x3, %rdi
addq 0x18(%rbp), %rdi
leaq 0x58(%rsp), %rax
leaq 0x60(%rsp), %r10
movl %r13d, %esi
movq 0x20(%rsp), %rdx
movq %rbp, %r14
movq %r9, %rbp
movq 0x138(%rsp), %r9
pushq %rax
pushq %r10
callq 0xc855e
popq %rax
popq %rcx
shlq $0x3, %r15
addq 0x18(%rbx), %r15
leaq 0xd8(%rsp), %rax
leaq 0xe0(%rsp), %rbx
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movl %ebp, %edx
movq %r14, %rbp
movq 0x138(%rsp), %r14
movq 0x128(%rsp), %rcx
movq 0x130(%rsp), %r8
movq %r14, %r9
pushq %rax
movq %rax, %r15
pushq %rbx
callq 0xc855e
popq %rax
popq %rcx
leaq 0x60(%rsp), %rax
movsd (%rax), %xmm2
movsd (%rbx), %xmm1
movapd %xmm2, %xmm4
addsd %xmm1, %xmm4
leaq 0x58(%rsp), %rax
movsd (%rax), %xmm0
movsd (%r15), %xmm3
movapd %xmm0, %xmm5
addsd %xmm3, %xmm5
cvttsd2si %xmm1, %rdi
cvttsd2si %xmm3, %rsi
xorpd %xmm1, %xmm1
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
xorpd %xmm3, %xmm3
jmp 0xcc72f
movq 0x18(%rbp), %rdi
leaq 0x58(%rsp), %rbx
leaq 0x60(%rsp), %r15
movl %r10d, %esi
movq 0x20(%rsp), %rdx
movq %r14, %r9
pushq %rbx
pushq %r15
callq 0xc855e
popq %rax
popq %rcx
movsd (%r15), %xmm2
xorpd %xmm1, %xmm1
movq $0x0, 0x30(%rsp)
movsd (%rbx), %xmm0
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
xorl %edi, %edi
xorpd %xmm3, %xmm3
movapd %xmm0, %xmm5
movapd %xmm2, %xmm4
jmp 0xcc72f
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rbx
leaq 0x10(%rsp), %r15
movl %eax, %esi
movl %r9d, %edx
movq %r14, %r9
pushq %rbx
pushq %r15
callq 0xc855e
popq %rax
popq %rcx
movsd (%r15), %xmm3
xorpd %xmm0, %xmm0
movq $0x0, 0x30(%rsp)
movsd (%rbx), %xmm1
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
xorl %edi, %edi
xorpd %xmm2, %xmm2
movapd %xmm1, %xmm5
movapd %xmm3, %xmm4
jmp 0xcc72f
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rax
leaq 0x10(%rsp), %r10
movl %r9d, %edx
movq %r14, %r9
pushq %rax
pushq %r10
movq %rcx, %rbx
callq 0xc855e
popq %rax
popq %rcx
movl %r15d, %edi
shlq $0x3, %rdi
addq 0x18(%rbp), %rdi
leaq 0xc8(%rsp), %rax
leaq 0xd0(%rsp), %r15
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %rcx
movq 0x130(%rsp), %r8
movq %r14, %r9
pushq %rax
movq %rax, %rbx
pushq %r15
callq 0xc855e
popq %rax
popq %rcx
leaq 0x10(%rsp), %rax
movsd (%rax), %xmm3
movsd (%r15), %xmm0
movapd %xmm3, %xmm4
addsd %xmm0, %xmm4
leaq 0x8(%rsp), %rax
movsd (%rax), %xmm1
movsd (%rbx), %xmm2
movapd %xmm1, %xmm5
addsd %xmm2, %xmm5
cvttsd2si %xmm0, %rax
movq %rax, 0x28(%rsp)
cvttsd2si %xmm2, %rax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %edi, %edi
xorpd %xmm2, %xmm2
andl $0x0, 0x18(%r12)
andq $0x0, (%r12)
cvttsd2si %xmm5, %rcx
pushq $0x1
popq %r15
testl %ecx, %ecx
je 0xcc8bd
movl 0x148(%rsp), %edx
testl %edx, %edx
jne 0xcc778
cvttsd2si %xmm4, %rdx
movl %edx, %edx
cvtsi2ss %rdx, %xmm4
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm5
mulss 0x4(%rsp), %xmm5
ucomiss %xmm5, %xmm4
jbe 0xcc8bd
cvttsd2si %xmm3, %rcx
cvttsd2si %xmm1, %r8
cvttsd2si %xmm2, %r9
cvttsd2si %xmm0, %rdx
movq %rdx, 0x90(%rsp)
movzbl 0x28(%r12), %ebx
subq $0x8, %rsp
leaq 0xb0(%rsp), %r15
leaq 0xb8(%rsp), %r11
leaq 0xc0(%rsp), %r10
leaq 0xc8(%rsp), %rax
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq 0x58(%rsp), %rdi
movq %rbp, 0x80(%rsp)
movq %rbp, %rsi
movq 0x70(%rsp), %rdx
movss 0xc(%rsp), %xmm0
pushq %r15
pushq %r11
pushq %r10
pushq %rax
pushq %rbx
pushq %r14
movq 0x168(%rsp), %rbx
pushq %rbx
movq 0x168(%rsp), %rbp
pushq %rbp
pushq 0xb8(%rsp)
movl 0x198(%rsp), %ebx
pushq %rbx
movq 0xa0(%rsp), %rbx
pushq %rbx
pushq 0x80(%rsp)
pushq 0x80(%rsp)
pushq 0x110(%rsp)
pushq 0xb0(%rsp)
movq 0x118(%rsp), %r15
pushq %r15
pushq %r13
pushq 0xd0(%rsp)
pushq %r15
pushq $0x0
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0x138(%rsp)
pushq 0x148(%rsp)
pushq 0x158(%rsp)
callq 0xc8de8
addq $0xd0, %rsp
leaq 0xc0(%rsp), %rax
movq (%rax), %r13
leaq 0xb8(%rsp), %rax
movq (%rax), %rcx
movq %r13, %rax
orq %rcx, %rax
leaq 0xb0(%rsp), %rdx
movq (%rdx), %rdx
orq %rdx, %rax
leaq 0xa8(%rsp), %rsi
movq (%rsi), %rsi
pushq $0x1
popq %r15
orq %rsi, %rax
jns 0xcc8d2
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r9
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
leaq (,%r13,8), %rdi
movq 0x50(%rsp), %rax
addq 0x18(%rax), %rdi
movq %rcx, 0x18(%rsp)
movl %ecx, %esi
subl %r13d, %esi
incl %esi
leaq 0x8(%rsp), %rax
leaq 0x10(%rsp), %r14
movl %ebx, %edx
movq %rbp, %rcx
movq 0x130(%rsp), %r8
pushq %rax
pushq %r14
callq 0xc855e
popq %rax
popq %rcx
movsd (%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0xcc92e
jnp 0xcc8bd
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jne 0xcc93c
jnp 0xcc8bd
movl 0x148(%rsp), %eax
testl %eax, %eax
jne 0xcc95b
cvtss2sd 0x4(%rsp), %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0xcc8bd
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x11692d(%rip), %xmm1   # 0x1e3298
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, (%r12)
cvttsd2si %xmm0, %rax
movl %eax, 0x18(%r12)
movq 0x178(%rsp), %r11
movq 0x68(%rsp), %rax
movl %eax, 0x1c(%r12)
movq 0x170(%rsp), %rax
movl %r13d, 0x8(%r12)
movq 0x168(%rsp), %r13
movq 0x18(%rsp), %rcx
movl %ecx, 0xc(%r12)
movl 0x158(%rsp), %r14d
movq 0x38(%rsp), %rcx
movl %ecx, 0x10(%r12)
movl 0x150(%rsp), %r10d
movq 0x40(%rsp), %rcx
movl %ecx, 0x14(%r12)
movq 0x140(%rsp), %r9
subq $0x8, %rsp
movq %r12, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
movq 0x80(%rsp), %rbp
movq %rbp, %rdx
movq 0x140(%rsp), %rcx
movq 0x130(%rsp), %r8
pushq 0x1a8(%rsp)
pushq 0x1a8(%rsp)
pushq 0x1a8(%rsp)
pushq 0x1a8(%rsp)
pushq %r11
pushq %rax
pushq %r13
pushq 0x88(%rsp)
pushq 0x68(%rsp)
pushq %r14
pushq %r10
pushq 0xd0(%rsp)
pushq 0x198(%rsp)
callq 0xcbd98
addq $0x70, %rsp
movl %eax, 0x1c(%r12)
cmpl $-0x1, %eax
je 0xcc8bd
cmpl $0x3, %eax
je 0xcca8c
cmpl $0x2, %eax
movq 0x130(%rsp), %r10
jne 0xccaa8
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%rbp), %eax
movl (%rbx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jg 0xcc8bd
jmp 0xccaa8
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%rbx), %eax
movl (%rbp), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
movq 0x130(%rsp), %r10
jg 0xcc8bd
movq 0x160(%rsp), %r13
movq 0x188(%rsp), %rax
movq (%rax), %r14
movq %rbp, %rcx
movq %rbx, %rbp
movq 0x190(%rsp), %rax
movq (%rax), %rbx
movq 0x198(%rsp), %rax
movq (%rax), %r8
movq 0x1a0(%rsp), %rax
movq (%rax), %r9
movzbl 0x28(%r12), %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
pushq 0x188(%rsp)
pushq %r10
pushq %rax
movq %r10, %r12
callq 0xc814e
addq $0x20, %rsp
decq %r13
cmpq %rax, %r13
jb 0xcc8bd
movq 0x1a8(%rsp), %r15
subq $0x8, %rsp
movq %rbp, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq 0x140(%rsp), %r8
movq %r14, %r9
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
pushq %rbx
callq 0xc83a5
addq $0x20, %rsp
movsd 0x8(%rsp), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm3, %xmm0
setnp %cl
sete %al
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
jne 0xccb81
jp 0xccb81
xorpd %xmm2, %xmm2
andb %cl, %al
cvtsd2ss %xmm2, %xmm2
movss %xmm2, (%r15)
ucomisd %xmm1, %xmm0
setnp %cl
sete %dl
andb %cl, %dl
orb %al, %dl
movss 0x4(%rsp), %xmm0
ucomiss %xmm2, %xmm0
setae %al
orb %dl, %al
movzbl %al, %r15d
andl $0x1, %r15d
jmp 0xcc8bd
callq 0x20f0