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

/* Variables and functions */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ realloc(char *, int);
char *read_from_file(const char *filename) {
  char ch, *s;
  FILE *fd;
  int c = 0;
  s = (char *)(malloc)(sizeof(char));
  fd = fopen(filename, "r");
  while ((ch = fgetc(fd)) != EOF) {
    if (ch == '\n')
      s[c] = ' ';
    else
      s[c] = ch;
    c++;
    s = (char *)realloc(s, (c + 1) * sizeof(char));
  }
  s[c] = '\0';
  fclose(fd);
  return s;
}


x86 assembly (clang, -O0):
<read_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x1,%eax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
mov    0x2e62(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11e6 <read_from_file+0xa6>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jne    11af <read_from_file+0x6f>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    11bd <read_from_file+0x7d>
mov    -0x9(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    1170 <read_from_file+0x30>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e82(%rip),%r15
cmp    (%r15),%al
jne    1174 <read_from_file+0x34>
mov    $0x1,%ebx
xor    %eax,%eax
jmp    11c1 <read_from_file+0x81>
mov    $0x1,%ebx
xor    %r12d,%r12d
mov    $0x20,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%al
movzbl %al,%eax
cmove  %ebp,%eax
mov    %r12,%rsi
mov    %al,(%rbx,%r12,1)
add    $0x1,%r12
add    $0x2,%esi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%al
jne    1190 <read_from_file+0x50>
mov    %r12d,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i8* @read_from_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = tail call signext i8 @fgetc(i32* %2)
%4 = load i8, i8* @EOF
%5 = icmp eq i8 %3, %4
br i1 %5, label %24, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %13, %6 ], [ 0, %1 ]
%8 = phi i8 [ %18, %6 ], [ %3, %1 ]
%9 = phi i8* [ %17, %6 ], [ inttoptr (i64 1 to i8*), %1 ]
%10 = icmp eq i8 %8, 10
%11 = select i1 %10, i8 32, i8 %8
%12 = getelementptr inbounds i8, i8* %9, i64 %7
store i8 %11, i8* %12
%13 = add nuw i64 %7, 1
%14 = trunc i64 %7 to i32
%15 = add i32 %14, 2
%16 = tail call i64 @realloc(i8* %9, i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = tail call signext i8 @fgetc(i32* %2)
%19 = load i8, i8* @EOF
%20 = icmp eq i8 %18, %19
br i1 %20, label %21, label %6
21: ; preds = %6
%22 = inttoptr i64 %16 to i8*
%23 = and i64 %13, 4294967295
br label %24
24: ; preds = %21, %1
%25 = phi i8* [ %22, %21 ], [ inttoptr (i64 1 to i8*), %1 ]
%26 = phi i64 [ %23, %21 ], [ 0, %1 ]
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @fclose(i32* %2)
ret i8* %25
}