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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int pread(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
uint64_t rdmsr(int cpu, uint32_t reg) {
  uint64_t data;
  char fdchar[30];
  sprintf(fdchar, "/dev/cpu/%d/msr", cpu);
  int fd = open(fdchar, O_RDONLY);
  if (pread(fd, &data, sizeof data, reg) != sizeof data) {
    fprintf(stderr, "Failed to read data from %s\n", fdchar);
    close(fd);
    exit(EXIT_FAILURE);
  }
  close(fd);
  return data;
}


x86 assembly (clang, -O0):
<rdmsr>:
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    0xe84(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <pread@plt>
cltq
cmp    $0x4,%rax
je     11e5 <rdmsr+0x85>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdmsr>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
mov    %ebp,%ecx
call   1060 <pread@plt>
cmp    $0x4,%eax
jne    11bb <rdmsr+0x5b>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0xc(%rsp),%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"Failed to read data from %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @rdmsr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = call i32 @pread(i32 %14, i32* %5, i32 4, i32 %15)
%17 = sext i32 %16 to i64
%18 = icmp ne i64 %17, 4
br i1 %18, label %19, label %27
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %2
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"Failed to read data from %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @rdmsr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [30 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [30 x i8], [30 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 = call i32 @pread(i32 %9, i32* %3, i32 4, i32 %1)
%11 = icmp eq i32 %10, 4
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%15 = call i32 @close(i32 %9)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = tail call i32 @close(i32 %9)
%20 = load i32, i32* %3
ret i32 %20
}