#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 pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _exit(int);
scalar_t__ errno;
int /*<<< orphan*/ execl(char const *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
int fork();
char *getenv(char *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             struct sigaction *);
int waitpid(int, int *, int /*<<< orphan*/);
int system(const char *cmd) {
  const char *shell = getenv("SHELL");
  if (shell == NULL) {
    shell = "/bin/sh";
  };
  int stat;
  pid_t pid;
  struct sigaction sa, savintr, savequit;
  if (cmd == NULL)
    return 1;
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  sigaction(SIGINT, &sa, &savintr);
  sigaction(SIGQUIT, &sa, &savequit);
  if ((pid = fork()) == 0) {
    sigaction(SIGINT, &savintr, NULL);
    sigaction(SIGQUIT, &savequit, NULL);
    execl(shell, "sh", "-c", cmd, NULL);
    _exit(127);
  };
  if (pid == -1) {
    stat = -1;
  } else {
    while (waitpid(pid, &stat, 0) == -1) {
      if (errno != EINTR) {
        stat = -1;
        break;
      };
    };
  };
  sigaction(SIGINT, &savintr, NULL);
  sigaction(SIGQUIT, &savequit, NULL);
  return stat;
}


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe8d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1192 <system+0x32>
lea    0xe78(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <system+0x49>
movl   $0x1,-0x4(%rbp)
jmp    12e6 <system+0x186>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1050 <sigaction@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1050 <sigaction@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1252 <system+0xf2>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xdd4(%rip),%rsi
lea    0xdd0(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1268 <system+0x108>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12b4 <system+0x154>
jmp    126d <system+0x10d>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12af <system+0x14f>
mov    0x2d3d(%rip),%rax
mov    (%rax),%rax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
je     12aa <system+0x14a>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12af <system+0x14f>
jmp    126d <system+0x10d>
jmp    12b4 <system+0x154>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
lea    0xe88(%rip),%rdi
call   1030 <getenv@plt>
test   %rbx,%rbx
je     1247 <system+0xe7>
mov    %rax,%r14
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
lea    0x28(%rsp),%rbp
lea    0x18(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sigaction@plt>
mov    0x2e1d(%rip),%r15
mov    (%r15),%edi
lea    0x8(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sigaction@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     121a <system+0xba>
mov    %eax,%ebp
test   %eax,%eax
je     125b <system+0xfb>
lea    0x4(%rsp),%r14
mov    0x2dd7(%rip),%r13
mov    0x2dc8(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1222 <system+0xc2>
mov    0x0(%r13),%rax
cmp    (%rbx),%rax
je     1200 <system+0xa0>
movl   $0xffffffff,0x4(%rsp)
ff
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x4(%rsp),%eax
jmp    124c <system+0xec>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
lea    0xda1(%rip),%rbp
cmovne %r14,%rbp
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
lea    0xd7c(%rip),%rsi
lea    0xd78(%rip),%rdx
mov    %rbp,%rdi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1070 <execl@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

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

LLVM IR (-O2):
%struct.sigaction = type { i64, i32 }
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIGQUIT = global i32 0
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [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 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = bitcast i32* %2 to i8*
%8 = bitcast %struct.sigaction* %3 to i8*
%9 = bitcast %struct.sigaction* %4 to i8*
%10 = bitcast %struct.sigaction* %5 to i8*
%11 = icmp eq i8* %0, null
br i1 %11, label %45, label %12
12: ; preds = %1
%13 = load i32, i32* @SIG_IGN
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 0, i64* %15
%16 = load i32, i32* @SIGINT
%17 = call i32 @sigaction(i32 %16, %struct.sigaction* %3, %struct.sigaction* %4)
%18 = load i32, i32* @SIGQUIT
%19 = call i32 @sigaction(i32 %18, %struct.sigaction* %3, %struct.sigaction* %5)
%20 = call i32 (...) @fork()
switch i32 %20, label %31 [
i32 0, label %21
i32 -1, label %30
]
21: ; preds = %12
%22 = icmp eq i8* %6, null
%23 = select i1 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %6
%24 = load i32, i32* @SIGINT
%25 = call i32 @sigaction(i32 %24, %struct.sigaction* %4, %struct.sigaction* null)
%26 = load i32, i32* @SIGQUIT
%27 = call i32 @sigaction(i32 %26, %struct.sigaction* %5, %struct.sigaction* null)
%28 = call i32 @execl(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32* null)
%29 = call i32 @_exit(i32 127)
unreachable
30: ; preds = %12
store i32 -1, i32* %2
br label %39
31: ; preds = %12, %34
%32 = call i32 @waitpid(i32 %20, i32* %2, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %31, label %38
38: ; preds = %34
store i32 -1, i32* %2
br label %39
39: ; preds = %31, %38, %30
%40 = load i32, i32* @SIGINT
%41 = call i32 @sigaction(i32 %40, %struct.sigaction* %4, %struct.sigaction* null)
%42 = load i32, i32* @SIGQUIT
%43 = call i32 @sigaction(i32 %42, %struct.sigaction* %5, %struct.sigaction* null)
%44 = load i32, i32* %2
br label %45
45: ; preds = %1, %39
%46 = phi i32 [ %44, %39 ], [ 1, %1 ]
ret i32 %46
}