// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]ramulator/src/Controller.h
// Function: ramulator::Controller<ramulator::DDR3>::tick()
void tick()
    {
        clk++;
        req_queue_length_sum += readq.size() + writeq.size() + pending.size();
        read_req_queue_length_sum += readq.size() + pending.size();
        write_req_queue_length_sum += writeq.size();

        /*** 1. Serve completed reads ***/
        if (pending.size()) {
            Request& req = pending[0];
            if (req.depart <= clk) {
                if (req.depart - req.arrive > 1) { // this request really accessed a row
                  read_latency_sum += req.depart - req.arrive;
                  channel->update_serving_requests(
                      req.addr_vec.data(), -1, clk);
                }
                req.callback(req);
                pending.pop_front();
            }
        }

        /*** 2. Refresh scheduler ***/
        refresh->tick_ref();

        /*** 3. Should we schedule writes? ***/
        if (!write_mode) {
            // yes -- write queue is almost full or read queue is empty
            if (writeq.size() > int(wr_high_watermark * writeq.max) || readq.size() == 0)
                write_mode = true;
        }
        else {
            // no -- write queue is almost empty and read queue is not empty
            if (writeq.size() < int(wr_low_watermark * writeq.max) && readq.size() != 0)
                write_mode = false;
        }

        /*** 4. Find the best command to schedule, if any ***/

        // First check the actq (which has higher priority) to see if there
        // are requests available to service in this cycle
        Queue* queue = &actq;
        typename T::Command cmd;
        auto req = scheduler->get_head(queue->q);

        bool is_valid_req = (req != queue->q.end());

        if(is_valid_req) {
            cmd = get_first_cmd(req);
            is_valid_req = is_ready(cmd, req->addr_vec);
        }

        if (!is_valid_req) {
            queue = !write_mode ? &readq : &writeq;

            if (otherq.size())
                queue = &otherq;  // "other" requests are rare, so we give them precedence over reads/writes

            req = scheduler->get_head(queue->q);

            is_valid_req = (req != queue->q.end());

            if(is_valid_req){
                cmd = get_first_cmd(req);
                is_valid_req = is_ready(cmd, req->addr_vec);
            }
        }

        if (!is_valid_req) {
            // we couldn't find a command to schedule -- let's try to be speculative
            auto cmd = T::Command::PRE;
            vector<int> victim = rowpolicy->get_victim(cmd);
            if (!victim.empty()){
                issue_cmd(cmd, victim);
            }
            return;  // nothing more to be done this cycle
        }

        if (req->is_first_command) {
            req->is_first_command = false;
            int coreid = req->coreid;
            if (req->type == Request::Type::READ || req->type == Request::Type::WRITE) {
              channel->update_serving_requests(req->addr_vec.data(), 1, clk);
            }
            int tx = (channel->spec->prefetch_size * channel->spec->channel_width / 8);
            if (req->type == Request::Type::READ) {
                if (is_row_hit(req)) {
                    ++read_row_hits[coreid];
                    ++row_hits;
                } else if (is_row_open(req)) {
                    ++read_row_conflicts[coreid];
                    ++row_conflicts;
                } else {
                    ++read_row_misses[coreid];
                    ++row_misses;
                }
              read_transaction_bytes += tx;
            } else if (req->type == Request::Type::WRITE) {
              if (is_row_hit(req)) {
                  ++write_row_hits[coreid];
                  ++row_hits;
              } else if (is_row_open(req)) {
                  ++write_row_conflicts[coreid];
                  ++row_conflicts;
              } else {
                  ++write_row_misses[coreid];
                  ++row_misses;
              }
              write_transaction_bytes += tx;
            }
        }

        // issue command on behalf of request
        issue_cmd(cmd, get_addr_vec(cmd, req));

        // check whether this is the last command (which finishes the request)
        //if (cmd != channel->spec->translate[int(req->type)]){
        if (cmd != channel->spec->translate[int(req->type)]) {
            if(channel->spec->is_opening(cmd)) {
                // promote the request that caused issuing activation to actq
                actq.q.push_back(*req);
                queue->q.erase(req);
            }

            return;
        }

        // set a future completion time for read requests
        if (req->type == Request::Type::READ) {
            req->depart = clk + channel->spec->read_latency;
            pending.push_back(*req);
        }

        if (req->type == Request::Type::WRITE) {
            channel->update_serving_requests(req->addr_vec.data(), -1, clk);
            // req->callback(*req);
        }

        // remove request from queue
        queue->q.erase(req);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x1090(%rdi), %rcx
movq 0x10d0(%rdi), %rsi
incq %rcx
movq %rcx, 0x1090(%rdi)
movl 0x10f0(%rdi), %eax
leal (%rax,%rsi), %r8d
movq 0x1188(%rdi), %rdx
movq %rdx, %rdi
subq 0x1168(%rbx), %rdi
sarq $0x3, %rdi
cmpq $0x1, %rdx
adcq $-0x1, %rdi
leaq (%rdi,%rdi,4), %rdi
movq 0x1170(%rbx), %r9
subq 0x1178(%rbx), %r9
sarq $0x5, %r9
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %r9
addq %rdi, %r9
movq 0x1160(%rbx), %rdi
movq 0x1150(%rbx), %r15
subq %r15, %rdi
sarq $0x5, %rdi
imulq %rdx, %rdi
addq %r9, %rdi
addq %rdi, %r8
movq %r8, %xmm0
movq 0x124e3d(%rip), %xmm1   # 0x14d040
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x124e41(%rip), %xmm2   # 0x14d050
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addsd 0x9e8(%rbx), %xmm3
movsd %xmm3, 0x9e8(%rbx)
movl %esi, %esi
addq %rdi, %rsi
movq %rsi, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
addsd 0xb18(%rbx), %xmm1
leaq 0x1140(%rbx), %r14
movsd %xmm1, 0xb18(%rbx)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addsd 0xc48(%rbx), %xmm0
movsd %xmm0, 0xc48(%rbx)
testq %rdi, %rdi
je 0x28338
movq %r15, %rsi
subq 0x1158(%rbx), %rsi
movq %rsi, %rax
sarq $0x5, %rax
imulq %rdx, %rax
cmpq $0x5, %rax
jb 0x282da
movq 0x1168(%rbx), %rdi
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
testq %rsi, %rsi
jle 0x282bf
mulq %rdx
shrq $0x2, %rdx
jmp 0x282cc
notq %rax
mulq %rdx
shrq $0x2, %rdx
notq %rdx
imulq $-0x1e0, %rdx, %r15     # imm = 0xFE20
addq (%rdi,%rdx,8), %r15
addq %rsi, %r15
movq 0x38(%r15), %rax
cmpq %rcx, %rax
jg 0x28338
subq 0x30(%r15), %rax
cmpq $0x2, %rax
jl 0x2831a
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addsd 0x8b8(%rbx), %xmm0
movsd %xmm0, 0x8b8(%rbx)
movq 0x1098(%rbx), %rdi
movq 0x10(%r15), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x287d4
cmpq $0x0, 0x50(%r15)
je 0x287ab
leaq 0x40(%r15), %rdi
movq %r15, %rsi
callq *0x58(%r15)
movq %r14, %rdi
callq 0x28a00
movq 0x10b8(%rbx), %rdi
callq 0x28a2e
movl 0x10f8(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
movl 0x10d0(%rbx), %ecx
movl 0x10f0(%rbx), %eax
cmpb $0x0, 0x1190(%rbx)
je 0x28386
testq %rcx, %rcx
mulss 0x1198(%rbx), %xmm0
cvttss2si %xmm0, %ecx
sete %dl
cmpl %ecx, %eax
setae %al
orb %dl, %al
jne 0x283a9
xorl %eax, %eax
jmp 0x283a3
testq %rcx, %rcx
mulss 0x1194(%rbx), %xmm0
sete %cl
cvttss2si %xmm0, %edx
cmpl %edx, %eax
seta %al
orb %cl, %al
movb $0x1, %al
je 0x283a9
movb %al, 0x1190(%rbx)
leaq 0x1100(%rbx), %r12
movq 0x10a0(%rbx), %rdi
movq %r12, %rsi
callq 0x28a66
movq %rax, %r15
cmpq %r12, %rax
setne %al
je 0x28409
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x244fc
movl %eax, %ebp
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq 0x20(%r15), %rdx
movl %eax, %esi
callq 0x244a0
jmp 0x28409
testb %al, %al
je 0x28412
movq %r12, %r13
jmp 0x2848d
movzbl 0x1190(%rbx), %eax
shll $0x5, %eax
orq $0x10c0, %rax           # imm = 0x10C0
cmpl $0x0, 0x1130(%rbx)
movl $0x1120, %r13d          # imm = 0x1120
cmoveq %rax, %r13
addq %rbx, %r13
movq 0x10a0(%rbx), %rdi
movq %r13, %rsi
callq 0x28a66
movq %rax, %r15
cmpq %r13, %rax
setne %al
je 0x2848d
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x244fc
movl %eax, %ebp
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq 0x20(%r15), %rdx
movl %eax, %esi
callq 0x244a0
testb %al, %al
je 0x2854c
movq %r14, 0x40(%rsp)
cmpb $0x1, 0x10(%r15)
jne 0x286d7
movb $0x0, 0x10(%r15)
movl 0x38(%r15), %r14d
cmpl $0x1, 0x3c(%r15)
ja 0x284d1
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq 0x20(%r15), %rsi
movl $0x1, %edx
callq 0x287d4
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movl 0x2744(%rax), %ecx
imull 0x2740(%rax), %ecx
leal 0x7(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
movl %edx, 0x20(%rsp)
movl 0x3c(%r15), %ecx
cmpl $0x1, %ecx
je 0x285c2
testl %ecx, %ecx
jne 0x286d7
movl %r14d, 0xc(%rsp)
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x24a20
testb %al, %al
je 0x285fd
movl $0x70, %eax
movq %rax, 0x18(%rsp)
movl $0x1a0, %eax            # imm = 0x1A0
movq %rax, 0x10(%rsp)
movl $0x2f8, %edi            # imm = 0x2F8
jmp 0x28686
movq 0x10a8(%rbx), %rax
movslq 0x8(%rax), %rcx
shlq $0x5, %rcx
movl $0x1, 0x24(%rsp)
cmpq $0x0, 0x20(%rax,%rcx)
je 0x287ab
leaq (%rax,%rcx), %rsi
addq $0x10, %rsi
leaq 0x28(%rsp), %r14
leaq 0x24(%rsp), %rdx
movq %r14, %rdi
callq *0x18(%rsi)
movq (%r14), %rax
cmpq 0x8(%r14), %rax
je 0x285a2
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x28e40
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x2879c
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x2879c
movl %r14d, 0xc(%rsp)
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x24a20
testb %al, %al
je 0x28637
movl $0x108, %eax            # imm = 0x108
movq %rax, 0x18(%rsp)
movl $0x1a0, %eax            # imm = 0x1A0
movq %rax, 0x10(%rsp)
movl $0x508, %edi            # imm = 0x508
jmp 0x28686
movl $0x70, %eax
movq %rax, 0x18(%rsp)
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x2aa5e
testb %al, %al
movl $0x458, %eax            # imm = 0x458
movl $0x3a8, %edi            # imm = 0x3A8
jmp 0x2866f
movl $0x108, %eax            # imm = 0x108
movq %rax, 0x18(%rsp)
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r15), %rcx
movl 0x1c4(%rax,%rcx,4), %esi
movq 0x20(%r15), %rdx
callq 0x2aa5e
testb %al, %al
movl $0x668, %eax            # imm = 0x668
movl $0x5b8, %edi            # imm = 0x5B8
cmovneq %rax, %rdi
movl $0x2d0, %eax            # imm = 0x2D0
movl $0x238, %ecx            # imm = 0x238
cmovneq %rax, %rcx
movq %rcx, 0x10(%rsp)
movl 0x20(%rsp), %r14d
sarl $0x3, %r14d
addq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x2aa1c
movsd 0x1249d5(%rip), %xmm0   # 0x14d078
movsd 0x70(%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rax)
movq 0x10(%rsp), %rax
addsd (%rbx,%rax), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
movsd %xmm0, (%rbx,%rax)
movq 0x18(%rsp), %rax
addsd (%rbx,%rax), %xmm1
movsd %xmm1, (%rbx,%rax)
leaq 0x20(%r15), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x24b6e
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x28e40
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
movq 0x40(%rsp), %r14
je 0x28711
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
leaq 0x10(%r15), %rsi
movq 0x1098(%rbx), %rax
movq 0x390(%rax), %rax
movslq 0x3c(%r15), %rcx
cmpl 0x1c4(%rax,%rcx,4), %ebp
jne 0x28773
testl %ecx, %ecx
jne 0x2874e
movslq 0x2dd8(%rax), %rax
addq 0x1090(%rbx), %rax
movq %rax, 0x48(%r15)
movq %r14, %rdi
callq 0x290d6
cmpl $0x1, 0x3c(%r15)
jne 0x28791
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq 0x20(%r15), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x287d4
jmp 0x28791
testl %ebp, %ebp
jne 0x2879c
movq %r12, %rdi
callq 0x2aaac
movq %rax, %rdi
movq %r12, %rsi
callq 0xb080
incq 0x1110(%rbx)
movq %r13, %rdi
movq %r15, %rsi
callq 0x29984
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xb160
jmp 0x287b2
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x287cc
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510