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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *,
                           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int pread(int, unsigned long long *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int get_msr(int cpu, off_t offset, unsigned long long *msr) {
  ssize_t retval;
  char pathname[32];
  int fd;
  sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  fd = open(pathname, O_RDONLY);
  if (fd < 0)
    return -1;
  retval = pread(fd, msr, sizeof *msr, offset);
  close(fd);
  if (retval != sizeof *msr) {
    fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11a0 <get_msr+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <get_msr+0xac>
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%ecx
mov    $0x8,%edx
call   1060 <pread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <close@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     11f5 <get_msr+0xa5>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe2c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <get_msr+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%edx
lea    0xe99(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <sprintf@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
test   %ebp,%ebp
js     11d0 <get_msr+0x80>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
mov    %r14d,%ecx
call   1060 <pread@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    $0x8,%ebx
je     11d0 <get_msr+0x80>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %rsp,%rdx
mov    %r14d,%ecx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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"%s offset 0x%zx read failed\0A\00"
define i32 @get_msr(i32 %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i32
%9 = alloca [32 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64* %2, i64** %7
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* %5
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %36
20: ; preds = %3
%21 = load i32, i32* %10
%22 = load i64*, i64** %7
%23 = load i32, i32* %6
%24 = call i32 @pread(i32 %21, i64* %22, i32 8, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %10
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = icmp ne i64 %28, 8
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%33 = load i32, i32* %6
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
store i32 -1, i32* %4
br label %36
35: ; preds = %20
store i32 0, i32* %4
br label %36
36: ; preds = %35, %30, %19
%37 = load i32, i32* %4
ret i32 %37
}

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"%s offset 0x%zx read failed\0A\00"
define i32 @get_msr(i32 %0, i32 %1, i64* nocapture %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %3
%11 = tail call i32 @pread(i32 %8, i64* %2, i32 8, i32 %1)
%12 = tail call i32 @close(i32 %8)
%13 = icmp eq i32 %11, 8
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %1)
br label %17
17: ; preds = %10, %3, %14
%18 = phi i32 [ -1, %14 ], [ -1, %3 ], [ 0, %10 ]
ret i32 %18
}