#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_usec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
float timeit(void (*func)(int)) {
  int count, elapsed;
  struct timeval start, end;
  count = 1;
  while (1) {
    gettimeofday(&start, NULL);
    func(count);
    gettimeofday(&end, NULL);
    elapsed = end.tv_usec - start.tv_usec;
    elapsed += 1000000 * (end.tv_sec - start.tv_sec);
    if (elapsed > 1000000)
      break;
    count *= 2;
  }
  return 1000000.0 * count / elapsed;
}


x86 assembly (clang, -O0):
<timeit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%edi
call   *%rax
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x14(%rbp),%eax
imul   $0xf4240,%eax,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jle    1173 <timeit+0x63>
jmp    1181 <timeit+0x71>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1123 <timeit+0x13>
cvtsi2sdl -0xc(%rbp),%xmm1
movsd  0xe72(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeit>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
mov    $0x1,%ecx
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
nopl   (%rax)
mov    %ecx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %ebx,%edi
call   *%r12
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%eax
sub    0x10(%rsp),%ecx
sub    0x14(%rsp),%eax
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
lea    (%rbx,%rbx,1),%ecx
cmp    $0xf4241,%eax
jl     1130 <timeit+0x20>
cvtsi2sd %ebx,%xmm0
mulsd  0xe87(%rip),%xmm0
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define float @timeit(void (i32)* %0) {
%2 = alloca void (i32)*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
store void (i32)* %0, void (i32)** %2
store i32 1, i32* %3
br label %7
7: ; preds = %1, %28
%8 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%9 = load void (i32)*, void (i32)** %2
%10 = load i32, i32* %3
%11 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 %13, %15
store i32 %16, i32* %4
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = mul nsw i32 1000000, %21
%23 = load i32, i32* %4
%24 = add nsw i32 %23, %22
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 1000000
br i1 %26, label %27, label %28
27: ; preds = %7
br label %31
28: ; preds = %7
%29 = load i32, i32* %3
%30 = mul nsw i32 %29, 2
store i32 %30, i32* %3
br label %7
31: ; preds = %27
%32 = load i32, i32* %3
%33 = sitofp i32 %32 to double
%34 = fmul double 1.000000e+06, %33
%35 = load i32, i32* %4
%36 = sitofp i32 %35 to double
%37 = fdiv double %34, %36
%38 = fptrunc double %37 to float
ret float %38
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define float @timeit(void (i32)* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
br label %10
10: ; preds = %10, %1
%11 = phi i32 [ 1, %1 ], [ %23, %10 ]
%12 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
tail call void %0(i32 %11)
%13 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = sub nsw i32 %14, %15
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = sub nsw i32 %17, %18
%20 = mul nsw i32 %19, 1000000
%21 = add nsw i32 %20, %16
%22 = icmp sgt i32 %21, 1000000
%23 = shl nsw i32 %11, 1
br i1 %22, label %24, label %10
24: ; preds = %10
%25 = sitofp i32 %11 to double
%26 = fmul double %25, 1.000000e+06
%27 = sitofp i32 %21 to double
%28 = fdiv double %26, %27
%29 = fptrunc double %28 to float
ret float %29
}