#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *readfile_stdc(const char *filename, size_t *filesize) {
  if (filesize == NULL)
    return NULL;
  *filesize = 0;
  if (filename == NULL)
    return NULL;
  FILE *f = fopen(filename, "r");
  if (f == NULL) {
    perror("fopen");
    return NULL;
  }
  if (fseek(f, 0L, SEEK_END) != 0) {
    perror("fseek");
    fclose(f);
    return NULL;
  }
  long size = ftell(f);
  if (size == -1) {
    perror("ftell");
    fclose(f);
    return NULL;
  }
  if (fseek(f, 0L, SEEK_SET) != 0) {
    perror("fseek");
    fclose(f);
    return NULL;
  }
  *filesize = (size_t)size;
  char *fileContents = (char *)malloc(sizeof(char) * size);
  if (fileContents == NULL) {
    fprintf(stderr, "malloc() failed.\n");
    fclose(f);
    return NULL;
  }
  if (fread(fileContents, 1, *filesize, f) < *filesize) {
    perror("fread");
    fprintf(stderr, "We read less from the file than we expected due to the "
                    "problem above.\n");
    free(fileContents);
    fclose(f);
    return NULL;
  }
  char oneMoreByte;
  if (fread(&oneMoreByte, 1, 1, f) == 1) {
    fprintf(stderr, "File increased in size unexpectedly.\n");
    free(fileContents);
    fclose(f);
    return NULL;
  }
  return fileContents;
}


