#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int read_disk(unsigned char *dst, size_t size, char *path, size_t offset) {
  FILE *file;
  if (!(file = fopen(path, "rb"))) {
    perror("fopen");
    return 0;
  }
  fseek(file, offset, SEEK_SET);
  if (fread(dst, sizeof(char), size, file) < size) {
    if (feof(file)) {
      fprintf(stderr, "EOF ERROR!\n");
    }
    if (ferror(file)) {
      fprintf(stderr, "ferror(file)\n");
    }
    fprintf(stderr, "fread error!\n");
    return 0;
  }
  fclose(file);
  return 1;
}


x86 assembly (clang, -O0):
<read_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ce <read_disk+0x4e>
lea    0xe46(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1285 <read_disk+0x105>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%rax
jae    1275 <read_disk+0xf5>
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     122c <read_disk+0xac>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1254 <read_disk+0xd4>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1285 <read_disk+0x105>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_disk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe69(%rip),%rsi
mov    %rdx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     124e <read_disk+0xce>
mov    %rax,%rbx
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <fseek@plt>
mov    $0x1,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r14,%rax
jae    1239 <read_disk+0xb9>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11fe <read_disk+0x7e>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1220 <read_disk+0xa0>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebp,%ebp
jmp    1241 <read_disk+0xc1>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdae(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebp,%ebp
jmp    1241 <read_disk+0xc1>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.2 = [12 x i8] c"EOF ERROR!\0A\00"
@.str.3 = [14 x i8] c"ferror(file)\0A\00"
@.str.4 = [14 x i8] c"fread error!\0A\00"
define i32 @read_disk(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i8*, i8** %8
%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 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %47
16: ; preds = %4
%17 = load i32*, i32** %10
%18 = load i64, i64* %9
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %17, i64 %18, i32 %19)
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = load i32*, i32** %10
%24 = call i64 @fread(i8* %21, i32 1, i64 %22, i32* %23)
%25 = load i64, i64* %7
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %44
27: ; preds = %16
%28 = load i32*, i32** %10
%29 = call i64 @feof(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31, %27
%35 = load i32*, i32** %10
%36 = call i64 @ferror(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %38, %34
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %47
44: ; preds = %16
%45 = load i32*, i32** %10
%46 = call i32 @fclose(i32* %45)
store i32 1, i32* %5
br label %47
47: ; preds = %44, %41, %14
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.2 = [12 x i8] c"EOF ERROR!\0A\00"
@.str.3 = [14 x i8] c"ferror(file)\0A\00"
@.str.4 = [14 x i8] c"fread error!\0A\00"
define i32 @read_disk(i8* nocapture %0, i64 %1, i8* nocapture %2, i64 %3) {
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %31
9: ; preds = %4
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %5, i64 %3, i32 %10)
%12 = tail call i64 @fread(i8* %0, i32 1, i64 %1, i32* %5)
%13 = icmp ult i64 %12, %1
br i1 %13, label %14, label %29
14: ; preds = %9
%15 = tail call i64 @feof(i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
%21 = tail call i64 @ferror(i32* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %31
29: ; preds = %9
%30 = tail call i32 @fclose(i32* %5)
br label %31
31: ; preds = %29, %26, %7
%32 = phi i32 [ 0, %26 ], [ 1, %29 ], [ 0, %7 ]
ret i32 %32
}