#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ printf(char *, long, char const *, long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_file(const char *filename) {
  FILE *file;
  char *buffer;
  long size, nread;
  file = fopen(filename, "rb");
  if (!file) {
    fprintf(stderr, "error: failed to open file '%s': %s\n", filename,
            strerror(errno));
    goto open_file_error;
  }
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  if (size <= 0) {
    fprintf(stderr, "error: failed to get '%s' file size: %s\n", filename,
            strerror(errno));
    goto file_size_error;
  }
  fseek(file, 0, SEEK_SET);
  buffer = malloc(size + 2);
  if (!buffer) {
    fprintf(stderr,
            "error: failed to allocate %ld bytes for file '%s'\'s buffer: %s\n",
            size, filename, strerror(errno));
    goto buffer_alloc_error;
  }
  nread = fread(buffer, 1, size, file);
  if (nread < size) {
    fprintf(stderr,
            "error: failed to read the %ld bytes of file '%s' (%ld were read): "
            "%s\n",
            size, filename, nread, strerror(errno));
    goto file_read_error;
  }
  fclose(file);
  buffer[nread] = '\n';
  buffer[nread + 1] = '\0';
  printf("read %ld bytes from file '%s' and it has %ld\n", nread, filename,
         size);
  return buffer;
file_read_error:
  free(buffer);
buffer_alloc_error:
file_size_error:
  fclose(file);
open_file_error:
  return NULL;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <read_file+0x6d>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13d6 <read_file+0x236>
mov    -0x18(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jg     1278 <read_file+0xd8>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13cd <read_file+0x22d>
mov    -0x18(%rbp),%rdi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12f3 <read_file+0x153>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    %rax,%r8
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13c8 <read_file+0x228>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1378 <read_file+0x1d8>
mov    0x2cb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    -0x70(%rbp),%r8
mov    %rax,%r9
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13bf <read_file+0x21f>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xd2a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13de <read_file+0x23e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    13cd <read_file+0x22d>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe4b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1274 <read_file+0xd4>
mov    %rax,%r12
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
jle    12a5 <read_file+0x105>
mov    %rax,%r15
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x2(%r15),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12d4 <read_file+0x134>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
cmp    %r15,%rax
jge    1320 <read_file+0x180>
mov    0x2d9d(%rip),%rax
mov    (%rax),%ebp
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe3f(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r13,%r8
mov    %rax,%r9
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1304 <read_file+0x164>
mov    0x2d5d(%rip),%rax
mov    (%rax),%ebp
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd71(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    130e <read_file+0x16e>
mov    0x2d2c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd65(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1304 <read_file+0x164>
mov    0x2cfd(%rip),%rax
mov    (%rax),%ebx
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd5f(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1050 <fclose@plt>
movw   $0xa,(%rbx,%r13,1)
lea    0xda1(%rip),%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <printf@plt>
jmp    130e <read_file+0x16e>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"error: failed to open file '%s': %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"error: failed to get '%s' file size: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [64 x i8] c"error: failed to allocate %ld bytes for file '%s''s buffer: %s\0A\00"
@.str.4 = [70 x i8] c"error: failed to read the %ld bytes of file '%s' (%ld were read): %s\0A\00"
@.str.5 = [46 x i8] c"read %ld bytes from file '%s' and it has %ld\0A\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %18, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
br label %86
18: ; preds = %1
%19 = load i32*, i32** %4
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %4
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp sle i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %30)
br label %83
32: ; preds = %18
%33 = load i32*, i32** %4
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i64, i64* %6
%37 = add nsw i64 %36, 2
%38 = call i8* @malloc(i64 %37)
store i8* %38, i8** %5
%39 = load i8*, i8** %5
%40 = icmp ne i8* %39, null
br i1 %40, label %48, label %41
41: ; preds = %32
%42 = load i32, i32* @stderr
%43 = load i64, i64* %6
%44 = load i8*, i8** %3
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i64 %43, i8* %44, i8* %46)
br label %82
48: ; preds = %32
%49 = load i8*, i8** %5
%50 = load i64, i64* %6
%51 = load i32*, i32** %4
%52 = call i64 @fread(i8* %49, i32 1, i64 %50, i32* %51)
store i64 %52, i64* %7
%53 = load i64, i64* %7
%54 = load i64, i64* %6
%55 = icmp slt i64 %53, %54
br i1 %55, label %56, label %64
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = load i64, i64* %6
%59 = load i8*, i8** %3
%60 = load i64, i64* %7
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i64 %58, i8* %59, i64 %60, i8* %62)
br label %79
64: ; preds = %48
%65 = load i32*, i32** %4
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %5
%68 = load i64, i64* %7
%69 = getelementptr inbounds i8, i8* %67, i64 %68
store i8 10, i8* %69
%70 = load i8*, i8** %5
%71 = load i64, i64* %7
%72 = add nsw i64 %71, 1
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8 0, i8* %73
%74 = load i64, i64* %7
%75 = load i8*, i8** %3
%76 = load i64, i64* %6
%77 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i64 %74, i8* %75, i64 %76)
%78 = load i8*, i8** %5
store i8* %78, i8** %2
br label %87
79: ; preds = %56
%80 = load i8*, i8** %5
%81 = call i32 @free(i8* %80)
br label %82
82: ; preds = %79, %41
br label %83
83: ; preds = %82, %26
%84 = load i32*, i32** %4
%85 = call i32 @fclose(i32* %84)
br label %86
86: ; preds = %83, %12
store i8* null, i8** %2
br label %87
87: ; preds = %86, %64
%88 = load i8*, i8** %2
ret i8* %88
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"error: failed to open file '%s': %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"error: failed to get '%s' file size: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [64 x i8] c"error: failed to allocate %ld bytes for file '%s''s buffer: %s\0A\00"
@.str.4 = [70 x i8] c"error: failed to read the %ld bytes of file '%s' (%ld were read): %s\0A\00"
@.str.5 = [46 x i8] c"read %ld bytes from file '%s' and it has %ld\0A\00"
define noalias i8* @read_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
br label %47
9: ; preds = %1
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %2, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %2)
%13 = icmp slt i64 %12, 1
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %17)
br label %45
19: ; preds = %9
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %2, i32 0, i32 %20)
%22 = add nuw nsw i64 %12, 2
%23 = tail call i8* @malloc(i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i64 %12, i8* %0, i8* %28)
br label %45
30: ; preds = %19
%31 = tail call i64 @fread(i8* %23, i32 1, i64 %12, i32* %2)
%32 = icmp slt i64 %31, %12
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i64 %12, i8* %0, i64 %31, i8* %36)
%38 = tail call i32 @free(i8* %23)
br label %45
39: ; preds = %30
%40 = tail call i32 @fclose(i32* %2)
%41 = getelementptr inbounds i8, i8* %23, i64 %31
store i8 10, i8* %41
%42 = add nsw i64 %31, 1
%43 = getelementptr inbounds i8, i8* %23, i64 %42
store i8 0, i8* %43
%44 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i64 %31, i8* %0, i64 %12)
br label %47
45: ; preds = %25, %33, %14
%46 = tail call i32 @fclose(i32* %2)
br label %47
47: ; preds = %4, %45, %39
%48 = phi i8* [ %23, %39 ], [ null, %45 ], [ null, %4 ]
ret i8* %48
}