#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;
typedef int /*<<< orphan*/ pthread_t;

/* Variables and functions */
scalar_t__ SIGRTMAX;
scalar_t__ SIGRTMIN;
scalar_t__ SIGUSR1;
scalar_t__ SIGUSR2;
int errno;
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ sig_handler(int);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigwait(int /*<<< orphan*/ *, int *);
char *strerror(int);
void *sigmgr_thread() {
  sigset_t waitset, oset;
  int sig;
  int rc;
  pthread_t ppid = pthread_self();
  pthread_detach(ppid);
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGRTMIN);
  sigaddset(&waitset, SIGRTMIN + 2);
  sigaddset(&waitset, SIGRTMAX);
  sigaddset(&waitset, SIGUSR1);
  sigaddset(&waitset, SIGUSR2);
  while (1) {
    rc = sigwait(&waitset, &sig);
    if (rc != -1) {
      sig_handler(sig);
    } else {
      printf("sigwaitinfo() returned err: %d; %s\n", errno, strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<sigmgr_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <pthread_detach@plt>
lea    -0x4(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rsi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rsi
add    $0x2,%rsi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%rsi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%rsi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rsi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
lea    -0x4(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1080 <sigwait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     122d <sigmgr_thread+0xad>
mov    -0xc(%rbp),%edi
call   1030 <sig_handler@plt>
jmp    1259 <sigmgr_thread+0xd9>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%esi
mov    %rax,%rdx
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
jmp    1206 <sigmgr_thread+0x86>

x86 assembly (clang, -O2):
<sigmgr_thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %eax,%edi
call   1060 <pthread_detach@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e1a(%rip),%rbx
mov    (%rbx),%rsi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    (%rbx),%rsi
add    $0x2,%rsi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
lea    0x8(%rsp),%rbx
mov    0x2db6(%rip),%r12
lea    0xdef(%rip),%r15
jmp    1229 <sigmgr_thread+0xa9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%edi
call   1030 <sig_handler@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <sigwait@plt>
cmp    $0xffffffff,%eax
jne    1220 <sigmgr_thread+0xa0>
mov    (%r12),%ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
jmp    1229 <sigmgr_thread+0xa9>

LLVM IR (-O0):
@SIGRTMIN = global i64 0
@SIGRTMAX = global i64 0
@SIGUSR1 = global i64 0
@SIGUSR2 = global i64 0
@.str = [36 x i8] c"sigwaitinfo() returned err: %d; %s\0A\00"
@errno = global i32 0
define i8* @sigmgr_thread() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32 (...) @pthread_self()
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = call i32 @pthread_detach(i32 %7)
%9 = call i32 @sigemptyset(i32* %1)
%10 = load i64, i64* @SIGRTMIN
%11 = call i32 @sigaddset(i32* %1, i64 %10)
%12 = load i64, i64* @SIGRTMIN
%13 = add nsw i64 %12, 2
%14 = call i32 @sigaddset(i32* %1, i64 %13)
%15 = load i64, i64* @SIGRTMAX
%16 = call i32 @sigaddset(i32* %1, i64 %15)
%17 = load i64, i64* @SIGUSR1
%18 = call i32 @sigaddset(i32* %1, i64 %17)
%19 = load i64, i64* @SIGUSR2
%20 = call i32 @sigaddset(i32* %1, i64 %19)
br label %21
21: ; preds = %0, %33
%22 = call i32 @sigwait(i32* %1, i32* %3)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %3
%27 = call i32 @sig_handler(i32 %26)
br label %33
28: ; preds = %21
%29 = load i32, i32* @errno
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %29, i8* %31)
br label %33
33: ; preds = %28, %25
br label %21
}

LLVM IR (-O2):
@SIGRTMIN = global i64 0
@SIGRTMAX = global i64 0
@SIGUSR1 = global i64 0
@SIGUSR2 = global i64 0
@.str = [36 x i8] c"sigwaitinfo() returned err: %d; %s\0A\00"
@errno = global i32 0
define noalias i8* @sigmgr_thread() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @pthread_self()
%6 = tail call i32 @pthread_detach(i32 %5)
%7 = call i32 @sigemptyset(i32* %1)
%8 = load i64, i64* @SIGRTMIN
%9 = call i32 @sigaddset(i32* %1, i64 %8)
%10 = load i64, i64* @SIGRTMIN
%11 = add nsw i64 %10, 2
%12 = call i32 @sigaddset(i32* %1, i64 %11)
%13 = load i64, i64* @SIGRTMAX
%14 = call i32 @sigaddset(i32* %1, i64 %13)
%15 = load i64, i64* @SIGUSR1
%16 = call i32 @sigaddset(i32* %1, i64 %15)
%17 = load i64, i64* @SIGUSR2
%18 = call i32 @sigaddset(i32* %1, i64 %17)
br label %19
19: ; preds = %29, %0
%20 = call i32 @sigwait(i32* %1, i32* %2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @sig_handler(i32 %23)
br label %29
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %26, i8* %27)
br label %29
29: ; preds = %25, %22
br label %19
}