#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 */

/* Type definitions */
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
scalar_t__ SIGINT;
scalar_t__ SIGRTMIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ sigaddset(int /*<<< orphan*/ *, scalar_t__);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigwait(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait_for_signal(void) {
  int signo;
  sigset_t sigset;
  if (sigemptyset(&sigset)) {
    fprintf(stderr, "creating empty signal wait set: %s\n", strerror(errno));
    return -1;
  }
  if (sigaddset(&sigset, SIGINT)) {
    fprintf(stderr, "adding SIGINT to signal set: %s\n", strerror(errno));
    return -1;
  }
  if (sigaddset(&sigset, (SIGRTMIN + 1))) {
    fprintf(stderr, "adding TIMER_SIGNAL to signal set: %s\n", strerror(errno));
    return -1;
  }
  if (sigwait(&sigset, &signo)) {
    fprintf(stderr, "waiting for signal: %s\n", strerror(errno));
    return -1;
  }
  return signo;
}


x86 assembly (clang, -O0):
<wait_for_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0xc(%rbp),%rdi
call   1040 <sigemptyset@plt>
cmp    $0x0,%rax
je     11a3 <wait_for_signal+0x53>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe6e(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <wait_for_signal+0x156>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rsi
lea    -0xc(%rbp),%rdi
call   1070 <sigaddset@plt>
cmp    $0x0,%rax
je     11f8 <wait_for_signal+0xa8>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe3d(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <wait_for_signal+0x156>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
lea    -0xc(%rbp),%rdi
call   1070 <sigaddset@plt>
cmp    $0x0,%rax
je     1251 <wait_for_signal+0x101>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe05(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <wait_for_signal+0x156>
lea    -0xc(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <sigwait@plt>
cmp    $0x0,%rax
je     12a0 <wait_for_signal+0x150>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xddd(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <wait_for_signal+0x156>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_signal>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1040 <sigemptyset@plt>
test   %rax,%rax
je     1187 <wait_for_signal+0x37>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7e(%rip),%rsi
jmp    122f <wait_for_signal+0xdf>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rsi
lea    0x8(%rsp),%rdi
call   1070 <sigaddset@plt>
test   %rax,%rax
je     11c0 <wait_for_signal+0x70>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe66(%rip),%rsi
jmp    122f <wait_for_signal+0xdf>
mov    0x2e09(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
lea    0x8(%rsp),%rdi
call   1070 <sigaddset@plt>
test   %rax,%rax
je     11fd <wait_for_signal+0xad>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe4a(%rip),%rsi
jmp    122f <wait_for_signal+0xdf>
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1050 <sigwait@plt>
test   %rax,%rax
je     1244 <wait_for_signal+0xf4>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebx
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0xc(%rsp),%eax
jmp    123e <wait_for_signal+0xee>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"creating empty signal wait set: %s\0A\00"
@errno = global i32 0
@SIGINT = global i64 0
@.str.1 = [33 x i8] c"adding SIGINT to signal set: %s\0A\00"
@SIGRTMIN = global i64 0
@.str.2 = [39 x i8] c"adding TIMER_SIGNAL to signal set: %s\0A\00"
@.str.3 = [24 x i8] c"waiting for signal: %s\0A\00"
define i32 @wait_for_signal() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i64 @sigemptyset(i32* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %9)
store i32 -1, i32* %1
br label %40
11: ; preds = %0
%12 = load i64, i64* @SIGINT
%13 = call i64 @sigaddset(i32* %3, i64 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %1
br label %40
20: ; preds = %11
%21 = load i64, i64* @SIGRTMIN
%22 = add nsw i64 %21, 1
%23 = call i64 @sigaddset(i32* %3, i64 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 -1, i32* %1
br label %40
30: ; preds = %20
%31 = call i64 @sigwait(i32* %3, i32* %2)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i32 -1, i32* %1
br label %40
38: ; preds = %30
%39 = load i32, i32* %2
store i32 %39, i32* %1
br label %40
40: ; preds = %38, %33, %25, %15, %6
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"creating empty signal wait set: %s\0A\00"
@errno = global i32 0
@SIGINT = global i64 0
@.str.1 = [33 x i8] c"adding SIGINT to signal set: %s\0A\00"
@SIGRTMIN = global i64 0
@.str.2 = [39 x i8] c"adding TIMER_SIGNAL to signal set: %s\0A\00"
@.str.3 = [24 x i8] c"waiting for signal: %s\0A\00"
define i32 @wait_for_signal() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i64 @sigemptyset(i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %41
12: ; preds = %0
%13 = load i64, i64* @SIGINT
%14 = call i64 @sigaddset(i32* %2, i64 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %41
21: ; preds = %12
%22 = load i64, i64* @SIGRTMIN
%23 = add nsw i64 %22, 1
%24 = call i64 @sigaddset(i32* %2, i64 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %41
31: ; preds = %21
%32 = call i64 @sigwait(i32* %2, i32* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %41
39: ; preds = %31
%40 = load i32, i32* %1
br label %41
41: ; preds = %39, %34, %26, %16, %7
%42 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %26 ], [ -1, %34 ], [ %40, %39 ]
ret i32 %42
}