#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 uint64_t;
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ lseek(int, int, int);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ read(int, int *, int);
scalar_t__ write(int, int *, int);
void msr_write_test(int fildes, uint64_t cpu) {
  uint64_t data;
  ssize_t sz;
  int msr_reg;
  printf("==MSR WRITE TEST CORE %d==\n", (int)(cpu / 0x0001000000000000));
  data = 0x1111222233334444;
  msr_reg = 0xE7;
  printf("Write %lx to MSR 0x%x (IA32_MPERF), ", data, msr_reg);
  lseek(fildes, cpu | msr_reg, 1);
  sz = write(fildes, &data, sizeof(data));
  printf("Error Code: %d \n", (int)sz);
  data = 0;
  lseek(fildes, cpu | msr_reg, 1);
  sz = read(fildes, &data, sizeof(data));
  printf("Read from MSR 0x%x (IA32_MPERF) = 0x%lx, ", msr_reg, data);
  printf("Error Code: %d \n", (int)sz);
  data = 0x5555666677778888;
  msr_reg = 0xE8;
  printf("Write %lx to MSR 0x%x (IA32_APERF), ", data, msr_reg);
  lseek(fildes, cpu | msr_reg, 1);
  sz = write(fildes, &data, sizeof(data));
  printf("Error Code: %d \n", (int)sz);
  data = 0;
  lseek(fildes, cpu | msr_reg, 1);
  sz = read(fildes, &data, sizeof(data));
  printf("Read from MSR 0x%x (IA32_APERF) = 0x%lx, ", msr_reg, data);
  printf("Error Code: %d \n", (int)sz);
}


x86 assembly (clang, -O0):
<msr_write_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movabs $0x1000000000000,%rcx
cqto
idiv   %rcx
mov    %eax,%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x33334444,-0xc(%rbp)
movl   $0xe7,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
or     -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
or     -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x77778888,-0xc(%rbp)
movl   $0xe8,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
or     -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
or     -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msr_write_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r12d
mov    %edi,%ebx
lea    0xea8(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x33334444,0xc(%rsp)
33
lea    0xeac(%rip),%rdi
mov    $0x33334444,%esi
mov    $0xe7,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%ebp
or     $0xe7,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
lea    0xc(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
lea    0xe8e(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0xc(%rsp)
mov    %ebx,%edi
mov    %ebp,%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,%rbp
mov    0xc(%rsp),%edx
lea    0xe60(%rip),%rdi
mov    $0xe7,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x77778888,0xc(%rsp)
77
lea    0xe63(%rip),%rdi
mov    $0x77778888,%esi
mov    $0xe8,%edx
xor    %eax,%eax
call   1040 <printf@plt>
or     $0xe8,%r12d
mov    %ebx,%edi
mov    %r12d,%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0xc(%rsp)
mov    %ebx,%edi
mov    %r12d,%esi
mov    $0x1,%edx
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edx
lea    0xe12(%rip),%rdi
mov    $0xe8,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"==MSR WRITE TEST CORE %d==\0A\00"
@.str.1 = [37 x i8] c"Write %lx to MSR 0x%x (IA32_MPERF), \00"
@.str.2 = [17 x i8] c"Error Code: %d \0A\00"
@.str.3 = [42 x i8] c"Read from MSR 0x%x (IA32_MPERF) = 0x%lx, \00"
@.str.4 = [37 x i8] c"Write %lx to MSR 0x%x (IA32_APERF), \00"
@.str.5 = [42 x i8] c"Read from MSR 0x%x (IA32_APERF) = 0x%lx, \00"
define void @msr_write_test(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = sdiv i64 %9, 281474976710656
%11 = trunc i64 %10 to i32
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 858997828, i32* %5
store i32 231, i32* %7
%13 = load i32, i32* %5
%14 = load i32, i32* %7
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = or i32 %17, %18
%20 = call i32 @lseek(i32 %16, i32 %19, i32 1)
%21 = load i32, i32* %3
%22 = call i64 @write(i32 %21, i32* %5, i32 4)
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %24)
store i32 0, i32* %5
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = load i32, i32* %7
%29 = or i32 %27, %28
%30 = call i32 @lseek(i32 %26, i32 %29, i32 1)
%31 = load i32, i32* %3
%32 = call i64 @read(i32 %31, i32* %5, i32 4)
store i64 %32, i64* %6
%33 = load i32, i32* %7
%34 = load i32, i32* %5
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %34)
%36 = load i64, i64* %6
%37 = trunc i64 %36 to i32
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 2004322440, i32* %5
store i32 232, i32* %7
%39 = load i32, i32* %5
%40 = load i32, i32* %7
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32, i32* %3
%43 = load i32, i32* %4
%44 = load i32, i32* %7
%45 = or i32 %43, %44
%46 = call i32 @lseek(i32 %42, i32 %45, i32 1)
%47 = load i32, i32* %3
%48 = call i64 @write(i32 %47, i32* %5, i32 4)
store i64 %48, i64* %6
%49 = load i64, i64* %6
%50 = trunc i64 %49 to i32
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %50)
store i32 0, i32* %5
%52 = load i32, i32* %3
%53 = load i32, i32* %4
%54 = load i32, i32* %7
%55 = or i32 %53, %54
%56 = call i32 @lseek(i32 %52, i32 %55, i32 1)
%57 = load i32, i32* %3
%58 = call i64 @read(i32 %57, i32* %5, i32 4)
store i64 %58, i64* %6
%59 = load i32, i32* %7
%60 = load i32, i32* %5
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %59, i32 %60)
%62 = load i64, i64* %6
%63 = trunc i64 %62 to i32
%64 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %63)
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"==MSR WRITE TEST CORE %d==\0A\00"
@.str.1 = [37 x i8] c"Write %lx to MSR 0x%x (IA32_MPERF), \00"
@.str.2 = [17 x i8] c"Error Code: %d \0A\00"
@.str.3 = [42 x i8] c"Read from MSR 0x%x (IA32_MPERF) = 0x%lx, \00"
@.str.4 = [37 x i8] c"Write %lx to MSR 0x%x (IA32_APERF), \00"
@.str.5 = [42 x i8] c"Read from MSR 0x%x (IA32_APERF) = 0x%lx, \00"
define void @msr_write_test(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 858997828, i32* %3
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 858997828, i32 231)
%7 = or i32 %1, 231
%8 = tail call i32 @lseek(i32 %0, i32 %7, i32 1)
%9 = call i64 @write(i32 %0, i32* %3, i32 4)
%10 = trunc i64 %9 to i32
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %10)
store i32 0, i32* %3
%12 = tail call i32 @lseek(i32 %0, i32 %7, i32 1)
%13 = call i64 @read(i32 %0, i32* %3, i32 4)
%14 = load i32, i32* %3
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 231, i32 %14)
%16 = trunc i64 %13 to i32
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 2004322440, i32* %3
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 2004322440, i32 232)
%19 = or i32 %1, 232
%20 = tail call i32 @lseek(i32 %0, i32 %19, i32 1)
%21 = call i64 @write(i32 %0, i32* %3, i32 4)
%22 = trunc i64 %21 to i32
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 0, i32* %3
%24 = tail call i32 @lseek(i32 %0, i32 %19, i32 1)
%25 = call i64 @read(i32 %0, i32* %3, i32 4)
%26 = load i32, i32* %3
%27 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 232, i32 %26)
%28 = trunc i64 %25 to i32
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %28)
ret void
}