#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 sigaction {
  int /*<<< orphan*/ sa_mask;
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_handler;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ getchar();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     struct sigaction *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int main(void) {
  sigset_t block_sigint, prev_mask;
  sigemptyset(&block_sigint);
  sigaddset(&block_sigint, SIGINT);
  if (sigprocmask(SIG_SETMASK, &block_sigint, &prev_mask) < 0) {
    perror("Couldn't block SIGINT");
    return 0;
  }
  printf("SIGINT blocked: kill -SIGINT %ld to generate a pending SIGINT. Press "
         "return when done.\n",
         (long)getpid());
  getchar();
  struct sigaction ign_sigint, prev;
  ign_sigint.sa_handler = SIG_IGN;
  ign_sigint.sa_flags = 0;
  sigemptyset(&ign_sigint.sa_mask);
  if (sigaction(SIGINT, &ign_sigint, &prev) < 0) {
    perror("Couldn't ignore SIGINT");
    return 0;
  }
  printf("SIGINT ignored - pending SIGINT was canceled.\n");
  if (sigaction(SIGINT, &prev, NULL) < 0) {
    perror("Couldn't restore default SIGINT behavior");
    return 0;
  }
  if (sigprocmask(SIG_SETMASK, &prev_mask, NULL) < 0) {
    perror("Couldn't restore original process sigmask");
    return 0;
  }
  printf("Process sigmask and action for SIGINT are now restored\n");
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    11e2 <main+0x62>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x170>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    1256 <main+0xd6>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x170>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    129c <main+0x11c>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x170>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    12d4 <main+0x154>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x170>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    0x2e3b(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   10b0 <sigaddset@plt>
mov    0x2e21(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     1282 <main+0xf2>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe30(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    (%r15),%edi
lea    0x28(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <sigaction@plt>
test   %rax,%rax
js     1290 <main+0x100>
lea    0xea2(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   %rax,%rax
js     129e <main+0x10e>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     12ac <main+0x11c>
lea    0xe9c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd77(%rip),%rdi
call   10a0 <perror@plt>
jmp    1276 <main+0xe6>
lea    0xdd7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1276 <main+0xe6>
lea    0xde0(%rip),%rdi
call   10a0 <perror@plt>
jmp    1276 <main+0xe6>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1276 <main+0xe6>

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@SIGINT = global i32 0
@SIG_SETMASK = global i32 0
@.str = [22 x i8] c"Couldn't block SIGINT\00"
@.str.1 = [88 x i8] c"SIGINT blocked: kill -SIGINT %ld to generate a pending SIGINT. Press return when done.\0A\00"
@SIG_IGN = global i32 0
@.str.2 = [23 x i8] c"Couldn't ignore SIGINT\00"
@.str.3 = [47 x i8] c"SIGINT ignored - pending SIGINT was canceled.\0A\00"
@.str.4 = [41 x i8] c"Couldn't restore default SIGINT behavior\00"
@.str.5 = [42 x i8] c"Couldn't restore original process sigmask\00"
@.str.6 = [56 x i8] c"Process sigmask and action for SIGINT are now restored\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = alloca %struct.sigaction
store i32 0, i32* %1
%6 = call i32 @sigemptyset(i32* %2)
%7 = load i32, i32* @SIGINT
%8 = call i32 @sigaddset(i32* %2, i32 %7)
%9 = load i32, i32* @SIG_SETMASK
%10 = call i64 @sigprocmask(i32 %9, i32* %2, i32* %3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %44
14: ; preds = %0
%15 = call i64 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = call i32 (...) @getchar()
%18 = load i32, i32* @SIG_IGN
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i32 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i32 0, i32 1
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i32 0, i32 0
%22 = call i32 @sigemptyset(i32* %21)
%23 = load i32, i32* @SIGINT
%24 = call i64 @sigaction(i32 %23, %struct.sigaction* %4, %struct.sigaction* %5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %44
28: ; preds = %14
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @SIGINT
%31 = call i64 @sigaction(i32 %30, %struct.sigaction* %5, %struct.sigaction* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %44
35: ; preds = %28
%36 = load i32, i32* @SIG_SETMASK
%37 = call i64 @sigprocmask(i32 %36, i32* %3, i32* null)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %44
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 (...) @getchar()
store i32 0, i32* %1
br label %44
44: ; preds = %41, %39, %33, %26, %12
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@SIGINT = global i32 0
@SIG_SETMASK = global i32 0
@.str = [22 x i8] c"Couldn't block SIGINT\00"
@.str.1 = [88 x i8] c"SIGINT blocked: kill -SIGINT %ld to generate a pending SIGINT. Press return when done.\0A\00"
@SIG_IGN = global i32 0
@.str.2 = [23 x i8] c"Couldn't ignore SIGINT\00"
@.str.4 = [41 x i8] c"Couldn't restore default SIGINT behavior\00"
@.str.5 = [42 x i8] c"Couldn't restore original process sigmask\00"
@str = [46 x i8] c"SIGINT ignored - pending SIGINT was canceled.\00"
@str.7 = [55 x i8] c"Process sigmask and action for SIGINT are now restored\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = call i32 @sigemptyset(i32* %1)
%8 = load i32, i32* @SIGINT
%9 = call i32 @sigaddset(i32* %1, i32 %8)
%10 = load i32, i32* @SIG_SETMASK
%11 = call i64 @sigprocmask(i32 %10, i32* %1, i32* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %48
15: ; preds = %0
%16 = call i64 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = call i32 (...) @getchar()
%19 = bitcast %struct.sigaction* %3 to i8*
%20 = bitcast %struct.sigaction* %4 to i8*
%21 = load i32, i32* @SIG_IGN
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%25 = call i32 @sigemptyset(i32* %24)
%26 = load i32, i32* @SIGINT
%27 = call i64 @sigaction(i32 %26, %struct.sigaction* %3, %struct.sigaction* %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %47
31: ; preds = %15
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%33 = load i32, i32* @SIGINT
%34 = call i64 @sigaction(i32 %33, %struct.sigaction* %4, %struct.sigaction* null)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %47
38: ; preds = %31
%39 = load i32, i32* @SIG_SETMASK
%40 = call i64 @sigprocmask(i32 %39, i32* %2, i32* null)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %47
44: ; preds = %38
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.7, i64 0, i64 0))
%46 = call i32 (...) @getchar()
br label %47
47: ; preds = %44, %42, %36, %29
br label %48
48: ; preds = %47, %13
ret i32 0
}