#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 uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
FILE *sys_fopen_read(const char *fname, uint32_t *s) {
  FILE *fd = fopen(fname, "rb");
  uint32_t size;
  if (fd == NULL) {
    fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno));
    return NULL;
  }
  fseek(fd, 0, SEEK_END);
  size = ftell(fd);
  if (size >= 2 * 1048576) {
    fprintf(stderr, "Size of file (%d) exceeds MAX_FILE_SIZE(%d)\n", size,
            2 * 1048576);
    fclose(fd);
    return NULL;
  }
  rewind(fd);
  *s = size;
  return fd;
}


x86 assembly (clang, -O0):
<sys_fopen_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e6 <sys_fopen_read+0x76>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <sys_fopen_read+0xf2>
mov    -0x20(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x200000,-0x24(%rbp)
jl     1248 <sys_fopen_read+0xd8>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xdf9(%rip),%rsi
mov    $0x200000,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <sys_fopen_read+0xf2>
mov    -0x20(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_fopen_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11df <sys_fopen_read+0x6f>
mov    %rax,%rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
cmp    $0x200000,%eax
jl     1210 <sys_fopen_read+0xa0>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
mov    %ebp,%edx
mov    $0x200000,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    121b <sys_fopen_read+0xab>
mov    0x2df2(%rip),%rax
mov    (%rax),%ebp
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe06(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    121b <sys_fopen_read+0xab>
mov    %rbx,%rdi
call   1040 <rewind@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to open file %s: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [45 x i8] c"Size of file (%d) exceeds MAX_FILE_SIZE(%d)\0A\00"
define i32* @sys_fopen_read(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
store i32* null, i32** %3
br label %38
18: ; preds = %2
%19 = load i32*, i32** %6
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 @ftell(i32* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp sge i32 %24, 2097152
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i32, i32* %7
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 2097152)
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
store i32* null, i32** %3
br label %38
32: ; preds = %18
%33 = load i32*, i32** %6
%34 = call i32 @rewind(i32* %33)
%35 = load i32, i32* %7
%36 = load i32*, i32** %5
store i32 %35, i32* %36
%37 = load i32*, i32** %6
store i32* %37, i32** %3
br label %38
38: ; preds = %32, %26, %12
%39 = load i32*, i32** %3
ret i32* %39
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to open file %s: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [45 x i8] c"Size of file (%d) exceeds MAX_FILE_SIZE(%d)\0A\00"
define noalias i32* @sys_fopen_read(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %8)
br label %21
10: ; preds = %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i32 @ftell(i32* %3)
%14 = icmp sgt i32 %13, 2097151
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 2097152)
%18 = tail call i32 @fclose(i32* %3)
br label %21
19: ; preds = %10
%20 = tail call i32 @rewind(i32* %3)
store i32 %13, i32* %1
br label %21
21: ; preds = %19, %15, %5
%22 = phi i32* [ null, %5 ], [ null, %15 ], [ %3, %19 ]
ret i32* %22
}