// File: /workspace/github_cpp_makefile_star2_O3/dramninjasUMD[P]BOBSim/SimpleController.cpp
// Function: BOBSim::SimpleController::Update()
void SimpleController::Update()
{
	//
	//Stats
	//
	unsigned currentCount = 0;
	//count all the ACTIVATES waiting in the queue
	for(unsigned i=0; i<commandQueue.size(); i++)
	{
		if(commandQueue[i]->busPacketType==ACTIVATE)
		{
			currentCount++;
		}
	}
	if(currentCount>commandQueueMax) commandQueueMax = currentCount;

	//cumulative rolling average
	commandQueueAverage += currentCount;

	//count the number of idle banks
	unsigned num=0;
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned b=0; b<NUM_BANKS; b++)
		{
			if(bankStates[r][b].currentBankState==IDLE)
			{
				num++;
			}
		}
	}
	numIdleBanksAverage += num;

	//count the number of active banks
	unsigned numActive=0;
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned b=0; b<NUM_BANKS; b++)
		{
			if(bankStates[r][b].currentBankState==ROW_ACTIVE)
			{
				numActive++;
			}
		}
	}
	numActBanksAverage += numActive;

	//count the number of precharging banks
	unsigned numPre=0;
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned b=0; b<NUM_BANKS; b++)
		{
			if(bankStates[r][b].currentBankState==PRECHARGING)
			{
				numPre++;
			}
		}
	}
	numPreBanksAverage += numPre;

	//count the number of refreshing banks
	unsigned numRef=0;
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned b=0; b<NUM_BANKS; b++)
		{
			if(bankStates[r][b].currentBankState==REFRESHING)
			{
				numRef++;
			}
		}
	}
	numRefBanksAverage += numRef;

	//
	//Power
	//
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		bool bankOpen = false;
		for(unsigned b=0; b<NUM_RANKS; b++)
		{
			if(bankStates[r][b].currentBankState == ROW_ACTIVE ||
			        bankStates[r][b].currentBankState == REFRESHING)
			{
				bankOpen = true;
				break;
			}
		}

		if(bankOpen)
		{
			//DRAM_BUS_WIDTH/2 because value accounts for DDR
			backgroundEnergy[r] += IDD3N * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);
		}
		else
		{
			//DRAM_BUS_WIDTH/2 because value accounts for DDR
			idd2nCount[r]++;
			backgroundEnergy[r] += IDD2N * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);
		}
	}


	//
	//Update
	//
	//Updates the sliding window for tFAW
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned i=0; i<tFAWWindow[r].size(); i++)
		{
			tFAWWindow[r][i]--;
			if(tFAWWindow[r][i]==0) tFAWWindow[r].erase(tFAWWindow[r].begin());
		}
	}

	//Updates the bank states for each rank
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		for(unsigned b=0; b<NUM_BANKS; b++)
		{
			bankStates[r][b].UpdateStateChange();
		}
	}

	//Handle refresh counters
	for(unsigned i=0; i<NUM_RANKS; i++)
	{
		if(refreshCounters[i]>0)
		{
			refreshCounters[i]--;
		}
	}

	//Send write data to data bus
	for(unsigned i=0; i<writeBurstCountdown.size(); i++)
	{
		writeBurstCountdown[i]--;
	}
	if(writeBurstCountdown.size()>0&&writeBurstCountdown[0]==0)
	{
		if(DEBUG_CHANNEL) DEBUG("     == Sending Write Data : "<<*writeBurstQueue[0]);
		(*DataCallback)(writeBurstQueue[0],0);
		writeBurstQueue.erase(writeBurstQueue.begin());
		writeBurstCountdown.erase(writeBurstCountdown.begin());
	}

	bool issuingRefresh = false;
	bool canIssueRefresh = true;

	//Figure out if everyone who needs a refresh can actually receive one
	for(unsigned r=0; r<NUM_RANKS; r++)
	{
		if(refreshCounters[r]==0)
		{
			if(DEBUG_CHANNEL) DEBUG("      !! -- Rank "<<r<<" needs refresh");
			//Check to be sure we can issue a refresh
			for(unsigned b=0; b<NUM_BANKS; b++)
			{
				if(bankStates[r][b].nextActivate > currentClockCycle ||
				        bankStates[r][b].currentBankState != IDLE)
				{
					canIssueRefresh = false;

				}
			}

			//Once all counters have reached 0 and everyone is either idle or ready to accept refresh-CAS
			if(canIssueRefresh)
			{
				if(DEBUG_CHANNEL) DEBUGN("-- !! Refresh is issuable - Sending : ");

				//BusPacketType packtype, unsigned transactionID, unsigned col, unsigned rw, unsigned r, unsigned b, unsigned prt, unsigned bl
				BusPacket *refreshPacket = new BusPacket(REFRESH, -1, 0, 0, r, 0, 0, 0, 0, 0, false);
				refreshPacket->channel = channel->channelID;

				//Send to command bus
				(*CommandCallback)(refreshPacket,0);

				//make sure we don't send anythign else
				issuingRefresh = true;

				refreshEnergy[r] += (IDD5B-IDD3N) * tRFC * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);

				for(unsigned b=0; b<NUM_BANKS; b++)
				{
					bankStates[r][b].currentBankState = REFRESHING;
					bankStates[r][b].stateChangeCountdown = tRFC;
					bankStates[r][b].nextActivate = currentClockCycle + tRFC;
					bankStates[r][b].lastCommand = REFRESH;
				}

				//reset refresh counters
				for(unsigned i=0; i<NUM_RANKS; i++)
				{
					if(refreshCounters[r]==0)
					{
						refreshCounters[r] = 7800/tCK;
					}
				}

				//only issue one
				break;
			}
		}
	}

	//If no refresh is being issued then do this block
	if(!issuingRefresh)
	{
		for(unsigned i=0; i<commandQueue.size(); i++)
		{
			//Checks to see if this particular request can be issued
			if(IsIssuable(commandQueue[i]))
			{
				//make sure we don't send a command ahead of its own ACTIVATE
				if(i>0 && commandQueue[i]->transactionID == commandQueue[i-1]->transactionID)
					continue;

				//send to channel
				(*CommandCallback)(commandQueue[i],0);

				//update channel controllers bank state bookkeeping
				unsigned rank = commandQueue[i]->rank;
				unsigned bank = commandQueue[i]->bank;
				BusPacket *writeData;

				//
				//Main block for determining what to do with each type of command
				//
				switch(commandQueue[i]->busPacketType)
				{
				case READ_P:
					outstandingReads++;
					waitingACTS--;
					if(waitingACTS<0)
					{
						ERROR("#@)($J@)#(RJ");
						exit(0);
					}

					//keep track of energy
					burstEnergy[rank] += (IDD4R - IDD3N) * BL/2 * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);

					bankStates[rank][bank].lastCommand = READ_P;
					bankStates[rank][bank].stateChangeCountdown = (4*tCK>7.5)?tRTP:ceil(7.5/tCK); //4 clk or 7.5ns
					bankStates[rank][bank].nextActivate = max(bankStates[rank][bank].nextActivate, currentClockCycle + tRTP + tRP);
					bankStates[rank][bank].nextRefresh = currentClockCycle + tRTP + tRP;

					for(unsigned r=0; r<NUM_RANKS; r++)
					{
						if(r==rank)
						{
							for(unsigned b=0; b<NUM_BANKS; b++)
							{
								bankStates[r][b].nextRead = max(bankStates[r][b].nextRead,
								                                currentClockCycle + max(tCCD, TRANSACTION_SIZE/DRAM_BUS_WIDTH));
								bankStates[r][b].nextWrite = max(bankStates[r][b].nextWrite,
								                                 currentClockCycle + (tCL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tRTRS - tCWL));
							}
						}
						else
						{
							for(unsigned b=0; b<NUM_BANKS; b++)
							{
								bankStates[r][b].nextRead = max(bankStates[r][b].nextRead,
								                                currentClockCycle + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tRTRS);
								bankStates[r][b].nextWrite = max(bankStates[r][b].nextWrite,
								                                 currentClockCycle + (tCL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tRTRS - tCWL));
							}
						}
					}

					//prevents read or write being issued while waiting for auto-precharge to close page
					bankStates[rank][bank].nextRead = bankStates[rank][bank].nextActivate;
					bankStates[rank][bank].nextWrite = bankStates[rank][bank].nextActivate;

					break;
				case WRITE_P:
					waitingACTS--;
					if(waitingACTS<0)
					{
						ERROR(")(JWE)(FJEWF");
						exit(0);
					}

					//keep track of energy
					burstEnergy[rank] += (IDD4W - IDD3N) * BL/2 * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);

					writeData = new BusPacket(*commandQueue[i]);
					writeData->busPacketType = WRITE_DATA;
					writeBurstQueue.push_back(writeData);
					writeBurstCountdown.push_back(tCWL);
					if(DEBUG_CHANNEL) DEBUG("     !!! After Issuing WRITE_P, burstQueue is :"<<writeBurstQueue.size()<<" "<<writeBurstCountdown.size()<<" with head : "<<writeBurstCountdown[0]);

					bankStates[rank][bank].lastCommand = WRITE_P;
					bankStates[rank][bank].stateChangeCountdown = tCWL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tWR;
					bankStates[rank][bank].nextActivate = currentClockCycle + tCWL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tWR + tRP;
					bankStates[rank][bank].nextRefresh = currentClockCycle + tCWL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tWR + tRP;

					for(unsigned r=0; r<NUM_RANKS; r++)
					{
						if(r==rank)
						{
							for(unsigned b=0; b<NUM_BANKS; b++)
							{
								bankStates[r][b].nextRead = max(bankStates[r][b].nextRead, currentClockCycle + tCWL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tWTR);
								bankStates[r][b].nextWrite = max(bankStates[r][b].nextWrite, currentClockCycle+(uint64_t)max(tCCD, TRANSACTION_SIZE/DRAM_BUS_WIDTH));
							}
						}
						else
						{
							for(unsigned b=0; b<NUM_BANKS; b++)
							{
								bankStates[r][b].nextRead = max(bankStates[r][b].nextRead, currentClockCycle + tCWL + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tRTRS - tCL);
								bankStates[r][b].nextWrite = max(bankStates[r][b].nextWrite, currentClockCycle + TRANSACTION_SIZE/DRAM_BUS_WIDTH + tRTRS);
							}
						}
					}

					//prevents read or write being issued while waiting for auto-precharge to close page
					bankStates[rank][bank].nextRead = bankStates[rank][bank].nextActivate;
					bankStates[rank][bank].nextWrite = bankStates[rank][bank].nextActivate;

					break;
				case ACTIVATE:
					for(unsigned b=0; b<NUM_BANKS; b++)
					{
						if(b!=bank)
						{
							bankStates[rank][b].nextActivate = max(currentClockCycle + tRRD, bankStates[rank][b].nextActivate);
						}
					}

					actpreEnergy[rank] += ((IDD0 * tRC) - ((IDD3N * tRAS) + (IDD2N * (tRC - tRAS)))) * ((DRAM_BUS_WIDTH/2 * 8) / DEVICE_WIDTH);

					bankStates[rank][bank].lastCommand = ACTIVATE;
					bankStates[rank][bank].currentBankState = ROW_ACTIVE;
					bankStates[rank][bank].openRowAddress = commandQueue[i]->row;
					bankStates[rank][bank].nextActivate = currentClockCycle + tRC;
					bankStates[rank][bank].nextRead = max(currentClockCycle + tRCD, bankStates[rank][bank].nextRead);
					bankStates[rank][bank].nextWrite = max(currentClockCycle + tRCD, bankStates[rank][bank].nextWrite);

					//keep track of sliding window
					tFAWWindow[rank].push_back(tFAW);

					break;
				default:
					ERROR("Unexpected packet type" << *commandQueue[i]);
					abort();


				}

				//erase
				commandQueue.erase(commandQueue.begin()+i);

				break;
			}
		}
	}
	
	//
	//Debug output
	//
	if(DEBUG_CHANNEL)
	{
		if(commandQueue.size()>0)
		{
			DEBUG("     == Command queue");
			for(unsigned i=0; i<commandQueue.size(); i++)
			{
				DEBUG("       "<<i<<"] " << *commandQueue[i]);
			}
		}

		DEBUG("     == Bank states");

		for(unsigned i=0; i<NUM_RANKS; i++)
		{
			for(unsigned j=0; j<NUM_BANKS; j++)
			{
				DEBUGN("       ");
				if(bankStates[i][j].currentBankState == ROW_ACTIVE)
				{
					DEBUGN("[" << hex << bankStates[i][j].openRowAddress << dec << "] ");
				}
				else if(bankStates[i][j].currentBankState == IDLE)
				{
					DEBUGN("[idle]");
				}
				else if(bankStates[i][j].currentBankState == PRECHARGING)
				{
					DEBUGN("[pre] ");
				}
				else if(bankStates[i][j].currentBankState == REFRESHING)
				{
					DEBUGN("[ref] ");
				}
				else
				{
					ERROR("== Error - unknown state");
					exit(0);
				}
			}
			DEBUG("");
		}
	}

	//increment clock cycle
	currentClockCycle++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movq 0x70(%rdi), %rcx
movq 0x50(%rdi), %rax
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
shlq $0x6, %rdx
movq 0x58(%rdi), %r8
subq 0x60(%rdi), %r8
sarq $0x3, %r8
addq %rdx, %r8
movq 0x38(%rdi), %rdx
movq 0x48(%rdi), %rsi
subq %rdx, %rsi
sarq $0x3, %rsi
xorl %ecx, %ecx
movl $0x0, %edi
addq %r8, %rsi
je 0x10947
movq %rdx, %r8
subq 0x40(%rbx), %r8
movq %r8, %r9
sarq $0x3, %r9
xorl %r11d, %r11d
movl $0x1, %r10d
xorl %edi, %edi
testq %r9, %r9
js 0x108ff
cmpq $0x3f, %r9
ja 0x10908
leaq (%rdx,%r11,8), %r14
jmp 0x1091d
movq %r9, %r11
sarq $0x6, %r11
jmp 0x1090f
movq %r9, %r11
shrq $0x6, %r11
movq (%rax,%r11,8), %r14
shlq $0x9, %r11
subq %r11, %r14
addq %r8, %r14
movq (%r14), %r11
xorl %ebp, %ebp
cmpl $0x4, 0x4(%r11)
sete %bpl
addl %ebp, %edi
movl %r10d, %r11d
incq %r9
addq $0x8, %r8
incl %r10d
cmpq %r11, %rsi
ja 0x108ee
cmpl 0x10(%rbx), %edi
jbe 0x10947
movl %edi, 0x10(%rbx)
addl %edi, 0x14(%rbx)
leaq 0x28(%rbx), %r13
movq 0x78(%rbx), %rax
pxor %xmm0, %xmm0
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movd %ecx, %xmm1
xorl %ecx, %ecx
movd 0xc0(%rsi,%rcx), %xmm2
movd 0x80(%rsi,%rcx), %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movd 0x40(%rsi,%rcx), %xmm2
movd (%rsi,%rcx), %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
punpcklqdq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0]
pcmpeqd %xmm0, %xmm4
psubd %xmm4, %xmm1
addq $0x100, %rcx            # imm = 0x100
cmpq $0x200, %rcx            # imm = 0x200
jne 0x10962
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %ecx
incq %rdx
cmpq $0x4, %rdx
jne 0x10958
addl %ecx, 0x18(%rbx)
xorl %ecx, %ecx
movdqa 0x4071(%rip), %xmm0     # 0x14a40
xorl %esi, %esi
movq (%rax,%rcx,8), %rdx
movd %esi, %xmm1
xorl %esi, %esi
movd 0xc0(%rdx,%rsi), %xmm2
movd 0x80(%rdx,%rsi), %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movd 0x40(%rdx,%rsi), %xmm2
movd (%rdx,%rsi), %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
punpcklqdq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0]
pcmpeqd %xmm0, %xmm4
psubd %xmm4, %xmm1
addq $0x100, %rsi            # imm = 0x100
cmpq $0x200, %rsi            # imm = 0x200
jne 0x109db
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %esi
incq %rcx
cmpq $0x4, %rcx
jne 0x109d1
addl %esi, 0x1c(%rbx)
xorl %ecx, %ecx
movdqa 0x4008(%rip), %xmm0     # 0x14a50
xorl %esi, %esi
movq (%rax,%rcx,8), %rdx
movd %esi, %xmm1
xorl %esi, %esi
movd 0xc0(%rdx,%rsi), %xmm2
movd 0x80(%rdx,%rsi), %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movd 0x40(%rdx,%rsi), %xmm2
movd (%rdx,%rsi), %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
punpcklqdq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0]
pcmpeqd %xmm0, %xmm4
psubd %xmm4, %xmm1
addq $0x100, %rsi            # imm = 0x100
cmpq $0x200, %rsi            # imm = 0x200
jne 0x10a54
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %esi
incq %rcx
cmpq $0x4, %rcx
jne 0x10a4a
addl %esi, 0x20(%rbx)
xorl %ecx, %ecx
movdqa 0x3f9f(%rip), %xmm0     # 0x14a60
xorl %esi, %esi
movq (%rax,%rcx,8), %rdx
movd %esi, %xmm1
xorl %esi, %esi
movd 0xc0(%rdx,%rsi), %xmm2
movd 0x80(%rdx,%rsi), %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movd 0x40(%rdx,%rsi), %xmm2
movd (%rdx,%rsi), %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
punpcklqdq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0]
pcmpeqd %xmm0, %xmm4
psubd %xmm4, %xmm1
addq $0x100, %rsi            # imm = 0x100
cmpq $0x200, %rsi            # imm = 0x200
jne 0x10acd
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %esi
incq %rcx
cmpq $0x4, %rcx
jne 0x10ac3
addl %esi, 0x24(%rbx)
movq 0xf8(%rbx), %rcx
movq 0x158(%rbx), %rdx
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
xorl %r8d, %r8d
movl (%rdi,%r8), %r9d
orl $0x2, %r9d
cmpl $0x3, %r9d
je 0x10b6c
addq $0x40, %r8
cmpq $0x100, %r8             # imm = 0x100
jne 0x10b47
incl (%rdx,%rsi,4)
movl $0x280, %edi            # imm = 0x280
jmp 0x10b71
movl $0x2d0, %edi            # imm = 0x2D0
addq %rdi, (%rcx,%rsi,8)
incq %rsi
cmpq $0x4, %rsi
jne 0x10b40
movq 0xb0(%rbx), %rax
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r15
movq (%rax,%r15,8), %rdi
movq 0x8(%rax,%r15,8), %rdx
cmpq %rdi, %rdx
je 0x10bf6
leaq (%rax,%r15,8), %r12
addq $0x8, %r12
movl $0x1, %ebp
xorl %ecx, %ecx
decl (%rdi,%rcx,4)
jne 0x10bd2
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x10bca
subq %rsi, %rdx
callq 0x32f0
movq (%r12), %rdx
movq 0xb0(%rbx), %rax
addq $-0x4, %rdx
movq %rdx, (%r12)
movl %ebp, %ecx
leaq (%rax,%r15,8), %r12
addq $0x8, %r12
movq -0x8(%r12), %rdi
movq (%r12), %rdx
movq %rdx, %rsi
subq %rdi, %rsi
sarq $0x2, %rsi
incl %ebp
cmpq %rcx, %rsi
ja 0x10ba9
incq %r14
cmpq $0x4, %r14
jne 0x10b88
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x78(%rbx), %rax
movq (%rax,%r14,8), %rdi
addq %r15, %rdi
callq 0xc38e
addq $0x40, %r15
cmpq $0x200, %r15            # imm = 0x200
jne 0x10c05
incq %r14
cmpq $0x4, %r14
jne 0x10c02
movq 0xc8(%rbx), %rax
movdqu (%rax), %xmm0
pxor %xmm2, %xmm2
pcmpeqd %xmm0, %xmm2
movd %xmm2, %ecx
notl %ecx
testb $0x1, %cl
je 0x10c51
movd %xmm0, %ecx
decl %ecx
movl %ecx, (%rax)
pcmpeqd %xmm1, %xmm1
pxor %xmm1, %xmm2
pextrw $0x2, %xmm2, %ecx
testb $0x1, %cl
je 0x10c71
pshufd $0x55, %xmm0, %xmm2     # xmm2 = xmm0[1,1,1,1]
movd %xmm2, %ecx
decl %ecx
movl %ecx, 0x4(%rax)
pxor %xmm2, %xmm2
pcmpeqd %xmm0, %xmm2
pxor %xmm2, %xmm1
pextrw $0x4, %xmm1, %ecx
testb $0x1, %cl
je 0x10c95
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
movd %xmm1, %ecx
decl %ecx
movl %ecx, 0x8(%rax)
pcmpeqd %xmm1, %xmm1
pxor %xmm1, %xmm2
pextrw $0x6, %xmm2, %ecx
testb $0x1, %cl
je 0x10cb5
pshufd $0xff, %xmm0, %xmm0     # xmm0 = xmm0[3,3,3,3]
movd %xmm0, %ecx
decl %ecx
movl %ecx, 0xc(%rax)
movq 0x80(%rbx), %rcx
movq 0x88(%rbx), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
je 0x10d77
sarq $0x2, %rsi
xorl %edi, %edi
decl (%rcx,%rdi,4)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rsi
ja 0x10cd5
cmpq %rcx, %rdx
je 0x10d77
cmpl $0x0, (%rcx)
jne 0x10d77
movq 0x98(%rbx), %rax
movq 0x1b0(%rbx), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x10(%rax)
movq 0x98(%rbx), %rdi
movq 0xa0(%rbx), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x10d34
subq %rsi, %rdx
callq 0x32f0
movq 0xa0(%rbx), %rdx
addq $-0x8, %rdx
movq %rdx, 0xa0(%rbx)
movq 0x80(%rbx), %rdi
movq 0x88(%rbx), %rdx
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x10d65
subq %rsi, %rdx
callq 0x32f0
movq 0x88(%rbx), %rdx
addq $-0x4, %rdx
movq %rdx, 0x88(%rbx)
movq 0xc8(%rbx), %rax
leaq 0x80(%rbx), %r14
movq 0x8(%rbx), %rcx
movq 0x78(%rbx), %rdx
movb $0x1, %sil
xorl %r15d, %r15d
cmpl $0x0, (%rax,%r15,4)
jne 0x10de6
movq (%rdx,%r15,8), %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
leaq 0x1(%r8), %r10
shlq $0x6, %r8
addq %rdi, %r8
movq %r8, %r9
movq %r10, %r8
cmpq %rcx, (%r9)
ja 0x10dca
cmpl $0x0, -0x8(%r9)
jne 0x10dca
incq %r8
addq $0x40, %r9
cmpq $0x9, %r8
jne 0x10daf
jmp 0x10dd4
xorl %esi, %esi
cmpq $0x8, %r8
jne 0x10d9e
jmp 0x10de6
xorl %r12d, %r12d
testb $0x1, %sil
movl $0x0, %esi
jne 0x10f34
incq %r15
cmpq $0x4, %r15
jne 0x10d8c
xorl %r15d, %r15d
movl %r15d, %r15d
movq 0x70(%rbx), %rcx
movq 0x50(%rbx), %rax
movq %rcx, %rsi
subq %rax, %rsi
shrq $0x3, %rsi
cmpq $0x1, %rcx
adcq $-0x1, %rsi
shlq $0x6, %rsi
movq 0x58(%rbx), %rcx
subq 0x60(%rbx), %rcx
sarq $0x3, %rcx
movq 0x38(%rbx), %rdx
movq 0x48(%rbx), %rdi
subq %rdx, %rdi
sarq $0x3, %rdi
addq %rcx, %rdi
addq %rsi, %rdi
cmpq %r15, %rdi
jbe 0x1156a
movq %rdx, %rcx
subq 0x40(%rbx), %rcx
sarq $0x3, %rcx
addq %r15, %rcx
js 0x10e59
cmpq $0x3f, %rcx
ja 0x10e62
leaq (%rdx,%r15,8), %rcx
jmp 0x10e7b
movq %rcx, %rdx
sarq $0x6, %rdx
jmp 0x10e69
movq %rcx, %rdx
shrq $0x6, %rdx
movq %rdx, %rsi
shlq $0x6, %rsi
subq %rsi, %rcx
shlq $0x3, %rcx
addq (%rax,%rdx,8), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq 0x116da
testb %al, %al
je 0x10f2c
movq 0x38(%rbx), %rdx
movq 0x50(%rbx), %rax
movq %rdx, %rsi
subq 0x40(%rbx), %rsi
sarq $0x3, %rsi
testq %r15, %r15
je 0x10ff8
movq %rsi, %rcx
addq %r15, %rcx
js 0x10ebe
cmpq $0x3f, %rcx
ja 0x10ec7
leaq (%rdx,%r15,8), %rdi
jmp 0x10ee3
movq %rcx, %r8
sarq $0x6, %r8
jmp 0x10ece
movq %rcx, %r8
shrq $0x6, %r8
movq %r8, %r9
shlq $0x6, %r9
movq %rcx, %rdi
subq %r9, %rdi
shlq $0x3, %rdi
addq (%rax,%r8,8), %rdi
leal -0x1(%r15), %r8d
addq %r8, %rsi
js 0x10ef8
cmpq $0x3f, %rsi
ja 0x10f01
leaq (%rdx,%r8,8), %rsi
jmp 0x10f1a
movq %rsi, %r8
sarq $0x6, %r8
jmp 0x10f08
movq %rsi, %r8
shrq $0x6, %r8
movq %r8, %r9
shlq $0x6, %r9
subq %r9, %rsi
shlq $0x3, %rsi
addq (%rax,%r8,8), %rsi
movq (%rdi), %rdi
movl 0x18(%rdi), %edi
movq (%rsi), %rsi
cmpl 0x18(%rsi), %edi
jne 0x10ffb
incl %r15d
jmp 0x10df2
movl $0x38, %edi
callq 0x31d0
movq %rax, %r14
movq %rax, %rdi
movl $0x5, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r15d, %r9d
pushq %r12
pushq $0x0
pushq %r12
pushq %r12
pushq %r12
pushq %r12
callq 0xc4ec
addq $0x30, %rsp
movq 0x170(%rbx), %rax
movl 0x1e0(%rax), %eax
movl %eax, 0x28(%r14)
movq 0x1a8(%rbx), %rdi
movq (%rdi), %rax
xorl %r12d, %r12d
movq %r14, %rsi
xorl %edx, %edx
callq *0x10(%rax)
movq 0x140(%rbx), %rax
addq $0x4d530, (%rax,%r15,8) # imm = 0x4D530
movq 0x8(%rbx), %rax
movq 0x78(%rbx), %rcx
movq (%rcx,%r15,8), %rcx
addq $0x6b, %rax
movabsq $0x6b00000005, %rdx     # imm = 0x6B00000005
movl $0x3, (%rcx,%r12)
movq %rax, 0x8(%rcx,%r12)
movq %rdx, 0x38(%rcx,%r12)
addq $0x40, %r12
cmpq $0x200, %r12            # imm = 0x200
jne 0x10fba
movq 0xc8(%rbx), %rax
cmpl $0x0, (%rax,%r15,4)
jne 0x1156a
movl $0x1450, (%rax,%r15,4)  # imm = 0x1450
jmp 0x1156a
movq %rsi, %rcx
movq 0x1a8(%rbx), %rdi
testq %rcx, %rcx
movq %r13, 0x8(%rsp)
movq %r14, 0x10(%rsp)
js 0x1101d
cmpq $0x3f, %rcx
ja 0x11026
leaq (%rdx,%r15,8), %rcx
jmp 0x1103f
movq %rcx, %rdx
sarq $0x6, %rdx
jmp 0x1102d
movq %rcx, %rdx
shrq $0x6, %rdx
movq %rdx, %rsi
shlq $0x6, %rsi
subq %rsi, %rcx
shlq $0x3, %rcx
addq (%rax,%rdx,8), %rcx
movq (%rcx), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x10(%rax)
movq 0x38(%rbx), %r14
movq 0x50(%rbx), %rbp
movq %r14, %r13
subq 0x40(%rbx), %r13
sarq $0x3, %r13
addq %r15, %r13
js 0x11074
cmpq $0x3f, %r13
ja 0x1107d
movq (%r14,%r15,8), %rcx
movl 0x10(%rcx), %edi
movl 0x14(%rcx), %eax
jmp 0x110a7
movq %r13, %rcx
sarq $0x6, %rcx
jmp 0x11084
movq %r13, %rcx
shrq $0x6, %rcx
movq (%rbp,%rcx,8), %rdx
movl %r13d, %eax
andl $0x3f, %eax
movq (%rdx,%rax,8), %rax
movl 0x10(%rax), %edi
movl 0x14(%rax), %eax
shlq $0x6, %rcx
movq %r13, %rsi
subq %rcx, %rsi
movq (%rdx,%rsi,8), %rcx
movl 0x4(%rcx), %ecx
cmpl $0x1, %ecx
je 0x11193
cmpl $0x3, %ecx
je 0x11140
cmpl $0x4, %ecx
jne 0x1157d
movq 0x8(%rbx), %rsi
movq 0x78(%rbx), %rdx
addq $0x4, %rsi
movl %eax, %eax
movl %edi, %ecx
shlq $0x6, %rcx
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x110f5
movq (%rdx,%rax,8), %r8
movq 0x8(%r8,%rdi), %r9
cmpq %r9, %rsi
cmovaq %rsi, %r9
movq %r9, 0x8(%r8,%rdi)
addq $0x40, %rdi
cmpq $0x200, %rdi            # imm = 0x200
jne 0x110db
movq 0x128(%rbx), %rsi
addq $0x40b0, (%rsi,%rax,8)  # imm = 0x40B0
movq (%rdx,%rax,8), %rdx
movl $0x4, 0x38(%rdx,%rcx)
movl $0x1, (%rdx,%rcx)
testq %r13, %r13
js 0x112ae
cmpq $0x3f, %r13
ja 0x112c3
leaq (%r14,%r15,8), %r13
jmp 0x112dd
movl %edi, 0x1c(%rsp)
movl 0xf4(%rbx), %ecx
leal -0x1(%rcx), %edx
movl %edx, 0xf4(%rbx)
testl %ecx, %ecx
jle 0x11601
movl %eax, %r12d
movq 0x110(%rbx), %rax
addq $0x1b80, (%rax,%r12,8)  # imm = 0x1B80
movl $0x38, %edi
callq 0x31d0
testq %r13, %r13
js 0x112b7
cmpq $0x3f, %r13
ja 0x1135a
leaq (%r14,%r15,8), %r13
jmp 0x11374
movq 0xf0(%rbx), %xmm0
movq 0x38cd(%rip), %xmm1     # 0x14a70
paddd %xmm0, %xmm1
movq %xmm1, 0xf0(%rbx)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
testl %ecx, %ecx
jle 0x11657
movl %eax, %eax
movq 0x110(%rbx), %rcx
addq $0x1a40, (%rcx,%rax,8)  # imm = 0x1A40
movq 0x78(%rbx), %rsi
movq (%rsi,%rax,8), %rcx
movl %edi, %edx
shlq $0x6, %rdx
movabsq $0x500000001, %rdi      # imm = 0x500000001
movq %rdi, 0x38(%rcx,%rdx)
movq 0x8(%rbx), %r8
leaq 0xe(%r8), %r9
movq 0x8(%rcx,%rdx), %rdi
cmpq %r9, %rdi
cmovbeq %r9, %rdi
movq %rdi, 0x8(%rcx,%rdx)
movq %r9, 0x30(%rcx,%rdx)
movl $0xf, %r9d
subl 0x806b(%rip), %r9d      # 0x19284
leaq 0x6(%r8), %r10
addq %r8, %r9
addq $0x4, %r8
xorl %r11d, %r11d
cmpq %rax, %r11
jne 0x11260
xorl %r14d, %r14d
movq 0x10(%rcx,%r14), %r12
movq 0x18(%rcx,%r14), %r13
cmpq %r8, %r12
cmovbeq %r8, %r12
cmpq %r9, %r13
cmovbeq %r9, %r13
movq %r12, 0x10(%rcx,%r14)
movq %r13, 0x18(%rcx,%r14)
addq $0x40, %r14
cmpq $0x200, %r14            # imm = 0x200
jne 0x1122f
jmp 0x11296
movq (%rsi,%r11,8), %r14
xorl %r12d, %r12d
movq 0x10(%r14,%r12), %r13
movq 0x18(%r14,%r12), %rbp
cmpq %r10, %r13
cmovbeq %r10, %r13
cmpq %r9, %rbp
cmovbeq %r9, %rbp
movq %r13, 0x10(%r14,%r12)
movq %rbp, 0x18(%r14,%r12)
addq $0x40, %r12
cmpq $0x200, %r12            # imm = 0x200
jne 0x11267
incq %r11
cmpq $0x4, %r11
jne 0x11227
movq %rdi, 0x10(%rcx,%rdx)
movq %rdi, 0x18(%rcx,%rdx)
jmp 0x114f7
movq %r13, %rsi
sarq $0x6, %rsi
jmp 0x112ca
movq %r13, %rcx
sarq $0x6, %rcx
jmp 0x11361
movq %r13, %rsi
shrq $0x6, %rsi
movq %rsi, %rdi
shlq $0x6, %rdi
subq %rdi, %r13
shlq $0x3, %r13
addq (%rbp,%rsi,8), %r13
movq (%r13), %rsi
movl 0xc(%rsi), %esi
movl %esi, 0x4(%rdx,%rcx)
movq 0x8(%rbx), %rsi
leaq 0x21(%rsi), %rdi
movq %rdi, 0x8(%rdx,%rcx)
addq $0x9, %rsi
movq 0x10(%rdx,%rcx), %rdi
movq 0x18(%rdx,%rcx), %r8
cmpq %rdi, %rsi
cmovaq %rsi, %rdi
cmpq %r8, %rsi
cmovbeq %r8, %rsi
movq %rdi, 0x10(%rdx,%rcx)
movq %rsi, 0x18(%rdx,%rcx)
movq 0xb0(%rbx), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x11349
movl 0x7f4e(%rip), %eax      # 0x19288
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x114f7
leaq 0x7f38(%rip), %rdx      # 0x19288
callq 0xc292
jmp 0x114f7
movq %r13, %rcx
shrq $0x6, %rcx
movq %rcx, %rdx
shlq $0x6, %rdx
subq %rdx, %r13
shlq $0x3, %r13
addq (%rbp,%rcx,8), %r13
movq 0x10(%rsp), %r14
movq (%r13), %rcx
movq 0x30(%rcx), %rdx
movq %rdx, 0x30(%rax)
movdqu (%rcx), %xmm0
movdqu 0x10(%rcx), %xmm1
movdqu 0x20(%rcx), %xmm2
movdqu %xmm2, 0x20(%rax)
movdqu %xmm1, 0x10(%rax)
movdqu %xmm0, (%rax)
movq %rax, 0x20(%rsp)
movl $0x8, 0x4(%rax)
movq 0xa0(%rbx), %rsi
cmpq 0xa8(%rbx), %rsi
je 0x113ca
movq %rax, (%rsi)
addq $0x8, 0xa0(%rbx)
jmp 0x113db
leaq 0x98(%rbx), %rdi
leaq 0x20(%rsp), %rdx
callq 0x1031e
movq 0x88(%rbx), %rsi
cmpq 0x90(%rbx), %rsi
je 0x11400
movl 0x7e93(%rip), %edi      # 0x19284
movl %edi, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x88(%rbx)
jmp 0x11415
leaq 0x7e7d(%rip), %rdx      # 0x19284
movq %r14, %rdi
callq 0xc292
movl 0x7e6f(%rip), %edi      # 0x19284
movq 0x78(%rbx), %rdx
movq (%rdx,%r12,8), %rax
movl 0x1c(%rsp), %ecx
shlq $0x6, %rcx
movl $0x3, 0x38(%rax,%rcx)
addl $0xe, %edi
movl %edi, 0x3c(%rax,%rcx)
movq 0x8(%rbx), %rsi
movl 0x7e45(%rip), %r10d     # 0x19284
leaq (%rsi,%r10), %rdi
addq $0x17, %rdi
movq %rdi, 0x8(%rax,%rcx)
movq %rdi, 0x10(%rsp)
movq %rdi, 0x30(%rax,%rcx)
leaq (%rsi,%r10), %r8
addq $-0x3, %r8
leaq 0x6(%rsi), %r9
addq %rsi, %r10
addq $0x9, %r10
addq $0x4, %rsi
xorl %r11d, %r11d
cmpq %r12, %r11
jne 0x114a9
xorl %r14d, %r14d
movq 0x10(%rax,%r14), %r13
movq 0x18(%rax,%r14), %rbp
cmpq %r10, %r13
cmovbeq %r10, %r13
cmpq %rsi, %rbp
cmovbeq %rsi, %rbp
movq %r13, 0x10(%rax,%r14)
movq %rbp, 0x18(%rax,%r14)
addq $0x40, %r14
cmpq $0x200, %r14            # imm = 0x200
jne 0x11478
jmp 0x114df
movq (%rdx,%r11,8), %r14
xorl %r13d, %r13d
movq 0x10(%r14,%r13), %rbp
movq 0x18(%r14,%r13), %rdi
cmpq %r8, %rbp
cmovbeq %r8, %rbp
cmpq %r9, %rdi
cmovbeq %r9, %rdi
movq %rbp, 0x10(%r14,%r13)
movq %rdi, 0x18(%r14,%r13)
addq $0x40, %r13
cmpq $0x200, %r13            # imm = 0x200
jne 0x114b0
incq %r11
cmpq $0x4, %r11
jne 0x11470
movq 0x10(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movq %rdx, 0x18(%rax,%rcx)
movq 0x8(%rsp), %rsi
movq 0x38(%rbx), %rdx
movq 0x50(%rbx), %rax
movq %rdx, %rcx
subq 0x40(%rbx), %rcx
sarq $0x3, %rcx
addq %r15, %rcx
js 0x11523
cmpq $0x3f, %rcx
ja 0x1152c
leaq (%rdx,%r15,8), %rcx
movq (%rax), %rdi
jmp 0x11545
movq %rcx, %rdx
sarq $0x6, %rdx
jmp 0x11533
movq %rcx, %rdx
shrq $0x6, %rdx
leaq (%rax,%rdx,8), %rax
movq (%rax), %rdi
shlq $0x6, %rdx
subq %rdx, %rcx
leaq (%rdi,%rcx,8), %rcx
leaq 0x48(%rsp), %rdx
movq %rcx, (%rdx)
movq %rdi, 0x8(%rdx)
addq $0x200, %rdi            # imm = 0x200
movq %rdi, 0x10(%rdx)
movq %rax, 0x18(%rdx)
leaq 0x28(%rsp), %rdi
callq 0x74cc
incq 0x8(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7a6c(%rip), %rdi      # 0x18ff0
leaq 0x1c3d(%rip), %rsi      # 0x131c8
callq 0x31c0
leaq 0x34e9(%rip), %rsi      # 0x14a80
movq %rax, %rdi
callq 0x31c0
leaq 0x1bcb(%rip), %rsi      # 0x13171
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movl $0x1d6, %esi            # imm = 0x1D6
callq 0x3330
leaq 0x1c17(%rip), %rsi      # 0x131d9
movq %rax, %rdi
callq 0x31c0
leaq 0x34de(%rip), %rsi      # 0x14aaf
movq %rax, %rdi
callq 0x31c0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x11ec0
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xc7bb
movq %rax, %rdi
callq 0x30d0
callq 0x3100
movq 0x79e8(%rip), %rdi      # 0x18ff0
leaq 0x1bb9(%rip), %rsi      # 0x131c8
callq 0x31c0
leaq 0x3465(%rip), %rsi      # 0x14a80
movq %rax, %rdi
callq 0x31c0
leaq 0x1b47(%rip), %rsi      # 0x13171
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0x3330
leaq 0x1b93(%rip), %rsi      # 0x131d9
movq %rax, %rdi
callq 0x31c0
leaq 0x344d(%rip), %rsi      # 0x14aa2
jmp 0x116ab
movq 0x7992(%rip), %rdi      # 0x18ff0
leaq 0x1b63(%rip), %rsi      # 0x131c8
callq 0x31c0
leaq 0x340f(%rip), %rsi      # 0x14a80
movq %rax, %rdi
callq 0x31c0
leaq 0x1af1(%rip), %rsi      # 0x13171
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movl $0x167, %esi            # imm = 0x167
callq 0x3330
leaq 0x1b3d(%rip), %rsi      # 0x131d9
movq %rax, %rdi
callq 0x31c0
leaq 0x33ea(%rip), %rsi      # 0x14a95
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
callq 0x30d0
xorl %edi, %edi
callq 0x3220
movq %rax, %rbx
movl $0x38, %esi
movq %r14, %rdi
callq 0x31f0
movq %rbx, %rdi
callq 0x3340