// File: /workspace/github_cpp_makefile_star2_O3/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), %r11d
testq %rcx, %rcx
jne 0x162f3a
testl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
je 0x162f2b
movq 0x10(%r14), %r8
xorl %esi, %esi
movl (%r8), %edi
cmpl 0x4(%r8), %edi
jae 0x162f16
movq 0x28(%r14), %rcx
addl 0x8(%r8), %edi
movl %edx, (%rcx,%rdi,4)
incl (%r8)
movl 0x14(%rax), %r11d
incq %rsi
movl %r11d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0xc, %r8
cmpq %rdi, %rsi
jb 0x162efb
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbp
movq %rcx, 0x20(%rsp)
movq %r8, 0x48(%rsp)
movq 0xc0(%rsp), %r10
movq 0xb8(%rsp), %rax
movq %rax, (%rsp)
movq 0xb0(%rsp), %rax
movq 0xa8(%rsp), %r15
movq 0xa0(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %r13
testl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq %rdi, 0x28(%rsp)
jne 0x162f9c
movq (%r13), %rbx
jmp 0x163014
movl $0x1, %ecx
xorl %r11d, %r11d
xorl %esi, %esi
movb %r11b, (%r15,%rsi)
movq (%rdi), %rdi
movb %r11b, (%rdi,%rsi)
movq 0x10(%r14), %rdi
movl -0x4(%rdi,%rcx,4), %r8d
movq (%r13), %rbx
cmpl (%rdi,%rcx,4), %r8d
jae 0x162fdf
movl (%rbx,%rsi,4), %edi
movq 0x80(%r13), %r8
incq %r8
cmpq %rdi, %r8
jae 0x162ff2
movl 0x30(%r13), %edi
movl %edi, (%rbx,%rsi,4)
jmp 0x162ff2
movl $0xffffffff, (%rbx,%rsi,4) # imm = 0xFFFFFFFF
movb $0x1, (%r15,%rsi)
movq (%r13), %rbx
movl (%rbx,%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
movq 0x28(%rsp), %rdi
jb 0x162fa6
movq %rax, (%r13)
movq 0x10(%rbp), %rcx
movq 0x138(%rcx), %rax
movq (%rsp), %rsi
cmpq %rax, 0x8(%rsi)
movq %rdx, 0x8(%rsp)
movq %r9, 0x50(%rsp)
movq %r10, 0x40(%rsp)
jae 0x1630a0
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(%rsi)
movq 0x10(%rsi), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq (%rsp), %rsi
movq %rax, %rdi
movq %rax, 0x10(%rsi)
movq 0x10(%rbp), %rcx
movq 0x138(%rcx), %rax
jmp 0x1630a4
movq 0x10(%rsi), %rdi
movq %rax, (%rsi)
movq 0x148(%rcx), %rsi
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2160
movq (%rsp), %r8
movq 0x10(%rbp), %rax
movq 0x150(%rax), %rdx
cmpq %rdx, 0x20(%r8)
jae 0x163135
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%r8)
movq 0x28(%r8), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, %rdi
movq %rax, 0x28(%r8)
movq 0x10(%rbp), %rax
movq 0x150(%rax), %rdx
jmp 0x163139
movq 0x28(%r8), %rdi
movq %r12, 0x10(%rsp)
movq %rdx, 0x18(%r8)
movq 0x160(%rax), %rsi
shlq $0x3, %rdx
movq %r8, %r12
callq 0x2160
movl $0x138, %eax            # imm = 0x138
movq 0x10(%rbp), %rsi
addq %rax, %rsi
movq %r12, %rdi
callq 0x1629c6
movq 0x20(%rsp), %rcx
movq %rcx, %rax
negq %rax
movq %rax, 0x30(%rsp)
movq %rbp, 0x18(%rsp)
movl $0x1, %ebp
movq 0x18(%rsp), %r12
movq 0x10(%r12), %rsi
movl $0x138, %eax            # imm = 0x138
addq %rax, %rsi
cmpq %rcx, %rbp
movl $0x0, %ecx
cmovbq 0x40(%rsp), %rcx
movq %r13, %rdi
movq (%rsp), %rdx
movq 0x10(%rsp), %r8
callq 0x162ab4
movq %r12, %rdi
movq 0x48(%rsp), %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r14
pushq %r15
callq 0x1bc06
movq 0x30(%rsp), %rcx
addq $0x10, %rsp
movq 0x30(%rsp), %rax
addq %rbp, %rax
incq %rax
incq %rbp
cmpq $0x1, %rax
jne 0x163188
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jne 0x16321d
xorl %r8d, %r8d
movq 0x8(%rsp), %rcx
movq %r9, %r12
jmp 0x1632a9
movq 0x10(%r14), %r11
xorl %r8d, %r8d
leaq 0x13252d(%rip), %rdx    # 0x295758
xorl %esi, %esi
movq 0x8(%rsp), %rcx
movq %r9, %r12
cmpb $0x2, (%r15,%rsi)
movl (%r11), %r9d
movl 0x4(%r11), %r10d
jne 0x163270
cmpl %r10d, %r9d
jae 0x1632f0
movq 0x28(%r14), %rdi
addl 0x8(%r11), %r9d
movl %ecx, (%rdi,%r9,4)
incl (%r11)
movq 0x20(%rax), %rdi
movl (%rdi,%rsi,8), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq %rdi, %r8
movl 0x14(%rax), %edi
jmp 0x163293
cmpl %r10d, %r9d
jne 0x163293
movslq 0x1bc(%rdx), %r9
cmpq %r9, %r10
jne 0x163293
movq 0x20(%rax), %r9
movl (%r9,%rsi,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %r8
incq %rsi
movl %edi, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq $0xc, %r11
cmpq %r9, %rsi
jb 0x163235
movq 0x131d30(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
incq %rcx
leaq 0xd988a(%rip), %rsi     # 0x23cb47
leaq 0xd98a6(%rip), %rdx     # 0x23cb6a
xorl %eax, %eax
callq 0x22b0
movq %rbx, (%r13)
movl $0x138, %esi            # imm = 0x138
addq 0x10(%r12), %rsi
movq (%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1629c6
leaq 0xd9779(%rip), %rdi     # 0x23ca70
leaq 0xd978e(%rip), %rsi     # 0x23ca8c
leaq 0xd978f(%rip), %rcx     # 0x23ca94
movl $0x44ef, %edx           # imm = 0x44EF
callq 0x2110