// File: /workspace/github_cpp_makefile_star2_O1/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 0x45f6a(%rip), %xmm0    # 0x14d078
movq %rsi, %rbx
movsd %xmm0, 0x400(%rdi)
movl 0x2c(%rsi), %ecx
testl %ecx, %ecx
je 0x107149
movl $0x368, %eax            # imm = 0x368
cmpl $0x1, %ecx
je 0x10714e
leaq 0x51558(%rip), %rdi     # 0x158689
leaq 0x512d7(%rip), %rsi     # 0x15840f
leaq 0x5156a(%rip), %rcx     # 0x1586a9
movl $0x70, %edx
callq 0xb200
movl $0x2d0, %eax            # imm = 0x2D0
movsd (%r14,%rax), %xmm0
addsd 0x45f1c(%rip), %xmm0    # 0x14d078
movsd %xmm0, (%r14,%rax)
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x107fc4
movq %rax, %r15
movq 0x8(%rbx), %r12
movq (%rax), %r13
cmpq %rax, %r13
je 0x107199
movb 0x684(%r14), %cl
movq %r12, %rax
sarq %cl, %rax
cmpq %rax, 0x18(%r13)
je 0x107199
movq (%r13), %r13
cmpq %r15, %r13
jne 0x10718a
cmpq %r15, %r13
je 0x1071a9
cmpb $0x0, 0x20(%r13)
je 0x10728b
movsd 0x1a0(%r14), %xmm0
addsd 0x45ebe(%rip), %xmm0    # 0x14d078
movsd %xmm0, 0x1a0(%r14)
movl 0x2c(%rbx), %r13d
testl %r13d, %r13d
je 0x1071f6
movl $0x108, %eax            # imm = 0x108
cmpl $0x1, %r13d
je 0x1071fb
leaq 0x514ab(%rip), %rdi     # 0x158689
leaq 0x5122a(%rip), %rsi     # 0x15840f
leaq 0x514bd(%rip), %rcx     # 0x1586a9
movl $0x8a, %edx
callq 0xb200
movl $0x70, %eax
movsd (%r14,%rax), %xmm0
addsd 0x45e6f(%rip), %xmm0    # 0x14d078
movsd %xmm0, (%r14,%rax)
cmpl $0x1, %r13d
sete %bpl
jne 0x107220
movl $0x0, 0x2c(%rbx)
cmpl $0x0, 0x2c(%rbx)
jne 0x1074f0
movq 0x690(%r14), %rdi
movq 0x698(%r14), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1085ec
movq 0x698(%r14), %rcx
cmpq %rcx, %rax
je 0x107365
movsd 0x498(%r14), %xmm0
addsd 0x45e14(%rip), %xmm0    # 0x14d078
movsd %xmm0, 0x498(%r14)
movb $0x1, %bpl
movb $0x1, %cl
cmpl $0x1, %r13d
je 0x10727f
movq 0x8(%rax), %rcx
movb 0x21(%rcx), %cl
movq 0x8(%rax), %rax
movb %cl, 0x21(%rax)
jmp 0x107426
movb 0x684(%r14), %cl
movq %r12, %rbp
sarq %cl, %rbp
movb $0x1, %al
cmpb $0x0, 0x21(%r13)
jne 0x1072a8
cmpl $0x1, 0x2c(%rbx)
sete %al
movb %al, 0x7(%rsp)
movl $0x28, %edi
callq 0xb2e0
movq %r12, 0x10(%rax)
movq %rbp, 0x18(%rax)
movb $0x0, 0x20(%rax)
movb 0x7(%rsp), %cl
movb %cl, 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 %rbx, %rdx
callq 0x10857e
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10870e
movq 0x68(%rsp), %rax
testq %rax, %rax
je 0x107346
leaq 0x58(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x10735d
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movb $0x1, %bpl
jmp 0x107426
subq 0x690(%r14), %rcx
sarq $0x4, %rcx
movl 0x688(%r14), %eax
cmpq %rax, %rcx
jne 0x10739b
movsd 0x530(%r14), %xmm0
addsd 0x45ceb(%rip), %xmm0    # 0x14d078
movsd %xmm0, 0x530(%r14)
jmp 0x107424
movl 0x670(%r14), %eax
cmpq %rax, 0x10(%r15)
jb 0x1073b9
movq %r15, %rax
movq (%rax), %rax
cmpq %r15, %rax
je 0x10740a
cmpb $0x0, 0x20(%rax)
jne 0x1073ab
movl %ebp, %r12d
movq 0x8(%rbx), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x107572
cmpq %r15, %rax
setne %bpl
je 0x107426
movb %r12b, 0x21(%rax)
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x698(%r14), %rsi
cmpq 0x6a0(%r14), %rsi
je 0x10743a
movupd 0x10(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x698(%r14)
jmp 0x10744b
movsd 0x5c8(%r14), %xmm0
addsd 0x45c5d(%rip), %xmm0    # 0x14d078
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 0x1087c8
cmpb $0x0, 0x661(%r14)
je 0x1074a1
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 0x10857e
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10870e
leaq 0x10(%rsp), %rdi
callq 0x108114
jmp 0x107426
movq 0x658(%r14), %r15
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x108150
movq %r15, %rdi
movq %r12, %rsi
callq 0x1070ea
movl %eax, %r15d
leaq 0x78(%rsp), %rdi
callq 0x25bb6
testb %r15b, %r15b
jne 0x107426
addq $0x6a8, %r14            # imm = 0x6A8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1081b8
jmp 0x107426
leaq 0x51192(%rip), %rdi     # 0x158689
leaq 0x50f11(%rip), %rsi     # 0x15840f
leaq 0x511a4(%rip), %rcx     # 0x1586a9
movl $0x97, %edx
callq 0xb200
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x25bb6
jmp 0x107535
jmp 0x107528
movq %rax, %rdi
callq 0x1718f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x108114
movq %rbx, %rdi
callq 0xb510
nop