#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 EOF;
int getchar();
unsigned char *realloc(unsigned char *, int);
unsigned char *loadstdin(size_t *nbytes) {
  static unsigned char *buf;
  static int n_read = 0, n_alloced = 0;
  int ch;
  while (EOF != (ch = getchar())) {
    if (n_read == n_alloced) {
      n_alloced += 8192;
      buf = realloc(buf, n_alloced);
    }
    buf[n_read++] = ch;
  }
  *nbytes = n_read;
  return buf;
}


x86 assembly (clang, -O0):
<loadstdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
mov    %ecx,-0xc(%rbp)
cmp    %ecx,%eax
je     11b3 <loadstdin+0x93>
mov    0x2eeb(%rip),%eax
cmp    0x2ee9(%rip),%eax
jne    118b <loadstdin+0x6b>
mov    0x2edd(%rip),%eax
add    $0x2000,%eax
mov    %eax,0x2ed2(%rip)
mov    0x2ebf(%rip),%rdi
mov    0x2ec5(%rip),%esi
call   1040 <realloc@plt>
mov    %rax,0x2ead(%rip)
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    0x2ea1(%rip),%rax
mov    0x2ea3(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e98(%rip)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    112c <loadstdin+0xc>
movslq 0x2e86(%rip),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e70(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadstdin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2eb6(%rip),%r12
mov    (%r12),%r15d
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    %eax,%r15d
jne    115c <loadstdin+0x3c>
movslq 0x2ef7(%rip),%rax
mov    %rax,(%r14)
mov    0x2ee5(%rip),%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
jmp    11a6 <loadstdin+0x86>
add    $0x2000,%esi
mov    %esi,0x2ed8(%rip)
mov    0x2ec5(%rip),%rdi
call   1040 <realloc@plt>
mov    %rax,0x2eb9(%rip)
mov    0x2ebb(%rip),%esi
lea    0x1(%rsi),%ecx
mov    %ecx,0x2eb2(%rip)
movslq %esi,%rcx
mov    %bpl,(%rax,%rcx,1)
mov    (%r12),%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
cmp    %eax,%ebx
je     1142 <loadstdin+0x22>
mov    0x2e94(%rip),%esi
cmp    0x2e92(%rip),%esi
je     1160 <loadstdin+0x40>
mov    0x2e7d(%rip),%rax
jmp    1185 <loadstdin+0x65>

LLVM IR (-O0):
@loadstdin.buf = internal global i8* null
@loadstdin.n_read = internal global i32 0
@loadstdin.n_alloced = internal global i32 0
@EOF = global i32 0
define i8* @loadstdin(i64* %0) {
%2 = alloca i64*
%3 = alloca i32
store i64* %0, i64** %2
br label %4
4: ; preds = %18, %1
%5 = load i32, i32* @EOF
%6 = call i32 (...) @getchar()
store i32 %6, i32* %3
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %26
8: ; preds = %4
%9 = load i32, i32* @loadstdin.n_read
%10 = load i32, i32* @loadstdin.n_alloced
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* @loadstdin.n_alloced
%14 = add nsw i32 %13, 8192
store i32 %14, i32* @loadstdin.n_alloced
%15 = load i8*, i8** @loadstdin.buf
%16 = load i32, i32* @loadstdin.n_alloced
%17 = call i8* @realloc(i8* %15, i32 %16)
store i8* %17, i8** @loadstdin.buf
br label %18
18: ; preds = %12, %8
%19 = load i32, i32* %3
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** @loadstdin.buf
%22 = load i32, i32* @loadstdin.n_read
%23 = add nsw i32 %22, 1
store i32 %23, i32* @loadstdin.n_read
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
br label %4
26: ; preds = %4
%27 = load i32, i32* @loadstdin.n_read
%28 = sext i32 %27 to i64
%29 = load i64*, i64** %2
store i64 %28, i64* %29
%30 = load i8*, i8** @loadstdin.buf
ret i8* %30
}

LLVM IR (-O2):
@loadstdin.buf = internal unnamed_addr global i8* null
@loadstdin.n_read = internal unnamed_addr global i32 0
@loadstdin.n_alloced = internal unnamed_addr global i32 0
@EOF = global i32 0
define i8* @loadstdin(i64* nocapture writeonly %0) {
%2 = load i32, i32* @EOF
%3 = tail call i32 (...) @getchar()
%4 = icmp eq i32 %2, %3
br i1 %4, label %27, label %5
5: ; preds = %1, %17
%6 = phi i32 [ %25, %17 ], [ %3, %1 ]
%7 = load i32, i32* @loadstdin.n_read
%8 = load i32, i32* @loadstdin.n_alloced
%9 = icmp eq i32 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = load i8*, i8** @loadstdin.buf
br label %17
12: ; preds = %5
%13 = add nsw i32 %7, 8192
store i32 %13, i32* @loadstdin.n_alloced
%14 = load i8*, i8** @loadstdin.buf
%15 = tail call i8* @realloc(i8* %14, i32 %13)
store i8* %15, i8** @loadstdin.buf
%16 = load i32, i32* @loadstdin.n_read
br label %17
17: ; preds = %10, %12
%18 = phi i32 [ %7, %10 ], [ %16, %12 ]
%19 = phi i8* [ %11, %10 ], [ %15, %12 ]
%20 = trunc i32 %6 to i8
%21 = add nsw i32 %18, 1
store i32 %21, i32* @loadstdin.n_read
%22 = sext i32 %18 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 %20, i8* %23
%24 = load i32, i32* @EOF
%25 = tail call i32 (...) @getchar()
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %5
27: ; preds = %17, %1
%28 = load i32, i32* @loadstdin.n_read
%29 = sext i32 %28 to i64
store i64 %29, i64* %0
%30 = load i8*, i8** @loadstdin.buf
ret i8* %30
}