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

/* Variables and functions */
int close(int);
int dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char const *);
int fork();
int pipe(int *);
FILE *popen(const char *cmd, const char *mode) {
  int pfp[2];
  pid_t pid;
  FILE *fp;
  int parent_end, child_end;
  if (*mode == 'r') {
    parent_end = 0;
    child_end = 1;
  } else if (*mode == 'w') {
    parent_end = 1;
    child_end = 0;
  } else
    return NULL;
  if (pipe(pfp) == -1)
    return NULL;
  if ((pid = fork()) == -1) {
    close(pfp[0]);
    close(pfp[1]);
    return NULL;
  }
  if (pid > 0) {
    if (close(pfp[child_end]) == -1)
      return NULL;
    return fdopen(pfp[parent_end], mode);
  }
  if (close(pfp[parent_end]) == -1)
    exit(1);
  if (dup2(pfp[child_end], child_end) == -1)
    exit(1);
  if (close(pfp[child_end]) == -1)
    exit(1);
  execl("/bin/sh", "sh", "-c", cmd, NULL);
  exit(1);
}


x86 assembly (clang, -O0):
<popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    11a3 <popen+0x33>
movl   $0x0,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    11d8 <popen+0x68>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11c6 <popen+0x56>
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
jmp    11d3 <popen+0x63>
movq   $0x0,-0x8(%rbp)
jmp    12fe <popen+0x18e>
jmp    11d8 <popen+0x68>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f7 <popen+0x87>
movq   $0x0,-0x8(%rbp)
jmp    12fe <popen+0x18e>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1227 <popen+0xb7>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12fe <popen+0x18e>
cmpl   $0x0,-0x24(%rbp)
jle    126e <popen+0xfe>
movslq -0x38(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1254 <popen+0xe4>
movq   $0x0,-0x8(%rbp)
jmp    12fe <popen+0x18e>
movslq -0x34(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    12fe <popen+0x18e>
movslq -0x34(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    128e <popen+0x11e>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x38(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x38(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12b1 <popen+0x141>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x38(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12d1 <popen+0x161>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rcx
lea    0xd24(%rip),%rdi
lea    0xd22(%rip),%rsi
lea    0xd1e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%al
xor    %ebx,%ebx
cmp    $0x72,%al
je     1192 <popen+0x22>
cmp    $0x77,%al
jne    11e6 <popen+0x76>
xor    %ebp,%ebp
mov    $0x1,%ebx
jmp    1197 <popen+0x27>
mov    $0x1,%ebp
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11e4 <popen+0x74>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11d3 <popen+0x63>
test   %eax,%eax
jle    11f4 <popen+0x84>
mov    %ebp,%eax
mov    (%rsp,%rax,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     11e4 <popen+0x74>
mov    (%rsp,%rbx,4),%edi
mov    %r15,%rsi
call   1060 <fdopen@plt>
mov    %rax,%rbx
jmp    11e6 <popen+0x76>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp,%rbx,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     121f <popen+0xaf>
mov    %ebp,%ebx
mov    (%rsp,%rbx,4),%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     121f <popen+0xaf>
mov    (%rsp,%rbx,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1229 <popen+0xb9>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdd0(%rip),%rdi
lea    0xdce(%rip),%rsi
lea    0xdca(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0x1,%edi
call   1070 <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*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 114
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %9
store i32 1, i32* %10
br label %24
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 119
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %9
store i32 0, i32* %10
br label %23
22: ; preds = %16
store i32* null, i32** %3
br label %89
23: ; preds = %21
br label %24
24: ; preds = %23, %15
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = call i32 @pipe(i32* %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %24
store i32* null, i32** %3
br label %89
29: ; preds = %24
%30 = call i32 (...) @fork()
store i32 %30, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
store i32* null, i32** %3
br label %89
39: ; preds = %29
%40 = load i32, i32* %7
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %57
42: ; preds = %39
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %44
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %42
store i32* null, i32** %3
br label %89
50: ; preds = %42
%51 = load i32, i32* %9
%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 i8*, i8** %5
%56 = call i32* @fdopen(i32 %54, i8* %55)
store i32* %56, i32** %3
br label %89
57: ; preds = %39
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %57
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %57
%67 = load i32, i32* %10
%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 = load i32, i32* %10
%72 = call i32 @dup2(i32 %70, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %66
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %66
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %78
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %76
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %76
%86 = load i8*, i8** %4
%87 = 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* %86, i32* null)
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %50, %49, %32, %28, %22
%90 = load i32*, i32** %3
ret i32* %90
}

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 = load i8, i8* %1
switch i8 %5, label %58 [
i8 114, label %7
i8 119, label %6
]
6: ; preds = %2
br label %7
7: ; preds = %2, %6
%8 = phi i64 [ 1, %6 ], [ 0, %2 ]
%9 = phi i32 [ 0, %6 ], [ 1, %2 ]
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %58, label %13
13: ; preds = %7
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %10
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
br label %58
22: ; preds = %13
%23 = icmp sgt i32 %14, 0
br i1 %23, label %24, label %34
24: ; preds = %22
%25 = zext i32 %9 to i64
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %25
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %58, label %30
30: ; preds = %24
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %8
%32 = load i32, i32* %31
%33 = call i32* @fdopen(i32 %32, i8* %1)
br label %58
34: ; preds = %22
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %8
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = zext i32 %9 to i64
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = call i32 @dup2(i32 %44, i32 %9)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i32, i32* %43
%51 = call i32 @close(i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = 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, i32* null)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %24, %7, %2, %30, %16
%59 = phi i32* [ null, %16 ], [ %33, %30 ], [ null, %2 ], [ null, %7 ], [ null, %24 ]
ret i32* %59
}