#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
double timeval_to_double(void) {
  struct timeval tv;
redo:
  errno = 0;
  if (gettimeofday(&tv, NULL) < 0) {
    if (errno == EINTR)
      goto redo;
    (void)fprintf(stderr, "gettimeofday error: errno=%d (%s).\n", errno,
                  strerror(errno));
    return -1.0;
  }
  return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
}


x86 assembly (clang, -O0):
<timeval_to_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e99(%rip),%rax
movq   $0x0,(%rax)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    11cd <timeval_to_double+0x9d>
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <timeval_to_double+0x4c>
jmp    1138 <timeval_to_double+0x8>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
movsd  0xe45(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ee <timeval_to_double+0xbe>
cvtsi2sdq -0x10(%rbp),%xmm0
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  0xe1f(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_to_double>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e97(%rip),%rbp
lea    0x8(%rsp),%r14
mov    0x2e83(%rip),%r15
nopl   (%rax)
movq   $0x0,0x0(%rbp)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jns    119f <timeval_to_double+0x6f>
mov    0x0(%rbp),%rbx
cmp    (%r15),%rbx
je     1150 <timeval_to_double+0x20>
mov    0x2e69(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1050 <strerror@plt>
lea    0xe88(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
movsd  0xe6b(%rip),%xmm0
jmp    11b9 <timeval_to_double+0x89>
cvtsi2sdq 0x10(%rsp),%xmm1
cvtsi2sdq 0x8(%rsp),%xmm0
divsd  0xe4b(%rip),%xmm0
addsd  %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"gettimeofday error: errno=%d (%s).\0A\00"
define double @timeval_to_double() {
%1 = alloca double
%2 = alloca %struct.timeval
br label %3
3: ; preds = %10, %0
store i64 0, i64* @errno
%4 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %6
br label %3
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = load i64, i64* @errno
%14 = load i64, i64* @errno
%15 = call i8* @strerror(i64 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %13, i8* %15)
store double -1.000000e+00, double* %1
br label %26
17: ; preds = %3
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%19 = load i64, i64* %18
%20 = sitofp i64 %19 to double
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%22 = load i64, i64* %21
%23 = sitofp i64 %22 to double
%24 = fdiv double %23, 1.000000e+06
%25 = fadd double %20, %24
store double %25, double* %1
br label %26
26: ; preds = %17, %11
%27 = load double, double* %1
ret double %27
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"gettimeofday error: errno=%d (%s).\0A\00"
define double @timeval_to_double() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
br label %3
3: ; preds = %6, %0
store i64 0, i64* @errno
%4 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %3, label %10
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i8* @strerror(i64 %7)
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %7, i8* %12)
br label %23
14: ; preds = %3
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%16 = load i64, i64* %15
%17 = sitofp i64 %16 to double
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%19 = load i64, i64* %18
%20 = sitofp i64 %19 to double
%21 = fdiv double %20, 1.000000e+06
%22 = fadd double %21, %17
br label %23
23: ; preds = %14, %10
%24 = phi double [ -1.000000e+00, %10 ], [ %22, %14 ]
ret double %24
}