// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: u2g_hybrid_detan(ul_resolve_t*, usg_t*, unsigned int, asg64_v*, asg64_v*)
void u2g_hybrid_detan(ul_resolve_t *uidx, usg_t *ng, uint32_t max_ext, asg64_v *in, asg64_v *ib)
{
    uint64_t k, i, x, m, *tmp, sn; asg64_v tx = {0,0,0}, tb = {0,0,0}, *ob = NULL, *ub = NULL; 
    ob = (in?(in):(&tx)); ub = (ib?(ib):(&tb)); ob->n = ub->n = 0;

    for (k = 0; k < uidx->str_b.n_thread; k++) {
        uidx->str_b.buf[k].res_dump.n = uidx->str_b.buf[k].u.n = uidx->str_b.buf[k].o.n = 0;
    }
    kt_for(uidx->str_b.n_thread, worker_integer_realign_g, uidx, uidx->uovl.i_ug->u.n);

    for (k = ob->n = ub->n = m = 0; k < uidx->str_b.n_thread; k++) {
        for (i = 0; i < uidx->str_b.buf[k].res_dump.n; i++) {
            x = uidx->str_b.buf[k].res_dump.a[i];
            kv_push(uint64_t, *ob, x);//aln details
            if(x&((uint64_t)0x8000000000000000)) {
                x -= ((uint64_t)0x8000000000000000); x >>= 32; x <<= 32;//seq_id
                x |= ob->n;//offset
                kv_push(uint64_t, *ub, x);///idx:: seq_id|offset_in_ob
            } else {
                m++;
            }
        }
    }

    radix_sort_srt64(ub->a, ub->a + ub->n);
    kv_resize(uint64_t, *ub, ub->n+m); tmp = ub->a + ub->n; m = 0;
    for (k = 0; k < ub->n; k++) {
        sn = ((uint32_t)(ob->a[((uint32_t)ub->a[k])-1]));
        memcpy(tmp + m, ob->a + ((uint32_t)ub->a[k]), sn*sizeof((*tmp))); 
        ub->a[k] = m; ub->a[k] <<= 32; ub->a[k] |= sn;//offset_in_ob|occ
        m += sn;
    }
    assert(m <= ob->n);
    memcpy(ob->a, tmp, m*sizeof((*tmp))); ob->n = m;


    // for (k = 0, p = NULL; k < ub->n; k++) {
    //     p = &(ob->a[((uint32_t)ub->a[k])-1]);
    //     x = ub->a[k]<<32;//offset
    //     x |= ((uint32_t)(*p));///occ
    //     ub->a[k] = x;
    //     (*p) >>= 32; (*p) <<= 32; (*p) |= k; 
    // }
    // for (k = m = 0; k < ob->n; k++) {
    //     if(ob->a[k]&((uint64_t)0x8000000000000000)) {
    //         x = m; x <<= 32; x |= ((uint32_t)ub->a[(uint32_t)ob->a[k]]);
    //         ub->a[(uint32_t)ob->a[k]] = x;
    //     } else {
    //         ob->a[m++] = ob->a[k];
    //     }
    // }
    // ob->n = m;

    // for (k = ob->n = ub->n = 0; k < uidx->str_b.n_thread; k++) {
    //     for (i = 0; i < uidx->str_b.buf[k].res_dump.n; i++) {
    //         if((uidx->str_b.buf[k].res_dump.a[i]>>32)==((uint32_t)-1)) {
    //             x = ob->n; x <<= 32; x |= ((uint32_t)uidx->str_b.buf[k].res_dump.a[i]);
    //             kv_push(uint64_t, *ub, x);///idx:: offset_in_ob|occ
    //         } else {
    //             kv_push(uint64_t, *ob, uidx->str_b.buf[k].res_dump.a[i]);//aln details
    //         }
    //     }        
    // }

    /*********debugging*********/
    fprintf(stderr, "[M::%s::] # iug::%u, # gchain::%u\n", __func__, (uint32_t)uidx->uovl.i_ug->u.n, (uint32_t)ub->n);
    // for (k = 0; k < ub->n; k++) {
    //     // fprintf(stderr, "[M::%s::k->%lu] # iug::%u, # chain::%u, off chain::%u\n", 
    //     // __func__, k, (uint32_t)uidx->uovl.cc.iug_a[k].n, (uint32_t)ub->a[k], (uint32_t)(ub->a[k]>>32));
    //     for (i = 0; i < (uint32_t)ub->a[k]; i++) {
    //         // fprintf(stderr, "utg%.6dl(%c) <------> utg%.6dl(%c)\n", 
    //         // ((int32_t)(uidx->uovl.cc.iug_a[k].a[i].v>>1))+1, "+-"[uidx->uovl.cc.iug_a[k].a[i].v&1],
    //         // ((int32_t)(ob->a[(ub->a[k]>>32)+i]>>1))+1, "+-"[ob->a[(ub->a[k]>>32)+i]&1]);
    //         assert(uidx->uovl.cc.iug_a[k].a[i].v == ob->a[(ub->a[k]>>32)+i]);
    //     }
    // }
    /*********debugging*********/

    ///ub->idx; ob->nodes
    // u_ug = gen_unique_g(uidx, ng, ng_occ, ub->a, ub->n, ob->a);//ma_ug_hybrid_gen(ng);
    ///debug
    debug_sysm_usg_t(ng, __func__);
    // prt_usg_t(uidx, ng, "ng4");
    if(gen_unique_g_adv(uidx, ng, ub->a, ub->n, ob->a, max_ext)) {
        // usg_arc_t *z = get_usg_arc(ng, 2, 576), *q = get_usg_arc(ng, 577, 3);
        // fprintf(stderr, "xxxx0xxx[M::%s::] p->del::%u, q->del::%u\n", 
        //             __func__, z?z->del:1, q?q->del:1);
        ///debug
        debug_sysm_usg_t(ng, __func__);
        // z = get_usg_arc(ng, 2, 576); q = get_usg_arc(ng, 577, 3);
        // fprintf(stderr, "xxxx1xxx[M::%s::] p->del::%u, q->del::%u\n", 
        //             __func__, z?z->del:1, q?q->del:1);
        // fprintf(stderr, "+[M::%s::] ng->n::%u\n", __func__, (uint32_t)ng->n);
        usg_cleanup(ng);
        ///debug
        debug_sysm_usg_t(ng, __func__);
        // fprintf(stderr, "-[M::%s::] ng->n::%u\n", __func__, (uint32_t)ng->n);
    }
    // prt_usg_t(uidx, ng, "ng_dbg");
    if(!in) free(tx.a); if(!ib) free(tb.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, %r14
movl %edx, 0x54(%rsp)
movq %rdi, 0x10(%rsp)
testq %r8, %r8
je 0x21d6f2
movq $0x0, (%r15)
testq %r14, %r14
je 0x21d6fe
movq $0x0, (%r14)
movq %rsi, 0x40(%rsp)
movq 0x10(%rsp), %rax
movq 0x1e0(%rax), %rdi
testq %rdi, %rdi
je 0x21d742
movq 0x10(%rsp), %rax
movq 0x1d8(%rax), %rax
addq $0x78, %rax
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rcx, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x288(%rax)
addq $0x328, %rax            # imm = 0x328
decq %rdx
jne 0x21d729
movq 0x10(%rsp), %rdx
movq 0x230(%rdx), %rax
movq (%rax), %rcx
leaq 0x5c1(%rip), %rsi       # 0x21dd19
callq 0x108f2c
testq %r15, %r15
je 0x21d769
movq $0x0, (%r15)
testq %r14, %r14
je 0x21d775
movq $0x0, (%r14)
movq 0x10(%rsp), %rax
movq 0x1e0(%rax), %rcx
xorl %edi, %edi
movl $0x0, %eax
movq %rax, 0x20(%rsp)
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %eax
movq %rax, 0x38(%rsp)
movl $0x0, %eax
movq %rax, (%rsp)
movl $0x0, %eax
movq %rax, 0x18(%rsp)
testq %rcx, %rcx
movq %r15, 0x28(%rsp)
je 0x21d9f0
movq 0x10(%rsp), %rax
movq 0x1d8(%rax), %rax
xorl %edx, %edx
movq $0x0, 0x18(%rsp)
movq $0x0, (%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
xorl %edi, %edi
imulq $0x328, %rdx, %r12      # imm = 0x328
cmpq $0x0, 0x300(%rax,%r12)
je 0x21d9e4
movq %rdx, 0x58(%rsp)
xorl %ebx, %ebx
movq %r12, 0x48(%rsp)
movq 0x310(%rax,%r12), %rax
movq (%rax,%rbx,8), %r13
testq %r14, %r14
je 0x21d889
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x21d874
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rdi, %rbp
movq %rax, %rdi
callq 0x2370
movq %rbp, %rdi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r13, (%rax,%rcx,8)
testq %r13, %r13
jns 0x21d8e4
movq (%r14), %rbp
jmp 0x21d8ee
movq 0x30(%rsp), %r12
cmpq %r12, %rdi
jne 0x21d8cb
leaq (%r12,%r12), %rbp
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x2370
movq %rax, 0x20(%rsp)
movq %r13, (%rax,%r12,8)
movq %r12, %rdi
incq %rdi
movq %rbp, 0x30(%rsp)
jmp 0x21d8d7
movq 0x20(%rsp), %rax
movq %r13, (%rax,%rdi,8)
incq %rdi
movq %rdi, %rbp
testq %r13, %r13
movq 0x48(%rsp), %r12
js 0x21d8ee
incq 0x18(%rsp)
jmp 0x21d9b6
testq %r15, %r15
je 0x21d947
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x21d93e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %r14, %r12
movq %r15, %r14
movq %rdi, %r15
movq %rax, %rdi
callq 0x2370
movq %r15, %rdi
movq %r14, %r15
movq %r12, %r14
movq 0x48(%rsp), %r12
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
jmp 0x21d9a2
movq 0x38(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jne 0x21d98a
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq %rdi, 0x60(%rsp)
movq (%rsp), %rdi
callq 0x2370
movq 0x60(%rsp), %rdi
movq %r15, 0x38(%rsp)
movq 0x28(%rsp), %r15
jmp 0x21d98e
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rax, (%rsp)
movq %rdx, 0x8(%rsp)
movabsq $0x7fffffff00000000, %rdx # imm = 0x7FFFFFFF00000000
andq %rdx, %r13
orq %r13, %rbp
movq %rbp, (%rax,%rcx,8)
incq %rbx
movq 0x10(%rsp), %rax
movq 0x1d8(%rax), %rax
cmpq 0x300(%rax,%r12), %rbx
jb 0x21d829
movq 0x10(%rsp), %rcx
movq 0x1e0(%rcx), %rcx
movq 0x58(%rsp), %rdx
incq %rdx
cmpq %rcx, %rdx
jb 0x21d807
movq %rdi, %rbx
testq %r15, %r15
jne 0x21da2d
movq (%rsp), %rdi
movq 0x18(%rsp), %r13
movq %r15, %r12
movq 0x8(%rsp), %r15
leaq (%rdi,%r15,8), %rsi
callq 0x1ee586
addq %r15, %r13
movq %r12, %r15
movl $0x8, %eax
movq %r13, %rdx
cmpq %r13, 0x38(%rsp)
jb 0x21da55
movq (%rsp), %rax
jmp 0x21daa2
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
movq (%r15), %rcx
movq 0x18(%rsp), %rax
addq %rcx, %rax
cmpq %rax, 0x8(%r15)
jae 0x21daa9
movq %rax, %rdx
leaq 0x8(%r15), %rax
decq %rdx
movq %rdx, %rcx
shrq %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %rsi
shrq $0x10, %rsi
orq %rdx, %rsi
incq %rsi
testq %r15, %r15
jne 0x21daaf
shlq $0x3, %rsi
movq (%rsp), %rdi
callq 0x2370
movq %rax, (%rsp)
movq 0x8(%rsp), %rcx
jmp 0x21dac6
movq 0x10(%r15), %rax
jmp 0x21dac6
movq %rsi, (%rax)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
leaq 0x10(%r14), %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rbx, %rdx
movq 0x28(%rsp), %rbp
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
je 0x21daf8
movq (%rcx), %rax
cmpq %rax, %r15
jae 0x21db6c
movq 0x20(%rsp), %rax
testq %r14, %r14
je 0x21db0f
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rsp), %rcx
testq %rbp, %rbp
je 0x21db1c
movq 0x10(%rbp), %rcx
movq (%rcx,%r15,8), %rcx
leal -0x1(%rcx), %edx
movl (%rax,%rdx,8), %r12d
movq 0x18(%rsp), %rdx
leaq (%rdx,%r13,8), %rdi
movl %ecx, %ecx
leaq (%rax,%rcx,8), %rsi
leaq (,%r12,8), %rdx
callq 0x2160
movq (%rsp), %rax
testq %rbp, %rbp
je 0x21db50
movq 0x10(%rbp), %rax
movq %r13, %rcx
shlq $0x20, %rcx
orq %r12, %rcx
movq %rcx, (%rax,%r15,8)
addq %r12, %r13
incq %r15
movq %rbx, %rdx
jmp 0x21dae6
testq %r14, %r14
jne 0x21dba1
cmpq %rdx, %r13
movq 0x28(%rsp), %r15
ja 0x21dcfa
shlq $0x3, %r13
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x2160
movl $0x10, %eax
movq %rax, 0x30(%rsp)
jmp 0x21dbc8
cmpq (%r14), %r13
movq 0x28(%rsp), %r15
ja 0x21dcfa
movq 0x10(%r14), %rdi
leaq (,%r13,8), %rdx
movq 0x18(%rsp), %rsi
callq 0x2160
movq %r13, (%r14)
movq 0x77411(%rip), %rax     # 0x294fe0
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rax
movl (%rax), %ecx
testq %r15, %r15
jne 0x21dc15
leaq 0x2b5d7(%rip), %rsi     # 0x2491c3
leaq 0x2b5f3(%rip), %r13     # 0x2491e6
movq %r13, %rdx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x22b0
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x219965
movq (%rsp), %rdx
jmp 0x21dc4e
movl (%r15), %r8d
leaq 0x2b5a4(%rip), %rsi     # 0x2491c3
leaq 0x2b5c0(%rip), %r15     # 0x2491e6
movq %r15, %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movq 0x28(%rsp), %r15
callq 0x219965
movq (%r15), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%r15), %rdx
movq 0x20(%rsp), %r8
testq %r14, %r14
je 0x21dc60
movq 0x30(%rsp), %rax
movq (%rax), %r8
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq 0x8(%rsp), %rcx
movl 0x54(%rsp), %r9d
callq 0x21cccc
testq %rax, %rax
je 0x21dcc0
leaq 0x2b55e(%rip), %r15     # 0x2491e6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x219965
leaq 0x77abe(%rip), %rax     # 0x295758
movl 0x7c(%rax), %edi
movq 0x8(%rbx), %rcx
leaq -0xa4c5(%rip), %rsi     # 0x2137e3
movq %rbx, %rdx
callq 0x108f2c
movq %rbx, %rdi
movq %r15, %rsi
movq 0x28(%rsp), %r15
callq 0x219965
testq %r14, %r14
movq (%rsp), %rdi
jne 0x21dcd7
movq 0x20(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
addq $0x68, %rsp
testq %r15, %r15
je 0x21dceb
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x2b468(%rip), %rdi     # 0x249169
leaq 0x27901(%rip), %rsi     # 0x245609
leaq 0x2b465(%rip), %rcx     # 0x249174
movl $0x3574, %edx           # imm = 0x3574
callq 0x2110