// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl %edx, 0x54(%rsp)
movq %rdi, %rbp
testq %r8, %r8
je 0x1ce996
andq $0x0, (%r8)
testq %rcx, %rcx
je 0x1ce99f
andq $0x0, (%rcx)
movq 0x1e0(%rbp), %rdi
pushq $0x78
popq %rax
movq %rdi, %r9
subq $0x1, %r9
jb 0x1ce9d5
movq 0x1d8(%rbp), %rdx
andq $0x0, (%rdx,%rax)
andq $0x0, 0x18(%rdx,%rax)
andq $0x0, 0x288(%rdx,%rax)
addq $0x328, %rax            # imm = 0x328
jmp 0x1ce9ac
movq %rcx, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x230(%rbp), %rax
movq (%rax), %rcx
leaq 0x5a3(%rip), %rsi       # 0x1cef93
movq %rbp, %rdx
movq %r8, 0x28(%rsp)
callq 0xc67c0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1cea0b
andq $0x0, (%rdi)
movq 0x18(%rsp), %r10
testq %r10, %r10
je 0x1cea19
andq $0x0, (%r10)
leaq 0x10(%r10), %rax
movq %rax, 0x38(%rsp)
xorl %r9d, %r9d
movq $0x0, 0x68(%rsp)
xorl %r8d, %r8d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
movq %rbp, 0x58(%rsp)
cmpq 0x1e0(%rbp), %rax
jae 0x1cec7a
movq %rax, 0x70(%rsp)
imulq $0x328, %rax, %r14      # imm = 0x328
xorl %r15d, %r15d
movq %r14, 0x60(%rsp)
movq 0x1d8(%rbp), %rax
cmpq 0x300(%rax,%r14), %r15
jae 0x1cec6d
addq %r14, %rax
addq $0x300, %rax            # imm = 0x300
movq 0x10(%rax), %rax
movq (%rax,%r15,8), %rbx
testq %r10, %r10
je 0x1cead8
movq (%r10), %rcx
movq 0x10(%r10), %rax
cmpq 0x8(%r10), %rcx
jne 0x1ceb43
movq %r15, %r13
movq %r12, %r14
movq %rbp, %r12
movq %r9, %rbp
movq %r8, 0x20(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r10)
jmp 0x1ceb0a
cmpq 0x68(%rsp), %r9
jne 0x1ceb71
movq %r15, %r13
movq %r12, %r14
movq %rbp, %r12
leaq (%r9,%r9), %rsi
movq %r9, %rbp
testq %r9, %r9
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x68(%rsp)
movq %r8, 0x20(%rsp)
movq %r8, %rax
movq %r10, %r15
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
testq %r15, %r15
movq %r15, %r10
je 0x1ceb58
movq %rax, 0x10(%r10)
movq (%r10), %rcx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %r8
movq %rbp, %r9
movq %r12, %rbp
movq %r14, %r12
movq 0x60(%rsp), %r14
movq %r13, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r10)
movq %rbx, (%rax,%rcx,8)
testq %rbx, %rbx
jns 0x1ceb80
movq (%r10), %r13
jmp 0x1ceb88
movq %rax, %r8
movq 0x28(%rsp), %rdi
movq %rbp, %r9
movq %r12, %rbp
movq %r14, %r12
movq 0x60(%rsp), %r14
movq %r13, %r15
movq %rbx, (%r8,%r9,8)
incq %r9
movq %r9, %r13
testq %rbx, %rbx
js 0x1ceb88
incq %r12
jmp 0x1cec65
testq %rdi, %rdi
je 0x1cebb8
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x1cec18
movq %r9, 0x48(%rsp)
movq %r8, 0x20(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1cebe6
movq 0x10(%rsp), %rax
cmpq 0x40(%rsp), %rax
jne 0x1cec21
movq %r9, 0x48(%rsp)
movq %r8, 0x20(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq %rdi, %rbp
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
testq %rbp, %rbp
movq %rbp, %rdi
je 0x1cec28
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x58(%rsp), %rbp
movq 0x20(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x18(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
jmp 0x1cec51
movq 0x8(%rsp), %rax
jmp 0x1cec41
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rbp
movq 0x20(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rdx, 0x10(%rsp)
movabsq $0x7fffffff00000000, %rdx # imm = 0x7FFFFFFF00000000
andq %rdx, %rbx
orq %rbx, %r13
movq %r13, (%rax,%rcx,8)
incq %r15
jmp 0x1cea77
movq 0x70(%rsp), %rax
incq %rax
jmp 0x1cea56
movq %r12, %r14
movq %r9, %r13
movq %r8, %r12
testq %rdi, %rdi
jne 0x1cecb7
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rbx
leaq (%rdi,%rbx,8), %rsi
callq 0x1a3def
movq 0x28(%rsp), %rdx
addq %rbx, %r14
cmpq %r14, 0x40(%rsp)
jb 0x1cecdd
movq %rbx, %rsi
movq 0x8(%rsp), %rax
jmp 0x1ced43
movq (%rdi), %rax
movq 0x10(%rdi), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
addq %rcx, %r14
cmpq %r14, 0x8(%rdx)
movq 0x10(%rsp), %rsi
jae 0x1ced48
decq %r14
movq %r14, %rax
shrq %rax
orq %r14, %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, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq 0x8(%rsp), %rdi
testq %rdx, %rdx
je 0x1ced26
movq %rsi, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
jne 0x1ced51
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rsi
movq %rsi, %rcx
jmp 0x1ced4c
movq 0x10(%rdx), %rax
movq %r12, %rdi
jmp 0x1ced60
movq %rax, 0x10(%rdx)
movq (%rdx), %rcx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %r8
movq 0x18(%rsp), %r9
leaq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rsi, %rax
testq %rdx, %rdx
je 0x1ced81
movq (%rdx), %rax
cmpq %rax, %rbx
jae 0x1cee0b
movq %rdi, %rax
testq %r9, %r9
je 0x1ced9a
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
testq %rdx, %rdx
je 0x1ceda8
movq 0x10(%rdx), %rcx
movq (%rcx,%rbx,8), %rcx
leal -0x1(%rcx), %edx
movl (%rax,%rdx,8), %r15d
movq 0x20(%rsp), %rdx
leaq (%rdx,%r14,8), %rdi
movl %ecx, %ecx
leaq (%rax,%rcx,8), %rsi
leaq (,%r15,8), %rdx
callq 0x2160
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %rax
testq %rdx, %rdx
je 0x1cede2
movq 0x10(%rdx), %rax
movq %r14, %rcx
shlq $0x20, %rcx
orq %r15, %rcx
movq %rcx, (%rax,%rbx,8)
addq %r15, %r14
incq %rbx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %r8
movq 0x18(%rsp), %r9
jmp 0x1ced76
testq %r9, %r9
jne 0x1cee34
cmpq %r8, %r14
ja 0x1cef74
shlq $0x3, %r14
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x2160
pushq $0x10
popq %rax
movq %rax, 0x38(%rsp)
jmp 0x1cee59
cmpq (%r9), %r14
ja 0x1cef74
movq 0x10(%r9), %rdi
leaq (,%r14,8), %rdx
movq 0x20(%rsp), %rsi
movq %r9, %rbx
callq 0x2160
movq %r14, (%rbx)
movq 0x84180(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
movq 0x230(%rbp), %rax
movl (%rax), %ecx
movq 0x28(%rsp), %rax
testq %rax, %rax
movq 0x10(%rsp), %r8
jne 0x1ceea7
leaq 0x2c300(%rip), %rsi     # 0x1fb182
leaq 0x2c31c(%rip), %r13     # 0x1fb1a5
movq %r13, %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x1cb1f3
movq 0x8(%rsp), %rdx
jmp 0x1ceede
movq %rax, %rbx
movl (%rax), %r8d
leaq 0x2c2ce(%rip), %rsi     # 0x1fb182
leaq 0x2c2ea(%rip), %r15     # 0x1fb1a5
movq %r15, %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x1cb1f3
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rbx), %rdx
movq %r12, %r8
cmpq $0x0, 0x18(%rsp)
je 0x1ceef1
movq 0x38(%rsp), %rax
movq (%rax), %r8
movq %rbp, %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq 0x10(%rsp), %rcx
movl 0x54(%rsp), %r9d
callq 0x1cdfa9
testq %rax, %rax
je 0x1cef35
leaq 0x2c28e(%rip), %r15     # 0x1fb1a5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1cb1f3
movq %rbx, %rdi
callq 0x1c5ce9
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1cb1f3
cmpq $0x0, 0x18(%rsp)
jne 0x1cef45
movq %r12, %rdi
callq 0x2220
cmpq $0x0, 0x28(%rsp)
je 0x1cef5c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x2c1ad(%rip), %rdi     # 0x1fb128
leaq 0x28557(%rip), %rsi     # 0x1f74d9
leaq 0x2c1aa(%rip), %rcx     # 0x1fb133
movl $0x3574, %edx           # imm = 0x3574
callq 0x2110