#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 */
double allocateAndTest(int, int, int);
double allocateAndTestMainMemory(int, int, int /*<<< orphan*/);
double get90thPercentile(double *, int);
int /*<<< orphan*/ printf(char *, double, ...);
void discoverCachesWithoutCPUID() {
  int i, j;
  unsigned int cycles;
  unsigned short counter = 1;
  double cycleCollection[50];
  for (j = 0; j < 50; j++)
    cycleCollection[j] = allocateAndTest(1024, 512, 1);
  double val;
  val = get90thPercentile(cycleCollection, 50);
  printf("L1_dlat = %.2lf\n", val);
  printf("L1_ilat = %.2lf\n", val);
  cycles = val;
  for (i = 32768; i < 4 * 1024 * 1024; i += 32768) {
    for (j = 0; j < 50; j++)
      cycleCollection[j] = allocateAndTest(i, 512, 1);
    val = get90thPercentile(cycleCollection, 50);
    if (val > 5 * cycles) {
      if (counter == 1)
        printf("L%d_dlat = %.2lf\n", ++counter, val);
      else
        printf("L%d_lat = %.2lf\n", ++counter, val);
      cycles = val;
      if (counter == 3) {
        break;
      }
    }
  }
  for (j = 0; j < 50; j++)
    cycleCollection[j] = allocateAndTestMainMemory(30 * i, 1024, 0);
  printf("mem_lat = %.2lf\n", get90thPercentile(cycleCollection, 50));
}


