#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 {
  scalar_t__ st_size;
};
typedef scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ INT_MAX;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *read_file(char *filename, int *length_return) {
  char buf[1024];
  char *ret = NULL, *input = NULL, *p = NULL;
  int fd = -1;
  struct stat st;
  off_t length = 0, remain = 0;
  ssize_t bytes_read = 0;
  if (length_return) {
    *length_return = -1;
  }
  if (stat(filename, &st) != 0) {
    perror(filename);
    goto done;
  }
  length = st.st_size;
  if (length > INT_MAX) {
    fprintf(stderr, "%s: file size too large\n", filename);
    goto done;
  }
  input = (char *)malloc((size_t)length);
  if (!input) {
    fprintf(stderr, "%s: cannot allocate memory for file\n", filename);
    goto done;
  }
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror(filename);
    goto done;
  }
  p = input;
  remain = length;
  while (remain > 0) {
    bytes_read = read(fd, buf, 1024);
    if (bytes_read < 0) {
      perror(filename);
      goto done;
    } else if (bytes_read == 0) {
      break;
    }
    memcpy(p, buf, bytes_read);
    p += bytes_read;
    remain -= bytes_read;
  }
  if (remain > 0) {
    fprintf(stderr, "could not read all of file '%s' into memory\n", filename);
    goto done;
  }
  ret = input;
  input = NULL;
  close(fd);
  fd = -1;
