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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ ENOSYS;
int /*<<< orphan*/ errno;
int hack_clock_gettime(int /*<<< orphan*/, struct timespec *);
int hack_gettimeofday(struct timeval *tv, struct timezone *tz) {
  if (tz != NULL) {
    errno = ENOSYS;
    return -1;
  }
  struct timespec ts;
  int ret = hack_clock_gettime(CLOCK_REALTIME, &ts);
  if (ret != 0) {
    return ret;
  }
  tv->tv_sec = ts.tv_sec;
  tv->tv_usec = ts.tv_nsec / 1000;
  return 0;
}


x86 assembly (clang, -O0):
<hack_gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1149 <hack_gettimeofday+0x39>
mov    0x2eb6(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <hack_gettimeofday+0x87>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <hack_clock_gettime@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1173 <hack_gettimeofday+0x63>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1197 <hack_gettimeofday+0x87>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%eax
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):
<hack_gettimeofday>:
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     1133 <hack_gettimeofday+0x23>
mov    0x2ec7(%rip),%rax
mov    (%rax),%eax
mov    0x2eae(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    116f <hack_gettimeofday+0x5f>
mov    %rdi,%rbx
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <hack_clock_gettime@plt>
test   %eax,%eax
jne    116f <hack_gettimeofday+0x5f>
mov    0xc(%rsp),%ecx
mov    %ecx,0x4(%rbx)
movslq 0x8(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
%struct.timespec = type { i32, i32 }
@ENOSYS = global i32 0
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
define i32 @hack_gettimeofday(%struct.timeval* %0, %struct.timezone* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca %struct.timezone*
%6 = alloca %struct.timespec
%7 = alloca i32
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timezone* %1, %struct.timezone** %5
%8 = load %struct.timezone*, %struct.timezone** %5
%9 = icmp ne %struct.timezone* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @ENOSYS
store i32 %11, i32* @errno
store i32 -1, i32* %3
br label %29
12: ; preds = %2
%13 = load i32, i32* @CLOCK_REALTIME
%14 = call i32 @hack_clock_gettime(i32 %13, %struct.timespec* %6)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i32, i32* %7
store i32 %18, i32* %3
br label %29
19: ; preds = %12
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load %struct.timeval*, %struct.timeval** %4
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
store i32 %21, i32* %23
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000
%27 = load %struct.timeval*, %struct.timeval** %4
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 0
store i32 %26, i32* %28
store i32 0, i32* %3
br label %29
29: ; preds = %19, %17, %10
%30 = load i32, i32* %3
ret i32 %30
}

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