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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int LONG_MAX;
scalar_t__ __clock_gettime(int /*<<< orphan*/, struct timespec *);
clock_t clock() {
  struct timespec ts;
  if (__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts))
    return -1;
  if (ts.tv_sec > LONG_MAX / 1000000 ||
      ts.tv_nsec / 1000 > LONG_MAX - 1000000 * ts.tv_sec)
    return -1;
  return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}


x86 assembly (clang, -O0):
<clock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ec9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <__clock_gettime@plt>
cmp    $0x0,%rax
je     1140 <clock+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <clock+0xa4>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jg     1189 <clock+0x79>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ecx
imul   $0xf4240,-0x10(%rbp),%edx
sub    %edx,%ecx
cmp    %ecx,%eax
jle    1195 <clock+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <clock+0xa4>
imul   $0xf4240,-0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock>:
push   %rax
mov    0x2ed0(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <__clock_gettime@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1131 <clock+0x21>
pop    %rcx
ret
mov    (%rsp),%edx
mov    0x2e9d(%rip),%rcx
movslq (%rcx),%rcx
imul   $0x431bde83,%rcx,%rsi
mov    %rsi,%rdi
shr    $0x3f,%rdi
sar    $0x32,%rsi
add    %edi,%esi
cmp    %esi,%edx
jg     112f <clock+0x1f>
movslq 0x4(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rax
add    %esi,%eax
imul   $0xf4240,%edx,%edx
sub    %edx,%ecx
add    %eax,%edx
cmp    %ecx,%eax
mov    $0xffffffff,%eax
cmovle %edx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@LONG_MAX = global i32 0
define i32 @clock() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%4 = call i64 @__clock_gettime(i32 %3, %struct.timespec* %2)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 -1, i32* %1
br label %32
7: ; preds = %0
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @LONG_MAX
%11 = sdiv i32 %10, 1000000
%12 = icmp sgt i32 %9, %11
br i1 %12, label %23, label %13
13: ; preds = %7
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000
%17 = load i32, i32* @LONG_MAX
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%19 = load i32, i32* %18
%20 = mul nsw i32 1000000, %19
%21 = sub nsw i32 %17, %20
%22 = icmp sgt i32 %16, %21
br i1 %22, label %23, label %24
23: ; preds = %13, %7
store i32 -1, i32* %1
br label %32
24: ; preds = %13
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%26 = load i32, i32* %25
%27 = mul nsw i32 %26, 1000000
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%29 = load i32, i32* %28
%30 = sdiv i32 %29, 1000
%31 = add nsw i32 %27, %30
store i32 %31, i32* %1
br label %32
32: ; preds = %24, %23, %6
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@LONG_MAX = global i32 0
define i32 @clock() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%4 = call i64 @__clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %0
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @LONG_MAX
%10 = sdiv i32 %9, 1000000
%11 = icmp sgt i32 %8, %10
br i1 %11, label %21, label %12
12: ; preds = %6
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%14 = load i32, i32* %13
%15 = sdiv i32 %14, 1000
%16 = mul nsw i32 %8, 1000000
%17 = sub nsw i32 %9, %16
%18 = icmp sgt i32 %15, %17
%19 = add nsw i32 %15, %16
%20 = select i1 %18, i32 -1, i32 %19
br label %21
21: ; preds = %12, %6, %0
%22 = phi i32 [ -1, %0 ], [ -1, %6 ], [ %20, %12 ]
ret i32 %22
}