#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 int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int lseek(int, unsigned int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned long long *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int rdmsr(unsigned int cpu, unsigned int msr, unsigned int *lo,
          unsigned int *hi) {
  int fd;
  char file[20];
  unsigned long long val;
  int retval = -1;
  *lo = *hi = 0;
  if (cpu > 32) {
    goto err1;
  }
  sprintf(file, "/dev/cpu/%d/msr", cpu);
  fd = open(file, O_RDONLY);
  if (fd < 0) {
    goto err1;
  }
  if (lseek(fd, msr, SEEK_CUR) == -1) {
    goto err2;
  }
  if (read(fd, &val, 8) != 8) {
    goto err2;
  }
  *lo = (uint32_t)(val & 0xffffffffull);
  *hi = (uint32_t)(val >> 32 & 0xffffffffull);
  retval = 0;
err2:
  close(fd);
err1:
  return retval;
}


x86 assembly (clang, -O0):
<rdmsr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x20,-0x4(%rbp)
jbe    1190 <rdmsr+0x40>
jmp    124a <rdmsr+0xfa>
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe62(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c7 <rdmsr+0x77>
jmp    124a <rdmsr+0xfa>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e9 <rdmsr+0x99>
jmp    1242 <rdmsr+0xf2>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
je     1208 <rdmsr+0xb8>
jmp    1242 <rdmsr+0xf2>
movabs $0xffffffff,%rax
and    -0x38(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
shr    $0x20,%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdmsr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%rbx
movl   $0x0,(%rcx)
movl   $0x0,(%rdx)
mov    $0xffffffff,%ebp
cmp    $0x20,%edi
ja     11fa <rdmsr+0xaa>
mov    %rcx,%r14
mov    %esi,%r12d
mov    %edi,%edx
lea    0xe78(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1070 <sprintf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11fa <rdmsr+0xaa>
mov    %eax,%r15d
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12d,%esi
call   1030 <lseek@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11f2 <rdmsr+0xa2>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
jne    11f2 <rdmsr+0xa2>
mov    0x8(%rsp),%rax
mov    %eax,(%rbx)
shr    $0x20,%rax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
define i32 @rdmsr(i32 %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [20 x i8]
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 -1, i32* %12
%13 = load i32*, i32** %8
store i32 0, i32* %13
%14 = load i32*, i32** %7
store i32 0, i32* %14
%15 = load i32, i32* %5
%16 = icmp ugt i32 %15, 32
br i1 %16, label %17, label %18
17: ; preds = %4
br label %53
18: ; preds = %4
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%20 = load i32, i32* %5
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %18
br label %53
28: ; preds = %18
%29 = load i32, i32* %9
%30 = load i32, i32* %6
%31 = load i32, i32* @SEEK_CUR
%32 = call i32 @lseek(i32 %29, i32 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %28
br label %50
35: ; preds = %28
%36 = load i32, i32* %9
%37 = call i32 @read(i32 %36, i64* %11, i32 8)
%38 = icmp ne i32 %37, 8
br i1 %38, label %39, label %40
39: ; preds = %35
br label %50
40: ; preds = %35
%41 = load i64, i64* %11
%42 = and i64 %41, 4294967295
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %7
store i32 %43, i32* %44
%45 = load i64, i64* %11
%46 = lshr i64 %45, 32
%47 = and i64 %46, 4294967295
%48 = trunc i64 %47 to i32
%49 = load i32*, i32** %8
store i32 %48, i32* %49
store i32 0, i32* %12
br label %50
50: ; preds = %40, %39, %34
%51 = load i32, i32* %9
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %27, %17
%54 = load i32, i32* %12
ret i32 %54
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
define i32 @rdmsr(i32 %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca [20 x i8]
%6 = alloca i64
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%8 = bitcast i64* %6 to i8*
store i32 0, i32* %3
store i32 0, i32* %2
%9 = icmp ugt i32 %0, 32
br i1 %9, label %30, label %10
10: ; preds = %4
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %7, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %10
%16 = load i32, i32* @SEEK_CUR
%17 = tail call i32 @lseek(i32 %13, i32 %1, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = call i32 @read(i32 %13, i64* %6, i32 8)
%21 = icmp eq i32 %20, 8
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i64, i64* %6
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
%25 = lshr i64 %23, 32
%26 = trunc i64 %25 to i32
store i32 %26, i32* %3
br label %27
27: ; preds = %19, %15, %22
%28 = phi i32 [ -1, %15 ], [ -1, %19 ], [ 0, %22 ]
%29 = tail call i32 @close(i32 %13)
br label %30
30: ; preds = %10, %4, %27
%31 = phi i32 [ -1, %4 ], [ -1, %10 ], [ %28, %27 ]
ret i32 %31
}