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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
char *realloc(char *, size_t);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int chpl_run(const char *cmd, const char **out) {
  char buffer[1024];
  char *output = NULL;
  char *temp_output = NULL;
  size_t size = 1;
  size_t line_length;
  FILE *proc;
  if ((proc = popen(cmd, "r")) == NULL) {
  }
  while (fgets(buffer, sizeof(buffer), proc) != NULL) {
    line_length = strlen(buffer);
    temp_output = realloc(output, size + line_length);
    if (temp_output == NULL) {
    } else {
      output = temp_output;
    }
    strncpy(output + size - 1, buffer, strlen(buffer));
    size += line_length;
  }
  int result = pclose(proc);
  *out = output;
  return result;
}


x86 assembly (clang, -O0):
<chpl_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
movq   $0x1,-0x428(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
jne    11ba <chpl_run+0x5a>
jmp    11ba <chpl_run+0x5a>
jmp    11bf <chpl_run+0x5f>
lea    -0x410(%rbp),%rdi
mov    -0x438(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1299 <chpl_run+0x139>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x430(%rbp)
mov    -0x418(%rbp),%rdi
mov    -0x428(%rbp),%rsi
add    -0x430(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1228 <chpl_run+0xc8>
jmp    1236 <chpl_run+0xd6>
mov    -0x420(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
add    -0x428(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x450(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    -0x430(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
jmp    11bf <chpl_run+0x5f>
mov    -0x438(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x43c(%rbp)
mov    -0x418(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x43c(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chpl_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
lea    0xe83(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11fe <chpl_run+0x9e>
mov    $0x1,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%rbp,1),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
cmovne %rax,%rbx
lea    (%rbx,%rbp,1),%rdi
add    $0xffffffffffffffff,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
mov    %r14,%rbp
test   %rax,%rax
jne    11b0 <chpl_run+0x50>
jmp    1200 <chpl_run+0xa0>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    0x8(%rsp),%rcx
mov    %rbx,(%rcx)
add    $0x418,%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 i32 @chpl_run(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i8* null, i8** %6
store i8* null, i8** %7
store i64 1, i64* %8
%12 = load i8*, i8** %3
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
br label %16
16: ; preds = %15, %2
br label %17
17: ; preds = %35, %16
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %10
%20 = call i32* @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %47
22: ; preds = %17
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %9
%25 = load i8*, i8** %6
%26 = load i64, i64* %8
%27 = load i64, i64* %9
%28 = add i64 %26, %27
%29 = call i8* @realloc(i8* %25, i64 %28)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %22
br label %35
33: ; preds = %22
%34 = load i8*, i8** %7
store i8* %34, i8** %6
br label %35
35: ; preds = %33, %32
%36 = load i8*, i8** %6
%37 = load i64, i64* %8
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 -1
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
%43 = call i32 @strncpy(i8* %39, i8* %40, i64 %42)
%44 = load i64, i64* %9
%45 = load i64, i64* %8
%46 = add i64 %45, %44
store i64 %46, i64* %8
br label %17
47: ; preds = %17
%48 = load i32*, i32** %10
%49 = call i32 @pclose(i32* %48)
store i32 %49, i32* %11
%50 = load i8*, i8** %6
%51 = load i8**, i8*** %4
store i8* %50, i8** %51
%52 = load i32, i32* %11
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @chpl_run(i8* nocapture %0, i8** nocapture writeonly %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %21, label %8
8: ; preds = %2, %8
%9 = phi i8* [ %15, %8 ], [ null, %2 ]
%10 = phi i64 [ %12, %8 ], [ 1, %2 ]
%11 = call i64 @strlen(i8* %4)
%12 = add i64 %11, %10
%13 = call i8* @realloc(i8* %9, i64 %12)
%14 = icmp eq i8* %13, null
%15 = select i1 %14, i8* %9, i8* %13
%16 = getelementptr inbounds i8, i8* %15, i64 -1
%17 = getelementptr inbounds i8, i8* %16, i64 %10
%18 = call i32 @strncpy(i8* %17, i8* %4, i64 %11)
%19 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %8
21: ; preds = %8, %2
%22 = phi i8* [ null, %2 ], [ %15, %8 ]
%23 = call i32 @pclose(i32* %5)
store i8* %22, i8** %1
ret i32 %23
}