#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 clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ err(int, 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)
      err(1, "clock_gettime");
  }
  if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
    err(1, "clock_gettime");
  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,0x2ef9(%rip)
je     1183 <print_elapsed_time+0x53>
movl   $0x0,0x2ee9(%rip)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
lea    0x2ee1(%rip),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    117e <print_elapsed_time+0x4e>
mov    $0x1,%edi
lea    0xe87(%rip),%rsi
call   1050 <err@plt>
jmp    1183 <print_elapsed_time+0x53>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11af <print_elapsed_time+0x7f>
mov    $0x1,%edi
lea    0xe56(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%eax
sub    0x2e88(%rip),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sub    0x2e80(%rip),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e5 <print_elapsed_time+0xb5>
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    0xe0d(%rip),%rdi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_elapsed_time>:
push   %rax
cmpb   $0x0,0x2f0c(%rip)
jne    116c <print_elapsed_time+0x3c>
movb   $0x1,0x2f03(%rip)
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
lea    0x2eeb(%rip),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    116c <print_elapsed_time+0x3c>
lea    0xe9e(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1193 <print_elapsed_time+0x63>
lea    0xe77(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x2e9c(%rip),%eax
sub    0x2e9a(%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    0xe32(%rip),%rdi
call   1040 <printf@plt>
pop    %rax
ret

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"
@.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 %13
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 %12
10: ; preds = %6
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
br label %13
13: ; preds = %12, %0
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i32 0, i32 0)
%23 = sub nsw i32 %21, %22
store i32 %23, i32* %2
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i32 0, i32 1)
%27 = sub nsw i32 %25, %26
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %19
%31 = load i32, i32* %2
%32 = add nsw i32 %31, -1
store i32 %32, i32* %2
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1000000000
store i32 %34, i32* %3
br label %35
35: ; preds = %30, %19
%36 = load i32, i32* %2
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 500000
%39 = sdiv i32 %38, 1000000
%40 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %39)
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"
@.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 %10, 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 %10
8: ; preds = %4
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %4, %8, %0
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i64 0, i32 0)
%20 = sub i32 %18, %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @print_elapsed_time.start, i64 0, i32 1)
%24 = sub nsw i32 %22, %23
%25 = icmp slt i32 %24, 0
%26 = add nsw i32 %24, 1000000000
%27 = ashr i32 %24, 31
%28 = add nsw i32 %20, %27
%29 = select i1 %25, i32 %26, i32 %24
%30 = add nsw i32 %29, 500000
%31 = sdiv i32 %30, 1000000
%32 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %31)
ret void
}