// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: purge_phase(ha_ug_index*, mmhap_t*, unsigned long, unsigned long, hc_links*, kvec_pe_hit*, bubble_type*, ps_t*, kv_u_trans_t*, unsigned char*, unsigned int*, kv_u_trans_t*, asg64_v*)
void purge_phase(ha_ug_index* idx, mmhap_t *hh, uint64_t hapid, uint64_t max_round, hc_links *link, kvec_pe_hit* hits,
bubble_type *bub, ps_t *s, kv_u_trans_t *k_trans, uint8_t *del, uint32_t *bidx, kv_u_trans_t *ref, asg64_v *srt)
{
    uint64_t k, len; uint32_t *bm;
    ma_ug_t *ug = idx->ug;
    if(max_round <= 0) {
        for (k = 0; k < ug->g->n_seq; k++) {
            if(hh->h.a[k].n >= hh->h.a[k].m) continue;
            hh->a.a[hh->h.a[k].a+hh->h.a[k].n] = hapid;
            hh->h.a[k].n++;
        }
        return;
    }

    for (k = 0; k < ug->g->n_seq; k++) {
        del[k] = 0; s->s[k] = 0;
        if(hh->h.a[k].n >= hh->h.a[k].m) {///all haplotypes have been set
            bub->index[k] = (uint32_t)-1; del[k] = 1;
        } else {
            if(IF_HOM(k, (*bub))) bub->index[k] = bub->num.n;
        }
        bidx[k] = bub->index[k];
    }
    bm = bub->index; bub->index = bidx; bidx = bm;

    kv_resize(u_trans_t, *ref, idx->t_ch->k_trans.n); ref->n = idx->t_ch->k_trans.n;
    memcpy(ref->a, idx->t_ch->k_trans.a, ref->n*sizeof((*(ref->a))));
    kv_resize(uint64_t, ref->idx, idx->t_ch->k_trans.idx.n); ref->idx.n = idx->t_ch->k_trans.idx.n;
    memcpy(ref->idx.a, idx->t_ch->k_trans.idx.a, ref->idx.n*sizeof((*(ref->idx.a))));
    exchange_kv_u_trans_t(ref, &(idx->t_ch->k_trans));

    for (k = 0; k < max_round; k++) {
        clean_trans_ovlp(bub, &(idx->t_ch->k_trans), ref, ((k+1)<max_round)?srt:NULL, ug);
        purge_phase_0(idx, link, bub, s, hits, k_trans, del, hh);
    }

    for (k = len = 0; k < ug->g->n_seq; k++) {
        if(del[k] != 2) {
            if((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;
        }
        assert(hh->h.a[k].n < hh->h.a[k].m);
        hh->a.a[hh->h.a[k].a+hh->h.a[k].n] = hapid;
        hh->h.a[k].n++; len += ug->g->seq[k].len;
    }
    fprintf(stderr, "[M::%s::stat] # hap%lu bases: %lu\n", __func__, hapid+1, len);
    
    bm = bub->index; bub->index = bidx; bidx = bm;
    exchange_kv_u_trans_t(ref, &(idx->t_ch->k_trans));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq (%rdi), %r12
movq 0x18(%r12), %rax
movl 0x14(%rax), %esi
testq %rcx, %rcx
jne 0x15c48d
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x15c47e
movq 0x10(%r14), %rcx
xorl %r8d, %r8d
movl (%rcx), %esi
cmpl 0x4(%rcx), %esi
jae 0x15c469
movq 0x28(%r14), %rdi
addl 0x8(%rcx), %esi
movl %edx, (%rdi,%rsi,4)
incl (%rcx)
incq %r8
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq $0xc, %rcx
cmpq %rsi, %r8
jb 0x15c456
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movq %rcx, 0x20(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xb8(%rsp), %r11
movq 0xb0(%rsp), %rax
movq 0xa8(%rsp), %r15
movq 0xa0(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x98(%rsp), %rbp
movq 0x90(%rsp), %r13
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x15c55f
movl $0x1, %ecx
xorl %r10d, %r10d
xorl %esi, %esi
movb %r10b, (%r15,%rsi)
movq (%rbp), %rdi
movb %r10b, (%rdi,%rsi)
movq 0x10(%r14), %r8
movl -0x4(%r8,%rcx,4), %r9d
movq (%r13), %rdi
cmpl (%r8,%rcx,4), %r9d
jae 0x15c52f
movl (%rdi,%rsi,4), %r8d
movq 0x80(%r13), %r9
incq %r9
cmpq %r8, %r9
jae 0x15c53b
movl 0x30(%r13), %r8d
movl %r8d, (%rdi,%rsi,4)
jmp 0x15c53b
movl $0xffffffff, (%rdi,%rsi,4) # imm = 0xFFFFFFFF
movb $0x1, (%r15,%rsi)
movq (%r13), %rdi
movl (%rdi,%rsi,4), %edi
movl %edi, (%rax,%rsi,4)
incq %rsi
movq 0x18(%r12), %rdi
movl 0x14(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x3, %rcx
cmpq %rdi, %rsi
jb 0x15c4f3
movq %rdx, 0x8(%rsp)
movq (%r13), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, (%r13)
movq 0x10(%rbx), %rax
movq 0x138(%rax), %rax
cmpq %rax, 0x8(%r11)
movq %r11, (%rsp)
jae 0x15c5da
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x8(%r11)
movq 0x10(%r11), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq (%rsp), %r11
movq %rax, 0x10(%r11)
movq %r12, 0x10(%rsp)
movq 0x10(%rbx), %rax
movq 0x138(%rax), %rcx
movq %rcx, (%r11)
movq 0x10(%r11), %rdi
movq 0x148(%rax), %rsi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rdx
callq 0x2160
movq (%rsp), %r12
movq 0x10(%rbx), %rax
movq 0x150(%rax), %rax
cmpq %rax, 0x20(%r12)
jae 0x15c673
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x20(%r12)
movq 0x28(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r12
movq %rax, 0x28(%r12)
movq 0x10(%rbx), %rax
movq 0x150(%rax), %rdx
movq %rdx, 0x18(%r12)
movq 0x28(%r12), %rdi
movq 0x160(%rax), %rsi
shlq $0x3, %rdx
callq 0x2160
movl $0x138, %eax            # imm = 0x138
movq 0x10(%rbx), %rsi
addq %rax, %rsi
movq %r12, %rdi
callq 0x15bf0e
xorl %eax, %eax
movq 0x20(%rsp), %rcx
cmpq $0x1, %rcx
sbbq %rcx, %rax
movq %rax, 0x30(%rsp)
movq %rbx, 0x18(%rsp)
movl $0x1, %ebx
movq %rbp, %r12
movq 0x18(%rsp), %rbp
movq 0x10(%rbp), %rsi
movl $0x138, %eax            # imm = 0x138
addq %rax, %rsi
cmpq %rcx, %rbx
movl $0x0, %ecx
cmovbq 0x40(%rsp), %rcx
movq %r13, %rdi
movq (%rsp), %rdx
movq 0x10(%rsp), %r8
callq 0x15c01c
movq %rbp, %rdi
movq %r12, %rbp
movq 0x48(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r14
pushq %r15
callq 0x1c2cd
movq 0x30(%rsp), %rcx
addq $0x10, %rsp
movq 0x30(%rsp), %rax
addq %rbx, %rax
incq %rax
incq %rbx
cmpq $0x1, %rax
jne 0x15c6c9
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
jne 0x15c761
xorl %r8d, %r8d
movq 0x8(%rsp), %rcx
jmp 0x15c7e2
xorl %r11d, %r11d
leaq 0x12dfed(%rip), %rdx    # 0x28a758
xorl %r8d, %r8d
xorl %esi, %esi
movq 0x8(%rsp), %rcx
cmpb $0x2, (%r15,%rsi)
jne 0x15c7a2
movq 0x10(%r14), %rdi
movl (%rdi,%r11), %r9d
cmpl 0x4(%rdi,%r11), %r9d
jae 0x15c82d
movq 0x28(%r14), %r10
addl 0x8(%rdi,%r11), %r9d
movl %ecx, (%r10,%r9,4)
incl (%rdi,%r11)
jmp 0x15c7bd
movq 0x10(%r14), %r9
movl 0x4(%r9,%r11), %edi
cmpl %edi, (%r9,%r11)
jne 0x15c7cd
movslq 0x1bc(%rdx), %r9
cmpq %r9, %rdi
jne 0x15c7cd
movq 0x20(%rax), %rdi
movl (%rdi,%rsi,8), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq %rdi, %r8
incq %rsi
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0xc, %r11
cmpq %rdi, %rsi
jb 0x15c775
movq 0x12d7f7(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
incq %rcx
leaq 0xd4145(%rip), %rsi     # 0x23093b
leaq 0xd4161(%rip), %rdx     # 0x23095e
xorl %eax, %eax
callq 0x22b0
movq 0x28(%rsp), %rax
movq %rax, (%r13)
movl $0x138, %esi            # imm = 0x138
addq 0x10(%rbx), %rsi
movq (%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x15bf0e
leaq 0xd4030(%rip), %rdi     # 0x230864
leaq 0xd4045(%rip), %rsi     # 0x230880
leaq 0xd4046(%rip), %rcx     # 0x230888
movl $0x44ef, %edx           # imm = 0x44EF
callq 0x2110