#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 /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
char *realloc(char *, int);
void run(const char *cmd, char **data) {
  FILE *pipe = NULL;
  char buffer[8192];
  int dist = 0;
  *data = 0;
  int size;
  pipe = popen(cmd, "r");
  if (pipe) {
    while (!feof(pipe)) {
      size = (int)fread(buffer, 1, 8192, pipe);
      *data = realloc(*data, (dist + size + 1) * sizeof(char *));
      memcpy((*data) + dist, buffer, size);
      dist += size;
    }
    (*data)[dist] = '\0';
    pclose(pipe);
  }
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x2024(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1274 <run+0x114>
jmp    11b4 <run+0x54>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d2 <run+0x72>
jmp    1259 <run+0xf9>
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x2028(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2024(%rbp),%eax
add    -0x2028(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x2024(%rbp),%rax
add    %rax,%rdi
lea    -0x2020(%rbp),%rsi
mov    -0x2028(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x2028(%rbp),%eax
add    -0x2024(%rbp),%eax
mov    %eax,-0x2024(%rbp)
jmp    11b4 <run+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x2024(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%r14
movq   $0x0,(%rsi)
lea    0xe7e(%rip),%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     11fe <run+0x9e>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    11ef <run+0x8f>
mov    %rsp,%r15
mov    %ebx,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
mov    (%r14),%rdi
lea    (%rax,%rbp,1),%ebx
lea    0x8(,%rbx,8),%esi
call   1070 <realloc@plt>
mov    %rax,(%r14)
movslq %ebp,%rdi
add    %rax,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r12,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11a0 <run+0x40>
movslq %ebx,%rbx
mov    (%r14),%rax
movb   $0x0,(%rax,%rbx,1)
mov    %r12,%rdi
call   1040 <pclose@plt>
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define void @run(i8* nocapture %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
store i8* null, i8** %1
%5 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %31, label %7
7: ; preds = %2
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %7, %10
%11 = phi i32 [ %15, %10 ], [ 0, %7 ]
%12 = call i64 @fread(i8* %4, i32 1, i32 8192, i32* %5)
%13 = trunc i64 %12 to i32
%14 = load i8*, i8** %1
%15 = add nsw i32 %11, %13
%16 = shl i32 %15, 3
%17 = add i32 %16, 8
%18 = call i8* @realloc(i8* %14, i32 %17)
store i8* %18, i8** %1
%19 = sext i32 %11 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = call i32 @memcpy(i8* %20, i8* %4, i32 %13)
%22 = call i32 @feof(i32* %5)
%23 = icmp eq i32 %22, 0
br i1 %23, label %10, label %24
24: ; preds = %10
%25 = sext i32 %15 to i64
br label %26
26: ; preds = %24, %7
%27 = phi i64 [ 0, %7 ], [ %25, %24 ]
%28 = load i8*, i8** %1
%29 = getelementptr inbounds i8, i8* %28, i64 %27
store i8 0, i8* %29
%30 = call i32 @pclose(i32* %5)
br label %31
31: ; preds = %26, %2
ret void
}