#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, unsigned long long *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
unsigned long long get_msr(int cpu, int offset) {
  unsigned long long msr;
  char msr_path[32];
  int retval;
  int fd;
  sprintf(msr_path, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_path, O_RDONLY);
  if (fd < 0) {
    printf("Try \"# modprobe msr\"\n");
    perror(msr_path);
    exit(1);
  }
  retval = pread(fd, &msr, sizeof msr, offset);
  if (retval != sizeof msr) {
    printf("pread cpu%d 0x%x = %d\n", cpu, offset, retval);
    exit(-2);
  }
  close(fd);
  return msr;
}


x86 assembly (clang, -O0):
<get_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe74(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11d1 <get_msr+0x61>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <pread@plt>
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    $0x8,%rax
je     1217 <get_msr+0xa7>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xe6c(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e8 <get_msr+0x68>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
mov    %r14d,%ecx
call   1080 <pread@plt>
cmp    $0x8,%eax
jne    1208 <get_msr+0x88>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe01(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Try \22# modprobe msr\22\0A\00"
@.str.2 = [23 x i8] c"pread cpu%d 0x%x = %d\0A\00"
define i64 @get_msr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%20 = call i32 @perror(i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i32, i32* %8
%24 = load i32, i32* %4
%25 = call i32 @pread(i32 %23, i64* %5, i32 8, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 8
br i1 %28, label %29, label %35
29: ; preds = %22
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = load i32, i32* %7
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31, i32 %32)
%34 = call i32 @exit(i32 -2)
unreachable
35: ; preds = %22
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = load i64, i64* %5
ret i64 %38
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.2 = [23 x i8] c"pread cpu%d 0x%x = %d\0A\00"
@str = [21 x i8] c"Try \22# modprobe msr\22\00"
define i64 @get_msr(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca [32 x i8]
%5 = bitcast i64* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%13 = call i32 @perror(i8* %6)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @pread(i32 %9, i64* %3, i32 8, i32 %1)
%17 = icmp eq i32 %16, 8
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %1, i32 %16)
%20 = tail call i32 @exit(i32 -2)
unreachable
21: ; preds = %15
%22 = tail call i32 @close(i32 %9)
%23 = load i64, i64* %3
ret i64 %23
}