#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, int warn) {
  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) {
    if (warn)
      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)
mov    %ecx,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11a3 <_get_msr+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <_get_msr+0xb9>
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%ecx
mov    $0x8,%edx
call   1060 <pread@plt>
mov    %eax,-0x20(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <close@plt>
movslq -0x20(%rbp),%rax
cmp    $0x8,%rax
je     1202 <_get_msr+0xb2>
cmpl   $0x0,-0x1c(%rbp)
je     11f6 <_get_msr+0xa6>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe1f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <_get_msr+0xb9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_msr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %edi,%edx
lea    0xe94(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11d0 <_get_msr+0x80>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
mov    %r15d,%ecx
call   1060 <pread@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    $0x8,%ebp
je     11d5 <_get_msr+0x85>
test   %r14d,%r14d
je     11d0 <_get_msr+0x80>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %rsp,%rdx
mov    %r15d,%ecx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
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
@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, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [32 x i8]
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%14 = load i32, i32* %6
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -1, i32* %5
br label %42
22: ; preds = %4
%23 = load i32, i32* %12
%24 = load i64*, i64** %8
%25 = load i32, i32* %7
%26 = call i32 @pread(i32 %23, i64* %24, i32 8, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %12
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %30, 8
br i1 %31, label %32, label %41
32: ; preds = %22
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %38)
br label %40
40: ; preds = %35, %32
store i32 -1, i32* %5
br label %42
41: ; preds = %22
store i32 0, i32* %5
br label %42
42: ; preds = %41, %40, %21
%43 = load i32, i32* %5
ret i32 %43
}

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, i32 %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, 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 %20, label %11
11: ; preds = %4
%12 = tail call i32 @pread(i32 %9, i64* %2, i32 8, i32 %1)
%13 = tail call i32 @close(i32 %9)
%14 = icmp eq i32 %12, 8
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = icmp eq i32 %3, 0
br i1 %16, label %20, label %17
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 %1)
br label %20
20: ; preds = %11, %15, %17, %4
%21 = phi i32 [ -1, %4 ], [ -1, %17 ], [ -1, %15 ], [ 0, %11 ]
ret i32 %21
}