// File: /workspace/github_cpp_makefile_star2_O3/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 0x1bcf6
movq %r9, %r14
movq %rsi, %r15
movq %r8, 0x10(%rsp)
movq 0x10(%r13), %rcx
movl $0x138, %eax            # imm = 0x138
addq %rax, %rcx
subq $0x8, %rsp
movq %r14, %rdi
movq %r15, %rsi
movq %r8, %rdx
movq %r13, %r8
movq %r12, %r9
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq (%rbx)
callq 0x1a338
addq $0x20, %rsp
movq (%r13), %rcx
movq 0x8(%r13), %r8
xorl %eax, %eax
cmpl $0x0, 0x8(%r12)
leaq 0x27a596(%rip), %rdx    # 0x296230
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 %r14, %rdx
movsd 0x2158f0(%rip), %xmm0   # 0x2315b0
pushq $0x0
pushq $0x0
pushq %r10
pushq %r12
pushq $0x1
popq %r10
pushq %r10
pushq (%rbx)
pushq %rax
callq 0x1e07b
movq 0x50(%rsp), %r8
addq $0x40, %rsp
movl 0x8(%r12), %eax
incl %eax
movl %eax, 0x8(%r12)
cmpl 0xc(%r12), %eax
jb 0x1bc53
movq (%r13), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jne 0x1bd10
xorl %ecx, %ecx
jmp 0x1be22
xorl %esi, %esi
xorl %r8d, %r8d
xorl %ecx, %ecx
movq 0x8(%rsp), %r15
cmpb $0x1, (%rbp,%rcx)
je 0x1bd4b
movq (%rbx), %r9
movb (%r9,%rcx), %r10b
testb %r10b, %r10b
je 0x1bd4b
movq 0x20(%rdx), %r9
movl (%r9,%rcx,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
testb %r10b, %r10b
jle 0x1bd48
addq %r9, %r8
jmp 0x1bd4b
addq %r9, %rsi
incq %rcx
cmpq %rcx, %rdi
jne 0x1bd1c
xorl %ecx, %ecx
cmpq %rsi, %r8
movl $0x0, %esi
sbbq %rsi, %rsi
testl %edi, %edi
je 0x1be22
orq $0x1, %rsi
pushq $0x2
popq %rdi
xorl %ecx, %ecx
leaq 0x2799e0(%rip), %r8     # 0x295758
xorl %r9d, %r9d
movb (%rbp,%r9), %r10b
cmpb $0x1, %r10b
jne 0x1bda3
movq 0x10(%r15), %r11
movl (%r11,%rdi,2), %r10d
cmpl %r10d, -0x4(%r11,%rdi,2)
jne 0x1be07
movslq 0x1bc(%r8), %r11
cmpq %r11, %r10
je 0x1bdd8
jmp 0x1be07
movq (%rbx), %r11
movsbq (%r11,%r9), %r11
testq %r11, %r11
je 0x1bdd2
cmpq %r11, %rsi
jne 0x1bdec
movb $0x2, (%rbp,%r9)
movq 0x18(%rax), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%r9,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %rcx
jmp 0x1bdfb
cmpb $0x2, %r10b
jne 0x1bd86
movq 0x20(%rdx), %r10
movl (%r10,%r9,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
addq %r10, %rcx
jmp 0x1be07
movb $0x1, (%rbp,%r9)
movq (%r12), %rdx
orl $-0x1, (%rdx,%r9,4)
movq (%rbx), %rdx
movb $0x0, (%rdx,%r9)
movq 0x18(%rax), %rdx
incq %r9
movl 0x14(%rdx), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
addq $0x6, %rdi
cmpq %r10, %r9
jb 0x1bd7b
movq 0x2791b7(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x220c09(%rip), %rsi    # 0x23ca3c
leaq 0x220c28(%rip), %rdx    # 0x23ca62
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0