#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 */
typedef int uint64_t;
struct timespec {
  scalar_t__ tv_nsec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
uint64_t timediff(const struct timespec *start) {
  struct timespec end;
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
  struct timespec temp;
  if ((end.tv_nsec - start->tv_nsec) < 0) {
    temp.tv_sec = end.tv_sec - start->tv_sec - 1;
    temp.tv_nsec = 1000000000 + end.tv_nsec - start->tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec - start->tv_sec;
    temp.tv_nsec = end.tv_nsec - start->tv_nsec;
  }
  return temp.tv_sec * 1000000 + temp.tv_nsec / 1000;
}


x86 assembly (clang, -O0):
<timediff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ec5(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%rax
cmp    $0x0,%rax
jge    116d <timediff+0x5d>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
sub    0x8(%rcx),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x3b9aca00,%rax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x28(%rbp)
jmp    1189 <timediff+0x79>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x28(%rbp)
imul   $0xf4240,-0x20(%rbp),%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timediff>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ec9(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%rdx
mov    (%rbx),%rsi
mov    %rdx,%rax
sub    %rsi,%rax
mov    0x8(%rsp),%ecx
mov    0x8(%rbx),%edi
js     1143 <timediff+0x33>
sub    %edi,%ecx
jmp    1154 <timediff+0x44>
not    %edi
add    %edi,%ecx
sub    %rsi,%rdx
add    $0x3b9aca00,%rdx
mov    %rdx,%rax
imul   $0xf4240,%ecx,%ecx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x7,%rdx
add    %edx,%eax
add    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
define i32 @timediff(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store %struct.timespec* %0, %struct.timespec** %2
%5 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load %struct.timespec*, %struct.timespec** %2
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = sub nsw i64 %8, %11
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load %struct.timespec*, %struct.timespec** %2
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = sub nsw i32 %16, %19
%21 = sub nsw i32 %20, 1
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%24 = load i64, i64* %23
%25 = add nsw i64 1000000000, %24
%26 = load %struct.timespec*, %struct.timespec** %2
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = sub nsw i64 %25, %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %29, i64* %30
br label %46
31: ; preds = %1
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load %struct.timespec*, %struct.timespec** %2
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sub nsw i32 %33, %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load %struct.timespec*, %struct.timespec** %2
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = sub nsw i64 %40, %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %44, i64* %45
br label %46
46: ; preds = %31, %14
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%48 = load i32, i32* %47
%49 = mul nsw i32 %48, 1000000
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%52 = load i64, i64* %51
%53 = sdiv i64 %52, 1000
%54 = add nsw i64 %50, %53
%55 = trunc i64 %54 to i32
ret i32 %55
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
define i32 @timediff(%struct.timespec* nocapture %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%9 = load i64, i64* %8
%10 = sub nsw i64 %7, %9
%11 = icmp slt i64 %10, 0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%15 = load i32, i32* %14
br i1 %11, label %16, label %21
16: ; preds = %1
%17 = xor i32 %15, -1
%18 = add i32 %13, %17
%19 = add nsw i64 %7, 1000000000
%20 = sub i64 %19, %9
br label %23
21: ; preds = %1
%22 = sub nsw i32 %13, %15
br label %23
23: ; preds = %21, %16
%24 = phi i32 [ %18, %16 ], [ %22, %21 ]
%25 = phi i64 [ %20, %16 ], [ %10, %21 ]
%26 = mul nsw i32 %24, 1000000
%27 = sdiv i64 %25, 1000
%28 = trunc i64 %27 to i32
%29 = add i32 %26, %28
ret i32 %29
}