#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;
char *checked_realloc(char *, size_t);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
size_t stdin_read(char **buffer) {
  char *buf = NULL;
  size_t bufsize = 0, i = 0;
  int c;
  while (1) {
    c = fgetc(stdin);
    if (c == EOF)
      break;
    if (i + 1 >= bufsize) {
      bufsize += 32;
      buf = checked_realloc(buf, bufsize);
    }
    buf[i++] = c;
  }
  if (buf)
    buf[i] = '\0';
  *buffer = buf;
  return i;
}


x86 assembly (clang, -O0):
<stdin_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%eax
jne    116c <stdin_read+0x4c>
jmp    11bb <stdin_read+0x9b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jb     119b <stdin_read+0x7b>
mov    -0x18(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <checked_realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1144 <stdin_read+0x24>
cmpq   $0x0,-0x10(%rbp)
je     11d2 <stdin_read+0xb2>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eb3(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    0x2e9d(%rip),%r14
xor    %r15d,%r15d
mov    $0x0,%r13d
cmp    (%r14),%eax
je     11b2 <stdin_read+0x92>
mov    %eax,%ebp
mov    %rbx,(%rsp)
xor    %r15d,%r15d
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    118c <stdin_read+0x6c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %bpl,(%r15,%rbx,1)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,%ebp
mov    %r13,%rbx
cmp    (%r14),%eax
je     11a9 <stdin_read+0x89>
lea    0x1(%rbx),%r13
cmp    %r12,%r13
jb     1170 <stdin_read+0x50>
add    $0x20,%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <checked_realloc@plt>
mov    %rax,%r15
jmp    1170 <stdin_read+0x50>
movb   $0x0,(%r15,%r13,1)
mov    (%rsp),%rbx
mov    %r15,(%rbx)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i32 0
define i64 @stdin_read(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8** %0, i8*** %2
store i8* null, i8** %3
store i64 0, i64* %4
store i64 0, i64* %5
br label %7
7: ; preds = %1, %25
%8 = load i32, i32* @stdin
%9 = call i32 @fgetc(i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %7
br label %32
14: ; preds = %7
%15 = load i64, i64* %5
%16 = add i64 %15, 1
%17 = load i64, i64* %4
%18 = icmp uge i64 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i64, i64* %4
%21 = add i64 %20, 32
store i64 %21, i64* %4
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = call i8* @checked_realloc(i8* %22, i64 %23)
store i8* %24, i8** %3
br label %25
25: ; preds = %19, %14
%26 = load i32, i32* %6
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %3
%29 = load i64, i64* %5
%30 = add i64 %29, 1
store i64 %30, i64* %5
%31 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 %27, i8* %31
br label %7
32: ; preds = %13
%33 = load i8*, i8** %3
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i8*, i8** %3
%37 = load i64, i64* %5
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %35, %32
%40 = load i8*, i8** %3
%41 = load i8**, i8*** %2
store i8* %40, i8** %41
%42 = load i64, i64* %5
ret i64 %42
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
define i64 @stdin_read(i8** nocapture writeonly %0) {
%2 = load i32, i32* @stdin
%3 = tail call i32 @fgetc(i32 %2)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %27, label %6
6: ; preds = %1, %16
%7 = phi i32 [ %22, %16 ], [ %3, %1 ]
%8 = phi i8* [ %18, %16 ], [ null, %1 ]
%9 = phi i64 [ %11, %16 ], [ 0, %1 ]
%10 = phi i64 [ %17, %16 ], [ 0, %1 ]
%11 = add i64 %9, 1
%12 = icmp ult i64 %11, %10
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = add i64 %10, 32
%15 = tail call i8* @checked_realloc(i8* %8, i64 %14)
br label %16
16: ; preds = %13, %6
%17 = phi i64 [ %14, %13 ], [ %10, %6 ]
%18 = phi i8* [ %15, %13 ], [ %8, %6 ]
%19 = trunc i32 %7 to i8
%20 = getelementptr inbounds i8, i8* %18, i64 %9
store i8 %19, i8* %20
%21 = load i32, i32* @stdin
%22 = tail call i32 @fgetc(i32 %21)
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %6
25: ; preds = %16
%26 = getelementptr inbounds i8, i8* %18, i64 %11
store i8 0, i8* %26
br label %27
27: ; preds = %1, %25
%28 = phi i8* [ %18, %25 ], [ null, %1 ]
%29 = phi i64 [ %11, %25 ], [ 0, %1 ]
store i8* %28, i8** %0
ret i64 %29
}