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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, scalar_t__);
int /*<<< orphan*/ stderr;
void bench(int flag) {
  static struct timespec s;
  struct timespec e, res;
  clock_gettime(CLOCK_MONOTONIC, &e);
  if (flag & 1) {
    s = e;
    return;
  }
  res.tv_sec = e.tv_sec - s.tv_sec;
  res.tv_nsec = e.tv_nsec - s.tv_nsec;
  if (res.tv_sec > 0 && res.tv_nsec < 0) {
    res.tv_sec--;
    res.tv_nsec += 1e9;
  }
  fprintf(stderr, "%02ld.%09ld\n", res.tv_sec, res.tv_nsec);
}


x86 assembly (clang, -O0):
<bench>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1167 <bench+0x47>
mov    -0x18(%rbp),%rax
mov    %rax,0x2ee1(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,0x2ede(%rip)
jmp    11df <bench+0xbf>
mov    -0x18(%rbp),%rax
sub    0x2ec6(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
sub    0x2ebf(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    11c2 <bench+0xa2>
cmpq   $0x0,-0x20(%rbp)
jge    11c2 <bench+0xa2>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm0
movsd  0xe4b(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe2e(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bench>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   $0x1,%bl
jne    1195 <bench+0x75>
mov    (%rsp),%rdx
sub    0x2ef0(%rip),%rdx
mov    0x8(%rsp),%rax
sub    0x2eec(%rip),%rax
test   %rdx,%rdx
setg   %cl
test   %rax,%rax
sets   %bl
and    %cl,%bl
cvtsi2sd %rax,%xmm0
addsd  0xe91(%rip),%xmm0
cvttsd2si %xmm0,%rcx
cmove  %rax,%rcx
movzbl %bl,%eax
sub    %rax,%rdx
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11a2 <bench+0x82>
movupd (%rsp),%xmm0
movupd %xmm0,0x2e96(%rip)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@bench.s = internal global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"%02ld.%09ld\0A\00"
define void @bench(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = load i32, i32* %2
%8 = and i32 %7, 1
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = bitcast %struct.timespec* %3 to i8*
br label %46
12: ; preds = %1
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @bench.s, i32 0, i32 0)
%16 = sub nsw i64 %14, %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %16, i64* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%19 = load i64, i64* %18
%20 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @bench.s, i32 0, i32 1)
%21 = sub nsw i64 %19, %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %21, i64* %22
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %12
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%32 = load i64, i64* %31
%33 = add nsw i64 %32, -1
store i64 %33, i64* %31
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%35 = load i64, i64* %34
%36 = sitofp i64 %35 to double
%37 = fadd double %36, 1.000000e+09
%38 = fptosi double %37 to i64
store i64 %38, i64* %34
br label %39
39: ; preds = %30, %26, %12
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%44 = load i64, i64* %43
%45 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %42, i64 %44)
br label %46
46: ; preds = %39, %10
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@bench.s = internal unnamed_addr global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"%02ld.%09ld\0A\00"
define void @bench(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = and i32 %0, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %9, label %8
8: ; preds = %1
br label %29
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @bench.s, i64 0, i32 0)
%13 = sub nsw i64 %11, %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%15 = load i64, i64* %14
%16 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @bench.s, i64 0, i32 1)
%17 = sub nsw i64 %15, %16
%18 = icmp sgt i64 %13, 0
%19 = icmp slt i64 %17, 0
%20 = select i1 %18, i1 %19, i1 false
%21 = sitofp i64 %17 to double
%22 = fadd double %21, 1.000000e+09
%23 = fptosi double %22 to i64
%24 = select i1 %20, i64 %23, i64 %17
%25 = sext i1 %20 to i64
%26 = add nsw i64 %13, %25
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %26, i64 %24)
br label %29
29: ; preds = %9, %8
ret void
}