// File: /workspace/github_cpp_makefile_star2_O2/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 $0xde38, %rsp           # imm = 0xDE38
movq %rsi, %rbx
movq %rdi, %r13
leaq -0x70d0(%rbp), %r14
movl $0x5dd8, %edx           # imm = 0x5DD8
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
andq $0x0, 0x6d7c(%r14)
movl $0x5dd8, %edx           # imm = 0x5DD8
movq %r13, %rdi
xorl %esi, %esi
callq 0x20f0
andq $0x0, 0x6d7c(%r13)
leaq -0x348(%rbp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x21f0
leaq -0x100(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
movq %r13, -0x38(%rbp)
leaq -0x348(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x28ea
cmpl $0x1, %r14d
je 0x24fa
testl %r14d, %r14d
jne 0x2517
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2c89
movl %eax, 0x6d88(%r13)
jmp 0x28e2
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2c89
movl %eax, 0x6d8c(%r13)
jmp 0x28e2
leaq -0x140(%rbp), %rdi
leaq -0x110(%rbp), %rsi
pushq $0x3a
popq %rdx
callq 0x236c
movq -0x140(%rbp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2c89
movl %eax, -0x3c(%rbp)
movq -0x140(%rbp), %rsi
addq $0x20, %rsi
leaq -0x128(%rbp), %rdi
pushq $0x7c
popq %rdx
callq 0x236c
incl -0x64(%rbp)
movq -0x128(%rbp), %rsi
movq -0x120(%rbp), %rax
movq %rax, -0x98(%rbp)
xorl %r15d, %r15d
movq %r14, -0x90(%rbp)
cmpq -0x98(%rbp), %rsi
je 0x28be
leaq -0x60(%rbp), %rdi
movq %rsi, -0xa0(%rbp)
callq 0x21a0
movl %r15d, -0x68(%rbp)
movl %ebx, -0x6c(%rbp)
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r14
leaq (%rbx,%r14), %rax
movq %rax, -0x30(%rbp)
movq %r14, %r13
sarq $0x2, %r13
movq %rbx, %r15
testq %r13, %r13
jle 0x2609
movq %r15, %rdi
callq 0x2bf6
testb %al, %al
jne 0x264b
leaq 0x1(%r15), %r12
movq %r12, %rdi
callq 0x2bf6
testb %al, %al
jne 0x264e
leaq 0x2(%r15), %r12
movq %r12, %rdi
callq 0x2bf6
testb %al, %al
jne 0x264e
leaq 0x3(%r15), %r12
movq %r12, %rdi
callq 0x2bf6
testb %al, %al
jne 0x264e
addq $0x4, %r15
decq %r13
addq $-0x4, %r14
jmp 0x25bb
cmpq $0x1, %r14
je 0x282e
cmpq $0x2, %r14
movq -0x38(%rbp), %r13
je 0x2636
cmpq $0x3, %r14
jne 0x2834
movq %r15, %rdi
callq 0x2bf6
testb %al, %al
jne 0x2646
incq %r15
movq %r15, %rdi
callq 0x2bf6
testb %al, %al
je 0x28a4
movq %r15, %r12
jmp 0x2652
movq %r15, %r12
movq -0x38(%rbp), %r13
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2060
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r14
leaq (%rbx,%r14), %rax
movq %rax, -0x30(%rbp)
movq %r14, %r12
sarq $0x2, %r12
leaq (%rbx,%r14), %r13
testq %r12, %r12
jle 0x2708
movq %r13, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x2c0a
testb %al, %al
jne 0x272e
leaq (%rbx,%r14), %r15
decq %r15
movq %r15, -0xf0(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x2c0a
testb %al, %al
jne 0x2731
leaq (%rbx,%r14), %r15
addq $-0x2, %r15
movq %r15, -0xe8(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x2c0a
testb %al, %al
jne 0x2731
leaq (%rbx,%r14), %r15
addq $-0x3, %r15
movq %r15, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x2c0a
testb %al, %al
jne 0x2731
decq %r12
addq $-0x4, %r14
jmp 0x2678
cmpq $0x3, %r14
movq -0x30(%rbp), %r12
je 0x283d
cmpq $0x2, %r14
je 0x2862
movq %rbx, %r15
cmpq $0x1, %r14
jne 0x2735
jmp 0x2883
movq %r13, %r15
movq -0x30(%rbp), %r12
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq -0x38(%rbp), %r13
callq 0x2060
leaq -0xb0(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movq -0x60(%rbp), %rax
leaq -0x50(%rbp), %rdx
cmpq %rdx, %rax
movq -0x90(%rbp), %r14
je 0x277e
movq %rax, -0xc0(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x2784
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
movq -0x58(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq %rdx, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
movb $0x0, -0x50(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x20c0
leaq -0x88(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
pushq $0x20
popq %rdx
callq 0x236c
movq -0x88(%rbp), %rdi
xorl %esi, %esi
pushq $0xa
popq %r15
movl %r15d, %edx
callq 0x2c89
movl %eax, %ebx
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x2c89
leaq -0x70d0(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl %ebx, %edx
movl %eax, %ecx
callq 0x2caa
movl -0x6c(%rbp), %ebx
incl %ebx
movl -0x68(%rbp), %r15d
incl %r15d
leaq -0x88(%rbp), %rdi
callq 0x2c5e
leaq -0xc0(%rbp), %rdi
callq 0x20c0
movq -0xa0(%rbp), %rsi
addq $0x20, %rsi
jmp 0x257d
movq -0x38(%rbp), %r13
jmp 0x28a7
movq -0x30(%rbp), %r12
jmp 0x2652
movq %r13, -0xd8(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2c0a
movq %r13, %r15
testb %al, %al
jne 0x2735
leaq (%rbx,%r14), %r13
decq %r13
movq %r13, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2c0a
movq %r13, %r15
testb %al, %al
jne 0x2735
decq %r13
movq %r13, -0xc8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x2c0a
testb %al, %al
cmovneq %r13, %rbx
movq %rbx, %r15
jmp 0x2735
incq %r15
movq %r15, %rdi
callq 0x2bf6
testb %al, %al
movq -0x30(%rbp), %r12
cmovneq %r15, %r12
jmp 0x2652
movslq -0x3c(%rbp), %rax
movl %r15d, -0x12f8(%rbp,%rax,4)
leaq -0x128(%rbp), %rdi
callq 0x2c5e
leaq -0x140(%rbp), %rdi
callq 0x2c5e
incl %r14d
jmp 0x24ad
leaq -0x70d0(%rbp), %rsi
movl -0x64(%rbp), %eax
movl %eax, 0x6d7c(%rsi)
movl %ebx, 0x6d80(%rsi)
leaq -0xde58(%rbp), %rdi
callq 0x2cda
leaq -0xde58(%rbp), %rsi
movq %r13, %rdi
callq 0x301c
leaq -0xde58(%rbp), %rdi
callq 0x32de
leaq -0x110(%rbp), %rdi
callq 0x20c0
leaq -0x348(%rbp), %rdi
callq 0x2070
leaq -0x70d0(%rbp), %rdi
callq 0x32de
movq %r13, %rax
addq $0xde38, %rsp           # imm = 0xDE38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0xde58(%rbp), %rdi
callq 0x32de
jmp 0x29ce
jmp 0x2981
movq %rax, %rbx
jmp 0x29e6
jmp 0x297a
movq %rax, %rbx
jmp 0x29c2
jmp 0x2981
movq %rax, %rbx
jmp 0x29ce
jmp 0x2992
movq %rax, %rbx
jmp 0x29a1
movq %rax, %rbx
jmp 0x29b6
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2c5e
leaq -0xc0(%rbp), %rdi
jmp 0x29b1
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x20c0
leaq -0x128(%rbp), %rdi
callq 0x2c5e
leaq -0x140(%rbp), %rdi
callq 0x2c5e
leaq -0x110(%rbp), %rdi
callq 0x20c0
leaq -0x348(%rbp), %rdi
callq 0x2070
movq %r13, %rdi
callq 0x32de
leaq -0x70d0(%rbp), %rdi
callq 0x32de
movq %rbx, %rdi
callq 0x2210