// File: /workspace/github_cpp_makefile_star2_O3/arush15june[P]DFA-CPP/dfa.cpp
// Function: build_dfa_from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
DFA build_dfa_from_file(std::string filename) {
    /*
    Build DFA and StateDiagram from file.

    File Structure:
        (Adjacency List's)

    First Line
        q -> initial state
    Second Line
        f -> final state
    Next Line(s):
        <vertex_no>: <weight> <vertex_no> | [<weight> <vertex_no> ... | ...] 
    ``` dfa.gph
    1               -> initial state
    2               -> final state
    1: 97 2 | 37 3    -> Adjacency List for Node 1
    2: 97 1 | 27 3
    3: 37 1 | 27 2
    ```

    @param std::string filename: filename to read from
    @return DFA dfa: returns a DFA.
    */

    StateDiagram state_diagram;
    DFA dfa;
    
    std::ifstream state_file(filename);
    std::string str;
    int counter = 0;
    int nvertices, nedges;

    while(std::getline(state_file, str)) {
        if(counter == 0) {
            dfa.setInitialState(std::stoi(str));
        }
        else if(counter == 1){
            dfa.setFinalState(std::stoi(str));
        }
        else {
            /*  Build State Diagram
                - Split at colon.
                - set current state
                - Split the part after colon at |
                - Iterate over split at |
                    - Trim whitespace
                    - Split pair at space
                    - Convert to integers
                        weight -> pair[0]
                        state_no_y -> pair[1]
                    - insert edge in state diagram.
            */
            auto split_at_colon = split(str, ':');
            auto state = std::stoi(split_at_colon[0]);

            nvertices += 1;
            
            auto adj_list_vector = split(split_at_colon[1], '|');
            auto degree = 0;
            for(auto &pair: adj_list_vector) {
                auto trimmed_pair = trim_copy(pair);
                auto split_pair = split(trimmed_pair, ' ');
                
                int weight = std::stoi(split_pair[0]);
                int state_no_y = std::stoi(split_pair[1]);
                
                state_diagram.insertEdge(state, weight, state_no_y);
                nedges += 1;
                degree += 1;
            }
            state_diagram.degree[state] = degree;
        }
        counter++;
    }
    state_diagram.nvertices = nvertices;
    state_diagram.nedges = nedges;

    dfa.setStateDiagram(state_diagram);

    return dfa;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xde18, %rsp           # imm = 0xDE18
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x70b0(%rbp), %r15
xorl %r13d, %r13d
movl $0x5dd8, %edx           # imm = 0x5DD8
movq %r15, %rdi
xorl %esi, %esi
callq 0x20d0
movq %r13, 0x6d7c(%r15)
movl $0x5dd8, %edx           # imm = 0x5DD8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20d0
movq %rbx, -0x30(%rbp)
movq %r13, 0x6d7c(%rbx)
leaq -0x328(%rbp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x21f0
leaq -0xa8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x48(%rbp), %r14
leaq -0x328(%rbp), %rbx
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %edx
movq %rbx, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x2230
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2b8d
cmpl $0x1, %r13d
je 0x264c
testl %r13d, %r13d
jne 0x26bc
movq -0xb8(%rbp), %r15
callq 0x2040
movq %rax, %r12
movl (%rax), %ebx
movl $0x0, (%rax)
movq %r15, %rdi
leaq -0x78(%rbp), %rsi
movl $0xa, %edx
callq 0x21a0
cmpq %r15, -0x78(%rbp)
je 0x2cfc
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x2d08
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x2d08
testl %ecx, %ecx
jne 0x263d
movl %ebx, (%r12)
movq -0x30(%rbp), %rcx
movl %eax, 0x6d88(%rcx)
jmp 0x2b85
movq -0xb8(%rbp), %r15
callq 0x2040
movq %rax, %r12
movl (%rax), %ebx
movl $0x0, (%rax)
movq %r15, %rdi
leaq -0x78(%rbp), %rsi
movl $0xa, %edx
callq 0x21a0
cmpq %r15, -0x78(%rbp)
je 0x2d14
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x2d20
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x2d20
testl %ecx, %ecx
jne 0x26ad
movl %ebx, (%r12)
movq -0x30(%rbp), %rcx
movl %eax, 0x6d8c(%rcx)
jmp 0x2b85
leaq -0x120(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
movl $0x3a, %edx
callq 0x23fc
movq -0x120(%rbp), %rax
movq (%rax), %r12
callq 0x2040
movq %rax, %r15
movl (%rax), %ebx
movl $0x0, (%rax)
movq %r12, %rdi
leaq -0x78(%rbp), %rsi
movl $0xa, %edx
callq 0x21a0
cmpq %r12, -0x78(%rbp)
movq %r15, -0x38(%rbp)
je 0x2d2c
leaq -0x80000000(%rax), %rdx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rdx
jb 0x2d38
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x2d38
movq %rax, -0x98(%rbp)
testl %ecx, %ecx
jne 0x2741
movl %ebx, (%r15)
movq -0x120(%rbp), %rsi
addq $0x20, %rsi
leaq -0x108(%rbp), %rdi
movl $0x7c, %edx
callq 0x23fc
movq -0x108(%rbp), %r15
movq -0x100(%rbp), %rax
movq %rax, -0xd0(%rbp)
xorl %ebx, %ebx
cmpq %rax, %r15
je 0x2b59
movq %r13, -0xc8(%rbp)
movq %r14, -0x58(%rbp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x3758
movq %r15, -0xd8(%rbp)
movl %ebx, -0x8c(%rbp)
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %rax
leaq (%rbx,%rax), %r13
movq %rax, %r14
sarq $0x2, %r14
movq %rbx, %r15
testq %r14, %r14
jle 0x2820
andq $-0x4, %rax
movq %rax, %r15
addq %rbx, %r15
incq %r14
leaq 0x3(%rbx), %r12
movsbl -0x3(%r12), %edi
callq 0x2050
testl %eax, %eax
je 0x286c
movsbl -0x2(%r12), %edi
callq 0x2050
testl %eax, %eax
je 0x2872
movsbl -0x1(%r12), %edi
callq 0x2050
testl %eax, %eax
je 0x2878
movsbl (%r12), %edi
callq 0x2050
testl %eax, %eax
je 0x287b
decq %r14
addq $0x4, %r12
cmpq $0x1, %r14
jg 0x27d4
movq %r13, %rax
subq %r15, %rax
cmpq $0x1, %rax
je 0x2858
cmpq $0x2, %rax
je 0x2848
cmpq $0x3, %rax
jne 0x2893
movsbl (%r15), %edi
callq 0x2050
testl %eax, %eax
je 0x2867
incq %r15
movsbl (%r15), %edi
callq 0x2050
testl %eax, %eax
je 0x2867
incq %r15
movsbl (%r15), %edi
callq 0x2050
testl %eax, %eax
cmovneq %r13, %r15
movq %r15, %r12
jmp 0x287b
addq $-0x3, %r12
jmp 0x287b
addq $-0x2, %r12
jmp 0x287b
decq %r12
cmpq %r12, %r13
je 0x2893
subq %rbx, %r12
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2190
jmp 0x289e
movq $0x0, -0x50(%rbp)
movb $0x0, (%rbx)
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %r14
leaq (%r15,%r14), %r12
movq %r14, %r13
sarq $0x2, %r13
testq %r13, %r13
jle 0x2941
movq %r14, %rax
andq $-0x4, %rax
movq %r12, %rbx
subq %rax, %rbx
leaq (,%r13,4), %rax
subq %rax, %r12
movq %r12, -0xc0(%rbp)
incq %r13
movsbl -0x1(%r15,%r14), %edi
callq 0x2050
testl %eax, %eax
je 0x29a5
movsbl -0x2(%r15,%r14), %edi
callq 0x2050
testl %eax, %eax
je 0x29aa
movsbl -0x3(%r15,%r14), %edi
callq 0x2050
testl %eax, %eax
je 0x29b2
movsbl -0x4(%r15,%r14), %edi
callq 0x2050
testl %eax, %eax
je 0x29bb
addq $-0x4, %r14
decq %r13
cmpq $0x1, %r13
jg 0x28dc
movq %rbx, %r12
movq -0xc0(%rbp), %rbx
jmp 0x2944
movq %r12, %rbx
movq %r12, %rax
subq %r15, %rax
cmpq $0x1, %rax
je 0x298f
cmpq $0x2, %rax
je 0x2977
movq %r15, %r14
cmpq $0x3, %rax
jne 0x29c2
movsbl -0x1(%r12), %edi
callq 0x2050
movq %r12, %r14
testl %eax, %eax
je 0x29c2
decq %rbx
movq %rbx, %r12
movsbl -0x1(%r12), %edi
callq 0x2050
movq %r12, %r14
testl %eax, %eax
je 0x29c2
decq %rbx
movq %rbx, %r12
movsbl -0x1(%r12), %edi
callq 0x2050
testl %eax, %eax
cmovneq %r15, %r12
movq %r12, %r14
jmp 0x29c2
addq %r15, %r14
jmp 0x29c2
addq %r15, %r14
decq %r14
jmp 0x29c2
addq %r15, %r14
addq $-0x2, %r14
jmp 0x29c2
addq %r15, %r14
addq $-0x3, %r14
movq %r14, %rax
subq %r15, %rax
movq %rax, -0x50(%rbp)
movb $0x0, (%r14)
leaq -0x68(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq -0x58(%rbp), %rax
leaq -0x48(%rbp), %r14
cmpq %r14, %rax
je 0x29f3
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x29fa
movups (%r14), %xmm0
movups %xmm0, (%rcx)
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %r14, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movb $0x0, -0x48(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movl $0x20, %edx
callq 0x23fc
movq -0xf0(%rbp), %rax
movq (%rax), %r15
movq -0x38(%rbp), %rax
movl (%rax), %r13d
movl $0x0, (%rax)
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
movl $0xa, %edx
callq 0x21a0
cmpq %r15, -0x80(%rbp)
je 0x2ce0
movq %rax, %r12
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
movq -0x38(%rbp), %r15
jb 0x2cf0
movl (%r15), %ebx
cmpl $0x22, %ebx
je 0x2cf0
testl %ebx, %ebx
jne 0x2a8f
movl %r13d, (%r15)
movl %r13d, %ebx
movq -0xf0(%rbp), %rax
movq %r15, %rcx
movq 0x20(%rax), %r15
movl $0x0, (%rcx)
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
movl $0xa, %edx
callq 0x21a0
cmpq %r15, -0x80(%rbp)
je 0x2cd0
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
movq -0x38(%rbp), %r15
jb 0x2cc4
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x2cc4
testl %ecx, %ecx
jne 0x2aef
movl %ebx, (%r15)
leaq -0x70b0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl %r12d, %edx
movl %eax, %ecx
callq 0x336c
movq -0xc8(%rbp), %r13
leaq -0xf0(%rbp), %rdi
callq 0x32fe
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x2b33
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2170
incl -0x88(%rbp)
movl -0x8c(%rbp), %ebx
incl %ebx
movq -0xd8(%rbp), %r15
addq $0x20, %r15
cmpq -0xd0(%rbp), %r15
jne 0x2784
incl -0x84(%rbp)
movslq -0x98(%rbp), %rax
movl %ebx, -0x12d8(%rbp,%rax,4)
leaq -0x108(%rbp), %rdi
callq 0x32fe
leaq -0x120(%rbp), %rdi
callq 0x32fe
incl %r13d
jmp 0x258a
movl -0x84(%rbp), %eax
movl %eax, -0x334(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x330(%rbp)
xorl %ebx, %ebx
leaq (%rbx,%rbp), %r15
addq $-0xde38, %r15          # imm = 0xFFFF21C8
leaq (%rbx,%rbp), %rsi
addq $-0x70b0, %rsi          # imm = 0x8F50
movq %r15, %rdi
callq 0x36d8
addq $0x18, %rbx
cmpq $0x5dd8, %rbx           # imm = 0x5DD8
jne 0x2ba7
leaq -0x8060(%rbp), %r15
movl $0xfac, %edx            # imm = 0xFAC
movq %r15, %rdi
leaq -0x12d8(%rbp), %rsi
callq 0x2130
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
leaq (%rax,%rbx), %rdi
leaq (%rbx,%rbp), %rsi
addq $-0xde38, %rsi          # imm = 0xFFFF21C8
callq 0x35ac
addq $0x18, %rbx
cmpq $0x5dd8, %rbx           # imm = 0x5DD8
jne 0x2bef
movq -0x30(%rbp), %rdi
addq $0x5dd8, %rdi           # imm = 0x5DD8
movl $0xfac, %edx            # imm = 0xFAC
movq %r15, %rsi
callq 0x2130
movl $0x5dd8, %ebx           # imm = 0x5DD8
movq -0xde50(%rbp,%rbx), %rdi
testq %rdi, %rdi
je 0x2c4e
movq -0xde40(%rbp,%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
addq $-0x18, %rbx
jne 0x2c31
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x2c76
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x328(%rbp), %rdi
callq 0x2060
movl $0x5dd8, %ebx           # imm = 0x5DD8
movq -0x30(%rbp), %rax
movq -0x70c8(%rbp,%rbx), %rdi
testq %rdi, %rdi
je 0x2cac
movq -0x70b8(%rbp,%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0x30(%rbp), %rax
addq $-0x18, %rbx
jne 0x2c8b
addq $0xde18, %rsp           # imm = 0xDE18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13b2(%rip), %rdi      # 0x407d
callq 0x21b0
leaq 0x13a6(%rip), %rdi      # 0x407d
movq -0x38(%rbp), %r15
callq 0x20e0
leaq 0x1396(%rip), %rdi      # 0x407d
movq -0x38(%rbp), %r15
callq 0x20e0
leaq 0x1386(%rip), %rdi      # 0x407d
callq 0x21b0
leaq 0x137a(%rip), %rdi      # 0x407d
callq 0x20e0
leaq 0x136e(%rip), %rdi      # 0x407d
callq 0x21b0
leaq 0x1362(%rip), %rdi      # 0x407d
callq 0x20e0
leaq 0x1356(%rip), %rdi      # 0x407d
callq 0x21b0
leaq 0x134a(%rip), %rdi      # 0x407d
callq 0x20e0
leaq 0x133e(%rip), %rdi      # 0x407d
callq 0x21b0
movq %rax, %r14
jmp 0x2e70
jmp 0x2dc1
movq %rax, %r14
jmp 0x2e36
movq %rax, %r14
movq -0x38(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x2e36
movq -0x38(%rbp), %rax
movl %ebx, (%rax)
jmp 0x2e36
jmp 0x2d73
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x2e42
movl %ebx, (%r12)
jmp 0x2e42
movq %rax, %r14
leaq -0xde38(%rbp), %rsi
movq %r15, %rdi
callq 0x2280
jmp 0x2e42
movq %rax, %r14
testq %rbx, %rbx
je 0x2e42
leaq -0xde38(%rbp), %rsi
movq %r15, %rdi
callq 0x2250
jmp 0x2e42
movq %rax, %r14
jmp 0x2e42
movq %rax, %r14
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x2e2a
movq -0x48(%rbp), %rsi
jmp 0x2e22
movq %rax, %r14
jmp 0x2e11
movq %rax, %r14
jmp 0x2e05
movq %rax, %r14
jmp 0x2e2a
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x2e05
movl %r13d, (%r15)
jmp 0x2e05
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x2e05
movl %ebx, (%r15)
leaq -0xf0(%rbp), %rdi
callq 0x32fe
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x2e2a
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x108(%rbp), %rdi
callq 0x32fe
leaq -0x120(%rbp), %rdi
callq 0x32fe
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x2e64
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x328(%rbp), %rdi
callq 0x2060
movl $0x5dd8, %ebx           # imm = 0x5DD8
movq -0x30(%rbp), %rax
movq -0x18(%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x2e94
movq -0x8(%rax,%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0x30(%rbp), %rax
addq $-0x18, %rbx
jne 0x2e79
movl $0x5dd8, %ebx           # imm = 0x5DD8
movq -0x70c8(%rbp,%rbx), %rdi
testq %rdi, %rdi
je 0x2ebc
movq -0x70b8(%rbp,%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
addq $-0x18, %rbx
jne 0x2e9f
movq %r14, %rdi
callq 0x2210