// File: /workspace/github_cpp_makefile_star2_O2/dudulu233[P]EMA-Tool/ema_tool.cpp
// Function: get_cfg(char const*, Config_ema*)
void get_cfg(const char* cfg_path, cfg_ema* cfg) {

	string key[9] = { "cache_file","cache_size",
					"sampling_P","sampling_T",
					"io_trace_start_time","re_dis_start_time","io_trace_end_time",
					"fixed_mem_size","unique_ratio" };
	string value[9] = { "" };
	int index = 0;
	fstream cfg_file;
	cfg_file.open(cfg_path);
	if (!cfg_file.is_open()) {
		printf("error! config file open fail!\n");
		exit(-1);
	}
	while (index < 9)
	{
		char tmp[128];
		cfg_file.getline(tmp, 128);//128 probably enough
		string line(tmp);

		size_t pos = line.find('=');//find the location of "=",get the key and value
		if (pos == string::npos) {
			printf("error! config format incorrect!\n");
			exit(-1);
		}

		string tmpKey = line.substr(0, pos);//get key
		if (key[index] != tmpKey)
		{
			printf("error! config key incorrect!\n");
			exit(-1);
		}
		value[index] = line.substr(pos + 1);//get value
		++index;

	}
	cfg_file.close();

	cfg->cache_file = value[0];
	cfg->cache_size = stoi(value[1]);
	cfg->sampling_P = stoi(value[2]);
	cfg->sampling_T = stoi(value[3]);
	cfg->io_trace_start_time = StringToDatetime(value[4].c_str());
	cfg->re_dis_start_time = StringToDatetime(value[5].c_str());
	cfg->io_trace_end_time = StringToDatetime(value[6].c_str());
	cfg->fixed_mem_size = stoi(value[7]);
	cfg->unique_ratio = stoi(value[8]);
	
	uint64 trace_line = 0;
	string tmp_str;
	if(cfg->fixed_mem_size > 0){
		ifstream tmp_file(cfg->cache_file.c_str());
		while(getline(tmp_file,tmp_str)){
			trace_line++;
		}
		tmp_file.close();
		//limit_trace_line = 13500·fixed_mem_size - 8888
		//sampling_P = trace_line / limit_trace_line / unique_ratio
		cfg->sampling_P = trace_line / (13500 * cfg->fixed_mem_size - 8888) / cfg->unique_ratio * 1.05 + 0.6;
		cfg->sampling_T = 1;

	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6c8, %rsp            # imm = 0x6C8
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
leaq 0x2949(%rip), %rsi      # 0x50c0
leaq 0x190(%rsp), %r14
leaq 0x2b0(%rsp), %rdx
movq %r14, %rdi
callq 0x2110
leaq 0x1b0(%rsp), %r15
leaq 0x292d(%rip), %rsi      # 0x50cb
leaq 0x4c0(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x1d0(%rsp), %r15
leaq 0x2919(%rip), %rsi      # 0x50d6
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x1f0(%rsp), %r15
leaq 0x2908(%rip), %rsi      # 0x50e1
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x210(%rsp), %r15
leaq 0x28f7(%rip), %rsi      # 0x50ec
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x230(%rsp), %r15
leaq 0x28ef(%rip), %rsi      # 0x5100
leaq 0x50(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x250(%rsp), %r15
leaq 0x28e5(%rip), %rsi      # 0x5112
leaq 0x7(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x270(%rsp), %r15
leaq 0x28db(%rip), %rsi      # 0x5124
leaq 0x6(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x290(%rsp), %r15
leaq 0x28ce(%rip), %rsi      # 0x5133
leaq 0x5(%rsp), %rdx
movq %r15, %rdi
callq 0x2110
leaq 0x2d08(%rip), %rsi      # 0x5581
leaq 0x70(%rsp), %rdi
leaq 0x2b0(%rsp), %rdx
callq 0x2110
pushq $0x30
popq %rax
movq 0x8(%rsp), %r14
leaq (%rsp,%rax), %rcx
addq $0x70, %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x130, %rax            # imm = 0x130
jne 0x2893
leaq 0x2b0(%rsp), %rdi
callq 0x22c0
leaq 0x2b0(%rsp), %rdi
pushq $0x18
popq %rdx
movq %r12, %rsi
callq 0x2220
leaq 0x330(%rsp), %rdi
callq 0x20b0
testb %al, %al
je 0x2bd1
xorl %r15d, %r15d
leaq 0x4c0(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq 0x50(%rsp), %rbx
cmpq $0x120, %r15            # imm = 0x120
je 0x29c1
movl $0x80, %edx
leaq 0x2b0(%rsp), %rdi
movq %r12, %rsi
callq 0x20f0
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2110
movq %r13, %rdi
pushq $0x3d
popq %rsi
xorl %edx, %edx
callq 0x2300
cmpq $-0x1, %rax
je 0x2bb4
movq %rax, %r14
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x2280
leaq (%rsp,%r15), %rdi
addq $0x190, %rdi            # imm = 0x190
movq %rbp, %rsi
callq 0x3479
testb %al, %al
jne 0x2bbd
incq %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
pushq $-0x1
popq %rcx
callq 0x2280
leaq (%rsp,%r15), %rdi
addq $0x70, %rdi
movq %rbx, %rsi
callq 0x22e0
movq %rbx, %rdi
callq 0x2150
movq %rbp, %rdi
callq 0x2150
movq %r13, %rdi
callq 0x2150
addq $0x20, %r15
movq 0x8(%rsp), %r14
jmp 0x2902
leaq 0x2b0(%rsp), %rdi
callq 0x2050
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x2160
pushq $0xa
popq %rdx
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x3483
movl %eax, 0x20(%r14)
leaq 0xb0(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3483
movl %eax, 0x24(%r14)
leaq 0xd0(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3483
movl %eax, 0x28(%r14)
leaq 0x150(%rsp), %rbx
movq -0x60(%rbx), %rdi
callq 0x26c4
movq %rax, 0x38(%r14)
movq -0x40(%rbx), %rdi
callq 0x26c4
movq %rax, 0x40(%r14)
movq -0x20(%rbx), %rdi
callq 0x26c4
movq %rax, 0x48(%r14)
pushq $0xa
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3483
movl %eax, 0x2c(%r14)
leaq 0x170(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3483
movl %eax, 0x30(%r14)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x0, 0x2c(%r14)
jle 0x2b50
movq (%r14), %rsi
leaq 0x4c0(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2400
xorl %ebx, %ebx
leaq 0x4c0(%rsp), %r14
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x2070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2acf
incq %rbx
jmp 0x2ab1
leaq 0x4c0(%rsp), %rdi
callq 0x2090
movq 0x8(%rsp), %rsi
movslq 0x2c(%rsi), %rax
imulq $0x34bc, %rax, %rcx     # imm = 0x34BC
addq $-0x22b8, %rcx          # imm = 0xDD48
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
movslq 0x30(%rsi), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %xmm0
punpckldq 0x24ff(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2507(%rip), %xmm0     # 0x5020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x2503(%rip), %xmm1     # 0x5030
addsd 0x2503(%rip), %xmm1     # 0x5038
cvttsd2si %xmm1, %eax
movl %eax, 0x24(%rsi)
movl $0x1, 0x28(%rsi)
leaq 0x4c0(%rsp), %rdi
callq 0x20d0
leaq 0x10(%rsp), %rdi
callq 0x2150
leaq 0x2b0(%rsp), %rdi
callq 0x20a0
movl $0x100, %ebx            # imm = 0x100
leaq (%rsp,%rbx), %rdi
addq $0x70, %rdi
callq 0x2150
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x2b6c
movl $0x100, %ebx            # imm = 0x100
leaq (%rsp,%rbx), %rdi
addq $0x190, %rdi            # imm = 0x190
callq 0x2150
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x2b88
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x267f(%rip), %rdi      # 0x523a
jmp 0x2bc4
leaq 0x2659(%rip), %rdi      # 0x521d
callq 0x2390
pushq $-0x1
popq %rdi
callq 0x22d0
leaq 0x2627(%rip), %rdi      # 0x51ff
jmp 0x2bc4
jmp 0x2c14
jmp 0x2c26
movq %rax, %rbx
jmp 0x2c56
movq %rax, %rbx
jmp 0x2c73
jmp 0x2bf9
jmp 0x2bf9
jmp 0x2bf9
jmp 0x2bf9
jmp 0x2bf9
jmp 0x2bf9
jmp 0x2bf9
movq %rax, %rbx
addq $-0x20, %r15
movq %r15, %rdi
callq 0x2150
cmpq %r14, %r15
jne 0x2bfc
jmp 0x2c93
jmp 0x2c46
movq %rax, %rbx
leaq 0x4c0(%rsp), %rdi
callq 0x20d0
jmp 0x2c38
movq %rax, %rbx
jmp 0x2c38
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2150
leaq 0x10(%rsp), %rdi
callq 0x2150
jmp 0x2c49
jmp 0x2c46
movq %rax, %rbx
leaq 0x2b0(%rsp), %rdi
callq 0x20a0
movl $0x100, %r14d           # imm = 0x100
leaq (%rsp,%r14), %rdi
addq $0x70, %rdi
callq 0x2150
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x2c5c
movl $0x100, %r14d           # imm = 0x100
leaq (%rsp,%r14), %rdi
addq $0x190, %rdi            # imm = 0x190
callq 0x2150
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x2c79
movq %rbx, %rdi
callq 0x23e0