#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_sec;
  long tv_nsec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ do_work();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void measure_diff_impl(clockid_t clk_id, const char *clock_name) {
  struct timespec start = {}, finish = {};
  clock_gettime(clk_id, &start);
  do_work();
  int res = clock_gettime(clk_id, &finish);
  if (res != 0) {
    fprintf(stderr, "Error clock_gettime(%s): %s\n", clock_name,
            strerror(errno));
    assert(res == 0);
  }
  long seconds = finish.tv_sec - start.tv_sec;
  long ns = finish.tv_nsec - start.tv_nsec;
  long total = (double)seconds + (double)ns / (double)1000000000;
  assert(total >= 0);
}


x86 assembly (clang, -O0):
<measure_diff_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x0,%al
call   1050 <do_work@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1204 <measure_diff_impl+0xa4>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
cmpl   $0x0,-0x34(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
cvtsi2sdq -0x40(%rbp),%xmm0
cvtsi2sdq -0x48(%rbp),%xmm1
movsd  0xdd0(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_diff_impl>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %eax,%eax
call   1040 <do_work@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1030 <clock_gettime@plt>
test   %eax,%eax
je     11b8 <measure_diff_impl+0x68>
mov    0x2e53(%rip),%rax
mov    (%rax),%ebp
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe64(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1050 <assert@plt>
mov    (%rsp),%rax
mov    0x8(%rsp),%rcx
sub    0x10(%rsp),%rax
sub    0x18(%rsp),%rcx
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
cvtsi2sd %rcx,%xmm1
divsd  0xe20(%rip),%xmm1
addsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1050 <assert@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"Error clock_gettime(%s): %s\0A\00"
@errno = global i32 0
define void @measure_diff_impl(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%11 = bitcast %struct.timespec* %5 to i8*
%12 = bitcast %struct.timespec* %6 to i8*
%13 = load i32, i32* %3
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %5)
%15 = call i32 (...) @do_work()
%16 = load i32, i32* %3
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %6)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
br label %30
30: ; preds = %20, %2
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%34 = load i64, i64* %33
%35 = sub nsw i64 %32, %34
store i64 %35, i64* %8
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%37 = load i64, i64* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%39 = load i64, i64* %38
%40 = sub nsw i64 %37, %39
store i64 %40, i64* %9
%41 = load i64, i64* %8
%42 = sitofp i64 %41 to double
%43 = load i64, i64* %9
%44 = sitofp i64 %43 to double
%45 = fdiv double %44, 1.000000e+09
%46 = fadd double %42, %45
%47 = fptosi double %46 to i64
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = icmp sge i64 %48, 0
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"Error clock_gettime(%s): %s\0A\00"
@errno = global i32 0
define void @measure_diff_impl(i32 %0, i8* %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = call i32 @clock_gettime(i32 %0, %struct.timespec* %3)
%8 = call i32 (...) @do_work()
%9 = call i32 @clock_gettime(i32 %0, %struct.timespec* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %14)
%16 = call i32 @assert(i32 0)
br label %17
17: ; preds = %11, %2
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = sub nsw i64 %19, %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%26 = load i64, i64* %25
%27 = sub nsw i64 %24, %26
%28 = sitofp i64 %22 to double
%29 = sitofp i64 %27 to double
%30 = fdiv double %29, 1.000000e+09
%31 = fadd double %30, %28
%32 = fptosi double %31 to i64
%33 = lshr i64 %32, 63
%34 = trunc i64 %33 to i32
%35 = xor i32 %34, 1
%36 = call i32 @assert(i32 %35)
ret void
}