#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;
  double tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ timespecsub(struct timespec *, struct timespec *,
                               struct timespec *);
double app_timer_real(int stop) {
  static struct timespec start;
  struct timespec elapsed, now;
  clock_gettime(CLOCK_MONOTONIC, &now);
  if (stop) {
    timespecsub(&now, &start, &elapsed);
    return elapsed.tv_sec + elapsed.tv_nsec / 1000000000.0;
  }
  start = now;
  return 0.0;
}


x86 assembly (clang, -O0):
<app_timer_real>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmpl   $0x0,-0xc(%rbp)
je     117f <app_timer_real+0x5f>
lea    -0x30(%rbp),%rdi
lea    0x2ee6(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <timespecsub@plt>
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  0xe93(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    119d <app_timer_real+0x7d>
mov    -0x30(%rbp),%rax
mov    %rax,0x2eae(%rip)
mov    -0x28(%rbp),%rax
mov    %rax,0x2eab(%rip)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<app_timer_real>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %ebx,%ebx
je     1166 <app_timer_real+0x46>
lea    0x2ef5(%rip),%rsi
mov    %rsp,%rdi
lea    0x10(%rsp),%rdx
call   1040 <timespecsub@plt>
movsd  0x18(%rsp),%xmm0
divsd  0xea2(%rip),%xmm0
addsd  0x10(%rsp),%xmm0
jmp    1177 <app_timer_real+0x57>
movupd (%rsp),%xmm0
movupd %xmm0,0x2ec5(%rip)
xorpd  %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { double, double }
@app_timer_real.start = internal global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
define double @app_timer_real(i32 %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %3
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %5)
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = call i32 @timespecsub(%struct.timespec* %5, %struct.timespec* @app_timer_real.start, %struct.timespec* %4)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%13 = load double, double* %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%15 = load double, double* %14
%16 = fdiv double %15, 1.000000e+09
%17 = fadd double %13, %16
store double %17, double* %2
br label %20
18: ; preds = %1
%19 = bitcast %struct.timespec* %5 to i8*
store double 0.000000e+00, double* %2
br label %20
20: ; preds = %18, %10
%21 = load double, double* %2
ret double %21
}

LLVM IR (-O2):
%struct.timespec = type { double, double }
@app_timer_real.start = internal global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
define double @app_timer_real(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %3)
%8 = icmp eq i32 %0, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = call i32 @timespecsub(%struct.timespec* %3, %struct.timespec* @app_timer_real.start, %struct.timespec* %2)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%12 = load double, double* %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%14 = load double, double* %13
%15 = fdiv double %14, 1.000000e+09
%16 = fadd double %12, %15
br label %18
17: ; preds = %1
br label %18
18: ; preds = %17, %9
%19 = phi double [ %16, %9 ], [ 0.000000e+00, %17 ]
ret double %19
}