#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 */
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 *read_string(FILE *f) {
  char c, *ptr;
  int i;
  char tmp[256];
  i = 0;
  while (1) {
    c = fgetc(f);
    if (c == EOF) {
      printf("EOF While Reading String\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):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%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 <read_string+0x4e>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x9(%rbp),%cl
movslq -0x1c(%rbp),%rax
mov    %cl,-0x120(%rbp,%rax,1)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    11ce <read_string+0x6e>
jmp    11dc <read_string+0x7c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <read_string+0x16>
lea    -0x120(%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    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r15
call   1060 <fgetc@plt>
mov    0x2e6d(%rip),%r14
cmp    (%r14),%al
je     11a7 <read_string+0x47>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %al,(%rbx)
test   %al,%al
je     11bd <read_string+0x5d>
mov    %r15,%rdi
call   1060 <fgetc@plt>
add    $0x1,%rbx
cmp    (%r14),%al
jne    1190 <read_string+0x30>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
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    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i8 0
@.str = [26 x i8] c"EOF While Reading String\0A\00"
define i8* @read_string(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [256 x i8]
store i32* %0, i32** %2
store i32 0, i32* %5
br label %7
7: ; preds = %1, %27
%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 i8, i8* %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %21
store i8 %19, i8* %22
%23 = load i8, i8* %3
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
br label %30
27: ; preds = %18
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %7
30: ; preds = %26
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %32, 1
%34 = call i8* @malloc(i64 %33)
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%37 = call i32 @strcpy(i8* %35, i8* %36)
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
@EOF = global i8 0
@str = [25 x i8] c"EOF While Reading String\00"
define i8* @read_string(i32* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 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 = %15
%8 = add nuw 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, 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 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %16
store i8 %17, i8* %18
%19 = icmp eq i8 %17, 0
br i1 %19, label %20, label %7
20: ; preds = %15
%21 = call i64 @strlen(i8* %3)
%22 = add nsw i64 %21, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = call i32 @strcpy(i8* %23, i8* %3)
ret i8* %23
}