#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_mask;
  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*/ _exit(int);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *);
scalar_t__ fork();
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*/ *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int system(const char *cmdstring) {
  pid_t pid;
  int status;
  struct sigaction ignore, saveintr, savequit;
  sigset_t childmask, savemask;
  if (cmdstring == NULL)
    return (1);
  ignore.sa_handler = SIG_IGN;
  sigemptyset(&ignore.sa_mask);
  ignore.sa_flags = 0;
  if (sigaction(SIGINT, &ignore, &saveintr) < 0)
    return (-1);
  if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
    return (-1);
  sigemptyset(&childmask);
  sigaddset(&childmask, SIGCHLD);
  if (sigprocmask(SIG_BLOCK, &childmask, &savemask) < 0)
    return (-1);
  if ((pid = fork()) < 0) {
    status = -1;
  } else if (pid == 0) {
    sigaction(SIGINT, &saveintr, NULL);
    sigaction(SIGQUIT, &savequit, NULL);
    sigprocmask(SIG_SETMASK, &savemask, NULL);
    execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
    _exit(127);
  } else {
    while (waitpid(pid, &status, 0) < 0)
      if (errno != EINTR) {
        status = -1;
        break;
      }
  }
  if (sigaction(SIGINT, &saveintr, NULL) < 0)
    return (-1);
  if (sigaction(SIGQUIT, &savequit, NULL) < 0)
    return (-1);
  if (sigprocmask(SIG_SETMASK, &savemask, NULL) == 0)
    return (-1);
  return (status);
}


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x30(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    11f0 <system+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    121c <system+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
lea    -0x54(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0x54(%rbp),%rdi
call   1090 <sigaddset@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    -0x54(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    1263 <system+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1284 <system+0x104>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1351 <system+0x1d1>
cmpq   $0x0,-0x18(%rbp)
jne    12fe <system+0x17e>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rcx
lea    0xd24(%rip),%rdi
lea    0xd22(%rip),%rsi
lea    0xd1e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
jmp    1303 <system+0x183>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jge    1347 <system+0x1c7>
mov    0x2c95(%rip),%rax
mov    (%rax),%rax
mov    0x2c7b(%rip),%rcx
cmp    (%rcx),%rax
je     1342 <system+0x1c2>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1347 <system+0x1c7>
jmp    1303 <system+0x183>
jmp    134c <system+0x1cc>
jmp    1351 <system+0x1d1>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    137d <system+0x1fd>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    13a9 <system+0x229>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jne    13d5 <system+0x255>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <system+0x25b>
mov    -0x1c(%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     12e4 <system+0x164>
mov    %rdi,%rbx
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2e27(%rip),%r12
mov    (%r12),%edi
lea    0x10(%rsp),%rsi
lea    0x38(%rsp),%rdx
call   1050 <sigaction@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     12ea <system+0x16a>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
lea    0x28(%rsp),%rdx
call   1050 <sigaction@plt>
test   %rax,%rax
js     12ea <system+0x16a>
lea    0x24(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <sigemptyset@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <sigaddset@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     12ea <system+0x16a>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     128a <system+0x10a>
je     12fc <system+0x17c>
mov    %rax,%rbp
lea    0x8(%rsp),%r13
mov    0x2d56(%rip),%rbx
mov    0x2d3f(%rip),%r15
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
jns    1292 <system+0x112>
mov    (%rbx),%rax
cmp    (%r15),%rax
je     1270 <system+0xf0>
movl   $0xffffffff,0x8(%rsp)
ff
mov    (%r12),%edi
lea    0x38(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   %rax,%rax
js     12ea <system+0x16a>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   %rax,%rax
js     12ea <system+0x16a>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0x1,%rax
sbb    %r14d,%r14d
or     0x8(%rsp),%r14d
jmp    12ea <system+0x16a>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edi
lea    0x38(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xcc3(%rip),%rdi
lea    0xcc1(%rip),%rsi
lea    0xcbd(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

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

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