#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int load_file(const char *filename, char *buffer, int buffer_len) {
  int fd, length, r;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Cannot open \"%s\": %s\n", filename, strerror(errno));
    return -1;
  }
  length = 0;
  while (1) {
    r = read(fd, &buffer[length], buffer_len - length);
    if (r == 0)
      break;
    else if (r > 0)
      length += r;
    else {
      fprintf(stderr, "Cannot read \"%s\": %s\n", filename, strerror(errno));
      close(fd);
      return -1;
    }
    if (length == buffer_len) {
      fprintf(stderr, "Out of buffer space reading \"%s\"\n", filename);
      close(fd);
      return -1;
    }
  }
  close(fd);
  return length;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c8 <load_file+0x78>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <load_file+0x163>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11fa <load_file+0xaa>
jmp    12a5 <load_file+0x155>
cmpl   $0x0,-0x28(%rbp)
jle    1212 <load_file+0xc2>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1260 <load_file+0x110>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <load_file+0x163>
jmp    1265 <load_file+0x115>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12a0 <load_file+0x150>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <load_file+0x163>
jmp    11cf <load_file+0x7f>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11bb <load_file+0x6b>
mov    %eax,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r12,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1040 <read@plt>
test   %eax,%eax
je     11ea <load_file+0x9a>
jle    11f4 <load_file+0xa4>
add    %eax,%ebx
cmp    %ebp,%ebx
jne    1180 <load_file+0x30>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1221 <load_file+0xd1>
mov    0x2e16(%rip),%rax
mov    (%rax),%ebx
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe27(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1229 <load_file+0xd9>
mov    %r15d,%edi
call   1030 <close@plt>
jmp    122e <load_file+0xde>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15d,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot open \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Cannot read \22%s\22: %s\0A\00"
@.str.2 = [34 x i8] c"Out of buffer space reading \22%s\22\0A\00"
define i32 @load_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %5
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
store i32 -1, i32* %4
br label %67
22: ; preds = %3
store i32 0, i32* %9
br label %23
23: ; preds = %22, %62
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %7
%30 = load i32, i32* %9
%31 = sub nsw i32 %29, %30
%32 = call i32 @read(i32 %24, i8* %28, i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %23
br label %63
36: ; preds = %23
%37 = load i32, i32* %10
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* %10
%41 = load i32, i32* %9
%42 = add nsw i32 %41, %40
store i32 %42, i32* %9
br label %51
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %5
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %47)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %4
br label %67
51: ; preds = %39
br label %52
52: ; preds = %51
%53 = load i32, i32* %9
%54 = load i32, i32* %7
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %5
%59 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %4
br label %67
62: ; preds = %52
br label %23
63: ; preds = %35
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %9
store i32 %66, i32* %4
br label %67
67: ; preds = %63, %56, %43, %16
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot open \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Cannot read \22%s\22: %s\0A\00"
@.str.2 = [34 x i8] c"Out of buffer space reading \22%s\22\0A\00"
define i32 @load_file(i8* %0, i8* nocapture %1, i32 %2) {
%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 %12
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %36
12: ; preds = %3, %21
%13 = phi i32 [ %22, %21 ], [ 0, %3 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = sub nsw i32 %2, %13
%17 = tail call i32 @read(i32 %5, i8* %15, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %34, label %19
19: ; preds = %12
%20 = icmp sgt i32 %17, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = add nsw i32 %17, %13
%23 = icmp eq i32 %22, %2
br i1 %23, label %30, label %12
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %27)
%29 = tail call i32 @close(i32 %5)
br label %36
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%33 = tail call i32 @close(i32 %5)
br label %36
34: ; preds = %12
%35 = tail call i32 @close(i32 %5)
br label %36
36: ; preds = %34, %30, %24, %7
%37 = phi i32 [ -1, %7 ], [ %13, %34 ], [ -1, %30 ], [ -1, %24 ]
ret i32 %37
}