#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 */
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_3__ {
  int /*<<< orphan*/ si_signo;
};
typedef TYPE_1__ siginfo_t;
typedef int /*<<< orphan*/ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ SIGRTMIN;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
int errno;
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ sig_handler(int /*<<< orphan*/);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigwaitinfo(int /*<<< orphan*/ *, TYPE_1__ *);
char *strerror(int);
void *sigmgr_thread() {
  sigset_t waitset, oset;
  siginfo_t info;
  int rc;
  pthread_t ppid = pthread_self();
  pthread_detach(ppid);
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGRTMIN);
  sigaddset(&waitset, SIGUSR1);
  sigaddset(&waitset, SIGUSR2);
  while (1) {
    rc = sigwaitinfo(&waitset, &info);
    if (rc != -1) {
      printf("sigwaitinfo() fetch the signal - %d\n", rc);
      sig_handler(info.si_signo);
    } 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   1080 <pthread_self@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <pthread_detach@plt>
lea    -0x4(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdi
call   10a0 <sigaddset@plt>
lea    -0x4(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <sigwaitinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1211 <sigmgr_thread+0x91>
mov    -0x14(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1030 <sig_handler@plt>
jmp    123f <sigmgr_thread+0xbf>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rdx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d9 <sigmgr_thread+0x59>

x86 assembly (clang, -O2):
<sigmgr_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %eax,%edi
call   1070 <pthread_detach@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    %rsp,%rbx
mov    0x2de5(%rip),%r13
lea    0xe3b(%rip),%r15
lea    0xe0f(%rip),%r12
jmp    1214 <sigmgr_thread+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%edi
call   1030 <sig_handler@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <sigwaitinfo@plt>
cmp    $0xffffffff,%eax
jne    1200 <sigmgr_thread+0x80>
mov    0x0(%r13),%ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1214 <sigmgr_thread+0x94>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@SIGRTMIN = global i32 0
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@.str = [37 x i8] c"sigwaitinfo() fetch the signal - %d\0A\00"
@.str.1 = [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 %struct.TYPE_3__
%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 i32, i32* @SIGRTMIN
%11 = call i32 @sigaddset(i32* %1, i32 %10)
%12 = load i32, i32* @SIGUSR1
%13 = call i32 @sigaddset(i32* %1, i32 %12)
%14 = load i32, i32* @SIGUSR2
%15 = call i32 @sigaddset(i32* %1, i32 %14)
br label %16
16: ; preds = %0, %31
%17 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_3__* %3)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @sig_handler(i32 %24)
br label %31
26: ; preds = %16
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %29)
br label %31
31: ; preds = %26, %20
br label %16
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@SIGRTMIN = global i32 0
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@.str = [37 x i8] c"sigwaitinfo() fetch the signal - %d\0A\00"
@.str.1 = [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 %struct.TYPE_3__
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.TYPE_3__* %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 i32, i32* @SIGRTMIN
%9 = call i32 @sigaddset(i32* %1, i32 %8)
%10 = load i32, i32* @SIGUSR1
%11 = call i32 @sigaddset(i32* %1, i32 %10)
%12 = load i32, i32* @SIGUSR2
%13 = call i32 @sigaddset(i32* %1, i32 %12)
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 0
br label %15
15: ; preds = %26, %0
%16 = call i32 @sigwaitinfo(i32* %1, %struct.TYPE_3__* %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %16)
%20 = load i32, i32* %14
%21 = call i32 @sig_handler(i32 %20)
br label %26
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %24)
br label %26
26: ; preds = %22, %18
br label %15
}