#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 *);
scalar_t__ floor(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sqrtf(int);
int /*<<< orphan*/ startperf(char *);
int /*<<< orphan*/ stopperf();
int main() {
  int *data = (int *)malloc((1024 * 1024) * sizeof(int));
  assert(data);
  for (int i = 0; i < (1024 * 1024); i++) {
    data[i] = (int)floor(sqrtf(i));
  }
  startperf("perf stat -e cache-misses -p %d");
  sleep(1);
  unsigned total = 0;
  for (int iter = 0; iter < 500; iter++) {
    int result = 0;
    for (unsigned i = 0; i < (1024 * 1024); i++) {
      result += data[i];
    }
    total += (unsigned)result;
  }
  stopperf();
  printf("Result: %d\n", total);
  free(data);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x400000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x100000,-0x14(%rbp)
jge    11f4 <main+0x64>
mov    -0x14(%rbp),%edi
call   1090 <sqrtf@plt>
mov    %eax,%edi
call   10b0 <floor@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bd <main+0x2d>
lea    0xe05(%rip),%rdi
call   1060 <startperf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1f4,-0x1c(%rbp)
jge    1275 <main+0xe5>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x100000,-0x24(%rbp)
jae    125e <main+0xce>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1233 <main+0xa3>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1218 <main+0x88>
mov    $0x0,%al
call   1050 <stopperf@plt>
mov    -0x18(%rbp),%esi
lea    0xd9a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x400000,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <assert@plt>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1090 <sqrtf@plt>
mov    %eax,%edi
call   10b0 <floor@plt>
mov    %eax,(%rbx,%rbp,4)
lea    0x1(%rbp),%edi
call   1090 <sqrtf@plt>
mov    %eax,%edi
call   10b0 <floor@plt>
mov    %eax,0x4(%rbx,%rbp,4)
lea    0x2(%rbp),%edi
call   1090 <sqrtf@plt>
mov    %eax,%edi
call   10b0 <floor@plt>
mov    %eax,0x8(%rbx,%rbp,4)
lea    0x3(%rbp),%edi
call   1090 <sqrtf@plt>
mov    %eax,%edi
call   10b0 <floor@plt>
mov    %eax,0xc(%rbx,%rbp,4)
add    $0x4,%rbp
cmp    $0x100000,%rbp
jne    11b0 <main+0x20>
lea    0xdf2(%rip),%rdi
call   1060 <startperf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
pxor   %xmm0,%xmm0
mov    $0x1c,%ecx
pxor   %xmm1,%xmm1
nopl   (%rax)
movdqu -0x70(%rbx,%rcx,4),%xmm2
paddd  %xmm0,%xmm2
movdqu -0x60(%rbx,%rcx,4),%xmm0
paddd  %xmm1,%xmm0
movdqu -0x50(%rbx,%rcx,4),%xmm1
movdqu -0x40(%rbx,%rcx,4),%xmm3
movdqu -0x30(%rbx,%rcx,4),%xmm4
paddd  %xmm1,%xmm4
paddd  %xmm2,%xmm4
movdqu -0x20(%rbx,%rcx,4),%xmm2
paddd  %xmm3,%xmm2
paddd  %xmm0,%xmm2
movdqu -0x10(%rbx,%rcx,4),%xmm0
paddd  %xmm4,%xmm0
movdqu (%rbx,%rcx,4),%xmm1
paddd  %xmm2,%xmm1
add    $0x20,%rcx
cmp    $0x10001c,%rcx
jne    1240 <main+0xb0>
paddd  %xmm0,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%ecx
add    %ecx,%ebp
add    $0x1,%eax
cmp    $0x1f4,%eax
jne    1230 <main+0xa0>
xor    %eax,%eax
call   1050 <stopperf@plt>
lea    0xd4c(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"perf stat -e cache-misses -p %d\00"
@.str.1 = [12 x i8] c"Result: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i64 @malloc(i32 4194304)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = call i32 @assert(i32* %10)
store i32 0, i32* %3
br label %12
12: ; preds = %24, %0
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 1048576
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = load i32, i32* %3
%17 = call i32 @sqrtf(i32 %16)
%18 = call i64 @floor(i32 %17)
%19 = trunc i64 %18 to i32
%20 = load i32*, i32** %2
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 %19, i32* %23
br label %24
24: ; preds = %15
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %12
27: ; preds = %12
%28 = call i32 @startperf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @sleep(i32 1)
store i32 0, i32* %4
store i32 0, i32* %5
br label %30
30: ; preds = %52, %27
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 500
br i1 %32, label %33, label %55
33: ; preds = %30
store i32 0, i32* %6
store i32 0, i32* %7
br label %34
34: ; preds = %45, %33
%35 = load i32, i32* %7
%36 = icmp ult i32 %35, 1048576
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = load i32*, i32** %2
%39 = load i32, i32* %7
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, %42
store i32 %44, i32* %6
br label %45
45: ; preds = %37
%46 = load i32, i32* %7
%47 = add i32 %46, 1
store i32 %47, i32* %7
br label %34
48: ; preds = %34
%49 = load i32, i32* %6
%50 = load i32, i32* %4
%51 = add i32 %50, %49
store i32 %51, i32* %4
br label %52
52: ; preds = %48
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %30
55: ; preds = %30
%56 = call i32 (...) @stopperf()
%57 = load i32, i32* %4
%58 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %57)
%59 = load i32*, i32** %2
%60 = call i32 @free(i32* %59)
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"perf stat -e cache-misses -p %d\00"
@.str.1 = [12 x i8] c"Result: %d\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 4194304)
%2 = inttoptr i64 %1 to i32*
%3 = tail call i32 @assert(i32* %2)
br label %7
4: ; preds = %7
%5 = tail call i32 @startperf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @sleep(i32 1)
br label %34
7: ; preds = %7, %0
%8 = phi i64 [ 0, %0 ], [ %32, %7 ]
%9 = trunc i64 %8 to i32
%10 = tail call i32 @sqrtf(i32 %9)
%11 = tail call i64 @floor(i32 %10)
%12 = trunc i64 %11 to i32
%13 = getelementptr inbounds i32, i32* %2, i64 %8
store i32 %12, i32* %13
%14 = or i64 %8, 1
%15 = trunc i64 %14 to i32
%16 = tail call i32 @sqrtf(i32 %15)
%17 = tail call i64 @floor(i32 %16)
%18 = trunc i64 %17 to i32
%19 = getelementptr inbounds i32, i32* %2, i64 %14
store i32 %18, i32* %19
%20 = or i64 %8, 2
%21 = trunc i64 %20 to i32
%22 = tail call i32 @sqrtf(i32 %21)
%23 = tail call i64 @floor(i32 %22)
%24 = trunc i64 %23 to i32
%25 = getelementptr inbounds i32, i32* %2, i64 %20
store i32 %24, i32* %25
%26 = or i64 %8, 3
%27 = trunc i64 %26 to i32
%28 = tail call i32 @sqrtf(i32 %27)
%29 = tail call i64 @floor(i32 %28)
%30 = trunc i64 %29 to i32
%31 = getelementptr inbounds i32, i32* %2, i64 %26
store i32 %30, i32* %31
%32 = add nuw nsw i64 %8, 4
%33 = icmp eq i64 %32, 1048576
br i1 %33, label %4, label %7
34: ; preds = %78, %4
%35 = phi i32 [ 0, %4 ], [ %82, %78 ]
%36 = phi i32 [ 0, %4 ], [ %81, %78 ]
br label %37
37: ; preds = %37, %34
%38 = phi i64 [ 0, %34 ], [ %76, %37 ]
%39 = phi <4 x i32> [ zeroinitializer, %34 ], [ %74, %37 ]
%40 = phi <4 x i32> [ zeroinitializer, %34 ], [ %75, %37 ]
%41 = getelementptr inbounds i32, i32* %2, i64 %38
%42 = bitcast i32* %41 to <4 x i32>*
%43 = load <4 x i32>, <4 x i32>* %42
%44 = getelementptr inbounds i32, i32* %41, i64 4
%45 = bitcast i32* %44 to <4 x i32>*
%46 = load <4 x i32>, <4 x i32>* %45
%47 = add <4 x i32> %43, %39
%48 = add <4 x i32> %46, %40
%49 = or i64 %38, 8
%50 = getelementptr inbounds i32, i32* %2, i64 %49
%51 = bitcast i32* %50 to <4 x i32>*
%52 = load <4 x i32>, <4 x i32>* %51
%53 = getelementptr inbounds i32, i32* %50, i64 4
%54 = bitcast i32* %53 to <4 x i32>*
%55 = load <4 x i32>, <4 x i32>* %54
%56 = add <4 x i32> %52, %47
%57 = add <4 x i32> %55, %48
%58 = or i64 %38, 16
%59 = getelementptr inbounds i32, i32* %2, i64 %58
%60 = bitcast i32* %59 to <4 x i32>*
%61 = load <4 x i32>, <4 x i32>* %60
%62 = getelementptr inbounds i32, i32* %59, i64 4
%63 = bitcast i32* %62 to <4 x i32>*
%64 = load <4 x i32>, <4 x i32>* %63
%65 = add <4 x i32> %61, %56
%66 = add <4 x i32> %64, %57
%67 = or i64 %38, 24
%68 = getelementptr inbounds i32, i32* %2, i64 %67
%69 = bitcast i32* %68 to <4 x i32>*
%70 = load <4 x i32>, <4 x i32>* %69
%71 = getelementptr inbounds i32, i32* %68, i64 4
%72 = bitcast i32* %71 to <4 x i32>*
%73 = load <4 x i32>, <4 x i32>* %72
%74 = add <4 x i32> %70, %65
%75 = add <4 x i32> %73, %66
%76 = add nuw nsw i64 %38, 32
%77 = icmp eq i64 %76, 1048576
br i1 %77, label %78, label %37
78: ; preds = %37
%79 = add <4 x i32> %75, %74
%80 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %79)
%81 = add i32 %80, %36
%82 = add nuw nsw i32 %35, 1
%83 = icmp eq i32 %82, 500
br i1 %83, label %84, label %34
84: ; preds = %78
%85 = tail call i32 (...) @stopperf()
%86 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %81)
%87 = tail call i32 @free(i32* %2)
ret i32 0
}