// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, %r14
movq 0x10(%rsi), %r8
movq %rsi, 0x18(%rsp)
movq 0xa8(%rsi), %r13
xorl %eax, %eax
movq %rax, 0xa0(%r13)
movq %rax, 0x50(%r13)
cmpq %rax, 0xa8(%r13)
movq %r8, 0x10(%rsp)
je 0xe2115
movq 0xb0(%r13), %rax
movq $0x1, 0xa0(%r13)
movl %ebp, (%rax)
movl $0x1, %esi
jmp 0xe215e
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), %rdx
movq %rdx, %rsi
incq %rsi
movq %rsi, 0xa0(%r13)
movl %ebp, (%rax,%rdx,4)
je 0xe2319
shrl %ebp
shrl %ecx
movl %ecx, 0xc(%rsp)
movq %r14, 0x20(%rsp)
movq 0xb0(%r13), %rax
movl -0x4(%rax,%rsi,4), %r15d
movl %r15d, %r12d
shrl %r12d
cmpq $-0x1, (%r8,%r12,8)
jne 0xe218c
movq $0x0, (%r8,%r12,8)
movq 0x28(%r14), %rdx
movq (%rdx,%r15,8), %rdi
testl %edi, %edi
je 0xe2285
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq 0x8(%r14), %rdx
movq %rdi, %rsi
shrq $0x20, %rsi
movl %edi, %edi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r15
addq $0x8, %r15
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpl $0x0, 0x4(%r15,%r12)
js 0xe2258
movl (%r15,%r12), %r9d
movl %r9d, %edx
shrl %edx
cmpl %ebp, %edx
sete %sil
cmpl %ecx, %edx
sete %dil
orb %sil, %dil
jne 0xe2258
cmpq $-0x1, (%r8,%rdx,8)
jne 0xe2258
movq 0xa0(%r13), %rdx
cmpq 0xa8(%r13), %rdx
jne 0xe2247
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0xa8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %r8
movq %rax, 0xb0(%r13)
movl (%r15,%r12), %r9d
movq 0xa0(%r13), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0xa0(%r13)
movl %r9d, (%rax,%rdx,4)
incl %ebx
addq $0x18, %r12
cmpq %r12, %r14
jne 0xe21cc
movq 0xa0(%r13), %rsi
testl %ebx, %ebx
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r12
jne 0xe2310
jmp 0xe228c
movq 0xa0(%r13), %rsi
decq %rsi
movq %rsi, 0xa0(%r13)
cmpq $0x1, (%r8,%r12,8)
je 0xe2310
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rdi
movq 0x50(%rdi), %rdx
cmpq 0x58(%rdi), %rdx
jne 0xe22f1
movq %rax, %rbx
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x58(%rdi)
movq 0x60(%rdi), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %r8
movq 0xa8(%rbx), %rdi
movq %rax, 0x60(%rdi)
movq 0x50(%rdi), %rdx
jmp 0xe22f5
movq 0x60(%rdi), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, 0x50(%rdi)
movl %r15d, (%rax,%rdx,4)
movq $0x1, (%r8,%r12,8)
movq 0xa0(%r13), %rsi
testq %rsi, %rsi
jne 0xe216b
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rcx
cmpq $0x0, 0x50(%rcx)
je 0xe2366
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 0xe2337
decq %rax
movq %rax, 0x50(%rcx)
cmpq $0x2, %rax
jb 0xe23a3
movq %rax, %rdx
shrq %rdx
jmp 0xe237f
movq $-0x1, 0x50(%rcx)
movq $-0x1, %rax
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
movq 0x60(%rcx), %rcx
decq %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 0xe2388
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq