#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_file(char *name, int *length, int silent_test) {
  int file_length, read_length;
  int allocation_size;
  char *contents;
  FILE *f;
  if ((f = fopen(name, "r")) == NULL) {
    if (silent_test == 0)
      fprintf(stderr, "BCA: %s %s\n", name, strerror(errno));
    return NULL;
  }
  if (fseek(f, 0, SEEK_END)) {
    fprintf(stderr, "BCA: %s %s\n", name, strerror(errno));
    fclose(f);
    return NULL;
  }
  if ((file_length = ftell(f)) < 0) {
    fprintf(stderr, "BCA: ftell(%s) failed", name);
    fclose(f);
    return NULL;
  }
  if (fseek(f, 0, SEEK_SET)) {
    fprintf(stderr, "BCA: %s %s\n", name, strerror(errno));
    fclose(f);
    return NULL;
  }
  allocation_size = file_length + 1;
  if ((contents = (char *)malloc(allocation_size)) == NULL) {
    perror("BCA: malloc()");
    fclose(f);
    return NULL;
  }
  if ((read_length = fread(contents, 1, file_length, f)) < file_length) {
    fprintf(stderr, "BCA: fread() returned %d instead of %d", (int)read_length,
            (int)file_length);
    fclose(f);
    return NULL;
  }
  contents[read_length] = 0;
  fclose(f);
  *length = read_length;
  return contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1212 <read_file+0x82>
cmpl   $0x0,-0x1c(%rbp)
jne    1205 <read_file+0x75>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x38(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1280 <read_file+0xf0>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x38(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    12c6 <read_file+0x136>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x38(%rbp),%rdi
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1334 <read_file+0x1a4>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1375 <read_file+0x1e5>
lea    0xcca(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmp    -0x20(%rbp),%eax
jge    13c9 <read_file+0x239>
mov    0x2c3b(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xc86(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ef <read_file+0x25f>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120c <read_file+0x7c>
mov    %rax,%rbx
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     121c <read_file+0x8c>
mov    0x2e02(%rip),%rax
mov    (%rax),%ebp
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe15(%rip),%rsi
xor    %r14d,%r14d
mov    %ebp,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12ce <read_file+0x13e>
test   %ebp,%ebp
je     129e <read_file+0x10e>
xor    %r14d,%r14d
jmp    12ce <read_file+0x13e>
mov    %rbx,%rdi
call   1060 <ftell@plt>
test   %eax,%eax
js     12e0 <read_file+0x150>
mov    %eax,%r13d
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    11cf <read_file+0x3f>
lea    0x1(%r13),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1318 <read_file+0x188>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
cmp    %r13d,%eax
jge    1302 <read_file+0x172>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
xor    %r14d,%r14d
mov    %ebp,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11ff <read_file+0x6f>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd46(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xd1e(%rip),%rsi
xor    %r14d,%r14d
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11ff <read_file+0x6f>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,(%r12)
jmp    12ce <read_file+0x13e>
lea    0xd05(%rip),%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
jmp    12ce <read_file+0x13e>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"BCA: %s %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [22 x i8] c"BCA: ftell(%s) failed\00"
@SEEK_SET = global i32 0
@.str.3 = [14 x i8] c"BCA: malloc()\00"
@.str.4 = [39 x i8] c"BCA: fread() returned %d instead of %d\00"
define i8* @read_file(i8* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %8, label %59
8: ; preds = %6
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %11)
br label %59
13: ; preds = %3
%14 = load i32, i32* @SEEK_END
%15 = tail call i64 @fseek(i32* %4, i32 0, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %20)
%22 = tail call i32 @fclose(i32* %4)
br label %59
23: ; preds = %13
%24 = tail call i32 @ftell(i32* %4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%29 = tail call i32 @fclose(i32* %4)
br label %59
30: ; preds = %23
%31 = load i32, i32* @SEEK_SET
%32 = tail call i64 @fseek(i32* %4, i32 0, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %37)
%39 = tail call i32 @fclose(i32* %4)
br label %59
40: ; preds = %30
%41 = add nuw nsw i32 %24, 1
%42 = tail call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
%44 = icmp eq i64 %42, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%47 = tail call i32 @fclose(i32* %4)
br label %59
48: ; preds = %40
%49 = tail call i32 @fread(i8* %43, i32 1, i32 %24, i32* %4)
%50 = icmp slt i32 %49, %24
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32 %24)
%54 = tail call i32 @fclose(i32* %4)
br label %59
55: ; preds = %48
%56 = sext i32 %49 to i64
%57 = getelementptr inbounds i8, i8* %43, i64 %56
store i8 0, i8* %57
%58 = tail call i32 @fclose(i32* %4)
store i32 %49, i32* %1
br label %59
59: ; preds = %6, %8, %55, %51, %45, %34, %26, %17
%60 = phi i8* [ null, %17 ], [ null, %26 ], [ null, %34 ], [ null, %45 ], [ null, %51 ], [ %43, %55 ], [ null, %8 ], [ null, %6 ]
ret i8* %60
}