// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %edi, -0x30(%rbp)
movl %esi, %eax
movq %rsp, %r15
movq %rax, %r14
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, %r12
movq %rcx, %rsp
movl %esi, -0x2c(%rbp)
cvtsi2sd %esi, %xmm1
movsd 0xb4e(%rip), %xmm0      # 0x2008
movsd %xmm1, -0x90(%rbp)
divsd %xmm1, %xmm0
callq 0x1080
divsd 0xb3d(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xb38(%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
movl %ecx, %ebx
movq %rcx, -0x68(%rbp)
testl %ecx, %ecx
js 0x1518
leaq 0x8(,%rbx,8), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1070
movq %rbx, -0x70(%rbp)
leaq 0xb6a(%rip), %rdi       # 0x208d
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %ebx
movl %ebx, %edx
xorl %eax, %eax
callq 0x1050
testl %ebx, %ebx
jle 0x169c
movq %r14, %rcx
movl -0x30(%rbp), %eax
movq %rax, -0x78(%rbp)
xorl %r14d, %r14d
movq %rcx, -0x50(%rbp)
movq %r15, -0x58(%rbp)
movq %r12, -0x48(%rbp)
movq %r13, -0x88(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x1585
xorl %ebx, %ebx
movq %rcx, %r15
callq 0x10e0
movq -0x58(%rbp), %rcx
movl %eax, (%rcx,%rbx,4)
callq 0x10e0
movq %r15, %rcx
movl %eax, (%r12,%r14,4)
incq %rbx
cmpq %rbx, %r15
jne 0x1562
movq %r14, -0x80(%rbp)
cmpl $0x2, -0x2c(%rbp)
jl 0x1683
movl $0x1, %eax
movl %eax, -0x3c(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0x1080
divsd 0xa54(%rip), %xmm0      # 0x2010
callq 0x10d0
addsd 0xa4f(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x98(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0x174b
movq %rax, -0x38(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0x174b
movq %rax, %rbx
xorl %r14d, %r14d
movq -0x50(%rbp), %r15
movq -0x58(%rbp), %r12
movq -0x48(%rbp), %r13
movl (%r12,%r14,4), %esi
movq -0x38(%rbp), %rdi
callq 0x18d9
movl (%r13,%r14,4), %esi
movq %rbx, %rdi
callq 0x18d9
incq %r14
cmpq %r14, %r15
jne 0x15fb
cvttsd2si -0x98(%rbp), %r14d
callq 0x1040
movq %rax, %r15
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x192c
movq %rax, %rbx
callq 0x1040
subq %r15, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x9cb(%rip), %xmm0      # 0x2020
movslq %r14d, %rax
movq -0x88(%rbp), %r13
addsd (%r13,%rax,8), %xmm0
movsd %xmm0, (%r13,%rax,8)
movq %rbx, %rdi
callq 0x184a
movl -0x3c(%rbp), %eax
addl %eax, %eax
cmpl -0x2c(%rbp), %eax
jl 0x1598
movq -0x80(%rbp), %r14
incq %r14
cmpq -0x78(%rbp), %r14
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %r12
jne 0x155a
cmpl $0x6, -0x68(%rbp)
movq -0x70(%rbp), %r14
jl 0x16e0
xorps %xmm0, %xmm0
cvtsi2sdl -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
leaq 0xa03(%rip), %rbx       # 0x20bd
movsd (%r13,%r14,8), %xmm0
divsd -0x38(%rbp), %xmm0
movq %rbx, %rdi
movl %r14d, %esi
movb $0x1, %al
callq 0x1050
leaq -0x1(%r14), %rax
cmpq $0x6, %r14
movq %rax, %r14
jg 0x16ba
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq