#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_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char const *);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
FILE *popen(const char *command, const char *mode) {
  int p[2];
  int parent_end, child_end;
  if (*mode == 'r') {
    parent_end = 0;
    child_end = 1;
  } else if (*mode == 'w') {
    parent_end = 1;
    child_end = 0;
  }
  pipe(p);
  if (fork() > 0) {
    close(p[child_end]);
    return fdopen(p[parent_end], mode);
  }
  close(p[parent_end]);
  dup2(p[child_end], child_end);
  close(p[child_end]);
  execl("/bin/sh", "sh", "-c", command, NULL);
  return EXIT_SUCCESS;
}


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    1193 <popen+0x33>
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11b6 <popen+0x56>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11b1 <popen+0x51>
movl   $0x1,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    11b6 <popen+0x56>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jle    11f7 <popen+0x97>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    1252 <popen+0xf2>
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
mov    -0x28(%rbp),%esi
call   1030 <dup2@plt>
movslq -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xdd4(%rip),%rdi
lea    0xdd2(%rip),%rsi
lea    0xdce(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <execl@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%rax
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %ebx,%ebx
cmpb   $0x77,(%rsi)
setne  %bpl
sete   %bl
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jle    11b2 <popen+0x52>
mov    %bpl,%r12b
mov    0x8(%rsp,%r12,4),%edi
call   1040 <close@plt>
mov    0x8(%rsp,%rbx,4),%edi
mov    %r15,%rsi
call   1060 <fdopen@plt>
jmp    1200 <popen+0xa0>
movzbl %bpl,%r15d
mov    0x8(%rsp,%rbx,4),%edi
call   1040 <close@plt>
mov    0x8(%rsp,%r15,4),%edi
mov    %r15d,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp,%r15,4),%edi
call   1040 <close@plt>
lea    0xe23(%rip),%rdi
lea    0xe21(%rip),%rsi
lea    0xe1d(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1070 <execl@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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"
@EXIT_SUCCESS = global i32* null
define 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
%6 = icmp eq i8 %5, 119
%7 = zext i1 %6 to i64
%8 = xor i1 %6, true
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i64 (...) @fork()
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = zext i1 %8 to i64
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %7
%19 = load i32, i32* %18
%20 = call i32* @fdopen(i32 %19, i8* %1)
br label %34
21: ; preds = %2
%22 = zext i1 %8 to i32
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %7
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = zext i1 %8 to i64
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @dup2(i32 %28, i32 %22)
%30 = load i32, i32* %27
%31 = call i32 @close(i32 %30)
%32 = 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)
%33 = load i32*, i32** @EXIT_SUCCESS
br label %34
34: ; preds = %21, %13
%35 = phi i32* [ %20, %13 ], [ %33, %21 ]
ret i32* %35
}