#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
char *cl_utils_read_file(char *filename) {
  FILE *file;
  int size;
  char *contents;
  if ((file = fopen(filename, "r")) == NULL)
    goto out_no_open;
  if (fseek(file, 0, SEEK_END) == -1)
    goto out_info_err;
  if ((size = ftell(file)) == -1)
    goto out_info_err;
  if (fseek(file, 0, SEEK_SET) == -1)
    goto out_info_err;
  contents = malloc(size + 1);
  if (contents == NULL)
    goto out_nomem;
  fread(contents, 1, size, file);
  if (ferror(file)) {
    free(contents);
    goto out_read_err;
  }
  fclose(file);
  contents[size] = '\0';
  return contents;
out_read_err:
  free(contents);
out_nomem:
out_info_err:
  fclose(file);
out_no_open:
  return NULL;
}


x86 assembly (clang, -O0):
<cl_utils_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11af <cl_utils_read_file+0x2f>
jmp    129b <cl_utils_read_file+0x11b>
mov    -0x18(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
jne    11d1 <cl_utils_read_file+0x51>
jmp    1292 <cl_utils_read_file+0x112>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11eb <cl_utils_read_file+0x6b>
jmp    1292 <cl_utils_read_file+0x112>
mov    -0x18(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
jne    120d <cl_utils_read_file+0x8d>
jmp    1292 <cl_utils_read_file+0x112>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122c <cl_utils_read_file+0xac>
jmp    128d <cl_utils_read_file+0x10d>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1262 <cl_utils_read_file+0xe2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1284 <cl_utils_read_file+0x104>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a3 <cl_utils_read_file+0x123>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1292 <cl_utils_read_file+0x112>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_utils_read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1226 <cl_utils_read_file+0xa6>
mov    %rax,%rbx
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
je     121e <cl_utils_read_file+0x9e>
mov    %rbx,%rdi
call   1070 <ftell@plt>
cmp    $0xffffffff,%eax
je     121e <cl_utils_read_file+0x9e>
mov    %eax,%r14d
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
je     121e <cl_utils_read_file+0x9e>
lea    0x1(%r14),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     121e <cl_utils_read_file+0x9e>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1232 <cl_utils_read_file+0xb2>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1060 <fclose@plt>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1229 <cl_utils_read_file+0xa9>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @cl_utils_read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %61
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %11
br label %58
17: ; preds = %11
%18 = load i32*, i32** %4
%19 = call i32 @ftell(i32* %18)
store i32 %19, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %17
br label %58
22: ; preds = %17
%23 = load i32*, i32** %4
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
br label %58
28: ; preds = %22
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %28
br label %57
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = load i32, i32* %5
%38 = load i32*, i32** %4
%39 = call i32 @fread(i8* %36, i32 1, i32 %37, i32* %38)
%40 = load i32*, i32** %4
%41 = call i64 @ferror(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i8*, i8** %6
%45 = call i32 @free(i8* %44)
br label %54
46: ; preds = %35
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %6
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %6
store i8* %53, i8** %2
br label %62
54: ; preds = %43
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %34
br label %58
58: ; preds = %57, %27, %21, %16
%59 = load i32*, i32** %4
%60 = call i32 @fclose(i32* %59)
br label %61
61: ; preds = %58, %10
store i8* null, i8** %2
br label %62
62: ; preds = %61, %46
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @cl_utils_read_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %32, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %30, label %8
8: ; preds = %4
%9 = tail call i32 @ftell(i32* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %30, label %11
11: ; preds = %8
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %2, i32 0, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %30, label %15
15: ; preds = %11
%16 = add nuw nsw i32 %9, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %30, label %19
19: ; preds = %15
%20 = tail call i32 @fread(i8* %17, i32 1, i32 %9, i32* %2)
%21 = tail call i64 @ferror(i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = tail call i32 @free(i8* %17)
%25 = tail call i32 @free(i8* %17)
br label %30
26: ; preds = %19
%27 = tail call i32 @fclose(i32* %2)
%28 = sext i32 %9 to i64
%29 = getelementptr inbounds i8, i8* %17, i64 %28
store i8 0, i8* %29
br label %32
30: ; preds = %23, %15, %11, %8, %4
%31 = tail call i32 @fclose(i32* %2)
br label %32
32: ; preds = %30, %1, %26
%33 = phi i8* [ %17, %26 ], [ null, %1 ], [ null, %30 ]
ret i8* %33
}