// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movl %esi, %ebx
movq %rdi, 0x10(%rsp)
movq (%rdi), %rax
addq %rax, %rax
xorl %r14d, %r14d
cmpq %rbx, %rax
jbe 0x189a1
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x189a1
movq 0x70(%rsp), %rax
testq %r9, %r9
je 0x186ab
movl $0x0, (%r9)
movq %rdx, 0x28(%rsp)
movq %r9, 0x30(%rsp)
testq %rax, %rax
je 0x186c1
movq $0x0, (%rax)
movabsq $0x3ffffffffffffff, %rbp # imm = 0x3FFFFFFFFFFFFFF
xorl %r15d, %r15d
cmpl $0x1, %ecx
movl $0x0, %eax
sbbl %eax, %eax
movl %eax, 0xc(%rsp)
cmpq $0x1, %r8
movl $0x0, %eax
sbbq %rax, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
movq %r8, (%rsp)
movl %ecx, 0x18(%rsp)
callq 0x1b528
movq %rax, %r12
movq (%rax), %rax
andq %rbp, %rax
btsq $0x3b, %rax
movq %rax, (%r12)
movl $0x3, %r14d
movq %r14, 0x10(%r12)
movl $0x20, %esi
xorl %edi, %edi
callq 0x1b695
movq %rax, 0x18(%r12)
movq %r15, 0x20(%r12)
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %r13
movq (%rax), %rax
andq %rbp, %rax
btsq $0x3b, %rax
movq %rax, (%r13)
movq %r14, 0x10(%r13)
movl $0x20, %esi
xorl %edi, %edi
callq 0x1b695
movq %rax, 0x18(%r13)
movq %r15, 0x20(%r13)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
addq %rdi, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %rbp
shlq $0x20, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x184f6
movq %r13, %rdi
xorl %esi, %esi
callq 0x184f6
movq 0x8(%r12), %rax
testq %rax, %rax
movq 0x28(%rsp), %r8
je 0x18915
movl 0x18(%rsp), %ecx
orl %ecx, 0xc(%rsp)
movq (%rsp), %rcx
orq %rcx, 0x20(%rsp)
movq (%r12), %rcx
movq 0x18(%r12), %rdx
movq %rcx, %rsi
movabsq $0x3ffffffffffffff, %r9 # imm = 0x3FFFFFFFFFFFFFF
andq %r9, %rsi
leaq 0x1(%rcx), %rdi
andq %r9, %rdi
movabsq $-0x400000000000000, %r10 # imm = 0xFC00000000000000
andq %r10, %rcx
andq 0x10(%r12), %rdi
orq %rcx, %rdi
movq %rdi, (%r12)
decq %rax
movq %rax, 0x8(%r12)
movq (%rdx,%rsi,8), %rbx
movq (%r13), %rcx
movq 0x18(%r13), %rdx
leaq 0x1(%rcx), %rax
andq %r9, %rax
movq %rcx, %rsi
andq %r10, %rsi
andq 0x10(%r13), %rax
orq %rsi, %rax
movq %rax, (%r13)
decq 0x8(%r13)
movq %rbx, %rax
shrq $0x20, %rax
cmpb $0x0, (%rbp,%rax)
je 0x18853
movq 0x8(%r12), %rax
testq %rax, %rax
jne 0x187c9
jmp 0x18915
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rcx
movq (%rdx,%rcx,8), %rcx
movq %rcx, (%rsp)
movb $0x1, (%rbp,%rax)
cmpl %ebx, 0xc(%rsp)
jbe 0x18840
movq (%rsp), %rcx
cmpq 0x20(%rsp), %rcx
jae 0x18840
movq 0x10(%rsp), %rcx
movq 0x38(%rcx), %rcx
movl (%rcx,%rax,8), %r14d
testl %r14d, %r14d
je 0x18840
movq 0x10(%rsp), %rdx
movq 0x28(%rdx), %rcx
movq 0x30(%rdx), %rdx
movq (%rdx,%rax,8), %rax
movl %ebx, %edx
incl %edx
movq %rdx, 0x18(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %r15
addq $0x8, %r15
movq (%r15), %rsi
cmpq %r8, %rsi
je 0x18941
cmpb $0x0, (%rbp,%rsi)
jne 0x18907
shlq $0x20, %rsi
addq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x184f6
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq (%r15), %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x18(%rax,%rcx,8), %rsi
addq (%rsp), %rsi
subq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x184f6
movq 0x28(%rsp), %r8
addq $0x30, %r15
decl %r14d
jne 0x188ba
jmp 0x18840
cmpq $0x0, 0x8(%r13)
movl $0x0, %r14d
je 0x18964
leaq 0x631c(%rip), %rdi      # 0x1ec45
leaq 0x6326(%rip), %rsi      # 0x1ec56
leaq 0x6384(%rip), %rcx      # 0x1ecbb
movl $0x502, %edx            # imm = 0x502
callq 0x21d0
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x1894d
movl %ebx, (%rax)
movl $0x1, %r14d
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x18964
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x18(%r12), %rsi
movq 0x20(%r12), %rdi
callq 0x1b5ca
movq 0x20(%r12), %rdi
movq %r12, %rsi
callq 0x1b5ca
movq 0x18(%r13), %rsi
movq 0x20(%r13), %rdi
callq 0x1b5ca
movq 0x20(%r13), %rdi
movq %r13, %rsi
callq 0x1b5ca
movq %rbp, %rdi
callq 0x2060
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq