// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]ramulator/src/Refresh.cpp
// Function: ramulator::Refresh<ramulator::DSARP>::early_inject_refresh()
void Refresh<DSARP>::early_inject_refresh() {
  // Only enabled during reads
  if (ctrl->write_mode)
    return;

  // OoO bank-level refresh
  vector<bool> is_bank_occupied(max_rank_count * max_bank_count, false);
  Controller<DSARP>::Queue& rdq = ctrl->readq;

  // Figure out which banks are idle in order to refresh one of them
  for (auto req: rdq.q)
  {
    assert(req.addr_vec[level_chan] == ctrl->channel->id);
    int ridx = req.addr_vec[level_rank] * max_bank_count;
    int bidx = req.addr_vec[level_bank];
    is_bank_occupied[ridx+bidx] = true;
  }

  // Try to pick an idle bank to refresh per rank
  for (int r = 0; r < max_rank_count; r++) {
    // Randomly pick a bank to examine
    int bidx_start = rand() % max_bank_count;

    for (int b = 0; b < max_bank_count; b++)
    {
      int bidx = (bidx_start + b) % max_bank_count;
      // Idle cycle only
      if (is_bank_occupied[(r * max_bank_count) + bidx])
        continue;

      // Pending refresh
      bool pending_ref = false;
      for (Request req : ctrl->otherq.q)
        if (req.type == Request::Type::REFRESH
            && req.addr_vec[level_chan] == ctrl->channel->id
            && req.addr_vec[level_rank] == r && req.addr_vec[level_bank] == bidx)
          pending_ref = true;
      if (pending_ref)
        continue;

      // Only pull in refreshes when we are almost running out of credits
      if ((*(bank_refresh_backlog[r]))[bidx] >= backlog_early_pull_threshold ||
          ctrl->otherq.q.size() >= ctrl->otherq.max)
        continue;

      // Refresh now
      refresh_target(ctrl, r, bidx, subarray_ref_counters[r][bidx]);
      // One credit for delaying a future ref
      (*(bank_refresh_backlog[r]))[bidx]++;
      subarray_ref_counters[r][bidx] = (subarray_ref_counters[r][bidx]+1) % max_sa_count;
      break;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x8(%rdi), %rax
cmpb $0x0, 0x1190(%rax)
jne 0x132e2c
movq %rdi, %rbx
movslq 0x38(%rdi), %rax
movslq 0x3c(%rdi), %rsi
imulq %rax, %rsi
leaq 0x50(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x18(%rsp), %rdi
leaq 0xb(%rsp), %rcx
callq 0x131fa2
movq 0x8(%rbx), %r12
movq 0x10c0(%r12), %r13
addq $0x10c0, %r12           # imm = 0x10C0
cmpq %r12, %r13
je 0x132bac
leaq 0x60(%rsp), %r14
leaq 0x90(%rsp), %r15
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
leaq 0x3f(%rax), %rbp
movups 0x10(%r13), %xmm0
movaps %xmm0, 0x50(%rsp)
leaq 0x20(%r13), %rsi
movq %r14, %rdi
callq 0x24b6e
movq 0x48(%r13), %rax
movq %rax, 0x28(%r14)
movups 0x38(%r13), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%r13), %rsi
movq %r15, %rdi
callq 0x295b4
movslq 0x40(%rbx), %rcx
movq 0x60(%rsp), %rax
movl (%rax,%rcx,4), %ecx
movq 0x8(%rbx), %rdx
movq 0x1098(%rdx), %rdx
cmpl 0x39c(%rdx), %ecx
jne 0x132e3e
movslq 0x44(%rbx), %rcx
movslq (%rax,%rcx,4), %rcx
movslq 0x3c(%rbx), %rdx
imulq %rcx, %rdx
movslq 0x48(%rbx), %rcx
movslq (%rax,%rcx,4), %rcx
leaq (%rcx,%rdx), %rax
addq $0x3f, %rax
addq %rdx, %rcx
cmovnsq %rcx, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq 0x18(%rsp), %rax
movq %rcx, %rdx
andq %rbp, %rdx
xorl %esi, %esi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
cmpq %rdi, %rdx
setbe %sil
movl $0x1, %edx
shlq %cl, %rdx
orq %rdx, -0x8(%rax,%rsi,8)
movq 0xa0(%rsp), %rax
testq %rax, %rax
je 0x132b88
movq %r15, %rdi
movq %r15, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x132b9f
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r13), %r13
cmpq %r12, %r13
jne 0x132abd
cmpl $0x0, 0x38(%rbx)
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
jle 0x132dfc
leaq 0x60(%rsp), %r14
xorl %r12d, %r12d
leaq 0x3f(%r15), %rdi
movq %rdi, 0x40(%rsp)
callq 0xb0e0
movq 0x40(%rsp), %rdi
movl 0x3c(%rbx), %ecx
cltd
idivl %ecx
movq %rdx, 0x48(%rsp)
testl %ecx, %ecx
jle 0x132dec
xorl %r13d, %r13d
movq %r12, 0x10(%rsp)
movq 0x48(%rsp), %rax
addl %r13d, %eax
cltd
idivl %ecx
imull %r12d, %ecx
movl %edx, 0xc(%rsp)
addl %edx, %ecx
movslq %ecx, %rax
leaq 0x3f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x6, %rcx
shlq $0x3, %rcx
addq 0x18(%rsp), %rcx
movq %rax, %rdx
andq %rdi, %rdx
xorl %esi, %esi
cmpq %r15, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x132c55
incl %r13d
movl 0x3c(%rbx), %ecx
cmpl %ecx, %r13d
movq 0x10(%rsp), %r12
jl 0x132bf6
jmp 0x132dec
movq 0x8(%rbx), %r12
movq 0x1120(%r12), %rbp
addq $0x1120, %r12           # imm = 0x1120
cmpq %r12, %rbp
je 0x132d56
xorl %r15d, %r15d
movups 0x10(%rbp), %xmm0
movaps %xmm0, 0x50(%rsp)
leaq 0x20(%rbp), %rsi
movq %r14, %rdi
callq 0x24b6e
movq 0x48(%rbp), %rax
movq %rax, 0x28(%r14)
movups 0x38(%rbp), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%rbp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x295b4
cmpl $0x2, 0x7c(%rsp)
jne 0x132cfa
movslq 0x40(%rbx), %rcx
movq 0x60(%rsp), %rax
movl (%rax,%rcx,4), %ecx
movq 0x8(%rbx), %rdx
movq 0x1098(%rdx), %rdx
cmpl 0x39c(%rdx), %ecx
jne 0x132cfa
movq %rbx, %rsi
movslq 0x44(%rbx), %rcx
movl (%rax,%rcx,4), %ecx
cmpq %rcx, 0x10(%rsp)
jne 0x132cfa
movslq 0x48(%rsi), %rcx
movl 0xc(%rsp), %edx
cmpl %edx, (%rax,%rcx,4)
movzbl %r15b, %r15d
movl $0x1, %eax
cmovel %eax, %r15d
movq 0xa0(%rsp), %rax
testq %rax, %rax
je 0x132d19
leaq 0x90(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x132d30
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%rbp), %rbp
cmpq %r12, %rbp
jne 0x132c74
testb $0x1, %r15b
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
movq 0x40(%rsp), %rdi
jne 0x132c40
movq 0x50(%rbx), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0xc(%rsp), %rbp
movq (%rax), %rax
movl (%rax,%rbp,4), %eax
cmpl 0x8c(%rbx), %eax
jge 0x132c40
movq 0x8(%rbx), %rsi
movl 0x1138(%rsi), %eax
cmpq %rax, 0x1130(%rsi)
jae 0x132c40
movq 0x68(%rbx), %rax
movq %rbx, %r12
movq 0x10(%rsp), %r13
leaq (,%r13,2), %rbx
addq %r13, %rbx
movq (%rax,%rbx,8), %rax
movl (%rax,%rbp,4), %r8d
movq %r12, %rdi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x133626
movq 0x50(%r12), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rax
incl (%rax,%rbp,4)
movq 0x68(%r12), %rax
movq (%rax,%rbx,8), %rcx
movl (%rcx,%rbp,4), %eax
incl %eax
cltd
idivl 0x80(%r12)
movl %edx, (%rcx,%rbp,4)
movq %r12, %rbx
movq %r13, %r12
incq %r12
movslq 0x38(%rbx), %rax
cmpq %rax, %r12
jl 0x132bd1
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x132e2c
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %eax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b42b(%rip), %rdi     # 0x15e270
leaq 0x2b452(%rip), %rsi     # 0x15e29e
leaq 0x2b45b(%rip), %rcx     # 0x15e2ae
movl $0x39, %edx
callq 0xb200
jmp 0x132e6d
jmp 0x132e65
jmp 0x132e6d
jmp 0x132e72
movq %rax, %rdi
callq 0x1718f
movq %rax, %rbx
jmp 0x132e8c
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x132e8c
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x132ebc
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %eax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0xb510