// File: /workspace/github_cpp_makefile_star2_O3/dudulu233[P]EMA-Tool/ema_tool.cpp
// Function: main
int main(int argc, char* argv[])
{
	if (2 != argc)
	{
		printf("Usage: ./ema_tool config_file_name\n");
		exit(-1);
	}

	if (0 != access(argv[1], F_OK))
	{
		printf("Error, config file %s not exist\n");
		exit(-1);
	}

	_time_analysis.set_start_time();

	bool get_reuse_dis = false;
	uint64 total_trace_cnt = 0;

	cfg_ema* cfg = new cfg_ema();
	string conf_file_name(argv[1]);
	assert(NULL != cfg);
	get_cfg(conf_file_name.c_str(), cfg);

	StatCache* sc = new StatCache(cfg->io_trace_start_time);
	IoRecord* io_trace = new IoRecord();
	LogClear();
	
	LogWrite(1, "process start~");

	FILE* fp = fopen(cfg->cache_file.c_str(), "r");

	_time_analysis.set_end_time();
	_time_analysis.add_time("ema init time");

	while (1) {
		assert(NULL != fp);
		assert(NULL != io_trace);

		_time_analysis.set_start_time();

		if(fscanf(fp, "%lu,%lu",
			&io_trace->alloc_time,
			&io_trace->cache_addr
		) == EOF) break;

		_time_analysis.set_end_time();
		_time_analysis.add_time("read trace");

		//program begin

		if (io_trace->alloc_time < cfg->io_trace_start_time) {
			continue;
		}
		//begin to get the reuse distance
		if (io_trace->alloc_time >= cfg->re_dis_start_time) {
			get_reuse_dis = true;
		}

		//hash(A) mod P < T, sampling rate = T / P * 100%
		//e.g. P=100, T=1,so the sampling rate = 0.01
		string str_cache_addr = to_string(static_cast<long long>(io_trace->cache_addr));
		uint32_t hash = murmurhash(str_cache_addr.c_str(), (str_cache_addr).length(), 0);

		total_trace_cnt++;
		if (hash % cfg->sampling_P < cfg->sampling_T) {
			sc->main_operation(io_trace, get_reuse_dis, cfg->cache_size, cfg->sampling_P, cfg->sampling_T);
		}

		//finish
		if (io_trace->alloc_time >= cfg->io_trace_end_time) {
			sc->main_operation(io_trace, get_reuse_dis, cfg->cache_size, cfg->sampling_P, cfg->sampling_T);
			break;
		}

		
	}


	_time_analysis.set_start_time();
	sc->output_reuse_distance();
	_time_analysis.set_end_time();
	_time_analysis.add_time("output time");

	_time_analysis.print_all_latency();

	string map_size_info = "map_size: " + to_string(static_cast<long long>(sc->get_map_size()));
	int unique_ratio = total_trace_cnt * cfg->sampling_T / sc->get_map_size() / cfg->sampling_P;
	LogWrite(1, map_size_info.c_str());
	string unique_ratio_info = "unique_ratio: 1/" + to_string(static_cast<long long>(unique_ratio));
	LogWrite(1, unique_ratio_info.c_str());
	LogWrite(1, "process end~~");

	delete io_trace;
	io_trace = NULL;
	delete sc;
	sc = NULL;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
cmpl $0x2, %edi
jne 0x3aa8
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
xorl %esi, %esi
callq 0x23d0
testl %eax, %eax
jne 0x3ab6
leaq 0x64f9(%rip), %rdi      # 0x9660
callq 0x4e50
movl $0x50, %edi
callq 0x2260
movq %rax, %r14
addq $0x10, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x40(%r14)
movq %rax, (%r14)
movq $0x0, 0x8(%r14)
movq 0x8(%rbx), %rsi
leaq 0x178(%rsp), %rbx
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x20f0
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x279f
movl $0x25a0, %edi           # imm = 0x25A0
callq 0x2260
movq %rax, %r12
movq 0x38(%r14), %rsi
movq %rax, %rdi
callq 0x3cc0
movq %r14, 0x8(%rsp)
movl $0x10, %edi
callq 0x2260
xorps %xmm0, %xmm0
movq %rax, (%rsp)
movaps %xmm0, (%rax)
movq 0x6044(%rip), %rdi      # 0x9240
leaq 0x325e(%rip), %rsi      # 0x6461
callq 0x22c0
movq %rax, 0x6059(%rip)      # 0x9268
movq %rax, %rdi
callq 0x2250
movq 0x5daa(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
leaq 0x2f5d(%rip), %r14      # 0x6185
movl $0xe, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x2450
movl $0xa, %edi
callq 0x2330
movq 0x5ff5(%rip), %rdi      # 0x9240
leaq 0x2f42(%rip), %rsi      # 0x6194
callq 0x22c0
movq %rax, %rbx
movq %rax, 0x6007(%rip)      # 0x9268
leaq 0x168(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2f1f(%rip), %rdx      # 0x6193
leaq 0x158(%rsp), %rdi
movq %r14, %rsi
callq 0x3c28
leaq 0x158(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x261c
leaq 0x5fd0(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2350
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x32b7
callq 0x2220
movq 0x5faa(%rip), %rdi      # 0x9268
callq 0x22b0
movq 0x5f9e(%rip), %rdi      # 0x9268
callq 0x2250
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x2eb8(%rip), %rsi      # 0x6196
callq 0x22c0
movq %rax, 0x70(%rsp)
leaq 0x6371(%rip), %rdi      # 0x9660
callq 0x4e58
movq (%rsp), %r15
leaq 0xe8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2e8d(%rip), %rsi      # 0x6198
leaq 0x2e93(%rip), %rdx      # 0x61a5
leaq 0xd8(%rsp), %rdi
callq 0x3c28
leaq 0x633a(%rip), %rdi      # 0x9660
leaq 0xd8(%rsp), %rsi
callq 0x4e64
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3345
callq 0x2220
movq %r12, 0x38(%rsp)
cmpq $0x0, 0x70(%rsp)
movq 0x8(%rsp), %r14
je 0x3ace
movq %r15, %rbp
addq $0x8, %rbp
leaq 0xc8(%rsp), %r13
movq $0x0, 0x30(%rsp)
leaq 0x62e6(%rip), %rbx      # 0x9660
leaq 0xb8(%rsp), %r12
movq $0x0, 0x68(%rsp)
movq %rbx, %rdi
callq 0x4e50
movq 0x70(%rsp), %rdi
leaq 0x2e12(%rip), %rsi      # 0x61b1
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2420
cmpl $-0x1, %eax
je 0x3564
movq %rbx, %rdi
callq 0x4e58
movq %r13, 0xb8(%rsp)
movq %r12, %rdi
leaq 0x2dea(%rip), %rsi      # 0x61b9
leaq 0x2ded(%rip), %rdx      # 0x61c3
callq 0x3c28
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4e64
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x33f8
callq 0x2220
movq (%r15), %rax
cmpq 0x38(%r14), %rax
jb 0x338b
cmpq 0x40(%r14), %rax
movq 0x30(%rsp), %rax
movzbl %al, %eax
movl $0x1, %r14d
cmovael %r14d, %eax
movq %rax, 0x30(%rsp)
movq (%rbp), %r15
movq %r15, %rsi
negq %rsi
cmovsq %r15, %rsi
cmpq $0xa, %rsi
jb 0x3484
movl $0x4, %r14d
movq %rsi, %rcx
cmpq $0x63, %rcx
jbe 0x347b
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3481
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3484
movq %rcx, %rax
movabsq $0x346dc5d63886594b, %rdx # imm = 0x346DC5D63886594B
mulq %rdx
shrq $0xb, %rdx
addl $0x4, %r14d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x3439
addl $-0x3, %r14d
jmp 0x3484
addl $-0x2, %r14d
jmp 0x3484
decl %r14d
movq %rsi, 0x40(%rsp)
shrq $0x3f, %r15
leal (%r15,%r14), %esi
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
movl $0x2d, %edx
callq 0x22a0
addq 0x10(%rsp), %r15
movq %r15, %rdi
movl %r14d, %esi
movq 0x40(%rsp), %rdx
callq 0x3bac
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %esi
xorl %edx, %edx
callq 0x3073
movq 0x8(%rsp), %r14
movl 0x24(%r14), %r8d
movl 0x28(%r14), %r9d
xorl %edx, %edx
divl %r8d
cmpl %r9d, %edx
jae 0x3500
movl 0x20(%r14), %ecx
movzbl 0x30(%rsp), %edx
andl $0x1, %edx
movq 0x38(%rsp), %rdi
movq (%rsp), %rsi
callq 0x3e4e
movq (%rsp), %rax
movq (%rax), %rax
movq 0x48(%r14), %r15
movq %rax, 0x40(%rsp)
cmpq %r15, %rax
jb 0x3537
movl 0x20(%r14), %ecx
movl 0x24(%r14), %r8d
movl 0x28(%r14), %r9d
movzbl 0x30(%rsp), %edx
andl $0x1, %edx
movq 0x38(%rsp), %rdi
movq (%rsp), %rsi
callq 0x3e4e
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x354b
callq 0x2220
incq 0x68(%rsp)
cmpq %r15, 0x40(%rsp)
movq (%rsp), %r15
movq 0x8(%rsp), %r14
jb 0x338b
leaq 0x60f5(%rip), %rdi      # 0x9660
callq 0x4e50
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x460a
leaq 0x60dc(%rip), %rdi      # 0x9660
callq 0x4e58
leaq 0xa8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2c28(%rip), %rsi      # 0x61c4
leaq 0x2c2c(%rip), %rdx      # 0x61cf
leaq 0x98(%rsp), %rdi
callq 0x3c28
leaq 0x60a9(%rip), %rdi      # 0x9660
leaq 0x98(%rsp), %rsi
callq 0x4e64
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x35d6
callq 0x2220
leaq 0x6083(%rip), %rdi      # 0x9660
callq 0x4f9a
movq %rbp, %rdi
callq 0x46c0
movq %rax, %rbx
movq %rax, %r12
negq %r12
cmovsq %rax, %r12
movl $0x1, %r14d
cmpq $0xa, %r12
jb 0x3657
movl $0x4, %r14d
movq %r12, %rcx
cmpq $0x63, %rcx
jbe 0x364e
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3654
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3657
movq %rcx, %rax
movabsq $0x346dc5d63886594b, %rdx # imm = 0x346DC5D63886594B
mulq %rdx
shrq $0xb, %rdx
addl $0x4, %r14d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x360c
addl $-0x3, %r14d
jmp 0x3657
addl $-0x2, %r14d
jmp 0x3657
decl %r14d
shrq $0x3f, %rbx
leal (%rbx,%r14), %esi
leaq 0x58(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x22a0
leaq 0x48(%rsp), %r15
addq (%r15), %rbx
movq %rbx, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x3bac
leaq 0x2b3c(%rip), %rcx      # 0x61d0
movl $0xa, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
leaq 0x20(%rsp), %rsi
movq %rsi, 0x10(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x36ce
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0x36d4
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rsp), %r14
movq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x3700
callq 0x2220
movslq 0x28(%r14), %r15
movq %rbp, %rdi
callq 0x46c0
movq %rax, %rbx
movslq 0x24(%r14), %r12
movq 0x58ae(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x2350
movl $0xa, %edi
callq 0x2330
movq 0x5b06(%rip), %rdi      # 0x9240
leaq 0x2a53(%rip), %rsi      # 0x6194
callq 0x22c0
movq %rax, %r14
movq %rax, 0x5b18(%rip)      # 0x9268
movq 0x10(%rsp), %rsi
leaq 0x138(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x20f0
movq 0x68(%rsp), %rax
imulq %r15, %rax
xorl %edx, %edx
divq %rbx
xorl %edx, %edx
divq %r12
movq %rax, %r15
movl $0x1, %ebx
leaq 0x138(%rsp), %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x261c
leaq 0x5ad2(%rip), %rsi      # 0x9270
movq %r14, %rdi
xorl %eax, %eax
callq 0x2350
movq (%r12), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x37be
callq 0x2220
movq 0x5aa3(%rip), %rdi      # 0x9268
callq 0x22b0
movq 0x5a97(%rip), %rdi      # 0x9268
callq 0x2250
movslq %r15d, %r12
movl %r12d, %r15d
negl %r15d
cmovsl %r12d, %r15d
cmpl $0xa, %r15d
jb 0x3838
movl $0x4, %ebx
movq %r15, %rcx
cmpq $0x63, %rcx
jbe 0x3831
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3836
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3838
movq %rcx, %rax
movabsq $0x346dc5d63886594b, %rdx # imm = 0x346DC5D63886594B
mulq %rdx
shrq $0xb, %rdx
addl $0x4, %ebx
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x37f1
addl $-0x3, %ebx
jmp 0x3838
addl $-0x2, %ebx
jmp 0x3838
decl %ebx
shrq $0x3f, %r12
leal (%r12,%rbx), %esi
leaq 0x88(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x78(%rsp), %rdi
movl $0x2d, %edx
callq 0x22a0
leaq 0x78(%rsp), %r14
addq (%r14), %r12
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x3bac
leaq 0x2964(%rip), %rcx      # 0x61db
movl $0x10, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
movq %r13, 0x48(%rsp)
movq (%rax), %rsi
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rsi
je 0x38ac
movq %rsi, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x38b7
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq %r13, %rsi
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x78(%rsp), %rdi
cmpq %rbp, %rdi
je 0x38e3
callq 0x2220
movq 0x48(%rsp), %rsi
movq 0x56de(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2350
movl $0xa, %edi
callq 0x2330
movq 0x593b(%rip), %rdi      # 0x9240
leaq 0x2888(%rip), %rsi      # 0x6194
callq 0x22c0
movq %rax, %rbx
movq %rax, 0x594d(%rip)      # 0x9268
movq 0x48(%rsp), %rsi
leaq 0x118(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x20f0
movq 0x38(%rsp), %r12
leaq 0x118(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x261c
leaq 0x591d(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2350
movq (%r14), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x3972
callq 0x2220
movq 0x58ef(%rip), %rdi      # 0x9268
callq 0x22b0
movq 0x58e3(%rip), %rdi      # 0x9268
callq 0x2250
movq 0x5637(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
leaq 0x2851(%rip), %r14      # 0x61ec
movl $0xd, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x2450
movl $0xa, %edi
callq 0x2330
movq 0x5882(%rip), %rdi      # 0x9240
leaq 0x27cf(%rip), %rsi      # 0x6194
callq 0x22c0
movq %rax, %rbx
movq %rax, 0x5894(%rip)      # 0x9268
leaq 0x108(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2812(%rip), %rdx      # 0x61f9
leaq 0xf8(%rsp), %rdi
movq %r14, %rsi
callq 0x3c28
leaq 0xf8(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x261c
leaq 0x585d(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2350
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x3a2a
callq 0x2220
movq 0x5837(%rip), %rdi      # 0x9268
callq 0x22b0
movq 0x582b(%rip), %rdi      # 0x9268
callq 0x2250
movq (%rsp), %rdi
callq 0x2220
movq %r12, %rdi
callq 0x3e2e
movq %r12, %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x20(%rsp), %rbx
je 0x3a6f
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3a7e
callq 0x2220
leaq 0x188(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3a94
callq 0x2220
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27d5(%rip), %rdi      # 0x6284
callq 0x2390
jmp 0x3ac4
leaq 0x2683(%rip), %rdi      # 0x6140
xorl %eax, %eax
callq 0x2040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22e0
leaq 0x26d1(%rip), %rdi      # 0x61a6
leaq 0x2685(%rip), %rsi      # 0x6161
leaq 0x268b(%rip), %rcx      # 0x616e
movl $0xbf, %edx
callq 0x2150
jmp 0x3b1d
jmp 0x3b8b
jmp 0x3af6
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
jne 0x3b12
jmp 0x3b20
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3b20
callq 0x2220
jmp 0x3b20
jmp 0x3b1d
jmp 0x3b1d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
jne 0x3b84
jmp 0x3b8e
movq %rax, %rbx
movq 0x48(%rsp), %rdi
jmp 0x3b7f
movq %rax, %rbx
movq 0x98(%rsp), %rdi
jmp 0x3b55
jmp 0x3b8b
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
jne 0x3b84
jmp 0x3b8e
jmp 0x3b8b
jmp 0x3b8b
jmp 0x3b8b
movq %rax, %rbx
movq %r12, %rdi
jmp 0x3b84
jmp 0x3b8b
jmp 0x3b8b
jmp 0x3b8b
jmp 0x3b8b
jmp 0x3b8b
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x3b8e
callq 0x2220
jmp 0x3b8e
movq %rax, %rbx
leaq 0x188(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3ba4
callq 0x2220
movq %rbx, %rdi
callq 0x23e0