// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/graph.c
// Function: asmg_subgraph
uint32_t *asmg_subgraph(asmg_t *g, uint32_t *seeds, uint32_t n, uint32_t step, 
        uint64_t dist, uint32_t *_nv, int modify_graph)
{
    // make a subgraph from given a vertex set and radius
    // if modify_graph
    //     all vertices and arcs in other components will be marked as deleted
    //     return 0
    // else
    //     keep graph unchanged
    //     return the list of vertices in the subgraph
    if (n == 0) {
        if (_nv) *_nv = 0;
        return 0;
    }

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

    if (step == 0)
        step = UINT32_MAX;
    if (dist == 0)
        dist = UINT64_MAX;

    MYCALLOC(flag, asmg_vtx_n(g));
    // flag deleted segments
    for (i = 0; i < g->n_vtx; ++i) {
        if (g->vtx[i].del) {
            flag[i<<1|0] = -1;
            flag[i<<1|1] = -1;
        }
    }

    q = kdq_init(uint64_t, 0);
    d = kdq_init(uint64_t, 0);
    for (i = 0; i < n; ++i) {
        if (seeds[i] < g->n_vtx) {
            kdq_push(uint64_t, q, ((uint64_t)seeds[i]<<1|0)<<32);
            kdq_push(uint64_t, d, 0);
            kdq_push(uint64_t, q, ((uint64_t)seeds[i]<<1|1)<<32);
            kdq_push(uint64_t, d, 0);
        }
    }

    if (modify_graph) {
        for (i = 0; i < g->n_vtx; ++i) // mark all segments to be deleted
            g->vtx[i].del = 1;
        // for (i = 0; i < g->n_arc; ++i) // mark all arcs to be deleted
        //     g->arc[i].del = 1;
    }

    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] != 0) continue; // already visited or deleted
        flag[v] = 1;
        if (modify_graph) g->vtx[v>>1].del = 0;
        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].del) continue;
                // if (modify_graph) av[i].del = 0;
                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);
                }
                if (flag[av[i].w^1] == 0) {
                    kdq_push(uint64_t, q, (uint64_t)(av[i].w^1)<<32 | (r + 1));
                    kdq_push(uint64_t, d, rd + g->vtx[av[i].w>>1].len - av[i].ls);
                }
            }
        }
    }
    assert(kdq_size(d) == 0);

    // change flag index from vtx<<1 to vtx
    for (i = 0; i < g->n_vtx; ++i)
        flag[i] = (flag[i<<1|0] > 0 || flag[i<<1|1] > 0);

    uint32_t *vs;
    nv = 0;
    vs = 0;
    if (!modify_graph) {
        // make vlist
        kvec_t(uint32_t) vlist;
        kv_init(vlist);
        for (i = 0; i < g->n_vtx; ++i)
            if (flag[i])
                kv_push(uint32_t, vlist, i);
        MYREALLOC(vlist.a, vlist.n);
        vs = vlist.a;
        nv = vlist.n;
    } else {
        // delete dirty arcs
        for (i = 0; i < g->n_arc; ++i) {
            if (!flag[g->arc[i].v>>1] || !flag[g->arc[i].w>>1])
                g->arc[i].del = 1;
        }
        if (_nv) {
            for (i = 0; i < g->n_vtx; ++i)
                nv += flag[i];
        }
    }
    
    if (_nv) *_nv = nv;
    
    kdq_destroy(uint64_t, q);
    kdq_destroy(uint64_t, d);
    free(flag);

    return vs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x10(%rsp)
