// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]ramulator/src/Main.cpp
// Function: void run_dramtrace<ramulator::DDR3>(ramulator::Config const&, ramulator::Memory<ramulator::DDR3, ramulator::Controller>&, char const*)
void run_dramtrace(const Config& configs, Memory<T, Controller>& memory, const char* tracename) {

    /* initialize DRAM trace */
    Trace trace(tracename);

    /* run simulation */
    bool stall = false, end = false;
    int reads = 0, writes = 0, clks = 0;
    long addr = 0;
    Request::Type type = Request::Type::READ;
    map<int, int> latencies;
    auto read_complete = [&latencies](Request& r){latencies[r.depart - r.arrive]++;};

    Request req(addr, type, read_complete);

    while (!end || memory.pending_requests()){
        if (!end && !stall){
            end = !trace.get_dramtrace_request(addr, type);
        }

        if (!end){
            req.addr = addr;
            req.type = type;
            stall = !memory.send(req);
            if (!stall){
                if (type == Request::Type::READ) reads++;
                else if (type == Request::Type::WRITE) writes++;
            }
        }
        else {
            memory.set_high_writeq_watermark(0.0f); // make sure that all write requests in the
                                                    // write queue are drained
        }

        memory.tick();
        clks ++;
        Stats::curTick++; // memory clock, global, for Statistics
    }
    // This a workaround for statistics set only initially lost in the end
    memory.finish();
    Stats::statlist.printall();

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rsi, %rbx
leaq 0x128(%rsp), %rdi
movq %rdx, %rsi
callq 0x1314f0
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl %eax, 0x4(%rsp)
leaq 0x100(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rcx
movaps %xmm0, (%rcx)
movaps %xmm0, 0x10(%rcx)
leaq 0xf8(%rsp), %rax
movq %rax, (%rcx)
leaq 0xbd44(%rip), %rax      # 0x2c8c4
movq %rax, 0x18(%rcx)
leaq 0xbd53(%rip), %rax      # 0x2c8de
movq %rax, 0x10(%rcx)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2c85e
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x20bb9
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0xa0(%rsp), %r14
leaq 0x40(%rsp), %r15
leaq 0xd0(%rsp), %r12
xorl %ebp, %ebp
xorl %r13d, %r13d
testb $0x1, %r13b
je 0x20beb
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x20c0f
jmp 0x20ce2
testb $0x1, %bpl
jne 0x20c0f
leaq 0x128(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
callq 0x1315b2
movl %eax, %r13d
xorb $0x1, %r13b
testb $0x1, %r13b
je 0x20c26
movq (%rbx), %rax
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq *0x48(%rax)
jmp 0x20cca
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x24b6e
movq 0x28(%r15), %rax
movq %rax, 0x28(%r14)
movups 0x18(%r15), %xmm0
movups %xmm0, 0x18(%r14)
movq %r12, %rdi
leaq 0x70(%rsp), %rsi
callq 0x295b4
movq (%rbx), %rax
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq *0x20(%rax)
movl %eax, %ebp
movq 0xe0(%rsp), %rax
testq %rax, %rax
je 0x20ca9
movq %r12, %rdi
movq %r12, %rsi
movl $0x3, %edx
callq *%rax
xorb $0x1, %bpl
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x20cca
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
leaq 0x18461e(%rip), %rax    # 0x1a52f8
incq (%rax)
jmp 0x20bd3
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
leaq 0x1843ee(%rip), %rdi    # 0x1a50e0
callq 0x2bb46
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x20d13
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x20d2a
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
leaq 0xf8(%rsp), %rdi
callq 0x2cb6e
leaq 0x348(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20d58
movq 0x348(%rsp), %rsi
incq %rsi
callq 0xb2f0
leaq 0x130(%rsp), %rdi
callq 0xb0d0
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x20d9d
jmp 0x20d9d
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x20de6
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x20de6
jmp 0x20d9d
jmp 0x20dd9
movq %rax, %rdi
callq 0x1718f
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x20ddc
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x20ddc
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x25bb6
jmp 0x20ddc
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x25bb6
leaq 0xf8(%rsp), %rdi
callq 0x2cb6e
leaq 0x128(%rsp), %rdi
callq 0x2c7b8
movq %rbx, %rdi
callq 0xb510