#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 time_t;
struct timespec {
  double tv_sec;
  long tv_nsec;
};

/* Variables and functions */
double timespec_diff(struct timespec start, struct timespec end) {
  long weight = 1000000000;
  time_t s = (end.tv_sec - start.tv_sec);
  long ns = (end.tv_nsec - start.tv_nsec) % weight;
  return s + ((double)ns) / weight;
}


x86 assembly (clang, -O0):
<timespec_diff>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x8(%rbp)
movsd  %xmm1,-0x20(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x3b9aca00,-0x28(%rbp)
3b
movsd  -0x20(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x8(%rbp),%rax
cqto
idivq  -0x28(%rbp)
mov    %rdx,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
cvtsi2sdq -0x38(%rbp),%xmm1
cvtsi2sdq -0x28(%rbp),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec_diff>:
sub    %rdi,%rsi
movabs $0x112e0be826d694b3,%rcx
0b 2e 11
mov    %rsi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rsi
cvtsi2sd %rsi,%xmm2
subsd  %xmm0,%xmm1
divsd  0xec4(%rip),%xmm2
addsd  %xmm1,%xmm2
movapd %xmm2,%xmm0
ret

LLVM IR (-O0):
%struct.timespec = type { double, i64 }
define double @timespec_diff(double %0, i64 %1, double %2, i64 %3) {
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i64
%8 = alloca double
%9 = alloca i64
%10 = bitcast %struct.timespec* %5 to { double, i64 }*
%11 = getelementptr inbounds { double, i64 }, { double, i64 }* %10, i32 0, i32 0
store double %0, double* %11
%12 = getelementptr inbounds { double, i64 }, { double, i64 }* %10, i32 0, i32 1
store i64 %1, i64* %12
%13 = bitcast %struct.timespec* %6 to { double, i64 }*
%14 = getelementptr inbounds { double, i64 }, { double, i64 }* %13, i32 0, i32 0
store double %2, double* %14
%15 = getelementptr inbounds { double, i64 }, { double, i64 }* %13, i32 0, i32 1
store i64 %3, i64* %15
store i64 1000000000, i64* %7
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%17 = load double, double* %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%19 = load double, double* %18
%20 = fsub double %17, %19
store double %20, double* %8
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%24 = load i64, i64* %23
%25 = sub nsw i64 %22, %24
%26 = load i64, i64* %7
%27 = srem i64 %25, %26
store i64 %27, i64* %9
%28 = load double, double* %8
%29 = load i64, i64* %9
%30 = sitofp i64 %29 to double
%31 = load i64, i64* %7
%32 = sitofp i64 %31 to double
%33 = fdiv double %30, %32
%34 = fadd double %28, %33
ret double %34
}

LLVM IR (-O2):
define double @timespec_diff(double %0, i64 %1, double %2, i64 %3) {
%5 = fsub double %2, %0
%6 = sub nsw i64 %3, %1
%7 = srem i64 %6, 1000000000
%8 = sitofp i64 %7 to double
%9 = fdiv double %8, 1.000000e+09
%10 = fadd double %5, %9
ret double %10
}