#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ ferror(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *readFromStdin(void) {
  char buffer[1024];
  size_t contentSize = 1;
  char *content = malloc(sizeof(char) * 1024);
  if (content == NULL) {
    fprintf(stderr, "FATAL ERROR: failed to allocate memory\n");
    exit(-1);
  }
  content[0] = '\0';
  while (fgets(buffer, 1024, stdin)) {
    char *old = content;
    contentSize += strlen(buffer);
    content = realloc(content, contentSize);
    if (content == NULL) {
      fprintf(stderr, "FATAL ERROR: failed to allocate memory\n");
      free(old);
      exit(-1);
    }
    strcat(content, buffer);
  }
  if (ferror(stdin)) {
    free(content);
    fprintf(stderr, "Input error while reading from stdin\n");
    exit(-1);
  }
  return content;
}


x86 assembly (clang, -O0):
<readFromStdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movq   $0x1,-0x408(%rbp)
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    11e4 <readFromStdin+0x54>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x400(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12a5 <readFromStdin+0x115>
mov    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x400(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x408(%rbp),%rax
mov    %rax,-0x408(%rbp)
mov    -0x410(%rbp),%rdi
mov    -0x408(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    128d <readFromStdin+0xfd>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   10a0 <strcat@plt>
jmp    11ee <readFromStdin+0x5e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12e8 <readFromStdin+0x158>
mov    -0x410(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFromStdin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1276 <readFromStdin+0xe6>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    0x2e28(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     122a <readFromStdin+0x9a>
mov    $0x1,%ebx
mov    %rsp,%r14
xchg   %ax,%ax
mov    %rbx,%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <realloc@plt>
test   %rax,%rax
je     124f <readFromStdin+0xbf>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %r12,%r15
test   %rax,%rax
jne    11e0 <readFromStdin+0x50>
jmp    122d <readFromStdin+0x9d>
mov    %r15,%r12
mov    0x0(%r13),%edi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1288 <readFromStdin+0xf8>
mov    %r12,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
jmp    12a0 <readFromStdin+0x110>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"FATAL ERROR: failed to allocate memory\0A\00"
@stdin = global i32 0
@.str.1 = [38 x i8] c"Input error while reading from stdin\0A\00"
define i8* @readFromStdin() {
%1 = alloca [1024 x i8]
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
store i64 1, i64* %2
%5 = call i8* @malloc(i32 1024)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %14
br label %15
15: ; preds = %37, %12
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%17 = load i32, i32* @stdin
%18 = call i64 @fgets(i8* %16, i32 1024, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %15
%21 = load i8*, i8** %3
store i8* %21, i8** %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
%24 = load i64, i64* %2
%25 = add i64 %24, %23
store i64 %25, i64* %2
%26 = load i8*, i8** %3
%27 = load i64, i64* %2
%28 = call i8* @realloc(i8* %26, i64 %27)
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %37
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %20
%38 = load i8*, i8** %3
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%40 = call i32 @strcat(i8* %38, i8* %39)
br label %15
41: ; preds = %15
%42 = load i32, i32* @stdin
%43 = call i64 @ferror(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i8*, i8** %3
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %41
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"FATAL ERROR: failed to allocate memory\0A\00"
@stdin = global i32 0
@.str.1 = [38 x i8] c"Input error while reading from stdin\0A\00"
define i8* @readFromStdin() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
store i8 0, i8* %3
%10 = load i32, i32* @stdin
%11 = call i64 @fgets(i8* %2, i32 1024, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %9, %25
%14 = phi i64 [ %17, %25 ], [ 1, %9 ]
%15 = phi i8* [ %18, %25 ], [ %3, %9 ]
%16 = call i64 @strlen(i8* %2)
%17 = add i64 %16, %14
%18 = call i8* @realloc(i8* %15, i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @free(i8* %15)
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %13
%26 = call i32 @strcat(i8* %18, i8* %2)
%27 = load i32, i32* @stdin
%28 = call i64 @fgets(i8* %2, i32 1024, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %13
30: ; preds = %25, %9
%31 = phi i8* [ %3, %9 ], [ %18, %25 ]
%32 = load i32, i32* @stdin
%33 = call i64 @ferror(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = call i32 @free(i8* %31)
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %30
ret i8* %31
}