#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 PIPE_BUF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  FILE *fp;
  char *cmd = "cat file1";
  char buf[PIPE_BUF];
  if ((fp = popen(cmd, "r")) == NULL) {
    perror(" failed to popen ");
    exit(1);
  }
  while ((fgets(buf, PIPE_BUF, fp)) != NULL)
    printf("%s", buf);
  pclose(fp);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11da <main+0x7a>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11df <main+0x7f>
mov    -0x30(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1214 <main+0xb4>
mov    -0x30(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
jmp    11df <main+0x7f>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e6e(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe72(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
jne    11b5 <main+0x55>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%r14
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11ff <main+0x9f>
lea    0xe4b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11e0 <main+0x80>
mov    %r14,%rdi
call   1040 <pclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"cat file1\00"
@PIPE_BUF = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c" failed to popen \00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = load i32, i32* @PIPE_BUF
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %4
%9 = alloca i8, i64 %7
store i64 %7, i64* %5
%10 = load i8*, i8** %3
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %22, %16
%18 = load i32, i32* @PIPE_BUF
%19 = load i32*, i32** %2
%20 = call i32* @fgets(i8* %9, i32 %18, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %17
24: ; preds = %17
%25 = load i32*, i32** %2
%26 = call i32 @pclose(i32* %25)
%27 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"cat file1\00"
@PIPE_BUF = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c" failed to popen \00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = load i32, i32* @PIPE_BUF
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @popen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @PIPE_BUF
%8 = call i32* @fgets(i8* %3, i32 %7, i32* %4)
%9 = icmp eq i32* %8, null
br i1 %9, label %18, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %13
%14 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%15 = load i32, i32* @PIPE_BUF
%16 = call i32* @fgets(i8* %3, i32 %15, i32* %4)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %13
18: ; preds = %13, %6
%19 = call i32 @pclose(i32* %4)
%20 = call i32 @exit(i32 0)
unreachable
}