#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*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, size_t);
char *file_read_str_ascii(FILE *fp) {
  int c;
  char *str, *buffer;
  size_t szStr;
  szStr = 256;
  str = malloc(sizeof(*str) * szStr);
  buffer = str;
  while ((c = fgetc(fp)) != EOF) {
    if (szStr == 10240) {
      printf("max string size\n");
      return NULL;
    }
    if (buffer - str <= szStr) {
      str = realloc(str, szStr * 2);
      buffer = str + szStr;
      szStr *= 2;
    }
    *buffer = c;
    buffer++;
  }
  return str;
}


x86 assembly (clang, -O0):
<file_read_str_ascii>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x100,-0x30(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%eax
je     120f <file_read_str_ascii+0xcf>
cmpq   $0x2800,-0x30(%rbp)
jne    11b1 <file_read_str_ascii+0x71>
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1217 <file_read_str_ascii+0xd7>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x30(%rbp),%rax
ja     11f3 <file_read_str_ascii+0xb3>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
shl    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <file_read_str_ascii+0x2f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_str_ascii>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    0x2e7e(%rip),%r13
cmp    0x0(%r13),%eax
jne    1175 <file_read_str_ascii+0x35>
mov    %rbx,%r15
jmp    11f1 <file_read_str_ascii+0xb1>
mov    %eax,%ebp
mov    $0x100,%r12d
mov    %rbx,%r15
mov    %r14,(%rsp)
jmp    11a7 <file_read_str_ascii+0x67>
cs nopw 0x0(%rax,%rax,1)
mov    %bpl,(%rbx)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
je     11f1 <file_read_str_ascii+0xb1>
cmp    $0x2800,%r12
je     11e2 <file_read_str_ascii+0xa2>
mov    %rbx,%rax
sub    %r15,%rax
cmp    %r12,%rax
ja     1190 <file_read_str_ascii+0x50>
mov    %r13,%r14
lea    (%r12,%r12,1),%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rbx
add    %r12,%rbx
mov    %r13,%r12
mov    %r14,%r13
mov    (%rsp),%r14
jmp    1190 <file_read_str_ascii+0x50>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [17 x i8] c"max string size\0A\00"
define i8* @file_read_str_ascii(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %3
store i64 256, i64* %7
%8 = load i64, i64* %7
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
store i8* %12, i8** %6
br label %13
13: ; preds = %41, %1
%14 = load i32*, i32** %3
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %4
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %47
18: ; preds = %13
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, 10240
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %49
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i8*, i8** %5
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = load i64, i64* %7
%30 = icmp ule i64 %28, %29
br i1 %30, label %31, label %41
31: ; preds = %23
%32 = load i8*, i8** %5
%33 = load i64, i64* %7
%34 = mul i64 %33, 2
%35 = call i8* @realloc(i8* %32, i64 %34)
store i8* %35, i8** %5
%36 = load i8*, i8** %5
%37 = load i64, i64* %7
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %6
%39 = load i64, i64* %7
%40 = mul i64 %39, 2
store i64 %40, i64* %7
br label %41
41: ; preds = %31, %23
%42 = load i32, i32* %4
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %6
store i8 %43, i8* %44
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %6
br label %13
47: ; preds = %13
%48 = load i8*, i8** %5
store i8* %48, i8** %2
br label %49
49: ; preds = %47, %21
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@EOF = global i32 0
@str = [16 x i8] c"max string size\00"
define i8* @file_read_str_ascii(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%3 = tail call i32 @fgetc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %32, label %6
6: ; preds = %1, %23
%7 = phi i32 [ %29, %23 ], [ %3, %1 ]
%8 = phi i64 [ %26, %23 ], [ 256, %1 ]
%9 = phi i8* [ %28, %23 ], [ %2, %1 ]
%10 = phi i8* [ %24, %23 ], [ %2, %1 ]
%11 = icmp eq i64 %8, 10240
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %32
14: ; preds = %6
%15 = ptrtoint i8* %9 to i64
%16 = ptrtoint i8* %10 to i64
%17 = sub i64 %15, %16
%18 = icmp ugt i64 %17, %8
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = shl i64 %8, 1
%21 = tail call i8* @realloc(i8* %10, i64 %20)
%22 = getelementptr inbounds i8, i8* %21, i64 %8
br label %23
23: ; preds = %19, %14
%24 = phi i8* [ %21, %19 ], [ %10, %14 ]
%25 = phi i8* [ %22, %19 ], [ %9, %14 ]
%26 = phi i64 [ %20, %19 ], [ %8, %14 ]
%27 = trunc i32 %7 to i8
store i8 %27, i8* %25
%28 = getelementptr inbounds i8, i8* %25, i64 1
%29 = tail call i32 @fgetc(i32* %0)
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %6
32: ; preds = %23, %1, %12
%33 = phi i8* [ null, %12 ], [ %2, %1 ], [ %24, %23 ]
ret i8* %33
}