#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 double clock_t;

/* Variables and functions */
double clock();
int /*<<< orphan*/ printf(char *, double, char *);
void time_test(void (*test_func)()) {
  clock_t start = clock();
  test_func();
  clock_t end = clock();
  double elapsed = end - start;
  char *unit = "ns";
  if (elapsed > 1e9) {
    elapsed /= 1e9;
    unit = "s";
  } else if (elapsed > 1e6) {
    elapsed /= 1e6;
    unit = "ms";
  } else if (elapsed > 1e3) {
    elapsed /= 1e3;
    unit = "μs";
  }
  printf("\nelapsed time: %.3f%s\n", elapsed, unit);
}


x86 assembly (clang, -O0):
<time_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   *-0x8(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    0xeb9(%rip),%rax
mov    %rax,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe90(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11a0 <time_test+0x80>
movsd  -0x20(%rbp),%xmm0
movsd  0xe79(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    0xe85(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    121f <time_test+0xff>
movsd  -0x20(%rbp),%xmm0
movsd  0xe5b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11dd <time_test+0xbd>
movsd  -0x20(%rbp),%xmm0
movsd  0xe44(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    0xe47(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    121a <time_test+0xfa>
movsd  -0x20(%rbp),%xmm0
movsd  0xe26(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1215 <time_test+0xf5>
movsd  -0x20(%rbp),%xmm0
movsd  0xe0f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    0xe0d(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    121a <time_test+0xfa>
jmp    121f <time_test+0xff>
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_test>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <clock@plt>
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   *%rbx
xor    %eax,%eax
call   1030 <clock@plt>
subsd  0x8(%rsp),%xmm0
ucomisd 0xeb2(%rip),%xmm0
jbe    1161 <time_test+0x41>
divsd  0xea8(%rip),%xmm0
lea    0xebd(%rip),%rsi
jmp    119e <time_test+0x7e>
ucomisd 0xe9f(%rip),%xmm0
jbe    117c <time_test+0x5c>
divsd  0xe95(%rip),%xmm0
lea    0xea1(%rip),%rsi
jmp    119e <time_test+0x7e>
ucomisd 0xe8c(%rip),%xmm0
jbe    1197 <time_test+0x77>
divsd  0xe82(%rip),%xmm0
lea    0xe89(%rip),%rsi
jmp    119e <time_test+0x7e>
lea    0xe7a(%rip),%rsi
lea    0xe7d(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"ns\00"
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [3 x i8] c"ms\00"
@.str.3 = [4 x i8] c"\CE\BCs\00"
@.str.4 = [23 x i8] c"\0Aelapsed time: %.3f%s\0A\00"
define void @time_test(void (...)* %0) {
%2 = alloca void (...)*
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca i8*
store void (...)* %0, void (...)** %2
%7 = call double (...) @clock()
store double %7, double* %3
%8 = load void (...)*, void (...)** %2
%9 = call double (...) @clock()
store double %9, double* %4
%10 = load double, double* %4
%11 = load double, double* %3
%12 = fsub double %10, %11
store double %12, double* %5
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %6
%13 = load double, double* %5
%14 = fcmp ogt double %13, 1.000000e+09
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load double, double* %5
%17 = fdiv double %16, 1.000000e+09
store double %17, double* %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %32
18: ; preds = %1
%19 = load double, double* %5
%20 = fcmp ogt double %19, 1.000000e+06
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load double, double* %5
%23 = fdiv double %22, 1.000000e+06
store double %23, double* %5
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %31
24: ; preds = %18
%25 = load double, double* %5
%26 = fcmp ogt double %25, 1.000000e+03
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load double, double* %5
%29 = fdiv double %28, 1.000000e+03
store double %29, double* %5
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %6
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30, %21
br label %32
32: ; preds = %31, %15
%33 = load double, double* %5
%34 = load i8*, i8** %6
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), double %33, i8* %34)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"ns\00"
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [3 x i8] c"ms\00"
@.str.3 = [4 x i8] c"\CE\BCs\00"
@.str.4 = [23 x i8] c"\0Aelapsed time: %.3f%s\0A\00"
define void @time_test(void (...)* nocapture %0) {
%2 = tail call double (...) @clock()
tail call void (...) %0()
%3 = tail call double (...) @clock()
%4 = fsub double %3, %2
%5 = fcmp ogt double %4, 1.000000e+09
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = fdiv double %4, 1.000000e+09
br label %16
8: ; preds = %1
%9 = fcmp ogt double %4, 1.000000e+06
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = fdiv double %4, 1.000000e+06
br label %16
12: ; preds = %8
%13 = fcmp ogt double %4, 1.000000e+03
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = fdiv double %4, 1.000000e+03
br label %16
16: ; preds = %10, %14, %12, %6
%17 = phi double [ %7, %6 ], [ %11, %10 ], [ %15, %14 ], [ %4, %12 ]
%18 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %12 ]
%19 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), double %17, i8* %18)
ret void
}