#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ timer_t;
struct sigaction {
  void (*sa_handler)(int);
  int /*<<< orphan*/ member_0;
};
struct TYPE_3__ {
  long tv_nsec;
};
struct TYPE_4__ {
  long tv_nsec;
};
struct itimerspec {
  TYPE_1__ it_interval;
  TYPE_2__ it_value;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ perror(char *);
int sigaction(int /*<<< orphan*/, struct sigaction *, int /*<<< orphan*/ *);
int timer_create(int /*<<< orphan*/, int /*<<< orphan*/ *,
                 int /*<<< orphan*/ *);
int timer_settime(int /*<<< orphan*/, int /*<<< orphan*/, struct itimerspec *,
                  struct itimerspec *);
timer_t set_alarm(long timeout, long interval, void (*handler)(int)) {
  timer_t timer;
  struct itimerspec nv = {0};
  struct itimerspec ov = {0};
  nv.it_value.tv_nsec = 1000L * timeout;
  nv.it_interval.tv_nsec = 1000L * interval;
  struct sigaction action = {0};
  action.sa_handler = *handler;
  if (sigaction(SIGALRM, &action, NULL) == -1) {
    perror("sigaction");
  }
  if (timer_create(CLOCK_MONOTONIC, NULL, &timer) == -1) {
    perror("timer_create");
  }
  if (timer_settime(timer, 0, &nv, &ov) == -1) {
    perror("timer_settime");
  }
  return timer;
}


x86 assembly (clang, -O0):
<set_alarm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
imul   $0x3e8,-0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
imul   $0x3e8,-0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    11df <set_alarm+0x8f>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x1c(%rbp),%rdx
call   1050 <timer_create@plt>
cmp    $0xffffffff,%eax
jne    120a <set_alarm+0xba>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
lea    -0x38(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1030 <timer_settime@plt>
cmp    $0xffffffff,%eax
jne    1231 <set_alarm+0xe1>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_alarm>:
sub    $0x48,%rsp
movq   $0x0,0x28(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movq   $0x0,0x40(%rsp)
imul   $0x3e8,%rdi,%rax
mov    %rax,0x20(%rsp)
imul   $0x3e8,%rsi,%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rdx,0x8(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
cmp    $0xffffffff,%eax
je     11db <set_alarm+0x9b>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rdx
xor    %esi,%esi
call   1050 <timer_create@plt>
cmp    $0xffffffff,%eax
je     11e9 <set_alarm+0xa9>
mov    0x4(%rsp),%edi
lea    0x18(%rsp),%rdx
lea    0x30(%rsp),%rcx
xor    %esi,%esi
call   1030 <timer_settime@plt>
cmp    $0xffffffff,%eax
je     11f7 <set_alarm+0xb7>
mov    0x4(%rsp),%eax
add    $0x48,%rsp
ret
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
jmp    119e <set_alarm+0x5e>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
jmp    11b8 <set_alarm+0x78>
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
jmp    11d2 <set_alarm+0x92>

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

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