// File: /workspace/github_cpp_makefile_star2_O2/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 0xbb470
andq $0x0, 0x10(%rsp)
andl $0x0, 0xc(%rsp)
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x18(%rsp), %rcx
andq $0x0, 0x8(%rcx)
leaq 0x38(%rsp), %rax
movq %rax, (%rcx)
leaq 0xa449(%rip), %rax      # 0x22786
movq %rax, 0x18(%rcx)
leaq 0xa444(%rip), %rax      # 0x2278c
movq %rax, 0x10(%rcx)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x22732
leaq 0x18(%rsp), %rdi
callq 0x96b6a
xorl %r13d, %r13d
leaq 0x11ef87(%rip), %rbp    # 0x1372f8
leaq 0xc8(%rsp), %r14
leaq 0x68(%rsp), %r15
xorl %r12d, %r12d
testb $0x1, %r12b
je 0x18399
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x183bd
jmp 0x1841e
testb $0x1, %r13b
jne 0x183bd
leaq 0x128(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0xbb510
movl %eax, %r12d
xorb $0x1, %r12b
testb $0x1, %r12b
je 0x183d1
movq (%rbx), %rax
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq *0x48(%rax)
jmp 0x1840c
movq 0x10(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x94(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2000a
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movl %eax, %r13d
movq %r14, %rdi
callq 0x1cc14
xorb $0x1, %r13b
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
incq (%rbp)
jmp 0x18381
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
leaq 0x11ecb2(%rip), %rdi    # 0x1370e0
callq 0x22084
leaq 0x68(%rsp), %rdi
callq 0x1cc14
leaq 0x38(%rsp), %rdi
callq 0x22ab0
leaq 0x128(%rsp), %rdi
callq 0x2260e
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x96b6a
jmp 0x18496
jmp 0x18489
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x1cc14
jmp 0x1848c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x1cc14
leaq 0x38(%rsp), %rdi
callq 0x22ab0
leaq 0x128(%rsp), %rdi
callq 0x2260e
movq %rbx, %rdi
callq 0xb4d0
nop