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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ terminal_error(char *);
unsigned long get_usecs(struct timespec *myts) {
  if (clock_gettime(CLOCK_REALTIME, myts))
    terminal_error("clock_gettime");
  return (myts->tv_sec * 1000000 + myts->tv_nsec / 1000);
}


x86 assembly (clang, -O0):
<get_usecs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     1154 <get_usecs+0x34>
lea    0xeb1(%rip),%rdi
call   1040 <terminal_error@plt>
mov    -0x8(%rbp),%rax
imul   $0xf4240,(%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
add    %ecx,%eax
cltq
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_usecs>:
push   %rbx
mov    %rdi,%rbx
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     1146 <get_usecs+0x26>
lea    0xebf(%rip),%rdi
call   1040 <terminal_error@plt>
movslq (%rbx),%rax
imul   $0xf4240,%rax,%rcx
movslq 0x4(%rbx),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
cltq
add    %rcx,%rax
pop    %rbx
ret

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

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