#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*/ write_data(int /*<<< orphan*/ *);
int main(void) {
  FILE *output, *input;
  int wstatus, rstatus;
  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    0xe5a(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e3 <main+0x53>
lea    0xe56(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe50(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <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    0xe2f(%rip),%rdi
call   1060 <printf@plt>
lea    0xe3f(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124b <main+0xbb>
lea    0xe26(%rip),%rdi
call   10a0 <perror@plt>
lea    0xde8(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <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    0xe00(%rip),%rdi
call   1060 <printf@plt>
lea    0xde0(%rip),%rdi
call   1040 <remove@plt>
mov    0x2d5e(%rip),%rax
movq   $0x0,(%rax)
lea    0xdf6(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12cd <main+0x13d>
lea    0xddf(%rip),%rdi
call   1050 <puts@plt>
lea    0xd66(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%rax
mov    0x2cfa(%rip),%rcx
cmp    (%rcx),%rax
je     1309 <main+0x179>
lea    0xdd7(%rip),%rdi
call   1050 <puts@plt>
lea    0xd2a(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%ecx
or     -0x20(%rbp),%ecx
lea    0xdeb(%rip),%rdi
lea    0xd07(%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    0xe65(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1275 <main+0xe5>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <write_data@plt>
mov    %rbx,%rdi
call   1070 <pclose@plt>
mov    %eax,%r14d
lea    0xe64(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0xe72(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     127e <main+0xee>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <read_data@plt>
mov    %rbp,%rdi
call   1070 <pclose@plt>
mov    %eax,%ebp
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0xe3c(%rip),%rdi
call   1040 <remove@plt>
mov    0x2dba(%rip),%rbx
movq   $0x0,(%rbx)
lea    0xe52(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
jne    128c <main+0xfc>
mov    (%rbx),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
jne    1295 <main+0x105>
or     %r14d,%ebp
lea    0xea3(%rip),%rax
lea    0xdbf(%rip),%rdi
cmove  %rax,%rdi
call   1050 <puts@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda2(%rip),%rdi
jmp    1285 <main+0xf5>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a1 <main+0x111>
lea    0xdfe(%rip),%rdi
jmp    129c <main+0x10c>
lea    0xe29(%rip),%rdi
call   1050 <puts@plt>
lea    0xd7c(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

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