#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 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 *command, const char *mode) {
  int pfp[2];
  int pid;
  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);
  }
  execl("/bin/sh", "sh", "-c", command, NULL);
  exit(1);
}


x86 assembly (clang, -O0):
<popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11d8 <popen+0x68>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11c6 <popen+0x56>
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    11d3 <popen+0x63>
movq   $0x0,-0x8(%rbp)
jmp    12de <popen+0x16e>
jmp    11d8 <popen+0x68>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f7 <popen+0x87>
movq   $0x0,-0x8(%rbp)
jmp    12de <popen+0x16e>
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    12de <popen+0x16e>
cmpl   $0x0,-0x24(%rbp)
jle    126e <popen+0xfe>
movslq -0x2c(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1254 <popen+0xe4>
movq   $0x0,-0x8(%rbp)
jmp    12de <popen+0x16e>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    12de <popen+0x16e>
movslq -0x28(%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 -0x2c(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x2c(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12b1 <popen+0x141>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rcx
lea    0xd44(%rip),%rdi
lea    0xd42(%rip),%rsi
lea    0xd3e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%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     1212 <popen+0xa2>
mov    %ebp,%eax
mov    (%rsp,%rax,4),%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    121c <popen+0xac>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xddd(%rip),%rdi
lea    0xddb(%rip),%rsi
lea    0xdd7(%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
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 114
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %8
store i32 1, i32* %9
br label %23
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 119
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 1, i32* %8
store i32 0, i32* %9
br label %22
21: ; preds = %15
store i32* null, i32** %3
br label %79
22: ; preds = %20
br label %23
23: ; preds = %22, %14
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%25 = call i32 @pipe(i32* %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %23
store i32* null, i32** %3
br label %79
28: ; preds = %23
%29 = call i32 (...) @fork()
store i32 %29, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
store i32* null, i32** %3
br label %79
38: ; preds = %28
%39 = load i32, i32* %7
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %56
41: ; preds = %38
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %41
store i32* null, i32** %3
br label %79
49: ; preds = %41
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = load i8*, i8** %5
%55 = call i32* @fdopen(i32 %53, i8* %54)
store i32* %55, i32** %3
br label %79
56: ; preds = %38
%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)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %56
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %56
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %67
%69 = load i32, i32* %68
%70 = load i32, i32* %9
%71 = call i32 @dup2(i32 %69, i32 %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %65
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %65
%76 = load i8*, i8** %4
%77 = 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* %76, i32* null)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %49, %48, %31, %27, %21
%80 = load i32*, i32** %3
ret i32* %80
}

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 %52 [
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 %52, 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 %52
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 %52, 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 %52
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 = 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)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %24, %7, %2, %30, %16
%53 = phi i32* [ null, %16 ], [ %33, %30 ], [ null, %2 ], [ null, %7 ], [ null, %24 ]
ret i32* %53
}