// File: /workspace/github_c_makefile_star2_O0/LPD-EPFL[P]mctop/src/mctop.c
// Function: mem_bw_estimate
double
mem_bw_estimate(volatile cache_line_t* mem, const uint do_read, const size_t n, const size_t reps)
{
  volatile uint64_t* m0 = (volatile uint64_t*) mem;
  size_t sum = 0;
  volatile size_t suma = 0;

  const size_t n_warmup = n >> 2;
  for (size_t i = 0; i < n_warmup; i++)
    {
      sum = m0[i];
    }

  struct timespec start, stop;
  clock_gettime(CLOCK_REALTIME, &start);
  if (do_read == BW_READ)
    {
      for (uint r = 0; r < reps; r++)
	{
	  for (size_t i = 0; i < n; i++)
	    {
	      sum = m0[i];
	    }
	}
    }
  else if (do_read == BW_WRITE)
    {
      for (uint r = 0; r < reps; r++)
	{
	  for (size_t i = 0; i < n; i++)
	    {
	      m0[i] = 0xAAAAFFFF;
	    }
	}
    }
  clock_gettime(CLOCK_REALTIME, &stop);

  for (size_t i = 0; i < n_warmup; i++)
    {
      sum = m0[i];
    }


  struct timespec dur = timespec_diff(start, stop);
  double dur_s = dur.tv_sec + (dur.tv_nsec / 1e9);
  if (dur_s == 9)
    {
      printf("%p%zu%zu", m0, sum, suma);
    }

  double bw = (reps * n * sizeof(uint64_t)) / (1e9 * dur_s);
  return bw;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x357e
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3556
xorl %edi, %edi
leaq -0x58(%rbp), %rsi
callq 0x2080
cmpl $0x0, -0xc(%rbp)
jne 0x35de
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x35dc
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x35cf
movq -0x28(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x35a7
jmp 0x35d1
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3596
jmp 0x3645
cmpl $0x1, -0xc(%rbp)
jne 0x3643
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x3641
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x3634
movq -0x28(%rbp), %rax
movq -0x88(%rbp), %rcx
movl $0xaaaaffff, %edx       # imm = 0xAAAAFFFF
movq %rdx, (%rax,%rcx,8)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x35ff
jmp 0x3636
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x35eb
jmp 0x3643
jmp 0x3645
xorl %edi, %edi
leaq -0x68(%rbp), %rsi
callq 0x2080
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x368f
movq -0x28(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x365b
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x6e00
movq %rax, -0xa0(%rbp)
movq %rdx, -0x98(%rbp)
cvtsi2sdq -0xa0(%rbp), %xmm0
cvtsi2sdq -0x98(%rbp), %xmm1
movsd 0xb96c(%rip), %xmm2     # 0xf038
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd 0xb944(%rip), %xmm1     # 0xf030
ucomisd %xmm1, %xmm0
jne 0x370e
jp 0x370e
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0xbda5(%rip), %rdi      # 0xf4ac
movb $0x0, %al
callq 0x20d0
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq %rcx, %rax
shlq $0x3, %rax
movq %rax, %xmm0
movaps 0xb926(%rip), %xmm1     # 0xf050
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xb92a(%rip), %xmm1     # 0xf060
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xb8eb(%rip), %xmm1     # 0xf038
mulsd -0xa8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)