#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_7__ TYPE_3__;
typedef struct TYPE_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
typedef scalar_t__ timer_t;
struct TYPE_6__ {
  int tv_sec;
  scalar_t__ tv_nsec;
};
struct TYPE_5__ {
  int tv_sec;
  scalar_t__ tv_nsec;
};
struct itimerspec {
  TYPE_2__ it_interval;
  TYPE_1__ it_value;
};
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_7__ {
  long si_signo;
};
typedef TYPE_3__ siginfo_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int sigwaitinfo(int /*<<< orphan*/ *, TYPE_3__ *);
int timer_create(int /*<<< orphan*/, int /*<<< orphan*/ *, scalar_t__ *);
int timer_settime(scalar_t__, int /*<<< orphan*/, struct itimerspec *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ timestamp(char *);
int main() {
  sigset_t waitset;
  struct itimerspec ts;
  timer_t tid;
  siginfo_t info;
  int j, result;
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGALRM);
  sigprocmask(SIG_BLOCK, &waitset, NULL);
  if (timer_create(CLOCK_REALTIME, NULL, &tid) == -1)
    perror("timer_create");
  printf("timer ID = %ld\n", (long)tid);
  ts.it_value.tv_sec = 2;
  ts.it_value.tv_nsec = 0;
  ts.it_interval.tv_sec = 2;
  ts.it_interval.tv_nsec = 0;
  if (timer_settime(tid, 0, &ts, NULL) == -1)
    perror("timer_settime");
  for (j = 0;; j++) {
    result = sigwaitinfo(&waitset, &info);
    if (result < 0)
      printf("sigwaitinfo failed : \n");
    printf("sigwaitinfo() returned for signal %d\n", info.si_signo);
    timestamp("after sigwaitinfo()");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   10b0 <sigaddset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1060 <timer_create@plt>
cmp    $0xffffffff,%eax
jne    11fb <main+0x6b>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x2,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x2,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <timer_settime@plt>
cmp    $0xffffffff,%eax
jne    1253 <main+0xc3>
lea    0xdcf(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x3c(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <sigwaitinfo@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1282 <main+0xf2>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdcd(%rip),%rdi
call   1050 <timestamp@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    125a <main+0xca>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10c0 <sigaddset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rdx
xor    %esi,%esi
call   1070 <timer_create@plt>
cmp    $0xffffffff,%eax
je     12a0 <main+0x100>
mov    0x10(%rsp),%rsi
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
movl   $0x2,0x30(%rsp)
movq   $0x0,0x38(%rsp)
movl   $0x2,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x10(%rsp),%rdi
lea    0x20(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1030 <timer_settime@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x111>
lea    0xc(%rsp),%r14
lea    0x18(%rsp),%r12
lea    0xe0b(%rip),%r15
lea    0xdca(%rip),%r13
lea    0xde9(%rip),%rbx
jmp    1287 <main+0xe7>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1060 <timestamp@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10a0 <sigwaitinfo@plt>
test   %eax,%eax
jns    1270 <main+0xd0>
mov    %r15,%rdi
call   1050 <puts@plt>
jmp    1270 <main+0xd0>
lea    0xd59(%rip),%rdi
call   10b0 <perror@plt>
jmp    11fc <main+0x5c>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
jmp    1249 <main+0xa9>

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_7__ = type { i64 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [13 x i8] c"timer_create\00"
@.str.1 = [16 x i8] c"timer ID = %ld\0A\00"
@.str.2 = [14 x i8] c"timer_settime\00"
@.str.3 = [23 x i8] c"sigwaitinfo failed : \0A\00"
@.str.4 = [38 x i8] c"sigwaitinfo() returned for signal %d\0A\00"
@.str.5 = [20 x i8] c"after sigwaitinfo()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.itimerspec
%4 = alloca i64
%5 = alloca %struct.TYPE_7__
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @sigemptyset(i32* %2)
%9 = load i32, i32* @SIGALRM
%10 = call i32 @sigaddset(i32* %2, i32 %9)
%11 = load i32, i32* @SIG_BLOCK
%12 = call i32 @sigprocmask(i32 %11, i32* %2, i32* null)
%13 = load i32, i32* @CLOCK_REALTIME
%14 = call i32 @timer_create(i32 %13, i32* null, i64* %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = load i64, i64* %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %21, i32 0, i32 0
store i32 2, i32* %22
%23 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %23, i32 0, i32 1
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %25, i32 0, i32 0
store i32 2, i32* %26
%27 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %27, i32 0, i32 1
store i64 0, i64* %28
%29 = load i64, i64* %4
%30 = call i32 @timer_settime(i64 %29, i32 0, %struct.itimerspec* %3, i32* null)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %18
store i32 0, i32* %6
br label %35
35: ; preds = %46, %34
%36 = call i32 @sigwaitinfo(i32* %2, %struct.TYPE_7__* %5)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%45 = call i32 @timestamp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %41
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %35
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_7__ = type { i64 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [13 x i8] c"timer_create\00"
@.str.1 = [16 x i8] c"timer ID = %ld\0A\00"
@.str.2 = [14 x i8] c"timer_settime\00"
@.str.4 = [38 x i8] c"sigwaitinfo() returned for signal %d\0A\00"
@.str.5 = [20 x i8] c"after sigwaitinfo()\00"
@str = [22 x i8] c"sigwaitinfo failed : \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.itimerspec
%3 = alloca i64
%4 = alloca %struct.TYPE_7__
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.itimerspec* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast %struct.TYPE_7__* %4 to i8*
%9 = call i32 @sigemptyset(i32* %1)
%10 = load i32, i32* @SIGALRM
%11 = call i32 @sigaddset(i32* %1, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %1, i32* null)
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @timer_create(i32 %14, i32* null, i64* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = load i64, i64* %3
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i64 0, i32 1, i32 0
store i32 2, i32* %22
%23 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i64 0, i32 1, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i64 0, i32 0, i32 0
store i32 2, i32* %24
%25 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i64 0, i32 0, i32 1
store i64 0, i64* %25
%26 = load i64, i64* %3
%27 = call i32 @timer_settime(i64 %26, i32 0, %struct.itimerspec* %2, i32* null)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %19
%32 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %4, i64 0, i32 0
br label %33
33: ; preds = %38, %31
%34 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_7__* %4)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i64, i64* %32
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i64 %39)
%41 = call i32 @timestamp(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %33
}