#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 */
typedef int /*<<< orphan*/ input;

/* Variables and functions */
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
char *grab_stdin(size_t *_size) {
  static char input[65536 + 1];
  int n, tmp;
  n = 0;
  do {
    tmp = read(0, input + n, sizeof(input) - 1 - n);
    if (tmp < 0)
      error("stdin");
    if (tmp == 0)
      break;
    n += tmp;
  } while (n < sizeof(input));
  if (n >= sizeof(input)) {
    fprintf(stderr, "Too much data read on stdin\n");
    exit(1);
  }
  input[n] = '\0';
  *_size = n;
  return input;
}


x86 assembly (clang, -O0):
<grab_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
lea    0x2ef2(%rip),%rsi
add    %rax,%rsi
movslq -0xc(%rbp),%rcx
mov    $0x10000,%eax
sub    %rcx,%rax
mov    %eax,%edx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    118f <grab_stdin+0x4f>
lea    0xe76(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x0,-0x10(%rbp)
jne    119e <grab_stdin+0x5e>
jmp    11b7 <grab_stdin+0x77>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x10001,%rax
jb     1153 <grab_stdin+0x13>
movslq -0xc(%rbp),%rax
cmp    $0x10001,%rax
jb     11e6 <grab_stdin+0xa6>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movslq -0xc(%rbp),%rcx
lea    0x2e5f(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
movslq -0xc(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
lea    0x2e49(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
xor    %ebx,%ebx
lea    0x2ef8(%rip),%r13
lea    0xea1(%rip),%r15
jmp    1182 <grab_stdin+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <error@plt>
add    %ebp,%ebx
cmp    $0x10001,%ebx
jae    11c4 <grab_stdin+0x84>
mov    %ebx,%r14d
lea    (%r14,%r13,1),%r12
mov    $0x10000,%edx
sub    %ebx,%edx
xor    %edi,%edi
mov    %r12,%rsi
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1170 <grab_stdin+0x30>
jne    1178 <grab_stdin+0x38>
movb   $0x0,(%r12)
mov    (%rsp),%rax
mov    %r14,(%rax)
lea    0x2e9b(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@grab_stdin.input = internal global [65537 x i8] zeroinitializer
@.str = [6 x i8] c"stdin\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Too much data read on stdin\0A\00"
define i8* @grab_stdin(i64* %0) {
%2 = alloca i64*
%3 = alloca i32
%4 = alloca i32
store i64* %0, i64** %2
store i32 0, i32* %3
br label %5
5: ; preds = %26, %1
%6 = load i32, i32* %3
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* getelementptr inbounds ([65537 x i8], [65537 x i8]* @grab_stdin.input, i64 0, i64 0), i64 %7
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = sub i64 65536, %10
%12 = trunc i64 %11 to i32
%13 = call i32 @read(i32 0, i8* %8, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %5
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
br label %30
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = add nsw i32 %24, %23
store i32 %25, i32* %3
br label %26
26: ; preds = %22
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %28, 65537
br i1 %29, label %5, label %30
30: ; preds = %26, %21
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = icmp uge i64 %32, 65537
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [65537 x i8], [65537 x i8]* @grab_stdin.input, i64 0, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = load i64*, i64** %2
store i64 %43, i64* %44
ret i8* getelementptr inbounds ([65537 x i8], [65537 x i8]* @grab_stdin.input, i64 0, i64 0)
}

LLVM IR (-O2):
@grab_stdin.input = internal global [65537 x i8] zeroinitializer
@.str = [6 x i8] c"stdin\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Too much data read on stdin\0A\00"
define i8* @grab_stdin(i64* nocapture writeonly %0) {
br label %2
2: ; preds = %13, %1
%3 = phi i32 [ 0, %1 ], [ %14, %13 ]
%4 = zext i32 %3 to i64
%5 = getelementptr inbounds [65537 x i8], [65537 x i8]* @grab_stdin.input, i64 0, i64 %4
%6 = sub nuw nsw i32 65536, %3
%7 = tail call i32 @read(i32 0, i8* %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %2
%12 = icmp eq i32 %7, 0
br i1 %12, label %20, label %13
13: ; preds = %9, %11
%14 = add nsw i32 %7, %3
%15 = icmp ult i32 %14, 65537
br i1 %15, label %2, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = zext i32 %3 to i64
store i8 0, i8* %5
store i64 %21, i64* %0
ret i8* getelementptr inbounds ([65537 x i8], [65537 x i8]* @grab_stdin.input, i64 0, i64 0)
}