#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 timespec {
  long tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
double time_it(int (*action)(int), int arg) {
  struct timespec tsi, tsf;
  clock_gettime(CLOCK_MONOTONIC, &tsi);
  action(arg);
  clock_gettime(CLOCK_MONOTONIC, &tsf);
  double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec);
  long elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
  return elaps_s + ((double)elaps_ns) / 1.0e9;
}


x86 assembly (clang, -O0):
<time_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%edi
call   *%rax
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <difftime@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
cvtsi2sdq -0x40(%rbp),%xmm1
movsd  0xe75(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_it>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2eb3(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %ebx,%edi
call   *%r14
mov    (%r15),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    0x18(%rsp),%esi
call   1040 <difftime@plt>
mov    (%rsp),%rax
sub    0x10(%rsp),%rax
cvtsi2sd %rax,%xmm1
divsd  0xe8b(%rip),%xmm1
addsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
define double @time_it(i32 (i32)* %0, i32 %1) {
%3 = alloca i32 (i32)*
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca double
%8 = alloca i64
store i32 (i32)* %0, i32 (i32)** %3
store i32 %1, i32* %4
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %5)
%11 = load i32 (i32)*, i32 (i32)** %3
%12 = load i32, i32* %4
%13 = call i32 %11(i32 %12)
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %6)
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call double @difftime(i32 %17, i32 %19)
store double %20, double* %7
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%24 = load i64, i64* %23
%25 = sub nsw i64 %22, %24
store i64 %25, i64* %8
%26 = load double, double* %7
%27 = load i64, i64* %8
%28 = sitofp i64 %27 to double
%29 = fdiv double %28, 1.000000e+09
%30 = fadd double %26, %29
ret double %30
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
define double @time_it(i32 (i32)* nocapture %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = call i32 %0(i32 %1)
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %4)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call double @difftime(i32 %13, i32 %15)
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = sub nsw i64 %18, %20
%22 = sitofp i64 %21 to double
%23 = fdiv double %22, 1.000000e+09
%24 = fadd double %16, %23
ret double %24
}