// File: /workspace/github_cpp_makefile_star2_O1/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), %rdx
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rcx
cmpq %rax, %rdx
je 0x29352
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x8(%rax), %r8
movl $0x1000, %edi           # imm = 0x1000
movl $0x1, %r9d
movq %r14, %r10
subq -0x8(%r8), %r10
cmpq %rsi, %r10
jae 0x29330
leal -0x1(%r9), %r11d
cmpb $0x0, (%r8)
cmovnel %r11d, %edi
cmovneq %r10, %rsi
movl %r9d, %r10d
addq $0x18, %r8
incl %r9d
cmpq %r10, %rcx
ja 0x29314
decq %rsi
cmpq $0xfff, %rsi            # imm = 0xFFF
setb %r8b
movslq %edi, %rsi
jmp 0x2935a
xorl %r8d, %r8d
movl $0x1000, %esi           # imm = 0x1000
testb %r8b, %r8b
je 0x293ed
leaq (%rsi,%rsi,2), %rdx
movq %r14, %rcx
subq (%rax,%rdx,8), %rcx
movq %r14, (%rax,%rdx,8)
leaq 0x1cb9f(%rip), %rsi     # 0x45f18
movq (%rsi), %rsi
movq %rsi, 0x10(%rax,%rdx,8)
testl %ecx, %ecx
je 0x29484
cmpl $0x0, 0x8(%r15)
jle 0x29484
movslq %ecx, %r13
leaq (%r14,%r13), %rbp
xorl %r12d, %r12d
movq %rbp, (%rsp)
cmpb $0x1, 0x15(%r15)
jne 0x293b7
movq %rbp, %rax
xorq %r14, %rax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x293d9
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x293ce
movq %rbp, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x293d9
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x1d8c4
incq %r12
movslq 0x8(%r15), %rax
addq %r13, %rbp
cmpq %rax, %r12
jl 0x2939e
jmp 0x29484
cmpq %rax, %rdx
je 0x29430
leaq 0x10(%rax), %rsi
movl $0x1, %edi
xorl %r9d, %r9d
leaq 0x1cb13(%rip), %r8      # 0x45f18
cmpb $0x0, -0x8(%rsi)
jne 0x2941c
movq %r14, -0x10(%rsi)
movb $0x1, -0x8(%rsi)
movq (%r8), %r9
movq %r9, (%rsi)
movb $0x1, %r9b
movl %edi, %r10d
addq $0x18, %rsi
incl %edi
cmpq %r10, %rcx
ja 0x29405
testb $0x1, %r9b
jne 0x29484
xorl %esi, %esi
cmpq %rax, %rdx
je 0x29468
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x10(%rax), %rdi
xorl %r8d, %r8d
movq (%rdi), %r9
cmpq %rdx, %r9
cmovbq %r9, %rdx
cmovbl %r8d, %esi
incq %r8
movl %r8d, %r9d
addq $0x18, %rdi
cmpq %r9, %rcx
ja 0x29448
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rcx
movq %r14, (%rax,%rcx,8)
movb $0x1, 0x8(%rax,%rcx,8)
leaq 0x1ca9c(%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 0x294a6
callq 0x52a0
movq %r14, %rdi
callq 0x55a0