#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int filesize(char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printerr(char *, ...);
int /*<<< orphan*/ printerrno(char *, ...);
unsigned char *load_from_file(const char *filename, unsigned char *user_buffer,
                              int *size) {
  FILE *f;
  int fsize;
  unsigned char *buffer = user_buffer;
  if ((fsize = filesize(filename)) < 0)
    return NULL;
  if (*size == 0)
    *size = fsize;
  if (*size > fsize) {
    printerr("Problem during size check for file %s (requested size %i < file "
             "size %i)\n",
             filename, *size, fsize);
    return NULL;
  }
  if (!buffer && (buffer = (unsigned char *)malloc(*size)) == NULL) {
    printerrno("malloc(%i) for file '%s' loading", *size, filename);
    return NULL;
  }
  if ((f = fopen(filename, "rb")) == NULL) {
    printerrno("fopen(%s)", filename);
    if (!user_buffer)
      free(buffer);
    return NULL;
  }
  if (fread(buffer, *size, 1, f) != 1) {
    if (ferror(f))
      printerrno("read(%s)", filename);
    else
      printerr("Could not read %i bytes from file %s\n", fsize, filename);
    if (!user_buffer)
      free(buffer);
    return NULL;
  }
  fclose(f);
  return buffer;
}


x86 assembly (clang, -O0):
<load_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <filesize@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11ce <load_from_file+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    1347 <load_from_file+0x1b7>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11e4 <load_from_file+0x54>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
jle    121b <load_from_file+0x8b>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%ecx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1070 <printerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    1347 <load_from_file+0x1b7>
cmpq   $0x0,-0x38(%rbp)
jne    1264 <load_from_file+0xd4>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1264 <load_from_file+0xd4>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1080 <printerrno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1347 <load_from_file+0x1b7>
mov    -0x10(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12b5 <load_from_file+0x125>
mov    -0x10(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1080 <printerrno@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12a8 <load_from_file+0x118>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1347 <load_from_file+0x1b7>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1336 <load_from_file+0x1a6>
mov    -0x28(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1300 <load_from_file+0x170>
mov    -0x10(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1080 <printerrno@plt>
jmp    1315 <load_from_file+0x185>
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1070 <printerr@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1329 <load_from_file+0x199>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1347 <load_from_file+0x1b7>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   10a0 <filesize@plt>
test   %eax,%eax
js     125c <load_from_file+0xcc>
mov    %eax,%ebp
mov    (%rbx),%edx
test   %edx,%edx
je     11d8 <load_from_file+0x48>
cmp    %ebp,%edx
jle    11dc <load_from_file+0x4c>
lea    0xe3c(%rip),%rdi
xor    %r15d,%r15d
mov    %r12,%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <printerr@plt>
jmp    125f <load_from_file+0xcf>
mov    %ebp,(%rbx)
mov    %ebp,%edx
mov    %r14,%r15
test   %r14,%r14
jne    11f3 <load_from_file+0x63>
mov    %edx,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1271 <load_from_file+0xe1>
lea    0xe71(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122b <load_from_file+0x9b>
mov    %rax,%r13
mov    (%rbx),%esi
mov    %r15,%rdi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %r13,%rdi
cmp    $0x1,%eax
jne    1234 <load_from_file+0xa4>
call   1060 <fclose@plt>
jmp    125f <load_from_file+0xcf>
lea    0xe3c(%rip),%rdi
jmp    1245 <load_from_file+0xb5>
call   1040 <ferror@plt>
test   %rax,%rax
je     1289 <load_from_file+0xf9>
lea    0xe33(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1080 <printerrno@plt>
test   %r14,%r14
jne    125c <load_from_file+0xcc>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%esi
lea    0xdd0(%rip),%rdi
xor    %r15d,%r15d
mov    %r12,%rdx
xor    %eax,%eax
call   1080 <printerrno@plt>
jmp    125f <load_from_file+0xcf>
lea    0xdf1(%rip),%rdi
mov    %ebp,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <printerr@plt>
test   %r14,%r14
jne    125c <load_from_file+0xcc>
jmp    1254 <load_from_file+0xc4>

LLVM IR (-O0):
@.str = [74 x i8] c"Problem during size check for file %s (requested size %i < file size %i)\0A\00"
@.str.1 = [33 x i8] c"malloc(%i) for file '%s' loading\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [10 x i8] c"fopen(%s)\00"
@.str.4 = [9 x i8] c"read(%s)\00"
@.str.5 = [38 x i8] c"Could not read %i bytes from file %s\0A\00"
define i8* @load_from_file(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i8*, i8** %5
%13 = call i32 @filesize(i8* %12)
store i32 %13, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* null, i8** %4
br label %90
16: ; preds = %3
%17 = load i32*, i32** %7
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %9
%22 = load i32*, i32** %7
store i32 %21, i32* %22
br label %23
23: ; preds = %20, %16
%24 = load i32*, i32** %7
%25 = load i32, i32* %24
%26 = load i32, i32* %9
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load i32*, i32** %7
%31 = load i32, i32* %30
%32 = load i32, i32* %9
%33 = call i32 (i8*, ...) @printerr(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %29, i32 %31, i32 %32)
store i8* null, i8** %4
br label %90
34: ; preds = %23
%35 = load i8*, i8** %10
%36 = icmp ne i8* %35, null
br i1 %36, label %48, label %37
37: ; preds = %34
%38 = load i32*, i32** %7
%39 = load i32, i32* %38
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %10
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = load i8*, i8** %5
%47 = call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %45, i8* %46)
store i8* null, i8** %4
br label %90
48: ; preds = %37, %34
%49 = load i8*, i8** %5
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %50, i32** %8
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %61
52: ; preds = %48
%53 = load i8*, i8** %5
%54 = call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %6
%56 = icmp ne i8* %55, null
br i1 %56, label %60, label %57
57: ; preds = %52
%58 = load i8*, i8** %10
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %57, %52
store i8* null, i8** %4
br label %90
61: ; preds = %48
%62 = load i8*, i8** %10
%63 = load i32*, i32** %7
%64 = load i32, i32* %63
%65 = load i32*, i32** %8
%66 = call i32 @fread(i8* %62, i32 %64, i32 1, i32* %65)
%67 = icmp ne i32 %66, 1
br i1 %67, label %68, label %86
68: ; preds = %61
%69 = load i32*, i32** %8
%70 = call i64 @ferror(i32* %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %68
%73 = load i8*, i8** %5
%74 = call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %73)
br label %79
75: ; preds = %68
%76 = load i32, i32* %9
%77 = load i8*, i8** %5
%78 = call i32 (i8*, ...) @printerr(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %76, i8* %77)
br label %79
79: ; preds = %75, %72
%80 = load i8*, i8** %6
%81 = icmp ne i8* %80, null
br i1 %81, label %85, label %82
82: ; preds = %79
%83 = load i8*, i8** %10
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %82, %79
store i8* null, i8** %4
br label %90
86: ; preds = %61
%87 = load i32*, i32** %8
%88 = call i32 @fclose(i32* %87)
%89 = load i8*, i8** %10
store i8* %89, i8** %4
br label %90
90: ; preds = %86, %85, %60, %43, %28, %15
%91 = load i8*, i8** %4
ret i8* %91
}

LLVM IR (-O2):
@.str = [74 x i8] c"Problem during size check for file %s (requested size %i < file size %i)\0A\00"
@.str.1 = [33 x i8] c"malloc(%i) for file '%s' loading\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [10 x i8] c"fopen(%s)\00"
@.str.4 = [9 x i8] c"read(%s)\00"
@.str.5 = [38 x i8] c"Could not read %i bytes from file %s\0A\00"
define i8* @load_from_file(i8* %0, i8* %1, i32* nocapture %2) {
%4 = tail call i32 @filesize(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %48, label %6
6: ; preds = %3
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %6
store i32 %4, i32* %2
br label %14
10: ; preds = %6
%11 = icmp sgt i32 %7, %4
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 (i8*, ...) @printerr(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7, i32 %4)
br label %48
14: ; preds = %9, %10
%15 = phi i32 [ %4, %9 ], [ %7, %10 ]
%16 = icmp eq i8* %1, null
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = tail call i64 @malloc(i32 %15)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %2
%23 = tail call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %0)
br label %48
24: ; preds = %17, %14
%25 = phi i8* [ %1, %14 ], [ %19, %17 ]
%26 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = tail call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br i1 %16, label %30, label %48
30: ; preds = %28
%31 = tail call i32 @free(i8* %25)
br label %48
32: ; preds = %24
%33 = load i32, i32* %2
%34 = tail call i32 @fread(i8* %25, i32 %33, i32 1, i32* %26)
%35 = icmp eq i32 %34, 1
br i1 %35, label %46, label %36
36: ; preds = %32
%37 = tail call i64 @ferror(i32* %26)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = tail call i32 (i8*, ...) @printerrno(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %43
41: ; preds = %36
%42 = tail call i32 (i8*, ...) @printerr(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %4, i8* %0)
br label %43
43: ; preds = %41, %39
br i1 %16, label %44, label %48
44: ; preds = %43
%45 = tail call i32 @free(i8* %25)
br label %48
46: ; preds = %32
%47 = tail call i32 @fclose(i32* %26)
br label %48
48: ; preds = %43, %44, %28, %30, %3, %46, %21, %12
%49 = phi i8* [ null, %12 ], [ %25, %46 ], [ null, %21 ], [ null, %3 ], [ null, %30 ], [ null, %28 ], [ null, %44 ], [ null, %43 ]
ret i8* %49
}