#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 uld {
  int dummy;
};
struct timespec {
  long long tv_sec;
  long long tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ timeout_add(struct uld *, struct timespec *,
                               void (*)(void *), void *);
void timeout_add_usec(struct uld *uld, int usec, void (*fun)(void *),
                      void *arg) {
  struct timespec ts;
  long long nsec;
  nsec = usec;
  nsec *= 1000;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  ts.tv_sec += nsec / 1000000000;
  ts.tv_nsec += (nsec % 1000000000);
  if (ts.tv_nsec >= 1000000000) {
    ts.tv_nsec -= 1000000000;
    ts.tv_sec += 1;
  }
  timeout_add(uld, &ts, fun, arg);
}


x86 assembly (clang, -O0):
<timeout_add_usec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x38(%rbp)
imul   $0x3e8,-0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x38(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
add    -0x28(%rbp),%rdx
mov    %rdx,-0x28(%rbp)
cmpq   $0x3b9aca00,-0x28(%rbp)
3b
jl     11b1 <timeout_add_usec+0x91>
mov    -0x28(%rbp),%rax
sub    $0x3b9aca00,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rsi
call   1040 <timeout_add@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_add_usec>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%r12
movslq %esi,%r13
imul   $0x3e8,%r13,%rbx
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
imul   $0x431bde83,%r13,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rax
add    %ecx,%eax
movslq %eax,%rcx
add    (%rsp),%rcx
mov    %rcx,(%rsp)
movabs $0x112e0be826d694b3,%rdx
0b 2e 11
mov    %rbx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rbx
add    0x8(%rsp),%rbx
mov    %rbx,0x8(%rsp)
cmp    $0x3b9aca00,%rbx
jl     11bf <timeout_add_usec+0x9f>
add    $0xffffffffc4653600,%rbx
mov    %rbx,0x8(%rsp)
add    $0x1,%rcx
mov    %rcx,(%rsp)
mov    %rsp,%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <timeout_add@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.uld = type { i32 }
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define void @timeout_add_usec(%struct.uld* %0, i32 %1, void (i8*)* %2, i8* %3) {
%5 = alloca %struct.uld*
%6 = alloca i32
%7 = alloca void (i8*)*
%8 = alloca i8*
%9 = alloca %struct.timespec
%10 = alloca i64
store %struct.uld* %0, %struct.uld** %5
store i32 %1, i32* %6
store void (i8*)* %2, void (i8*)** %7
store i8* %3, i8** %8
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
store i64 %12, i64* %10
%13 = load i64, i64* %10
%14 = mul nsw i64 %13, 1000
store i64 %14, i64* %10
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %9)
%17 = load i64, i64* %10
%18 = sdiv i64 %17, 1000000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%20 = load i64, i64* %19
%21 = add nsw i64 %20, %18
store i64 %21, i64* %19
%22 = load i64, i64* %10
%23 = srem i64 %22, 1000000000
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%25 = load i64, i64* %24
%26 = add nsw i64 %25, %23
store i64 %26, i64* %24
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp sge i64 %28, 1000000000
br i1 %29, label %30, label %37
30: ; preds = %4
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%32 = load i64, i64* %31
%33 = sub nsw i64 %32, 1000000000
store i64 %33, i64* %31
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%35 = load i64, i64* %34
%36 = add nsw i64 %35, 1
store i64 %36, i64* %34
br label %37
37: ; preds = %30, %4
%38 = load %struct.uld*, %struct.uld** %5
%39 = load void (i8*)*, void (i8*)** %7
%40 = load i8*, i8** %8
%41 = call i32 @timeout_add(%struct.uld* %38, %struct.timespec* %9, void (i8*)* %39, i8* %40)
ret void
}

LLVM IR (-O2):
%struct.uld = type { i32 }
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define void @timeout_add_usec(%struct.uld* %0, i32 %1, void (i8*)* %2, i8* %3) {
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %5 to i8*
%7 = sext i32 %1 to i64
%8 = mul nsw i64 %7, 1000
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %5)
%11 = sdiv i32 %1, 1000000
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%14 = load i64, i64* %13
%15 = add nsw i64 %14, %12
store i64 %15, i64* %13
%16 = srem i64 %8, 1000000000
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%18 = load i64, i64* %17
%19 = add nsw i64 %18, %16
store i64 %19, i64* %17
%20 = icmp sgt i64 %19, 999999999
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = add nsw i64 %19, -1000000000
store i64 %22, i64* %17
%23 = add nsw i64 %15, 1
store i64 %23, i64* %13
br label %24
24: ; preds = %21, %4
%25 = call i32 @timeout_add(%struct.uld* %0, %struct.timespec* %5, void (i8*)* %2, i8* %3)
ret void
}