#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 unsigned long long uint64_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open_msr(int);
int /*<<< orphan*/ printf(char *, ...);
unsigned long long read_msr(int, int);
unsigned long long write_msr(int, int, unsigned long long);
int enable_prefetch_core2(int core) {
  int fd;
  uint64_t result;
  int begin, end, c;
  printf("Enable 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, 0x1a0);
    printf("\tCore %d old : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\n", c,
           result & (1ULL << 9) ? 'N' : 'Y', result & (1ULL << 19) ? 'N' : 'Y',
           result & (1ULL << 37) ? 'N' : 'Y',
           result & (1ULL << 39) ? 'N' : 'Y');
    result &= ~((1ULL << 9) | (1ULL << 19) | (1ULL << 37) | (1ULL << 39));
    result = write_msr(fd, 0x1a0, result);
    result = read_msr(fd, 0x1a0);
    printf("\tCore %d new : L2HW=%c L2ADJ=%c DCU=%c DCUIP=%c\n", c,
           result & (1ULL << 9) ? 'N' : 'Y', result & (1ULL << 19) ? 'N' : 'Y',
           result & (1ULL << 37) ? 'N' : 'Y',
           result & (1ULL << 39) ? 'N' : 'Y');
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<enable_prefetch_core2>:
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 <enable_prefetch_core2+0x36>
movl   $0x0,-0x14(%rbp)
movl   $0x400,-0x18(%rbp)
jmp    1192 <enable_prefetch_core2+0x42>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1333 <enable_prefetch_core2+0x1e3>
mov    -0x1c(%rbp),%edi
call   1050 <open_msr@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11be <enable_prefetch_core2+0x6e>
jmp    1333 <enable_prefetch_core2+0x1e3>
mov    -0x8(%rbp),%edi
mov    $0x1a0,%esi
call   1060 <read_msr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
and    $0x200,%rcx
mov    $0x59,%edx
mov    $0x4e,%eax
cmp    $0x0,%rcx
cmovne %eax,%edx
mov    -0x10(%rbp),%rdi
and    $0x80000,%rdi
mov    $0x59,%ecx
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%ecx
movabs $0x2000000000,%rdi
and    -0x10(%rbp),%rdi
mov    $0x59,%r8d
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%r8d
movabs $0x8000000000,%rdi
and    -0x10(%rbp),%rdi
mov    $0x59,%r9d
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%r9d
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movabs $0xffffff5ffff7fdff,%rax
ff ff ff
and    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    $0x1a0,%esi
call   1070 <write_msr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1a0,%esi
call   1060 <read_msr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
and    $0x200,%rcx
mov    $0x59,%edx
mov    $0x4e,%eax
cmp    $0x0,%rcx
cmovne %eax,%edx
mov    -0x10(%rbp),%rdi
and    $0x80000,%rdi
mov    $0x59,%ecx
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%ecx
movabs $0x2000000000,%rdi
and    -0x10(%rbp),%rdi
mov    $0x59,%r8d
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%r8d
movabs $0x8000000000,%rdi
and    -0x10(%rbp),%rdi
mov    $0x59,%r9d
mov    $0x4e,%eax
cmp    $0x0,%rdi
cmovne %eax,%r9d
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1198 <enable_prefetch_core2+0x48>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enable_prefetch_core2>:
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    %r12d,%r12d
cmp    $0xffffffff,%ebp
cmovne %ebp,%r12d
mov    $0x400,%eax
cmovne %ebp,%eax
mov    %eax,0x4(%rsp)
cmp    %eax,%r12d
jg     12af <enable_prefetch_core2+0x14f>
addl   $0x1,0x4(%rsp)
mov    $0x59,%r14d
movabs $0x2000000000,%r13
movabs $0x8000000000,%r15
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1060 <open_msr@plt>
test   %eax,%eax
js     12af <enable_prefetch_core2+0x14f>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1a0,%esi
call   1070 <read_msr@plt>
mov    %rax,%rbx
test   $0x200,%ebx
mov    $0x4e,%edx
cmove  %r14d,%edx
test   $0x80000,%ebx
mov    $0x4e,%ecx
cmove  %r14d,%ecx
test   %r13,%rax
mov    $0x4e,%r8d
cmove  %r14d,%r8d
test   %r15,%rax
mov    $0x4e,%r9d
cmove  %r14d,%r9d
lea    0xde0(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0xffffff5ffff7fdff,%rax
ff ff ff
and    %rax,%rbx
mov    %ebp,%edi
mov    $0x1a0,%esi
mov    %rbx,%rdx
call   1080 <write_msr@plt>
mov    %ebp,%edi
mov    $0x1a0,%esi
call   1070 <read_msr@plt>
test   $0x200,%eax
mov    $0x4e,%edx
cmove  %r14d,%edx
test   $0x80000,%eax
mov    $0x4e,%ecx
cmove  %r14d,%ecx
test   %r13,%rax
mov    $0x4e,%r8d
cmove  %r14d,%r8d
test   %r15,%rax
mov    $0x4e,%r9d
cmove  %r14d,%r9d
lea    0xda2(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%r12d
cmp    %r12d,0x4(%rsp)
jne    11c0 <enable_prefetch_core2+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Enable 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 @enable_prefetch_core2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 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 = %83, %15
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %86
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 %86
27: ; preds = %21
%28 = load i32, i32* %3
%29 = call i64 @read_msr(i32 %28, i32 416)
store i64 %29, i64* %4
%30 = load i32, i32* %7
%31 = load i64, i64* %4
%32 = and i64 %31, 512
%33 = icmp ne i64 %32, 0
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 78, i32 89
%36 = load i64, i64* %4
%37 = and i64 %36, 524288
%38 = icmp ne i64 %37, 0
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 78, i32 89
%41 = load i64, i64* %4
%42 = and i64 %41, 137438953472
%43 = icmp ne i64 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 78, i32 89
%46 = load i64, i64* %4
%47 = and i64 %46, 549755813888
%48 = icmp ne i64 %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 i64, i64* %4
%53 = and i64 %52, -687195292161
store i64 %53, i64* %4
%54 = load i32, i32* %3
%55 = load i64, i64* %4
%56 = call i64 @write_msr(i32 %54, i32 416, i64 %55)
store i64 %56, i64* %4
%57 = load i32, i32* %3
%58 = call i64 @read_msr(i32 %57, i32 416)
store i64 %58, i64* %4
%59 = load i32, i32* %7
%60 = load i64, i64* %4
%61 = and i64 %60, 512
%62 = icmp ne i64 %61, 0
%63 = zext i1 %62 to i64
%64 = select i1 %62, i32 78, i32 89
%65 = load i64, i64* %4
%66 = and i64 %65, 524288
%67 = icmp ne i64 %66, 0
%68 = zext i1 %67 to i64
%69 = select i1 %67, i32 78, i32 89
%70 = load i64, i64* %4
%71 = and i64 %70, 137438953472
%72 = icmp ne i64 %71, 0
%73 = zext i1 %72 to i64
%74 = select i1 %72, i32 78, i32 89
%75 = load i64, i64* %4
%76 = and i64 %75, 549755813888
%77 = icmp ne i64 %76, 0
%78 = zext i1 %77 to i64
%79 = select i1 %77, i32 78, i32 89
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %59, i32 %64, i32 %69, i32 %74, i32 %79)
%81 = load i32, i32* %3
%82 = call i32 @close(i32 %81)
br label %83
83: ; preds = %27
%84 = load i32, i32* %7
%85 = add nsw i32 %84, 1
store i32 %85, i32* %7
br label %17
86: ; 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 = [20 x i8] c"Enable all prefetch\00"
define i32 @enable_prefetch_core2(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 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 %45, label %7
7: ; preds = %1, %11
%8 = phi i32 [ %43, %11 ], [ %4, %1 ]
%9 = tail call i32 @open_msr(i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %45, label %11
11: ; preds = %7
%12 = tail call i64 @read_msr(i32 %9, i32 416)
%13 = and i64 %12, 512
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i32 89, i32 78
%16 = and i64 %12, 524288
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i32 89, i32 78
%19 = and i64 %12, 137438953472
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i32 89, i32 78
%22 = and i64 %12, 549755813888
%23 = icmp eq i64 %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 = and i64 %12, -687195292161
%27 = tail call i64 @write_msr(i32 %9, i32 416, i64 %26)
%28 = tail call i64 @read_msr(i32 %9, i32 416)
%29 = and i64 %28, 512
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 89, i32 78
%32 = and i64 %28, 524288
%33 = icmp eq i64 %32, 0
%34 = select i1 %33, i32 89, i32 78
%35 = and i64 %28, 137438953472
%36 = icmp eq i64 %35, 0
%37 = select i1 %36, i32 89, i32 78
%38 = and i64 %28, 549755813888
%39 = icmp eq i64 %38, 0
%40 = select i1 %39, i32 89, i32 78
%41 = 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 %31, i32 %34, i32 %37, i32 %40)
%42 = tail call i32 @close(i32 %9)
%43 = add i32 %8, 1
%44 = icmp eq i32 %8, %5
br i1 %44, label %45, label %7
45: ; preds = %11, %7, %1
ret i32 0
}