#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ generate_randindices(int *);
scalar_t__ malloc(int);
int main(int argc, char *argv[]) {
  int i = 0, num_misses_per_iter = 32;
  int cache_size = ((32) << 20);
  int num_cache_lines = cache_size / 128;
  int max_index = cache_size / sizeof(long);
  unsigned long j = 0;
  int *indexarr = (int *)malloc(sizeof(int) * 128);
  int *randindices = (int *)malloc(sizeof(int) * 128);
  FILE *defines, *flush;
  defines = fopen("defines.h", "w");
  flush = fopen("flush.h", "w");
  if (argc > 1)
    num_misses_per_iter = atoi(argv[1]);
  if (num_misses_per_iter == 0)
    return 0;
  int period = 128 / num_misses_per_iter;
  int offset = 2 * 128 / sizeof(long);
  int max_offset = 128 * 2 * 128 / sizeof(long);
  int ind = 0;
  for (j = 0; j < 128; j++) {
    indexarr[j] = ind;
    i++;
    if (i >= period) {
      i = 0;
      ind += offset;
      if (ind >= max_offset)
        ind -= offset;
    }
  }
  generate_randindices(randindices);
  for (i = 0; i < 128; i++) {
    fprintf(defines, "read_barrier();\n");
  }
  fclose(defines);
  fclose(flush);
  return 0;
}


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   $0x0,-0x14(%rbp)
movl   $0x20,-0x18(%rbp)
movl   $0x2000000,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x80,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
lea    0xe31(%rip),%rdi
lea    0xe34(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x48(%rbp)
lea    0xe26(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1210 <main+0xb0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1226 <main+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    1316 <main+0x1b6>
mov    $0x80,%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x54(%rbp)
movl   $0x20,-0x58(%rbp)
movl   $0x1000,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x80,-0x30(%rbp)
jae    12c2 <main+0x162>
mov    -0x60(%rbp),%edx
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x54(%rbp),%eax
jl     12ac <main+0x14c>
movl   $0x0,-0x14(%rbp)
mov    -0x58(%rbp),%eax
add    -0x60(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jl     12a7 <main+0x147>
mov    -0x58(%rbp),%ecx
mov    -0x60(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x60(%rbp)
jmp    12ac <main+0x14c>
jmp    12b1 <main+0x151>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    124f <main+0xef>
mov    -0x40(%rbp),%rdi
call   1080 <generate_randindices@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jge    12fd <main+0x19d>
mov    -0x48(%rbp),%rdi
lea    0xd2a(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12d2 <main+0x172>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x50(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
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,%r13
mov    %edi,%r14d
mov    $0x200,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    $0x200,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
lea    0xe6e(%rip),%rdi
lea    0xe71(%rip),%rbp
mov    %rbp,%rsi
call   1050 <fopen@plt>
mov    %rax,%r15
lea    0xe61(%rip),%rdi
mov    %rbp,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
mov    $0x20,%ecx
cmp    $0x2,%r14d
jl     11d4 <main+0x74>
mov    0x8(%r13),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
test   %eax,%eax
je     127a <main+0x11a>
mov    %rbp,%r14
xor    %r8d,%r8d
mov    $0x80,%eax
xor    %edx,%edx
idiv   %ecx
mov    $0x1,%r9d
xor    %ebp,%ebp
xor    %edx,%edx
xor    %edi,%edi
nop
mov    %ebp,(%rbx,%rdi,4)
lea    0x1(%rdx),%esi
lea    0x20(%rbp),%ecx
cmp    $0xfe0,%ebp
cmovge %ebp,%ecx
add    $0x2,%edx
cmp    %eax,%esi
cmovl  %ebp,%ecx
cmovge %r9d,%edx
lea    0x20(%rcx),%ebp
cmp    $0xfe0,%ecx
cmovge %ecx,%ebp
mov    %ecx,0x4(%rbx,%rdi,4)
cmp    %eax,%edx
cmovge %r8d,%edx
cmovl  %ecx,%ebp
add    $0x2,%rdi
cmp    $0x80,%rdi
jne    11f0 <main+0x90>
mov    %r12,%rdi
call   1080 <generate_randindices@plt>
mov    $0x80,%ebp
lea    0xdcc(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rdi
mov    %r15,%rcx
call   1070 <fwrite@plt>
add    $0xffffffff,%ebp
jne    1250 <main+0xf0>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"defines.h\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"flush.h\00"
@.str.3 = [17 x i8] c"read_barrier();\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 i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 32, i32* %7
store i32 33554432, i32* %8
%20 = load i32, i32* %8
%21 = sdiv i32 %20, 128
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = udiv i64 %23, 8
%25 = trunc i64 %24 to i32
store i32 %25, i32* %10
store i64 0, i64* %11
%26 = call i64 @malloc(i32 512)
%27 = inttoptr i64 %26 to i32*
store i32* %27, i32** %12
%28 = call i64 @malloc(i32 512)
%29 = inttoptr i64 %28 to i32*
store i32* %29, i32** %13
%30 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %14
%31 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %15
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %2
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %34, %2
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 0, i32* %3
br label %92
43: ; preds = %39
%44 = load i32, i32* %7
%45 = sdiv i32 128, %44
store i32 %45, i32* %16
store i32 32, i32* %17
store i32 4096, i32* %18
store i32 0, i32* %19
store i64 0, i64* %11
br label %46
46: ; preds = %72, %43
%47 = load i64, i64* %11
%48 = icmp ult i64 %47, 128
br i1 %48, label %49, label %75
49: ; preds = %46
%50 = load i32, i32* %19
%51 = load i32*, i32** %12
%52 = load i64, i64* %11
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 %50, i32* %53
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = load i32, i32* %16
%58 = icmp sge i32 %56, %57
br i1 %58, label %59, label %71
59: ; preds = %49
store i32 0, i32* %6
%60 = load i32, i32* %17
%61 = load i32, i32* %19
%62 = add nsw i32 %61, %60
store i32 %62, i32* %19
%63 = load i32, i32* %19
%64 = load i32, i32* %18
%65 = icmp sge i32 %63, %64
br i1 %65, label %66, label %70
66: ; preds = %59
%67 = load i32, i32* %17
%68 = load i32, i32* %19
%69 = sub nsw i32 %68, %67
store i32 %69, i32* %19
br label %70
70: ; preds = %66, %59
br label %71
71: ; preds = %70, %49
br label %72
72: ; preds = %71
%73 = load i64, i64* %11
%74 = add i64 %73, 1
store i64 %74, i64* %11
br label %46
75: ; preds = %46
%76 = load i32*, i32** %13
%77 = call i32 @generate_randindices(i32* %76)
store i32 0, i32* %6
br label %78
78: ; preds = %84, %75
%79 = load i32, i32* %6
%80 = icmp slt i32 %79, 128
br i1 %80, label %81, label %87
81: ; preds = %78
%82 = load i32*, i32** %14
%83 = call i32 @fprintf(i32* %82, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %84
84: ; preds = %81
%85 = load i32, i32* %6
%86 = add nsw i32 %85, 1
store i32 %86, i32* %6
br label %78
87: ; preds = %78
%88 = load i32*, i32** %14
%89 = call i32 @fclose(i32* %88)
%90 = load i32*, i32** %15
%91 = call i32 @fclose(i32* %90)
store i32 0, i32* %3
br label %92
92: ; preds = %87, %42
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@.str = [10 x i8] c"defines.h\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"flush.h\00"
@.str.3 = [17 x i8] c"read_barrier();\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @malloc(i32 512)
%4 = inttoptr i64 %3 to i32*
%5 = tail call i64 @malloc(i32 512)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %51, label %15
15: ; preds = %2, %10
%16 = phi i32 [ %13, %10 ], [ 32, %2 ]
%17 = sdiv i32 128, %16
br label %18
18: ; preds = %18, %15
%19 = phi i32 [ 0, %15 ], [ %38, %18 ]
%20 = phi i32 [ 0, %15 ], [ %36, %18 ]
%21 = phi i64 [ 0, %15 ], [ %39, %18 ]
%22 = getelementptr inbounds i32, i32* %4, i64 %21
store i32 %19, i32* %22
%23 = add nsw i32 %20, 1
%24 = icmp slt i32 %23, %17
%25 = add nsw i32 %19, 32
%26 = icmp sgt i32 %19, 4063
%27 = select i1 %24, i1 true, i1 %26
%28 = select i1 %27, i32 %19, i32 %25
%29 = or i64 %21, 1
%30 = getelementptr inbounds i32, i32* %4, i64 %29
store i32 %28, i32* %30
%31 = add nsw i32 %20, 2
%32 = select i1 %24, i32 %31, i32 1
%33 = icmp slt i32 %32, %17
%34 = add nsw i32 %28, 32
%35 = icmp sgt i32 %28, 4063
%36 = select i1 %33, i32 %32, i32 0
%37 = select i1 %33, i1 true, i1 %35
%38 = select i1 %37, i32 %28, i32 %34
%39 = add nuw nsw i64 %21, 2
%40 = icmp eq i64 %39, 128
br i1 %40, label %41, label %18
41: ; preds = %18
%42 = tail call i32 @generate_randindices(i32* %6)
br label %43
43: ; preds = %41, %43
%44 = phi i32 [ 0, %41 ], [ %46, %43 ]
%45 = tail call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i64 16, i64 1, i32* %7)
%46 = add nuw nsw i32 %44, 1
%47 = icmp eq i32 %46, 128
br i1 %47, label %48, label %43
48: ; preds = %43
%49 = tail call i32 @fclose(i32* %7)
%50 = tail call i32 @fclose(i32* %8)
br label %51
51: ; preds = %10, %48
ret i32 0
}