// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xa8, %rsp
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)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x20bd90
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x138, %rcx            # imm = 0x138
movq -0x10(%rbp), %r8
movq -0x20(%rbp), %r9
movq -0x28(%rbp), %rax
movq (%rax), %rax
xorl %r10d, %r10d
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x2043f0
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %r8
leaq 0x1b95c1(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %r9
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
xorl %ebx, %ebx
movl $0x1, %eax
cmpl $0x0, %esi
cmovel %eax, %ebx
movq -0x28(%rbp), %rax
movq (%rax), %r11
movq -0x20(%rbp), %r10
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x138, %rax            # imm = 0x138
xorl %esi, %esi
movsd 0x1764e6(%rip), %xmm0   # 0x382230
xorl %edi, %edi
movq %rsi, %rdi
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
callq 0x227770
movq -0x20(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x20bc9a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x20be67
movq 0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0x20bdf8
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x20bdfa
jmp 0x20be56
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jle 0x20be32
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x20be54
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x20be56
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x20bdb3
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpq %rsi, %rdx
cmovael %ecx, %eax
cltq
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x20bfe4
movq 0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0x20bede
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x20bf60
movq 0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0x20bf3c
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0xc, -0x58(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq 0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0xc, -0x58(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x20bf5e
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0xc, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
leaq 0x1b88c8(%rip), %rcx    # 0x3c47f8
movslq 0x1bc(%rcx), %rcx
cmpq %rcx, %rax
jne 0x20bf5e
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x20bfd3
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movsbq (%rax,%rcx), %rax
cmpq -0x68(%rbp), %rax
jne 0x20bfa6
movq 0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movb $0x2, (%rax,%rcx)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x20bfc4
movq 0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x20be95
movq 0x1b7ff5(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x60(%rbp), %rcx
leaq 0x1816a4(%rip), %rsi    # 0x38d69d
leaq 0x1816c3(%rip), %rdx    # 0x38d6c3
movb $0x0, %al
callq 0x22a0
addq $0xa8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)