#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int count = 0;
  int fd, i;
  struct stat statres;
  char *str;
  fd = open("/tmp/test", O_RDONLY);
  if (fd < 0) {
    perror("open()");
    exit(1);
  }
  if (fstat(fd, &statres) < 0) {
    perror("fstat()");
    exit(1);
  }
  str = mmap(NULL, statres.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (str == MAP_FAILED) {
    perror("mmap()");
    exit(1);
  }
  close(fd);
  for (i = 0; i < statres.st_size; i++)
    if (str[i] == 'a')
      count++;
  printf("%d\n", count);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11be <main+0x4e>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    11ea <main+0x7a>
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    0x2deb(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <main+0xcd>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1289 <main+0x119>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x61,%eax
jne    1276 <main+0x106>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    127b <main+0x10b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    124c <main+0xdc>
mov    -0x8(%rbp),%esi
lea    0xd8d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1199 <main+0x29>
lea    0xe93(%rip),%rdi
jmp    11f1 <main+0x81>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jns    11b3 <main+0x43>
lea    0xe80(%rip),%rdi
jmp    11f1 <main+0x81>
mov    (%rsp),%r14d
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2de3(%rip),%rax
cmp    (%rax),%r15
jne    1200 <main+0x90>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %r14d,%r14d
jle    1365 <main+0x1f5>
cmp    $0x8,%r14d
jae    121f <main+0xaf>
xor    %eax,%eax
xor    %ebx,%ebx
jmp    1350 <main+0x1e0>
mov    %r14d,%eax
and    $0xfffffff8,%eax
lea    -0x8(%rax),%rdx
mov    %rdx,%rcx
shr    $0x3,%rcx
add    $0x1,%rcx
test   %rdx,%rdx
jne    1248 <main+0xd8>
pxor   %xmm0,%xmm0
xor    %edx,%edx
pxor   %xmm1,%xmm1
jmp    12e5 <main+0x175>
mov    %rcx,%rsi
and    $0xfffffffffffffffe,%rsi
pxor   %xmm0,%xmm0
xor    %edx,%edx
movdqa 0xda3(%rip),%xmm2
movdqa 0xdab(%rip),%xmm3
pxor   %xmm1,%xmm1
nopl   0x0(%rax)
movd   (%r15,%rdx,1),%xmm5
movd   0x4(%r15,%rdx,1),%xmm4
pcmpeqb %xmm2,%xmm5
punpcklbw %xmm5,%xmm5
punpcklwd %xmm5,%xmm5
pand   %xmm3,%xmm5
paddd  %xmm0,%xmm5
pcmpeqb %xmm2,%xmm4
punpcklbw %xmm4,%xmm4
punpcklwd %xmm4,%xmm4
pand   %xmm3,%xmm4
paddd  %xmm1,%xmm4
movd   0x8(%r15,%rdx,1),%xmm0
movd   0xc(%r15,%rdx,1),%xmm1
pcmpeqb %xmm2,%xmm0
punpcklbw %xmm0,%xmm0
punpcklwd %xmm0,%xmm0
pand   %xmm3,%xmm0
paddd  %xmm5,%xmm0
pcmpeqb %xmm2,%xmm1
punpcklbw %xmm1,%xmm1
punpcklwd %xmm1,%xmm1
pand   %xmm3,%xmm1
paddd  %xmm4,%xmm1
add    $0x10,%rdx
add    $0xfffffffffffffffe,%rsi
jne    1270 <main+0x100>
test   $0x1,%cl
je     132f <main+0x1bf>
movd   (%r15,%rdx,1),%xmm2
movd   0x4(%r15,%rdx,1),%xmm3
movdqa 0xd01(%rip),%xmm4
pcmpeqb %xmm4,%xmm2
punpcklbw %xmm2,%xmm2
punpcklwd %xmm2,%xmm2
movdqa 0xcfd(%rip),%xmm5
pand   %xmm5,%xmm2
paddd  %xmm2,%xmm0
pcmpeqb %xmm4,%xmm3
punpcklbw %xmm3,%xmm3
punpcklwd %xmm3,%xmm3
pand   %xmm5,%xmm3
paddd  %xmm3,%xmm1
paddd  %xmm1,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%ebx
cmp    %r14,%rax
je     1365 <main+0x1f5>
xchg   %ax,%ax
xor    %ecx,%ecx
cmpb   $0x61,(%r15,%rax,1)
sete   %cl
add    %ecx,%ebx
add    $0x1,%rax
cmp    %rax,%r14
jne    1350 <main+0x1e0>
lea    0xcd4(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/tmp/test\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [8 x i8] c"fstat()\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %3
%16 = call i64 @fstat(i32 %15, %struct.stat* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %3
%27 = call i8* @mmap(i32* null, i32 %23, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
store i32 0, i32* %4
br label %37
37: ; preds = %54, %34
%38 = load i32, i32* %4
%39 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = icmp slt i32 %38, %40
br i1 %41, label %42, label %57
42: ; preds = %37
%43 = load i8*, i8** %6
%44 = load i32, i32* %4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 97
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %2
%52 = add nsw i32 %51, 1
store i32 %52, i32* %2
br label %53
53: ; preds = %50, %42
br label %54
54: ; preds = %53
%55 = load i32, i32* %4
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
br label %37
57: ; preds = %37
%58 = load i32, i32* %2
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/tmp/test\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [8 x i8] c"fstat()\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i64 @fstat(i32 %4, %struct.stat* %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %4, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = tail call i32 @close(i32 %4)
%28 = icmp sgt i32 %17, 0
br i1 %28, label %29, label %113
29: ; preds = %26
%30 = zext i32 %17 to i64
%31 = icmp ult i32 %17, 8
br i1 %31, label %100, label %32
32: ; preds = %29
%33 = and i64 %30, 4294967288
%34 = add nsw i64 %33, -8
%35 = lshr exact i64 %34, 3
%36 = add nuw nsw i64 %35, 1
%37 = and i64 %36, 1
%38 = icmp eq i64 %34, 0
br i1 %38, label %74, label %39
39: ; preds = %32
%40 = and i64 %36, 4611686018427387902
br label %41
41: ; preds = %41, %39
%42 = phi i64 [ 0, %39 ], [ %71, %41 ]
%43 = phi <4 x i32> [ zeroinitializer, %39 ], [ %69, %41 ]
%44 = phi <4 x i32> [ zeroinitializer, %39 ], [ %70, %41 ]
%45 = phi i64 [ 0, %39 ], [ %72, %41 ]
%46 = getelementptr inbounds i8, i8* %20, i64 %42
%47 = bitcast i8* %46 to <4 x i8>*
%48 = load <4 x i8>, <4 x i8>* %47
%49 = getelementptr inbounds i8, i8* %46, i64 4
%50 = bitcast i8* %49 to <4 x i8>*
%51 = load <4 x i8>, <4 x i8>* %50
%52 = icmp eq <4 x i8> %48, <i8 97, i8 97, i8 97, i8 97>
%53 = icmp eq <4 x i8> %51, <i8 97, i8 97, i8 97, i8 97>
%54 = zext <4 x i1> %52 to <4 x i32>
%55 = zext <4 x i1> %53 to <4 x i32>
%56 = add <4 x i32> %43, %54
%57 = add <4 x i32> %44, %55
%58 = or i64 %42, 8
%59 = getelementptr inbounds i8, i8* %20, i64 %58
%60 = bitcast i8* %59 to <4 x i8>*
%61 = load <4 x i8>, <4 x i8>* %60
%62 = getelementptr inbounds i8, i8* %59, i64 4
%63 = bitcast i8* %62 to <4 x i8>*
%64 = load <4 x i8>, <4 x i8>* %63
%65 = icmp eq <4 x i8> %61, <i8 97, i8 97, i8 97, i8 97>
%66 = icmp eq <4 x i8> %64, <i8 97, i8 97, i8 97, i8 97>
%67 = zext <4 x i1> %65 to <4 x i32>
%68 = zext <4 x i1> %66 to <4 x i32>
%69 = add <4 x i32> %56, %67
%70 = add <4 x i32> %57, %68
%71 = add nuw i64 %42, 16
%72 = add i64 %45, 2
%73 = icmp eq i64 %72, %40
br i1 %73, label %74, label %41
74: ; preds = %41, %32
%75 = phi <4 x i32> [ undef, %32 ], [ %69, %41 ]
%76 = phi <4 x i32> [ undef, %32 ], [ %70, %41 ]
%77 = phi i64 [ 0, %32 ], [ %71, %41 ]
%78 = phi <4 x i32> [ zeroinitializer, %32 ], [ %69, %41 ]
%79 = phi <4 x i32> [ zeroinitializer, %32 ], [ %70, %41 ]
%80 = icmp eq i64 %37, 0
br i1 %80, label %94, label %81
81: ; preds = %74
%82 = getelementptr inbounds i8, i8* %20, i64 %77
%83 = bitcast i8* %82 to <4 x i8>*
%84 = load <4 x i8>, <4 x i8>* %83
%85 = getelementptr inbounds i8, i8* %82, i64 4
%86 = bitcast i8* %85 to <4 x i8>*
%87 = load <4 x i8>, <4 x i8>* %86
%88 = icmp eq <4 x i8> %84, <i8 97, i8 97, i8 97, i8 97>
%89 = icmp eq <4 x i8> %87, <i8 97, i8 97, i8 97, i8 97>
%90 = zext <4 x i1> %88 to <4 x i32>
%91 = zext <4 x i1> %89 to <4 x i32>
%92 = add <4 x i32> %78, %90
%93 = add <4 x i32> %79, %91
br label %94
94: ; preds = %74, %81
%95 = phi <4 x i32> [ %75, %74 ], [ %92, %81 ]
%96 = phi <4 x i32> [ %76, %74 ], [ %93, %81 ]
%97 = add <4 x i32> %96, %95
%98 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %97)
%99 = icmp eq i64 %33, %30
br i1 %99, label %113, label %100
100: ; preds = %29, %94
%101 = phi i64 [ 0, %29 ], [ %33, %94 ]
%102 = phi i32 [ 0, %29 ], [ %98, %94 ]
br label %103
103: ; preds = %100, %103
%104 = phi i64 [ %111, %103 ], [ %101, %100 ]
%105 = phi i32 [ %110, %103 ], [ %102, %100 ]
%106 = getelementptr inbounds i8, i8* %20, i64 %104
%107 = load i8, i8* %106
%108 = icmp eq i8 %107, 97
%109 = zext i1 %108 to i32
%110 = add nuw nsw i32 %105, %109
%111 = add nuw nsw i64 %104, 1
%112 = icmp eq i64 %111, %30
br i1 %112, label %113, label %103
113: ; preds = %103, %94, %26
%114 = phi i32 [ 0, %26 ], [ %98, %94 ], [ %110, %103 ]
%115 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %114)
%116 = tail call i32 @exit(i32 0)
unreachable
}