#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_3__ TYPE_1__;

/* Type definitions */
struct timespec {
  int tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_3__ {
  int si_signo;
};
typedef TYPE_1__ siginfo_t;

/* Variables and functions */
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ exit(int);
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 sigtimedwait(int /*<<< orphan*/ *, TYPE_1__ *, struct timespec *);
int /*<<< orphan*/ timestamp(char *);
int main(int argc, char *argv[]) {
  int result = 0;
  sigset_t waitset;
  siginfo_t info;
  struct timespec timeout;
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGALRM);
  sigprocmask(SIG_BLOCK, &waitset, NULL);
  timeout.tv_sec = 5;
  timeout.tv_nsec = 1000;
  alarm(5);
  timestamp("before sigtimedwait()");
  result = sigtimedwait(&waitset, &info, &timeout);
  if (result < 0) {
    printf("sigtimedwait failed : \n");
    exit(1);
  }
  printf("sigtimedwait returned for signal %d\n", info.si_signo);
  timestamp("after sigtimedwait()");
  return (result);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
movl   $0x5,-0x28(%rbp)
movl   $0x3e8,-0x24(%rbp)
mov    $0x5,%edi
call   1060 <alarm@plt>
lea    0xe13(%rip),%rdi
call   1040 <timestamp@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <sigtimedwait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1228 <main+0xa8>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe13(%rip),%rdi
call   1040 <timestamp@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <sigaddset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
movabs $0x3e800000005,%rax
mov    %rax,0x10(%rsp)
mov    $0x5,%edi
call   1070 <alarm@plt>
lea    0xe1a(%rip),%rdi
call   1050 <timestamp@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <sigtimedwait@plt>
test   %eax,%eax
js     1229 <main+0x99>
mov    %eax,%ebx
mov    0x8(%rsp),%esi
lea    0xe08(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1f(%rip),%rdi
call   1050 <timestamp@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
%struct.timespec = type { i32, i32 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@.str = [22 x i8] c"before sigtimedwait()\00"
@.str.1 = [24 x i8] c"sigtimedwait failed : \0A\00"
@.str.2 = [37 x i8] c"sigtimedwait returned for signal %d\0A\00"
@.str.3 = [21 x i8] c"after sigtimedwait()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.TYPE_3__
%9 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = call i32 @sigemptyset(i32* %7)
%11 = load i32, i32* @SIGALRM
%12 = call i32 @sigaddset(i32* %7, i32 %11)
%13 = load i32, i32* @SIG_BLOCK
%14 = call i32 @sigprocmask(i32 %13, i32* %7, i32* null)
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
store i32 5, i32* %15
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
store i32 1000, i32* %16
%17 = call i32 @alarm(i32 5)
%18 = call i32 @timestamp(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @sigtimedwait(i32* %7, %struct.TYPE_3__* %8, %struct.timespec* %9)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 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 = load i32, i32* %6
ret i32 %30
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
%struct.timespec = type { i32, i32 }
@SIGALRM = global i32 0
@SIG_BLOCK = global i32 0
@.str = [22 x i8] c"before sigtimedwait()\00"
@.str.2 = [37 x i8] c"sigtimedwait returned for signal %d\0A\00"
@.str.3 = [21 x i8] c"after sigtimedwait()\00"
@str = [23 x i8] c"sigtimedwait failed : \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_3__
%5 = alloca %struct.timespec
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.TYPE_3__* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = call i32 @sigemptyset(i32* %3)
%10 = load i32, i32* @SIGALRM
%11 = call i32 @sigaddset(i32* %3, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %3, i32* null)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 5, i32* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i32 1000, i32* %15
%16 = call i32 @alarm(i32 5)
%17 = call i32 @timestamp(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @sigtimedwait(i32* %3, %struct.TYPE_3__* %4, %struct.timespec* %5)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @timestamp(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
ret i32 %18
}