#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*/ feof(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
size_t fread(char *, int, int, int /*<<< orphan*/);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
int read_stdin(char **buf) {
  int pos = 0;
  int size = 1024;
  *buf = malloc(size);
  while (!feof(stdin)) {
    if (pos >= size - 1) {
      size *= 2;
      *buf = realloc(*buf, size);
    }
    size_t read = fread(*buf + pos, 1, size - pos, stdin);
    if (ferror(stdin)) {
      return -1;
    }
    pos += read;
  }
  return pos;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119f <read_stdin+0x4f>
jmp    1234 <read_stdin+0xe4>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     11d2 <read_stdin+0x82>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1221 <read_stdin+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <read_stdin+0xea>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    117c <read_stdin+0x2c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x400,%r15d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
mov    0x2e71(%rip),%r12
jmp    11bf <read_stdin+0x6f>
nopl   0x0(%rax)
add    %r15d,%r15d
mov    (%r14),%rdi
mov    %r15d,%esi
call   1070 <realloc@plt>
mov    %rax,(%r14)
movslq %ebp,%rdi
add    %rax,%rdi
mov    %r15d,%edx
sub    %ebp,%edx
mov    (%r12),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rbx
mov    (%r12),%edi
call   1030 <ferror@plt>
add    %ebp,%ebx
mov    %ebx,%ebp
test   %rax,%rax
jne    11d9 <read_stdin+0x89>
mov    (%r12),%edi
call   1050 <feof@plt>
test   %eax,%eax
jne    11de <read_stdin+0x8e>
lea    -0x1(%r15),%eax
cmp    %eax,%ebp
jge    1180 <read_stdin+0x30>
mov    (%r14),%rax
jmp    1191 <read_stdin+0x41>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @read_stdin(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i8** %0, i8*** %3
store i32 0, i32* %4
store i32 1024, i32* %5
%7 = load i32, i32* %5
%8 = call i8* @malloc(i32 %7)
%9 = load i8**, i8*** %3
store i8* %8, i8** %9
br label %10
10: ; preds = %43, %1
%11 = load i32, i32* @stdin
%12 = call i32 @feof(i32 %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %49
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i32, i32* %5
%18 = sub nsw i32 %17, 1
%19 = icmp sge i32 %16, %18
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i32, i32* %5
%22 = mul nsw i32 %21, 2
store i32 %22, i32* %5
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
%25 = load i32, i32* %5
%26 = call i8* @realloc(i8* %24, i32 %25)
%27 = load i8**, i8*** %3
store i8* %26, i8** %27
br label %28
28: ; preds = %20, %15
%29 = load i8**, i8*** %3
%30 = load i8*, i8** %29
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %5
%35 = load i32, i32* %4
%36 = sub nsw i32 %34, %35
%37 = load i32, i32* @stdin
%38 = call i64 @fread(i8* %33, i32 1, i32 %36, i32 %37)
store i64 %38, i64* %6
%39 = load i32, i32* @stdin
%40 = call i64 @ferror(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %28
store i32 -1, i32* %2
br label %51
43: ; preds = %28
%44 = load i64, i64* %6
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = add i64 %46, %44
%48 = trunc i64 %47 to i32
store i32 %48, i32* %4
br label %10
49: ; preds = %10
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %51
51: ; preds = %49, %42
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @read_stdin(i8** nocapture %0) {
%2 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
store i8* %2, i8** %0
br label %3
3: ; preds = %18, %1
%4 = phi i32 [ 0, %1 ], [ %30, %18 ]
%5 = phi i32 [ 1024, %1 ], [ %20, %18 ]
%6 = load i32, i32* @stdin
%7 = tail call i32 @feof(i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %31
9: ; preds = %3
%10 = add nsw i32 %5, -1
%11 = icmp slt i32 %4, %10
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i8*, i8** %0
br label %18
14: ; preds = %9
%15 = shl nsw i32 %5, 1
%16 = load i8*, i8** %0
%17 = tail call i8* @realloc(i8* %16, i32 %15)
store i8* %17, i8** %0
br label %18
18: ; preds = %12, %14
%19 = phi i8* [ %17, %14 ], [ %13, %12 ]
%20 = phi i32 [ %15, %14 ], [ %5, %12 ]
%21 = sext i32 %4 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = sub nsw i32 %20, %4
%24 = load i32, i32* @stdin
%25 = tail call i64 @fread(i8* %22, i32 1, i32 %23, i32 %24)
%26 = load i32, i32* @stdin
%27 = tail call i64 @ferror(i32 %26)
%28 = icmp eq i64 %27, 0
%29 = trunc i64 %25 to i32
%30 = add i32 %4, %29
br i1 %28, label %3, label %31
31: ; preds = %3, %18
%32 = phi i32 [ -1, %18 ], [ %4, %3 ]
ret i32 %32
}