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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
double stopwatch_ns() {
  int mul = 1;
  static struct timeval t, ot;
  gettimeofday(&t, NULL);
  double elapsed_us =
      1000000.0 * (t.tv_sec - ot.tv_sec) + (t.tv_usec - ot.tv_usec);
  mul = 1000;
  ot = t;
  return elapsed_us * mul;
}


x86 assembly (clang, -O0):
<stopwatch_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0x4(%rbp)
lea    0x2f0a(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movsd  0x2ef9(%rip),%xmm0
subsd  0x2f01(%rip),%xmm0
movsd  0x2ef1(%rip),%xmm1
subsd  0x2ef9(%rip),%xmm1
movsd  0xea9(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x3e8,-0x4(%rbp)
mov    0x2ebe(%rip),%rax
mov    %rax,0x2ec7(%rip)
mov    0x2eb8(%rip),%rax
mov    %rax,0x2ec1(%rip)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
mulsd  %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stopwatch_ns>:
push   %rax
lea    0x2f18(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movsd  0x2f09(%rip),%xmm0
subsd  0x2f11(%rip),%xmm0
movsd  0x2f01(%rip),%xmm1
subsd  0x2f09(%rip),%xmm1
mulsd  0xeb9(%rip),%xmm0
addsd  %xmm1,%xmm0
movups 0x2ede(%rip),%xmm1
movups %xmm1,0x2ee7(%rip)
mulsd  0xea7(%rip),%xmm0
pop    %rax
ret

LLVM IR (-O0):
%struct.timeval = type { double, double }
@stopwatch_ns.t = internal global %struct.timeval zeroinitializer
@stopwatch_ns.ot = internal global %struct.timeval zeroinitializer
define double @stopwatch_ns() {
%1 = alloca i32
%2 = alloca double
store i32 1, i32* %1
%3 = call i32 @gettimeofday(%struct.timeval* @stopwatch_ns.t, i32* null)
%4 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.t, i32 0, i32 0)
%5 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.ot, i32 0, i32 0)
%6 = fsub double %4, %5
%7 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.t, i32 0, i32 1)
%8 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.ot, i32 0, i32 1)
%9 = fsub double %7, %8
%10 = call double @llvm.fmuladd.f64(double 1.000000e+06, double %6, double %9)
store double %10, double* %2
store i32 1000, i32* %1
%11 = load double, double* %2
%12 = load i32, i32* %1
%13 = sitofp i32 %12 to double
%14 = fmul double %11, %13
ret double %14
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@stopwatch_ns.t = internal global %struct.timeval zeroinitializer
@stopwatch_ns.ot = internal unnamed_addr global %struct.timeval zeroinitializer
define double @stopwatch_ns() {
%1 = tail call i32 @gettimeofday(%struct.timeval* @stopwatch_ns.t, i32* null)
%2 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.t, i64 0, i32 0)
%3 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.ot, i64 0, i32 0)
%4 = fsub double %2, %3
%5 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.t, i64 0, i32 1)
%6 = load double, double* getelementptr inbounds (%struct.timeval, %struct.timeval* @stopwatch_ns.ot, i64 0, i32 1)
%7 = fsub double %5, %6
%8 = tail call double @llvm.fmuladd.f64(double %4, double 1.000000e+06, double %7)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 dereferenceable(16) bitcast (%struct.timeval* @stopwatch_ns.ot to i8*), i8* align 8 dereferenceable(16) bitcast (%struct.timeval* @stopwatch_ns.t to i8*), i64 16, i1 false)
%9 = fmul double %8, 1.000000e+03
ret double %9
}