#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);
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, "src[i + %d] = dest;\n", indexarr[randindices[i]]);
    fprintf(defines, "flush_cl(src + i + %d);\n", indexarr[randindices[i]]);
    fprintf(flush, "flush_cl(src + i + %d);\n", indexarr[randindices[i]]);
  }
  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    136f <main+0x20f>
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    1356 <main+0x1f6>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movslq -0x14(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd17(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movslq -0x14(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd09(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movslq -0x14(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xce6(%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,%r12
mov    %edi,%r13d
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe6e(%rip),%rdi
lea    0xe71(%rip),%rbp
mov    %rbp,%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xe61(%rip),%rdi
mov    %rbp,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
mov    $0x20,%ecx
cmp    $0x2,%r13d
jl     11d5 <main+0x75>
mov    0x8(%r12),%rdi
call   1070 <atoi@plt>
mov    %eax,%ecx
test   %eax,%eax
je     12a7 <main+0x147>
mov    %rbp,%r13
xor    %r8d,%r8d
mov    $0x80,%eax
xor    %edx,%edx
idiv   %ecx
mov    $0x1,%r9d
xor    %ebp,%ebp
xor    %edx,%edx
xor    %edi,%edi
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    %r14,%rdi
call   1080 <generate_randindices@plt>
xor    %r12d,%r12d
lea    0xde3(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
movslq (%r14,%r12,4),%rax
mov    (%rbx,%rax,4),%edx
mov    %r15,%rdi
lea    0xdb3(%rip),%rsi
call   1040 <fprintf@plt>
movslq (%r14,%r12,4),%rax
mov    (%rbx,%rax,4),%edx
mov    %r15,%rdi
mov    %rbp,%rsi
call   1040 <fprintf@plt>
movslq (%r14,%r12,4),%rax
mov    (%rbx,%rax,4),%edx
mov    %r13,%rdi
mov    %rbp,%rsi
call   1040 <fprintf@plt>
add    $0x1,%r12
cmp    $0x80,%r12
jne    1250 <main+0xf0>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r13,%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 = [21 x i8] c"src[i + %d] = dest;\0A\00"
@.str.4 = [25 x i8] c"flush_cl(src + i + %d);\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 %123
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 = %115, %75
%79 = load i32, i32* %6
%80 = icmp slt i32 %79, 128
br i1 %80, label %81, label %118
81: ; preds = %78
%82 = load i32*, i32** %14
%83 = load i32*, i32** %12
%84 = load i32*, i32** %13
%85 = load i32, i32* %6
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
%88 = load i32, i32* %87
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i32, i32* %83, i64 %89
%91 = load i32, i32* %90
%92 = call i32 @fprintf(i32* %82, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %91)
%93 = load i32*, i32** %14
%94 = load i32*, i32** %12
%95 = load i32*, i32** %13
%96 = load i32, i32* %6
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
%99 = load i32, i32* %98
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i32, i32* %94, i64 %100
%102 = load i32, i32* %101
%103 = call i32 @fprintf(i32* %93, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %102)
%104 = load i32*, i32** %15
%105 = load i32*, i32** %12
%106 = load i32*, i32** %13
%107 = load i32, i32* %6
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i32, i32* %106, i64 %108
%110 = load i32, i32* %109
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds i32, i32* %105, i64 %111
%113 = load i32, i32* %112
%114 = call i32 @fprintf(i32* %104, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %113)
br label %115
115: ; preds = %81
%116 = load i32, i32* %6
%117 = add nsw i32 %116, 1
store i32 %117, i32* %6
br label %78
118: ; preds = %78
%119 = load i32*, i32** %14
%120 = call i32 @fclose(i32* %119)
%121 = load i32*, i32** %15
%122 = call i32 @fclose(i32* %121)
store i32 0, i32* %3
br label %123
123: ; preds = %118, %42
%124 = load i32, i32* %3
ret i32 %124
}

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 = [21 x i8] c"src[i + %d] = dest;\0A\00"
@.str.4 = [25 x i8] c"flush_cl(src + i + %d);\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 %66, 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 i64 [ 0, %41 ], [ %61, %43 ]
%45 = getelementptr inbounds i32, i32* %6, i64 %44
%46 = load i32, i32* %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %4, i64 %47
%49 = load i32, i32* %48
%50 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %45
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %4, i64 %52
%54 = load i32, i32* %53
%55 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %45
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %4, i64 %57
%59 = load i32, i32* %58
%60 = tail call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = add nuw nsw i64 %44, 1
%62 = icmp eq i64 %61, 128
br i1 %62, label %63, label %43
63: ; preds = %43
%64 = tail call i32 @fclose(i32* %7)
%65 = tail call i32 @fclose(i32* %8)
br label %66
66: ; preds = %10, %63
ret i32 0
}