done:
  if (input) {
    free(input);
    input = NULL;
  }
  if (fd >= 0) {
    close(fd);
    fd = -1;
  }
  if (length_return) {
    *length_return = length;
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
movq   $0x0,-0x428(%rbp)
movl   $0xffffffff,-0x42c(%rbp)
ff ff ff
movq   $0x0,-0x440(%rbp)
movq   $0x0,-0x448(%rbp)
movq   $0x0,-0x450(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1204 <read_file+0x74>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x438(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     122c <read_file+0x9c>
mov    -0x8(%rbp),%rdi
call   10b0 <perror@plt>
jmp    1403 <read_file+0x273>
mov    -0x438(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jle    126f <read_file+0xdf>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd9b(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1403 <read_file+0x273>
mov    -0x440(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    12ae <read_file+0x11e>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd75(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1403 <read_file+0x273>
mov    -0x8(%rbp),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    12e1 <read_file+0x151>
mov    -0x8(%rbp),%rdi
call   10b0 <perror@plt>
jmp    1403 <read_file+0x273>
mov    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x440(%rbp),%rax
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jle    13a9 <read_file+0x219>
mov    -0x42c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x450(%rbp)
cmpq   $0x0,-0x450(%rbp)
jge    1345 <read_file+0x1b5>
mov    -0x8(%rbp),%rdi
call   10b0 <perror@plt>
jmp    1403 <read_file+0x273>
cmpq   $0x0,-0x450(%rbp)
jne    1358 <read_file+0x1c8>
jmp    13a9 <read_file+0x219>
jmp    135d <read_file+0x1cd>
mov    -0x428(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x450(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x450(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x450(%rbp),%rcx
mov    -0x448(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x448(%rbp)
jmp    12fd <read_file+0x16d>
cmpq   $0x0,-0x448(%rbp)
jle    13d5 <read_file+0x245>
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xc73(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1403 <read_file+0x273>
mov    -0x420(%rbp),%rax
mov    %rax,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
mov    -0x42c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x42c(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     1428 <read_file+0x298>
mov    -0x420(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x420(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jl     144a <read_file+0x2ba>
mov    -0x42c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x42c(%rbp)
ff ff ff
cmpq   $0x0,-0x10(%rbp)
je     1464 <read_file+0x2d4>
mov    -0x440(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x418(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
mov    %rdi,%r13
test   %rsi,%rsi
je     11b2 <read_file+0x22>
movl   $0xffffffff,(%rbx)
lea    0x18(%rsp),%rsi
mov    %r13,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    12fb <read_file+0x16b>
mov    0x18(%rsp),%r14
mov    0x2e0c(%rip),%rax
cmp    (%rax),%r14
jle    11ee <read_file+0x5e>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
jmp    129f <read_file+0x10f>
mov    %r14,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     128f <read_file+0xff>
mov    %rax,%r15
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     130f <read_file+0x17f>
mov    %eax,%ebp
mov    %r15,(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r14,0x8(%rsp)
test   %r14,%r14
jle    1273 <read_file+0xe3>
lea    0x20(%rsp),%rbx
mov    0x8(%rsp),%r14
mov    (%rsp),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1321 <read_file+0x191>
je     12c3 <read_file+0x133>
mov    %rax,%r15
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1080 <memcpy@plt>
add    %r15,%r12
sub    %r15,%r14
jg     1240 <read_file+0xb0>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x10(%rsp),%rbx
mov    0x8(%rsp),%r14
mov    (%rsp),%rax
test   %rbx,%rbx
jne    12ae <read_file+0x11e>
jmp    12b1 <read_file+0x121>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
test   %rbx,%rbx
je     12b1 <read_file+0x121>
mov    %r14d,(%rbx)
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
mov    0x10(%rsp),%rbx
mov    0x8(%rsp),%r14
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %ebp,%ebp
js     12a7 <read_file+0x117>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    12a7 <read_file+0x117>
mov    %r13,%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
xor    %r14d,%r14d
test   %rbx,%rbx
jne    12ae <read_file+0x11e>
jmp    12b1 <read_file+0x121>
mov    %r13,%rdi
call   10b0 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12a7 <read_file+0x117>
mov    %r13,%rdi
call   10b0 <perror@plt>
jmp    12db <read_file+0x14b>

LLVM IR (-O0):
%struct.stat = type { i64 }
@INT_MAX = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: file size too large\0A\00"
@.str.1 = [37 x i8] c"%s: cannot allocate memory for file\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [45 x i8] c"could not read all of file '%s' into memory\0A\00"
define i8* @read_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %3
store i32* %1, i32** %4
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i32 -1, i32* %9
store i64 0, i64* %11
store i64 0, i64* %12
store i64 0, i64* %13
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32*, i32** %4
store i32 -1, i32* %17
br label %18
18: ; preds = %16, %2
%19 = load i8*, i8** %3
%20 = call i64 @stat(i8* %19, %struct.stat* %10)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = call i32 @perror(i8* %23)
br label %96
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%27 = load i64, i64* %26
store i64 %27, i64* %11
%28 = load i64, i64* %11
%29 = load i64, i64* @INT_MAX
%30 = icmp sgt i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %3
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %96
35: ; preds = %25
%36 = load i64, i64* %11
%37 = call i64 @malloc(i64 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %45, label %41
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %3
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %43)
br label %96
45: ; preds = %35
%46 = load i8*, i8** %3
%47 = load i32, i32* @O_RDONLY
%48 = call i32 @open(i8* %46, i32 %47)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i8*, i8** %3
%53 = call i32 @perror(i8* %52)
br label %96
54: ; preds = %45
%55 = load i8*, i8** %7
store i8* %55, i8** %8
%56 = load i64, i64* %11
store i64 %56, i64* %12
br label %57
57: ; preds = %74, %54
%58 = load i64, i64* %12
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %85
60: ; preds = %57
%61 = load i32, i32* %9
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%63 = call i64 @read(i32 %61, i8* %62, i32 1024)
store i64 %63, i64* %13
%64 = load i64, i64* %13
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = load i8*, i8** %3
%68 = call i32 @perror(i8* %67)
br label %96
69: ; preds = %60
%70 = load i64, i64* %13
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %69
br label %85
73: ; preds = %69
br label %74
74: ; preds = %73
%75 = load i8*, i8** %8
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%77 = load i64, i64* %13
%78 = call i32 @memcpy(i8* %75, i8* %76, i64 %77)
%79 = load i64, i64* %13
%80 = load i8*, i8** %8
%81 = getelementptr inbounds i8, i8* %80, i64 %79
store i8* %81, i8** %8
%82 = load i64, i64* %13
%83 = load i64, i64* %12
%84 = sub nsw i64 %83, %82
store i64 %84, i64* %12
br label %57
85: ; preds = %72, %57
%86 = load i64, i64* %12
%87 = icmp sgt i64 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %85
%89 = load i32, i32* @stderr
%90 = load i8*, i8** %3
%91 = call i32 @fprintf(i32 %89, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %90)
br label %96
92: ; preds = %85
%93 = load i8*, i8** %7
store i8* %93, i8** %6
store i8* null, i8** %7
%94 = load i32, i32* %9
%95 = call i32 @close(i32 %94)
store i32 -1, i32* %9
br label %96
96: ; preds = %92, %88, %66, %51, %41, %31, %22
%97 = load i8*, i8** %7
%98 = icmp ne i8* %97, null
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i8*, i8** %7
%101 = call i32 @free(i8* %100)
store i8* null, i8** %7
br label %102
102: ; preds = %99, %96
%103 = load i32, i32* %9
%104 = icmp sge i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %102
%106 = load i32, i32* %9
%107 = call i32 @close(i32 %106)
store i32 -1, i32* %9
br label %108
108: ; preds = %105, %102
%109 = load i32*, i32** %4
%110 = icmp ne i32* %109, null
br i1 %110, label %111, label %115
111: ; preds = %108
%112 = load i64, i64* %11
%113 = trunc i64 %112 to i32
%114 = load i32*, i32** %4
store i32 %113, i32* %114
br label %115
115: ; preds = %111, %108
%116 = load i8*, i8** %6
ret i8* %116
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@INT_MAX = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: file size too large\0A\00"
@.str.1 = [37 x i8] c"%s: cannot allocate memory for file\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [45 x i8] c"could not read all of file '%s' into memory\0A\00"
define i8* @read_file(i8* %0, i32* writeonly %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32* %1, null
br i1 %7, label %9, label %8
8: ; preds = %2
store i32 -1, i32* %1
br label %9
9: ; preds = %8, %2
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @perror(i8* %0)
br label %62
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @INT_MAX
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %62
22: ; preds = %14
%23 = call i64 @malloc(i64 %16)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %62
29: ; preds = %22
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %0, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = icmp sgt i64 %16, 0
br i1 %34, label %38, label %55
35: ; preds = %29
%36 = call i32 @perror(i8* %0)
%37 = call i32 @free(i8* %24)
br label %62
38: ; preds = %33, %47
%39 = phi i64 [ %50, %47 ], [ %16, %33 ]
%40 = phi i8* [ %49, %47 ], [ %24, %33 ]
%41 = call i64 @read(i32 %31, i8* %5, i32 1024)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* %0)
br label %57
45: ; preds = %38
%46 = icmp eq i64 %41, 0
br i1 %46, label %52, label %47
47: ; preds = %45
%48 = call i32 @memcpy(i8* %40, i8* %5, i64 %41)
%49 = getelementptr inbounds i8, i8* %40, i64 %41
%50 = sub nsw i64 %39, %41
%51 = icmp sgt i64 %50, 0
br i1 %51, label %38, label %55
52: ; preds = %45
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %57
55: ; preds = %47, %33
%56 = call i32 @close(i32 %31)
br label %62
57: ; preds = %43, %52
%58 = call i32 @free(i8* %24)
%59 = icmp sgt i32 %31, -1
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @close(i32 %31)
br label %62
62: ; preds = %35, %12, %19, %55, %26, %60, %57
%63 = phi i8* [ null, %60 ], [ null, %57 ], [ null, %26 ], [ %24, %55 ], [ null, %19 ], [ null, %12 ], [ null, %35 ]
%64 = phi i64 [ %16, %60 ], [ %16, %57 ], [ %16, %26 ], [ %16, %55 ], [ %16, %19 ], [ 0, %12 ], [ %16, %35 ]
br i1 %7, label %67, label %65
65: ; preds = %62
%66 = trunc i64 %64 to i32
store i32 %66, i32* %1
br label %67
67: ; preds = %65, %62
ret i8* %63
}