// File: 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
subq $0xde80, %rsp           # imm = 0xDE80
movq %rsi, -0xde48(%rbp)
movq %rdi, -0xde38(%rbp)
movq %rdi, %rax
movq %rax, -0xde40(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x6d98(%rbp), %rdi
callq 0x3830
movq -0xde38(%rbp), %rdi
movb $0x0, -0x6d99(%rbp)
callq 0x3870
jmp 0x259c
movq -0xde48(%rbp), %rsi
leaq -0x6fb8(%rbp), %rdi
movl $0x8, %edx
callq 0x2250
jmp 0x25b6
leaq -0x6fd8(%rbp), %rdi
callq 0x2200
movl $0x0, -0x6fdc(%rbp)
leaq -0x6fb8(%rbp), %rdi
leaq -0x6fd8(%rbp), %rsi
callq 0x2050
movq %rax, -0xde50(%rbp)
jmp 0x25e8
movq -0xde50(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x21e0
movb %al, -0xde51(%rbp)
jmp 0x2606
movb -0xde51(%rbp), %al
testb $0x1, %al
jne 0x2615
jmp 0x2a0b
cmpl $0x0, -0x6fdc(%rbp)
jne 0x2699
xorl %eax, %eax
movl %eax, %esi
leaq -0x6fd8(%rbp), %rdi
movl $0xa, %edx
callq 0x38b0
movl %eax, -0xde58(%rbp)
jmp 0x263b
movl -0xde58(%rbp), %esi
movq -0xde38(%rbp), %rdi
callq 0x3890
jmp 0x264f
jmp 0x29f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x2ae9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x2add
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x2a9b
cmpl $0x1, -0x6fdc(%rbp)
jne 0x26d8
xorl %eax, %eax
movl %eax, %esi
leaq -0x6fd8(%rbp), %rdi
movl $0xa, %edx
callq 0x38b0
movl %eax, -0xde5c(%rbp)
jmp 0x26bf
movl -0xde5c(%rbp), %esi
movq -0xde38(%rbp), %rdi
callq 0x38f0
jmp 0x26d3
jmp 0x29f5
leaq -0x7000(%rbp), %rdi
leaq -0x6fd8(%rbp), %rsi
movl $0x3a, %edx
callq 0x23f0
jmp 0x26f2
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0xde68(%rbp)
leaq -0x7000(%rbp), %rdi
callq 0x3910
movq -0xde68(%rbp), %rsi
movq %rax, %rdi
movl $0xa, %edx
callq 0x38b0
movl %eax, -0xde60(%rbp)
jmp 0x2725
movl -0xde60(%rbp), %eax
movl %eax, -0x7004(%rbp)
movl -0x6fe0(%rbp), %eax
incl %eax
movl %eax, -0x6fe0(%rbp)
leaq -0x7000(%rbp), %rdi
movl $0x1, %esi
callq 0x3910
movq %rax, %rsi
leaq -0x7020(%rbp), %rdi
movl $0x7c, %edx
callq 0x23f0
jmp 0x2766
movl $0x0, -0x7024(%rbp)
leaq -0x7020(%rbp), %rax
movq %rax, -0x7030(%rbp)
movq -0x7030(%rbp), %rdi
callq 0x3930
movq %rax, -0x7038(%rbp)
movq -0x7030(%rbp), %rdi
callq 0x3960
movq %rax, -0x7040(%rbp)
leaq -0x7038(%rbp), %rdi
leaq -0x7040(%rbp), %rsi
callq 0x3990
testb $0x1, %al
jne 0x27c0
jmp 0x2990
leaq -0x7038(%rbp), %rdi
callq 0x39d0
movq %rax, -0x7048(%rbp)
movq -0x7048(%rbp), %rsi
leaq -0x7088(%rbp), %rdi
callq 0x20c0
jmp 0x27e8
leaq -0x7068(%rbp), %rdi
leaq -0x7088(%rbp), %rsi
callq 0x2b10
jmp 0x27fd
leaq -0x7088(%rbp), %rdi
callq 0x2110
leaq -0x70a0(%rbp), %rdi
leaq -0x7068(%rbp), %rsi
movl $0x20, %edx
callq 0x23f0
jmp 0x2823
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0xde78(%rbp)
leaq -0x70a0(%rbp), %rdi
callq 0x3910
movq -0xde78(%rbp), %rsi
movq %rax, %rdi
movl $0xa, %edx
callq 0x38b0
movl %eax, -0xde6c(%rbp)
jmp 0x2856
movl -0xde6c(%rbp), %eax
movl %eax, -0x70a4(%rbp)
leaq -0x70a0(%rbp), %rdi
movl $0x1, %esi
callq 0x3910
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x38b0
movl %eax, -0xde7c(%rbp)
jmp 0x288c
movl -0xde7c(%rbp), %eax
movl %eax, -0x70a8(%rbp)
movl -0x7004(%rbp), %esi
movl -0x70a4(%rbp), %edx
movl -0x70a8(%rbp), %ecx
leaq -0x6d98(%rbp), %rdi
callq 0x39f0
jmp 0x28b8
movl -0x6fe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6fe4(%rbp)
movl -0x7024(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7024(%rbp)
leaq -0x70a0(%rbp), %rdi
callq 0x37d0
leaq -0x7068(%rbp), %rdi
callq 0x2110
leaq -0x7038(%rbp), %rdi
callq 0x3a50
jmp 0x27a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x29e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x29d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
leaq -0x7088(%rbp), %rdi
callq 0x2110
jmp 0x29d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
jmp 0x2982
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
leaq -0x70a0(%rbp), %rdi
callq 0x37d0
leaq -0x7068(%rbp), %rdi
callq 0x2110
jmp 0x29d8
movl -0x7024(%rbp), %eax
movl %eax, -0xde80(%rbp)
leaq -0x6d98(%rbp), %rdi
addq $0x5dd8, %rdi           # imm = 0x5DD8
movslq -0x7004(%rbp), %rsi
callq 0x3a70
movl -0xde80(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x7020(%rbp), %rdi
callq 0x37d0
leaq -0x7000(%rbp), %rdi
callq 0x37d0
jmp 0x29f5
leaq -0x7020(%rbp), %rdi
callq 0x37d0
leaq -0x7000(%rbp), %rdi
callq 0x37d0
jmp 0x2a9b
jmp 0x29f7
movl -0x6fdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6fdc(%rbp)
jmp 0x25cc
movl -0x6fe0(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x6fe4(%rbp), %eax
movl %eax, -0x18(%rbp)
leaq -0xde30(%rbp), %rdi
leaq -0x6d98(%rbp), %rsi
callq 0x3ac0
jmp 0x2a32
movq -0xde38(%rbp), %rdi
leaq -0xde30(%rbp), %rsi
callq 0x3aa0
jmp 0x2a47
leaq -0xde30(%rbp), %rdi
callq 0x3b10
movb $0x1, -0x6d99(%rbp)
leaq -0x6fd8(%rbp), %rdi
callq 0x2110
leaq -0x6fb8(%rbp), %rdi
callq 0x2070
testb $0x1, -0x6d99(%rbp)
jne 0x2ac1
jmp 0x2ab5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x6da8(%rbp)
movl %eax, -0x6dac(%rbp)
leaq -0xde30(%rbp), %rdi
callq 0x3b10
leaq -0x6fd8(%rbp), %rdi
callq 0x2110
leaq -0x6fb8(%rbp), %rdi
callq 0x2070
jmp 0x2add
movq -0xde38(%rbp), %rdi
callq 0x3b30
leaq -0x6d98(%rbp), %rdi
callq 0x3b10
movq -0xde40(%rbp), %rax
addq $0xde80, %rsp           # imm = 0xDE80
popq %rbp
retq
movq -0xde38(%rbp), %rdi
callq 0x3b30
leaq -0x6d98(%rbp), %rdi
callq 0x3b10
movq -0x6da8(%rbp), %rdi
callq 0x2280
nopw %cs:(%rax,%rax)