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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
unsigned difftime_us(void) {
  static struct timespec ts0;
  struct timespec ts1;
  unsigned dt;
  if (clock_gettime(CLOCK_MONOTONIC, &ts1) != 0) {
    perror("clock_gettime()");
    exit(1);
  }
  if (ts0.tv_sec != 0) {
    struct timespec tsd;
    tsd.tv_sec = ts1.tv_sec - ts0.tv_sec;
    tsd.tv_nsec = ts1.tv_nsec - ts0.tv_nsec;
    dt = tsd.tv_sec * 1000000 + tsd.tv_nsec / (1000000000 / 1000000);
  } else {
    dt = 0;
  }
  ts0 = ts1;
  return dt;
}


x86 assembly (clang, -O0):
<difftime_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     116a <difftime_us+0x3a>
lea    0xea5(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,0x2ece(%rip)
je     11be <difftime_us+0x8e>
mov    -0x10(%rbp),%rax
sub    0x2ebd(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
sub    0x2eb8(%rip),%eax
mov    %eax,-0x20(%rbp)
imul   $0xf4240,-0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    11c5 <difftime_us+0x95>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,0x2e70(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,0x2e6d(%rip)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<difftime_us>:
sub    $0x18,%rsp
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    119c <difftime_us+0x6c>
mov    0x2eed(%rip),%rax
test   %rax,%rax
je     1189 <difftime_us+0x59>
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%edx
sub    0x2ee2(%rip),%edx
sub    %eax,%ecx
movslq %edx,%rax
imul   $0x10624dd3,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x26,%rdx
add    %eax,%edx
imul   $0xf4240,%ecx,%eax
add    %edx,%eax
jmp    118b <difftime_us+0x5b>
xor    %eax,%eax
movups 0x8(%rsp),%xmm0
movups %xmm0,0x2ea9(%rip)
add    $0x18,%rsp
ret
lea    0xe5d(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@difftime_us.ts0 = internal global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@.str = [16 x i8] c"clock_gettime()\00"
define i32 @difftime_us() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %1)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @difftime_us.ts0, i32 0, i32 0)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @difftime_us.ts0, i32 0, i32 0)
%17 = sub nsw i64 %15, %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @difftime_us.ts0, i32 0, i32 1)
%22 = sub nsw i32 %20, %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%25 = load i64, i64* %24
%26 = mul nsw i64 %25, 1000000
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%28 = load i32, i32* %27
%29 = sdiv i32 %28, 1000
%30 = sext i32 %29 to i64
%31 = add nsw i64 %26, %30
%32 = trunc i64 %31 to i32
store i32 %32, i32* %2
br label %34
33: ; preds = %10
store i32 0, i32* %2
br label %34
34: ; preds = %33, %13
%35 = bitcast %struct.timespec* %1 to i8*
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@difftime_us.ts0 = internal unnamed_addr global %struct.timespec zeroinitializer
@CLOCK_MONOTONIC = global i32 0
@.str = [16 x i8] c"clock_gettime()\00"
define i32 @difftime_us() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i64 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @difftime_us.ts0, i64 0, i32 0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%14 = load i64, i64* %13
%15 = sub nsw i64 %14, %10
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%17 = load i32, i32* %16
%18 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @difftime_us.ts0, i64 0, i32 1)
%19 = sub nsw i32 %17, %18
%20 = sdiv i32 %19, 1000
%21 = trunc i64 %15 to i32
%22 = mul i32 %21, 1000000
%23 = add i32 %20, %22
br label %24
24: ; preds = %9, %12
%25 = phi i32 [ %23, %12 ], [ 0, %9 ]
ret i32 %25
}