#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ *stdin;
void readStdin(char **read_buffer) {
  int file_size = 1;
  if (stdin == NULL) {
    printf("unknown failure\n");
    exit(EXIT_FAILURE);
  }
  *read_buffer = (char *)malloc(file_size);
  if (read_buffer == NULL) {
    printf("allocation fail\n");
    exit(EXIT_FAILURE);
  }
  int itr = 0;
  while (fread(*read_buffer + itr, 1, 1, stdin) == 1) {
    if (file_size == itr + 1) {
      file_size *= 2;
      *read_buffer = (char *)realloc(*read_buffer, file_size);
      if (*read_buffer == NULL) {
        printf("memory failure\n");
      }
    }
    itr++;
  }
  fclose(stdin);
}


x86 assembly (clang, -O0):
<readStdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e6e(%rip),%rax
cmpq   $0x0,(%rax)
jne    119e <readStdin+0x3e>
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    11d5 <readStdin+0x75>
lea    0xe4f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x10(%rbp),%rax
add    %rax,%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1269 <readStdin+0x109>
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    125b <readStdin+0xfb>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1256 <readStdin+0xf6>
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
jmp    125b <readStdin+0xfb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11dc <readStdin+0x7c>
mov    0x2d78(%rip),%rax
mov    (%rax),%rdi
call   1040 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readStdin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e79(%rip),%r15
cmpq   $0x0,(%r15)
je     1219 <readStdin+0xb9>
mov    %rdi,%r12
mov    $0x1,%ebp
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,(%r12)
mov    (%r15),%rcx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1209 <readStdin+0xa9>
mov    $0x1,%ebx
lea    0xe4b(%rip),%r14
jmp    11e2 <readStdin+0x82>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rdi
add    %rbx,%rdi
mov    (%r15),%rcx
mov    $0x1,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
add    $0x1,%rbx
cmp    $0x1,%eax
jne    1209 <readStdin+0xa9>
mov    %ebp,%eax
cmp    %rax,%rbx
jne    11c0 <readStdin+0x60>
add    %ebp,%ebp
mov    (%r12),%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    11c0 <readStdin+0x60>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    11c0 <readStdin+0x60>
mov    (%r15),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32* null
@.str = [17 x i8] c"unknown failure\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"allocation fail\0A\00"
@.str.2 = [16 x i8] c"memory failure\0A\00"
define void @readStdin(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
store i8** %0, i8*** %2
store i32 1, i32* %3
%5 = load i32*, i32** @stdin
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = load i32, i32* %3
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i8**, i8*** %2
store i8* %14, i8** %15
%16 = load i8**, i8*** %2
%17 = icmp eq i8** %16, null
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %11
store i32 0, i32* %4
br label %23
23: ; preds = %52, %22
%24 = load i8**, i8*** %2
%25 = load i8*, i8** %24
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32*, i32** @stdin
%30 = call i32 @fread(i8* %28, i32 1, i32 1, i32* %29)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %55
32: ; preds = %23
%33 = load i32, i32* %3
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = load i32, i32* %3
%39 = mul nsw i32 %38, 2
store i32 %39, i32* %3
%40 = load i8**, i8*** %2
%41 = load i8*, i8** %40
%42 = load i32, i32* %3
%43 = call i64 @realloc(i8* %41, i32 %42)
%44 = inttoptr i64 %43 to i8*
%45 = load i8**, i8*** %2
store i8* %44, i8** %45
%46 = load i8**, i8*** %2
%47 = load i8*, i8** %46
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %37
br label %52
52: ; preds = %51, %32
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %23
55: ; preds = %23
%56 = load i32*, i32** @stdin
%57 = call i32 @fclose(i32* %56)
ret void
}

LLVM IR (-O2):
@stdin = global i32* null
@EXIT_FAILURE = global i32 0
@str = [15 x i8] c"memory failure\00"
@str.3 = [16 x i8] c"unknown failure\00"
define void @readStdin(i8** nocapture %0) {
%2 = load i32*, i32** @stdin
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.3, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1
%9 = tail call i64 @malloc(i32 1)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
%11 = load i32*, i32** @stdin
%12 = tail call i32 @fread(i8* %10, i32 1, i32 1, i32* %11)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %35
14: ; preds = %8, %28
%15 = phi i64 [ %17, %28 ], [ 0, %8 ]
%16 = phi i32 [ %29, %28 ], [ 1, %8 ]
%17 = add nuw i64 %15, 1
%18 = zext i32 %16 to i64
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %28
20: ; preds = %14
%21 = shl nsw i32 %16, 1
%22 = load i8*, i8** %0
%23 = tail call i64 @realloc(i8* %22, i32 %21)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %0
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %20, %26, %14
%29 = phi i32 [ %21, %26 ], [ %21, %20 ], [ %16, %14 ]
%30 = load i8*, i8** %0
%31 = getelementptr inbounds i8, i8* %30, i64 %17
%32 = load i32*, i32** @stdin
%33 = tail call i32 @fread(i8* %31, i32 1, i32 1, i32* %32)
%34 = icmp eq i32 %33, 1
br i1 %34, label %14, label %35
35: ; preds = %28, %8
%36 = load i32*, i32** @stdin
%37 = tail call i32 @fclose(i32* %36)
ret void
}