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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ EXIT_FAILURE;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
void print_elapsed_time(void) {
  static struct timespec start;
  struct timespec curr;
  static int first_call = 1;
  int secs, nsecs;
  if (first_call) {
    first_call = 0;
    if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
      do {
        perror("clock_gettime");
        exit(EXIT_FAILURE);
      } while (0);
  }
  if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
    do {
      perror("clock_gettime");
      exit(EXIT_FAILURE);
    } while (0);
  secs = curr.tv_sec - start.tv_sec;
  nsecs = curr.tv_nsec - start.tv_nsec;
  if (nsecs < 0) {
    secs--;
    nsecs += 1000000000;
  }
  printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
}


x86 assembly (clang, -O0):
<print_elapsed_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0x0,0x2ef1(%rip)
je     11a1 <print_elapsed_time+0x61>
movl   $0x0,0x2ee1(%rip)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0x2ed9(%rip),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    119c <print_elapsed_time+0x5c>
jmp    1182 <print_elapsed_time+0x42>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11a1 <print_elapsed_time+0x61>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11db <print_elapsed_time+0x9b>
jmp    11c1 <print_elapsed_time+0x81>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%eax
sub    0x2e64(%rip),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sub    0x2e5c(%rip),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1211 <print_elapsed_time+0xd1>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
add    $0x7a120,%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_elapsed_time>:
push   %rax
cmpb   $0x0,0x2f04(%rip)
jne    116b <print_elapsed_time+0x2b>
movb   $0x1,0x2efb(%rip)
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
lea    0x2ee3(%rip),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     11d1 <print_elapsed_time+0x91>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     11d1 <print_elapsed_time+0x91>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x2eb6(%rip),%eax
sub    0x2eb4(%rip),%ecx
lea    0x3b9aca00(%rcx),%edx
mov    %ecx,%esi
sar    $0x1f,%esi
add    %eax,%esi
test   %ecx,%ecx
cmovns %ecx,%edx
add    $0x7a120,%edx
movslq %edx,%rax
imul   $0x431bde83,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x32,%rdx
add    %eax,%edx
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
pop    %rax
ret
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@print_elapsed_time.start = internal global %struct.timespec zeroinitializer
@print_elapsed_time.first_call = internal global i32 1
@CLOCK_MONOTONIC = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"%d.%03d: \00"
define void @print_elapsed_time() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @print_elapsed_time.first_call
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %0
store i32 0, i32* @print_elapsed_time.first_call
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* @print_elapsed_time.start)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %6
br label %11
11: ; preds = %10
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; No predecessors!
br label %16
16: ; preds = %15, %6
br label %17
17: ; preds = %16, %0
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i32 @clock_gettime(i32 %18, %struct.timespec* %1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %17
br label %22
22: ; preds = %21
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; No predecessors!
br label %27
27: ; preds = %26, %17
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i32 0, i32 0)
%31 = sub nsw i32 %29, %30
store i32 %31, i32* %2
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i32 0, i32 1)
%35 = sub nsw i32 %33, %34
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %27
%39 = load i32, i32* %2
%40 = add nsw i32 %39, -1
store i32 %40, i32* %2
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1000000000
store i32 %42, i32* %3
br label %43
43: ; preds = %38, %27
%44 = load i32, i32* %2
%45 = load i32, i32* %3
%46 = add nsw i32 %45, 500000
%47 = sdiv i32 %46, 1000000
%48 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %44, i32 %47)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@print_elapsed_time.start = internal global %struct.timespec zeroinitializer
@print_elapsed_time.first_call = internal unnamed_addr global i1 false
@CLOCK_MONOTONIC = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"%d.%03d: \00"
define void @print_elapsed_time() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i1, i1* @print_elapsed_time.first_call
br i1 %3, label %12, label %4
4: ; preds = %0
store i1 true, i1* @print_elapsed_time.first_call
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = tail call i32 @clock_gettime(i32 %5, %struct.timespec* @print_elapsed_time.start)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %4, %0
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i64 0, i32 0)
%24 = sub i32 %22, %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%26 = load i32, i32* %25
%27 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i64 0, i32 1)
%28 = sub nsw i32 %26, %27
%29 = icmp slt i32 %28, 0
%30 = add nsw i32 %28, 1000000000
%31 = ashr i32 %28, 31
%32 = add nsw i32 %24, %31
%33 = select i1 %29, i32 %30, i32 %28
%34 = add nsw i32 %33, 500000
%35 = sdiv i32 %34, 1000000
%36 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %35)
ret void
}