#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 TYPE_3__ {
  int /*<<< orphan*/ *sival_ptr;
};
struct sigevent {
  TYPE_1__ sigev_value;
  int /*<<< orphan*/ sigev_signo;
  int /*<<< orphan*/ sigev_notify;
};
struct TYPE_4__ {
  unsigned int tv_sec;
  scalar_t__ tv_nsec;
};
struct itimerspec {
  TYPE_2__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ SIGRTMIN;
int /*<<< orphan*/ __ploop_err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errno;
scalar_t__ timer_create(int /*<<< orphan*/, struct sigevent *,
                        int /*<<< orphan*/ *);
scalar_t__ timer_settime(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct itimerspec *, int /*<<< orphan*/ *);
int set_timer(timer_t *tid, unsigned int timeout) {
  struct sigevent sigev = {};
  struct itimerspec it = {};
  sigev.sigev_notify = SIGEV_SIGNAL;
  sigev.sigev_signo = SIGRTMIN;
  sigev.sigev_value.sival_ptr = tid;
  if (timer_create(CLOCK_MONOTONIC, &sigev, tid)) {
    __ploop_err(errno, "timer_create");
    return -1;
  }
  it.it_value.tv_sec = timeout;
  it.it_value.tv_nsec = 0;
  if (timer_settime(*tid, 0, &it, NULL)) {
    __ploop_err(errno, "timer_settime");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rsi
call   1040 <timer_create@plt>
cmp    $0x0,%rax
je     11d0 <set_timer+0x90>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1060 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <set_timer+0xe5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x38(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <timer_settime@plt>
cmp    $0x0,%rax
je     121e <set_timer+0xde>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1060 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <set_timer+0xe5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_timer>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %rdi,0x18(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
mov    %rbx,%rdx
call   1040 <timer_create@plt>
test   %rax,%rax
je     1187 <set_timer+0x57>
lea    0xe7b(%rip),%rsi
jmp    11b2 <set_timer+0x82>
mov    %ebp,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%rbx),%edi
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1030 <timer_settime@plt>
test   %rax,%rax
je     11c5 <set_timer+0x95>
lea    0xe5b(%rip),%rsi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1050 <__ploop_err@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sigevent = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i32* }
%struct.itimerspec = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32, i64 }
@SIGEV_SIGNAL = global i32 0
@SIGRTMIN = global i32 0
@CLOCK_MONOTONIC = global i32 0
@errno = global i32 0
@.str = [13 x i8] c"timer_create\00"
@.str.1 = [14 x i8] c"timer_settime\00"
define i32 @set_timer(i32* %0, i32 %1) {
%3 = alloca %struct.sigevent
%4 = alloca %struct.itimerspec
%5 = bitcast %struct.sigevent* %3 to i8*
%6 = bitcast %struct.itimerspec* %4 to i8*
%7 = bitcast %struct.itimerspec* %4 to i64*
store i64 0, i64* %7
%8 = load i32, i32* @SIGEV_SIGNAL
%9 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @SIGRTMIN
%11 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %3, i64 0, i32 0, i32 0
store i32* %0, i32** %12
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i64 @timer_create(i32 %13, %struct.sigevent* %3, i32* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 0, i32 0
store i32 %1, i32* %17
%18 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* %0
%20 = call i64 @timer_settime(i32 %19, i32 0, %struct.itimerspec* %4, i32* null)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16, %2
%23 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %16 ]
%24 = load i32, i32* @errno
%25 = call i32 @__ploop_err(i32 %24, i8* %23)
br label %26
26: ; preds = %22, %16
%27 = phi i32 [ 0, %16 ], [ -1, %22 ]
ret i32 %27
}