#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*/ chaz_Util_die(char *, ...);
size_t chaz_Util_flength(int /*<<< orphan*/ *const);
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *const);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(char *, int, size_t, int /*<<< orphan*/ *const);
scalar_t__ malloc(size_t);
int strerror(int /*<<< orphan*/);
char *chaz_Util_slurp_file(const char *file_path, size_t *len_ptr) {
  FILE *const file = fopen(file_path, "rb");
  char *contents;
  size_t len;
  long check_val;
  if (file == NULL) {
    chaz_Util_die("Error opening file '%s': %s", file_path, strerror(errno));
  }
  len = chaz_Util_flength(file);
  if (len == 0) {
    *len_ptr = 0;
    return NULL;
  }
  contents = (char *)malloc(len * sizeof(char) + 1);
  if (contents == NULL) {
    chaz_Util_die("Out of memory at %d, %s", "src/Charmonizer/Core/Util.c", 71);
  }
  contents[len] = '\0';
  check_val = fread(contents, sizeof(char), len, file);
  if (check_val <= 0) {
    chaz_Util_die("Tried to read %d characters of '%s', got %d", (int)len,
                  file_path, check_val);
  }
  *len_ptr = check_val;
  if (fclose(file)) {
    chaz_Util_die("Error closing file '%s': %s", file_path, strerror(errno));
  }
  return contents;
}


x86 assembly (clang, -O0):
<chaz_Util_slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <chaz_Util_slurp_file+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1090 <chaz_Util_die@plt>
mov    -0x20(%rbp),%rdi
call   1050 <chaz_Util_flength@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <chaz_Util_slurp_file+0x89>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12d0 <chaz_Util_slurp_file+0x160>
mov    -0x30(%rbp),%rdi
shl    $0x0,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1233 <chaz_Util_slurp_file+0xc3>
lea    0xdff(%rip),%rdi
lea    0xe10(%rip),%rsi
mov    $0x47,%edx
mov    $0x0,%al
call   1090 <chaz_Util_die@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jg     1280 <chaz_Util_slurp_file+0x110>
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1090 <chaz_Util_die@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12c8 <chaz_Util_slurp_file+0x158>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1090 <chaz_Util_die@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaz_Util_slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b6 <chaz_Util_slurp_file+0x46>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe59(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <chaz_Util_die@plt>
mov    %r13,%rdi
call   1050 <chaz_Util_flength@plt>
test   %rax,%rax
je     1261 <chaz_Util_slurp_file+0xf1>
mov    %rax,%rbx
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f8 <chaz_Util_slurp_file+0x88>
lea    0xe3a(%rip),%rdi
lea    0xe4b(%rip),%rsi
mov    $0x47,%edx
xor    %eax,%eax
call   1090 <chaz_Util_die@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %rax,%rbp
test   %rax,%rax
jg     122e <chaz_Util_slurp_file+0xbe>
lea    0xe34(%rip),%rdi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1090 <chaz_Util_die@plt>
mov    %rbp,(%r14)
mov    %r13,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     126b <chaz_Util_slurp_file+0xfb>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2c(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <chaz_Util_die@plt>
jmp    126b <chaz_Util_slurp_file+0xfb>
movq   $0x0,(%r14)
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Error opening file '%s': %s\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Out of memory at %d, %s\00"
@.str.3 = [28 x i8] c"src/Charmonizer/Core/Util.c\00"
@.str.4 = [44 x i8] c"Tried to read %d characters of '%s', got %d\00"
@.str.5 = [28 x i8] c"Error closing file '%s': %s\00"
define i8* @chaz_Util_slurp_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%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 eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %17)
br label %19
19: ; preds = %14, %2
%20 = load i32*, i32** %6
%21 = call i64 @chaz_Util_flength(i32* %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i64*, i64** %5
store i64 0, i64* %25
store i8* null, i8** %3
br label %65
26: ; preds = %19
%27 = load i64, i64* %8
%28 = mul i64 %27, 1
%29 = add i64 %28, 1
%30 = call i64 @malloc(i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 71)
br label %36
36: ; preds = %34, %26
%37 = load i8*, i8** %7
%38 = load i64, i64* %8
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %7
%41 = load i64, i64* %8
%42 = load i32*, i32** %6
%43 = call i64 @fread(i8* %40, i32 1, i64 %41, i32* %42)
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %36
%47 = load i64, i64* %8
%48 = trunc i64 %47 to i32
%49 = load i8*, i8** %4
%50 = load i64, i64* %9
%51 = call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %48, i8* %49, i64 %50)
br label %52
52: ; preds = %46, %36
%53 = load i64, i64* %9
%54 = load i64*, i64** %5
store i64 %53, i64* %54
%55 = load i32*, i32** %6
%56 = call i64 @fclose(i32* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %52
%59 = load i8*, i8** %4
%60 = load i32, i32* @errno
%61 = call i32 @strerror(i32 %60)
%62 = call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %61)
br label %63
63: ; preds = %58, %52
%64 = load i8*, i8** %7
store i8* %64, i8** %3
br label %65
65: ; preds = %63, %24
%66 = load i8*, i8** %3
ret i8* %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Error opening file '%s': %s\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Out of memory at %d, %s\00"
@.str.3 = [28 x i8] c"src/Charmonizer/Core/Util.c\00"
@.str.4 = [44 x i8] c"Tried to read %d characters of '%s', got %d\00"
@.str.5 = [28 x i8] c"Error closing file '%s': %s\00"
define i8* @chaz_Util_slurp_file(i8* %0, i64* nocapture 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 %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %7)
br label %9
9: ; preds = %5, %2
%10 = tail call i64 @chaz_Util_flength(i32* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i64 0, i64* %1
br label %34
13: ; preds = %9
%14 = add i64 %10, 1
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 71)
br label %20
20: ; preds = %18, %13
%21 = getelementptr inbounds i8, i8* %16, i64 %10
store i8 0, i8* %21
%22 = tail call i64 @fread(i8* %16, i32 1, i64 %10, i32* %3)
%23 = icmp slt i64 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = trunc i64 %10 to i32
%26 = tail call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %25, i8* %0, i64 %22)
br label %27
27: ; preds = %24, %20
store i64 %22, i64* %1
%28 = tail call i64 @fclose(i32* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = tail call i32 @strerror(i32 %31)
%33 = tail call i32 (i8*, ...) @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %32)
br label %34
34: ; preds = %27, %30, %12
%35 = phi i8* [ null, %12 ], [ %16, %30 ], [ %16, %27 ]
ret i8* %35
}