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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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)
cmpl   $0x1,-0x8(%rbp)
jle    11d0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
jmp    11ea <main+0x6a>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1312 <main+0x192>
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x48(%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    1241 <main+0xc1>
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    120d <main+0x8d>
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x40,-0x20(%rbp)
jge    1302 <main+0x182>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x24(%rbp)
cmpl   $0x200000,-0x24(%rbp)
jge    12bb <main+0x13b>
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    -0x2c(%rbp),%eax
cltd
idivl  -0x20(%rbp)
movslq %eax,%rcx
shr    $0x2,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1272 <main+0xf2>
mov    $0x0,%al
call   1060 <wall_clock_time@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
cvtsi2ss %rax,%xmm0
movss  0xd22(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd3f(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1256 <main+0xd6>
mov    -0x48(%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     12c4 <main+0x134>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    $0x500000,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %ebp,%ebp
nopl   (%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    $0x140000,%rbp
jne    11d0 <main+0x40>
xor    %eax,%eax
call   10b0 <rand@plt>
movslq %eax,%rbp
imul   $0xffffffffccccd70b,%rbp,%rax
shr    $0x20,%rax
add    %ebp,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x14,%eax
add    %ecx,%eax
imul   $0x13ffff,%eax,%eax
sub    %eax,%ebp
mov    $0x1,%r13d
lea    0xdcc(%rip),%r15
jmp    1274 <main+0xe4>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
sub    %r12,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
divss  0xda6(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %r15,%rdi
mov    %r13d,%esi
mov    $0x1,%al
call   1050 <printf@plt>
add    %r13d,%r13d
cmp    $0x40,%r13d
jae    12d7 <main+0x147>
xor    %eax,%eax
call   1070 <wall_clock_time@plt>
mov    %rax,%r12
mov    %r14d,%eax
cltd
idiv   %r13d
sar    $0x2,%eax
lea    (%rax,%rax,1),%ecx
mov    $0x1ffffe,%edx
mov    %ebp,%esi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %esi,%edi
and    $0x13ffff,%edi
addl   $0x1,(%rbx,%rdi,4)
lea    (%rax,%rsi,1),%ebp
test   %edx,%edx
je     1240 <main+0xb0>
and    $0x13ffff,%ebp
addl   $0x1,(%rbx,%rbp,4)
add    $0xfffffffe,%edx
add    %ecx,%esi
jmp    12a0 <main+0x110>
lea    0xd51(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12e1 <main+0x151>
mov    %rbx,%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):
@.str = [40 x i8] c"Please input the cache capacity in KB.\0A\00"
@.str.1 = [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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5242880, i32* %6
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = udiv i64 %17, 4
%19 = trunc i64 %18 to i32
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %8
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %12
br label %31
29: ; preds = %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %99
31: ; preds = %24
%32 = load i32, i32* %6
%33 = call i32* @malloc(i32 %32)
store i32* %33, i32** %15
%34 = call i32 @time(i32* null)
%35 = call i32 @srand(i32 %34)
store i32 0, i32* %9
br label %36
36: ; preds = %47, %31
%37 = load i32, i32* %9
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %50
40: ; preds = %36
%41 = call i32 (...) @rand()
%42 = srem i32 %41, 5
%43 = load i32*, i32** %15
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
store i32 %42, i32* %46
br label %47
47: ; preds = %40
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %36
50: ; preds = %36
%51 = call i32 (...) @rand()
%52 = load i32, i32* %8
%53 = srem i32 %51, %52
store i32 %53, i32* %11
store i32 1, i32* %9
br label %54
54: ; preds = %93, %50
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 64
br i1 %56, label %57, label %96
57: ; preds = %54
%58 = call i64 (...) @wall_clock_time()
store i64 %58, i64* %13
store i32 1, i32* %10
br label %59
59: ; preds = %80, %57
%60 = load i32, i32* %10
%61 = icmp slt i32 %60, 2097152
br i1 %61, label %62, label %83
62: ; preds = %59
%63 = load i32*, i32** %15
%64 = load i32, i32* %11
%65 = load i32, i32* %8
%66 = and i32 %64, %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %63, i64 %67
%69 = load i32, i32* %68
%70 = add nsw i32 %69, 1
store i32 %70, i32* %68
%71 = load i32, i32* %12
%72 = load i32, i32* %9
%73 = sdiv i32 %71, %72
%74 = sext i32 %73 to i64
%75 = udiv i64 %74, 4
%76 = load i32, i32* %11
%77 = sext i32 %76 to i64
%78 = add i64 %77, %75
%79 = trunc i64 %78 to i32
store i32 %79, i32* %11
br label %80
80: ; preds = %62
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %59
83: ; preds = %59
%84 = call i64 (...) @wall_clock_time()
store i64 %84, i64* %14
%85 = load i32, i32* %9
%86 = load i64, i64* %14
%87 = load i64, i64* %13
%88 = sub nsw i64 %86, %87
%89 = sitofp i64 %88 to float
%90 = fdiv float %89, 1.000000e+09
%91 = fpext float %90 to double
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %85, double %91)
br label %93
93: ; preds = %83
%94 = load i32, i32* %9
%95 = mul nsw i32 %94, 2
store i32 %95, i32* %9
br label %54
96: ; preds = %54
%97 = load i32*, i32** %15
%98 = call i32 @free(i32* %97)
store i32 0, i32* %3
br label %99
99: ; preds = %96, %29
%100 = load i32, i32* %3
ret i32 %100
}

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