// File: dudulu233[P]EMA-Tool/cache_stat.cpp
// Function: StatCache::main_operation(_IoRecord*, bool, int, int, int)
void StatCache::main_operation(IoRecord* ir, bool get_reuse_dis, int cache_size, int sp, int st)
{
	struct SeqNum* sn_new = NULL;
	struct SeqNum* sn_old = NULL;
	if (is_new_day(ir))

	{
		sn_new = (struct SeqNum*)malloc(sizeof(struct SeqNum));
		sn_new->start_total_seq = total_seq;
		sn_new->re_seq = 0;
		sn_new->start_time = ir->alloc_time;
		sn_new->a = (double*)malloc(2 * sizeof(double));
		sn_new->b = (double*)malloc(2 * sizeof(double));
		sn_new->re_retio = (double*)malloc(3 * sizeof(double));
		sn_new->a[0] = sn_new->a[1] = sn_new->b[0] = sn_new->b[1] =
			sn_new->re_retio[0] = sn_new->re_retio[1] = sn_new->re_retio[2] = 0.0;

		seq_map[current_day] = sn_new;

		if (current_day - get_time_day(start_time) > 1) {
			//plot RAR curve of 2 days ago
			struct SeqNum* sn_tmp = seq_map[current_day - 2];
			assert(sn_tmp);
			plot_rar_curve(sn_tmp);
		}
	}
	else
	{
		sn_new = seq_map[current_day];
	}
	if (current_day - get_time_day(start_time) > 0)
	{
		sn_old = seq_map[current_day - 1];
		assert(sn_old);
	}
	assert(sn_new);

	if (is_new_hour(ir)) {
		if (current_hour - get_time_hour(sn_new->start_time) == 1) {
			assert(total_seq - sn_new->start_total_seq != 0);
			sn_new->re_retio[0] = (double)(sn_new->re_seq) / (total_seq - sn_new->start_total_seq);
		}
		if (current_hour - get_time_hour(sn_new->start_time) == 20) {
			assert(total_seq - sn_new->start_total_seq != 0);
			sn_new->re_retio[1] = (double)(sn_new->re_seq) / (total_seq - sn_new->start_total_seq);
		}
		if (NULL != sn_old) {
			if (current_hour - get_time_hour(sn_old->start_time) == 47) {
				assert(total_seq - sn_old->start_total_seq != 0);
				sn_old->re_retio[2] = (double)(sn_old->re_seq) / (total_seq - sn_old->start_total_seq);
			}
		}
	}

	total_seq++;

	auto it = stat_map.find(ir->cache_addr);
	struct BlockStat* bs;
	int reuse_dis = -1;

	if (it == stat_map.end())
	{
		bs = (struct BlockStat*)malloc(sizeof(struct BlockStat));
		bs->last_time = ir->alloc_time;
		bs->last_total_seq = total_seq;
		stat_map[ir->cache_addr] = bs;
		++map_size;
		reuse_dis = 1023;
	}
	else
	{
		bs = it->second;
		re_seq++;
		if (current_day == get_time_day(bs->last_time)) {
			sn_new->re_seq++;
		}
		if ((current_day - get_time_day(bs->last_time) <= 1) && (NULL != sn_old)) {
			sn_old->re_seq++;
		}
		long x = (ir->alloc_time - bs->last_time);
		double re_ratio = get_reuse_curve(x, ir->alloc_time);
		double reuse_tmp = (total_seq - bs->last_total_seq) *
			(1 - re_ratio);
		reuse_dis = (int)(reuse_tmp * sp * cache_size / st / 1024 / 1024);
		if (reuse_dis >= 1024)
		{
			reuse_dis = 1023;
		}
		if (reuse_dis <= 0)
		{
			reuse_dis = 0;
		}
		bs->last_time = ir->alloc_time;
		bs->last_total_seq = total_seq;
	}
	if (get_reuse_dis) {
		reuse_dis_array[reuse_dis]++;
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x88(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rsi
callq 0x6410
testb $0x1, %al
jne 0x5c52
jmp 0x5dfd
movl $0x30, %edi
callq 0x3380
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x10, %edi
callq 0x3380
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x10, %edi
callq 0x3380
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x18, %edi
callq 0x3380
movq -0x88(%rbp), %rsi
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movq %rsi, %rdi
addq $0x2068, %rdi           # imm = 0x2068
addq $0x20, %rsi
callq 0x6af0
movq -0x98(%rbp), %rcx
movq -0x88(%rbp), %rdi
movq %rcx, (%rax)
movq 0x20(%rdi), %rax
movq %rax, -0x90(%rbp)
movq 0x28(%rdi), %rsi
callq 0x6490
movq %rax, %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
cmpq $0x1, %rax
jle 0x5dfb
movq -0x88(%rbp), %rax
movq %rax, %rdi
addq $0x2068, %rdi           # imm = 0x2068
movq 0x20(%rax), %rax
subq $0x2, %rax
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x6b20
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x5dcc
jmp 0x5deb
leaq 0x65f6(%rip), %rdi      # 0xc3c9
leaq 0x65c5(%rip), %rsi      # 0xc39f
movl $0x31, %edx
leaq 0x65ea(%rip), %rcx      # 0xc3d0
callq 0x31c0
movq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x64c0
jmp 0x5e1e
movq -0x88(%rbp), %rsi
movq %rsi, %rdi
addq $0x2068, %rdi           # imm = 0x2068
addq $0x20, %rsi
callq 0x6af0
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x88(%rbp), %rdi
movq 0x20(%rdi), %rax
movq %rax, -0xa0(%rbp)
movq 0x28(%rdi), %rsi
callq 0x6490
movq %rax, %rcx
movq -0xa0(%rbp), %rax
subq %rcx, %rax
cmpq $0x0, %rax
jle 0x5ea3
movq -0x88(%rbp), %rax
movq %rax, %rdi
addq $0x2068, %rdi           # imm = 0x2068
movq 0x20(%rax), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x6b20
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x5e82
jmp 0x5ea1
leaq 0x6587(%rip), %rdi      # 0xc410
leaq 0x650f(%rip), %rsi      # 0xc39f
movl $0x3c, %edx
leaq 0x6534(%rip), %rcx      # 0xc3d0
callq 0x31c0
jmp 0x5ea3
cmpq $0x0, -0x28(%rbp)
je 0x5eac
jmp 0x5ecb
leaq 0x6564(%rip), %rdi      # 0xc417
leaq 0x64e5(%rip), %rsi      # 0xc39f
movl $0x3e, %edx
leaq 0x650a(%rip), %rcx      # 0xc3d0
callq 0x31c0
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6620
testb $0x1, %al
jne 0x5ee4
jmp 0x616f
movq -0x88(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x66a0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
subq %rcx, %rax
cmpq $0x1, %rax
jne 0x5fb7
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
subq 0x20(%rcx), %rax
cmpq $0x0, %rax
je 0x5f34
jmp 0x5f53
leaq 0x64e3(%rip), %rdi      # 0xc41e
leaq 0x645d(%rip), %rsi      # 0xc39f
movl $0x42, %edx
leaq 0x6482(%rip), %rcx      # 0xc3d0
callq 0x31c0
movq -0x88(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x28(%rcx), %xmm0
movaps 0x60b6(%rip), %xmm3     # 0xc020
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x60ba(%rip), %xmm2     # 0xc030
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq (%rax), %rax
movq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movsd %xmm0, (%rax)
movq -0x88(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x66a0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
subq %rcx, %rax
cmpq $0x14, %rax
jne 0x608b
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
subq 0x20(%rcx), %rax
cmpq $0x0, %rax
je 0x6007
jmp 0x6026
leaq 0x6410(%rip), %rdi      # 0xc41e
leaq 0x638a(%rip), %rsi      # 0xc39f
movl $0x46, %edx
leaq 0x63af(%rip), %rcx      # 0xc3d0
callq 0x31c0
movq -0x88(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x28(%rcx), %xmm0
movaps 0x5fe3(%rip), %xmm3     # 0xc020
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x5fe7(%rip), %xmm2     # 0xc030
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq (%rax), %rax
movq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movsd %xmm0, 0x8(%rax)
xorl %eax, %eax
cmpq -0x30(%rbp), %rax
je 0x616d
movq -0x88(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x66a0
movq %rax, %rcx
movq -0xb8(%rbp), %rax
subq %rcx, %rax
cmpq $0x2f, %rax
jne 0x616b
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
subq 0x20(%rcx), %rax
cmpq $0x0, %rax
je 0x60e7
jmp 0x6106
leaq 0x6359(%rip), %rdi      # 0xc447
leaq 0x62aa(%rip), %rsi      # 0xc39f
movl $0x4b, %edx
leaq 0x62cf(%rip), %rcx      # 0xc3d0
callq 0x31c0
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %xmm0
movaps 0x5f03(%rip), %xmm3     # 0xc020
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x5f07(%rip), %xmm2     # 0xc030
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq (%rax), %rax
movq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x616d
jmp 0x616f
movq -0x88(%rbp), %rdi
movq (%rdi), %rax
addq $0x1, %rax
movq %rax, (%rdi)
addq $0x2030, %rdi           # imm = 0x2030
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x6b50
movq -0x88(%rbp), %rdi
movq %rax, -0x50(%rbp)
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
addq $0x2030, %rdi           # imm = 0x2030
callq 0x6bb0
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x6b80
testb $0x1, %al
jne 0x61c9
jmp 0x6242
movl $0x10, %edi
callq 0x3380
movq -0x88(%rbp), %rdi
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq (%rdi), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
movq %rax, -0xc0(%rbp)
addq $0x2030, %rdi           # imm = 0x2030
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x6be0
movq -0xc0(%rbp), %rdx
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rdx, (%rcx)
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movl $0x3ff, -0x5c(%rbp)     # imm = 0x3FF
jmp 0x63e4
leaq -0x50(%rbp), %rdi
callq 0x6c10
movq -0x88(%rbp), %rdi
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq 0x8(%rdi), %rax
addq $0x1, %rax
movq %rax, 0x8(%rdi)
movq 0x20(%rdi), %rax
movq %rax, -0xc8(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rsi
callq 0x6490
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jne 0x629c
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x88(%rbp), %rdi
movq 0x20(%rdi), %rax
movq %rax, -0xd0(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rsi
callq 0x6490
movq %rax, %rcx
movq -0xd0(%rbp), %rax
subq %rcx, %rax
cmpq $0x1, %rax
jg 0x62e5
xorl %eax, %eax
cmpq -0x30(%rbp), %rax
je 0x62e5
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
callq 0x66c0
movq -0x88(%rbp), %rax
movsd %xmm0, -0x78(%rbp)
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movq %rax, %xmm0
movaps 0x5ce9(%rip), %xmm1     # 0xc020
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x5ced(%rip), %xmm1     # 0xc030
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x601e(%rip), %xmm1     # 0xc378
subsd -0x78(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x5fe0(%rip), %xmm1     # 0xc370
divsd %xmm1, %xmm0
movsd 0x5fd4(%rip), %xmm1     # 0xc370
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x400, -0x5c(%rbp)     # imm = 0x400
jl 0x63b7
movl $0x3ff, -0x5c(%rbp)     # imm = 0x3FF
cmpl $0x0, -0x5c(%rbp)
jg 0x63c4
movl $0x0, -0x5c(%rbp)
movq -0x88(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rdx
movq -0x58(%rbp), %rcx
movq %rdx, (%rcx)
movq (%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
testb $0x1, -0x11(%rbp)
je 0x6403
movq -0x88(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq 0x30(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x30(%rax,%rcx,8)
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)