// File: /workspace/github_cpp_makefile_star2_O3/gvaumour[P]cache-simulator/src/Prefetcher.cc
// Function: SimplePrefetcher::getNextAddress(unsigned long)
vector<uint64_t>
SimplePrefetcher::getNextAddress(uint64_t current_address)
{

	uint64_t distance = INT64_MAX;
	int index = PAGE_SIZE;
	vector<uint64_t> prefetch_addresses;
	
	for(unsigned i = 0; i < m_prev_address.size() ; i++)
	{
		if( (current_address - m_prev_address[i].addr < distance) && m_prev_address[i].isValid)
		{
			index = i;
			distance = current_address - m_prev_address[i].addr;
		}
	}
	//DPRINTF("SimplePrefetcher::getNextAddress Distance %ld, index %d\n", distance , index);
	
	if( !(distance > 0 && distance < PAGE_SIZE) ) //No address within proper distances, create an entry 
	{
		//Select the older entry
		uint64_t age = INT64_MAX;
		int index_old = 0;
		bool done = false;
		for(unsigned i = 0 ; i < m_prev_address.size();i++)
		{
			if(!m_prev_address[i].isValid)
			{
				m_prev_address[i].addr = current_address;
				m_prev_address[i].isValid = true;
				m_prev_address[i].LRU_info = cpt_time;
				done = true;
			}
		}
		if(!done)
		{
		
			for(unsigned i = 0 ; i < m_prev_address.size();i++)
			{
				if(m_prev_address[i].LRU_info < age)
				{
					age = m_prev_address[i].LRU_info;
					index_old = i;
				}
			}
			m_prev_address[index_old].addr = current_address;
			m_prev_address[index_old].isValid = true;
			m_prev_address[index_old].LRU_info = cpt_time;
		}
		//DPRINTF("SimplePrefetcher::getNextAddress No Address Generated\n");
		return prefetch_addresses;
	}
	
	
	int stride = current_address - m_prev_address[index].addr;
	m_prev_address[index].addr = current_address;
	m_prev_address[index].LRU_info = cpt_time;

	if (stride != 0)
	{
		//DPRINTF("SimplePrefetcher::getNextAddress Generated prefecth For address %#lx\n", current_address);

		for(int i = 0; i < m_prefetchDegree; ++i)
		{
			 uint64_t prefetch_address = current_address + (i+1) * stride;
			 // But stay within the page if requested
			 if (!m_stop_at_page || ((prefetch_address & PAGE_MASK) == (current_address & PAGE_MASK)))
			    prefetch_addresses.push_back(prefetch_address);
			//DPRINTF("SimplePrefetcher::getNextAddress - %#lx\n",prefetch_address );
		}
		
	}

	return prefetch_addresses;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x18(%rsi), %rax
movq 0x20(%rsi), %rsi
movq %rsi, %rdx
subq %rax, %rdx
je 0x290ac
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x8(%rax), %r8
movl $0x1000, %edi           # imm = 0x1000
movl $0x1, %r10d
movq %rdx, %r9
movq %r14, %r11
subq -0x8(%r8), %r11
cmpq %r9, %r11
jae 0x28ffb
leal -0x1(%r10), %ebp
cmpb $0x0, (%r8)
cmovnel %ebp, %edi
cmovneq %r11, %r9
movl %r10d, %r11d
addq $0x18, %r8
incl %r10d
cmpq %r11, %rcx
ja 0x28fe0
decq %r9
cmpq $0xfff, %r9             # imm = 0xFFF
jae 0x290b3
movslq %edi, %rcx
leaq (%rcx,%rcx,2), %rdx
movq %r14, %rcx
subq (%rax,%rdx,8), %rcx
movq %r14, (%rax,%rdx,8)
leaq 0x1cee5(%rip), %rsi     # 0x45f18
movq (%rsi), %rsi
movq %rsi, 0x10(%rax,%rdx,8)
testl %ecx, %ecx
je 0x29151
movl 0x8(%r15), %eax
testl %eax, %eax
jle 0x29151
movslq %ecx, %r13
leaq (%r14,%r13), %rbp
xorl %r12d, %r12d
movq %rbp, (%rsp)
cmpb $0x1, 0x15(%r15)
jne 0x29073
movq %rbp, %rcx
xorq %r14, %rcx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x29099
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x2908a
movq %rbp, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x29099
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x1d796
movl 0x8(%r15), %eax
incq %r12
movslq %eax, %rcx
addq %r13, %rbp
cmpq %rcx, %r12
jl 0x29059
jmp 0x29151
xorl %edi, %edi
jmp 0x29135
xorl %edi, %edi
cmpq %rax, %rsi
je 0x29135
leaq 0x1ce57(%rip), %r8      # 0x45f18
xorl %r10d, %r10d
xorl %r9d, %r9d
incl %edi
leaq (%r10,%r10,2), %r10
cmpb $0x1, 0x8(%rax,%r10,8)
jne 0x290e1
movl %edi, %r10d
incl %edi
cmpq %r10, %rcx
ja 0x290c9
jmp 0x29101
leaq (%rax,%r10,8), %r9
movq %r14, (%r9)
movb $0x1, 0x8(%r9)
movq (%r8), %r10
movq %r10, 0x10(%r9)
movl %edi, %r10d
movb $0x1, %r9b
cmpq %r10, %rcx
ja 0x290c7
jmp 0x29151
testb $0x1, %r9b
jne 0x29151
xorl %edi, %edi
cmpq %rax, %rsi
je 0x29135
leaq 0x10(%rax), %rsi
xorl %r8d, %r8d
movq (%rsi), %r9
cmpq %rdx, %r9
cmovbq %r9, %rdx
cmovbl %r8d, %edi
incq %r8
movl %r8d, %r9d
addq $0x18, %rsi
cmpq %r9, %rcx
ja 0x29115
movslq %edi, %rdi
leaq (%rdi,%rdi,2), %rcx
movq %r14, (%rax,%rcx,8)
movb $0x1, 0x8(%rax,%rcx,8)
leaq 0x1cdcf(%rip), %rdx     # 0x45f18
movq (%rdx), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x29173
callq 0x52b0
movq %r14, %rdi
callq 0x55a0
nop