// File: /workspace/github_cpp_makefile_star2_O1/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 0x13351a
movq %rdi, %rbx
leaq 0x40(%rsp), %r14
xorl %ebp, %ebp
movq 0x8(%rbx), %r15
movl $0x1120, %eax           # imm = 0x1120
addq %rax, %r15
movl $0x0, 0xc(%rsp)
movq %r15, %r13
movq (%r13), %r13
cmpq %r15, %r13
je 0x13321d
movups 0x10(%r13), %xmm0
movaps %xmm0, 0x30(%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
leaq 0x70(%rsp), %rdi
callq 0x295b4
cmpl $0x2, 0x5c(%rsp)
jne 0x1331de
movslq 0x44(%rbx), %rax
movq 0x40(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpq %rax, %rbp
jne 0x1331de
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r12d, %r12d
jmp 0x1331e1
movb $0x1, %r12b
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x1331fd
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x133214
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
testb %r12b, %r12b
jne 0x133178
testb $0x1, 0xc(%rsp)
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %r13
jne 0x13350a
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x3c(%rbx)
jle 0x13326d
xorl %r15d, %r15d
movl $0x0, 0x30(%rsp)
movl %r15d, 0x34(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x13387c
incl %r15d
cmpl 0x3c(%rbx), %r15d
jl 0x13324c
movq 0x8(%rbx), %r12
movq 0x10e0(%r12), %r13
addq $0x10e0, %r12           # imm = 0x10E0
cmpq %r12, %r13
je 0x1334f3
xorl %r15d, %r15d
movups 0x10(%r13), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x20(%r13), %rsi
leaq 0x40(%rsp), %rdi
callq 0x24b6e
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 0x295b4
movslq 0x44(%rbx), %rcx
movq 0x40(%rsp), %rax
movl (%rax,%rcx,4), %ecx
cmpq %rcx, %rbp
jne 0x1332e8
movslq 0x48(%rbx), %rcx
movslq (%rax,%rcx,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
incl %r15d
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x133304
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x13331b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r13), %r13
cmpq %r12, %r13
jne 0x13328c
testl %r15d, %r15d
je 0x1334f3
movq 0x8(%rbx), %r15
movq 0x10c0(%r15), %r12
addq $0x10c0, %r15           # imm = 0x10C0
cmpq %r15, %r12
leaq 0x40(%rsp), %r13
je 0x1333ed
movups 0x10(%r12), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x20(%r12), %rsi
movq %r13, %rdi
callq 0x24b6e
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 0x295b4
movslq 0x44(%rbx), %rcx
movq 0x40(%rsp), %rax
movl (%rax,%rcx,4), %ecx
cmpq %rcx, %rbp
jne 0x1333ad
movslq 0x48(%rbx), %rcx
movslq (%rax,%rcx,4), %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,8)
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x1333c9
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1333e0
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r12), %r12
cmpq %r15, %r12
jne 0x133351
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r15
cmpq %r15, %r12
je 0x133437
movq %r15, %rax
subq %r12, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r15, %rsi
leaq -0x2eb(%rip), %rcx      # 0x133135
callq 0x1339f1
movq %r12, %rdi
movq %r15, %rsi
leaq -0x2fd(%rip), %rdx      # 0x133135
callq 0x133abc
movslq 0x3c(%rbx), %rax
xorl %r15d, %r15d
testq %rax, %rax
jle 0x13345e
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rdx,%rcx,8)
jne 0x13345b
incq %rcx
cmpq %rcx, %rax
jne 0x13344a
jmp 0x13345e
movl %ecx, %r15d
testl %r15d, %r15d
je 0x133471
callq 0xb0e0
cltd
idivl %r15d
movslq %edx, %rax
jmp 0x133473
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movslq 0x4(%rcx,%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 0x1334f3
movq 0x8(%rbx), %rsi
movl 0x1138(%rsi), %eax
cmpq %rax, 0x1130(%rsi)
jae 0x1334f3
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 0x133626
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 0x13350a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
incq %rbp
movslq 0x38(%rbx), %rax
cmpq %rax, %rbp
jl 0x133161
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13355e
jmp 0x13355e
jmp 0x133538
jmp 0x13355e
jmp 0x133542
jmp 0x133538
movq %rax, %rdi
callq 0x1718f
jmp 0x13355e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x133561
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x133561
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x13358c
movq 0x20(%rsp), %rsi
jmp 0x133584
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x13358c
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510