// File: /workspace/github_cpp_makefile_star2_O3/jmfenner92[P]Log-Parser/src/log_parser.cpp
// Function: main
int main(int argc, char** argv) {

	// check the number of arguments. it should be 2: one for the name of the program and 
	// one for the log file to be read in
	if (argc != 2) {
		cerr << "Invalid number of arguments!" << endl;
		cerr << "Usage: ./log_parser.cpp <log file>" << endl;
		exit(-1);
	}

	// open the file for reading and check for success
	ifstream log_file;
	log_file.open(argv[1]);
	if (!log_file) {
		cerr << "File failed to open. Are you sure it exists?" << endl;
		exit(-1);
	}

	/* 
	** Okay, real talk...
	** We set up a temp_record variable of type CmdRecord to start collecting the data for the
	** record to be put into the list. If we see one the the addresses that we need, we can
	** insert the record at the end of the record loop. If it's not, then just keep moving on.
	** TODO: this might be better as a function. 
	*/
	

	// now that we're in the clear, let's define some useful variables
	string curr_field = "";
	int counter = 0;
	int field_counter = 0;
	string temp_data = "";
	string past_time = "";

	// we also have to set up a new command list, and a temporary record collector
	CmdList commands;
	CmdRecord temp_record;

	// do a getline() to remove the header and fire up the record collection loop
	std::string temp;
	getline(log_file, temp);
	while(log_file >> curr_field) {
		field_counter++;

		if (field_counter == RELTIME) {
			// we have the Rel Time field value
			temp_record.Set_RelTime(curr_field);

		} else if (field_counter == ADDRESS) {
			// we have the Address field value
			temp_record.Set_Address(curr_field);

		} else if (field_counter == DATA) {
			// we have the Data field value (this is special if it's in the address of
			// a command we want, so set it aside for later)
			temp_data = curr_field;

		} else if(field_counter == SIZE) {
			// we have the Size field value
			temp_record.Set_Size(curr_field);

		} else if(field_counter == CYCLE) {
			// we have the Cycle field value
			temp_record.Set_Cycle(curr_field);

		}

		// if the field_counter is 14, that means we've hit the end of a line. this is
		// where decision making comes in. two things can happen here: if it's not a 
		// record want (the easist), we reset the field_counter and increment the 
		// line_count. if it's a record we want, we do the above and calculate the size
		// of the data, and then enter the data collection loop.
		if (field_counter == ENDLINE) {
			if (temp_record.Get_Address() == CMD1 || 
				temp_record.Get_Address() == CMD2) {
				// set the line number and past time (which is the time for the 
				// current command)
				temp_record.Set_Line_Number(patch::to_string(line_count));
				temp_record.Set_RelTime(past_time);

				// pass the current node and the ifstream to the dataCollection 
				// function to keep this from getting cluttered
				dataCollection(&temp_record, log_file, temp_data);

				//add # of words to record
				//std::cout << temp_record.HexToDec(temp_data) / 2 << "\n";
				temp_record.Set_Total_Words(temp_record.HexToDec(temp_data) / 2);
				//std::cout << temp_record.Get_Total_Words() << "\n";

				// add it to the command list
				commands.AddRecord(temp_record);
			}
			
			// this happens every time we reach the end of a line
			past_time = temp_record.Get_RelTime();
			field_counter = 0;
			line_count++;
		}  

	}
	
	log_file.close();
	//commands.PrintList();
	commands.Display();
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x558, %rsp            # imm = 0x558
cmpl $0x2, %edi
jne 0x2a9a
movq %rsi, %r14
leaq -0x4b8(%rbp), %rbx
movq %rbx, %rdi
callq 0x21b0
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x20f0
movq -0x4b8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x498(%rbp,%rax)
jne 0x2ad9
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x3f81(%rip), %rdx      # 0x6465
leaq -0x50(%rbp), %rdi
movq %rdx, %rsi
callq 0x35be
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x3f66(%rip), %rdx      # 0x6465
leaq -0x70(%rbp), %rdi
movq %rdx, %rsi
callq 0x35be
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x3f48(%rip), %rdx      # 0x6465
leaq -0xb0(%rbp), %rdi
movq %rdx, %rsi
callq 0x35be
leaq -0x30(%rbp), %rdi
callq 0x4838
leaq -0x2b0(%rbp), %rdi
callq 0x365c
leaq -0x1c0(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x4b8(%rbp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %edx
leaq -0x4b8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x22d0
xorl %r15d, %r15d
leaq -0x4b8(%rbp), %rbx
leaq -0x50(%rbp), %r14
leaq 0x3a6a(%rip), %r13      # 0x6004
leaq -0x2b0(%rbp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x29da
leal 0x1(%r15), %eax
movl %r15d, %ecx
addl $-0x2, %ecx
movl %eax, %r15d
cmpl $0xb, %ecx
ja 0x25a1
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
movl %eax, %r15d
jmpq *%rcx
leaq -0x180(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x190(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0x190(%rbp), %rsi
callq 0x3960
movq -0x190(%rbp), %rdi
movl $0x3, %r15d
leaq -0x180(%rbp), %rax
cmpq %rax, %rdi
je 0x25a1
movq -0x180(%rbp), %rsi
jmp 0x2760
leaq -0x120(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0x130(%rbp), %rsi
callq 0x3922
movq -0x130(%rbp), %rdi
movl $0xa, %r15d
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x25a1
movq -0x120(%rbp), %rsi
jmp 0x2760
movl $0x8, %r15d
leaq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x20e0
jmp 0x25a1
leaq -0x140(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x150(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0x150(%rbp), %rsi
callq 0x38e6
movq -0x150(%rbp), %rdi
movl $0x9, %r15d
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x25a1
movq -0x140(%rbp), %rsi
jmp 0x2760
leaq -0x160(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x170(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0x170(%rbp), %rsi
callq 0x387c
movq -0x170(%rbp), %rdi
movl $0x7, %r15d
leaq -0x160(%rbp), %rax
cmpq %rax, %rdi
je 0x25a1
movq -0x160(%rbp), %rsi
incq %rsi
callq 0x2170
jmp 0x25a1
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x388a
movq %r15, %rdi
leaq 0x392c(%rip), %rsi      # 0x60b5
callq 0x2060
testl %eax, %eax
je 0x27dd
leaq -0x1f0(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x388a
movq %r15, %rdi
leaq 0x3910(%rip), %rsi      # 0x60be
callq 0x2060
testl %eax, %eax
sete %r15b
movq -0x1f0(%rbp), %rdi
leaq -0x1e0(%rbp), %rax
cmpq %rax, %rdi
je 0x27e0
movq -0x1e0(%rbp), %rsi
incq %rsi
callq 0x2170
jmp 0x27e0
movb $0x1, %r15b
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x27fc
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2170
testb %r15b, %r15b
je 0x298f
leaq -0x1b0(%rbp), %rdi
leaq 0x6975(%rip), %rsi      # 0x9188
callq 0x3067
movq %r12, %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3846
movq -0x1b0(%rbp), %rdi
leaq -0x1a0(%rbp), %rax
cmpq %rax, %rdi
je 0x2849
movq -0x1a0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x100(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0x110(%rbp), %rsi
callq 0x3960
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x28a5
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
movq %rbx, %rsi
leaq -0xf0(%rbp), %rdx
callq 0x2d27
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x28fe
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3520
movq %r12, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3a66
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movq %r12, %rdi
callq 0x39a6
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2965
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x578(%rbp), %rdi
movq %r12, %rsi
callq 0x3100
leaq -0x30(%rbp), %rdi
leaq -0x578(%rbp), %r15
movq %r15, %rsi
callq 0x4892
movq %r15, %rdi
callq 0x328e
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3972
leaq -0xb0(%rbp), %rdi
movq %r15, %rsi
callq 0x21e0
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x29cc
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2170
incl 0x67b6(%rip)            # 0x9188
xorl %r15d, %r15d
jmp 0x25a1
leaq -0x4b8(%rbp), %rdi
callq 0x2030
leaq -0x30(%rbp), %rdi
callq 0x4bc8
movq -0x1d0(%rbp), %rdi
leaq -0x1c0(%rbp), %rax
cmpq %rax, %rdi
leaq -0x40(%rbp), %rbx
leaq -0x60(%rbp), %r14
je 0x2a19
movq -0x1c0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x2b0(%rbp), %rdi
callq 0x328e
leaq -0x30(%rbp), %rdi
callq 0x4846
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2a50
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x70(%rbp), %rdi
cmpq %r14, %rdi
je 0x2a65
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x50(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2a7a
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x4b8(%rbp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0x558, %rsp            # imm = 0x558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x654f(%rip), %rbx      # 0x8ff0
leaq 0x35a0(%rip), %rsi      # 0x6048
movq %rbx, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq 0x35a6(%rip), %rsi      # 0x6065
movq %rbx, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21d0
movq 0x6510(%rip), %rdi      # 0x8ff0
leaq 0x35a1(%rip), %rsi      # 0x6088
movl $0x2c, %edx
callq 0x21c0
movq 0x64f8(%rip), %rdi      # 0x8ff0
callq 0x20b0
jmp 0x2acf
movq %rax, %rbx
jmp 0x2cb6
movq %rax, %rbx
jmp 0x2cbf
movq %rax, %rbx
jmp 0x2ce1
movq %rax, %rbx
jmp 0x2cfa
jmp 0x2b21
movq %rax, %rbx
jmp 0x2d13
movq %rax, %rbx
movq -0x1b0(%rbp), %rdi
leaq -0x1a0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x1a0(%rbp), %rsi
jmp 0x2c7b
movq %rax, %rbx
leaq -0x578(%rbp), %rdi
callq 0x328e
jmp 0x2c88
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0xe0(%rbp), %rsi
jmp 0x2c7b
movq %rax, %rbx
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x100(%rbp), %rsi
jmp 0x2c7b
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x80(%rbp), %rsi
jmp 0x2c7b
jmp 0x2c85
movq %rax, %rbx
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x140(%rbp), %rsi
jmp 0x2c7b
jmp 0x2c85
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0xc0(%rbp), %rsi
jmp 0x2c7b
movq %rax, %rbx
movq -0x170(%rbp), %rdi
leaq -0x160(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x160(%rbp), %rsi
jmp 0x2c7b
movq %rax, %rbx
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x120(%rbp), %rsi
jmp 0x2c7b
jmp 0x2c85
movq %rax, %rbx
movq -0x190(%rbp), %rdi
leaq -0x180(%rbp), %rax
cmpq %rax, %rdi
je 0x2c88
movq -0x180(%rbp), %rsi
incq %rsi
callq 0x2170
jmp 0x2c88
movq %rax, %rbx
movq -0x1d0(%rbp), %rdi
leaq -0x1c0(%rbp), %rax
cmpq %rax, %rdi
je 0x2caa
movq -0x1c0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x2b0(%rbp), %rdi
callq 0x328e
leaq -0x30(%rbp), %rdi
callq 0x4846
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2ce1
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x2cfa
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x2d13
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x4b8(%rbp), %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x22b0