// File: /workspace/github_cpp_makefile_star2_O1/JameyWoo[P]re2graph/algorithms.cpp
// Function: re2nfa(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
NFA re2nfa(string rexp) {  // 将正则表达式转化为nfa的函数
    // 对rexp进行一些预处理, 处理[] 的情况
    string prexp, middle;
    bool is_mid = false;
    for (auto c : rexp) {
        if (c == '[') {
            is_mid = true;
            prexp.push_back('(');
        } else if (c == ']') {
            string mid = parse_range(middle);
            for (auto m : mid) {
                prexp += m;
                prexp += '|';
            }
            prexp.pop_back();
            prexp.push_back(')');
            middle = "";
            is_mid = false;
        } else {
            if (is_mid)
                middle += c;
            else
                prexp += c;
        }
    }
    // cout << "prexp: " << prexp << endl;
    rexp = prexp;

    int state       = 0;
    Graph nfa_graph = parse_re(rexp, state);
    // cout << "start: " << nfa_graph.start << endl;
    // cout << "end: " << nfa_graph.end << endl;
    // for (auto edge : nfa_graph.edges) {
    //     cout << edge.from << ' ' << edge.cond << ' ' << edge.to << endl;
    // }

    // 获取除规则字符之外的字符
    set<char> char_set;
    char_set.insert('~');
    for (auto c : rexp) {
        if (not(c == '|' || c == '(' || c == ')' || c == '*')) {
            char_set.insert(c);
        }
    }

    // 将数据存入到nfa中
    NFA nfa;
    nfa.char_cnt      = char_set.size();
    nfa.state_cnt     = state;
    nfa.start_state   = 0;
    nfa.end_state_cnt = 1;
    nfa.end_states.push_back(state - 1);
    for (auto s : char_set) {
        nfa.chars.push_back(s);
    }
    for (int i = 0; i < state; ++i) {
        nfa.states.push_back(i);
    }
    vector<map<char, vector<int>>> jump(state);
    for (auto edge : nfa_graph.edges) {
        jump[edge.from][edge.cond].push_back(edge.to);
    }
    nfa.jump = jump;
    return nfa;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %rbx
leaq 0xa0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x50(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rsi, 0x38(%rsp)
movq 0x8(%rsi), %r12
testq %r12, %r12
je 0x47a1
movq 0x38(%rsp), %rax
movq (%rax), %r14
addq %r14, %r12
leaq 0xc8(%rsp), %rbp
xorl %r15d, %r15d
leaq 0x90(%rsp), %r13
movq %r12, 0xb0(%rsp)
movzbl (%r14), %eax
cmpl $0x5d, %eax
je 0x469a
cmpl $0x5b, %eax
jne 0x477d
movb $0x1, %r15b
movq %r13, %rdi
movl $0x28, %esi
callq 0x21c0
jmp 0x4795
movq %rbp, 0xb8(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
callq 0x44e2
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x4b9c
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x46e9
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x68(%rsp), %r15
testq %r15, %r15
je 0x471c
movq 0x60(%rsp), %r12
xorl %ebp, %ebp
movsbl (%r12,%rbp), %esi
movq %r13, %rdi
callq 0x21c0
movq %r13, %rdi
movl $0x7c, %esi
callq 0x21c0
incq %rbp
cmpq %rbp, %r15
jne 0x46fa
movq %r13, %rdi
callq 0x2280
movq %r13, %rdi
movl $0x29, %esi
callq 0x21c0
movq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
leaq 0xeaad(%rip), %rcx      # 0x131f1
xorl %r8d, %r8d
callq 0x22e0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
movq 0xb0(%rsp), %r12
je 0x4770
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2200
xorl %r15d, %r15d
leaq 0xc8(%rsp), %rbp
jmp 0x4795
testb $0x1, %r15b
movq %r13, %rdi
leaq 0x40(%rsp), %rcx
cmovneq %rcx, %rdi
movsbl %al, %esi
callq 0x21c0
incq %r14
cmpq %r12, %r14
jne 0x4673
leaq 0x90(%rsp), %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x2100
movl $0x0, 0xc(%rsp)
leaq 0xe8(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0xd8(%rsp), %rdi
callq 0x44e2
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x4ca3
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4818
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x68(%rsp), %r13
movl $0x0, (%r13)
xorl %eax, %eax
movq %rax, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rax, 0x20(%r13)
leaq 0x10(%rsp), %rsi
movb $0x7e, (%rsi)
leaq 0x60(%rsp), %rdi
callq 0xebb2
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x4889
movq (%r15), %rbp
xorl %r14d, %r14d
leaq 0x10(%rsp), %r15
movzbl (%rbp,%r14), %eax
movb %al, 0x10(%rsp)
leal -0x28(%rax), %ecx
cmpl $0x3, %ecx
jb 0x4881
cmpl $0x7c, %eax
je 0x4881
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0xecc2
incq %r14
cmpq %r14, %r12
jne 0x485d
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
movups %xmm0, 0x50(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movl 0x88(%rsp), %eax
movl %eax, (%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rbx)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x8(%rbx)
decl %eax
movl %eax, 0x10(%rsp)
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x48de
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rbx)
jmp 0x48ec
leaq 0x40(%rbx), %rdi
leaq 0x10(%rsp), %rdx
callq 0xed6c
movq 0x78(%rsp), %r14
cmpq %r13, %r14
je 0x4934
leaq 0x10(%rbx), %r15
leaq 0x10(%rsp), %r12
movb 0x20(%r14), %al
movb %al, 0x10(%rsp)
movq 0x18(%rbx), %rsi
cmpq 0x20(%rbx), %rsi
je 0x4919
movb %al, (%rsi)
incq 0x18(%rbx)
jmp 0x4924
movq %r15, %rdi
movq %r12, %rdx
callq 0xee8c
movq %r14, %rdi
callq 0x2250
movq %rax, %r14
cmpq %r13, %rax
jne 0x48ff
movl $0x0, 0x10(%rsp)
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
jle 0x4982
leaq 0x28(%rbx), %r14
xorl %eax, %eax
leaq 0x10(%rsp), %r15
movq 0x30(%rbx), %rsi
cmpq 0x38(%rbx), %rsi
je 0x4965
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x30(%rbx)
jmp 0x4970
movq %r14, %rdi
movq %r15, %rdx
callq 0xef7e
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
jl 0x494f
movslq %ecx, %rsi
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xd424
movq 0x100(%rsp), %r12
movq 0x108(%rsp), %r13
cmpq %r13, %r12
je 0x4a0a
leaq 0x30(%rsp), %r14
leaq 0x2c(%rsp), %r15
movl 0x8(%r12), %eax
movl %eax, 0x30(%rsp)
movq (%r12), %rax
movq %rax, 0x28(%rsp)
cltq
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
addq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0xd4c0
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x49f6
movl 0x2c(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x4a01
movq %rax, %rdi
movq %r15, %rdx
callq 0xef7e
addq $0xc, %r12
cmpq %r13, %r12
jne 0x49b3
leaq 0x58(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xd524
leaq 0x10(%rsp), %rdi
callq 0x412e
leaq 0x60(%rsp), %rdi
callq 0xe2aa
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x4a49
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4a65
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x4a8a
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4aef
jmp 0x4afb
jmp 0x4aef
jmp 0x4acc
movq %rax, %r15
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4b53
movq 0xe8(%rsp), %rsi
jmp 0x4b4b
jmp 0x4af6
jmp 0x4af6
movq %rax, %r15
jmp 0x4b10
movq %rax, %r15
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4b53
movq 0xc8(%rsp), %rsi
jmp 0x4b4b
jmp 0x4af6
jmp 0x4aef
movq %rax, %r15
jmp 0x4b08
jmp 0x4b34
movq %rax, %r15
jmp 0x4b53
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x412e
movq %rbx, %rdi
callq 0x3ae6
leaq 0x60(%rsp), %rdi
callq 0xe2aa
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x4b53
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
jmp 0x4b4e
movq %rax, %r15
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4b53
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4b6f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x4b94
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2200
movq %r15, %rdi
callq 0x2350