#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 /*<<< orphan*/ CLOCK_MONOTONIC;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int _get_time_of_day(struct timeval *timeval, void *arg) {
  struct timespec spec = {};
  int ret = clock_gettime(CLOCK_MONOTONIC, &spec);
  if (ret == -1) {
    return ret;
  }
  timeval->tv_sec = spec.tv_sec;
  timeval->tv_usec = spec.tv_nsec / 1000;
  return ret;
}


x86 assembly (clang, -O0):
<_get_time_of_day>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116a <_get_time_of_day+0x4a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <_get_time_of_day+0x6d>
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)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_time_of_day>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2ec0(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     115b <_get_time_of_day+0x4b>
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.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define i32 @_get_time_of_day(%struct.timeval* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca i8*
%6 = alloca %struct.timespec
%7 = alloca i32
store %struct.timeval* %0, %struct.timeval** %4
store i8* %1, i8** %5
%8 = bitcast %struct.timespec* %6 to i8*
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %6)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %7
store i32 %14, i32* %3
br label %26
15: ; preds = %2
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load %struct.timeval*, %struct.timeval** %4
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
store i32 %17, i32* %19
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sdiv i32 %21, 1000
%23 = load %struct.timeval*, %struct.timeval** %4
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %7
store i32 %25, i32* %3
br label %26
26: ; preds = %15, %13
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define i32 @_get_time_of_day(%struct.timeval* nocapture writeonly %0, i8* nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %17, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i32 %11, i32* %12
%13 = bitcast i64* %3 to i32*
%14 = load i32, i32* %13
%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, %9
ret i32 %7
}