// File: /workspace/github_cpp_makefile_star2_O3/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
movss %xmm0, 0xc(%rsp)
movq %r9, %r14
movq %r8, %r11
movl %ecx, %r10d
movq %rdx, 0x38(%rsp)
movq %rsi, %rdx
movq %rdi, %r12
movl 0x10(%r8), %esi
movl 0x10(%r9), %eax
xorl %r13d, %r13d
movq %r13, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq %r13, 0x60(%rsp)
movq %r13, 0x48(%rsp)
movq %r13, 0xe0(%rsp)
movq %r13, 0x88(%rsp)
movq %r13, 0xd8(%rsp)
movq %r13, 0x80(%rsp)
movl 0x38(%rdi), %ecx
shrl %ecx
movl (%rdx,%rcx,8), %ebx
cmpb $0x0, 0x28(%rdi)
je 0x10ffef
movl %ebx, %ecx
incl %ecx
movl %eax, %ebx
subl %ecx, %ebx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
jmp 0x110004
movl %eax, %ecx
subl %ebx, %ecx
movq %rcx, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movl %ebx, %r13d
movl 0x1c(%r12), %edi
cmpq $0x3, %rdi
ja 0x11073d
movq 0x30(%r12), %rcx
shrq $0x21, %rcx
movq %rdx, 0x70(%rsp)
movq (%rdx,%rcx,8), %r15
movq 0x138(%rsp), %r9
movq 0x130(%rsp), %r8
movq 0x128(%rsp), %rbp
movl %esi, %ecx
subl %r15d, %ecx
movq %rcx, 0x78(%rsp)
leaq 0x1296aa(%rip), %rcx    # 0x2396f8
movq %rdi, 0x68(%rsp)
movslq (%rcx,%rdi,4), %rdx
addq %rcx, %rdx
movq %r10, 0x50(%rsp)
movq %r11, 0x58(%rsp)
movq %r15, 0xb0(%rsp)
jmpq *%rdx
movl %r15d, %r15d
movl 0x30(%rsp), %edi
shlq $0x3, %rdi
addq 0x18(%r14), %rdi
leaq 0x48(%rsp), %rax
movl %ebx, %esi
movq 0x38(%rsp), %rdx
movq %rbp, %rcx
movq %r14, 0x20(%rsp)
movq %r11, %r14
movq %r10, %rbp
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
callq 0x10ac64
addq $0x10, %rsp
shlq $0x3, %r15
addq 0x18(%r14), %r15
leaq 0x88(%rsp), %rax
leaq 0xe0(%rsp), %r10
movq %r15, %rdi
movq 0x78(%rsp), %rsi
movl %ebp, %edx
movq 0x128(%rsp), %rbp
movq 0x20(%rsp), %r14
movq %rbp, %rcx
movq 0x130(%rsp), %r8
movq 0x138(%rsp), %r9
pushq %rax
pushq %r10
movq %r10, %r15
callq 0x10ac64
addq $0x10, %rsp
leaq 0x60(%rsp), %rax
movsd (%rax), %xmm2
movsd (%r15), %xmm1
movapd %xmm2, %xmm4
addsd %xmm1, %xmm4
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x88(%rsp), %rax
movsd (%rax), %xmm3
movapd %xmm0, %xmm5
addsd %xmm3, %xmm5
cvttsd2si %xmm1, %r8
cvttsd2si %xmm3, %rsi
xorpd %xmm1, %xmm1
movq $0x0, 0x40(%rsp)
xorl %r9d, %r9d
xorpd %xmm3, %xmm3
jmp 0x1102bc
movq 0x18(%r14), %rdi
leaq 0x48(%rsp), %r10
leaq 0x60(%rsp), %r15
movl %eax, %esi
movq 0x38(%rsp), %rdx
movq %rbp, %rcx
pushq %r10
pushq %r15
callq 0x10ac64
addq $0x10, %rsp
movsd (%r15), %xmm2
xorpd %xmm1, %xmm1
movq $0x0, 0x40(%rsp)
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm0
xorl %r9d, %r9d
xorl %esi, %esi
xorl %r8d, %r8d
xorpd %xmm3, %xmm3
movapd %xmm0, %xmm5
movapd %xmm2, %xmm4
jmp 0x1102bc
movq 0x18(%r11), %rdi
leaq 0x10(%rsp), %rax
leaq 0x18(%rsp), %r15
movl %r10d, %edx
movq %rbp, %rcx
pushq %rax
pushq %r15
callq 0x10ac64
addq $0x10, %rsp
movsd (%r15), %xmm3
xorpd %xmm0, %xmm0
movq $0x0, 0x40(%rsp)
leaq 0x10(%rsp), %rax
movsd (%rax), %xmm1
xorl %r9d, %r9d
xorl %esi, %esi
xorl %r8d, %r8d
xorpd %xmm2, %xmm2
movapd %xmm1, %xmm5
movapd %xmm3, %xmm4
jmp 0x1102bc
movq 0x18(%r11), %rdi
leaq 0x10(%rsp), %rax
leaq 0x18(%rsp), %r11
movl %r15d, %esi
movl %r10d, %edx
movq %rbp, %rcx
movq %r9, %rbp
pushq %rax
pushq %r11
callq 0x10ac64
addq $0x10, %rsp
movl %r13d, %edi
shlq $0x3, %rdi
addq 0x18(%r14), %rdi
leaq 0x80(%rsp), %rax
leaq 0xd8(%rsp), %r15
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x128(%rsp), %rcx
movq 0x130(%rsp), %r8
movq %rbp, %r9
movq 0x128(%rsp), %rbp
pushq %rax
pushq %r15
callq 0x10ac64
addq $0x10, %rsp
leaq 0x18(%rsp), %rax
movsd (%rax), %xmm3
movsd (%r15), %xmm0
movapd %xmm3, %xmm4
addsd %xmm0, %xmm4
leaq 0x10(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x80(%rsp), %rax
movsd (%rax), %xmm2
movapd %xmm1, %xmm5
addsd %xmm2, %xmm5
cvttsd2si %xmm0, %r9
cvttsd2si %xmm2, %rax
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %r8d, %r8d
xorpd %xmm2, %xmm2
cvttsd2si %xmm5, %rcx
movl $0x0, 0x18(%r12)
movq $0x0, (%r12)
movl $0x1, %eax
testl %ecx, %ecx
je 0x11062a
movl 0x148(%rsp), %edx
testl %edx, %edx
jne 0x110312
cvttsd2si %xmm4, %rdx
movl %edx, %edx
xorps %xmm4, %xmm4
cvtsi2ss %rdx, %xmm4
movl %ecx, %ecx
xorps %xmm5, %xmm5
cvtsi2ss %rcx, %xmm5
mulss 0xc(%rsp), %xmm5
ucomiss %xmm5, %xmm4
jbe 0x11062a
cvttsd2si %xmm3, %rcx
movq %r8, 0xa0(%rsp)
cvttsd2si %xmm1, %r8
movq %r9, 0x98(%rsp)
cvttsd2si %xmm2, %r9
cvttsd2si %xmm0, %rdx
movq %rdx, 0xa8(%rsp)
movzbl 0x28(%r12), %r15d
subq $0x8, %rsp
leaq 0xc0(%rsp), %r10
leaq 0xc8(%rsp), %r11
leaq 0xd0(%rsp), %rax
movq %rsi, 0x98(%rsp)
movq 0x60(%rsp), %rdi
movq %r14, 0x28(%rsp)
movq %r14, %rsi
movq 0x70(%rsp), %rdx
movss 0x14(%rsp), %xmm0
pushq %r10
pushq %r11
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
pushq %r15
movq 0x168(%rsp), %r14
pushq %r14
movq 0x168(%rsp), %r14
pushq %r14
pushq %rbp
pushq 0xb8(%rsp)
movl 0x198(%rsp), %r15d
pushq %r15
movq 0xa8(%rsp), %r15
pushq %r15
pushq 0x98(%rsp)
pushq 0x90(%rsp)
pushq %r13
pushq 0xf0(%rsp)
movq 0x130(%rsp), %r13
pushq %r13
pushq %rbx
pushq 0xc0(%rsp)
pushq %r13
pushq $0x0
pushq 0xe8(%rsp)
pushq 0x148(%rsp)
pushq 0x148(%rsp)
pushq 0x160(%rsp)
pushq 0x170(%rsp)
callq 0x10b850
addq $0xd0, %rsp
leaq 0xd0(%rsp), %rax
movq (%rax), %r13
leaq 0xc8(%rsp), %rax
movq (%rax), %rbx
movq %r13, %rsi
orq %rbx, %rsi
leaq 0xc0(%rsp), %rax
movq (%rax), %rdi
orq %rdi, %rsi
movq 0x58(%rsp), %rdx
leaq 0xb8(%rsp), %rax
movq (%rax), %r8
movl $0x1, %eax
orq %r8, %rsi
js 0x11062a
movq %rbp, %rcx
movq %r8, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
leaq (,%r13,8), %rdi
addq 0x18(%rdx), %rdi
movl %ebx, %esi
subl %r13d, %esi
incl %esi
leaq 0x10(%rsp), %rax
leaq 0x18(%rsp), %rbp
movl %r15d, %edx
movq %r14, %r15
movq %r14, %r8
movq 0x138(%rsp), %r9
pushq %rax
pushq %rbp
callq 0x10ac64
addq $0x10, %rsp
movsd (%rbp), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x1104bf
jnp 0x110625
movsd 0x10(%rsp), %xmm0
ucomisd %xmm2, %xmm0
movl $0x1, %eax
jne 0x1104d6
jnp 0x11062a
movl 0x148(%rsp), %ecx
testl %ecx, %ecx
jne 0x1104f8
xorps %xmm2, %xmm2
cvtss2sd 0xc(%rsp), %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x11062a
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x120e00(%rip), %xmm1   # 0x231308
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
movq 0x190(%rsp), %rcx
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
movl %ebx, 0xc(%r12)
movq %rcx, %rbx
movl 0x158(%rsp), %r14d
movq 0x30(%rsp), %rcx
movl %ecx, 0x10(%r12)
movl 0x150(%rsp), %r10d
movq 0x28(%rsp), %rcx
movl %ecx, 0x14(%r12)
movq 0x140(%rsp), %r9
subq $0x8, %rsp
movq %r12, %rdi
movq 0x60(%rsp), %rbp
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
movq 0x140(%rsp), %rcx
movq 0x130(%rsp), %r8
pushq 0x1a8(%rsp)
pushq 0x1a8(%rsp)
pushq %rbx
pushq 0x1a8(%rsp)
pushq %r11
pushq %rax
pushq %r13
pushq 0x90(%rsp)
pushq 0x80(%rsp)
pushq %r14
pushq %r10
pushq 0xd0(%rsp)
pushq %r15
callq 0x10f901
addq $0x70, %rsp
movl %eax, 0x1c(%r12)
cmpl $-0x1, %eax
je 0x110625
cmpl $0x3, %eax
je 0x110610
cmpl $0x2, %eax
movq 0x20(%rsp), %rdx
jne 0x11063c
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%rdx), %eax
movl (%rbp), %ecx
jmp 0x11061f
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%rbp), %eax
movq 0x20(%rsp), %rdx
movl (%rdx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jle 0x11063c
movl $0x1, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x160(%rsp), %r13
movq %rdx, %rcx
movq 0x188(%rsp), %rax
movq (%rax), %r14
movq (%rbx), %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 %r15
pushq %rax
movq %r15, %r12
callq 0x10a814
addq $0x20, %rsp
decq %r13
cmpq %rax, %r13
movl $0x1, %eax
jb 0x11062a
movq 0x1a8(%rsp), %r15
subq $0x8, %rsp
movq %rbp, %rdi
movq %r12, %rsi
movq 0x58(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq 0x140(%rsp), %r8
movq %r14, %r9
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq %rbx
callq 0x10aa93
addq $0x20, %rsp
movsd 0x10(%rsp), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm3, %xmm0
setnp %cl
sete %al
movsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
jne 0x11070b
jp 0x11070b
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 0xc(%rsp), %xmm0
ucomiss %xmm2, %xmm0
setae %al
orb %dl, %al
movzbl %al, %eax
andl $0x1, %eax
jmp 0x11062a
callq 0x20f0