#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 {
  int tv_sec;
  int tv_nsec;
};
typedef int clock_t;

/* Variables and functions */
int CLOCKS_PER_SEC;
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
clock_t clock(void) {
  struct timespec tm;
  static int clock_inited;
  static clock_t clock_start;
  clock_t now;
  clock_gettime(CLOCK_MONOTONIC, &tm);
  now = tm.tv_sec * CLOCKS_PER_SEC + (tm.tv_nsec * (CLOCKS_PER_SEC / 1e9));
  if (!clock_inited) {
    clock_start = now;
    clock_inited = 1;
  }
  return now - clock_start;
}


x86 assembly (clang, -O0):
<clock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ec9(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%eax
mov    0x2eac(%rip),%rcx
imul   (%rcx),%eax
cvtsi2sd %eax,%xmm1
cvtsi2sdl -0x4(%rbp),%xmm0
mov    0x2e99(%rip),%rax
cvtsi2sdl (%rax),%xmm2
movsd  0xead(%rip),%xmm3
divsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,0x2ebf(%rip)
jne    1186 <clock+0x76>
mov    -0xc(%rbp),%eax
mov    %eax,0x2eb4(%rip)
movl   $0x1,0x2ea6(%rip)
mov    -0xc(%rbp),%eax
sub    0x2ea1(%rip),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock>:
push   %rax
mov    0x2ed0(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
mov    (%rsp),%ecx
imul   %eax,%ecx
cvtsi2sd %ecx,%xmm0
cvtsi2sdl 0x4(%rsp),%xmm1
cvtsi2sd %eax,%xmm2
divsd  0xeb9(%rip),%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm0,%xmm2
cvttsd2si %xmm2,%eax
cmpb   $0x1,0x2ed2(%rip)
jne    1164 <clock+0x54>
mov    0x2ece(%rip),%ecx
jmp    1173 <clock+0x63>
mov    %eax,0x2ec6(%rip)
movb   $0x1,0x2ebb(%rip)
mov    %eax,%ecx
sub    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@clock.clock_inited = internal global i32 0
@clock.clock_start = internal global i32 0
@CLOCK_MONOTONIC = global i32 0
@CLOCKS_PER_SEC = global i32 0
define i32 @clock() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @CLOCKS_PER_SEC
%8 = mul nsw i32 %6, %7
%9 = sitofp i32 %8 to double
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%11 = load i32, i32* %10
%12 = sitofp i32 %11 to double
%13 = load i32, i32* @CLOCKS_PER_SEC
%14 = sitofp i32 %13 to double
%15 = fdiv double %14, 1.000000e+09
%16 = call double @llvm.fmuladd.f64(double %12, double %15, double %9)
%17 = fptosi double %16 to i32
store i32 %17, i32* %2
%18 = load i32, i32* @clock.clock_inited
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %0
%21 = load i32, i32* %2
store i32 %21, i32* @clock.clock_start
store i32 1, i32* @clock.clock_inited
br label %22
22: ; preds = %20, %0
%23 = load i32, i32* %2
%24 = load i32, i32* @clock.clock_start
%25 = sub nsw i32 %23, %24
ret i32 %25
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@clock.clock_inited = internal unnamed_addr global i1 false
@clock.clock_start = internal unnamed_addr global i32 0
@CLOCK_MONOTONIC = global i32 0
@CLOCKS_PER_SEC = global i32 0
define i32 @clock() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @CLOCKS_PER_SEC
%8 = mul nsw i32 %7, %6
%9 = sitofp i32 %8 to double
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sitofp i32 %11 to double
%13 = sitofp i32 %7 to double
%14 = fdiv double %13, 1.000000e+09
%15 = call double @llvm.fmuladd.f64(double %12, double %14, double %9)
%16 = fptosi double %15 to i32
%17 = load i1, i1* @clock.clock_inited
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = load i32, i32* @clock.clock_start
br label %21
20: ; preds = %0
store i32 %16, i32* @clock.clock_start
store i1 true, i1* @clock.clock_inited
br label %21
21: ; preds = %18, %20
%22 = phi i32 [ %19, %18 ], [ %16, %20 ]
%23 = sub nsw i32 %16, %22
ret i32 %23
}