#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 /*<<< orphan*/ clockid_t;
typedef int clock_t;

/* Variables and functions */
int clock();
int clock_gettime(clockid_t __attribute__((unused)) clk_id,
                  struct timespec *tp) {
  clock_t t = clock();
  if (t == -1)
    return -1;
  tp->tv_sec = t / 1000;
  tp->tv_nsec = (t % 1000) * 1000000;
  return 0;
}


x86 assembly (clang, -O0):
<clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    113f <clock_gettime+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1171 <clock_gettime+0x61>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_gettime>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
cmp    $0xffffffff,%eax
je     1150 <clock_gettime+0x40>
movslq %eax,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rbx)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x4(%rbx)
jmp    1155 <clock_gettime+0x45>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @clock_gettime(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = call i32 (...) @clock()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %21
11: ; preds = %2
%12 = load i32, i32* %6
%13 = sdiv i32 %12, 1000
%14 = load %struct.timespec*, %struct.timespec** %5
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %6
%17 = srem i32 %16, 1000
%18 = mul nsw i32 %17, 1000000
%19 = load %struct.timespec*, %struct.timespec** %5
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 1
store i32 %18, i32* %20
store i32 0, i32* %3
br label %21
21: ; preds = %11, %10
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @clock_gettime(i32 %0, %struct.timespec* nocapture writeonly %1) {
%3 = tail call i32 (...) @clock()
%4 = icmp eq i32 %3, -1
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = sdiv i32 %3, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %6, i32* %7
%8 = srem i32 %3, 1000
%9 = mul nsw i32 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %2, %5
%12 = phi i32 [ 0, %5 ], [ -1, %2 ]
ret i32 %12
}