// File: /workspace/github_cpp_makefile_star2_O1/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 %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rsi), %r8
movq 0xa8(%rsi), %r13
xorl %eax, %eax
movq %rax, 0xa0(%r13)
movq %rax, 0x50(%r13)
cmpq %rax, 0xa8(%r13)
movq %r8, 0x10(%rsp)
jne 0xe0df2
movq $0x2, 0xa8(%r13)
movq 0xb0(%r13), %rdi
movl $0x8, %esi
movl %ecx, %ebx
callq 0x2370
movl %ebx, %ecx
movq 0x10(%rsp), %r8
movq %rax, 0xb0(%r13)
movq 0xa0(%r13), %rsi
movq 0xb0(%r13), %rdx
movq %rsi, %rax
incq %rax
movq %rax, 0xa0(%r13)
movl %ebp, (%rdx,%rsi,4)
je 0xe0fc5
shrl %ebp
shrl %ecx
movl %ecx, 0xc(%rsp)
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq 0xb0(%r13), %rdx
movl -0x4(%rdx,%rax,4), %ebx
movl %ebx, %r12d
shrl %r12d
cmpq $-0x1, (%r8,%r12,8)
jne 0xe0e48
movq $0x0, (%r8,%r12,8)
movq 0x28(%r15), %rax
movq (%rax,%rbx,8), %rax
testl %eax, %eax
je 0xe0f3f
movq %r12, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq 0x8(%r15), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %r15
addq $0x8, %r15
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpl $0x0, 0x4(%r15,%r12)
js 0xe0f1a
movl (%r15,%r12), %eax
shrl %eax
cmpl %ebp, %eax
sete %sil
cmpl %ecx, %eax
sete %dl
orb %sil, %dl
jne 0xe0f1a
cmpq $-0x1, (%r8,%rax,8)
jne 0xe0f1a
movq 0xa8(%r13), %rax
cmpq %rax, 0xa0(%r13)
jne 0xe0ef8
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xa8(%r13)
movq 0xb0(%r13), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %r8
movq %rax, 0xb0(%r13)
movl (%r15,%r12), %eax
movq 0xa0(%r13), %rdi
movq 0xb0(%r13), %rdx
leaq 0x1(%rdi), %rsi
movq %rsi, 0xa0(%r13)
movl %eax, (%rdx,%rdi,4)
incl %ebx
addq $0x18, %r12
cmpq %r12, %r14
jne 0xe0e88
testl %ebx, %ebx
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r15
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r12
jne 0xe0fb5
decq 0xa0(%r13)
cmpq $0x1, (%r8,%r12,8)
je 0xe0fb5
movq 0xa8(%r14), %rax
movq 0x58(%rax), %rdx
cmpq %rdx, 0x50(%rax)
jne 0xe0f93
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rax)
movq 0x60(%rax), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %r8
movq 0xa8(%r14), %rdx
movq %rax, 0x60(%rdx)
movq 0xa8(%r14), %rax
movq 0x50(%rax), %rdi
movq 0x60(%rax), %rdx
leaq 0x1(%rdi), %rsi
movq %rsi, 0x50(%rax)
movl %ebx, (%rdx,%rdi,4)
movq $0x1, (%r8,%r12,8)
movq 0xa0(%r13), %rax
testq %rax, %rax
jne 0xe0e28
movq 0xa8(%r14), %rcx
movq 0x50(%rcx), %rax
testq %rax, %rax
je 0xe0ffa
movq 0x60(%rcx), %rdx
movl $0x1, %esi
xorl %edi, %edi
movl (%rdx,%rdi,4), %eax
shrl %eax
movq $-0x1, (%r8,%rax,8)
movl %esi, %edi
movq 0x50(%rcx), %rax
incl %esi
cmpq %rdi, %rax
ja 0xe0fe0
leaq -0x1(%rax), %rdx
movq %rdx, 0x50(%rcx)
cmpq $0x2, %rdx
jb 0xe1030
shrq %rdx
movq 0x60(%rcx), %rcx
addq $-0x2, %rax
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movl (%rcx,%rax,4), %r8d
movl %r8d, (%rcx,%rsi,4)
movl %edi, (%rcx,%rax,4)
incq %rsi
movl %esi, %edi
decq %rax
cmpq %rdi, %rdx
ja 0xe1015
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq