// File: sopanpatra160[P]OOO_Superscalar_Processor_Simulator/main.cc
// Function: Execute(cache*, cache*, cache*)
void Execute(cache *l1,cache *l2,cache *v)
{

	vector <unsigned int> delete_inst_tag_list;
	for (vector<instruction>::iterator it = execute_list.begin() ; it != execute_list.end(); ++it)
        {

		if(it->operation_type == 2 && l1size !=0)
		{
			if(it->mem_latency == 0)
			{
				l1->read(it->mem_addr);
				if ((l1->hit  ==	1) && (l2size != 0) )			it->mem_latency = 5;
				if ((l1->miss ==	1) && (l2->hit == 1) && (l2size != 0))	it->mem_latency = 10;
				if ((l1->miss ==	1) && (l2->miss== 1) && (l2size != 0)) 	it->mem_latency = 20;
				if ((l1->hit  == 1) && (l2size == 0) )			it->mem_latency = 5;
				if ((l1->miss == 1) && (l2size == 0) )                   it->mem_latency = 20;
				
				it->ex_latency ++;
			}
			else
			{
				if(it->ex_latency == it->mem_latency)
				{				
					it->state=WB;
					it->ex_duration = cycles - it->ex_begin_cycle;
					it->wb_begin_cycle = cycles;
					it->wb_duration = 1;
					
					for(vector<instruction>::iterator it1 = fake_rob.begin() ; it1 != fake_rob.end(); ++it1)
					{
						//printf("Here2 ! \n");
						if(it1->tag == it->tag)
						{
								
							*it1 = *it;
						}
					}		
					
					if(it->dest_regno != -1)
					{
						for(vector<instruction>::iterator it2 = issue_list.begin() ; it2 != issue_list.end(); ++it2)
                        			{
							//printf("Here3 ! \n");
                        				if(it2->src1_regno_new ==  it->tag)
							{
								it2->src1_ready = 1;
							}
							if(it2->src2_regno_new ==  it->tag)
                        			        {
								it2->src2_ready = 1;
                        			        }

						}
						if(it->tag == new_name[it->dest_regno])
						{
							rdy[it->dest_regno]=1;
							new_name[it->dest_regno] = it->dest_regno;
						}
					}
					
					//execute_list.erase(it);
					delete_inst_tag_list.push_back(it->tag);
				}
				
				else
				{
						//printf("Here:1\t%d\n", it->tag);
						it->ex_latency++;
				}
			}
		}

		else
		{
			if(	(it->operation_type == 0 && it->ex_latency == 1) || 
				(it->operation_type == 1 && it->ex_latency == 2) || 
				(it->operation_type == 2 && it->ex_latency ==5)
			)

			{
				//printf("Here1 ! \n");
				it->state=WB;
				it->ex_duration = cycles - it->ex_begin_cycle;
				it->wb_begin_cycle = cycles;
				it->wb_duration = 1;
				
				for(vector<instruction>::iterator it1 = fake_rob.begin() ; it1 != fake_rob.end(); ++it1)
				{
				//printf("Here2 ! \n");
					if(it1->tag == it->tag)
					{
							
						*it1 = *it;
					}
				}		
				
				if(it->dest_regno != -1)
				{
					for(vector<instruction>::iterator it2 = issue_list.begin() ; it2 != issue_list.end(); ++it2)
                	        	{
				//printf("Here3 ! \n");
                	        		if(it2->src1_regno_new ==  it->tag)
						{
							it2->src1_ready = 1;
						}
						if(it2->src2_regno_new ==  it->tag)
                	        	        {
							it2->src2_ready = 1;
                	        	        }

					}
					if(it->tag == new_name[it->dest_regno])
					{
						rdy[it->dest_regno]=1;
						new_name[it->dest_regno] = it->dest_regno;
					}
				}
				
				//execute_list.erase(it);
				delete_inst_tag_list.push_back(it->tag);
			}
			else
			{
				//printf("Here:1\t%d\n", it->tag);
				it->ex_latency++;
			}
        	}

	}

	for (unsigned int i = 0; i < delete_inst_tag_list.size(); i++) {
		for (unsigned int j = 0; j < execute_list.size(); j++) {
			if (execute_list[j].tag == delete_inst_tag_list[i]) {
				execute_list.erase(execute_list.begin() + j);

			}
		}
	}


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4ed0
leaq 0x6851(%rip), %rdi      # 0xa3d8
callq 0x4cc0
movq %rax, -0x38(%rbp)
leaq 0x6841(%rip), %rdi      # 0xa3d8
callq 0x4d30
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x3bb6
jmp 0x42d7
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x2, (%rax)
jne 0x3fca
cmpl $0x0, 0x67ad(%rip)      # 0xa37c
je 0x3fca
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x0, 0x48(%rax)
jne 0x3d18
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq -0xc0(%rbp), %rdi
movl 0x54(%rax), %esi
callq 0x19f0
jmp 0x3c0d
movq -0x8(%rbp), %rax
cmpl $0x1, 0x9c(%rax)
jne 0x3c4f
cmpl $0x0, 0x6763(%rip)      # 0xa384
je 0x3c4f
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x5, 0x48(%rax)
jmp 0x3c4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4fa0
jmp 0x442b
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x3c82
movq -0x10(%rbp), %rax
cmpl $0x1, 0x9c(%rax)
jne 0x3c82
cmpl $0x0, 0x6714(%rip)      # 0xa384
je 0x3c82
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0xa, 0x48(%rax)
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x3cb5
movq -0x10(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x3cb5
cmpl $0x0, 0x66e1(%rip)      # 0xa384
je 0x3cb5
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x14, 0x48(%rax)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x9c(%rax)
jne 0x3cdb
cmpl $0x0, 0x66bb(%rip)      # 0xa384
jne 0x3cdb
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x5, 0x48(%rax)
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x3d01
cmpl $0x0, 0x6695(%rip)      # 0xa384
jne 0x3d01
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x14, 0x48(%rax)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x44(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rax)
jmp 0x3fc5
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x44(%rax), %eax
movl %eax, -0xc4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xc4(%rbp), %eax
cmpl 0x48(%rcx), %eax
jne 0x3fb1
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x4, 0x4c(%rax)
movl 0x638d(%rip), %eax      # 0xa0e8
movl %eax, -0xd0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xd0(%rbp), %eax
subl 0x34(%rcx), %eax
movl %eax, -0xcc(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0xcc(%rbp), %ecx
movl %ecx, 0x38(%rax)
movl 0x6354(%rip), %eax      # 0xa0e8
movl %eax, -0xc8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0xc8(%rbp), %ecx
movl %ecx, 0x3c(%rax)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x1, 0x40(%rax)
leaq 0x65cd(%rip), %rdi      # 0xa390
callq 0x4cc0
movq %rax, -0x58(%rbp)
leaq 0x65bd(%rip), %rdi      # 0xa390
callq 0x4d30
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x3def
jmp 0x3e55
leaq -0x58(%rbp), %rdi
callq 0x4d60
movl 0x50(%rax), %eax
movl %eax, -0xd4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xd4(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x3e45
leaq -0x38(%rbp), %rdi
callq 0x4d80
movq %rax, -0xe0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x4d80
movq -0xe0(%rbp), %rsi
movq %rax, %rdi
movl $0x58, %edx
callq 0x10a0
jmp 0x3e47
leaq -0x58(%rbp), %rdi
callq 0x4da0
jmp 0x3dcc
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $-0x1, 0x8(%rax)
je 0x3f94
leaq 0x6551(%rip), %rdi      # 0xa3c0
callq 0x4cc0
movq %rax, -0x68(%rbp)
leaq 0x6541(%rip), %rdi      # 0xa3c0
callq 0x4d30
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x3e9b
jmp 0x3f17
leaq -0x68(%rbp), %rdi
callq 0x4d60
movl 0x14(%rax), %eax
movl %eax, -0xe4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xe4(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x3ed1
leaq -0x68(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x4(%rax)
leaq -0x68(%rbp), %rdi
callq 0x4d60
movl 0x18(%rax), %eax
movl %eax, -0xe8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xe8(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x3f07
leaq -0x68(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x5(%rax)
jmp 0x3f09
leaq -0x68(%rbp), %rdi
callq 0x4da0
jmp 0x3e78
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x50(%rax), %eax
movl %eax, -0xec(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xec(%rbp), %eax
movslq 0x8(%rcx), %rdx
leaq 0x61aa(%rip), %rcx      # 0xa0f0
cmpl (%rcx,%rdx,4), %eax
jne 0x3f92
leaq -0x38(%rbp), %rdi
callq 0x4d60
movslq 0x8(%rax), %rcx
leaq 0x6391(%rip), %rax      # 0xa2f0
movb $0x1, (%rax,%rcx)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x8(%rax), %eax
movl %eax, -0xf0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0xf0(%rbp), %edx
movslq 0x8(%rax), %rcx
leaq 0x6161(%rip), %rax      # 0xa0f0
movl %edx, (%rax,%rcx,4)
jmp 0x3f94
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rsi
addq $0x50, %rsi
leaq -0x30(%rbp), %rdi
callq 0x4ef0
jmp 0x3faf
jmp 0x3fc3
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x44(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rax)
jmp 0x3fc5
jmp 0x42c7
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x0, (%rax)
jne 0x3fe7
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x1, 0x44(%rax)
je 0x4029
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x1, (%rax)
jne 0x4004
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x2, 0x44(%rax)
je 0x4029
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x2, (%rax)
jne 0x42b3
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $0x5, 0x44(%rax)
jne 0x42b3
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x4, 0x4c(%rax)
movl 0x60a9(%rip), %eax      # 0xa0e8
movl %eax, -0xfc(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xfc(%rbp), %eax
subl 0x34(%rcx), %eax
movl %eax, -0xf8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0xf8(%rbp), %ecx
movl %ecx, 0x38(%rax)
movl 0x6070(%rip), %eax      # 0xa0e8
movl %eax, -0xf4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0xf4(%rbp), %ecx
movl %ecx, 0x3c(%rax)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl $0x1, 0x40(%rax)
leaq 0x62e9(%rip), %rdi      # 0xa390
callq 0x4cc0
movq %rax, -0x78(%rbp)
leaq 0x62d9(%rip), %rdi      # 0xa390
callq 0x4d30
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x40d3
jmp 0x4139
leaq -0x78(%rbp), %rdi
callq 0x4d60
movl 0x50(%rax), %eax
movl %eax, -0x100(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0x100(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x4129
leaq -0x38(%rbp), %rdi
callq 0x4d80
movq %rax, -0x108(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4d80
movq -0x108(%rbp), %rsi
movq %rax, %rdi
movl $0x58, %edx
callq 0x10a0
jmp 0x412b
leaq -0x78(%rbp), %rdi
callq 0x4da0
jmp 0x40b0
leaq -0x38(%rbp), %rdi
callq 0x4d60
cmpl $-0x1, 0x8(%rax)
je 0x4296
leaq 0x626d(%rip), %rdi      # 0xa3c0
callq 0x4cc0
movq %rax, -0x88(%rbp)
leaq 0x625a(%rip), %rdi      # 0xa3c0
callq 0x4d30
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x418e
jmp 0x4219
leaq -0x88(%rbp), %rdi
callq 0x4d60
movl 0x14(%rax), %eax
movl %eax, -0x10c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0x10c(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x41ca
leaq -0x88(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x4(%rax)
leaq -0x88(%rbp), %rdi
callq 0x4d60
movl 0x18(%rax), %eax
movl %eax, -0x110(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0x110(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x4206
leaq -0x88(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x5(%rax)
jmp 0x4208
leaq -0x88(%rbp), %rdi
callq 0x4da0
jmp 0x415f
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x50(%rax), %eax
movl %eax, -0x114(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0x114(%rbp), %eax
movslq 0x8(%rcx), %rdx
leaq 0x5ea8(%rip), %rcx      # 0xa0f0
cmpl (%rcx,%rdx,4), %eax
jne 0x4294
leaq -0x38(%rbp), %rdi
callq 0x4d60
movslq 0x8(%rax), %rcx
leaq 0x608f(%rip), %rax      # 0xa2f0
movb $0x1, (%rax,%rcx)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x8(%rax), %eax
movl %eax, -0x118(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl -0x118(%rbp), %edx
movslq 0x8(%rax), %rcx
leaq 0x5e5f(%rip), %rax      # 0xa0f0
movl %edx, (%rax,%rcx,4)
jmp 0x4296
leaq -0x38(%rbp), %rdi
callq 0x4d60
movq %rax, %rsi
addq $0x50, %rsi
leaq -0x30(%rbp), %rdi
callq 0x4ef0
jmp 0x42b1
jmp 0x42c5
leaq -0x38(%rbp), %rdi
callq 0x4d60
movl 0x44(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rax)
jmp 0x42c7
jmp 0x42c9
leaq -0x38(%rbp), %rdi
callq 0x4da0
jmp 0x3b90
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq %rax, -0x120(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4f60
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jae 0x4419
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
movq %rax, -0x128(%rbp)
leaq 0x60b0(%rip), %rdi      # 0xa3d8
callq 0x4c20
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x4403
movl -0x98(%rbp), %eax
movl %eax, %esi
leaq 0x6089(%rip), %rdi      # 0xa3d8
callq 0x4e70
movl 0x50(%rax), %eax
movl %eax, -0x12c(%rbp)
movl -0x94(%rbp), %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x4f80
movq %rax, %rcx
movl -0x12c(%rbp), %eax
cmpl (%rcx), %eax
jne 0x43ed
leaq 0x6056(%rip), %rdi      # 0xa3d8
movq %rdi, -0x140(%rbp)
callq 0x4cc0
movq %rax, -0xb0(%rbp)
movl -0x98(%rbp), %eax
movl %eax, %esi
leaq -0xb0(%rbp), %rdi
callq 0x4e90
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x4e30
movq -0x140(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x4dc0
movq %rax, -0x138(%rbp)
jmp 0x43df
movq -0x138(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x43ef
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x4314
jmp 0x4405
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x42e1
leaq -0x30(%rbp), %rdi
callq 0x4fa0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x1140
nopw %cs:(%rax,%rax)