// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]ramulator/src/Scheduler.h
// Function: ramulator::Scheduler<ramulator::DDR3>::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 queue is empty, return end of queue
            if (!q.size())
                return q.end();

            //Else return based on the policy
            auto head = q.begin();
            for (auto itr = next(q.begin(), 1); itr != q.end(); itr++)
                head = compare[int(type)](head, itr);

            return head;
        } 
        else { //Code to get around edge cases for FRFCFS_PriorHit
            
       //If queue is empty, return end of queue
            if (!q.size())
                return q.end();

       //Else return based on FRFCFS_PriorHit Scheduling Policy
            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 0x28ed8
testq %rax, %rax
je 0x28f41
movq (%r14), %rbp
movq (%rbp), %rbx
cmpq %r14, %rbx
je 0x28f49
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 0x291e8
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
callq *0x90(%r12)
movq %rax, %rbp
movq (%rbx), %rbx
cmpq %r14, %rbx
jne 0x28e9f
jmp 0x28f4e
testq %rax, %rax
je 0x28f41
movq (%r14), %rbp
movq (%rbp), %rbx
cmpq %r14, %rbx
je 0x291d6
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 0x291e8
addq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x18(%rdi)
movq %rax, %rbp
movq (%rbx), %rbx
cmpq 0x30(%rsp), %rbx
jne 0x28f00
jmp 0x291d6
movq %r14, %rbp
jmp 0x291d6
movq 0x8(%rsp), %r12
movq (%r12), %rdi
movq %rbp, %rsi
callq 0x2445a
testb %al, %al
je 0x28f90
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x1098(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%rbp), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%rbp), %rdx
callq 0x249f6
testb %al, %al
jne 0x291d6
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x30(%rsp), %rbx
movq (%rbx), %r13
cmpq %rbx, %r13
je 0x2904b
leaq 0x10(%rsp), %r15
leaq 0x38(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq 0x1098(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x249f6
testb %al, %al
je 0x2903d
movq 0x20(%r13), %rsi
movq (%r14), %rax
movq 0x1098(%rax), %rax
movq 0x390(%rax), %rax
movslq 0x184(%rax), %rax
leaq 0x4(%rsi,%rax,4), %rdx
movq %r15, %rdi
movq %r12, %rcx
callq 0x29cd2
movq %rbp, %rdi
movq %r15, %rsi
callq 0x29d14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2903d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x28fc6
movq (%rbx), %r13
jmp 0x29050
movq 0x8(%rsp), %r14
cmpq %rbx, %r13
je 0x291c9
leaq 0x38(%r14), %r12
leaq 0x10(%rsp), %r15
movq %rbx, %rax
movq %r12, 0x28(%rsp)
movq %rax, 0x60(%rsp)
movq (%r14), %rax
movq 0x1098(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x249f6
testb %al, %al
jne 0x29180
movq (%r14), %rax
movq 0x1098(%rax), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r13), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r13), %rdx
callq 0x2ac7c
testb %al, %al
je 0x29180
movq 0x20(%r13), %rsi
movq (%r14), %rax
movq 0x1098(%rax), %rax
movq 0x390(%rax), %rax
movslq 0x184(%rax), %rax
leaq 0x4(%rsi,%rax,4), %rdx
movq %r15, %rdi
leaq 0x38(%rsp), %rcx
callq 0x29cd2
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
movb $0x1, %al
movq 0x10(%rsp), %rbp
cmpq %r14, %r15
je 0x29154
movq 0x18(%rsp), %rbx
movq %rbx, %r12
subq %rbp, %r12
movq (%r15), %rsi
movq 0x8(%r15), %rax
subq %rsi, %rax
cmpq %rax, %r12
jne 0x2913b
cmpq %rbp, %rbx
je 0x2914d
movq %rbp, %rdi
movq %r12, %rdx
callq 0xb300
testl %eax, %eax
je 0x2914d
addq $0x18, %r15
cmpq %r14, %r15
jne 0x29118
movq 0x28(%rsp), %r12
movb $0x1, %al
jmp 0x29154
xorl %eax, %eax
movq 0x28(%rsp), %r12
testq %rbp, %rbp
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
je 0x2917c
movq 0x20(%rsp), %rsi
subq %rbp, %rsi
movq %rbp, %rdi
movl %eax, %ebp
callq 0xb2f0
movl %ebp, %eax
testb %al, %al
je 0x291c2
movq %r13, %rbp
movq 0x60(%rsp), %rax
cmpq %rbx, %rax
je 0x291b0
movq %rax, 0x10(%rsp)
movq %r13, 0x38(%rsp)
cmpq $0x0, 0x48(%r14)
je 0x291ed
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 0x2906a
jmp 0x291cc
movq 0x60(%rsp), %rbp
jmp 0x291b0
movq %rbx, %rbp
leaq 0x40(%rsp), %rdi
callq 0x24ef2
movq %rbp, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xb160
callq 0xb160
jmp 0x29218
jmp 0x29218
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2921b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x2921b
jmp 0x29218
jmp 0x29218
jmp 0x29218
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x24ef2
movq %rbx, %rdi
callq 0xb510
nop