// File: /workspace/github_c_makefile_star2_O1/millimat[P]Soft-Heap/epsilon-timing.c
// Function: time_insert_extract
void time_insert_extract(int tries, int n) {
  int elts[n];

  int max_r = ceil(-log(1./n)/log(2))+5;
  double cumul_insert[max_r+1];
  double cumul_extract[max_r+1];
  for(int i = 0; i < max_r+1; i++)
    cumul_insert[i] = cumul_extract[i] = 0;

  printf("--------------- Insert-Extract: %d ---------------\n", n);

  for(int i = 0; i < tries; i++) {

    for(int j = 0; j < n; j++)
      elts[j] = rand();

    // go over all relevant values of r(epsilon)
    for(int k = 1; k < n; k *= 2) {
      double epsilon = ((double)k)/n;
      int r = ceil(-log(epsilon)/log(2)) + 5;

      softheap *P = makeheap_empty(epsilon);

      clock_t start = clock();
      for(int j = 0; j < n; j++) {
        insert(P, elts[j]);
      }
      clock_t stop = clock();
      double insert_secs = (double)(stop-start) / CLOCKS_PER_SEC;

      start = clock();
      for(int j = 0; j < n; j++) {
        extract_min(P);
      }
      stop = clock();
      double extract_secs = (double)(stop-start) / CLOCKS_PER_SEC;

      destroy_heap(P);

      cumul_insert[r] += insert_secs;
      cumul_extract[r] += extract_secs;
    }
  }

  for(int i = max_r; i > 5; i--) {
    printf("r=%d \t average insert: %f \t average extract: %f\n", i, cumul_insert[i]/tries, cumul_extract[i]/tries);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edi, -0x38(%rbp)
movl %esi, %r14d
movq %rsp, %r15
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movl %esi, -0x34(%rbp)
cvtsi2sd %esi, %xmm1
movsd 0xde1(%rip), %xmm0      # 0x2008
movsd %xmm1, -0x70(%rbp)
divsd %xmm1, %xmm0
callq 0x1080
divsd 0xdd3(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xdce(%rip), %xmm0      # 0x2018
cvttsd2si %xmm0, %ecx
leal 0x1(%rcx), %eax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movl %ecx, %ebx
movq %rcx, -0x50(%rbp)
testl %ecx, %ecx
js 0x129d
leaq 0x8(,%rbx,8), %rdx
movq %rdx, -0x30(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x1070
movq %r13, %rdi
xorl %esi, %esi
movq -0x30(%rbp), %rdx
callq 0x1070
movq %rbx, -0x58(%rbp)
movq %r12, -0x48(%rbp)
leaq 0xd7c(%rip), %rdi       # 0x2028
xorl %ebx, %ebx
movl -0x34(%rbp), %esi
xorl %eax, %eax
callq 0x1050
cmpl $0x0, -0x38(%rbp)
jle 0x140c
movq %r13, -0x68(%rbp)
movq %rbx, -0x60(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x12e3
xorl %ebx, %ebx
callq 0x10e0
movl %eax, (%r15,%rbx,4)
incq %rbx
cmpq %rbx, %r14
jne 0x12d2
cmpl $0x2, -0x34(%rbp)
jl 0x13fd
movl $0x1, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1080
divsd 0xcfe(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xcf9(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x40(%rbp)
movsd -0x30(%rbp), %xmm0
callq 0x174b
movq %rax, %rbx
callq 0x1040
movq %rax, -0x30(%rbp)
xorl %r13d, %r13d
movl (%r15,%r13,4), %esi
movq %rbx, %rdi
callq 0x18d9
incq %r13
cmpq %r13, %r14
jne 0x133d
callq 0x1040
subq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1040
movq %rax, -0x78(%rbp)
movl -0x34(%rbp), %r13d
movq %rbx, %rdi
callq 0x1b75
decl %r13d
jne 0x1374
cvttsd2si -0x40(%rbp), %r13d
movsd 0xc91(%rip), %xmm1      # 0x2020
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1040
subq -0x78(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0xc6a(%rip), %xmm0      # 0x2020
movsd %xmm0, -0x40(%rbp)
movq %rbx, %rdi
callq 0x184a
movslq %r13d, %rax
movq -0x68(%rbp), %r13
movsd -0x30(%rbp), %xmm0
addsd (%r13,%rax,8), %xmm0
movq -0x48(%rbp), %rcx
movsd -0x40(%rbp), %xmm1
addsd (%rcx,%rax,8), %xmm1
movsd %xmm0, (%r13,%rax,8)
movsd %xmm1, (%rcx,%rax,8)
addl %r12d, %r12d
cmpl -0x34(%rbp), %r12d
jl 0x12f3
movq -0x60(%rbp), %rbx
incl %ebx
cmpl -0x38(%rbp), %ebx
jne 0x12c6
cmpl $0x6, -0x50(%rbp)
movq -0x48(%rbp), %r14
movq -0x58(%rbp), %r15
jl 0x1462
xorps %xmm0, %xmm0
cvtsi2sdl -0x38(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0xc2e(%rip), %rbx       # 0x205c
movsd (%r13,%r15,8), %xmm0
movsd -0x30(%rbp), %xmm2
divsd %xmm2, %xmm0
movsd (%r14,%r15,8), %xmm1
divsd %xmm2, %xmm1
movq %rbx, %rdi
movl %r15d, %esi
movb $0x2, %al
callq 0x1050
leaq -0x1(%r15), %rax
cmpq $0x6, %r15
movq %rax, %r15
jg 0x142e
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq