// File: /workspace/github_cpp_makefile_star2_O3/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 0xc248
movl %eax, %ebp
movl $0x7418, %eax           # imm = 0x7418
cmpl $-0x1, %ebp
jne 0x5f75
movl 0x73dc(%rbx), %eax
cmpl 0x73c8(%rbx), %eax
jae 0x5f8b
movslq 0x73d8(%rbx), %r15
movq 0x7440(%rbx), %rdi
imulq $0x380, %r15, %r12      # imm = 0x380
cmpq $0x0, 0x340(%rdi,%r12)
jne 0x5faa
addq %r12, %rdi
movl $0x380, %edx            # imm = 0x380
movq %r14, %rsi
callq 0x31c0
movq 0x7440(%rbx), %rax
movq 0x378(%rax,%r12), %rcx
movl 0x330(%r14), %edx
leaq 0x7af8b(%rip), %rsi     # 0x80ec0
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 0xe0d5(%rip), %rdi      # 0x14067
leaq 0xe0d0(%rip), %rsi      # 0x14069
leaq 0xe2a9(%rip), %rcx      # 0x14249
movl $0x430, %edx            # imm = 0x430
callq 0x3160
movq 0x1403f(%rip), %r14     # 0x19ff0
leaq 0xe0d8(%rip), %rsi      # 0x14090
movq %r14, %rdi
callq 0x3250
leaq 0x7350(%rbx), %rsi
movq %rax, %rdi
callq 0x31e0
leaq 0xed40(%rip), %rsi      # 0x14d16
movq %rax, %rdi
callq 0x3250
leaq 0xe288(%rip), %rsi      # 0x1426d
movq %rax, %rdi
callq 0x3250
leaq 0xe23f(%rip), %rsi      # 0x14233
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl %r15d, %esi
callq 0x33c0
leaq 0xe175(%rip), %rsi      # 0x14183
movq %r14, %rdi
callq 0x3250
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 0x3090
leaq 0xe145(%rip), %rsi      # 0x1418e
movq %r14, %rdi
callq 0x3250
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x3090
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 0x3110
leaq 0xdfdb(%rip), %rdi      # 0x14067
leaq 0xdfd6(%rip), %rsi      # 0x14069
leaq 0xe1af(%rip), %rcx      # 0x14249
movl $0x438, %edx            # imm = 0x438
callq 0x3160