// File: /workspace/github_cpp_makefile_star2_O1/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 0x2a4e
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 0x2a8d
leaq -0x60(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x3f81(%rip), %rdx      # 0x6465
leaq -0x70(%rbp), %rdi
movq %rdx, %rsi
callq 0x3568
leaq -0x80(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x3f66(%rip), %rdx      # 0x6465
leaq -0x90(%rbp), %rdi
movq %rdx, %rsi
callq 0x3568
leaq -0xa0(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x3f44(%rip), %rdx      # 0x6465
leaq -0xb0(%rbp), %rdi
movq %rdx, %rsi
callq 0x3568
leaq -0x50(%rbp), %rdi
callq 0x4864
leaq -0x2b0(%rbp), %rdi
callq 0x3604
leaq -0x38(%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 -0x48(%rbp), %rsi
callq 0x22d0
xorl %r15d, %r15d
leaq 0x3a77(%rip), %r14      # 0x6004
leaq -0x2b0(%rbp), %r12
leaq -0x130(%rbp), %r13
movl %r15d, %ebx
leaq -0x4b8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2994
leal 0x1(%rbx), %r15d
addl $-0x2, %ebx
cmpl $0xb, %ebx
ja 0x259b
movslq (%r14,%rbx,4), %rax
addq %r14, %rax
jmpq *%rax
leaq -0x1a0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1b0(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3908
movq -0x1b0(%rbp), %rdi
leaq -0x1a0(%rbp), %rcx
jmp 0x26fb
leaq -0x160(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0x170(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0x170(%rbp), %rsi
callq 0x388e
movq -0x170(%rbp), %rdi
leaq -0x160(%rbp), %rcx
jmp 0x26fb
leaq -0x180(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0x190(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0x190(%rbp), %rsi
callq 0x3824
movq -0x190(%rbp), %rdi
leaq -0x180(%rbp), %rcx
jmp 0x26fb
leaq -0x90(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x20e0
jmp 0x270e
leaq -0x140(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0x150(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0x150(%rbp), %rsi
callq 0x38ca
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rcx
movq %rcx, %rax
cmpq %rcx, %rdi
je 0x270e
movq (%rax), %rsi
incq %rsi
callq 0x2170
cmpl $0xe, %r15d
jne 0x259b
movq %r13, %rdi
movq %r12, %rsi
callq 0x3832
movq %r13, %rdi
leaq 0x3988(%rip), %rsi      # 0x60b5
callq 0x2060
movl %eax, %r15d
testl %eax, %eax
leaq -0x1f0(%rbp), %rbx
je 0x2761
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3832
movq %rbx, %rdi
leaq 0x3969(%rip), %rsi      # 0x60be
callq 0x2060
testl %eax, %eax
sete %bl
jmp 0x2763
movb $0x1, %bl
testl %r15d, %r15d
je 0x278a
movq -0x1f0(%rbp), %rdi
leaq -0x1e0(%rbp), %rax
cmpq %rax, %rdi
je 0x278a
movq -0x1e0(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x27ac
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2170
testb %bl, %bl
je 0x294a
leaq -0x1d0(%rbp), %rdi
leaq 0x69c6(%rip), %rsi      # 0x9188
callq 0x3019
movq %r12, %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x37ee
movq -0x1d0(%rbp), %rdi
leaq -0x1c0(%rbp), %rax
cmpq %rax, %rdi
je 0x27f8
movq -0x1c0(%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 0x34ce
movq %r12, %rdi
leaq -0x110(%rbp), %rsi
callq 0x3908
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x2854
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0x4b8(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x2cd9
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x28b7
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x34ce
movq %r12, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3a0e
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movq %r12, %rdi
callq 0x394e
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
leaq -0x578(%rbp), %rbx
je 0x292b
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2170
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30b2
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x48be
movq %rbx, %rdi
callq 0x3240
movq %r13, %rdi
movq %r12, %rsi
callq 0x391a
leaq -0xb0(%rbp), %rdi
movq %r13, %rsi
callq 0x21e0
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x2986
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2170
incl 0x67fc(%rip)            # 0x9188
xorl %r15d, %r15d
jmp 0x259b
leaq -0x4b8(%rbp), %rdi
callq 0x2030
leaq -0x50(%rbp), %rdi
callq 0x4c08
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
leaq -0x60(%rbp), %rbx
leaq -0x80(%rbp), %r14
leaq -0xa0(%rbp), %r15
je 0x29d1
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x2b0(%rbp), %rdi
callq 0x3240
leaq -0x50(%rbp), %rdi
callq 0x4872
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2a01
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0x2a19
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x70(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2a2e
movq -0x60(%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 0x659b(%rip), %rbx      # 0x8ff0
leaq 0x35ec(%rip), %rsi      # 0x6048
movq %rbx, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq 0x35f2(%rip), %rsi      # 0x6065
movq %rbx, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21d0
movq 0x655c(%rip), %rdi      # 0x8ff0
leaq 0x35ed(%rip), %rsi      # 0x6088
movl $0x2c, %edx
callq 0x21c0
movq 0x6544(%rip), %rdi      # 0x8ff0
callq 0x20b0
jmp 0x2a83
movq %rax, %rbx
jmp 0x2c74
movq %rax, %rbx
jmp 0x2c7d
movq %rax, %rbx
jmp 0x2c98
movq %rax, %rbx
jmp 0x2cb0
jmp 0x2ad5
movq %rax, %rbx
jmp 0x2cc5
jmp 0x2c3d
movq %rax, %rbx
movq -0x170(%rbp), %rdi
leaq -0x160(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x160(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x140(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0x1b0(%rbp), %rdi
leaq -0x1a0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x1a0(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0x190(%rbp), %rdi
leaq -0x180(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x180(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0xe0(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0x1d0(%rbp), %rdi
leaq -0x1c0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x1c0(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
leaq -0x578(%rbp), %rdi
callq 0x3240
jmp 0x2c40
movq %rax, %rbx
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x100(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0x120(%rbp), %rsi
jmp 0x2c2f
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c40
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2170
jmp 0x2c40
jmp 0x2c3d
jmp 0x2c3d
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdi
cmpq %rax, %rdi
leaq -0x60(%rbp), %r14
leaq -0x80(%rbp), %r15
leaq -0xa0(%rbp), %r12
je 0x2c68
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x2b0(%rbp), %rdi
callq 0x3240
leaq -0x50(%rbp), %rdi
callq 0x4872
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x2c98
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x90(%rbp), %rdi
cmpq %r15, %rdi
je 0x2cb0
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2170
movq -0x70(%rbp), %rdi
cmpq %r14, %rdi
je 0x2cc5
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2170
leaq -0x4b8(%rbp), %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x22b0