#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *read_file(const char *f_name, size_t *sz) {
  FILE *fp = fopen(f_name, "rb");
  if (!fp) {
    fprintf(stderr, "read_file error: failed to open file: %s\n", f_name);
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  size_t size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  if (sz) {
    *sz = size;
  }
  char *content = malloc(sizeof(char) * size + sizeof(char));
  if (!content) {
    fprintf(stderr, "read_file error: buffer allocation failed\n");
    return NULL;
  }
  if (fread(content, sizeof(char), size, fp) != size) {
    fprintf(stderr, "read_file error: short read\n");
    free(content);
    return NULL;
  }
  content[size] = '\0';
  return content;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <read_file+0x57>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <read_file+0x149>
mov    -0x20(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmpq   $0x0,-0x18(%rbp)
je     1212 <read_file+0xa2>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1258 <read_file+0xe8>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <read_file+0x149>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x28(%rbp),%rax
je     12a5 <read_file+0x135>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <read_file+0x149>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f8 <read_file+0x88>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %r14,%r14
je     11c8 <read_file+0x58>
mov    %r15,(%r14)
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1217 <read_file+0xa7>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    1233 <read_file+0xc3>
movb   $0x0,(%r14,%r15,1)
jmp    1255 <read_file+0xe5>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
xor    %r14d,%r14d
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1255 <read_file+0xe5>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1255 <read_file+0xe5>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"read_file error: failed to open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [43 x i8] c"read_file error: buffer allocation failed\0A\00"
@.str.3 = [29 x i8] c"read_file error: short read\0A\00"
define i8* @read_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i8* null, i8** %3
br label %59
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %6
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %7
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i64*, i64** %5
%27 = icmp ne i64* %26, null
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load i64, i64* %7
%30 = load i64*, i64** %5
store i64 %29, i64* %30
br label %31
31: ; preds = %28, %17
%32 = load i64, i64* %7
%33 = mul i64 1, %32
%34 = add i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %59
42: ; preds = %31
%43 = load i8*, i8** %8
%44 = load i64, i64* %7
%45 = load i32*, i32** %6
%46 = call i64 @fread(i8* %43, i32 1, i64 %44, i32* %45)
%47 = load i64, i64* %7
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%52 = load i8*, i8** %8
%53 = call i32 @free(i8* %52)
store i8* null, i8** %3
br label %59
54: ; preds = %42
%55 = load i8*, i8** %8
%56 = load i64, i64* %7
%57 = getelementptr inbounds i8, i8* %55, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %8
store i8* %58, i8** %3
br label %59
59: ; preds = %54, %49, %39, %13
%60 = load i8*, i8** %3
ret i8* %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"read_file error: failed to open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [43 x i8] c"read_file error: buffer allocation failed\0A\00"
@.str.3 = [29 x i8] c"read_file error: short read\0A\00"
define noalias i8* @read_file(i8* %0, i64* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %3)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %3, i32 0, i32 %12)
%14 = icmp eq i64* %1, null
br i1 %14, label %16, label %15
15: ; preds = %8
store i64 %11, i64* %1
br label %16
16: ; preds = %15, %8
%17 = trunc i64 %11 to i32
%18 = add i32 %17, 1
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %33
24: ; preds = %16
%25 = tail call i64 @fread(i8* %19, i32 1, i64 %11, i32* %3)
%26 = icmp eq i64 %25, %11
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @free(i8* %19)
br label %33
31: ; preds = %24
%32 = getelementptr inbounds i8, i8* %19, i64 %11
store i8 0, i8* %32
br label %33
33: ; preds = %21, %27, %31, %5
%34 = phi i8* [ null, %5 ], [ null, %27 ], [ %19, %31 ], [ null, %21 ]
ret i8* %34
}