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


x86 assembly (clang, -O0):
<gettime>:
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>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1141 <gettime+0x31>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    115f <gettime+0x4f>
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):
<gettime>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
js     113a <gettime+0x2a>
mov    0x4(%rsp),%eax
mov    %eax,0x4(%rbx)
imul   $0x3e8,(%rsp),%eax
mov    %eax,(%rbx)
jmp    113d <gettime+0x2d>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
define i32 @gettime(%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 slt i32 %4, 0
br i1 %5, label %14, label %6
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 i32, i32* %10
%12 = mul nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i32 %12, i32* %13
br label %14
14: ; preds = %1, %6
%15 = phi i32 [ 0, %6 ], [ %4, %1 ]
ret i32 %15
}