// File: /workspace/github_c_makefile_star2_O0/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
subq $0xb0, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x10(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x18(%rbp)
movl -0x8(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rax, -0x20(%rbp)
cvtsi2sdl -0x8(%rbp), %xmm1
movsd 0x1a1d(%rip), %xmm0     # 0x3008
divsd %xmm1, %xmm0
callq 0x1070
movaps 0x1a35(%rip), %xmm1     # 0x3030
pxor %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x1a01(%rip), %xmm0     # 0x3010
callq 0x1070
movaps %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x10c0
movsd 0x19e8(%rip), %xmm1     # 0x3018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
incl %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1692
movq -0x90(%rbp), %rax
movslq -0x34(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1667
movl -0x8(%rbp), %esi
movl -0x4(%rbp), %edx
leaq 0x1a06(%rip), %rdi      # 0x30a5
movb $0x0, %al
callq 0x1050
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x1868
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x16fd
callq 0x10d0
movl %eax, %edx
movq -0xa8(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
callq 0x10d0
movl %eax, %edx
movq -0xa0(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x16c0
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1858
cvtsi2sdl -0x40(%rbp), %xmm0
cvtsi2sdl -0x8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x1070
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x18bc(%rip), %xmm0     # 0x3010
callq 0x1070
movaps %xmm0, %xmm1
movsd -0xb0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x10c0
movsd 0x18a3(%rip), %xmm1     # 0x3018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x4c(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x1980
movq %rax, -0x58(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x1980
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x17e4
movq -0xa8(%rbp), %rax
movq -0x58(%rbp), %rdi
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1bf0
movq -0xa0(%rbp), %rax
movq -0x60(%rbp), %rdi
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1bf0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x17a3
callq 0x1040
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x1c60
movq %rax, -0x58(%rbp)
callq 0x1040
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, -0x78(%rbp)
movq -0x78(%rbp), %rcx
subq -0x70(%rbp), %rcx
cvtsi2sd %rcx, %xmm0
movsd 0x17fa(%rip), %xmm1     # 0x3020
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movslq -0x4c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rdi
callq 0x1af0
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x40(%rbp)
jmp 0x1704
jmp 0x185a
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x16ad
movl -0x24(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x5, -0x84(%rbp)
jle 0x18bb
movq -0x90(%rbp), %rax
movl -0x84(%rbp), %esi
movslq -0x84(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl -0x4(%rbp), %xmm1
divsd %xmm1, %xmm0
leaq 0x1832(%rip), %rdi      # 0x30d5
movb $0x1, %al
callq 0x1050
movl -0x84(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x1871
movq -0x10(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)