#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_us(struct timespec *ts, uint32_t us) {
  uint32_t sec = us / 1000000;
  us = us - sec * 1000000;
  ts->tv_nsec += us * 1000;
  ts->tv_sec += ts->tv_nsec / 1000000000 + sec;
  ts->tv_nsec = ts->tv_nsec % 1000000000;
}


x86 assembly (clang, -O0):
<timespec_add_us>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
imul   $0xf4240,-0x10(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
imul   $0x3e8,-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_us>:
movslq %esi,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xfff0bdc0,%ecx,%edx
add    %edx,%eax
imul   $0x3e8,%eax,%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_us(%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, 1000000
store i32 %7, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = mul nsw i32 %9, 1000000
%11 = sub nsw i32 %8, %10
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = mul nsw i32 %12, 1000
%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 = add nsw i32 %16, %13
store i32 %17, i32* %15
%18 = load %struct.timespec*, %struct.timespec** %3
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sdiv i32 %20, 1000000000
%22 = load i32, i32* %5
%23 = add nsw i32 %21, %22
%24 = load %struct.timespec*, %struct.timespec** %3
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = add nsw i32 %26, %23
store i32 %27, i32* %25
%28 = load %struct.timespec*, %struct.timespec** %3
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = srem i32 %30, 1000000000
%32 = load %struct.timespec*, %struct.timespec** %3
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 0
store i32 %31, i32* %33
ret void
}

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