#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*/ compile(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
void parse(FILE *f, char *name) {
  FILE *out = fopen(".temp.c", "w");
  fprintf(out,
          "#include <stdio.h>\n#include <stdlib.h>\n\nint bfIncrement(int "
          "curr)\n{\n    if(curr >= 1024)\n    {\n        return 0;\n    }\n   "
          " else\n    {\n        return curr+1;\n    }\n}\n\nint "
          "bfDecrement(int curr)\n{\n    if(curr <= 0)\n    {\n        return "
          "1024;\n    }\n    else\n    {\n        return curr-1;\n    "
          "}\n}\n\nint main(int argc, char* argv[])\n{\n	char* ptr = "
          "(char*)malloc(sizeof(char)*1024);\n	int index = 0;\n");
  char c = '/';
  while (c != EOF) {
    if (c == '>') {
      fprintf(out, "index = bfIncrement(index);\n");
    }
    if (c == '<') {
      fprintf(out, "index = bfDecrement(index);\n");
    }
    if (c == '+') {
      fprintf(out, "ptr[index]++;\n");
    }
    if (c == '-') {
      fprintf(out, "ptr[index]--;\n");
    }
    if (c == '.') {
      fprintf(out, "fputc(ptr[index], stdout);\n");
    }
    if (c == ',') {
      fprintf(out, "ptr[index] = fgetc(stdin);\n");
    }
    if (c == '[') {
      fprintf(out, "while(ptr[index])\n{\n");
    }
    if (c == ']') {
      fprintf(out, "}\n");
    }
    c = fgetc(f);
  }
  fprintf(out, "return 0;\n");
  fprintf(out, "}\n");
  fclose(out);
  compile(name);
}


