#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 const *, char *);
int fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int read_file(const char *src_fname, char **file_in_data,
              unsigned *file_in_size) {
  FILE *fd = NULL;
  size_t size = 0;
  fd = fopen(src_fname, "rb");
  (*file_in_data) = NULL;
  if (!fd) {
    perror("safeu: "
           "unable to open input file");
    goto error;
  }
  fseek(fd, 0, SEEK_END);
  size = ftell(fd);
  if (size > (1 << 30)) {
    perror("safeu: "
           "input file is too large");
    goto error;
  }
  (*file_in_size) = size;
  (*file_in_data) = malloc(size);
  if ((*file_in_data) == NULL) {
    perror("safeu: "
           "unable to allocate memory for input file");
    goto error;
  }
  fseek(fd, 0, SEEK_SET);
  if (size != 0) {
    if (fread((*file_in_data), size, 1, fd) != 1) {
      perror("safeu: "
             "unable to read data from input file");
      goto error;
    }
  }
  fclose(fd);
  return 1;
error:
  if (fd) {
    fclose(fd);
  }
  free((*file_in_data));
  (*file_in_data) = NULL;
  (*file_in_size) = 0;
  return 0;
}


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,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    11df <read_file+0x5f>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c9 <read_file+0x149>
mov    -0x28(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x40000000,-0x30(%rbp)
40
jbe    121f <read_file+0x9f>
lea    0xe0f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c9 <read_file+0x149>
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    125d <read_file+0xdd>
lea    0xdf0(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c9 <read_file+0x149>
mov    -0x28(%rbp),%rdi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmpq   $0x0,-0x30(%rbp)
je     12b4 <read_file+0x134>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     12af <read_file+0x12f>
lea    0xdce(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c9 <read_file+0x149>
jmp    12b4 <read_file+0x134>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <read_file+0x185>
cmpq   $0x0,-0x28(%rbp)
je     12dd <read_file+0x15d>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
movq   $0x0,(%r15)
test   %rax,%rax
je     122f <read_file+0xaf>
mov    %rax,%r12
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1060 <ftell@plt>
cmp    $0x40000000,%rax
ja     123d <read_file+0xbd>
mov    %rax,%rbx
mov    %ebx,(%r14)
mov    %rax,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1246 <read_file+0xc6>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rbx,%rbx
je     1216 <read_file+0x96>
mov    (%r15),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    124f <read_file+0xcf>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdcd(%rip),%rdi
call   10a0 <perror@plt>
jmp    1263 <read_file+0xe3>
lea    0xde0(%rip),%rdi
jmp    1256 <read_file+0xd6>
lea    0xdf6(%rip),%rdi
jmp    1256 <read_file+0xd6>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
movl   $0x0,(%r14)
xor    %eax,%eax
jmp    1223 <read_file+0xa3>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [33 x i8] c"safeu: unable to open input file\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"safeu: input file is too large\00"
@.str.3 = [48 x i8] c"safeu: unable to allocate memory for input file\00"
@SEEK_SET = global i32 0
@.str.4 = [43 x i8] c"safeu: unable to read data from input file\00"
define i32 @read_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32* null, i32** %8
store i64 0, i64* %9
%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 i8**, i8*** %6
store i8* null, i8** %12
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %58
17: ; preds = %3
%18 = load i32*, i32** %8
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %8
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp ugt i64 %23, 1073741824
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %58
27: ; preds = %17
%28 = load i64, i64* %9
%29 = trunc i64 %28 to i32
%30 = load i32*, i32** %7
store i32 %29, i32* %30
%31 = load i64, i64* %9
%32 = call i8* @malloc(i64 %31)
%33 = load i8**, i8*** %6
store i8* %32, i8** %33
%34 = load i8**, i8*** %6
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %58
39: ; preds = %27
%40 = load i32*, i32** %8
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @fseek(i32* %40, i32 0, i32 %41)
%43 = load i64, i64* %9
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i64, i64* %9
%49 = load i32*, i32** %8
%50 = call i32 @fread(i8* %47, i64 %48, i32 1, i32* %49)
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %58
54: ; preds = %45
br label %55
55: ; preds = %54, %39
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
store i32 1, i32* %4
br label %70
58: ; preds = %52, %37, %25, %15
%59 = load i32*, i32** %8
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32*, i32** %8
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %61, %58
%65 = load i8**, i8*** %6
%66 = load i8*, i8** %65
%67 = call i32 @free(i8* %66)
%68 = load i8**, i8*** %6
store i8* null, i8** %68
%69 = load i32*, i32** %7
store i32 0, i32* %69
store i32 0, i32* %4
br label %70
70: ; preds = %64, %55
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [33 x i8] c"safeu: unable to open input file\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"safeu: input file is too large\00"
@.str.3 = [48 x i8] c"safeu: unable to allocate memory for input file\00"
@SEEK_SET = global i32 0
@.str.4 = [43 x i8] c"safeu: unable to read data from input file\00"
define i32 @read_file(i8* nocapture %0, i8** nocapture %1, i32* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %25, 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 = icmp ugt i64 %9, 1073741824
br i1 %10, label %27, label %11
11: ; preds = %6
%12 = trunc i64 %9 to i32
store i32 %12, i32* %2
%13 = tail call i8* @malloc(i64 %9)
store i8* %13, i8** %1
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %11
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %4, i32 0, i32 %16)
%18 = icmp eq i64 %9, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i8*, i8** %1
%21 = tail call i32 @fread(i8* %20, i64 %9, i32 1, i32* %4)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %19, %15
%24 = tail call i32 @fclose(i32* %4)
br label %34
25: ; preds = %3
%26 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %31
27: ; preds = %19, %11, %6
%28 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), %11 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), %19 ]
%29 = tail call i32 @perror(i8* %28)
%30 = tail call i32 @fclose(i32* %4)
br label %31
31: ; preds = %25, %27
%32 = load i8*, i8** %1
%33 = tail call i32 @free(i8* %32)
store i8* null, i8** %1
store i32 0, i32* %2
br label %34
34: ; preds = %31, %23
%35 = phi i32 [ 0, %31 ], [ 1, %23 ]
ret i32 %35
}