#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 {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
struct timespec *abstime_to_reltime(const struct timespec *abstime,
                                    struct timespec *reltime) {
  if (!abstime)
    return NULL;
  struct timespec now;
  clock_gettime(CLOCK_REALTIME, &now);
  reltime->tv_sec = abstime->tv_sec - now.tv_sec;
  reltime->tv_nsec = abstime->tv_nsec - now.tv_nsec;
  if (reltime->tv_nsec < 0) {
    reltime->tv_nsec += 1000000000;
    --reltime->tv_sec;
  }
  return reltime;
}


x86 assembly (clang, -O0):
<abstime_to_reltime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1138 <abstime_to_reltime+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11a7 <abstime_to_reltime+0x97>
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
sub    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
sub    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jge    119f <abstime_to_reltime+0x8f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<abstime_to_reltime>:
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     115a <abstime_to_reltime+0x4a>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb7(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
movdqu (%rbx),%xmm0
psubq  (%rsp),%xmm0
movdqu %xmm0,(%r14)
movq   %xmm0,%rax
test   %rax,%rax
jns    115d <abstime_to_reltime+0x4d>
paddq  0xead(%rip),%xmm0
movdqu %xmm0,(%r14)
jmp    115d <abstime_to_reltime+0x4d>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define %struct.timespec* @abstime_to_reltime(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca <2 x i64>
%4 = icmp eq %struct.timespec* %0, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = bitcast <2 x i64>* %3 to %struct.timespec*
%7 = bitcast <2 x i64>* %3 to i8*
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %6)
%10 = bitcast %struct.timespec* %0 to <2 x i64>*
%11 = load <2 x i64>, <2 x i64>* %10
%12 = load <2 x i64>, <2 x i64>* %3
%13 = sub nsw <2 x i64> %11, %12
%14 = bitcast %struct.timespec* %1 to <2 x i64>*
store <2 x i64> %13, <2 x i64>* %14
%15 = extractelement <2 x i64> %13, i64 0
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = add nsw <2 x i64> %13, <i64 1000000000, i64 -1>
%19 = bitcast %struct.timespec* %1 to <2 x i64>*
store <2 x i64> %18, <2 x i64>* %19
br label %20
20: ; preds = %17, %5
br label %21
21: ; preds = %2, %20
%22 = phi %struct.timespec* [ %1, %20 ], [ null, %2 ]
ret %struct.timespec* %22
}