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

/* Variables and functions */
int /*<<< orphan*/ LogErrno(char *);
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_SETMASK;
int errno;
scalar_t__ fork();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
pid_t ForkWithoutSigHandlers(void) {
  sigset_t oldset, set;
  sigemptyset(&set);
  sigaddset(&set, SIGUSR1);
  sigaddset(&set, SIGTERM);
  sigaddset(&set, SIGCHLD);
  sigemptyset(&oldset);
  if (sigprocmask(SIG_BLOCK, &set, &oldset)) {
    LogErrno("Unable to block signals");
  }
  pid_t pid = fork();
  int fork_errno = errno;
  if (pid == 0) {
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = SIG_DFL;
    if (sigaction(SIGUSR1, &sa, NULL)) {
      LogErrno("sigaction(SIGUSR1)");
    }
    if (sigaction(SIGTERM, &sa, NULL)) {
      LogErrno("sigaction(SIGTERM)");
    }
    if (sigaction(SIGCHLD, &sa, NULL)) {
      LogErrno("sigaction(SIGCHLD)");
    }
  }
  if (sigprocmask(SIG_SETMASK, &oldset, NULL)) {
    LogErrno("Unable to restore signal mask");
  }
  errno = fork_errno;
  return pid;
}


x86 assembly (clang, -O0):
<ForkWithoutSigHandlers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1070 <sigaddset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1070 <sigaddset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1070 <sigaddset@plt>
lea    -0x4(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
lea    -0x4(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
je     11dc <ForkWithoutSigHandlers+0x7c>
lea    0xe29(%rip),%rdi
call   1050 <LogErrno@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12a8 <ForkWithoutSigHandlers+0x148>
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x28(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
je     124b <ForkWithoutSigHandlers+0xeb>
lea    0xdd2(%rip),%rdi
call   1050 <LogErrno@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
je     1277 <ForkWithoutSigHandlers+0x117>
lea    0xdb9(%rip),%rdi
call   1050 <LogErrno@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
je     12a3 <ForkWithoutSigHandlers+0x143>
lea    0xda0(%rip),%rdi
call   1050 <LogErrno@plt>
jmp    12a8 <ForkWithoutSigHandlers+0x148>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
lea    -0x4(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
je     12d4 <ForkWithoutSigHandlers+0x174>
lea    0xd82(%rip),%rdi
call   1050 <LogErrno@plt>
mov    -0x14(%rbp),%ecx
mov    0x2ce2(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ForkWithoutSigHandlers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x24(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigemptyset@plt>
mov    0x2e66(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <sigaddset@plt>
mov    0x2e4b(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdi
call   1070 <sigaddset@plt>
mov    0x2e28(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1070 <sigaddset@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1060 <sigemptyset@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <sigprocmask@plt>
test   %rax,%rax
je     11e5 <ForkWithoutSigHandlers+0x85>
lea    0xe20(%rip),%rdi
call   1050 <LogErrno@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    0x2dca(%rip),%r14
mov    (%r14),%ebp
test   %rax,%rax
jne    1284 <ForkWithoutSigHandlers+0x124>
lea    0x18(%rsp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%r13),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
je     1243 <ForkWithoutSigHandlers+0xe3>
lea    0xdda(%rip),%rdi
call   1050 <LogErrno@plt>
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
je     1264 <ForkWithoutSigHandlers+0x104>
lea    0xdcc(%rip),%rdi
call   1050 <LogErrno@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
je     1284 <ForkWithoutSigHandlers+0x124>
lea    0xdbf(%rip),%rdi
call   1050 <LogErrno@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %rax,%rax
je     12aa <ForkWithoutSigHandlers+0x14a>
lea    0xdac(%rip),%rdi
call   1050 <LogErrno@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@SIGUSR1 = global i32 0
@SIGTERM = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [24 x i8] c"Unable to block signals\00"
@errno = global i32 0
@SIG_DFL = global i32 0
@.str.1 = [19 x i8] c"sigaction(SIGUSR1)\00"
@.str.2 = [19 x i8] c"sigaction(SIGTERM)\00"
@.str.3 = [19 x i8] c"sigaction(SIGCHLD)\00"
@SIG_SETMASK = global i32 0
@.str.4 = [30 x i8] c"Unable to restore signal mask\00"
define i64 @ForkWithoutSigHandlers() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca %struct.sigaction
%6 = call i32 @sigemptyset(i32* %2)
%7 = load i32, i32* @SIGUSR1
%8 = call i32 @sigaddset(i32* %2, i32 %7)
%9 = load i32, i32* @SIGTERM
%10 = call i32 @sigaddset(i32* %2, i32 %9)
%11 = load i32, i32* @SIGCHLD
%12 = call i32 @sigaddset(i32* %2, i32 %11)
%13 = call i32 @sigemptyset(i32* %1)
%14 = load i32, i32* @SIG_BLOCK
%15 = call i64 @sigprocmask(i32 %14, i32* %2, i32* %1)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @LogErrno(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = call i64 (...) @fork()
store i64 %20, i64* %3
%21 = load i32, i32* @errno
store i32 %21, i32* %4
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %48
24: ; preds = %19
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 2
%26 = call i32 @sigemptyset(i32* %25)
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 1
store i64 0, i64* %27
%28 = load i32, i32* @SIG_DFL
%29 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @SIGUSR1
%31 = call i64 @sigaction(i32 %30, %struct.sigaction* %5, i32* null)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %24
%36 = load i32, i32* @SIGTERM
%37 = call i64 @sigaction(i32 %36, %struct.sigaction* %5, i32* null)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* @SIGCHLD
%43 = call i64 @sigaction(i32 %42, %struct.sigaction* %5, i32* null)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
br label %48
48: ; preds = %47, %19
%49 = load i32, i32* @SIG_SETMASK
%50 = call i64 @sigprocmask(i32 %49, i32* %1, i32* null)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @LogErrno(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* %4
store i32 %55, i32* @errno
%56 = load i64, i64* %3
ret i64 %56
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@SIGUSR1 = global i32 0
@SIGTERM = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [24 x i8] c"Unable to block signals\00"
@errno = global i32 0
@SIG_DFL = global i32 0
@.str.1 = [19 x i8] c"sigaction(SIGUSR1)\00"
@.str.2 = [19 x i8] c"sigaction(SIGTERM)\00"
@.str.3 = [19 x i8] c"sigaction(SIGCHLD)\00"
@SIG_SETMASK = global i32 0
@.str.4 = [30 x i8] c"Unable to restore signal mask\00"
define i64 @ForkWithoutSigHandlers() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sigaction
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = call i32 @sigemptyset(i32* %2)
%7 = load i32, i32* @SIGUSR1
%8 = call i32 @sigaddset(i32* %2, i32 %7)
%9 = load i32, i32* @SIGTERM
%10 = call i32 @sigaddset(i32* %2, i32 %9)
%11 = load i32, i32* @SIGCHLD
%12 = call i32 @sigaddset(i32* %2, i32 %11)
%13 = call i32 @sigemptyset(i32* %1)
%14 = load i32, i32* @SIG_BLOCK
%15 = call i64 @sigprocmask(i32 %14, i32* %2, i32* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = call i32 @LogErrno(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = call i64 (...) @fork()
%21 = load i32, i32* @errno
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %48
23: ; preds = %19
%24 = bitcast %struct.sigaction* %3 to i8*
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%26 = call i32 @sigemptyset(i32* %25)
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %27
%28 = load i32, i32* @SIG_DFL
%29 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @SIGUSR1
%31 = call i64 @sigaction(i32 %30, %struct.sigaction* %3, i32* null)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %23
%36 = load i32, i32* @SIGTERM
%37 = call i64 @sigaction(i32 %36, %struct.sigaction* %3, i32* null)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* @SIGCHLD
%43 = call i64 @sigaction(i32 %42, %struct.sigaction* %3, i32* null)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
br label %48
48: ; preds = %47, %19
%49 = load i32, i32* @SIG_SETMASK
%50 = call i64 @sigprocmask(i32 %49, i32* %1, i32* null)
%51 = icmp eq i64 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %48
%53 = call i32 @LogErrno(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
store i32 %21, i32* @errno
ret i64 %20
}