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

/* Variables and functions */
#define CLOCK_MONOTONIC 129
#define CLOCK_REALTIME 128
int /*<<< orphan*/ errx(int, char *, int);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int clock_gettime(clockid_t cid, struct timespec *ts) {
  struct timeval tv;
  int rc;
  switch (cid) {
  case CLOCK_MONOTONIC:
    rc = gettimeofday(&tv, NULL);
    break;
  case CLOCK_REALTIME:
    rc = gettimeofday(&tv, NULL);
    break;
  default:
    errx(1, "invalid clock ID: %d", cid);
  }
  if (rc == 0) {
    ts->tv_sec = tv.tv_sec;
    ts->tv_nsec = tv.tv_usec * 1000;
  }
  return (-rc);
}


x86 assembly (clang, -O0):
<clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     116d <clock_gettime+0x4d>
jmp    1145 <clock_gettime+0x25>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1182 <clock_gettime+0x62>
jmp    1158 <clock_gettime+0x38>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
jmp    1196 <clock_gettime+0x76>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
jmp    1196 <clock_gettime+0x76>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xe6f(%rip),%rsi
call   1030 <errx@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11b7 <clock_gettime+0x97>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
imul   $0x3e8,-0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_gettime>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1152 <clock_gettime+0x32>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %eax,%eax
jne    116e <clock_gettime+0x4e>
mov    0xc(%rsp),%ecx
imul   $0x3e8,0x8(%rsp),%eax
jmp    1167 <clock_gettime+0x47>
mov    %edi,%edx
lea    0xea5(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
xor    %eax,%eax
mov    %ecx,0x4(%rbx)
mov    %eax,(%rbx)
xor    %eax,%eax
neg    %eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"invalid clock ID: %d\00"
define i32 @clock_gettime(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca %struct.timeval
%6 = alloca i32
store i32 %0, i32* %3
store %struct.timespec* %1, %struct.timespec** %4
%7 = load i32, i32* %3
switch i32 %7, label %12 [
i32 129, label %8
i32 128, label %10
]
8: ; preds = %2
%9 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %9, i32* %6
br label %15
10: ; preds = %2
%11 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %11, i32* %6
br label %15
12: ; preds = %2
%13 = load i32, i32* %3
%14 = call i32 @errx(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %10, %8
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.timespec*, %struct.timespec** %4
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%24 = load i32, i32* %23
%25 = mul nsw i32 %24, 1000
%26 = load %struct.timespec*, %struct.timespec** %4
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 0
store i32 %25, i32* %27
br label %28
28: ; preds = %18, %15
%29 = load i32, i32* %6
%30 = sub nsw i32 0, %29
ret i32 %30
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"invalid clock ID: %d\00"
define i32 @clock_gettime(i32 %0, %struct.timespec* nocapture writeonly %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 128
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %18
9: ; preds = %2
%10 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = mul i32 %16, 1000
br label %18
18: ; preds = %12, %7
%19 = phi i32 [ %17, %12 ], [ 0, %7 ]
%20 = phi i32 [ %14, %12 ], [ undef, %7 ]
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %19, i32* %22
br label %23
23: ; preds = %18, %9
%24 = phi i32 [ 0, %18 ], [ %10, %9 ]
%25 = sub nsw i32 0, %24
ret i32 %25
}