#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 */
typedef int /*<<< orphan*/ sig_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_IGN;
char *_PATH_BSHELL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ execl(char const *, char const *, char *, char const *,
                         int /*<<< orphan*/ *);
char *getenv(char *);
int sigblock(int /*<<< orphan*/);
int /*<<< orphan*/ sigmask(int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sigsetmask(int);
char *strrchr(char const *, char);
int vfork();
int waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char const *);
int shell_system(const char *command) {
  static const char *name, *shell;
  int status;
  int omask;
  pid_t pid;
  sig_t intsave, quitsave;
  if (shell == NULL) {
    if ((shell = getenv("SHELL")) == NULL)
      shell = _PATH_BSHELL;
    if ((name = strrchr(shell, '/')) == NULL)
      name = shell;
    else
      ++name;
  }
  if (!command)
    return (1);
  omask = sigblock(sigmask(SIGCHLD));
  switch (pid = vfork()) {
  case -1:
    err(EXIT_FAILURE, "vfork");
  case 0:
    (void)sigsetmask(omask);
    (void)execl(shell, name, "-c", command, NULL);
    warn("%s", shell);
    _exit(1);
  default:
    intsave = signal(SIGINT, SIG_IGN);
    quitsave = signal(SIGQUIT, SIG_IGN);
    pid = waitpid(pid, &status, 0);
    (void)sigsetmask(omask);
    (void)signal(SIGINT, intsave);
    (void)signal(SIGQUIT, quitsave);
    return pid == -1 ? -1 : status;
  }
}


