// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: renew_usg_t_bub(ul_resolve_t*, usg_t*, unsigned int*, unsigned char*, unsigned int)
void renew_usg_t_bub(ul_resolve_t *uidx, usg_t *ng, uint32_t *id_map, uint8_t *ff, uint32_t rocc_cut)
{
    ma_ug_t *ug = ma_ug_hybrid_gen(ng); 
    uint32_t i, k, v, p[2]; ma_utg_t *u; p[0] = 1; p[1] = 2;
    memset(id_map, -1, sizeof((*id_map))*ng->n);
    memset(ff, 0, sizeof((*ff))*(ng->n<<1));
    kvec_asg_arc_t_warp e; kv_init(e.a); e.i = 0;
    // for (i = dn = 0; i < ng->n; i++) {
    //     if(ng->a[i].del) continue;
    //     dn++;
    // }
    // fprintf(stderr, "ng->n::%u, ug->u.n::%u, dn::%u\n", (uint32_t)ng->n, (uint32_t)ug->u.n, dn);
    // dn = 0
    for (i = 0; i < ug->u.n; i++) {
        ug->g->seq[i].c = PRIMARY_LABLE;
        u = &(ug->u.a[i]);
        if(u->m == 0) continue;
        for (k = 0; k < u->n; k++) {
            v = u->a[k]>>32; 
            id_map[v>>1] = i<<2; 
            if(k == 0) id_map[v>>1] |= p[(v^1)&1];
            if(k + 1 == u->n) id_map[v>>1] |= p[(v)&1];
            // dn++;
        }
        // fprintf(stderr, "+[M::%s] i::%u\n", __func__, i);
        merge_hybrid_utg_content(u, uidx->l1_ug, uidx->sg, ng, &e);
        // fprintf(stderr, "-[M::%s] i::%u\n", __func__, i);
        ug->g->seq[i].len = u->len;
    }
    kv_destroy(e.a);

    destory_bubbles(uidx->bub); free(uidx->bub); CALLOC(uidx->bub, 1);
    // fprintf(stderr, "[M::%s] homozygous read coverage threshold: %d\n", __func__, asm_opt.hom_global_coverage_set?
    //                         asm_opt.hom_global_coverage:(int)(((double)asm_opt.hom_global_coverage)/((double)HOM_PEAK_RATE)));
    // identify_bubbles(ug, uidx->bub, uidx->r_het, NULL);
    if(asm_opt.polyploidy <= 2) {
        identify_bubbles_recal(uidx->sg, ug, uidx->bub, uidx->r_het, uidx->uopt->sources, uidx->uopt->ruIndex, NULL);
    } else {
        identify_bubbles_recal_poy(uidx->sg, ug, uidx->bub, uidx->r_het, uidx->uopt->sources, uidx->uopt->ruIndex, NULL);
    }
    
    // fprintf(stderr, "0[M::%s::] f[51]::%u\n", __func__, ff[51]);
    for (i = 0; i < ng->n; i++) {
        if(id_map[i] != (uint32_t)-1) {
            k = id_map[i]>>2; 
            // fprintf(stderr, "k::%u, ng->n::%u, ug->u.n::%u, dn::%u\n", k, (uint32_t)ng->n, (uint32_t)ug->u.n, dn);
            // if(i == 7075 || i == 28174 || i == 77111 || i == 3826 || i == 12150 || i == 58312 || i == 59190 || i == 72134) {
            //     fprintf(stderr, "[M::%s::k->%u] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u, is_hom::%u\n", __func__, k, 
            //         i+1, "+-"[0], (uint32_t)ug->u.a[k].n, rocc_cut, IF_HOM(k, (*(uidx->bub))));
            // }
            if((ug->u.a[k].n >= rocc_cut) && 
                            ((!(IF_HOM(k, (*(uidx->bub))))) || (asm_opt.purge_level_primary == 0))) {
                if(id_map[i]&p[0]) {
                    ff[i<<1] = 2;
                    // fprintf(stderr, "[M::%s::] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u\n", __func__, 
                    // i+1, "+-"[0], (uint32_t)ug->u.a[k].n, rocc_cut);
                }
                if(id_map[i]&p[1]) {
                    ff[(i<<1)+1] = 2;
                    // fprintf(stderr, "[M::%s::] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u\n", __func__, 
                    // i+1, "+-"[1], (uint32_t)ug->u.a[k].n, rocc_cut);
                }
            }
            id_map[i] = uidx->bub->index[k];
        }
    }
    // fprintf(stderr, "1[M::%s::] f[51]::%u\n", __func__, ff[51]);
    free(uidx->bub->index); MALLOC(uidx->bub->index, ng->n); 
    memcpy(uidx->bub->index, id_map, sizeof((*id_map))*ng->n);
    if(asm_opt.purge_level_primary == 0) {///all nodes are het
        for (k = 0; k < ug->g->n_seq; k++) {
            if(IF_HOM(k, *(uidx->bub))) uidx->bub->index[k] = uidx->bub->f_bub+1;
        }
    }
    ma_ug_destroy(ug);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r8d, 0x14(%rsp)
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
callq 0x21a43d
movq %rax, %rbx
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x30(%rsp)
movq 0x8(%r12), %rdx
shlq $0x2, %rdx
movq %r14, %rdi
movl $0xff, %esi
callq 0x20e0
movq 0x8(%r12), %rdx
addq %rdx, %rdx
xorl %r13d, %r13d
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq (%rbx), %rbp
testq %rbp, %rbp
movq %rbx, 0x38(%rsp)
je 0x21eb0a
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movq 0x10(%rbx), %r12
movq 0x18(%rbx), %r15
movq 0x20(%r15), %rax
movb $0x0, 0x4(%rax,%r13,8)
leaq (,%r13,4), %rax
addq %r13, %rax
cmpl $0x0, 0xc(%r12,%rax,8)
je 0x21eaef
leaq (%r12,%rax,8), %rbx
cmpl $0x0, 0x10(%rbx)
je 0x21eab2
movq 0x18(%rbx), %rax
leal (,%r13,4), %ecx
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
shrq $0x21, %rsi
movl %ecx, (%r14,%rsi,4)
movl %ecx, %r8d
testq %rdx, %rdx
jne 0x21ea91
movl %edi, %r8d
notl %r8d
andl $0x1, %r8d
movl 0x30(%rsp,%r8,4), %r8d
orl %ecx, %r8d
movl %r8d, (%r14,%rsi,4)
incq %rdx
movl 0x10(%rbx), %r9d
cmpq %r9, %rdx
jne 0x21eaad
andl $0x1, %edi
orl 0x30(%rsp,%rdi,4), %r8d
movl %r8d, (%r14,%rsi,4)
movl 0x10(%rbx), %r9d
cmpq %r9, %rdx
jb 0x21ea60
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0x21ecc9
movl (%rbx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x20(%r15), %rcx
movl (%rcx,%r13,8), %edx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %edx
orl %eax, %edx
movl %edx, (%rcx,%r13,8)
incl %r13d
cmpq %r13, %rbp
ja 0x21ea27
movq 0x50(%rsp), %r13
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r12
movq %r13, %rdi
callq 0x2220
movq 0x28(%r15), %rdi
callq 0x13dea7
movq 0x28(%r15), %rdi
callq 0x2220
movl $0x1, %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2120
movq %rax, 0x28(%r15)
leaq 0x76c1a(%rip), %r13     # 0x295758
cmpl $0x2, 0x1bc(%r13)
movq (%r15), %rdx
movq 0x20(%r15), %rdi
movq 0x58(%r15), %rcx
movq 0x8(%rdx), %r8
movq 0x30(%rdx), %r9
movq 0x38(%rsp), %rbp
jg 0x21eb75
movq $0x0, (%rsp)
movq %rbp, %rsi
movq %rax, %rdx
callq 0x13f794
jmp 0x21eb88
movq $0x0, (%rsp)
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1409f5
movq 0x8(%r12), %rdi
testq %rdi, %rdi
movl 0x14(%rsp), %r11d
je 0x21ec3a
xorl %eax, %eax
movl $0x1, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl (%r14,%rsi,4), %r8d
cmpq %rdx, %r8
je 0x21ec2a
movl %r8d, %edi
shrl $0x2, %edi
movq 0x10(%rbp), %r9
leaq (%rdi,%rdi,4), %r10
cmpl %r11d, 0x10(%r9,%r10,8)
jb 0x21ec16
movq 0x28(%r15), %r9
movq (%r9), %r10
movq 0x80(%r9), %r9
movl (%r10,%rdi,4), %r10d
incq %r9
cmpq %r10, %r9
jae 0x21ebeb
cmpl $0x0, 0x130(%r13)
jne 0x21ec16
testb $0x1, %r8b
je 0x21ec02
movl %eax, %r8d
movq 0x18(%rsp), %r9
movb $0x2, (%r9,%r8)
movl (%r14,%rsi,4), %r8d
testb $0x2, %r8b
je 0x21ec16
leal 0x1(%rax), %r8d
movq 0x18(%rsp), %r9
movb $0x2, (%r9,%r8)
movq 0x28(%r15), %r8
movq (%r8), %r8
movl (%r8,%rdi,4), %edi
movl %edi, (%r14,%rsi,4)
movq 0x8(%r12), %rdi
movl %ecx, %esi
addl $0x2, %eax
incl %ecx
cmpq %rsi, %rdi
ja 0x21eba9
movq 0x28(%r15), %rax
movq (%rax), %rdi
callq 0x2220
movq 0x8(%r12), %r12
shlq $0x2, %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %rbx
movq 0x28(%r15), %r15
movq %rax, (%r15)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2160
cmpl $0x0, 0x130(%r13)
jne 0x21ecb2
movq 0x18(%rbp), %rax
movl 0x14(%rax), %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x21ecb2
movq 0x80(%r15), %rdx
incq %rdx
xorl %esi, %esi
movl (%rbx,%rsi,4), %edi
cmpq %rdi, %rdx
jae 0x21eca2
movl %edx, (%rbx,%rsi,4)
movl 0x14(%rax), %ecx
incq %rsi
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmpq %rdi, %rsi
jb 0x21ec94
movq %rbp, %rdi
callq 0xa0761
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq