// File: /workspace/github_cpp_makefile_star2_O2/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::check_and_add_lsq(unsigned int)
uint32_t O3_CPU::check_and_add_lsq(uint32_t rob_index) 
{
    uint32_t num_mem_ops = 0, num_added = 0;

    // load
    for (uint32_t i=0; i<NUM_INSTR_SOURCES; i++) {
        if (ROB.entry[rob_index].source_memory[i]) {
            num_mem_ops++;
            if (ROB.entry[rob_index].source_added[i])
                num_added++;
            else if (LQ.occupancy < LQ.SIZE) {
                add_load_queue(rob_index, i);
                num_added++;
            }
            else {
                DP(if(warmup_complete[cpu]) {
                cout << "[LQ] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id;
                cout << " cannot be added in the load queue occupancy: " << LQ.occupancy << " cycle: " << current_core_cycle[cpu] << endl; });
            }
        }
    }

    // store
    for (uint32_t i=0; i<MAX_INSTR_DESTINATIONS; i++) {
        if (ROB.entry[rob_index].destination_memory[i]) {
            num_mem_ops++;
            if (ROB.entry[rob_index].destination_added[i])
                num_added++;
            else if (SQ.occupancy < SQ.SIZE) {
                if (STA[STA_head] == ROB.entry[rob_index].instr_id) {
                    add_store_queue(rob_index, i);
                    num_added++;
                }
                //add_store_queue(rob_index, i);
                //num_added++;
            }
            else {
                DP(if(warmup_complete[cpu]) {
                cout << "[SQ] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id;
                cout << " cannot be added in the store queue occupancy: " << SQ.occupancy << " cycle: " << current_core_cycle[cpu] << endl; });
            }
        }
    }

    if (num_added == num_mem_ops)
        return 0;

    uint32_t not_available = num_mem_ops - num_added;
    if (not_available > num_mem_ops) {
        cerr << "instr_id: " << ROB.entry[rob_index].instr_id << endl;
        assert(0);
    }

    return not_available;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movl %esi, %eax
imulq $0x758, %rax, %r15      # imm = 0x758
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq $0x4, %r14
je 0xb5ab
movq 0x9b8(%rbx), %rax
addq %r15, %rax
cmpq $0x0, 0x5c8(%rax,%r14,8)
je 0xb5a6
incl %r12d
cmpb $0x0, 0x4e(%r14,%rax)
jne 0xb5a3
movl 0x9e4(%rbx), %eax
cmpl 0x9e0(%rbx), %eax
jae 0xb5a6
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xb68c
incl %r13d
incq %r14
jmp 0xb562
leaq 0xac4e(%rip), %rax      # 0x16200
movb (%rax), %al
xorl %r14d, %r14d
movzbl %al, %ecx
cmpq %rcx, %r14
jae 0xb624
movq 0x9b8(%rbx), %rcx
addq %r15, %rcx
cmpq $0x0, 0x5a8(%rcx,%r14,8)
je 0xb61f
incl %r12d
cmpb $0x0, 0x52(%r14,%rcx)
je 0xb5e4
incl %r13d
jmp 0xb61f
movl 0xa1c(%rbx), %edx
cmpl 0xa18(%rbx), %edx
jae 0xb61f
movq 0x2a30(%rbx), %rdx
movq 0xa30(%rbx,%rdx,8), %rdx
cmpq (%rcx), %rdx
jne 0xb61f
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xba58
incl %r13d
leaq 0xabe3(%rip), %rax      # 0x16200
movb (%rax), %al
incq %r14
jmp 0xb5b7
xorl %eax, %eax
subl %r13d, %r12d
je 0xb630
jb 0xb63f
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa9aa(%rip), %rdi      # 0x15ff0
leaq 0x580c(%rip), %rsi      # 0x10e59
callq 0x2230
movq 0x9b8(%rbx), %rcx
movq (%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2080
movq %rax, %rdi
callq 0x2110
leaq 0x49f3(%rip), %rdi      # 0x10067
leaq 0x5573(%rip), %rsi      # 0x10bee
leaq 0x56ab(%rip), %rcx      # 0x10d2d
movl $0x355, %edx            # imm = 0x355
callq 0x2160