#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(char *, unsigned long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void readAsBinary(char *filename) {
  FILE *file;
  char *buffer;
  unsigned long fileLen;
  file = fopen(filename, "rb");
  if (!file) {
    fprintf(stderr, "Unable to open file %s", filename);
    return;
  }
  fseek(file, 0, SEEK_END);
  fileLen = ftell(file);
  fseek(file, 0, SEEK_SET);
  buffer = (char *)malloc(fileLen + 1);
  if (!buffer) {
    fprintf(stderr, "Memory error!");
    fclose(file);
    return;
  }
  fread(buffer, fileLen, 1, file);
  fclose(file);
  printf("Binary file: \n");
  printf("%s\n", buffer);
  free(buffer);
}


x86 assembly (clang, -O0):
<readAsBinary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <readAsBinary+0x4b>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1299 <readAsBinary+0x109>
mov    -0x10(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1251 <readAsBinary+0xc1>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1299 <readAsBinary+0x109>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readAsBinary>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe61(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122e <readAsBinary+0x9e>
mov    %rax,%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x1(%r15),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     124d <readAsBinary+0xbd>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <fprintf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"Memory error!\00"
@.str.3 = [15 x i8] c"Binary file: \0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @readAsBinary(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %2
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %46
14: ; preds = %1
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %3
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %5
%20 = load i32*, i32** %3
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i64, i64* %5
%24 = add i64 %23, 1
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %34, label %29
29: ; preds = %14
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
br label %46
34: ; preds = %14
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = load i32*, i32** %3
%38 = call i32 @fread(i8* %35, i64 %36, i32 1, i32* %37)
%39 = load i32*, i32** %3
%40 = call i32 @fclose(i32* %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%42 = load i8*, i8** %4
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %34, %29, %10
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"Memory error!\00"
@str = [14 x i8] c"Binary file: \00"
define void @readAsBinary(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i32 0, i32 %11)
%13 = add i64 %10, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %21
17: ; preds = %7
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @fclose(i32* %2)
br label %27
21: ; preds = %7
%22 = tail call i32 @fread(i8* %15, i64 %10, i32 1, i32* %2)
%23 = tail call i32 @fclose(i32* %2)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @puts(i8* %15)
%26 = tail call i32 @free(i8* %15)
br label %27
27: ; preds = %21, %17, %4
ret void
}