#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_nsec;
  scalar_t__ tv_sec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
struct timespec time_diff(const struct timespec *begin,
                          const struct timespec *end) {
  struct timespec result = {0};
  if (end->tv_nsec >= begin->tv_nsec) {
    result.tv_sec = end->tv_sec - begin->tv_sec;
    result.tv_nsec = end->tv_nsec - begin->tv_nsec;
  } else {
    result.tv_sec = end->tv_sec - begin->tv_sec - 1;
    result.tv_nsec = 1000000000 - begin->tv_nsec + end->tv_nsec;
  }
  return result;
}


x86 assembly (clang, -O0):
<time_diff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x20(%rbp)
mov    %rdi,%rax
mov    %rax,-0x18(%rbp)
mov    %rsi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jl     1174 <time_diff+0x64>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    -0x8(%rbp),%rdx
sub    0x8(%rdx),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
mov    -0x8(%rbp),%rdx
sub    (%rdx),%ecx
mov    %ecx,(%rax)
jmp    11a3 <time_diff+0x93>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    -0x8(%rbp),%rdx
sub    0x8(%rdx),%rcx
sub    $0x1,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rdx
mov    $0x3b9aca00,%ecx
sub    (%rdx),%ecx
mov    -0x10(%rbp),%rdx
add    (%rdx),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_diff>:
mov    %rdi,%rax
xorps  %xmm0,%xmm0
movups %xmm0,(%rdi)
movq   $0x0,0x10(%rdi)
mov    (%rdx),%r9d
mov    (%rsi),%r8d
mov    0x8(%rsi),%rsi
lea    0x3b9aca00(%r9),%edi
mov    %rsi,%rcx
neg    %rcx
cmp    %r8d,%r9d
not    %rsi
cmovge %rcx,%rsi
cmovge %r9d,%edi
add    0x8(%rdx),%rsi
sub    %r8d,%edi
mov    %rsi,0x8(%rax)
mov    %edi,(%rax)
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64, i32 }
define void @time_diff(%struct.timespec* noalias nocapture writeonly sret(%struct.timespec) align 8 %0, %struct.timespec* nocapture %1, %struct.timespec* nocapture %2) {
%4 = bitcast %struct.timespec* %0 to i8*
tail call void @llvm.memset.p0i8.i64(i8* align 8 dereferenceable(24) %4, i8 0, i64 24, i1 false)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp slt i32 %6, %8
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%13 = load i64, i64* %12
%14 = xor i64 %13, -1
%15 = add i32 %6, 1000000000
%16 = sub i64 0, %13
%17 = select i1 %9, i64 %14, i64 %16
%18 = add i64 %11, %17
%19 = select i1 %9, i32 %15, i32 %6
%20 = sub i32 %19, %8
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i64 %18, i64* %21
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i32 %20, i32* %22
ret void
}