#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 */
struct stat {
  int st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *readfile_fstat(const char *filename, size_t *filesize) {
  if (filesize == NULL)
    return NULL;
  *filesize = 0;
  if (filename == NULL)
    return NULL;
  int fd = open(filename, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return NULL;
  }
  struct stat result;
  if (fstat(fd, &result) == -1) {
    perror("stat");
    close(fd);
    return NULL;
  }
  if (!S_ISREG(result.st_mode)) {
    fprintf(stderr, "Not a file: %s\n", filename);
    close(fd);
    return NULL;
  }
  off_t size = result.st_size;
  *filesize = (size_t)size;
  if (size == 0) {
    close(fd);
    return NULL;
  }
  char *fileContents = (char *)malloc(sizeof(char) * size);
  if (fileContents == NULL) {
    fprintf(stderr, "malloc() failed.\n");
    close(fd);
    return NULL;
  }
  if (read(fd, fileContents, size) < (ssize_t)size) {
    perror("read");
    fprintf(stderr, "We read less from the file than we expected due to the "
                    "problem above.\n");
    free(fileContents);
    close(fd);
    return NULL;
  }
  if (read(fd, fileContents, 1) == 1) {
    fprintf(stderr, "File increased in size unexpectedly.\n");
    free(fileContents);
    close(fd);
    return NULL;
  }
  if (close(fd) != 0)
    perror("close");
  return fileContents;
}


x86 assembly (clang, -O0):
<readfile_fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <readfile_fstat+0x28>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11db <readfile_fstat+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x10(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1213 <readfile_fstat+0x83>
lea    0xdff(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1249 <readfile_fstat+0xb9>
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x24(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    128a <readfile_fstat+0xfa>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x2c(%rbp)
jne    12ba <readfile_fstat+0x12a>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1304 <readfile_fstat+0x174>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
cmp    -0x2c(%rbp),%eax
jge    135d <readfile_fstat+0x1cd>
lea    0xd09(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    13ac <readfile_fstat+0x21c>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    0xcf1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13d2 <readfile_fstat+0x242>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     13ca <readfile_fstat+0x23a>
lea    0xcd9(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile_fstat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     12df <readfile_fstat+0x14f>
mov    %rsi,%rbx
mov    %rdi,%rbp
movq   $0x0,(%rsi)
test   %rdi,%rdi
je     12df <readfile_fstat+0x14f>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12bd <readfile_fstat+0x12d>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12cb <readfile_fstat+0x13b>
mov    0xc(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1261 <readfile_fstat+0xd1>
movslq 0x8(%rsp),%rbp
mov    %rbp,(%rbx)
test   %rbp,%rbp
je     12d7 <readfile_fstat+0x147>
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127f <readfile_fstat+0xef>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    %ebp,%eax
jl     12ed <readfile_fstat+0x15d>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    12a2 <readfile_fstat+0x112>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    12d7 <readfile_fstat+0x147>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1298 <readfile_fstat+0x108>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
call   1040 <close@plt>
jmp    12e1 <readfile_fstat+0x151>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
je     12e1 <readfile_fstat+0x151>
lea    0xde8(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e1 <readfile_fstat+0x151>
lea    0xd3c(%rip),%rdi
call   10a0 <perror@plt>
jmp    12df <readfile_fstat+0x14f>
lea    0xd33(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd38(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd28(%rip),%rsi
jmp    1250 <readfile_fstat+0xc0>

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

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