#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(void *);
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 = 5 * 1024 * 1024, arraylength = arraysize / sizeof(int),
      lengthmod = arraylength - 1, i, j, k, cap;
  long long start, end;
  volatile int *array = malloc(arraysize);
  if (argc > 1) {
    cap = atoi(argv[1]) * 1024;
    printf("Cache capacity: %d\n", cap);
  } else {
    printf("Please input cache capacity in KB.\n");
    return 1;
  }
  srand(time(NULL));
  for (i = 0; i < arraylength; i++)
    array[i] = rand() % 5;
  k = rand() % lengthmod;
  printf("testing starts!\n");
  for (i = 1; i < 64; i *= 2) {
    int inc = cap / i / sizeof(int);
    start = wall_clock_time();
    for (j = 0; j < 2 * 1024 * 1024; j++) {
      array[k & lengthmod]++;
      k += inc;
    }
    end = wall_clock_time();
    float total = end - start;
    start = wall_clock_time();
    for (j = 0; j < 2 * 1024 * 1024; j++) {
      k += inc;
    }
    end = wall_clock_time();
    float overhead = end - start;
    printf("test way: %2d time:%lf\n", i,
           ((float)(total - overhead)) / 1000000);
  }
  free((void *)array);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x500000,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11f0 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
shl    $0xa,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120a <main+0x8a>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x210>
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    1255 <main+0xd5>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    -0x48(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1221 <main+0xa1>
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x28(%rbp)
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x20(%rbp)
cmpl   $0x40,-0x20(%rbp)
jge    1387 <main+0x207>
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x20(%rbp)
cltq
shr    $0x2,%rax
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x200000,-0x24(%rbp)
jge    12de <main+0x15e>
mov    -0x48(%rbp),%rax
mov    -0x28(%rbp),%ecx
and    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x4c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12a4 <main+0x124>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x50(%rbp)
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x200000,-0x24(%rbp)
jge    1331 <main+0x1b1>
mov    -0x4c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    130d <main+0x18d>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x54(%rbp)
mov    -0x20(%rbp),%esi
movss  -0x50(%rbp),%xmm0
subss  -0x54(%rbp),%xmm0
movss  0xc9d(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xcdb(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1278 <main+0xf8>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%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,%r14
mov    %edi,%ebp
mov    $0x500000,%edi
call   1090 <malloc@plt>
cmp    $0x2,%ebp
jl     131f <main+0x18f>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
shl    $0xa,%r14d
lea    0xe37(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
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    $0x140000,%rbp
jne    11f0 <main+0x60>
xor    %eax,%eax
call   10b0 <rand@plt>
movslq %eax,%r15
imul   $0xffffffffccccd70b,%r15,%rax
shr    $0x20,%rax
add    %r15d,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x14,%eax
add    %ecx,%eax
imul   $0x13ffff,%eax,%eax
sub    %eax,%r15d
lea    0xdff(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r12d
nop
mov    %r14d,%eax
cltd
idiv   %r12d
mov    %eax,%ebp
sar    $0x2,%ebp
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
mov    %rax,%r13
mov    $0x200000,%eax
nopl   0x0(%rax,%rax,1)
mov    %r15d,%ecx
and    $0x13ffff,%ecx
addl   $0x1,(%rbx,%rcx,4)
add    %ebp,%r15d
mov    %r15d,%ecx
and    $0x13ffff,%ecx
addl   $0x1,(%rbx,%rcx,4)
add    %ebp,%r15d
add    $0xfffffffe,%eax
jne    1280 <main+0xf0>
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
mov    %rax,%rbp
sub    %r13,%rbp
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
mov    %rax,%r13
xorps  %xmm0,%xmm0
cvtsi2ss %rbp,%xmm0
movss  %xmm0,0x4(%rsp)
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
sub    %r13,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  0x4(%rsp),%xmm1
subss  %xmm0,%xmm1
divss  0xd12(%rip),%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
lea    0xd1c(%rip),%rdi
mov    %r12d,%esi
mov    $0x1,%al
call   1050 <printf@plt>
add    %r12d,%r12d
cmp    $0x40,%r12d
jb     1260 <main+0xd0>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1330 <main+0x1a0>
lea    0xd0a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Cache capacity: %d\0A\00"
@.str.1 = [36 x i8] c"Please input cache capacity in KB.\0A\00"
@.str.2 = [17 x i8] c"testing starts!\0A\00"
@.str.3 = [24 x i8] c"test way: %2d 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 i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32*
%16 = alloca i32
%17 = alloca float
%18 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5242880, i32* %6
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = udiv i64 %20, 4
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %8
%25 = load i32, i32* %6
%26 = call i32* @malloc(i32 %25)
store i32* %26, i32** %15
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %37
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = mul nsw i32 %33, 1024
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %39
37: ; preds = %2
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %127
39: ; preds = %29
%40 = call i32 @time(i32* null)
%41 = call i32 @srand(i32 %40)
store i32 0, i32* %9
br label %42
42: ; preds = %53, %39
%43 = load i32, i32* %9
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %56
46: ; preds = %42
%47 = call i32 (...) @rand()
%48 = srem i32 %47, 5
%49 = load i32*, i32** %15
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store volatile i32 %48, i32* %52
br label %53
53: ; preds = %46
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %42
56: ; preds = %42
%57 = call i32 (...) @rand()
%58 = load i32, i32* %8
%59 = srem i32 %57, %58
store i32 %59, i32* %11
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %9
br label %61
61: ; preds = %120, %56
%62 = load i32, i32* %9
%63 = icmp slt i32 %62, 64
br i1 %63, label %64, label %123
64: ; preds = %61
%65 = load i32, i32* %12
%66 = load i32, i32* %9
%67 = sdiv i32 %65, %66
%68 = sext i32 %67 to i64
%69 = udiv i64 %68, 4
%70 = trunc i64 %69 to i32
store i32 %70, i32* %16
%71 = call i64 (...) @wall_clock_time()
store i64 %71, i64* %13
store i32 0, i32* %10
br label %72
72: ; preds = %87, %64
%73 = load i32, i32* %10
%74 = icmp slt i32 %73, 2097152
br i1 %74, label %75, label %90
75: ; preds = %72
%76 = load i32*, i32** %15
%77 = load i32, i32* %11
%78 = load i32, i32* %8
%79 = and i32 %77, %78
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %76, i64 %80
%82 = load volatile i32, i32* %81
%83 = add nsw i32 %82, 1
store volatile i32 %83, i32* %81
%84 = load i32, i32* %16
%85 = load i32, i32* %11
%86 = add nsw i32 %85, %84
store i32 %86, i32* %11
br label %87
87: ; preds = %75
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 1
store i32 %89, i32* %10
br label %72
90: ; preds = %72
%91 = call i64 (...) @wall_clock_time()
store i64 %91, i64* %14
%92 = load i64, i64* %14
%93 = load i64, i64* %13
%94 = sub nsw i64 %92, %93
%95 = sitofp i64 %94 to float
store float %95, float* %17
%96 = call i64 (...) @wall_clock_time()
store i64 %96, i64* %13
store i32 0, i32* %10
br label %97
97: ; preds = %104, %90
%98 = load i32, i32* %10
%99 = icmp slt i32 %98, 2097152
br i1 %99, label %100, label %107
100: ; preds = %97
%101 = load i32, i32* %16
%102 = load i32, i32* %11
%103 = add nsw i32 %102, %101
store i32 %103, i32* %11
br label %104
104: ; preds = %100
%105 = load i32, i32* %10
%106 = add nsw i32 %105, 1
store i32 %106, i32* %10
br label %97
107: ; preds = %97
%108 = call i64 (...) @wall_clock_time()
store i64 %108, i64* %14
%109 = load i64, i64* %14
%110 = load i64, i64* %13
%111 = sub nsw i64 %109, %110
%112 = sitofp i64 %111 to float
store float %112, float* %18
%113 = load i32, i32* %9
%114 = load float, float* %17
%115 = load float, float* %18
%116 = fsub float %114, %115
%117 = fdiv float %116, 1.000000e+06
%118 = fpext float %117 to double
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %113, double %118)
br label %120
120: ; preds = %107
%121 = load i32, i32* %9
%122 = mul nsw i32 %121, 2
store i32 %122, i32* %9
br label %61
123: ; preds = %61
%124 = load i32*, i32** %15
%125 = bitcast i32* %124 to i8*
%126 = call i32 @free(i8* %125)
br label %127
127: ; preds = %123, %37
%128 = load i32, i32* %3
ret i32 %128
}

LLVM IR (-O2):
@.str = [20 x i8] c"Cache capacity: %d\0A\00"
@.str.3 = [24 x i8] c"test way: %2d time:%lf\0A\00"
@str = [35 x i8] c"Please input cache capacity in KB.\00"
@str.4 = [16 x i8] c"testing starts!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32* @malloc(i32 5242880)
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %13
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, 10
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @time(i32* null)
%12 = tail call i32 @srand(i32 %11)
br label %15
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %66
15: ; preds = %5, %15
%16 = phi i64 [ 0, %5 ], [ %20, %15 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 5
%19 = getelementptr inbounds i32, i32* %3, i64 %16
store volatile i32 %18, i32* %19
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, 1310720
br i1 %21, label %22, label %15
22: ; preds = %15
%23 = tail call i32 (...) @rand()
%24 = srem i32 %23, 1310719
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
br label %26
26: ; preds = %22, %49
%27 = phi i32 [ 1, %22 ], [ %61, %49 ]
%28 = phi i32 [ %24, %22 ], [ %46, %49 ]
%29 = sdiv i32 %9, %27
%30 = ashr i32 %29, 2
%31 = tail call i64 (...) @wall_clock_time()
br label %32
32: ; preds = %32, %26
%33 = phi i32 [ 0, %26 ], [ %47, %32 ]
%34 = phi i32 [ %28, %26 ], [ %46, %32 ]
%35 = and i32 %34, 1310719
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %3, i64 %36
%38 = load volatile i32, i32* %37
%39 = add nsw i32 %38, 1
store volatile i32 %39, i32* %37
%40 = add i32 %34, %30
%41 = and i32 %40, 1310719
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %3, i64 %42
%44 = load volatile i32, i32* %43
%45 = add nsw i32 %44, 1
store volatile i32 %45, i32* %43
%46 = add i32 %40, %30
%47 = add nuw nsw i32 %33, 2
%48 = icmp eq i32 %47, 2097152
br i1 %48, label %49, label %32
49: ; preds = %32
%50 = tail call i64 (...) @wall_clock_time()
%51 = sub nsw i64 %50, %31
%52 = tail call i64 (...) @wall_clock_time()
%53 = sitofp i64 %51 to float
%54 = tail call i64 (...) @wall_clock_time()
%55 = sub nsw i64 %54, %52
%56 = sitofp i64 %55 to float
%57 = fsub float %53, %56
%58 = fdiv float %57, 1.000000e+06
%59 = fpext float %58 to double
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %27, double %59)
%61 = shl nsw i32 %27, 1
%62 = icmp ult i32 %61, 64
br i1 %62, label %26, label %63
63: ; preds = %49
%64 = bitcast i32* %3 to i8*
%65 = tail call i32 @free(i8* %64)
br label %66
66: ; preds = %63, %13
%67 = phi i32 [ 0, %63 ], [ 1, %13 ]
ret i32 %67
}