#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, struct timespec *, 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, 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
call   1030 <__syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    114a <sc_clock_gettime+0x3a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <sc_clock_gettime+0xb5>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e92(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11b0 <sc_clock_gettime+0xa0>
mov    -0x10(%rbp),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <sc_clock_gettime+0x92>
mov    -0x18(%rbp),%rsi
mov    $0x4e,%edi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <__syscall@plt>
mov    -0x18(%rbp),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11c5 <sc_clock_gettime+0xb5>
xor    %eax,%eax
mov    0x2e25(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
xor    %ecx,%ecx
sub    -0x1c(%rbp),%ecx
mov    0x2e1c(%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   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x107,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <__syscall@plt>
test   %eax,%eax
je     1164 <sc_clock_gettime+0x54>
mov    0x2eb3(%rip),%rcx
mov    (%rcx),%ecx
add    %eax,%ecx
jne    1173 <sc_clock_gettime+0x63>
mov    0x2e86(%rip),%rax
cmp    %rbx,(%rax)
jne    1168 <sc_clock_gettime+0x58>
xor    %ebx,%ebx
mov    $0x4e,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <__syscall@plt>
imul   $0x3e8,(%r14),%eax
mov    %eax,(%r14)
jmp    1183 <sc_clock_gettime+0x73>
xor    %ebx,%ebx
jmp    1183 <sc_clock_gettime+0x73>
mov    0x2e61(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
neg    %eax
mov    0x2e5c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 = inttoptr i64 %7 to %struct.timespec*
%9 = load %struct.timespec*, %struct.timespec** %5
%10 = call i32 @__syscall(i32 263, %struct.timespec* %8, %struct.timespec* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %39
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @ENOSYS
%18 = sub nsw i32 0, %17
%19 = icmp eq i32 %16, %18
br i1 %19, label %20, label %36
20: ; preds = %15
%21 = load i64, i64* %4
%22 = load i64, i64* @CLOCK_REALTIME
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load %struct.timespec*, %struct.timespec** %5
%26 = call i32 @__syscall(i32 78, %struct.timespec* %25, %struct.timespec* null)
%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 = %20
%34 = load i32, i32* @EINVAL
%35 = sub nsw i32 0, %34
store i32 %35, i32* %6
br label %36
36: ; preds = %33, %15
%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, %24, %13
%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 = inttoptr i64 %0 to %struct.timespec*
%4 = tail call i32 @__syscall(i32 263, %struct.timespec* %3, %struct.timespec* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = load i32, i32* @ENOSYS
%8 = sub nsw i32 0, %7
%9 = icmp eq i32 %4, %8
br i1 %9, label %10, label %21
10: ; preds = %6
%11 = load i64, i64* @CLOCK_REALTIME
%12 = icmp eq i64 %11, %0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @__syscall(i32 78, %struct.timespec* %1, %struct.timespec* null)
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%16 = load i32, i32* %15
%17 = mul nsw i32 %16, 1000
store i32 %17, i32* %15
br label %24
18: ; preds = %10
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
br label %21
21: ; preds = %18, %6
%22 = phi i32 [ %20, %18 ], [ %4, %6 ]
%23 = sub nsw i32 0, %22
store i32 %23, i32* @errno
br label %24
24: ; preds = %2, %21, %13
%25 = phi i32 [ 0, %13 ], [ -1, %21 ], [ 0, %2 ]
ret i32 %25
}