#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 {
  scalar_t__ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int gettimeofday(struct timeval *tv, void *tz) {
  struct timespec ts;
  if (!tv)
    return 0;
  clock_gettime(CLOCK_REALTIME, &ts);
  tv->tv_sec = ts.tv_sec;
  tv->tv_usec = (int)ts.tv_nsec / 1000;
  return 0;
}


x86 assembly (clang, -O0):
<gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1137 <gettimeofday+0x27>
movl   $0x0,-0x4(%rbp)
jmp    116e <gettimeofday+0x5e>
mov    0x2ea2(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettimeofday>:
test   %rdi,%rdi
je     1154 <gettimeofday+0x44>
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ebc(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x4(%rbx)
movslq (%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,(%rbx)
add    $0x10,%rsp
pop    %rbx
xor    %eax,%eax
ret

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

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