// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
leaq 0x24349e(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %edi
movq 0x10(%rbp), %rdx
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
leaq 0x1f8(%rip), %rsi       # 0x181570
callq 0x16d710
movq 0x18(%rbp), %rdi
movl 0x28(%rbp), %esi
callq 0x17ba50
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x20(%rbp), %r9
callq 0x17a7f0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x181130
movq -0x70(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jae 0x181470
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x18, -0x50(%rbp), %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x18145d
movq 0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq 0x18(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x18, -0x50(%rbp), %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movq -0x58(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1813eb
jmp 0x18145f
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1813cf
movq -0x78(%rbp), %rax
shrq %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1814c3
movq 0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x18148f
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x20(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1814ff
movq 0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1814cb
movq -0x68(%rbp), %xmm0
movaps 0x200b05(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x200b09(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x60(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x181552
movl $0x1, -0x4(%rbp)
jmp 0x181559
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)