#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int read_text_file(const char *filename, char **buf, int *filesize) {
  FILE *fp = fopen(filename, "rb");
  if (fp == NULL)
    return 1;
  fseek(fp, 0, SEEK_END);
  *filesize = ftell(fp);
  rewind(fp);
  char *p = (char *)malloc(*filesize + 1);
  size_t res = fread(p, 1, *filesize, fp);
  fclose(fp);
  p[*filesize] = '\0';
  *buf = p;
  return 0;
  return 0;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <read_text_file+0x3f>
movl   $0x1,-0x4(%rbp)
jmp    1235 <read_text_file+0xc5>
mov    -0x28(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f4 <read_text_file+0x84>
mov    %rax,%rbp
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbp,%rdi
call   1060 <ftell@plt>
mov    %eax,(%r12)
mov    %rbp,%rdi
call   1050 <rewind@plt>
mov    (%r12),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    (%r12),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
movslq (%r12),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r14)
jmp    11fa <read_text_file+0x8a>
mov    $0x1,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @read_text_file(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
store i32 %9, i32* %2
%10 = tail call i32 @rewind(i32* %4)
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i32, i32* %2
%16 = tail call i64 @fread(i8* %14, i32 1, i32 %15, i32* %4)
%17 = tail call i32 @fclose(i32* %4)
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %14, i64 %19
store i8 0, i8* %20
store i8* %14, i8** %1
br label %21
21: ; preds = %3, %6
%22 = phi i32 [ 0, %6 ], [ 1, %3 ]
ret i32 %22
}