// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
callq 0x20f140
movq %rax, %rbx
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x18(%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, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpq $0x0, (%rbx)
je 0x2136a0
movq 0x18(%rbx), %rax
movq 0x20(%rax), %rax
movb $0x0, 0x4(%rax,%r13,8)
movq 0x10(%rbx), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0x213694
leaq (%rax,%rcx,8), %rbp
cmpl $0x0, 0x10(%rbp)
je 0x213659
movq 0x18(%rbp), %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)
testq %rdx, %rdx
jne 0x21363a
movl %edi, %r8d
notl %r8d
andl $0x1, %r8d
movl 0x18(%rsp,%r8,4), %r8d
orl %ecx, %r8d
movl %r8d, (%r14,%rsi,4)
incq %rdx
movl 0x10(%rbp), %r8d
cmpq %r8, %rdx
jne 0x213651
andl $0x1, %edi
movl 0x18(%rsp,%rdi,4), %edi
orl %edi, (%r14,%rsi,4)
movl 0x10(%rbp), %esi
cmpq %rsi, %rdx
jb 0x21360c
movq 0x18(%r15), %rsi
movq 0x20(%r15), %rdx
movq %rbp, %rdi
movq %r12, %rcx
leaq 0x20(%rsp), %r8
callq 0x213850
movl (%rbp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x18(%rbx), %rcx
movq 0x20(%rcx), %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, (%rbx)
ja 0x2135cc
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x28(%r15), %rdi
callq 0x1380d4
movq 0x28(%r15), %rdi
callq 0x2220
movl $0x1, %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2120
movq %rax, 0x28(%r15)
leaq 0x77082(%rip), %r13     # 0x28a758
cmpl $0x2, 0x1bc(%r13)
movq (%r15), %rdx
movq 0x20(%r15), %rdi
movq 0x58(%r15), %rcx
movq 0x8(%rdx), %r8
movq 0x30(%rdx), %r9
jg 0x213708
movq $0x0, (%rsp)
movq %rbx, %rsi
movq %rax, %rdx
callq 0x139982
jmp 0x21371b
movq $0x0, (%rsp)
movq %rbx, %rsi
movq %rax, %rdx
callq 0x13abb6
cmpq $0x0, 0x8(%r12)
movl 0xc(%rsp), %r11d
je 0x2137c5
xorl %eax, %eax
movl $0x1, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl (%r14,%rsi,4), %r8d
cmpq %rdx, %r8
je 0x2137b3
movl %r8d, %edi
shrl $0x2, %edi
movq 0x10(%rbx), %r9
leaq (%rdi,%rdi,4), %r10
cmpl %r11d, 0x10(%r9,%r10,8)
jb 0x2137a4
movq 0x28(%r15), %r9
movq (%r9), %r10
movq 0x80(%r9), %r9
movl (%r10,%rdi,4), %r10d
incq %r9
cmpq %r10, %r9
jae 0x21377c
cmpl $0x0, 0x130(%r13)
jne 0x2137a4
testb $0x1, %r8b
je 0x21378f
movl %eax, %r8d
movq 0x10(%rsp), %r9
movb $0x2, (%r9,%r8)
testb $0x2, (%r14,%rsi,4)
je 0x2137a4
leal 0x1(%rax), %r8d
movq 0x10(%rsp), %r9
movb $0x2, (%r9,%r8)
movq 0x28(%r15), %r8
movq (%r8), %r8
movl (%r8,%rdi,4), %edi
movl %edi, (%r14,%rsi,4)
movl %ecx, %esi
addl $0x2, %eax
incl %ecx
cmpq %rsi, 0x8(%r12)
ja 0x21373a
movq 0x28(%r15), %rax
movq (%rax), %rdi
callq 0x2220
movq 0x8(%r12), %r12
shlq $0x2, %r12
movq %r12, %rdi
callq 0x2280
movq 0x28(%r15), %r15
movq %rax, (%r15)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2160
cmpl $0x0, 0x130(%r13)
jne 0x213839
movq 0x18(%rbx), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x213839
movq (%r15), %rcx
movq 0x80(%r15), %rdx
incq %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpq %rdi, %rdx
jae 0x213828
movl %edx, (%rcx,%rsi,4)
incq %rsi
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmpq %rdi, %rsi
jb 0x21381d
movq %rbx, %rdi
callq 0xa0004
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq