#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_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ts_set(struct timespec *const, scalar_t__, scalar_t__);
int timer_update(struct timespec *const t, struct timespec *const prev) {
  struct timespec now;
  if (0 != clock_gettime(CLOCK_MONOTONIC, &now)) {
    perror("clock_gettime");
    return -5;
  }
  if (t)
    ts_set(t, t->tv_sec - (now.tv_sec - prev->tv_sec),
           t->tv_nsec - (now.tv_nsec - prev->tv_nsec));
  ts_set(prev, now.tv_sec, now.tv_nsec);
  return 0;
}


x86 assembly (clang, -O0):
<timer_update>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1178 <timer_update+0x48>
lea    0xe99(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    11d0 <timer_update+0xa0>
cmpq   $0x0,-0x10(%rbp)
je     11b8 <timer_update+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    0x8(%rcx),%rax
sub    %rax,%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
sub    %rax,%rdx
call   1040 <ts_set@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <ts_set@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_update>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    1196 <timer_update+0x66>
test   %rbx,%rbx
je     117a <timer_update+0x4a>
mov    (%rbx),%rdx
mov    0x8(%rbx),%rsi
sub    0x10(%rsp),%rsi
add    0x8(%r14),%rsi
sub    0x8(%rsp),%rdx
add    (%r14),%rdx
mov    %rbx,%rdi
call   1040 <ts_set@plt>
mov    0x8(%rsp),%rdx
mov    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1040 <ts_set@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffb,%eax
jmp    118e <timer_update+0x5e>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
define i32 @timer_update(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca i32
%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 i32, i32* @CLOCK_MONOTONIC
%8 = call i64 @clock_gettime(i32 %7, %struct.timespec* %6)
%9 = icmp ne i64 0, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -5, i32* %3
br label %45
12: ; preds = %2
%13 = load %struct.timespec*, %struct.timespec** %4
%14 = icmp ne %struct.timespec* %13, null
br i1 %14, label %15, label %38
15: ; preds = %12
%16 = load %struct.timespec*, %struct.timespec** %4
%17 = load %struct.timespec*, %struct.timespec** %4
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 1
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%21 = load i64, i64* %20
%22 = load %struct.timespec*, %struct.timespec** %5
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = sub nsw i64 %21, %24
%26 = sub nsw i64 %19, %25
%27 = load %struct.timespec*, %struct.timespec** %4
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load %struct.timespec*, %struct.timespec** %5
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = sub nsw i64 %31, %34
%36 = sub nsw i64 %29, %35
%37 = call i32 @ts_set(%struct.timespec* %16, i64 %26, i64 %36)
br label %38
38: ; preds = %15, %12
%39 = load %struct.timespec*, %struct.timespec** %5
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%41 = load i64, i64* %40
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i32 @ts_set(%struct.timespec* %39, i64 %41, i64 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %38, %10
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [14 x i8] c"clock_gettime\00"
define i32 @timer_update(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i64 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %2
%11 = icmp eq %struct.timespec* %0, null
br i1 %11, label %30, label %12
12: ; preds = %10
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%18 = load i64, i64* %17
%19 = sub i64 %14, %16
%20 = add i64 %19, %18
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%26 = load i64, i64* %25
%27 = sub i64 %22, %24
%28 = add i64 %27, %26
%29 = call i32 @ts_set(%struct.timespec* %0, i64 %20, i64 %28)
br label %30
30: ; preds = %12, %10
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = call i32 @ts_set(%struct.timespec* %1, i64 %32, i64 %34)
br label %36
36: ; preds = %30, %8
%37 = phi i32 [ -5, %8 ], [ 0, %30 ]
ret i32 %37
}