#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*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
char *ycf_malloc(long);
char *file_to_str(const char *filename) {
  char *buffer;
  long length;
  FILE *f = fopen(filename, "rb");
  if (f) {
    fseek(f, 0, SEEK_END);
    length = ftell(f);
    fseek(f, 0, SEEK_SET);
    buffer = ycf_malloc(length + 1);
    size_t nr_of_read_bytes = fread(buffer, 1, length, f);
    if (nr_of_read_bytes != length) {
      printf("error: while reading file %s\n", filename);
      exit(1);
    }
    fclose(f);
    buffer[length] = 0;
  } else {
    printf("error: could not open file %s\n", filename);
    exit(1);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<file_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     124d <file_to_str+0xcd>
mov    -0x20(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1080 <ycf_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1233 <file_to_str+0xb3>
mov    -0x8(%rbp),%rsi
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1267 <file_to_str+0xe7>
mov    -0x8(%rbp),%rsi
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1210 <file_to_str+0x90>
mov    %rax,%rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r15),%rdi
call   1080 <ycf_malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    1219 <file_to_str+0x99>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r12,%r15,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0a(%rip),%rdi
jmp    1220 <file_to_str+0xa0>
lea    0xde3(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [30 x i8] c"error: while reading file %s\0A\00"
@.str.2 = [31 x i8] c"error: could not open file %s\0A\00"
define i8* @file_to_str(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = tail call i64 @ftell(i32* %2)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = add nsw i64 %7, 1
%11 = tail call i8* @ycf_malloc(i64 %10)
%12 = tail call i64 @fread(i8* %11, i32 1, i64 %7, i32* %2)
%13 = icmp eq i64 %12, %7
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %4
%18 = tail call i32 @fclose(i32* %2)
%19 = getelementptr inbounds i8, i8* %11, i64 %7
store i8 0, i8* %19
ret i8* %11
20: ; preds = %1
%21 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%22 = tail call i32 @exit(i32 1)
unreachable
}