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

/* Variables and functions */
char EOF;
int /*<<< orphan*/ exit(int);
char fgetc(int /*<<< orphan*/ *);
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *simple_read_string(FILE *f) {
  char c, *ptr;
  int i;
  char tmp[10240];
  i = 0;
  while (1) {
    c = fgetc(f);
    if (c == EOF) {
      printf("EOF While Reading String\n");
      exit(1);
    }
    if (i >= sizeof(tmp)) {
      printf("FATAL: Simple string buffer full\n");
      exit(1);
    }
    tmp[i] = c;
    if (c == '\0')
      break;
    i++;
  }
  ptr = malloc(strlen(tmp) + 1);
  strcpy(ptr, tmp);
  return ptr;
}


x86 assembly (clang, -O0):
<simple_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2820,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
mov    0x2e5b(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11ae <simple_read_string+0x4e>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x2800,%rax
jb     11d4 <simple_read_string+0x74>
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x9(%rbp),%cl
movslq -0x1c(%rbp),%rax
mov    %cl,-0x2820(%rbp,%rax,1)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    11f4 <simple_read_string+0x94>
jmp    1202 <simple_read_string+0xa2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <simple_read_string+0x16>
lea    -0x2820(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2820(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x2820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_read_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x2800,%rsp
mov    %rdi,%r15
call   1060 <fgetc@plt>
mov    0x2e6d(%rip),%r14
cmp    (%r14),%al
je     11b1 <simple_read_string+0x51>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2800,%rbx
je     11ec <simple_read_string+0x8c>
mov    %al,(%rsp,%rbx,1)
test   %al,%al
je     11ba <simple_read_string+0x5a>
add    $0x1,%rbx
mov    %r15,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%al
jne    1190 <simple_read_string+0x30>
lea    0xe69(%rip),%rdi
jmp    11f3 <simple_read_string+0x93>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x2800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EOF = global i8 0
@.str = [26 x i8] c"EOF While Reading String\0A\00"
@.str.1 = [34 x i8] c"FATAL: Simple string buffer full\0A\00"
define i8* @simple_read_string(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [10240 x i8]
store i32* %0, i32** %2
store i32 0, i32* %5
br label %7
7: ; preds = %1, %34
%8 = load i32*, i32** %2
%9 = call signext i8 @fgetc(i32* %8)
store i8 %9, i8* %3
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = load i8, i8* @EOF
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %11, %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %7
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = icmp uge i64 %20, 10240
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = load i8, i8* %3
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 %28
store i8 %26, i8* %29
%30 = load i8, i8* %3
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
br label %37
34: ; preds = %25
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %7
37: ; preds = %33
%38 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 0
%39 = call i64 @strlen(i8* %38)
%40 = add nsw i64 %39, 1
%41 = call i8* @malloc(i64 %40)
store i8* %41, i8** %4
%42 = load i8*, i8** %4
%43 = getelementptr inbounds [10240 x i8], [10240 x i8]* %6, i64 0, i64 0
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %4
ret i8* %45
}

LLVM IR (-O2):
@EOF = global i8 0
@str = [33 x i8] c"FATAL: Simple string buffer full\00"
@str.2 = [25 x i8] c"EOF While Reading String\00"
define i8* @simple_read_string(i32* nocapture %0) {
%2 = alloca [10240 x i8]
%3 = getelementptr inbounds [10240 x i8], [10240 x i8]* %2, i64 0, i64 0
%4 = tail call signext i8 @fgetc(i32* %0)
%5 = load i8, i8* @EOF
%6 = icmp eq i8 %4, %5
br i1 %6, label %12, label %15
7: ; preds = %22
%8 = add nuw nsw i64 %16, 1
%9 = tail call signext i8 @fgetc(i32* %0)
%10 = load i8, i8* @EOF
%11 = icmp eq i8 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %7, %1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1, %7
%16 = phi i64 [ %8, %7 ], [ 0, %1 ]
%17 = phi i8 [ %9, %7 ], [ %4, %1 ]
%18 = icmp eq i64 %16, 10240
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = getelementptr inbounds [10240 x i8], [10240 x i8]* %2, i64 0, i64 %16
store i8 %17, i8* %23
%24 = icmp eq i8 %17, 0
br i1 %24, label %25, label %7
25: ; preds = %22
%26 = call i64 @strlen(i8* %3)
%27 = add nsw i64 %26, 1
%28 = tail call i8* @malloc(i64 %27)
%29 = call i32 @strcpy(i8* %28, i8* %3)
ret i8* %28
}