// File: suyunu[P]Discrete-Event-Simulation/MinQueuing.cpp
// Function: MinQueuing::simulate()
void MinQueuing::simulate(){
	Job current;
	
	while(!order.empty()){
		pop_heap(order.begin(),order.end(),greater1());
		current = order.back();
		order.pop_back();
		
		currentTime = current.timeToGo;
		
		if(current.cond == -1){
			work(current, 0);
		}
		else{
			int toGo = whereToGoMin(current);
			/* If the queue is not empty, the next job in the queue starts to be processed:
			 * The function calculates the check-out time for the job (currentTime + units[current.cond].procTime)
			 * and pushes it to the heap
			 */
			if(!units[current.cond].jobQ.empty()){
				units[current.cond].Jcounter++;
				Job temp = units[current.cond].jobQ.front();
				units[current.cond].jobQ.pop();
				
				temp.timeToGo = currentTime + units[current.cond].procTime;
				jobs[temp.no] = temp;
				
				order.push_back(temp);
				push_heap (order.begin(),order.end(),greater1());
			}
			// Sets the status of the unit as not working.
			else
				units[current.cond].isWorking = false;
			
			if(toGo != -1)
				work(current, toGo);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2820
movq -0x90(%rbp), %rdi
addq $0x28, %rdi
callq 0x4900
xorb $-0x1, %al
testb $0x1, %al
jne 0x75de
jmp 0x784a
movq -0x90(%rbp), %rdi
addq $0x28, %rdi
movq %rdi, -0x98(%rbp)
callq 0x4820
movq -0x98(%rbp), %rdi
movq %rax, -0x30(%rbp)
callq 0x4850
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4940
jmp 0x7618
movq -0x90(%rbp), %rdi
addq $0x28, %rdi
callq 0x49b0
movq -0x90(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x18(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x10(%rbp)
addq $0x28, %rdi
callq 0x49f0
movq -0x90(%rbp), %rax
movsd -0x18(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
cmpl $-0x1, -0x10(%rbp)
jne 0x7696
movq -0x90(%rbp), %rdi
leaq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x7870
jmp 0x7680
jmp 0x7845
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x785c
movq -0x90(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x7a10
movl %eax, -0x9c(%rbp)
jmp 0x76ae
movq -0x90(%rbp), %rax
movl -0x9c(%rbp), %ecx
movl %ecx, -0x50(%rbp)
movq 0x20(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x70, %rcx, %rcx
leaq 0x10(%rax,%rcx), %rdi
callq 0x4a20
movb %al, -0x9d(%rbp)
jmp 0x76dc
movb -0x9d(%rbp), %al
testb $0x1, %al
jne 0x780c
jmp 0x76ec
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x10(%rbp), %rdx
imulq $0x70, %rdx, %rdx
movl 0x64(%rcx,%rdx), %esi
incl %esi
movl %esi, 0x64(%rcx,%rdx)
movq 0x20(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x70, %rcx, %rcx
leaq 0x10(%rax,%rcx), %rdi
callq 0x4a40
movq %rax, -0xa8(%rbp)
jmp 0x7728
movq -0x90(%rbp), %rax
movq -0xa8(%rbp), %rcx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq 0x20(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x70, %rcx, %rcx
leaq 0x10(%rax,%rcx), %rdi
callq 0x4a60
jmp 0x775d
movq -0x90(%rbp), %rdi
movsd 0x8(%rdi), %xmm0
movq 0x20(%rdi), %rax
movslq -0x10(%rbp), %rcx
imulq $0x70, %rcx, %rcx
movsd 0x8(%rax,%rcx), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movq 0x18(%rdi), %rax
movslq -0x70(%rbp), %rcx
shlq $0x5, %rcx
movups -0x64(%rbp), %xmm0
movups %xmm0, 0xc(%rax,%rcx)
movaps -0x70(%rbp), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x28, %rdi
leaq -0x70(%rbp), %rsi
callq 0x4770
jmp 0x77b0
movq -0x90(%rbp), %rdi
addq $0x28, %rdi
movq %rdi, -0xb0(%rbp)
callq 0x4820
movq -0xb0(%rbp), %rdi
movq %rax, -0x78(%rbp)
callq 0x4850
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x4a80
jmp 0x77ea
leaq -0x70(%rbp), %rdi
callq 0x2850
jmp 0x7826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2850
jmp 0x785c
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x6c(%rax)
cmpl $-0x1, -0x50(%rbp)
je 0x7843
movq -0x90(%rbp), %rdi
movl -0x50(%rbp), %edx
leaq -0x28(%rbp), %rsi
callq 0x7870
jmp 0x7841
jmp 0x7843
jmp 0x7845
jmp 0x75c3
leaq -0x28(%rbp), %rdi
callq 0x2850
addq $0xb0, %rsp
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x2850
movq -0x48(%rbp), %rdi
callq 0x2280
nop