// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, 0x8(%rsp)
movl %r8d, (%rsp)
movl %ecx, 0x4(%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %r13
leaq 0x7080(%r13), %rax
movabsq $0x1845c8a0ce512957, %rcx # imm = 0x1845C8A0CE512957
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xd, %rdx
addq %rax, %rdx
leaq 0x20(%rdi), %r12
cmpq 0x20(%rdi), %rdx
jle 0x3fbd
movq %rdx, 0x20(%rbx)
movl $0x30, %edi
callq 0x2320
movq %rax, %r15
movq (%rbx), %rax
movq %rax, 0x20(%r15)
movq $0x0, 0x28(%r15)
movq %r13, 0x18(%r15)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2160
movq %rax, (%r15)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2160
movq %rax, 0x8(%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x2160
movq %rax, 0x10(%r15)
leaq 0x2068(%rbx), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x4816
movq %r15, (%rax)
movl $0x7080, %eax           # imm = 0x7080
addq 0x28(%rbx), %rax
movabsq $-0x1845c8a0ce512957, %rcx # imm = 0xE7BA375F31AED6A9
imulq %rcx
movq 0x20(%rbx), %rax
movq %rdx, %rcx
shrq $0x3f, %rcx
sarq $0xd, %rdx
addq %rcx, %rdx
addq %rax, %rdx
cmpq $0x2, %rdx
jl 0x3fd7
addq $-0x2, %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %r13, %rdi
callq 0x4ab8
movq (%rax), %rax
testq %rax, %rax
je 0x4382
movq (%rax), %rcx
movq 0x10(%rax), %rdx
movsd (%rdx), %xmm0
movsd 0x8(%rdx), %xmm1
movhpd 0x10(%rdx), %xmm0       # xmm0 = xmm0[0],mem[0]
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm0, %xmm2
subpd %xmm1, %xmm2
movapd 0x23ed(%rip), %xmm3     # 0x6380
divpd %xmm3, %xmm2
movupd %xmm2, (%rcx)
mulpd 0x23ed(%rip), %xmm0     # 0x6390
mulpd 0x23f5(%rip), %xmm1     # 0x63a0
movq 0x8(%rax), %rax
addpd %xmm0, %xmm1
divpd %xmm3, %xmm1
movupd %xmm1, (%rax)
jmp 0x3fd7
jge 0x3fc5
incq %r13
movq %r13, (%r14)
leaq 0x2068(%rbx), %rdi
movq %r12, %rsi
callq 0x4816
movq (%rax), %r15
movl %ebp, 0xc(%rsp)
movq 0x20(%rbx), %rcx
movl $0x7080, %eax           # imm = 0x7080
addq 0x28(%rbx), %rax
movabsq $0x1845c8a0ce512957, %rdx # imm = 0x1845C8A0CE512957
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xd, %rdx
addq %rax, %rdx
cmpq %rdx, %rcx
jle 0x4046
leaq 0x2068(%rbx), %rdi
decq %rcx
leaq 0x10(%rsp), %rsi
movq %rcx, (%rsi)
callq 0x4ab8
movq (%rax), %rbp
testq %rbp, %rbp
jne 0x4048
leaq 0x2427(%rip), %rdi      # 0x6455
leaq 0x23af(%rip), %rsi      # 0x63e4
leaq 0x23d9(%rip), %rcx      # 0x6415
movl $0x3c, %edx
callq 0x2150
xorl %ebp, %ebp
testq %r15, %r15
je 0x4363
movq (%r14), %rsi
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
movq %rsi, %rax
imulq %rcx
movq %rdx, %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rcx
addq %rax, %rcx
cmpq 0x18(%rbx), %rcx
jle 0x410f
movq %rcx, 0x18(%rbx)
movabsq $-0x48d159e26af37c05, %rax # imm = 0xB72EA61D950C83FB
imulq 0x18(%r15)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
addq %rcx, %rdx
cmpq $0x14, %rdx
je 0x4120
cmpq $0x1, %rdx
jne 0x417c
movq (%rbx), %rax
subq 0x20(%r15), %rax
je 0x43a1
movsd 0x28(%r15), %xmm0
movsd 0x1f43(%rip), %xmm1     # 0x6010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1f48(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movq 0x10(%r15), %rax
movsd %xmm3, (%rax)
jmp 0x417c
jge 0x4205
incq %rsi
movq %rsi, (%r14)
jmp 0x4205
movq (%rbx), %rax
subq 0x20(%r15), %rax
je 0x43c0
movsd 0x28(%r15), %xmm0
movsd 0x1ed5(%rip), %xmm1     # 0x6010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1eda(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movq 0x10(%r15), %rax
movsd %xmm3, 0x8(%rax)
testq %rbp, %rbp
je 0x4205
movabsq $-0x48d159e26af37c05, %rax # imm = 0xB72EA61D950C83FB
imulq 0x18(%rbp)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
addq %rcx, %rdx
cmpq $0x2f, %rdx
jne 0x4205
movq (%rbx), %rax
subq 0x20(%rbp), %rax
je 0x43df
movsd 0x28(%rbp), %xmm0
movsd 0x1e4c(%rip), %xmm1     # 0x6010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1e51(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movq 0x10(%rbp), %rax
movsd %xmm3, 0x10(%rax)
incq (%rbx)
leaq 0x2030(%rbx), %r12
leaq 0x8(%r14), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x4b6c
testq %rax, %rax
je 0x4317
movq 0x10(%rax), %r12
incq 0x8(%rbx)
movq 0x20(%rbx), %rsi
movq (%r12), %rcx
leaq 0x7080(%rcx), %rax
movabsq $0x1845c8a0ce512957, %rdx # imm = 0x1845C8A0CE512957
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xd, %rdx
addq %rax, %rdx
subq %rdx, %rsi
jne 0x4262
incq 0x28(%r15)
testq %rbp, %rbp
je 0x4271
cmpq $0x1, %rsi
jg 0x4271
incq 0x28(%rbp)
movq (%r14), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rbx, %rdi
callq 0x4518
movq (%rbx), %rax
movq %rax, %rcx
subq 0x8(%r12), %rcx
movq %rcx, %xmm1
punpckldq 0x1d76(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1d7e(%rip), %xmm1     # 0x6020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
movsd 0x20fa(%rip), %xmm1     # 0x63b0
subsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl (%rsp), %xmm0
mulsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%rsp), %xmm1
mulsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd 0x20cc(%rip), %xmm0     # 0x63b8
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movl $0x3ff, %edx            # imm = 0x3FF
cmpl %edx, %ecx
cmovll %ecx, %edx
xorl %ecx, %ecx
testl %edx, %edx
cmovgl %edx, %ecx
movq (%r14), %rdx
movq %rdx, (%r12)
movq %rax, 0x8(%r12)
jmp 0x4348
movl $0x10, %edi
callq 0x2320
movq %rax, %r15
movq (%r14), %rax
movq %rax, (%r15)
movq (%rbx), %rax
movq %rax, 0x8(%r15)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4bc8
movq %r15, (%rax)
incq 0x10(%rbx)
movl $0x3ff, %ecx            # imm = 0x3FF
cmpb $0x0, 0xc(%rsp)
je 0x4354
incq 0x30(%rbx,%rcx,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20f2(%rip), %rdi      # 0x645c
leaq 0x2073(%rip), %rsi      # 0x63e4
leaq 0x209d(%rip), %rcx      # 0x6415
movl $0x3e, %edx
callq 0x2150
leaq 0x2085(%rip), %rdi      # 0x640e
leaq 0x2054(%rip), %rsi      # 0x63e4
leaq 0x207e(%rip), %rcx      # 0x6415
movl $0x31, %edx
callq 0x2150
leaq 0x20bb(%rip), %rdi      # 0x6463
leaq 0x2035(%rip), %rsi      # 0x63e4
leaq 0x205f(%rip), %rcx      # 0x6415
movl $0x42, %edx
callq 0x2150
leaq 0x209c(%rip), %rdi      # 0x6463
leaq 0x2016(%rip), %rsi      # 0x63e4
leaq 0x2040(%rip), %rcx      # 0x6415
movl $0x46, %edx
callq 0x2150
leaq 0x20a6(%rip), %rdi      # 0x648c
leaq 0x1ff7(%rip), %rsi      # 0x63e4
leaq 0x2021(%rip), %rcx      # 0x6415
movl $0x4b, %edx
callq 0x2150