#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 timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef scalar_t__ clockid_t;

/* Variables and functions */
scalar_t__ CLOCK_REALTIME;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int clock_gettime(clockid_t clock, struct timespec *ts) {
  struct timeval tv;
  if (clock != CLOCK_REALTIME) {
    errno = EINVAL;
    return -1;
  }
  if (gettimeofday(&tv, NULL))
    return -1;
  ts->tv_sec = tv.tv_sec;
  ts->tv_nsec = tv.tv_usec * 1000;
  return 0;
}


x86 assembly (clang, -O0):
<clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ea5(%rip),%rcx
cmp    (%rcx),%rax
je     1152 <clock_gettime+0x42>
mov    0x2e9d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <clock_gettime+0x83>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     1175 <clock_gettime+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <clock_gettime+0x83>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
imul   $0x3e8,-0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_gettime>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eb4(%rip),%rax
cmp    %rdi,(%rax)
jne    1152 <clock_gettime+0x42>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1169 <clock_gettime+0x59>
mov    0xc(%rsp),%eax
mov    %eax,0x4(%rbx)
imul   $0x3e8,0x8(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1169 <clock_gettime+0x59>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e7e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@CLOCK_REALTIME = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @clock_gettime(i64 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.timespec*
%6 = alloca %struct.timeval
store i64 %0, i64* %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load i64, i64* %4
%8 = load i64, i64* @CLOCK_REALTIME
%9 = icmp ne i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
store i32 -1, i32* %3
br label %26
12: ; preds = %2
%13 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %26
16: ; preds = %12
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.timespec*, %struct.timespec** %5
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 1
store i32 %18, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = mul nsw i32 %22, 1000
%24 = load %struct.timespec*, %struct.timespec** %5
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %24, i32 0, i32 0
store i32 %23, i32* %25
store i32 0, i32* %3
br label %26
26: ; preds = %16, %15, %10
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@CLOCK_REALTIME = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @clock_gettime(i64 %0, %struct.timespec* nocapture writeonly %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = load i64, i64* @CLOCK_REALTIME
%6 = icmp eq i64 %5, %0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %20
9: ; preds = %2
%10 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 1000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %18, i32* %19
br label %20
20: ; preds = %9, %12, %7
%21 = phi i32 [ -1, %7 ], [ 0, %12 ], [ -1, %9 ]
ret i32 %21
}