#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 */
scalar_t__ EINVAL;
int /*<<< orphan*/ SIGABRT;
#define SIG_BLOCK 130
#define SIG_SETMASK 129
#define SIG_UNBLOCK 128
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigprocmask(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  sigset_t set;
  int r, i, fails = 0;
  sigemptyset(&set);
  sigaddset(&set, SIGABRT);
  for (i = 0; i < 100000; i++) {
    r = rand() % (i + 1);
    switch (r) {
    case SIG_BLOCK:
    case SIG_UNBLOCK:
    case SIG_SETMASK:
      continue;
    default:
      break;
    }
    if (sigprocmask(r, &set, NULL) != -1 || errno != EINVAL) {
      printf("sigprocmask(%i, ...) failed to fail\n", r);
      fails++;
    }
  }
  if (fails) {
    printf("Test FAILED\n");
    return 1;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1060 <sigaddset@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x186a0,-0x10(%rbp)
jge    1226 <main+0xd6>
xor    %eax,%eax
call   1070 <rand@plt>
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffff80,%eax
sub    $0x2,%eax
ja     11c1 <main+0x71>
jmp    11bc <main+0x6c>
jmp    1218 <main+0xc8>
jmp    11c6 <main+0x76>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0xa9>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <main+0xc3>
mov    -0xc(%rbp),%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1218 <main+0xc8>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1188 <main+0x38>
cmpl   $0x0,-0x14(%rbp)
je     124a <main+0xfa>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0x10f>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1060 <sigemptyset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <sigaddset@plt>
mov    $0x1,%ebp
mov    0x2e43(%rip),%r13
lea    0xe64(%rip),%r14
xor    %r12d,%r12d
jmp    11cb <main+0x6b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r12d
add    $0x1,%ebp
cmp    $0x186a1,%ebp
je     1202 <main+0xa2>
xor    %eax,%eax
call   1080 <rand@plt>
cltd
idiv   %ebp
mov    %edx,%ebx
lea    -0x80(%rbx),%eax
cmp    $0x3,%eax
jb     11c0 <main+0x60>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x50>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    11b0 <main+0x50>
jmp    11c0 <main+0x60>
xor    %ebx,%ebx
test   %r12d,%r12d
setne  %bl
lea    0xe14(%rip),%rax
lea    0xe19(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGABRT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [37 x i8] c"sigprocmask(%i, ...) failed to fail\0A\00"
@.str.1 = [13 x i8] c"Test FAILED\0A\00"
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%6 = call i32 @sigemptyset(i32* %2)
%7 = load i32, i32* @SIGABRT
%8 = call i32 @sigaddset(i32* %2, i32 %7)
store i32 0, i32* %4
br label %9
9: ; preds = %34, %0
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 100000
br i1 %11, label %12, label %37
12: ; preds = %9
%13 = call i32 (...) @rand()
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
%16 = srem i32 %13, %15
store i32 %16, i32* %3
%17 = load i32, i32* %3
switch i32 %17, label %19 [
i32 130, label %18
i32 128, label %18
i32 129, label %18
]
18: ; preds = %12, %12, %12
br label %34
19: ; preds = %12
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = call i32 @sigprocmask(i32 %21, i32* %2, i32* null)
%23 = icmp ne i32 %22, -1
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINVAL
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24, %20
%29 = load i32, i32* %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %33
33: ; preds = %28, %24
br label %34
34: ; preds = %33, %18
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %9
37: ; preds = %9
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %44
42: ; preds = %37
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %44
44: ; preds = %42, %40
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@SIGABRT = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [37 x i8] c"sigprocmask(%i, ...) failed to fail\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.3 = [12 x i8] c"Test FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @sigemptyset(i32* %1)
%4 = load i32, i32* @SIGABRT
%5 = call i32 @sigaddset(i32* %1, i32 %4)
br label %6
6: ; preds = %0, %24
%7 = phi i32 [ 0, %0 ], [ %25, %24 ]
%8 = phi i32 [ 0, %0 ], [ %10, %24 ]
%9 = call i32 (...) @rand()
%10 = add nuw nsw i32 %8, 1
%11 = srem i32 %9, %10
%12 = add nsw i32 %11, -128
%13 = icmp ult i32 %12, 3
br i1 %13, label %24, label %14
14: ; preds = %6
%15 = call i32 @sigprocmask(i32 %11, i32* %1, i32* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINVAL
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17, %14
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %11)
%23 = add nsw i32 %7, 1
br label %24
24: ; preds = %6, %17, %21
%25 = phi i32 [ %23, %21 ], [ %7, %17 ], [ %7, %6 ]
%26 = icmp eq i32 %10, 100000
br i1 %26, label %27, label %6
27: ; preds = %24
%28 = icmp eq i32 %25, 0
%29 = select i1 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0)
%30 = xor i1 %28, true
%31 = zext i1 %30 to i32
%32 = call i32 @puts(i8* dereferenceable(1) %29)
ret i32 %31
}