// File: sopanpatra160[P]OOO_Superscalar_Processor_Simulator/main.cc
// Function: Dispatch(unsigned int, unsigned int)
void Dispatch(unsigned int s,unsigned int n)
{

	vector<instruction>     temp_dispatch_list;
  	unsigned int		dispatch_count=0;

	for (vector<instruction>::iterator it = dispatch_list.begin() ; it != dispatch_list.end(); ++it)
	{
		if(it->state == ID)
		{
			temp_dispatch_list.push_back(*it);			
		}
	}

	for (vector<instruction>::iterator it = temp_dispatch_list.begin() ; it != temp_dispatch_list.end(); ++it)
        {
                if(issue_list.size() < s && dispatch_count < n)
                {
			
			dispatch_count ++;
			it->state=IS;
			if(it->src1_regno_new != -1)
			{
				if(rdy[it->src1_regno_new])
				{
					it->src1_ready=1;
				}
				else
				{
					it->src1_ready=0;
					it->src1_regno_new=new_name[it->src1_regno];
				}
			}
			else
			{
                                it->src1_ready=1;
	
			}
			

			if(it->src2_regno_new != -1)
			{
				if(rdy[it->src2_regno_new])

                        	{
                                	it->src2_ready=1;
                        	}
                        	else
                        	{
                                	it->src2_ready=0;
					it->src2_regno_new=new_name[it->src2_regno];
                        	}
		
			}
			else
			{
                                it->src2_ready=1;
	
			}

			if (it->dest_regno != -1) 
			{
				rdy[it->dest_regno]=0;
				new_name[it->dest_regno] = it->tag;
			}
			it->id_duration = cycles - it->id_begin_cycle;
			it->is_begin_cycle = cycles;
			
			issue_list.push_back(*it);
			//temp_dispatch_list.remove(*it);
			dispatch_list.erase(dispatch_list.begin());
		}
	}
	for (vector<instruction>::iterator it = dispatch_list.begin() ; it != dispatch_list.end(); ++it)
	{
		if(it->state == IF)
		{
			it->state = ID;
			it->if_duration = cycles - it->if_begin_cycle;
			it->id_begin_cycle = cycles;
		}
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x4ba0
movl $0x0, -0x24(%rbp)
leaq 0x6ec0(%rip), %rdi      # 0xa3a8
callq 0x4cc0
movq %rax, -0x30(%rbp)
leaq 0x6eb0(%rip), %rdi      # 0xa3a8
callq 0x4d30
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x3514
jmp 0x3563
leaq -0x30(%rbp), %rdi
callq 0x4d60
cmpl $0x1, 0x4c(%rax)
jne 0x3556
leaq -0x30(%rbp), %rdi
callq 0x4d80
movq %rax, %rsi
leaq -0x20(%rbp), %rdi
callq 0x4c50
jmp 0x353a
jmp 0x3556
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x4bc0
jmp 0x38d5
jmp 0x3558
leaq -0x30(%rbp), %rdi
callq 0x4da0
jmp 0x34f1
leaq -0x20(%rbp), %rdi
callq 0x4cc0
movq %rax, -0x50(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x4d30
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x3593
jmp 0x3807
leaq 0x6e26(%rip), %rdi      # 0xa3c0
callq 0x4c20
movl -0x4(%rbp), %ecx
cmpq %rcx, %rax
jae 0x37f7
movl -0x24(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x37f7
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movl $0x2, 0x4c(%rax)
leaq -0x50(%rbp), %rdi
callq 0x4d60
cmpl $-0x1, 0x14(%rax)
je 0x3646
leaq -0x50(%rbp), %rdi
callq 0x4d60
movslq 0x14(%rax), %rcx
leaq 0x6cfd(%rip), %rax      # 0xa2f0
testb $0x1, (%rax,%rcx)
je 0x3608
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x4(%rax)
jmp 0x3644
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x0, 0x4(%rax)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movslq 0xc(%rax), %rcx
leaq 0x6ac7(%rip), %rax      # 0xa0f0
movl (%rax,%rcx,4), %eax
movl %eax, -0x84(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movl -0x84(%rbp), %ecx
movl %ecx, 0x14(%rax)
jmp 0x3653
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x4(%rax)
leaq -0x50(%rbp), %rdi
callq 0x4d60
cmpl $-0x1, 0x18(%rax)
je 0x36c9
leaq -0x50(%rbp), %rdi
callq 0x4d60
movslq 0x18(%rax), %rcx
leaq 0x6c7a(%rip), %rax      # 0xa2f0
testb $0x1, (%rax,%rcx)
je 0x368b
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x5(%rax)
jmp 0x36c7
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x0, 0x5(%rax)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movslq 0x10(%rax), %rcx
leaq 0x6a44(%rip), %rax      # 0xa0f0
movl (%rax,%rcx,4), %eax
movl %eax, -0x88(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movl -0x88(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x36d6
leaq -0x50(%rbp), %rdi
callq 0x4d60
movb $0x1, 0x5(%rax)
leaq -0x50(%rbp), %rdi
callq 0x4d60
cmpl $-0x1, 0x8(%rax)
je 0x372c
leaq -0x50(%rbp), %rdi
callq 0x4d60
movslq 0x8(%rax), %rcx
leaq 0x6bf7(%rip), %rax      # 0xa2f0
movb $0x0, (%rax,%rcx)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movl 0x50(%rax), %eax
movl %eax, -0x8c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4d60
movl -0x8c(%rbp), %edx
movslq 0x8(%rax), %rcx
leaq 0x69c7(%rip), %rax      # 0xa0f0
movl %edx, (%rax,%rcx,4)
movl 0x69b6(%rip), %eax      # 0xa0e8
movl %eax, -0xa4(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x4d60
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl -0xa4(%rbp), %eax
movl 0x24(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0xa0(%rbp)
callq 0x4d60
movl -0xa0(%rbp), %ecx
movq -0x98(%rbp), %rdi
movl %ecx, 0x28(%rax)
movl 0x696a(%rip), %eax      # 0xa0e8
movl %eax, -0x9c(%rbp)
callq 0x4d60
movl -0x9c(%rbp), %ecx
movq -0x98(%rbp), %rdi
movl %ecx, 0x2c(%rax)
callq 0x4d80
movq %rax, %rsi
leaq 0x6c18(%rip), %rdi      # 0xa3c0
callq 0x4c50
jmp 0x37af
leaq 0x6bf2(%rip), %rdi      # 0xa3a8
movq %rdi, -0xb8(%rbp)
callq 0x4cc0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4e30
movq -0xb8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x4dc0
movq %rax, -0xb0(%rbp)
jmp 0x37ec
movq -0xb0(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x37f9
leaq -0x50(%rbp), %rdi
callq 0x4da0
jmp 0x3570
leaq 0x6b9a(%rip), %rdi      # 0xa3a8
callq 0x4cc0
movq %rax, -0x78(%rbp)
leaq 0x6b8a(%rip), %rdi      # 0xa3a8
callq 0x4d30
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x4cf0
testb $0x1, %al
jne 0x383d
jmp 0x38c3
leaq -0x78(%rbp), %rdi
callq 0x4d60
cmpl $0x0, 0x4c(%rax)
jne 0x38b3
leaq -0x78(%rbp), %rdi
callq 0x4d60
movl $0x1, 0x4c(%rax)
movl 0x6886(%rip), %eax      # 0xa0e8
movl %eax, -0xc4(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4d60
movq %rax, %rcx
movl -0xc4(%rbp), %eax
subl 0x1c(%rcx), %eax
movl %eax, -0xc0(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4d60
movl -0xc0(%rbp), %ecx
movl %ecx, 0x20(%rax)
movl 0x684d(%rip), %eax      # 0xa0e8
movl %eax, -0xbc(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4d60
movl -0xbc(%rbp), %ecx
movl %ecx, 0x24(%rax)
jmp 0x38b5
leaq -0x78(%rbp), %rdi
callq 0x4da0
jmp 0x3817
leaq -0x20(%rbp), %rdi
callq 0x4bc0
addq $0xd0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x1140
nop