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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *);
void twatch_stop_add(struct timespec *start, struct timespec *tgt) {
  struct timespec tmp;
  if (clock_gettime(CLOCK_MONOTONIC, &tmp) != 0) {
    printf("gettime failed\n");
    return;
  }
  tgt->tv_sec += tmp.tv_sec - start->tv_sec;
  tgt->tv_nsec += tmp.tv_nsec - start->tv_nsec;
  if (tgt->tv_nsec < 0) {
    tgt->tv_sec -= 1;
    tgt->tv_nsec += 1000000000;
  }
}


x86 assembly (clang, -O0):
<twatch_stop_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     115d <twatch_stop_add+0x3d>
lea    0xead(%rip),%rdi
call   1040 <printf@plt>
jmp    11b3 <twatch_stop_add+0x93>
mov    -0x20(%rbp),%ecx
mov    -0x8(%rbp),%rax
sub    (%rax),%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jge    11b3 <twatch_stop_add+0x93>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,0x8(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twatch_stop_add>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2eb4(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
je     1153 <twatch_stop_add+0x33>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
jmp    1181 <twatch_stop_add+0x61>
mov    0x8(%rsp),%eax
sub    (%r14),%eax
add    (%rbx),%eax
mov    %eax,(%rbx)
mov    0x10(%rsp),%rcx
sub    0x8(%r14),%rcx
add    0x8(%rbx),%rcx
mov    %rcx,0x8(%rbx)
jns    1181 <twatch_stop_add+0x61>
add    $0xffffffff,%eax
mov    %eax,(%rbx)
add    $0x3b9aca00,%rcx
mov    %rcx,0x8(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@str = [15 x i8] c"gettime failed\00"
define void @twatch_stop_add(%struct.timespec* nocapture %0, %struct.timespec* nocapture %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 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %31
10: ; preds = %2
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = sub nsw i32 %12, %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, %15
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%22 = load i64, i64* %21
%23 = sub nsw i64 %20, %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%25 = load i64, i64* %24
%26 = add nsw i64 %25, %23
store i64 %26, i64* %24
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %10
%29 = add nsw i32 %18, -1
store i32 %29, i32* %16
%30 = add nsw i64 %26, 1000000000
store i64 %30, i64* %24
br label %31
31: ; preds = %10, %28, %8
ret void
}