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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ _exit(int);
scalar_t__ chdir(char const *);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ execve(char *, char **, char **);
scalar_t__ fork();
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     struct sigaction *);
scalar_t__ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int runshellcmd(const char *cmd, const char *envp[], const char *cwd) {
  enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
  unsigned int doneflags = 0;
  int status = 0;
  struct sigaction newsa, oldsaint, oldsaquit;
  sigset_t oldmask;
  memset(&newsa, 0, sizeof(newsa));
  newsa.sa_handler = SIG_IGN;
  newsa.sa_flags = 0;
  if (sigemptyset(&newsa.sa_mask) < 0)
    goto done;
  if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
    goto done;
  doneflags |= F_SIGINT;
  if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
    goto done;
  doneflags |= F_SIGQUIT;
  if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
    goto done;
  if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
    goto done;
  doneflags |= F_SIGMASK;
  pid_t pid = fork();
  if (pid < 0)
    goto done;
  if (pid == 0) {
    sigaction(SIGINT, &oldsaint, NULL);
    sigaction(SIGQUIT, &oldsaquit, NULL);
    sigprocmask(SIG_SETMASK, &oldmask, NULL);
    if (cwd && chdir(cwd) < 0)
      _exit(127);
    const char *argv[] = {"sh", "-c", cmd, NULL};
    if (envp) {
      execve("/bin/sh", (char **)argv, (char **)envp);
    } else {
      execv("/bin/sh", (char **)argv);
    }
    _exit(127);
  } else {
    if (waitpid(pid, &status, 0) < 0)
      goto done;
    doneflags |= F_WAITPID;
  }
done:
  if (doneflags & F_SIGINT)
    sigaction(SIGINT, &oldsaint, NULL);
  if (doneflags & F_SIGQUIT)
    sigaction(SIGQUIT, &oldsaquit, NULL);
  if (doneflags & F_SIGMASK)
    sigprocmask(SIG_SETMASK, &oldmask, NULL);
  if (!(doneflags & F_WAITPID))
    return 127;
  if (WIFEXITED(status))
    return WEXITSTATUS(status);
  if (WIFSIGNALED(status))
    return -WTERMSIG(status);
  return 127;
}


