#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 */
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ 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*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigwait(int /*<<< orphan*/ *, int *);
char *strerror(int);
void *sigmgr_thread(void *argc) {
  ;
  sigset_t waitset;
  int sig;
  int rc;
  pthread_t ppid = pthread_self();
  pthread_detach(ppid);
  sigemptyset(&waitset);
  sigaddset(&waitset, SIGUSR1);
  sigaddset(&waitset, SIGUSR2);
  sigaddset(&waitset, SIGTERM);
  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    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <pthread_detach@plt>
lea    -0xc(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10a0 <sigaddset@plt>
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <sigwait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1204 <sigmgr_thread+0x84>
mov    -0x10(%rbp),%edi
call   1030 <sig_handler@plt>
jmp    1230 <sigmgr_thread+0xb0>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rdx
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
jmp    11dd <sigmgr_thread+0x5d>

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    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
lea    0x8(%rsp),%rbx
mov    0x2de2(%rip),%r12
lea    0xe13(%rip),%r15
jmp    11f9 <sigmgr_thread+0x79>
nop
mov    0x8(%rsp),%edi
call   1030 <sig_handler@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <sigwait@plt>
cmp    $0xffffffff,%eax
jne    11f0 <sigmgr_thread+0x70>
mov    (%r12),%ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
jmp    11f9 <sigmgr_thread+0x79>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@SIGTERM = global i32 0
@.str = [36 x i8] c"sigwaitinfo() returned err: %d; %s\0A\00"
@errno = global i32 0
define i8* @sigmgr_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i32 (...) @pthread_self()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = call i32 @pthread_detach(i32 %8)
%10 = call i32 @sigemptyset(i32* %3)
%11 = load i32, i32* @SIGUSR1
%12 = call i32 @sigaddset(i32* %3, i32 %11)
%13 = load i32, i32* @SIGUSR2
%14 = call i32 @sigaddset(i32* %3, i32 %13)
%15 = load i32, i32* @SIGTERM
%16 = call i32 @sigaddset(i32* %3, i32 %15)
br label %17
17: ; preds = %1, %29
%18 = call i32 @sigwait(i32* %3, i32* %4)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @sig_handler(i32 %22)
br label %29
24: ; preds = %17
%25 = load i32, i32* @errno
%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 %25, i8* %27)
br label %29
29: ; preds = %24, %21
br label %17
}

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