#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, unsigned long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int file_utils_read(char *buffer, size_t size, const char *filename) {
  FILE *fd;
  fd = fopen(filename, "r");
  if (fd)
    fclose(fd);
  else {
    printf("failed to open: %s\n", filename);
    exit(0);
  }
  fd = fopen(filename, "rb");
  if (!fd)
    return 0;
  fseek(fd, 0L, SEEK_END);
  unsigned long sz = ftell(fd);
  fseek(fd, 0L, SEEK_SET);
  fread(buffer, sz, 1, fd);
  fclose(fd);
  return 1;
}


x86 assembly (clang, -O0):
<file_utils_read>:
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    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b1 <file_utils_read+0x41>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11c8 <file_utils_read+0x58>
mov    -0x20(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f3 <file_utils_read+0x83>
movl   $0x0,-0x4(%rbp)
jmp    1252 <file_utils_read+0xe2>
mov    -0x28(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_utils_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rdi,%r14
lea    0xe7c(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1216 <file_utils_read+0xa6>
mov    %rax,%rdi
call   1040 <fclose@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1207 <file_utils_read+0x97>
mov    %rax,%rbx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1209 <file_utils_read+0x99>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde5(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"failed to open: %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @file_utils_read(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %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 ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = call i32 @fclose(i32* %15)
br label %21
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %14
%22 = load i8*, i8** %7
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
store i32 0, i32* %4
br label %42
27: ; preds = %21
%28 = load i32*, i32** %8
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i64 0, i32 %29)
%31 = load i32*, i32** %8
%32 = call i64 @ftell(i32* %31)
store i64 %32, i64* %9
%33 = load i32*, i32** %8
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i64 0, i32 %34)
%36 = load i8*, i8** %5
%37 = load i64, i64* %9
%38 = load i32*, i32** %8
%39 = call i32 @fread(i8* %36, i64 %37, i32 1, i32* %38)
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
store i32 1, i32* %4
br label %42
42: ; preds = %27, %26
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"failed to open: %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @file_utils_read(i8* nocapture %0, i64 %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 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 i32 @fclose(i32* %4)
%8 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %13
10: ; preds = %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %6
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @fseek(i32* %8, i64 0, i32 %14)
%16 = tail call i64 @ftell(i32* %8)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %8, i64 0, i32 %17)
%19 = tail call i32 @fread(i8* %0, i64 %16, i32 1, i32* %8)
%20 = tail call i32 @fclose(i32* %8)
br label %21
21: ; preds = %6, %13
%22 = phi i32 [ 1, %13 ], [ 0, %6 ]
ret i32 %22
}