#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 */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int SIGTERM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigwait(int /*<<< orphan*/ *, int *);
void *gestore_segnali(void *arg) {
  sigset_t set;
  int sig;
  sigemptyset(&set);
  sigaddset(&set, SIGTERM);
  while (1) {
    if (sigwait(&set, &sig) > 0) {
      perror("servr: invalid set in sigwait");
      exit(EINVAL);
    }
    if (sig == SIGTERM) {
      exit(EXIT_SUCCESS);
    } else {
      perror("Server: sigwait wrong signal");
      exit(EXIT_FAILURE);
    }
  }
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<gestore_segnali>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <sigemptyset@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1070 <sigaddset@plt>
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <sigwait@plt>
cmp    $0x0,%rax
jle    11a8 <gestore_segnali+0x58>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%eax
jne    11c8 <gestore_segnali+0x78>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe4f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<gestore_segnali>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <sigemptyset@plt>
mov    0x2e80(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
call   1070 <sigaddset@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <sigwait@plt>
test   %rax,%rax
jle    119d <gestore_segnali+0x4d>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    (%rsp),%eax
cmp    (%r14),%eax
jne    11b3 <gestore_segnali+0x63>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe64(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@SIGTERM = global i32 0
@.str = [30 x i8] c"servr: invalid set in sigwait\00"
@EINVAL = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [29 x i8] c"Server: sigwait wrong signal\00"
@EXIT_FAILURE = global i32 0
define i8* @gestore_segnali(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i32 @sigemptyset(i32* %3)
%6 = load i32, i32* @SIGTERM
%7 = call i32 @sigaddset(i32* %3, i32 %6)
br label %8
8: ; preds = %1
%9 = call i64 @sigwait(i32* %3, i32* %4)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EINVAL
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = load i32, i32* %4
%17 = load i32, i32* @SIGTERM
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
}

LLVM IR (-O2):
@SIGTERM = global i32 0
@.str = [30 x i8] c"servr: invalid set in sigwait\00"
@EINVAL = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [29 x i8] c"Server: sigwait wrong signal\00"
@EXIT_FAILURE = global i32 0
define noalias i8* @gestore_segnali(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sigemptyset(i32* %2)
%7 = load i32, i32* @SIGTERM
%8 = call i32 @sigaddset(i32* %2, i32 %7)
%9 = call i64 @sigwait(i32* %2, i32* %3)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EINVAL
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i32, i32* @SIGTERM
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
}