testl %edx, %edx
je 0x183fe
movq %r8, %r15
movl %ecx, %r13d
movq %rdx, 0x30(%rsp)
movq %r9, 0x20(%rsp)
xorl %eax, %eax
cmpl $0x1, %ecx
movl $0x0, %ecx
sbbl %ecx, %ecx
movl %ecx, 0x1c(%rsp)
cmpq $0x1, %r8
sbbq %rax, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x8(%rsp)
movq (%rdi), %r12
leaq (%r12,%r12), %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r14
testq %r12, %r12
je 0x1806f
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x20, %rax
xorl %ecx, %ecx
testb $0x40, 0x3(%rax)
je 0x18063
movw $0xffff, (%r14,%rcx,2)  # imm = 0xFFFF
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r12
jne 0x18056
orl %r13d, 0x1c(%rsp)
orq %r15, 0x28(%rsp)
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
xorl %ebp, %ebp
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %r15
movq (%rax), %rax
andq %r12, %rax
btsq $0x3b, %rax
movq %rax, (%r15)
movl $0x3, %ebx
movq %rbx, 0x10(%r15)
movl $0x20, %esi
xorl %edi, %edi
callq 0x1b695
movq %rax, 0x18(%r15)
movq %rbp, 0x20(%r15)
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %r13
movq (%rax), %rax
andq %r12, %rax
btsq $0x3b, %rax
movq %rax, (%r13)
movq %rbx, 0x10(%r13)
movl $0x20, %esi
xorl %edi, %edi
callq 0x1b695
movq %rax, 0x18(%r13)
movq %rbp, 0x20(%r13)
movq 0x30(%rsp), %rbx
cmpl $0x1, %ebx
adcl $0x0, %ebx
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %rcx
movl (%rcx,%rbp,4), %esi
cmpq %rsi, (%r12)
jbe 0x18156
shlq $0x21, %rsi
movq %r15, %rdi
callq 0x184f6
movq %r13, %rdi
xorl %esi, %esi
callq 0x184f6
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %esi
shlq $0x21, %rsi
btsq $0x20, %rsi
movq %r15, %rdi
callq 0x184f6
movq %r13, %rdi
xorl %esi, %esi
callq 0x184f6
incq %rbp
cmpq %rbp, %rbx
jne 0x1810f
cmpl $0x0, 0x70(%rsp)
je 0x1833f
cmpq $0x0, (%r12)
je 0x1833f
movl $0x20, %eax
xorl %ecx, %ecx
movq 0x10(%r12), %rdx
orl $0x40000000, (%rdx,%rax) # imm = 0x40000000
incq %rcx
addq $0x28, %rax
cmpq (%r12), %rcx
jb 0x1817b
jmp 0x1833f
movq (%r15), %rcx
movq 0x18(%r15), %rdx
movq %rcx, %rsi
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
andq %r8, %rsi
leaq 0x1(%rcx), %rdi
andq %r8, %rdi
movabsq $-0x400000000000000, %r9 # imm = 0xFC00000000000000
andq %r9, %rcx
andq 0x10(%r15), %rdi
orq %rcx, %rdi
movq %rdi, (%r15)
decq %rax
movq %rax, 0x8(%r15)
movq (%rdx,%rsi,8), %rbp
movq (%r13), %rcx
movq 0x18(%r13), %rdx
leaq 0x1(%rcx), %rax
andq %r8, %rax
movq %rcx, %rsi
andq %r9, %rsi
andq 0x10(%r13), %rax
orq %rsi, %rax
movq %rax, (%r13)
decq 0x8(%r13)
movq %rbp, %rax
shrq $0x20, %rax
cmpb $0x0, (%r14,%rax)
jne 0x1833f
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rcx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
movb $0x1, (%r14,%rax)
cmpl $0x0, 0x70(%rsp)
je 0x1824d
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rbp, %rdx
shrq $0x21, %rdx
leaq (%rdx,%rdx,4), %rdx
andl $0xbfffffff, 0x20(%rcx,%rdx,8) # imm = 0xBFFFFFFF
cmpl %ebp, 0x1c(%rsp)
jbe 0x1833f
movq 0x10(%rsp), %rcx
cmpq 0x28(%rsp), %rcx
jae 0x1833f
movq 0x8(%rsp), %rcx
movq 0x38(%rcx), %rcx
movq (%rcx,%rax,8), %r12
testq %r12, %r12
je 0x1833f
movq 0x8(%rsp), %rdx
movq 0x28(%rdx), %rcx
movq 0x30(%rdx), %rdx
movq (%rdx,%rax,8), %rax
incl %ebp
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %rbx
addq $0x8, %rbx
testb $0x40, 0x1b(%rbx)
jne 0x18332
movq (%rbx), %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x182ec
shlq $0x20, %rsi
orq %rbp, %rsi
movq %r15, %rdi
callq 0x184f6
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rbx), %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x18(%rax,%rcx,8), %rsi
addq 0x10(%rsp), %rsi
subq 0x10(%rbx), %rsi
movq %r13, %rdi
callq 0x184f6
movq (%rbx), %rsi
xorq $0x1, %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x18332
shlq $0x20, %rsi
orq %rbp, %rsi
movq %r15, %rdi
callq 0x184f6
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rbx), %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x18(%rax,%rcx,8), %rsi
addq 0x10(%rsp), %rsi
subq 0x10(%rbx), %rsi
movq %r13, %rdi
callq 0x184f6
addq $0x30, %rbx
decq %r12
jne 0x182a0
movq 0x8(%r15), %rax
testq %rax, %rax
jne 0x18199
cmpq $0x0, 0x8(%r13)
jne 0x184d7
movq 0x8(%rsp), %r8
movq (%r8), %rax
testq %rax, %rax
movq 0x20(%rsp), %rdi
je 0x18389
xorl %ecx, %ecx
movb $0x1, %dl
cmpb $0x0, (%r14,%rcx,2)
jg 0x1837d
cmpb $0x0, 0x1(%r14,%rcx,2)
setg %dl
movb %dl, (%r14,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x1836b
cmpl $0x0, 0x70(%rsp)
je 0x18417
cmpq $0x0, 0x18(%r8)
je 0x183d6
movl $0x20, %eax
xorl %ecx, %ecx
movq 0x28(%r8), %rdx
movq -0x20(%rdx,%rax), %rsi
shrq %rsi
cmpb $0x0, (%r14,%rsi)
je 0x183c4
movq -0x18(%rdx,%rax), %rsi
shrq %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x183c9
orb $0x40, 0x3(%rdx,%rax)
incq %rcx
addq $0x30, %rax
cmpq 0x18(%r8), %rcx
jb 0x183a2
testq %rdi, %rdi
je 0x18411
movq (%r8), %rax
testq %rax, %rax
je 0x18411
xorl %ebx, %ebx
xorl %ecx, %ecx
xorl %ebp, %ebp
movsbq (%r14,%rcx), %rdx
addq %rdx, %rbp
incq %rcx
cmpq %rcx, %rax
jne 0x183e9
jmp 0x18484
testq %r9, %r9
je 0x1840a
movl $0x0, (%r9)
xorl %ebx, %ebx
jmp 0x184c5
xorl %ebp, %ebp
xorl %ebx, %ebx
jmp 0x18484
cmpq $0x0, (%r8)
je 0x1846b
xorl %ebp, %ebp
xorl %eax, %eax
xorl %edi, %edi
xorl %r12d, %r12d
cmpb $0x0, (%r14,%r12)
je 0x1845c
cmpq %rax, %rbp
jne 0x18455
leaq (%rax,%rax), %rbx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x2360
movq %rax, %rdi
movq %rbx, %rax
movl %r12d, (%rdi,%rbp,4)
incq %rbp
incq %r12
movq 0x8(%rsp), %rcx
cmpq (%rcx), %r12
jb 0x18426
jmp 0x1846f
xorl %edi, %edi
xorl %ebp, %ebp
leaq (,%rbp,4), %rsi
callq 0x2360
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1848b
movl %ebp, (%rdi)
movq 0x18(%r15), %rsi
movq 0x20(%r15), %rdi
callq 0x1b5ca
movq 0x20(%r15), %rdi
movq %r15, %rsi
callq 0x1b5ca
movq 0x18(%r13), %rsi
movq 0x20(%r13), %rdi
callq 0x1b5ca
movq 0x20(%r13), %rdi
movq %r13, %rsi
callq 0x1b5ca
movq %r14, %rdi
callq 0x2060
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6767(%rip), %rdi      # 0x1ec45
leaq 0x6771(%rip), %rsi      # 0x1ec56
leaq 0x6772(%rip), %rcx      # 0x1ec5e
movl $0x4a6, %edx            # imm = 0x4A6
callq 0x21d0