// File: /workspace/github_c_makefile_star2_O0/LPD-EPFL[P]mctop/src/mctop_cache.c
// Function: mctop_cache_size_estimate
mctop_cache_info_t*
mctop_cache_size_estimate()
{
  /* +1 level for 0: i cache */
  mctop_cache_info_t* mci = mctop_cache_info_create(mctop_cache_n_lvls + 1);

  int sizes_OS_ok = mctop_cache_size_read_OS(mci->sizes_OS);
  if (!sizes_OS_ok)
    {
      fprintf(stderr, "MCTOP Warning: Can't read cache sizes from OS\n");
    }

  const size_t KB = 1024;
  
  mci->sizes_estimated[0] = 4;

  const size_t n_steps_fix = 32;
      

  for (int lvl = 1; lvl <= mctop_cache_n_lvls; lvl++)
    {
      size_t n_steps = (lvl * n_steps_fix);
      size_t stp = 4 * mci->sizes_estimated[lvl - 1];
      if (stp > 1024)
	{
	  stp = 1024;
	}
      size_t sensitivity = 1.2 * mci->latencies[lvl - 1];
      int64_t min = stp;
      size_t max = n_steps * stp;

      size_t size_OS = mci->sizes_OS[lvl];
      if (size_OS > 0)
	{
	  const size_t n_steps_side = 8;
	  min = size_OS;
	  n_steps = 0;
	  while (n_steps++ < n_steps_side && min > stp)
	    {
	      min -= stp;
	    }
	  max = size_OS + (n_steps_side * stp);
	  n_steps += n_steps_side + 1;
	}


      printf("## Looking for L%d cache size (%zu to %zu KB, step %zu KB)\n",
	     lvl, min, max, stp);

      ticks* lat = calloc_assert(n_steps, sizeof(ticks));

      size_t n = 0, kb;
      for (kb = min; kb < max; kb += stp)
	{
	  lat[n] = ll_random_latency(((kb * 0.98) * KB));

	  //	  printf("[%2zu[ %-5zu KB -> %-5zu cycles\n", n, kb, lat[n]);
	  if (n > 1 && lat[n] > (lat[n - 1] +  sensitivity))
	    {
	      break;
	    }
	  n++;
	}

      cdf_t* cdf = cdf_calc(lat + 1, n - 1);
      //      cdf_t* cdf = cdf_calc(lat, n);
      /* cdf_print(cdf); */
      cdf_cluster_t* cc = cdf_cluster(cdf, sensitivity, 0);
      cdf_cluster_print(cc);

      size_t tlat = cc->clusters[0].val_max;

      for (kb = min, n = 0; kb < max && lat[n] <= tlat; kb += stp, n++);
      kb -= stp;
    
      size_t clat = array_get_min(lat, n - 1);

      mci->latencies[lvl] = clat;
      mci->sizes_estimated[lvl] = kb;

      printf("## Cache L%d / Latency: %-4zu / Size:    OS: %5zu KB     Estimated: %5zu KB\n",
	     lvl, clat, mci->sizes_OS[lvl], mci->sizes_estimated[lvl]);

      cdf_cluster_free(cc);
      cdf_free(cdf);

      free(lat);
    }

  mci->sizes_estimated[0] = mci->sizes_OS[0];

  return mci;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x4, %edi
callq 0xb0b0
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xded0
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0xda97
movq 0x7572(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x3960(%rip), %rsi      # 0x113f0
movb $0x0, %al
callq 0x2140
movq $0x400, -0x18(%rbp)     # imm = 0x400
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq $0x4, (%rax)
movq $0x20, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
movslq -0x24(%rbp), %rax
cmpq $0x3, %rax
ja 0xdeab
movslq -0x24(%rbp), %rax
shlq $0x5, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
cmpq $0x400, -0x38(%rbp)     # imm = 0x400
jbe 0xdb06
movq $0x400, -0x38(%rbp)     # imm = 0x400
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %xmm0
movaps 0x152e(%rip), %xmm1     # 0xf050
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1532(%rip), %xmm1     # 0xf060
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x3893(%rip), %xmm1     # 0x113d8
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x14e0(%rip), %xmm1     # 0xf040
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
imulq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jbe 0xdc29
movq $0x8, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x8, %rcx
movb %al, -0x99(%rbp)
jae 0xdbed
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
seta %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0xdbf9
jmp 0xdc0a
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0xdbbf
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %esi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %r8
leaq 0x37e0(%rip), %rdi      # 0x1141f
movb $0x0, %al
callq 0x20d0
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0xdfe0
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0xdd3a
movaps 0x13d3(%rip), %xmm1     # 0xf050
movq -0x78(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x13d2(%rip), %xmm1     # 0xf060
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x136b(%rip), %xmm1     # 0xf010
mulsd %xmm1, %xmm0
movsd 0x372f(%rip), %xmm1     # 0x113e0
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rdi
movq %rdi, %rcx
sarq $0x3f, %rcx
movsd 0x1374(%rip), %xmm1     # 0xf040
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rcx, %rax
orq %rax, %rdi
callq 0xe040
movq %rax, %rdx
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x1, -0x70(%rbp)
jbe 0xdd1d
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rdx
subq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
addq -0x40(%rbp), %rcx
cmpq %rcx, %rax
jbe 0xdd1d
jmp 0xdd3a
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0xdc68
movq -0x68(%rbp), %rdi
addq $0x8, %rdi
movq -0x70(%rbp), %rsi
subq $0x1, %rsi
callq 0x7560
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
xorl %edx, %edx
callq 0x7910
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x7f40
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x78(%rbp), %rcx
xorl %eax, %eax
cmpq -0x50(%rbp), %rcx
movb %al, -0x9a(%rbp)
jae 0xddcb
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x90(%rbp), %rax
setbe %al
movb %al, -0x9a(%rbp)
movb -0x9a(%rbp), %al
testb $0x1, %al
jne 0xddd7
jmp 0xddf3
jmp 0xddd9
movq -0x38(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0xdd9d
movq -0x38(%rbp), %rcx
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rax
subq $0x1, %rax
movl %eax, %esi
callq 0xe0f0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x78(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %esi
movq -0x98(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x24(%rbp), %rdi
movq (%rax,%rdi,8), %r8
leaq 0x35e3(%rip), %rdi      # 0x1145b
movb $0x0, %al
callq 0x20d0
movq -0x88(%rbp), %rdi
callq 0x7de0
movq -0x80(%rbp), %rdi
callq 0x7860
movq -0x68(%rbp), %rdi
callq 0x2030
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xdabd
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nop