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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int clock_gettime(int id, struct timespec *tp) {
  struct timeval tv;
  switch (id) {
  case 0:
  case 1:
    if (gettimeofday(&tv, NULL) < 0)
      return -1;
    tp->tv_sec = tv.tv_sec;
    tp->tv_nsec = tv.tv_usec * 1000;
    return 0;
  default:
    errno = EINVAL;
    return -1;
  }
}


x86 assembly (clang, -O0):
<clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
ja     1175 <clock_gettime+0x65>
jmp    1130 <clock_gettime+0x20>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%eax
jge    1152 <clock_gettime+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    118e <clock_gettime+0x7e>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
imul   $0x3e8,-0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    118e <clock_gettime+0x7e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_gettime>:
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
ja     1140 <clock_gettime+0x30>
mov    %rsi,%r14
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
js     1152 <clock_gettime+0x42>
mov    0x4(%rsp),%eax
mov    %eax,0x4(%r14)
imul   $0x3e8,(%rsp),%eax
mov    %eax,(%r14)
jmp    1157 <clock_gettime+0x47>
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    0x2e90(%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, i32 }
%struct.timeval = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @clock_gettime(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec*
%6 = alloca %struct.timeval
store i32 %0, i32* %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load i32, i32* %4
switch i32 %7, label %22 [
i32 0, label %8
i32 1, label %8
]
8: ; preds = %2, %2
%9 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %8
store i32 -1, i32* %3
br label %24
12: ; preds = %8
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load %struct.timespec*, %struct.timespec** %5
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %15, i32 0, i32 1
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = mul nsw i32 %18, 1000
%20 = load %struct.timespec*, %struct.timespec** %5
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %20, i32 0, i32 0
store i32 %19, i32* %21
store i32 0, i32* %3
br label %24
22: ; preds = %2
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
store i32 -1, i32* %3
br label %24
24: ; preds = %22, %12, %11
%25 = load i32, i32* %3
ret i32 %25
}

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