#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 {
  unsigned long long tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
unsigned long long getustime(int clockid) {
  struct timespec tp;
  if (clock_gettime((clockid_t)clockid, &tp)) {
    perror("clock_gettime");
    return 0;
  }
  return 1000000ULL * tp.tv_sec + tp.tv_nsec / 1000;
}


x86 assembly (clang, -O0):
<getustime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     115a <getustime+0x3a>
lea    0xeb8(%rip),%rdi
call   1040 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1181 <getustime+0x61>
imul   $0xf4240,-0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getustime>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    115f <getustime+0x3f>
imul   $0xf4240,0x8(%rsp),%rcx
movslq 0x10(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
cltq
add    %rcx,%rax
add    $0x18,%rsp
ret
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

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

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