#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef scalar_t__ pf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, unsigned int, unsigned int,
                          unsigned int);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
void run_timed_loop(char *name, int (*pf)(unsigned int, char *, double),
                    unsigned int count, char *fmt, double val) {
  struct timeval start, end, delta;
  if (gettimeofday(&start, NULL) != 0)
    exit(EXIT_FAILURE);
  if ((pf)(count, fmt, val) != 0)
    exit(EXIT_FAILURE);
  if (gettimeofday(&end, NULL) != 0)
    exit(EXIT_FAILURE);
  timersub(&end, &start, &delta);
  printf(
      "   %s took %u.%6.6u seconds (%fus per iteration)\n", name,
      (unsigned int)delta.tv_sec, (unsigned int)delta.tv_usec,
      (((unsigned int)delta.tv_sec) * 1000000.0 + (unsigned int)delta.tv_usec) /
          count);
}


x86 assembly (clang, -O0):
<run_timed_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     1181 <run_timed_loop+0x41>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movsd  -0x28(%rbp),%xmm0
call   *%rax
cmp    $0x0,%eax
je     11aa <run_timed_loop+0x6a>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     11cf <run_timed_loop+0x8f>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
lea    -0x48(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1060 <timersub@plt>
mov    -0x8(%rbp),%rsi
mov    -0x58(%rbp),%eax
mov    -0x50(%rbp),%ecx
mov    %ecx,%edi
mov    %edi,%edx
mov    %eax,%ecx
cvtsi2sd %rdi,%xmm0
cvtsi2sd %rax,%xmm1
movsd  0xdfe(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x14(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,%r8d
lea    0xde3(%rip),%rdi
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_timed_loop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
movsd  %xmm0,(%rsp)
mov    %rcx,%rbp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x28(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %rax,%rax
jne    11f2 <run_timed_loop+0xb2>
mov    %r15d,%edi
mov    %rbp,%rsi
movsd  (%rsp),%xmm0
call   *%rbx
test   %eax,%eax
jne    11f2 <run_timed_loop+0xb2>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %rax,%rax
jne    11f2 <run_timed_loop+0xb2>
lea    0x18(%rsp),%rdi
lea    0x28(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <timersub@plt>
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%edx
xorps  %xmm0,%xmm0
cvtsi2sd %rdx,%xmm0
cvtsi2sd %rcx,%xmm1
mulsd  0xe3d(%rip),%xmm0
mov    %r15d,%eax
cvtsi2sd %rax,%xmm2
addsd  %xmm1,%xmm0
divsd  %xmm2,%xmm0
cvttsd2si %xmm0,%r8
lea    0xe29(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@EXIT_FAILURE = global i32 0
@.str = [50 x i8] c" %s took %u.%6.6u seconds (%fus per iteration)\0A\00"
define void @run_timed_loop(i8* %0, i32 (i32, i8*, double)* %1, i32 %2, i8* %3, double %4) {
%6 = alloca i8*
%7 = alloca i32 (i32, i8*, double)*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca double
%11 = alloca %struct.timeval
%12 = alloca %struct.timeval
%13 = alloca %struct.timeval
store i8* %0, i8** %6
store i32 (i32, i8*, double)* %1, i32 (i32, i8*, double)** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store double %4, double* %10
%14 = call i64 @gettimeofday(%struct.timeval* %11, i32* null)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %5
%20 = load i32 (i32, i8*, double)*, i32 (i32, i8*, double)** %7
%21 = load i32, i32* %8
%22 = load i8*, i8** %9
%23 = load double, double* %10
%24 = call i32 %20(i32 %21, i8* %22, double %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = call i64 @gettimeofday(%struct.timeval* %12, i32* null)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
%36 = call i32 @timersub(%struct.timeval* %12, %struct.timeval* %11, %struct.timeval* %13)
%37 = load i8*, i8** %6
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%45 = load i64, i64* %44
%46 = trunc i64 %45 to i32
%47 = uitofp i32 %46 to double
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%49 = load i64, i64* %48
%50 = trunc i64 %49 to i32
%51 = uitofp i32 %50 to double
%52 = call double @llvm.fmuladd.f64(double %47, double 1.000000e+06, double %51)
%53 = load i32, i32* %8
%54 = uitofp i32 %53 to double
%55 = fdiv double %52, %54
%56 = fptoui double %55 to i32
%57 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %37, i32 %40, i32 %43, i32 %56)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@EXIT_FAILURE = global i32 0
@.str = [50 x i8] c" %s took %u.%6.6u seconds (%fus per iteration)\0A\00"
define void @run_timed_loop(i8* %0, i32 (i32, i8*, double)* nocapture %1, i32 %2, i8* %3, double %4) {
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = bitcast %struct.timeval* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = bitcast %struct.timeval* %8 to i8*
%12 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %5
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %5
%18 = tail call i32 %1(i32 %2, i8* %3, double %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %17
%24 = call i64 @gettimeofday(%struct.timeval* %7, i32* null)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %23
%30 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %6, %struct.timeval* %8)
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = uitofp i32 %33 to double
%38 = uitofp i32 %36 to double
%39 = call double @llvm.fmuladd.f64(double %37, double 1.000000e+06, double %38)
%40 = uitofp i32 %2 to double
%41 = fdiv double %39, %40
%42 = fptoui double %41 to i32
%43 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %33, i32 %36, i32 %42)
ret void
}