#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*/ EINVAL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
scalar_t__ execl(char *, char *, char *, char const *const,
                 int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char const *const);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
FILE *my_popen(const char *const cmd, const char *const mode, int *const pid) {
  FILE *retfile;
  int thepid = 0;
  int pipefd[2];
  int i;
  if (*mode != 'r' && *mode != 'w') {
    errno = EINVAL;
    return NULL;
  }
  if (pipe(pipefd) < 0)
    return NULL;
  fflush(stdout);
  fflush(stderr);
  if ((thepid = fork()) < 0) {
    close(pipefd[0]);
    close(pipefd[1]);
    return NULL;
  } else if (thepid == 0) {
    if (*mode == 'r') {
      close(1);
      dup2(pipefd[1], 1);
    } else {
      close(0);
      dup2(pipefd[0], 0);
    }
    for (i = 3; i < 64; i++)
      close(i);
    if (execl("/bin/sh", "sh", "-c", cmd, NULL) < 0)
      exit(127);
  }
  if (*mode == 'r') {
    close(pipefd[1]);
    retfile = fdopen(pipefd[0], mode);
  } else {
    close(pipefd[0]);
    retfile = fdopen(pipefd[1], mode);
  }
  *pid = thepid;
  return retfile;
}


x86 assembly (clang, -O0):
<my_popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
je     11da <my_popen+0x5a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
je     11da <my_popen+0x5a>
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    134b <my_popen+0x1cb>
lea    -0x34(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11fa <my_popen+0x7a>
movq   $0x0,-0x8(%rbp)
jmp    134b <my_popen+0x1cb>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1246 <my_popen+0xc6>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    134b <my_popen+0x1cb>
cmpl   $0x0,-0x2c(%rbp)
jne    12f0 <my_popen+0x170>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    127c <my_popen+0xfc>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
jmp    128d <my_popen+0x10d>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
movl   $0x3,-0x38(%rbp)
cmpl   $0x40,-0x38(%rbp)
jge    12b4 <my_popen+0x134>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1294 <my_popen+0x114>
mov    -0x10(%rbp),%rcx
lea    0xd41(%rip),%rdi
lea    0xd3f(%rip),%rsi
lea    0xd3b(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <execl@plt>
cmp    $0x0,%rax
jge    12eb <my_popen+0x16b>
mov    $0x7f,%edi
call   1080 <exit@plt>
jmp    12f0 <my_popen+0x170>
jmp    12f5 <my_popen+0x175>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1322 <my_popen+0x1a2>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x28(%rbp)
jmp    133a <my_popen+0x1ba>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_popen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    (%rsi),%al
cmp    $0x72,%al
je     119f <my_popen+0x1f>
cmp    $0x77,%al
jne    11f8 <my_popen+0x78>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     120a <my_popen+0x8a>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1211 <my_popen+0x91>
mov    %eax,%r15d
jne    1284 <my_popen+0x104>
cmpb   $0x72,(%rbx)
jne    122a <my_popen+0xaa>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
jmp    1237 <my_popen+0xb7>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    0x2dd0(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    12b0 <my_popen+0x130>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    12b0 <my_popen+0x130>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    $0x3,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    $0x40,%ebp
jne    1250 <my_popen+0xd0>
lea    0xd9a(%rip),%rdi
lea    0xd98(%rip),%rsi
lea    0xd94(%rip),%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1090 <execl@plt>
test   %rax,%rax
js     12bd <my_popen+0x13d>
cmpb   $0x72,(%rbx)
jne    1298 <my_popen+0x118>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
jmp    12a5 <my_popen+0x125>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   1070 <fdopen@plt>
mov    %r15d,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x7f,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.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* @my_popen(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [2 x i32]
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %9
%12 = load i8*, i8** %6
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 114
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 119
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
store i32* null, i32** %4
br label %103
23: ; preds = %16, %3
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %10, 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
store i32* null, i32** %4
br label %103
28: ; preds = %23
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* @stderr
%32 = call i32 @fflush(i32 %31)
%33 = call i32 (...) @fork()
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
store i32* null, i32** %4
br label %103
42: ; preds = %28
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %77
45: ; preds = %42
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 114
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = call i32 @close(i32 1)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @dup2(i32 %53, i32 1)
br label %60
55: ; preds = %45
%56 = call i32 @close(i32 0)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @dup2(i32 %58, i32 0)
br label %60
60: ; preds = %55, %50
store i32 3, i32* %11
br label %61
61: ; preds = %67, %60
%62 = load i32, i32* %11
%63 = icmp slt i32 %62, 64
br i1 %63, label %64, label %70
64: ; preds = %61
%65 = load i32, i32* %11
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %64
%68 = load i32, i32* %11
%69 = add nsw i32 %68, 1
store i32 %69, i32* %11
br label %61
70: ; preds = %61
%71 = load i8*, i8** %5
%72 = call i64 @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* %71, i32* null)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @exit(i32 127)
unreachable
76: ; preds = %70
br label %77
77: ; preds = %76, %42
br label %78
78: ; preds = %77
%79 = load i8*, i8** %6
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = icmp eq i32 %81, 114
br i1 %82, label %83, label %91
83: ; preds = %78
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%85 = load i32, i32* %84
%86 = call i32 @close(i32 %85)
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%88 = load i32, i32* %87
%89 = load i8*, i8** %6
%90 = call i32* @fdopen(i32 %88, i8* %89)
store i32* %90, i32** %8
br label %99
91: ; preds = %78
%92 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%93 = load i32, i32* %92
%94 = call i32 @close(i32 %93)
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%96 = load i32, i32* %95
%97 = load i8*, i8** %6
%98 = call i32* @fdopen(i32 %96, i8* %97)
store i32* %98, i32** %8
br label %99
99: ; preds = %91, %83
%100 = load i32, i32* %9
%101 = load i32*, i32** %7
store i32 %100, i32* %101
%102 = load i32*, i32** %8
store i32* %102, i32** %4
br label %103
103: ; preds = %99, %35, %27, %21
%104 = load i32*, i32** %4
ret i32* %104
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.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* @my_popen(i8* %0, i8* nocapture %1, i32* nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = load i8, i8* %1
switch i8 %6, label %7 [
i8 114, label %9
i8 119, label %9
]
7: ; preds = %3
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %67
9: ; preds = %3, %3
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %67, label %13
13: ; preds = %9
%14 = load i32, i32* @stdout
%15 = call i32 @fflush(i32 %14)
%16 = load i32, i32* @stderr
%17 = call i32 @fflush(i32 %16)
%18 = call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = load i32, i32* %10
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
br label %67
26: ; preds = %13
%27 = icmp eq i32 %18, 0
br i1 %27, label %28, label %51
28: ; preds = %26
%29 = load i8, i8* %1
%30 = icmp eq i8 %29, 114
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = call i32 @close(i32 1)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @dup2(i32 %34, i32 1)
br label %40
36: ; preds = %28
%37 = call i32 @close(i32 0)
%38 = load i32, i32* %10
%39 = call i32 @dup2(i32 %38, i32 0)
br label %40
40: ; preds = %36, %31
br label %41
41: ; preds = %40, %41
%42 = phi i32 [ %44, %41 ], [ 3, %40 ]
%43 = call i32 @close(i32 %42)
%44 = add nuw nsw i32 %42, 1
%45 = icmp eq i32 %44, 64
br i1 %45, label %46, label %41
46: ; preds = %41
%47 = call i64 @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)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @exit(i32 127)
unreachable
51: ; preds = %26, %46
%52 = load i8, i8* %1
%53 = icmp eq i8 %52, 114
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %10
br label %64
59: ; preds = %51
%60 = load i32, i32* %10
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%63 = load i32, i32* %62
br label %64
64: ; preds = %59, %54
%65 = phi i32 [ %63, %59 ], [ %58, %54 ]
%66 = call i32* @fdopen(i32 %65, i8* %1)
store i32 %18, i32* %2
br label %67
67: ; preds = %9, %64, %20, %7
%68 = phi i32* [ null, %7 ], [ null, %20 ], [ %66, %64 ], [ null, %9 ]
ret i32* %68
}