// File: /workspace/github_cpp_makefile_star2_O3/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), %rcx
cmpq %rcx, 0x58(%rdi)
jae 0x203163
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %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, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%rbx)
movq 0x60(%rbx), %rdi
leaq 0x4(,%rax,4), %rsi
callq 0x2370
movq %rax, 0x60(%rbx)
movq (%rbx), %rcx
cmpq %rcx, 0x70(%rbx)
jae 0x2031bd
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %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, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x70(%rbx)
movq 0x78(%rbx), %rdi
leaq 0x4(,%rax,4), %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq (%rbx), %rcx
cmpq %rcx, 0x88(%rbx)
jae 0x203223
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %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, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x88(%rbx)
movq 0x90(%rbx), %rdi
leaq 0x4(,%rax,4), %rsi
callq 0x2370
movq %rax, 0x90(%rbx)
movq (%rbx), %rcx
cmpq %rcx, 0xa0(%rbx)
jae 0x203289
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %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, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xa0(%rbx)
movq 0xa8(%rbx), %rdi
leaq 0x4(,%rax,4), %rsi
callq 0x2370
movq %rax, 0xa8(%rbx)
movq (%rbx), %rcx
xorl %r14d, %r14d
movq %r14, 0x98(%rbx)
movq %r14, 0x80(%rbx)
movq %r14, 0x68(%rbx)
movq %r14, 0x50(%rbx)
testq %rcx, %rcx
je 0x203457
movl $0x1, %ebp
movl $0x2, %r15d
xorl %edx, %edx
movl %ebp, %eax
movq 0x40(%rbx), %rsi
movq 0x60(%rbx), %rdi
movl (%rsi,%rax,8), %eax
movl %eax, (%rdi,%r14,4)
testl %eax, %eax
jne 0x20330c
movq 0x78(%rbx), %rax
cmpq 0x70(%rbx), %rdx
jne 0x2032fd
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r15, %rsi
movq %rsi, 0x70(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x78(%rbx)
movq (%rbx), %rcx
movq 0x68(%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x68(%rbx)
movl %r14d, (%rax,%rdx,4)
movq %rsi, %rdx
incl %r14d
addl $0x2, %ebp
cmpq %r14, %rcx
ja 0x2032b8
testq %rdx, %rdx
je 0x20341d
movl $0x2, %r14d
leaq -0x1(%rdx), %rax
movq %rax, 0x68(%rbx)
movq 0x78(%rbx), %rax
movq 0x80(%rbx), %rcx
movl -0x4(%rax,%rdx,4), %r15d
movq 0x90(%rbx), %rax
cmpq 0x88(%rbx), %rcx
jne 0x20337a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r14, %rsi
movq %rsi, 0x88(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x90(%rbx)
movq 0x80(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x80(%rbx)
movl %r15d, (%rax,%rcx,4)
addl %r15d, %r15d
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rax
testl %eax, %eax
je 0x20340d
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 0x203404
movq 0x68(%rbx), %rcx
movq 0x78(%rbx), %rax
cmpq 0x70(%rbx), %rcx
jne 0x2033f9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r14, %rsi
movq %rsi, 0x70(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x78(%rbx)
movq 0x68(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x68(%rbx)
movl %ebp, (%rax,%rcx,4)
addq $0x10, %r13
cmpq %r13, %r15
jne 0x2033b9
movq 0x68(%rbx), %rdx
testq %rdx, %rdx
jne 0x203326
movq (%rbx), %rcx
xorl %eax, %eax
cmpq %rcx, 0x80(%rbx)
jne 0x203464
movl $0x1, %eax
testq %rcx, %rcx
je 0x203464
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 0x203442
jmp 0x203464
xorl %eax, %eax
cmpq $0x0, 0x80(%rbx)
sete %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq