#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int uint64_t;
struct TYPE_2__ {
  int tv_sec;
  int tv_usec;
};
struct itimerval {
  TYPE_1__ it_value;
  TYPE_1__ it_interval;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int /*<<< orphan*/ perror(char *);
int setitimer(int /*<<< orphan*/, struct itimerval *, struct itimerval *);
uint64_t timer(uint64_t ns) {
  int rc;
  uint64_t ticks = ~0;
  struct itimerval itimer = {0};
  itimer.it_value.tv_sec = ns / 1000000000;
  itimer.it_value.tv_usec = (ns % 1000000000) / 1000;
  itimer.it_interval = itimer.it_value;
  rc = setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
  if (rc == 0) {
    ticks = itimer.it_value.tv_sec;
    ticks *= 1000000000;
    ticks += (itimer.it_value.tv_usec * 1000);
  } else {
    perror("setitimer");
  }
  return ticks;
}


x86 assembly (clang, -O0):
<timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <setitimer@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11c5 <timer+0x95>
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
imul   $0x3b9aca00,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
imul   $0x3e8,-0x1c(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11d1 <timer+0xa1>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x10(%rsp)
movslq %edi,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3b9aca00,%ecx,%ebp
sub    %ebp,%eax
cltq
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
mov    %ebx,0x4(%rsp)
mov    (%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <setitimer@plt>
test   %eax,%eax
jne    1199 <timer+0x79>
imul   $0x3e8,%ebx,%eax
add    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe60(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1192 <timer+0x72>

LLVM IR (-O0):
%struct.itimerval = type { %struct.TYPE_2__, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32 }
@ITIMER_REAL = global i32 0
@.str = [10 x i8] c"setitimer\00"
define i32 @timer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.itimerval
store i32 %0, i32* %2
store i32 -1, i32* %4
%6 = bitcast %struct.itimerval* %5 to i8*
%7 = load i32, i32* %2
%8 = sdiv i32 %7, 1000000000
%9 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 0
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* %2
%12 = srem i32 %11, 1000000000
%13 = sdiv i32 %12, 1000
%14 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 1
store i32 %13, i32* %15
%16 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 1
%17 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 0
%18 = bitcast %struct.TYPE_2__* %16 to i8*
%19 = bitcast %struct.TYPE_2__* %17 to i8*
%20 = load i32, i32* @ITIMER_REAL
%21 = call i32 @setitimer(i32 %20, %struct.itimerval* %5, %struct.itimerval* null)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %1
%25 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = mul nsw i32 %28, 1000000000
store i32 %29, i32* %4
%30 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %5, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = mul nsw i32 %32, 1000
%34 = load i32, i32* %4
%35 = add nsw i32 %34, %33
store i32 %35, i32* %4
br label %38
36: ; preds = %1
%37 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %24
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.itimerval = type { %struct.TYPE_2__, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32 }
@ITIMER_REAL = global i32 0
@.str = [10 x i8] c"setitimer\00"
define i32 @timer(i32 %0) {
%2 = alloca %struct.itimerval
%3 = bitcast %struct.itimerval* %2 to i8*
%4 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 1
%5 = bitcast %struct.TYPE_2__* %4 to i8*
%6 = sdiv i32 %0, 1000000000
%7 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 0
store i32 %6, i32* %7
%8 = srem i32 %0, 1000000000
%9 = sdiv i32 %8, 1000
%10 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 1
%12 = bitcast %struct.itimerval* %2 to i64*
%13 = bitcast %struct.TYPE_2__* %11 to i64*
%14 = load i64, i64* %12
store i64 %14, i64* %13
%15 = load i32, i32* @ITIMER_REAL
%16 = call i32 @setitimer(i32 %15, %struct.itimerval* %2, %struct.itimerval* null)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = mul nsw i32 %6, 1000000000
%20 = mul nsw i32 %9, 1000
%21 = add nsw i32 %20, %19
br label %24
22: ; preds = %1
%23 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = phi i32 [ %21, %18 ], [ -1, %22 ]
ret i32 %25
}