x86 assembly (clang, -O0):
<discoverCachesWithoutCPUID>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movw   $0x1,-0xe(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    1191 <discoverCachesWithoutCPUID+0x51>
mov    $0x400,%edi
mov    $0x200,%esi
mov    $0x1,%edx
call   1060 <allocateAndTest@plt>
movslq -0x8(%rbp),%rax
movsd  %xmm0,-0x1a0(%rbp,%rax,8)
ff ff
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1158 <discoverCachesWithoutCPUID+0x18>
lea    -0x1a0(%rbp),%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
movsd  %xmm0,-0x1a8(%rbp)
ff
movsd  -0x1a8(%rbp),%xmm0
ff
lea    0xe47(%rip),%rdi
mov    $0x1,%al
mov    %al,-0x1a9(%rbp)
call   1030 <printf@plt>
mov    -0x1a9(%rbp),%al
movsd  -0x1a8(%rbp),%xmm0
ff
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
cvttsd2si -0x1a8(%rbp),%rax
ff ff
mov    %eax,-0xc(%rbp)
movl   $0x8000,-0x4(%rbp)
cmpl   $0x400000,-0x4(%rbp)
jge    1310 <discoverCachesWithoutCPUID+0x1d0>
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    123e <discoverCachesWithoutCPUID+0xfe>
mov    -0x4(%rbp),%edi
mov    $0x200,%esi
mov    $0x1,%edx
call   1060 <allocateAndTest@plt>
movslq -0x8(%rbp),%rax
movsd  %xmm0,-0x1a0(%rbp,%rax,8)
ff ff
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1207 <discoverCachesWithoutCPUID+0xc7>
lea    -0x1a0(%rbp),%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
movsd  %xmm0,-0x1a8(%rbp)
ff
movsd  -0x1a8(%rbp),%xmm0
ff
mov    -0xc(%rbp),%ecx
mov    %ecx,%eax
lea    (%rax,%rax,4),%eax
cvtsi2sd %rax,%xmm1
ucomisd %xmm1,%xmm0
jbe    12fb <discoverCachesWithoutCPUID+0x1bb>
movzwl -0xe(%rbp),%eax
cmp    $0x1,%eax
jne    12b0 <discoverCachesWithoutCPUID+0x170>
movzwl -0xe(%rbp),%eax
add    $0x1,%eax
mov    %ax,-0xe(%rbp)
movzwl %ax,%eax
cvtsi2sd %eax,%xmm0
movsd  -0x1a8(%rbp),%xmm1
ff
lea    0xd7e(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
jmp    12d8 <discoverCachesWithoutCPUID+0x198>
movzwl -0xe(%rbp),%eax
add    $0x1,%eax
mov    %ax,-0xe(%rbp)
movzwl %ax,%eax
cvtsi2sd %eax,%xmm0
movsd  -0x1a8(%rbp),%xmm1
ff
lea    0xd63(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
cvttsd2si -0x1a8(%rbp),%rax
ff ff
mov    %eax,-0xc(%rbp)
movzwl -0xe(%rbp),%eax
cmp    $0x3,%eax
jne    12f6 <discoverCachesWithoutCPUID+0x1b6>
jmp    1310 <discoverCachesWithoutCPUID+0x1d0>
jmp    12fb <discoverCachesWithoutCPUID+0x1bb>
jmp    1300 <discoverCachesWithoutCPUID+0x1c0>
mov    -0x4(%rbp),%eax
add    $0x8000,%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <discoverCachesWithoutCPUID+0xb3>
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    134c <discoverCachesWithoutCPUID+0x20c>
imul   $0x1e,-0x4(%rbp),%edi
mov    $0x400,%esi
xor    %edx,%edx
call   1040 <allocateAndTestMainMemory@plt>
movslq -0x8(%rbp),%rax
movsd  %xmm0,-0x1a0(%rbp,%rax,8)
ff ff
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1317 <discoverCachesWithoutCPUID+0x1d7>
lea    -0x1a0(%rbp),%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
lea    0xce1(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discoverCachesWithoutCPUID>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x400,%edi
mov    $0x200,%esi
mov    $0x1,%edx
call   1060 <allocateAndTest@plt>
movsd  %xmm0,0x10(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x32,%rbx
jne    1160 <discoverCachesWithoutCPUID+0x20>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
movsd  %xmm0,0x8(%rsp)
lea    0xe65(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
lea    0xe68(%rip),%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1030 <printf@plt>
cvttsd2si 0x8(%rsp),%r13
mov    $0x8000,%ebx
mov    $0x1,%r12w
jmp    1214 <discoverCachesWithoutCPUID+0xd4>
lea    0xe52(%rip),%rdi
movsd  0xe20(%rip),%xmm0
mov    $0x2,%al
call   1030 <printf@plt>
cvttsd2si 0x8(%rsp),%r13
mov    $0x2,%r12w
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x8000(%rbx),%eax
cmp    $0x3f8000,%ebx
mov    %eax,%ebx
jae    12a6 <discoverCachesWithoutCPUID+0x166>
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x200,%esi
mov    $0x1,%edx
call   1060 <allocateAndTest@plt>
movsd  %xmm0,0x10(%rsp,%r15,8)
add    $0x1,%r15
cmp    $0x32,%r15
jne    1220 <discoverCachesWithoutCPUID+0xe0>
mov    %r14,%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
movapd %xmm0,%xmm1
lea    0x0(%r13,%r13,4),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
ucomisd %xmm0,%xmm1
jbe    1200 <discoverCachesWithoutCPUID+0xc0>
cmp    $0x1,%r12w
movsd  %xmm1,0x8(%rsp)
je     11d1 <discoverCachesWithoutCPUID+0x91>
add    $0x1,%r12d
movzwl %r12w,%ebp
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
lea    0xdaf(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
cmp    $0x3,%bp
je     12ab <discoverCachesWithoutCPUID+0x16b>
cvttsd2si 0x8(%rsp),%r13
jmp    1200 <discoverCachesWithoutCPUID+0xc0>
mov    $0x400000,%ebx
mov    %ebx,%ebp
shl    $0x5,%ebp
sub    %ebx,%ebp
sub    %ebx,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0x400,%esi
xor    %edx,%edx
call   1040 <allocateAndTestMainMemory@plt>
movsd  %xmm0,0x10(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x32,%rbx
jne    12c0 <discoverCachesWithoutCPUID+0x180>
lea    0x10(%rsp),%rdi
mov    $0x32,%esi
call   1050 <get90thPercentile@plt>
lea    0xd59(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"L1_dlat = %.2lf\0A\00"
@.str.1 = [17 x i8] c"L1_ilat = %.2lf\0A\00"
@.str.2 = [18 x i8] c"L%d_dlat = %.2lf\0A\00"
@.str.3 = [17 x i8] c"L%d_lat = %.2lf\0A\00"
@.str.4 = [17 x i8] c"mem_lat = %.2lf\0A\00"
define void @discoverCachesWithoutCPUID() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i16
%5 = alloca [50 x double]
%6 = alloca double
store i16 1, i16* %4
store i32 0, i32* %2
br label %7
7: ; preds = %15, %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 50
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = call double @allocateAndTest(i32 1024, i32 512, i32 1)
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 %13
store double %11, double* %14
br label %15
15: ; preds = %10
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %7
18: ; preds = %7
%19 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 0
%20 = call double @get90thPercentile(double* %19, i32 50)
store double %20, double* %6
%21 = load double, double* %6
%22 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), double %21)
%23 = load double, double* %6
%24 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %23)
%25 = load double, double* %6
%26 = fptoui double %25 to i32
store i32 %26, i32* %3
store i32 32768, i32* %1
br label %27
27: ; preds = %76, %18
%28 = load i32, i32* %1
%29 = icmp slt i32 %28, 4194304
br i1 %29, label %30, label %79
30: ; preds = %27
store i32 0, i32* %2
br label %31
31: ; preds = %40, %30
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 50
br i1 %33, label %34, label %43
34: ; preds = %31
%35 = load i32, i32* %1
%36 = call double @allocateAndTest(i32 %35, i32 512, i32 1)
%37 = load i32, i32* %2
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 %38
store double %36, double* %39
br label %40
40: ; preds = %34
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
br label %31
43: ; preds = %31
%44 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 0
%45 = call double @get90thPercentile(double* %44, i32 50)
store double %45, double* %6
%46 = load double, double* %6
%47 = load i32, i32* %3
%48 = mul i32 5, %47
%49 = uitofp i32 %48 to double
%50 = fcmp ogt double %46, %49
br i1 %50, label %51, label %75
51: ; preds = %43
%52 = load i16, i16* %4
%53 = zext i16 %52 to i32
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %61
55: ; preds = %51
%56 = load i16, i16* %4
%57 = add i16 %56, 1
store i16 %57, i16* %4
%58 = uitofp i16 %57 to double
%59 = load double, double* %6
%60 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %58, double %59)
br label %67
61: ; preds = %51
%62 = load i16, i16* %4
%63 = add i16 %62, 1
store i16 %63, i16* %4
%64 = uitofp i16 %63 to double
%65 = load double, double* %6
%66 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), double %64, double %65)
br label %67
67: ; preds = %61, %55
%68 = load double, double* %6
%69 = fptoui double %68 to i32
store i32 %69, i32* %3
%70 = load i16, i16* %4
%71 = zext i16 %70 to i32
%72 = icmp eq i32 %71, 3
br i1 %72, label %73, label %74
73: ; preds = %67
br label %79
74: ; preds = %67
br label %75
75: ; preds = %74, %43
br label %76
76: ; preds = %75
%77 = load i32, i32* %1
%78 = add nsw i32 %77, 32768
store i32 %78, i32* %1
br label %27
79: ; preds = %73, %27
store i32 0, i32* %2
br label %80
80: ; preds = %90, %79
%81 = load i32, i32* %2
%82 = icmp slt i32 %81, 50
br i1 %82, label %83, label %93
83: ; preds = %80
%84 = load i32, i32* %1
%85 = mul nsw i32 30, %84
%86 = call double @allocateAndTestMainMemory(i32 %85, i32 1024, i32 0)
%87 = load i32, i32* %2
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 %88
store double %86, double* %89
br label %90
90: ; preds = %83
%91 = load i32, i32* %2
%92 = add nsw i32 %91, 1
store i32 %92, i32* %2
br label %80
93: ; preds = %80
%94 = getelementptr inbounds [50 x double], [50 x double]* %5, i64 0, i64 0
%95 = call double @get90thPercentile(double* %94, i32 50)
%96 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), double %95)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"L1_dlat = %.2lf\0A\00"
@.str.1 = [17 x i8] c"L1_ilat = %.2lf\0A\00"
@.str.2 = [18 x i8] c"L%d_dlat = %.2lf\0A\00"
@.str.3 = [17 x i8] c"L%d_lat = %.2lf\0A\00"
@.str.4 = [17 x i8] c"mem_lat = %.2lf\0A\00"
define void @discoverCachesWithoutCPUID() {
%1 = alloca [50 x double]
%2 = bitcast [50 x double]* %1 to i8*
br label %3
3: ; preds = %0, %3
%4 = phi i64 [ 0, %0 ], [ %7, %3 ]
%5 = tail call double @allocateAndTest(i32 1024, i32 512, i32 1)
%6 = getelementptr inbounds [50 x double], [50 x double]* %1, i64 0, i64 %4
store double %5, double* %6
%7 = add nuw nsw i64 %4, 1
%8 = icmp eq i64 %7, 50
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = getelementptr inbounds [50 x double], [50 x double]* %1, i64 0, i64 0
%11 = call double @get90thPercentile(double* %10, i32 50)
%12 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), double %11)
%13 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %11)
%14 = fptoui double %11 to i32
br label %15
15: ; preds = %9, %41
%16 = phi i16 [ 1, %9 ], [ %43, %41 ]
%17 = phi i32 [ %14, %9 ], [ %42, %41 ]
%18 = phi i32 [ 32768, %9 ], [ %44, %41 ]
br label %19
19: ; preds = %15, %19
%20 = phi i64 [ 0, %15 ], [ %23, %19 ]
%21 = call double @allocateAndTest(i32 %18, i32 512, i32 1)
%22 = getelementptr inbounds [50 x double], [50 x double]* %1, i64 0, i64 %20
store double %21, double* %22
%23 = add nuw nsw i64 %20, 1
%24 = icmp eq i64 %23, 50
br i1 %24, label %25, label %19
25: ; preds = %19
%26 = call double @get90thPercentile(double* %10, i32 50)
%27 = mul i32 %17, 5
%28 = uitofp i32 %27 to double
%29 = fcmp ogt double %26, %28
br i1 %29, label %30, label %41
30: ; preds = %25
%31 = icmp eq i16 %16, 1
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double 2.000000e+00, double %26)
%34 = fptoui double %26 to i32
br label %41
35: ; preds = %30
%36 = add i16 %16, 1
%37 = uitofp i16 %36 to double
%38 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), double %37, double %26)
%39 = fptoui double %26 to i32
%40 = icmp eq i16 %36, 3
br i1 %40, label %46, label %41
41: ; preds = %32, %25, %35
%42 = phi i32 [ %39, %35 ], [ %17, %25 ], [ %34, %32 ]
%43 = phi i16 [ %36, %35 ], [ %16, %25 ], [ 2, %32 ]
%44 = add nuw nsw i32 %18, 32768
%45 = icmp ult i32 %18, 4161536
br i1 %45, label %15, label %46
46: ; preds = %35, %41
%47 = phi i32 [ %18, %35 ], [ 4194304, %41 ]
%48 = mul nsw i32 %47, 30
br label %49
49: ; preds = %46, %49
%50 = phi i64 [ 0, %46 ], [ %53, %49 ]
%51 = call double @allocateAndTestMainMemory(i32 %48, i32 1024, i32 0)
%52 = getelementptr inbounds [50 x double], [50 x double]* %1, i64 0, i64 %50
store double %51, double* %52
%53 = add nuw nsw i64 %50, 1
%54 = icmp eq i64 %53, 50
br i1 %54, label %55, label %49
55: ; preds = %49
%56 = call double @get90thPercentile(double* %10, i32 50)
%57 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), double %56)
ret void
}