#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 {
  long long tv_usec;
  int /*<<< orphan*/ tv_sec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct timespec {
  long long tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int gettimespecofday(struct timespec *ts) {
  struct timeval tv = {0, 0};
  int rc;
  rc = gettimeofday(&tv, NULL);
  if (rc >= 0) {
    ts->tv_sec = tv.tv_sec;
    ts->tv_nsec = tv.tv_usec / 1000LL;
  }
  return rc;
}


x86 assembly (clang, -O0):
<gettimespecofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1178 <gettimespecofday+0x58>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettimespecofday>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %eax,%ecx
test   %eax,%eax
js     115e <gettimespecofday+0x4e>
mov    0x8(%rsp),%eax
mov    %eax,0x8(%rbx)
movabs $0x20c49ba5e353f7cf,%rax
9b c4 20
imulq  (%rsp)
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,(%rbx)
mov    %ecx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i64, i32, i32, i32 }
define i32 @gettimespecofday(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca %struct.timeval
%4 = alloca i32
store %struct.timespec* %0, %struct.timespec** %2
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load %struct.timespec*, %struct.timespec** %2
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%15 = load i64, i64* %14
%16 = sdiv i64 %15, 1000
%17 = load %struct.timespec*, %struct.timespec** %2
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 0
store i64 %16, i64* %18
br label %19
19: ; preds = %9, %1
%20 = load i32, i32* %4
ret i32 %20
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i64, i32, i32, i32 }
define i32 @gettimespecofday(%struct.timespec* nocapture writeonly %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = sdiv i64 %11, 1000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %12, i64* %13
br label %14
14: ; preds = %6, %1
ret i32 %4
}