#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 atoi(char *);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
long long wall_clock_time();
int main(int argc, char *argv[]) {
  int arraysize, arraylength, lengthmod, i, linesize;
  long long start, end;
  int *array;
  if (argc > 1) {
    linesize = atoi(argv[1]);
  } else {
    printf("Please input the linesize in bytes.\n");
    return 1;
  }
  for (arraysize = 2 * 1024; arraysize < 1024 * 1024; arraysize *= 2) {
    array = malloc(arraysize);
    arraylength = arraysize / sizeof(int);
    lengthmod = arraylength - 1;
    srand(time(NULL));
    for (i = 0; i < arraylength; i++) {
      array[i] = rand() % 5;
    }
    start = wall_clock_time();
    for (i = 0; i < 2 * 1024 * 1024; i++) {
      array[(i * linesize / sizeof(int)) & lengthmod]++;
    }
    end = wall_clock_time();
    printf("test cache capacity (KB): %3d time:%lf\n", arraysize / 1024,
           ((float)(end - start)) / 1000000000);
    free(array);
  }
  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)
cmpl   $0x1,-0x8(%rbp)
jle    11b5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    11cf <main+0x4f>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x17e>
movl   $0x800,-0x14(%rbp)
cmpl   $0x100000,-0x14(%rbp)
jge    12f7 <main+0x177>
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
movslq -0x14(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    124e <main+0xce>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    -0x40(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    121a <main+0x9a>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x200000,-0x20(%rbp)
jge    129d <main+0x11d>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x24(%rbp),%ecx
movslq %ecx,%rcx
shr    $0x2,%rcx
movslq -0x1c(%rbp),%rdx
and    %rdx,%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1260 <main+0xe0>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2ss %rax,%xmm0
movss  0xd36(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd50(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d6 <main+0x56>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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
cmp    $0x2,%edi
jl     12b5 <main+0x125>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
movslq %eax,%r15
lea    (%r15,%r15,1),%r14
mov    $0x800,%r13d
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %r13d,%r12d
shr    $0x2,%r12d
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r12
jne    11f0 <main+0x60>
add    $0xffffffff,%r12d
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
mov    %rax,%rbp
movslq %r12d,%rax
mov    $0x200000,%ecx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
mov    %rdx,%rsi
shr    $0x2,%rsi
and    %rax,%rsi
addl   $0x1,(%rbx,%rsi,4)
lea    (%r15,%rdx,1),%rsi
shr    $0x2,%rsi
and    %rax,%rsi
addl   $0x1,(%rbx,%rsi,4)
add    %r14,%rdx
add    $0xfffffffffffffffe,%rcx
jne    1240 <main+0xb0>
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
sub    %rbp,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
mov    %r13d,%esi
shr    $0xa,%esi
divss  0xd77(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd70(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    %r13d,%r13d
cmp    $0x100000,%r13d
jb     11c0 <main+0x30>
jmp    12c7 <main+0x137>
lea    0xd70(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Please input the linesize in bytes.\0A\00"
@.str.1 = [40 x i8] c"test cache capacity (KB): %3d time:%lf\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %10
br label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %90
23: ; preds = %16
store i32 2048, i32* %6
br label %24
24: ; preds = %86, %23
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 1048576
br i1 %26, label %27, label %89
27: ; preds = %24
%28 = load i32, i32* %6
%29 = call i32* @malloc(i32 %28)
store i32* %29, i32** %13
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = udiv i64 %31, 4
%33 = trunc i64 %32 to i32
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
store i32 %35, i32* %8
%36 = call i32 @time(i32* null)
%37 = call i32 @srand(i32 %36)
store i32 0, i32* %9
br label %38
38: ; preds = %49, %27
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %52
42: ; preds = %38
%43 = call i32 (...) @rand()
%44 = srem i32 %43, 5
%45 = load i32*, i32** %13
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
br label %49
49: ; preds = %42
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %38
52: ; preds = %38
%53 = call i64 (...) @wall_clock_time()
store i64 %53, i64* %11
store i32 0, i32* %9
br label %54
54: ; preds = %70, %52
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 2097152
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i32*, i32** %13
%59 = load i32, i32* %9
%60 = load i32, i32* %10
%61 = mul nsw i32 %59, %60
%62 = sext i32 %61 to i64
%63 = udiv i64 %62, 4
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = and i64 %63, %65
%67 = getelementptr inbounds i32, i32* %58, i64 %66
%68 = load i32, i32* %67
%69 = add nsw i32 %68, 1
store i32 %69, i32* %67
br label %70
70: ; preds = %57
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %54
73: ; preds = %54
%74 = call i64 (...) @wall_clock_time()
store i64 %74, i64* %12
%75 = load i32, i32* %6
%76 = sdiv i32 %75, 1024
%77 = load i64, i64* %12
%78 = load i64, i64* %11
%79 = sub nsw i64 %77, %78
%80 = sitofp i64 %79 to float
%81 = fdiv float %80, 1.000000e+09
%82 = fpext float %81 to double
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %76, double %82)
%84 = load i32*, i32** %13
%85 = call i32 @free(i32* %84)
br label %86
86: ; preds = %73
%87 = load i32, i32* %6
%88 = mul nsw i32 %87, 2
store i32 %88, i32* %6
br label %24
89: ; preds = %24
store i32 0, i32* %3
br label %90
90: ; preds = %89, %21
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str.1 = [40 x i8] c"test cache capacity (KB): %3d time:%lf\0A\00"
@str = [36 x i8] c"Please input the linesize in bytes.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = sext i32 %7 to i64
br label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %57
11: ; preds = %4, %46
%12 = phi i32 [ 2048, %4 ], [ %55, %46 ]
%13 = tail call i32* @malloc(i32 %12)
%14 = lshr exact i32 %12, 2
%15 = tail call i32 @time(i32* null)
%16 = tail call i32 @srand(i32 %15)
%17 = zext i32 %14 to i64
br label %18
18: ; preds = %11, %18
%19 = phi i64 [ 0, %11 ], [ %23, %18 ]
%20 = tail call i32 (...) @rand()
%21 = srem i32 %20, 5
%22 = getelementptr inbounds i32, i32* %13, i64 %19
store i32 %21, i32* %22
%23 = add nuw nsw i64 %19, 1
%24 = icmp eq i64 %23, %17
br i1 %24, label %25, label %18
25: ; preds = %18
%26 = add nsw i32 %14, -1
%27 = tail call i64 (...) @wall_clock_time()
%28 = sext i32 %26 to i64
br label %29
29: ; preds = %29, %25
%30 = phi i64 [ 0, %25 ], [ %44, %29 ]
%31 = mul nsw i64 %30, %8
%32 = lshr i64 %31, 2
%33 = and i64 %32, %28
%34 = getelementptr inbounds i32, i32* %13, i64 %33
%35 = load i32, i32* %34
%36 = add nsw i32 %35, 1
store i32 %36, i32* %34
%37 = or i64 %30, 1
%38 = mul nsw i64 %37, %8
%39 = lshr i64 %38, 2
%40 = and i64 %39, %28
%41 = getelementptr inbounds i32, i32* %13, i64 %40
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
store i32 %43, i32* %41
%44 = add nuw nsw i64 %30, 2
%45 = icmp eq i64 %44, 2097152
br i1 %45, label %46, label %29
46: ; preds = %29
%47 = tail call i64 (...) @wall_clock_time()
%48 = lshr exact i32 %12, 10
%49 = sub nsw i64 %47, %27
%50 = sitofp i64 %49 to float
%51 = fdiv float %50, 1.000000e+09
%52 = fpext float %51 to double
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %48, double %52)
%54 = tail call i32 @free(i32* %13)
%55 = shl nsw i32 %12, 1
%56 = icmp ult i32 %55, 1048576
br i1 %56, label %11, label %57
57: ; preds = %46, %9
%58 = phi i32 [ 1, %9 ], [ 0, %46 ]
ret i32 %58
}