// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movl %r8d, 0x1c(%rsp)
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
callq 0x1cbbbd
movq %rax, %rbx
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x28(%rsp)
movq 0x8(%r12), %rdx
shlq $0x2, %rdx
movq %r14, %rdi
movl $0xff, %esi
callq 0x20e0
movq %r12, 0x10(%rsp)
movq 0x8(%r12), %rdx
addq %rdx, %rdx
xorl %r13d, %r13d
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq %rbx, %rsi
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq (%rbx), %r12
movq %rbx, 0x8(%rsp)
movl %r13d, %r13d
cmpq %r13, %r12
jbe 0x1cfc83
movq 0x18(%rsi), %rbx
movq 0x20(%rbx), %rax
movb $0x0, 0x4(%rax,%r13,8)
movq 0x10(%rsi), %rbp
imulq $0x28, %r13, %rax
cmpl $0x0, 0xc(%rbp,%rax)
je 0x1cfc7b
addq %rax, %rbp
leal (,%r13,4), %eax
xorl %ecx, %ecx
movl 0x10(%rbp), %edx
cmpq %rdx, %rcx
jae 0x1cfc3d
movq 0x18(%rbp), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
shrq $0x21, %rdx
movl %eax, (%r14,%rdx,4)
movl %eax, %edi
testq %rcx, %rcx
jne 0x1cfc24
movl %esi, %edi
notl %edi
andl $0x1, %edi
movl 0x28(%rsp,%rdi,4), %edi
orl %eax, %edi
movl %edi, (%r14,%rdx,4)
incq %rcx
movl 0x10(%rbp), %r8d
cmpq %r8, %rcx
jne 0x1cfbed
andl $0x1, %esi
orl 0x28(%rsp,%rsi,4), %edi
movl %edi, (%r14,%rdx,4)
jmp 0x1cfbed
movq 0x18(%r15), %rsi
movq 0x20(%r15), %rdx
movq %rbp, %rdi
movq 0x10(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x1cfe19
movq 0x8(%rsp), %rsi
movl (%rbp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x20(%rbx), %rcx
movl (%rcx,%r13,8), %edx
movl $0x80000000, %edi       # imm = 0x80000000
andl %edi, %edx
orl %eax, %edx
movl %edx, (%rcx,%r13,8)
incl %r13d
jmp 0x1cfbb3
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x28(%r15), %rdi
callq 0xf6a60
movq 0x28(%r15), %rdi
callq 0x2220
pushq $0x1
popq %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x2120
movq %rax, 0x28(%r15)
leaq 0x83aa1(%rip), %rbp     # 0x253758
cmpl $0x2, 0x1bc(%rbp)
movq (%r15), %rdx
movq 0x20(%r15), %rdi
movq 0x58(%r15), %rcx
movq 0x8(%rdx), %r8
movq 0x30(%rdx), %r9
jg 0x1cfce7
andq $0x0, (%rsp)
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0xf80df
jmp 0x1cfcf9
andq $0x0, (%rsp)
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0xf90db
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r12
xorl %eax, %eax
pushq $0x1
popq %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r10
movl 0x1c(%rsp), %r11d
movl %eax, %eax
cmpq %rax, 0x8(%rbx)
jbe 0x1cfd93
movl (%r14,%rax,4), %edi
cmpq %rdx, %rdi
je 0x1cfd8c
movl %edi, %esi
shrl $0x2, %esi
movq 0x10(%r10), %r8
imulq $0x28, %rsi, %r9
cmpl %r11d, 0x10(%r8,%r9)
jb 0x1cfd7e
movq 0x28(%r15), %r8
movq (%r8), %r9
movq 0x80(%r8), %r8
movl (%r9,%rsi,4), %r9d
incq %r8
cmpq %r9, %r8
jae 0x1cfd5f
cmpl $0x0, 0x130(%rbp)
jne 0x1cfd7e
testb $0x1, %dil
je 0x1cfd71
leal -0x1(%rcx), %edi
movb $0x2, (%r12,%rdi)
movl (%r14,%rax,4), %edi
testb $0x2, %dil
je 0x1cfd7e
movl %ecx, %edi
movb $0x2, (%r12,%rdi)
movq 0x28(%r15), %rdi
movq (%rdi), %rdi
movl (%rdi,%rsi,4), %esi
movl %esi, (%r14,%rax,4)
incl %eax
addl $0x2, %ecx
jmp 0x1cfd17
movq 0x28(%r15), %rax
movq (%rax), %rdi
callq 0x2220
movq 0x8(%rbx), %r13
shlq $0x2, %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %r12
movq 0x28(%r15), %rbx
movq %rax, (%rbx)
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2160
movq 0x8(%rsp), %rdi
cmpl $0x0, 0x130(%rbp)
je 0x1cfde9
callq 0x6921d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rdi), %rax
xorl %ecx, %ecx
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jae 0x1cfdd5
movl (%r12,%rcx,4), %esi
movq 0x80(%rbx), %rdx
incq %rdx
cmpq %rsi, %rdx
jae 0x1cfe14
movl %edx, (%r12,%rcx,4)
incq %rcx
jmp 0x1cfdef