// File: dramninjasUMD[P]BOBSim/RandomStreamSim.cpp
// Function: FillTransactionBuffer(int)
void FillTransactionBuffer(int port)
{
	useCounters[port] = 0;

	//Fill with a random number of requests between 2 and 5 or 6
	unsigned randomCount = rand_r(&randomSeeds[port]) % 5 + 2;
	//cout<<"port "<<port<<" - random count is "<<randomCount<<endl;
	for(unsigned i=0; i<randomCount; i++)
	{
		unsigned long temp = rand();
		unsigned long physicalAddress = (temp<<32)|rand_r(&randomSeeds[port]);
		Transaction *newTrans;
		
		if(physicalAddress%1000<READ_WRITE_RATIO*10)
		{
			newTrans = new Transaction(DATA_READ,TRANSACTION_SIZE,physicalAddress);
			//cout<<*newTrans<<endl;
			useCounters[port]+= RD_REQUEST_PACKET_OVERHEAD/PORT_WIDTH+!!(RD_REQUEST_PACKET_OVERHEAD%PORT_WIDTH);
		}
		else
		{
			newTrans = new Transaction(DATA_WRITE,TRANSACTION_SIZE,physicalAddress);
			//cout<<*newTrans<<endl;
			useCounters[port]+=(WR_REQUEST_PACKET_OVERHEAD + TRANSACTION_SIZE)/PORT_WIDTH +
				!!((WR_REQUEST_PACKET_OVERHEAD + TRANSACTION_SIZE)%PORT_WIDTH);
		}
		
		transactionBuffer[port].push_back(newTrans);
	}
	
	//cout<<"use for "<<port<<" is "<<useCounters[port]<<endl;
	//figure out how much idle time between requests
	waitCounters[port] = ceil(useCounters[port]/PORT_UTILIZATION) - useCounters[port];
	//cout<<"wait for "<<port<<" is "<<waitCounters[port]<<endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movslq -0x4(%rbp), %rsi
leaq 0x1878f(%rip), %rdi     # 0x387f8
callq 0xbec0
movl $0x0, (%rax)
movslq -0x4(%rbp), %rsi
leaq 0x18749(%rip), %rdi     # 0x387c8
callq 0xbec0
movq %rax, %rdi
callq 0x31e0
movl $0x5, %ecx
cltd
idivl %ecx
addl $0x2, %edx
movl %edx, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x202b0
callq 0x3080
cltq
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x48(%rbp)
movslq -0x4(%rbp), %rsi
leaq 0x186f9(%rip), %rdi     # 0x387c8
callq 0xbec0
movq %rax, %rdi
callq 0x31e0
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq -0x40(%rbp), %rax
shrq $0x4, %rdx
movl %edx, %ecx
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
cvtsi2ss %eax, %xmm1
movss 0xbc49(%rip), %xmm0     # 0x2bd6c
mulss 0x18255(%rip), %xmm0    # 0x38380
ucomiss %xmm1, %xmm0
jbe 0x201d7
movl $0x90, %edi
callq 0x31d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movl 0x18236(%rip), %edx     # 0x38384
movq -0x20(%rbp), %rcx
xorl %esi, %esi
callq 0x293f0
jmp 0x2015b
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movl 0x1821f(%rip), %eax     # 0x38388
xorl %edx, %edx
divl 0x1821b(%rip)           # 0x3838c
movl %eax, -0x58(%rbp)
movl 0x1820e(%rip), %eax     # 0x38388
xorl %edx, %edx
divl 0x1820a(%rip)           # 0x3838c
movl -0x58(%rbp), %eax
cmpl $0x0, %edx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
movl %eax, -0x54(%rbp)
movslq -0x4(%rbp), %rsi
leaq 0x18651(%rip), %rdi     # 0x387f8
callq 0xbec0
movl -0x54(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x20286
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x90, %esi
callq 0x31f0
jmp 0x20324
movl $0x90, %edi
callq 0x31d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movl 0x18193(%rip), %edx     # 0x38384
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x293f0
jmp 0x20201
movq -0x60(%rbp), %rax
movq %rax, -0x28(%rbp)
movl 0x18181(%rip), %eax     # 0x38390
addl 0x1816f(%rip), %eax     # 0x38384
xorl %edx, %edx
divl 0x1816f(%rip)           # 0x3838c
movl %eax, -0x68(%rbp)
movl 0x1816a(%rip), %eax     # 0x38390
addl 0x18158(%rip), %eax     # 0x38384
xorl %edx, %edx
divl 0x18158(%rip)           # 0x3838c
movl -0x68(%rbp), %eax
cmpl $0x0, %edx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
movl %eax, -0x64(%rbp)
movslq -0x4(%rbp), %rsi
leaq 0x1859f(%rip), %rdi     # 0x387f8
callq 0xbec0
movl -0x64(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x20286
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x90, %esi
callq 0x31f0
jmp 0x20324
movslq -0x4(%rbp), %rsi
leaq 0x1857f(%rip), %rdi     # 0x38810
callq 0x20d10
movq %rax, %rdi
leaq -0x28(%rbp), %rsi
callq 0xbfd0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x200a1
movslq -0x4(%rbp), %rsi
leaq 0x1853d(%rip), %rdi     # 0x387f8
movq %rdi, -0x78(%rbp)
callq 0xbec0
movl (%rax), %eax
cvtsi2ss %rax, %xmm0
movss 0x180c1(%rip), %xmm1    # 0x38394
divss %xmm1, %xmm0
callq 0x20d30
movq -0x78(%rbp), %rdi
movss %xmm0, -0x70(%rbp)
movslq -0x4(%rbp), %rsi
callq 0xbec0
movss -0x70(%rbp), %xmm0
movl (%rax), %eax
cvtsi2ss %rax, %xmm1
subss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x6c(%rbp)
movslq -0x4(%rbp), %rsi
leaq 0x184cf(%rip), %rdi     # 0x387e0
callq 0xbec0
movl -0x6c(%rbp), %ecx
movl %ecx, (%rax)
addq $0x80, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x33a0
nopl (%rax)