// File: /workspace/github_cpp_makefile_star2_O3/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 0x479a
movq 0x38(%rsp), %rax
movq (%rax), %r13
addq %r13, %r12
leaq 0xc8(%rsp), %rbp
xorl %r14d, %r14d
leaq 0x90(%rsp), %r15
movq %r12, 0xb0(%rsp)
movzbl (%r13), %eax
cmpl $0x5b, %eax
je 0x4761
cmpl $0x5d, %eax
jne 0x4768
movq %rbp, 0xb8(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x44cc
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x4b7f
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x46cd
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x68(%rsp), %r14
testq %r14, %r14
je 0x4700
movq 0x60(%rsp), %r12
xorl %ebp, %ebp
movsbl (%r12,%rbp), %esi
movq %r15, %rdi
callq 0x21c0
movq %r15, %rdi
movl $0x7c, %esi
callq 0x21c0
incq %rbp
cmpq %rbp, %r14
jne 0x46de
movq %r15, %rdi
callq 0x2280
movq %r15, %rdi
movl $0x29, %esi
callq 0x21c0
movq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
leaq 0xeac9(%rip), %rcx      # 0x131f1
xorl %r8d, %r8d
callq 0x22e0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
movq 0xb0(%rsp), %r12
je 0x4754
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2200
xorl %r14d, %r14d
leaq 0xc8(%rsp), %rbp
jmp 0x478e
movb $0x28, %al
movq %r15, %rdi
jmp 0x4773
testb $0x1, %r14b
je 0x4780
leaq 0x40(%rsp), %rdi
movb $0x1, %r14b
movsbl %al, %esi
callq 0x21c0
jmp 0x478e
movsbl %al, %esi
movq %r15, %rdi
callq 0x21c0
xorl %r14d, %r14d
incq %r13
cmpq %r12, %r13
jne 0x4667
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 0x44cc
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x4c79
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4811
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 0xebac
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x4882
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 0x487a
cmpl $0x7c, %eax
je 0x487a
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0xecba
incq %r14
cmpq %r14, %r12
jne 0x4856
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
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
leaq 0x40(%rbx), %rdi
xorl %esi, %esi
callq 0xed64
movq 0x78(%rsp), %r14
cmpq %r13, %r14
je 0x4917
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 0x48fc
movb %al, (%rsi)
incq 0x18(%rbx)
jmp 0x4907
movq %r15, %rdi
movq %r12, %rdx
callq 0xee84
movq %r14, %rdi
callq 0x2250
movq %rax, %r14
cmpq %r13, %rax
jne 0x48e2
movl $0x0, 0x10(%rsp)
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
jle 0x4965
leaq 0x28(%rbx), %r14
xorl %eax, %eax
leaq 0x10(%rsp), %r15
movq 0x30(%rbx), %rsi
cmpq 0x38(%rbx), %rsi
je 0x4948
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x30(%rbx)
jmp 0x4953
movq %r14, %rdi
movq %r15, %rdx
callq 0xef76
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
jl 0x4932
movslq %ecx, %rsi
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xd384
movq 0x100(%rsp), %r12
movq 0x108(%rsp), %r13
cmpq %r13, %r12
je 0x49ed
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 0xd41c
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x49d9
movl 0x2c(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x49e4
movq %rax, %rdi
movq %r15, %rdx
callq 0xef76
addq $0xc, %r12
cmpq %r13, %r12
jne 0x4996
leaq 0x58(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xd482
leaq 0x10(%rsp), %rdi
callq 0x410c
leaq 0x60(%rsp), %rdi
callq 0xe288
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x4a2c
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4a48
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x4a6d
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 0x4ade
jmp 0x4ad2
jmp 0x4ad2
jmp 0x4aaf
movq %rax, %r15
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4b36
movq 0xe8(%rsp), %rsi
jmp 0x4b2e
jmp 0x4ad9
jmp 0x4ad9
movq %rax, %r15
jmp 0x4af3
movq %rax, %r15
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4b36
movq 0xc8(%rsp), %rsi
jmp 0x4b2e
jmp 0x4ad9
jmp 0x4ad2
movq %rax, %r15
jmp 0x4aeb
jmp 0x4b17
movq %rax, %r15
jmp 0x4b36
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x410c
movq %rbx, %rdi
callq 0x3ac4
leaq 0x60(%rsp), %rdi
callq 0xe288
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x4b36
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
jmp 0x4b31
movq %rax, %r15
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4b36
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4b52
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x4b77
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2200
movq %r15, %rdi
callq 0x2350