// File: /workspace/github_cpp_makefile_star2_O3/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 %r13d, %r13d
xorl %r12d, %r12d
movq 0x9b8(%rbx), %rax
addq %r15, %rax
cmpq $0x0, 0x5c8(%rax,%r14,8)
je 0xe224
incl %r12d
cmpb $0x0, 0x4e(%r14,%rax)
jne 0xe221
movl 0x9e4(%rbx), %eax
cmpl 0x9e0(%rbx), %eax
jae 0xe224
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xe310
incl %r13d
incq %r14
cmpq $0x4, %r14
jne 0xe1e6
leaq 0xbfdc(%rip), %rax      # 0x1a210
movb (%rax), %al
testb %al, %al
je 0xe2a8
xorl %r14d, %r14d
movq 0x9b8(%rbx), %rcx
addq %r15, %rcx
cmpq $0x0, 0x5a8(%rcx,%r14,8)
je 0xe29d
incl %r12d
cmpb $0x0, 0x52(%r14,%rcx)
je 0xe262
incl %r13d
jmp 0xe29d
movl 0xa1c(%rbx), %edx
cmpl 0xa18(%rbx), %edx
jae 0xe29d
movq 0x2a30(%rbx), %rdx
movq 0xa30(%rbx,%rdx,8), %rdx
cmpq (%rcx), %rdx
jne 0xe29d
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xe75a
incl %r13d
leaq 0xbf75(%rip), %rax      # 0x1a210
movb (%rax), %al
incq %r14
movzbl %al, %ecx
cmpq %rcx, %r14
jb 0xe23d
xorl %eax, %eax
subl %r13d, %r12d
je 0xe2b4
jb 0xe2c3
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbd26(%rip), %rdi      # 0x19ff0
leaq 0x6c80(%rip), %rsi      # 0x14f51
callq 0x3250
movq 0x9b8(%rbx), %rcx
movq (%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x3090
movq %rax, %rdi
callq 0x3110
leaq 0x5d6f(%rip), %rdi      # 0x14067
leaq 0x69e7(%rip), %rsi      # 0x14ce6
leaq 0x6b1f(%rip), %rcx      # 0x14e25
movl $0x355, %edx            # imm = 0x355
callq 0x3160