// File: /workspace/github_cpp_makefile_star2_O3/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 0x3a59
movq %rsi, %rbx
movq 0x8(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x3a59
cmpb $0x46, 0x1(%rax)
jne 0x3a49
cmpb $0x0, 0x2(%rax)
je 0x3b30
cmpb $0x4c, 0x1(%rax)
jne 0x3a59
cmpb $0x0, 0x2(%rax)
je 0x3b30
movq 0x4588(%rip), %rbx      # 0x7fe8
leaq 0x1682(%rip), %rsi      # 0x50e9
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 0x1668(%rip), %rsi      # 0x510d
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 0x1650(%rip), %rsi      # 0x5133
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
movl %ebx, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x46, 0x1(%rax)
jne 0x3b62
xorl %edx, %edx
cmpb $0x0, 0x2(%rax)
sete %dl
leaq -0x430(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x14b4(%rip), %rax      # 0x5005
leaq 0x14da(%rip), %rsi      # 0x5032
cmoveq %rax, %rsi
addq $0x3, %rdx
jmp 0x3b79
leaq -0x430(%rbp), %r14
movq %r14, -0x10(%r14)
movl $0x3, %edx
leaq 0x14b9(%rip), %rsi      # 0x5032
addq %rsi, %rdx
leaq -0x440(%rbp), %rdi
callq 0x43c0
leaq -0x228(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x15bd(%rip), %rsi      # 0x5157
leaq 0x15c7(%rip), %rdx      # 0x5168
leaq -0x238(%rbp), %rdi
callq 0x43c0
leaq -0x840(%rbp), %rdi
leaq -0x440(%rbp), %rsi
leaq -0x238(%rbp), %rdx
callq 0x29b4
movq -0x238(%rbp), %rdi
cmpq %r15, %rdi
je 0x3bd8
callq 0x2200
movq -0x440(%rbp), %rdi
cmpq %r14, %rdi
je 0x3be9
callq 0x2200
movq 0x10(%rbx), %rsi
leaq -0x440(%rbp), %rbx
movq %rbx, %rdi
movl $0x8, %edx
callq 0x23a0
movq -0x440(%rbp), %rax
movq -0x18(%rax), %rcx
testb $0x5, -0x420(%rbp,%rcx)
je 0x3c38
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x43c6(%rip), %rdi      # 0x7fe8
leaq 0x1540(%rip), %rsi      # 0x5169
movl $0x1a, %edx
callq 0x2240
jmp 0x41cc
leaq -0xa8(%rbp), %rdx
movq %rdx, -0x10(%rdx)
xorl %ecx, %ecx
movq %rcx, -0x8(%rdx)
movb %cl, (%rdx)
leaq -0x80(%rbp), %rdx
movq %rdx, -0x10(%rdx)
movq %rcx, -0x8(%rdx)
movb %cl, (%rdx)
leaq -0x70(%rbp), %r14
movq %rcx, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
leaq -0x50(%rbp), %r12
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %edx
movq %rbx, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x23b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4068
leaq -0x238(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
movl $0x8, %edx
callq 0x2350
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 0x3cf7
movq %r14, %rdi
movq %r12, %rsi
callq 0x431c
jmp 0x3ccd
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x3d09
callq 0x2200
movq -0x70(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x2150
movq -0x70(%rbp), %rax
movq 0x20(%rax), %r15
callq 0x2040
movq %rax, %r14
movl (%rax), %r13d
movl $0x0, (%rax)
movq %r15, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x22d0
cmpq %r15, -0x50(%rbp)
je 0x41f5
movq %rax, %rbx
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x41e9
movl (%r14), %eax
cmpl $0x22, %eax
je 0x41e9
testl %eax, %eax
jne 0x3d7b
movl %r13d, (%r14)
movl %ebx, %r15d
sarl $0x8, %r15d
leaq -0x840(%rbp), %rdi
movl %r15d, %esi
callq 0x2d4a
movl %eax, -0x30(%rbp)
leaq -0x90(%rbp), %rdi
leaq 0x13e2(%rip), %rsi      # 0x5184
callq 0x20c0
movslq -0x30(%rbp), %r13
testl %eax, %eax
je 0x3ec0
leaq -0x90(%rbp), %rdi
leaq 0x13c5(%rip), %rsi      # 0x5186
callq 0x20c0
testl %eax, %eax
je 0x3e2b
movl $0xf, %edx
movq 0x4212(%rip), %rdi      # 0x7fe8
leaq 0x13ab(%rip), %rsi      # 0x5188
callq 0x2240
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq 0x41f1(%rip), %rdi      # 0x7fe8
callq 0x2240
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
jmp 0x3ec0
movl %r15d, -0x50(%rbp)
leaq -0x840(%rbp), %rdi
movq %r12, %rsi
callq 0x30d6
movb $0x1, 0x4(%rax)
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rdi
movq %rdi, -0x98(%rbp)
movl (%r14), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, (%r14)
movq %r12, %rsi
movl $0xa, %edx
callq 0x22d0
movq -0x98(%rbp), %rcx
cmpq %rcx, -0x50(%rbp)
je 0x4201
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x420d
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x420d
testl %ecx, %ecx
jne 0x3eac
movl -0x2c(%rbp), %ecx
movl %ecx, (%r14)
movzbl %bl, %ecx
movq %r13, %rdx
shlq $0x8, %rdx
addq 0x40d3(%rip), %rdx      # 0x7f90
movb %al, (%rcx,%rdx)
movzbl %bl, %eax
shlq $0x8, %r13
addq 0x40c2(%rip), %r13      # 0x7f90
movzbl (%rax,%r13), %r14d
movq 0x40de(%rip), %rdi      # 0x7fb8
movl %r15d, %esi
callq 0x2370
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2240
movzbl %bl, %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x2370
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 0x2370
leaq -0x90(%rbp), %rdi
leaq 0x11de(%rip), %rsi      # 0x5184
callq 0x20c0
testl %eax, %eax
jne 0x3fd2
movb $0x20, -0x50(%rbp)
movl $0x1, %edx
movq 0x3ff9(%rip), %rdi      # 0x7fb8
movq %r12, %rsi
callq 0x2240
movq %rax, %rdi
movl %r14d, %esi
callq 0x2370
movq 0x3fdf(%rip), %rcx      # 0x7fb8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3fc1(%rip), %rdi      # 0x7fb8
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq -0x70(%rbp), %rbx
movq -0x68(%rbp), %r14
cmpq %rbx, %r14
je 0x4032
movq %rbx, %r15
movq (%r15), %rdi
addq $0x10, %r15
cmpq %r15, %rdi
je 0x4025
callq 0x2200
addq $0x10, %r15
cmpq %r14, %r15
jne 0x4014
movq %rbx, -0x68(%rbp)
leaq -0x238(%rbp), %rdi
movq 0x3f68(%rip), %rsi      # 0x7fa8
callq 0x22e0
leaq -0x1c0(%rbp), %rdi
callq 0x20d0
movq -0x440(%rbp), %rax
leaq -0x440(%rbp), %rbx
leaq -0x70(%rbp), %r14
jmp 0x3c6c
movq 0x3f49(%rip), %rdi      # 0x7fb8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x3f2e(%rip), %rbx      # 0x7fb8
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %rax, %r14
leaq 0x10f4(%rip), %rsi      # 0x5198
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 0x2fa2
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 0x3e89(%rip), %rdi      # 0x7fb8
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl -0x44c(%rbp), %esi
movq %rax, %rdi
callq 0x2370
movq %rax, %rbx
leaq 0x1050(%rip), %rsi      # 0x51a4
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 0x4368
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x41b2
callq 0x2200
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x41ca
callq 0x2200
xorl %ebx, %ebx
leaq -0x440(%rbp), %rdi
callq 0x20a0
leaq -0x840(%rbp), %rdi
callq 0x2c30
jmp 0x3b1c
leaq 0xfc1(%rip), %rdi       # 0x51b1
callq 0x22f0
leaq 0xfb5(%rip), %rdi       # 0x51b1
callq 0x2170
leaq 0xfa9(%rip), %rdi       # 0x51b1
callq 0x2170
leaq 0xf9d(%rip), %rdi       # 0x51b1
callq 0x22f0
movq %rax, %rbx
jmp 0x42fb
movq %rax, %rbx
jmp 0x4307
movq %rax, %rbx
movq -0x238(%rbp), %rdi
cmpq %r15, %rdi
je 0x4242
callq 0x2200
jmp 0x4242
movq %rax, %rbx
movq -0x440(%rbp), %rdi
cmpq %r14, %rdi
je 0x4313
callq 0x2200
jmp 0x4313
movq %rax, %rbx
jmp 0x4313
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x42a6
movl -0x2c(%rbp), %eax
movl %eax, (%r14)
jmp 0x42a6
jmp 0x4287
jmp 0x4287
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x42a6
movl %r13d, (%r14)
jmp 0x42a6
movq %rax, %rbx
jmp 0x42c5
movq %rax, %rbx
jmp 0x42a6
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x42a6
callq 0x2200
movq 0x3cfb(%rip), %rsi      # 0x7fa8
leaq -0x238(%rbp), %rdi
callq 0x22e0
leaq -0x1c0(%rbp), %rdi
callq 0x20d0
leaq -0x70(%rbp), %rdi
callq 0x4368
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x42e3
callq 0x2200
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x42fb
callq 0x2200
leaq -0x440(%rbp), %rdi
callq 0x20a0
leaq -0x840(%rbp), %rdi
callq 0x2c30
movq %rbx, %rdi
callq 0x2380
nop