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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SIG_SETMASK;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ chdir(unsigned char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ close_memstream(int /*<<< orphan*/ *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, unsigned char const *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int getc(int /*<<< orphan*/ *);
scalar_t__ isspace(char);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/);
size_t strlen(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ xfree(char *);
unsigned char *xstrdup(char *);
unsigned char *read_process_output(const unsigned char *cmd,
                                   const unsigned char *workdir,
                                   int max_ok_code, int redirect_stderr) {
  FILE *fin = 0;
  FILE *fout = 0;
  char *out_txt = 0;
  size_t out_len = 0;
  int pfd[2] = {-1, -1};
  int c, pid, status;
  sigset_t mask;
  if (!(fout = open_memstream(&out_txt, &out_len)))
    goto failed;
  if (pipe(pfd) < 0)
    goto failed;
  if ((pid = fork()) < 0)
    goto failed;
  if (!pid) {
    close(pfd[0]);
    dup2(pfd[1], 1);
    if (redirect_stderr)
      dup2(pfd[1], 2);
    close(pfd[1]);
    if (workdir)
      chdir(workdir);
    sigemptyset(&mask);
    sigprocmask(SIG_SETMASK, &mask, 0);
    execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
    _exit(1);
  }
  close(pfd[1]);
  pfd[1] = -1;
  if (!(fin = fdopen(pfd[0], "r")))
    goto failed;
  pfd[0] = -1;
  while ((c = getc(fin)) != EOF)
    putc(c, fout);
  waitpid(pid, &status, 0);
  fclose(fin);
  close_memstream(fout);
  fout = 0;
  c = 1;
  if (WIFEXITED(status) && (WEXITSTATUS(status) & 0xff) <= max_ok_code)
    c = 0;
  if (c) {
    xfree(out_txt);
    return xstrdup("");
  }
  out_len = strlen(out_txt);
  while (out_len > 0 && isspace(out_txt[out_len - 1]))
    out_txt[--out_len] = 0;
  return out_txt;
failed:
  if (pfd[0] >= 0)
    close(pfd[0]);
  if (pfd[1] >= 0)
    close(pfd[1]);
  if (fin)
    pclose(fin);
  if (fout)
    close_memstream(fout);
  xfree(out_txt);
  return 0;
}


x86 assembly (clang, -O0):
<read_process_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0xd53(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12d1 <read_process_output+0x61>
jmp    14f9 <read_process_output+0x289>
lea    -0x48(%rbp),%rdi
call   10e0 <pipe@plt>
cmp    $0x0,%rax
jge    12e9 <read_process_output+0x79>
jmp    14f9 <read_process_output+0x289>
mov    $0x0,%al
call   1190 <fork@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x0,%eax
jge    1301 <read_process_output+0x91>
jmp    14f9 <read_process_output+0x289>
cmpl   $0x0,-0x50(%rbp)
jne    1399 <read_process_output+0x129>
mov    -0x48(%rbp),%edi
call   10c0 <close@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%esi
call   1090 <dup2@plt>
cmpl   $0x0,-0x20(%rbp)
je     1337 <read_process_output+0xc7>
mov    -0x44(%rbp),%edi
mov    $0x2,%esi
call   1090 <dup2@plt>
mov    -0x44(%rbp),%edi
call   10c0 <close@plt>
cmpq   $0x0,-0x18(%rbp)
je     1353 <read_process_output+0xe3>
mov    -0x18(%rbp),%rdi
call   1080 <chdir@plt>
lea    -0x58(%rbp),%rdi
call   1110 <sigemptyset@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rcx
lea    0xc8d(%rip),%rsi
lea    0xc8e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1170 <execl@plt>
mov    $0x1,%edi
call   1050 <_exit@plt>
mov    -0x44(%rbp),%edi
call   10c0 <close@plt>
movl   $0xffffffff,-0x44(%rbp)
mov    -0x48(%rbp),%edi
lea    0xc61(%rip),%rsi
call   1120 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    13ca <read_process_output+0x15a>
jmp    14f9 <read_process_output+0x289>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1180 <getc@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2bfc(%rip),%rcx
cmp    (%rcx),%eax
je     13fd <read_process_output+0x18d>
mov    -0x4c(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1100 <putc@plt>
jmp    13d1 <read_process_output+0x161>
mov    -0x50(%rbp),%edi
lea    -0x54(%rbp),%rsi
xor    %edx,%edx
call   1140 <waitpid@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1160 <close_memstream@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x54(%rbp),%edi
call   1150 <WIFEXITED@plt>
cmp    $0x0,%rax
je     145b <read_process_output+0x1eb>
mov    -0x54(%rbp),%edi
call   1130 <WEXITSTATUS@plt>
and    $0xff,%eax
cmp    -0x1c(%rbp),%eax
jg     145b <read_process_output+0x1eb>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1483 <read_process_output+0x213>
mov    -0x38(%rbp),%rdi
call   10a0 <xfree@plt>
lea    0xb9d(%rip),%rdi
call   10d0 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1556 <read_process_output+0x2e6>
mov    -0x38(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x40(%rbp)
mov    %al,-0x59(%rbp)
jbe    14bf <read_process_output+0x24f>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   10f0 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    14cf <read_process_output+0x25f>
jmp    14ec <read_process_output+0x27c>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x40(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    1490 <read_process_output+0x220>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1556 <read_process_output+0x2e6>
cmpl   $0x0,-0x48(%rbp)
jl     150b <read_process_output+0x29b>
mov    -0x48(%rbp),%edi
call   10c0 <close@plt>
cmpl   $0x0,-0x44(%rbp)
jl     151d <read_process_output+0x2ad>
mov    -0x44(%rbp),%edi
call   10c0 <close@plt>
cmpq   $0x0,-0x28(%rbp)
je     1531 <read_process_output+0x2c1>
mov    -0x28(%rbp),%rdi
call   10b0 <pclose@plt>
cmpq   $0x0,-0x30(%rbp)
je     1545 <read_process_output+0x2d5>
mov    -0x30(%rbp),%rdi
call   1160 <close_memstream@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <xfree@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_process_output>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0xffffffffffffffff,0x8(%rsp)
ff ff
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,%rbx
test   %rax,%rax
je     13a0 <read_process_output+0x140>
lea    0x8(%rsp),%rdi
call   10d0 <pipe@plt>
test   %rax,%rax
js     13a0 <read_process_output+0x140>
xor    %eax,%eax
call   1180 <fork@plt>
test   %eax,%eax
js     13a0 <read_process_output+0x140>
je     144f <read_process_output+0x1ef>
mov    %eax,%r13d
mov    0xc(%rsp),%edi
call   10b0 <close@plt>
movl   $0xffffffff,0xc(%rsp)
ff
mov    0x8(%rsp),%edi
lea    0xd15(%rip),%rsi
call   1110 <fdopen@plt>
test   %rax,%rax
je     13a0 <read_process_output+0x140>
mov    %rax,%rbp
movl   $0xffffffff,0x8(%rsp)
ff
mov    %rax,%rdi
call   1170 <getc@plt>
mov    0x2cc2(%rip),%r15
cmp    (%r15),%eax
je     1347 <read_process_output+0xe7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%edi
mov    %rbx,%rsi
call   10f0 <putc@plt>
mov    %rbp,%rdi
call   1170 <getc@plt>
cmp    (%r15),%eax
jne    1330 <read_process_output+0xd0>
lea    0x1c(%rsp),%rsi
mov    %r13d,%edi
xor    %edx,%edx
call   1130 <waitpid@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1150 <close_memstream@plt>
mov    0x1c(%rsp),%edi
call   1140 <WIFEXITED@plt>
test   %rax,%rax
je     1385 <read_process_output+0x125>
mov    0x1c(%rsp),%edi
call   1120 <WEXITSTATUS@plt>
movzbl %al,%eax
cmp    %r14d,%eax
jle    13e5 <read_process_output+0x185>
mov    0x10(%rsp),%rdi
call   10a0 <xfree@plt>
lea    0xc74(%rip),%rdi
call   10c0 <xstrdup@plt>
mov    %rax,%rbx
jmp    13d3 <read_process_output+0x173>
mov    0x8(%rsp),%edi
test   %edi,%edi
js     13ad <read_process_output+0x14d>
call   10b0 <close@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
js     13ba <read_process_output+0x15a>
call   10b0 <close@plt>
test   %rbx,%rbx
je     13c7 <read_process_output+0x167>
mov    %rbx,%rdi
call   1150 <close_memstream@plt>
mov    0x10(%rsp),%rdi
call   10a0 <xfree@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     13d3 <read_process_output+0x173>
movsbl -0x1(%rax,%rbx,1),%edi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
call   10e0 <isspace@plt>
test   %rax,%rax
je     1448 <read_process_output+0x1e8>
mov    0x10(%rsp),%rax
mov    0x20(%rsp),%rcx
lea    -0x1(%rcx),%rdx
mov    %rdx,0x20(%rsp)
movb   $0x0,-0x1(%rax,%rcx,1)
mov    0x20(%rsp),%rax
test   %rax,%rax
je     1448 <read_process_output+0x1e8>
mov    0x10(%rsp),%rcx
movsbl -0x1(%rax,%rcx,1),%edi
jmp    1410 <read_process_output+0x1b0>
mov    0x10(%rsp),%rbx
jmp    13d3 <read_process_output+0x173>
mov    0x8(%rsp),%edi
call   10b0 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1090 <dup2@plt>
test   %ebp,%ebp
je     1478 <read_process_output+0x218>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1090 <dup2@plt>
mov    0xc(%rsp),%edi
call   10b0 <close@plt>
test   %r12,%r12
je     148e <read_process_output+0x22e>
mov    %r12,%rdi
call   1080 <chdir@plt>
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   1100 <sigemptyset@plt>
mov    0x2b46(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xb4b(%rip),%rdi
lea    0xb4c(%rip),%rdx
mov    %rdi,%rsi
mov    %r15,%rcx
xor    %r8d,%r8d
call   1160 <execl@plt>
mov    $0x1,%edi
call   1050 <_exit@plt>

LLVM IR (-O0):
@__const.read_process_output.pfd = [2 x i32] [i32 -1, i32 -1]
@SIG_SETMASK = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.3 = [1 x i8] zeroinitializer
define i8* @read_process_output(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca [2 x i32]
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32* null, i32** %10
store i32* null, i32** %11
store i8* null, i8** %12
store i64 0, i64* %13
%19 = bitcast [2 x i32]* %14 to i8*
%20 = call i32* @open_memstream(i8** %12, i64* %13)
store i32* %20, i32** %11
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %4
br label %132
23: ; preds = %4
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%25 = call i64 @pipe(i32* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %132
28: ; preds = %23
%29 = call i32 (...) @fork()
store i32 %29, i32* %16
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %132
32: ; preds = %28
%33 = load i32, i32* %16
%34 = icmp ne i32 %33, 0
br i1 %34, label %64, label %35
35: ; preds = %32
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @dup2(i32 %40, i32 1)
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %35
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @dup2(i32 %46, i32 2)
br label %48
48: ; preds = %44, %35
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i8*, i8** %7
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i8*, i8** %7
%56 = call i32 @chdir(i8* %55)
br label %57
57: ; preds = %54, %48
%58 = call i32 @sigemptyset(i32* %18)
%59 = load i32, i32* @SIG_SETMASK
%60 = call i32 @sigprocmask(i32 %59, i32* %18, i32 0)
%61 = load i8*, i8** %6
%62 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %61, i32* null)
%63 = call i32 @_exit(i32 1)
unreachable
64: ; preds = %32
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
store i32 -1, i32* %68
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%70 = load i32, i32* %69
%71 = call i32* @fdopen(i32 %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %71, i32** %10
%72 = icmp ne i32* %71, null
br i1 %72, label %74, label %73
73: ; preds = %64
br label %132
74: ; preds = %64
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
store i32 -1, i32* %75
br label %76
76: ; preds = %81, %74
%77 = load i32*, i32** %10
%78 = call i32 @getc(i32* %77)
store i32 %78, i32* %15
%79 = load i32, i32* @EOF
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = load i32, i32* %15
%83 = load i32*, i32** %11
%84 = call i32 @putc(i32 %82, i32* %83)
br label %76
85: ; preds = %76
%86 = load i32, i32* %16
%87 = call i32 @waitpid(i32 %86, i32* %17, i32 0)
%88 = load i32*, i32** %10
%89 = call i32 @fclose(i32* %88)
%90 = load i32*, i32** %11
%91 = call i32 @close_memstream(i32* %90)
store i32* null, i32** %11
store i32 1, i32* %15
%92 = load i32, i32* %17
%93 = call i64 @WIFEXITED(i32 %92)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %102
95: ; preds = %85
%96 = load i32, i32* %17
%97 = call i32 @WEXITSTATUS(i32 %96)
%98 = and i32 %97, 255
%99 = load i32, i32* %8
%100 = icmp sle i32 %98, %99
br i1 %100, label %101, label %102
101: ; preds = %95
store i32 0, i32* %15
br label %102
102: ; preds = %101, %95, %85
%103 = load i32, i32* %15
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %109
105: ; preds = %102
%106 = load i8*, i8** %12
%107 = call i32 @xfree(i8* %106)
%108 = call i8* @xstrdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
store i8* %108, i8** %5
br label %163
109: ; preds = %102
%110 = load i8*, i8** %12
%111 = call i64 @strlen(i8* %110)
store i64 %111, i64* %13
br label %112
112: ; preds = %125, %109
%113 = load i64, i64* %13
%114 = icmp ugt i64 %113, 0
br i1 %114, label %115, label %123
115: ; preds = %112
%116 = load i8*, i8** %12
%117 = load i64, i64* %13
%118 = sub i64 %117, 1
%119 = getelementptr inbounds i8, i8* %116, i64 %118
%120 = load i8, i8* %119
%121 = call i64 @isspace(i8 signext %120)
%122 = icmp ne i64 %121, 0
br label %123
123: ; preds = %115, %112
%124 = phi i1 [ false, %112 ], [ %122, %115 ]
br i1 %124, label %125, label %130
125: ; preds = %123
%126 = load i8*, i8** %12
%127 = load i64, i64* %13
%128 = add i64 %127, -1
store i64 %128, i64* %13
%129 = getelementptr inbounds i8, i8* %126, i64 %128
store i8 0, i8* %129
br label %112
130: ; preds = %123
%131 = load i8*, i8** %12
store i8* %131, i8** %5
br label %163
132: ; preds = %73, %31, %27, %22
%133 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%134 = load i32, i32* %133
%135 = icmp sge i32 %134, 0
br i1 %135, label %136, label %140
136: ; preds = %132
%137 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%138 = load i32, i32* %137
%139 = call i32 @close(i32 %138)
br label %140
140: ; preds = %136, %132
%141 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%142 = load i32, i32* %141
%143 = icmp sge i32 %142, 0
br i1 %143, label %144, label %148
144: ; preds = %140
%145 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%146 = load i32, i32* %145
%147 = call i32 @close(i32 %146)
br label %148
148: ; preds = %144, %140
%149 = load i32*, i32** %10
%150 = icmp ne i32* %149, null
br i1 %150, label %151, label %154
151: ; preds = %148
%152 = load i32*, i32** %10
%153 = call i32 @pclose(i32* %152)
br label %154
154: ; preds = %151, %148
%155 = load i32*, i32** %11
%156 = icmp ne i32* %155, null
br i1 %156, label %157, label %160
157: ; preds = %154
%158 = load i32*, i32** %11
%159 = call i32 @close_memstream(i32* %158)
br label %160
160: ; preds = %157, %154
%161 = load i8*, i8** %12
%162 = call i32 @xfree(i8* %161)
store i8* null, i8** %5
br label %163
163: ; preds = %160, %130, %105
%164 = load i8*, i8** %5
ret i8* %164
}

LLVM IR (-O2):
@SIG_SETMASK = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.3 = [1 x i8] zeroinitializer
define i8* @read_process_output(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = bitcast i64* %7 to [2 x i32]*
%9 = alloca i32
%10 = alloca i32
%11 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%12 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%13 = bitcast i64* %7 to i8*
store i64 -1, i64* %7
%14 = bitcast i32* %9 to i8*
%15 = bitcast i32* %10 to i8*
%16 = call i32* @open_memstream(i8** %5, i64* %6)
%17 = icmp eq i32* %16, null
br i1 %17, label %108, label %18
18: ; preds = %4
%19 = bitcast i64* %7 to i32*
%20 = call i64 @pipe(i32* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %108, label %22
22: ; preds = %18
%23 = call i32 (...) @fork()
%24 = icmp slt i32 %23, 0
br i1 %24, label %108, label %25
25: ; preds = %22
%26 = icmp eq i32 %23, 0
br i1 %26, label %27, label %49
27: ; preds = %25
%28 = load i32, i32* %19
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = icmp eq i32 %3, 0
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = load i32, i32* %30
%36 = call i32 @dup2(i32 %35, i32 2)
br label %37
37: ; preds = %34, %27
%38 = load i32, i32* %30
%39 = call i32 @close(i32 %38)
%40 = icmp eq i8* %1, null
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @chdir(i8* %1)
br label %43
43: ; preds = %41, %37
%44 = call i32 @sigemptyset(i32* %10)
%45 = load i32, i32* @SIG_SETMASK
%46 = call i32 @sigprocmask(i32 %45, i32* %10, i32 0)
%47 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 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)
%48 = call i32 @_exit(i32 1)
unreachable
49: ; preds = %25
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %50
%53 = load i32, i32* %19
%54 = call i32* @fdopen(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%55 = icmp eq i32* %54, null
br i1 %55, label %108, label %56
56: ; preds = %49
store i32 -1, i32* %19
%57 = call i32 @getc(i32* %54)
%58 = load i32, i32* @EOF
%59 = icmp eq i32 %57, %58
br i1 %59, label %66, label %60
60: ; preds = %56, %60
%61 = phi i32 [ %63, %60 ], [ %57, %56 ]
%62 = call i32 @putc(i32 %61, i32* %16)
%63 = call i32 @getc(i32* %54)
%64 = load i32, i32* @EOF
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %60
66: ; preds = %60, %56
%67 = call i32 @waitpid(i32 %23, i32* %9, i32 0)
%68 = call i32 @fclose(i32* %54)
%69 = call i32 @close_memstream(i32* %16)
%70 = load i32, i32* %9
%71 = call i64 @WIFEXITED(i32 %70)
%72 = icmp eq i64 %71, 0
br i1 %72, label %78, label %73
73: ; preds = %66
%74 = load i32, i32* %9
%75 = call i32 @WEXITSTATUS(i32 %74)
%76 = and i32 %75, 255
%77 = icmp sgt i32 %76, %2
br i1 %77, label %78, label %82
78: ; preds = %66, %73
%79 = load i8*, i8** %5
%80 = call i32 @xfree(i8* %79)
%81 = call i8* @xstrdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %126
82: ; preds = %73
%83 = load i8*, i8** %5
%84 = call i64 @strlen(i8* dereferenceable(1) %83)
store i64 %84, i64* %6
%85 = icmp eq i64 %84, 0
br i1 %85, label %126, label %86
86: ; preds = %82
%87 = add i64 %84, -1
%88 = getelementptr inbounds i8, i8* %83, i64 %87
%89 = load i8, i8* %88
%90 = call i64 @isspace(i8 signext %89)
%91 = icmp eq i64 %90, 0
br i1 %91, label %106, label %92
92: ; preds = %86, %99
%93 = load i8*, i8** %5
%94 = load i64, i64* %6
%95 = add i64 %94, -1
store i64 %95, i64* %6
%96 = getelementptr inbounds i8, i8* %93, i64 %95
store i8 0, i8* %96
%97 = load i64, i64* %6
%98 = icmp eq i64 %97, 0
br i1 %98, label %106, label %99
99: ; preds = %92
%100 = load i8*, i8** %5
%101 = add i64 %97, -1
%102 = getelementptr inbounds i8, i8* %100, i64 %101
%103 = load i8, i8* %102
%104 = call i64 @isspace(i8 signext %103)
%105 = icmp eq i64 %104, 0
br i1 %105, label %106, label %92
106: ; preds = %99, %92, %86
%107 = load i8*, i8** %5
br label %126
108: ; preds = %4, %49, %22, %18
%109 = bitcast i64* %7 to i32*
%110 = load i32, i32* %109
%111 = icmp sgt i32 %110, -1
br i1 %111, label %112, label %114
112: ; preds = %108
%113 = call i32 @close(i32 %110)
br label %114
114: ; preds = %112, %108
%115 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%116 = load i32, i32* %115
%117 = icmp sgt i32 %116, -1
br i1 %117, label %118, label %120
118: ; preds = %114
%119 = call i32 @close(i32 %116)
br label %120
120: ; preds = %114, %118
br i1 %17, label %123, label %121
121: ; preds = %120
%122 = call i32 @close_memstream(i32* %16)
br label %123
123: ; preds = %121, %120
%124 = load i8*, i8** %5
%125 = call i32 @xfree(i8* %124)
br label %126
126: ; preds = %82, %106, %123, %78
%127 = phi i8* [ null, %123 ], [ %81, %78 ], [ %107, %106 ], [ %83, %82 ]
ret i8* %127
}