#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int);
int getchar();
scalar_t__ isatty(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_usage(char *);
char *realloc(char *, int);
char *read_stdin() {
  if (isatty(STDIN_FILENO)) {
    print_usage("ERROR: Not reading from tty!");
  }
  long long buffer_size, content_size;
  buffer_size = 50;
  content_size = 0;
  char *buffer = malloc(sizeof(char) * buffer_size);
  int c;
  while ((c = getchar()) != EOF) {
    content_size++;
    if (content_size > buffer_size) {
      buffer_size += 50;
      buffer = realloc(buffer, sizeof(char) * buffer_size);
      if (buffer == NULL) {
        perror("ERROR: Unable to allocate memory!");
        exit(2);
      }
    }
    buffer[content_size - 1] = (char)c;
  }
  if (content_size != buffer_size)
    buffer[content_size] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     119c <read_stdin+0x2c>
lea    0xe69(%rip),%rdi
call   1090 <print_usage@plt>
movq   $0x32,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
je     1254 <read_stdin+0xe4>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jle    123b <read_stdin+0xcb>
mov    -0x8(%rbp),%rax
add    $0x32,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <read_stdin+0xc6>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    123b <read_stdin+0xcb>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11bf <read_stdin+0x4f>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     126e <read_stdin+0xfe>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1197 <read_stdin+0x27>
lea    0xe6e(%rip),%rdi
call   1090 <print_usage@plt>
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2e34(%rip),%r12
cmp    (%r12),%eax
je     121e <read_stdin+0xae>
mov    %eax,%ebp
mov    $0x32,%r15d
xor    %ebx,%ebx
jmp    11e7 <read_stdin+0x77>
cs nopw 0x0(%rax,%rax,1)
mov    %bpl,(%r14,%rbx,1)
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebp
add    $0x1,%rbx
cmp    (%r12),%eax
je     1219 <read_stdin+0xa9>
cmp    %r15,%rbx
jl     11d0 <read_stdin+0x60>
add    $0x32,%r15
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <read_stdin+0x60>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
cmp    %r15,%rbx
je     1223 <read_stdin+0xb3>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [29 x i8] c"ERROR: Not reading from tty!\00"
@EOF = global i32 0
@.str.1 = [34 x i8] c"ERROR: Unable to allocate memory!\00"
define i8* @read_stdin() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = load i32, i32* @STDIN_FILENO
%6 = call i64 @isatty(i32 %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @print_usage(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
store i64 50, i64* %1
store i64 0, i64* %2
%11 = load i64, i64* %1
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %3
br label %15
15: ; preds = %39, %10
%16 = call i32 (...) @getchar()
store i32 %16, i32* %4
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %46
19: ; preds = %15
%20 = load i64, i64* %2
%21 = add nsw i64 %20, 1
store i64 %21, i64* %2
%22 = load i64, i64* %2
%23 = load i64, i64* %1
%24 = icmp sgt i64 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %19
%26 = load i64, i64* %1
%27 = add nsw i64 %26, 50
store i64 %27, i64* %1
%28 = load i8*, i8** %3
%29 = load i64, i64* %1
%30 = mul i64 1, %29
%31 = trunc i64 %30 to i32
%32 = call i8* @realloc(i8* %28, i32 %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %25
br label %39
39: ; preds = %38, %19
%40 = load i32, i32* %4
%41 = trunc i32 %40 to i8
%42 = load i8*, i8** %3
%43 = load i64, i64* %2
%44 = sub nsw i64 %43, 1
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 %41, i8* %45
br label %15
46: ; preds = %15
%47 = load i64, i64* %2
%48 = load i64, i64* %1
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %3
%52 = load i64, i64* %2
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %50, %46
%55 = load i8*, i8** %3
ret i8* %55
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [29 x i8] c"ERROR: Not reading from tty!\00"
@EOF = global i32 0
@.str.1 = [34 x i8] c"ERROR: Unable to allocate memory!\00"
define i8* @read_stdin() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i64 @isatty(i32 %1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @print_usage(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%8 = tail call i32 (...) @getchar()
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %36, label %11
11: ; preds = %6, %26
%12 = phi i32 [ %31, %26 ], [ %8, %6 ]
%13 = phi i64 [ %28, %26 ], [ 50, %6 ]
%14 = phi i8* [ %27, %26 ], [ %7, %6 ]
%15 = phi i64 [ %16, %26 ], [ 0, %6 ]
%16 = add nuw nsw i64 %15, 1
%17 = icmp slt i64 %15, %13
br i1 %17, label %26, label %18
18: ; preds = %11
%19 = add nsw i64 %13, 50
%20 = trunc i64 %19 to i32
%21 = tail call i8* @realloc(i8* %14, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 2)
unreachable
26: ; preds = %18, %11
%27 = phi i8* [ %21, %18 ], [ %14, %11 ]
%28 = phi i64 [ %19, %18 ], [ %13, %11 ]
%29 = trunc i32 %12 to i8
%30 = getelementptr inbounds i8, i8* %27, i64 %15
store i8 %29, i8* %30
%31 = tail call i32 (...) @getchar()
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %11
34: ; preds = %26
%35 = icmp eq i64 %16, %28
br i1 %35, label %40, label %36
36: ; preds = %6, %34
%37 = phi i8* [ %27, %34 ], [ %7, %6 ]
%38 = phi i64 [ %16, %34 ], [ 0, %6 ]
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %36, %34
%41 = phi i8* [ %37, %36 ], [ %27, %34 ]
ret i8* %41
}