// File: CMU-SAFARI[P]MQSim/src/host/IO_Flow_Trace_Based.cpp
// Function: Host_Components::IO_Flow_Trace_Based::Start_simulation()
void IO_Flow_Trace_Based::Start_simulation()
{
	IO_Flow_Base::Start_simulation();
	std::string trace_line;
	char *pEnd;

	trace_file.open(trace_file_path, std::ios::in);
	if (!trace_file.is_open())
	{
		PRINT_ERROR("Error while opening input trace file: " << trace_file_path)
	}
	PRINT_MESSAGE("Investigating input trace file: " << trace_file_path);

	sim_time_type last_request_arrival_time = 0;
	while (std::getline(trace_file, trace_line))
	{
		Utils::Helper_Functions::Remove_cr(trace_line);
		current_trace_line.clear();
		Utils::Helper_Functions::Tokenize(trace_line, ASCIILineDelimiter, current_trace_line);
		if (current_trace_line.size() != ASCIIItemsPerLine)
		{
			break;
		}
		total_requests_in_file++;
		sim_time_type prev_time = last_request_arrival_time;
		last_request_arrival_time = std::strtoll(current_trace_line[ASCIITraceTimeColumn].c_str(), &pEnd, 10);
		if (last_request_arrival_time < prev_time)
		{
			PRINT_ERROR("Unexpected request arrival time: " << last_request_arrival_time << "\nMQSim expects request arrival times to be monotonically increasing in the input trace!")
		}
	}

	trace_file.close();
	PRINT_MESSAGE("Trace file: " << trace_file_path << " seems healthy");

	if (total_replay_no == 1)
	{
		total_requests_to_be_generated = (int)(((double)percentage_to_be_simulated / 100) * total_requests_in_file);
	}
	else
	{
		total_requests_to_be_generated = total_requests_in_file * total_replay_no;
	}

	trace_file.open(trace_file_path);
	current_trace_line.clear();
	std::getline(trace_file, trace_line);
	Utils::Helper_Functions::Remove_cr(trace_line);
	Utils::Helper_Functions::Tokenize(trace_line, ASCIILineDelimiter, current_trace_line);
	Simulator->Register_sim_event(std::strtoll(current_trace_line[ASCIITraceTimeColumn].c_str(), &pEnd, 10), this);
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq 0xe0(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x308c0
leaq 0xc0(%rsp), %rdi
callq 0x85e0
movq 0x90(%rsp), %rsi
movq %rsi, %rdi
addq $0x498, %rdi            # imm = 0x498
addq $0x478, %rsi            # imm = 0x478
movl $0x8, %edx
callq 0x83b0
jmp 0x39856
movq 0x90(%rsp), %rdi
addq $0x498, %rdi            # imm = 0x498
callq 0x86a0
movb %al, 0x8f(%rsp)
jmp 0x39873
movb 0x8f(%rsp), %al
testb $0x1, %al
jne 0x3992a
jmp 0x39884
movq 0xa0765(%rip), %rdi     # 0xd9ff0
leaq 0x71056(%rip), %rsi     # 0xaa8e8
callq 0x8440
jmp 0x39899
movq 0xa0750(%rip), %rdi     # 0xd9ff0
leaq 0x725f9(%rip), %rsi     # 0xabea0
callq 0x8440
movq %rax, 0x80(%rsp)
jmp 0x398b6
movq 0x80(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x478, %rsi            # imm = 0x478
callq 0x83e0
movq %rax, 0x78(%rsp)
jmp 0x398d9
movq 0x78(%rsp), %rdi
movq 0xa0673(%rip), %rsi     # 0xd9f58
callq 0x8480
jmp 0x398ec
movq 0xa06dd(%rip), %rdi     # 0xd9fd0
callq 0x84f0
jmp 0x398fa
movl $0x1, %edi
callq 0x8500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x8848
jmp 0x39d13
movq 0xa0667(%rip), %rdi     # 0xd9f98
leaq 0x7258f(%rip), %rsi     # 0xabec7
callq 0x8440
movq %rax, 0x70(%rsp)
jmp 0x39944
movq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x478, %rsi            # imm = 0x478
callq 0x83e0
movq %rax, 0x68(%rsp)
jmp 0x39964
movq 0x68(%rsp), %rdi
movq 0xa05e8(%rip), %rsi     # 0xd9f58
callq 0x8480
jmp 0x39977
movq $0x0, 0xa0(%rsp)
movq 0x90(%rsp), %rdi
addq $0x498, %rdi            # imm = 0x498
leaq 0xc0(%rsp), %rsi
callq 0x80b0
movq %rax, 0x60(%rsp)
jmp 0x399a6
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x8530
movb %al, 0x5f(%rsp)
jmp 0x399c0
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x399cd
jmp 0x39b2f
leaq 0xc0(%rsp), %rdi
callq 0x3af30
jmp 0x399dc
movq 0x90(%rsp), %rdi
addq $0x6b0, %rdi            # imm = 0x6B0
movq %rdi, 0x50(%rsp)
callq 0x3afa0
movq 0x50(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
movl $0x20, %esi
callq 0x3afc0
jmp 0x39a0e
movq 0x90(%rsp), %rdi
addq $0x6b0, %rdi            # imm = 0x6B0
callq 0x3aef0
cmpq $0x5, %rax
je 0x39a2d
jmp 0x39b2f
movq 0x90(%rsp), %rdi
movl 0x6a8(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x6a8(%rdi)
movq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
addq $0x6b0, %rdi            # imm = 0x6B0
xorl %eax, %eax
movl %eax, %esi
callq 0x3af10
movq %rax, %rdi
callq 0x8190
movq %rax, %rdi
leaq 0xb8(%rsp), %rsi
movl $0xa, %edx
callq 0x8830
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
cmpq 0x98(%rsp), %rax
jae 0x39b2a
movq 0xa054a(%rip), %rdi     # 0xd9ff0
leaq 0x70e3b(%rip), %rsi     # 0xaa8e8
callq 0x8440
jmp 0x39ab4
movq 0xa0535(%rip), %rdi     # 0xd9ff0
leaq 0x72426(%rip), %rsi     # 0xabee8
callq 0x8440
movq %rax, 0x48(%rsp)
jmp 0x39ace
movq 0x48(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x8100
movq %rax, 0x40(%rsp)
jmp 0x39ae7
movq 0x40(%rsp), %rdi
leaq 0x72417(%rip), %rsi     # 0xabf0a
callq 0x8440
movq %rax, 0x38(%rsp)
jmp 0x39aff
movq 0x38(%rsp), %rdi
movq 0xa044d(%rip), %rsi     # 0xd9f58
callq 0x8480
jmp 0x39b12
movq 0xa04b7(%rip), %rdi     # 0xd9fd0
callq 0x84f0
jmp 0x39b20
movl $0x1, %edi
callq 0x8500
jmp 0x39983
movq 0x90(%rsp), %rdi
addq $0x498, %rdi            # imm = 0x498
callq 0x80d0
jmp 0x39b45
movq 0xa044c(%rip), %rdi     # 0xd9f98
leaq 0x7240f(%rip), %rsi     # 0xabf62
callq 0x8440
movq %rax, 0x30(%rsp)
jmp 0x39b5f
movq 0x30(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x478, %rsi            # imm = 0x478
callq 0x83e0
movq %rax, 0x28(%rsp)
jmp 0x39b7f
movq 0x28(%rsp), %rdi
leaq 0x723e4(%rip), %rsi     # 0xabf6f
callq 0x8440
movq %rax, 0x20(%rsp)
jmp 0x39b97
movq 0x20(%rsp), %rdi
movq 0xa03b5(%rip), %rsi     # 0xd9f58
callq 0x8480
jmp 0x39baa
movq 0x90(%rsp), %rax
cmpl $0x1, 0x6a0(%rax)
jne 0x39bf2
movq 0x90(%rsp), %rax
movl 0x470(%rax), %ecx
cvtsi2sd %rcx, %xmm0
movsd 0x71502(%rip), %xmm1    # 0xab0d8
divsd %xmm1, %xmm0
movl 0x6a8(%rax), %ecx
cvtsi2sd %rcx, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x50(%rax)
jmp 0x39c0a
movq 0x90(%rsp), %rax
movl 0x6a8(%rax), %ecx
imull 0x6a0(%rax), %ecx
movl %ecx, 0x50(%rax)
movq 0x90(%rsp), %rsi
movq %rsi, %rdi
addq $0x498, %rdi            # imm = 0x498
addq $0x478, %rsi            # imm = 0x478
movl $0x8, %edx
callq 0x83b0
jmp 0x39c2f
movq 0x90(%rsp), %rdi
addq $0x6b0, %rdi            # imm = 0x6B0
callq 0x3afa0
movq 0x90(%rsp), %rdi
addq $0x498, %rdi            # imm = 0x498
leaq 0xc0(%rsp), %rsi
callq 0x80b0
jmp 0x39c61
leaq 0xc0(%rsp), %rdi
callq 0x3af30
jmp 0x39c70
movq 0x90(%rsp), %rdx
addq $0x6b0, %rdx            # imm = 0x6B0
leaq 0xc0(%rsp), %rdi
movl $0x20, %esi
callq 0x3afc0
jmp 0x39c93
callq 0x46500
movq %rax, 0x18(%rsp)
jmp 0x39c9f
movq 0x90(%rsp), %rdi
addq $0x6b0, %rdi            # imm = 0x6B0
xorl %eax, %eax
movl %eax, 0x14(%rsp)
movl %eax, %esi
movq %rsi, 0x8(%rsp)
callq 0x3af10
movq %rax, %rdi
callq 0x8190
movq %rax, %rdi
leaq 0xb8(%rsp), %rsi
movl $0xa, %edx
callq 0x8830
movq 0x18(%rsp), %rdi
movq 0x90(%rsp), %rdx
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %r8d
movq %rax, %rsi
callq 0x46c60
jmp 0x39cfe
leaq 0xc0(%rsp), %rdi
callq 0x8848
addq $0xe8, %rsp
retq
movq 0xb0(%rsp), %rdi
callq 0x8770