#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *tea_file_read(char *path, int *len) {
  int size;
  char *out;
  FILE *file;
  file = fopen(path, "rb");
  if (!file) {
    fprintf(stderr, "file read error: %s\n", path);
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  rewind(file);
  out = (char *)malloc(size + 1);
  fread(out, sizeof(char), size, file);
  out[size] = 0;
  fclose(file);
  if (len)
    *len = size;
  return out;
}


x86 assembly (clang, -O0):
<tea_file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d5 <tea_file_read+0x55>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <tea_file_read+0xd3>
mov    -0x30(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     124b <tea_file_read+0xcb>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tea_file_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1202 <tea_file_read+0x82>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1050 <rewind@plt>
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     121d <tea_file_read+0x9d>
mov    %r12d,(%r14)
jmp    121d <tea_file_read+0x9d>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"file read error: %s\0A\00"
@SEEK_END = global i32 0
define i8* @tea_file_read(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i8* null, i8** %3
br label %46
17: ; preds = %2
%18 = load i32*, i32** %8
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %8
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %6
%23 = load i32*, i32** %8
%24 = call i32 @rewind(i32* %23)
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = load i32, i32* %6
%31 = load i32*, i32** %8
%32 = call i32 @fread(i8* %29, i32 1, i32 %30, i32* %31)
%33 = load i8*, i8** %7
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
%39 = load i32*, i32** %5
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %17
%42 = load i32, i32* %6
%43 = load i32*, i32** %5
store i32 %42, i32* %43
br label %44
44: ; preds = %41, %17
%45 = load i8*, i8** %7
store i8* %45, i8** %3
br label %46
46: ; preds = %44, %13
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"file read error: %s\0A\00"
@SEEK_END = global i32 0
define i8* @tea_file_read(i8* %0, i32* writeonly %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 %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %3)
%12 = tail call i32 @rewind(i32* %3)
%13 = add nsw i32 %11, 1
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i32 1, i32 %11, i32* %3)
%17 = sext i32 %11 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
%19 = tail call i32 @fclose(i32* %3)
%20 = icmp eq i32* %1, null
br i1 %20, label %22, label %21
21: ; preds = %8
store i32 %11, i32* %1
br label %22
22: ; preds = %8, %21, %5
%23 = phi i8* [ null, %5 ], [ %15, %21 ], [ %15, %8 ]
ret i8* %23
}