// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
cmpl $0x0, 0x38(%rdi)
jle 0x1303b0
movq %rdi, %rbx
leaq 0x40(%rsp), %r14
xorl %ebp, %ebp
movq 0x8(%rbx), %r13
movl $0x1120, %eax           # imm = 0x1120
addq %rax, %r13
movl $0x0, 0xc(%rsp)
movq %r13, %r15
movq (%r15), %r15
cmpq %r13, %r15
je 0x1300b8
movups 0x10(%r15), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x20(%r15), %rsi
movq %r14, %rdi
callq 0x24b42
movq 0x48(%r15), %rax
movq %rax, 0x28(%r14)
movups 0x38(%r15), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%r15), %rsi
leaq 0x70(%rsp), %rdi
callq 0x29968
cmpl $0x2, 0x5c(%rsp)
jne 0x130079
movslq 0x44(%rbx), %rax
movq 0x40(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpq %rax, %rbp
jne 0x130079
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r12d, %r12d
jmp 0x13007c
movb $0x1, %r12b
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x130098
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1300af
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
testb %r12b, %r12b
jne 0x130014
testb $0x1, 0xc(%rsp)
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %r13
jne 0x1303a0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x3c(%rbx)
jle 0x130108
xorl %r15d, %r15d
movl $0x0, 0x30(%rsp)
movl %r15d, 0x34(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1306fa
incl %r15d
cmpl 0x3c(%rbx), %r15d
jl 0x1300e7
movq 0x8(%rbx), %r12
movq 0x10e0(%r12), %r13
addq $0x10e0, %r12           # imm = 0x10E0
cmpq %r12, %r13
je 0x130389
xorl %r15d, %r15d
movups 0x10(%r13), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x20(%r13), %rsi
leaq 0x40(%rsp), %rdi
callq 0x24b42
movq 0x48(%r13), %rax
movq %rax, 0x28(%r14)
movups 0x38(%r13), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%r13), %rsi
leaq 0x70(%rsp), %rdi
callq 0x29968
movslq 0x44(%rbx), %rax
movq 0x40(%rsp), %rdi
movl (%rdi,%rax,4), %eax
cmpq %rax, %rbp
jne 0x130183
movslq 0x48(%rbx), %rax
movslq (%rdi,%rax,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
incl %r15d
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x1301a9
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1301b6
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r13), %r13
cmpq %r12, %r13
jne 0x130127
testl %r15d, %r15d
je 0x130389
movq 0x8(%rbx), %r15
movq 0x10c0(%r15), %r12
addq $0x10c0, %r15           # imm = 0x10C0
cmpq %r15, %r12
leaq 0x40(%rsp), %r13
je 0x130288
movups 0x10(%r12), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x20(%r12), %rsi
movq %r13, %rdi
callq 0x24b42
movq 0x48(%r12), %rax
movq %rax, 0x28(%r14)
movups 0x38(%r12), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%r12), %rsi
leaq 0x70(%rsp), %rdi
callq 0x29968
movslq 0x44(%rbx), %rax
movq 0x40(%rsp), %rdi
movl (%rdi,%rax,4), %eax
cmpq %rax, %rbp
jne 0x130248
movslq 0x48(%rbx), %rax
movslq (%rdi,%rax,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x13026e
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x13027b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r12), %r12
cmpq %r15, %r12
jne 0x1301ec
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
je 0x1302d6
movq %r13, %rax
subq %r12, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r13, %rsi
leaq -0x2e9(%rip), %r15      # 0x12ffd2
movq %r15, %rcx
callq 0x130877
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x130942
movq 0x10(%rsp), %r12
movslq 0x3c(%rbx), %rax
testq %rax, %rax
jle 0x13030c
xorl %r15d, %r15d
cmpl $0x0, 0x4(%r12,%r15,8)
jne 0x1302f4
incq %r15
cmpq %r15, %rax
jne 0x1302e2
jmp 0x13030c
testq %r15, %r15
je 0x13030c
callq 0xb0e0
cltd
idivl %r15d
movslq %edx, %rax
movq 0x10(%rsp), %r12
jmp 0x13030e
xorl %eax, %eax
movslq 0x4(%r12,%rax,8), %r12
movq 0x50(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rax
movl (%rax,%r12,4), %eax
cmpl 0x84(%rbx), %eax
jge 0x130389
movq 0x8(%rbx), %rsi
movl 0x1138(%rsi), %eax
cmpq %rax, 0x1130(%rsi)
jae 0x130389
movq 0x68(%rbx), %rax
leaq (,%rbp,2), %r15
addq %rbp, %r15
movq (%rax,%r15,8), %rax
movl (%rax,%r12,4), %r8d
movq %rbx, %rdi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x1304c2
movq 0x50(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rax
incl (%rax,%r12,4)
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rcx
movl (%rcx,%r12,4), %eax
incl %eax
cltd
idivl 0x80(%rbx)
movl %edx, (%rcx,%r12,4)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1303a0
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
incq %rbp
movslq 0x38(%rbx), %rax
cmpq %rax, %rbp
jl 0x12fffd
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1303f4
jmp 0x1303f4
jmp 0x1303ce
jmp 0x1303f4
jmp 0x1303d8
jmp 0x1303ce
movq %rax, %rdi
callq 0x171a3
jmp 0x1303f4
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1303f7
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x1303f7
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x130422
movq 0x20(%rsp), %rsi
jmp 0x13041a
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x130422
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510