// File: CMU-SAFARI[P]ramulator/src/Cache.cpp
// Function: ramulator::CacheSystem::tick()
void CacheSystem::tick() {
  debug("clk %ld", clk);

  ++clk;

  // Sends ready waiting request to memory
  auto it = wait_list.begin();
  while (it != wait_list.end() && clk >= it->first) {
    if (!send_memory(it->second)) {
      ++it;
    } else {

      debug("complete req: addr %lx", (it->second).addr);

      it = wait_list.erase(it);
    }
  }

  // hit request callback
  it = hit_list.begin();
  while (it != hit_list.end()) {
    if (clk >= it->first) {
      it->second.callback(it->second);

      debug("finish hit: addr %lx", (it->second).addr);

      it = hit_list.erase(it);
    } else {
      ++it;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
movq 0x50(%rdi), %rax
addq $0x1, %rax
movq %rax, 0x50(%rdi)
callq 0x180cb0
movq %rax, -0x10(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x180d10
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x180ce0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xc1(%rbp)
jne 0x17ef2d
jmp 0x17ef5e
movq -0xc0(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0xd0(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x180d40
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq (%rcx), %rax
setge %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x17ef6d
jmp 0x17f03f
movq -0xc0(%rbp), %rax
addq $0x30, %rax
movq %rax, -0xe8(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x180d40
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x78(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x42ba0
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x180d60
movb %al, -0xd1(%rbp)
jmp 0x17efba
movb -0xd1(%rbp), %al
xorb $-0x1, %al
movb %al, -0xe9(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x36780
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x17efdd
jmp 0x17f005
leaq -0x10(%rbp), %rdi
callq 0x180db0
jmp 0x17f03a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x36780
jmp 0x17f14f
leaq -0x98(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x180e20
movq -0xc0(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x180dd0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x17eeff
movq -0xc0(%rbp), %rdi
addq $0x18, %rdi
callq 0x180cb0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0xc0(%rbp), %rdi
addq $0x18, %rdi
callq 0x180d10
movq %rax, -0xa8(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x180ce0
testb $0x1, %al
jne 0x17f091
jmp 0x17f146
movq -0xc0(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0xf8(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x180d40
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq (%rcx), %rax
jl 0x17f138
leaq -0x10(%rbp), %rdi
callq 0x180d40
addq $0x8, %rax
addq $0x40, %rax
movq %rax, -0x108(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x180d40
movq -0x108(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x40f00
movq -0xc0(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x100(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x180e20
movq -0x100(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x180dd0
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x17f141
leaq -0x10(%rbp), %rdi
callq 0x180db0
jmp 0x17f061
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0xc5d0
nopl (%rax,%rax)