// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]ramulator/src/Cache.cpp
// Function: ramulator::Cache::send(ramulator::Request)
bool Cache::send(Request req) {
  debug("level %d req.addr %lx req.type %d, index %d, tag %ld",
      int(level), req.addr, int(req.type), get_index(req.addr),
      get_tag(req.addr));

  cache_total_access++;
  if (req.type == Request::Type::WRITE) {
    cache_write_access++;
  } else {
    assert(req.type == Request::Type::READ);
    cache_read_access++;
  }
  // If there isn't a set, create it.
  auto& lines = get_lines(req.addr);
  std::list<Line>::iterator line;

  if (is_hit(lines, req.addr, &line)) {
    lines.push_back(Line(req.addr, get_tag(req.addr), false,
        line->dirty || (req.type == Request::Type::WRITE)));
    lines.erase(line);
    cachesys->hit_list.push_back(
        make_pair(cachesys->clk + latency[int(level)], req));

    debug("hit, update timestamp %ld", cachesys->clk);
    debug("hit finish time %ld",
        cachesys->clk + latency[int(level)]);

    return true;

  } else {
    debug("miss @level %d", int(level));
    cache_total_miss++;
    if (req.type == Request::Type::WRITE) {
      cache_write_miss++;
    } else {
      assert(req.type == Request::Type::READ);
      cache_read_miss++;
    }

    // The dirty bit will be set if this is a write request and @L1
    bool dirty = (req.type == Request::Type::WRITE);

    // Modify the type of the request to lower level
    if (req.type == Request::Type::WRITE) {
      req.type = Request::Type::READ;
    }

    // Look it up in MSHR entries
    assert(req.type == Request::Type::READ);
    auto mshr = hit_mshr(req.addr);
    if (mshr != mshr_entries.end()) {
      debug("hit mshr");
      cache_mshr_hit++;
      mshr->second->dirty = dirty || mshr->second->dirty;
      return true;
    }

    // All requests come to this stage will be READ, so they
    // should be recorded in MSHR entries.
    if (mshr_entries.size() == mshr_entry_num) {
      // When no MSHR entries available, the miss request
      // is stalling.
      cache_mshr_unavailable++;
      debug("no mshr entry available");
      return false;
    }

    // Check whether there is a line available
    if (all_sets_locked(lines)) {
      cache_set_unavailable++;
      return false;
    }

    auto newline = allocate_line(lines, req.addr);
    if (newline == lines.end()) {
      return false;
    }

    newline->dirty = dirty;

    // Add to MSHR entries
    mshr_entries.push_back(make_pair(req.addr, newline));

    // Send the request to next level;
    if (!is_last_level) {
      if(!lower_cache->send(req)) {
        retry_list.push_back(req);
      }
    } else {
      cachesys->wait_list.push_back(
          make_pair(cachesys->clk + latency[int(level)], req));
    }
    return true;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %r14
movsd 0x400(%rdi), %xmm0
addsd 0x45a06(%rip), %xmm0    # 0x14a0b8
movq %rsi, %rbx
movsd %xmm0, 0x400(%rdi)
movl 0x2c(%rsi), %ecx
testl %ecx, %ecx
je 0x1046ed
movl $0x368, %eax            # imm = 0x368
cmpl $0x1, %ecx
je 0x1046f2
leaq 0x50ff4(%rip), %rdi     # 0x1556c9
leaq 0x50d73(%rip), %rsi     # 0x15544f
leaq 0x51006(%rip), %rcx     # 0x1556e9
movl $0x70, %edx
callq 0xb200
movl $0x2d0, %eax            # imm = 0x2D0
movsd (%r14,%rax), %xmm0
addsd 0x459b8(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, (%r14,%rax)
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x105514
movq %rax, %r15
movq 0x8(%rbx), %r12
movq (%rax), %r13
cmpq %rax, %r13
je 0x10473d
movb 0x684(%r14), %cl
movq %r12, %rax
sarq %cl, %rax
cmpq 0x18(%r13), %rax
je 0x10473d
movq (%r13), %r13
cmpq %r15, %r13
jne 0x10472e
cmpq %r15, %r13
je 0x10474d
cmpb $0x0, 0x20(%r13)
je 0x10480d
movsd 0x1a0(%r14), %xmm0
addsd 0x4595a(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x1a0(%r14)
movl 0x2c(%rbx), %eax
testl %eax, %eax
je 0x10479d
cmpl $0x1, %eax
jne 0x104a73
movsd 0x108(%r14), %xmm0
addsd 0x45930(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x108(%r14)
movl $0x0, 0x2c(%rbx)
movb $0x1, %r13b
jmp 0x1047b4
movsd 0x70(%r14), %xmm0
addsd 0x4590d(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x70(%r14)
xorl %r13d, %r13d
movq 0x690(%r14), %rdi
movq 0x698(%r14), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x105b28
cmpq 0x698(%r14), %rax
je 0x1048e6
movsd 0x498(%r14), %xmm0
addsd 0x458cd(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x498(%r14)
movq 0x8(%rax), %rax
movb $0x1, %bpl
movb $0x1, %cl
testb %r13b, %r13b
jne 0x104805
movb 0x21(%rax), %cl
movb %cl, 0x21(%rax)
jmp 0x1049a3
movb 0x684(%r14), %cl
movq %r12, %rbp
sarq %cl, %rbp
movb $0x1, %al
cmpb $0x0, 0x21(%r13)
jne 0x10482a
cmpl $0x1, 0x2c(%rbx)
sete %al
movl $0x28, %edi
movq %rbx, (%rsp)
movl %eax, %ebx
callq 0xb2e0
movq %r12, 0x10(%rax)
movq %rbp, 0x18(%rax)
movb $0x0, 0x20(%rax)
movb %bl, 0x21(%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0xb080
movq %r13, %rdi
callq 0xb490
movl $0x28, %esi
movq %r13, %rdi
callq 0xb2f0
movq 0x630(%r14), %r15
movslq 0x5f0(%r14), %rax
movslq 0x618(%r14,%rax,4), %rax
addq 0x50(%r15), %rax
addq $0x18, %r15
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq (%rsp), %rdx
callq 0x105aba
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x105c4e
movq 0x68(%rsp), %rax
testq %rax, %rax
je 0x1048c7
leaq 0x58(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1048de
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movb $0x1, %bpl
jmp 0x1049a3
movq 0x690(%r14), %rcx
subq %rcx, %rax
sarq $0x4, %rax
movl 0x688(%r14), %ecx
cmpq %rcx, %rax
jne 0x10491f
movsd 0x530(%r14), %xmm0
addsd 0x457a7(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x530(%r14)
jmp 0x1049a1
movl 0x670(%r14), %eax
cmpq %rax, 0x10(%r15)
jb 0x10493d
movq %r15, %rax
movq (%rax), %rax
cmpq %r15, %rax
je 0x104987
cmpb $0x0, 0x20(%rax)
jne 0x10492f
movq 0x8(%rbx), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x104af4
cmpq %r15, %rax
je 0x1049a1
movb %r13b, 0x21(%rax)
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x698(%r14), %rsi
cmpq 0x6a0(%r14), %rsi
je 0x1049b7
movupd 0x10(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x698(%r14)
jmp 0x1049c8
movsd 0x5c8(%r14), %xmm0
addsd 0x45720(%rip), %xmm0    # 0x14a0b8
movsd %xmm0, 0x5c8(%r14)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x690(%r14), %rdi
leaq 0x10(%rsp), %rdx
callq 0x105d00
cmpb $0x0, 0x661(%r14)
je 0x104a21
movq 0x630(%r14), %r15
movslq 0x5f0(%r14), %rax
movslq 0x618(%r14,%rax,4), %rax
addq 0x50(%r15), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x105aba
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x105c4e
leaq 0x10(%rsp), %rdi
callq 0x105654
jmp 0x1048de
movq 0x658(%r14), %r15
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x105690
movq %r15, %rdi
movq %r12, %rsi
callq 0x10468e
movl %eax, %r15d
leaq 0x78(%rsp), %rdi
callq 0x25c78
movb $0x1, %bpl
testb %r15b, %r15b
jne 0x1049a3
addq $0x6a8, %r14            # imm = 0x6A8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1056f8
jmp 0x1049a3
leaq 0x50c4f(%rip), %rdi     # 0x1556c9
leaq 0x509ce(%rip), %rsi     # 0x15544f
leaq 0x50c61(%rip), %rcx     # 0x1556e9
movl $0x8a, %edx
callq 0xb200
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x25c78
jmp 0x104ab8
jmp 0x104aab
movq %rax, %rdi
callq 0x171a3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x105654
movq %rbx, %rdi
callq 0xb510