#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*/ u_char;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int m_read_file(const char *filename, u_char **buffer, size_t *length) {
  FILE *fd;
  long len;
  fd = fopen(filename, "rb");
  if (fd == NULL) {
    return (-1);
  }
  fseek(fd, 0, SEEK_END);
  len = ftell(fd);
  fseek(fd, 0, SEEK_SET);
  if (len < 0 || ferror(fd)) {
    fclose(fd);
    return (-1);
  }
  if (length) {
    *length = (size_t)len;
  }
  if (buffer) {
    *buffer = (u_char *)malloc((size_t)len);
    if (*buffer == NULL || fread(*buffer, (size_t)len, 1, fd) != 1) {
      free(*buffer);
      *buffer = NULL;
      fclose(fd);
      return (-1);
    }
  }
  fclose(fd);
  return (0);
}


x86 assembly (clang, -O0):
<m_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <m_read_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <m_read_file+0x14c>
mov    -0x28(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmpq   $0x0,-0x30(%rbp)
jl     1212 <m_read_file+0x92>
mov    -0x28(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1227 <m_read_file+0xa7>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <m_read_file+0x14c>
cmpq   $0x0,-0x20(%rbp)
je     123d <m_read_file+0xbd>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     12bc <m_read_file+0x13c>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     128b <m_read_file+0x10b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     12b7 <m_read_file+0x137>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <m_read_file+0x14c>
jmp    12bc <m_read_file+0x13c>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1243 <m_read_file+0xc3>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    $0xffffffff,%ebp
test   %r12,%r12
js     1239 <m_read_file+0xb9>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1239 <m_read_file+0xb9>
test   %r15,%r15
je     11f6 <m_read_file+0x76>
mov    %r12,(%r15)
xor    %ebp,%ebp
test   %r14,%r14
je     1239 <m_read_file+0xb9>
mov    %r12,%rdi
call   1080 <malloc@plt>
mov    %rax,%rdi
mov    %rax,(%r14)
test   %rax,%rax
je     1228 <m_read_file+0xa8>
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1239 <m_read_file+0xb9>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    1248 <m_read_file+0xc8>
mov    $0xffffffff,%ebp
mov    %ebp,%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
@SEEK_SET = global i32 0
define i32 @m_read_file(i8* %0, i32** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i32** %1, i32*** %6
store i64* %2, i64** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %68
15: ; preds = %3
%16 = load i32*, i32** %8
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %8
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %9
%21 = load i32*, i32** %8
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %9
%25 = icmp slt i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %15
%27 = load i32*, i32** %8
%28 = call i64 @ferror(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26, %15
%31 = load i32*, i32** %8
%32 = call i32 @fclose(i32* %31)
store i32 -1, i32* %4
br label %68
33: ; preds = %26
%34 = load i64*, i64** %7
%35 = icmp ne i64* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i64, i64* %9
%38 = load i64*, i64** %7
store i64 %37, i64* %38
br label %39
39: ; preds = %36, %33
%40 = load i32**, i32*** %6
%41 = icmp ne i32** %40, null
br i1 %41, label %42, label %65
42: ; preds = %39
%43 = load i64, i64* %9
%44 = call i64 @malloc(i64 %43)
%45 = inttoptr i64 %44 to i32*
%46 = load i32**, i32*** %6
store i32* %45, i32** %46
%47 = load i32**, i32*** %6
%48 = load i32*, i32** %47
%49 = icmp eq i32* %48, null
br i1 %49, label %57, label %50
50: ; preds = %42
%51 = load i32**, i32*** %6
%52 = load i32*, i32** %51
%53 = load i64, i64* %9
%54 = load i32*, i32** %8
%55 = call i32 @fread(i32* %52, i64 %53, i32 1, i32* %54)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %64
57: ; preds = %50, %42
%58 = load i32**, i32*** %6
%59 = load i32*, i32** %58
%60 = call i32 @free(i32* %59)
%61 = load i32**, i32*** %6
store i32* null, i32** %61
%62 = load i32*, i32** %8
%63 = call i32 @fclose(i32* %62)
store i32 -1, i32* %4
br label %68
64: ; preds = %50
br label %65
65: ; preds = %64, %39
%66 = load i32*, i32** %8
%67 = call i32 @fclose(i32* %66)
store i32 0, i32* %4
br label %68
68: ; preds = %65, %57, %30, %14
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @m_read_file(i8* nocapture %0, i32** %1, i64* writeonly %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 %36, 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 i64 @ftell(i32* %4)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = icmp slt i64 %9, 0
br i1 %12, label %33, label %13
13: ; preds = %6
%14 = tail call i64 @ferror(i32* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = icmp eq i64* %2, null
br i1 %17, label %19, label %18
18: ; preds = %16
store i64 %9, i64* %2
br label %19
19: ; preds = %18, %16
%20 = icmp eq i32** %1, null
br i1 %20, label %33, label %21
21: ; preds = %19
%22 = tail call i64 @malloc(i64 %9)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %1
%24 = icmp eq i64 %22, 0
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = tail call i32 @fread(i32* %23, i64 %9, i32 1, i32* %4)
%27 = icmp eq i32 %26, 1
br i1 %27, label %33, label %28
28: ; preds = %25
%29 = load i32*, i32** %1
br label %30
30: ; preds = %28, %21
%31 = phi i32* [ %29, %28 ], [ %23, %21 ]
%32 = tail call i32 @free(i32* %31)
store i32* null, i32** %1
br label %33
33: ; preds = %19, %25, %6, %13, %30
%34 = phi i32 [ -1, %30 ], [ -1, %13 ], [ -1, %6 ], [ 0, %25 ], [ 0, %19 ]
%35 = tail call i32 @fclose(i32* %4)
br label %36
36: ; preds = %33, %3
%37 = phi i32 [ -1, %3 ], [ %34, %33 ]
ret i32 %37
}