#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*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int getpid();
int /*<<< orphan*/ printf(char *, int, char *, int);
void timeit(char *msg) {
  char *t_msg = "timeit:";
  static struct timespec ts1, ts2, *ts;
  static int first_in = 0;
  first_in = !first_in;
  if (first_in)
    ts = &ts1;
  else
    ts = &ts2;
  clock_gettime(CLOCK_MONOTONIC, ts);
  if (!first_in) {
    if (!msg)
      msg = t_msg;
    printf("%d:%s: time delta msecs=%ld\n", getpid(), msg,
           (ts2.tv_sec - ts1.tv_sec) * 1000 +
               (ts2.tv_nsec - ts1.tv_nsec) / 1000000);
  }
}


x86 assembly (clang, -O0):
<timeit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xebd(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,0x2f0a(%rip)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,0x2efa(%rip)
cmpl   $0x0,0x2ef3(%rip)
je     117e <timeit+0x4e>
lea    0x2ece(%rip),%rax
mov    %rax,0x2ed7(%rip)
jmp    118c <timeit+0x5c>
lea    0x2ec3(%rip),%rax
mov    %rax,0x2ec4(%rip)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2eb4(%rip),%rsi
call   1030 <clock_gettime@plt>
cmpl   $0x0,0x2eb0(%rip)
jne    120a <timeit+0xda>
cmpq   $0x0,-0x8(%rbp)
jne    11c1 <timeit+0x91>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e70(%rip),%eax
sub    0x2e62(%rip),%eax
imul   $0x3e8,%eax,%ecx
mov    0x2e62(%rip),%eax
sub    0x2e54(%rip),%eax
mov    $0xf4240,%edi
cltd
idiv   %edi
mov    -0x18(%rbp),%rdx
add    %eax,%ecx
lea    0xe03(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeit>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
cmpl   $0x0,0x2f18(%rip)
lea    0x2ef9(%rip),%rcx
lea    0x2efa(%rip),%rsi
cmove  %rcx,%rsi
sete   %al
mov    %eax,0x2efd(%rip)
mov    %rsi,0x2eee(%rip)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1030 <clock_gettime@plt>
cmpl   $0x0,0x2ee1(%rip)
je     1181 <timeit+0x51>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
test   %rbx,%rbx
lea    0xe75(%rip),%r14
cmovne %rbx,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2eac(%rip),%ecx
sub    0x2e9e(%rip),%ecx
mov    0x2ea4(%rip),%edx
sub    0x2e96(%rip),%edx
imul   $0x3e8,%ecx,%esi
movslq %edx,%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
add    %esi,%ecx
lea    0xe34(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <printf@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [8 x i8] c"timeit:\00"
@timeit.ts1 = internal global %struct.timespec zeroinitializer
@timeit.ts2 = internal global %struct.timespec zeroinitializer
@timeit.ts = internal global %struct.timespec* null
@timeit.first_in = internal global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [29 x i8] c"%d:%s: time delta msecs=%ld\0A\00"
define void @timeit(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = load i32, i32* @timeit.first_in
%5 = icmp ne i32 %4, 0
%6 = xor i1 %5, true
%7 = zext i1 %6 to i32
store i32 %7, i32* @timeit.first_in
%8 = load i32, i32* @timeit.first_in
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store %struct.timespec* @timeit.ts1, %struct.timespec** @timeit.ts
br label %12
11: ; preds = %1
store %struct.timespec* @timeit.ts2, %struct.timespec** @timeit.ts
br label %12
12: ; preds = %11, %10
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = load %struct.timespec*, %struct.timespec** @timeit.ts
%15 = call i32 @clock_gettime(i32 %13, %struct.timespec* %14)
%16 = load i32, i32* @timeit.first_in
%17 = icmp ne i32 %16, 0
br i1 %17, label %36, label %18
18: ; preds = %12
%19 = load i8*, i8** %2
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = load i8*, i8** %3
store i8* %22, i8** %2
br label %23
23: ; preds = %21, %18
%24 = call i32 (...) @getpid()
%25 = load i8*, i8** %2
%26 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts2, i32 0, i32 0)
%27 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts1, i32 0, i32 0)
%28 = sub nsw i32 %26, %27
%29 = mul nsw i32 %28, 1000
%30 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts2, i32 0, i32 1)
%31 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts1, i32 0, i32 1)
%32 = sub nsw i32 %30, %31
%33 = sdiv i32 %32, 1000000
%34 = add nsw i32 %29, %33
%35 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %24, i8* %25, i32 %34)
br label %36
36: ; preds = %23, %12
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [8 x i8] c"timeit:\00"
@timeit.ts1 = internal global %struct.timespec zeroinitializer
@timeit.ts2 = internal global %struct.timespec zeroinitializer
@timeit.ts = internal unnamed_addr global %struct.timespec* null
@timeit.first_in = internal unnamed_addr global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [29 x i8] c"%d:%s: time delta msecs=%ld\0A\00"
define void @timeit(i8* %0) {
%2 = load i32, i32* @timeit.first_in
%3 = icmp eq i32 %2, 0
%4 = zext i1 %3 to i32
store i32 %4, i32* @timeit.first_in
%5 = select i1 %3, %struct.timespec* @timeit.ts1, %struct.timespec* @timeit.ts2
store %struct.timespec* %5, %struct.timespec** @timeit.ts
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = tail call i32 @clock_gettime(i32 %6, %struct.timespec* %5)
%8 = load i32, i32* @timeit.first_in
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %1
%11 = icmp eq i8* %0, null
%12 = select i1 %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0
%13 = tail call i32 (...) @getpid()
%14 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts2, i64 0, i32 0)
%15 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts1, i64 0, i32 0)
%16 = sub nsw i32 %14, %15
%17 = mul nsw i32 %16, 1000
%18 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts2, i64 0, i32 1)
%19 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @timeit.ts1, i64 0, i32 1)
%20 = sub nsw i32 %18, %19
%21 = sdiv i32 %20, 1000000
%22 = add nsw i32 %21, %17
%23 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %12, i32 %22)
br label %24
24: ; preds = %10, %1
ret void
}