#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*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     struct sigaction *);
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 *cmd) {
  if (cmd == NULL) {
    return 1;
  }
  struct sigaction ign, saveint, savequit;
  sigset_t block_chld, savemask;
  ign.sa_handler = SIG_IGN;
  sigemptyset(&ign.sa_mask);
  ign.sa_flags = 0;
  if (sigaction(SIGQUIT, &ign, &savequit) < 0) {
    return -1;
  }
  if (sigaction(SIGINT, &ign, &saveint) < 0) {
    sigaction(SIGQUIT, &savequit, NULL);
    return -1;
  }
  sigemptyset(&block_chld);
  sigprocmask(SIG_BLOCK, &block_chld, &savemask);
  int ret;
  pid_t f = fork();
  if (f < 0) {
    ret = -1;
  } else if (f == 0) {
    sigaction(SIGQUIT, &savequit, NULL);
    sigaction(SIGINT, &saveint, NULL);
    sigprocmask(SIG_SETMASK, &savemask, NULL);
    execl("/bin/sh", "sh", "-c", cmd, (char *)0);
    exit(127);
  } else {
    while (waitpid(f, &ret, 0) < 0) {
      if (errno != EINTR) {
        ret = -1;
        break;
      }
    }
  }
  sigaction(SIGINT, &saveint, NULL);
  sigaction(SIGQUIT, &savequit, NULL);
  sigprocmask(SIG_SETMASK, &savemask, NULL);
  return ret;
}


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1378 <system+0x208>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
call   1050 <sigemptyset@plt>
movq   $0x0,-0x20(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    11e0 <system+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <system+0x208>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    1222 <system+0xb2>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <system+0x208>
lea    -0x44(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    -0x44(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    1263 <system+0xf3>
movl   $0xffffffff,-0x4c(%rbp)
jmp    1330 <system+0x1c0>
cmpq   $0x0,-0x58(%rbp)
jne    12dd <system+0x16d>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%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   1080 <execl@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
jmp    12e2 <system+0x172>
mov    -0x58(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    1326 <system+0x1b6>
mov    0x2cbe(%rip),%rax
mov    (%rax),%rax
mov    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
je     1321 <system+0x1b1>
movl   $0xffffffff,-0x4c(%rbp)
jmp    1326 <system+0x1b6>
jmp    12e2 <system+0x172>
jmp    132b <system+0x1bb>
jmp    1330 <system+0x1c0>
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     129c <system+0x12c>
mov    %rdi,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1050 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2e2f(%rip),%r14
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
call   1040 <sigaction@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12b2 <system+0x142>
mov    0x2e10(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
lea    0x38(%rsp),%rdx
call   1040 <sigaction@plt>
test   %rax,%rax
js     12a3 <system+0x133>
lea    0x34(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <sigemptyset@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     125b <system+0xeb>
je     12c3 <system+0x153>
mov    %rax,%rbp
lea    0x8(%rsp),%r12
mov    0x2d89(%rip),%r13
mov    0x2d72(%rip),%rbx
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    1263 <system+0xf3>
mov    0x0(%r13),%rax
cmp    (%rbx),%rax
je     1240 <system+0xd0>
movl   $0xffffffff,0x8(%rsp)
ff
mov    (%r15),%edi
lea    0x38(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x8(%rsp),%ebp
jmp    12b2 <system+0x142>
mov    $0x1,%ebp
jmp    12b2 <system+0x142>
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    (%r15),%edi
lea    0x38(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xd03(%rip),%rdi
lea    0xd01(%rip),%rsi
lea    0xcfd(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

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

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