#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 */
struct TYPE_6__ {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_5__ {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct itimerval {
  TYPE_2__ it_interval;
  TYPE_1__ it_value;
};
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_7__ {
  int si_signo;
};
typedef TYPE_3__ siginfo_t;

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setitimer(int /*<<< orphan*/, struct itimerval *,
                             int /*<<< orphan*/ *);
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 /*<<< orphan*/ timestamp(char *);
int main() {
  int result = 0;
  sigset_t waitset;
  struct itimerval timer;
  siginfo_t info;
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGALRM);
  sigprocmask(SIG_BLOCK, &waitset, NULL);
  timer.it_value.tv_sec = 5;
  timer.it_value.tv_usec = 0;
  timer.it_interval.tv_sec = 5;
  timer.it_interval.tv_usec = 0;
  setitimer(ITIMER_REAL, &timer, NULL);
  timestamp("before sigwaitinfo()");
  while (1) {
    result = sigwaitinfo(&waitset, &info);
    if (result < 0) {
      perror("Timeout Reached: ");
      exit(1);
    }
    printf("sigtimedwait() returned for signal %d\n", info.si_signo);
    timestamp("after sigtimedwait()");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10b0 <sigaddset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
movl   $0x5,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x5,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <setitimer@plt>
lea    0xdee(%rip),%rdi
call   1040 <timestamp@plt>
lea    -0xc(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <sigwaitinfo@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1247 <main+0xb7>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf1(%rip),%rdi
call   1040 <timestamp@plt>
jmp    1217 <main+0x87>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigemptyset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <sigaddset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
movl   $0x5,0x18(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1080 <setitimer@plt>
lea    0xdf6(%rip),%rdi
call   1040 <timestamp@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <sigwaitinfo@plt>
test   %eax,%eax
js     1262 <main+0xd2>
lea    0xe02(%rip),%r14
lea    0xe22(%rip),%r15
lea    0x4(%rsp),%r12
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <timestamp@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <sigwaitinfo@plt>
test   %eax,%eax
jns    1240 <main+0xb0>
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.itimerval = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_7__ = type { i32 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@ITIMER_REAL = global i32 0
@.str = [21 x i8] c"before sigwaitinfo()\00"
@.str.1 = [18 x i8] c"Timeout Reached: \00"
@.str.2 = [39 x i8] c"sigtimedwait() returned for signal %d\0A\00"
@.str.3 = [21 x i8] c"after sigtimedwait()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.itimerval
%5 = alloca %struct.TYPE_7__
store i32 0, i32* %1
store i32 0, i32* %2
%6 = call i32 @sigemptyset(i32* %3)
%7 = load i32, i32* @SIGALRM
%8 = call i32 @sigaddset(i32* %3, i32 %7)
%9 = load i32, i32* @SIG_BLOCK
%10 = call i32 @sigprocmask(i32 %9, i32* %3, i32* null)
%11 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %4, i32 0, i32 1
%12 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 0
store i32 5, i32* %12
%13 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %4, i32 0, i32 1
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %13, i32 0, i32 1
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %4, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %15, i32 0, i32 0
store i32 5, i32* %16
%17 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %4, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %17, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @ITIMER_REAL
%20 = call i32 @setitimer(i32 %19, %struct.itimerval* %4, i32* null)
%21 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %0, %29
%23 = call i32 @sigwaitinfo(i32* %3, %struct.TYPE_7__* %5)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %22
}

LLVM IR (-O2):
%struct.itimerval = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_7__ = type { i32 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@ITIMER_REAL = global i32 0
@.str = [21 x i8] c"before sigwaitinfo()\00"
@.str.1 = [18 x i8] c"Timeout Reached: \00"
@.str.2 = [39 x i8] c"sigtimedwait() returned for signal %d\0A\00"
@.str.3 = [21 x i8] c"after sigtimedwait()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.itimerval
%3 = alloca %struct.TYPE_7__
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.itimerval* %2 to i8*
%6 = bitcast %struct.TYPE_7__* %3 to i8*
%7 = call i32 @sigemptyset(i32* %1)
%8 = load i32, i32* @SIGALRM
%9 = call i32 @sigaddset(i32* %1, i32 %8)
%10 = load i32, i32* @SIG_BLOCK
%11 = call i32 @sigprocmask(i32 %10, i32* %1, i32* null)
%12 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 1, i32 0
store i32 5, i32* %12
%13 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 1, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 0
store i32 5, i32* %14
%15 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* @ITIMER_REAL
%17 = call i32 @setitimer(i32 %16, %struct.itimerval* %2, i32* null)
%18 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_7__* %3)
%20 = icmp slt i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %0
%22 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %3, i64 0, i32 0
br label %26
23: ; preds = %26, %0
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %21, %26
%27 = load i32, i32* %22
%28 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_7__* %3)
%31 = icmp slt i32 %30, 0
br i1 %31, label %23, label %26
}