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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ memset(struct itimerspec *, int /*<<< orphan*/, int);
int timer_create(int /*<<< orphan*/, struct sigevent *, int /*<<< orphan*/ *);
int /*<<< orphan*/ timer_delete(int /*<<< orphan*/);
int timer_settime(int /*<<< orphan*/, int /*<<< orphan*/, struct itimerspec *,
                  int /*<<< orphan*/ *);
int mt_set_alarm(time_t at, const char *death_msg, timer_t *timer) {
  struct itimerspec ispec;
  struct sigevent evp;
  int ret;
  evp.sigev_value.sival_ptr = (void *)death_msg;
  evp.sigev_notify = SIGEV_SIGNAL;
  evp.sigev_signo = SIGALRM;
  ret = timer_create(CLOCK_MONOTONIC, &evp, timer);
  if (ret)
    return ((ret > 0) ? -ret : ret);
  memset(&ispec, 0, sizeof(ispec));
  ispec.it_value.tv_sec = at;
  ret = timer_settime(*timer, TIMER_ABSTIME, &ispec, NULL);
  if (ret) {
    timer_delete(*timer);
    return ((ret > 0) ? -ret : ret);
  }
  return 0;
}


x86 assembly (clang, -O0):
<mt_set_alarm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rsi
call   1040 <timer_create@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11be <mt_set_alarm+0x7e>
cmpl   $0x0,-0x34(%rbp)
jle    11ad <mt_set_alarm+0x6d>
xor    %eax,%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11b3 <mt_set_alarm+0x73>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <mt_set_alarm+0xf7>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <timer_settime@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1230 <mt_set_alarm+0xf0>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <timer_delete@plt>
cmpl   $0x0,-0x34(%rbp)
jle    121f <mt_set_alarm+0xdf>
xor    %eax,%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1225 <mt_set_alarm+0xe5>
mov    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <mt_set_alarm+0xf7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mt_set_alarm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %edi,%ebp
mov    %rsi,0x10(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <timer_create@plt>
test   %eax,%eax
je     118e <mt_set_alarm+0x4e>
mov    %eax,%ebx
neg    %ebx
cmovns %eax,%ebx
jmp    11d0 <mt_set_alarm+0x90>
mov    %rsp,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %ebp,(%rsp)
mov    (%r14),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1030 <timer_settime@plt>
test   %eax,%eax
je     11d0 <mt_set_alarm+0x90>
mov    %eax,%ebp
mov    (%r14),%edi
call   1060 <timer_delete@plt>
mov    %ebp,%ebx
neg    %ebx
cmovns %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sigevent = type { i32, i32, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i8* }
@SIGEV_SIGNAL = global i32 0
@SIGALRM = global i32 0
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define i32 @mt_set_alarm(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.itimerspec
%9 = alloca %struct.sigevent
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i8*, i8** %6
%12 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 2
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %12, i32 0, i32 0
store i8* %11, i8** %13
%14 = load i32, i32* @SIGEV_SIGNAL
%15 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @SIGALRM
%17 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = load i32*, i32** %7
%20 = call i32 @timer_create(i32 %18, %struct.sigevent* %9, i32* %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %3
%24 = load i32, i32* %10
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %10
%28 = sub nsw i32 0, %27
br label %31
29: ; preds = %23
%30 = load i32, i32* %10
br label %31
31: ; preds = %29, %26
%32 = phi i32 [ %28, %26 ], [ %30, %29 ]
store i32 %32, i32* %4
br label %58
33: ; preds = %3
%34 = call i32 @memset(%struct.itimerspec* %8, i32 0, i32 4)
%35 = load i32, i32* %5
%36 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %8, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32*, i32** %7
%39 = load i32, i32* %38
%40 = load i32, i32* @TIMER_ABSTIME
%41 = call i32 @timer_settime(i32 %39, i32 %40, %struct.itimerspec* %8, i32* null)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %33
%45 = load i32*, i32** %7
%46 = load i32, i32* %45
%47 = call i32 @timer_delete(i32 %46)
%48 = load i32, i32* %10
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* %10
%52 = sub nsw i32 0, %51
br label %55
53: ; preds = %44
%54 = load i32, i32* %10
br label %55
55: ; preds = %53, %50
%56 = phi i32 [ %52, %50 ], [ %54, %53 ]
store i32 %56, i32* %4
br label %58
57: ; preds = %33
store i32 0, i32* %4
br label %58
58: ; preds = %57, %55, %31
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sigevent = type { i32, i32, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i8* }
@SIGEV_SIGNAL = global i32 0
@SIGALRM = global i32 0
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define i32 @mt_set_alarm(i32 %0, i8* %1, i32* %2) {
%4 = alloca %struct.itimerspec
%5 = alloca %struct.sigevent
%6 = bitcast %struct.itimerspec* %4 to i8*
%7 = bitcast %struct.sigevent* %5 to i8*
%8 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %5, i64 0, i32 2, i32 0
store i8* %1, i8** %8
%9 = load i32, i32* @SIGEV_SIGNAL
%10 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %5, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @SIGALRM
%12 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %5, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @timer_create(i32 %13, %struct.sigevent* %5, i32* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = call i32 @llvm.abs.i32(i32 %14, i1 false)
%18 = sub i32 0, %17
br label %31
19: ; preds = %3
%20 = call i32 @memset(%struct.itimerspec* %4, i32 0, i32 4)
%21 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 0, i32 0
store i32 %0, i32* %21
%22 = load i32, i32* %2
%23 = load i32, i32* @TIMER_ABSTIME
%24 = call i32 @timer_settime(i32 %22, i32 %23, %struct.itimerspec* %4, i32* null)
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = load i32, i32* %2
%28 = call i32 @timer_delete(i32 %27)
%29 = call i32 @llvm.abs.i32(i32 %24, i1 false)
%30 = sub i32 0, %29
br label %31
31: ; preds = %19, %26, %16
%32 = phi i32 [ %18, %16 ], [ %30, %26 ], [ 0, %19 ]
ret i32 %32
}