#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;
long atol(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, scalar_t__);
int main(int argc, char **argv) {
  struct timespec tm;
  struct timespec tm1;
  struct timespec tm2;
  long loopamount = 1;
  long i = 0;
  if (argc >= 2) {
    loopamount = atol(argv[1]);
  }
  for (i = 0; i < loopamount; i++) {
    tm1.tv_sec = 0;
    tm1.tv_nsec = 0;
    tm2.tv_sec = 0;
    tm2.tv_nsec = 0;
    clock_gettime(CLOCK_MONOTONIC, &tm1);
    clock_gettime(CLOCK_MONOTONIC, &tm2);
    printf("time for 2 gettime calls: %li\n", (tm2.tv_nsec - tm1.tv_nsec));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1171 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atol@plt>
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    11f0 <main+0xc0>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x40(%rbp),%rsi
sub    -0x30(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1179 <main+0x49>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    $0x1,%ebx
cmp    $0x2,%edi
jl     1158 <main+0x28>
mov    0x8(%rsi),%rdi
call   1050 <atol@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11ab <main+0x7b>
mov    0x2e89(%rip),%r13
lea    0x10(%rsp),%r14
mov    %rsp,%r15
lea    0xe92(%rip),%r12
xchg   %ax,%ax
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%rsi
sub    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1040 <printf@plt>
add    $0xffffffffffffffff,%rbx
jne    1170 <main+0x40>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [31 x i8] c"time for 2 gettime calls: %li\0A\00"
define i32 @main(i32 %0, i8** 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 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @atol(i8* %10)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %2, %8
%14 = phi i64 [ %11, %8 ], [ 1, %2 ]
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%17 = bitcast %struct.timespec* %3 to i8*
%18 = bitcast %struct.timespec* %4 to i8*
br label %19
19: ; preds = %13, %19
%20 = phi i64 [ 0, %13 ], [ %29, %19 ]
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %3)
%23 = load i32, i32* @CLOCK_MONOTONIC
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %4)
%25 = load i64, i64* %16
%26 = load i64, i64* %15
%27 = sub nsw i64 %25, %26
%28 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %27)
%29 = add nuw nsw i64 %20, 1
%30 = icmp eq i64 %29, %14
br i1 %30, label %31, label %19
31: ; preds = %19, %8
ret i32 0
}