// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
cmpl $0x3, 0x8(%rdi)
movq 0x10(%rsi), %rax
movq %rsi, 0x30(%rsp)
jne 0x182ba
testq %rax, %rax
je 0x18323
movq (%r14), %rbp
movq (%rbp), %rbx
cmpq %r14, %rbx
je 0x1832b
movq 0x8(%rsp), %r12
leaq 0x78(%r12), %r15
leaq 0x10(%rsp), %r13
movq %rbp, 0x40(%rsp)
movq %rbx, 0x10(%rsp)
cmpq $0x0, 0x88(%r12)
je 0x185ca
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
callq *0x90(%r12)
movq %rax, %rbp
movq (%rbx), %rbx
cmpq %r14, %rbx
jne 0x18281
jmp 0x18330
testq %rax, %rax
je 0x18323
movq (%r14), %rbp
movq (%rbp), %rbx
cmpq %r14, %rbx
je 0x185b8
movq 0x8(%rsp), %rax
leaq 0x18(%rax), %r14
leaq 0x40(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x8(%rsp), %rax
movslq 0x8(%rax), %rdi
shlq $0x5, %rdi
movq %rbp, 0x40(%rsp)
movq %rbx, 0x10(%rsp)
cmpq $0x0, 0x10(%r14,%rdi)
je 0x185ca
addq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x18(%rdi)
movq %rax, %rbp
movq (%rbx), %rbx
cmpq 0x30(%rsp), %rbx
jne 0x182e2
jmp 0x185b8
movq %r14, %rbp
jmp 0x185b8
movq 0x8(%rsp), %r12
movq (%r12), %rdi
movq %rbp, %rsi
callq 0x1474a
testb %al, %al
je 0x18372
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x1000(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%rbp), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%rbp), %rdx
callq 0x15138
testb %al, %al
jne 0x185b8
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x30(%rsp), %rbx
movq (%rbx), %r13
cmpq %rbx, %r13
je 0x1842d
leaq 0x10(%rsp), %r15
leaq 0x38(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq 0x1000(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x15138
testb %al, %al
je 0x1841f
movq 0x20(%r13), %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 %r12, %rcx
callq 0x14a68
movq %rbp, %rdi
movq %r15, %rsi
callq 0x19126
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1841f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x52d0
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x183a8
movq (%rbx), %r13
jmp 0x18432
movq 0x8(%rsp), %r14
cmpq %rbx, %r13
je 0x185ab
leaq 0x38(%r14), %r12
leaq 0x10(%rsp), %r15
movq %rbx, %rax
movq %r12, 0x28(%rsp)
movq %rax, 0x60(%rsp)
movq (%r14), %rax
movq 0x1000(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x15138
testb %al, %al
jne 0x18562
movq (%r14), %rax
movq 0x1000(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x19d7c
testb %al, %al
je 0x18562
movq 0x20(%r13), %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
leaq 0x38(%rsp), %rcx
callq 0x14a68
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
movb $0x1, %al
movq 0x10(%rsp), %rbp
cmpq %r14, %r15
je 0x18536
movq 0x18(%rsp), %rbx
movq %rbx, %r12
subq %rbp, %r12
movq (%r15), %rsi
movq 0x8(%r15), %rax
subq %rsi, %rax
cmpq %rax, %r12
jne 0x1851d
cmpq %rbp, %rbx
je 0x1852f
movq %rbp, %rdi
movq %r12, %rdx
callq 0x52e0
testl %eax, %eax
je 0x1852f
addq $0x18, %r15
cmpq %r14, %r15
jne 0x184fa
movq 0x28(%rsp), %r12
movb $0x1, %al
jmp 0x18536
xorl %eax, %eax
movq 0x28(%rsp), %r12
testq %rbp, %rbp
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
je 0x1855e
movq 0x20(%rsp), %rsi
subq %rbp, %rsi
movq %rbp, %rdi
movl %eax, %ebp
callq 0x52d0
movl %ebp, %eax
testb %al, %al
je 0x185a4
movq %r13, %rbp
movq 0x60(%rsp), %rax
cmpq %rbx, %rax
je 0x18592
movq %rax, 0x10(%rsp)
movq %r13, 0x38(%rsp)
cmpq $0x0, 0x48(%r14)
je 0x185cf
movq %r12, %rdi
movq %r15, %rsi
leaq 0x38(%rsp), %rdx
callq *0x50(%r14)
movq %rax, %rbp
movq (%r13), %r13
movq %rbp, %rax
cmpq %rbx, %r13
jne 0x1844c
jmp 0x185ae
movq 0x60(%rsp), %rbp
jmp 0x18592
movq %rbx, %rbp
leaq 0x40(%rsp), %rdi
callq 0x154fe
movq %rbp, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5150
callq 0x5150
jmp 0x185fa
jmp 0x185fa
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x185fd
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x52d0
jmp 0x185fd
jmp 0x185fa
jmp 0x185fa
jmp 0x185fa
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x154fe
movq %rbx, %rdi
callq 0x54c0
nop