// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
testl %edx, %edx
je 0x149e9
movl %ecx, %r15d
movl %edx, %ebx
movq %rdi, %r13
movq %r8, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq (%rdi), %r12
leaq (%r12,%r12), %rdi
pushq $0x1
popq %rsi
callq 0x2250
movq %rax, %r14
pushq $0x10
popq %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x14760
movq 0x10(%r13), %rdx
testb $0x40, 0x3(%rdx,%rax,2)
je 0x14757
orw $-0x1, (%r14,%rcx,2)
incq %rcx
addq $0x14, %rax
jmp 0x14741
xorl %r12d, %r12d
cmpl $0x1, %r15d
movl $0x0, %eax
sbbl %eax, %eax
orl %r15d, %eax
movl %eax, 0x24(%rsp)
callq 0x14ab0
movq %rax, 0x8(%rsp)
callq 0x14ab0
movq %rax, 0x10(%rsp)
movl %ebx, %r15d
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rbp
cmpq %r12, %r15
je 0x147e9
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %esi
cmpq %rsi, (%r13)
jbe 0x147e4
shlq $0x21, %rsi
movq %rbx, %rdi
callq 0x14af6
movq %rbp, %rdi
xorl %esi, %esi
callq 0x14af6
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %esi
shlq $0x21, %rsi
btsq $0x20, %rsi
movq %rbx, %rdi
callq 0x14af6
movq %rbp, %rdi
xorl %esi, %esi
callq 0x14af6
incq %r12
jmp 0x14796
xorl %eax, %eax
movq 0x30(%rsp), %rsi
cmpq $0x1, %rsi
sbbq %rax, %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x70(%rsp)
je 0x14822
pushq $0x20
popq %rax
xorl %ecx, %ecx
cmpq (%r13), %rcx
jae 0x14822
movq 0x10(%r13), %rdx
orl $0x40000000, (%rdx,%rax) # imm = 0x40000000
incq %rcx
addq $0x28, %rax
jmp 0x14808
orq %rsi, 0x18(%rsp)
movq 0x8(%rsp), %rdi
cmpq $0x0, 0x8(%rdi)
je 0x14949
callq 0x14c47
movq (%rax), %rbp
movq %rbp, %r15
shrq $0x20, %r15
movq 0x10(%rsp), %rdi
callq 0x14c47
cmpb $0x0, (%r14,%r15)
jne 0x14827
movq (%rax), %rbx
movb $0x1, (%r14,%r15)
cmpl $0x0, 0x70(%rsp)
je 0x1487d
movq 0x10(%r13), %rax
movq %rbp, %rcx
shrq $0x21, %rcx
imulq $0x28, %rcx, %rcx
andl $0xbfffffff, 0x20(%rax,%rcx) # imm = 0xBFFFFFFF
cmpl %ebp, 0x24(%rsp)
jbe 0x14827
cmpq 0x18(%rsp), %rbx
jae 0x14827
movq 0x38(%r13), %rax
movq (%rax,%r15,8), %r12
movq 0x28(%r13), %rax
movq 0x30(%r13), %rcx
incl %ebp
imulq $0x30, (%rcx,%r15,8), %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
subq $0x1, %r12
jb 0x14827
testb $0x40, 0x1b(%r15)
jne 0x14940
movq (%r15), %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x14900
shlq $0x20, %rsi
orq %rbp, %rsi
movq 0x8(%rsp), %rdi
callq 0x14af6
movq 0x10(%r13), %rax
movq (%r15), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
movq 0x18(%rax,%rcx), %rsi
addq %rbx, %rsi
subq 0x10(%r15), %rsi
movq 0x10(%rsp), %rdi
callq 0x14af6
movq (%r15), %rsi
xorq $0x1, %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x14940
shlq $0x20, %rsi
orq %rbp, %rsi
movq 0x8(%rsp), %rdi
callq 0x14af6
movq 0x10(%r13), %rax
movq (%r15), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
movq 0x18(%rax,%rcx), %rsi
addq %rbx, %rsi
subq 0x10(%r15), %rsi
movq 0x10(%rsp), %rdi
callq 0x14af6
addq $0x30, %r15
jmp 0x148a9
movq 0x10(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x14a91
movq (%r13), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1497f
movb $0x1, %dl
cmpb $0x0, (%r14,%rcx,2)
jg 0x14976
cmpb $0x0, 0x1(%r14,%rcx,2)
setg %dl
movb %dl, (%r14,%rcx)
incq %rcx
jmp 0x1495f
cmpl $0x0, 0x70(%rsp)
je 0x149f9
pushq $0x20
popq %rax
xorl %ecx, %ecx
cmpq 0x18(%r13), %rcx
jae 0x149c1
movq 0x28(%r13), %rdx
movq -0x20(%rdx,%rax), %rsi
shrq %rsi
cmpb $0x0, (%r14,%rsi)
je 0x149b3
movq -0x18(%rdx,%rax), %rsi
shrq %rsi
cmpb $0x0, (%r14,%rsi)
jne 0x149b8
orb $0x40, 0x3(%rdx,%rax)
incq %rcx
addq $0x30, %rax
jmp 0x1498b
cmpq $0x0, 0x28(%rsp)
je 0x14a66
movq (%r13), %rax
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x14a53
movsbq (%r14,%rcx), %rdx
addq %rdx, %rbp
incq %rcx
jmp 0x149d7
testq %r9, %r9
je 0x149f2
andl $0x0, (%r9)
xorl %ebx, %ebx
jmp 0x14a7f
xorl %ebx, %ebx
pushq $0x2
popq %r15
xorl %edi, %edi
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpq %rax, %rbx
jae 0x14a43
cmpb $0x0, (%r14,%rbx)
je 0x14a3e
cmpq %rcx, %rbp
jne 0x14a38
leaq (%rcx,%rcx), %r12
testq %rcx, %rcx
cmoveq %r15, %r12
leaq (,%r12,4), %rsi
callq 0x2350
movq %rax, %rdi
movq (%r13), %rax
movq %r12, %rcx
movl %ebx, (%rdi,%rbp,4)
incq %rbp
incq %rbx
jmp 0x14a05
leaq (,%rbp,4), %rsi
callq 0x2350
movq %rax, %rbx
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x14a5f
movl %ebp, (%rax)
movq 0x8(%rsp), %rdi
jmp 0x14a68
xorl %ebx, %ebx
callq 0x14c91
movq 0x10(%rsp), %rdi
callq 0x14c91
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 0x6218(%rip), %rdi      # 0x1acb0
leaq 0x6222(%rip), %rsi      # 0x1acc1
leaq 0x6223(%rip), %rcx      # 0x1acc9
movl $0x4a6, %edx            # imm = 0x4A6
callq 0x21d0