// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: purge_phase_0(ha_ug_index*, hc_links*, bubble_type*, ps_t*, kvec_pe_hit*, kv_u_trans_t*, unsigned char*, mmhap_t*)
void purge_phase_0(ha_ug_index* idx, hc_links *link, bubble_type *bub, ps_t *s, kvec_pe_hit* hits, kv_u_trans_t *k_trans, uint8_t *del, mmhap_t *hh)
{
    k_trans->idx.n = k_trans->n = 0; hits->idx.n = 0; 
    for (bub->round_id = 0; bub->round_id < bub->n_round; bub->round_id++) {
        renew_kv_u_trans(k_trans, link, hits, &(idx->t_ch->k_trans), idx, bub, s->s, NULL, 1/**0**/);
        mc_solve(NULL, NULL, k_trans, idx->ug, idx->read_g, 0.8, R_INF.trio_flag, 
        (bub->round_id==0?1:0), s->s, 1, bub, &(idx->t_ch->k_trans), 0, 
        /**(((bub.round_id+1) == bub.n_round)?1:0)**/0);
        /*******************************for debug************************************/
        // label_unitigs_sm(s->s, NULL, idx->ug);
    }

    uint64_t l[2], k, len; int64_t p; ma_ug_t *ug = idx->ug;
    for (k = l[0] = l[1] = 0; k < ug->g->n_seq; k++) {
        if((del[k] == 1) || (s->s[k] == 0)) continue;
        if(s->s[k] > 0) l[0] += ug->g->seq[k].len;
        else l[1] += ug->g->seq[k].len;
    }

    p = (l[0]>=l[1])?1:-1;
    for (k = len = 0; k < ug->g->n_seq; k++) {
        if((del[k] == 1) || (s->s[k] == 0)) {
            if((del[k] == 2) || 
                ((hh->h.a[k].n == hh->h.a[k].m) && (hh->h.a[k].m == ((uint64_t)asm_opt.polyploidy)))) {
                len += ug->g->seq[k].len;
            }
            continue;
        }
        if(s->s[k] == p) {
            del[k] = 2; len += ug->g->seq[k].len;
        } else {
            del[k] = 1; bub->index[k] = (uint32_t)-1;
        }
        s->s[k] = 0;///reset
    }
    fprintf(stderr, "[M::%s::stat] # remaining bases: %lu\n", __func__, len);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
andq $0x0, (%r9)
movq %rdi, %rbp
andq $0x0, 0x18(%r9)
movq %r8, 0x20(%rsp)
andq $0x0, 0x18(%r8)
xorl %eax, %eax
movl $0x138, %r12d           # imm = 0x138
pushq $0x1
popq %r13
movl %eax, 0x8(%r14)
cmpl 0xc(%r14), %eax
jae 0x11d74a
movq 0x10(%rbp), %rcx
addq %r12, %rcx
subq $0x8, %rsp
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rbp, %r8
movq %r14, %r9
pushq %r13
pushq $0x0
pushq (%rbx)
callq 0x1191a6
addq $0x20, %rsp
movq (%rbp), %rcx
movq 0x8(%rbp), %r8
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
leaq 0x136b28(%rip), %rdx    # 0x254230
movq 0x28(%rdx), %r9
sete %al
movq 0x10(%rbp), %r10
addq %r12, %r10
subq $0x8, %rsp
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %rdx
movsd 0xc5dd7(%rip), %xmm0    # 0x1e3500
pushq $0x0
pushq $0x0
pushq %r10
pushq %r14
pushq %r13
pushq (%rbx)
pushq %rax
callq 0x12b808
addq $0x40, %rsp
movl 0x8(%r14), %eax
incl %eax
jmp 0x11d6b7
movq (%rbp), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %esi
movl %esi, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
cmpq %rdi, %rcx
je 0x11d7a9
cmpb $0x1, (%r13,%rdi)
je 0x11d7a4
movq (%rbx), %r10
movb (%r10,%rdi), %r11b
testb %r11b, %r11b
je 0x11d7a4
movq 0x20(%rdx), %r10
movl (%r10,%rdi,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
testb %r11b, %r11b
jle 0x11d7a1
addq %r10, %r8
jmp 0x11d7a4
addq %r10, %r9
incq %rdi
jmp 0x11d76f
xorl %edi, %edi
cmpq %r9, %r8
movl $0x0, %r8d
sbbq %r8, %r8
orq $0x1, %r8
pushq $0x2
popq %r9
leaq 0x135f92(%rip), %r10    # 0x253758
xorl %ecx, %ecx
movl %esi, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %r11, %rdi
jae 0x11d873
movb (%r13,%rdi), %r11b
cmpb $0x1, %r11b
jne 0x11d804
movq 0x10(%r12), %r15
movl (%r15,%r9,2), %r11d
cmpl %r11d, -0x4(%r15,%r9,2)
jne 0x11d867
movslq 0x1bc(%r10), %r15
cmpq %r15, %r11
je 0x11d838
jmp 0x11d867
movq (%rbx), %r15
movsbq (%r15,%rdi), %r15
testq %r15, %r15
je 0x11d832
cmpq %r15, %r8
jne 0x11d84c
movb $0x2, (%r13,%rdi)
movq 0x18(%rax), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rdi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %rcx
jmp 0x11d859
cmpb $0x2, %r11b
jne 0x11d7e6
movq 0x20(%rdx), %r11
movl (%r11,%rdi,8), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
addq %r11, %rcx
jmp 0x11d867
movb $0x1, (%r13,%rdi)
movq (%r14), %rdx
orl $-0x1, (%rdx,%rdi,4)
movq (%rbx), %rdx
movb $0x0, (%rdx,%rdi)
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %esi
incq %rdi
addq $0x6, %r9
jmp 0x11d7c8
movq 0x135766(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xd10a8(%rip), %rsi     # 0x1ee92c
leaq 0xd10c7(%rip), %rdx     # 0x1ee952
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0