#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 *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int readfile(char *path, char *buf, int maxlen) {
  FILE *infile = fopen(path, "rb");
  if (!infile) {
    printf("Can't open file '%s'\n", path);
    return -1;
  }
  fseek(infile, 0, SEEK_END);
  int iflen = ftell(infile);
  rewind(infile);
  if (iflen <= maxlen) {
    fread(buf, iflen, 1, infile);
  } else {
    printf("File '%s' is too long (max is 0x%X bytes)\n", path, maxlen);
    iflen = -2;
  }
  fclose(infile);
  return iflen;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <readfile+0x50>
mov    -0x10(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <readfile+0xca>
mov    -0x28(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     120f <readfile+0x9f>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
jmp    122b <readfile+0xbb>
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11da <readfile+0x6a>
mov    %rax,%rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <rewind@plt>
cmp    %r12d,%ebp
jle    11f2 <readfile+0x82>
lea    0xe53(%rip),%rdi
mov    %r14,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffe,%ebp
jmp    1204 <readfile+0x94>
lea    0xe22(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    120c <readfile+0x9c>
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"Can't open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [43 x i8] c"File '%s' is too long (max is 0x%X bytes)\0A\00"
define i32 @readfile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %4
br label %41
17: ; preds = %3
%18 = load i32*, i32** %8
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %8
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %9
%23 = load i32*, i32** %8
%24 = call i32 @rewind(i32* %23)
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %17
%29 = load i8*, i8** %6
%30 = load i32, i32* %9
%31 = load i32*, i32** %8
%32 = call i32 @fread(i8* %29, i32 %30, i32 1, i32* %31)
br label %37
33: ; preds = %17
%34 = load i8*, i8** %5
%35 = load i32, i32* %7
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %35)
store i32 -2, i32* %9
br label %37
37: ; preds = %33, %28
%38 = load i32*, i32** %8
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* %9
store i32 %40, i32* %4
br label %41
41: ; preds = %37, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"Can't open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [43 x i8] c"File '%s' is too long (max is 0x%X bytes)\0A\00"
define i32 @readfile(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %4)
%12 = tail call i32 @rewind(i32* %4)
%13 = icmp sgt i32 %11, %2
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @fread(i8* %1, i32 %11, i32 1, i32* %4)
br label %18
16: ; preds = %8
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %2)
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %11, %14 ], [ -2, %16 ]
%20 = tail call i32 @fclose(i32* %4)
br label %21
21: ; preds = %18, %6
%22 = phi i32 [ %19, %18 ], [ -1, %6 ]
ret i32 %22
}