#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *read_file(char *path, long int *read_len) {
  FILE *fp;
  char *contents;
  long int filesz;
  *read_len = 0;
  fp = fopen(path, "rb");
  if (!fp) {
    fprintf(stderr, "Cannot open file: %s, error: %d\n", path, errno);
    return NULL;
  }
  if (fseek(fp, 0, SEEK_END)) {
    fprintf(stderr, "Failed to seek end of file, error: %d\n", errno);
    fclose(fp);
    return NULL;
  }
  if ((filesz = ftell(fp)) < 0) {
    fprintf(stderr, "Failed to get the file size, error: %d\n", errno);
    fclose(fp);
    return NULL;
  }
  rewind(fp);
  contents = malloc((filesz + 1) * sizeof(char));
  if (!contents) {
    fprintf(stderr, "Failed to allocate memory, error: %d\n", errno);
    fclose(fp);
    return NULL;
  }
  if (fread(contents, sizeof(char), filesz, fp) != filesz) {
    fprintf(stderr, "Failed to read the file, error: %d\n", errno);
    free(contents);
    fclose(fp);
    return NULL;
  }
  contents[filesz] = '\0';
  fclose(fp);
  *read_len = filesz;
  return contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fb <read_file+0x6b>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1382 <read_file+0x1f2>
mov    -0x20(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     124f <read_file+0xbf>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1382 <read_file+0x1f2>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    129c <read_file+0x10c>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1382 <read_file+0x1f2>
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12fb <read_file+0x16b>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1382 <read_file+0x1f2>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x30(%rbp),%rax
je     135a <read_file+0x1ca>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
mov    0x2ca5(%rip),%rax
mov    (%rax),%edx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1382 <read_file+0x1f2>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,(%rsi)
lea    0xe53(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     11ff <read_file+0x6f>
mov    %rax,%r13
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
je     1232 <read_file+0xa2>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0xe39(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
jmp    1225 <read_file+0x95>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
lea    0xdeb(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r13,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     1286 <read_file+0xf6>
mov    %rax,%r12
mov    %r13,%rdi
call   1060 <rewind@plt>
lea    0x1(%r12),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12a4 <read_file+0x114>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    12c2 <read_file+0x132>
movb   $0x0,(%r14,%r12,1)
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,(%r15)
jmp    1225 <read_file+0x95>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
lea    0xdac(%rip),%rsi
jmp    11eb <read_file+0x5b>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
lea    0xdb6(%rip),%rsi
jmp    11eb <read_file+0x5b>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%edx
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
jmp    1225 <read_file+0x95>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Cannot open file: %s, error: %d\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [39 x i8] c"Failed to seek end of file, error: %d\0A\00"
@.str.3 = [40 x i8] c"Failed to get the file size, error: %d\0A\00"
@.str.4 = [38 x i8] c"Failed to allocate memory, error: %d\0A\00"
@.str.5 = [36 x i8] c"Failed to read the file, error: %d\0A\00"
define noalias i8* @read_file(i8* %0, i64* nocapture writeonly %1) {
store i64 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* @stderr
%7 = load i32, i32* @errno
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %7)
br label %48
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @fseek(i32* %3, i32 0, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @fclose(i32* %3)
br label %48
18: ; preds = %9
%19 = tail call i64 @ftell(i32* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = tail call i32 @fclose(i32* %3)
br label %48
26: ; preds = %18
%27 = tail call i32 @rewind(i32* %3)
%28 = add nuw nsw i64 %19, 1
%29 = tail call i8* @malloc(i64 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = tail call i32 @fclose(i32* %3)
br label %48
36: ; preds = %26
%37 = tail call i64 @fread(i8* %29, i32 1, i64 %19, i32* %3)
%38 = icmp eq i64 %37, %19
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%43 = tail call i32 @free(i8* %29)
%44 = tail call i32 @fclose(i32* %3)
br label %48
45: ; preds = %36
%46 = getelementptr inbounds i8, i8* %29, i64 %19
store i8 0, i8* %46
%47 = tail call i32 @fclose(i32* %3)
store i64 %19, i64* %1
br label %48
48: ; preds = %45, %39, %31, %21, %13, %5
%49 = phi i8* [ null, %13 ], [ null, %21 ], [ null, %39 ], [ %29, %45 ], [ null, %31 ], [ null, %5 ]
ret i8* %49
}