// File: NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::schedule_instruction()
void O3_CPU::schedule_instruction()
{
    if ((ROB.head == ROB.tail) && ROB.occupancy == 0)
        return;

    // execution is out-of-order but we have an in-order scheduling algorithm to detect all RAW dependencies
    uint32_t limit = ROB.next_fetch[1];
    num_searched = 0;
    if (ROB.head < limit) {
        for (uint32_t i=ROB.head; i<limit; i++) { 
            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                return;

            if (ROB.entry[i].scheduled == 0)
                do_scheduling(i);

            num_searched++;
        }
    }
    else {
        for (uint32_t i=ROB.head; i<ROB.SIZE; i++) {
            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                return;

            if (ROB.entry[i].scheduled == 0)
                do_scheduling(i);

            num_searched++;
        }
        for (uint32_t i=0; i<limit; i++) { 
            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                return;

            if (ROB.entry[i].scheduled == 0)
                do_scheduling(i);

            num_searched++;
        }
    }
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movl 0x958(%rcx), %eax
cmpl 0x95c(%rcx), %eax
jne 0x167f4
movq 0x8(%rsp), %rax
cmpl $0x0, 0x960(%rax)
jne 0x167f4
jmp 0x16a94
movq 0x8(%rsp), %rax
movl 0x97c(%rax), %ecx
movl %ecx, 0x1c(%rsp)
movl $0x0, 0x928(%rax)
movl 0x958(%rax), %eax
cmpl 0x1c(%rsp), %eax
jae 0x168f3
movq 0x8(%rsp), %rax
movl 0x958(%rax), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jae 0x168ee
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x64(%rax)
jne 0x16897
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x18(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x7963d(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x16897
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x1689c
jmp 0x16a94
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x0, 0x68(%rax)
jne 0x168ca
movq 0x8(%rsp), %rdi
movl 0x18(%rsp), %esi
callq 0x16aa0
movq 0x8(%rsp), %rax
movl 0x928(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x928(%rax)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x1682c
jmp 0x16a94
movq 0x8(%rsp), %rax
movl 0x958(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl 0x950(%rcx), %eax
jae 0x169cb
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x64(%rax)
jne 0x16974
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x14(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x79560(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x16974
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x16979
jmp 0x16a94
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x0, 0x68(%rax)
jne 0x169a7
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x16aa0
movq 0x8(%rsp), %rax
movl 0x928(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x928(%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x16902
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jae 0x16a92
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x64(%rax)
jne 0x16a3e
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x10(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x79496(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x16a3e
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x16a40
jmp 0x16a94
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x0, 0x68(%rax)
jne 0x16a6e
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %esi
callq 0x16aa0
movq 0x8(%rsp), %rax
movl 0x928(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x928(%rax)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x169d3
jmp 0x16a94
addq $0x28, %rsp
retq
nopl (%rax)