#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char *, int, char *, char *, char *);
int /*<<< orphan*/ snftime(char *, int, double);
double walltime();
double benchmark(char *name, void (*func)(void *), void *ctx, int atleast_iters,
                 double atleast_time) {
  double dtmin, t0, tit0, tit1, t1, dtmean, dtmax;
  int n;
  char sbuf1[20], sbuf2[20], sbuf3[20];
  dtmin = 1e300;
  dtmax = 0;
  t0 = walltime();
  n = 0;
  do {
    tit0 = walltime();
    func(ctx);
    tit1 = t1 = walltime();
    if (tit1 - tit0 < dtmin)
      dtmin = tit1 - tit0;
    if (tit1 - tit0 > dtmax)
      dtmax = tit1 - tit0;
    n++;
  } while (n < atleast_iters || t1 - t0 < atleast_time);
  dtmean = (t1 - t0) / n;
  snftime(sbuf1, 20, dtmin);
  snftime(sbuf2, 20, dtmean);
  snftime(sbuf3, 20, dtmax);
  printf("%s (%d reps): %s (min), %s (mean), %s (max) \n", name, n, sbuf1,
         sbuf2, sbuf3);
  return dtmin;
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movsd  %xmm0,-0x28(%rbp)
movsd  0xea9(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    $0x0,%al
call   1040 <walltime@plt>
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x64(%rbp)
mov    $0x0,%al
call   1040 <walltime@plt>
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
call   *%rax
mov    $0x0,%al
call   1040 <walltime@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
subsd  -0x40(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11c6 <benchmark+0x96>
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
ucomisd -0x60(%rbp),%xmm0
jbe    11ea <benchmark+0xba>
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%ecx
mov    $0x1,%al
cmp    -0x1c(%rbp),%ecx
mov    %al,-0xc1(%rbp)
jl     1223 <benchmark+0xf3>
movsd  -0x50(%rbp),%xmm1
subsd  -0x38(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0xc1(%rbp)
mov    -0xc1(%rbp),%al
test   $0x1,%al
jne    1177 <benchmark+0x47>
movsd  -0x50(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
cvtsi2sdl -0x64(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
lea    -0x80(%rbp),%rdi
movsd  -0x30(%rbp),%xmm0
mov    $0x14,%esi
call   1050 <snftime@plt>
lea    -0xa0(%rbp),%rdi
movsd  -0x58(%rbp),%xmm0
mov    $0x14,%esi
call   1050 <snftime@plt>
lea    -0xc0(%rbp),%rdi
movsd  -0x60(%rbp),%xmm0
mov    $0x14,%esi
call   1050 <snftime@plt>
mov    -0x8(%rbp),%rsi
mov    -0x64(%rbp),%edx
lea    -0x80(%rbp),%rcx
lea    -0xa0(%rbp),%r8
lea    -0xc0(%rbp),%r9
lea    0xd60(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x30(%rbp),%xmm0
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
movsd  %xmm0,0x28(%rsp)
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <walltime@plt>
movsd  %xmm0,0x20(%rsp)
xorpd  %xmm2,%xmm2
movsd  0xe96(%rip),%xmm0
xor    %ebp,%ebp
nopl   0x0(%rax)
movsd  %xmm2,0x18(%rsp)
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1040 <walltime@plt>
movsd  %xmm0,0x8(%rsp)
mov    %r12,%rdi
call   *%rbx
xor    %eax,%eax
call   1040 <walltime@plt>
movapd %xmm0,%xmm3
movapd %xmm0,%xmm2
subsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm0
minsd  0x10(%rsp),%xmm0
maxsd  0x18(%rsp),%xmm2
add    $0x1,%ebp
cmp    %r15d,%ebp
jl     1170 <benchmark+0x40>
subsd  0x20(%rsp),%xmm3
movsd  0x28(%rsp),%xmm1
ucomisd %xmm3,%xmm1
ja     1170 <benchmark+0x40>
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
divsd  %xmm1,%xmm3
movsd  %xmm3,0x8(%rsp)
lea    0x70(%rsp),%r15
mov    %r15,%rdi
mov    $0x14,%esi
movsd  %xmm0,0x18(%rsp)
movsd  %xmm2,0x10(%rsp)
call   1050 <snftime@plt>
lea    0x50(%rsp),%r12
mov    %r12,%rdi
mov    $0x14,%esi
movsd  0x8(%rsp),%xmm0
call   1050 <snftime@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
movsd  0x10(%rsp),%xmm0
call   1050 <snftime@plt>
lea    0xdd5(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r15,%rcx
mov    %r12,%r8
mov    %rbx,%r9
call   1030 <printf@plt>
movsd  0x18(%rsp),%xmm0
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"%s (%d reps): %s (min), %s (mean), %s (max) \0A\00"
define double @benchmark(i8* %0, void (i8*)* %1, i8* %2, i32 %3, double %4) {
%6 = alloca i8*
%7 = alloca void (i8*)*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca i32
%19 = alloca [20 x i8]
%20 = alloca [20 x i8]
%21 = alloca [20 x i8]
store i8* %0, i8** %6
store void (i8*)* %1, void (i8*)** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store double %4, double* %10
store double 1.000000e+300, double* %11
store double 0.000000e+00, double* %17
%22 = call double (...) @walltime()
store double %22, double* %12
store i32 0, i32* %18
br label %23
23: ; preds = %60, %5
%24 = call double (...) @walltime()
store double %24, double* %13
%25 = load void (i8*)*, void (i8*)** %7
%26 = load i8*, i8** %8
%27 = call double (...) @walltime()
store double %27, double* %15
store double %27, double* %14
%28 = load double, double* %14
%29 = load double, double* %13
%30 = fsub double %28, %29
%31 = load double, double* %11
%32 = fcmp olt double %30, %31
br i1 %32, label %33, label %37
33: ; preds = %23
%34 = load double, double* %14
%35 = load double, double* %13
%36 = fsub double %34, %35
store double %36, double* %11
br label %37
37: ; preds = %33, %23
%38 = load double, double* %14
%39 = load double, double* %13
%40 = fsub double %38, %39
%41 = load double, double* %17
%42 = fcmp ogt double %40, %41
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load double, double* %14
%45 = load double, double* %13
%46 = fsub double %44, %45
store double %46, double* %17
br label %47
47: ; preds = %43, %37
%48 = load i32, i32* %18
%49 = add nsw i32 %48, 1
store i32 %49, i32* %18
br label %50
50: ; preds = %47
%51 = load i32, i32* %18
%52 = load i32, i32* %9
%53 = icmp slt i32 %51, %52
br i1 %53, label %60, label %54
54: ; preds = %50
%55 = load double, double* %15
%56 = load double, double* %12
%57 = fsub double %55, %56
%58 = load double, double* %10
%59 = fcmp olt double %57, %58
br label %60
60: ; preds = %54, %50
%61 = phi i1 [ true, %50 ], [ %59, %54 ]
br i1 %61, label %23, label %62
62: ; preds = %60
%63 = load double, double* %15
%64 = load double, double* %12
%65 = fsub double %63, %64
%66 = load i32, i32* %18
%67 = sitofp i32 %66 to double
%68 = fdiv double %65, %67
store double %68, double* %16
%69 = getelementptr inbounds [20 x i8], [20 x i8]* %19, i64 0, i64 0
%70 = load double, double* %11
%71 = call i32 @snftime(i8* %69, i32 20, double %70)
%72 = getelementptr inbounds [20 x i8], [20 x i8]* %20, i64 0, i64 0
%73 = load double, double* %16
%74 = call i32 @snftime(i8* %72, i32 20, double %73)
%75 = getelementptr inbounds [20 x i8], [20 x i8]* %21, i64 0, i64 0
%76 = load double, double* %17
%77 = call i32 @snftime(i8* %75, i32 20, double %76)
%78 = load i8*, i8** %6
%79 = load i32, i32* %18
%80 = getelementptr inbounds [20 x i8], [20 x i8]* %19, i64 0, i64 0
%81 = getelementptr inbounds [20 x i8], [20 x i8]* %20, i64 0, i64 0
%82 = getelementptr inbounds [20 x i8], [20 x i8]* %21, i64 0, i64 0
%83 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %78, i32 %79, i8* %80, i8* %81, i8* %82)
%84 = load double, double* %11
ret double %84
}

LLVM IR (-O2):
@.str = [46 x i8] c"%s (%d reps): %s (min), %s (mean), %s (max) \0A\00"
define double @benchmark(i8* %0, void (i8*)* nocapture %1, i8* %2, i32 %3, double %4) {
%6 = alloca [20 x i8]
%7 = alloca [20 x i8]
%8 = alloca [20 x i8]
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%12 = tail call double (...) @walltime()
br label %13
13: ; preds = %13, %5
%14 = phi double [ 1.000000e+300, %5 ], [ %21, %13 ]
%15 = phi double [ 0.000000e+00, %5 ], [ %23, %13 ]
%16 = phi i32 [ 0, %5 ], [ %24, %13 ]
%17 = tail call double (...) @walltime()
tail call void %1(i8* %2)
%18 = tail call double (...) @walltime()
%19 = fsub double %18, %17
%20 = fcmp olt double %19, %14
%21 = select i1 %20, double %19, double %14
%22 = fcmp ogt double %19, %15
%23 = select i1 %22, double %19, double %15
%24 = add nuw nsw i32 %16, 1
%25 = icmp slt i32 %24, %3
%26 = fsub double %18, %12
%27 = fcmp olt double %26, %4
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %13, label %29
29: ; preds = %13
%30 = sitofp i32 %24 to double
%31 = fdiv double %26, %30
%32 = call i32 @snftime(i8* %9, i32 20, double %21)
%33 = call i32 @snftime(i8* %10, i32 20, double %31)
%34 = call i32 @snftime(i8* %11, i32 20, double %23)
%35 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %24, i8* %9, i8* %10, i8* %11)
ret double %21
}