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

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int gettime_internal(struct timespec *time) {
  struct timeval tv;
  int res = gettimeofday(&tv, NULL);
  if (!res) {
    time->tv_sec = tv.tv_sec;
    time->tv_nsec = tv.tv_usec * 1000UL;
  }
  return res;
}


x86 assembly (clang, -O0):
<gettime_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    114f <gettime_internal+0x3f>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
imul   $0x3e8,-0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettime_internal>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
jne    1138 <gettime_internal+0x28>
mov    0x8(%rsp),%ecx
mov    %ecx,0x8(%rbx)
imul   $0x3e8,(%rsp),%rcx
mov    %rcx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i64, i32 }
define i32 @gettime_internal(%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 eq i32 %4, 0
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 = mul 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
}