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


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1340 <system+0x1c0>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x30(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1050 <sigaction@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
lea    -0x68(%rbp),%rdx
call   1050 <sigaction@plt>
lea    -0x38(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   1090 <sigaddset@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     12b0 <system+0x130>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x68(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12a4 <system+0x124>
mov    -0x10(%rbp),%rcx
lea    0xd7e(%rip),%rdi
lea    0xd7c(%rip),%rsi
lea    0xd78(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <system+0x1c0>
jmp    12b5 <system+0x135>
mov    -0x18(%rbp),%rdi
lea    -0x6c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12f8 <system+0x178>
mov    0x2ce4(%rip),%rax
mov    (%rax),%rax
mov    0x2cca(%rip),%rcx
cmp    (%rcx),%rax
je     12f3 <system+0x173>
movl   $0xffffffff,-0x6c(%rbp)
jmp    12f8 <system+0x178>
jmp    12b5 <system+0x135>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    -0x68(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
test   %rdi,%rdi
je     129e <system+0x11e>
mov    %rdi,%r14
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x50(%rsp)
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x48(%rsp)
mov    0x2e24(%rip),%r12
mov    (%r12),%edi
lea    0x28(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <sigaction@plt>
mov    0x2e04(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <sigaction@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <sigaddset@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    12a5 <system+0x125>
lea    0x4(%rsp),%r14
mov    0x2d84(%rip),%r13
mov    0x2d6d(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1264 <system+0xe4>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1240 <system+0xc0>
movl   $0xffffffff,0x4(%rsp)
ff
mov    (%r12),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x4(%rsp),%eax
jmp    12e3 <system+0x163>
mov    $0x1,%eax
jmp    12e3 <system+0x163>
mov    (%r12),%edi
lea    0x28(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     12f2 <system+0x172>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd07(%rip),%rdi
lea    0xd05(%rip),%rsi
lea    0xd01(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

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

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