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

/* Variables and functions */
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ pipe(int *);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
FILE *Xpms_popen(char *cmd, const char *type) {
  pid_t pid;
  int pfd[2];
  int rpipe = 0, wpipe = 0, i;
  char **argv;
  char *ptr;
  char *cmdcpy;
  if ((cmd == NULL) || (strlen(cmd) == 0))
    return (NULL);
  if (type[0] != 'r' && type[0] != 'w')
    return (NULL);
  if ((cmdcpy = strdup(cmd)) == NULL)
    return (NULL);
  argv = NULL;
  if ((ptr = strtok(cmdcpy, " ")) == NULL) {
    free(cmdcpy);
    return (NULL);
  }
  for (i = 0;; i++) {
    if ((argv = (char **)realloc(argv, (i + 1) * sizeof(char *))) == NULL) {
      free(cmdcpy);
      return (NULL);
    }
    if ((*(argv + i) = (char *)malloc((strlen(ptr) + 1) * sizeof(char))) ==
        NULL) {
      free(cmdcpy);
      return (NULL);
    }
    strcpy(argv[i], ptr);
    if ((ptr = strtok(NULL, " ")) == NULL) {
      if ((argv = (char **)realloc(argv, (i + 2) * sizeof(char *))) == NULL) {
        free(cmdcpy);
        return (NULL);
      }
      argv[i + 1] = NULL;
      break;
    }
  }
  if (type[0] == 'r')
    rpipe = 1;
  else
    wpipe = 1;
  if (pipe(pfd) < 0) {
    free(cmdcpy);
    return (NULL);
  }
  if ((pid = fork()) < 0) {
    close(pfd[0]);
    close(pfd[1]);
    free(cmdcpy);
    return (NULL);
  }
  if (pid == 0) {
    if ((pid = fork()) < 0) {
      close(pfd[0]);
      close(pfd[1]);
      free(cmdcpy);
      return (NULL);
    }
    if (pid > 0) {
      exit(0);
    }
    if (rpipe) {
      close(pfd[0]);
      dup2(STDOUT_FILENO, STDERR_FILENO);
      if (pfd[1] != STDOUT_FILENO)
        dup2(pfd[1], STDOUT_FILENO);
    } else {
      close(pfd[1]);
      if (pfd[0] != STDIN_FILENO)
        dup2(pfd[0], STDIN_FILENO);
    }
    if (strchr(argv[0], '/') == NULL)
      execvp(argv[0], argv);
    else
      execv(argv[0], argv);
    close(pfd[0]);
    close(pfd[1]);
    free(cmdcpy);
    return (NULL);
  } else {
    waitpid(pid, NULL, 0);
    if (rpipe) {
      close(pfd[1]);
      free(cmdcpy);
      return (fdopen(pfd[0], "r"));
    } else {
      close(pfd[0]);
      free(cmdcpy);
      return (fdopen(pfd[1], "w"));
    }
  }
}


