// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]SMLA/src/Scheduler.h
// Function: ramulator::Scheduler<ramulator::WideIO>::get_head(std::__cxx11::list<ramulator::Request, std::allocator<ramulator::Request>>&)
list<Request>::iterator get_head(list<Request>& q)
    {
      // TODO make the decision at compile time
      if (type != Type::FRFCFS_PriorHit) {
        if (!q.size())
            return q.end();

        auto head = q.begin();
        for (auto itr = next(q.begin(), 1); itr != q.end(); itr++)
            head = compare[int(type)](head, itr);

        return head;
      } else {
        if (!q.size())
            return q.end();

        auto head = q.begin();
        for (auto itr = next(q.begin(), 1); itr != q.end(); itr++) {
            head = compare[int(Type::FRFCFS_PriorHit)](head, itr);
        }

        if (this->ctrl->is_ready(head) && this->ctrl->is_row_hit(head)) {
          return head;
        }

        // prepare a list of hit request
        vector<vector<int>> hit_reqs;
        for (auto itr = q.begin() ; itr != q.end() ; ++itr) {
          if (this->ctrl->is_row_hit(itr)) {
            auto begin = itr->addr_vec.begin();
            // TODO Here it assumes all DRAM standards use PRE to close a row
            // It's better to make it more general.
            auto end = begin + int(ctrl->channel->spec->scope[int(T::Command::PRE)]) + 1;
            vector<int> rowgroup(begin, end); // bank or subarray
            hit_reqs.push_back(rowgroup);
          }
        }
        // if we can't find proper request, we need to return q.end(),
        // so that no command will be scheduled
        head = q.end();
        for (auto itr = q.begin(); itr != q.end(); itr++) {
          bool violate_hit = false;
          if ((!this->ctrl->is_row_hit(itr)) && this->ctrl->is_row_open(itr)) {
            // so the next instruction to be scheduled is PRE, might violate hit
            auto begin = itr->addr_vec.begin();
            // TODO Here it assumes all DRAM standards use PRE to close a row
            // It's better to make it more general.
            auto end = begin + int(ctrl->channel->spec->scope[int(T::Command::PRE)]) + 1;
            vector<int> rowgroup(begin, end); // bank or subarray
            for (const auto& hit_req_rowgroup : hit_reqs) {
              if (rowgroup == hit_req_rowgroup) {
                  violate_hit = true;
                  break;
              }
            }
          }
          if (violate_hit) {
            continue;
          }
          // If it comes here, that means it won't violate any hit request
          if (head == q.end()) {
            head = itr;
          } else {
            head = compare[int(Type::FRFCFS)](head, itr);
          }
        }

        return head;
      }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
cmpl $0x3, 0x8(%rdi)
movq %rsi, 0x10(%rsp)
movq 0x10(%rsi), %rax
jne 0x11b3a
testq %rax, %rax
je 0x11b82
movq 0x10(%rsp), %rax
movq (%rax), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x126cd
movq %rax, %rbx
leaq 0x78(%r14), %r13
cmpq 0x10(%rsp), %rbx
je 0x11b8c
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x126ec
movq %rax, %r15
movq (%rbx), %rbx
jmp 0x11b1d
testq %rax, %rax
je 0x11b82
movq 0x10(%rsp), %rax
movq (%rax), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x126cd
movq %rax, %rbx
leaq 0x18(%r14), %r13
cmpq 0x10(%rsp), %rbx
je 0x11cff
movslq 0x8(%r14), %rdi
shlq $0x5, %rdi
addq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x126ec
movq %rax, %r15
movq (%rbx), %rbx
jmp 0x11b59
movq 0x10(%rsp), %r15
jmp 0x11cff
movq (%r14), %rdi
movq %r15, %rsi
callq 0xe51c
testb %al, %al
je 0x11bae
movq (%r14), %rdi
movq %r15, %rsi
callq 0xef7e
testb %al, %al
jne 0x11cff
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rbx
andq $0x0, 0x10(%rbx)
movaps %xmm0, (%rbx)
leaq 0x40(%rsp), %r15
leaq 0xf(%rsp), %r13
movq 0x10(%rsp), %rbp
movq (%rbp), %rbp
cmpq 0x10(%rsp), %rbp
je 0x11c28
movq (%r14), %rdi
movq %rbp, %rsi
callq 0xef7e
testb %al, %al
je 0x11bcd
movq 0x20(%rbp), %rsi
movq (%r14), %rax
movq 0x1000(%rax), %rax
movq 0x390(%rax), %rax
movslq 0x184(%rax), %rax
leaq 0x4(%rsi,%rax,4), %rdx
movq %r15, %rdi
movq %r13, %rcx
callq 0xe83e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x12718
movq %r15, %rdi
callq 0xe932
jmp 0x11bcd
leaq 0x38(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x40(%rsp), %r13
movq 0x10(%rsp), %rax
movq %rax, %rbx
movq %rax, %r15
movq (%rbx), %rbx
cmpq 0x10(%rsp), %rbx
je 0x11cf5
movq (%r14), %rdi
movq %rbx, %rsi
callq 0xef7e
testb %al, %al
jne 0x11cd2
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x11f42
testb %al, %al
je 0x11cd2
movq 0x20(%rbx), %rsi
movq (%r14), %rax
movq 0x1000(%rax), %rax
movq 0x390(%rax), %rax
movslq 0x184(%rax), %rax
leaq 0x4(%rsi,%rax,4), %rdx
movq %r13, %rdi
leaq 0xf(%rsp), %rcx
callq 0xe83e
movq 0x20(%rsp), %rbp
movq 0x28(%rsp), %r12
cmpq %r12, %rbp
je 0x11cca
movq %r13, %rdi
movq %rbp, %rsi
callq 0x12744
addq $0x18, %rbp
testb %al, %al
je 0x11ca5
movq %r13, %rdi
callq 0xe932
jmp 0x11c41
movq %r13, %rdi
callq 0xe932
movq %rbx, %rax
cmpq 0x10(%rsp), %r15
je 0x11c3e
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x126ec
jmp 0x11c3e
leaq 0x20(%rsp), %rdi
callq 0xf2f2
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11d1b
jmp 0x11d1b
jmp 0x11d20
jmp 0x11d1b
jmp 0x11d1b
movq %rax, %rbx
jmp 0x11d2d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xe932
leaq 0x20(%rsp), %rdi
callq 0xf2f2
movq %rbx, %rdi
callq 0x54a0
nop