#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;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int _sndio_clock_gettime(int timer, struct timespec *ts) {
  struct timeval tv;
  if (timer != 0) {
    errno = EINVAL;
    return -1;
  }
  if (gettimeofday(&tv, NULL) < 0)
    return -1;
  ts->tv_sec = tv.tv_sec;
  ts->tv_nsec = tv.tv_usec * 1000;
  return 0;
}


x86 assembly (clang, -O0):
<_sndio_clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1147 <_sndio_clock_gettime+0x37>
mov    0x2ea8(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea7(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <_sndio_clock_gettime+0x78>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    116a <_sndio_clock_gettime+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <_sndio_clock_gettime+0x78>
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)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sndio_clock_gettime>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     1131 <_sndio_clock_gettime+0x21>
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    0x2eb8(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1157 <_sndio_clock_gettime+0x47>
mov    %rsi,%r14
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
js     112a <_sndio_clock_gettime+0x1a>
mov    0x4(%rsp),%eax
mov    %eax,0x4(%r14)
imul   $0x3e8,(%rsp),%eax
mov    %eax,(%r14)
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 @_sndio_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
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %25
11: ; preds = %2
%12 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %3
br label %25
15: ; preds = %11
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load %struct.timespec*, %struct.timespec** %5
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
store i32 %17, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000
%23 = load %struct.timespec*, %struct.timespec** %5
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %23, i32 0, i32 0
store i32 %22, i32* %24
store i32 0, i32* %3
br label %25
25: ; preds = %15, %14, %9
%26 = load i32, i32* %3
ret i32 %26
}

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