#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
int test_loads(char *, void *, size_t);
int main() {
  unsigned int NUM_PAGES = 20;
  size_t PAGE_SIZE = 4096;
  size_t buf_size = NUM_PAGES * PAGE_SIZE;
  char *buffer = (char *)malloc(buf_size * sizeof(char));
  for (int i = 0; i < buf_size; i++) {
    buffer[i] = (char)i;
  }
  FILE *stream = fopen("test.data", "wb");
  fwrite(buffer, sizeof(char), buf_size, stream);
  fclose(stream);
  int fd = open("test.data", O_RDONLY);
  void *addr = mmap(NULL, buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap failed");
    return -1;
  }
  int num_failures = test_loads(buffer, addr, buf_size);
  if (num_failures != 0) {
    fprintf(stdout, "Test failed with %d errors.", num_failures);
    return -1;
  }
  fprintf(stdout, "Test passed!\n");
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x14,-0x8(%rbp)
movq   $0x1000,-0x10(%rbp)
mov    -0x8(%rbp),%eax
imul   -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    120e <main+0x6e>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11e2 <main+0x42>
lea    0xdeb(%rip),%rdi
lea    0xdee(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xdac(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <main+0x112>
lea    0xd6c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <main+0x17d>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <test_loads@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12f6 <main+0x156>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%edx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <main+0x17d>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x14000,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movdqa 0xe47(%rip),%xmm2
mov    $0x70,%eax
movdqa 0xe49(%rip),%xmm8
movdqa 0xe50(%rip),%xmm9
movdqa 0xe58(%rip),%xmm3
movdqa 0xe60(%rip),%xmm4
movdqa 0xe68(%rip),%xmm5
movdqa 0xe70(%rip),%xmm6
movdqa 0xe78(%rip),%xmm7
movdqa 0xe80(%rip),%xmm1
movdqu %xmm2,-0x70(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm8,%xmm0
movdqu %xmm0,-0x60(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm9,%xmm0
movdqu %xmm0,-0x50(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm3,%xmm0
movdqu %xmm0,-0x40(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm4,%xmm0
movdqu %xmm0,-0x30(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm5,%xmm0
movdqu %xmm0,-0x20(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm6,%xmm0
movdqu %xmm0,-0x10(%rbx,%rax,1)
movdqa %xmm2,%xmm0
paddb  %xmm7,%xmm0
movdqu %xmm0,(%rbx,%rax,1)
pxor   %xmm1,%xmm2
sub    $0xffffffffffffff80,%rax
cmp    $0x14070,%rax
jne    1200 <main+0x60>
lea    0xe10(%rip),%r14
lea    0xe13(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbp
mov    $0x14000,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
mov    0x2d31(%rip),%rcx
mov    (%rcx),%edx
mov    0x2cf8(%rip),%rcx
mov    (%rcx),%ecx
mov    $0x14000,%esi
xor    %edi,%edi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2cd5(%rip),%rcx
cmp    (%rcx),%rax
je     1343 <main+0x1a3>
mov    $0x14000,%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <test_loads@plt>
mov    0x2cc9(%rip),%rcx
mov    (%rcx),%edi
test   %eax,%eax
je     1324 <main+0x184>
lea    0xd95(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    133c <main+0x19c>
lea    0xd9a(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd53(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    133c <main+0x19c>

LLVM IR (-O0):
@.str = [10 x i8] c"test.data\00"
@.str.1 = [3 x i8] c"wb\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@stdout = global i32 0
@.str.3 = [28 x i8] c"Test failed with %d errors.\00"
@.str.4 = [14 x i8] c"Test passed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %1
store i32 20, i32* %2
store i64 4096, i64* %3
%11 = load i32, i32* %2
%12 = zext i32 %11 to i64
%13 = load i64, i64* %3
%14 = mul i64 %12, %13
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = mul i64 %15, 1
%17 = call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %5
store i32 0, i32* %6
br label %19
19: ; preds = %31, %0
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = load i64, i64* %4
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i32, i32* %6
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
br label %31
31: ; preds = %24
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %19
34: ; preds = %19
%35 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %7
%36 = load i8*, i8** %5
%37 = load i64, i64* %4
%38 = load i32*, i32** %7
%39 = call i32 @fwrite(i8* %36, i32 1, i64 %37, i32* %38)
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %42)
store i32 %43, i32* %8
%44 = load i64, i64* %4
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @MAP_PRIVATE
%47 = load i32, i32* %8
%48 = call i8* @mmap(i32* null, i64 %44, i32 %45, i32 %46, i32 %47, i32 0)
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = load i8*, i8** @MAP_FAILED
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %54
52: ; preds = %34
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %70
54: ; preds = %34
%55 = load i8*, i8** %5
%56 = load i8*, i8** %9
%57 = load i64, i64* %4
%58 = call i32 @test_loads(i8* %55, i8* %56, i64 %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %54
%62 = load i32, i32* @stdout
%63 = load i32, i32* %10
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %63)
store i32 -1, i32* %1
br label %70
65: ; preds = %54
%66 = load i32, i32* @stdout
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%68 = load i8*, i8** %5
%69 = call i32 @free(i8* %68)
store i32 0, i32* %1
br label %70
70: ; preds = %65, %61, %52
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@.str = [10 x i8] c"test.data\00"
@.str.1 = [3 x i8] c"wb\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@stdout = global i32 0
@.str.3 = [28 x i8] c"Test failed with %d errors.\00"
@.str.4 = [14 x i8] c"Test passed!\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i64 81920)
%2 = inttoptr i64 %1 to i8*
br label %3
3: ; preds = %3, %0
%4 = phi i64 [ 0, %0 ], [ %36, %3 ]
%5 = phi <16 x i8> [ <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, %0 ], [ %37, %3 ]
%6 = getelementptr inbounds i8, i8* %2, i64 %4
%7 = bitcast i8* %6 to <16 x i8>*
store <16 x i8> %5, <16 x i8>* %7
%8 = or i64 %4, 16
%9 = add <16 x i8> %5, <i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16>
%10 = getelementptr inbounds i8, i8* %2, i64 %8
%11 = bitcast i8* %10 to <16 x i8>*
store <16 x i8> %9, <16 x i8>* %11
%12 = or i64 %4, 32
%13 = add <16 x i8> %5, <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>
%14 = getelementptr inbounds i8, i8* %2, i64 %12
%15 = bitcast i8* %14 to <16 x i8>*
store <16 x i8> %13, <16 x i8>* %15
%16 = or i64 %4, 48
%17 = add <16 x i8> %5, <i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48>
%18 = getelementptr inbounds i8, i8* %2, i64 %16
%19 = bitcast i8* %18 to <16 x i8>*
store <16 x i8> %17, <16 x i8>* %19
%20 = or i64 %4, 64
%21 = add <16 x i8> %5, <i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64>
%22 = getelementptr inbounds i8, i8* %2, i64 %20
%23 = bitcast i8* %22 to <16 x i8>*
store <16 x i8> %21, <16 x i8>* %23
%24 = or i64 %4, 80
%25 = add <16 x i8> %5, <i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80>
%26 = getelementptr inbounds i8, i8* %2, i64 %24
%27 = bitcast i8* %26 to <16 x i8>*
store <16 x i8> %25, <16 x i8>* %27
%28 = or i64 %4, 96
%29 = add <16 x i8> %5, <i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96>
%30 = getelementptr inbounds i8, i8* %2, i64 %28
%31 = bitcast i8* %30 to <16 x i8>*
store <16 x i8> %29, <16 x i8>* %31
%32 = or i64 %4, 112
%33 = add <16 x i8> %5, <i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112>
%34 = getelementptr inbounds i8, i8* %2, i64 %32
%35 = bitcast i8* %34 to <16 x i8>*
store <16 x i8> %33, <16 x i8>* %35
%36 = add nuw nsw i64 %4, 128
%37 = xor <16 x i8> %5, <i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128>
%38 = icmp eq i64 %36, 81920
br i1 %38, label %39, label %3
39: ; preds = %3
%40 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%41 = tail call i32 @fwrite(i8* %2, i32 1, i64 81920, i32* %40)
%42 = tail call i32 @fclose(i32* %40)
%43 = load i32, i32* @O_RDONLY
%44 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @MAP_PRIVATE
%47 = tail call i8* @mmap(i32* null, i64 81920, i32 %45, i32 %46, i32 %44, i32 0)
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %52
50: ; preds = %39
%51 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %61
52: ; preds = %39
%53 = tail call i32 @test_loads(i8* %2, i8* %47, i64 81920)
%54 = icmp eq i32 %53, 0
%55 = load i32, i32* @stdout
br i1 %54, label %58, label %56
56: ; preds = %52
%57 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %53)
br label %61
58: ; preds = %52
%59 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%60 = tail call i32 @free(i8* %2)
br label %61
61: ; preds = %56, %58, %50
%62 = phi i32 [ -1, %50 ], [ -1, %56 ], [ 0, %58 ]
ret i32 %62
}