// File: /workspace/github_cpp_makefile_star2_O3/gibsjose[P]VirtualMemorySimulation/Simulation.cpp
// Function: Simulation::Run()
void Simulation::Run(void) {
    //Read in the input file
    std::ifstream file(filepath);
    std::string line;
    std::vector<std::string> values;
    std::string name;
    std::string pageString;

    unsigned long lineNumber = 0;
    uint16_t frame;

    if(!file.is_open()) {
        std::cerr << "File \"" << filepath << "\" could not be opened. Check to see if it exists" << std::endl;
        exit(-1);
    }

    //Iterate over each line of the file
    while(std::getline(file, line)) {

        //Skip blank lines
        if(line.empty()) {
            continue;
        }

        values = StringUtilities::SplitString(line, ":\t");

        if(values.size() < 2) {
            std::cerr << "Error: Line " << lineNumber << ": \"" << line << "\" does not follow format of \"{NAME}:<TAB>{Page #}\"" << std::endl;
            exit(-1);
        }

        name = values.at(0);
        pageString = values.at(1);

        //New process: Add it to the map
        if(!processes.count(name)) {
            Process process(name);
            processes[name] = process;

            std::cout << std::endl << Color::Green("* [NEW PROCESS] ") << name << std::endl;
        }

        //Get the process object
        Process & process = processes.find(name)->second;
        uint16_t page = process.PageAccess(pageString);

        //Reference the process page in memory
        // This could result in one of three things:
        // 1. There is already a frame associated with the page number, and it is already in memory
        // 2. There is already a frame associated with the page number, and it is on the free frame list
        // 3. There is no frame associated with this page number --> Page fault: Assign it a page number and bring frame into
        //     memory
        memory.Reference(process, page);

        //Increment line number
        lineNumber++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq %rdi, %rbx
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x22b0
leaq 0x78(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x30(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xd8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x218(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x312e
leaq 0x20(%rbx), %r12
leaq 0x28(%rbx), %rax
movq %rax, 0xe8(%rsp)
addq $0x50, %rbx
movq $0x0, 0x60(%rsp)
leaq 0x68(%rsp), %r13
movq 0x1a0(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2310
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x30aa
movq 0x70(%rsp), %rdx
testq %rdx, %rdx
je 0x2ceb
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x68(%rsp), %rsi
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x4d1e
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x108(%rsp), %rbp
movq %rbp, %rdi
leaq 0x535d(%rip), %rsi      # 0x80d2
leaq 0x5358(%rip), %rdx      # 0x80d4
callq 0x43f0
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
leaq 0x88(%rsp), %rsi
movq %rbp, %rdx
callq 0x3520
movq 0x10(%rsp), %rax
movaps 0xf0(%rsp), %xmm0
movq 0x100(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movaps (%rsp), %xmm1
movaps %xmm0, (%rsp)
movaps %xmm1, 0x40(%rsp)
movq %rax, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x35fe
movq %r15, %rdi
callq 0x35fe
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2e16
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2e3b
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21d0
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
cmpq $0x20, %rax
jbe 0x3182
leaq 0x20(%rsp), %rdi
callq 0x20f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
subq %rsi, %rdx
sarq $0x5, %rdx
cmpq $0x1, %rdx
jbe 0x31f5
addq $0x20, %rsi
leaq 0xc8(%rsp), %rdi
callq 0x20f0
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x51f0
cmpq 0xe8(%rsp), %rax
jne 0x306e
leaq 0x108(%rsp), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x6762
movq %r12, %rdi
movq %r15, %rsi
callq 0x365c
movq %rax, %rbp
movq %rax, %rdi
leaq 0x108(%rsp), %rsi
callq 0x20f0
leaq 0x118(%rsp), %rcx
movzwl 0x14(%rcx), %eax
movw %ax, 0x24(%rbp)
movl 0x10(%rcx), %eax
movl %eax, 0x20(%rbp)
leaq 0x108(%rsp), %rax
cmpq %rbp, %rax
je 0x2f08
leaq 0x28(%rbp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x4632
leaq 0x118(%rsp), %rcx
movb 0x54(%rcx), %al
movb %al, 0x64(%rbp)
movl 0x50(%rcx), %eax
movl %eax, 0x60(%rbp)
addq $0x68, %rbp
movq %rbp, %rdi
leaq 0x170(%rsp), %rsi
callq 0x49b4
movq 0x9091(%rip), %rcx      # 0xbfc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x9073(%rip), %rdi      # 0xbfc8
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq %rax, %rbp
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
leaq 0x518e(%rip), %rsi      # 0x8115
leaq 0x5197(%rip), %rdx      # 0x8125
callq 0x43f0
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x36d8
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rbp, %rdi
callq 0x2200
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rax, %rdi
callq 0x2200
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x300a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x302f
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x170(%rsp), %rdi
callq 0x3d8e
leaq 0x130(%rsp), %rdi
callq 0x4cf8
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x306e
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5622
movq %rax, %rbp
addq $0x40, %rbp
movq %rbp, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x680e
movzwl %ax, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5668
incq 0x60(%rsp)
jmp 0x2ceb
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x30cf
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x30eb
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rsp, %rdi
callq 0x35fe
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x310f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x1a0(%rsp), %rdi
callq 0x2080
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ebb(%rip), %rdi      # 0xbff0
leaq 0x4f5e(%rip), %rsi      # 0x809a
movl $0x6, %edx
callq 0x2200
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8e9c(%rip), %rdi      # 0xbff0
callq 0x2200
movq %rax, %rbx
leaq 0x4f3e(%rip), %rsi      # 0x80a1
movl $0x30, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20d0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2230
movq 0x8e67(%rip), %rdi      # 0xbff0
leaq 0x4f45(%rip), %rsi      # 0x80d5
movl $0xc, %edx
callq 0x2200
movq 0x8e4f(%rip), %rdi      # 0xbff0
movq 0x60(%rsp), %rsi
callq 0x2130
movq %rax, %rbx
leaq 0x514a(%rip), %rsi      # 0x82ff
movl $0x3, %edx
movq %rax, %rdi
callq 0x2200
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq %rbx, %rdi
callq 0x2200
movq %rax, %rbx
leaq 0x4f04(%rip), %rsi      # 0x80e2
movl $0x32, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20d0
jmp 0x3178
leaq 0x4f7c(%rip), %rdi      # 0x8178
movl $0x1, %esi
xorl %eax, %eax
callq 0x2280
jmp 0x32d0
movq %rax, %rbx
jmp 0x3291
jmp 0x32ba
movq %rax, %rbx
jmp 0x3249
jmp 0x32d0
movq %rax, %rbx
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x3249
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x32d3
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x32d3
jmp 0x32d0
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3291
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x32bd
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x32bd
jmp 0x32d0
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x37ec
jmp 0x32d3
jmp 0x32d0
jmp 0x32d0
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x32f8
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3314
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rsp, %rdi
callq 0x35fe
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x3338
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x1a0(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x22f0
nop