#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 */
struct signalfd_siginfo {
  scalar_t__ ssi_signo;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ SIGINT;
scalar_t__ SIGQUIT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, struct signalfd_siginfo *, int);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int signalfd(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  sigset_t mask;
  int sfd;
  struct signalfd_siginfo fdsi;
  ssize_t s;
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigaddset(&mask, SIGQUIT);
  if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
    do {
      perror("sigprocmask");
      exit(EXIT_FAILURE);
    } while (0);
  sfd = signalfd(-1, &mask, 0);
  if (sfd == -1)
    do {
      perror("signalfd");
      exit(EXIT_FAILURE);
    } while (0);
  for (;;) {
    s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
    if (s != sizeof(struct signalfd_siginfo))
      do {
        perror("read");
        exit(EXIT_FAILURE);
      } while (0);
    if (fdsi.ssi_signo == SIGINT) {
      printf("Got SIGINT\n");
    } else if (fdsi.ssi_signo == SIGQUIT) {
      printf("Got SIGQUIT\n");
      exit(EXIT_SUCCESS);
    } else {
      printf("Read unexpected signal\n");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <sigemptyset@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%rsi
lea    -0x14(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%rsi
lea    -0x14(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    1203 <main+0x83>
jmp    11e9 <main+0x69>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <signalfd@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123f <main+0xbf>
jmp    1225 <main+0xa5>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1244 <main+0xc4>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x8,%rax
je     1285 <main+0x105>
jmp    126b <main+0xeb>
lea    0xda3(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%rax
jne    12aa <main+0x12a>
lea    0xd7a(%rip),%rdi
call   1040 <printf@plt>
jmp    12e9 <main+0x169>
mov    -0x20(%rbp),%rax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
jne    12d8 <main+0x158>
lea    0xd61(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd54(%rip),%rdi
call   1040 <printf@plt>
jmp    12e9 <main+0x169>
jmp    1244 <main+0xc4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sigemptyset@plt>
mov    0x2e46(%rip),%r15
mov    (%r15),%rsi
mov    %rbx,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e2c(%rip),%r12
mov    (%r12),%rsi
mov    %rbx,%rdi
call   10a0 <sigaddset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    11e4 <main+0x64>
lea    0xe21(%rip),%rdi
jmp    1290 <main+0x110>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1050 <signalfd@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0x86>
lea    0xe0b(%rip),%rdi
jmp    1290 <main+0x110>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x8,%eax
jne    1289 <main+0x109>
lea    0xe18(%rip),%r13
lea    0x10(%rsp),%r14
lea    0xde9(%rip),%rbp
jmp    1259 <main+0xd9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x8,%eax
jne    1289 <main+0x109>
mov    0x10(%rsp),%rax
mov    %r13,%rdi
cmp    (%r15),%rax
je     1240 <main+0xc0>
mov    %rbp,%rdi
cmp    (%r12),%rax
jne    1240 <main+0xc0>
lea    0xdbb(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd85(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.signalfd_siginfo = type { i64 }
@SIGINT = global i64 0
@SIGQUIT = global i64 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"signalfd\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [12 x i8] c"Got SIGINT\0A\00"
@.str.4 = [13 x i8] c"Got SIGQUIT\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [24 x i8] c"Read unexpected signal\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.signalfd_siginfo
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @sigemptyset(i32* %6)
%11 = load i64, i64* @SIGINT
%12 = call i32 @sigaddset(i32* %6, i64 %11)
%13 = load i64, i64* @SIGQUIT
%14 = call i32 @sigaddset(i32* %6, i64 %13)
%15 = load i32, i32* @SIG_BLOCK
%16 = call i32 @sigprocmask(i32 %15, i32* %6, i32* null)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %2
br label %19
19: ; preds = %18
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; No predecessors!
br label %24
24: ; preds = %23, %2
%25 = call i32 @signalfd(i32 -1, i32* %6, i32 0)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %24
br label %29
29: ; preds = %28
%30 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; No predecessors!
br label %34
34: ; preds = %33, %24
br label %35
35: ; preds = %66, %34
%36 = load i32, i32* %7
%37 = call i32 @read(i32 %36, %struct.signalfd_siginfo* %8, i32 8)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = icmp ne i64 %39, 8
br i1 %40, label %41, label %47
41: ; preds = %35
br label %42
42: ; preds = %41
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; No predecessors!
br label %47
47: ; preds = %46, %35
%48 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %8, i32 0, i32 0
%49 = load i64, i64* %48
%50 = load i64, i64* @SIGINT
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %66
54: ; preds = %47
%55 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %8, i32 0, i32 0
%56 = load i64, i64* %55
%57 = load i64, i64* @SIGQUIT
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* @EXIT_SUCCESS
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %54
%64 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %63
br label %66
66: ; preds = %65, %52
br label %35
}

LLVM IR (-O2):
%struct.signalfd_siginfo = type { i64 }
@SIGINT = global i64 0
@SIGQUIT = global i64 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"signalfd\00"
@.str.2 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"Read unexpected signal\00"
@str.6 = [12 x i8] c"Got SIGQUIT\00"
@str.7 = [11 x i8] c"Got SIGINT\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.signalfd_siginfo
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.signalfd_siginfo* %4 to i8*
%7 = call i32 @sigemptyset(i32* %3)
%8 = load i64, i64* @SIGINT
%9 = call i32 @sigaddset(i32* %3, i64 %8)
%10 = load i64, i64* @SIGQUIT
%11 = call i32 @sigaddset(i32* %3, i64 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %3, i32* null)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = call i32 @signalfd(i32 -1, i32* %3, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = call i32 @read(i32 %20, %struct.signalfd_siginfo* %4, i32 8)
%24 = icmp eq i32 %23, 8
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %4, i64 0, i32 0
br label %35
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %46, %22
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25, %46
%36 = load i64, i64* %26
%37 = load i64, i64* @SIGINT
%38 = icmp eq i64 %36, %37
br i1 %38, label %46, label %39
39: ; preds = %35
%40 = load i64, i64* @SIGQUIT
%41 = icmp eq i64 %36, %40
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39, %35
%47 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0), %35 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0), %39 ]
%48 = call i32 @puts(i8* dereferenceable(1) %47)
%49 = call i32 @read(i32 %20, %struct.signalfd_siginfo* %4, i32 8)
%50 = icmp eq i32 %49, 8
br i1 %50, label %35, label %31
}