#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*/ debug(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int read_file(const char *filename, char *buf, int size) {
  FILE *file;
  int readsize;
  file = fopen(filename, "r");
  if (file == NULL) {
    debug("error opening '%s': %s", filename, strerror(errno));
    return 0;
  }
  readsize = fread(buf, sizeof(char), size, file);
  fclose(file);
  return readsize;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b6 <read_file+0x66>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe5d(%rip),%rdi
call   1050 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <read_file+0x8d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1195 <read_file+0x45>
mov    %rax,%rbp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
jmp    11b6 <read_file+0x66>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <debug@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%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 eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %17)
store i32 0, i32* %4
br label %27
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = call i32 @fread(i8* %20, i32 1, i32 %21, i32* %22)
store i32 %23, i32* %9
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %9
store i32 %26, i32* %4
br label %27
27: ; preds = %19, %14
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
define i32 @read_file(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %13
10: ; preds = %3
%11 = tail call i32 @fread(i8* %1, i32 1, i32 %2, i32* %4)
%12 = tail call i32 @fclose(i32* %4)
br label %13
13: ; preds = %10, %6
%14 = phi i32 [ 0, %6 ], [ %11, %10 ]
ret i32 %14
}