#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 SIGINT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ safe_signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
scalar_t__ sigismember(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
void prepare_child(sigset_t *nset, int infd, int outfd) {
  int i;
  sigset_t fset;
  if (infd >= 0)
    dup2(infd, 0);
  if (outfd >= 0)
    dup2(outfd, 1);
  if (nset) {
    for (i = 1; i < 32; i++)
      if (sigismember(nset, i))
        safe_signal(i, SIG_IGN);
    if (!sigismember(nset, SIGINT))
      safe_signal(SIGINT, SIG_DFL);
  }
  sigfillset(&fset);
  sigprocmask(SIG_UNBLOCK, &fset, (sigset_t *)NULL);
}


x86 assembly (clang, -O0):
<prepare_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1176 <prepare_child+0x26>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmpl   $0x0,-0x10(%rbp)
jl     118d <prepare_child+0x3d>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmpq   $0x0,-0x8(%rbp)
je     121b <prepare_child+0xcb>
movl   $0x1,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jge    11e3 <prepare_child+0x93>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <sigismember@plt>
cmp    $0x0,%rax
je     11d0 <prepare_child+0x80>
mov    -0x14(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
call   1050 <safe_signal@plt>
jmp    11d5 <prepare_child+0x85>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119f <prepare_child+0x4f>
mov    -0x8(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1070 <sigismember@plt>
cmp    $0x0,%rax
jne    1216 <prepare_child+0xc6>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1050 <safe_signal@plt>
jmp    121b <prepare_child+0xcb>
lea    -0x18(%rbp),%rdi
call   1060 <sigfillset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_child>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rdi,%rbx
test   %esi,%esi
js     116a <prepare_child+0x1a>
mov    %esi,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %ebp,%ebp
js     117a <prepare_child+0x2a>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
test   %rbx,%rbx
je     11db <prepare_child+0x8b>
mov    $0x1,%ebp
mov    0x2e4d(%rip),%r14
jmp    1198 <prepare_child+0x48>
nopl   (%rax)
add    $0x1,%ebp
cmp    $0x20,%ebp
je     11b3 <prepare_child+0x63>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <sigismember@plt>
test   %rax,%rax
je     1190 <prepare_child+0x40>
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <safe_signal@plt>
jmp    1190 <prepare_child+0x40>
mov    0x2e26(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1070 <sigismember@plt>
test   %rax,%rax
jne    11db <prepare_child+0x8b>
mov    0x0(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1050 <safe_signal@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigfillset@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIG_DFL = global i32 0
@SIG_UNBLOCK = global i32 0
define void @prepare_child(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* %5
%13 = call i32 @dup2(i32 %12, i32 0)
br label %14
14: ; preds = %11, %3
%15 = load i32, i32* %6
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %6
%19 = call i32 @dup2(i32 %18, i32 1)
br label %20
20: ; preds = %17, %14
%21 = load i32*, i32** %4
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %50
23: ; preds = %20
store i32 1, i32* %7
br label %24
24: ; preds = %37, %23
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 32
br i1 %26, label %27, label %40
27: ; preds = %24
%28 = load i32*, i32** %4
%29 = load i32, i32* %7
%30 = call i64 @sigismember(i32* %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* %7
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @safe_signal(i32 %33, i32 %34)
br label %36
36: ; preds = %32, %27
br label %37
37: ; preds = %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %24
40: ; preds = %24
%41 = load i32*, i32** %4
%42 = load i32, i32* @SIGINT
%43 = call i64 @sigismember(i32* %41, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %40
%46 = load i32, i32* @SIGINT
%47 = load i32, i32* @SIG_DFL
%48 = call i32 @safe_signal(i32 %46, i32 %47)
br label %49
49: ; preds = %45, %40
br label %50
50: ; preds = %49, %20
%51 = call i32 @sigfillset(i32* %8)
%52 = load i32, i32* @SIG_UNBLOCK
%53 = call i32 @sigprocmask(i32 %52, i32* %8, i32* null)
ret void
}

LLVM IR (-O2):
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIG_DFL = global i32 0
@SIG_UNBLOCK = global i32 0
define void @prepare_child(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp sgt i32 %1, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @dup2(i32 %1, i32 0)
br label %9
9: ; preds = %7, %3
%10 = icmp sgt i32 %2, -1
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @dup2(i32 %2, i32 1)
br label %13
13: ; preds = %11, %9
%14 = icmp eq i32* %0, null
br i1 %14, label %33, label %15
15: ; preds = %13, %22
%16 = phi i32 [ %23, %22 ], [ 1, %13 ]
%17 = tail call i64 @sigismember(i32* %0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* @SIG_IGN
%21 = tail call i32 @safe_signal(i32 %16, i32 %20)
br label %22
22: ; preds = %15, %19
%23 = add nuw nsw i32 %16, 1
%24 = icmp eq i32 %23, 32
br i1 %24, label %25, label %15
25: ; preds = %22
%26 = load i32, i32* @SIGINT
%27 = tail call i64 @sigismember(i32* %0, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @SIGINT
%31 = load i32, i32* @SIG_DFL
%32 = tail call i32 @safe_signal(i32 %30, i32 %31)
br label %33
33: ; preds = %25, %29, %13
%34 = call i32 @sigfillset(i32* %4)
%35 = load i32, i32* @SIG_UNBLOCK
%36 = call i32 @sigprocmask(i32 %35, i32* %4, i32* null)
ret void
}