#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ **);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *);
int read_file_to_buffer(const char *file_path, uint8_t **buffer,
                        size_t *buf_size) {
  int ret = 0;
  FILE *fp = NULL;
  size_t file_size;
  uint8_t *buf_ptr;
  fp = fopen(file_path, "rb");
  if (fp == NULL) {
    printf("Error opening .encr file\n");
    ret = 3;
    goto err;
  }
  fseek(fp, 0, SEEK_END);
  file_size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  if (!file_size) {
    ret = 4;
    goto err;
  }
  buf_ptr = (uint8_t *)malloc(file_size);
  if (buf_ptr == NULL) {
    ret = 2;
    goto err;
  }
  ret = fread(buf_ptr, file_size, 1, fp);
  if (ret != 1) {
    ret = 4;
    goto err;
  }
  *buffer = buf_ptr;
  *buf_size = file_size;
  fclose(fp);
  return ret;
err:
  free(buffer);
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<read_file_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11da <read_file_to_buffer+0x5a>
lea    0xe3a(%rip),%rdi
call   1060 <printf@plt>
movl   $0x3,-0x24(%rbp)
jmp    12a3 <read_file_to_buffer+0x123>
mov    -0x30(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1226 <read_file_to_buffer+0xa6>
movl   $0x4,-0x24(%rbp)
jmp    12a3 <read_file_to_buffer+0x123>
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    124a <read_file_to_buffer+0xca>
movl   $0x2,-0x24(%rbp)
jmp    12a3 <read_file_to_buffer+0x123>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     1279 <read_file_to_buffer+0xf9>
movl   $0x4,-0x24(%rbp)
jmp    12a3 <read_file_to_buffer+0x123>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <read_file_to_buffer+0x13b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r14
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     122a <read_file_to_buffer+0xaa>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    $0x4,%r15d
test   %r12,%r12
je     1244 <read_file_to_buffer+0xc4>
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     123e <read_file_to_buffer+0xbe>
mov    %rax,%r13
mov    %rbp,(%rsp)
mov    $0x1,%ebp
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    1244 <read_file_to_buffer+0xc4>
mov    %r13,(%r14)
mov    (%rsp),%rax
mov    %r12,(%rax)
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    1257 <read_file_to_buffer+0xd7>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%r15d
jmp    1244 <read_file_to_buffer+0xc4>
mov    $0x2,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"Error opening .encr file\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_file_to_buffer(i8* %0, i32** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
store i8* %0, i8** %5
store i32** %1, i32*** %6
store i64* %2, i64** %7
store i32 0, i32* %8
store i32* null, i32** %9
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %8
br label %53
18: ; preds = %3
%19 = load i32*, i32** %9
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %9
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %10
%24 = load i32*, i32** %9
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i64, i64* %10
%28 = icmp ne i64 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %18
store i32 4, i32* %8
br label %53
30: ; preds = %18
%31 = load i64, i64* %10
%32 = call i64 @malloc(i64 %31)
%33 = inttoptr i64 %32 to i32*
store i32* %33, i32** %11
%34 = load i32*, i32** %11
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 2, i32* %8
br label %53
37: ; preds = %30
%38 = load i32*, i32** %11
%39 = load i64, i64* %10
%40 = load i32*, i32** %9
%41 = call i32 @fread(i32* %38, i64 %39, i32 1, i32* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 4, i32* %8
br label %53
45: ; preds = %37
%46 = load i32*, i32** %11
%47 = load i32**, i32*** %6
store i32* %46, i32** %47
%48 = load i64, i64* %10
%49 = load i64*, i64** %7
store i64 %48, i64* %49
%50 = load i32*, i32** %9
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %59
53: ; preds = %44, %36, %29, %16
%54 = load i32**, i32*** %6
%55 = call i32 @free(i32** %54)
%56 = load i32*, i32** %9
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %8
store i32 %58, i32* %4
br label %59
59: ; preds = %53, %45
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [25 x i8] c"Error opening .encr file\00"
define i32 @read_file_to_buffer(i8* nocapture %0, i32** nocapture %1, i64* nocapture writeonly %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 %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %24
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %4)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %4, i32 0, i32 %12)
%14 = icmp eq i64 %11, 0
br i1 %14, label %24, label %15
15: ; preds = %8
%16 = tail call i64 @malloc(i64 %11)
%17 = inttoptr i64 %16 to i32*
%18 = icmp eq i64 %16, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = tail call i32 @fread(i32* %17, i64 %11, i32 1, i32* %4)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %19
store i32* %17, i32** %1
store i64 %11, i64* %2
%23 = tail call i32 @fclose(i32* %4)
br label %28
24: ; preds = %19, %15, %8, %6
%25 = phi i32 [ 3, %6 ], [ 4, %8 ], [ 2, %15 ], [ 4, %19 ]
%26 = tail call i32 @free(i32** %1)
%27 = tail call i32 @fclose(i32* %4)
br label %28
28: ; preds = %24, %22
%29 = phi i32 [ %25, %24 ], [ 1, %22 ]
ret i32 %29
}