#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *, double, int);
void stub1(int /*<<< orphan*/);
void benchmark(const char *name, void (*func)(clockid_t clkid),
               clockid_t clkid) {
  unsigned int run;
  double dt, best;
  struct timeval before, after;
  struct timeval tmpval;
  best = -1.0;
  for (run = 0; run < 5; run++) {
    gettimeofday(&before, NULL);
    (*func)(clkid);
    gettimeofday(&after, NULL);
    dt = (after.tv_sec - before.tv_sec) * 1e9;
    if (after.tv_usec >= before.tv_usec)
      dt += (after.tv_usec - before.tv_usec) * 1e3;
    else
      dt -= (before.tv_usec - after.tv_usec) * 1e3;
    dt /= 100000;
    dt /= 10;
    if (best != -1.0) {
      if (dt < best)
        best = dt;
    } else
      best = dt;
  }
  printf("%s: %.0f ns\n", name, best, 100000);
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  0xec5(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jae    1295 <benchmark+0x175>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%edi
call   *%rax
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x48(%rbp),%eax
sub    -0x38(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  0xe82(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
jl     11c9 <benchmark+0xa9>
mov    -0x40(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  0xe59(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1207 <benchmark+0xe7>
mov    -0x30(%rbp),%rax
sub    -0x40(%rbp),%rax
cvtsi2sd %rax,%xmm0
movsd  -0x20(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xe16(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe0c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xdee(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xdc0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1255 <benchmark+0x135>
jp     1255 <benchmark+0x135>
jmp    1278 <benchmark+0x158>
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1273 <benchmark+0x153>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1282 <benchmark+0x162>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1287 <benchmark+0x167>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1147 <benchmark+0x27>
mov    -0x8(%rbp),%rsi
movsd  -0x28(%rbp),%xmm0
lea    0xd83(%rip),%rdi
mov    $0x186a0,%edx
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%r14
movsd  0xec1(%rip),%xmm0
mov    $0x5,%ebp
lea    0x18(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    115d <benchmark+0x3d>
movapd %xmm2,%xmm0
add    $0xffffffff,%ebp
je     1208 <benchmark+0xe8>
movsd  %xmm0,(%rsp)
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r13d,%edi
call   *%rbx
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x8(%rsp),%eax
sub    0x18(%rsp),%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mulsd  0xe76(%rip),%xmm1
mov    0x10(%rsp),%rax
mov    0x20(%rsp),%rcx
mov    %rax,%rdx
sub    %rcx,%rdx
jge    11c0 <benchmark+0xa0>
sub    %rax,%rcx
xorps  %xmm2,%xmm2
cvtsi2sd %rcx,%xmm2
xorpd  0xe79(%rip),%xmm2
jmp    11c8 <benchmark+0xa8>
nopl   0x0(%rax)
xorps  %xmm2,%xmm2
cvtsi2sd %rdx,%xmm2
movsd  (%rsp),%xmm0
mulsd  0xe3b(%rip),%xmm2
addsd  %xmm1,%xmm2
divsd  0xe37(%rip),%xmm2
divsd  0xe37(%rip),%xmm2
ucomisd 0xe0f(%rip),%xmm0
jne    11f9 <benchmark+0xd9>
jnp    1150 <benchmark+0x30>
ucomisd %xmm2,%xmm0
ja     1150 <benchmark+0x30>
jmp    1154 <benchmark+0x34>
lea    0xe31(%rip),%rdi
mov    %r14,%rsi
mov    $0x186a0,%edx
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"%s: %.0f ns\0A\00"
define void @benchmark(i8* %0, void (i32)* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca void (i32)*
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca %struct.timeval
store i8* %0, i8** %4
store void (i32)* %1, void (i32)** %5
store i32 %2, i32* %6
store double -1.000000e+00, double* %9
store i32 0, i32* %7
br label %13
13: ; preds = %69, %3
%14 = load i32, i32* %7
%15 = icmp ult i32 %14, 5
br i1 %15, label %16, label %72
16: ; preds = %13
%17 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%18 = load void (i32)*, void (i32)** %5
%19 = load i32, i32* %6
%20 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %22, %24
%26 = sitofp i32 %25 to double
%27 = fmul double %26, 1.000000e+09
store double %27, double* %8
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%31 = load i64, i64* %30
%32 = icmp sge i64 %29, %31
br i1 %32, label %33, label %42
33: ; preds = %16
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%37 = load i64, i64* %36
%38 = sub nsw i64 %35, %37
%39 = sitofp i64 %38 to double
%40 = load double, double* %8
%41 = call double @llvm.fmuladd.f64(double %39, double 1.000000e+03, double %40)
store double %41, double* %8
br label %52
42: ; preds = %16
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%46 = load i64, i64* %45
%47 = sub nsw i64 %44, %46
%48 = sitofp i64 %47 to double
%49 = load double, double* %8
%50 = fneg double %48
%51 = call double @llvm.fmuladd.f64(double %50, double 1.000000e+03, double %49)
store double %51, double* %8
br label %52
52: ; preds = %42, %33
%53 = load double, double* %8
%54 = fdiv double %53, 1.000000e+05
store double %54, double* %8
%55 = load double, double* %8
%56 = fdiv double %55, 1.000000e+01
store double %56, double* %8
%57 = load double, double* %9
%58 = fcmp une double %57, -1.000000e+00
br i1 %58, label %59, label %66
59: ; preds = %52
%60 = load double, double* %8
%61 = load double, double* %9
%62 = fcmp olt double %60, %61
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = load double, double* %8
store double %64, double* %9
br label %65
65: ; preds = %63, %59
br label %68
66: ; preds = %52
%67 = load double, double* %8
store double %67, double* %9
br label %68
68: ; preds = %66, %65
br label %69
69: ; preds = %68
%70 = load i32, i32* %7
%71 = add i32 %70, 1
store i32 %71, i32* %7
br label %13
72: ; preds = %13
%73 = load i8*, i8** %4
%74 = load double, double* %9
%75 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %73, double %74, i32 100000)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"%s: %.0f ns\0A\00"
define void @benchmark(i8* %0, void (i32)* nocapture %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
br label %12
12: ; preds = %3, %41
%13 = phi i32 [ 0, %3 ], [ %43, %41 ]
%14 = phi double [ -1.000000e+00, %3 ], [ %42, %41 ]
%15 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
tail call void %1(i32 %2)
%16 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = sub nsw i32 %17, %18
%20 = sitofp i32 %19 to double
%21 = fmul double %20, 1.000000e+09
%22 = load i64, i64* %10
%23 = load i64, i64* %11
%24 = icmp slt i64 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %12
%26 = sub nsw i64 %22, %23
%27 = sitofp i64 %26 to double
br label %32
28: ; preds = %12
%29 = sub nsw i64 %23, %22
%30 = sitofp i64 %29 to double
%31 = fneg double %30
br label %32
32: ; preds = %28, %25
%33 = phi double [ %31, %28 ], [ %27, %25 ]
%34 = tail call double @llvm.fmuladd.f64(double %33, double 1.000000e+03, double %21)
%35 = fdiv double %34, 1.000000e+05
%36 = fdiv double %35, 1.000000e+01
%37 = fcmp une double %14, -1.000000e+00
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = fcmp olt double %36, %14
br i1 %39, label %40, label %41
40: ; preds = %38
br label %41
41: ; preds = %32, %40, %38
%42 = phi double [ %36, %40 ], [ %14, %38 ], [ %36, %32 ]
%43 = add nuw nsw i32 %13, 1
%44 = icmp eq i32 %43, 5
br i1 %44, label %45, label %12
45: ; preds = %41
%46 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, double %42, i32 100000)
ret void
}