#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ show_error(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *read_file(const char *fname, size_t size) {
  char *data;
  FILE *f = fopen(fname, "rb");
  if (!f)
    show_error("can't open file '%s': %s\n", fname, strerror(errno));
  data = malloc(size);
  if (size != fread(data, 1, size, f))
    show_error("error reading file '%s': %s\n", fname, strerror(errno));
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <read_file+0x57>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe51(%rip),%rdi
call   1060 <show_error@plt>
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     121a <read_file+0xba>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe08(%rip),%rdi
call   1060 <show_error@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a1 <read_file+0x41>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe6c(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <show_error@plt>
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %rbx,%rax
je     11e3 <read_file+0x83>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe44(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <show_error@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"can't open file '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"error reading file '%s': %s\0A\00"
define i8* @read_file(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @show_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %14)
br label %16
16: ; preds = %11, %2
%17 = load i64, i64* %4
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %5
%19 = load i64, i64* %4
%20 = load i8*, i8** %5
%21 = load i64, i64* %4
%22 = load i32*, i32** %6
%23 = call i64 @fread(i8* %20, i32 1, i64 %21, i32* %22)
%24 = icmp ne i64 %19, %23
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i8*, i8** %3
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 @show_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %28)
br label %30
30: ; preds = %25, %16
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"can't open file '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"error reading file '%s': %s\0A\00"
define noalias i8* @read_file(i8* %0, i64 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @show_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %7)
br label %9
9: ; preds = %5, %2
%10 = tail call i8* @malloc(i64 %1)
%11 = tail call i64 @fread(i8* %10, i32 1, i64 %1, i32* %3)
%12 = icmp eq i64 %11, %1
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @show_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %15)
br label %17
17: ; preds = %13, %9
%18 = tail call i32 @fclose(i32* %3)
ret i8* %10
}