x86 assembly (clang, -O0):
<readfile_stdc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <readfile_stdc+0x28>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11db <readfile_stdc+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x10(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1213 <readfile_stdc+0x83>
lea    0xe01(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x20(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     1255 <readfile_stdc+0xc5>
lea    0xdce(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    128f <readfile_stdc+0xff>
lea    0xd9a(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x20(%rbp),%rdi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     12d1 <readfile_stdc+0x141>
lea    0xd52(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1325 <readfile_stdc+0x195>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cltq
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jae    138f <readfile_stdc+0x1ff>
lea    0xcd0(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
lea    0xcc1(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x20(%rbp),%rcx
lea    -0x31(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    13e0 <readfile_stdc+0x250>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
lea    0xcb7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e8 <readfile_stdc+0x258>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile_stdc>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     12c7 <readfile_stdc+0x137>
mov    %rsi,%rbx
movq   $0x0,(%rsi)
test   %rdi,%rdi
je     12c7 <readfile_stdc+0x137>
lea    0xe44(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12a5 <readfile_stdc+0x115>
mov    %rax,%r14
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    129c <readfile_stdc+0x10c>
mov    %r14,%rdi
call   1070 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     12b3 <readfile_stdc+0x123>
mov    %rax,%r15
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    129c <readfile_stdc+0x10c>
mov    %r15,(%rbx)
mov    %r15d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1285 <readfile_stdc+0xf5>
mov    %rax,%r15
mov    (%rbx),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
cltq
cmp    %rax,(%rbx)
ja     12d7 <readfile_stdc+0x147>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    12ca <readfile_stdc+0x13a>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12bf <readfile_stdc+0x12f>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12bf <readfile_stdc+0x12f>
lea    0xd65(%rip),%rdi
jmp    12ba <readfile_stdc+0x12a>
lea    0xd56(%rip),%rdi
call   10b0 <perror@plt>
jmp    12c7 <readfile_stdc+0x137>
lea    0xd54(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd48(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
jmp    1276 <readfile_stdc+0xe6>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
@.str.2 = [6 x i8] c"fseek\00"
@.str.3 = [6 x i8] c"ftell\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.4 = [18 x i8] c"malloc() failed.\0A\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [71 x i8] c"We read less from the file than we expected due to the problem above.\0A\00"
@.str.7 = [38 x i8] c"File increased in size unexpectedly.\0A\00"
define i8* @readfile_stdc(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i64*, i64** %5
%11 = icmp eq i64* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* null, i8** %3
br label %99
13: ; preds = %2
%14 = load i64*, i64** %5
store i64 0, i64* %14
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i8* null, i8** %3
br label %99
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %99
25: ; preds = %18
%26 = load i32*, i32** %6
%27 = load i32, i32* @SEEK_END
%28 = call i64 @fseek(i32* %26, i64 0, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
store i8* null, i8** %3
br label %99
34: ; preds = %25
%35 = load i32*, i32** %6
%36 = call i64 @ftell(i32* %35)
store i64 %36, i64* %7
%37 = load i64, i64* %7
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i8* null, i8** %3
br label %99
43: ; preds = %34
%44 = load i32*, i32** %6
%45 = load i32, i32* @SEEK_SET
%46 = call i64 @fseek(i32* %44, i64 0, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
store i8* null, i8** %3
br label %99
52: ; preds = %43
%53 = load i64, i64* %7
%54 = load i64*, i64** %5
store i64 %53, i64* %54
%55 = load i64, i64* %7
%56 = mul i64 1, %55
%57 = trunc i64 %56 to i32
%58 = call i64 @malloc(i32 %57)
%59 = inttoptr i64 %58 to i8*
store i8* %59, i8** %8
%60 = load i8*, i8** %8
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %67
62: ; preds = %52
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
store i8* null, i8** %3
br label %99
67: ; preds = %52
%68 = load i8*, i8** %8
%69 = load i64*, i64** %5
%70 = load i64, i64* %69
%71 = trunc i64 %70 to i32
%72 = load i32*, i32** %6
%73 = call i32 @fread(i8* %68, i32 1, i32 %71, i32* %72)
%74 = sext i32 %73 to i64
%75 = load i64*, i64** %5
%76 = load i64, i64* %75
%77 = icmp ult i64 %74, %76
br i1 %77, label %78, label %86
78: ; preds = %67
%79 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%80 = load i32, i32* @stderr
%81 = call i32 @fprintf(i32 %80, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.6, i64 0, i64 0))
%82 = load i8*, i8** %8
%83 = call i32 @free(i8* %82)
%84 = load i32*, i32** %6
%85 = call i32 @fclose(i32* %84)
store i8* null, i8** %3
br label %99
86: ; preds = %67
%87 = load i32*, i32** %6
%88 = call i32 @fread(i8* %9, i32 1, i32 1, i32* %87)
%89 = icmp eq i32 %88, 1
br i1 %89, label %90, label %97
90: ; preds = %86
%91 = load i32, i32* @stderr
%92 = call i32 @fprintf(i32 %91, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
%93 = load i8*, i8** %8
%94 = call i32 @free(i8* %93)
%95 = load i32*, i32** %6
%96 = call i32 @fclose(i32* %95)
store i8* null, i8** %3
br label %99
97: ; preds = %86
%98 = load i8*, i8** %8
store i8* %98, i8** %3
br label %99
99: ; preds = %97, %90, %78, %62, %48, %39, %30, %23, %17, %12
%100 = load i8*, i8** %3
ret i8* %100
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
@.str.2 = [6 x i8] c"fseek\00"
@.str.3 = [6 x i8] c"ftell\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.4 = [18 x i8] c"malloc() failed.\0A\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [71 x i8] c"We read less from the file than we expected due to the problem above.\0A\00"
@.str.7 = [38 x i8] c"File increased in size unexpectedly.\0A\00"
define i8* @readfile_stdc(i8* %0, i64* %1) {
%3 = alloca i8
%4 = icmp eq i64* %1, null
br i1 %4, label %64, label %5
5: ; preds = %2
store i64 0, i64* %1
%6 = icmp eq i8* %0, null
br i1 %6, label %64, label %7
7: ; preds = %5
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %64
12: ; preds = %7
%13 = load i32, i32* @SEEK_END
%14 = tail call i64 @fseek(i32* %8, i64 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %8)
br label %64
19: ; preds = %12
%20 = tail call i64 @ftell(i32* %8)
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @fclose(i32* %8)
br label %64
25: ; preds = %19
%26 = load i32, i32* @SEEK_SET
%27 = tail call i64 @fseek(i32* %8, i64 0, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @fclose(i32* %8)
br label %64
32: ; preds = %25
store i64 %20, i64* %1
%33 = trunc i64 %20 to i32
%34 = tail call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
%36 = icmp eq i64 %34, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @fclose(i32* %8)
br label %64
41: ; preds = %32
%42 = load i64, i64* %1
%43 = trunc i64 %42 to i32
%44 = tail call i32 @fread(i8* %35, i32 1, i32 %43, i32* %8)
%45 = sext i32 %44 to i64
%46 = load i64, i64* %1
%47 = icmp ugt i64 %46, %45
br i1 %47, label %48, label %54
48: ; preds = %41
%49 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%50 = load i32, i32* @stderr
%51 = tail call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.6, i64 0, i64 0))
%52 = tail call i32 @free(i8* %35)
%53 = tail call i32 @fclose(i32* %8)
br label %64
54: ; preds = %41
%55 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %8)
%56 = icmp eq i32 %55, 1
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load i32, i32* @stderr
%59 = tail call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
%60 = tail call i32 @free(i8* %35)
%61 = tail call i32 @fclose(i32* %8)
br label %62
62: ; preds = %54, %57
%63 = phi i8* [ null, %57 ], [ %35, %54 ]
br label %64
64: ; preds = %10, %16, %37, %48, %62, %29, %22, %5, %2
%65 = phi i8* [ null, %2 ], [ null, %5 ], [ null, %10 ], [ null, %16 ], [ null, %22 ], [ null, %29 ], [ null, %37 ], [ null, %48 ], [ %63, %62 ]
ret i8* %65
}