// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/path.c
// Function: graph_path_extension
static llnode **graph_path_extension(asmg_t *g, llnode *root, kh_u32_t *seg_dups, int max_path, uint64_t *n_leaf, int *_exceed_limit)
{
    uint64_t i, j, v, w, nv;
    int skip, exceed_limit;
    llnode *next_node, *node;
    kdq_llnodep_t *node_q;
    void *km;
    asmg_arc_t *av;
    khint32_t k32;

    kvec_t(llnodep) leaf_node;
    kvec_t(uint32_t) dups;

    kv_init(leaf_node);
    kv_init(dups);
    km = km_init();
    node_q = kdq_init_llnodep(km);

    exceed_limit = 0;
    *kdq_pushp_llnodep(node_q) = root;

    while (kdq_size(node_q) > 0) {
        node = *kdq_shift_llnodep(node_q);
        v = node->v;
        nv = asmg_arc_n(g, v);
        av = asmg_arc_a(g, v);
        dups.n = 0;
        for (i = 0; i < nv; ++i) {
            if (av[i].del) continue;

            w = av[i].w;
            skip = 0;
            k32 = kh_u32_get(seg_dups, w >> 1);
            if (k32 < kh_end(seg_dups)) {
                // dup seg
                // check if already processed
                for (j = 0; j < dups.n; ++j) {
                    if (dups.a[j] == kh_val(seg_dups, k32)) {
                        skip = 1;
                        break;
                    }
                }
            }

            if (!skip && !path_contain_vertex(node, w)) {
                next_node = new_node(w);
                next_node->prev = node;
                add_next(node, next_node);
                *kdq_pushp_llnodep(node_q) = next_node;
                
                if (k32 < kh_end(seg_dups))
                    kv_push(uint32_t, dups, kh_val(seg_dups, k32));
            }
        }

        if (node->n_n == 0) {
            // leaf node
            kv_push(llnodep, leaf_node, node);
        }
        
        if (kdq_size(node_q) + leaf_node.n > max_path) {
            exceed_limit = 1;
            break;
        }
    }

    kdq_destroy_llnodep(node_q);
    km_destroy(km);
    kv_destroy(dups);

    if (exceed_limit) {
        *n_leaf = 0;
        *_exceed_limit = 1;
        kv_destroy(leaf_node);
        return 0;
    } else {
        *n_leaf = leaf_node.n;
        *_exceed_limit = 0;
        return leaf_node.a;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movl %ecx, %ebx
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, 0x70(%rsp)
movabsq $0x3ffffffffffffff, %r13 # imm = 0x3FFFFFFFFFFFFFF
callq 0x145fb
movq %rax, %r15
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
movq %rax, %rdi
callq 0x145b8
movq %rax, %r12
movq (%rax), %rax
andq %r13, %rax
btsq $0x3b, %rax
movq %rax, (%r12)
movq $0x3, 0x10(%r12)
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x14734
movq %rax, 0x18(%r12)
movq %r15, 0x60(%rsp)
movq %r15, 0x20(%r12)
movq %r12, %rdi
callq 0xf037
movq %r14, (%rax)
movslq %ebx, %rax
movq %rax, 0x68(%rsp)
movq 0x8(%r12), %rax
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %ebp, %ebp
movq %r12, 0x30(%rsp)
movq %rax, %r13
movq 0x18(%r12), %rsi
testq %rax, %rax
je 0x6f16
movq %rbp, 0x18(%rsp)
movq %rdx, 0x48(%rsp)
movq %r14, 0x58(%rsp)
movq (%r12), %rax
movq %rax, %rcx
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
andq %rdi, %rcx
leaq 0x1(%rax), %rdx
andq %rdi, %rdx
andq 0x10(%r12), %rdx
movabsq $-0x400000000000000, %rdi # imm = 0xFC00000000000000
andq %rdi, %rax
orq %rax, %rdx
movq %rdx, (%r12)
movq %r13, 0x50(%rsp)
leaq -0x1(%r13), %rax
movq %rax, 0x8(%r12)
movq (%rsi,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movl (%rax), %eax
movq 0x70(%rsp), %rsi
movq 0x30(%rsi), %rcx
movq 0x38(%rsi), %rdx
movq (%rdx,%rax,8), %rdx
imulq $0x30, (%rcx,%rax,8), %rcx
addq 0x28(%rsi), %rcx
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %rdx, 0x10(%rsp)
movq %rcx, (%rsp)
cmpq %rdx, %rbp
je 0x6e72
imulq $0x30, %rbp, %rax
testb $0x40, 0x23(%rcx,%rax)
jne 0x6e6a
addq %rcx, %rax
movq 0x8(%rax), %rbx
movq %rbx, %rsi
shrq %rsi
movq 0x28(%rsp), %rdi
movq %r8, %r15
callq 0x706e
movq %r15, %r8
movq 0x28(%rsp), %rcx
movl %eax, %r13d
movq 0x10(%rcx), %rax
movl %r13d, %r14d
testq %rax, %rax
je 0x6d93
movb (%rcx), %cl
movl %r13d, %edx
shrl %cl, %edx
testl %edx, %edx
je 0x6e43
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x6db0
movl (%rax), %ecx
xorl %ebx, %ecx
cmpl $0x2, %ecx
jb 0x6e61
movq 0x8(%rax), %rax
jmp 0x6d98
movl %ebx, %edi
callq 0x6bbe
movq %rax, %rbx
movq 0x20(%rsp), %rdi
movq %rdi, 0x8(%rax)
movq %rax, %rsi
callq 0x6f8d
movq 0x30(%rsp), %rdi
callq 0xf037
movq 0x28(%rsp), %rcx
movq %rbx, (%rax)
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x6e5e
movb (%rcx), %cl
shrl %cl, %r13d
testl %r13d, %r13d
movq %r15, %r8
jne 0x6e61
movq 0x18(%rsp), %rcx
cmpq %rcx, %r12
jne 0x6e2c
leaq (%rcx,%rcx), %rbx
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq %r8, %rdi
callq 0x3330
movq %rax, %r8
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rbx, 0x18(%rsp)
movq (%rsp), %rcx
movl 0x4(%rax,%r14,8), %eax
movl %eax, (%r8,%r12,4)
incq %r12
movq 0x10(%rsp), %rdx
jmp 0x6e6a
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x6d93
movl (%r8,%rcx,4), %edx
incq %rcx
cmpl 0x4(%rax,%r14,8), %edx
jne 0x6e45
jmp 0x6e61
movq %r15, %r8
movq 0x10(%rsp), %rdx
movq (%rsp), %rcx
incq %rbp
jmp 0x6d3b
movq 0x20(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x6e99
movq 0x30(%rsp), %r12
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %r13
movq 0x48(%rsp), %rdx
movq 0x18(%rsp), %rbp
jmp 0x6ef8
movq 0x58(%rsp), %r14
movq 0x48(%rsp), %rdx
cmpq %rdx, %r14
movq 0x30(%rsp), %r12
movq 0x50(%rsp), %r13
movq 0x18(%rsp), %rbp
jne 0x6ee2
leaq (%rdx,%rdx), %rbx
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
movq %r8, %r15
callq 0x3330
movq %r15, %r8
movq %rbx, %rdx
jmp 0x6ee7
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, (%rax,%r14,8)
incq %r14
movq 0x8(%r12), %rax
leaq (%rax,%r14), %rcx
cmpq 0x68(%rsp), %rcx
jbe 0x6ca6
movq %r8, %rbx
movq 0x18(%r12), %rsi
jmp 0x6f19
movq %r8, %rbx
movq 0x20(%r12), %rdi
callq 0x1463f
movq 0x20(%r12), %rdi
movq %r12, %rsi
callq 0x1463f
movq 0x60(%rsp), %rdi
callq 0x14604
movq %rbx, %rdi
callq 0x3060
testq %r13, %r13
je 0x6f69
movq 0x38(%rsp), %rax
andq $0x0, (%rax)
movq 0x40(%rsp), %rax
movl $0x1, (%rax)
movq 0x8(%rsp), %rdi
callq 0x3060
xorl %eax, %eax
jmp 0x6f7e
movq 0x38(%rsp), %rax
movq %r14, (%rax)
movq 0x40(%rsp), %rax
andl $0x0, (%rax)
movq 0x8(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq