#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 {
  unsigned long long tv_sec;
  scalar_t__ tv_nsec;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int /*<<< orphan*/ __NR_getpid;
int /*<<< orphan*/ assert(int);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, unsigned long long, scalar_t__,
                          unsigned long long, scalar_t__, unsigned long long,
                          double);
scalar_t__ syscall(int /*<<< orphan*/);
unsigned long long timing(clockid_t clk_id, unsigned long long samples) {
  struct timespec start, finish;
  unsigned long long i;
  pid_t pid, ret;
  pid = getpid();
  assert(clock_gettime(clk_id, &start) == 0);
  for (i = 0; i < samples; i++) {
    ret = syscall(__NR_getpid);
    assert(pid == ret);
  }
  assert(clock_gettime(clk_id, &finish) == 0);
  i = finish.tv_sec - start.tv_sec;
  i *= 1000000000ULL;
  i += finish.tv_nsec - start.tv_nsec;
  printf("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n", finish.tv_sec,
         finish.tv_nsec, start.tv_sec, start.tv_nsec, i,
         (double)i / 1000000000.0);
  return i;
}


x86 assembly (clang, -O0):
<timing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11d5 <timing+0x85>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    118f <timing+0x3f>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
sete   %al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
imul   $0x3b9aca00,%rax,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x38(%rbp),%r9
movq   %r9,%xmm0
movaps 0xdcd(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xdd1(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd9a(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xdbf(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timing>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbp
lea    0x18(%rsp),%rsi
mov    %r14d,%edi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
test   %rbx,%rbx
je     11bc <timing+0x6c>
mov    0x2e57(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edi
call   1070 <syscall@plt>
xor    %edi,%edi
cmp    %rax,%rbp
sete   %dil
call   1060 <assert@plt>
add    $0xffffffffffffffff,%rbx
jne    11a0 <timing+0x50>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    0x18(%rsp),%rcx
mov    0x20(%rsp),%r8
mov    %rsi,%rax
sub    %rcx,%rax
imul   $0x3b9aca00,%rax,%rax
mov    %rdx,%rbx
sub    %r8,%rbx
add    %rax,%rbx
movq   %rbx,%xmm1
punpckldq 0xdf2(%rip),%xmm1
subpd  0xdfa(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
divsd  0xdf6(%rip),%xmm0
lea    0xdf7(%rip),%rdi
mov    %rbx,%r9
call   1050 <printf@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@__NR_getpid = global i32 0
@.str = [38 x i8] c"%lu.%09lu - %lu.%09lu = %llu (%.1fs)\0A\00"
define i64 @timing(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
%10 = call i64 (...) @getpid()
store i64 %10, i64* %8
%11 = load i32, i32* %3
%12 = call i64 @clock_gettime(i32 %11, %struct.timespec* %5)
%13 = icmp eq i64 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
store i64 0, i64* %7
br label %16
16: ; preds = %28, %2
%17 = load i64, i64* %7
%18 = load i64, i64* %4
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = load i32, i32* @__NR_getpid
%22 = call i64 @syscall(i32 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %8
%24 = load i64, i64* %9
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
br label %28
28: ; preds = %20
%29 = load i64, i64* %7
%30 = add i64 %29, 1
store i64 %30, i64* %7
br label %16
31: ; preds = %16
%32 = load i32, i32* %3
%33 = call i64 @clock_gettime(i32 %32, %struct.timespec* %6)
%34 = icmp eq i64 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%40 = load i64, i64* %39
%41 = sub i64 %38, %40
store i64 %41, i64* %7
%42 = load i64, i64* %7
%43 = mul i64 %42, 1000000000
store i64 %43, i64* %7
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%47 = load i64, i64* %46
%48 = sub nsw i64 %45, %47
%49 = load i64, i64* %7
%50 = add i64 %49, %48
store i64 %50, i64* %7
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%52 = load i64, i64* %51
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%54 = load i64, i64* %53
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%56 = load i64, i64* %55
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%58 = load i64, i64* %57
%59 = load i64, i64* %7
%60 = load i64, i64* %7
%61 = uitofp i64 %60 to double
%62 = fdiv double %61, 1.000000e+09
%63 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %52, i64 %54, i64 %56, i64 %58, i64 %59, double %62)
%64 = load i64, i64* %7
ret i64 %64
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@__NR_getpid = global i32 0
@.str = [38 x i8] c"%lu.%09lu - %lu.%09lu = %llu (%.1fs)\0A\00"
define i64 @timing(i32 %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = tail call i64 (...) @getpid()
%8 = call i64 @clock_gettime(i32 %0, %struct.timespec* %3)
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = icmp eq i64 %1, 0
br i1 %12, label %22, label %13
13: ; preds = %2, %13
%14 = phi i64 [ %20, %13 ], [ 0, %2 ]
%15 = load i32, i32* @__NR_getpid
%16 = call i64 @syscall(i32 %15)
%17 = icmp eq i64 %7, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = add nuw i64 %14, 1
%21 = icmp eq i64 %20, %1
br i1 %21, label %22, label %13
22: ; preds = %13, %2
%23 = call i64 @clock_gettime(i32 %0, %struct.timespec* %4)
%24 = icmp eq i64 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = sub i64 %28, %30
%32 = mul i64 %31, 1000000000
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%34 = load i64, i64* %33
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%36 = load i64, i64* %35
%37 = sub nsw i64 %34, %36
%38 = add i64 %37, %32
%39 = uitofp i64 %38 to double
%40 = fdiv double %39, 1.000000e+09
%41 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %28, i64 %34, i64 %30, i64 %36, i64 %38, double %40)
ret i64 %38
}