#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*/ LOG_ERR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ output(int /*<<< orphan*/, char *, ...);
char const *strerror(int /*<<< orphan*/);
void read_file(const char *path, unsigned char **pval, int *plen) {
  FILE *file;
  file = fopen(path, "rb");
  if (!file) {
    output(LOG_ERR, "error: fopen failed. %s", path);
    exit(1);
  }
  if (fseek(file, 0L, SEEK_END) == -1) {
    output(LOG_ERR, "error: fseek failed. %s", strerror(errno));
    exit(1);
  }
  *plen = ftell(file);
  *pval = malloc(*plen);
  if (!*pval) {
    output(LOG_ERR, "error: memory allocation failed");
    exit(1);
  }
  if (fseek(file, 0L, SEEK_SET) == -1) {
    output(LOG_ERR, "error: fseek failed. %s", strerror(errno));
    exit(1);
  }
  if (fread(*pval, *plen, 1, file) != 1) {
    output(LOG_ERR, "error: fread failed. %s", strerror(errno));
    exit(1);
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <read_file+0x58>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0xffffffff,%eax
jne    123f <read_file+0xaf>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1294 <read_file+0x104>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0xffffffff,%eax
jne    12eb <read_file+0x15b>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1347 <read_file+0x1b7>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     124e <read_file+0xbe>
mov    %rax,%rbx
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0xffffffff,%eax
je     122e <read_file+0x9e>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,(%r14)
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1263 <read_file+0xd3>
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0xffffffff,%eax
je     122e <read_file+0x9e>
mov    (%r15),%rdi
mov    (%r14),%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1284 <read_file+0xf4>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdcf(%rip),%rsi
jmp    12a2 <read_file+0x112>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    %r12,%rdx
jmp    12a7 <read_file+0x117>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
xor    %eax,%eax
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%ebx
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <output@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@LOG_ERR = global i32 0
@.str.1 = [24 x i8] c"error: fopen failed. %s\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"error: fseek failed. %s\00"
@errno = global i32 0
@.str.3 = [32 x i8] c"error: memory allocation failed\00"
@SEEK_SET = global i32 0
@.str.4 = [24 x i8] c"error: fread failed. %s\00"
define void @read_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = load i32, i32* @LOG_ERR
%14 = load i8*, i8** %4
%15 = call i32 (i32, i8*, ...) @output(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @LOG_ERR
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @output(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i32*, i32** %7
%30 = call i32 @ftell(i32* %29)
%31 = load i32*, i32** %6
store i32 %30, i32* %31
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = call i8* @malloc(i32 %33)
%35 = load i8**, i8*** %5
store i8* %34, i8** %35
%36 = load i8**, i8*** %5
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %43, label %39
39: ; preds = %28
%40 = load i32, i32* @LOG_ERR
%41 = call i32 (i32, i8*, ...) @output(i32 %40, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %28
%44 = load i32*, i32** %7
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %44, i64 0, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i32, i32* @LOG_ERR
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i32, i8*, ...) @output(i32 %49, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %43
%55 = load i8**, i8*** %5
%56 = load i8*, i8** %55
%57 = load i32*, i32** %6
%58 = load i32, i32* %57
%59 = load i32*, i32** %7
%60 = call i32 @fread(i8* %56, i32 %58, i32 1, i32* %59)
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %68
62: ; preds = %54
%63 = load i32, i32* @LOG_ERR
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @output(i32 %63, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %54
%69 = load i32*, i32** %7
%70 = call i32 @fclose(i32* %69)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@LOG_ERR = global i32 0
@.str.1 = [24 x i8] c"error: fopen failed. %s\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"error: fseek failed. %s\00"
@errno = global i32 0
@.str.3 = [32 x i8] c"error: memory allocation failed\00"
@SEEK_SET = global i32 0
@.str.4 = [24 x i8] c"error: fread failed. %s\00"
define void @read_file(i8* %0, i8** nocapture %1, i32* 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 %6, label %10
6: ; preds = %3
%7 = load i32, i32* @LOG_ERR
%8 = tail call i32 (i32, i8*, ...) @output(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %4, i64 0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i32, i32* @LOG_ERR
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @output(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = tail call i32 @ftell(i32* %4)
store i32 %21, i32* %2
%22 = tail call i8* @malloc(i32 %21)
store i8* %22, i8** %1
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @LOG_ERR
%26 = tail call i32 (i32, i8*, ...) @output(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @fseek(i32* %4, i64 0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i32, i32* @LOG_ERR
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i32, i8*, ...) @output(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %28
%39 = load i8*, i8** %1
%40 = load i32, i32* %2
%41 = tail call i32 @fread(i8* %39, i32 %40, i32 1, i32* %4)
%42 = icmp eq i32 %41, 1
br i1 %42, label %49, label %43
43: ; preds = %38
%44 = load i32, i32* @LOG_ERR
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @output(i32 %44, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %38
%50 = tail call i32 @fclose(i32* %4)
ret void
}