#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 size_t ssize_t;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ invalid(char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
size_t read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int checkfile(char *filename) {
  char buf[BUFSIZ];
  size_t pos = 0;
  int fd = strcmp(filename, "-") == 0 ? 0 : open(filename, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  ssize_t r;
  while ((r = read(fd, buf, BUFSIZ)) > 0) {
    for (ssize_t i = 0; i < r; i++)
      if (invalid(buf[i])) {
        printf("%s: 0x%02x @ pos %lu\n", filename, buf[i] & 0xff, pos + i);
        return 1;
      }
    pos += r;
  }
  close(fd);
  printf("%s: OK\n", filename);
  return 0;
}


x86 assembly (clang, -O0):
<checkfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11d4 <checkfile+0x64>
xor    %eax,%eax
mov    %eax,-0x4c(%rbp)
jmp    11e9 <checkfile+0x79>
mov    -0x10(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1218 <checkfile+0xa8>
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12ff <checkfile+0x18f>
jmp    121d <checkfile+0xad>
mov    -0x48(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jbe    12d7 <checkfile+0x167>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    12c6 <checkfile+0x156>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <invalid@plt>
cmp    $0x0,%rax
je     12b0 <checkfile+0x140>
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
and    $0xff,%edx
mov    -0x28(%rbp),%rcx
add    -0x40(%rbp),%rcx
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12ff <checkfile+0x18f>
jmp    12b5 <checkfile+0x145>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1248 <checkfile+0xd8>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    121d <checkfile+0xad>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rsi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkfile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe5b(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d9 <checkfile+0x69>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11dc <checkfile+0x6c>
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
jmp    1279 <checkfile+0x109>
xor    %r15d,%r15d
mov    %r14,-0x38(%rbp)
mov    (%rbx),%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <read@plt>
test   %rax,%rax
je     123f <checkfile+0xcf>
mov    %rax,%rbx
xor    %r12d,%r12d
mov    %r15d,-0x2c(%rbp)
xor    %r15d,%r15d
nop
movsbl 0x0(%r13,%r15,1),%r14d
mov    %r14d,%edi
call   1070 <invalid@plt>
test   %rax,%rax
jne    125d <checkfile+0xed>
add    $0x1,%r15
cmp    %r15,%rbx
jne    1200 <checkfile+0x90>
add    %rbx,%r12
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%r15d
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fc <checkfile+0x8c>
mov    %r15d,%edi
call   1040 <close@plt>
lea    0xdcf(%rip),%rdi
xor    %ebx,%ebx
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    127e <checkfile+0x10e>
movzbl %r14b,%edx
add    %r15,%r12
lea    0xd9c(%rip),%rdi
mov    -0x38(%rbp),%rsi
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"-\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [22 x i8] c"%s: 0x%02x @ pos %lu\0A\00"
@.str.3 = [8 x i8] c"%s: OK\0A\00"
define i32 @checkfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
store i64 0, i64* %6
%15 = load i8*, i8** %3
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
br label %23
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
br label %23
23: ; preds = %19, %18
%24 = phi i32 [ 0, %18 ], [ %22, %19 ]
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
store i32 1, i32* %8
br label %70
29: ; preds = %23
br label %30
30: ; preds = %61, %29
%31 = load i32, i32* %7
%32 = load i32, i32* @BUFSIZ
%33 = call i64 @read(i32 %31, i8* %14, i32 %32)
store i64 %33, i64* %9
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %65
35: ; preds = %30
store i64 0, i64* %10
br label %36
36: ; preds = %58, %35
%37 = load i64, i64* %10
%38 = load i64, i64* %9
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %61
40: ; preds = %36
%41 = load i64, i64* %10
%42 = getelementptr inbounds i8, i8* %14, i64 %41
%43 = load i8, i8* %42
%44 = call i64 @invalid(i8 signext %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %40
%47 = load i8*, i8** %3
%48 = load i64, i64* %10
%49 = getelementptr inbounds i8, i8* %14, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = and i32 %51, 255
%53 = load i64, i64* %6
%54 = load i64, i64* %10
%55 = add i64 %53, %54
%56 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %52, i64 %55)
store i32 1, i32* %2
store i32 1, i32* %8
br label %70
57: ; preds = %40
br label %58
58: ; preds = %57
%59 = load i64, i64* %10
%60 = add i64 %59, 1
store i64 %60, i64* %10
br label %36
61: ; preds = %36
%62 = load i64, i64* %9
%63 = load i64, i64* %6
%64 = add i64 %63, %62
store i64 %64, i64* %6
br label %30
65: ; preds = %30
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = load i8*, i8** %3
%69 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %68)
store i32 0, i32* %2
store i32 1, i32* %8
br label %70
70: ; preds = %65, %46, %27
%71 = load i8*, i8** %4
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"-\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [22 x i8] c"%s: 0x%02x @ pos %lu\0A\00"
@.str.3 = [8 x i8] c"%s: OK\0A\00"
define i32 @checkfile(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %1, %7
%12 = phi i32 [ %9, %7 ], [ 0, %1 ]
%13 = load i32, i32* @BUFSIZ
%14 = call i64 @read(i32 %12, i8* %4, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %39, label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %42
18: ; preds = %11, %34
%19 = phi i64 [ %37, %34 ], [ %14, %11 ]
%20 = phi i64 [ %35, %34 ], [ 0, %11 ]
br label %21
21: ; preds = %18, %31
%22 = phi i64 [ 0, %18 ], [ %32, %31 ]
%23 = getelementptr inbounds i8, i8* %4, i64 %22
%24 = load i8, i8* %23
%25 = tail call i64 @invalid(i8 signext %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = zext i8 %24 to i32
%29 = add i64 %22, %20
%30 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %28, i64 %29)
br label %42
31: ; preds = %21
%32 = add nuw i64 %22, 1
%33 = icmp eq i64 %32, %19
br i1 %33, label %34, label %21
34: ; preds = %31
%35 = add i64 %19, %20
%36 = load i32, i32* @BUFSIZ
%37 = call i64 @read(i32 %12, i8* %4, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %18
39: ; preds = %34, %11
%40 = tail call i32 @close(i32 %12)
%41 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %42
42: ; preds = %27, %39, %16
%43 = phi i32 [ 1, %16 ], [ 1, %27 ], [ 0, %39 ]
ret i32 %43
}