#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 dummy;
};
struct timespec {
  int dummy;
};
typedef int clockid_t;

/* Variables and functions */
#define CLOCK_REALTIME 128
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, struct timespec *);
int /*<<< orphan*/ errno;
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int clock_gettime(clockid_t clock, struct timespec *ts) {
  struct timeval tv;
  int retval = -1;
  switch (clock) {
  case CLOCK_REALTIME:
    retval = gettimeofday(&tv, NULL);
    if (retval == 0) {
      TIMEVAL_TO_TIMESPEC(&tv, ts);
    }
    break;
  default:
    errno = EINVAL;
    break;
  }
  return retval;
}


x86 assembly (clang, -O0):
<clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x80,%eax
jne    1175 <clock_gettime+0x55>
jmp    1149 <clock_gettime+0x29>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1170 <clock_gettime+0x50>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1040 <TIMEVAL_TO_TIMESPEC@plt>
jmp    1187 <clock_gettime+0x67>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_gettime>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x80,%edi
jne    1151 <clock_gettime+0x31>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
jne    1168 <clock_gettime+0x48>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1040 <TIMEVAL_TO_TIMESPEC@plt>
xor    %eax,%eax
jmp    1168 <clock_gettime+0x48>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    0x2e7f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.timeval = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
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
store i32 -1, i32* %6
%7 = load i32, i32* %3
switch i32 %7, label %16 [
i32 128, label %8
]
8: ; preds = %2
%9 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load %struct.timespec*, %struct.timespec** %4
%14 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %5, %struct.timespec* %13)
br label %15
15: ; preds = %12, %8
br label %18
16: ; preds = %2
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
br label %18
18: ; preds = %16, %15
%19 = load i32, i32* %6
ret i32 %19
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.timeval = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @clock_gettime(i32 %0, %struct.timespec* %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = icmp eq i32 %0, 128
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %1)
br label %13
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %13
13: ; preds = %6, %9, %11
%14 = phi i32 [ 0, %9 ], [ %7, %6 ], [ -1, %11 ]
ret i32 %14
}