#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*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *);
int /*<<< orphan*/ *fdopen(int, char const *);
int fork();
scalar_t__ pipe(int *);
FILE *popen(const char *command, const char *rw) {
  int pipe_fd[2];
  int pid, reading;
  if (pipe(pipe_fd) < 0)
    return NULL;
  reading = (rw[0] == 'r');
  pid = fork();
  if (pid < 0) {
    close(pipe_fd[0]);
    close(pipe_fd[1]);
    return NULL;
  }
  if (pid == 0) {
    close(pipe_fd[!reading]);
    close(reading);
    if (pipe_fd[reading] != reading) {
      dup2(pipe_fd[reading], reading);
      close(pipe_fd[reading]);
    }
    execl("/bin/sh", "sh", "-c", command, (char *)0);
    _exit(255);
  }
  close(pipe_fd[reading]);
  return fdopen(pipe_fd[!reading], rw);
}


x86 assembly (clang, -O0):
<popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11a0 <popen+0x30>
movq   $0x0,-0x8(%rbp)
jmp    129a <popen+0x12a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e6 <popen+0x76>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    129a <popen+0x12a>
cmpl   $0x0,-0x24(%rbp)
jne    126c <popen+0xfc>
cmpl   $0x0,-0x28(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cltq
mov    -0x20(%rbp,%rax,4),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%eax
cmp    -0x28(%rbp),%eax
je     123f <popen+0xcf>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x28(%rbp),%esi
call   1040 <dup2@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xdb6(%rip),%rdi
lea    0xdb4(%rip),%rsi
lea    0xdb0(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x28(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cltq
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11dd <popen+0x6d>
mov    0x0(%r13),%bl
xor    %r15d,%r15d
cmp    $0x72,%bl
sete   %r12b
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11cc <popen+0x5c>
je     11ee <popen+0x7e>
xor    %ebp,%ebp
xor    %eax,%eax
cmp    $0x72,%bl
setne  %bpl
sete   %al
mov    (%rsp,%rax,4),%edi
call   1050 <close@plt>
mov    (%rsp,%rbp,4),%edi
mov    %r13,%rsi
call   1070 <fdopen@plt>
jmp    11df <popen+0x6f>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12b,%r15b
xor    %ebp,%ebp
xor    %eax,%eax
cmp    $0x72,%bl
sete   %bpl
setne  %al
mov    (%rsp,%rax,4),%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    (%rsp,%rbp,4),%edi
cmp    %r15d,%edi
je     1227 <popen+0xb7>
mov    %r15d,%esi
call   1040 <dup2@plt>
mov    (%rsp,%rbp,4),%edi
call   1050 <close@plt>
lea    0xdd2(%rip),%rdi
lea    0xdd0(%rip),%rsi
lea    0xdcc(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define i32* @popen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32* null, i32** %3
br label %81
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 114
%19 = zext i1 %18 to i32
store i32 %19, i32* %8
%20 = call i32 (...) @fork()
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %13
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
store i32* null, i32** %3
br label %81
30: ; preds = %13
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %66
33: ; preds = %30
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = load i32, i32* %8
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %62
50: ; preds = %33
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = load i32, i32* %8
%56 = call i32 @dup2(i32 %54, i32 %55)
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %50, %33
%63 = load i8*, i8** %4
%64 = call i32 @execl(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %63, i8* null)
%65 = call i32 @_exit(i32 255)
unreachable
66: ; preds = %30
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %68
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %8
%73 = icmp ne i32 %72, 0
%74 = xor i1 %73, true
%75 = zext i1 %74 to i32
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %76
%78 = load i32, i32* %77
%79 = load i8*, i8** %5
%80 = call i32* @fdopen(i32 %78, i8* %79)
store i32* %80, i32** %3
br label %81
81: ; preds = %66, %23, %12
%82 = load i32*, i32** %3
ret i32* %82
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define noalias i32* @popen(i8* %0, i8* nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %50, label %8
8: ; preds = %2
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 114
%11 = zext i1 %10 to i32
%12 = call i32 (...) @fork()
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i32, i32* %5
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
br label %50
20: ; preds = %8
%21 = icmp eq i32 %12, 0
br i1 %21, label %22, label %40
22: ; preds = %20
%23 = xor i1 %10, true
%24 = zext i1 %23 to i64
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 @close(i32 %11)
%29 = zext i1 %10 to i64
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, %11
br i1 %32, label %37, label %33
33: ; preds = %22
%34 = call i32 @dup2(i32 %31, i32 %11)
%35 = load i32, i32* %30
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %33, %22
%38 = call i32 @execl(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* null)
%39 = call i32 @_exit(i32 255)
unreachable
40: ; preds = %20
%41 = zext i1 %10 to i64
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = xor i1 %10, true
%46 = zext i1 %45 to i64
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = call i32* @fdopen(i32 %48, i8* %1)
br label %50
50: ; preds = %2, %40, %14
%51 = phi i32* [ null, %14 ], [ %49, %40 ], [ null, %2 ]
ret i32* %51
}