#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;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
double timeval_to_double(void) {
  struct timeval tv;
redo:
  errno = 0;
  if (gettimeofday(&tv, NULL) < 0) {
    if (errno == EINTR)
      goto redo;
    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    $0x20,%rsp
mov    0x2ec1(%rip),%rax
movq   $0x0,(%rax)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    116e <timeval_to_double+0x5e>
mov    0x2e9c(%rip),%rax
mov    (%rax),%rax
mov    0x2e8a(%rip),%rcx
cmp    (%rcx),%rax
jne    115c <timeval_to_double+0x4c>
jmp    1118 <timeval_to_double+0x8>
movsd  0xea4(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    118f <timeval_to_double+0x7f>
cvtsi2sdq -0x10(%rbp),%xmm0
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  0xe7e(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_to_double>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ec0(%rip),%rbx
mov    %rsp,%r14
mov    0x2eae(%rip),%r15
nopw   0x0(%rax,%rax,1)
movq   $0x0,(%rbx)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jns    1158 <timeval_to_double+0x48>
mov    (%rbx),%rax
cmp    (%r15),%rax
je     1130 <timeval_to_double+0x20>
movsd  0xeb2(%rip),%xmm0
jmp    1171 <timeval_to_double+0x61>
cvtsi2sdq 0x8(%rsp),%xmm1
cvtsi2sdq (%rsp),%xmm0
divsd  0xe93(%rip),%xmm0
addsd  %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
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 %12
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
store double -1.000000e+00, double* %1
br label %21
12: ; preds = %3
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%14 = load i64, i64* %13
%15 = sitofp i64 %14 to double
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%17 = load i64, i64* %16
%18 = sitofp i64 %17 to double
%19 = fdiv double %18, 1.000000e+06
%20 = fadd double %15, %19
store double %20, double* %1
br label %21
21: ; preds = %12, %11
%22 = load double, double* %1
ret double %22
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
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 %10
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 %19
10: ; preds = %3
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%12 = load i64, i64* %11
%13 = sitofp i64 %12 to double
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%15 = load i64, i64* %14
%16 = sitofp i64 %15 to double
%17 = fdiv double %16, 1.000000e+06
%18 = fadd double %17, %13
br label %19
19: ; preds = %6, %10
%20 = phi double [ %18, %10 ], [ -1.000000e+00, %6 ]
ret double %20
}