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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fread(char *, int, 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 main() {
  FILE *fp = popen("ls -l", "r");
  if (!fp) {
    perror("popen");
    exit(-1);
  }
  char buf[512];
  memset(buf, 0, sizeof(buf));
  int ret = fread(buf, sizeof(char), 512, fp);
  if (ret > 0) {
    printf("%s\n", buf);
  }
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <main+0x4a>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jle    120d <main+0x9d>
lea    -0x210(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x208,%rsp
lea    0xe7f(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11e4 <main+0x74>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11cf <main+0x5f>
lea    0xe47(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"ls -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [512 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @popen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 512)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i32 @fread(i8* %14, i32 1, i32 512, i32* %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %11
%23 = load i32*, i32** %2
%24 = call i32 @pclose(i32* %23)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"ls -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = tail call i32* @popen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 512)
%10 = call i32 @fread(i8* %8, i32 1, i32 512, i32* %2)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %14
14: ; preds = %12, %7
%15 = call i32 @pclose(i32* %2)
ret i32 0
}