#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 */
struct stat {
  int st_size;
};
typedef int __u32;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int __cpu_to_be32(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void read_data(char *filename, unsigned char *memspace, __u32 len) {
  FILE *image;
  struct stat imgstat;
  int size;
  __u32 buf = 0, *mem_32 = (__u32 *)memspace;
  int i;
  if (!filename) {
    fprintf(stderr, "read_image: NULL filename\n");
    return;
  }
  if (!(image = fopen(filename, "rb"))) {
    fprintf(stderr, "Unable to open %s for reading: %s\n", filename,
            strerror(errno));
    return;
  }
  if (fstat(fileno(image), &imgstat)) {
    fprintf(stderr, "Unable to stat file %s: %s\n", filename, strerror(errno));
    fclose(image);
    return;
  }
  size = imgstat.st_size;
  if (len < size)
    size = len;
  for (i = 0; i < size; i += 4) {
    fread(&buf, 1, 4, image);
    mem_32[i / 4] = __cpu_to_be32(buf);
  }
  fclose(image);
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c9 <read_data+0x49>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1312 <read_data+0x192>
mov    -0x8(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1226 <read_data+0xa6>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1312 <read_data+0x192>
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
je     128c <read_data+0x10c>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1312 <read_data+0x192>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12a4 <read_data+0x124>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1309 <read_data+0x189>
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%edi
call   1030 <__cpu_to_be32@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    -0x6c(%rbp),%edx
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x3c(%rbp)
jmp    12ab <read_data+0x12b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
je     1207 <read_data+0x87>
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     122c <read_data+0xac>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <fileno@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
je     1267 <read_data+0xe7>
mov    0x2e04(%rip),%rax
mov    (%rax),%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe4e(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12aa <read_data+0x12a>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd4(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
mov    0x10(%rsp),%ebp
cmp    %r12d,%ebp
cmovg  %r12d,%ebp
test   %ebp,%ebp
jle    12aa <read_data+0x12a>
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
nopl   (%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    0xc(%rsp),%edi
call   1030 <__cpu_to_be32@plt>
mov    %eax,(%r14,%rbx,1)
add    $0x4,%rbx
cmp    %ebx,%ebp
jg     1280 <read_data+0x100>
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"read_image: NULL filename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [35 x i8] c"Unable to open %s for reading: %s\0A\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"Unable to stat file %s: %s\0A\00"
define void @read_data(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %10
%13 = load i8*, i8** %5
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %11
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %72
20: ; preds = %3
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %28)
br label %72
30: ; preds = %20
%31 = load i32*, i32** %7
%32 = call i32 @fileno(i32* %31)
%33 = call i64 @fstat(i32 %32, %struct.stat* %8)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %4
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
br label %72
43: ; preds = %30
%44 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %9
%46 = load i32, i32* %6
%47 = load i32, i32* %9
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = load i32, i32* %6
store i32 %50, i32* %9
br label %51
51: ; preds = %49, %43
store i32 0, i32* %12
br label %52
52: ; preds = %66, %51
%53 = load i32, i32* %12
%54 = load i32, i32* %9
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %69
56: ; preds = %52
%57 = load i32*, i32** %7
%58 = call i32 @fread(i32* %10, i32 1, i32 4, i32* %57)
%59 = load i32, i32* %10
%60 = call i32 @__cpu_to_be32(i32 %59)
%61 = load i32*, i32** %11
%62 = load i32, i32* %12
%63 = sdiv i32 %62, 4
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
store i32 %60, i32* %65
br label %66
66: ; preds = %56
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 4
store i32 %68, i32* %12
br label %52
69: ; preds = %52
%70 = load i32*, i32** %7
%71 = call i32 @fclose(i32* %70)
br label %72
72: ; preds = %69, %35, %24, %17
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"read_image: NULL filename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [35 x i8] c"Unable to open %s for reading: %s\0A\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"Unable to stat file %s: %s\0A\00"
define void @read_data(i8* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca i32
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = bitcast i8* %1 to i32*
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %49
13: ; preds = %3
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %19)
br label %49
21: ; preds = %13
%22 = tail call i32 @fileno(i32* %14)
%23 = call i64 @fstat(i32 %22, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %28)
%30 = tail call i32 @fclose(i32* %14)
br label %49
31: ; preds = %21
%32 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%33 = load i32, i32* %32
%34 = icmp sgt i32 %33, %2
%35 = select i1 %34, i32 %2, i32 %33
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %31, %37
%38 = phi i64 [ %44, %37 ], [ 0, %31 ]
%39 = call i32 @fread(i32* %5, i32 1, i32 4, i32* %14)
%40 = load i32, i32* %5
%41 = tail call i32 @__cpu_to_be32(i32 %40)
%42 = lshr exact i64 %38, 2
%43 = getelementptr inbounds i32, i32* %8, i64 %42
store i32 %41, i32* %43
%44 = add nuw i64 %38, 4
%45 = trunc i64 %44 to i32
%46 = icmp sgt i32 %35, %45
br i1 %46, label %37, label %47
47: ; preds = %37, %31
%48 = tail call i32 @fclose(i32* %14)
br label %49
49: ; preds = %47, %25, %16, %10
ret void
}