#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*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int test_popen(void) {
  FILE *fstream = NULL;
  char buff[1024];
  memset(buff, 0, sizeof(buff));
  if ((fstream = popen("ls -al", "r")) == NULL) {
    perror("execute command failed : ");
    return -1;
  }
  while (fgets(buff, sizeof(buff), fstream) != NULL) {
    printf("%s\n", buff);
  }
  pclose(fstream);
  return 0;
}


x86 assembly (clang, -O0):
<test_popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movq   $0x0,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0xe73(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11bf <test_popen+0x5f>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <test_popen+0xab>
jmp    11c4 <test_popen+0x64>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11fb <test_popen+0x9b>
lea    -0x410(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
jmp    11c4 <test_popen+0x64>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_popen>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0xe7e(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11f7 <test_popen+0x97>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e0 <test_popen+0x80>
lea    0xe71(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11c0 <test_popen+0x60>
mov    %r15,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ea <test_popen+0x8a>

LLVM IR (-O0):
@.str = [7 x i8] c"ls -al\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"execute command failed : \00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @test_popen() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
store i32* null, i32** %2
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 1024)
%6 = call i32* @popen(i8* getelementptr inbounds ([7 x i8], [7 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 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %22
10: ; preds = %0
br label %11
11: ; preds = %16, %10
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %12, i32 1024, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %17)
br label %11
19: ; preds = %11
%20 = load i32*, i32** %2
%21 = call i32 @pclose(i32* %20)
store i32 0, i32* %1
br label %22
22: ; preds = %19, %8
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [7 x i8] c"ls -al\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"execute command failed : \00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @test_popen() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 1024)
%4 = call i32* @popen(i8* getelementptr inbounds ([7 x i8], [7 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 %9, label %6
6: ; preds = %0
%7 = call i32* @fgets(i8* %2, i32 1024, i32* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %17
11: ; preds = %6, %11
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%13 = call i32* @fgets(i8* %2, i32 1024, i32* %4)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %11
15: ; preds = %11, %6
%16 = call i32 @pclose(i32* %4)
br label %17
17: ; preds = %15, %9
%18 = phi i32 [ -1, %9 ], [ 0, %15 ]
ret i32 %18
}