#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strchr(char *, char);
char *read_bf(char *filepath) {
  int i;
  char *commands = (char *)malloc(30000);
  FILE *read_f;
  int total_c;
  int r_ptr = 0;
  if ((read_f = fopen(filepath, "r")) == NULL) {
    perror("Error opening file");
    exit(-1);
  }
  total_c = fread(commands, 1, 30000, read_f);
  fclose(read_f);
  for (i = 0; i < total_c; i++) {
    if (strchr(".,+-<>[]", commands[i])) {
      commands[r_ptr++] = commands[i];
    }
  }
  commands[r_ptr] = '\0';
  return commands;
}


x86 assembly (clang, -O0):
<read_bf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x7530,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c5 <read_bf+0x55>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7530,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1250 <read_bf+0xe0>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
lea    0xe0b(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     123d <read_bf+0xcd>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1242 <read_bf+0xd2>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ef <read_bf+0x7f>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
mov    $0x7530,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
lea    0xe71(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1222 <read_bf+0xb2>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x7530,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %ebp,%ebp
jle    120c <read_bf+0x9c>
mov    %ebp,%r15d
xor    %ebx,%ebx
lea    0xe43(%rip),%r14
xor    %ebp,%ebp
jmp    11e9 <read_bf+0x79>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r15
je     120e <read_bf+0x9e>
movsbl (%r12,%rbx,1),%esi
mov    %r14,%rdi
call   1050 <strchr@plt>
test   %rax,%rax
je     11e0 <read_bf+0x70>
movzbl (%r12,%rbx,1),%eax
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r12,%rcx,1)
jmp    11e0 <read_bf+0x70>
xor    %ebp,%ebp
movslq %ebp,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [9 x i8] c".,+-<>[]\00"
define i8* @read_bf(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = call i64 @malloc(i32 30000)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
store i32 0, i32* %7
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %1
%17 = load i8*, i8** %4
%18 = load i32*, i32** %5
%19 = call i32 @fread(i8* %17, i32 1, i32 30000, i32* %18)
store i32 %19, i32* %6
%20 = load i32*, i32** %5
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %3
br label %22
22: ; preds = %46, %16
%23 = load i32, i32* %3
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %49
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = call i64 @strchr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %26
%35 = load i8*, i8** %4
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %4
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 %39, i8* %44
br label %45
45: ; preds = %34, %26
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %22
49: ; preds = %22
%50 = load i8*, i8** %4
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %4
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [9 x i8] c".,+-<>[]\00"
define i8* @read_bf(i8* nocapture %0) {
%2 = tail call i64 @malloc(i32 30000)
%3 = inttoptr i64 %2 to i8*
%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 %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %1
%10 = tail call i32 @fread(i8* %3, i32 1, i32 30000, i32* %4)
%11 = tail call i32 @fclose(i32* %4)
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %31
13: ; preds = %9
%14 = zext i32 %10 to i64
br label %15
15: ; preds = %13, %27
%16 = phi i64 [ 0, %13 ], [ %29, %27 ]
%17 = phi i32 [ 0, %13 ], [ %28, %27 ]
%18 = getelementptr inbounds i8, i8* %3, i64 %16
%19 = load i8, i8* %18
%20 = tail call i64 @strchr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %15
%23 = load i8, i8* %18
%24 = add nsw i32 %17, 1
%25 = sext i32 %17 to i64
%26 = getelementptr inbounds i8, i8* %3, i64 %25
store i8 %23, i8* %26
br label %27
27: ; preds = %15, %22
%28 = phi i32 [ %24, %22 ], [ %17, %15 ]
%29 = add nuw nsw i64 %16, 1
%30 = icmp eq i64 %29, %14
br i1 %30, label %31, label %15
31: ; preds = %27, %9
%32 = phi i32 [ 0, %9 ], [ %28, %27 ]
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %3, i64 %33
store i8 0, i8* %34
ret i8* %3
}