#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 {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, char const *, scalar_t__, scalar_t__);
void benchmark(const char *s, void (*f)(void)) {
  struct timespec start, stop, elapsed;
  clock_gettime(CLOCK_MONOTONIC, &start);
  f();
  clock_gettime(CLOCK_MONOTONIC, &stop);
  elapsed.tv_sec = stop.tv_sec - start.tv_sec;
  elapsed.tv_nsec = stop.tv_nsec - start.tv_nsec;
  if (elapsed.tv_nsec < 0) {
    elapsed.tv_nsec += 1000000000;
    elapsed.tv_sec--;
  }
  printf("benchmarked %s:\t%ld.%09lds\n", s, elapsed.tv_sec, elapsed.tv_nsec);
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
call   *-0x10(%rbp)
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1194 <benchmark+0x74>
mov    -0x40(%rbp),%rax
add    $0x3b9aca00,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2eb2(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
call   *%rbx
mov    (%r15),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%rax
mov    0x8(%rsp),%rsi
sub    0x18(%rsp),%rsi
sub    0x10(%rsp),%rax
lea    0x3b9aca00(%rax),%rcx
mov    %rax,%rdx
sar    $0x3f,%rdx
add    %rsi,%rdx
test   %rax,%rax
cmovns %rax,%rcx
lea    0xe7e(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [28 x i8] c"benchmarked %s:\09%ld.%09lds\0A\00"
define void @benchmark(i8* %0, void ()* %1) {
%3 = alloca i8*
%4 = alloca void ()*
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
store i8* %0, i8** %3
store void ()* %1, void ()** %4
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %5)
%10 = load void ()*, void ()** %4
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %6)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%16 = load i64, i64* %15
%17 = sub nsw i64 %14, %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%22 = load i64, i64* %21
%23 = sub nsw i64 %20, %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
store i64 %23, i64* %24
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %2
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%30 = load i64, i64* %29
%31 = add nsw i64 %30, 1000000000
store i64 %31, i64* %29
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%33 = load i64, i64* %32
%34 = add nsw i64 %33, -1
store i64 %34, i64* %32
br label %35
35: ; preds = %28, %2
%36 = load i8*, i8** %3
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%40 = load i64, i64* %39
%41 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %36, i64 %38, i64 %40)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [28 x i8] c"benchmarked %s:\09%ld.%09lds\0A\00"
define void @benchmark(i8* %0, void ()* nocapture %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 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %4)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%12 = load i64, i64* %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%14 = load i64, i64* %13
%15 = sub i64 %12, %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%19 = load i64, i64* %18
%20 = sub nsw i64 %17, %19
%21 = icmp slt i64 %20, 0
%22 = add nsw i64 %20, 1000000000
%23 = ashr i64 %20, 63
%24 = add nsw i64 %15, %23
%25 = select i1 %21, i64 %22, i64 %20
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %24, i64 %25)
ret void
}