x86 assembly (clang, -O0):
<shell_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2ebc(%rip)
jne    1254 <shell_system+0x94>
lea    0xe1f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ea3(%rip)
cmp    $0x0,%rax
jne    1208 <shell_system+0x48>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2e88(%rip)
mov    0x2e81(%rip),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,0x2e68(%rip)
cmp    $0x0,%rax
jne    123d <shell_system+0x7d>
mov    0x2e5f(%rip),%rax
mov    %rax,0x2e50(%rip)
jmp    124f <shell_system+0x8f>
mov    0x2e44(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e39(%rip)
jmp    1254 <shell_system+0x94>
cmpq   $0x0,-0x10(%rbp)
jne    126b <shell_system+0xab>
movl   $0x1,-0x4(%rbp)
jmp    139d <shell_system+0x1dd>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1070 <sigmask@plt>
mov    %eax,%edi
call   10a0 <sigblock@plt>
mov    %eax,-0x18(%rbp)
call   10d0 <vfork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     12ae <shell_system+0xee>
jmp    129e <shell_system+0xde>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     12c3 <shell_system+0x103>
jmp    130b <shell_system+0x14b>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
call   1090 <err@plt>
mov    -0x18(%rbp),%edi
call   10b0 <sigsetmask@plt>
mov    0x2dbe(%rip),%rdi
mov    0x2daf(%rip),%rsi
mov    -0x10(%rbp),%rcx
lea    0xd28(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   10e0 <execl@plt>
mov    0x2d9b(%rip),%rsi
lea    0xd13(%rip),%rdi
call   1050 <warn@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    %eax,-0x20(%rbp)
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
mov    0x2c93(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   10b0 <sigsetmask@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%esi
call   1080 <signal@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%esi
call   1080 <signal@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1391 <shell_system+0x1d1>
mov    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1397 <shell_system+0x1d7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_system>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpq   $0x0,0x2eba(%rip)
je     1283 <shell_system+0xc3>
test   %rbx,%rbx
je     12d7 <shell_system+0x117>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <sigmask@plt>
mov    %eax,%edi
call   10a0 <sigblock@plt>
mov    %eax,%r14d
call   10d0 <vfork@plt>
test   %eax,%eax
je     1300 <shell_system+0x140>
cmp    $0xffffffff,%eax
je     12eb <shell_system+0x12b>
mov    %eax,%ebx
mov    0x2dcc(%rip),%r12
mov    (%r12),%edi
mov    0x2da1(%rip),%rbp
mov    0x0(%rbp),%esi
call   1080 <signal@plt>
mov    %eax,%r15d
mov    0x2da7(%rip),%r13
mov    0x0(%r13),%edi
mov    0x0(%rbp),%esi
call   1080 <signal@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
xor    %edx,%edx
mov    %ebx,%edi
call   10c0 <waitpid@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   10b0 <sigsetmask@plt>
mov    (%r12),%edi
mov    %r15d,%esi
call   1080 <signal@plt>
mov    0x0(%r13),%edi
mov    %ebp,%esi
call   1080 <signal@plt>
mov    %ebx,%eax
cmp    $0xffffffff,%ebx
je     12dc <shell_system+0x11c>
mov    0x4(%rsp),%eax
jmp    12dc <shell_system+0x11c>
lea    0xd76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2dfa(%rip)
mov    %rax,%rbp
test   %rax,%rax
jne    12af <shell_system+0xef>
mov    0x2d2b(%rip),%rax
mov    (%rax),%rbp
mov    %rbp,0x2de1(%rip)
mov    $0x2f,%esi
mov    %rbp,%rdi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rcx
test   %rax,%rax
cmove  %rbp,%rcx
mov    %rcx,0x2dba(%rip)
test   %rbx,%rbx
jne    11e5 <shell_system+0x25>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
call   1090 <err@plt>
mov    %r14d,%edi
call   10b0 <sigsetmask@plt>
mov    0x2d81(%rip),%rdi
mov    0x2d72(%rip),%rsi
lea    0xcef(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rbx,%rcx
call   10e0 <execl@plt>
mov    0x2d5f(%rip),%rsi
lea    0xcd7(%rip),%rdi
call   1050 <warn@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@shell_system.name = internal global i8* null
@shell_system.shell = internal global i8* null
@.str = [6 x i8] c"SHELL\00"
@_PATH_BSHELL = global i8* null
@SIGCHLD = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"vfork\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [3 x i8] c"%s\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @shell_system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** @shell_system.shell
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %26
11: ; preds = %1
%12 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** @shell_system.shell
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i8*, i8** @_PATH_BSHELL
store i8* %15, i8** @shell_system.shell
br label %16
16: ; preds = %14, %11
%17 = load i8*, i8** @shell_system.shell
%18 = call i8* @strrchr(i8* %17, i8 signext 47)
store i8* %18, i8** @shell_system.name
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i8*, i8** @shell_system.shell
store i8* %21, i8** @shell_system.name
br label %25
22: ; preds = %16
%23 = load i8*, i8** @shell_system.name
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** @shell_system.name
br label %25
25: ; preds = %22, %20
br label %26
26: ; preds = %25, %1
%27 = load i8*, i8** %3
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %26
store i32 1, i32* %2
br label %72
30: ; preds = %26
%31 = load i32, i32* @SIGCHLD
%32 = call i32 @sigmask(i32 %31)
%33 = call i32 @sigblock(i32 %32)
store i32 %33, i32* %5
%34 = call i32 @vfork()
store i32 %34, i32* %6
switch i32 %34, label %48 [
i32 -1, label %35
i32 0, label %38
]
35: ; preds = %30
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @err(i32 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %30, %35
%39 = load i32, i32* %5
%40 = call i32 @sigsetmask(i32 %39)
%41 = load i8*, i8** @shell_system.shell
%42 = load i8*, i8** @shell_system.name
%43 = load i8*, i8** %3
%44 = call i32 @execl(i8* %41, i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32* null)
%45 = load i8*, i8** @shell_system.shell
%46 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = call i32 @_exit(i32 1)
unreachable
48: ; preds = %30
%49 = load i32, i32* @SIGINT
%50 = load i32, i32* @SIG_IGN
%51 = call i32 @signal(i32 %49, i32 %50)
store i32 %51, i32* %7
%52 = load i32, i32* @SIGQUIT
%53 = load i32, i32* @SIG_IGN
%54 = call i32 @signal(i32 %52, i32 %53)
store i32 %54, i32* %8
%55 = load i32, i32* %6
%56 = call i32 @waitpid(i32 %55, i32* %4, i32 0)
store i32 %56, i32* %6
%57 = load i32, i32* %5
%58 = call i32 @sigsetmask(i32 %57)
%59 = load i32, i32* @SIGINT
%60 = load i32, i32* %7
%61 = call i32 @signal(i32 %59, i32 %60)
%62 = load i32, i32* @SIGQUIT
%63 = load i32, i32* %8
%64 = call i32 @signal(i32 %62, i32 %63)
%65 = load i32, i32* %6
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %68
67: ; preds = %48
br label %70
68: ; preds = %48
%69 = load i32, i32* %4
br label %70
70: ; preds = %68, %67
%71 = phi i32 [ -1, %67 ], [ %69, %68 ]
store i32 %71, i32* %2
br label %72
72: ; preds = %70, %29
%73 = load i32, i32* %2
ret i32 %73
}

LLVM IR (-O2):
@shell_system.name = internal unnamed_addr global i8* null
@shell_system.shell = internal unnamed_addr global i8* null
@.str = [6 x i8] c"SHELL\00"
@_PATH_BSHELL = global i8* null
@SIGCHLD = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"vfork\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [3 x i8] c"%s\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @shell_system(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i8*, i8** @shell_system.shell
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** @shell_system.shell
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = load i8*, i8** @_PATH_BSHELL
store i8* %10, i8** @shell_system.shell
br label %11
11: ; preds = %9, %6
%12 = phi i8* [ %10, %9 ], [ %7, %6 ]
%13 = call i8* @strrchr(i8* dereferenceable(1) %12, i8 signext 47)
%14 = icmp eq i8* %13, null
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = select i1 %14, i8* %12, i8* %15
store i8* %16, i8** @shell_system.name
br label %17
17: ; preds = %11, %1
%18 = icmp eq i8* %0, null
br i1 %18, label %51, label %19
19: ; preds = %17
%20 = load i32, i32* @SIGCHLD
%21 = call i32 @sigmask(i32 %20)
%22 = call i32 @sigblock(i32 %21)
%23 = call i32 @vfork()
switch i32 %23, label %35 [
i32 -1, label %24
i32 0, label %27
]
24: ; preds = %19
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @err(i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %19, %24
%28 = call i32 @sigsetmask(i32 %22)
%29 = load i8*, i8** @shell_system.shell
%30 = load i8*, i8** @shell_system.name
%31 = call i32 @execl(i8* %29, i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%32 = load i8*, i8** @shell_system.shell
%33 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = call i32 @_exit(i32 1)
unreachable
35: ; preds = %19
%36 = load i32, i32* @SIGINT
%37 = load i32, i32* @SIG_IGN
%38 = call i32 @signal(i32 %36, i32 %37)
%39 = load i32, i32* @SIGQUIT
%40 = load i32, i32* @SIG_IGN
%41 = call i32 @signal(i32 %39, i32 %40)
%42 = call i32 @waitpid(i32 %23, i32* %2, i32 0)
%43 = call i32 @sigsetmask(i32 %22)
%44 = load i32, i32* @SIGINT
%45 = call i32 @signal(i32 %44, i32 %38)
%46 = load i32, i32* @SIGQUIT
%47 = call i32 @signal(i32 %46, i32 %41)
%48 = icmp eq i32 %42, -1
%49 = load i32, i32* %2
%50 = select i1 %48, i32 -1, i32 %49
br label %51
51: ; preds = %17, %35
%52 = phi i32 [ %50, %35 ], [ 1, %17 ]
ret i32 %52
}