#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 EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int read_file(FILE *fp, char *code, size_t length) {
  int ch;
  size_t cnt = 0;
  for (; (ch = fgetc(fp)) != EOF; cnt++) {
    if (cnt > length) {
      fprintf(stderr, "Buffer overflow!\n");
      return 0;
    }
    switch (ch) {
    case '>':
    case '<':
    case '+':
    case '-':
    case '.':
    case ',':
    case '[':
    case ']':
      *code++ = (char)ch;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%eax
je     120d <read_file+0xed>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1186 <read_file+0x66>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <read_file+0xf4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
add    $0xffffffd5,%eax
sub    $0x4,%eax
jb     11e1 <read_file+0xc1>
jmp    119d <read_file+0x7d>
mov    -0x34(%rbp),%eax
sub    $0x3c,%eax
je     11e1 <read_file+0xc1>
jmp    11ae <read_file+0x8e>
mov    -0x34(%rbp),%eax
sub    $0x3e,%eax
je     11e1 <read_file+0xc1>
jmp    11bf <read_file+0x9f>
mov    -0x34(%rbp),%eax
sub    $0x5b,%eax
je     11e1 <read_file+0xc1>
jmp    11d0 <read_file+0xb0>
mov    -0x34(%rbp),%eax
sub    $0x5d,%eax
jne    11f7 <read_file+0xd7>
jmp    11e1 <read_file+0xc1>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11fc <read_file+0xdc>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    113c <read_file+0x1c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r13
call   1030 <fgetc@plt>
mov    0x2ea8(%rip),%r12
mov    $0x1,%ecx
cmp    (%r12),%eax
je     11aa <read_file+0x8a>
mov    $0xffffffffffffffff,%rbx
movabs $0x50000000a000f,%r14
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    %r15,%rbx
ja     1193 <read_file+0x73>
lea    -0x2b(%rax),%ecx
cmp    $0x32,%ecx
ja     117e <read_file+0x5e>
bt     %rcx,%r14
jae    117e <read_file+0x5e>
mov    %al,0x0(%rbp)
add    $0x1,%rbp
mov    %r13,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%eax
jne    1160 <read_file+0x40>
mov    $0x1,%ecx
jmp    11aa <read_file+0x8a>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Buffer overflow!\0A\00"
define i32 @read_file(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %10
10: ; preds = %30, %3
%11 = load i32*, i32** %5
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %33
15: ; preds = %10
%16 = load i64, i64* %9
%17 = load i64, i64* %7
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %34
22: ; preds = %15
%23 = load i32, i32* %8
switch i32 %23, label %29 [
i32 62, label %24
i32 60, label %24
i32 43, label %24
i32 45, label %24
i32 46, label %24
i32 44, label %24
i32 91, label %24
i32 93, label %24
]
24: ; preds = %22, %22, %22, %22, %22, %22, %22, %22
%25 = load i32, i32* %8
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %6
store i8 %26, i8* %27
br label %29
29: ; preds = %24, %22
br label %30
30: ; preds = %29
%31 = load i64, i64* %9
%32 = add i64 %31, 1
store i64 %32, i64* %9
br label %10
33: ; preds = %10
store i32 1, i32* %4
br label %34
34: ; preds = %33, %19
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@EOF = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Buffer overflow!\0A\00"
define i32 @read_file(i32* nocapture %0, i8* nocapture writeonly %1, i64 %2) {
%4 = tail call i32 @fgetc(i32* %0)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %25, label %7
7: ; preds = %3, %19
%8 = phi i32 [ %22, %19 ], [ %4, %3 ]
%9 = phi i64 [ %21, %19 ], [ 0, %3 ]
%10 = phi i8* [ %20, %19 ], [ %1, %3 ]
%11 = icmp ugt i64 %9, %2
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
15: ; preds = %7
switch i32 %8, label %19 [
i32 62, label %16
i32 60, label %16
i32 43, label %16
i32 45, label %16
i32 46, label %16
i32 44, label %16
i32 91, label %16
i32 93, label %16
]
16: ; preds = %15, %15, %15, %15, %15, %15, %15, %15
%17 = trunc i32 %8 to i8
%18 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %17, i8* %10
br label %19
19: ; preds = %15, %16
%20 = phi i8* [ %10, %15 ], [ %18, %16 ]
%21 = add i64 %9, 1
%22 = tail call i32 @fgetc(i32* %0)
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %7
25: ; preds = %19, %3, %12
%26 = phi i32 [ 0, %12 ], [ 1, %3 ], [ 1, %19 ]
ret i32 %26
}