#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;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t fread(char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(unsigned long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t, unsigned long);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
char *plot_read(const char *const filename) {
  FILE *f;
  long fsize;
  unsigned long ufsize;
  size_t read;
  char *buffer;
  f = fopen(filename, "r");
  if (f == NULL) {
    puts("ERROR: fopen");
    perror("fopen error:");
    return 0;
  }
  if (fseek(f, 0, SEEK_END)) {
    fputs("ERROR: fseek SEEK_END", stderr);
    fclose(f);
    return 0;
  }
  fsize = ftell(f);
  if (fsize < 0) {
    perror("ftell error:");
    fclose(f);
    return 0;
  }
  ufsize = fsize;
  if (fseek(f, 0, SEEK_SET)) {
    fputs("ERROR: fseek SEEK_SET", stderr);
    fclose(f);
    return 0;
  }
  buffer = malloc(ufsize + 1);
  if (!buffer) {
    perror("ERROR: malloc failed:");
    fclose(f);
    return 0;
  }
  buffer[ufsize] = '\0';
  read = fread(buffer, 1, ufsize, f);
  if (read != ufsize) {
    printf("ERROR: fread read '%zd' when we expected '%lu'\n", read, ufsize);
    free(buffer);
    fclose(f);
    return 0;
  }
  if (fclose(f)) {
    fputs("ERROR: fclose", stderr);
    free(buffer);
    return 0;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<plot_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1200 <plot_read+0x50>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
lea    0xe21(%rip),%rdi
call   10d0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x18(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
cmp    $0x0,%rax
je     1249 <plot_read+0x99>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0xdee(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1283 <plot_read+0xd3>
lea    0xdca(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
cmp    $0x0,%rax
je     12d4 <plot_read+0x124>
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
lea    0xd86(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1312 <plot_read+0x162>
lea    0xd5e(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1379 <plot_read+0x1c9>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd16(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
cmp    $0x0,%rax
je     13b7 <plot_read+0x207>
mov    0x2c45(%rip),%rax
mov    (%rax),%esi
lea    0xcff(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    13bf <plot_read+0x20f>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plot_read>:
push   %r15
push   %r14
push   %rbx
lea    0xe44(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12aa <plot_read+0xfa>
mov    %rax,%rbx
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
test   %rax,%rax
je     11f7 <plot_read+0x47>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
jmp    1233 <plot_read+0x83>
mov    %rbx,%rdi
call   1090 <ftell@plt>
test   %rax,%rax
js     12e6 <plot_read+0x136>
mov    %rax,%r15
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
test   %rax,%rax
je     124c <plot_read+0x9c>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0xe0c(%rip),%rdi
call   1080 <fputs@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x1(%r15),%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12f7 <plot_read+0x147>
mov    %rax,%r14
movb   $0x0,(%rax,%r15,1)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %r15,%rax
jne    12c7 <plot_read+0x117>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
je     1243 <plot_read+0x93>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
call   1080 <fputs@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1240 <plot_read+0x90>
lea    0xd51(%rip),%rdi
call   1040 <puts@plt>
lea    0xd52(%rip),%rdi
call   10d0 <perror@plt>
jmp    1240 <plot_read+0x90>
lea    0xd9d(%rip),%rdi
mov    %rax,%rsi
mov    %r15,%rdx
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1238 <plot_read+0x88>
lea    0xd45(%rip),%rdi
call   10d0 <perror@plt>
jmp    1238 <plot_read+0x88>
lea    0xd57(%rip),%rdi
call   10d0 <perror@plt>
jmp    1238 <plot_read+0x88>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"ERROR: fopen\00"
@.str.2 = [13 x i8] c"fopen error:\00"
@SEEK_END = global i32 0
@.str.3 = [22 x i8] c"ERROR: fseek SEEK_END\00"
@stderr = global i32 0
@.str.4 = [13 x i8] c"ftell error:\00"
@SEEK_SET = global i32 0
@.str.5 = [22 x i8] c"ERROR: fseek SEEK_SET\00"
@.str.6 = [22 x i8] c"ERROR: malloc failed:\00"
@.str.7 = [48 x i8] c"ERROR: fread read '%zd' when we expected '%lu'\0A\00"
@.str.8 = [14 x i8] c"ERROR: fclose\00"
define i8* @plot_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %86
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = load i32, i32* @SEEK_END
%19 = call i64 @fseek(i32* %17, i32 0, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %4
%25 = call i64 @fclose(i32* %24)
store i8* null, i8** %2
br label %86
26: ; preds = %16
%27 = load i32*, i32** %4
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32*, i32** %4
%34 = call i64 @fclose(i32* %33)
store i8* null, i8** %2
br label %86
35: ; preds = %26
%36 = load i64, i64* %5
store i64 %36, i64* %6
%37 = load i32*, i32** %4
%38 = load i32, i32* @SEEK_SET
%39 = call i64 @fseek(i32* %37, i32 0, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %42)
%44 = load i32*, i32** %4
%45 = call i64 @fclose(i32* %44)
store i8* null, i8** %2
br label %86
46: ; preds = %35
%47 = load i64, i64* %6
%48 = add i64 %47, 1
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = icmp ne i8* %50, null
br i1 %51, label %56, label %52
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32*, i32** %4
%55 = call i64 @fclose(i32* %54)
store i8* null, i8** %2
br label %86
56: ; preds = %46
%57 = load i8*, i8** %8
%58 = load i64, i64* %6
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %8
%61 = load i64, i64* %6
%62 = load i32*, i32** %4
%63 = call i64 @fread(i8* %60, i32 1, i64 %61, i32* %62)
store i64 %63, i64* %7
%64 = load i64, i64* %7
%65 = load i64, i64* %6
%66 = icmp ne i64 %64, %65
br i1 %66, label %67, label %75
67: ; preds = %56
%68 = load i64, i64* %7
%69 = load i64, i64* %6
%70 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i64 %68, i64 %69)
%71 = load i8*, i8** %8
%72 = call i32 @free(i8* %71)
%73 = load i32*, i32** %4
%74 = call i64 @fclose(i32* %73)
store i8* null, i8** %2
br label %86
75: ; preds = %56
%76 = load i32*, i32** %4
%77 = call i64 @fclose(i32* %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %84
79: ; preds = %75
%80 = load i32, i32* @stderr
%81 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %80)
%82 = load i8*, i8** %8
%83 = call i32 @free(i8* %82)
store i8* null, i8** %2
br label %86
84: ; preds = %75
%85 = load i8*, i8** %8
store i8* %85, i8** %2
br label %86
86: ; preds = %84, %79, %67, %52, %41, %31, %21, %13
%87 = load i8*, i8** %2
ret i8* %87
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"ERROR: fopen\00"
@.str.2 = [13 x i8] c"fopen error:\00"
@SEEK_END = global i32 0
@.str.3 = [22 x i8] c"ERROR: fseek SEEK_END\00"
@stderr = global i32 0
@.str.4 = [13 x i8] c"ftell error:\00"
@SEEK_SET = global i32 0
@.str.5 = [22 x i8] c"ERROR: fseek SEEK_SET\00"
@.str.6 = [22 x i8] c"ERROR: malloc failed:\00"
@.str.7 = [48 x i8] c"ERROR: fread read '%zd' when we expected '%lu'\0A\00"
@.str.8 = [14 x i8] c"ERROR: fclose\00"
define noalias i8* @plot_read(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %51
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %2, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i64 @fclose(i32* %2)
br label %51
15: ; preds = %7
%16 = tail call i64 @ftell(i32* %2)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i64 @fclose(i32* %2)
br label %51
21: ; preds = %15
%22 = load i32, i32* @SEEK_SET
%23 = tail call i64 @fseek(i32* %2, i32 0, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = tail call i64 @fclose(i32* %2)
br label %51
29: ; preds = %21
%30 = add nuw i64 %16, 1
%31 = tail call i8* @malloc(i64 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%35 = tail call i64 @fclose(i32* %2)
br label %51
36: ; preds = %29
%37 = getelementptr inbounds i8, i8* %31, i64 %16
store i8 0, i8* %37
%38 = tail call i64 @fread(i8* %31, i32 1, i64 %16, i32* %2)
%39 = icmp eq i64 %38, %16
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i64 %38, i64 %16)
%42 = tail call i32 @free(i8* %31)
%43 = tail call i64 @fclose(i32* %2)
br label %51
44: ; preds = %36
%45 = tail call i64 @fclose(i32* %2)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = tail call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %48)
%50 = tail call i32 @free(i8* %31)
br label %51
51: ; preds = %44, %47, %40, %33, %25, %18, %11, %4
%52 = phi i8* [ null, %4 ], [ null, %11 ], [ null, %18 ], [ null, %25 ], [ null, %40 ], [ null, %47 ], [ null, %33 ], [ %31, %44 ]
ret i8* %52
}