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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, double);
void do_time_it() {
  static int flag_in = 0;
  static struct timespec ts[2];
  double delta_ts;
  clock_gettime(CLOCK_MONOTONIC, &ts[flag_in]);
  if (flag_in) {
    delta_ts = (((double)((ts[1].tv_sec - ts[0].tv_sec) * 1000000000.0 +
                          (ts[1].tv_nsec - ts[0].tv_nsec) + 100.0)) /
                1000000.0);
    printf("delta_ts = %lf ms\n", delta_ts);
  }
  flag_in = !flag_in;
}


x86 assembly (clang, -O0):
<do_time_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
movslq 0x2f08(%rip),%rax
lea    0x2f11(%rip),%rsi
shl    $0x4,%rax
add    %rax,%rsi
call   1030 <clock_gettime@plt>
cmpl   $0x0,0x2eee(%rip)
je     11b9 <do_time_it+0x99>
movsd  0x2f00(%rip),%xmm0
subsd  0x2ee8(%rip),%xmm0
mov    0x2ef9(%rip),%rax
sub    0x2ee2(%rip),%rax
cvtsi2sd %rax,%xmm1
movsd  0xe8d(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe75(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe61(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,0x2e80(%rip)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,0x2e70(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_time_it>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    (%rax),%edi
mov    0x2f10(%rip),%eax
shl    $0x4,%rax
lea    0x2f15(%rip),%rsi
add    %rax,%rsi
call   1030 <clock_gettime@plt>
cmpl   $0x0,0x2ef6(%rip)
je     11a5 <do_time_it+0x85>
movsd  0x2f0c(%rip),%xmm0
subsd  0x2ef4(%rip),%xmm0
mov    0x2f05(%rip),%rax
sub    0x2eee(%rip),%rax
cvtsi2sd %rax,%xmm1
mulsd  0xe89(%rip),%xmm0
addsd  %xmm1,%xmm0
addsd  0xe85(%rip),%xmm0
divsd  0xe85(%rip),%xmm0
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
cmpl   $0x0,0x2ea0(%rip)
sete   %al
jmp    11aa <do_time_it+0x8a>
mov    $0x1,%eax
mov    %eax,0x2e90(%rip)
pop    %rax
ret

LLVM IR (-O0):
%struct.timespec = type { double, i64 }
@do_time_it.flag_in = internal global i32 0
@do_time_it.ts = internal global [2 x %struct.timespec] zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@.str = [19 x i8] c"delta_ts = %lf ms\0A\00"
define void @do_time_it() {
%1 = alloca double
%2 = load i32, i32* @CLOCK_MONOTONIC
%3 = load i32, i32* @do_time_it.flag_in
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 %4
%6 = call i32 @clock_gettime(i32 %2, %struct.timespec* %5)
%7 = load i32, i32* @do_time_it.flag_in
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %0
%10 = load double, double* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 1, i32 0)
%11 = load double, double* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 0, i32 0)
%12 = fsub double %10, %11
%13 = load i64, i64* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 1, i32 1)
%14 = load i64, i64* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 0, i32 1)
%15 = sub nsw i64 %13, %14
%16 = sitofp i64 %15 to double
%17 = call double @llvm.fmuladd.f64(double %12, double 1.000000e+09, double %16)
%18 = fadd double %17, 1.000000e+02
%19 = fdiv double %18, 1.000000e+06
store double %19, double* %1
%20 = load double, double* %1
%21 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), double %20)
br label %22
22: ; preds = %9, %0
%23 = load i32, i32* @do_time_it.flag_in
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
%26 = zext i1 %25 to i32
store i32 %26, i32* @do_time_it.flag_in
ret void
}

LLVM IR (-O2):
%struct.timespec = type { double, i64 }
@do_time_it.flag_in = internal unnamed_addr global i32 0
@do_time_it.ts = internal global [2 x %struct.timespec] zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@.str = [19 x i8] c"delta_ts = %lf ms\0A\00"
define void @do_time_it() {
%1 = load i32, i32* @CLOCK_MONOTONIC
%2 = load i32, i32* @do_time_it.flag_in
%3 = zext i32 %2 to i64
%4 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 %3
%5 = tail call i32 @clock_gettime(i32 %1, %struct.timespec* %4)
%6 = load i32, i32* @do_time_it.flag_in
%7 = icmp eq i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %0
%9 = load double, double* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 1, i32 0)
%10 = load double, double* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 0, i32 0)
%11 = fsub double %9, %10
%12 = load i64, i64* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 1, i32 1)
%13 = load i64, i64* getelementptr inbounds ([2 x %struct.timespec], [2 x %struct.timespec]* @do_time_it.ts, i64 0, i64 0, i32 1)
%14 = sub nsw i64 %12, %13
%15 = sitofp i64 %14 to double
%16 = tail call double @llvm.fmuladd.f64(double %11, double 1.000000e+09, double %15)
%17 = fadd double %16, 1.000000e+02
%18 = fdiv double %17, 1.000000e+06
%19 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), double %18)
%20 = load i32, i32* @do_time_it.flag_in
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %8, %0
%24 = phi i32 [ %22, %8 ], [ 1, %0 ]
store i32 %24, i32* @do_time_it.flag_in
ret void
}