#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 const *, char *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
size_t fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
size_t read_file_as_c_str(char **ppOutput, const char *filename) {
  {
    if (*ppOutput) {
      free((void *)*ppOutput);
      *ppOutput = NULL;
    }
  };
  FILE *fp = fopen(filename, "rb");
  if (!fp)
    return 0;
  size_t sz = (fseek(fp, 0, SEEK_END), ftell(fp));
  fseek(fp, 0, SEEK_SET);
  *ppOutput = (char *)malloc(sz + 1);
  fread(*ppOutput, sz, 1, fp);
  (*ppOutput)[sz] = 0;
  fclose(fp);
  return sz + 1;
}


x86 assembly (clang, -O0):
<read_file_as_c_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a5 <read_file_as_c_str+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <read_file_as_c_str+0x61>
movq   $0x0,-0x8(%rbp)
jmp    125c <read_file_as_c_str+0xec>
mov    -0x20(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_as_c_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1192 <read_file_as_c_str+0x22>
call   1030 <free@plt>
movq   $0x0,(%r14)
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     120f <read_file_as_c_str+0x9f>
mov    %rax,%rbx
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
movslq %eax,%r12
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r12),%r15
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%r12,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1212 <read_file_as_c_str+0xa2>
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"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @read_file_as_c_str(i8** %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %4
%13 = load i8*, i8** %12
%14 = call i32 @free(i8* %13)
%15 = load i8**, i8*** %4
store i8* null, i8** %15
br label %16
16: ; preds = %11, %2
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %16
store i64 0, i64* %3
br label %50
22: ; preds = %16
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_END
%25 = call i64 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32*, i32** %6
%27 = call i32 @ftell(i32* %26)
%28 = sext i32 %27 to i64
store i64 %28, i64* %7
%29 = load i32*, i32** %6
%30 = load i32, i32* @SEEK_SET
%31 = call i64 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i64, i64* %7
%33 = add i64 %32, 1
%34 = call i64 @malloc(i64 %33)
%35 = inttoptr i64 %34 to i8*
%36 = load i8**, i8*** %4
store i8* %35, i8** %36
%37 = load i8**, i8*** %4
%38 = load i8*, i8** %37
%39 = load i64, i64* %7
%40 = load i32*, i32** %6
%41 = call i32 @fread(i8* %38, i64 %39, i32 1, i32* %40)
%42 = load i8**, i8*** %4
%43 = load i8*, i8** %42
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i32*, i32** %6
%47 = call i32 @fclose(i32* %46)
%48 = load i64, i64* %7
%49 = add i64 %48, 1
store i64 %49, i64* %3
br label %50
50: ; preds = %22, %21
%51 = load i64, i64* %3
ret i64 %51
}

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