#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int read_file(char *filename, unsigned char **textt, unsigned long *length) {
  unsigned char *text;
  unsigned long t;
  FILE *infile;
  infile = fopen(filename, "rb");
  if (infile == NULL)
    return 1;
  if (fseek(infile, 0, SEEK_END) != 0)
    return 1;
  *length = ftell(infile);
  text = (unsigned char *)malloc((*length) * sizeof(*text));
  if (text == NULL)
    return 1;
  rewind(infile);
  t = fread(text, sizeof(*text), (size_t)*length, infile);
  if (t != *length)
    return 1;
  *textt = text;
  fclose(infile);
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11af <read_file+0x3f>
movl   $0x1,-0x4(%rbp)
jmp    1275 <read_file+0x105>
mov    -0x38(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11d9 <read_file+0x69>
movl   $0x1,-0x4(%rbp)
jmp    1275 <read_file+0x105>
mov    -0x38(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1217 <read_file+0xa7>
movl   $0x1,-0x4(%rbp)
jmp    1275 <read_file+0x105>
mov    -0x38(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
je     125a <read_file+0xea>
movl   $0x1,-0x4(%rbp)
jmp    1275 <read_file+0x105>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11af <read_file+0x3f>
mov    %rax,%rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11ba <read_file+0x4a>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11af <read_file+0x3f>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    (%r15),%rdx
mov    $0x1,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    (%r15),%rax
jne    11af <read_file+0x3f>
mov    %r12,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    11af <read_file+0x3f>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @read_file(i8* nocapture %0, i8** nocapture writeonly %1, i64* nocapture %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 %23, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %4, i32 0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %6
%11 = tail call i64 @ftell(i32* %4)
store i64 %11, i64* %2
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = tail call i32 @rewind(i32* %4)
%17 = load i64, i64* %2
%18 = tail call i64 @fread(i8* %13, i32 1, i64 %17, i32* %4)
%19 = load i64, i64* %2
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %15
store i8* %13, i8** %1
%22 = tail call i32 @fclose(i32* %4)
br label %23
23: ; preds = %15, %10, %6, %3, %21
%24 = phi i32 [ 0, %21 ], [ 1, %3 ], [ 1, %6 ], [ 1, %10 ], [ 1, %15 ]
ret i32 %24
}