// File: /workspace/github_cpp_makefile_star2_O1/yiboyang[P]VirtualMemory/main.cpp
// Function: main
int main(int argc, char** argv) {

	if (!((argc == 3) && (strcmp(argv[1], "-F") == 0 || strcmp(argv[1], "-L") == 0))) {
		cerr << "Usage: ./a.out [-F|-L] [INPUT FILE]" << endl
			<< "\t-F, use FIFO page replacement policy" << endl
			<< "\t-L, use LRU page replacment policy" << endl;
		return -1;
	}

	// create page table
	PageTable T(strcmp(argv[1], "-F") == 0 ? "FIFO" : "LRU");

	// open input file
	ifstream input(argv[2]);
	if (!input) {
		cerr << "Unable to open input file\n";
		return -1;
	}



	// input line looks like
	// R(ead) Address, or W(rite) Address Argument, separated by spaces. e.g.
	// R 245
	// W 5421 132
	string line, op;
	int addr, arg, pnum, offset, fnum, memval;
	vector<string> fields;
	while (getline(input, line)) {
		istringstream iss(line);
		for (string s; iss >> s; )
			fields.push_back(s);
		op = fields[0];
		addr = stoi(fields[1]);

		pnum = addr >> FRAME_SIZE_BITS;	// page number
		offset = addr & FRAME_OFFSET_MASK;	// page offset

		// access the page and get its frame number
		fnum = T[pnum];

		if (op == "R") {
		}
		else if (op == "W") {
			T.setDirty(pnum, true);
			arg = stoi(fields[2]);
			Memory[fnum][offset] = arg;
		}
		else
			cerr << "Bad operation: " << op << endl;

		memval = Memory[fnum][offset];

		// output format: page #, offset, TLB hit, page fault, physical address, value (only for reads)
		cout << pnum << ' ' << offset << ' ' << (!T.tlbMiss ? 'H' : 'N') << ' '
			<< (T.pageFault ? 'F' : 'N') << ' ' << ((fnum << FRAME_SIZE_BITS) | offset);
		// print memory content for reads
		if (op == "R")
			cout << ' ' << memval;
		cout << endl;

		fields.clear();
	}
	cout << endl << "Page Table:" << endl << T << endl;
	cout << endl << T.numPageFaults << " Page Faults" << endl;
	input.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x818, %rsp            # imm = 0x818
cmpl $0x3, %edi
jne 0x3aa2
movq %rsi, %rbx
movq 0x8(%rsi), %r14
leaq 0x166e(%rip), %rsi      # 0x50e9
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0x3b6a
leaq 0x165a(%rip), %rsi      # 0x50ec
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0x3b6a
movq 0x453f(%rip), %rbx      # 0x7fe8
leaq 0x163f(%rip), %rsi      # 0x50ef
movl $0x23, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %rax, %rbx
leaq 0x1625(%rip), %rsi      # 0x5113
movl $0x25, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %rax, %rbx
leaq 0x160d(%rip), %rsi      # 0x5139
movl $0x23, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x420c
leaq 0x1578(%rip), %rsi      # 0x50e9
movq %r14, %rdi
callq 0x22d0
xorl %ecx, %ecx
testl %eax, %eax
leaq 0x1481(%rip), %rax      # 0x5005
leaq 0x14a7(%rip), %rsi      # 0x5032
cmoveq %rax, %rsi
sete %cl
leaq -0x430(%rbp), %r15
movq %r15, -0x10(%r15)
leaq (%rcx,%rsi), %rdx
addq $0x3, %rdx
leaq -0x440(%rbp), %rdi
callq 0x43ea
leaq -0x228(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1599(%rip), %rsi      # 0x515d
leaq 0x15a3(%rip), %rdx      # 0x516e
leaq -0x238(%rbp), %rdi
callq 0x43ea
leaq -0x840(%rbp), %rdi
leaq -0x440(%rbp), %rsi
leaq -0x238(%rbp), %rdx
callq 0x29e0
movq -0x238(%rbp), %rdi
cmpq %r12, %rdi
je 0x3c02
callq 0x2200
movq -0x440(%rbp), %rdi
cmpq %r15, %rdi
je 0x3c13
callq 0x2200
movq 0x10(%rbx), %rsi
leaq -0x440(%rbp), %rbx
movq %rbx, %rdi
movl $0x8, %edx
callq 0x23b0
movq -0x440(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x420(%rbp,%rax)
je 0x3c62
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x439c(%rip), %rdi      # 0x7fe8
leaq 0x151c(%rip), %rsi      # 0x516f
movl $0x1a, %edx
callq 0x2240
jmp 0x41f4
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x80(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x70(%rbp), %r14
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
leaq -0x50(%rbp), %r12
movq -0x440(%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %edx
movq %rbx, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x23c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4090
leaq -0x238(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
movl $0x8, %edx
callq 0x2360
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
leaq -0x238(%rbp), %rdi
movq %r12, %rsi
callq 0x2280
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3d28
movq %r14, %rdi
movq %r12, %rsi
callq 0x4350
jmp 0x3cfe
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x3d3a
callq 0x2200
movq -0x70(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x2150
movq -0x70(%rbp), %rax
movq 0x20(%rax), %r14
callq 0x2040
movq %rax, %rbx
movl (%rax), %r13d
movl $0x0, (%rax)
movq %r14, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x22e0
cmpq %r14, -0x50(%rbp)
je 0x422c
movq %rax, %r15
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x4220
movl (%rbx), %eax
cmpl $0x22, %eax
je 0x4220
testl %eax, %eax
jne 0x3dab
movl %r13d, (%rbx)
movl %r15d, %r14d
sarl $0x8, %r14d
leaq -0x840(%rbp), %rdi
movl %r14d, %esi
callq 0x2d76
movl %eax, -0x30(%rbp)
leaq -0x90(%rbp), %rdi
leaq 0x13b8(%rip), %rsi      # 0x518a
callq 0x20c0
movslq -0x30(%rbp), %r13
testl %eax, %eax
je 0x3eed
leaq -0x90(%rbp), %rdi
leaq 0x139b(%rip), %rsi      # 0x518c
callq 0x20c0
testl %eax, %eax
je 0x3e5b
movl $0xf, %edx
movq 0x41e2(%rip), %rdi      # 0x7fe8
leaq 0x1381(%rip), %rsi      # 0x518e
callq 0x2240
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq 0x41c1(%rip), %rdi      # 0x7fe8
callq 0x2240
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
jmp 0x3eed
movl %r14d, -0x50(%rbp)
leaq -0x840(%rbp), %rdi
movq %r12, %rsi
callq 0x3106
movb $0x1, 0x4(%rax)
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rdi
movq %rdi, -0x98(%rbp)
movl (%rbx), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, (%rbx)
movq %r12, %rsi
movl $0xa, %edx
callq 0x22e0
movq -0x98(%rbp), %rcx
cmpq %rcx, -0x50(%rbp)
je 0x4238
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x4244
movl (%rbx), %ecx
cmpl $0x22, %ecx
je 0x4244
testl %ecx, %ecx
jne 0x3ed8
movl -0x2c(%rbp), %ecx
movl %ecx, (%rbx)
movzbl %r15b, %ecx
movq %r13, %rdx
shlq $0x8, %rdx
addq 0x40a6(%rip), %rdx      # 0x7f90
movb %al, (%rcx,%rdx)
movzbl %r15b, %eax
shlq $0x8, %r13
addq 0x4094(%rip), %r13      # 0x7f90
movzbl (%rax,%r13), %r13d
movq 0x40b0(%rip), %rdi      # 0x7fb8
movl %r14d, %esi
callq 0x2380
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movzbl %r15b, %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
cmpb $0x0, -0x448(%rbp)
movl $0x48, %ecx
movl $0x4e, %edx
cmovnel %edx, %ecx
movb %cl, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movb -0x450(%rbp), %cl
xorb $0x1, %cl
shlb $0x3, %cl
orb $0x46, %cl
movb %cl, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movl -0x30(%rbp), %esi
shll $0x8, %esi
orl %ebx, %esi
movq %rax, %rdi
callq 0x2380
leaq -0x90(%rbp), %rdi
leaq 0x11b5(%rip), %rsi      # 0x518a
callq 0x20c0
testl %eax, %eax
jne 0x4001
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq 0x3fca(%rip), %rdi      # 0x7fb8
movq %r12, %rsi
callq 0x2240
movq %rax, %rdi
movl %r13d, %esi
callq 0x2380
movq 0x3fb0(%rip), %rcx      # 0x7fb8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3f92(%rip), %rdi      # 0x7fb8
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq -0x70(%rbp), %rbx
movq -0x68(%rbp), %r14
cmpq %rbx, %r14
je 0x4061
movq %rbx, %r15
movq (%r15), %rdi
addq $0x10, %r15
cmpq %r15, %rdi
je 0x4054
callq 0x2200
addq $0x10, %r15
cmpq %r14, %r15
jne 0x4043
movq %rbx, -0x68(%rbp)
leaq -0x238(%rbp), %rdi
movq 0x3f39(%rip), %rsi      # 0x7fa8
callq 0x22f0
leaq -0x1c0(%rbp), %rdi
callq 0x20d0
leaq -0x440(%rbp), %rbx
leaq -0x70(%rbp), %r14
jmp 0x3c96
movq 0x3f21(%rip), %rdi      # 0x7fb8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3f06(%rip), %rbx      # 0x7fb8
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %rax, %r14
leaq 0x10d2(%rip), %rsi      # 0x519e
movl $0xb, %edx
movq %rax, %rdi
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq -0x840(%rbp), %rsi
movq %rax, %rdi
callq 0x2fd1
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3e61(%rip), %rdi      # 0x7fb8
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl -0x44c(%rbp), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rbx
leaq 0x102e(%rip), %rsi      # 0x51aa
movl $0xc, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq -0x440(%rbp), %rdi
callq 0x2080
leaq -0x70(%rbp), %rdi
callq 0x439c
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x41da
callq 0x2200
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x41f2
callq 0x2200
xorl %ebx, %ebx
leaq -0x440(%rbp), %rdi
callq 0x20a0
leaq -0x840(%rbp), %rdi
callq 0x2c5c
movl %ebx, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf90(%rip), %rdi       # 0x51b7
callq 0x2300
leaq 0xf84(%rip), %rdi       # 0x51b7
callq 0x2170
leaq 0xf78(%rip), %rdi       # 0x51b7
callq 0x2170
leaq 0xf6c(%rip), %rdi       # 0x51b7
callq 0x2300
movq %rax, %r14
jmp 0x432f
movq %rax, %r14
jmp 0x433b
movq %rax, %r14
movq -0x238(%rbp), %rdi
cmpq %r12, %rdi
je 0x4279
callq 0x2200
jmp 0x4279
movq %rax, %r14
movq -0x440(%rbp), %rdi
cmpq %r15, %rdi
je 0x4347
callq 0x2200
jmp 0x4347
movq %rax, %r14
jmp 0x4347
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x42da
movl -0x2c(%rbp), %eax
movl %eax, (%rbx)
jmp 0x42da
jmp 0x42bb
jmp 0x42bb
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x42da
movl %r13d, (%rbx)
jmp 0x42da
movq %rax, %r14
jmp 0x42f9
movq %rax, %r14
jmp 0x42da
movq %rax, %r14
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x42da
callq 0x2200
movq 0x3cc7(%rip), %rsi      # 0x7fa8
leaq -0x238(%rbp), %rdi
callq 0x22f0
leaq -0x1c0(%rbp), %rdi
callq 0x20d0
leaq -0x70(%rbp), %rdi
callq 0x439c
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x4317
callq 0x2200
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x432f
callq 0x2200
leaq -0x440(%rbp), %rdi
callq 0x20a0
leaq -0x840(%rbp), %rdi
callq 0x2c5c
movq %r14, %rdi
callq 0x2390
nop