#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *read_file_into_heap_as_c_str(char *filename, int *output_file_size) {
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    return 0;
  }
  fseek(fp, 0L, SEEK_END);
  int file_size = (int)ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  char *c_str = (char *)malloc(file_size + 1);
  int bytes_read = (int)fread(c_str, file_size, 1, fp);
  fclose(fp);
  c_str[file_size] = 0;
  if (output_file_size) {
    *output_file_size = file_size;
  }
  return c_str;
}


x86 assembly (clang, -O0):
<read_file_into_heap_as_c_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <read_file_into_heap_as_c_str+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    122c <read_file_into_heap_as_c_str+0xcc>
mov    -0x20(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     1224 <read_file_into_heap_as_c_str+0xc4>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_into_heap_as_c_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e9 <read_file_into_heap_as_c_str+0x89>
mov    %rax,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movslq %r15d,%rax
movb   $0x0,(%r12,%rax,1)
test   %r14,%r14
je     11ec <read_file_into_heap_as_c_str+0x8c>
mov    %r15d,(%r14)
jmp    11ec <read_file_into_heap_as_c_str+0x8c>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_file_into_heap_as_c_str(i8* nocapture %0, i32* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i64 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %3, i64 0, i32 %10)
%12 = add nsw i32 %9, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i64 @fread(i8* %14, i32 %9, i32 1, i32* %3)
%16 = tail call i32 @fclose(i32* %3)
%17 = shl i64 %8, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %14, i64 %18
store i8 0, i8* %19
%20 = icmp eq i32* %1, null
br i1 %20, label %22, label %21
21: ; preds = %5
store i32 %9, i32* %1
br label %22
22: ; preds = %5, %21, %2
%23 = phi i8* [ null, %2 ], [ %14, %21 ], [ %14, %5 ]
ret i8* %23
}