#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ stderr;
size_t read_file(char *filename, char **buf) {
  FILE *fh;
  size_t file_size = 0;
  if ((fh = fopen(filename, "r")) != NULL) {
    if (fseek(fh, 0, SEEK_END) != 0) {
      fprintf(stderr, "Seek error on %s\n", filename);
      exit(EXIT_FAILURE);
    }
    file_size = (size_t)ftell(fh);
    fseek(fh, 0, SEEK_SET);
    char *b;
    b = malloc(file_size);
    fread(b, 1, file_size, fh);
    fclose(fh);
    *buf = b;
  } else {
    fprintf(stderr, "Error reading %s\n", filename);
    exit(EXIT_FAILURE);
  }
  return file_size;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1258 <read_file+0xd8>
mov    -0x18(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11fb <read_file+0x7b>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    127f <read_file+0xff>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xda8(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1210 <read_file+0x90>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    1222 <read_file+0xa2>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
jmp    1232 <read_file+0xb2>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"Seek error on %s\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"Error reading %s\0A\00"
define i64 @read_file(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @fseek(i32* %3, i32 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = tail call i64 @ftell(i32* %3)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @fseek(i32* %3, i32 0, i32 %16)
%18 = tail call i8* @malloc(i64 %15)
%19 = tail call i32 @fread(i8* %18, i32 1, i64 %15, i32* %3)
%20 = tail call i32 @fclose(i32* %3)
store i8* %18, i8** %1
ret i64 %15
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
}