#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 */
typedef int uint64_t;

/* Variables and functions */
int get_ticks();
int /*<<< orphan*/ printf(char *);
double benchmark(void (*func)(unsigned long n, void *data), void *data) {
  uint64_t ticks;
  unsigned long n = 1;
  printf("Benchmarking... ");
  while (n <<= 1) {
    ticks = get_ticks();
    func(n, data);
    ticks = get_ticks() - ticks;
    if (ticks >= 500000)
      break;
  }
  if (!n) {
    printf("CPU too fast :-)\n");
    return -1;
  }
  return 1e6 * n / ticks;
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
lea    0xef1(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1196 <benchmark+0x76>
mov    $0x0,%al
call   1040 <get_ticks@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    $0x0,%al
call   1040 <get_ticks@plt>
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x7a120,-0x1c(%rbp)
jl     1191 <benchmark+0x71>
jmp    1196 <benchmark+0x76>
jmp    1144 <benchmark+0x24>
cmpq   $0x0,-0x28(%rbp)
jne    11bf <benchmark+0x9f>
lea    0xe99(%rip),%rdi
call   1030 <printf@plt>
movsd  0xe4b(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1200 <benchmark+0xe0>
movaps 0xe4a(%rip),%xmm0
movq   -0x28(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xe49(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe1a(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xeec(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%ebx
xchg   %ax,%ax
add    %rbx,%rbx
je     11ad <benchmark+0x7d>
xor    %eax,%eax
call   1050 <get_ticks@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   *%r15
xor    %eax,%eax
call   1050 <get_ticks@plt>
sub    %ebp,%eax
cmp    $0x7a11f,%eax
jle    1150 <benchmark+0x20>
movq   %rbx,%xmm1
punpckldq 0xe7c(%rip),%xmm1
subpd  0xe84(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xe80(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
jmp    11c1 <benchmark+0x91>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
movsd  0xe67(%rip),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Benchmarking... \00"
@.str.1 = [18 x i8] c"CPU too fast :-)\0A\00"
define double @benchmark(void (i64, i8*)* %0, i8* %1) {
%3 = alloca double
%4 = alloca void (i64, i8*)*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store void (i64, i8*)* %0, void (i64, i8*)** %4
store i8* %1, i8** %5
store i64 1, i64* %7
%8 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %24, %2
%10 = load i64, i64* %7
%11 = shl i64 %10, 1
store i64 %11, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = call i32 (...) @get_ticks()
store i32 %14, i32* %6
%15 = load void (i64, i8*)*, void (i64, i8*)** %4
%16 = load i64, i64* %7
%17 = load i8*, i8** %5
%18 = call i32 (...) @get_ticks()
%19 = load i32, i32* %6
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp sge i32 %21, 500000
br i1 %22, label %23, label %24
23: ; preds = %13
br label %25
24: ; preds = %13
br label %9
25: ; preds = %23, %9
%26 = load i64, i64* %7
%27 = icmp ne i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store double -1.000000e+00, double* %3
br label %37
30: ; preds = %25
%31 = load i64, i64* %7
%32 = uitofp i64 %31 to double
%33 = fmul double 1.000000e+06, %32
%34 = load i32, i32* %6
%35 = sitofp i32 %34 to double
%36 = fdiv double %33, %35
store double %36, double* %3
br label %37
37: ; preds = %30, %28
%38 = load double, double* %3
ret double %38
}

LLVM IR (-O2):
@.str = [17 x i8] c"Benchmarking... \00"
@str = [17 x i8] c"CPU too fast :-)\00"
define double @benchmark(void (i64, i8*)* nocapture %0, i8* %1) {
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %8, %2
%5 = phi i64 [ 1, %2 ], [ %6, %8 ]
%6 = shl i64 %5, 1
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32 (...) @get_ticks()
tail call void %0(i64 %6, i8* %1)
%10 = tail call i32 (...) @get_ticks()
%11 = sub nsw i32 %10, %9
%12 = icmp sgt i32 %11, 499999
br i1 %12, label %15, label %4
13: ; preds = %4
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %20
15: ; preds = %8
%16 = uitofp i64 %6 to double
%17 = fmul double %16, 1.000000e+06
%18 = sitofp i32 %11 to double
%19 = fdiv double %17, %18
br label %20
20: ; preds = %15, %13
%21 = phi double [ %19, %15 ], [ -1.000000e+00, %13 ]
ret double %21
}