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

/* Variables and functions */
struct timespec timespec_add_ns(struct timespec ts, int64_t ns) {
  struct timespec res;
  res.tv_nsec = ts.tv_nsec + ns;
  res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
  res.tv_nsec %= 1000000000LL;
  return res;
}


x86 assembly (clang, -O0):
<timespec_add_ns>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movabs $0x112e0be826d694b3,%rcx
0b 2e 11
imul   %rcx
mov    -0x38(%rbp),%rax
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x1a,%rdx
add    %rsi,%rdx
add    %rdx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
imul   %rcx
mov    -0x30(%rbp),%rax
mov    %rdx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x1a,%rcx
add    %rdx,%rcx
imul   $0x3b9aca00,%rcx,%rcx
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdx
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec_add_ns>:
lea    (%rdi,%rdx,1),%rcx
movabs $0x112e0be826d694b3,%rdx
0b 2e 11
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
add    %rdx,%rsi
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,%rax
mov    %rsi,%rdx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define { i64, i64 } @timespec_add_ns(i64 %0, i64 %1, i64 %2) {
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca i64
%7 = bitcast %struct.timespec* %5 to { i64, i64 }*
%8 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %7, i32 0, i32 0
store i64 %0, i64* %8
%9 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %7, i32 0, i32 1
store i64 %1, i64* %9
store i64 %2, i64* %6
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* %6
%13 = add nsw i64 %11, %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = sdiv i64 %18, 1000000000
%20 = add nsw i64 %16, %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %20, i64* %21
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = srem i64 %23, 1000000000
store i64 %24, i64* %22
%25 = bitcast %struct.timespec* %4 to { i64, i64 }*
%26 = load { i64, i64 }, { i64, i64 }* %25
ret { i64, i64 } %26
}

LLVM IR (-O2):
define { i64, i64 } @timespec_add_ns(i64 %0, i64 %1, i64 %2) {
%4 = add nsw i64 %2, %0
%5 = sdiv i64 %4, 1000000000
%6 = add nsw i64 %5, %1
%7 = srem i64 %4, 1000000000
%8 = insertvalue { i64, i64 } poison, i64 %7, 0
%9 = insertvalue { i64, i64 } %8, i64 %6, 1
ret { i64, i64 } %9
}