// File: /workspace/github_cpp_makefile_star2_O2/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
callq 0x3c94
testb %al, %al
je 0x38f3
pushq $0x30
popq %rdi
callq 0x2320
movq %rax, %r15
movq (%rbx), %rax
movq %rax, 0x20(%r15)
andq $0x0, 0x28(%r15)
movq (%r14), %rax
movq %rax, 0x18(%r15)
pushq $0x1
popq %r12
pushq $0x10
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x21a0
movq %rax, (%r15)
movq %r12, %rdi
movq %r13, %rsi
callq 0x21a0
movq %rax, 0x8(%r15)
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0x21a0
movq %rax, 0x10(%r15)
leaq 0x2068(%rbx), %r12
leaq 0x20(%rbx), %rsi
movq %r12, %rdi
callq 0x4076
movq %r15, (%rax)
movl $0x7080, %eax           # imm = 0x7080
addq 0x28(%rbx), %rax
movq 0x20(%rbx), %rcx
movq $-0x15180, %rsi         # imm = 0xFFFEAE80
cqto
idivq %rsi
addq %rcx, %rax
cmpq $0x2, %rax
jl 0x3906
addq $-0x2, %rcx
leaq 0x10(%rsp), %rsi
movq %rcx, (%rsi)
movq %r12, %rdi
callq 0x43ea
movq (%rax), %rsi
testq %rsi, %rsi
je 0x3c2c
callq 0x3cda
jmp 0x3906
leaq 0x2068(%rbx), %rdi
leaq 0x20(%rbx), %rsi
callq 0x4076
movq (%rax), %r15
movl %ebp, 0xc(%rsp)
movq 0x20(%rbx), %rcx
movl $0x7080, %eax           # imm = 0x7080
addq 0x28(%rbx), %rax
movl $0x15180, %esi          # imm = 0x15180
cqto
idivq %rsi
cmpq %rax, %rcx
jle 0x3961
leaq 0x2068(%rbx), %rdi
decq %rcx
leaq 0x10(%rsp), %rsi
movq %rcx, (%rsi)
callq 0x43ea
movq (%rax), %rbp
testq %rbp, %rbp
jne 0x3963
leaq 0x1ad9(%rip), %rdi      # 0x5425
leaq 0x1a61(%rip), %rsi      # 0x53b4
leaq 0x1a8b(%rip), %rcx      # 0x53e5
pushq $0x3c
jmp 0x3c8e
xorl %ebp, %ebp
testq %r15, %r15
je 0x3c13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3d2c
testb %al, %al
je 0x3ad1
movq 0x18(%rbx), %rcx
movq 0x18(%r15), %rax
movq $-0xe10, %rsi           # imm = 0xF1F0
cqto
idivq %rsi
addq %rcx, %rax
cmpq $0x14, %rax
je 0x3a03
cmpq $0x1, %rax
jne 0x3a5f
movq (%rbx), %rax
subq 0x20(%r15), %rax
je 0x3c45
movsd 0x28(%r15), %xmm0
movsd 0x164f(%rip), %xmm1     # 0x5010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1654(%rip), %xmm2     # 0x5020
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 0x3a5f
movq (%rbx), %rax
subq 0x20(%r15), %rax
je 0x3c5e
movsd 0x28(%r15), %xmm0
movsd 0x15f2(%rip), %xmm1     # 0x5010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x15f7(%rip), %xmm2     # 0x5020
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 0x3ad1
movq 0x18(%rbp), %rax
cqto
idivq %rsi
addq %rcx, %rax
cmpq $0x2f, %rax
jne 0x3ad1
movq (%rbx), %rax
subq 0x20(%rbp), %rax
je 0x3c77
movsd 0x28(%rbp), %xmm0
movsd 0x1580(%rip), %xmm1     # 0x5010
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1585(%rip), %xmm2     # 0x5020
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 0x44bc
testq %rax, %rax
je 0x3bc9
movq 0x10(%rax), %r12
incq 0x8(%rbx)
movq 0x20(%rbx), %rsi
movq (%r12), %rcx
leaq 0x7080(%rcx), %rax
movl $0x15180, %edi          # imm = 0x15180
cqto
idivq %rdi
subq %rax, %rsi
jne 0x3b1d
incq 0x28(%r15)
testq %rbp, %rbp
je 0x3b2c
cmpq $0x1, %rsi
jg 0x3b2c
incq 0x28(%rbp)
movq (%r14), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rbx, %rdi
callq 0x3d6a
movq (%rbx), %rax
movq %rax, %rcx
subq 0x8(%r12), %rcx
movq %rcx, %xmm1
punpckldq 0x14bb(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x14c3(%rip), %xmm1     # 0x5020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
movsd 0x180f(%rip), %xmm1     # 0x5380
subsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
cvtsi2sdl (%rsp), %xmm0
mulsd %xmm1, %xmm0
cvtsi2sdl 0x4(%rsp), %xmm1
mulsd %xmm0, %xmm1
cvtsi2sdl 0x8(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd 0x17ea(%rip), %xmm0     # 0x5388
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 0x3bf8
pushq $0x10
popq %rdi
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 0x4538
movq %r15, (%rax)
incq 0x10(%rbx)
movl $0x3ff, %ecx            # imm = 0x3FF
cmpb $0x0, 0xc(%rsp)
je 0x3c04
incq 0x30(%rbx,%rcx,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1812(%rip), %rdi      # 0x542c
leaq 0x1793(%rip), %rsi      # 0x53b4
leaq 0x17bd(%rip), %rcx      # 0x53e5
pushq $0x3e
jmp 0x3c8e
leaq 0x17ab(%rip), %rdi      # 0x53de
leaq 0x177a(%rip), %rsi      # 0x53b4
leaq 0x17a4(%rip), %rcx      # 0x53e5
pushq $0x31
jmp 0x3c8e
leaq 0x17e7(%rip), %rdi      # 0x5433
leaq 0x1761(%rip), %rsi      # 0x53b4
leaq 0x178b(%rip), %rcx      # 0x53e5
pushq $0x42
jmp 0x3c8e
leaq 0x17ce(%rip), %rdi      # 0x5433
leaq 0x1748(%rip), %rsi      # 0x53b4
leaq 0x1772(%rip), %rcx      # 0x53e5
pushq $0x46
jmp 0x3c8e
leaq 0x17de(%rip), %rdi      # 0x545c
leaq 0x172f(%rip), %rsi      # 0x53b4
leaq 0x1759(%rip), %rcx      # 0x53e5
pushq $0x4b
popq %rdx
callq 0x2190