#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 */
typedef double uint64_t;
struct timespec {
  double tv_sec;
  long tv_nsec;
};

/* Variables and functions */
double timedelta_ns(const struct timespec *end, const struct timespec *start) {
  uint64_t delta_s = end->tv_sec - start->tv_sec;
  long delta_ns = end->tv_nsec - start->tv_nsec;
  if (delta_ns < 0)
    delta_ns += 1000000000L;
  return delta_ns * 1e-9 + delta_s;
}


x86 assembly (clang, -O0):
<timedelta_ns>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
subsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    114e <timedelta_ns+0x4e>
mov    -0x20(%rbp),%rax
add    $0x3b9aca00,%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  0xe9f(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<timedelta_ns>:
movsd  (%rdi),%xmm1
mov    0x8(%rdi),%rax
sub    0x8(%rsi),%rax
lea    0x3b9aca00(%rax),%rcx
test   %rax,%rax
cmovns %rax,%rcx
subsd  (%rsi),%xmm1
cvtsi2sd %rcx,%xmm0
mulsd  0xed5(%rip),%xmm0
addsd  %xmm1,%xmm0
ret

LLVM IR (-O0):
%struct.timespec = type { double, i64 }
define double @timedelta_ns(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca %struct.timespec*
%4 = alloca %struct.timespec*
%5 = alloca double
%6 = alloca i64
store %struct.timespec* %0, %struct.timespec** %3
store %struct.timespec* %1, %struct.timespec** %4
%7 = load %struct.timespec*, %struct.timespec** %3
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%9 = load double, double* %8
%10 = load %struct.timespec*, %struct.timespec** %4
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%12 = load double, double* %11
%13 = fsub double %9, %12
store double %13, double* %5
%14 = load %struct.timespec*, %struct.timespec** %3
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = load %struct.timespec*, %struct.timespec** %4
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 1
%19 = load i64, i64* %18
%20 = sub nsw i64 %16, %19
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = load i64, i64* %6
%25 = add nsw i64 %24, 1000000000
store i64 %25, i64* %6
br label %26
26: ; preds = %23, %2
%27 = load i64, i64* %6
%28 = sitofp i64 %27 to double
%29 = load double, double* %5
%30 = call double @llvm.fmuladd.f64(double %28, double 1.000000e-09, double %29)
ret double %30
}

LLVM IR (-O2):
%struct.timespec = type { double, i64 }
define double @timedelta_ns(%struct.timespec* nocapture %0, %struct.timespec* nocapture %1) {
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%4 = load double, double* %3
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%6 = load double, double* %5
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%8 = load i64, i64* %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%10 = load i64, i64* %9
%11 = sub nsw i64 %8, %10
%12 = icmp slt i64 %11, 0
%13 = add nsw i64 %11, 1000000000
%14 = select i1 %12, i64 %13, i64 %11
%15 = fsub double %4, %6
%16 = sitofp i64 %14 to double
%17 = tail call double @llvm.fmuladd.f64(double %16, double 1.000000e-09, double %15)
ret double %17
}