// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x331162
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x331164
jmp 0x331166
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x331245
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
jmp 0x331247
jmp 0x331249
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x331370
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x88(%rax), %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x88(%rax), %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x88(%rax), %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x88(%rax), %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x88(%rax), %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
jmp 0x331372
jmp 0x331374
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x33149b
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
jmp 0x33149d
movq -0x10(%rbp), %rax
movq $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x68(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x3315c7
movl -0x24(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x24(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x24(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x3315b7
jmp 0x33152b
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jne 0x331594
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x331559
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
shlq %rax
movq %rax, -0x40(%rbp)
jmp 0x331564
movl $0x2, %eax
movq %rax, -0x40(%rbp)
jmp 0x331564
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x68(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x68(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x3315b7
jmp 0x3315b9
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3314da
jmp 0x3315c9
movq -0x10(%rbp), %rax
cmpq $0x0, 0x68(%rax)
jbe 0x3317da
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x68(%rdx), %rcx
movq %rcx, %rsi
addq $-0x1, %rsi
movq %rsi, 0x68(%rdx)
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x88(%rcx), %rax
jne 0x33167b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x88(%rax)
je 0x331634
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
shlq %rax
movq %rax, -0x48(%rbp)
jmp 0x33163f
movl $0x2, %eax
movq %rax, -0x48(%rbp)
jmp 0x33163f
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x80(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x80(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x40(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x3317d5
movq -0x38(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x2c(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x3317c5
jmp 0x331739
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jne 0x3317a2
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x331767
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
shlq %rax
movq %rax, -0x50(%rbp)
jmp 0x331772
movl $0x2, %eax
movq %rax, -0x50(%rbp)
jmp 0x331772
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x68(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x68(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x3317c5
jmp 0x3317c7
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3316ec
jmp 0x3315c9
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0x3317f7
movl $0x0, -0x4(%rbp)
jmp 0x331873
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x33180d
jmp 0x33182c
leaq 0x6639b(%rip), %rdi     # 0x397baf
leaq 0x6499e(%rip), %rsi     # 0x3961b9
movl $0x1650, %edx           # imm = 0x1650
leaq 0x663a3(%rip), %rcx     # 0x397bca
callq 0x2130
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x33186c
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0x24(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x331833
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)