#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 size_t off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ file_exists(char const *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
int file_get_contents(const char *filename, size_t filesize, char *ret,
                      off_t length) {
  if (!file_exists(filename) || access(filename, R_OK) != 0)
    return -1;
  int fd;
  char buf[filesize];
  if ((fd = open(filename, O_RDONLY)) == -1)
    return -1;
  length = (length > 0 ? length : filesize);
  read(fd, buf, length);
  strcpy(ret, buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <file_exists@plt>
cmp    $0x0,%eax
je     11a6 <file_get_contents+0x46>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     11b2 <file_get_contents+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <file_get_contents+0x10c>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    120a <file_get_contents+0xaa>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1265 <file_get_contents+0x105>
cmpq   $0x0,-0x28(%rbp)
jbe    1222 <file_get_contents+0xc2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    122a <file_get_contents+0xca>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rdi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
call   1040 <file_exists@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
je     1202 <file_get_contents+0xa2>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
jne    1202 <file_get_contents+0xa2>
mov    %rsp,-0x30(%rbp)
mov    %rsp,%r13
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f8 <file_get_contents+0x98>
mov    %eax,%ebx
test   %r15,%r15
cmovne %r15,%r12
mov    %eax,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <read@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %r13d,%r13d
jmp    11fe <file_get_contents+0x9e>
mov    $0xffffffff,%r13d
mov    -0x30(%rbp),%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@R_OK = global i32 0
@O_RDONLY = global i32 0
define i32 @file_get_contents(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%14 = load i8*, i8** %6
%15 = call i32 @file_exists(i8* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = load i32, i32* @R_OK
%20 = call i64 @access(i8* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17, %4
store i32 -1, i32* %5
br label %50
23: ; preds = %17
%24 = load i64, i64* %7
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %11
%26 = alloca i8, i64 %24
store i64 %24, i64* %12
%27 = load i8*, i8** %6
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %10
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %23
store i32 -1, i32* %5
store i32 1, i32* %13
br label %48
32: ; preds = %23
%33 = load i64, i64* %9
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i64, i64* %9
br label %39
37: ; preds = %32
%38 = load i64, i64* %7
br label %39
39: ; preds = %37, %35
%40 = phi i64 [ %36, %35 ], [ %38, %37 ]
store i64 %40, i64* %9
%41 = load i32, i32* %10
%42 = load i64, i64* %9
%43 = call i32 @read(i32 %41, i8* %26, i64 %42)
%44 = load i8*, i8** %8
%45 = call i32 @strcpy(i8* %44, i8* %26)
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
store i32 0, i32* %5
store i32 1, i32* %13
br label %48
48: ; preds = %39, %31
%49 = load i8*, i8** %11
br label %50
50: ; preds = %48, %22
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@R_OK = global i32 0
@O_RDONLY = global i32 0
define i32 @file_get_contents(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = tail call i32 @file_exists(i8* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %4
%8 = load i32, i32* @R_OK
%9 = tail call i64 @access(i8* %0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %7
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %1
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = icmp eq i64 %3, 0
%19 = select i1 %18, i64 %1, i64 %3
%20 = call i32 @read(i32 %15, i8* %13, i64 %19)
%21 = call i32 @strcpy(i8* %2, i8* %13)
%22 = call i32 @close(i32 %15)
br label %23
23: ; preds = %11, %17
%24 = phi i32 [ 0, %17 ], [ -1, %11 ]
br label %25
25: ; preds = %4, %7, %23
%26 = phi i32 [ %24, %23 ], [ -1, %7 ], [ -1, %4 ]
ret i32 %26
}