// File: NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::return_data(PACKET*)
void CACHE::return_data(PACKET *packet)
{
    // check MSHR information
    int mshr_index = check_mshr(packet);

    // sanity check
    if (mshr_index == -1) {
        cerr << "[" << NAME << "_MSHR] " << __func__ << " instr_id: " << packet->instr_id << " cannot find a matching entry!";
        cerr << " full_addr: " << hex << packet->full_addr;
        cerr << " address: " << packet->address << dec;
        cerr << " event: " << packet->event_cycle << " current: " << current_core_cycle[packet->cpu] << endl;
        assert(0);
    }

    // MSHR holds the most updated information about this request
    // no need to do memcpy
    MSHR.num_returned++;
    MSHR.entry[mshr_index].returned = COMPLETED;
    MSHR.entry[mshr_index].data = packet->data;

    // ADD LATENCY
    if (MSHR.entry[mshr_index].event_cycle < current_core_cycle[packet->cpu])
        MSHR.entry[mshr_index].event_cycle = current_core_cycle[packet->cpu] + LATENCY;
    else
        MSHR.entry[mshr_index].event_cycle += LATENCY;

    update_fill_cycle();

    DP (if (warmup_complete[packet->cpu]) {
    cout << "[" << NAME << "_MSHR] " <<  __func__ << " instr_id: " << MSHR.entry[mshr_index].instr_id;
    cout << " address: " << hex << MSHR.entry[mshr_index].address << " full_addr: " << MSHR.entry[mshr_index].full_addr;
    cout << " data: " << MSHR.entry[mshr_index].data << dec << " num_returned: " << MSHR.num_returned;
    cout << " index: " << mshr_index << " occupancy: " << MSHR.occupancy;
    cout << " event: " << MSHR.entry[mshr_index].event_cycle << " current: " << current_core_cycle[packet->cpu] << " next: " << MSHR.next_fill_cycle << endl; });
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movq 0x18(%rsp), %rsi
callq 0x6370
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x94d8
movq 0x1fc78(%rip), %rdi     # 0x28ff0
leaq 0x19cae(%rip), %rsi     # 0x2302d
callq 0x3220
movq 0x8(%rsp), %rsi
movq %rax, %rdi
addq $0x7350, %rsi           # imm = 0x7350
callq 0x31c0
movq %rax, %rdi
leaq 0x19e9a(%rip), %rsi     # 0x2323c
callq 0x3220
movq %rax, %rdi
leaq 0x19e93(%rip), %rsi     # 0x23244
callq 0x3220
movq %rax, %rdi
leaq 0x1aa5f(%rip), %rsi     # 0x23e1f
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x368(%rax), %rsi
callq 0x3080
movq %rax, %rdi
leaq 0x19e6d(%rip), %rsi     # 0x23250
callq 0x3220
movq 0x1fc01(%rip), %rdi     # 0x28ff0
leaq 0x19d35(%rip), %rsi     # 0x2312b
callq 0x3220
movq %rax, %rdi
leaq 0x4ab(%rip), %rsi       # 0x98b0
callq 0x30d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x348(%rax), %rsi
callq 0x3080
movq 0x1fbcb(%rip), %rdi     # 0x28ff0
leaq 0x19cf4(%rip), %rsi     # 0x23120
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x340(%rax), %rsi
callq 0x3080
movq %rax, %rdi
leaq 0x481(%rip), %rsi       # 0x98d0
callq 0x30d0
movq 0x1fb95(%rip), %rdi     # 0x28ff0
leaq 0x19cd6(%rip), %rsi     # 0x23138
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x378(%rax), %rsi
callq 0x3080
movq %rax, %rdi
leaq 0x19dea(%rip), %rsi     # 0x2326f
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movl 0x330(%rax), %eax
movl %eax, %ecx
leaq 0x86a1f(%rip), %rax     # 0x8fec0
movq (%rax,%rcx,8), %rsi
callq 0x3080
movq %rax, %rdi
movq 0x1fb04(%rip), %rsi     # 0x28fb8
callq 0x3250
leaq 0x19b44(%rip), %rdi     # 0x23004
leaq 0x19b3f(%rip), %rsi     # 0x23006
movl $0x4f4, %edx            # imm = 0x4F4
leaq 0x19da7(%rip), %rcx     # 0x2327a
callq 0x3160
movq 0x8(%rsp), %rax
movl 0xc9c0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc9c0(%rax)
movq 0xca20(%rax), %rcx
movslq 0x14(%rsp), %rdx
imulq $0x380, %rdx, %rdx      # imm = 0x380
addq %rdx, %rcx
movb $0x2, 0x32c(%rcx)
movq 0x18(%rsp), %rcx
movq 0x360(%rcx), %rdx
movq 0xca20(%rax), %rcx
movslq 0x14(%rsp), %rsi
imulq $0x380, %rsi, %rsi      # imm = 0x380
addq %rsi, %rcx
movq %rdx, 0x360(%rcx)
movq 0xca20(%rax), %rax
movslq 0x14(%rsp), %rcx
imulq $0x380, %rcx, %rcx      # imm = 0x380
addq %rcx, %rax
movq 0x378(%rax), %rax
movq 0x18(%rsp), %rcx
movl 0x330(%rcx), %ecx
movl %ecx, %edx
leaq 0x8695d(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
jae 0x95ae
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movl 0x330(%rcx), %ecx
movl %ecx, %edx
leaq 0x8693e(%rip), %rcx     # 0x8fec0
movq (%rcx,%rdx,8), %rcx
movl 0x738c(%rax), %edx
addq %rdx, %rcx
movq 0xca20(%rax), %rax
movslq 0x14(%rsp), %rdx
imulq $0x380, %rdx, %rdx      # imm = 0x380
addq %rdx, %rax
movq %rcx, 0x378(%rax)
jmp 0x95dd
movq 0x8(%rsp), %rax
movl 0x738c(%rax), %ecx
movq 0xca20(%rax), %rax
movslq 0x14(%rsp), %rdx
imulq $0x380, %rdx, %rdx      # imm = 0x380
addq %rdx, %rax
addq 0x378(%rax), %rcx
movq %rcx, 0x378(%rax)
movq 0x8(%rsp), %rdi
callq 0x4a00
addq $0x28, %rsp
retq
nopl (%rax)