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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ EX_OSFILE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ ab_terror(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char const *const,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ pipe(int *);
int shell(char const *const cmd) {
  int procout[2];
  pid_t forkret;
  if (cmd == NULL) {
    return (-1);
  }
  if (pipe(procout) != 0) {
    return (-1);
  }
  forkret = fork();
  if (forkret == 0) {
    dup2(procout[1], STDOUT_FILENO);
    close(procout[0]);
    execlp("sh", "sh", "-c", cmd, NULL);
    if (errno == ENOENT) {
      exit(EX_OSFILE);
    } else {
      ab_terror("execlp("
                "\"sh\", "
                "\"sh\", "
                "\"-c\", "
                "cmd, "
                "NULL"
                ")");
      exit(EX_OSERR);
    }
  } else if (forkret > 0) {
    close(procout[1]);
  } else {
    return (-1);
  }
  return (procout[0]);
}


x86 assembly (clang, -O0):
<shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <shell+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <shell+0x101>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11b2 <shell+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <shell+0x101>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1242 <shell+0xd2>
mov    -0x14(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xe14(%rip),%rsi
lea    0xe10(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1080 <execlp@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <shell+0xb8>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdd7(%rip),%rdi
call   1060 <ab_terror@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    125a <shell+0xea>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    1266 <shell+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <shell+0x101>
jmp    126b <shell+0xfb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     11a7 <shell+0x37>
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11a7 <shell+0x37>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11b0 <shell+0x40>
jle    11a7 <shell+0x37>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x4(%rsp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
lea    0xe2f(%rip),%rdi
lea    0xe2b(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <execlp@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <shell+0x9a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdf5(%rip),%rdi
call   1060 <ab_terror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@errno = global i64 0
@ENOENT = global i64 0
@EX_OSFILE = global i32 0
@.str.2 = [36 x i8] c"execlp(\22sh\22, \22sh\22, \22-c\22, cmd, NULL)\00"
@EX_OSERR = global i32 0
define i32 @shell(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2 x i32]
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %50
9: ; preds = %1
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 -1, i32* %2
br label %50
14: ; preds = %9
%15 = call i64 (...) @fork()
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @dup2(i32 %20, i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i8*, i8** %3
%27 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32* null)
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOENT
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = load i32, i32* @EX_OSFILE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %18
%35 = call i32 @ab_terror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EX_OSERR
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %14
%39 = load i64, i64* %5
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
br label %46
45: ; preds = %38
store i32 -1, i32* %2
br label %50
46: ; preds = %41
br label %47
47: ; preds = %46
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%49 = load i32, i32* %48
store i32 %49, i32* %2
br label %50
50: ; preds = %47, %45, %13, %8
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@errno = global i64 0
@ENOENT = global i64 0
@EX_OSFILE = global i32 0
@.str.2 = [36 x i8] c"execlp(\22sh\22, \22sh\22, \22-c\22, cmd, NULL)\00"
@EX_OSERR = global i32 0
define i32 @shell(i8* %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %37, label %5
5: ; preds = %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %37
9: ; preds = %5
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32* null)
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = load i32, i32* @EX_OSFILE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %12
%27 = call i32 @ab_terror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EX_OSERR
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %9
%31 = icmp sgt i64 %10, 0
br i1 %31, label %32, label %37
32: ; preds = %30
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %6
br label %37
37: ; preds = %30, %5, %1, %32
%38 = phi i32 [ %36, %32 ], [ -1, %1 ], [ -1, %5 ], [ -1, %30 ]
ret i32 %38
}