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

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *get_output(char *command) {
  FILE *pfile = NULL;
  pfile = popen(command, "r");
  if (pfile == NULL) {
    fprintf(stderr, "Failed to run command\n");
    return NULL;
  }
  char temp[1035];
  char **full_output = NULL;
  char *output = NULL;
  full_output = (char **)calloc(1, sizeof(full_output));
  *full_output = "\0";
  while (fgets(temp, sizeof(temp) - 1, pfile) != NULL) {
    asprintf(full_output, "%s", temp);
  }
  pclose(pfile);
  output = (char *)calloc(strlen(*full_output), sizeof(char));
  strcpy(output, *full_output);
  free(full_output);
  int len = strlen(output);
  if (output[len - 1] == '\n')
    output[len - 1] = 0;
  return output;
}


x86 assembly (clang, -O0):
<get_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <get_output+0x58>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1304 <get_output+0x174>
movq   $0x0,-0x438(%rbp)
movq   $0x0,-0x440(%rbp)
mov    $0x1,%edi
mov    $0x8,%esi
call   1080 <calloc@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
lea    0xdfa(%rip),%rcx
mov    %rcx,(%rax)
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x40a,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1263 <get_output+0xd3>
mov    -0x438(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    0xdc0(%rip),%rsi
call   10a0 <asprintf@plt>
jmp    1225 <get_output+0x95>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x438(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rdi
mov    -0x438(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x438(%rbp),%rdi
call   1030 <free@plt>
mov    -0x440(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x444(%rbp)
mov    -0x440(%rbp),%rax
mov    -0x444(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12f9 <get_output+0x169>
mov    -0x440(%rbp),%rax
mov    -0x444(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x440(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_output>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xe5b(%rip),%rsi
call   10b0 <popen@plt>
test   %rax,%rax
je     126d <get_output+0xdd>
mov    %rax,%r12
mov    $0x1,%edi
mov    $0x8,%esi
call   1080 <calloc@plt>
mov    %rax,%r14
lea    0xe4d(%rip),%rax
mov    %rax,(%r14)
mov    %rsp,%rdi
mov    $0x40a,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1223 <get_output+0x93>
lea    0xe2b(%rip),%r15
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <asprintf@plt>
mov    %rbx,%rdi
mov    $0x40a,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <get_output+0x70>
mov    %r12,%rdi
call   1060 <pclose@plt>
mov    (%r14),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    (%r14),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1284 <get_output+0xf4>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    1284 <get_output+0xf4>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
call   1090 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to run command\0A\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%s\00"
define i8* @get_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1035 x i8]
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32* null, i32** %4
%9 = load i8*, i8** %3
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %61
16: ; preds = %1
store i8** null, i8*** %6
store i8* null, i8** %7
%17 = call i64 @calloc(i32 1, i32 8)
%18 = inttoptr i64 %17 to i8**
store i8** %18, i8*** %6
%19 = load i8**, i8*** %6
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %19
br label %20
20: ; preds = %25, %16
%21 = getelementptr inbounds [1035 x i8], [1035 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i32* @fgets(i8* %21, i32 1034, i32* %22)
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i8**, i8*** %6
%27 = getelementptr inbounds [1035 x i8], [1035 x i8]* %5, i64 0, i64 0
%28 = call i32 @asprintf(i8** %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %20
29: ; preds = %20
%30 = load i32*, i32** %4
%31 = call i32 @pclose(i32* %30)
%32 = load i8**, i8*** %6
%33 = load i8*, i8** %32
%34 = call i32 @strlen(i8* %33)
%35 = call i64 @calloc(i32 %34, i32 1)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %6
%39 = load i8*, i8** %38
%40 = call i32 @strcpy(i8* %37, i8* %39)
%41 = load i8**, i8*** %6
%42 = call i32 @free(i8** %41)
%43 = load i8*, i8** %7
%44 = call i32 @strlen(i8* %43)
store i32 %44, i32* %8
%45 = load i8*, i8** %7
%46 = load i32, i32* %8
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 10
br i1 %52, label %53, label %59
53: ; preds = %29
%54 = load i8*, i8** %7
%55 = load i32, i32* %8
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %53, %29
%60 = load i8*, i8** %7
store i8* %60, i8** %2
br label %61
61: ; preds = %59, %13
%62 = load i8*, i8** %2
ret i8* %62
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to run command\0A\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%s\00"
define i8* @get_output(i8* nocapture %0) {
%2 = alloca [1035 x i8]
%3 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %35
8: ; preds = %1
%9 = getelementptr inbounds [1035 x i8], [1035 x i8]* %2, i64 0, i64 0
%10 = tail call i64 @calloc(i32 1, i32 8)
%11 = inttoptr i64 %10 to i8**
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = call i32* @fgets(i8* %9, i32 1034, i32* %3)
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %8, %14
%15 = call i32 @asprintf(i8** %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%16 = call i32* @fgets(i8* %9, i32 1034, i32* %3)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %14
18: ; preds = %14, %8
%19 = call i32 @pclose(i32* %3)
%20 = load i8*, i8** %11
%21 = call i32 @strlen(i8* %20)
%22 = call i64 @calloc(i32 %21, i32 1)
%23 = inttoptr i64 %22 to i8*
%24 = load i8*, i8** %11
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = call i32 @free(i8** %11)
%27 = call i32 @strlen(i8* %23)
%28 = add nsw i32 %27, -1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %23, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 10
br i1 %32, label %33, label %34
33: ; preds = %18
store i8 0, i8* %30
br label %34
34: ; preds = %33, %18
br label %35
35: ; preds = %34, %5
%36 = phi i8* [ null, %5 ], [ %23, %34 ]
ret i8* %36
}