x86 assembly (clang, -O0):
<Xpms_popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     124b <Xpms_popen+0x3b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    1258 <Xpms_popen+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
je     1285 <Xpms_popen+0x75>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
je     1285 <Xpms_popen+0x75>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x10(%rbp),%rdi
call   1110 <strdup@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    12a9 <Xpms_popen+0x99>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
movq   $0x0,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xd44(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    12e5 <Xpms_popen+0xd5>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
movl   $0x0,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   10b0 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1327 <Xpms_popen+0x117>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    -0x40(%rbp),%rcx
movslq -0x34(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    136c <Xpms_popen+0x15c>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xc74(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    13f4 <Xpms_popen+0x1e4>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   10b0 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    13da <Xpms_popen+0x1ca>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x40(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
jmp    1407 <Xpms_popen+0x1f7>
jmp    13f9 <Xpms_popen+0x1e9>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12ec <Xpms_popen+0xdc>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1423 <Xpms_popen+0x213>
movl   $0x1,-0x2c(%rbp)
jmp    142a <Xpms_popen+0x21a>
movl   $0x1,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    1453 <Xpms_popen+0x243>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    148e <Xpms_popen+0x27e>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
cmpq   $0x0,-0x20(%rbp)
jne    15cd <Xpms_popen+0x3bd>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    14d4 <Xpms_popen+0x2c4>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
cmpq   $0x0,-0x20(%rbp)
jle    14e6 <Xpms_popen+0x2d6>
xor    %edi,%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
je     1537 <Xpms_popen+0x327>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    0x2af1(%rip),%rax
mov    (%rax),%edi
mov    0x2ad0(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x24(%rbp),%eax
mov    0x2ad7(%rip),%rcx
cmp    (%rcx),%eax
je     1532 <Xpms_popen+0x322>
mov    -0x24(%rbp),%edi
mov    0x2ac5(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
jmp    1567 <Xpms_popen+0x357>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%eax
mov    0x2a87(%rip),%rcx
cmp    (%rcx),%eax
je     1562 <Xpms_popen+0x352>
mov    -0x28(%rbp),%edi
mov    0x2a75(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
jmp    1567 <Xpms_popen+0x357>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
jne    1597 <Xpms_popen+0x387>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
call   10f0 <execvp@plt>
jmp    15a7 <Xpms_popen+0x397>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
call   1120 <execv@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10d0 <waitpid@plt>
cmpl   $0x0,-0x2c(%rbp)
je     160f <Xpms_popen+0x3ff>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%edi
lea    0xa01(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    1633 <Xpms_popen+0x423>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
lea    0x9da(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Xpms_popen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     144d <Xpms_popen+0x23d>
mov    %rsi,%r12
mov    %rdi,%rbx
call   1050 <strlen@plt>
test   %eax,%eax
je     144d <Xpms_popen+0x23d>
mov    (%r12),%al
cmp    $0x77,%al
je     124a <Xpms_popen+0x3a>
cmp    $0x72,%al
jne    144d <Xpms_popen+0x23d>
mov    %rbx,%rdi
call   1110 <strdup@plt>
test   %rax,%rax
je     144d <Xpms_popen+0x23d>
mov    %rax,%rbx
lea    0xd9b(%rip),%rsi
mov    %rax,%rdi
call   10e0 <strtok@plt>
test   %rax,%rax
je     1445 <Xpms_popen+0x235>
mov    %rax,%r13
mov    %rbx,0x10(%rsp)
mov    $0x8,%ebp
xor    %r14d,%r14d
lea    0xd73(%rip),%r15
xor    %ebx,%ebx
nop
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <realloc@plt>
test   %rax,%rax
je     1361 <Xpms_popen+0x151>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,(%rbx,%r14,8)
test   %rax,%rax
je     1361 <Xpms_popen+0x151>
mov    %rax,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
mov    %r15,%rsi
call   10e0 <strtok@plt>
mov    %rax,%r13
add    $0x8,%ebp
add    $0x1,%r14
test   %rax,%rax
jne    1290 <Xpms_popen+0x80>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <realloc@plt>
test   %rax,%rax
je     1361 <Xpms_popen+0x151>
mov    %rax,%r15
mov    %r14d,%eax
movq   $0x0,(%r15,%rax,8)
mov    (%r12),%bl
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
js     1361 <Xpms_popen+0x151>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     13aa <Xpms_popen+0x19a>
je     136b <Xpms_popen+0x15b>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <waitpid@plt>
cmp    $0x72,%bl
jne    13c6 <Xpms_popen+0x1b6>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%edi
lea    0xca6(%rip),%rsi
jmp    13e4 <Xpms_popen+0x1d4>
mov    0x10(%rsp),%rdi
jmp    1448 <Xpms_popen+0x238>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     13aa <Xpms_popen+0x19a>
jne    145e <Xpms_popen+0x24e>
cmp    $0x72,%bl
jne    13eb <Xpms_popen+0x1db>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x2c5e(%rip),%rbx
mov    (%rbx),%edi
mov    0x2c3d(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0xc(%rsp),%edi
mov    (%rbx),%esi
jmp    1401 <Xpms_popen+0x1f1>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x10(%rsp),%rdi
jmp    1448 <Xpms_popen+0x238>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%edi
lea    0xc20(%rip),%rsi
call   10c0 <fdopen@plt>
jmp    144f <Xpms_popen+0x23f>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2bd1(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%edi
mov    0x10(%rsp),%rbx
je     140f <Xpms_popen+0x1ff>
call   1060 <dup2@plt>
mov    (%r15),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
test   %rax,%rax
je     142e <Xpms_popen+0x21e>
call   1120 <execv@plt>
jmp    1433 <Xpms_popen+0x223>
call   10f0 <execvp@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define i32* @Xpms_popen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8**
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
store i32 0, i32* %9
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16, %2
store i32* null, i32** %3
br label %241
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 114
br i1 %26, label %27, label %34
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 119
br i1 %32, label %33, label %34
33: ; preds = %27
store i32* null, i32** %3
br label %241
34: ; preds = %27, %21
%35 = load i8*, i8** %4
%36 = call i8* @strdup(i8* %35)
store i8* %36, i8** %13
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %34
store i32* null, i32** %3
br label %241
39: ; preds = %34
store i8** null, i8*** %11
%40 = load i8*, i8** %13
%41 = call i8* @strtok(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %41, i8** %12
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8*, i8** %13
%45 = call i32 @free(i8* %44)
store i32* null, i32** %3
br label %241
46: ; preds = %39
store i32 0, i32* %10
br label %47
47: ; preds = %107, %46
%48 = load i8**, i8*** %11
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = mul i64 %51, 8
%53 = trunc i64 %52 to i32
%54 = call i64 @realloc(i8** %48, i32 %53)
%55 = inttoptr i64 %54 to i8**
store i8** %55, i8*** %11
%56 = icmp eq i8** %55, null
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = load i8*, i8** %13
%59 = call i32 @free(i8* %58)
store i32* null, i32** %3
br label %241
60: ; preds = %47
%61 = load i8*, i8** %12
%62 = call i32 @strlen(i8* %61)
%63 = add nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = mul i64 %64, 1
%66 = trunc i64 %65 to i32
%67 = call i64 @malloc(i32 %66)
%68 = inttoptr i64 %67 to i8*
%69 = load i8**, i8*** %11
%70 = load i32, i32* %10
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %69, i64 %71
store i8* %68, i8** %72
%73 = icmp eq i8* %68, null
br i1 %73, label %74, label %77
74: ; preds = %60
%75 = load i8*, i8** %13
%76 = call i32 @free(i8* %75)
store i32* null, i32** %3
br label %241
77: ; preds = %60
%78 = load i8**, i8*** %11
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8*, i8** %78, i64 %80
%82 = load i8*, i8** %81
%83 = load i8*, i8** %12
%84 = call i32 @strcpy(i8* %82, i8* %83)
%85 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %85, i8** %12
%86 = icmp eq i8* %85, null
br i1 %86, label %87, label %106
87: ; preds = %77
%88 = load i8**, i8*** %11
%89 = load i32, i32* %10
%90 = add nsw i32 %89, 2
%91 = sext i32 %90 to i64
%92 = mul i64 %91, 8
%93 = trunc i64 %92 to i32
%94 = call i64 @realloc(i8** %88, i32 %93)
%95 = inttoptr i64 %94 to i8**
store i8** %95, i8*** %11
%96 = icmp eq i8** %95, null
br i1 %96, label %97, label %100
97: ; preds = %87
%98 = load i8*, i8** %13
%99 = call i32 @free(i8* %98)
store i32* null, i32** %3
br label %241
100: ; preds = %87
%101 = load i8**, i8*** %11
%102 = load i32, i32* %10
%103 = add nsw i32 %102, 1
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8*, i8** %101, i64 %104
store i8* null, i8** %105
br label %110
106: ; preds = %77
br label %107
107: ; preds = %106
%108 = load i32, i32* %10
%109 = add nsw i32 %108, 1
store i32 %109, i32* %10
br label %47
110: ; preds = %100
%111 = load i8*, i8** %5
%112 = getelementptr inbounds i8, i8* %111, i64 0
%113 = load i8, i8* %112
%114 = sext i8 %113 to i32
%115 = icmp eq i32 %114, 114
br i1 %115, label %116, label %117
116: ; preds = %110
store i32 1, i32* %8
br label %118
117: ; preds = %110
store i32 1, i32* %9
br label %118
118: ; preds = %117, %116
%119 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%120 = call i64 @pipe(i32* %119)
%121 = icmp slt i64 %120, 0
br i1 %121, label %122, label %125
122: ; preds = %118
%123 = load i8*, i8** %13
%124 = call i32 @free(i8* %123)
store i32* null, i32** %3
br label %241
125: ; preds = %118
%126 = call i64 (...) @fork()
store i64 %126, i64* %6
%127 = icmp slt i64 %126, 0
br i1 %127, label %128, label %137
128: ; preds = %125
%129 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%130 = load i32, i32* %129
%131 = call i32 @close(i32 %130)
%132 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%133 = load i32, i32* %132
%134 = call i32 @close(i32 %133)
%135 = load i8*, i8** %13
%136 = call i32 @free(i8* %135)
store i32* null, i32** %3
br label %241
137: ; preds = %125
%138 = load i64, i64* %6
%139 = icmp eq i64 %138, 0
br i1 %139, label %140, label %218
140: ; preds = %137
%141 = call i64 (...) @fork()
store i64 %141, i64* %6
%142 = icmp slt i64 %141, 0
br i1 %142, label %143, label %152
143: ; preds = %140
%144 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%145 = load i32, i32* %144
%146 = call i32 @close(i32 %145)
%147 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%148 = load i32, i32* %147
%149 = call i32 @close(i32 %148)
%150 = load i8*, i8** %13
%151 = call i32 @free(i8* %150)
store i32* null, i32** %3
br label %241
152: ; preds = %140
%153 = load i64, i64* %6
%154 = icmp sgt i64 %153, 0
br i1 %154, label %155, label %157
155: ; preds = %152
%156 = call i32 @exit(i32 0)
unreachable
157: ; preds = %152
%158 = load i32, i32* %8
%159 = icmp ne i32 %158, 0
br i1 %159, label %160, label %177
160: ; preds = %157
%161 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%162 = load i32, i32* %161
%163 = call i32 @close(i32 %162)
%164 = load i32, i32* @STDOUT_FILENO
%165 = load i32, i32* @STDERR_FILENO
%166 = call i32 @dup2(i32 %164, i32 %165)
%167 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%168 = load i32, i32* %167
%169 = load i32, i32* @STDOUT_FILENO
%170 = icmp ne i32 %168, %169
br i1 %170, label %171, label %176
171: ; preds = %160
%172 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%173 = load i32, i32* %172
%174 = load i32, i32* @STDOUT_FILENO
%175 = call i32 @dup2(i32 %173, i32 %174)
br label %176
176: ; preds = %171, %160
br label %191
177: ; preds = %157
%178 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%179 = load i32, i32* %178
%180 = call i32 @close(i32 %179)
%181 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%182 = load i32, i32* %181
%183 = load i32, i32* @STDIN_FILENO
%184 = icmp ne i32 %182, %183
br i1 %184, label %185, label %190
185: ; preds = %177
%186 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%187 = load i32, i32* %186
%188 = load i32, i32* @STDIN_FILENO
%189 = call i32 @dup2(i32 %187, i32 %188)
br label %190
190: ; preds = %185, %177
br label %191
191: ; preds = %190, %176
%192 = load i8**, i8*** %11
%193 = getelementptr inbounds i8*, i8** %192, i64 0
%194 = load i8*, i8** %193
%195 = call i32* @strchr(i8* %194, i8 signext 47)
%196 = icmp eq i32* %195, null
br i1 %196, label %197, label %203
197: ; preds = %191
%198 = load i8**, i8*** %11
%199 = getelementptr inbounds i8*, i8** %198, i64 0
%200 = load i8*, i8** %199
%201 = load i8**, i8*** %11
%202 = call i32 @execvp(i8* %200, i8** %201)
br label %209
203: ; preds = %191
%204 = load i8**, i8*** %11
%205 = getelementptr inbounds i8*, i8** %204, i64 0
%206 = load i8*, i8** %205
%207 = load i8**, i8*** %11
%208 = call i32 @execv(i8* %206, i8** %207)
br label %209
209: ; preds = %203, %197
%210 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%211 = load i32, i32* %210
%212 = call i32 @close(i32 %211)
%213 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%214 = load i32, i32* %213
%215 = call i32 @close(i32 %214)
%216 = load i8*, i8** %13
%217 = call i32 @free(i8* %216)
store i32* null, i32** %3
br label %241
218: ; preds = %137
%219 = load i64, i64* %6
%220 = call i32 @waitpid(i64 %219, i32* null, i32 0)
%221 = load i32, i32* %8
%222 = icmp ne i32 %221, 0
br i1 %222, label %223, label %232
223: ; preds = %218
%224 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%225 = load i32, i32* %224
%226 = call i32 @close(i32 %225)
%227 = load i8*, i8** %13
%228 = call i32 @free(i8* %227)
%229 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%230 = load i32, i32* %229
%231 = call i32* @fdopen(i32 %230, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %231, i32** %3
br label %241
232: ; preds = %218
%233 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%234 = load i32, i32* %233
%235 = call i32 @close(i32 %234)
%236 = load i8*, i8** %13
%237 = call i32 @free(i8* %236)
%238 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%239 = load i32, i32* %238
%240 = call i32* @fdopen(i32 %239, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %240, i32** %3
br label %241
241: ; preds = %232, %223, %209, %143, %128, %122, %97, %74, %57, %43, %38, %33, %20
%242 = load i32*, i32** %3
ret i32* %242
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define noalias i32* @Xpms_popen(i8* %0, i8* nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %144, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %144, label %9
9: ; preds = %6
%10 = load i8, i8* %1
switch i8 %10, label %144 [
i8 114, label %11
i8 119, label %11
]
11: ; preds = %9, %9
%12 = tail call i8* @strdup(i8* %0)
%13 = icmp eq i8* %12, null
br i1 %13, label %144, label %14
14: ; preds = %11
%15 = tail call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @free(i8* %12)
br label %144
19: ; preds = %14, %40
%20 = phi i64 [ %23, %40 ], [ 0, %14 ]
%21 = phi i8** [ %27, %40 ], [ null, %14 ]
%22 = phi i8* [ %42, %40 ], [ %15, %14 ]
%23 = add nuw i64 %20, 1
%24 = trunc i64 %23 to i32
%25 = shl i32 %24, 3
%26 = tail call i64 @realloc(i8** %21, i32 %25)
%27 = inttoptr i64 %26 to i8**
%28 = icmp eq i64 %26, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = tail call i32 @free(i8* %12)
br label %144
31: ; preds = %19
%32 = tail call i32 @strlen(i8* %22)
%33 = add nsw i32 %32, 1
%34 = tail call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
%36 = getelementptr inbounds i8*, i8** %27, i64 %20
store i8* %35, i8** %36
%37 = icmp eq i64 %34, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = tail call i32 @free(i8* %12)
br label %144
40: ; preds = %31
%41 = tail call i32 @strcpy(i8* %35, i8* %22)
%42 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %19
44: ; preds = %40
%45 = inttoptr i64 %26 to i8**
%46 = trunc i64 %20 to i32
%47 = shl i32 %46, 3
%48 = add i32 %47, 16
%49 = tail call i64 @realloc(i8** %45, i32 %48)
%50 = inttoptr i64 %49 to i8**
%51 = icmp eq i64 %49, 0
br i1 %51, label %52, label %54
52: ; preds = %44
%53 = tail call i32 @free(i8* %12)
br label %144
54: ; preds = %44
%55 = and i64 %23, 4294967295
%56 = getelementptr inbounds i8*, i8** %50, i64 %55
store i8* null, i8** %56
%57 = load i8, i8* %1
%58 = icmp eq i8 %57, 114
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%60 = call i64 @pipe(i32* %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %54
%63 = call i32 @free(i8* %12)
br label %144
64: ; preds = %54
%65 = call i64 (...) @fork()
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %74
67: ; preds = %64
%68 = load i32, i32* %59
%69 = call i32 @close(i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = call i32 @free(i8* %12)
br label %144
74: ; preds = %64
%75 = icmp eq i64 %65, 0
br i1 %75, label %76, label %128
76: ; preds = %74
%77 = call i64 (...) @fork()
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %76
%80 = load i32, i32* %59
%81 = call i32 @close(i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
%85 = call i32 @free(i8* %12)
br label %144
86: ; preds = %76
%87 = icmp eq i64 %77, 0
br i1 %87, label %90, label %88
88: ; preds = %86
%89 = call i32 @exit(i32 0)
unreachable
90: ; preds = %86
br i1 %58, label %91, label %103
91: ; preds = %90
%92 = load i32, i32* %59
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* @STDOUT_FILENO
%95 = load i32, i32* @STDERR_FILENO
%96 = call i32 @dup2(i32 %94, i32 %95)
%97 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%98 = load i32, i32* %97
%99 = load i32, i32* @STDOUT_FILENO
%100 = icmp eq i32 %98, %99
br i1 %100, label %112, label %101
101: ; preds = %91
%102 = call i32 @dup2(i32 %98, i32 %99)
br label %112
103: ; preds = %90
%104 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%105 = load i32, i32* %104
%106 = call i32 @close(i32 %105)
%107 = load i32, i32* %59
%108 = load i32, i32* @STDIN_FILENO
%109 = icmp eq i32 %107, %108
br i1 %109, label %112, label %110
110: ; preds = %103
%111 = call i32 @dup2(i32 %107, i32 %108)
br label %112
112: ; preds = %103, %110, %91, %101
%113 = load i8*, i8** %50
%114 = call i32* @strchr(i8* %113, i8 signext 47)
%115 = icmp eq i32* %114, null
%116 = load i8*, i8** %50
br i1 %115, label %117, label %119
117: ; preds = %112
%118 = call i32 @execvp(i8* %116, i8** %50)
br label %121
119: ; preds = %112
%120 = call i32 @execv(i8* %116, i8** %50)
br label %121
121: ; preds = %119, %117
%122 = load i32, i32* %59
%123 = call i32 @close(i32 %122)
%124 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%125 = load i32, i32* %124
%126 = call i32 @close(i32 %125)
%127 = call i32 @free(i8* %12)
br label %144
128: ; preds = %74
%129 = call i32 @waitpid(i64 %65, i32* null, i32 0)
br i1 %58, label %130, label %137
130: ; preds = %128
%131 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%132 = load i32, i32* %131
%133 = call i32 @close(i32 %132)
%134 = call i32 @free(i8* %12)
%135 = load i32, i32* %59
%136 = call i32* @fdopen(i32 %135, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %144
137: ; preds = %128
%138 = load i32, i32* %59
%139 = call i32 @close(i32 %138)
%140 = call i32 @free(i8* %12)
%141 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%142 = load i32, i32* %141
%143 = call i32* @fdopen(i32 %142, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %144
144: ; preds = %11, %9, %2, %6, %137, %130, %121, %79, %67, %62, %52, %38, %29, %17
%145 = phi i32* [ null, %17 ], [ null, %29 ], [ null, %38 ], [ null, %52 ], [ null, %62 ], [ null, %67 ], [ null, %79 ], [ null, %121 ], [ %136, %130 ], [ %143, %137 ], [ null, %6 ], [ null, %2 ], [ null, %9 ], [ null, %11 ]
ret i32* %145
}