// File: /workspace/github_cpp_makefile_star2_O2/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 0x1b742e
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 0x1b7488
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 0x1b74ee
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 0x1b7551
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)
andq $0x0, 0x98(%rbx)
andq $0x0, 0x80(%rbx)
andq $0x0, 0x68(%rbx)
andq $0x0, 0x50(%rbx)
xorl %ecx, %ecx
pushq $0x1
popq %rbp
pushq $0x2
popq %r14
xorl %r15d, %r15d
movl %r15d, %eax
cmpq %rax, (%rbx)
jbe 0x1b76c1
movl %ebp, %edx
movq 0x40(%rbx), %rsi
movq 0x60(%rbx), %rdi
movl (%rsi,%rdx,8), %edx
movl %edx, (%rdi,%rax,4)
testl %edx, %edx
jne 0x1b75d3
movq 0x78(%rbx), %rax
cmpq 0x70(%rbx), %rcx
jne 0x1b75c4
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 %r15d, (%rax,%rcx,4)
movq %rdx, %rcx
incl %r15d
addl $0x2, %ebp
jmp 0x1b7577
leaq -0x1(%rcx), %rax
movq %rax, 0x68(%rbx)
movq 0x78(%rbx), %rax
movq 0x80(%rbx), %rdx
movl -0x4(%rax,%rcx,4), %r15d
movq 0x90(%rbx), %rax
cmpq 0x88(%rbx), %rdx
jne 0x1b762f
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r14, %rsi
movq %rsi, 0x88(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x90(%rbx)
movq 0x80(%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x80(%rbx)
movl %r15d, (%rax,%rdx,4)
addl %r15d, %r15d
movq 0x28(%rbx), %rax
movq 0x40(%rbx), %rcx
movq (%rcx,%r15,8), %rcx
movl %ecx, %r15d
shlq $0x4, %r15
shrq $0x1c, %rcx
andq $-0x10, %rcx
leaq (%rax,%rcx), %r12
addq $0x8, %r12
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x1b76bd
movl (%r12,%r13), %ebp
shrl %ebp
movq 0x60(%rbx), %rax
decl (%rax,%rbp,4)
jne 0x1b76b7
movq 0x68(%rbx), %rcx
movq 0x78(%rbx), %rax
cmpq 0x70(%rbx), %rcx
jne 0x1b76ac
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
jmp 0x1b7667
movq 0x68(%rbx), %rcx
testq %rcx, %rcx
jne 0x1b75db
movq 0x80(%rbx), %rcx
xorl %eax, %eax
cmpq (%rbx), %rcx
jne 0x1b76fd
xorl %edx, %edx
pushq $0x1
popq %rax
movl %edx, %esi
cmpq %rsi, %rcx
jbe 0x1b76fd
movq 0x90(%rbx), %rdi
movq 0xa8(%rbx), %r8
movl (%rdi,%rsi,4), %esi
movl %edx, (%r8,%rsi,4)
incl %edx
jmp 0x1b76dd
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq