// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movl %edi, -0x2c(%rbp)
movl %esi, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movl %esi, -0x30(%rbp)
cvtsi2sd %esi, %xmm1
movsd 0xb74(%rip), %xmm0      # 0x2008
movsd %xmm1, -0x68(%rbp)
divsd %xmm1, %xmm0
callq 0x1070
divsd 0xb66(%rip), %xmm0      # 0x2010
callq 0x10c0
addsd 0xb61(%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 %rdx, -0x58(%rbp)
movslq %edx, %rax
xorl %ecx, %ecx
movq -0x48(%rbp), %rdx
cmpq %rax, %rcx
jg 0x14f3
andq $0x0, (%rdx,%rcx,8)
incq %rcx
jmp 0x14e4
leaq 0xb93(%rip), %rdi       # 0x208d
xorl %r12d, %r12d
movl -0x30(%rbp), %r15d
movl %r15d, %esi
movl -0x2c(%rbp), %ebx
movl %ebx, %edx
xorl %eax, %eax
callq 0x1050
testl %r15d, %r15d
movl $0x0, %r13d
cmovgl %r15d, %r13d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, -0x60(%rbp)
cmpq -0x60(%rbp), %r12
je 0x1638
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x1556
callq 0x10d0
movq -0x40(%rbp), %rcx
movl %eax, (%rcx,%rbx,4)
callq 0x10d0
movl %eax, (%r14,%r12,4)
incq %rbx
jmp 0x1537
pushq $0x1
popq %rax
movq %r12, -0x70(%rbp)
cmpl -0x30(%rbp), %eax
jge 0x1630
movl %eax, -0x4c(%rbp)
cvtsi2sd %eax, %xmm0
divsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x1070
divsd 0xa8c(%rip), %xmm0      # 0x2010
callq 0x10c0
addsd 0xa87(%rip), %xmm0      # 0x2018
movsd %xmm0, -0x78(%rbp)
movsd -0x38(%rbp), %xmm0
callq 0x16d8
movq %rax, %r15
movsd -0x38(%rbp), %xmm0
callq 0x16d8
movq %rax, %r12
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x15d7
movq -0x40(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %r15, %rdi
callq 0x184a
movl (%r14,%rbx,4), %esi
movq %r12, %rdi
callq 0x184a
incq %rbx
jmp 0x15b2
cvttsd2si -0x78(%rbp), %ebx
callq 0x1040
movq %rax, -0x38(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x187c
movq %rax, %r15
callq 0x1040
subq -0x38(%rbp), %rax
cvtsi2sd %rax, %xmm0
divsd 0xa17(%rip), %xmm0      # 0x2020
movslq %ebx, %rax
movq -0x48(%rbp), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq %r15, %rdi
callq 0x17c1
movl -0x4c(%rbp), %eax
addl %eax, %eax
movq -0x70(%rbp), %r12
jmp 0x155d
incq %r12
jmp 0x152b
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x58(%rbp), %ebx
leaq 0xa71(%rip), %r14       # 0x20bd
cmpl $0x5, %ebx
jle 0x1670
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
divsd -0x40(%rbp), %xmm0
movq %r14, %rdi
movl %ebx, %esi
movb $0x1, %al
callq 0x1050
decq %rbx
jmp 0x164c
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq