#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 */
scalar_t__ fgets(char *, int const, int /*<<< orphan*/);
char *malloc(int);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *stdin_to_string() {
  const int BUFFER_SIZE = 4096;
  char buffer[BUFFER_SIZE];
  size_t content_size = 1;
  char *content = malloc(sizeof(char) * BUFFER_SIZE);
  content[0] = '\0';
  while (fgets(buffer, BUFFER_SIZE, stdin)) {
    content_size += strlen(buffer);
    content = realloc(content, content_size);
    strcat(content, buffer);
  }
  return content;
}


x86 assembly (clang, -O0):
<stdin_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x1000,-0x4(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <stdin_to_string+0x94>
lea    -0x1020(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1070 <strcat@plt>
jmp    1186 <stdin_to_string+0x36>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_to_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    0x2e73(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     11da <stdin_to_string+0x8a>
mov    $0x1,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x1000,%esi
call   1040 <fgets@plt>
test   %rax,%rax
jne    11a0 <stdin_to_string+0x50>
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
define i8* @stdin_to_string() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i32 4096, i32* %1
%5 = call i8* @llvm.stacksave()
store i8* %5, i8** %2
%6 = alloca i8, i64 4096
store i64 1, i64* %3
%7 = call i8* @malloc(i32 4096)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
br label %10
10: ; preds = %14, %0
%11 = load i32, i32* @stdin
%12 = call i64 @fgets(i8* %6, i32 4096, i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = call i64 @strlen(i8* %6)
%16 = load i64, i64* %3
%17 = add i64 %16, %15
store i64 %17, i64* %3
%18 = load i8*, i8** %4
%19 = load i64, i64* %3
%20 = call i8* @realloc(i8* %18, i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = call i32 @strcat(i8* %21, i8* %6)
br label %10
23: ; preds = %10
%24 = load i8*, i8** %4
%25 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@stdin = global i32 0
define i8* @stdin_to_string() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
store i8 0, i8* %3
%4 = load i32, i32* @stdin
%5 = call i64 @fgets(i8* %2, i32 4096, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %0, %7
%8 = phi i8* [ %12, %7 ], [ %3, %0 ]
%9 = phi i64 [ %11, %7 ], [ 1, %0 ]
%10 = call i64 @strlen(i8* %2)
%11 = add i64 %10, %9
%12 = call i8* @realloc(i8* %8, i64 %11)
%13 = call i32 @strcat(i8* %12, i8* %2)
%14 = load i32, i32* @stdin
%15 = call i64 @fgets(i8* %2, i32 4096, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %0
%18 = phi i8* [ %3, %0 ], [ %12, %7 ]
ret i8* %18
}