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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
long micros(void) {
  static long first = -1;
  long nsec;
  struct timespec now_ts;
  if (clock_gettime(CLOCK_REALTIME, &now_ts) < 0) {
    fprintf(stderr, "clock_gettime(CLOCK_REALTIME) failed: %s\n",
            strerror(errno));
    return 0;
  }
  if (first == -1)
    first = now_ts.tv_nsec;
  if (now_ts.tv_nsec < first)
    nsec = now_ts.tv_nsec + 1000000;
  else
    nsec = now_ts.tv_nsec;
  return nsec / 1000;
}


x86 assembly (clang, -O0):
<micros>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
jge    118d <micros+0x5d>
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe85(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <micros+0xb4>
cmpq   $0xffffffffffffffff,0x2ea3(%rip)
ff
jne    11a6 <micros+0x76>
mov    -0x18(%rbp),%rax
mov    %rax,0x2e92(%rip)
mov    -0x18(%rbp),%rax
cmp    0x2e87(%rip),%rax
jge    11ca <micros+0x9a>
mov    -0x18(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x10(%rbp)
jmp    11d2 <micros+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<micros>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
js     1197 <micros+0x67>
mov    0x2ee4(%rip),%rdx
mov    0x8(%rsp),%rcx
cmp    $0xffffffffffffffff,%rdx
jne    1169 <micros+0x39>
mov    %rcx,0x2ed2(%rip)
mov    %rcx,%rdx
lea    0xf4240(%rcx),%rax
cmp    %rdx,%rcx
cmovge %rcx,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
imul   %rcx
mov    %rdx,%rax
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x7,%rax
add    %rcx,%rax
jmp    11c1 <micros+0x91>
mov    0x2e42(%rip),%rax
mov    (%rax),%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
@micros.first = internal global i64 -1
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"clock_gettime(CLOCK_REALTIME) failed: %s\0A\00"
@errno = global i32 0
define i64 @micros() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = load i32, i32* @CLOCK_REALTIME
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %10)
store i64 0, i64* %1
br label %33
12: ; preds = %0
%13 = load i64, i64* @micros.first
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%17 = load i64, i64* %16
store i64 %17, i64* @micros.first
br label %18
18: ; preds = %15, %12
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @micros.first
%22 = icmp slt i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%25 = load i64, i64* %24
%26 = add nsw i64 %25, 1000000
store i64 %26, i64* %2
br label %30
27: ; preds = %18
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%29 = load i64, i64* %28
store i64 %29, i64* %2
br label %30
30: ; preds = %27, %23
%31 = load i64, i64* %2
%32 = sdiv i64 %31, 1000
store i64 %32, i64* %1
br label %33
33: ; preds = %30, %7
%34 = load i64, i64* %1
ret i64 %34
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@micros.first = internal unnamed_addr global i64 -1
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"clock_gettime(CLOCK_REALTIME) failed: %s\0A\00"
@errno = global i32 0
define i64 @micros() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_REALTIME
%4 = call i64 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %23
11: ; preds = %0
%12 = load i64, i64* @micros.first
%13 = icmp eq i64 %12, -1
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%15 = load i64, i64* %14
br i1 %13, label %16, label %17
16: ; preds = %11
store i64 %15, i64* @micros.first
br label %17
17: ; preds = %11, %16
%18 = phi i64 [ %15, %16 ], [ %12, %11 ]
%19 = icmp slt i64 %15, %18
%20 = add nsw i64 %15, 1000000
%21 = select i1 %19, i64 %20, i64 %15
%22 = sdiv i64 %21, 1000
br label %23
23: ; preds = %17, %6
%24 = phi i64 [ 0, %6 ], [ %22, %17 ]
ret i64 %24
}