x86 assembly (clang, -O0):
<parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe99(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1060 <fprintf@plt>
movb   $0x2f,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
mov    0x2e52(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     129a <parse+0x14a>
movsbl -0x19(%rbp),%eax
cmp    $0x3e,%eax
jne    11be <parse+0x6e>
mov    -0x18(%rbp),%rdi
lea    0xfd8(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x3c,%eax
jne    11db <parse+0x8b>
mov    -0x18(%rbp),%rdi
lea    0xfd8(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x2b,%eax
jne    11f8 <parse+0xa8>
mov    -0x18(%rbp),%rdi
lea    0xfd8(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x2d,%eax
jne    1215 <parse+0xc5>
mov    -0x18(%rbp),%rdi
lea    0xfca(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x2e,%eax
jne    1232 <parse+0xe2>
mov    -0x18(%rbp),%rdi
lea    0xfbc(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x2c,%eax
jne    124f <parse+0xff>
mov    -0x18(%rbp),%rdi
lea    0xfbb(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x5b,%eax
jne    126c <parse+0x11c>
mov    -0x18(%rbp),%rdi
lea    0xfba(%rip),%rsi
call   1060 <fprintf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x5d,%eax
jne    1289 <parse+0x139>
mov    -0x18(%rbp),%rdi
lea    0xfb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x19(%rbp)
jmp    118b <parse+0x3b>
mov    -0x18(%rbp),%rdi
lea    0xf94(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xf81(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <compile@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xf64(%rip),%rdi
lea    0xf65(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xf58(%rip),%rdi
mov    $0x186,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    0x2e51(%rip),%rbp
cmpb   $0x2f,0x0(%rbp)
jne    11ed <parse+0x9d>
lea    0x1161(%rip),%rdi
mov    $0xa,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
lea    0x1145(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <compile@plt>
mov    $0x2f,%al
lea    0xe0a(%rip),%r12
lea    0x1105(%rip),%r13
jmp    1229 <parse+0xd9>
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rdi
nopl   0x0(%rax)
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r15,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%rbp),%al
je     119d <parse+0x4d>
add    $0xd5,%al
cmp    $0x32,%al
ja     1218 <parse+0xc8>
movzbl %al,%eax
movslq (%r12,%rax,4),%rax
add    %r12,%rax
jmp    *%rax
mov    $0xe,%esi
mov    $0x1,%edx
lea    0x104b(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0x1b,%esi
mov    $0x1,%edx
lea    0x1072(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0xe,%esi
mov    $0x1,%edx
lea    0x1034(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0x1b,%esi
mov    $0x1,%edx
lea    0x1030(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0x1c,%esi
mov    $0x1,%edx
lea    0xfe2(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0x1c,%esi
mov    $0x1,%edx
lea    0xfaf(%rip),%rdi
jmp    1210 <parse+0xc0>
mov    $0x14,%esi
mov    $0x1,%edx
lea    0x1029(%rip),%rdi
jmp    1210 <parse+0xc0>

LLVM IR (-O0):
@.str = [8 x i8] c".temp.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [391 x i8] c"#include <stdio.h>\0A#include <stdlib.h>\0A\0Aint bfIncrement(int curr)\0A{\0A if(curr >= 1024)\0A {\0A return 0;\0A }\0A else\0A {\0A return curr+1;\0A }\0A}\0A\0Aint bfDecrement(int curr)\0A{\0A if(curr <= 0)\0A {\0A return 1024;\0A }\0A else\0A {\0A return curr-1;\0A }\0A}\0A\0Aint main(int argc, char* argv[])\0A{\0A\09char* ptr = (char*)malloc(sizeof(char)*1024);\0A\09int index = 0;\0A\00"
@EOF = global i8 0
@.str.3 = [29 x i8] c"index = bfIncrement(index);\0A\00"
@.str.4 = [29 x i8] c"index = bfDecrement(index);\0A\00"
@.str.5 = [15 x i8] c"ptr[index]++;\0A\00"
@.str.6 = [15 x i8] c"ptr[index]--;\0A\00"
@.str.7 = [28 x i8] c"fputc(ptr[index], stdout);\0A\00"
@.str.8 = [28 x i8] c"ptr[index] = fgetc(stdin);\0A\00"
@.str.9 = [21 x i8] c"while(ptr[index])\0A{\0A\00"
@.str.10 = [3 x i8] c"}\0A\00"
@.str.11 = [11 x i8] c"return 0;\0A\00"
define void @parse(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8
store i32* %0, i32** %3
store i8* %1, i8** %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([391 x i8], [391 x i8]* @.str.2, i64 0, i64 0))
store i8 47, i8* %6
br label %10
10: ; preds = %72, %2
%11 = load i8, i8* %6
%12 = sext i8 %11 to i32
%13 = load i8, i8* @EOF
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %12, %14
br i1 %15, label %16, label %75
16: ; preds = %10
%17 = load i8, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 62
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32*, i32** %5
%22 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %20, %16
%24 = load i8, i8* %6
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 60
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32*, i32** %5
%29 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %27, %23
%31 = load i8, i8* %6
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 43
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32*, i32** %5
%36 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %34, %30
%38 = load i8, i8* %6
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 45
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32*, i32** %5
%43 = call i32 @fprintf(i32* %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %41, %37
%45 = load i8, i8* %6
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32*, i32** %5
%50 = call i32 @fprintf(i32* %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %51
51: ; preds = %48, %44
%52 = load i8, i8* %6
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 44
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32*, i32** %5
%57 = call i32 @fprintf(i32* %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %55, %51
%59 = load i8, i8* %6
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 91
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i32*, i32** %5
%64 = call i32 @fprintf(i32* %63, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0))
br label %65
65: ; preds = %62, %58
%66 = load i8, i8* %6
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 93
br i1 %68, label %69, label %72
69: ; preds = %65
%70 = load i32*, i32** %5
%71 = call i32 @fprintf(i32* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
br label %72
72: ; preds = %69, %65
%73 = load i32*, i32** %3
%74 = call signext i8 @fgetc(i32* %73)
store i8 %74, i8* %6
br label %10
75: ; preds = %10
%76 = load i32*, i32** %5
%77 = call i32 @fprintf(i32* %76, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0))
%78 = load i32*, i32** %5
%79 = call i32 @fprintf(i32* %78, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%80 = load i32*, i32** %5
%81 = call i32 @fclose(i32* %80)
%82 = load i8*, i8** %4
%83 = call i32 @compile(i8* %82)
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c".temp.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [391 x i8] c"#include <stdio.h>\0A#include <stdlib.h>\0A\0Aint bfIncrement(int curr)\0A{\0A if(curr >= 1024)\0A {\0A return 0;\0A }\0A else\0A {\0A return curr+1;\0A }\0A}\0A\0Aint bfDecrement(int curr)\0A{\0A if(curr <= 0)\0A {\0A return 1024;\0A }\0A else\0A {\0A return curr-1;\0A }\0A}\0A\0Aint main(int argc, char* argv[])\0A{\0A\09char* ptr = (char*)malloc(sizeof(char)*1024);\0A\09int index = 0;\0A\00"
@EOF = global i8 0
@.str.3 = [29 x i8] c"index = bfIncrement(index);\0A\00"
@.str.4 = [29 x i8] c"index = bfDecrement(index);\0A\00"
@.str.5 = [15 x i8] c"ptr[index]++;\0A\00"
@.str.6 = [15 x i8] c"ptr[index]--;\0A\00"
@.str.7 = [28 x i8] c"fputc(ptr[index], stdout);\0A\00"
@.str.8 = [28 x i8] c"ptr[index] = fgetc(stdin);\0A\00"
@.str.9 = [21 x i8] c"while(ptr[index])\0A{\0A\00"
@.str.10 = [3 x i8] c"}\0A\00"
@.str.11 = [11 x i8] c"return 0;\0A\00"
define void @parse(i32* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([391 x i8], [391 x i8]* @.str.2, i64 0, i64 0), i64 390, i64 1, i32* %3)
%5 = load i8, i8* @EOF
%6 = icmp eq i8 %5, 47
br i1 %6, label %29, label %7
7: ; preds = %2, %25
%8 = phi i8 [ %26, %25 ], [ 47, %2 ]
switch i8 %8, label %25 [
i8 62, label %9
i8 60, label %11
i8 43, label %13
i8 45, label %15
i8 46, label %17
i8 44, label %19
i8 91, label %21
i8 93, label %23
]
9: ; preds = %7
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 28, i64 1, i32* %3)
br label %25
11: ; preds = %7
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 28, i64 1, i32* %3)
br label %25
13: ; preds = %7
%14 = tail call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i64 14, i64 1, i32* %3)
br label %25
15: ; preds = %7
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i64 14, i64 1, i32* %3)
br label %25
17: ; preds = %7
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i64 27, i64 1, i32* %3)
br label %25
19: ; preds = %7
%20 = tail call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i64 27, i64 1, i32* %3)
br label %25
21: ; preds = %7
%22 = tail call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i64 20, i64 1, i32* %3)
br label %25
23: ; preds = %7
%24 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i64 2, i64 1, i32* %3)
br label %25
25: ; preds = %21, %19, %17, %15, %13, %11, %9, %7, %23
%26 = tail call signext i8 @fgetc(i32* %0)
%27 = load i8, i8* @EOF
%28 = icmp eq i8 %26, %27
br i1 %28, label %29, label %7
29: ; preds = %25, %2
%30 = tail call i64 @fwrite(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i64 10, i64 1, i32* %3)
%31 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i64 2, i64 1, i32* %3)
%32 = tail call i32 @fclose(i32* %3)
%33 = tail call i32 @compile(i8* %1)
ret void
}