#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ system(char *);
char *result(char *command) {
  char buffer[1024];
  FILE *fp = NULL;
  static char *data = NULL;
  static size_t capacity = 0;
  size_t chars_read = 0;
  size_t tot_chars_read = 0;
  snprintf(buffer, 1024, "%s >tempdata.dat", command);
  system(buffer);
  fp = fopen("tempdata.dat", "r");
  if (!fp) {
    printf("could not open temporary data file\n");
    exit(1);
  }
  while (1) {
    if (tot_chars_read >= capacity)
      data = realloc(data, capacity += 1000);
    chars_read = fread(data + tot_chars_read, 1, 1000, fp);
    if (!chars_read)
      break;
    tot_chars_read += chars_read;
  }
  data = realloc(data, capacity + 5);
  data[tot_chars_read] = '\n';
  data[tot_chars_read + 1] = 0;
  fclose(fp);
  return data;
}


x86 assembly (clang, -O0):
<result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
movq   $0x0,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe39(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <system@plt>
lea    0xe25(%rip),%rdi
lea    0xe2b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1216 <result+0x96>
lea    0xe0c(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    121b <result+0x9b>
mov    -0x428(%rbp),%rax
cmp    0x2e47(%rip),%rax
jb     1257 <result+0xd7>
mov    0x2e32(%rip),%rdi
mov    0x2e33(%rip),%rsi
add    $0x3e8,%rsi
mov    %rsi,0x2e25(%rip)
call   1080 <realloc@plt>
mov    %rax,0x2e11(%rip)
mov    0x2e0a(%rip),%rdi
add    -0x428(%rbp),%rdi
mov    -0x418(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1030 <fread@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1295 <result+0x115>
jmp    12af <result+0x12f>
mov    -0x420(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
jmp    121b <result+0x9b>
mov    0x2db2(%rip),%rdi
mov    0x2db3(%rip),%rsi
add    $0x5,%rsi
call   1080 <realloc@plt>
mov    %rax,0x2d9b(%rip)
mov    0x2d94(%rip),%rax
mov    -0x428(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    0x2d82(%rip),%rax
mov    -0x428(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x418(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d63(%rip),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<result>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rcx
lea    0xe6c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <system@plt>
lea    0xe51(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1261 <result+0xe1>
mov    %rax,%r14
xor    %ecx,%ecx
jmp    11ee <result+0x6e>
nop
add    %rbx,%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
lea    (%rax,%rbx,1),%rcx
test   %rax,%rax
je     1223 <result+0xa3>
mov    %rcx,%rbx
mov    0x2e78(%rip),%rsi
mov    0x2e69(%rip),%rdi
cmp    %rsi,%rcx
jb     11d0 <result+0x50>
add    $0x3e8,%rsi
mov    %rsi,0x2e5e(%rip)
call   1080 <realloc@plt>
mov    %rax,%rdi
mov    %rax,0x2e47(%rip)
jmp    11d0 <result+0x50>
mov    0x2e3e(%rip),%rdi
mov    0x2e3f(%rip),%rsi
add    $0x5,%rsi
call   1080 <realloc@plt>
mov    %rax,0x2e27(%rip)
movw   $0xa,(%rax,%rbx,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x2e12(%rip),%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdab(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@result.data = internal global i8* null
@result.capacity = internal global i64 0
@.str = [17 x i8] c"%s >tempdata.dat\00"
@.str.1 = [13 x i8] c"tempdata.dat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [36 x i8] c"could not open temporary data file\0A\00"
define i8* @result(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
store i32* null, i32** %4
store i64 0, i64* %5
store i64 0, i64* %6
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32 @system(i8* %10)
%12 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
br label %19
19: ; preds = %18, %37
%20 = load i64, i64* %6
%21 = load i64, i64* @result.capacity
%22 = icmp uge i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** @result.data
%25 = load i64, i64* @result.capacity
%26 = add i64 %25, 1000
store i64 %26, i64* @result.capacity
%27 = call i8* @realloc(i8* %24, i64 %26)
store i8* %27, i8** @result.data
br label %28
28: ; preds = %23, %19
%29 = load i8*, i8** @result.data
%30 = load i64, i64* %6
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i32*, i32** %4
%33 = call i64 @fread(i8* %31, i32 1, i32 1000, i32* %32)
store i64 %33, i64* %5
%34 = load i64, i64* %5
%35 = icmp ne i64 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %28
br label %41
37: ; preds = %28
%38 = load i64, i64* %5
%39 = load i64, i64* %6
%40 = add i64 %39, %38
store i64 %40, i64* %6
br label %19
41: ; preds = %36
%42 = load i8*, i8** @result.data
%43 = load i64, i64* @result.capacity
%44 = add i64 %43, 5
%45 = call i8* @realloc(i8* %42, i64 %44)
store i8* %45, i8** @result.data
%46 = load i8*, i8** @result.data
%47 = load i64, i64* %6
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 10, i8* %48
%49 = load i8*, i8** @result.data
%50 = load i64, i64* %6
%51 = add i64 %50, 1
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** @result.data
ret i8* %55
}

LLVM IR (-O2):
@result.data = internal unnamed_addr global i8* null
@result.capacity = internal unnamed_addr global i64 0
@.str = [17 x i8] c"%s >tempdata.dat\00"
@.str.1 = [13 x i8] c"tempdata.dat\00"
@.str.2 = [2 x i8] c"r\00"
@str = [35 x i8] c"could not open temporary data file\00"
define i8* @result(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32 @system(i8* %3)
%6 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1, %19
%12 = phi i64 [ %24, %19 ], [ 0, %1 ]
%13 = load i64, i64* @result.capacity
%14 = icmp ult i64 %12, %13
%15 = load i8*, i8** @result.data
br i1 %14, label %19, label %16
16: ; preds = %11
%17 = add i64 %13, 1000
store i64 %17, i64* @result.capacity
%18 = call i8* @realloc(i8* %15, i64 %17)
store i8* %18, i8** @result.data
br label %19
19: ; preds = %11, %16
%20 = phi i8* [ %18, %16 ], [ %15, %11 ]
%21 = getelementptr inbounds i8, i8* %20, i64 %12
%22 = call i64 @fread(i8* %21, i32 1, i32 1000, i32* %6)
%23 = icmp eq i64 %22, 0
%24 = add i64 %22, %12
br i1 %23, label %25, label %11
25: ; preds = %19
%26 = load i8*, i8** @result.data
%27 = load i64, i64* @result.capacity
%28 = add i64 %27, 5
%29 = call i8* @realloc(i8* %26, i64 %28)
store i8* %29, i8** @result.data
%30 = getelementptr inbounds i8, i8* %29, i64 %12
store i8 10, i8* %30
%31 = add i64 %12, 1
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = call i32 @fclose(i32* %6)
%34 = load i8*, i8** @result.data
ret i8* %34
}