#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*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strncmp(char *, char *, int);
int pi_read(FILE **file, char **filedata, size_t *filesize, char *filename) {
  int rc = 0;
  char *start = NULL;
  *file = fopen(filename, "rb");
  if (!*file) {
    printf("Could not open %s\n", filename);
    rc = 1;
    goto cleanup;
  }
  (void)fseek(*file, 0, SEEK_END);
  *filesize = (size_t)ftell(*file);
  (void)fseek(*file, 0, SEEK_SET);
  if (*filesize == 0) {
    printf("%s is empty\n", filename);
    rc = 1;
    goto cleanup;
  }
  *filedata = malloc(sizeof(char) * *filesize + 1);
  if (!*filedata) {
    printf("Memory error\n");
    rc = 1;
    goto cleanup;
  }
  start = malloc(sizeof(char) * 3);
  if (fread(start, 1, 2, *file) != 2) {
    printf("Could not read %s\n", filename);
    rc = 1;
    goto cleanup;
  }
  if (strncmp(start, "3.", 2) == 0) {
    *filesize -= 2;
  } else {
    (void)fseek(*file, 0, SEEK_SET);
  }
  if (fread(*filedata, 1, *filesize, *file) != *filesize) {
    printf("Could not read %s\n", filename);
    rc = 1;
    goto cleanup;
  }
cleanup:
  if (start) {
    free(start);
  }
  return rc;
}


x86 assembly (clang, -O0):
<pi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11ed <pi_read+0x6d>
mov    -0x20(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    138d <pi_read+0x20d>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    125d <pi_read+0xdd>
mov    -0x20(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    138d <pi_read+0x20d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12a5 <pi_read+0x125>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    138d <pi_read+0x20d>
mov    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1050 <fread@plt>
cmp    $0x2,%rax
je     12f5 <pi_read+0x175>
mov    -0x20(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    138d <pi_read+0x20d>
mov    -0x30(%rbp),%rdi
lea    0xd44(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1327 <pi_read+0x1a7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    $0x2,%rcx
mov    %rcx,(%rax)
jmp    133e <pi_read+0x1be>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     1388 <pi_read+0x208>
mov    -0x20(%rbp),%rsi
lea    0xcbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    138d <pi_read+0x20d>
jmp    138d <pi_read+0x20d>
cmpq   $0x0,-0x30(%rbp)
je     13a1 <pi_read+0x221>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pi_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe52(%rip),%rsi
mov    %rcx,%rdi
call   10b0 <fopen@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1268 <pi_read+0xd8>
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%rbx),%rdi
call   1080 <ftell@plt>
mov    %rax,(%r15)
mov    (%rbx),%rdi
mov    0x2dee(%rip),%rbp
mov    0x0(%rbp),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%r15),%rdi
test   %rdi,%rdi
je     1271 <pi_read+0xe1>
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     1298 <pi_read+0x108>
mov    $0x3,%edi
call   1090 <malloc@plt>
mov    %rax,%r13
mov    (%rbx),%rcx
mov    $0x2,%edx
mov    %rax,%rdi
mov    $0x1,%esi
call   1060 <fread@plt>
cmp    $0x2,%rax
jne    12c8 <pi_read+0x138>
lea    0xdf2(%rip),%rsi
mov    %r13,%rdi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12a6 <pi_read+0x116>
mov    (%rbx),%rdi
mov    0x0(%rbp),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%r15),%rdx
jmp    12b0 <pi_read+0x120>
lea    0xd94(%rip),%rdi
jmp    1278 <pi_read+0xe8>
lea    0xd9e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
call   1050 <puts@plt>
jmp    1282 <pi_read+0xf2>
mov    (%r15),%rdx
add    $0xfffffffffffffffe,%rdx
mov    %rdx,(%r15)
mov    (%r12),%rdi
mov    (%rbx),%rcx
mov    $0x1,%esi
call   1060 <fread@plt>
xor    %ebx,%ebx
cmp    (%r15),%rax
je     12de <pi_read+0x14e>
lea    0xd54(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
test   %r13,%r13
je     1287 <pi_read+0xf7>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1287 <pi_read+0xf7>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"%s is empty\0A\00"
@.str.4 = [19 x i8] c"Could not read %s\0A\00"
@.str.5 = [3 x i8] c"3.\00"
@str = [13 x i8] c"Memory error\00"
define i32 @pi_read(i32** nocapture %0, i8** nocapture %1, i64* nocapture %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %0
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %58
9: ; preds = %4
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %5, i32 0, i32 %10)
%12 = load i32*, i32** %0
%13 = tail call i64 @ftell(i32* %12)
store i64 %13, i64* %2
%14 = load i32*, i32** %0
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i64, i64* %2
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %58
21: ; preds = %9
%22 = trunc i64 %17 to i32
%23 = add i32 %22, 1
%24 = tail call i8* @malloc(i32 %23)
store i8* %24, i8** %1
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %58
28: ; preds = %21
%29 = tail call dereferenceable_or_null(3) i8* @malloc(i32 3)
%30 = load i32*, i32** %0
%31 = tail call i64 @fread(i8* %29, i32 1, i64 2, i32* %30)
%32 = icmp eq i64 %31, 2
br i1 %32, label %33, label %51
33: ; preds = %28
%34 = tail call i64 @strncmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i64, i64* %2
%38 = add i64 %37, -2
store i64 %38, i64* %2
br label %44
39: ; preds = %33
%40 = load i32*, i32** %0
%41 = load i32, i32* @SEEK_SET
%42 = tail call i32 @fseek(i32* %40, i32 0, i32 %41)
%43 = load i64, i64* %2
br label %44
44: ; preds = %39, %36
%45 = phi i64 [ %43, %39 ], [ %38, %36 ]
%46 = load i8*, i8** %1
%47 = load i32*, i32** %0
%48 = tail call i64 @fread(i8* %46, i32 1, i64 %45, i32* %47)
%49 = load i64, i64* %2
%50 = icmp eq i64 %48, %49
br i1 %50, label %53, label %51
51: ; preds = %44, %28
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %53
53: ; preds = %51, %44
%54 = phi i32 [ 0, %44 ], [ 1, %51 ]
%55 = icmp eq i8* %29, null
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = tail call i32 @free(i8* %29)
br label %58
58: ; preds = %7, %26, %19, %56, %53
%59 = phi i32 [ %54, %56 ], [ %54, %53 ], [ 1, %19 ], [ 1, %26 ], [ 1, %7 ]
ret i32 %59
}