#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*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int elf_read(char *filename, char *image) {
  FILE *file = fopen(filename, "r");
  int len, rd;
  if (!file) {
    printf("ERROR--> Could not open file: '%s'\n", filename);
    return -1;
  }
  fseek(file, 0, SEEK_END);
  len = ftell(file);
  fseek(file, 0, SEEK_SET);
  if ((rd = fread(image, 1, len, file)) != len) {
    printf("ERROR--> Only got %d/%d bytes\n", rd, len);
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<elf_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <elf_read+0x4d>
mov    -0x10(%rbp),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <elf_read+0xc8>
mov    -0x20(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x28(%rbp)
cmp    -0x24(%rbp),%eax
je     1212 <elf_read+0xc2>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <elf_read+0xc8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_read>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c8 <elf_read+0x78>
mov    %rax,%rbx
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
je     11de <elf_read+0x8e>
lea    0xe6b(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d9 <elf_read+0x89>
lea    0xe33(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [36 x i8] c"ERROR--> Could not open file: '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [31 x i8] c"ERROR--> Only got %d/%d bytes\0A\00"
define i32 @elf_read(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %37
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %7
%22 = load i32*, i32** %6
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i8*, i8** %5
%26 = load i32, i32* %7
%27 = load i32*, i32** %6
%28 = call i32 @fread(i8* %25, i32 1, i32 %26, i32* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %7
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %16
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
store i32 -1, i32* %3
br label %37
35: ; preds = %16
%36 = load i32, i32* %7
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %31, %13
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [36 x i8] c"ERROR--> Could not open file: '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [31 x i8] c"ERROR--> Only got %d/%d bytes\0A\00"
define i32 @elf_read(i8* %0, i8* nocapture %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 %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i32 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i32 @fread(i8* %1, i32 1, i32 %10, i32* %3)
%14 = icmp eq i32 %13, %10
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %10)
br label %17
17: ; preds = %7, %15, %5
%18 = phi i32 [ -1, %15 ], [ -1, %5 ], [ %10, %7 ]
ret i32 %18
}