// File: /workspace/github_cpp_makefile_star2_O3/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 0x12e2fe
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)
leaq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
leaq 0xf8(%rsp), %rax
movq %rax, (%rcx)
leaq 0xbfd6(%rip), %rax      # 0x2caca
movq %rax, 0x18(%rcx)
leaq 0xbfe5(%rip), %rax      # 0x2cae4
movq %rax, 0x10(%rcx)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2ca64
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x20b2d
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0xa0(%rsp), %r15
leaq 0x40(%rsp), %r12
leaq 0xd0(%rsp), %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
testb $0x1, %r14b
je 0x20b5f
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x20b83
jmp 0x20c58
testb $0x1, %bpl
jne 0x20b83
leaq 0x128(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
callq 0x12e3c0
movl %eax, %r14d
xorb $0x1, %r14b
testb $0x1, %r14b
je 0x20b9a
movq (%rbx), %rax
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq *0x48(%rax)
jmp 0x20c40
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 %r15, %rdi
movq %r12, %rsi
callq 0x24b42
movq 0x28(%r12), %rax
movq %rax, 0x28(%r15)
movups 0x18(%r12), %xmm0
movups %xmm0, 0x18(%r15)
movq %r13, %rdi
leaq 0x70(%rsp), %rsi
callq 0x29968
movq (%rbx), %rax
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq *0x20(%rax)
movl %eax, %ebp
movq 0xe0(%rsp), %rax
testq %rax, %rax
je 0x20c1f
movq %r13, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq *%rax
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x20c3c
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
xorb $0x1, %bpl
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
leaq 0x1806a8(%rip), %rax    # 0x1a12f8
incq (%rax)
jmp 0x20b47
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
leaq 0x180478(%rip), %rdi    # 0x1a10e0
callq 0x2bd4e
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x20c89
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x20ca0
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
leaq 0xf8(%rsp), %rdi
callq 0x2cd74
leaq 0x348(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20cce
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 0x20d13
jmp 0x20d13
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x20d5c
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x20d5c
jmp 0x20d13
jmp 0x20d4f
movq %rax, %rdi
callq 0x171a3
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x20d52
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x20d52
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x25c78
jmp 0x20d52
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x25c78
leaq 0xf8(%rsp), %rdi
callq 0x2cd74
leaq 0x128(%rsp), %rdi
callq 0x2c9b8
movq %rbx, %rdi
callq 0xb510