#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logmsg(int, char *, ...);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int bnl_loadallocfiletomem(char *filename, char **data, int *datalen) {
  char rbuf[512];
  int r;
  FILE *f;
  f = fopen(filename, "r");
  if (!f) {
    logmsg(5, "Could not open file %s: %s", filename, strerror(errno));
    return 1;
  }
  *data = NULL;
  *datalen = 0;
  while ((r = fread(rbuf, 1, 512, f)) > 0) {
    *data = realloc(*data, *datalen + r);
    memcpy(*data + *datalen, rbuf, r);
    *datalen += r;
  }
  if (!feof(f)) {
    fclose(f);
    logmsg(5, "Error reading from file.");
    free(*data);
    return 1;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<bnl_loadallocfiletomem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    120d <bnl_loadallocfiletomem+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x238(%rbp),%rdx
mov    %eax,%ecx
mov    $0x5,%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1090 <logmsg@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <bnl_loadallocfiletomem+0x175>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x220(%rbp),%rdi
mov    -0x230(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x0,%eax
jle    12a6 <bnl_loadallocfiletomem+0x116>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
add    -0x224(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x224(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    1222 <bnl_loadallocfiletomem+0x92>
mov    -0x230(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
jne    12f2 <bnl_loadallocfiletomem+0x162>
mov    -0x230(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x5,%edi
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1090 <logmsg@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <bnl_loadallocfiletomem+0x175>
mov    -0x230(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bnl_loadallocfiletomem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1261 <bnl_loadallocfiletomem+0xd1>
mov    %rax,%r15
movq   $0x0,(%r14)
movl   $0x0,(%rbx)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    122c <bnl_loadallocfiletomem+0x9c>
mov    %eax,%ebp
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rdi
mov    (%rbx),%esi
add    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,(%r14)
movslq (%rbx),%rdi
add    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
add    %ebp,(%rbx)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11f0 <bnl_loadallocfiletomem+0x60>
mov    %r15,%rdi
call   1060 <feof@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
test   %ebx,%ebx
jne    128c <bnl_loadallocfiletomem+0xfc>
lea    0xdd2(%rip),%rsi
mov    $0x5,%edi
xor    %eax,%eax
call   1090 <logmsg@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
jmp    1287 <bnl_loadallocfiletomem+0xf7>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8c(%rip),%rsi
mov    $0x5,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <logmsg@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Could not open file %s: %s\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Error reading from file.\00"
define i32 @bnl_loadallocfiletomem(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca [512 x i8]
%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 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp ne i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @logmsg(i32 5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 %18)
store i32 1, i32* %4
br label %64
20: ; preds = %3
%21 = load i8**, i8*** %6
store i8* null, i8** %21
%22 = load i32*, i32** %7
store i32 0, i32* %22
br label %23
23: ; preds = %28, %20
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%25 = load i32*, i32** %10
%26 = call i32 @fread(i8* %24, i32 1, i32 512, i32* %25)
store i32 %26, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %23
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = load i32, i32* %9
%34 = add nsw i32 %32, %33
%35 = call i8* @realloc(i8* %30, i32 %34)
%36 = load i8**, i8*** %6
store i8* %35, i8** %36
%37 = load i8**, i8*** %6
%38 = load i8*, i8** %37
%39 = load i32*, i32** %7
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
%46 = load i32, i32* %9
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = add nsw i32 %48, %46
store i32 %49, i32* %47
br label %23
50: ; preds = %23
%51 = load i32*, i32** %10
%52 = call i32 @feof(i32* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %61, label %54
54: ; preds = %50
%55 = load i32*, i32** %10
%56 = call i32 @fclose(i32* %55)
%57 = call i32 (i32, i8*, ...) @logmsg(i32 5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%58 = load i8**, i8*** %6
%59 = load i8*, i8** %58
%60 = call i32 @free(i8* %59)
store i32 1, i32* %4
br label %64
61: ; preds = %50
%62 = load i32*, i32** %10
%63 = call i32 @fclose(i32* %62)
store i32 0, i32* %4
br label %64
64: ; preds = %61, %54, %15
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Could not open file %s: %s\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Error reading from file.\00"
define i32 @bnl_loadallocfiletomem(i8* %0, i8** nocapture %1, i32* nocapture %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @logmsg(i32 5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %10)
br label %37
12: ; preds = %3
store i8* null, i8** %1
store i32 0, i32* %2
%13 = call i32 @fread(i8* %5, i32 1, i32 512, i32* %6)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12, %15
%16 = phi i32 [ %27, %15 ], [ %13, %12 ]
%17 = load i8*, i8** %1
%18 = load i32, i32* %2
%19 = add nsw i32 %18, %16
%20 = call i8* @realloc(i8* %17, i32 %19)
store i8* %20, i8** %1
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @memcpy(i8* %23, i8* %5, i32 %16)
%25 = load i32, i32* %2
%26 = add nsw i32 %25, %16
store i32 %26, i32* %2
%27 = call i32 @fread(i8* %5, i32 1, i32 512, i32* %6)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %15, label %29
29: ; preds = %15, %12
%30 = call i32 @feof(i32* %6)
%31 = icmp eq i32 %30, 0
%32 = call i32 @fclose(i32* %6)
br i1 %31, label %33, label %37
33: ; preds = %29
%34 = call i32 (i32, i8*, ...) @logmsg(i32 5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%35 = load i8*, i8** %1
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %29, %33, %8
%38 = phi i32 [ 1, %33 ], [ 1, %8 ], [ 0, %29 ]
ret i32 %38
}