// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]ramulator/src/Refresh.cpp
// Function: ramulator::Refresh<ramulator::DSARP>::wrp()
void Refresh<DSARP>::wrp() {
  for (int ref_rid = 0; ref_rid < max_rank_count; ref_rid++)
  {
    // Pending refresh in the rank?
    bool pending_ref = false;
    for (Request req : ctrl->otherq.q) {
      if (req.type == Request::Type::REFRESH && req.addr_vec[level_rank] == ref_rid) {
        pending_ref = true;
        break;
      }
    }
    if (pending_ref)
      continue;

    // Find the bank with the lowest number of writes+reads
    vector<wrq_idx> sorted_bank_demand;
    for (int b = 0; b < max_bank_count; b++)
      sorted_bank_demand.push_back(wrq_idx(0,b));
    // Filter out all the writes to this rank
    int total_wr = 0;
    for (auto req : ctrl->writeq.q) {
      if (req.addr_vec[level_rank] == ref_rid) {
        sorted_bank_demand[req.addr_vec[level_bank]].first++;
        total_wr++;
      }
    }
    // If there's no write, just skip.
    if (total_wr == 0)
      continue;

    // Add read
    for (auto req : ctrl->readq.q)
      if (req.addr_vec[level_rank] == ref_rid)
        sorted_bank_demand[req.addr_vec[level_bank]].first++;

    // Sort based on the entries
    std::sort(sorted_bank_demand.begin(), sorted_bank_demand.end(), wrq_comp);

    // Randomly select an idle bank to refresh
    int top_idle_idx = 0;
    for (int i = 0; i < max_bank_count; i++) {
      if (sorted_bank_demand[i].second != 0) {
        top_idle_idx = i;
        break;
      }
    }

    // Select a bank to ref
    int ref_bid_idx = (top_idle_idx == 0) ? 0 : rand() % top_idle_idx;
    int ref_bid = sorted_bank_demand[ref_bid_idx].second;

    // Make sure we don't exceed the credit
    if ((*(bank_refresh_backlog[ref_rid]))[ref_bid] < backlog_max
        && ctrl->otherq.q.size() < ctrl->otherq.max) {
      refresh_target(ctrl, ref_rid, ref_bid, subarray_ref_counters[ref_rid][ref_bid]);
      // Get 1 ref credit
      (*(bank_refresh_backlog[ref_rid]))[ref_bid]++;
      subarray_ref_counters[ref_rid][ref_bid] = (subarray_ref_counters[ref_rid][ref_bid]+1) % max_sa_count;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
leaq 0x28(%rsp), %r15
xorl %r13d, %r13d
movslq 0x38(%rbx), %rax
cmpq %rax, %r13
jge 0xbcf44
movq 0x8(%rbx), %r14
movl $0x1120, %eax           # imm = 0x1120
addq %rax, %r14
movl $0x0, 0xc(%rsp)
movq %r14, %r12
movq (%r12), %r12
cmpq %r14, %r12
je 0xbcd84
leaq 0x10(%r12), %rsi
movq %r15, %rdi
callq 0x2000a
cmpl $0x2, 0x54(%rsp)
jne 0xbcd74
movslq 0x44(%rbx), %rax
movq 0x38(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpq %rax, %r13
jne 0xbcd74
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0xbcd77
movb $0x1, %bpl
movq %r15, %rdi
callq 0x1cc14
testb %bpl, %bpl
jne 0xbcd3c
testb $0x1, 0xc(%rsp)
leaq 0x10(%rsp), %r14
jne 0xbcf3c
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
cmpl 0x3c(%rbx), %ebp
jge 0xbcdc1
andl $0x0, 0x28(%rsp)
movl %ebp, 0x2c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xbd1da
incl %ebp
jmp 0xbcda4
movq 0x8(%rbx), %r12
movl $0x10e0, %eax           # imm = 0x10E0
addq %rax, %r12
xorl %ebp, %ebp
movq %r12, %r14
movq (%r14), %r14
cmpq %r12, %r14
je 0xbce13
leaq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0x2000a
movslq 0x44(%rbx), %rcx
movq 0x38(%rsp), %rax
movl (%rax,%rcx,4), %ecx
cmpq %rcx, %r13
jne 0xbce09
movslq 0x48(%rbx), %rcx
movslq (%rax,%rcx,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
incl %ebp
movq %r15, %rdi
callq 0x1cc14
jmp 0xbcdd2
testl %ebp, %ebp
je 0xbcf32
movq 0x8(%rbx), %r14
movl $0x10c0, %eax           # imm = 0x10C0
addq %rax, %r14
movq %r14, %r12
movq (%r12), %r12
cmpq %r14, %r12
je 0xbce6b
leaq 0x10(%r12), %rsi
movq %r15, %rdi
callq 0x2000a
movslq 0x44(%rbx), %rcx
movq 0x38(%rsp), %rax
movl (%rax,%rcx,4), %ecx
cmpq %rcx, %r13
jne 0xbce61
movslq 0x48(%rbx), %rcx
movslq (%rax,%rcx,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
movq %r15, %rdi
callq 0x1cc14
jmp 0xbce2a
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq -0x186(%rip), %rdx      # 0xbccf6
callq 0xbd357
movl 0x3c(%rbx), %ecx
movq 0x10(%rsp), %rax
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %r14d, %r14d
cmpq %r14, %rcx
je 0xbcec0
cmpl $0x0, 0x4(%rax,%r14,8)
jne 0xbcea8
incq %r14
jmp 0xbce96
testq %r14, %r14
je 0xbcec0
callq 0xb0f0
cltd
idivl %r14d
movslq %edx, %rcx
movq 0x10(%rsp), %rax
jmp 0xbcec2
xorl %ecx, %ecx
movslq 0x4(%rax,%rcx,8), %rbp
movq 0x50(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rax
movl (%rax,%rbp,4), %eax
cmpl 0x84(%rbx), %eax
jge 0xbcf32
movq 0x8(%rbx), %rsi
movl 0x1138(%rsi), %eax
cmpq %rax, 0x1130(%rsi)
jae 0xbcf32
movq 0x68(%rbx), %rax
imulq $0x18, %r13, %r14
movq (%rax,%r14), %rax
movl (%rax,%rbp,4), %r8d
movq %rbx, %rdi
movl %r13d, %edx
movl %ebp, %ecx
callq 0xbd00c
movq 0x50(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rax
incl (%rax,%rbp,4)
movq 0x68(%rbx), %rax
movq (%rax,%r14), %rcx
movl (%rcx,%rbp,4), %eax
incl %eax
cltd
idivl 0x80(%rbx)
movl %edx, (%rcx,%rbp,4)
leaq 0x10(%rsp), %rdi
callq 0xbd1a6
incq %r13
jmp 0xbcd18
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbcf5e
jmp 0xbcf5e
jmp 0xbcf5e
jmp 0xbcf5e
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xbd1a6
movq %rbx, %rdi
callq 0xb4d0
nop