// File: CMU-SAFARI[P]ramulator/src/Processor.cpp
// Function: ramulator::Core::tick()
void Core::tick()
{
    clk++;

    if(first_level_cache != nullptr)
        first_level_cache->tick();

    retired += window.retire();

    if (expected_limit_insts == 0 && !more_reqs) return;

    // bubbles (non-memory operations)
    int inserted = 0;
    while (bubble_cnt > 0) {
        if (inserted == window.ipc) return;
        if (window.is_full()) return;

        window.insert(true, -1);
        inserted++;
        bubble_cnt--;
        cpu_inst++;
        if (long(cpu_inst.value()) == expected_limit_insts && !reached_limit) {
          record_cycs = clk;
          record_insts = long(cpu_inst.value());
          memory.record_core(id);
          reached_limit = true;
        }
    }

    if (req_type == Request::Type::READ) {
        // read request
        if (inserted == window.ipc) return;
        if (window.is_full()) return;

        Request req(req_addr, req_type, callback, id);
        if (!send(req)) return;

        window.insert(false, req_addr);
        cpu_inst++;
    }
    else {
        // write request
        assert(req_type == Request::Type::WRITE);
        Request req(req_addr, req_type, callback, id);
        if (!send(req)) return;
        cpu_inst++;
    }
    if (long(cpu_inst.value()) == expected_limit_insts && !reached_limit) {
      record_cycs = clk;
      record_insts = long(cpu_inst.value());
      memory.record_core(id);
      reached_limit = true;
    }

    if (no_core_caches) {
      more_reqs = trace.get_filtered_request(
          bubble_cnt, req_addr, req_type);
      if (req_addr != -1) {
        req_addr = memory.page_allocator(req_addr, id);
      }
    } else {
      more_reqs = trace.get_unfiltered_request(
          bubble_cnt, req_addr, req_type);
      if (req_addr != -1) {
        req_addr = memory.page_allocator(req_addr, id);
      }
    }
    if (!more_reqs) {
      if (!reached_limit) { // if the length of this trace is shorter than expected length, then record it when the whole trace finishes, and set reached_limit to true.
        // Hasan: overriding this behavior. We start the trace from the
        // beginning until the requested amount of instructions are
        // simulated. This should never be reached now.
        assert((expected_limit_insts == 0) && "Shouldn't be reached when expected_limit_insts > 0 since we start over the trace");
        record_cycs = clk;
        record_insts = long(cpu_inst.value());
        memory.record_core(id);
        reached_limit = true;
      }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x208(%rbp)
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
cmpq $0x0, 0x488(%rax)
je 0x1f7af1
movq -0x208(%rbp), %rax
movq 0x488(%rax), %rdi
callq 0x17ed50
movq -0x208(%rbp), %rdi
addq $0x410, %rdi            # imm = 0x410
callq 0x1f9c60
movq %rax, %rcx
movq -0x208(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x1d0(%rax)
jne 0x1f7b35
movq -0x208(%rbp), %rax
testb $0x1, 0x47c(%rax)
jne 0x1f7b35
jmp 0x1f82d9
movl $0x0, -0xc(%rbp)
movq -0x208(%rbp), %rax
cmpq $0x0, 0x468(%rax)
jle 0x1f7c80
movq -0x208(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x410(%rcx), %eax
jne 0x1f7b68
jmp 0x1f82d9
movq -0x208(%rbp), %rdi
addq $0x410, %rdi            # imm = 0x410
callq 0x1f9d50
testb $0x1, %al
jne 0x1f7b81
jmp 0x1f7b86
jmp 0x1f82d9
movq -0x208(%rbp), %rdi
addq $0x410, %rdi            # imm = 0x410
movl $0x1, %esi
movq $-0x1, %rdx
callq 0x1f9d70
movq -0x208(%rbp), %rdi
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
movq 0x468(%rdi), %rax
addq $-0x1, %rax
movq %rax, 0x468(%rdi)
addq $0x528, %rdi            # imm = 0x528
xorl %esi, %esi
callq 0x40970
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
callq 0x485e0
movq -0x208(%rbp), %rcx
cvttsd2si %xmm0, %rax
cmpq 0x1d0(%rcx), %rax
jne 0x1f7c7b
movq -0x208(%rbp), %rax
testb $0x1, 0x1d8(%rax)
jne 0x1f7c7b
movq -0x208(%rbp), %rsi
movq %rsi, %rdi
addq $0xa0, %rdi
callq 0x386a0
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
callq 0x485e0
movq -0x208(%rbp), %rdi
cvttsd2si %xmm0, %rax
movq %rax, -0x18(%rbp)
addq $0x138, %rdi            # imm = 0x138
leaq -0x18(%rbp), %rsi
callq 0x386a0
movq -0x208(%rbp), %rax
movq 0x5c0(%rax), %rdi
movl 0x10(%rax), %esi
movq (%rdi), %rax
callq *0x40(%rax)
movq -0x208(%rbp), %rax
movb $0x1, 0x1d8(%rax)
jmp 0x1f7b3c
movq -0x208(%rbp), %rax
cmpl $0x0, 0x478(%rax)
jne 0x1f7e74
movq -0x208(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x410(%rcx), %eax
jne 0x1f7cab
jmp 0x1f82d9
movq -0x208(%rbp), %rdi
addq $0x410, %rdi            # imm = 0x410
callq 0x1f9d50
testb $0x1, %al
jne 0x1f7cc4
jmp 0x1f7cc9
jmp 0x1f82d9
movq -0x208(%rbp), %rsi
movq 0x470(%rsi), %rax
movq %rax, -0x220(%rbp)
movl 0x478(%rsi), %eax
movl %eax, -0x214(%rbp)
addq $0x38, %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x42940
movq -0x208(%rbp), %rax
movq -0x220(%rbp), %rsi
movl -0x214(%rbp), %edx
movq -0x210(%rbp), %rcx
movl 0x10(%rax), %r8d
leaq -0x78(%rbp), %rdi
callq 0x4caa0
jmp 0x1f7d2b
leaq -0x98(%rbp), %rdi
callq 0x367b0
movq -0x208(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x228(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x42ba0
jmp 0x1f7d5b
movq -0x228(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x180d60
movb %al, -0x229(%rbp)
jmp 0x1f7d76
movb -0x229(%rbp), %al
xorb $-0x1, %al
movb %al, -0x22a(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x36780
movb -0x22a(%rbp), %al
testb $0x1, %al
jne 0x1f7d9c
jmp 0x1f7e05
movl $0x1, -0x10c(%rbp)
jmp 0x1f7e47
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x367b0
jmp 0x1f82e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x1f7e66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x36780
jmp 0x1f7e66
movq -0x208(%rbp), %rax
movq %rax, %rdi
addq $0x410, %rdi            # imm = 0x410
movq 0x470(%rax), %rdx
xorl %esi, %esi
callq 0x1f9d70
jmp 0x1f7e26
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
xorl %esi, %esi
callq 0x40970
jmp 0x1f7e3d
movl $0x0, -0x10c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x36780
movl -0x10c(%rbp), %eax
testl %eax, %eax
je 0x1f7e61
jmp 0x1f7e5c
jmp 0x1f82d9
jmp 0x1f8032
leaq -0x78(%rbp), %rdi
callq 0x36780
jmp 0x1f82e2
movq -0x208(%rbp), %rax
cmpl $0x1, 0x478(%rax)
jne 0x1f7e86
jmp 0x1f7ea5
leaq 0x693ce(%rip), %rdi     # 0x26125b
leaq 0x69120(%rip), %rsi     # 0x260fb4
movl $0xfd, %edx
leaq 0x693dc(%rip), %rcx     # 0x26127c
callq 0xc280
movq -0x208(%rbp), %rsi
movq 0x470(%rsi), %rax
movq %rax, -0x248(%rbp)
movl 0x478(%rsi), %eax
movl %eax, -0x23c(%rbp)
addq $0x38, %rsi
leaq -0x190(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x42940
movq -0x208(%rbp), %rax
movq -0x248(%rbp), %rsi
movl -0x23c(%rbp), %edx
movq -0x238(%rbp), %rcx
movl 0x10(%rax), %r8d
leaq -0x170(%rbp), %rdi
callq 0x4caa0
jmp 0x1f7f0a
leaq -0x190(%rbp), %rdi
callq 0x367b0
movq -0x208(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x250(%rbp)
leaq -0x1f0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x42ba0
jmp 0x1f7f3d
movq -0x250(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x180d60
movb %al, -0x251(%rbp)
jmp 0x1f7f58
movb -0x251(%rbp), %al
xorb $-0x1, %al
movb %al, -0x252(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x36780
movb -0x252(%rbp), %al
testb $0x1, %al
jne 0x1f7f7e
jmp 0x1f7fe1
movl $0x1, -0x10c(%rbp)
jmp 0x1f8002
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x367b0
jmp 0x1f82e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x1f8021
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x36780
jmp 0x1f8021
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
xorl %esi, %esi
callq 0x40970
jmp 0x1f7ff8
movl $0x0, -0x10c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x36780
movl -0x10c(%rbp), %eax
testl %eax, %eax
je 0x1f801f
jmp 0x1f801a
jmp 0x1f82d9
jmp 0x1f8032
leaq -0x170(%rbp), %rdi
callq 0x36780
jmp 0x1f82e2
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
callq 0x485e0
movq -0x208(%rbp), %rcx
cvttsd2si %xmm0, %rax
cmpq 0x1d0(%rcx), %rax
jne 0x1f80e2
movq -0x208(%rbp), %rax
testb $0x1, 0x1d8(%rax)
jne 0x1f80e2
movq -0x208(%rbp), %rsi
movq %rsi, %rdi
addq $0xa0, %rdi
callq 0x386a0
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
callq 0x485e0
movq -0x208(%rbp), %rdi
cvttsd2si %xmm0, %rax
movq %rax, -0x1f8(%rbp)
addq $0x138, %rdi            # imm = 0x138
leaq -0x1f8(%rbp), %rsi
callq 0x386a0
movq -0x208(%rbp), %rax
movq 0x5c0(%rax), %rdi
movl 0x10(%rax), %esi
movq (%rdi), %rax
callq *0x40(%rax)
movq -0x208(%rbp), %rax
movb $0x1, 0x1d8(%rax)
movq -0x208(%rbp), %rax
testb $0x1, 0x58(%rax)
je 0x1f816d
movq -0x208(%rbp), %rcx
movq %rcx, %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
movq %rcx, %rsi
addq $0x468, %rsi            # imm = 0x468
movq %rcx, %rdx
addq $0x470, %rdx            # imm = 0x470
addq $0x478, %rcx            # imm = 0x478
callq 0x1f9610
movb %al, %cl
movq -0x208(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x47c(%rax)
cmpq $-0x1, 0x470(%rax)
je 0x1f816b
movq -0x208(%rbp), %rax
movq 0x5c0(%rax), %rdi
movq 0x470(%rax), %rsi
movl 0x10(%rax), %edx
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %rcx
movq -0x208(%rbp), %rax
movq %rcx, 0x470(%rax)
jmp 0x1f81eb
movq -0x208(%rbp), %rcx
movq %rcx, %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
movq %rcx, %rsi
addq $0x468, %rsi            # imm = 0x468
movq %rcx, %rdx
addq $0x470, %rdx            # imm = 0x470
addq $0x478, %rcx            # imm = 0x478
callq 0x1f9920
movb %al, %cl
movq -0x208(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x47c(%rax)
cmpq $-0x1, 0x470(%rax)
je 0x1f81e9
movq -0x208(%rbp), %rax
movq 0x5c0(%rax), %rdi
movq 0x470(%rax), %rsi
movl 0x10(%rax), %edx
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %rcx
movq -0x208(%rbp), %rax
movq %rcx, 0x470(%rax)
jmp 0x1f81eb
movq -0x208(%rbp), %rax
testb $0x1, 0x47c(%rax)
jne 0x1f82d9
movq -0x208(%rbp), %rax
testb $0x1, 0x1d8(%rax)
jne 0x1f82d7
movq -0x208(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x1d0(%rcx)
movb %al, -0x253(%rbp)
jne 0x1f8236
movb $0x1, %al
movb %al, -0x253(%rbp)
jmp 0x1f8236
movb -0x253(%rbp), %al
testb $0x1, %al
jne 0x1f8242
jmp 0x1f8244
jmp 0x1f8263
leaq 0x6909f(%rip), %rdi     # 0x2612ea
leaq 0x68d62(%rip), %rsi     # 0x260fb4
movl $0x11b, %edx            # imm = 0x11B
leaq 0x6901e(%rip), %rcx     # 0x26127c
callq 0xc280
movq -0x208(%rbp), %rsi
movq %rsi, %rdi
addq $0xa0, %rdi
callq 0x386a0
movq -0x208(%rbp), %rdi
addq $0x528, %rdi            # imm = 0x528
callq 0x485e0
movq -0x208(%rbp), %rdi
cvttsd2si %xmm0, %rax
movq %rax, -0x200(%rbp)
addq $0x138, %rdi            # imm = 0x138
leaq -0x200(%rbp), %rsi
callq 0x386a0
movq -0x208(%rbp), %rax
movq 0x5c0(%rax), %rdi
movl 0x10(%rax), %esi
movq (%rdi), %rax
callq *0x40(%rax)
movq -0x208(%rbp), %rax
movb $0x1, 0x1d8(%rax)
jmp 0x1f82d9
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0xc5d0
nop