// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movq 0x60(%rsp), %rbp
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %rdx
movq 0x18(%rdi), %rax
xorl %r14d, %r14d
leaq 0x10(%rsp), %r13
movq %r14, (%r13)
movq %rsp, %rcx
movq %r14, (%rcx)
leaq 0x179e6d(%rip), %rcx    # 0x28a758
movl 0x7c(%rcx), %edi
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x135(%rip), %rsi       # 0x110a33
callq 0x1047b4
movq %rbx, %rdi
movl 0x68(%rsp), %esi
callq 0x10cd21
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10c429
movq %r12, %rdi
movq %r15, %rsi
movq %rsp, %rdx
movq %r13, %rcx
callq 0x1107a6
movq (%r13), %rax
movl 0x8(%rbx), %ecx
testq %rcx, %rcx
je 0x110976
movq (%rbx), %rdx
xorl %r14d, %r14d
xorl %esi, %esi
leaq (%rsi,%rsi,2), %r8
movq (%rdx,%r8,8), %rdi
testq %rdi, %rdi
je 0x11096b
leaq (%rdx,%r8,8), %r8
movq 0x10(%r8), %r8
addq $0x8, %r8
movl (%r8), %r9d
subl -0x4(%r8), %r9d
addq %r9, %r14
addq $0x40, %r8
decq %rdi
jne 0x110958
incq %rsi
cmpq %rcx, %rsi
jne 0x11093f
shrq %r14
movq (%rsp), %rdx
addq %rax, %r14
movq %r14, 0x10(%rsp)
testq %rcx, %rcx
je 0x1109a5
movq (%rbx), %rsi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edi, %edi
movq $0x0, (%rsi,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rcx
jne 0x110994
movq 0x60(%rsp), %rcx
movl 0x8(%rcx), %esi
testq %rsi, %rsi
je 0x1109d0
movq (%rcx), %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
movq $0x0, (%rcx,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x1109bf
addq %rdx, %rax
movq %r14, %xmm0
movq 0x114860(%rip), %xmm1   # 0x225240
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x114864(%rip), %xmm2   # 0x225250
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 0x8(%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