#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ printf(char *, char *);
int read_file(char *fname, char *mode, unsigned char **buf) {
  FILE *f;
  long fsize = 0;
  f = fopen(fname, mode);
  if (!f) {
    printf("\nCan't read file %s\n", fname);
    return -2;
  }
  fseek(f, 0, SEEK_END);
  fsize = ftell(f);
  fseek(f, 0, SEEK_SET);
  *buf = (unsigned char *)malloc(fsize + 1);
  if (!(*buf)) {
    fclose(f);
    return -1;
  }
  fread(*buf, 1, fsize * sizeof(unsigned char), f);
  (*buf)[fsize] = 0;
  fclose(f);
  return fsize;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <read_file+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe4d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126f <read_file+0xff>
mov    -0x28(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1233 <read_file+0xc3>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <read_file+0xff>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rdx
shl    $0x0,%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%r15
call   1090 <fopen@plt>
test   %rax,%rax
je     11ef <read_file+0x7f>
mov    %rax,%rbx
mov    0x2e59(%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    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r15),%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1206 <read_file+0x96>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%r15,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1214 <read_file+0xa4>
lea    0xe0a(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    $0xfffffffe,%r15d
jmp    1214 <read_file+0xa4>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [21 x i8] c"\0ACan't read file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_file(i8* %0, i8* nocapture %1, i8** nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %26
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %4)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %4, i32 0, i32 %12)
%14 = add nsw i64 %11, 1
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %2
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = tail call i32 @fclose(i32* %4)
br label %26
20: ; preds = %8
%21 = tail call i32 @fread(i8* %16, i32 1, i64 %11, i32* %4)
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i64 %11
store i8 0, i8* %23
%24 = tail call i32 @fclose(i32* %4)
%25 = trunc i64 %11 to i32
br label %26
26: ; preds = %20, %18, %6
%27 = phi i32 [ %25, %20 ], [ -1, %18 ], [ -2, %6 ]
ret i32 %27
}