#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ clrtoeol();
int /*<<< orphan*/ deraw(int /*<<< orphan*/);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
char *findhome(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ move(int, int /*<<< orphan*/);
scalar_t__ pipe(int *);
scalar_t__ printw(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
FILE *openfile(char *fname, int *rpid, int *rfd) {
  int pipefd[4];
  int pid;
  FILE *f;
  char *efname;
  while (*fname && (*fname == ' '))
    fname++;
  if (*fname != '|') {
    *rpid = 0;
    if (rfd != NULL)
      *rfd = 1;
    efname = findhome(fname);
    return (fopen(efname, rfd == NULL ? "w" : "r"));
  }
  fname++;
  efname = findhome(fname);
  if (pipe(pipefd) < 0 || (rfd != NULL && pipe(pipefd + 2) < 0)) {
    isatty(fileno(stdout)) && !move(1, 0) && !clrtoeol() &&
        printw("Can't make pipe to child");
    *rpid = 0;
    return (0);
  }
  deraw(rfd == NULL);
  if ((pid = fork()) == 0) {
    (void)close(0);
    (void)close(pipefd[1]);
    (void)dup(pipefd[0]);
    if (rfd != NULL) {
      (void)close(1);
      (void)close(pipefd[2]);
      (void)dup(pipefd[3]);
    }
    (void)signal(SIGINT, SIG_DFL);
    (void)execl("/bin/sh", "sh", "-c", efname, 0);
    exit(-127);
  } else {
    *rpid = pid;
    if ((f = fdopen(pipefd[(rfd == NULL ? 1 : 2)], rfd == NULL ? "w" : "r")) ==
        NULL) {
      (void)kill(pid, 9);
      isatty(fileno(stdout)) && !move(1, 0) && !clrtoeol() &&
          printw("Can't fdopen %sput", rfd == NULL ? "out" : "in");
      (void)close(pipefd[1]);
      if (rfd != NULL)
        (void)close(pipefd[3]);
      *rpid = 0;
      return (0);
    }
  }
  (void)close(pipefd[0]);
  if (rfd != NULL) {
    (void)close(pipefd[3]);
    *rfd = pipefd[1];
  }
  return (f);
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x49(%rbp)
je     1249 <openfile+0x39>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1259 <openfile+0x49>
jmp    126a <openfile+0x5a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1224 <openfile+0x14>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7c,%eax
je     12d2 <openfile+0xc2>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1299 <openfile+0x89>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <findhome@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rcx
lea    0xd4d(%rip),%rsi
lea    0xd44(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x8(%rbp)
jmp    159f <openfile+0x38f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <findhome@plt>
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jl     1320 <openfile+0x110>
cmpq   $0x0,-0x20(%rbp)
je     13ac <openfile+0x19c>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    13ac <openfile+0x19c>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10c0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4a(%rbp)
je     1392 <openfile+0x182>
mov    $0x1,%edi
xor    %esi,%esi
call   1130 <move@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4a(%rbp)
jne    1392 <openfile+0x182>
mov    $0x0,%al
call   10e0 <clrtoeol@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4a(%rbp)
jne    1392 <openfile+0x182>
lea    0xc83(%rip),%rdi
mov    $0x0,%al
call   10b0 <printw@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x4a(%rbp)
mov    -0x4a(%rbp),%al
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    159f <openfile+0x38f>
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <deraw@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jne    144f <openfile+0x23f>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <dup@plt>
cmpq   $0x0,-0x20(%rbp)
je     140d <openfile+0x1fd>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <dup@plt>
mov    0x2bd4(%rip),%rax
mov    (%rax),%edi
mov    0x2bbb(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    -0x48(%rbp),%rcx
lea    0xbee(%rip),%rdi
lea    0xbec(%rip),%rsi
lea    0xbe8(%rip),%rdx
xor    %r8d,%r8d
call   1110 <execl@plt>
mov    $0xffffff81,%edi
call   1100 <exit@plt>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
cltq
mov    -0x30(%rbp,%rax,4),%edi
mov    -0x20(%rbp),%rcx
lea    0xb84(%rip),%rsi
lea    0xb7b(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    156e <openfile+0x35e>
mov    -0x34(%rbp),%edi
mov    $0x9,%esi
call   10a0 <kill@plt>
mov    0x2b1c(%rip),%rax
mov    (%rax),%edi
call   10c0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4b(%rbp)
je     1539 <openfile+0x329>
mov    $0x1,%edi
xor    %esi,%esi
call   1130 <move@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4b(%rbp)
jne    1539 <openfile+0x329>
mov    $0x0,%al
call   10e0 <clrtoeol@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4b(%rbp)
jne    1539 <openfile+0x329>
mov    -0x20(%rbp),%rcx
lea    0xb2d(%rip),%rsi
lea    0xb22(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rsi
lea    0xb00(%rip),%rdi
mov    $0x0,%al
call   10b0 <printw@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x4b(%rbp)
mov    -0x4b(%rbp),%al
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     1557 <openfile+0x347>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    159f <openfile+0x38f>
jmp    1573 <openfile+0x363>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     1597 <openfile+0x387>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rdi),%eax
cmp    $0x20,%al
jne    123d <openfile+0x2d>
add    $0x1,%rdi
jmp    1230 <openfile+0x20>
cmp    $0x7c,%al
jne    12f2 <openfile+0xe2>
add    $0x1,%rdi
call   1080 <findhome@plt>
mov    %rax,%r15
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     130f <openfile+0xff>
test   %r12,%r12
je     127a <openfile+0x6a>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     130f <openfile+0xff>
xor    %edi,%edi
test   %r12,%r12
sete   %dil
call   1090 <deraw@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     141a <openfile+0x20a>
mov    %eax,%ebp
xor    %eax,%eax
test   %r12,%r12
setne  %al
mov    %ebp,(%r14)
mov    0x4(%rsp,%rax,4),%edi
lea    0xd51(%rip),%rax
lea    0xd4c(%rip),%rsi
cmove  %rax,%rsi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     1386 <openfile+0x176>
mov    %rax,%rbx
mov    (%rsp),%edi
call   1050 <close@plt>
test   %r12,%r12
je     140a <openfile+0x1fa>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%r12)
jmp    140a <openfile+0x1fa>
movl   $0x0,(%r14)
test   %r12,%r12
je     1363 <openfile+0x153>
movl   $0x1,(%r12)
lea    0xcf5(%rip),%rbx
jmp    136a <openfile+0x15a>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10c0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1401 <openfile+0x1f1>
mov    $0x1,%edi
xor    %esi,%esi
call   1130 <move@plt>
test   %eax,%eax
jne    1401 <openfile+0x1f1>
xor    %eax,%eax
call   10e0 <clrtoeol@plt>
test   %eax,%eax
jne    1401 <openfile+0x1f1>
lea    0xcad(%rip),%rdi
xor    %eax,%eax
call   10b0 <printw@plt>
jmp    1401 <openfile+0x1f1>
lea    0xc96(%rip),%rbx
call   1080 <findhome@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    10f0 <fopen@plt>
mov    %ebp,%edi
mov    $0x9,%esi
call   10a0 <kill@plt>
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
call   10c0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     13ea <openfile+0x1da>
mov    $0x1,%edi
xor    %esi,%esi
call   1130 <move@plt>
test   %eax,%eax
jne    13ea <openfile+0x1da>
xor    %eax,%eax
call   10e0 <clrtoeol@plt>
test   %eax,%eax
jne    13ea <openfile+0x1da>
test   %r12,%r12
lea    0xc6a(%rip),%rax
lea    0xc67(%rip),%rsi
cmove  %rax,%rsi
lea    0xc45(%rip),%rdi
xor    %eax,%eax
call   10b0 <printw@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
test   %r12,%r12
je     1401 <openfile+0x1f1>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
movl   $0x0,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1040 <dup@plt>
test   %r12,%r12
je     1453 <openfile+0x243>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <dup@plt>
mov    0x2b8e(%rip),%rax
mov    (%rax),%edi
mov    0x2b75(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
lea    0xbac(%rip),%rdi
lea    0xbaa(%rip),%rsi
lea    0xba6(%rip),%rdx
mov    %r15,%rcx
xor    %r8d,%r8d
call   1110 <execl@plt>
mov    $0xffffff81,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"Can't make pipe to child\00"
@SIGINT = global i32 0
@SIG_DFL = global i32 0
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [19 x i8] c"Can't fdopen %sput\00"
@.str.7 = [4 x i8] c"out\00"
@.str.8 = [3 x i8] c"in\00"
define i32* @openfile(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [4 x i32]
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
br label %12
12: ; preds = %24, %3
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 32
br label %22
22: ; preds = %17, %12
%23 = phi i1 [ false, %12 ], [ %21, %17 ]
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
br label %12
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 124
br i1 %31, label %32, label %47
32: ; preds = %27
%33 = load i32*, i32** %6
store i32 0, i32* %33
%34 = load i32*, i32** %7
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32*, i32** %7
store i32 1, i32* %37
br label %38
38: ; preds = %36, %32
%39 = load i8*, i8** %5
%40 = call i8* @findhome(i8* %39)
store i8* %40, i8** %11
%41 = load i8*, i8** %11
%42 = load i32*, i32** %7
%43 = icmp eq i32* %42, null
%44 = zext i1 %43 to i64
%45 = select i1 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%46 = call i32* @fopen(i8* %41, i8* %45)
store i32* %46, i32** %4
br label %179
47: ; preds = %27
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
%50 = load i8*, i8** %5
%51 = call i8* @findhome(i8* %50)
store i8* %51, i8** %11
%52 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%53 = call i64 @pipe(i32* %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %63, label %55
55: ; preds = %47
%56 = load i32*, i32** %7
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %81
58: ; preds = %55
%59 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%60 = getelementptr inbounds i32, i32* %59, i64 2
%61 = call i64 @pipe(i32* %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %81
63: ; preds = %58, %47
%64 = load i32, i32* @stdout
%65 = call i32 @fileno(i32 %64)
%66 = call i64 @isatty(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %77
68: ; preds = %63
%69 = call i32 @move(i32 1, i32 0)
%70 = icmp ne i32 %69, 0
br i1 %70, label %77, label %71
71: ; preds = %68
%72 = call i32 (...) @clrtoeol()
%73 = icmp ne i32 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = call i64 (i8*, ...) @printw(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%76 = icmp ne i64 %75, 0
br label %77
77: ; preds = %74, %71, %68, %63
%78 = phi i1 [ false, %71 ], [ false, %68 ], [ false, %63 ], [ %76, %74 ]
%79 = zext i1 %78 to i32
%80 = load i32*, i32** %6
store i32 0, i32* %80
store i32* null, i32** %4
br label %179
81: ; preds = %58, %55
%82 = load i32*, i32** %7
%83 = icmp eq i32* %82, null
%84 = zext i1 %83 to i32
%85 = call i32 @deraw(i32 %84)
%86 = call i32 (...) @fork()
store i32 %86, i32* %9
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %113
88: ; preds = %81
%89 = call i32 @close(i32 0)
%90 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
%93 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%94 = load i32, i32* %93
%95 = call i32 @dup(i32 %94)
%96 = load i32*, i32** %7
%97 = icmp ne i32* %96, null
br i1 %97, label %98, label %106
98: ; preds = %88
%99 = call i32 @close(i32 1)
%100 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 2
%101 = load i32, i32* %100
%102 = call i32 @close(i32 %101)
%103 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%104 = load i32, i32* %103
%105 = call i32 @dup(i32 %104)
br label %106
106: ; preds = %98, %88
%107 = load i32, i32* @SIGINT
%108 = load i32, i32* @SIG_DFL
%109 = call i32 @signal(i32 %107, i32 %108)
%110 = load i8*, i8** %11
%111 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %110, i32 0)
%112 = call i32 @exit(i32 -127)
unreachable
113: ; preds = %81
%114 = load i32, i32* %9
%115 = load i32*, i32** %6
store i32 %114, i32* %115
%116 = load i32*, i32** %7
%117 = icmp eq i32* %116, null
%118 = zext i1 %117 to i64
%119 = select i1 %117, i32 1, i32 2
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 %120
%122 = load i32, i32* %121
%123 = load i32*, i32** %7
%124 = icmp eq i32* %123, null
%125 = zext i1 %124 to i64
%126 = select i1 %124, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%127 = call i32* @fdopen(i32 %122, i8* %126)
store i32* %127, i32** %10
%128 = icmp eq i32* %127, null
br i1 %128, label %129, label %163
129: ; preds = %113
%130 = load i32, i32* %9
%131 = call i32 @kill(i32 %130, i32 9)
%132 = load i32, i32* @stdout
%133 = call i32 @fileno(i32 %132)
%134 = call i64 @isatty(i32 %133)
%135 = icmp ne i64 %134, 0
br i1 %135, label %136, label %149
136: ; preds = %129
%137 = call i32 @move(i32 1, i32 0)
%138 = icmp ne i32 %137, 0
br i1 %138, label %149, label %139
139: ; preds = %136
%140 = call i32 (...) @clrtoeol()
%141 = icmp ne i32 %140, 0
br i1 %141, label %149, label %142
142: ; preds = %139
%143 = load i32*, i32** %7
%144 = icmp eq i32* %143, null
%145 = zext i1 %144 to i64
%146 = select i1 %144, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%147 = call i64 (i8*, ...) @printw(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %146)
%148 = icmp ne i64 %147, 0
br label %149
149: ; preds = %142, %139, %136, %129
%150 = phi i1 [ false, %139 ], [ false, %136 ], [ false, %129 ], [ %148, %142 ]
%151 = zext i1 %150 to i32
%152 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%153 = load i32, i32* %152
%154 = call i32 @close(i32 %153)
%155 = load i32*, i32** %7
%156 = icmp ne i32* %155, null
br i1 %156, label %157, label %161
157: ; preds = %149
%158 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%159 = load i32, i32* %158
%160 = call i32 @close(i32 %159)
br label %161
161: ; preds = %157, %149
%162 = load i32*, i32** %6
store i32 0, i32* %162
store i32* null, i32** %4
br label %179
163: ; preds = %113
br label %164
164: ; preds = %163
%165 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%166 = load i32, i32* %165
%167 = call i32 @close(i32 %166)
%168 = load i32*, i32** %7
%169 = icmp ne i32* %168, null
br i1 %169, label %170, label %177
170: ; preds = %164
%171 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%172 = load i32, i32* %171
%173 = call i32 @close(i32 %172)
%174 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%175 = load i32, i32* %174
%176 = load i32*, i32** %7
store i32 %175, i32* %176
br label %177
177: ; preds = %170, %164
%178 = load i32*, i32** %10
store i32* %178, i32** %4
br label %179
179: ; preds = %177, %161, %77, %38
%180 = load i32*, i32** %4
ret i32* %180
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"Can't make pipe to child\00"
@SIGINT = global i32 0
@SIG_DFL = global i32 0
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [19 x i8] c"Can't fdopen %sput\00"
@.str.7 = [4 x i8] c"out\00"
@.str.8 = [3 x i8] c"in\00"
define noalias i32* @openfile(i8* %0, i32* nocapture writeonly %1, i32* writeonly %2) {
%4 = alloca [4 x i32]
%5 = bitcast [4 x i32]* %4 to i8*
br label %6
6: ; preds = %9, %3
%7 = phi i8* [ %0, %3 ], [ %10, %9 ]
%8 = load i8, i8* %7
switch i8 %8, label %11 [
i8 32, label %9
i8 124, label %18
]
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
11: ; preds = %6
store i32 0, i32* %1
%12 = icmp eq i32* %2, null
br i1 %12, label %14, label %13
13: ; preds = %11
store i32 1, i32* %2
br label %14
14: ; preds = %13, %11
%15 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %11 ]
%16 = tail call i8* @findhome(i8* %7)
%17 = tail call i32* @fopen(i8* %16, i8* %15)
br label %110
18: ; preds = %6
%19 = getelementptr inbounds i8, i8* %7, i64 1
%20 = tail call i8* @findhome(i8* %19)
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%22 = call i64 @pipe(i32* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = icmp eq i32* %2, null
br i1 %25, label %44, label %26
26: ; preds = %24
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%28 = call i64 @pipe(i32* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %26, %18
%31 = load i32, i32* @stdout
%32 = call i32 @fileno(i32 %31)
%33 = call i64 @isatty(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %30
%36 = call i32 @move(i32 1, i32 0)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = call i32 (...) @clrtoeol()
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i64 (i8*, ...) @printw(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %38, %35, %30
store i32 0, i32* %1
br label %110
44: ; preds = %26, %24
%45 = zext i1 %25 to i32
%46 = call i32 @deraw(i32 %45)
%47 = call i32 (...) @fork()
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %70
49: ; preds = %44
%50 = call i32 @close(i32 0)
%51 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %21
%55 = call i32 @dup(i32 %54)
br i1 %25, label %64, label %56
56: ; preds = %49
%57 = call i32 @close(i32 1)
%58 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%62 = load i32, i32* %61
%63 = call i32 @dup(i32 %62)
br label %64
64: ; preds = %56, %49
%65 = load i32, i32* @SIGINT
%66 = load i32, i32* @SIG_DFL
%67 = call i32 @signal(i32 %65, i32 %66)
%68 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %20, i32 0)
%69 = call i32 @exit(i32 -127)
unreachable
70: ; preds = %44
store i32 %47, i32* %1
%71 = select i1 %25, i64 1, i64 2
%72 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 %71
%73 = load i32, i32* %72
%74 = select i1 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%75 = call i32* @fdopen(i32 %73, i8* %74)
%76 = icmp eq i32* %75, null
br i1 %76, label %77, label %101
77: ; preds = %70
%78 = call i32 @kill(i32 %47, i32 9)
%79 = load i32, i32* @stdout
%80 = call i32 @fileno(i32 %79)
%81 = call i64 @isatty(i32 %80)
%82 = icmp eq i64 %81, 0
br i1 %82, label %92, label %83
83: ; preds = %77
%84 = call i32 @move(i32 1, i32 0)
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %92
86: ; preds = %83
%87 = call i32 (...) @clrtoeol()
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = select i1 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0)
%91 = call i64 (i8*, ...) @printw(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %90)
br label %92
92: ; preds = %89, %86, %83, %77
%93 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%94 = load i32, i32* %93
%95 = call i32 @close(i32 %94)
br i1 %25, label %100, label %96
96: ; preds = %92
%97 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%98 = load i32, i32* %97
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %96, %92
store i32 0, i32* %1
br label %110
101: ; preds = %70
%102 = load i32, i32* %21
%103 = call i32 @close(i32 %102)
br i1 %25, label %110, label %104
104: ; preds = %101
%105 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%106 = load i32, i32* %105
%107 = call i32 @close(i32 %106)
%108 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%109 = load i32, i32* %108
store i32 %109, i32* %2
br label %110
110: ; preds = %101, %104, %100, %43, %14
%111 = phi i32* [ %17, %14 ], [ null, %43 ], [ null, %100 ], [ %75, %104 ], [ %75, %101 ]
ret i32* %111
}