#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int read_file_into_buffer(uint8_t *buffer, size_t bytes_to_read,
                          const char *filename) {
  FILE *ptr;
  ptr = fopen(filename, "rb");
  if (NULL == ptr)
    return -1;
  size_t bytes_read = fread(buffer, 1, bytes_to_read, ptr);
  (void)fclose(ptr);
  return (int)bytes_read;
}


x86 assembly (clang, -O0):
<read_file_into_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1170 <read_file_into_buffer+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <read_file_into_buffer+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_into_buffer>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xebe(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1172 <read_file_into_buffer+0x42>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1178 <read_file_into_buffer+0x48>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @read_file_into_buffer(i32* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %24
15: ; preds = %3
%16 = load i32*, i32** %5
%17 = load i64, i64* %6
%18 = load i32*, i32** %8
%19 = call i64 @fread(i32* %16, i32 1, i64 %17, i32* %18)
store i64 %19, i64* %9
%20 = load i32*, i32** %8
%21 = call i32 @fclose(i32* %20)
%22 = load i64, i64* %9
%23 = trunc i64 %22 to i32
store i32 %23, i32* %4
br label %24
24: ; preds = %15, %14
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @read_file_into_buffer(i32* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i64 @fread(i32* %0, i32 1, i64 %1, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = trunc i64 %7 to i32
br label %10
10: ; preds = %3, %6
%11 = phi i32 [ %9, %6 ], [ -1, %3 ]
ret i32 %11
}