// File: /workspace/github_c_makefile_star2_O3/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, -0x3c(%rbp)
movl %esi, %r15d
movq %rsp, %r14
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movl %esi, -0x2c(%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), %r13d
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rcx, -0x60(%rbp)
testl %ecx, %ecx
js 0x1296
shlq $0x3, %r13
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1070
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1070
movq %r12, -0x50(%rbp)
movq %rbx, -0x58(%rbp)
leaq 0xd83(%rip), %rdi       # 0x2028
xorl %ebx, %ebx
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x1050
cmpl $0x0, -0x3c(%rbp)
jle 0x13fd
movq %rbx, -0x68(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x13ee
xorl %ebx, %ebx
callq 0x10e0
movl %eax, (%r14,%rbx,4)
incq %rbx
cmpq %rbx, %r15
jne 0x12cb
cmpl $0x2, -0x2c(%rbp)
jl 0x13ee
movl $0x1, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x1080
divsd 0xd05(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xd00(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x48(%rbp)
movsd -0x38(%rbp), %xmm0
callq 0x172f
movq %rax, %r13
callq 0x1040
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movl (%r14,%rbx,4), %esi
movq %r13, %rdi
callq 0x18bd
incq %rbx
cmpq %rbx, %r15
jne 0x1335
callq 0x1040
subq -0x38(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x1040
movq %rax, -0x78(%rbp)
movl -0x2c(%rbp), %ebx
movq %r13, %rdi
callq 0x1b6b
decl %ebx
jne 0x136b
cvttsd2si -0x48(%rbp), %ebx
movsd 0xc9c(%rip), %xmm1      # 0x2020
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x1040
subq -0x78(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0xc75(%rip), %xmm0      # 0x2020
movsd %xmm0, -0x48(%rbp)
movq %r13, %rdi
callq 0x182e
movslq %ebx, %rax
movq -0x58(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
addsd (%rcx,%rax,8), %xmm0
movq -0x50(%rbp), %rdx
movsd -0x48(%rbp), %xmm1
addsd (%rdx,%rax,8), %xmm1
movsd %xmm0, (%rcx,%rax,8)
movsd %xmm1, (%rdx,%rax,8)
addl %r12d, %r12d
cmpl -0x2c(%rbp), %r12d
jl 0x12ec
movq -0x68(%rbp), %rbx
incl %ebx
cmpl -0x3c(%rbp), %ebx
jne 0x12bb
movq -0x60(%rbp), %rax
cmpl $0x6, %eax
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %r12
jl 0x1456
xorps %xmm0, %xmm0
cvtsi2sdl -0x3c(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl %eax, %ebx
leaq 0xc38(%rip), %r14       # 0x205c
movsd (%r15,%rbx,8), %xmm0
movsd -0x38(%rbp), %xmm2
divsd %xmm2, %xmm0
movsd (%r12,%rbx,8), %xmm1
divsd %xmm2, %xmm1
movq %r14, %rdi
movl %ebx, %esi
movb $0x2, %al
callq 0x1050
leaq -0x1(%rbx), %rax
cmpq $0x6, %rbx
movq %rax, %rbx
ja 0x1424
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq