// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/graph.c
// Function: asmg_path_exists
int asmg_path_exists(asmg_t *g, uint32_t source, uint32_t sink, uint32_t step, uint64_t dist, uint32_t *_step, uint64_t *_dist)
{
    // TODO dealing with graphs with deleted vertices and arcs
    // detect path between two vertices
    if (source >= asmg_vtx_n(g) || sink >= asmg_vtx_n(g))
        return 0;
    
    if (_step) *_step = 0;
    if (_dist) *_dist = 0;

    uint32_t i, v, r, nv;
    uint64_t x, rd;
    int8_t *flag;
    kdq_u64_t *q;
    kdq_u64_t *d;
    asmg_arc_t *av;

    int exists = 0;

    if (step == 0)
        step = UINT32_MAX;
    if (dist == 0)
        dist = UINT64_MAX;
    q = kdq_init(uint64_t, 0);
    d = kdq_init(uint64_t, 0);
    MYCALLOC(flag, asmg_vtx_n(g));
    kdq_push(uint64_t, q, (uint64_t)source<<32);
    kdq_push(uint64_t, d, 0);
    while (kdq_size(q) > 0) {
        x = *kdq_shift(uint64_t, q);
        v = x>>32;
        r = (uint32_t) x;
        rd = *kdq_shift(uint64_t, d);
        if (flag[v]) continue; // already visited
        flag[v] = 1;
        if (r < step && rd < dist) {
            nv = asmg_arc_n(g, v);
            av = asmg_arc_a(g, v);
            for (i = 0; i < nv; ++i) {
                if (av[i].w == sink) {
                    exists = 1;
                    if (_step) *_step = r;
                    if (_dist) *_dist = rd;
                    goto do_clean;
                }
                if (flag[av[i].w] == 0) {
                    kdq_push(uint64_t, q, (uint64_t)av[i].w<<32 | (r + 1));
                    kdq_push(uint64_t, d, rd + g->vtx[av[i].w>>1].len - av[i].ls);
                }
            }
        }
    }
    assert(kdq_size(d) == 0);

do_clean:
    kdq_destroy(uint64_t, q);
    kdq_destroy(uint64_t, d);
    free(flag);

    return exists;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
movq %rdi, 0x18(%rsp)
movq (%rdi), %rax
addq %rax, %rax
xorl %ebx, %ebx
cmpq %rbp, %rax
jbe 0x14ea2
movl %edx, %edx
movq %rdx, 0x40(%rsp)
cmpq %rdx, %rax
jbe 0x14ea2
movq 0x80(%rsp), %rax
testq %r9, %r9
je 0x14cfc
andl $0x0, (%r9)
movq %r9, 0x30(%rsp)
testq %rax, %rax
je 0x14d0a
andq $0x0, (%rax)
xorl %eax, %eax
cmpl $0x1, %ecx
movl $0x0, %edx
sbbl %edx, %edx
orl %ecx, %edx
movl %edx, 0x2c(%rsp)
cmpq $0x1, %r8
sbbq %rax, %rax
orq %r8, %rax
movq %rax, 0x38(%rsp)
callq 0x14ab0
movq %rax, %rbx
callq 0x14ab0
movq %rax, %r14
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %rdi, %rdi
pushq $0x1
popq %rsi
callq 0x2250
movq %rax, %r13
shlq $0x20, %rbp
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x14af6
movq %r14, 0x8(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x14af6
movq 0x20(%rsp), %rdi
cmpq $0x0, 0x8(%rdi)
je 0x14e7d
callq 0x14c47
movq (%rax), %rbp
movq %rbp, %r14
shrq $0x20, %r14
movq 0x8(%rsp), %rdi
callq 0x14c47
cmpb $0x0, (%r13,%r14)
jne 0x14d74
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movb $0x1, (%r13,%r14)
cmpl %ebp, 0x2c(%rsp)
jbe 0x14d74
movq 0x10(%rsp), %rax
cmpq 0x38(%rsp), %rax
jae 0x14d74
movq 0x18(%rsp), %rdx
movq 0x38(%rdx), %rax
movl (%rax,%r14,8), %eax
movq 0x28(%rdx), %rcx
movq 0x30(%rdx), %rdx
movl %ebp, %ebx
incl %ebx
imulq $0x30, %rax, %r12
imulq $0x30, (%rdx,%r14,8), %rax
leaq (%rcx,%rax), %r14
addq $0x8, %r14
xorl %r15d, %r15d
cmpq %r15, %r12
je 0x14d74
movq (%r14,%r15), %rsi
cmpq 0x40(%rsp), %rsi
je 0x14e52
cmpb $0x0, (%r13,%rsi)
jne 0x14e4c
shlq $0x20, %rsi
orq %rbx, %rsi
movq 0x20(%rsp), %rdi
callq 0x14af6
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq (%r14,%r15), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
movq 0x18(%rax,%rcx), %rsi
addq 0x10(%rsp), %rsi
subq 0x10(%r14,%r15), %rsi
movq 0x8(%rsp), %rdi
callq 0x14af6
addq $0x30, %r15
jmp 0x14df2
movq 0x30(%rsp), %rax
testq %rax, %rax
movq 0x20(%rsp), %rdi
je 0x14e63
movl %ebp, (%rax)
pushq $0x1
popq %rbx
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x14e8b
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x14e8b
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x14eb3
callq 0x14c91
movq 0x8(%rsp), %rdi
callq 0x14c91
movq %r13, %rdi
callq 0x2060
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5df6(%rip), %rdi      # 0x1acb0
leaq 0x5e00(%rip), %rsi      # 0x1acc1
leaq 0x5e5e(%rip), %rcx      # 0x1ad26
movl $0x502, %edx            # imm = 0x502
callq 0x21d0