#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 */
char EOF;
int /*<<< orphan*/ bf_eval(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
void *realloc(char *, size_t);
char *strchr(char *, char);
int bf_readfile(char *filename) {
  size_t n = 0, sz = 4;
  char *chars = malloc(sz);
  if (chars == NULL) {
    perror("Can't allocate memory");
    return 0;
  }
  FILE *f = fopen(filename, "r");
  if (f == NULL) {
    free(chars);
    perror("Can't open file");
    return 0;
  }
  char c, *r = NULL;
  while ((c = fgetc(f)) != EOF) {
    if ((r = strchr("<>+-[].,", c))) {
      if (n == sz) {
        sz *= 2;
        void *t = realloc(chars, sz);
        if (t == NULL) {
          free(chars);
          fclose(f);
          perror("Error allocating memory");
          return 0;
        }
        chars = t;
      }
      chars[n++] = *r;
    }
  }
  fclose(f);
  bf_eval(chars);
  free(chars);
  return 1;
}


x86 assembly (clang, -O0):
<bf_readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x4,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dc <bf_readfile+0x4c>
lea    0xe35(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130e <bf_readfile+0x17e>
mov    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121c <bf_readfile+0x8c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xe0d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130e <bf_readfile+0x17e>
movq   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x31(%rbp)
movsbl %al,%eax
mov    0x2dae(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12ec <bf_readfile+0x15c>
lea    0xddc(%rip),%rdi
movsbl -0x31(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12e7 <bf_readfile+0x157>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    12cb <bf_readfile+0x13b>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12c3 <bf_readfile+0x133>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd7f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130e <bf_readfile+0x17e>
mov    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%dl
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1224 <bf_readfile+0x94>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <bf_eval@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bf_readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x4,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1287 <bf_readfile+0xf7>
mov    %rax,%r15
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1290 <bf_readfile+0x100>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2e0a(%rip),%rbp
cmp    0x0(%rbp),%al
jne    120f <bf_readfile+0x7f>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   10b0 <bf_eval@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x4,%r13d
xor    %r12d,%r12d
jmp    123e <bf_readfile+0xae>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rax
movzbl (%rbx),%ecx
mov    %cl,(%rax,%r12,1)
add    $0x1,%r12
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <fgetc@plt>
cmp    0x0(%rbp),%al
je     11e3 <bf_readfile+0x53>
movsbl %al,%esi
lea    0xde0(%rip),%rdi
call   1050 <strchr@plt>
test   %rax,%rax
je     1231 <bf_readfile+0xa1>
mov    %rax,%rbx
cmp    %r13,%r12
jne    1220 <bf_readfile+0x90>
lea    (%r12,%r12,1),%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
jne    1223 <bf_readfile+0x93>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdac(%rip),%rdi
jmp    129f <bf_readfile+0x10f>
lea    0xd72(%rip),%rdi
jmp    129f <bf_readfile+0x10f>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
jmp    1200 <bf_readfile+0x70>

LLVM IR (-O0):
@.str = [22 x i8] c"Can't allocate memory\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"Can't open file\00"
@EOF = global i8 0
@.str.3 = [9 x i8] c"<>+-[].,\00"
@.str.4 = [24 x i8] c"Error allocating memory\00"
define i32 @bf_readfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %3
store i64 0, i64* %4
store i64 4, i64* %5
%11 = load i64, i64* %5
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %73
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i8*, i8** %6
%24 = call i32 @free(i8* %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %73
26: ; preds = %17
store i8* null, i8** %9
br label %27
27: ; preds = %65, %26
%28 = load i32*, i32** %7
%29 = call signext i8 @fgetc(i32* %28)
store i8 %29, i8* %8
%30 = sext i8 %29 to i32
%31 = load i8, i8* @EOF
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %30, %32
br i1 %33, label %34, label %66
34: ; preds = %27
%35 = load i8, i8* %8
%36 = call i8* @strchr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8 signext %35)
store i8* %36, i8** %9
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %65
38: ; preds = %34
%39 = load i64, i64* %4
%40 = load i64, i64* %5
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %58
42: ; preds = %38
%43 = load i64, i64* %5
%44 = mul i64 %43, 2
store i64 %44, i64* %5
%45 = load i8*, i8** %6
%46 = load i64, i64* %5
%47 = call i8* @realloc(i8* %45, i64 %46)
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %56
50: ; preds = %42
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
%55 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %73
56: ; preds = %42
%57 = load i8*, i8** %10
store i8* %57, i8** %6
br label %58
58: ; preds = %56, %38
%59 = load i8*, i8** %9
%60 = load i8, i8* %59
%61 = load i8*, i8** %6
%62 = load i64, i64* %4
%63 = add i64 %62, 1
store i64 %63, i64* %4
%64 = getelementptr inbounds i8, i8* %61, i64 %62
store i8 %60, i8* %64
br label %65
65: ; preds = %58, %34
br label %27
66: ; preds = %27
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
%69 = load i8*, i8** %6
%70 = call i32 @bf_eval(i8* %69)
%71 = load i8*, i8** %6
%72 = call i32 @free(i8* %71)
store i32 1, i32* %2
br label %73
73: ; preds = %66, %50, %22, %15
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
@.str = [22 x i8] c"Can't allocate memory\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"Can't open file\00"
@EOF = global i8 0
@.str.3 = [9 x i8] c"<>+-[].,\00"
@.str.4 = [24 x i8] c"Error allocating memory\00"
define i32 @bf_readfile(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %51
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call signext i8 @fgetc(i32* %7)
%11 = load i8, i8* @EOF
%12 = icmp eq i8 %10, %11
br i1 %12, label %46, label %16
13: ; preds = %6
%14 = tail call i32 @free(i8* %2)
%15 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %51
16: ; preds = %9, %39
%17 = phi i8 [ %43, %39 ], [ %10, %9 ]
%18 = phi i64 [ %42, %39 ], [ 0, %9 ]
%19 = phi i64 [ %41, %39 ], [ 4, %9 ]
%20 = phi i8* [ %40, %39 ], [ %2, %9 ]
%21 = tail call i8* @strchr(i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8 signext %17)
%22 = icmp eq i8* %21, null
br i1 %22, label %39, label %23
23: ; preds = %16
%24 = icmp eq i64 %18, %19
br i1 %24, label %25, label %33
25: ; preds = %23
%26 = shl i64 %18, 1
%27 = tail call i8* @realloc(i8* %20, i64 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = tail call i32 @free(i8* %20)
%31 = tail call i32 @fclose(i32* %7)
%32 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %51
33: ; preds = %25, %23
%34 = phi i8* [ %20, %23 ], [ %27, %25 ]
%35 = phi i64 [ %19, %23 ], [ %26, %25 ]
%36 = load i8, i8* %21
%37 = add i64 %18, 1
%38 = getelementptr inbounds i8, i8* %34, i64 %18
store i8 %36, i8* %38
br label %39
39: ; preds = %33, %16
%40 = phi i8* [ %34, %33 ], [ %20, %16 ]
%41 = phi i64 [ %35, %33 ], [ %19, %16 ]
%42 = phi i64 [ %37, %33 ], [ %18, %16 ]
%43 = tail call signext i8 @fgetc(i32* %7)
%44 = load i8, i8* @EOF
%45 = icmp eq i8 %43, %44
br i1 %45, label %46, label %16
46: ; preds = %39, %9
%47 = phi i8* [ %2, %9 ], [ %40, %39 ]
%48 = tail call i32 @fclose(i32* %7)
%49 = tail call i32 @bf_eval(i8* %47)
%50 = tail call i32 @free(i8* %47)
br label %51
51: ; preds = %29, %13, %46, %4
%52 = phi i32 [ 0, %4 ], [ 0, %13 ], [ 1, %46 ], [ 0, %29 ]
ret i32 %52
}