#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 */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read_data(int /*<<< orphan*/ *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ unsetenv(char *);
int /*<<< orphan*/ write_data(int /*<<< orphan*/ *);
int main(void) {
  FILE *output, *input;
  int wstatus, rstatus;
  unsetenv("LD_LIBRARY_PATH");
  output = popen("/bin/cat >/tmp/tstpopen.tmp", "w");
  if (output == NULL) {
    perror("popen");
    puts("Test FAILED!");
    exit(1);
  }
  write_data(output);
  wstatus = pclose(output);
  printf("writing pclose returned %d\n", wstatus);
  input = popen("/bin/cat /tmp/tstpopen.tmp", "r");
  if (input == NULL) {
    perror("/tmp/tstpopen.tmp");
    puts("Test FAILED!");
    exit(1);
  }
  read_data(input);
  rstatus = pclose(input);
  printf("reading pclose returned %d\n", rstatus);
  remove("/tmp/tstpopen.tmp");
  errno = 0;
  output = popen("/bin/cat", "m");
  if (output != NULL) {
    puts("popen called with illegal mode does not return NULL");
    puts("Test FAILED!");
    exit(1);
  }
  if (errno != EINVAL) {
    puts("popen called with illegal mode does not set errno to EINVAL");
    puts("Test FAILED!");
    exit(1);
  }
  puts(wstatus | rstatus ? "Test FAILED!" : "Test succeeded.");
  return (wstatus | rstatus);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
call   10b0 <unsetenv@plt>
lea    0xe4e(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ff <main+0x5f>
lea    0xe4a(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe44(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1080 <write_data@plt>
mov    -0x10(%rbp),%rdi
call   1070 <pclose@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe23(%rip),%rdi
call   1060 <printf@plt>
lea    0xe33(%rip),%rdi
lea    0xe47(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1267 <main+0xc7>
lea    0xe1a(%rip),%rdi
call   10a0 <perror@plt>
lea    0xddc(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <read_data@plt>
mov    -0x18(%rbp),%rdi
call   1070 <pclose@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdf4(%rip),%rdi
call   1060 <printf@plt>
lea    0xdd4(%rip),%rdi
call   1040 <remove@plt>
mov    0x2d42(%rip),%rax
movq   $0x0,(%rax)
lea    0xdea(%rip),%rdi
lea    0xdec(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e9 <main+0x149>
lea    0xdd3(%rip),%rdi
call   1050 <puts@plt>
lea    0xd5a(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cde(%rip),%rcx
cmp    (%rcx),%rax
je     1325 <main+0x185>
lea    0xdcb(%rip),%rdi
call   1050 <puts@plt>
lea    0xd1e(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%ecx
or     -0x20(%rbp),%ecx
lea    0xddf(%rip),%rdi
lea    0xcfb(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
call   1050 <puts@plt>
mov    -0x1c(%rbp),%eax
or     -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe55(%rip),%rdi
call   10b0 <unsetenv@plt>
lea    0xe59(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1291 <main+0xf1>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <write_data@plt>
mov    %rbx,%rdi
call   1070 <pclose@plt>
mov    %eax,%r14d
lea    0xe58(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0xe66(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     129a <main+0xfa>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <read_data@plt>
mov    %rbp,%rdi
call   1070 <pclose@plt>
mov    %eax,%ebp
lea    0xe52(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0xe30(%rip),%rdi
call   1040 <remove@plt>
mov    0x2d9e(%rip),%rbx
movq   $0x0,(%rbx)
lea    0xe46(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
jne    12a8 <main+0x108>
mov    (%rbx),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <main+0x111>
or     %r14d,%ebp
lea    0xe97(%rip),%rax
lea    0xdb3(%rip),%rdi
cmove  %rax,%rdi
call   1050 <puts@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd96(%rip),%rdi
jmp    12a1 <main+0x101>
lea    0xdc5(%rip),%rdi
call   10a0 <perror@plt>
jmp    12bd <main+0x11d>
lea    0xdf2(%rip),%rdi
jmp    12b8 <main+0x118>
lea    0xe1d(%rip),%rdi
call   1050 <puts@plt>
lea    0xd70(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.1 = [28 x i8] c"/bin/cat >/tmp/tstpopen.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [13 x i8] c"Test FAILED!\00"
@.str.5 = [28 x i8] c"writing pclose returned %d\0A\00"
@.str.6 = [27 x i8] c"/bin/cat /tmp/tstpopen.tmp\00"
@.str.7 = [2 x i8] c"r\00"
@.str.8 = [18 x i8] c"/tmp/tstpopen.tmp\00"
@.str.9 = [28 x i8] c"reading pclose returned %d\0A\00"
@errno = global i64 0
@.str.10 = [9 x i8] c"/bin/cat\00"
@.str.11 = [2 x i8] c"m\00"
@.str.12 = [52 x i8] c"popen called with illegal mode does not return NULL\00"
@EINVAL = global i64 0
@.str.13 = [60 x i8] c"popen called with illegal mode does not set errno to EINVAL\00"
@.str.14 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @unsetenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%12 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32*, i32** %2
%16 = call i32 @write_data(i32* %15)
%17 = load i32*, i32** %2
%18 = call i32 @pclose(i32* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = call i32* @popen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %21, i32** %3
%22 = load i32*, i32** %3
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%26 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %14
%29 = load i32*, i32** %3
%30 = call i32 @read_data(i32* %29)
%31 = load i32*, i32** %3
%32 = call i32 @pclose(i32* %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %33)
%35 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
store i64 0, i64* @errno
%36 = call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
store i32* %36, i32** %2
%37 = load i32*, i32** %2
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %28
%40 = call i32 @puts(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.12, i64 0, i64 0))
%41 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %28
%44 = load i64, i64* @errno
%45 = load i64, i64* @EINVAL
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = call i32 @puts(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.13, i64 0, i64 0))
%49 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = load i32, i32* %4
%53 = load i32, i32* %5
%54 = or i32 %52, %53
%55 = icmp ne i32 %54, 0
%56 = zext i1 %55 to i64
%57 = select i1 %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0)
%58 = call i32 @puts(i8* %57)
%59 = load i32, i32* %4
%60 = load i32, i32* %5
%61 = or i32 %59, %60
ret i32 %61
}

LLVM IR (-O2):
@.str = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.1 = [28 x i8] c"/bin/cat >/tmp/tstpopen.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [13 x i8] c"Test FAILED!\00"
@.str.5 = [28 x i8] c"writing pclose returned %d\0A\00"
@.str.6 = [27 x i8] c"/bin/cat /tmp/tstpopen.tmp\00"
@.str.7 = [2 x i8] c"r\00"
@.str.8 = [18 x i8] c"/tmp/tstpopen.tmp\00"
@.str.9 = [28 x i8] c"reading pclose returned %d\0A\00"
@errno = global i64 0
@.str.10 = [9 x i8] c"/bin/cat\00"
@.str.11 = [2 x i8] c"m\00"
@.str.12 = [52 x i8] c"popen called with illegal mode does not return NULL\00"
@EINVAL = global i64 0
@.str.13 = [60 x i8] c"popen called with illegal mode does not set errno to EINVAL\00"
@.str.14 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = tail call i32 @unsetenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @write_data(i32* %2)
%10 = tail call i32 @pclose(i32* %2)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %10)
%12 = tail call i32* @popen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = tail call i32 @read_data(i32* %12)
%20 = tail call i32 @pclose(i32* %12)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %20)
%22 = tail call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
store i64 0, i64* @errno
%23 = tail call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %29, label %25
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.12, i64 0, i64 0))
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINVAL
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.13, i64 0, i64 0))
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = or i32 %20, %10
%39 = icmp eq i32 %38, 0
%40 = select i1 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0)
%41 = tail call i32 @puts(i8* dereferenceable(1) %40)
ret i32 %38
}