#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int strlen(char *);
int main() {
  int n = 0, count = 0;
  char *buf = NULL;
  FILE *fp = popen("ps  ", "r");
  if (fp == NULL) {
    printf("open error!\n");
    return -1;
  }
  buf = realloc(buf, count + 512);
  n = fread(buf + count, 1, 512, fp);
  printf("n = %d\n", n);
  printf("buf = %s len = %d \n", buf, strlen(buf));
  pclose(fp);
  if (buf) {
    free(buf);
    buf = NULL;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe64(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <main+0x61>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <main+0xf3>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x200,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <pclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     1263 <main+0xf3>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rdi
lea    0xe72(%rip),%rsi
call   10a0 <popen@plt>
test   %rax,%rax
je     1209 <main+0x89>
mov    %rax,%r15
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x200,%esi
call   1090 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0xe29(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1080 <pclose@plt>
test   %rbx,%rbx
je     121b <main+0x9b>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    121b <main+0x9b>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"ps \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"open error!\0A\00"
@.str.3 = [8 x i8] c"n = %d\0A\00"
@.str.4 = [20 x i8] c"buf = %s len = %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i8* null, i8** %4
%6 = call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 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** %5
%7 = load i32*, i32** %5
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
11: ; preds = %0
%12 = load i8*, i8** %4
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 512
%15 = call i8* @realloc(i8* %12, i32 %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32*, i32** %5
%21 = call i32 @fread(i8* %19, i32 1, i32 512, i32* %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %4
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %24, i32 %26)
%28 = load i32*, i32** %5
%29 = call i32 @pclose(i32* %28)
%30 = load i8*, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %11
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
store i8* null, i8** %4
br label %35
35: ; preds = %9, %32, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"ps \00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"n = %d\0A\00"
@.str.4 = [20 x i8] c"buf = %s len = %d \0A\00"
@str = [12 x i8] c"open error!\00"
define i32 @main() {
%1 = tail call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 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 %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %15
5: ; preds = %0
%6 = tail call i8* @realloc(i8* null, i32 512)
%7 = tail call i32 @fread(i8* %6, i32 1, i32 512, i32* %1)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%9 = tail call i32 @strlen(i8* %6)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %6, i32 %9)
%11 = tail call i32 @pclose(i32* %1)
%12 = icmp eq i8* %6, null
br i1 %12, label %15, label %13
13: ; preds = %5
%14 = tail call i32 @free(i8* %6)
br label %15
15: ; preds = %5, %13, %3
%16 = phi i32 [ -1, %3 ], [ 0, %13 ], [ 0, %5 ]
ret i32 %16
}