// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rbp
andq $0x0, (%r9)
andq $0x0, 0x18(%r9)
movq %rcx, %rbx
andq $0x0, 0x18(%r8)
movq %rdx, %r12
andl $0x0, 0x8(%rdx)
movq %rdi, %r13
cmpl $0x0, 0xc(%rdx)
je 0x1c3bd
movq %r9, %r15
movq %rsi, %r14
movq %r8, 0x10(%rsp)
movq 0x10(%r13), %rcx
movl $0x138, %eax            # imm = 0x138
addq %rax, %rcx
subq $0x8, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r8, %rdx
movq %r13, %r8
movq %r12, %r9
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq (%rbx)
callq 0x1aa50
addq $0x20, %rsp
movq (%r13), %rcx
movq 0x8(%r13), %r8
xorl %eax, %eax
cmpl $0x0, 0x8(%r12)
leaq 0x26eecf(%rip), %rdx    # 0x28b230
movq 0x28(%rdx), %r9
sete %al
movq 0x10(%r13), %r10
movl $0x138, %edx            # imm = 0x138
addq %rdx, %r10
subq $0x8, %rsp
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %rdx
movsd 0x209129(%rip), %xmm0   # 0x2254b0
pushq $0x0
pushq $0x0
pushq %r10
pushq %r12
pushq $0x1
popq %r10
pushq %r10
pushq (%rbx)
pushq %rax
callq 0x1edea
movq 0x50(%rsp), %r8
addq $0x40, %rsp
movl 0x8(%r12), %eax
incl %eax
movl %eax, 0x8(%r12)
cmpl 0xc(%r12), %eax
jb 0x1c31a
movq (%r13), %rax
movq 0x18(%rax), %rcx
movl 0x14(%rcx), %edx
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
jne 0x1c3d7
xorl %edi, %edi
xorl %esi, %esi
jmp 0x1c420
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
cmpb $0x1, (%rbp,%r8)
je 0x1c418
movq (%rbx), %r9
cmpb $0x0, (%r9,%r8)
je 0x1c418
jle 0x1c406
movq 0x20(%rcx), %r9
movl (%r9,%r8,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rdi
jmp 0x1c418
movq 0x20(%rcx), %r9
movl (%r9,%r8,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rsi
incq %r8
cmpq %r8, %rdx
jne 0x1c3de
xorl %ecx, %ecx
cmpq %rsi, %rdi
movl $0x0, %edx
sbbq %rdx, %rdx
movq 0x18(%rax), %r9
testl $0x7fffffff, 0x14(%r9)  # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r15
je 0x1c4ff
orq $0x1, %rdx
pushq $0x2
popq %rsi
xorl %ecx, %ecx
leaq 0x26e304(%rip), %rdi    # 0x28a758
xorl %r8d, %r8d
movb (%rbp,%r8), %r10b
cmpb $0x1, %r10b
jne 0x1c47f
movq 0x10(%r15), %r11
movl (%r11,%rsi,2), %r10d
cmpl %r10d, -0x4(%r11,%rsi,2)
jne 0x1c4e0
movslq 0x1bc(%rdi), %r11
cmpq %r11, %r10
je 0x1c4b5
jmp 0x1c4e0
movq (%rbx), %r11
movsbq (%r11,%r8), %r11
testq %r11, %r11
je 0x1c4af
cmpq %r11, %rdx
jne 0x1c4c9
movb $0x2, (%rbp,%r8)
movq 0x18(%rax), %r9
movq 0x20(%r9), %r9
movl (%r9,%r8,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
jmp 0x1c4d8
cmpb $0x2, %r10b
jne 0x1c462
movq 0x20(%r9), %r9
movl (%r9,%r8,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
jmp 0x1c4e0
movb $0x1, (%rbp,%r8)
movq (%r12), %r9
orl $-0x1, (%r9,%r8,4)
movq (%rbx), %r9
movb $0x0, (%r9,%r8)
incq %r8
movq 0x18(%rax), %r9
movl 0x14(%r9), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
addq $0x6, %rsi
cmpq %r10, %r8
jb 0x1c457
movq 0x26dada(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x214320(%rip), %rsi    # 0x230830
leaq 0x21433f(%rip), %rdx    # 0x230856
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0