#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 */
unsigned int usecs_elapsed(struct timespec curtime, struct timespec timestamp) {
  struct timespec elapsed;
  unsigned int usec;
  elapsed.tv_sec = curtime.tv_sec - timestamp.tv_sec;
  if (!elapsed.tv_sec) {
    elapsed.tv_nsec = curtime.tv_nsec - timestamp.tv_nsec;
    usec = elapsed.tv_nsec / 1000;
  } else {
    usec = ((1000000000 - timestamp.tv_nsec) + curtime.tv_nsec) / 1000;
    usec += (elapsed.tv_sec - 1) * 1000000;
  }
  return usec;
}


x86 assembly (clang, -O0):
<usecs_elapsed>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    113b <usecs_elapsed+0x3b>
mov    -0x4(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    1163 <usecs_elapsed+0x63>
mov    $0x3b9aca00,%eax
sub    -0xc(%rbp),%eax
add    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
imul   $0xf4240,%eax,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<usecs_elapsed>:
mov    %rdi,%rax
shr    $0x20,%rax
mov    %rsi,%rcx
shr    $0x20,%rcx
sub    %ecx,%eax
sub    %esi,%edi
je     113d <usecs_elapsed+0x3d>
add    $0x3b9aca00,%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0xf4240,%edi,%ecx
add    %ecx,%eax
add    $0xfff0bdc0,%eax
ret
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @usecs_elapsed(i64 %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = bitcast %struct.timespec* %3 to i64*
store i64 %0, i64* %7
%8 = bitcast %struct.timespec* %4 to i64*
store i64 %1, i64* %8
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = sub nsw i32 %10, %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %2
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sub nsw i32 %20, %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%26 = load i32, i32* %25
%27 = sdiv i32 %26, 1000
store i32 %27, i32* %6
br label %42
28: ; preds = %2
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%30 = load i32, i32* %29
%31 = sub nsw i32 1000000000, %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%33 = load i32, i32* %32
%34 = add nsw i32 %31, %33
%35 = sdiv i32 %34, 1000
store i32 %35, i32* %6
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sub nsw i32 %37, 1
%39 = mul nsw i32 %38, 1000000
%40 = load i32, i32* %6
%41 = add i32 %40, %39
store i32 %41, i32* %6
br label %42
42: ; preds = %28, %18
%43 = load i32, i32* %6
ret i32 %43
}

LLVM IR (-O2):
define i32 @usecs_elapsed(i64 %0, i64 %1) {
%3 = trunc i64 %0 to i32
%4 = lshr i64 %0, 32
%5 = trunc i64 %4 to i32
%6 = trunc i64 %1 to i32
%7 = lshr i64 %1, 32
%8 = trunc i64 %7 to i32
%9 = sub nsw i32 %3, %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = sub nsw i32 %5, %8
%13 = sdiv i32 %12, 1000
br label %21
14: ; preds = %2
%15 = add i32 %5, 1000000000
%16 = sub i32 %15, %8
%17 = sdiv i32 %16, 1000
%18 = mul i32 %9, 1000000
%19 = add i32 %18, -1000000
%20 = add i32 %19, %17
br label %21
21: ; preds = %14, %11
%22 = phi i32 [ %20, %14 ], [ %13, %11 ]
ret i32 %22
}