// File: /workspace/github_c_makefile_star2_O3/MaoKo[P]FGFX/fgfl/src/nfa.c
// Function: build_nfa
int
build_nfa(lexical_spec_t* spec) {
    if (!spec)
        { return (ERROR); }

    // Transform all regex to nfa
    for (size_t i = 0; i < SIZE_VECTOR(spec->entry_vect); ++i) {
        spec_entry_t* entry = (spec_entry_t*)AT_VECTOR(spec->entry_vect, i);
        if (entry->kind == T_KEYWORD || entry->fragment)
            { continue; }
        else if (transform_regex_nfa(entry) == ERROR)
            { return (ERROR); }
    }

    bool active_state = (spec->start_state != -1);
    if (active_state)
        { return (build_nfa_from_state(spec)); }
    else {
        bitset_t* set_frag = new_bitset();
        for (size_t i = 0; i < SIZE_VECTOR(spec->entry_vect); ++i) {
            spec_entry_t* entry = (spec_entry_t*)AT_VECTOR(spec->entry_vect, i);
            if (entry->kind != T_KEYWORD && entry->nfa_m)
                { ADD_BITSET(set_frag, GET_INDEX(entry->nfa_m)); }
        }
        nfa_automaton_t* nfa_m = new_nfa_automaton(NFA_MASTER, set_frag);
        spec->master = new_nfa_state(EDGE_AUTOMATA, nfa_m, NULL_NFA_STATE);
    }

    return (DONE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x58c2
movq %rdi, %rbx
movq 0x8(%rdi), %rax
cmpq $0x0, 0x10(%rax)
je 0x5724
xorl %r13d, %r13d
movq (%rax), %rcx
movq (%rcx,%r13,8), %r14
cmpl $0x42, 0x8(%r14)
je 0x571b
cmpb $0x0, 0x18(%r14)
jne 0x571b
movq 0x20(%r14), %r15
movq %r15, %rdi
callq 0x5b26
movq %rax, %r12
movq %rax, 0x28(%r14)
testq %rax, %rax
je 0x56eb
movl (%r14), %eax
movq 0x28(%r12), %rcx
movl %eax, 0x8(%rcx)
cmpb $0x1, 0x19(%r14)
jne 0x56fe
movq 0x28(%r14), %rax
movq 0x28(%rax), %rax
movb $0x1, 0xc(%rax)
movq %r15, %rdi
callq 0x7b74
movq $0x0, 0x20(%r14)
testq %r12, %r12
je 0x58bc
movq 0x8(%rbx), %rax
incq %r13
cmpq 0x10(%rax), %r13
jb 0x56b3
cmpl $-0x1, 0x18(%rbx)
je 0x582b
movq 0x10(%rbx), %rax
cmpq $0x0, 0x10(%rax)
je 0x58b7
xorl %ecx, %ecx
movq (%rax), %rax
movq %rcx, -0x30(%rbp)
movq (%rax,%rcx,8), %r13
callq 0x247c
movq %rax, %r15
movq 0x8(%rbx), %rax
cmpq $0x0, 0x10(%rax)
je 0x57ed
xorl %r14d, %r14d
movq (%rax), %rax
movq (%rax,%r14,8), %r12
cmpl $0x42, 0x8(%r12)
je 0x57dc
cmpq $0x0, 0x28(%r12)
je 0x57dc
movq 0x38(%r12), %rdi
movl (%r13), %esi
callq 0x3bfb
cmpl $-0x1, %eax
je 0x57b5
movq 0x28(%r12), %rax
movq (%rax), %rcx
movl 0x4(%r15), %eax
cmpq %rax, %rcx
jae 0x57d1
movl $0x1, %eax
shlq %cl, %rax
movq 0x8(%r15), %rdx
shrq $0x6, %rcx
orq %rax, (%rdx,%rcx,8)
jmp 0x57dc
cmpq $0x0, 0x38(%r12)
jne 0x57dc
movslq 0x18(%rbx), %rax
cmpq %rax, (%r13)
je 0x578e
cmpb $0x1, 0x34(%r12)
je 0x578e
jmp 0x57dc
movq %r15, %rdi
movq %rcx, %rsi
callq 0x25b0
incq %r14
movq 0x8(%rbx), %rax
cmpq 0x10(%rax), %r14
jb 0x5764
xorl %r14d, %r14d
xorl %edi, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x58d4
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rax, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x54aa
movq %rax, 0x18(%r13)
movq -0x30(%rbp), %rcx
incq %rcx
movq 0x10(%rbx), %rax
cmpq 0x10(%rax), %rcx
jb 0x573f
jmp 0x58c2
callq 0x247c
movq %rax, %r15
movq 0x8(%rbx), %rax
cmpq $0x0, 0x10(%rax)
je 0x5891
xorl %r14d, %r14d
movq (%rax), %rcx
movq (%rcx,%r14,8), %rcx
cmpl $0x42, 0x8(%rcx)
je 0x5888
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
je 0x5888
movq (%rcx), %rcx
movl 0x4(%r15), %edx
cmpq %rdx, %rcx
jae 0x5879
movl $0x1, %edx
shlq %cl, %rdx
movq 0x8(%r15), %rsi
shrq $0x6, %rcx
orq %rdx, (%rsi,%rcx,8)
jmp 0x5888
movq %r15, %rdi
movq %rcx, %rsi
callq 0x25b0
movq 0x8(%rbx), %rax
incq %r14
cmpq 0x10(%rax), %r14
jb 0x5841
xorl %r14d, %r14d
xorl %edi, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x58d4
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rax, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x54aa
movq %rax, 0x20(%rbx)
jmp 0x58c2
xorl %r14d, %r14d
jmp 0x58c2
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq