#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_PROCESS_CPUTIME_ID;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *);
void twatch_stop_set_cpu_time(struct timespec *start, struct timespec *tgt) {
  struct timespec tmp;
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &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_set_cpu_time>:
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_set_cpu_time+0x3d>
lea    0xead(%rip),%rdi
call   1040 <printf@plt>
jmp    11b2 <twatch_stop_set_cpu_time+0x92>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jge    11b2 <twatch_stop_set_cpu_time+0x92>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
sub    $0x1,%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twatch_stop_set_cpu_time>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb4(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
je     1151 <twatch_stop_set_cpu_time+0x31>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
jmp    117a <twatch_stop_set_cpu_time+0x5a>
movdqa (%rsp),%xmm0
movdqu (%rbx),%xmm1
psubq  %xmm1,%xmm0
movdqu %xmm0,(%r14)
movq   %xmm0,%rax
test   %rax,%rax
jns    117a <twatch_stop_set_cpu_time+0x5a>
paddq  0xe8b(%rip),%xmm0
movdqu %xmm0,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@.str = [16 x i8] c"gettime failed\0A\00"
define void @twatch_stop_set_cpu_time(%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_PROCESS_CPUTIME_ID
%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 %41
11: ; preds = %2
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%13 = load i64, i64* %12
%14 = load %struct.timespec*, %struct.timespec** %3
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = sub nsw i64 %13, %16
%18 = load %struct.timespec*, %struct.timespec** %4
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
store i64 %17, i64* %19
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load %struct.timespec*, %struct.timespec** %3
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = sub nsw i64 %21, %24
%26 = load %struct.timespec*, %struct.timespec** %4
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 0
store i64 %25, i64* %27
%28 = load %struct.timespec*, %struct.timespec** %4
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %11
%33 = load %struct.timespec*, %struct.timespec** %4
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = sub nsw i64 %35, 1
store i64 %36, i64* %34
%37 = load %struct.timespec*, %struct.timespec** %4
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = add nsw i64 %39, 1000000000
store i64 %40, i64* %38
br label %41
41: ; preds = %9, %32, %11
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@str = [15 x i8] c"gettime failed\00"
define void @twatch_stop_set_cpu_time(%struct.timespec* nocapture %0, %struct.timespec* nocapture %1) {
%3 = alloca <2 x i64>
%4 = bitcast <2 x i64>* %3 to %struct.timespec*
%5 = bitcast <2 x i64>* %3 to i8*
%6 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%7 = call i64 @clock_gettime(i32 %6, %struct.timespec* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %22
11: ; preds = %2
%12 = load <2 x i64>, <2 x i64>* %3
%13 = bitcast %struct.timespec* %0 to <2 x i64>*
%14 = load <2 x i64>, <2 x i64>* %13
%15 = sub nsw <2 x i64> %12, %14
%16 = bitcast %struct.timespec* %1 to <2 x i64>*
store <2 x i64> %15, <2 x i64>* %16
%17 = extractelement <2 x i64> %15, i64 0
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = add nsw <2 x i64> %15, <i64 1000000000, i64 -1>
%21 = bitcast %struct.timespec* %1 to <2 x i64>*
store <2 x i64> %20, <2 x i64>* %21
br label %22
22: ; preds = %11, %19, %9
ret void
}