#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 {
  int tv_nsec;
};
typedef scalar_t__ clockid_t;

/* Variables and functions */
scalar_t__ CLOCK_MONOTONIC;
scalar_t__ CLOCK_REALTIME;
int clock_getres(scalar_t__, struct timespec *);
int /*<<< orphan*/ clock_gettime(scalar_t__, struct timespec *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  struct timespec res, t1, t2;
  clockid_t clock_id = CLOCK_MONOTONIC;
  int ok = clock_getres(clock_id, &res);
  if (ok < 0) {
    clock_id = CLOCK_REALTIME;
    ok = clock_getres(clock_id, &res);
    if (ok < 0) {
      perror("No system clocks available");
      exit(-1);
    }
  }
  printf("Clock type: %s\n",
         clock_id == CLOCK_MONOTONIC ? "CLOCK_MONOTONIC" : "CLOCK_REALTIME");
  printf("Reported Clock Resolution: %ldns\n", res.tv_nsec);
  clock_gettime(clock_id, &t1);
  clock_gettime(clock_id, &t2);
  printf("Real clock resolution: %ldns\n", t2.tv_nsec - t1.tv_nsec);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <clock_getres@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ca <main+0x7a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <clock_getres@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c5 <main+0x75>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11ca <main+0x7a>
mov    -0x20(%rbp),%rcx
mov    0x2e13(%rip),%rax
mov    (%rax),%rdx
lea    0xe5c(%rip),%rsi
lea    0xe45(%rip),%rax
cmp    %rdx,%rcx
cmove  %rax,%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%esi
sub    -0x10(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e8a(%rip),%r14
mov    (%r14),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <clock_getres@plt>
test   %eax,%eax
jns    1189 <main+0x39>
mov    0x2e61(%rip),%rax
mov    (%rax),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <clock_getres@plt>
test   %eax,%eax
js     11f7 <main+0xa7>
cmp    (%r14),%rbx
lea    0xe98(%rip),%rax
lea    0xea1(%rip),%rsi
cmove  %rax,%rsi
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%esi
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <clock_gettime@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%esi
sub    0x10(%rsp),%esi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe02(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i64 0
@CLOCK_REALTIME = global i64 0
@.str = [27 x i8] c"No system clocks available\00"
@.str.1 = [16 x i8] c"Clock type: %s\0A\00"
@.str.2 = [16 x i8] c"CLOCK_MONOTONIC\00"
@.str.3 = [15 x i8] c"CLOCK_REALTIME\00"
@.str.4 = [34 x i8] c"Reported Clock Resolution: %ldns\0A\00"
@.str.5 = [30 x i8] c"Real clock resolution: %ldns\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = load i64, i64* @CLOCK_MONOTONIC
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = call i32 @clock_getres(i64 %8, %struct.timespec* %2)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %0
%13 = load i64, i64* @CLOCK_REALTIME
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = call i32 @clock_getres(i64 %14, %struct.timespec* %2)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %12
br label %22
22: ; preds = %21, %0
%23 = load i64, i64* %5
%24 = load i64, i64* @CLOCK_MONOTONIC
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i64
%27 = select i1 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i64, i64* %5
%33 = call i32 @clock_gettime(i64 %32, %struct.timespec* %3)
%34 = load i64, i64* %5
%35 = call i32 @clock_gettime(i64 %34, %struct.timespec* %4)
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sub nsw i32 %37, %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %40)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i64 0
@CLOCK_REALTIME = global i64 0
@.str = [27 x i8] c"No system clocks available\00"
@.str.1 = [16 x i8] c"Clock type: %s\0A\00"
@.str.2 = [16 x i8] c"CLOCK_MONOTONIC\00"
@.str.3 = [15 x i8] c"CLOCK_REALTIME\00"
@.str.4 = [34 x i8] c"Reported Clock Resolution: %ldns\0A\00"
@.str.5 = [30 x i8] c"Real clock resolution: %ldns\0A\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = load i64, i64* @CLOCK_MONOTONIC
%8 = call i32 @clock_getres(i64 %7, %struct.timespec* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = load i64, i64* @CLOCK_REALTIME
%12 = call i32 @clock_getres(i64 %11, %struct.timespec* %1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %10, %0
%18 = phi i64 [ %11, %10 ], [ %7, %0 ]
%19 = load i64, i64* @CLOCK_MONOTONIC
%20 = icmp eq i64 %18, %19
%21 = select i1 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 @clock_gettime(i64 %18, %struct.timespec* %2)
%27 = call i32 @clock_gettime(i64 %18, %struct.timespec* %3)
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%31 = load i32, i32* %30
%32 = sub nsw i32 %29, %31
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %32)
ret i32 0
}