// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
movq %r9, %r15
movq %r8, %rbp
movl %ecx, %r10d
movq %rdx, 0x30(%rsp)
movq %rsi, %rdx
movq %rdi, %r12
movl 0x10(%r8), %esi
movl 0x10(%r9), %eax
xorl %r13d, %r13d
movq %r13, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movq %r13, 0x48(%rsp)
movq %r13, 0x38(%rsp)
movq %r13, 0x90(%rsp)
movq %r13, 0x68(%rsp)
movq %r13, 0x88(%rsp)
movq %r13, 0x60(%rsp)
movl 0x38(%rdi), %ecx
shrl %ecx
movl (%rdx,%rcx,8), %ebx
cmpb $0x0, 0x28(%rdi)
je 0x10af01
movl %ebx, %ecx
incl %ecx
movl %eax, %ebx
subl %ecx, %ebx
movq %rcx, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
jmp 0x10af16
movl %eax, %ecx
subl %ebx, %ecx
movq %rcx, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movl %ebx, %r13d
movl 0x1c(%r12), %r11d
cmpq $0x3, %r11
ja 0x10b5f4
movq 0x30(%r12), %rcx
shrq $0x21, %rcx
movq %rdx, 0x80(%rsp)
movq (%rdx,%rcx,8), %r14
movq 0x138(%rsp), %r9
movq 0x130(%rsp), %r8
movq 0x128(%rsp), %rcx
movl %esi, %edx
subl %r14d, %edx
movq %rdx, 0x40(%rsp)
leaq 0x122585(%rip), %rdi    # 0x22d4e8
movq %r11, 0x78(%rsp)
movslq (%rdi,%r11,4), %rdx
addq %rdi, %rdx
movq %r10, 0x58(%rsp)
movq %r14, 0xc0(%rsp)
jmpq *%rdx
movl %r14d, %r14d
movl 0x28(%rsp), %edi
shlq $0x3, %rdi
addq 0x18(%r15), %rdi
leaq 0x38(%rsp), %rax
leaq 0x48(%rsp), %r11
movl %ebx, %esi
movq 0x30(%rsp), %rdx
movq %r15, 0x50(%rsp)
movq %rbp, %r15
movq %r10, %rbp
pushq %rax
pushq %r11
callq 0x1064d3
addq $0x10, %rsp
shlq $0x3, %r14
addq 0x18(%r15), %r14
leaq 0x68(%rsp), %rax
leaq 0x90(%rsp), %r10
movq %r14, %rdi
movq 0x40(%rsp), %rsi
movl %ebp, %edx
movq %r15, %rbp
movq 0x50(%rsp), %r15
movq 0x128(%rsp), %rcx
movq 0x130(%rsp), %r8
movq 0x138(%rsp), %r9
pushq %rax
pushq %r10
movq %r10, %r14
callq 0x1064d3
addq $0x10, %rsp
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm0
addsd (%r14), %xmm0
leaq 0x38(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x68(%rsp), %rax
jmp 0x10b105
movq 0x18(%r15), %rdi
leaq 0x38(%rsp), %r10
leaq 0x48(%rsp), %r14
movl %eax, %esi
movq 0x30(%rsp), %rdx
pushq %r10
pushq %r14
callq 0x1064d3
addq $0x10, %rsp
movsd (%r14), %xmm0
leaq 0x38(%rsp), %rax
jmp 0x10b079
movq 0x18(%rbp), %rdi
leaq 0x8(%rsp), %rax
leaq 0x18(%rsp), %r14
movl %r10d, %edx
pushq %rax
pushq %r14
callq 0x1064d3
addq $0x10, %rsp
movsd (%r14), %xmm0
leaq 0x8(%rsp), %rax
movsd (%rax), %xmm1
jmp 0x10b109
movq 0x18(%rbp), %rdi
leaq 0x8(%rsp), %rax
leaq 0x18(%rsp), %r11
movl %r14d, %esi
movl %r10d, %edx
movq %r9, %r14
pushq %rax
pushq %r11
callq 0x1064d3
addq $0x10, %rsp
movl %r13d, %edi
shlq $0x3, %rdi
addq 0x18(%r15), %rdi
leaq 0x60(%rsp), %rax
leaq 0x88(%rsp), %r10
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x128(%rsp), %rcx
movq 0x130(%rsp), %r8
movq %r14, %r9
pushq %rax
pushq %r10
movq %r10, %r14
callq 0x1064d3
addq $0x10, %rsp
leaq 0x18(%rsp), %rax
movsd (%rax), %xmm0
addsd (%r14), %xmm0
leaq 0x8(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x60(%rsp), %rax
addsd (%rax), %xmm1
cvttsd2si %xmm1, %rdx
movl $0x0, 0x18(%r12)
movq $0x0, (%r12)
movl $0x1, %eax
testl %edx, %edx
je 0x10b5e2
movl 0x148(%rsp), %ecx
testl %ecx, %ecx
jne 0x10b15f
cvttsd2si %xmm0, %rcx
movl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movl %edx, %ecx
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x14(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x10b5e2
cvttsd2si 0x18(%rsp), %rcx
cvttsd2si 0x8(%rsp), %r8
cvttsd2si 0x48(%rsp), %r9
cvttsd2si 0x38(%rsp), %rax
movq %rax, 0xb8(%rsp)
cvttsd2si 0x90(%rsp), %rax
movq %rax, 0xb0(%rsp)
cvttsd2si 0x68(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rbp, %rdi
cvttsd2si 0x88(%rsp), %rax
movq %rax, 0xa0(%rsp)
cvttsd2si 0x60(%rsp), %rax
movq %rax, 0x98(%rsp)
movzbl 0x28(%r12), %r10d
subq $0x8, %rsp
leaq 0xd0(%rsp), %rbp
leaq 0xd8(%rsp), %r11
leaq 0xe0(%rsp), %rax
leaq 0xe8(%rsp), %r14
movq %rdi, 0x78(%rsp)
movq %r15, 0x58(%rsp)
movq %r15, %rsi
movq 0x80(%rsp), %rdx
movss 0x1c(%rsp), %xmm0
pushq %rbp
pushq %r11
pushq %rax
pushq %r14
pushq %r10
movq 0x168(%rsp), %r15
pushq %r15
movq 0x168(%rsp), %r15
pushq %r15
movq 0x168(%rsp), %rbp
pushq %rbp
pushq 0xc8(%rsp)
movl 0x198(%rsp), %eax
pushq %rax
movq 0xb0(%rsp), %r14
pushq %r14
pushq 0x90(%rsp)
pushq 0x88(%rsp)
pushq %r13
pushq 0xb8(%rsp)
movq 0x140(%rsp), %r10
pushq %r10
pushq %rbx
pushq 0xb8(%rsp)
pushq %r10
pushq $0x0
pushq 0x140(%rsp)
pushq 0x150(%rsp)
pushq 0x160(%rsp)
pushq 0x170(%rsp)
pushq 0x180(%rsp)
callq 0x1070f1
addq $0xd0, %rsp
leaq 0xe0(%rsp), %rax
movq (%rax), %r13
leaq 0xd8(%rsp), %rax
movq (%rax), %rdx
movq %r13, %rcx
orq %rdx, %rcx
leaq 0xd0(%rsp), %rax
movq (%rax), %rsi
orq %rsi, %rcx
leaq 0xc8(%rsp), %rax
movq (%rax), %rdi
movl $0x1, %eax
orq %rdi, %rcx
js 0x10b5e2
movq %rdi, 0x40(%rsp)
movq %rsi, 0x20(%rsp)
leaq (,%r13,8), %rdi
movq 0x70(%rsp), %rax
addq 0x18(%rax), %rdi
movq %rdx, 0x28(%rsp)
movl %edx, %esi
subl %r13d, %esi
incl %esi
leaq 0x8(%rsp), %rax
leaq 0x18(%rsp), %rbx
movl %r14d, %edx
movq %rbp, %rcx
movq %r15, %rbp
movq %r15, %r8
movq 0x138(%rsp), %r9
pushq %rax
pushq %rbx
callq 0x1064d3
addq $0x10, %rsp
movsd (%rbx), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x10b34e
jnp 0x10b5dd
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
movl $0x1, %eax
jne 0x10b365
jnp 0x10b5e2
movl 0x148(%rsp), %ecx
testl %ecx, %ecx
jne 0x10b387
xorps %xmm2, %xmm2
cvtss2sd 0x14(%rsp), %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x10b5e2
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x119ed1(%rip), %xmm1   # 0x225268
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 0x78(%rsp), %rax
movl %eax, 0x1c(%r12)
movq 0x170(%rsp), %rax
movl %r13d, 0x8(%r12)
movq 0x168(%rsp), %r13
movq 0x28(%rsp), %rcx
movl %ecx, 0xc(%r12)
movl 0x158(%rsp), %r15d
movq 0x20(%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 0x78(%rsp), %rbx
movq %rbx, %rsi
movq 0x58(%rsp), %r14
movq %r14, %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 0x98(%rsp)
pushq 0x78(%rsp)
pushq %r15
pushq %r10
pushq 0xe0(%rsp)
pushq %rbp
callq 0x10a80f
addq $0x70, %rsp
movl %eax, 0x1c(%r12)
cmpl $-0x1, %eax
je 0x10b5dd
cmpl $0x3, %eax
movq %rbp, %r10
je 0x10b4b6
cmpl $0x2, %eax
jne 0x10b4d3
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%r14), %ecx
movl (%rbx), %edx
addl %edx, %edx
cmpl %edx, %ecx
jg 0x10b5dd
cmpl $-0x1, %eax
je 0x10b5dd
cmpl $0x3, %eax
jne 0x10b4d3
cmpl $-0x1, %eax
je 0x10b5dd
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%rbx), %eax
movl (%r14), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jg 0x10b5dd
movq 0x160(%rsp), %r13
movq 0x188(%rsp), %rax
movq (%rax), %r15
movq 0x190(%rsp), %rax
movq (%rax), %rbp
movq 0x198(%rsp), %rax
movq (%rax), %r8
movq 0x1a0(%rsp), %rax
movq (%rax), %r9
movzbl 0x28(%r12), %eax
subq $0x8, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r14, %rcx
pushq 0x188(%rsp)
pushq %r10
pushq %rax
movq %r10, %r12
callq 0x10606a
addq $0x20, %rsp
decq %r13
cmpq %rax, %r13
movl $0x1, %eax
jb 0x10b5e2
movq 0x1a8(%rsp), %r14
subq $0x8, %rsp
movq %rbx, %rdi
movq %r12, %rsi
movq 0x60(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq 0x140(%rsp), %r8
movq %r15, %r9
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq %rbp
callq 0x1062f3
addq $0x20, %rsp
movsd 0x8(%rsp), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm3, %xmm0
setnp %cl
sete %al
movsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
jne 0x10b5ae
jp 0x10b5ae
xorpd %xmm2, %xmm2
andb %cl, %al
cvtsd2ss %xmm2, %xmm2
movss %xmm2, (%r14)
ucomisd %xmm1, %xmm0
setnp %cl
sete %dl
andb %cl, %dl
orb %al, %dl
movss 0x14(%rsp), %xmm0
ucomiss %xmm2, %xmm0
setae %al
orb %dl, %al
movzbl %al, %eax
andl $0x1, %eax
jmp 0x10b5e2
movl $0x1, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20f0