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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ _Exit(int);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             struct sigaction *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int system(const char *command) {
  if (!command) {
    return access("/bin/sh", X_OK) == 0;
  }
  struct sigaction ignore;
  ignore.sa_handler = SIG_IGN;
  ignore.sa_flags = 0;
  struct sigaction sigint;
  struct sigaction sigquit;
  sigaction(SIGINT, &ignore, &sigint);
  sigaction(SIGQUIT, &ignore, &sigquit);
  sigset_t maskSigchld;
  sigemptyset(&maskSigchld);
  sigaddset(&maskSigchld, SIGCHLD);
  sigset_t oldMask;
  sigprocmask(SIG_BLOCK, &maskSigchld, &oldMask);
  int status;
  pid_t pid = fork();
  if (pid < 0) {
    status = -1;
  } else if (pid == 0) {
    sigaction(SIGINT, &sigint, NULL);
    sigaction(SIGQUIT, &sigquit, NULL);
    sigprocmask(SIG_SETMASK, &oldMask, NULL);
    execl("/bin/sh", "sh", "-c", command, NULL);
    _Exit(127);
  } else {
    while (waitpid(pid, &status, 0) < 0) {
      if (errno != EINTR) {
        status = -1;
        break;
      }
    }
  }
  sigaction(SIGINT, &sigint, NULL);
  sigaction(SIGQUIT, &sigquit, NULL);
  sigprocmask(SIG_SETMASK, &oldMask, NULL);
  return status;
}


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d0 <system+0x40>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1070 <access@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    1378 <system+0x1e8>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <sigaction@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <sigaction@plt>
lea    -0x44(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    -0x44(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    -0x44(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    1263 <system+0xd3>
movl   $0xffffffff,-0x4c(%rbp)
jmp    1330 <system+0x1a0>
cmpq   $0x0,-0x58(%rbp)
jne    12dd <system+0x14d>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rcx
lea    0xd45(%rip),%rdi
lea    0xd43(%rip),%rsi
lea    0xd3f(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <execl@plt>
mov    $0x7f,%edi
call   1080 <_Exit@plt>
jmp    12e2 <system+0x152>
mov    -0x58(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    1326 <system+0x196>
mov    0x2cae(%rip),%rax
mov    (%rax),%rax
mov    0x2c94(%rip),%rcx
cmp    (%rcx),%rax
je     1321 <system+0x191>
movl   $0xffffffff,-0x4c(%rbp)
jmp    1326 <system+0x196>
jmp    12e2 <system+0x152>
jmp    132b <system+0x19b>
jmp    1330 <system+0x1a0>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
test   %rdi,%rdi
je     12ae <system+0x11e>
mov    %rdi,%r14
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
movq   $0x0,0x38(%rsp)
mov    0x2e21(%rip),%r12
mov    (%r12),%edi
lea    0x38(%rsp),%rbx
lea    0x28(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <sigaction@plt>
mov    0x2dfc(%rip),%r15
mov    (%r15),%edi
lea    0x18(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <sigaction@plt>
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigemptyset@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <sigaddset@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     126c <system+0xdc>
je     12dd <system+0x14d>
mov    %rax,%rbx
lea    0xc(%rsp),%r14
mov    0x2d67(%rip),%r13
mov    0x2d50(%rip),%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    1274 <system+0xe4>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1250 <system+0xc0>
movl   $0xffffffff,0xc(%rsp)
ff
mov    (%r12),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    (%r15),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0xc(%rsp),%eax
jmp    12ce <system+0x13e>
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    0xd42(%rip),%rdi
call   1070 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    (%r15),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xce8(%rip),%rdi
lea    0xce6(%rip),%rsi
lea    0xce2(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1090 <execl@plt>
mov    $0x7f,%edi
call   1080 <_Exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i64, i32 }
@.str = [8 x i8] c"/bin/sh\00"
@X_OK = global i32 0
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIGQUIT = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sigaction
%5 = alloca %struct.sigaction
%6 = alloca %struct.sigaction
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %1
%14 = load i32, i32* @X_OK
%15 = call i64 @access(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i64 %15, 0
%17 = zext i1 %16 to i32
store i32 %17, i32* %2
br label %69
18: ; preds = %1
%19 = load i32, i32* @SIG_IGN
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i32 0, i32 0
store i64 0, i64* %21
%22 = load i32, i32* @SIGINT
%23 = call i32 @sigaction(i32 %22, %struct.sigaction* %4, %struct.sigaction* %5)
%24 = load i32, i32* @SIGQUIT
%25 = call i32 @sigaction(i32 %24, %struct.sigaction* %4, %struct.sigaction* %6)
%26 = call i32 @sigemptyset(i32* %7)
%27 = load i32, i32* @SIGCHLD
%28 = call i32 @sigaddset(i32* %7, i32 %27)
%29 = load i32, i32* @SIG_BLOCK
%30 = call i32 @sigprocmask(i32 %29, i32* %7, i32* %8)
%31 = call i64 (...) @fork()
store i64 %31, i64* %10
%32 = load i64, i64* %10
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %18
store i32 -1, i32* %9
br label %61
35: ; preds = %18
%36 = load i64, i64* %10
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %35
%39 = load i32, i32* @SIGINT
%40 = call i32 @sigaction(i32 %39, %struct.sigaction* %5, %struct.sigaction* null)
%41 = load i32, i32* @SIGQUIT
%42 = call i32 @sigaction(i32 %41, %struct.sigaction* %6, %struct.sigaction* null)
%43 = load i32, i32* @SIG_SETMASK
%44 = call i32 @sigprocmask(i32 %43, i32* %8, i32* null)
%45 = load i8*, i8** %3
%46 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32* null)
%47 = call i32 @_Exit(i32 127)
unreachable
48: ; preds = %35
br label %49
49: ; preds = %58, %48
%50 = load i64, i64* %10
%51 = call i64 @waitpid(i64 %50, i32* %9, i32 0)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = load i64, i64* @errno
%55 = load i64, i64* @EINTR
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 -1, i32* %9
br label %59
58: ; preds = %53
br label %49
59: ; preds = %57, %49
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60, %34
%62 = load i32, i32* @SIGINT
%63 = call i32 @sigaction(i32 %62, %struct.sigaction* %5, %struct.sigaction* null)
%64 = load i32, i32* @SIGQUIT
%65 = call i32 @sigaction(i32 %64, %struct.sigaction* %6, %struct.sigaction* null)
%66 = load i32, i32* @SIG_SETMASK
%67 = call i32 @sigprocmask(i32 %66, i32* %8, i32* null)
%68 = load i32, i32* %9
store i32 %68, i32* %2
br label %69
69: ; preds = %61, %13
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i32 }
@.str = [8 x i8] c"/bin/sh\00"
@X_OK = global i32 0
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIGQUIT = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @system(i8* %0) {
%2 = alloca %struct.sigaction
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @X_OK
%11 = tail call i64 @access(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
br label %63
14: ; preds = %1
%15 = bitcast %struct.sigaction* %2 to i8*
%16 = load i32, i32* @SIG_IGN
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 0
store i64 0, i64* %18
%19 = bitcast %struct.sigaction* %3 to i8*
%20 = bitcast %struct.sigaction* %4 to i8*
%21 = load i32, i32* @SIGINT
%22 = call i32 @sigaction(i32 %21, %struct.sigaction* %2, %struct.sigaction* %3)
%23 = load i32, i32* @SIGQUIT
%24 = call i32 @sigaction(i32 %23, %struct.sigaction* %2, %struct.sigaction* %4)
%25 = bitcast i32* %5 to i8*
%26 = call i32 @sigemptyset(i32* %5)
%27 = load i32, i32* @SIGCHLD
%28 = call i32 @sigaddset(i32* %5, i32 %27)
%29 = bitcast i32* %6 to i8*
%30 = load i32, i32* @SIG_BLOCK
%31 = call i32 @sigprocmask(i32 %30, i32* %5, i32* %6)
%32 = bitcast i32* %7 to i8*
%33 = call i64 (...) @fork()
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %14
store i32 -1, i32* %7
br label %55
36: ; preds = %14
%37 = icmp eq i64 %33, 0
br i1 %37, label %38, label %47
38: ; preds = %36
%39 = load i32, i32* @SIGINT
%40 = call i32 @sigaction(i32 %39, %struct.sigaction* %3, %struct.sigaction* null)
%41 = load i32, i32* @SIGQUIT
%42 = call i32 @sigaction(i32 %41, %struct.sigaction* %4, %struct.sigaction* null)
%43 = load i32, i32* @SIG_SETMASK
%44 = call i32 @sigprocmask(i32 %43, i32* %6, i32* null)
%45 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%46 = call i32 @_Exit(i32 127)
unreachable
47: ; preds = %36, %50
%48 = call i64 @waitpid(i64 %33, i32* %7, i32 0)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br i1 %53, label %47, label %54
54: ; preds = %50
store i32 -1, i32* %7
br label %55
55: ; preds = %47, %54, %35
%56 = load i32, i32* @SIGINT
%57 = call i32 @sigaction(i32 %56, %struct.sigaction* %3, %struct.sigaction* null)
%58 = load i32, i32* @SIGQUIT
%59 = call i32 @sigaction(i32 %58, %struct.sigaction* %4, %struct.sigaction* null)
%60 = load i32, i32* @SIG_SETMASK
%61 = call i32 @sigprocmask(i32 %60, i32* %6, i32* null)
%62 = load i32, i32* %7
br label %63
63: ; preds = %55, %9
%64 = phi i32 [ %62, %55 ], [ %13, %9 ]
ret i32 %64
}