#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int imd_read(char *filename, unsigned char **diskbuff) {
  FILE *in;
  int filesize, rc;
  unsigned char *buffer;
  in = fopen(filename, "r");
  if (!in) {
    perror("Couldn't open image file");
    exit(1);
  }
  fseek(in, 0, SEEK_END);
  filesize = ftell(in);
  rewind(in);
  buffer = malloc(filesize);
  if (!buffer) {
    perror("failed to allocate disk buffer");
    exit(1);
  }
  rc = fread(buffer, 1, filesize, in);
  if (!rc) {
    perror("Failed to read");
    exit(1);
  }
  fclose(in);
  *diskbuff = buffer;
  return filesize;
}


x86 assembly (clang, -O0):
<imd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <imd_read+0x45>
lea    0xe3a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x1c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <imd_read+0x9b>
lea    0xdfd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1263 <imd_read+0xd3>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imd_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe95(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1214 <imd_read+0x84>
mov    %rax,%rbx
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r15d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121d <imd_read+0x8d>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1226 <imd_read+0x96>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde5(%rip),%rdi
jmp    122d <imd_read+0x9d>
lea    0xdf5(%rip),%rdi
jmp    122d <imd_read+0x9d>
lea    0xe0b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"Couldn't open image file\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"failed to allocate disk buffer\00"
@.str.3 = [15 x i8] c"Failed to read\00"
define i32 @imd_read(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32*, i32** %5
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %5
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %6
%22 = load i32*, i32** %5
%23 = call i32 @rewind(i32* %22)
%24 = load i32, i32* %6
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
%32 = load i8*, i8** %8
%33 = load i32, i32* %6
%34 = load i32*, i32** %5
%35 = call i32 @fread(i8* %32, i32 1, i32 %33, i32* %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = load i32*, i32** %5
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %8
%45 = load i8**, i8*** %4
store i8* %44, i8** %45
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"Couldn't open image file\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"failed to allocate disk buffer\00"
@.str.3 = [15 x i8] c"Failed to read\00"
define i32 @imd_read(i8* nocapture %0, i8** nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %3)
%12 = tail call i32 @rewind(i32* %3)
%13 = tail call i8* @malloc(i32 %11)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = tail call i32 @fread(i8* %13, i32 1, i32 %11, i32* %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = tail call i32 @fclose(i32* %3)
store i8* %13, i8** %1
ret i32 %11
}