// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebx
movl %edi, %ebp
xorps %xmm0, %xmm0
movq %rsp, %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq 0x4c97(%rip), %r15      # 0x73b8
cmpq 0x4c98(%rip), %r15      # 0x73c0
je 0x2742
cmpl $0x1, 0x4c(%r15)
jne 0x273c
movq %r14, %rdi
movq %r15, %rsi
callq 0x31b8
addq $0x58, %r15
jmp 0x2721
movq (%rsp), %r14
movl %ebp, %r13d
xorl %ebp, %ebp
pushq $0x58
popq %r15
leaq 0x4baa(%rip), %r12      # 0x7300
cmpq 0x8(%rsp), %r14
je 0x284e
movq 0x4c70(%rip), %rax      # 0x73d8
subq 0x4c61(%rip), %rax      # 0x73d0
cqto
idivq %r15
cmpq %r13, %rax
setb %al
cmpl %ebx, %ebp
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2845
movl $0x2, 0x4c(%r14)
movslq 0x14(%r14), %rax
cmpq $-0x1, %rax
je 0x27a3
cmpb $0x1, (%rax,%r12)
jne 0x27aa
movb $0x1, 0x4(%r14)
jmp 0x27c1
movb $0x0, 0x4(%r14)
movslq 0xc(%r14), %rax
leaq 0x4946(%rip), %rcx      # 0x7100
movl (%rcx,%rax,4), %eax
movl %eax, 0x14(%r14)
movslq 0x18(%r14), %rax
cmpq $-0x1, %rax
je 0x27d2
cmpb $0x1, (%rax,%r12)
jne 0x27d9
movb $0x1, 0x5(%r14)
jmp 0x27f0
movb $0x0, 0x5(%r14)
movslq 0x10(%r14), %rax
leaq 0x4917(%rip), %rcx      # 0x7100
movl (%rcx,%rax,4), %eax
movl %eax, 0x18(%r14)
movslq 0x8(%r14), %rax
cmpq $-0x1, %rax
je 0x280d
movb $0x0, (%rax,%r12)
movl 0x50(%r14), %ecx
leaq 0x48f6(%rip), %rdx      # 0x7100
movl %ecx, (%rdx,%rax,4)
movl 0x48e5(%rip), %eax      # 0x70f8
movl %eax, %ecx
subl 0x24(%r14), %ecx
movl %ecx, 0x28(%r14)
movl %eax, 0x2c(%r14)
leaq 0x4ba8(%rip), %rdi      # 0x73d0
movq %r14, %rsi
callq 0x31b8
movq 0x4b81(%rip), %rsi      # 0x73b8
leaq 0x4b7a(%rip), %rdi      # 0x73b8
callq 0x31e2
incl %ebp
addq $0x58, %r14
jmp 0x2756
movq 0x4b63(%rip), %rax      # 0x73b8
movq 0x4b64(%rip), %rcx      # 0x73c0
movl 0x4896(%rip), %edx      # 0x70f8
cmpq %rcx, %rax
je 0x2885
cmpl $0x0, 0x4c(%rax)
jne 0x287f
movl $0x1, 0x4c(%rax)
movl %edx, %esi
subl 0x1c(%rax), %esi
movl %esi, 0x20(%rax)
movl %edx, 0x24(%rax)
addq $0x58, %rax
jmp 0x2862
movq %rsp, %rdi
callq 0x3246
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28a0
jmp 0x28a0
movq %rax, %rbx
movq %rsp, %rdi
callq 0x3246
movq %rbx, %rdi
callq 0x1150