// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]ramulator/src/Cache.cpp
// Function: ramulator::Cache::evict(std::__cxx11::list<ramulator::Cache::Line, std::allocator<ramulator::Cache::Line>>*, std::_List_iterator<ramulator::Cache::Line>)
void Cache::evict(std::list<Line>* lines,
    std::list<Line>::iterator victim) {
  debug("level %d miss evict victim %lx", int(level), victim->addr);
  cache_eviction++;

  long addr = victim->addr;
  long invalidate_time = 0;
  bool dirty = victim->dirty;

  // First invalidate the victim line in higher level.
  if (higher_cache.size()) {
    for (auto hc : higher_cache) {
      auto result = hc->invalidate(addr);
      invalidate_time = max(invalidate_time,
          result.first + (result.second ? latency_each[int(level)] : 0));
      dirty = dirty || result.second || victim->dirty;
    }
  }

  debug("invalidate delay: %ld, dirty: %s", invalidate_time,
      dirty ? "true" : "false");

  if (!is_last_level) {
    // not LLC eviction
    assert(lower_cache != nullptr);
    lower_cache->evictline(addr, dirty);
  } else {
    // LLC eviction
    if (dirty) {
      Request write_req(addr, Request::Type::WRITE);
      cachesys->wait_list.push_back(make_pair(
          cachesys->clk + invalidate_time + latency[int(level)],
          write_req));

      debug("inject one write request to memory system "
          "addr %lx, invalidate time %ld, issue time %ld",
          write_req.addr, invalidate_time,
          cachesys->clk + invalidate_time + latency[int(level)]);
    }
  }

  lines->erase(victim);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movsd 0x238(%rdi), %xmm0
addsd 0x45769(%rip), %xmm0    # 0x14d078
movsd %xmm0, 0x238(%rdi)
movq 0x10(%rdx), %r12
movq %rdx, 0x8(%rsp)
movb 0x21(%rdx), %bpl
movq 0x648(%rdi), %r14
movq 0x640(%rdi), %rbx
xorl %r13d, %r13d
cmpq %rbx, %r14
je 0x107984
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x1077bc
testb $0x1, %dl
je 0x10795b
movslq 0x5f0(%r15), %rcx
movslq 0x624(%r15,%rcx,4), %rcx
jmp 0x10795d
xorl %ecx, %ecx
addq %rax, %rcx
cmpq %rcx, %r13
cmovleq %rcx, %r13
orb %bpl, %dl
movb $0x1, %bpl
testb $0x1, %dl
jne 0x10797b
movq 0x8(%rsp), %rax
movb 0x21(%rax), %bpl
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0x10793a
cmpb $0x0, 0x661(%r15)
je 0x107a91
testb %bpl, %bpl
je 0x107aa9
leaq 0x88(%rsp), %rdx
movb $0x1, (%rdx)
movq %r12, 0x8(%rdx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rdx)
movupd %xmm0, 0x1c(%rdx)
movl $0x1, 0x2c(%rdx)
pcmpeqd %xmm1, %xmm1
movdqu %xmm1, 0x30(%rdx)
movupd %xmm0, 0x40(%rdx)
leaq 0xd04(%rip), %rax       # 0x1086d8
movq %rax, 0x58(%rdx)
leaq 0xcfb(%rip), %rax       # 0x1086da
movq %rax, 0x50(%rdx)
movq 0x630(%r15), %r12
addq 0x50(%r12), %r13
movslq 0x5f0(%r15), %rax
movslq 0x618(%r15,%rax,4), %rax
addq %r13, %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x10857e
leaq 0x20(%rsp), %rdx
movq %r12, %rdi
movq %r12, %rsi
callq 0x10870e
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x107a3c
leaq 0x68(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x107a53
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq 0xd8(%rsp), %rax
testq %rax, %rax
je 0x107a72
leaq 0xc8(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x107aa9
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x107aa9
movq 0x658(%r15), %rdi
testq %rdi, %rdi
je 0x107ade
movzbl %bpl, %edx
movq %r12, %rsi
callq 0x10768e
movq 0x10(%rsp), %rax
decq 0x10(%rax)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xb490
movl $0x28, %esi
movq %rbx, %rdi
callq 0xb2f0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50c87(%rip), %rdi     # 0x15876c
leaq 0x50923(%rip), %rsi     # 0x15840f
leaq 0x50c90(%rip), %rcx     # 0x158783
movl $0x119, %edx            # imm = 0x119
callq 0xb200
jmp 0x107aff
movq %rax, %rdi
callq 0x1718f
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x108114
jmp 0x107b19
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x25bb6
movq %rbx, %rdi
callq 0xb510