#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
char *read_file2mem(const char *name, size_t *sz, int nulterm) {
  long n;
  size_t len;
  char *buf;
  FILE *f;
  f = fopen(name, "rb");
  if (f == NULL) {
    return NULL;
  }
  if (fseek(f, 0, SEEK_END) < 0) {
    fclose(f);
    return NULL;
  }
  n = ftell(f);
  if (n < 0) {
    fclose(f);
    return NULL;
  }
  if (n == 0 && nulterm) {
    fclose(f);
    buf = malloc(1);
    if (buf == NULL) {
      return NULL;
    }
    buf[0] = 0;
    return buf;
  }
  if (fseek(f, 0, SEEK_SET) < 0) {
    fclose(f);
    return NULL;
  }
  len = nulterm ? n + 1 : n;
  buf = malloc(len);
  if (buf == NULL) {
    fclose(f);
    return NULL;
  }
  if (fread(buf, n, 1, f) < 1) {
    fclose(f);
    free(buf);
    return NULL;
  }
  fclose(f);
  if (nulterm) {
    buf[n] = 0;
  }
  if (sz != NULL) {
    *sz = n;
  }
  return buf;
}


x86 assembly (clang, -O0):
<read_file2mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11af <read_file2mem+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x40(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    11e3 <read_file2mem+0x73>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x40(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1211 <read_file2mem+0xa1>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
cmpq   $0x0,-0x28(%rbp)
jne    1269 <read_file2mem+0xf9>
cmpl   $0x0,-0x1c(%rbp)
je     1269 <read_file2mem+0xf9>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1255 <read_file2mem+0xe5>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x40(%rbp),%rdi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    129d <read_file2mem+0x12d>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
cmpl   $0x0,-0x1c(%rbp)
je     12b8 <read_file2mem+0x148>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    12c0 <read_file2mem+0x150>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f6 <read_file2mem+0x186>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jge    1334 <read_file2mem+0x1c4>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1371 <read_file2mem+0x201>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1353 <read_file2mem+0x1e3>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     1369 <read_file2mem+0x1f9>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file2mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1262 <read_file2mem+0xf2>
mov    %rax,%rbx
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     125a <read_file2mem+0xea>
mov    %rbx,%rdi
call   1060 <ftell@plt>
test   %rax,%rax
js     125a <read_file2mem+0xea>
mov    %rax,%r12
test   %r15d,%r15d
je     11f2 <read_file2mem+0x82>
test   %r12,%r12
jne    11f2 <read_file2mem+0x82>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1262 <read_file2mem+0xf2>
mov    %rax,%rbp
movb   $0x0,(%rax)
jmp    1264 <read_file2mem+0xf4>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     125a <read_file2mem+0xea>
cmp    $0x1,%r15d
mov    %r12,%rdi
sbb    $0xffffffffffffffff,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     125a <read_file2mem+0xea>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %r13d,%r13d
jle    1276 <read_file2mem+0x106>
test   %r15d,%r15d
je     1250 <read_file2mem+0xe0>
movb   $0x0,0x0(%rbp,%r12,1)
test   %r14,%r14
je     1264 <read_file2mem+0xf4>
mov    %r12,(%r14)
jmp    1264 <read_file2mem+0xf4>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1262 <read_file2mem+0xf2>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_file2mem(i8* %0, i64* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32*
store i8* %0, i8** %5
store i64* %1, i64** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i8* null, i8** %4
br label %103
17: ; preds = %3
%18 = load i32*, i32** %11
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32*, i32** %11
%24 = call i32 @fclose(i32* %23)
store i8* null, i8** %4
br label %103
25: ; preds = %17
%26 = load i32*, i32** %11
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32*, i32** %11
%32 = call i32 @fclose(i32* %31)
store i8* null, i8** %4
br label %103
33: ; preds = %25
%34 = load i64, i64* %8
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %36
%40 = load i32*, i32** %11
%41 = call i32 @fclose(i32* %40)
%42 = call i8* @malloc(i64 1)
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %39
store i8* null, i8** %4
br label %103
46: ; preds = %39
%47 = load i8*, i8** %10
%48 = getelementptr inbounds i8, i8* %47, i64 0
store i8 0, i8* %48
%49 = load i8*, i8** %10
store i8* %49, i8** %4
br label %103
50: ; preds = %36, %33
%51 = load i32*, i32** %11
%52 = load i32, i32* @SEEK_SET
%53 = call i64 @fseek(i32* %51, i32 0, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = load i32*, i32** %11
%57 = call i32 @fclose(i32* %56)
store i8* null, i8** %4
br label %103
58: ; preds = %50
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i64, i64* %8
%63 = add nsw i64 %62, 1
br label %66
64: ; preds = %58
%65 = load i64, i64* %8
br label %66
66: ; preds = %64, %61
%67 = phi i64 [ %63, %61 ], [ %65, %64 ]
store i64 %67, i64* %9
%68 = load i64, i64* %9
%69 = call i8* @malloc(i64 %68)
store i8* %69, i8** %10
%70 = load i8*, i8** %10
%71 = icmp eq i8* %70, null
br i1 %71, label %72, label %75
72: ; preds = %66
%73 = load i32*, i32** %11
%74 = call i32 @fclose(i32* %73)
store i8* null, i8** %4
br label %103
75: ; preds = %66
%76 = load i8*, i8** %10
%77 = load i64, i64* %8
%78 = load i32*, i32** %11
%79 = call i32 @fread(i8* %76, i64 %77, i32 1, i32* %78)
%80 = icmp slt i32 %79, 1
br i1 %80, label %81, label %86
81: ; preds = %75
%82 = load i32*, i32** %11
%83 = call i32 @fclose(i32* %82)
%84 = load i8*, i8** %10
%85 = call i32 @free(i8* %84)
store i8* null, i8** %4
br label %103
86: ; preds = %75
%87 = load i32*, i32** %11
%88 = call i32 @fclose(i32* %87)
%89 = load i32, i32* %7
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %95
91: ; preds = %86
%92 = load i8*, i8** %10
%93 = load i64, i64* %8
%94 = getelementptr inbounds i8, i8* %92, i64 %93
store i8 0, i8* %94
br label %95
95: ; preds = %91, %86
%96 = load i64*, i64** %6
%97 = icmp ne i64* %96, null
br i1 %97, label %98, label %101
98: ; preds = %95
%99 = load i64, i64* %8
%100 = load i64*, i64** %6
store i64 %99, i64* %100
br label %101
101: ; preds = %98, %95
%102 = load i8*, i8** %10
store i8* %102, i8** %4
br label %103
103: ; preds = %101, %81, %72, %55, %46, %45, %30, %22, %16
%104 = load i8*, i8** %4
ret i8* %104
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @read_file2mem(i8* nocapture %0, i64* writeonly %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 %51, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %4, i32 0, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @fclose(i32* %4)
br label %51
12: ; preds = %6
%13 = tail call i64 @ftell(i32* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @fclose(i32* %4)
br label %51
17: ; preds = %12
%18 = icmp eq i64 %13, 0
%19 = icmp ne i32 %2, 0
%20 = and i1 %19, %18
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = tail call i32 @fclose(i32* %4)
%23 = tail call dereferenceable_or_null(1) i8* @malloc(i64 1)
%24 = icmp eq i8* %23, null
br i1 %24, label %51, label %25
25: ; preds = %21
store i8 0, i8* %23
br label %51
26: ; preds = %17
%27 = load i32, i32* @SEEK_SET
%28 = tail call i64 @fseek(i32* %4, i32 0, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @fclose(i32* %4)
br label %51
32: ; preds = %26
%33 = zext i1 %19 to i64
%34 = add nuw nsw i64 %13, %33
%35 = tail call i8* @malloc(i64 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = tail call i32 @fclose(i32* %4)
br label %51
39: ; preds = %32
%40 = tail call i32 @fread(i8* %35, i64 %13, i32 1, i32* %4)
%41 = icmp slt i32 %40, 1
%42 = tail call i32 @fclose(i32* %4)
br i1 %41, label %43, label %45
43: ; preds = %39
%44 = tail call i32 @free(i8* %35)
br label %51
45: ; preds = %39
br i1 %19, label %46, label %48
46: ; preds = %45
%47 = getelementptr inbounds i8, i8* %35, i64 %13
store i8 0, i8* %47
br label %48
48: ; preds = %46, %45
%49 = icmp eq i64* %1, null
br i1 %49, label %51, label %50
50: ; preds = %48
store i64 %13, i64* %1
br label %51
51: ; preds = %48, %50, %21, %3, %43, %37, %30, %25, %15, %10
%52 = phi i8* [ null, %10 ], [ null, %15 ], [ %23, %25 ], [ null, %30 ], [ null, %37 ], [ null, %43 ], [ null, %3 ], [ null, %21 ], [ %35, %50 ], [ %35, %48 ]
ret i8* %52
}