// File: NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::schedule_memory_instruction()
void O3_CPU::schedule_memory_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_schedule;
    num_searched = 0;
    if (ROB.head < limit) {
        for (uint32_t i=ROB.head; i<limit; i++) {

            if (ROB.entry[i].is_memory == 0)
                continue;

            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                break;

            if (ROB.entry[i].is_memory && ROB.entry[i].reg_ready && (ROB.entry[i].scheduled == INFLIGHT))
                do_memory_scheduling(i);
        }
    }
    else {
        for (uint32_t i=ROB.head; i<ROB.SIZE; i++) {

            if (ROB.entry[i].is_memory == 0)
                continue;

            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                break;

            if (ROB.entry[i].is_memory && ROB.entry[i].reg_ready && (ROB.entry[i].scheduled == INFLIGHT))
                do_memory_scheduling(i);
        }
        for (uint32_t i=0; i<limit; i++) {

            if (ROB.entry[i].is_memory == 0)
                continue;

            if ((ROB.entry[i].fetched != COMPLETED) || (ROB.entry[i].event_cycle > current_core_cycle[cpu]) || (num_searched >= SCHEDULER_SIZE))
                break;

            if (ROB.entry[i].is_memory && ROB.entry[i].reg_ready && (ROB.entry[i].scheduled == INFLIGHT))
                do_memory_scheduling(i);
        }
    }
}

// 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 0x17494
movq 0x8(%rsp), %rax
cmpl $0x0, 0x960(%rax)
jne 0x17494
jmp 0x17839
movq 0x8(%rsp), %rax
movl 0x980(%rax), %ecx
movl %ecx, 0x1c(%rsp)
movl $0x0, 0x928(%rax)
movl 0x958(%rax), %eax
cmpl 0x1c(%rsp), %eax
jae 0x175e9
movq 0x8(%rsp), %rax
movl 0x958(%rax), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jae 0x175e4
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movzbl 0x49(%rax), %eax
cmpl $0x0, %eax
jne 0x17502
jmp 0x175d4
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 0x1755f
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 0x78975(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x1755f
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x17564
jmp 0x175e4
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x49(%rax)
je 0x175d2
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x59(%rax)
je 0x175d2
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x18(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x1, 0x68(%rax)
jne 0x175d2
movq 0x8(%rsp), %rdi
movl 0x18(%rsp), %esi
callq 0x17840
jmp 0x175d4
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x174cc
jmp 0x17839
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 0x17717
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movzbl 0x49(%rax), %eax
cmpl $0x0, %eax
jne 0x17635
jmp 0x17707
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 0x17692
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 0x78842(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x17692
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x17697
jmp 0x17717
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x49(%rax)
je 0x17705
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x59(%rax)
je 0x17705
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x1, 0x68(%rax)
jne 0x17705
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x17840
jmp 0x17707
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x175f8
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jae 0x17837
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movzbl 0x49(%rax), %eax
cmpl $0x0, %eax
jne 0x17755
jmp 0x17827
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 0x177b2
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 0x78722(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x177b2
movq 0x8(%rsp), %rax
cmpl $0x64, 0x928(%rax)
jb 0x177b7
jmp 0x17837
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x49(%rax)
je 0x17825
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x59(%rax)
je 0x17825
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x10(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x1, 0x68(%rax)
jne 0x17825
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %esi
callq 0x17840
jmp 0x17827
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x1771f
jmp 0x17839
addq $0x28, %rsp
retq
nop