#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int atoi(char const *);
int /*<<< orphan*/ basic_test(unsigned short *, unsigned short *, size_t const,
                              size_t const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(unsigned short *);
unsigned short *malloc(size_t);
int /*<<< orphan*/ prefetch_test(unsigned short *, unsigned short *,
                                 size_t const, size_t const, int);
unsigned short random();
int /*<<< orphan*/ srandom(int);
int /*<<< orphan*/ stdout;
int main(int argc, const char *argv[]) {
  assert(sizeof(short) == 2);
  size_t buf_bytes = 64 * 1024 * 1024;
  if (argc >= 2) {
    buf_bytes = atoi(argv[1]) * 1024 * 1024;
    assert(buf_bytes > 0);
  }
  assert((buf_bytes & (buf_bytes - 1)) == 0);
  fprintf(stdout, "buffer size: %d MiB\n", (int)(buf_bytes / 1024 / 1024));
  const size_t buf_short = buf_bytes / 2;
  unsigned short *buf = malloc(buf_bytes);
  unsigned short *buf_end = buf + buf_short;
  srandom(1234);
  size_t i;
  for (i = 0; i < buf_short; ++i)
    buf[i] = random();
  basic_test(buf, buf_end, buf_short, buf_short);
  prefetch_test(buf, buf_end, buf_short, buf_short, 2);
  prefetch_test(buf, buf_end, buf_short, buf_short, 3);
  prefetch_test(buf, buf_end, buf_short, buf_short, 4);
  prefetch_test(buf, buf_end, buf_short, buf_short, 6);
  prefetch_test(buf, buf_end, buf_short, buf_short, 8);
  prefetch_test(buf, buf_end, buf_short, buf_short, 12);
  prefetch_test(buf, buf_end, buf_short, buf_short, 16);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
call   1070 <assert@plt>
movq   $0x4000000,-0x18(%rbp)
04
cmpl   $0x2,-0x8(%rbp)
jl     11ed <main+0x5d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
shl    $0xa,%eax
shl    $0xa,%eax
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
shr    $0xa,%rax
shr    $0xa,%rax
mov    %eax,%edx
lea    0xdd5(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    $0x4d2,%edi
call   1040 <srandom@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12a3 <main+0x113>
mov    $0x0,%al
call   1090 <random@plt>
mov    %ax,%dx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    126e <main+0xde>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <basic_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x2,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x3,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x6,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0xc,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1060 <prefetch_test@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%edi
call   1070 <assert@plt>
mov    $0x4000000,%r12d
cmp    $0x2,%ebp
jl     11d3 <main+0x43>
mov    0x8(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
shl    $0x14,%ecx
movslq %ecx,%r12
xor    %edi,%edi
test   %eax,%eax
setne  %dil
call   1070 <assert@plt>
lea    -0x1(%r12),%rax
xor    %edi,%edi
test   %rax,%r12
sete   %dil
call   1070 <assert@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rdx
shr    $0x14,%rdx
lea    0xe03(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r12,%r14
shr    %r14
mov    %r12,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r13
lea    (%rax,%r14,2),%r15
mov    $0x4d2,%edi
call   1040 <srandom@plt>
cmp    $0x2,%r12
jb     1256 <main+0xc6>
cmp    $0x2,%r14
mov    $0x1,%ebp
cmovae %r14,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <random@plt>
mov    %ax,0x0(%r13,%rbx,2)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1240 <main+0xb0>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
call   1050 <basic_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x2,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x3,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x4,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x6,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x8,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0xc,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r14,%rcx
mov    $0x10,%r8d
call   1060 <prefetch_test@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [21 x i8] c"buffer size: %d MiB\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i16*
%9 = alloca i16*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @assert(i32 1)
store i64 67108864, i64* %6
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %26
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
%19 = mul nsw i32 %18, 1024
%20 = mul nsw i32 %19, 1024
%21 = sext i32 %20 to i64
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp ugt i64 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
br label %26
26: ; preds = %14, %2
%27 = load i64, i64* %6
%28 = load i64, i64* %6
%29 = sub i64 %28, 1
%30 = and i64 %27, %29
%31 = icmp eq i64 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* @stdout
%35 = load i64, i64* %6
%36 = udiv i64 %35, 1024
%37 = udiv i64 %36, 1024
%38 = trunc i64 %37 to i32
%39 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = load i64, i64* %6
%41 = udiv i64 %40, 2
store i64 %41, i64* %7
%42 = load i64, i64* %6
%43 = call i16* @malloc(i64 %42)
store i16* %43, i16** %8
%44 = load i16*, i16** %8
%45 = load i64, i64* %7
%46 = getelementptr inbounds i16, i16* %44, i64 %45
store i16* %46, i16** %9
%47 = call i32 @srandom(i32 1234)
store i64 0, i64* %10
br label %48
48: ; preds = %57, %26
%49 = load i64, i64* %10
%50 = load i64, i64* %7
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = call zeroext i16 (...) @random()
%54 = load i16*, i16** %8
%55 = load i64, i64* %10
%56 = getelementptr inbounds i16, i16* %54, i64 %55
store i16 %53, i16* %56
br label %57
57: ; preds = %52
%58 = load i64, i64* %10
%59 = add i64 %58, 1
store i64 %59, i64* %10
br label %48
60: ; preds = %48
%61 = load i16*, i16** %8
%62 = load i16*, i16** %9
%63 = load i64, i64* %7
%64 = load i64, i64* %7
%65 = call i32 @basic_test(i16* %61, i16* %62, i64 %63, i64 %64)
%66 = load i16*, i16** %8
%67 = load i16*, i16** %9
%68 = load i64, i64* %7
%69 = load i64, i64* %7
%70 = call i32 @prefetch_test(i16* %66, i16* %67, i64 %68, i64 %69, i32 2)
%71 = load i16*, i16** %8
%72 = load i16*, i16** %9
%73 = load i64, i64* %7
%74 = load i64, i64* %7
%75 = call i32 @prefetch_test(i16* %71, i16* %72, i64 %73, i64 %74, i32 3)
%76 = load i16*, i16** %8
%77 = load i16*, i16** %9
%78 = load i64, i64* %7
%79 = load i64, i64* %7
%80 = call i32 @prefetch_test(i16* %76, i16* %77, i64 %78, i64 %79, i32 4)
%81 = load i16*, i16** %8
%82 = load i16*, i16** %9
%83 = load i64, i64* %7
%84 = load i64, i64* %7
%85 = call i32 @prefetch_test(i16* %81, i16* %82, i64 %83, i64 %84, i32 6)
%86 = load i16*, i16** %8
%87 = load i16*, i16** %9
%88 = load i64, i64* %7
%89 = load i64, i64* %7
%90 = call i32 @prefetch_test(i16* %86, i16* %87, i64 %88, i64 %89, i32 8)
%91 = load i16*, i16** %8
%92 = load i16*, i16** %9
%93 = load i64, i64* %7
%94 = load i64, i64* %7
%95 = call i32 @prefetch_test(i16* %91, i16* %92, i64 %93, i64 %94, i32 12)
%96 = load i16*, i16** %8
%97 = load i16*, i16** %9
%98 = load i64, i64* %7
%99 = load i64, i64* %7
%100 = call i32 @prefetch_test(i16* %96, i16* %97, i64 %98, i64 %99, i32 16)
%101 = load i16*, i16** %8
%102 = call i32 @free(i16* %101)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [21 x i8] c"buffer size: %d MiB\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @assert(i32 1)
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = shl nsw i32 %8, 20
%10 = sext i32 %9 to i64
%11 = icmp ne i32 %8, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
br label %14
14: ; preds = %5, %2
%15 = phi i64 [ %10, %5 ], [ 67108864, %2 ]
%16 = tail call i64 @llvm.ctpop.i64(i64 %15)
%17 = icmp ult i64 %16, 2
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = load i32, i32* @stdout
%21 = lshr i64 %15, 20
%22 = trunc i64 %21 to i32
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = lshr i64 %15, 1
%25 = tail call i16* @malloc(i64 %15)
%26 = getelementptr inbounds i16, i16* %25, i64 %24
%27 = tail call i32 @srandom(i32 1234)
%28 = icmp ult i64 %15, 2
br i1 %28, label %37, label %29
29: ; preds = %14
%30 = call i64 @llvm.umax.i64(i64 %24, i64 1)
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ %35, %31 ], [ 0, %29 ]
%33 = tail call zeroext i16 (...) @random()
%34 = getelementptr inbounds i16, i16* %25, i64 %32
store i16 %33, i16* %34
%35 = add nuw nsw i64 %32, 1
%36 = icmp eq i64 %35, %30
br i1 %36, label %37, label %31
37: ; preds = %31, %14
%38 = tail call i32 @basic_test(i16* %25, i16* %26, i64 %24, i64 %24)
%39 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 2)
%40 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 3)
%41 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 4)
%42 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 6)
%43 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 8)
%44 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 12)
%45 = tail call i32 @prefetch_test(i16* %25, i16* %26, i64 %24, i64 %24, i32 16)
%46 = tail call i32 @free(i16* %25)
ret i32 0
}