#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*/ close(int);
int open_msr(int);
int /*<<< orphan*/ printf(char *, ...);
int read_msr(int, int);
int write_msr(int, int, int);
int disable_prefetch_nhm(int core) {
  int fd;
  int result;
  int begin, end, c;
  printf("Disable all prefetch\n");
  if (core == -1) {
    begin = 0;
    end = 1024;
  } else {
    begin = core;
    end = core;
  }
  for (c = begin; c <= end; c++) {
    fd = open_msr(c);
    if (fd < 0)
      break;
    result = read_msr(fd, 0x1a4);
    printf("\tCore %d old : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\n", c,
           result & 0x1 ? 'N' : 'Y', result & 0x2 ? 'N' : 'Y',
           result & 0x4 ? 'N' : 'Y', result & 0x8 ? 'N' : 'Y');
    result = write_msr(fd, 0x1a4, 0xf);
    result = read_msr(fd, 0x1a4);
    printf("\tCore %d new : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\n", c,
           result & 0x1 ? 'N' : 'Y', result & 0x2 ? 'N' : 'Y',
           result & 0x4 ? 'N' : 'Y', result & 0x8 ? 'N' : 'Y');
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<disable_prefetch_nhm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x4(%rbp)
jne    1186 <disable_prefetch_nhm+0x36>
movl   $0x0,-0x10(%rbp)
movl   $0x400,-0x14(%rbp)
jmp    1192 <disable_prefetch_nhm+0x42>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     12e3 <disable_prefetch_nhm+0x193>
mov    -0x18(%rbp),%edi
call   1050 <open_msr@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11be <disable_prefetch_nhm+0x6e>
jmp    12e3 <disable_prefetch_nhm+0x193>
mov    -0x8(%rbp),%edi
mov    $0x1a4,%esi
call   1060 <read_msr@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%esi
mov    -0xc(%rbp),%ecx
and    $0x1,%ecx
mov    $0x59,%edx
mov    $0x4e,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0xc(%rbp),%edi
and    $0x2,%edi
mov    $0x59,%ecx
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%ecx
mov    -0xc(%rbp),%edi
and    $0x4,%edi
mov    $0x59,%r8d
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%r8d
mov    -0xc(%rbp),%edi
and    $0x8,%edi
mov    $0x59,%r9d
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%r9d
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x1a4,%esi
mov    $0xf,%edx
call   1070 <write_msr@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1a4,%esi
call   1060 <read_msr@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%esi
mov    -0xc(%rbp),%ecx
and    $0x1,%ecx
mov    $0x59,%edx
mov    $0x4e,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0xc(%rbp),%edi
and    $0x2,%edi
mov    $0x59,%ecx
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%ecx
mov    -0xc(%rbp),%edi
and    $0x4,%edi
mov    $0x59,%r8d
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%r8d
mov    -0xc(%rbp),%edi
and    $0x8,%edi
mov    $0x59,%r9d
mov    $0x4e,%eax
cmp    $0x0,%edi
cmovne %eax,%r9d
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1198 <disable_prefetch_nhm+0x48>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disable_prefetch_nhm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xeee(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%ebp
cmovne %ebp,%ebx
mov    $0x400,%r12d
cmovne %ebp,%r12d
cmp    %r12d,%ebx
jg     1271 <disable_prefetch_nhm+0x111>
add    $0x1,%r12d
mov    $0x59,%r13d
lea    0xe5b(%rip),%r14
lea    0xe85(%rip),%r15
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <open_msr@plt>
test   %eax,%eax
js     1271 <disable_prefetch_nhm+0x111>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1a4,%esi
call   1070 <read_msr@plt>
test   $0x1,%al
mov    $0x4e,%edx
cmove  %r13d,%edx
test   $0x2,%al
mov    $0x4e,%ecx
cmove  %r13d,%ecx
test   $0x4,%al
mov    $0x4e,%r8d
cmove  %r13d,%r8d
test   $0x8,%al
mov    $0x4e,%r9d
cmove  %r13d,%r9d
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x1a4,%esi
mov    $0xf,%edx
call   1080 <write_msr@plt>
mov    %ebp,%edi
mov    $0x1a4,%esi
call   1070 <read_msr@plt>
test   $0x1,%al
mov    $0x4e,%edx
cmove  %r13d,%edx
test   $0x2,%al
mov    $0x4e,%ecx
cmove  %r13d,%ecx
test   $0x4,%al
mov    $0x4e,%r8d
cmove  %r13d,%r8d
test   $0x8,%al
mov    $0x4e,%r9d
cmove  %r13d,%r9d
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%r12d
jne    11b0 <disable_prefetch_nhm+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Disable all prefetch\0A\00"
@.str.1 = [49 x i8] c"\09Core %d old : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\0A\00"
@.str.2 = [49 x i8] c"\09Core %d new : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\0A\00"
define i32 @disable_prefetch_nhm(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %5
store i32 1024, i32* %6
br label %15
12: ; preds = %1
%13 = load i32, i32* %2
store i32 %13, i32* %5
%14 = load i32, i32* %2
store i32 %14, i32* %6
br label %15
15: ; preds = %12, %11
%16 = load i32, i32* %5
store i32 %16, i32* %7
br label %17
17: ; preds = %80, %15
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %83
21: ; preds = %17
%22 = load i32, i32* %7
%23 = call i32 @open_msr(i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %83
27: ; preds = %21
%28 = load i32, i32* %3
%29 = call i32 @read_msr(i32 %28, i32 420)
store i32 %29, i32* %4
%30 = load i32, i32* %7
%31 = load i32, i32* %4
%32 = and i32 %31, 1
%33 = icmp ne i32 %32, 0
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 78, i32 89
%36 = load i32, i32* %4
%37 = and i32 %36, 2
%38 = icmp ne i32 %37, 0
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 78, i32 89
%41 = load i32, i32* %4
%42 = and i32 %41, 4
%43 = icmp ne i32 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 78, i32 89
%46 = load i32, i32* %4
%47 = and i32 %46, 8
%48 = icmp ne i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 78, i32 89
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %35, i32 %40, i32 %45, i32 %50)
%52 = load i32, i32* %3
%53 = call i32 @write_msr(i32 %52, i32 420, i32 15)
store i32 %53, i32* %4
%54 = load i32, i32* %3
%55 = call i32 @read_msr(i32 %54, i32 420)
store i32 %55, i32* %4
%56 = load i32, i32* %7
%57 = load i32, i32* %4
%58 = and i32 %57, 1
%59 = icmp ne i32 %58, 0
%60 = zext i1 %59 to i64
%61 = select i1 %59, i32 78, i32 89
%62 = load i32, i32* %4
%63 = and i32 %62, 2
%64 = icmp ne i32 %63, 0
%65 = zext i1 %64 to i64
%66 = select i1 %64, i32 78, i32 89
%67 = load i32, i32* %4
%68 = and i32 %67, 4
%69 = icmp ne i32 %68, 0
%70 = zext i1 %69 to i64
%71 = select i1 %69, i32 78, i32 89
%72 = load i32, i32* %4
%73 = and i32 %72, 8
%74 = icmp ne i32 %73, 0
%75 = zext i1 %74 to i64
%76 = select i1 %74, i32 78, i32 89
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %61, i32 %66, i32 %71, i32 %76)
%78 = load i32, i32* %3
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %27
%81 = load i32, i32* %7
%82 = add nsw i32 %81, 1
store i32 %82, i32* %7
br label %17
83: ; preds = %26, %17
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [49 x i8] c"\09Core %d old : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\0A\00"
@.str.2 = [49 x i8] c"\09Core %d new : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\0A\00"
@str = [21 x i8] c"Disable all prefetch\00"
define i32 @disable_prefetch_nhm(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%3 = icmp eq i32 %0, -1
%4 = select i1 %3, i32 0, i32 %0
%5 = select i1 %3, i32 1024, i32 %0
%6 = icmp sgt i32 %4, %5
br i1 %6, label %44, label %7
7: ; preds = %1, %11
%8 = phi i32 [ %42, %11 ], [ %4, %1 ]
%9 = tail call i32 @open_msr(i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %44, label %11
11: ; preds = %7
%12 = tail call i32 @read_msr(i32 %9, i32 420)
%13 = and i32 %12, 1
%14 = icmp eq i32 %13, 0
%15 = select i1 %14, i32 89, i32 78
%16 = and i32 %12, 2
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, i32 89, i32 78
%19 = and i32 %12, 4
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 89, i32 78
%22 = and i32 %12, 8
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i32 89, i32 78
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %15, i32 %18, i32 %21, i32 %24)
%26 = tail call i32 @write_msr(i32 %9, i32 420, i32 15)
%27 = tail call i32 @read_msr(i32 %9, i32 420)
%28 = and i32 %27, 1
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i32 89, i32 78
%31 = and i32 %27, 2
%32 = icmp eq i32 %31, 0
%33 = select i1 %32, i32 89, i32 78
%34 = and i32 %27, 4
%35 = icmp eq i32 %34, 0
%36 = select i1 %35, i32 89, i32 78
%37 = and i32 %27, 8
%38 = icmp eq i32 %37, 0
%39 = select i1 %38, i32 89, i32 78
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %30, i32 %33, i32 %36, i32 %39)
%41 = tail call i32 @close(i32 %9)
%42 = add i32 %8, 1
%43 = icmp eq i32 %8, %5
br i1 %43, label %44, label %7
44: ; preds = %11, %7, %1
ret i32 0
}