#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_REALTIME;
int EINVAL;
int ENOSYS;
int __syscall(int, scalar_t__, struct timespec *, ...);
int errno;
int sc_clock_gettime(clockid_t clk, struct timespec *ts) {
  int r = __syscall(263, clk, ts);
  if (!r)
    return r;
  if (r == -ENOSYS) {
    if (clk == CLOCK_REALTIME) {
      __syscall(78, clk, ts, 0);
      ts->tv_nsec = (int)ts->tv_nsec * 1000;
      return 0;
    }
    r = -EINVAL;
  }
  errno = -r;
  return -1;
}


x86 assembly (clang, -O0):
<sc_clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x107,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    114c <sc_clock_gettime+0x3c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cb <sc_clock_gettime+0xbb>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e90(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11b6 <sc_clock_gettime+0xa6>
mov    -0x10(%rbp),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <sc_clock_gettime+0x98>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x4e,%edi
xor    %ecx,%ecx
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x18(%rbp),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11cb <sc_clock_gettime+0xbb>
xor    %eax,%eax
mov    0x2e1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
xor    %ecx,%ecx
sub    -0x1c(%rbp),%ecx
mov    0x2e16(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_clock_gettime>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %ebp,%ebp
mov    $0x107,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <__syscall@plt>
test   %eax,%eax
je     1188 <sc_clock_gettime+0x78>
mov    0x2eaf(%rip),%rcx
mov    (%rcx),%ecx
add    %eax,%ecx
jne    1178 <sc_clock_gettime+0x68>
mov    0x2e82(%rip),%rax
cmp    %rbx,(%rax)
jne    116d <sc_clock_gettime+0x5d>
xor    %ebp,%ebp
mov    $0x4e,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1030 <__syscall@plt>
imul   $0x3e8,(%r14),%eax
mov    %eax,(%r14)
jmp    1188 <sc_clock_gettime+0x78>
mov    0x2e5c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
neg    %eax
mov    0x2e57(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@ENOSYS = global i32 0
@CLOCK_REALTIME = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sc_clock_gettime(i64 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.timespec*
%6 = alloca i32
store i64 %0, i64* %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load i64, i64* %4
%8 = load %struct.timespec*, %struct.timespec** %5
%9 = call i32 (i32, i64, %struct.timespec*, ...) @__syscall(i32 263, i64 %7, %struct.timespec* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = load i32, i32* %6
store i32 %13, i32* %3
br label %39
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i32, i32* @ENOSYS
%17 = sub nsw i32 0, %16
%18 = icmp eq i32 %15, %17
br i1 %18, label %19, label %36
19: ; preds = %14
%20 = load i64, i64* %4
%21 = load i64, i64* @CLOCK_REALTIME
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i64, i64* %4
%25 = load %struct.timespec*, %struct.timespec** %5
%26 = call i32 (i32, i64, %struct.timespec*, ...) @__syscall(i32 78, i64 %24, %struct.timespec* %25, i32 0)
%27 = load %struct.timespec*, %struct.timespec** %5
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = mul nsw i32 %29, 1000
%31 = load %struct.timespec*, %struct.timespec** %5
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %31, i32 0, i32 0
store i32 %30, i32* %32
store i32 0, i32* %3
br label %39
33: ; preds = %19
%34 = load i32, i32* @EINVAL
%35 = sub nsw i32 0, %34
store i32 %35, i32* %6
br label %36
36: ; preds = %33, %14
%37 = load i32, i32* %6
%38 = sub nsw i32 0, %37
store i32 %38, i32* @errno
store i32 -1, i32* %3
br label %39
39: ; preds = %36, %23, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@ENOSYS = global i32 0
@CLOCK_REALTIME = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sc_clock_gettime(i64 %0, %struct.timespec* %1) {
%3 = tail call i32 (i32, i64, %struct.timespec*, ...) @__syscall(i32 263, i64 %0, %struct.timespec* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = load i32, i32* @ENOSYS
%7 = sub nsw i32 0, %6
%8 = icmp eq i32 %3, %7
br i1 %8, label %9, label %20
9: ; preds = %5
%10 = load i64, i64* @CLOCK_REALTIME
%11 = icmp eq i64 %10, %0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = tail call i32 (i32, i64, %struct.timespec*, ...) @__syscall(i32 78, i64 %0, %struct.timespec* %1, i32 0)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = mul nsw i32 %15, 1000
store i32 %16, i32* %14
br label %23
17: ; preds = %9
%18 = load i32, i32* @EINVAL
%19 = sub nsw i32 0, %18
br label %20
20: ; preds = %17, %5
%21 = phi i32 [ %19, %17 ], [ %3, %5 ]
%22 = sub nsw i32 0, %21
store i32 %22, i32* @errno
br label %23
23: ; preds = %2, %20, %12
%24 = phi i32 [ 0, %12 ], [ -1, %20 ], [ 0, %2 ]
ret i32 %24
}