// File: /workspace/github_c_makefile_star2_O3/millimat[P]Soft-Heap/epsilon-timing.c
// Function: time_meld
void time_meld(int tries, int n) {
  int elts1[n];
  int elts2[n];

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

  printf("--------------- Meld: %d (%d tries) ----------\n", n, tries);

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

    for(int j = 0; j < n; j++) {
      elts1[j] = rand();
      elts2[i] = 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);
      softheap *Q = makeheap_empty(epsilon);

      for(int j = 0; j < n; j++) {
        insert(P, elts1[j]);
        insert(Q, elts2[j]);
      }

      clock_t start = clock();
      P = meld(P, Q);
      clock_t stop = clock();
      double meld_secs = (double)(stop-start) / CLOCKS_PER_SEC;

      cumul[r] += meld_secs;

      destroy_heap(P);
    }
  }

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

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edi, %r14d
movl %esi, %eax
movq %rsp, %rcx
movq %rax, -0x68(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movl %esi, -0x2c(%rbp)
cvtsi2sd %esi, %xmm1
movsd 0xb54(%rip), %xmm0      # 0x2008
movsd %xmm1, -0x88(%rbp)
divsd %xmm1, %xmm0
callq 0x1080
divsd 0xb43(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xb3e(%rip), %xmm0      # 0x2018
cvttsd2si %xmm0, %ecx
leal 0x1(%rcx), %edx
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %rcx, -0x78(%rbp)
testl %ecx, %ecx
js 0x150c
shlq $0x3, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
leaq 0xb7a(%rip), %rdi       # 0x208d
movl -0x2c(%rbp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1050
movl %r14d, -0x30(%rbp)
testl %r14d, %r14d
jle 0x167e
movl -0x30(%rbp), %eax
movq %rax, -0x80(%rbp)
xorl %eax, %eax
movq %r15, -0x50(%rbp)
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x166d
xorl %r14d, %r14d
movq -0x68(%rbp), %r15
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r12
movq -0x48(%rbp), %r13
callq 0x10e0
movl %eax, (%rbx,%r14,4)
callq 0x10e0
movl %eax, (%r12,%r13,4)
incq %r14
cmpq %r14, %r15
jne 0x155b
cmpl $0x2, -0x2c(%rbp)
movq -0x50(%rbp), %r15
jl 0x166d
movl $0x1, %eax
movl %eax, -0x3c(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
callq 0x1080
divsd 0xa64(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xa5f(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x90(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x172f
movq %rax, -0x38(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x172f
movq %rax, %r13
xorl %r14d, %r14d
movq -0x68(%rbp), %r15
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r12
movl (%rbx,%r14,4), %esi
movq -0x38(%rbp), %rdi
callq 0x18bd
movl (%r12,%r14,4), %esi
movq %r13, %rdi
callq 0x18bd
incq %r14
cmpq %r14, %r15
jne 0x15eb
cvttsd2si -0x90(%rbp), %r14d
callq 0x1040
movq %rax, %rbx
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1910
movq %rax, %r12
callq 0x1040
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x9dc(%rip), %xmm0      # 0x2020
movslq %r14d, %rax
movq -0x50(%rbp), %r15
addsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%r15,%rax,8)
movq %r12, %rdi
callq 0x182e
movl -0x3c(%rbp), %eax
addl %eax, %eax
cmpl -0x2c(%rbp), %eax
jl 0x1588
movq -0x48(%rbp), %rax
incq %rax
cmpq -0x80(%rbp), %rax
jne 0x153a
movq -0x78(%rbp), %rax
cmpl $0x6, %eax
jl 0x16c1
xorps %xmm0, %xmm0
cvtsi2sdl -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl %eax, %ebx
leaq 0xa20(%rip), %r14       # 0x20bd
movsd (%r15,%rbx,8), %xmm0
divsd -0x38(%rbp), %xmm0
movq %r14, %rdi
movl %ebx, %esi
movb $0x1, %al
callq 0x1050
leaq -0x1(%rbx), %rax
cmpq $0x6, %rbx
movq %rax, %rbx
ja 0x169d
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq