#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 {
  long long tv_sec;
  long long tv_nsec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long long, long long,
                           long long);
void ptimer_stopstore(int FEAT_TIME, int counter, long long start,
                      FILE *ptfilepntr) {
  struct timespec current;
  long long end, elapsed, microseconds;
  if ((FEAT_TIME) && (counter > 1)) {
    if (clock_gettime(CLOCK_MONOTONIC, &current)) {
      exit(EXIT_FAILURE);
    }
    end = current.tv_sec * 1000000000LL + current.tv_nsec;
    elapsed = end - start;
    microseconds = elapsed / 1000 + (elapsed % 1000 >= 500);
    fprintf(ptfilepntr, "%lld, %lld, %lld \n", start, end, microseconds);
  }
}


x86 assembly (clang, -O0):
<ptimer_stopstore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11fa <ptimer_stopstore+0xca>
cmpl   $0x1,-0x8(%rbp)
jle    11fa <ptimer_stopstore+0xca>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     1184 <ptimer_stopstore+0x54>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
imul   $0x3b9aca00,-0x28(%rbp),%rax
3b
add    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    -0x48(%rbp),%rax
cmp    $0x1f4,%rdx
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%r8
lea    0xe0b(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptimer_stopstore>:
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
je     11c1 <ptimer_stopstore+0x91>
cmp    $0x2,%esi
jl     11c1 <ptimer_stopstore+0x91>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    11c9 <ptimer_stopstore+0x99>
imul   $0x3b9aca00,0x8(%rsp),%rcx
9a 3b
add    0x10(%rsp),%rcx
mov    %rcx,%rsi
sub    %rbx,%rsi
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rsi,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rdx,%rax
imul   $0x3e8,%rax,%rdx
sub    %rdx,%rsi
xor    %r8d,%r8d
cmp    $0x1f4,%rsi
setge  %r8b
add    %rax,%r8
lea    0xe4a(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [19 x i8] c"%lld, %lld, %lld \0A\00"
define void @ptimer_stopstore(i32 %0, i32 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca %struct.timespec
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %48
15: ; preds = %4
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %48
18: ; preds = %15
%19 = load i32, i32* @CLOCK_MONOTONIC
%20 = call i64 @clock_gettime(i32 %19, %struct.timespec* %9)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%27 = load i64, i64* %26
%28 = mul nsw i64 %27, 1000000000
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %28, %30
store i64 %31, i64* %10
%32 = load i64, i64* %10
%33 = load i64, i64* %7
%34 = sub nsw i64 %32, %33
store i64 %34, i64* %11
%35 = load i64, i64* %11
%36 = sdiv i64 %35, 1000
%37 = load i64, i64* %11
%38 = srem i64 %37, 1000
%39 = icmp sge i64 %38, 500
%40 = zext i1 %39 to i32
%41 = sext i32 %40 to i64
%42 = add nsw i64 %36, %41
store i64 %42, i64* %12
%43 = load i32*, i32** %8
%44 = load i64, i64* %7
%45 = load i64, i64* %10
%46 = load i64, i64* %12
%47 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i64 %44, i64 %45, i64 %46)
br label %48
48: ; preds = %25, %15, %4
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [19 x i8] c"%lld, %lld, %lld \0A\00"
define void @ptimer_stopstore(i32 %0, i32 %1, i64 %2, i32* nocapture %3) {
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %5 to i8*
%7 = icmp ne i32 %0, 0
%8 = icmp sgt i32 %1, 1
%9 = and i1 %7, %8
br i1 %9, label %10, label %31
10: ; preds = %4
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i64 @clock_gettime(i32 %11, %struct.timespec* %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%19 = load i64, i64* %18
%20 = mul nsw i64 %19, 1000000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%22 = load i64, i64* %21
%23 = add nsw i64 %20, %22
%24 = sub nsw i64 %23, %2
%25 = sdiv i64 %24, 1000
%26 = srem i64 %24, 1000
%27 = icmp sgt i64 %26, 499
%28 = zext i1 %27 to i64
%29 = add nsw i64 %25, %28
%30 = call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i64 %2, i64 %23, i64 %29)
br label %31
31: ; preds = %17, %4
ret void
}