// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0x240c8
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jb 0x240d4
movl $0x0, -0x4(%rbp)
jmp 0x24362
cmpq $0x0, -0x30(%rbp)
je 0x240e5
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x10(%rbp)
je 0x240f7
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x74(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2410b
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x28(%rbp)
jne 0x2411a
movq $-0x1, -0x28(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x23e90
movq %rax, -0x60(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x23e90
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
shlq %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
shlq $0x20, %rsi
callq 0x23eb0
movq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x23eb0
movq -0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x24313
movq -0x60(%rbp), %rdi
callq 0x23f50
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x3c(%rbp)
movq -0x68(%rbp), %rdi
callq 0x23f50
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x241bb
jmp 0x2416b
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x3c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x2430e
movq -0x50(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x2430e
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x38(%rbp), %edx
imulq $0x30, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x2430c
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
cmpq %rcx, %rax
jne 0x2426c
movl $0x1, -0x74(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x24255
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x10(%rbp)
je 0x24267
movq -0x50(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x24341
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x242fc
movq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
shlq $0x20, %rsi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
orq %rax, %rsi
callq 0x23eb0
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
addq 0x18(%rax), %rsi
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
subq 0x18(%rax), %rsi
callq 0x23eb0
jmp 0x242fe
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x24218
jmp 0x2430e
jmp 0x2416b
movq -0x68(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x24320
jmp 0x2433f
leaq 0xa8a9(%rip), %rdi      # 0x2ebd0
leaq 0xa8b3(%rip), %rsi      # 0x2ebe1
movl $0x502, %edx            # imm = 0x502
leaq 0xa90c(%rip), %rcx      # 0x2ec46
callq 0x21e0
jmp 0x24341
movq -0x60(%rbp), %rdi
callq 0x24030
movq -0x68(%rbp), %rdi
callq 0x24030
movq -0x58(%rbp), %rdi
callq 0x2060
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop