// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: topo_srt_gen(poa_g_t*, unsigned int, ma_ug_t*)
uint32_t topo_srt_gen(poa_g_t *g, uint32_t debug_qid, ma_ug_t *ug)
{
    uint32_t k, v, w, a_n; poa_arc_t *a;
    kv_resize(uint32_t, g->srt_b.ind, g->seq.n);
    kv_resize(uint32_t, g->srt_b.stack, g->seq.n);
    kv_resize(uint32_t, g->srt_b.res, g->seq.n);
    kv_resize(uint32_t, g->srt_b.res2nid, g->seq.n);
    // kv_resize(uint64_t, g->srt_b.aln, g->seq.n); g->srt_b.aln.n = 0;
    g->srt_b.ind.n = g->srt_b.stack.n = g->srt_b.res.n = g->srt_b.res2nid.n = 0;

    for (k = 0; k < g->seq.n; k++) {
        v = (k<<1) + 1; 
        g->srt_b.ind.a[k] = poa_arc_n(g, v);
        if(g->srt_b.ind.a[k] == 0) kv_push(uint32_t, g->srt_b.stack, k);
    }
    
    while (g->srt_b.stack.n > 0) {
        v = g->srt_b.stack.a[--g->srt_b.stack.n]; kv_push(uint32_t, g->srt_b.res, v); v <<= 1;
        a_n = poa_arc_n(g, v); a = poa_arc_a(g, v);
        for (k = 0; k < a_n; k++) {
            w = a[k].v>>1;
            g->srt_b.ind.a[w]--;
            if(g->srt_b.ind.a[w] == 0) kv_push(uint32_t, g->srt_b.stack, w);
        }
    }
    // if(!(g->srt_b.res.n == g->seq.n)) {
    //     fprintf(stderr, "[M::%s::] debug_qid::%u, g->seq.n::%u, g->srt_b.res.n::%u\n", 
    //     __func__, debug_qid, (uint32_t)g->seq.n, (uint32_t)g->srt_b.res.n);
    //     print_integer_g(g, ug, 1);
    //     for (k = 0; k < g->seq.n; k++) {
    //         if(g->srt_b.ind.a[k] > 0) fprintf(stderr, "circle->nid::%u\n", k);
    //     }
    // }
    if(g->srt_b.res.n != g->seq.n) return 0;///there is a circle
    assert(g->srt_b.res.n == g->seq.n);
    for (k = 0; k < g->seq.n; k++) {
        g->srt_b.res2nid.a[g->srt_b.res.a[k]] = k;
        // g->srt_b.aln.a[k] = g->seq.a[g->srt_b.res.a[k]].nid;
        // g->srt_b.aln.a[k] <<= 32; g->srt_b.aln.a[k] += k;
    }
    // radix_sort_srt64(g->srt_b.aln.a, g->srt_b.aln.a + g->srt_b.aln.n);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq (%rdi), %rax
cmpq %rax, 0x58(%rdi)
jae 0x1f7b40
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x58(%rbx)
movq 0x60(%rbx), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x60(%rbx)
movq (%rbx), %rax
cmpq %rax, 0x70(%rbx)
jae 0x1f7b9a
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x70(%rbx)
movq 0x78(%rbx), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq (%rbx), %rax
cmpq %rax, 0x88(%rbx)
jae 0x1f7c00
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x88(%rbx)
movq 0x90(%rbx), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x90(%rbx)
movq (%rbx), %rax
cmpq %rax, 0xa0(%rbx)
jae 0x1f7c66
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0xa0(%rbx)
movq 0xa8(%rbx), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0xa8(%rbx)
xorl %r14d, %r14d
movq %r14, 0x98(%rbx)
movq %r14, 0x80(%rbx)
movq %r14, 0x68(%rbx)
movq %r14, 0x50(%rbx)
cmpq %r14, (%rbx)
je 0x1f7ced
movl $0x1, %ebp
movl $0x2, %r15d
movl %ebp, %eax
movq 0x40(%rbx), %rcx
movq 0x60(%rbx), %rdx
movl (%rcx,%rax,8), %eax
movl %eax, (%rdx,%r14,4)
testl %eax, %eax
jne 0x1f7ce2
movq 0x70(%rbx), %rax
cmpq %rax, 0x68(%rbx)
jne 0x1f7cce
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x70(%rbx)
movq 0x78(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq 0x68(%rbx), %rax
movq 0x78(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x68(%rbx)
movl %r14d, (%rcx,%rax,4)
incl %r14d
addl $0x2, %ebp
cmpq %r14, (%rbx)
ja 0x1f7c8f
movq 0x68(%rbx), %rax
testq %rax, %rax
je 0x1f7df9
movl $0x2, %r14d
leaq -0x1(%rax), %rcx
movq %rcx, 0x68(%rbx)
movq 0x78(%rbx), %rcx
movl -0x4(%rcx,%rax,4), %r15d
movq 0x88(%rbx), %rax
cmpq %rax, 0x80(%rbx)
jne 0x1f7d4a
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r14, %rsi
movq %rsi, 0x88(%rbx)
movq 0x90(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x90(%rbx)
movq 0x80(%rbx), %rax
movq 0x90(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x80(%rbx)
movl %r15d, (%rcx,%rax,4)
addl %r15d, %r15d
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rax
testl %eax, %eax
je 0x1f7dec
movq 0x28(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %r15d
shlq $0x4, %rdx
leaq (%rcx,%rdx), %r12
addq $0x8, %r12
shlq $0x4, %r15
xorl %r13d, %r13d
movl (%r12,%r13), %ebp
shrl %ebp
movq 0x60(%rbx), %rax
decl (%rax,%rbp,4)
jne 0x1f7de3
movq 0x70(%rbx), %rax
cmpq %rax, 0x68(%rbx)
jne 0x1f7dd0
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r14, %rsi
movq %rsi, 0x70(%rbx)
movq 0x78(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq 0x68(%rbx), %rax
movq 0x78(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x68(%rbx)
movl %ebp, (%rcx,%rax,4)
addq $0x10, %r13
cmpq %r13, %r15
jne 0x1f7d97
movq 0x68(%rbx), %rax
testq %rax, %rax
jne 0x1f7d00
movq (%rbx), %rcx
xorl %eax, %eax
cmpq %rcx, 0x80(%rbx)
jne 0x1f7e34
movl $0x1, %eax
testq %rcx, %rcx
je 0x1f7e34
movq 0x90(%rbx), %rdx
movq 0xa8(%rbx), %rsi
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
movl %edi, (%rsi,%r8,4)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rcx
ja 0x1f7e21
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq