// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: topologicalSortUtil(asg_t*, hap_cov_t*, unsigned int, unsigned int)
void topologicalSortUtil(asg_t *g, hap_cov_t *cov, uint32_t beg, uint32_t sink)
{
    buf_t *b = &(cov->t_ch->b_buf_0);
    uint64_t *visited = cov->pos_idx;
    uint32_t v = beg, nv, kv, i;
    asg_arc_t *av = NULL;

    b->b.n = 0; cov->t_ch->topo_res.n = 0;
    kv_push(uint32_t, b->b, v);
    while (b->b.n > 0)
    {
        ///b->b.n--;
        v = b->b.a[b->b.n-1];
        if(visited[v>>1] == (uint64_t)-1)
        {
            visited[v>>1] = 0;
        } 
        
        nv = asg_arc_n(g, v);
        av = asg_arc_a(g, v);
        for (i = kv = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            if((av[i].v>>1) == (beg>>1) || (av[i].v>>1) == (sink>>1)) continue;
            if(visited[av[i].v>>1] != (uint64_t)-1) continue;
            kv_push(uint32_t, b->b, av[i].v);
            kv++;
        }

        if(kv != 0) continue;
        b->b.n--;
        if(visited[v>>1] != 1)
        {
            kv_push(uint32_t, cov->t_ch->topo_res, v);
            visited[v>>1] = 1;
        }
    }
    for (i = 0; i < cov->t_ch->topo_res.n; ++i) 
    {
        visited[cov->t_ch->topo_res.a[i]>>1] = (uint64_t)-1;
    }

    cov->t_ch->topo_res.n--;//remove beg
    for (i = 0; i < (cov->t_ch->topo_res.n>>1); ++i) 
    {
        v = cov->t_ch->topo_res.a[i];
        cov->t_ch->topo_res.a[i] = cov->t_ch->topo_res.a[cov->t_ch->topo_res.n - i - 1];
        cov->t_ch->topo_res.a[cov->t_ch->topo_res.n - i - 1] = v;
    }

    /*******************************for debug************************************/
    // for (i = 0; i < cov->n; ++i) 
    // {
    //     if(visited[i] != (uint64_t)-1) fprintf(stderr, "ERROR-2\n");
    // }
    // debug_topo_sorting(g, cov, beg, sink);
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %ebx
movq %rsi, %r14
movq 0x10(%rsi), %r8
movq 0xa8(%rsi), %r9
andq $0x0, 0xa0(%r9)
movl %edx, %ebp
andq $0x0, 0x50(%r9)
movq %rdi, 0x20(%rsp)
xorl %eax, %eax
cmpq %rax, 0xa8(%r9)
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
je 0xa4f94
movq 0xb0(%r9), %rax
xorl %edx, %edx
jmp 0xa4fc6
movq $0x2, 0xa8(%r9)
movq 0xb0(%r9), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq %rax, 0xb0(%r9)
movq 0xa0(%r9), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0xa0(%r9)
movl %ebp, (%rax,%rdx,4)
shrl %ebp
shrl %ebx
movq %r14, 0x28(%rsp)
testq %rcx, %rcx
je 0xa517c
movq 0xb0(%r9), %rax
movl -0x4(%rax,%rcx,4), %esi
movl %esi, %ecx
shrl %ecx
movq %rcx, 0x8(%rsp)
cmpq $-0x1, (%r8,%rcx,8)
jne 0xa500b
movq 0x8(%rsp), %rcx
andq $0x0, (%r8,%rcx,8)
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq %rsi, 0x30(%rsp)
movq (%rdx,%rsi,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x18, %rsi, %r15
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r14
addq $0x8, %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq %r12, %r15
je 0xa50d5
cmpl $0x0, 0x4(%r14,%r12)
js 0xa50cc
movl (%r14,%r12), %ecx
movl %ecx, %edx
shrl %edx
cmpl %ebp, %edx
sete %sil
cmpl %ebx, %edx
sete %dil
orb %sil, %dil
jne 0xa50cc
cmpq $-0x1, (%r8,%rdx,8)
jne 0xa50cc
movq 0xa0(%r9), %rdx
cmpq 0xa8(%r9), %rdx
jne 0xa50bb
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0xa8(%r9)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq %rax, 0xb0(%r9)
movl (%r14,%r12), %ecx
movq 0xa0(%r9), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0xa0(%r9)
movl %ecx, (%rax,%rdx,4)
incl %r13d
addq $0x18, %r12
jmp 0xa503d
movq 0xa0(%r9), %rcx
testl %r13d, %r13d
movq 0x28(%rsp), %r14
jne 0xa4fdd
decq %rcx
movq %rcx, 0xa0(%r9)
movq 0x8(%rsp), %rax
cmpq $0x1, (%r8,%rax,8)
je 0xa4fdd
movq 0xa8(%r14), %rcx
movq 0x50(%rcx), %rdx
cmpq 0x58(%rcx), %rdx
jne 0xa514f
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x58(%rcx)
movq 0x60(%rcx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq 0xa8(%r14), %rcx
movq %rax, 0x60(%rcx)
movq 0x50(%rcx), %rdx
jmp 0xa5153
movq 0x60(%rcx), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, 0x50(%rcx)
movq 0x30(%rsp), %rcx
movl %ecx, (%rax,%rdx,4)
movq 0x8(%rsp), %rax
movq $0x1, (%r8,%rax,8)
movq 0xa0(%r9), %rcx
jmp 0xa4fdd
movq 0xa8(%r14), %rax
xorl %ecx, %ecx
movl %ecx, %edx
movq 0x50(%rax), %rcx
cmpq %rdx, %rcx
jbe 0xa51a3
movq 0x60(%rax), %rcx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
orq $-0x1, (%r8,%rcx,8)
leal 0x1(%rdx), %ecx
jmp 0xa5185
decq %rcx
movq %rcx, 0x50(%rax)
movq %rcx, %rdx
shrq %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rdx
jbe 0xa51db
movq 0x60(%rax), %rdi
movl (%rdi,%rsi,4), %r8d
movq %rsi, %r9
notq %r9
leaq (%rdi,%rcx,4), %r10
movl (%r10,%r9,4), %r11d
movl %r11d, (%rdi,%rsi,4)
movl %r8d, (%r10,%r9,4)
incl %esi
jmp 0xa51b2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq