#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 /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
char *M8_read_file(char *filename) {
  int ch;
  FILE *fp;
  char *s = malloc(2 * 256 * 2 * sizeof(char));
  int index = 0;
  fp = fopen(filename, "r");
  assert(fp != NULL);
  while ((ch = fgetc(fp)) != EOF) {
    s[index++] = (char)ch;
  }
  s[index] = '\0';
  fclose(fp);
  return s;
}


x86 assembly (clang, -O0):
<M8_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%eax
je     11d1 <M8_read_file+0x81>
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1197 <M8_read_file+0x47>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<M8_read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe91(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r12
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    0x2e51(%rip),%r15
cmp    (%r15),%eax
jne    11a0 <M8_read_file+0x50>
xor    %eax,%eax
jmp    11c7 <M8_read_file+0x77>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
jne    11b0 <M8_read_file+0x60>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @M8_read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i8* @malloc(i32 1024)
store i8* %7, i8** %5
store i32 0, i32* %6
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
br label %14
14: ; preds = %19, %1
%15 = load i32*, i32** %4
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %3
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i32, i32* %3
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
br label %14
27: ; preds = %14
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
%34 = load i8*, i8** %5
ret i8* %34
}

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