// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %edi, -0x34(%rbp)
movl %esi, %eax
movq %rsp, %rbx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl %esi, -0x38(%rbp)
cvtsi2sd %esi, %xmm1
movsd 0xdf2(%rip), %xmm0      # 0x2008
movsd %xmm1, -0x70(%rbp)
divsd %xmm1, %xmm0
callq 0x1070
divsd 0xde4(%rip), %xmm0      # 0x2010
callq 0x10c0
addsd 0xddf(%rip), %xmm0      # 0x2018
cvttsd2si %xmm0, %edx
leal 0x1(%rdx), %eax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq %rdx, -0x60(%rbp)
movslq %edx, %rax
xorl %ecx, %ecx
cmpq %rax, %rcx
jg 0x128b
movq -0x40(%rbp), %rdx
andq $0x0, (%rdx,%rcx,8)
movq -0x48(%rbp), %rdx
andq $0x0, (%rdx,%rcx,8)
incq %rcx
jmp 0x126f
leaq 0xd96(%rip), %rdi       # 0x2028
movl -0x38(%rbp), %r14d
movl %r14d, %esi
xorl %eax, %eax
callq 0x1050
testl %r14d, %r14d
movl $0x0, %r13d
cmovgl %r14d, %r13d
movl -0x34(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, -0x54(%rbp)
xorl %eax, %eax
cmpl -0x54(%rbp), %eax
je 0x13f5
movq %rax, -0x68(%rbp)
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x12e2
callq 0x10d0
movl %eax, (%rbx,%r14,4)
incq %r14
jmp 0x12cf
pushq $0x1
popq %rax
cmpl -0x38(%rbp), %eax
jge 0x13ea
movl %eax, -0x58(%rbp)
cvtsi2sd %eax, %xmm0
divsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1070
divsd 0xd04(%rip), %xmm0      # 0x2010
callq 0x10c0
addsd 0xcff(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x50(%rbp)
movsd -0x30(%rbp), %xmm0
callq 0x16d8
movq %rax, %r15
callq 0x1040
movq %rax, %r12
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x134c
movl (%rbx,%r14,4), %esi
movq %r15, %rdi
callq 0x184a
incq %r14
jmp 0x1336
callq 0x1040
subq %r12, %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1040
movq %rax, %r12
movl %r13d, %r14d
subl $0x1, %r14d
jb 0x1379
movq %r15, %rdi
callq 0x1a82
jmp 0x1369
cvttsd2si -0x50(%rbp), %r14d
movsd 0xc99(%rip), %xmm1      # 0x2020
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x1040
subq %r12, %rax
cvtsi2sd %rax, %xmm0
divsd 0xc76(%rip), %xmm0      # 0x2020
movsd %xmm0, -0x50(%rbp)
movq %r15, %rdi
callq 0x17c1
movslq %r14d, %rax
movq -0x48(%rbp), %rcx
movsd -0x30(%rbp), %xmm0
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq -0x40(%rbp), %rcx
movsd -0x50(%rbp), %xmm0
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movl -0x58(%rbp), %eax
addl %eax, %eax
jmp 0x12e5
movq -0x68(%rbp), %rax
incl %eax
jmp 0x12bf
cvtsi2sdl -0x34(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x60(%rbp), %ebx
leaq 0xc53(%rip), %r15       # 0x205c
cmpl $0x5, %ebx
jle 0x143e
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movsd -0x30(%rbp), %xmm2
divsd %xmm2, %xmm0
movq -0x40(%rbp), %rax
movsd (%rax,%rbx,8), %xmm1
divsd %xmm2, %xmm1
movq %r15, %rdi
movl %ebx, %esi
movb $0x2, %al
callq 0x1050
decq %rbx
jmp 0x1409
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq