// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: if_ploid_sample(ma_ug_t*, asg_t*, R_to_U*, ma_hit_t_alloc*, ma_hit_t_alloc*, ma_sub_t*, hap_alignment_struct_pip*, hap_overlaps_list*, hap_overlaps_list*, unsigned int, double)
int if_ploid_sample(ma_ug_t *ug, asg_t *read_g, R_to_U* ruIndex, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, ma_sub_t* coverage_cut, 
hap_alignment_struct_pip* hap_buf, hap_overlaps_list* all_ovlp, hap_overlaps_list* back_all_ovlp, 
uint32_t minLen, double purge_threshold)
{
    asg_t* nsg = ug->g;
    uint64_t v, k, total_bases = 0, alter_bases = 0, primary_bases = 0, purge_bases = 0;
    kt_for(asm_opt.thread_num, hap_alignment_advance_worker, hap_buf, nsg->n_seq);
    
    filter_hap_overlaps_by_length(all_ovlp, minLen);
    normalize_hap_overlaps_advance(all_ovlp, back_all_ovlp, ug, read_g, reverse_sources, ruIndex);

    get_contig_length(ug, read_g, &primary_bases, &alter_bases);
    total_bases = primary_bases + alter_bases;
    // fprintf(stderr, "primary_bases: %lu\n", primary_bases);
    // fprintf(stderr, "alter_bases: %lu\n", alter_bases);
    // fprintf(stderr, "total_bases: %lu\n", total_bases);


    for (v = 0; v < all_ovlp->num; v++)
    {
        for (k = 0; k < all_ovlp->x[v].a.n; k++)
        {
            purge_bases += all_ovlp->x[v].a.a[k].x_end_pos - all_ovlp->x[v].a.a[k].x_beg_pos;
        }
    }
    purge_bases = purge_bases/2;
    ///fprintf(stderr, "purge_bases: %lu\n", purge_bases);
    alter_bases = alter_bases + purge_bases;
    ///fprintf(stderr, "new alter_bases: %lu\n", alter_bases);


    for (v = 0; v < all_ovlp->num; v++)
    {
        all_ovlp->x[v].a.n = 0;
    }

    for (v = 0; v < back_all_ovlp->num; v++)
    {
        back_all_ovlp->x[v].a.n = 0;
    }

    if(alter_bases > total_bases * purge_threshold) return 1;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %xmm0, (%rsp)
movq %rsi, %r15
movq %rdi, %r12
movq 0x58(%rsp), %r14
movl 0x68(%rsp), %ebx
movq 0x50(%rsp), %rdx
movq 0x18(%rdi), %rax
xorl %ecx, %ecx
leaq 0x10(%rsp), %r13
movq %rcx, (%r13)
leaq 0x8(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0x17f717(%rip), %rcx    # 0x295758
movl 0x7c(%rcx), %edi
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x24c(%rip), %rsi       # 0x1162a0
callq 0x108f2c
movq %r14, %rdi
movl %ebx, %esi
movq 0x60(%rsp), %rbx
callq 0x111dbd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x111522
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x115eff
movq (%r13), %rax
movl 0x8(%r14), %edx
movq %rax, %rcx
testq %rdx, %rdx
je 0x116187
movq (%r14), %rsi
xorl %ecx, %ecx
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movq (%rsi,%r9,8), %r8
testq %r8, %r8
je 0x1160cb
leaq (%rsi,%r9,8), %r9
movq 0x10(%r9), %r9
addq $0x8, %r9
movl (%r9), %r10d
subl -0x4(%r9), %r10d
addq %r10, %rcx
addq $0x40, %r9
decq %r8
jne 0x1160b8
incq %rdi
cmpq %rdx, %rdi
jne 0x11609f
shrq %rcx
addq %rax, %rcx
testq %rdx, %rdx
je 0x116187
movq (%r14), %rsi
leaq -0x1(%rdx), %rdi
movq %rdi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0x8(,%rdx,8), %rdx
andq $-0x10, %rdx
leaq (%rdx,%rdx,2), %rdx
movdqa 0x11b1c5(%rip), %xmm1   # 0x2312d0
xorl %edi, %edi
movdqa 0x11b1cb(%rip), %xmm2   # 0x2312e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x11b3db(%rip), %xmm4   # 0x231500
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %r8d
notl %r8d
testb $0x1, %r8b
je 0x116161
movq $0x0, (%rsi,%rdi)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %r8d
testb $0x1, %r8b
je 0x11617a
movq $0x0, 0x18(%rsi,%rdi)
paddq %xmm4, %xmm1
addq $0x30, %rdi
cmpq %rdi, %rdx
jne 0x116125
movq 0x8(%rsp), %rdx
movl 0x8(%rbx), %edi
testq %rdi, %rdi
je 0x11623e
movq (%rbx), %rsi
leaq -0x1(%rdi), %r8
movq %r8, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0x8(,%rdi,8), %rdi
andq $-0x10, %rdi
leaq (%rdi,%rdi,2), %rdi
movdqa 0x11b10f(%rip), %xmm1   # 0x2312d0
xorl %r8d, %r8d
movdqa 0x11b114(%rip), %xmm2   # 0x2312e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x11b324(%rip), %xmm4   # 0x231500
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %r9d
notl %r9d
testb $0x1, %r9b
je 0x116218
movq $0x0, (%rsi,%r8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %r9d
testb $0x1, %r9b
je 0x116231
movq $0x0, 0x18(%rsi,%r8)
paddq %xmm4, %xmm1
addq $0x30, %r8
cmpq %r8, %rdi
jne 0x1161dc
addq %rdx, %rax
movq %rcx, %xmm0
movq 0x11b032(%rip), %xmm1   # 0x231280
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x11b036(%rip), %xmm2   # 0x231290
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd (%rsp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm3
seta %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq