// File: NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::complete_execution(unsigned int)
void O3_CPU::complete_execution(uint32_t rob_index)
{
    if (ROB.entry[rob_index].is_memory == 0) {
        if ((ROB.entry[rob_index].executed == INFLIGHT) && (ROB.entry[rob_index].event_cycle <= current_core_cycle[cpu])) {

            ROB.entry[rob_index].executed = COMPLETED; 
            inflight_reg_executions--;
            completed_executions++;

            if (ROB.entry[rob_index].reg_RAW_producer)
                reg_RAW_release(rob_index);

            if (ROB.entry[rob_index].branch_mispredicted) 
                fetch_stall = 0;

            DP(if(warmup_complete[cpu]) {
            cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id;
            cout << " branch_mispredicted: " << +ROB.entry[rob_index].branch_mispredicted << " fetch_stall: " << +fetch_stall;
            cout << " event: " << ROB.entry[rob_index].event_cycle << endl; });
        }
    }
    else {
        if (ROB.entry[rob_index].num_mem_ops == 0) {
            if ((ROB.entry[rob_index].executed == INFLIGHT) && (ROB.entry[rob_index].event_cycle <= current_core_cycle[cpu])) {
                ROB.entry[rob_index].executed = COMPLETED;
                inflight_mem_executions--;
                completed_executions++;
                
                if (ROB.entry[rob_index].reg_RAW_producer)
                    reg_RAW_release(rob_index);

                if (ROB.entry[rob_index].branch_mispredicted) 
                    fetch_stall = 0;

                DP(if(warmup_complete[cpu]) {
                cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id;
                cout << " is_memory: " << +ROB.entry[rob_index].is_memory << " branch_mispredicted: " << +ROB.entry[rob_index].branch_mispredicted;
                cout << " fetch_stall: " << +fetch_stall << " event: " << ROB.entry[rob_index].event_cycle << " current: " << current_core_cycle[cpu] << endl; });
            }
        }
    }
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movzbl 0x49(%rax), %eax
cmpl $0x0, %eax
jne 0x19b87
movq (%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x1, 0x78(%rax)
jne 0x19b82
movq (%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0xc(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x763dd(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x19b82
movq (%rsp), %rax
movq 0x9b8(%rax), %rcx
movl 0xc(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rcx
movl $0x2, 0x78(%rcx)
movl 0x920(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x920(%rax)
movq 0x8b8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8b8(%rax)
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x58(%rax)
je 0x19b56
movq (%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x19ca0
movq (%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x4b(%rax)
je 0x19b80
movq (%rsp), %rax
movb $0x0, 0x3678(%rax)
jmp 0x19b82
jmp 0x19c98
movq (%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x0, 0x70(%rax)
jne 0x19c96
movq (%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x1, 0x78(%rax)
jne 0x19c94
movq (%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0xc(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x762cb(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x19c94
movq (%rsp), %rax
movq 0x9b8(%rax), %rcx
movl 0xc(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rcx
movl $0x2, 0x78(%rcx)
movl 0x924(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x924(%rax)
movq 0x8b8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8b8(%rax)
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x58(%rax)
je 0x19c68
movq (%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x19ca0
movq (%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0xc(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x4b(%rax)
je 0x19c92
movq (%rsp), %rax
movb $0x0, 0x3678(%rax)
jmp 0x19c94
jmp 0x19c96
jmp 0x19c98
addq $0x18, %rsp
retq
nopl (%rax)