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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int SIZE_MAX;
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ mp_msg(int, int, char *, ...);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
char *load_file(const char *filename, off_t *length) {
  int fd;
  char *buffer;
  mp_msg(31, 6, "Loading cookie file: %s\n", filename);
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    mp_msg(31, 6, "Could not open");
    return NULL;
  }
  *length = lseek(fd, 0, SEEK_END);
  if (*length < 0) {
    mp_msg(31, 6, "Could not find EOF");
    return NULL;
  }
  if (*length > SIZE_MAX - 1) {
    mp_msg(31, 6, "File too big, could not malloc.");
    return NULL;
  }
  lseek(fd, SEEK_SET, 0);
  if (!(buffer = malloc(*length + 1))) {
    mp_msg(31, 6, "Could not malloc.");
    return NULL;
  }
  if (read(fd, buffer, *length) != *length) {
    mp_msg(31, 6, "Read is behaving funny.");
    return NULL;
  }
  close(fd);
  buffer[*length] = 0;
  return buffer;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xe7b(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d0 <load_file+0x70>
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xe5d(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1312 <load_file+0x1b2>
mov    -0x1c(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    121d <load_file+0xbd>
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xe1f(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1312 <load_file+0x1b2>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    125c <load_file+0xfc>
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xdf3(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1312 <load_file+0x1b2>
mov    -0x1c(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12b0 <load_file+0x150>
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xdbf(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1312 <load_file+0x1b2>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     12f3 <load_file+0x193>
mov    $0x1f,%edi
mov    $0x6,%esi
lea    0xd8e(%rip),%rdx
mov    $0x0,%al
call   1070 <mp_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1312 <load_file+0x1b2>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe8c(%rip),%rdx
mov    $0x1f,%edi
mov    $0x6,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <mp_msg@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     120b <load_file+0xab>
mov    %eax,%ebp
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     1214 <load_file+0xb4>
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%eax
jge    121d <load_file+0xbd>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lseek@plt>
mov    (%r14),%r15d
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1226 <load_file+0xc6>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
cmp    (%r14),%eax
jne    122f <load_file+0xcf>
mov    %ebp,%edi
call   1040 <close@plt>
movslq (%r14),%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1249 <load_file+0xe9>
lea    0xe07(%rip),%rdx
jmp    1236 <load_file+0xd6>
lea    0xe0d(%rip),%rdx
jmp    1236 <load_file+0xd6>
lea    0xe17(%rip),%rdx
jmp    1236 <load_file+0xd6>
lea    0xe2e(%rip),%rdx
jmp    1236 <load_file+0xd6>
lea    0xe37(%rip),%rdx
xor    %ebx,%ebx
mov    $0x1f,%edi
mov    $0x6,%esi
xor    %eax,%eax
call   1070 <mp_msg@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Loading cookie file: %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Could not open\00"
@SEEK_END = global i32 0
@.str.2 = [19 x i8] c"Could not find EOF\00"
@SIZE_MAX = global i32 0
@.str.3 = [32 x i8] c"File too big, could not malloc.\00"
@SEEK_SET = global i32 0
@.str.4 = [18 x i8] c"Could not malloc.\00"
@.str.5 = [24 x i8] c"Read is behaving funny.\00"
define i8* @load_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %66
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
%21 = load i32*, i32** %5
store i32 %20, i32* %21
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %66
27: ; preds = %17
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = load i32, i32* @SIZE_MAX
%31 = sub nsw i32 %30, 1
%32 = icmp sgt i32 %29, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %3
br label %66
35: ; preds = %27
%36 = load i32, i32* %6
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %36, i32 %37, i32 0)
%39 = load i32*, i32** %5
%40 = load i32, i32* %39
%41 = add nsw i32 %40, 1
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %7
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %35
%45 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %3
br label %66
46: ; preds = %35
%47 = load i32, i32* %6
%48 = load i8*, i8** %7
%49 = load i32*, i32** %5
%50 = load i32, i32* %49
%51 = call i32 @read(i32 %47, i8* %48, i32 %50)
%52 = load i32*, i32** %5
%53 = load i32, i32* %52
%54 = icmp ne i32 %51, %53
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %3
br label %66
57: ; preds = %46
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %7
%61 = load i32*, i32** %5
%62 = load i32, i32* %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
%65 = load i8*, i8** %7
store i8* %65, i8** %3
br label %66
66: ; preds = %57, %55, %44, %33, %25, %15
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
@.str = [25 x i8] c"Loading cookie file: %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Could not open\00"
@SEEK_END = global i32 0
@.str.2 = [19 x i8] c"Could not find EOF\00"
@SIZE_MAX = global i32 0
@.str.3 = [32 x i8] c"File too big, could not malloc.\00"
@SEEK_SET = global i32 0
@.str.4 = [18 x i8] c"Could not malloc.\00"
@.str.5 = [24 x i8] c"Read is behaving funny.\00"
define noalias i8* @load_file(i8* %0, i32* nocapture %1) {
%3 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %40
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @lseek(i32 %5, i32 0, i32 %10)
store i32 %11, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %40
15: ; preds = %9
%16 = load i32, i32* @SIZE_MAX
%17 = icmp slt i32 %11, %16
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %40
20: ; preds = %15
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %5, i32 %21, i32 0)
%23 = load i32, i32* %1
%24 = add nsw i32 %23, 1
%25 = tail call i8* @malloc(i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %40
29: ; preds = %20
%30 = tail call i32 @read(i32 %5, i8* %25, i32 %23)
%31 = load i32, i32* %1
%32 = icmp eq i32 %30, %31
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = tail call i32 (i32, i32, i8*, ...) @mp_msg(i32 31, i32 6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %40
35: ; preds = %29
%36 = tail call i32 @close(i32 %5)
%37 = load i32, i32* %1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %25, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %35, %33, %27, %18, %13, %7
%41 = phi i8* [ null, %7 ], [ null, %13 ], [ null, %18 ], [ null, %33 ], [ %25, %35 ], [ null, %27 ]
ret i8* %41
}