#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 const *, char *);
scalar_t__ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int file_get_contents(const char *path, char **contents, long *length,
                      void *error) {
  FILE *file;
  long size;
  char *buffer;
  file = fopen(path, "rb");
  if (!file)
    return -1;
  fseek(file, 0, SEEK_END);
  *length = size = ftell(file);
  rewind(file);
  buffer = malloc(size + 8);
  if (!buffer) {
    fclose(file);
    return -1;
  }
  size -= fread(buffer, 1, size, file);
  if (size) {
    fclose(file);
    free(buffer);
    return -1;
  }
  fclose(file);
  *contents = buffer;
  return 0;
}


x86 assembly (clang, -O0):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <file_get_contents+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <file_get_contents+0x110>
mov    -0x30(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x38(%rbp),%rdi
add    $0x8,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1228 <file_get_contents+0xa8>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <file_get_contents+0x110>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1275 <file_get_contents+0xf5>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <file_get_contents+0x110>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1223 <file_get_contents+0xa3>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
mov    %rax,(%r12)
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x8(%rbp),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1211 <file_get_contents+0x91>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r13,%rbp
jne    121b <file_get_contents+0x9b>
mov    %r12,(%r14)
xor    %r15d,%r15d
jmp    1223 <file_get_contents+0xa3>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1223 <file_get_contents+0xa3>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
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"
@SEEK_END = global i32 0
define i32 @file_get_contents(i8* %0, i8** %1, i64* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %4
store i32 -1, i32* %5
br label %54
18: ; preds = %4
%19 = load i32*, i32** %10
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %10
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %11
%24 = load i64*, i64** %8
store i64 %23, i64* %24
%25 = load i32*, i32** %10
%26 = call i32 @rewind(i32* %25)
%27 = load i64, i64* %11
%28 = add nsw i64 %27, 8
%29 = call i8* @malloc(i64 %28)
store i8* %29, i8** %12
%30 = load i8*, i8** %12
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %18
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
store i32 -1, i32* %5
br label %54
35: ; preds = %18
%36 = load i8*, i8** %12
%37 = load i64, i64* %11
%38 = load i32*, i32** %10
%39 = call i64 @fread(i8* %36, i32 1, i64 %37, i32* %38)
%40 = load i64, i64* %11
%41 = sub nsw i64 %40, %39
store i64 %41, i64* %11
%42 = load i64, i64* %11
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %35
%45 = load i32*, i32** %10
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %12
%48 = call i32 @free(i8* %47)
store i32 -1, i32* %5
br label %54
49: ; preds = %35
%50 = load i32*, i32** %10
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %12
%53 = load i8**, i8*** %7
store i8* %52, i8** %53
store i32 0, i32* %5
br label %54
54: ; preds = %49, %44, %32, %17
%55 = load i32, i32* %5
ret i32 %55
}

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