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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
int test_popen(void) {
  void *data = NULL;
  size_t chunk = 1024;
  size_t alloc_sz = chunk;
  size_t sz = 0;
  FILE *fp;
  fp = popen("/bin/ls -l /", "r");
  if (!fp) {
    fprintf(stderr, "popen failed\n");
    return 1;
  }
  while (!feof(fp)) {
    size_t ret;
    data = realloc(data, alloc_sz);
    if (!data) {
      fprintf(stderr, "realloc %zu failed\n", alloc_sz);
      break;
    }
    ret = fread((uint8_t *)data + sz, 1, chunk, fp);
    sz += ret;
    if (ret < chunk)
      break;
    alloc_sz += chunk;
  }
  free(data);
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<test_popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe61(%rip),%rdi
lea    0xe67(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dd <test_popen+0x6d>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12aa <test_popen+0x13a>
jmp    11e2 <test_popen+0x72>
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1200 <test_popen+0x90>
jmp    1291 <test_popen+0x121>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123c <test_popen+0xcc>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1291 <test_popen+0x121>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1280 <test_popen+0x110>
jmp    1291 <test_popen+0x121>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11e2 <test_popen+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_popen>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     11e9 <test_popen+0x79>
mov    %rax,%r14
mov    $0x400,%r12d
xor    %ebx,%ebx
xor    %r15d,%r15d
xchg   %ax,%ax
mov    %r14,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1224 <test_popen+0xb4>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1207 <test_popen+0x97>
mov    %rax,%r15
lea    (%rax,%rbx,4),%rdi
mov    $0x400,%edx
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
add    %rax,%rbx
add    $0x400,%r12
cmp    $0x3ff,%rax
ja     11a0 <test_popen+0x30>
jmp    1224 <test_popen+0xb4>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1236 <test_popen+0xc6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
xor    %r15d,%r15d
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/bin/ls -l /\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"popen failed\0A\00"
@.str.3 = [20 x i8] c"realloc %zu failed\0A\00"
define i32 @test_popen() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
store i8* null, i8** %2
store i64 1024, i64* %3
%8 = load i64, i64* %3
store i64 %8, i64* %4
store i64 0, i64* %5
%9 = call i32* @popen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %55
15: ; preds = %0
br label %16
16: ; preds = %46, %15
%17 = load i32*, i32** %6
%18 = call i32 @feof(i32* %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %50
21: ; preds = %16
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = call i8* @realloc(i8* %22, i64 %23)
store i8* %24, i8** %2
%25 = load i8*, i8** %2
%26 = icmp ne i8* %25, null
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i64, i64* %4
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %29)
br label %50
31: ; preds = %21
%32 = load i8*, i8** %2
%33 = bitcast i8* %32 to i32*
%34 = load i64, i64* %5
%35 = getelementptr inbounds i32, i32* %33, i64 %34
%36 = load i64, i64* %3
%37 = load i32*, i32** %6
%38 = call i64 @fread(i32* %35, i32 1, i64 %36, i32* %37)
store i64 %38, i64* %7
%39 = load i64, i64* %7
%40 = load i64, i64* %5
%41 = add i64 %40, %39
store i64 %41, i64* %5
%42 = load i64, i64* %7
%43 = load i64, i64* %3
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %31
br label %50
46: ; preds = %31
%47 = load i64, i64* %3
%48 = load i64, i64* %4
%49 = add i64 %48, %47
store i64 %49, i64* %4
br label %16
50: ; preds = %45, %27, %16
%51 = load i8*, i8** %2
%52 = call i32 @free(i8* %51)
%53 = load i32*, i32** %6
%54 = call i32 @pclose(i32* %53)
store i32 0, i32* %1
br label %55
55: ; preds = %50, %12
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [13 x i8] c"/bin/ls -l /\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"popen failed\0A\00"
@.str.3 = [20 x i8] c"realloc %zu failed\0A\00"
define i32 @test_popen() {
%1 = tail call i32* @popen(i8* getelementptr inbounds ([13 x i8], [13 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 %6
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %29
6: ; preds = %0, %18
%7 = phi i64 [ %24, %18 ], [ 1024, %0 ]
%8 = phi i64 [ %22, %18 ], [ 0, %0 ]
%9 = phi i8* [ %13, %18 ], [ null, %0 ]
%10 = tail call i32 @feof(i32* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %6
%13 = tail call i8* @realloc(i8* %9, i64 %7)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %7)
br label %25
18: ; preds = %12
%19 = bitcast i8* %13 to i32*
%20 = getelementptr inbounds i32, i32* %19, i64 %8
%21 = tail call i64 @fread(i32* %20, i32 1, i64 1024, i32* %1)
%22 = add i64 %21, %8
%23 = icmp ult i64 %21, 1024
%24 = add i64 %7, 1024
br i1 %23, label %25, label %6
25: ; preds = %18, %6, %15
%26 = phi i8* [ null, %15 ], [ %13, %18 ], [ %9, %6 ]
%27 = tail call i32 @free(i8* %26)
%28 = tail call i32 @pclose(i32* %1)
br label %29
29: ; preds = %25, %3
%30 = phi i32 [ 0, %25 ], [ 1, %3 ]
ret i32 %30
}