#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 */
typedef int uint32_t;
struct timespec {
  int tv_nsec;
  int tv_sec;
};

/* Variables and functions */
void timespec_add_ns(struct timespec *ts, uint32_t ns) {
  uint32_t sec = ns / 1000000000;
  ns = ns - sec * 1000000000;
  ts->tv_nsec += ns;
  ts->tv_sec += ts->tv_nsec / 1000000000 + sec;
  ts->tv_nsec = ts->tv_nsec % 1000000000;
}


x86 assembly (clang, -O0):
<timespec_add_ns>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
imul   $0x3b9aca00,-0x10(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
add    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    -0x8(%rbp),%rax
mov    %edx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec_add_ns>:
movslq %esi,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
imul   $0xc4653600,%ecx,%edx
add    %edx,%eax
add    (%rdi),%eax
cltq
imul   $0x44b82fa1,%rax,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x3c,%rdx
add    %esi,%edx
add    %edx,%ecx
add    %ecx,0x4(%rdi)
imul   $0x3b9aca00,%edx,%ecx
sub    %ecx,%eax
mov    %eax,(%rdi)
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @timespec_add_ns(%struct.timespec* nocapture %0, i32 %1) {
%3 = sdiv i32 %1, 1000000000
%4 = mul nsw i32 %3, -1000000000
%5 = add i32 %4, %1
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = add nsw i32 %5, %7
%9 = sdiv i32 %8, 1000000000
%10 = add nsw i32 %9, %3
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%12 = load i32, i32* %11
%13 = add nsw i32 %10, %12
store i32 %13, i32* %11
%14 = srem i32 %8, 1000000000
store i32 %14, i32* %6
ret void
}