#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 open(char *, int /*<<< orphan*/);
int pread(int, unsigned long long *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
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)
    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    0xea0(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1190 <get_msr+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <get_msr+0x90>
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%ecx
mov    $0x8,%edx
call   1050 <pread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <close@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     11c9 <get_msr+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <get_msr+0x90>
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    0xea9(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1060 <sprintf@plt>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     11a0 <get_msr+0x60>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
mov    %r14d,%ecx
call   1050 <pread@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    $0x8,%ebx
setne  %al
neg    %eax
jmp    11a5 <get_msr+0x65>
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
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 %32
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 %31
30: ; preds = %20
store i32 -1, i32* %4
br label %32
31: ; preds = %20
store i32 0, i32* %4
br label %32
32: ; preds = %31, %30, %19
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
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 %15, 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 ne i32 %11, 8
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %10, %3
%16 = phi i32 [ -1, %3 ], [ %14, %10 ]
ret i32 %16
}