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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ STDERR_FILENO;
int STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int WTERMSIG(int);
scalar_t__ close(int);
scalar_t__ dup2(int, scalar_t__);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int);
int fork();
char *grab_fd(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ pipe(int *);
int system(char const *);
int waitpid(int, int *, int /*<<< orphan*/);
char *run(const char *cmd, int *exitstatus) {
  pid_t pid;
  int p[2];
  char *ret;
  int status;
  if (pipe(p) != 0)
    err(1, "creating pipe");
  pid = fork();
  if (pid == -1)
    err(1, "forking");
  if (pid == 0) {
    if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO ||
        dup2(p[1], STDERR_FILENO) != STDERR_FILENO || close(p[0]) != 0 ||
        close(STDIN_FILENO) != 0 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
      exit(128);
    status = system(cmd);
    if (WIFEXITED(status))
      exit(WEXITSTATUS(status));
    exit(128 + WTERMSIG(status));
  }
  close(p[1]);
  ret = grab_fd(p[0]);
  if (waitpid(pid, &status, 0) != pid)
    err(1, "Failed to wait for child");
  close(p[0]);
  if (WIFEXITED(status))
    *exitstatus = WEXITSTATUS(status);
  else
    *exitstatus = -WTERMSIG(status);
  return ret;
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     1204 <run+0x34>
mov    $0x1,%edi
lea    0xe01(%rip),%rsi
call   1070 <err@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1229 <run+0x59>
mov    $0x1,%edi
lea    0xdea(%rip),%rsi
call   1070 <err@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1311 <run+0x141>
mov    -0x18(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    12c5 <run+0xf5>
mov    -0x18(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
jne    12c5 <run+0xf5>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jne    12c5 <run+0xf5>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jne    12c5 <run+0xf5>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    0xd65(%rip),%rdi
call   10c0 <open@plt>
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%eax
je     12cf <run+0xff>
mov    $0x80,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12fc <run+0x12c>
mov    -0x2c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10e0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%edi
add    $0x80,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1090 <grab_fd@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    -0x14(%rbp),%eax
je     134d <run+0x17d>
mov    $0x1,%edi
lea    0xcd8(%rip),%rsi
call   1070 <err@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     137c <run+0x1ac>
mov    -0x2c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    138e <run+0x1be>
mov    -0x2c(%rbp),%edi
call   1080 <WTERMSIG@plt>
xor    %ecx,%ecx
sub    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     1200 <run+0x30>
lea    0xe0a(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12c4 <run+0xf4>
test   %ebp,%ebp
jne    12d5 <run+0x105>
mov    0x10(%rsp),%edi
mov    0x2dcb(%rip),%rbx
mov    (%rbx),%rsi
call   1040 <dup2@plt>
cmp    (%rbx),%rax
jne    1347 <run+0x177>
mov    0x10(%rsp),%edi
mov    0x2d8f(%rip),%rbx
mov    (%rbx),%rsi
call   1040 <dup2@plt>
cmp    (%rbx),%rax
jne    1347 <run+0x177>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1347 <run+0x177>
mov    0x2d5d(%rip),%rbx
mov    (%rbx),%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1347 <run+0x177>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
call   10c0 <open@plt>
cmp    (%rbx),%eax
jne    1347 <run+0x177>
mov    %r14,%rdi
call   1030 <system@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   10d0 <WIFEXITED@plt>
mov    %ebx,%edi
test   %rax,%rax
jne    1351 <run+0x181>
call   1080 <WTERMSIG@plt>
lea    0x80(%rax),%edi
call   10e0 <exit@plt>
lea    0xd43(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1090 <grab_fd@plt>
mov    %rax,%rbx
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    %ebp,%eax
je     130d <run+0x13d>
lea    0xd1d(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   10d0 <WIFEXITED@plt>
mov    0x14(%rsp),%edi
test   %rax,%rax
je     132f <run+0x15f>
call   10a0 <WEXITSTATUS@plt>
jmp    1336 <run+0x166>
call   1080 <WTERMSIG@plt>
neg    %eax
mov    %eax,(%r15)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x80,%edi
call   10e0 <exit@plt>
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"creating pipe\00"
@.str.1 = [8 x i8] c"forking\00"
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@STDIN_FILENO = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Failed to wait for child\00"
define i8* @run(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 (...) @fork()
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %68
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = load i64, i64* @STDOUT_FILENO
%27 = call i64 @dup2(i32 %25, i64 %26)
%28 = load i64, i64* @STDOUT_FILENO
%29 = icmp ne i64 %27, %28
br i1 %29, label %51, label %30
30: ; preds = %23
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%32 = load i32, i32* %31
%33 = load i64, i64* @STDERR_FILENO
%34 = call i64 @dup2(i32 %32, i64 %33)
%35 = load i64, i64* @STDERR_FILENO
%36 = icmp ne i64 %34, %35
br i1 %36, label %51, label %37
37: ; preds = %30
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i64 @close(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %51, label %42
42: ; preds = %37
%43 = load i32, i32* @STDIN_FILENO
%44 = call i64 @close(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = load i32, i32* @O_RDONLY
%48 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @STDIN_FILENO
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46, %42, %37, %30, %23
%52 = call i32 @exit(i32 128)
unreachable
53: ; preds = %46
%54 = load i8*, i8** %3
%55 = call i32 @system(i8* %54)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = call i64 @WIFEXITED(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i32, i32* %8
%61 = call i32 @WEXITSTATUS(i32 %60)
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %53
%64 = load i32, i32* %8
%65 = call i32 @WTERMSIG(i32 %64)
%66 = add nsw i32 128, %65
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %20
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i64 @close(i32 %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i8* @grab_fd(i32 %73)
store i8* %74, i8** %7
%75 = load i32, i32* %5
%76 = call i32 @waitpid(i32 %75, i32* %8, i32 0)
%77 = load i32, i32* %5
%78 = icmp ne i32 %76, %77
br i1 %78, label %79, label %81
79: ; preds = %68
%80 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %81
81: ; preds = %79, %68
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%83 = load i32, i32* %82
%84 = call i64 @close(i32 %83)
%85 = load i32, i32* %8
%86 = call i64 @WIFEXITED(i32 %85)
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %81
%89 = load i32, i32* %8
%90 = call i32 @WEXITSTATUS(i32 %89)
%91 = load i32*, i32** %4
store i32 %90, i32* %91
br label %97
92: ; preds = %81
%93 = load i32, i32* %8
%94 = call i32 @WTERMSIG(i32 %93)
%95 = sub nsw i32 0, %94
%96 = load i32*, i32** %4
store i32 %95, i32* %96
br label %97
97: ; preds = %92, %88
%98 = load i8*, i8** %7
ret i8* %98
}

LLVM IR (-O2):
@.str = [14 x i8] c"creating pipe\00"
@.str.1 = [8 x i8] c"forking\00"
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@STDIN_FILENO = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Failed to wait for child\00"
define i8* @run(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = call i32 (...) @fork()
switch i32 %13, label %55 [
i32 -1, label %14
i32 0, label %16
]
14: ; preds = %12
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %55
16: ; preds = %12
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%18 = load i32, i32* %17
%19 = load i64, i64* @STDOUT_FILENO
%20 = call i64 @dup2(i32 %18, i64 %19)
%21 = load i64, i64* @STDOUT_FILENO
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %42
23: ; preds = %16
%24 = load i32, i32* %17
%25 = load i64, i64* @STDERR_FILENO
%26 = call i64 @dup2(i32 %24, i64 %25)
%27 = load i64, i64* @STDERR_FILENO
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %23
%30 = load i32, i32* %7
%31 = call i64 @close(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i32, i32* @STDIN_FILENO
%35 = call i64 @close(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @STDIN_FILENO
%41 = icmp eq i32 %39, %40
br i1 %41, label %44, label %42
42: ; preds = %37, %33, %29, %23, %16
%43 = call i32 @exit(i32 128)
unreachable
44: ; preds = %37
%45 = call i32 @system(i8* %0)
%46 = call i64 @WIFEXITED(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = call i32 @WEXITSTATUS(i32 %45)
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %44
%52 = call i32 @WTERMSIG(i32 %45)
%53 = add nsw i32 %52, 128
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %14, %12
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i64 @close(i32 %57)
%59 = load i32, i32* %7
%60 = call i8* @grab_fd(i32 %59)
%61 = call i32 @waitpid(i32 %13, i32* %4, i32 0)
%62 = icmp eq i32 %61, %13
br i1 %62, label %65, label %63
63: ; preds = %55
%64 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %63, %55
%66 = load i32, i32* %7
%67 = call i64 @close(i32 %66)
%68 = load i32, i32* %4
%69 = call i64 @WIFEXITED(i32 %68)
%70 = icmp eq i64 %69, 0
%71 = load i32, i32* %4
br i1 %70, label %74, label %72
72: ; preds = %65
%73 = call i32 @WEXITSTATUS(i32 %71)
br label %77
74: ; preds = %65
%75 = call i32 @WTERMSIG(i32 %71)
%76 = sub nsw i32 0, %75
br label %77
77: ; preds = %74, %72
%78 = phi i32 [ %76, %74 ], [ %73, %72 ]
store i32 %78, i32* %1
ret i8* %60
}