#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 */
typedef int clockid_t;

/* Variables and functions */
int CLOCK_BOOTTIME;
int CLOCK_MONOTONIC;
int TFD_CLOEXEC;
int TFD_NONBLOCK;
int /*<<< orphan*/ safe_close(int);
int timerfd_create(int, int);
clockid_t clock_boottime_or_monotonic(void) {
  static clockid_t clock = -1;
  int fd;
  if (clock != -1)
    return clock;
  fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK | TFD_CLOEXEC);
  if (fd < 0)
    clock = CLOCK_MONOTONIC;
  else {
    safe_close(fd);
    clock = CLOCK_BOOTTIME;
  }
  return clock;
}


x86 assembly (clang, -O0):
<clock_boottime_or_monotonic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2f01(%rip)
je     1143 <clock_boottime_or_monotonic+0x23>
mov    0x2ef5(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <clock_boottime_or_monotonic+0x84>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
call   1040 <timerfd_create@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1184 <clock_boottime_or_monotonic+0x64>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2eb1(%rip)
jmp    119b <clock_boottime_or_monotonic+0x7b>
mov    -0x8(%rbp),%edi
call   1030 <safe_close@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e95(%rip)
mov    0x2e8f(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_boottime_or_monotonic>:
push   %rbx
mov    0x2f09(%rip),%eax
cmp    $0xffffffff,%eax
je     112e <clock_boottime_or_monotonic+0xe>
pop    %rbx
ret
mov    0x2eab(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e8a(%rip),%rax
mov    0x2ea3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <timerfd_create@plt>
test   %eax,%eax
js     115b <clock_boottime_or_monotonic+0x3b>
mov    %eax,%edi
call   1030 <safe_close@plt>
jmp    1162 <clock_boottime_or_monotonic+0x42>
mov    0x2e76(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x2ec6(%rip)
pop    %rbx
ret

LLVM IR (-O0):
@clock_boottime_or_monotonic.clock = internal global i32 -1
@CLOCK_BOOTTIME = global i32 0
@TFD_NONBLOCK = global i32 0
@TFD_CLOEXEC = global i32 0
@CLOCK_MONOTONIC = global i32 0
define i32 @clock_boottime_or_monotonic() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @clock_boottime_or_monotonic.clock
%4 = icmp ne i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = load i32, i32* @clock_boottime_or_monotonic.clock
store i32 %6, i32* %1
br label %23
7: ; preds = %0
%8 = load i32, i32* @CLOCK_BOOTTIME
%9 = load i32, i32* @TFD_NONBLOCK
%10 = load i32, i32* @TFD_CLOEXEC
%11 = or i32 %9, %10
%12 = call i32 @timerfd_create(i32 %8, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = load i32, i32* @CLOCK_MONOTONIC
store i32 %16, i32* @clock_boottime_or_monotonic.clock
br label %21
17: ; preds = %7
%18 = load i32, i32* %2
%19 = call i32 @safe_close(i32 %18)
%20 = load i32, i32* @CLOCK_BOOTTIME
store i32 %20, i32* @clock_boottime_or_monotonic.clock
br label %21
21: ; preds = %17, %15
%22 = load i32, i32* @clock_boottime_or_monotonic.clock
store i32 %22, i32* %1
br label %23
23: ; preds = %21, %5
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@clock_boottime_or_monotonic.clock = internal unnamed_addr global i32 -1
@CLOCK_BOOTTIME = global i32 0
@TFD_NONBLOCK = global i32 0
@TFD_CLOEXEC = global i32 0
@CLOCK_MONOTONIC = global i32 0
define i32 @clock_boottime_or_monotonic() {
%1 = load i32, i32* @clock_boottime_or_monotonic.clock
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %15
3: ; preds = %0
%4 = load i32, i32* @CLOCK_BOOTTIME
%5 = load i32, i32* @TFD_NONBLOCK
%6 = load i32, i32* @TFD_CLOEXEC
%7 = or i32 %6, %5
%8 = tail call i32 @timerfd_create(i32 %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = tail call i32 @safe_close(i32 %8)
br label %12
12: ; preds = %3, %10
%13 = phi i32* [ @CLOCK_BOOTTIME, %10 ], [ @CLOCK_MONOTONIC, %3 ]
%14 = load i32, i32* %13
store i32 %14, i32* @clock_boottime_or_monotonic.clock
br label %15
15: ; preds = %0, %12
%16 = phi i32 [ %14, %12 ], [ %1, %0 ]
ret i32 %16
}