// File: /workspace/github_cpp_makefile_star2_O1/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 0x3ab2
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
xorl %esi, %esi
callq 0x23c0
testl %eax, %eax
jne 0x3ac0
leaq 0x64f0(%rip), %rdi      # 0x9660
callq 0x4f18
movl $0x50, %edi
callq 0x2250
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
addq $0x10, %rax
movq %rax, (%r14)
movq $0x0, 0x8(%r14)
movb $0x0, 0x10(%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 0x278f
movl $0x25a0, %edi           # imm = 0x25A0
callq 0x2250
movq %rax, %r12
movq 0x38(%r14), %rsi
movq %rax, %rdi
callq 0x3cc4
movq %r14, 0x8(%rsp)
movl $0x10, %edi
callq 0x2250
xorps %xmm0, %xmm0
movq %rax, (%rsp)
movaps %xmm0, (%rax)
movq 0x602f(%rip), %rdi      # 0x9238
leaq 0x3249(%rip), %rsi      # 0x6459
callq 0x22b0
movq %rax, 0x604c(%rip)      # 0x9268
movq %rax, %rdi
callq 0x2240
movq 0x5d9d(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
leaq 0x2f50(%rip), %r14      # 0x6185
movl $0xe, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x2440
movl $0xa, %edi
callq 0x2320
movq 0x5fe0(%rip), %rdi      # 0x9238
leaq 0x2f35(%rip), %rsi      # 0x6194
callq 0x22b0
movq %rax, %rbx
movq %rax, 0x5ffa(%rip)      # 0x9268
leaq 0x168(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2f12(%rip), %rdx      # 0x6193
leaq 0x158(%rsp), %rdi
movq %r14, %rsi
callq 0x3c32
leaq 0x158(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x260c
leaq 0x5fc3(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2340
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x32c4
callq 0x2210
movq 0x5f9d(%rip), %rdi      # 0x9268
callq 0x22a0
movq 0x5f91(%rip), %rdi      # 0x9268
callq 0x2240
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x2eab(%rip), %rsi      # 0x6196
callq 0x22b0
movq %rax, 0x70(%rsp)
leaq 0x6364(%rip), %rdi      # 0x9660
callq 0x4f20
movq (%rsp), %r15
leaq 0xe8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2e80(%rip), %rsi      # 0x6198
leaq 0x2e86(%rip), %rdx      # 0x61a5
leaq 0xd8(%rsp), %rdi
callq 0x3c32
leaq 0x632d(%rip), %rdi      # 0x9660
leaq 0xd8(%rsp), %rsi
callq 0x4f2c
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3352
callq 0x2210
movq %r12, 0x38(%rsp)
cmpq $0x0, 0x70(%rsp)
movq 0x8(%rsp), %r14
je 0x3ad8
movq %r15, %rbp
addq $0x8, %rbp
leaq 0xc8(%rsp), %r13
movq $0x0, 0x30(%rsp)
leaq 0x62d9(%rip), %rbx      # 0x9660
leaq 0xb8(%rsp), %r12
movq $0x0, 0x68(%rsp)
movq %rbx, %rdi
callq 0x4f18
movq 0x70(%rsp), %rdi
leaq 0x2e05(%rip), %rsi      # 0x61b1
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2410
cmpl $-0x1, %eax
je 0x3571
movq %rbx, %rdi
callq 0x4f20
movq %r13, 0xb8(%rsp)
movq %r12, %rdi
leaq 0x2ddd(%rip), %rsi      # 0x61b9
leaq 0x2de0(%rip), %rdx      # 0x61c3
callq 0x3c32
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f2c
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x3405
callq 0x2210
movq (%r15), %rax
cmpq 0x38(%r14), %rax
jb 0x3398
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 0x3491
movl $0x4, %r14d
movq %rsi, %rcx
cmpq $0x63, %rcx
jbe 0x3488
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x348e
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3491
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 0x3446
addl $-0x3, %r14d
jmp 0x3491
addl $-0x2, %r14d
jmp 0x3491
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 0x2290
addq 0x10(%rsp), %r15
movq %r15, %rdi
movl %r14d, %esi
movq 0x40(%rsp), %rdx
callq 0x3bb6
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %esi
xorl %edx, %edx
callq 0x307c
movq 0x8(%rsp), %r14
movl 0x24(%r14), %r8d
movl 0x28(%r14), %r9d
xorl %edx, %edx
divl %r8d
cmpl %r9d, %edx
jae 0x350d
movl 0x20(%r14), %ecx
movzbl 0x30(%rsp), %edx
andl $0x1, %edx
movq 0x38(%rsp), %rdi
movq (%rsp), %rsi
callq 0x3e52
movq (%rsp), %rax
movq (%rax), %rax
movq 0x48(%r14), %r15
movq %rax, 0x40(%rsp)
cmpq %r15, %rax
jb 0x3544
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 0x3e52
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3558
callq 0x2210
incq 0x68(%rsp)
cmpq %r15, 0x40(%rsp)
movq (%rsp), %r15
movq 0x8(%rsp), %r14
jb 0x3398
leaq 0x60e8(%rip), %rdi      # 0x9660
callq 0x4f18
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x46dc
leaq 0x60cf(%rip), %rdi      # 0x9660
callq 0x4f20
leaq 0xa8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2c1b(%rip), %rsi      # 0x61c4
leaq 0x2c1f(%rip), %rdx      # 0x61cf
leaq 0x98(%rsp), %rdi
callq 0x3c32
leaq 0x609c(%rip), %rdi      # 0x9660
leaq 0x98(%rsp), %rsi
callq 0x4f2c
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x35e3
callq 0x2210
leaq 0x6076(%rip), %rdi      # 0x9660
callq 0x5062
movq %rbp, %rdi
callq 0x4792
movq %rax, %rbx
movq %rax, %r12
negq %r12
cmovsq %rax, %r12
movl $0x1, %r14d
cmpq $0xa, %r12
jb 0x3664
movl $0x4, %r14d
movq %r12, %rcx
cmpq $0x63, %rcx
jbe 0x365b
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3661
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3664
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 0x3619
addl $-0x3, %r14d
jmp 0x3664
addl $-0x2, %r14d
jmp 0x3664
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 0x2290
leaq 0x48(%rsp), %r15
addq (%r15), %rbx
movq %rbx, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x3bb6
leaq 0x2b2f(%rip), %rcx      # 0x61d0
movl $0xa, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2280
leaq 0x20(%rsp), %rsi
movq %rsi, 0x10(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x36db
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0x36e1
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 0x370d
callq 0x2210
movslq 0x28(%r14), %r15
movq %rbp, %rdi
callq 0x4792
movq %rax, %rbx
movslq 0x24(%r14), %r12
movq 0x58a1(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x2340
movl $0xa, %edi
callq 0x2320
movq 0x5af1(%rip), %rdi      # 0x9238
leaq 0x2a46(%rip), %rsi      # 0x6194
callq 0x22b0
movq %rax, %r14
movq %rax, 0x5b0b(%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 0x260c
leaq 0x5ac5(%rip), %rsi      # 0x9270
movq %r14, %rdi
xorl %eax, %eax
callq 0x2340
movq (%r12), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x37cb
callq 0x2210
movq 0x5a96(%rip), %rdi      # 0x9268
callq 0x22a0
movq 0x5a8a(%rip), %rdi      # 0x9268
callq 0x2240
movslq %r15d, %r12
movl %r12d, %r15d
negl %r15d
cmovsl %r12d, %r15d
cmpl $0xa, %r15d
jb 0x3845
movl $0x4, %ebx
movq %r15, %rcx
cmpq $0x63, %rcx
jbe 0x383e
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3843
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3845
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 0x37fe
addl $-0x3, %ebx
jmp 0x3845
addl $-0x2, %ebx
jmp 0x3845
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 0x2290
leaq 0x78(%rsp), %r14
addq (%r14), %r12
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x3bb6
leaq 0x2957(%rip), %rcx      # 0x61db
movl $0x10, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2280
movq %r13, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x38b9
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x38c1
movups (%rcx), %xmm0
movups %xmm0, (%r13)
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 0x38e8
callq 0x2210
movq 0x56d9(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
movq 0x48(%rsp), %rsi
xorl %eax, %eax
callq 0x2340
movl $0xa, %edi
callq 0x2320
movq 0x5929(%rip), %rdi      # 0x9238
leaq 0x287e(%rip), %rsi      # 0x6194
callq 0x22b0
movq %rax, %rbx
movq %rax, 0x5943(%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 0x260c
leaq 0x5913(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2340
movq (%r14), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x397c
callq 0x2210
movq 0x58e5(%rip), %rdi      # 0x9268
callq 0x22a0
movq 0x58d9(%rip), %rdi      # 0x9268
callq 0x2240
movq 0x562d(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
leaq 0x2847(%rip), %r14      # 0x61ec
movl $0xd, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x2440
movl $0xa, %edi
callq 0x2320
movq 0x5870(%rip), %rdi      # 0x9238
leaq 0x27c5(%rip), %rsi      # 0x6194
callq 0x22b0
movq %rax, %rbx
movq %rax, 0x588a(%rip)      # 0x9268
leaq 0x108(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2808(%rip), %rdx      # 0x61f9
leaq 0xf8(%rsp), %rdi
movq %r14, %rsi
callq 0x3c32
leaq 0xf8(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x260c
leaq 0x5853(%rip), %rsi      # 0x9270
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2340
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x3a34
callq 0x2210
movq 0x582d(%rip), %rdi      # 0x9268
callq 0x22a0
movq 0x5821(%rip), %rdi      # 0x9268
callq 0x2240
movq (%rsp), %rdi
callq 0x2210
movq %r12, %rdi
callq 0x3e32
movq %r12, %rdi
callq 0x2210
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x20(%rsp), %rbx
je 0x3a79
callq 0x2210
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3a88
callq 0x2210
leaq 0x188(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3a9e
callq 0x2210
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27cb(%rip), %rdi      # 0x6284
callq 0x2380
jmp 0x3ace
leaq 0x2679(%rip), %rdi      # 0x6140
xorl %eax, %eax
callq 0x2040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22d0
leaq 0x26c7(%rip), %rdi      # 0x61a6
leaq 0x267b(%rip), %rsi      # 0x6161
leaq 0x2681(%rip), %rcx      # 0x616e
movl $0xbf, %edx
callq 0x2150
jmp 0x3b27
jmp 0x3b95
jmp 0x3b00
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
jne 0x3b1c
jmp 0x3b2a
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3b2a
callq 0x2210
jmp 0x3b2a
jmp 0x3b27
jmp 0x3b27
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
jne 0x3b8e
jmp 0x3b98
movq %rax, %rbx
movq 0x48(%rsp), %rdi
jmp 0x3b89
movq %rax, %rbx
movq 0x98(%rsp), %rdi
jmp 0x3b5f
jmp 0x3b95
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
jne 0x3b8e
jmp 0x3b98
jmp 0x3b95
jmp 0x3b95
jmp 0x3b95
movq %rax, %rbx
movq %r12, %rdi
jmp 0x3b8e
jmp 0x3b95
jmp 0x3b95
jmp 0x3b95
jmp 0x3b95
jmp 0x3b95
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x3b98
callq 0x2210
jmp 0x3b98
movq %rax, %rbx
leaq 0x188(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3bae
callq 0x2210
movq %rbx, %rdi
callq 0x23d0