#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(unsigned int *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int read_file(char *filename, unsigned int **sourceInts,
              unsigned long *length) {
  unsigned int *source;
  size_t t;
  FILE *infile;
  infile = fopen(filename, "rb");
  if (infile == NULL)
    return 30;
  if (fseek(infile, 0, SEEK_END) != 0)
    return 1;
  *length = ftell(infile) / (sizeof(unsigned int));
  source = (unsigned int *)malloc((*length) * sizeof(unsigned int));
  if (source == NULL)
    return 1;
  rewind(infile);
  t = fread(source, sizeof(unsigned int), (size_t)(*length), infile);
  if (t != *length) {
    fprintf(stderr, "\n fread failed\n");
    return 1;
  }
  *sourceInts = source;
  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    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11bf <read_file+0x3f>
movl   $0x1e,-0x4(%rbp)
jmp    129e <read_file+0x11e>
mov    -0x38(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     11e9 <read_file+0x69>
movl   $0x1,-0x4(%rbp)
jmp    129e <read_file+0x11e>
mov    -0x38(%rbp),%rdi
call   1070 <ftell@plt>
movslq %eax,%rcx
shr    $0x2,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
shl    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <read_file+0xab>
movl   $0x1,-0x4(%rbp)
jmp    129e <read_file+0x11e>
mov    -0x38(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
je     1283 <read_file+0x103>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <read_file+0x11e>
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    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1214 <read_file+0x94>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1219 <read_file+0x99>
mov    %rbx,%rdi
call   1070 <ftell@plt>
movslq %eax,%rdi
mov    %rdi,%rax
shr    $0x2,%rax
mov    %rax,(%r15)
and    $0xfffffffffffffffc,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1219 <read_file+0x99>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    (%r15),%rdx
mov    %r12,%rdi
mov    $0x4,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    (%r15),%rax
jne    1224 <read_file+0xa4>
mov    %r12,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1219 <read_file+0x99>
mov    $0x1e,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1219 <read_file+0x99>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"\0A fread failed\0A\00"
define i32 @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
%10 = alloca i32*
store i8* %0, i8** %5
store i32** %1, i32*** %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 30, i32* %4
br label %56
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 %56
22: ; preds = %16
%23 = load i32*, i32** %10
%24 = call i32 @ftell(i32* %23)
%25 = sext i32 %24 to i64
%26 = udiv i64 %25, 4
%27 = load i64*, i64** %7
store i64 %26, i64* %27
%28 = load i64*, i64** %7
%29 = load i64, i64* %28
%30 = mul i64 %29, 4
%31 = call i64 @malloc(i64 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %22
store i32 1, i32* %4
br label %56
36: ; preds = %22
%37 = load i32*, i32** %10
%38 = call i32 @rewind(i32* %37)
%39 = load i32*, i32** %8
%40 = load i64*, i64** %7
%41 = load i64, i64* %40
%42 = load i32*, i32** %10
%43 = call i64 @fread(i32* %39, i32 4, i64 %41, i32* %42)
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = load i64*, i64** %7
%46 = load i64, i64* %45
%47 = icmp ne i64 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %36
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %56
51: ; preds = %36
%52 = load i32*, i32** %8
%53 = load i32**, i32*** %6
store i32* %52, i32** %53
%54 = load i32*, i32** %10
%55 = call i32 @fclose(i32* %54)
store i32 0, i32* %4
br label %56
56: ; preds = %51, %48, %35, %21, %15
%57 = load i32, i32* %4
ret i32 %57
}

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