#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__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *read_stdin(void) {
  int n = 0;
  int t = 512;
  char *s = NULL;
  for (;;) {
    char *ss = realloc(s, t);
    if (!ss) {
      free(s);
      fprintf(stderr, "cannot allocate storage for stdin contents\n");
      return NULL;
    }
    s = ss;
    n += fread(s + n, 1, t - n - 1, stdin);
    if (n < t - 1)
      break;
    t *= 2;
  }
  if (ferror(stdin)) {
    free(s);
    fprintf(stderr, "error reading stdin\n");
    return NULL;
  }
  s[n] = 0;
  return s;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0x200,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <read_stdin+0x64>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    126b <read_stdin+0x11b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%edx
sub    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1206 <read_stdin+0xb6>
jmp    1214 <read_stdin+0xc4>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    116e <read_stdin+0x1e>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1257 <read_stdin+0x107>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    126b <read_stdin+0x11b>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x200,%r15d
xor    %ebx,%ebx
mov    0x2e80(%rip),%r13
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15d,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11de <read_stdin+0x8e>
mov    %rax,%r14
movslq %ebx,%r12
mov    %rax,%rdi
add    %r12,%rdi
mov    %r12d,%edx
not    %edx
add    %r15d,%edx
mov    0x0(%r13),%ecx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,%rbx
add    %r12d,%ebx
lea    -0x1(%r15),%eax
add    %r15d,%r15d
mov    %r14,%r12
cmp    %eax,%ebx
jge    1170 <read_stdin+0x20>
mov    0x0(%r13),%edi
call   1040 <ferror@plt>
test   %rax,%rax
je     120b <read_stdin+0xbb>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
jmp    11f6 <read_stdin+0xa6>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
jmp    11fe <read_stdin+0xae>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"cannot allocate storage for stdin contents\0A\00"
@stdin = global i32 0
@.str.1 = [21 x i8] c"error reading stdin\0A\00"
define i8* @read_stdin() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %2
store i32 512, i32* %3
store i8* null, i8** %4
br label %6
6: ; preds = %38, %0
%7 = load i8*, i8** %4
%8 = load i32, i32* %3
%9 = call i8* @realloc(i8* %7, i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = load i8*, i8** %4
%14 = call i32 @free(i8* %13)
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %56
17: ; preds = %6
%18 = load i8*, i8** %5
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = load i32, i32* %2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i32, i32* %3
%24 = load i32, i32* %2
%25 = sub nsw i32 %23, %24
%26 = sub nsw i32 %25, 1
%27 = load i32, i32* @stdin
%28 = call i64 @fread(i8* %22, i32 1, i32 %26, i32 %27)
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = load i32, i32* %3
%35 = sub nsw i32 %34, 1
%36 = icmp slt i32 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %17
br label %41
38: ; preds = %17
%39 = load i32, i32* %3
%40 = mul nsw i32 %39, 2
store i32 %40, i32* %3
br label %6
41: ; preds = %37
%42 = load i32, i32* @stdin
%43 = call i64 @ferror(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i8*, i8** %4
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %56
50: ; preds = %41
%51 = load i8*, i8** %4
%52 = load i32, i32* %2
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %4
store i8* %55, i8** %1
br label %56
56: ; preds = %50, %45, %12
%57 = load i8*, i8** %1
ret i8* %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"cannot allocate storage for stdin contents\0A\00"
@stdin = global i32 0
@.str.1 = [21 x i8] c"error reading stdin\0A\00"
define i8* @read_stdin() {
br label %1
1: ; preds = %11, %0
%2 = phi i32 [ 0, %0 ], [ %19, %11 ]
%3 = phi i32 [ 512, %0 ], [ %22, %11 ]
%4 = phi i8* [ null, %0 ], [ %5, %11 ]
%5 = tail call i8* @realloc(i8* %4, i32 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i32 @free(i8* %4)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %34
11: ; preds = %1
%12 = sext i32 %2 to i64
%13 = getelementptr inbounds i8, i8* %5, i64 %12
%14 = xor i32 %2, -1
%15 = add i32 %3, %14
%16 = load i32, i32* @stdin
%17 = tail call i64 @fread(i8* %13, i32 1, i32 %15, i32 %16)
%18 = trunc i64 %17 to i32
%19 = add i32 %2, %18
%20 = add nsw i32 %3, -1
%21 = icmp slt i32 %19, %20
%22 = shl nsw i32 %3, 1
br i1 %21, label %23, label %1
23: ; preds = %11
%24 = load i32, i32* @stdin
%25 = tail call i64 @ferror(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = tail call i32 @free(i8* %5)
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
31: ; preds = %23
%32 = sext i32 %19 to i64
%33 = getelementptr inbounds i8, i8* %5, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %7, %31, %27
%35 = phi i8* [ null, %27 ], [ %5, %31 ], [ null, %7 ]
ret i8* %35
}