#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *get_command_output(const char *cmd) {
  FILE *pipe;
  char *out;
  size_t out_size;
  size_t out_len;
  size_t buf_len;
  char buf[2048];
  pipe = popen(cmd, "r");
  if (pipe == NULL)
    return NULL;
  out = NULL;
  out_len = 0;
  while (fgets(buf, 2048, pipe)) {
    buf_len = strlen(buf);
    if (out == NULL) {
      out = (char *)malloc(2048);
      out_size = 2048;
      *out = '\0';
    } else if (out_len + buf_len + 1 >= out_size) {
      out = (char *)realloc(out, out_size += 2048);
    }
    strcpy(out + out_len, buf);
    out_len += buf_len;
  }
  pclose(pipe);
  return out;
}


x86 assembly (clang, -O0):
<get_command_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <get_command_output+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1284 <get_command_output+0x114>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x840(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1273 <get_command_output+0x103>
lea    -0x840(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1217 <get_command_output+0xa7>
mov    $0x800,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x800,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    124e <get_command_output+0xde>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0x1,%rax
cmp    -0x28(%rbp),%rax
jb     1249 <get_command_output+0xd9>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x800,%rsi
mov    %rsi,-0x28(%rbp)
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    124e <get_command_output+0xde>
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
lea    -0x840(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11bb <get_command_output+0x4b>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_command_output>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
lea    0xe78(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1236 <get_command_output+0xc6>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     123b <get_command_output+0xcb>
xor    %ebx,%ebx
mov    %rsp,%r15
xor    %r12d,%r12d
jmp    11ff <get_command_output+0x8f>
nopl   0x0(%rax)
mov    $0x800,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
add    %rbx,%rbp
mov    $0x800,%r13d
add    %r12,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
mov    %rbp,%rbx
test   %rax,%rax
je     123e <get_command_output+0xce>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
test   %r12,%r12
je     11c0 <get_command_output+0x50>
lea    (%rbx,%rbp,1),%rax
add    $0x1,%rax
add    %rbx,%rbp
cmp    %r13,%rax
jb     11d9 <get_command_output+0x69>
add    $0x800,%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <realloc@plt>
mov    %rax,%r12
jmp    11d9 <get_command_output+0x69>
xor    %r12d,%r12d
jmp    1246 <get_command_output+0xd6>
xor    %r12d,%r12d
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %r12,%rax
add    $0x808,%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 i8* @get_command_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca [2048 x i8]
store i8* %0, i8** %3
%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** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %57
15: ; preds = %1
store i8* null, i8** %5
store i64 0, i64* %7
br label %16
16: ; preds = %44, %15
%17 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i64 @fgets(i8* %17, i32 2048, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %53
21: ; preds = %16
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %8
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i64 @malloc(i32 2048)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %5
store i64 2048, i64* %6
%29 = load i8*, i8** %5
store i8 0, i8* %29
br label %44
30: ; preds = %21
%31 = load i64, i64* %7
%32 = load i64, i64* %8
%33 = add i64 %31, %32
%34 = add i64 %33, 1
%35 = load i64, i64* %6
%36 = icmp uge i64 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = add i64 %39, 2048
store i64 %40, i64* %6
%41 = call i64 @realloc(i8* %38, i64 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %5
br label %43
43: ; preds = %37, %30
br label %44
44: ; preds = %43, %26
%45 = load i8*, i8** %5
%46 = load i64, i64* %7
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%49 = call i32 @strcpy(i8* %47, i8* %48)
%50 = load i64, i64* %8
%51 = load i64, i64* %7
%52 = add i64 %51, %50
store i64 %52, i64* %7
br label %16
53: ; preds = %16
%54 = load i32*, i32** %4
%55 = call i32 @pclose(i32* %54)
%56 = load i8*, i8** %5
store i8* %56, i8** %2
br label %57
57: ; preds = %53, %14
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @get_command_output(i8* nocapture %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %38, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 2048, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %6, %27
%10 = phi i64 [ %28, %27 ], [ 0, %6 ]
%11 = phi i64 [ %30, %27 ], [ undef, %6 ]
%12 = phi i8* [ %29, %27 ], [ null, %6 ]
%13 = call i64 @strlen(i8* %3)
%14 = icmp eq i8* %12, null
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = call i64 @malloc(i32 2048)
%17 = inttoptr i64 %16 to i8*
store i8 0, i8* %17
%18 = add i64 %13, %10
br label %27
19: ; preds = %9
%20 = add i64 %13, %10
%21 = add i64 %20, 1
%22 = icmp ult i64 %21, %11
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = add i64 %11, 2048
%25 = call i64 @realloc(i8* %12, i64 %24)
%26 = inttoptr i64 %25 to i8*
br label %27
27: ; preds = %19, %23, %15
%28 = phi i64 [ %20, %19 ], [ %20, %23 ], [ %18, %15 ]
%29 = phi i8* [ %12, %19 ], [ %26, %23 ], [ %17, %15 ]
%30 = phi i64 [ %11, %19 ], [ %24, %23 ], [ 2048, %15 ]
%31 = getelementptr inbounds i8, i8* %29, i64 %10
%32 = call i32 @strcpy(i8* %31, i8* %3)
%33 = call i64 @fgets(i8* %3, i32 2048, i32* %4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %9
35: ; preds = %27, %6
%36 = phi i8* [ null, %6 ], [ %29, %27 ]
%37 = call i32 @pclose(i32* %4)
br label %38
38: ; preds = %1, %35
%39 = phi i8* [ %36, %35 ], [ null, %1 ]
ret i8* %39
}