x86 assembly (clang, -O0):
<runshellcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
call   10a0 <sigemptyset@plt>
cmp    $0x0,%rax
jge    1251 <runshellcmd+0x61>
jmp    142e <runshellcmd+0x23e>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    1276 <runshellcmd+0x86>
jmp    142e <runshellcmd+0x23e>
mov    -0x24(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
lea    -0x70(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    12a4 <runshellcmd+0xb4>
jmp    142e <runshellcmd+0x23e>
mov    -0x24(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x24(%rbp)
lea    -0x40(%rbp),%rdi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   10f0 <sigaddset@plt>
cmp    $0x0,%rax
jge    12ce <runshellcmd+0xde>
jmp    142e <runshellcmd+0x23e>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
lea    -0x74(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    12f3 <runshellcmd+0x103>
jmp    142e <runshellcmd+0x23e>
mov    -0x24(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jge    1317 <runshellcmd+0x127>
jmp    142e <runshellcmd+0x23e>
cmpq   $0x0,-0x80(%rbp)
jne    1402 <runshellcmd+0x212>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
lea    -0x74(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmpq   $0x0,-0x20(%rbp)
je     138c <runshellcmd+0x19c>
mov    -0x20(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    138c <runshellcmd+0x19c>
mov    $0x7f,%edi
call   1040 <_exit@plt>
lea    0xc75(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    0xc5f(%rip),%rax
mov    %rax,-0x98(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
movq   $0x0,-0x88(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     13e5 <runshellcmd+0x1f5>
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xc28(%rip),%rdi
call   1080 <execve@plt>
jmp    13f8 <runshellcmd+0x208>
lea    -0xa0(%rbp),%rsi
lea    0xc10(%rip),%rdi
call   1100 <execv@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
mov    -0x80(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   10d0 <waitpid@plt>
cmp    $0x0,%rax
jge    1420 <runshellcmd+0x230>
jmp    142e <runshellcmd+0x23e>
mov    -0x24(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0x24(%rbp)
jmp    142e <runshellcmd+0x23e>
mov    -0x24(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1453 <runshellcmd+0x263>
mov    0x2ba4(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    -0x24(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1478 <runshellcmd+0x288>
mov    0x2b77(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    -0x24(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     149d <runshellcmd+0x2ad>
mov    0x2b4a(%rip),%rax
mov    (%rax),%edi
lea    -0x74(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x24(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
jne    14b8 <runshellcmd+0x2c8>
movl   $0x7f,-0x4(%rbp)
jmp    1509 <runshellcmd+0x319>
mov    -0x28(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     14da <runshellcmd+0x2ea>
mov    -0x28(%rbp),%edi
call   10c0 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1509 <runshellcmd+0x319>
mov    -0x28(%rbp),%edi
call   1090 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1502 <runshellcmd+0x312>
mov    -0x28(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1509 <runshellcmd+0x319>
movl   $0x7f,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runshellcmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rbx,%rdi
call   10a0 <sigemptyset@plt>
mov    $0x7f,%ebx
test   %rax,%rax
js     138b <runshellcmd+0x19b>
mov    0x2d93(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rsi
lea    0x60(%rsp),%rdx
call   1050 <sigaction@plt>
test   %rax,%rax
js     138b <runshellcmd+0x19b>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
lea    0x48(%rsp),%rdx
call   1050 <sigaction@plt>
test   %rax,%rax
js     12fd <runshellcmd+0x10d>
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   10f0 <sigaddset@plt>
mov    $0x3,%r13d
test   %rax,%rax
js     1303 <runshellcmd+0x113>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     1303 <runshellcmd+0x113>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     139c <runshellcmd+0x1ac>
je     13a7 <runshellcmd+0x1b7>
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   10d0 <waitpid@plt>
xor    %ecx,%ecx
test   %rax,%rax
setns  %cl
lea    0x7(,%rcx,8),%r13d
jmp    1303 <runshellcmd+0x113>
mov    $0x1,%r13d
mov    0x0(%rbp),%edi
lea    0x60(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   $0x2,%r13b
jne    1326 <runshellcmd+0x136>
test   $0x4,%r13b
jne    1341 <runshellcmd+0x151>
test   $0x8,%r13b
jne    135c <runshellcmd+0x16c>
jmp    138b <runshellcmd+0x19b>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0x48(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   $0x4,%r13b
je     131e <runshellcmd+0x12e>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   $0x8,%r13b
je     138b <runshellcmd+0x19b>
mov    (%rsp),%edi
call   10e0 <WIFEXITED@plt>
mov    (%rsp),%edi
test   %rax,%rax
je     1375 <runshellcmd+0x185>
call   10c0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    138b <runshellcmd+0x19b>
call   1090 <WIFSIGNALED@plt>
test   %rax,%rax
je     138b <runshellcmd+0x19b>
mov    (%rsp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%ebx
neg    %ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x7,%r13d
jmp    1303 <runshellcmd+0x113>
mov    0x0(%rbp),%edi
lea    0x60(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2c23(%rip),%rax
mov    (%rax),%edi
lea    0x48(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %r12,%r12
je     13f2 <runshellcmd+0x202>
mov    %r12,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     1440 <runshellcmd+0x250>
lea    0xc0f(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xbfb(%rip),%rax
mov    %rax,0x28(%rsp)
mov    %r15,0x30(%rsp)
movq   $0x0,0x38(%rsp)
lea    0xbe4(%rip),%rdi
lea    0x20(%rsp),%rsi
test   %r14,%r14
jne    1438 <runshellcmd+0x248>
call   1100 <execv@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
mov    %r14,%rdx
call   1080 <execve@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 = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
define i32 @runshellcmd(i8* %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sigaction
%11 = alloca %struct.sigaction
%12 = alloca %struct.sigaction
%13 = alloca i32
%14 = alloca i64
%15 = alloca [4 x i8*]
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
%16 = call i32 @memset(%struct.sigaction* %10, i32 0, i32 24)
%17 = load i32, i32* @SIG_IGN
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 1
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 0
%21 = call i64 @sigemptyset(i32* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %3
br label %101
24: ; preds = %3
%25 = load i32, i32* @SIGINT
%26 = call i64 @sigaction(i32 %25, %struct.sigaction* %10, %struct.sigaction* %11)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %101
29: ; preds = %24
%30 = load i32, i32* %8
%31 = or i32 %30, 1
store i32 %31, i32* %8
%32 = load i32, i32* @SIGQUIT
%33 = call i64 @sigaction(i32 %32, %struct.sigaction* %10, %struct.sigaction* %12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
br label %101
36: ; preds = %29
%37 = load i32, i32* %8
%38 = or i32 %37, 2
store i32 %38, i32* %8
%39 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 0
%40 = load i32, i32* @SIGCHLD
%41 = call i64 @sigaddset(i32* %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
br label %101
44: ; preds = %36
%45 = load i32, i32* @SIG_BLOCK
%46 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 0
%47 = call i64 @sigprocmask(i32 %45, i32* %46, i32* %13)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %101
50: ; preds = %44
%51 = load i32, i32* %8
%52 = or i32 %51, 4
store i32 %52, i32* %8
%53 = call i64 (...) @fork()
store i64 %53, i64* %14
%54 = load i64, i64* %14
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
br label %101
57: ; preds = %50
%58 = load i64, i64* %14
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %92
60: ; preds = %57
%61 = load i32, i32* @SIGINT
%62 = call i64 @sigaction(i32 %61, %struct.sigaction* %11, %struct.sigaction* null)
%63 = load i32, i32* @SIGQUIT
%64 = call i64 @sigaction(i32 %63, %struct.sigaction* %12, %struct.sigaction* null)
%65 = load i32, i32* @SIG_SETMASK
%66 = call i64 @sigprocmask(i32 %65, i32* %13, i32* null)
%67 = load i8*, i8** %7
%68 = icmp ne i8* %67, null
br i1 %68, label %69, label %75
69: ; preds = %60
%70 = load i8*, i8** %7
%71 = call i64 @chdir(i8* %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 @_exit(i32 127)
unreachable
75: ; preds = %69, %60
%76 = getelementptr inbounds [4 x i8*], [4 x i8*]* %15, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %76
%77 = getelementptr inbounds i8*, i8** %76, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %77
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %5
store i8* %79, i8** %78
%80 = getelementptr inbounds i8*, i8** %78, i64 1
store i8* null, i8** %80
%81 = load i8**, i8*** %6
%82 = icmp ne i8** %81, null
br i1 %82, label %83, label %87
83: ; preds = %75
%84 = getelementptr inbounds [4 x i8*], [4 x i8*]* %15, i64 0, i64 0
%85 = load i8**, i8*** %6
%86 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %84, i8** %85)
br label %90
87: ; preds = %75
%88 = getelementptr inbounds [4 x i8*], [4 x i8*]* %15, i64 0, i64 0
%89 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %88)
br label %90
90: ; preds = %87, %83
%91 = call i32 @_exit(i32 127)
unreachable
92: ; preds = %57
%93 = load i64, i64* %14
%94 = call i64 @waitpid(i64 %93, i32* %9, i32 0)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %97
96: ; preds = %92
br label %101
97: ; preds = %92
%98 = load i32, i32* %8
%99 = or i32 %98, 8
store i32 %99, i32* %8
br label %100
100: ; preds = %97
br label %101
101: ; preds = %100, %96, %56, %49, %43, %35, %28, %23
%102 = load i32, i32* %8
%103 = and i32 %102, 1
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %101
%106 = load i32, i32* @SIGINT
%107 = call i64 @sigaction(i32 %106, %struct.sigaction* %11, %struct.sigaction* null)
br label %108
108: ; preds = %105, %101
%109 = load i32, i32* %8
%110 = and i32 %109, 2
%111 = icmp ne i32 %110, 0
br i1 %111, label %112, label %115
112: ; preds = %108
%113 = load i32, i32* @SIGQUIT
%114 = call i64 @sigaction(i32 %113, %struct.sigaction* %12, %struct.sigaction* null)
br label %115
115: ; preds = %112, %108
%116 = load i32, i32* %8
%117 = and i32 %116, 4
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %122
119: ; preds = %115
%120 = load i32, i32* @SIG_SETMASK
%121 = call i64 @sigprocmask(i32 %120, i32* %13, i32* null)
br label %122
122: ; preds = %119, %115
%123 = load i32, i32* %8
%124 = and i32 %123, 8
%125 = icmp ne i32 %124, 0
br i1 %125, label %127, label %126
126: ; preds = %122
store i32 127, i32* %4
br label %143
127: ; preds = %122
%128 = load i32, i32* %9
%129 = call i64 @WIFEXITED(i32 %128)
%130 = icmp ne i64 %129, 0
br i1 %130, label %131, label %134
131: ; preds = %127
%132 = load i32, i32* %9
%133 = call i32 @WEXITSTATUS(i32 %132)
store i32 %133, i32* %4
br label %143
134: ; preds = %127
%135 = load i32, i32* %9
%136 = call i64 @WIFSIGNALED(i32 %135)
%137 = icmp ne i64 %136, 0
br i1 %137, label %138, label %142
138: ; preds = %134
%139 = load i32, i32* %9
%140 = call i32 @WTERMSIG(i32 %139)
%141 = sub nsw i32 0, %140
store i32 %141, i32* %4
br label %143
142: ; preds = %134
store i32 127, i32* %4
br label %143
143: ; preds = %142, %138, %131, %126
%144 = load i32, i32* %4
ret i32 %144
}

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 = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
define i32 @runshellcmd(i8* %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sigaction
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
%8 = alloca i32
%9 = alloca [4 x i8*]
%10 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%11 = bitcast %struct.sigaction* %5 to i8*
%12 = bitcast %struct.sigaction* %6 to i8*
%13 = bitcast %struct.sigaction* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = call i32 @memset(%struct.sigaction* %5, i32 0, i32 24)
%16 = load i32, i32* @SIG_IGN
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 1
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
%20 = call i64 @sigemptyset(i32* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %105, label %22
22: ; preds = %3
%23 = load i32, i32* @SIGINT
%24 = call i64 @sigaction(i32 %23, %struct.sigaction* %5, %struct.sigaction* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %105, label %26
26: ; preds = %22
%27 = load i32, i32* @SIGQUIT
%28 = call i64 @sigaction(i32 %27, %struct.sigaction* %5, %struct.sigaction* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %73, label %30
30: ; preds = %26
%31 = load i32, i32* @SIGCHLD
%32 = call i64 @sigaddset(i32* %19, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %73, label %34
34: ; preds = %30
%35 = load i32, i32* @SIG_BLOCK
%36 = call i64 @sigprocmask(i32 %35, i32* %19, i32* %8)
%37 = icmp slt i64 %36, 0
br i1 %37, label %73, label %38
38: ; preds = %34
%39 = call i64 (...) @fork()
%40 = icmp slt i64 %39, 0
br i1 %40, label %73, label %41
41: ; preds = %38
%42 = icmp eq i64 %39, 0
br i1 %42, label %43, label %69
43: ; preds = %41
%44 = load i32, i32* @SIGINT
%45 = call i64 @sigaction(i32 %44, %struct.sigaction* %6, %struct.sigaction* null)
%46 = load i32, i32* @SIGQUIT
%47 = call i64 @sigaction(i32 %46, %struct.sigaction* %7, %struct.sigaction* null)
%48 = load i32, i32* @SIG_SETMASK
%49 = call i64 @sigprocmask(i32 %48, i32* %8, i32* null)
%50 = icmp eq i8* %2, null
br i1 %50, label %56, label %51
51: ; preds = %43
%52 = call i64 @chdir(i8* %2)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @_exit(i32 127)
unreachable
56: ; preds = %51, %43
%57 = bitcast [4 x i8*]* %9 to i8*
%58 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %58
%59 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %59
%60 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 2
store i8* %0, i8** %60
%61 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 3
store i8* null, i8** %61
%62 = icmp eq i8** %1, null
br i1 %62, label %65, label %63
63: ; preds = %56
%64 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %58, i8** %1)
br label %67
65: ; preds = %56
%66 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %58)
br label %67
67: ; preds = %65, %63
%68 = call i32 @_exit(i32 127)
unreachable
69: ; preds = %41
%70 = call i64 @waitpid(i64 %39, i32* %4, i32 0)
%71 = icmp slt i64 %70, 0
%72 = select i1 %71, i32 7, i32 15
br label %73
73: ; preds = %69, %38, %34, %30, %26
%74 = phi i32 [ %72, %69 ], [ 7, %38 ], [ 3, %34 ], [ 3, %30 ], [ 1, %26 ]
%75 = load i32, i32* @SIGINT
%76 = call i64 @sigaction(i32 %75, %struct.sigaction* %6, %struct.sigaction* null)
%77 = and i32 %74, 2
%78 = icmp eq i32 %77, 0
br i1 %78, label %82, label %79
79: ; preds = %73
%80 = load i32, i32* @SIGQUIT
%81 = call i64 @sigaction(i32 %80, %struct.sigaction* %7, %struct.sigaction* null)
br label %82
82: ; preds = %79, %73
%83 = and i32 %74, 4
%84 = icmp eq i32 %83, 0
br i1 %84, label %88, label %85
85: ; preds = %82
%86 = load i32, i32* @SIG_SETMASK
%87 = call i64 @sigprocmask(i32 %86, i32* %8, i32* null)
br label %88
88: ; preds = %85, %82
%89 = and i32 %74, 8
%90 = icmp eq i32 %89, 0
br i1 %90, label %105, label %91
91: ; preds = %88
%92 = load i32, i32* %4
%93 = call i64 @WIFEXITED(i32 %92)
%94 = icmp eq i64 %93, 0
%95 = load i32, i32* %4
br i1 %94, label %98, label %96
96: ; preds = %91
%97 = call i32 @WEXITSTATUS(i32 %95)
br label %105
98: ; preds = %91
%99 = call i64 @WIFSIGNALED(i32 %95)
%100 = icmp eq i64 %99, 0
br i1 %100, label %105, label %101
101: ; preds = %98
%102 = load i32, i32* %4
%103 = call i32 @WTERMSIG(i32 %102)
%104 = sub nsw i32 0, %103
br label %105
105: ; preds = %3, %22, %98, %88, %101, %96
%106 = phi i32 [ %97, %96 ], [ %104, %101 ], [ 127, %88 ], [ 127, %98 ], [ 127, %22 ], [ 127, %3 ]
ret i32 %106
}