// File: /workspace/github_cpp_makefile_star2_O2/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
movsd %xmm0, (%rsp)
movq %rsi, %r15
movq %rdi, %r13
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %rdx
movq 0x18(%rdi), %rax
leaq 0x10(%rsp), %r12
andq $0x0, (%r12)
leaq 0x8(%rsp), %rbp
andq $0x0, (%rbp)
leaq 0x182489(%rip), %rcx    # 0x253758
movl 0x7c(%rcx), %edi
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x116(%rip), %rsi       # 0xd13f8
callq 0xc67c0
movq %r14, %rdi
movl 0x68(%rsp), %esi
callq 0xce0d3
movq %r14, %rdi
movq %rbx, %rsi
callq 0xcd951
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xd1193
movq (%rbp), %rax
movq (%r12), %rcx
movl 0x8(%r14), %esi
xorl %edi, %edi
pushq $0x8
popq %r8
xorl %edx, %edx
cmpq %rsi, %rdi
je 0xd135b
movq (%r14), %r10
imulq $0x18, %rdi, %r11
leaq (%r10,%r11), %r9
movq (%r10,%r11), %r10
movq %r8, %r11
subq $0x1, %r10
jb 0xd1356
movq 0x10(%r9), %r15
movl (%r15,%r11), %r12d
subl -0x4(%r15,%r11), %r12d
addq %r12, %rdx
addq $0x40, %r11
jmp 0xd133a
incq %rdi
jmp 0xd1323
imulq $0x18, %rsi, %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xd1374
movq (%r14), %r8
andq $0x0, (%r8,%rdi)
addq $0x18, %rdi
jmp 0xd1361
movl 0x8(%rbx), %esi
imulq $0x18, %rsi, %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xd1390
movq (%rbx), %r8
andq $0x0, (%r8,%rdi)
addq $0x18, %rdi
jmp 0xd137d
shrq %rdx
addq %rcx, %rdx
addq %rcx, %rax
movq %rdx, %xmm0
movq 0x111eca(%rip), %xmm1   # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x111ece(%rip), %xmm2   # 0x1e3280
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