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


x86 assembly (clang, -O0):
<get_current_timespec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     1144 <get_current_timespec+0x34>
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1162 <get_current_timespec+0x52>
imul   $0x3e8,-0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_timespec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
je     1133 <get_current_timespec+0x23>
mov    0x2eb1(%rip),%rax
mov    (%rax),%ebx
jmp    1145 <get_current_timespec+0x35>
imul   $0x3e8,(%rsp),%eax
mov    %eax,(%r14)
mov    0x4(%rsp),%eax
mov    %eax,0x4(%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 }
@errno = global i32 0
define i32 @get_current_timespec(%struct.timespec* %0) {
%2 = alloca i32
%3 = alloca %struct.timespec*
%4 = alloca %struct.timeval
store %struct.timespec* %0, %struct.timespec** %3
%5 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @errno
store i32 %8, i32* %2
br label %19
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = mul nsw i32 %11, 1000
%13 = load %struct.timespec*, %struct.timespec** %3
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load %struct.timespec*, %struct.timespec** %3
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 1
store i32 %16, i32* %18
store i32 0, i32* %2
br label %19
19: ; preds = %9, %7
%20 = load i32, i32* %2
ret i32 %20
}

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