// File: /workspace/github_cpp_makefile_star2_O2/NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::add_wq(PACKET*)
int CACHE::add_wq(PACKET *packet)
{
    // check for duplicates in the write queue
    int index = WQ.check_queue(packet);
    if (index != -1) {

        WQ.MERGED++;
        WQ.ACCESS++;

        return index; // merged index
    }

    // sanity check
    if (WQ.occupancy >= WQ.SIZE)
        assert(0);

    // if there is no duplicate, add it to the write queue
    index = WQ.tail;
    if (WQ.entry[index].address != 0) {
        cerr << "[" << NAME << "_ERROR] " << __func__ << " is not empty index: " << index;
        cerr << " address: " << hex << WQ.entry[index].address;
        cerr << " full_addr: " << WQ.entry[index].full_addr << dec << endl;
        assert(0);
    }

    memcpy (&WQ.entry[index], packet, sizeof(PACKET));

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

    WQ.occupancy++;
    WQ.tail++;
    if (WQ.tail >= WQ.SIZE)
        WQ.tail = 0;

    DP (if (warmup_complete[WQ.entry[index].cpu]) {
    cout << "[" << NAME << "_WQ] " <<  __func__ << " instr_id: " << WQ.entry[index].instr_id << " address: " << hex << WQ.entry[index].address;
    cout << " full_addr: " << WQ.entry[index].full_addr << dec;
    cout << " head: " << WQ.head << " tail: " << WQ.tail << " occupancy: " << WQ.occupancy;
    cout << " data: " << hex << WQ.entry[index].data << dec;
    cout << " event: " << WQ.entry[index].event_cycle << " current: " << current_core_cycle[WQ.entry[index].cpu] << endl; });

    WQ.TO_CACHE++;
    WQ.ACCESS++;

    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r14
movq %rdi, %rbx
addq $0x73a8, %rdi           # imm = 0x73A8
callq 0x9b10
movl %eax, %ebp
movl $0x7418, %eax           # imm = 0x7418
cmpl $-0x1, %ebp
jne 0x4c35
movl 0x73dc(%rbx), %eax
cmpl 0x73c8(%rbx), %eax
jae 0x4c4b
movslq 0x73d8(%rbx), %r15
movq 0x7440(%rbx), %rdi
imulq $0x380, %r15, %r12      # imm = 0x380
cmpq $0x0, 0x340(%rdi,%r12)
jne 0x4c6a
addq %r12, %rdi
movl $0x380, %edx            # imm = 0x380
movq %r14, %rsi
callq 0x21b0
movq 0x7440(%rbx), %rax
movq 0x378(%rax,%r12), %rcx
movl 0x330(%r14), %edx
leaq 0x782bb(%rip), %rsi     # 0x7ceb0
movq (%rsi,%rdx,8), %rdx
cmpq %rdx, %rcx
cmovaq %rcx, %rdx
movl 0x738c(%rbx), %ecx
addq %rcx, %rdx
movq %rdx, 0x378(%rax,%r12)
incl 0x73dc(%rbx)
movl 0x73d8(%rbx), %eax
incl %eax
xorl %ecx, %ecx
cmpl 0x73c8(%rbx), %eax
cmovbl %eax, %ecx
movl %ecx, 0x73d8(%rbx)
movl $0x7420, %eax           # imm = 0x7420
incq (%rbx,%rax)
incq 0x7408(%rbx)
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb415(%rip), %rdi      # 0x10067
leaq 0xb410(%rip), %rsi      # 0x10069
leaq 0xb5e9(%rip), %rcx      # 0x10249
movl $0x430, %edx            # imm = 0x430
callq 0x2160
movq 0x1137f(%rip), %r14     # 0x15ff0
leaq 0xb418(%rip), %rsi      # 0x10090
movq %r14, %rdi
callq 0x2230
leaq 0x7350(%rbx), %rsi
movq %rax, %rdi
callq 0x21d0
leaq 0xbf88(%rip), %rsi      # 0x10c1e
movq %rax, %rdi
callq 0x2230
leaq 0xb5c8(%rip), %rsi      # 0x1026d
movq %rax, %rdi
callq 0x2230
leaq 0xb57f(%rip), %rsi      # 0x10233
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
movl %r15d, %esi
callq 0x2390
leaq 0xb4b5(%rip), %rsi      # 0x10183
movq %r14, %rdi
callq 0x2230
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x7440(%rbx), %rcx
movq 0x340(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x2080
leaq 0xb485(%rip), %rsi      # 0x1018e
movq %r14, %rdi
callq 0x2230
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x2080
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
callq 0x2110
leaq 0xb31b(%rip), %rdi      # 0x10067
leaq 0xb316(%rip), %rsi      # 0x10069
leaq 0xb4ef(%rip), %rcx      # 0x10249
movl $0x438, %edx            # imm = 0x438
callq 0x2160