#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ _SC_MONOTONIC_CLOCK;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
void sandglass_get_currtime(struct timespec *ts) {
  static int monotonic = 0;
  if (!monotonic) {
    monotonic = sysconf(_SC_MONOTONIC_CLOCK);
  }
  if (monotonic > 0) {
    if (clock_gettime(CLOCK_MONOTONIC, ts) != 0) {
      fprintf(stderr,
              "libsandglass: couldn't get value of clock CLOCK_MONOTONIC\n");
      exit(EXIT_FAILURE);
    }
  } else {
    if (clock_gettime(CLOCK_REALTIME, ts) != 0) {
      fprintf(stderr,
              "libsandglass: couldn't get value of clock CLOCK_REALTIME\n");
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<sandglass_get_currtime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpl   $0x0,0x2ef1(%rip)
jne    116d <sandglass_get_currtime+0x2d>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,0x2ed7(%rip)
cmpl   $0x0,0x2ed0(%rip)
jle    11be <sandglass_get_currtime+0x7e>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     11b9 <sandglass_get_currtime+0x79>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1202 <sandglass_get_currtime+0xc2>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     11fd <sandglass_get_currtime+0xbd>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1202 <sandglass_get_currtime+0xc2>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sandglass_get_currtime>:
push   %rbx
mov    %rdi,%rbx
mov    0x2efa(%rip),%eax
test   %eax,%eax
je     117a <sandglass_get_currtime+0x3a>
test   %eax,%eax
jle    1192 <sandglass_get_currtime+0x52>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     11a8 <sandglass_get_currtime+0x68>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
jmp    11ba <sandglass_get_currtime+0x7a>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,0x2eb6(%rip)
test   %eax,%eax
jg     1152 <sandglass_get_currtime+0x12>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    11aa <sandglass_get_currtime+0x6a>
pop    %rbx
ret
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@sandglass_get_currtime.monotonic = internal global i32 0
@_SC_MONOTONIC_CLOCK = global i32 0
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [59 x i8] c"libsandglass: couldn't get value of clock CLOCK_MONOTONIC\0A\00"
@EXIT_FAILURE = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [58 x i8] c"libsandglass: couldn't get value of clock CLOCK_REALTIME\0A\00"
define void @sandglass_get_currtime(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
store %struct.timespec* %0, %struct.timespec** %2
%3 = load i32, i32* @sandglass_get_currtime.monotonic
%4 = icmp ne i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = load i32, i32* @_SC_MONOTONIC_CLOCK
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* @sandglass_get_currtime.monotonic
br label %8
8: ; preds = %5, %1
%9 = load i32, i32* @sandglass_get_currtime.monotonic
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = load %struct.timespec*, %struct.timespec** %2
%14 = call i64 @clock_gettime(i32 %12, %struct.timespec* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
br label %33
22: ; preds = %8
%23 = load i32, i32* @CLOCK_REALTIME
%24 = load %struct.timespec*, %struct.timespec** %2
%25 = call i64 @clock_gettime(i32 %23, %struct.timespec* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
br label %33
33: ; preds = %32, %21
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@sandglass_get_currtime.monotonic = internal unnamed_addr global i32 0
@_SC_MONOTONIC_CLOCK = global i32 0
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [59 x i8] c"libsandglass: couldn't get value of clock CLOCK_MONOTONIC\0A\00"
@EXIT_FAILURE = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [58 x i8] c"libsandglass: couldn't get value of clock CLOCK_REALTIME\0A\00"
define void @sandglass_get_currtime(%struct.timespec* %0) {
%2 = load i32, i32* @sandglass_get_currtime.monotonic
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @_SC_MONOTONIC_CLOCK
%6 = tail call i32 @sysconf(i32 %5)
store i32 %6, i32* @sandglass_get_currtime.monotonic
br label %7
7: ; preds = %4, %1
%8 = phi i32 [ %6, %4 ], [ %2, %1 ]
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = tail call i64 @clock_gettime(i32 %11, %struct.timespec* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %7
%20 = load i32, i32* @CLOCK_REALTIME
%21 = tail call i64 @clock_gettime(i32 %20, %struct.timespec* %0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %19, %10
ret void
}