// File: CMU-SAFARI[P]ramulator/src/DRAM.h
// Function: ramulator::DRAM<ramulator::DDR3>::update_serving_requests(int const*, int, long)
void DRAM<T>::update_serving_requests(const int* addr, int delta, long clk) {
  assert(id == addr[int(level)]);
  assert(delta == 1 || delta == -1);
  // update total serving requests
  if (begin_of_cur_reqcnt != -1 && cur_serving_requests > 0) {
    serving_requests += (clk - begin_of_cur_reqcnt) * cur_serving_requests;
    active_cycles += clk - begin_of_cur_reqcnt;
  }
  // update begin of current request number
  begin_of_cur_reqcnt = clk;
  cur_serving_requests += delta;
  assert(cur_serving_requests >= 0);

  if (delta == 1 && cur_serving_requests == 1) {
    // transform from inactive to active
    begin_of_serving = clk;
    if (end_of_refreshing > begin_of_serving) {
      active_refresh_overlap_cycles += end_of_refreshing - begin_of_serving;
    }
  } else if (cur_serving_requests == 0) {
    // transform from active to inactive
    assert(begin_of_serving != -1);
    assert(delta == -1);
    active_cycles += clk - begin_of_cur_reqcnt;
    end_of_serving = clk;

    for (const auto& ref: refresh_intervals) {
      active_refresh_overlap_cycles += min(end_of_serving, ref.second) - ref.first;
    }
    refresh_intervals.clear();
  }

  int child_id = addr[int(level) + 1];
  // We only count the level bank or the level higher than bank
  if (child_id < 0 || !children.size() || (int(level) > int(T::Level::Bank)) ) {
    return;
  }
  children[child_id]->update_serving_requests(addr, delta, clk);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdx
movq %rdx, -0x78(%rbp)
movl 0x39c(%rdx), %eax
movq -0x10(%rbp), %rcx
movslq 0x398(%rdx), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x40bea
jmp 0x40c09
leaq 0x2104ff(%rip), %rdi    # 0x2510f0
leaq 0x21050f(%rip), %rsi    # 0x251107
movl $0x19a, %edx            # imm = 0x19A
leaq 0x21050e(%rip), %rcx    # 0x251112
callq 0xc280
movb $0x1, %al
cmpl $0x1, -0x14(%rbp)
movb %al, -0x79(%rbp)
je 0x40c1e
cmpl $-0x1, -0x14(%rbp)
sete %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x40c27
jmp 0x40c29
jmp 0x40c48
leaq 0x21054f(%rip), %rdi    # 0x25117f
leaq 0x2104d0(%rip), %rsi    # 0x251107
movl $0x19b, %edx            # imm = 0x19B
leaq 0x2104cf(%rip), %rcx    # 0x251112
callq 0xc280
movq -0x78(%rbp), %rax
cmpq $-0x1, 0x418(%rax)
je 0x40cad
movq -0x78(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jle 0x40cad
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rax
subq 0x418(%rdi), %rax
movslq 0x400(%rdi), %rcx
imulq %rcx, %rax
movq %rax, -0x28(%rbp)
addq $0x260, %rdi            # imm = 0x260
leaq -0x28(%rbp), %rsi
callq 0x40b80
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rax
subq 0x418(%rdi), %rax
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rsi
callq 0x40b80
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x418(%rax)
movl -0x14(%rbp), %ecx
addl 0x400(%rax), %ecx
movl %ecx, 0x400(%rax)
cmpl $0x0, 0x400(%rax)
jl 0x40cd6
jmp 0x40cf5
leaq 0x2104bc(%rip), %rdi    # 0x251199
leaq 0x210423(%rip), %rsi    # 0x251107
movl $0x1a4, %edx            # imm = 0x1A4
leaq 0x210422(%rip), %rcx    # 0x251112
callq 0xc280
cmpl $0x1, -0x14(%rbp)
jne 0x40d52
movq -0x78(%rbp), %rax
cmpl $0x1, 0x400(%rax)
jne 0x40d52
movq -0x78(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rax, 0x408(%rcx)
movq 0x428(%rcx), %rax
cmpq 0x408(%rcx), %rax
jle 0x40d4d
movq -0x78(%rbp), %rdi
movq 0x428(%rdi), %rax
subq 0x408(%rdi), %rax
movq %rax, -0x38(%rbp)
addq $0x1c8, %rdi            # imm = 0x1C8
leaq -0x38(%rbp), %rsi
callq 0x40b80
jmp 0x40e7f
movq -0x78(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x40e7d
movq -0x78(%rbp), %rax
cmpq $-0x1, 0x408(%rax)
je 0x40d73
jmp 0x40d92
leaq 0x210439(%rip), %rdi    # 0x2511b3
leaq 0x210386(%rip), %rsi    # 0x251107
movl $0x1ae, %edx            # imm = 0x1AE
leaq 0x210385(%rip), %rcx    # 0x251112
callq 0xc280
cmpl $-0x1, -0x14(%rbp)
jne 0x40d9a
jmp 0x40db9
leaq 0x2103ec(%rip), %rdi    # 0x25118d
leaq 0x21035f(%rip), %rsi    # 0x251107
movl $0x1af, %edx            # imm = 0x1AF
leaq 0x21035e(%rip), %rcx    # 0x251112
callq 0xc280
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rax
subq 0x418(%rdi), %rax
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x40b80
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x410(%rax)
addq $0x430, %rax            # imm = 0x430
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x42060
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x42090
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x420c0
testb $0x1, %al
jne 0x40e1b
jmp 0x40e6d
leaq -0x50(%rbp), %rdi
callq 0x42100
movq -0x78(%rbp), %rdi
movq %rax, -0x60(%rbp)
addq $0x410, %rdi            # imm = 0x410
movq -0x60(%rbp), %rsi
addq $0x8, %rsi
callq 0x2b380
movq -0x78(%rbp), %rdi
movq (%rax), %rax
movq -0x60(%rbp), %rcx
subq (%rcx), %rax
movq %rax, -0x68(%rbp)
addq $0x1c8, %rdi            # imm = 0x1C8
leaq -0x68(%rbp), %rsi
callq 0x40b80
leaq -0x50(%rbp), %rdi
callq 0x42120
jmp 0x40e08
movq -0x78(%rbp), %rdi
addq $0x430, %rdi            # imm = 0x430
callq 0x42140
jmp 0x40e7f
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x398(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jl 0x40ec2
movq -0x78(%rbp), %rdi
addq $0x3b0, %rdi            # imm = 0x3B0
callq 0x2bfd0
cmpq $0x0, %rax
je 0x40ec2
movq -0x78(%rbp), %rax
cmpl $0x2, 0x398(%rax)
jle 0x40ec4
jmp 0x40eeb
movq -0x78(%rbp), %rdi
addq $0x3b0, %rdi            # imm = 0x3B0
movslq -0x6c(%rbp), %rsi
callq 0x2f3f0
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
callq 0x40bb0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop