#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 */
typedef int uint64_t;
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
uint64_t watch(void) {
  int rc;
  uint64_t ticks = ~0;
  struct timespec spec = {0};
  rc = clock_gettime(CLOCK_MONOTONIC_RAW, &spec);
  if (rc == 0) {
    ticks = spec.tv_sec;
    ticks *= 1000000000;
    ticks += spec.tv_nsec;
  } else {
    perror("clock_gettime");
  }
  return ticks;
}


x86 assembly (clang, -O0):
<watch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1191 <watch+0x61>
mov    -0x20(%rbp),%eax
mov    %eax,-0x8(%rbp)
imul   $0x3b9aca00,-0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%rcx
movslq -0x8(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x8(%rbp)
jmp    119d <watch+0x6d>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch>:
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
jne    1159 <watch+0x39>
imul   $0x3b9aca00,(%rsp),%eax
add    0x8(%rsp),%eax
add    $0x18,%rsp
ret
lea    0xea0(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
define i32 @watch() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 -1, i32* %2
%4 = bitcast %struct.timespec* %3 to i8*
%5 = load i32, i32* @CLOCK_MONOTONIC_RAW
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %3)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = mul nsw i32 %12, 1000000000
store i32 %13, i32* %2
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%15 = load i64, i64* %14
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = add nsw i64 %17, %15
%19 = trunc i64 %18 to i32
store i32 %19, i32* %2
br label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %9
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
define i32 @watch() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_MONOTONIC_RAW
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %0
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = mul nsw i32 %8, 1000000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = add i32 %9, %12
br label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %6
%17 = phi i32 [ %13, %6 ], [ -1, %14 ]
ret i32 %17
}