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

/* Variables and functions */
scalar_t__ EIO;
scalar_t__ ENXIO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
uint64_t get_msr_value(int cpu, uint32_t reg, unsigned int highbit,
                       unsigned int lowbit, int *error_indx) {
  uint64_t data;
  int fd;
  char msr_file_name[64];
  int bits;
  *error_indx = 0;
  sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_file_name, O_RDONLY);
  if (fd < 0) {
    if (errno == ENXIO) {
      *error_indx = 1;
      return 1;
    } else if (errno == EIO) {
      *error_indx = 1;
      return 1;
    } else {
      *error_indx = 1;
      return 1;
    }
  }
  if (pread(fd, &data, sizeof data, reg) != sizeof data) {
    perror("rdmsr:pread");
    exit(127);
  }
  close(fd);
  bits = highbit - lowbit + 1;
  if (bits < 64) {
    data >>= lowbit;
    data &= (1ULL << bits) - 1;
  }
  if (data & (1ULL << (bits - 1))) {
    data &= ~(1ULL << (bits - 1));
    data = -data;
  }
  *error_indx = 0;
  return (data);
}


x86 assembly (clang, -O0):
<get_msr_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    122d <get_msr_value+0xcd>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <get_msr_value+0x87>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1309 <get_msr_value+0x1a9>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <get_msr_value+0xb7>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1309 <get_msr_value+0x1a9>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1309 <get_msr_value+0x1a9>
mov    -0x28(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <pread@plt>
cltq
cmp    $0x4,%rax
je     1263 <get_msr_value+0x103>
lea    0xdbc(%rip),%rdi
call   1050 <perror@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
cmpl   $0x40,-0x74(%rbp)
jge    12aa <get_msr_value+0x14a>
mov    -0x14(%rbp),%ecx
mov    -0x24(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x24(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,%rcx
sub    $0x1,%rcx
movslq -0x24(%rbp),%rax
and    %rcx,%rax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x74(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     12f9 <get_msr_value+0x199>
mov    -0x74(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,%rcx
xor    $0xffffffffffffffff,%rcx
movslq -0x24(%rbp),%rax
and    %rcx,%rax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msr_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %r8,%r14
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %esi,%r12d
mov    %edi,%edx
movl   $0x0,(%r8)
lea    0xe79(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <sprintf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     1210 <get_msr_value+0xb0>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
mov    %r12d,%ecx
call   1060 <pread@plt>
cmp    $0x4,%eax
jne    122a <get_msr_value+0xca>
mov    %ebp,%edi
call   1030 <close@plt>
sub    %r15d,%ebx
lea    0x1(%rbx),%eax
mov    0xc(%rsp),%edx
mov    %edx,%esi
mov    %r15d,%ecx
sar    %cl,%esi
mov    $0xffffffffffffffff,%rdi
mov    %eax,%ecx
shl    %cl,%rdi
not    %edi
and    %esi,%edi
cmp    $0x40,%eax
cmovge %edx,%edi
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%rax
movslq %edi,%rcx
not    %eax
and    %ecx,%eax
neg    %eax
bt     %rbx,%rcx
cmovae %ecx,%eax
xor    %ecx,%ecx
jmp    121a <get_msr_value+0xba>
mov    $0x1,%ecx
mov    $0x1,%eax
mov    %ecx,(%r14)
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddf(%rip),%rdi
call   1050 <perror@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@EIO = global i64 0
@.str.1 = [12 x i8] c"rdmsr:pread\00"
define i32 @get_msr_value(i32 %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [64 x i8]
%15 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%16 = load i32*, i32** %11
store i32 0, i32* %16
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%18 = load i32, i32* %7
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %5
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENXIO
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32*, i32** %11
store i32 1, i32* %30
store i32 1, i32* %6
br label %93
31: ; preds = %25
%32 = load i64, i64* @errno
%33 = load i64, i64* @EIO
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32*, i32** %11
store i32 1, i32* %36
store i32 1, i32* %6
br label %93
37: ; preds = %31
%38 = load i32*, i32** %11
store i32 1, i32* %38
store i32 1, i32* %6
br label %93
39: ; preds = %5
%40 = load i32, i32* %13
%41 = load i32, i32* %8
%42 = call i32 @pread(i32 %40, i32* %12, i32 4, i32 %41)
%43 = sext i32 %42 to i64
%44 = icmp ne i64 %43, 4
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 127)
unreachable
48: ; preds = %39
%49 = load i32, i32* %13
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %9
%52 = load i32, i32* %10
%53 = sub i32 %51, %52
%54 = add i32 %53, 1
store i32 %54, i32* %15
%55 = load i32, i32* %15
%56 = icmp slt i32 %55, 64
br i1 %56, label %57, label %69
57: ; preds = %48
%58 = load i32, i32* %10
%59 = load i32, i32* %12
%60 = ashr i32 %59, %58
store i32 %60, i32* %12
%61 = load i32, i32* %15
%62 = zext i32 %61 to i64
%63 = shl i64 1, %62
%64 = sub i64 %63, 1
%65 = load i32, i32* %12
%66 = sext i32 %65 to i64
%67 = and i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %12
br label %69
69: ; preds = %57, %48
%70 = load i32, i32* %12
%71 = sext i32 %70 to i64
%72 = load i32, i32* %15
%73 = sub nsw i32 %72, 1
%74 = zext i32 %73 to i64
%75 = shl i64 1, %74
%76 = and i64 %71, %75
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %90
78: ; preds = %69
%79 = load i32, i32* %15
%80 = sub nsw i32 %79, 1
%81 = zext i32 %80 to i64
%82 = shl i64 1, %81
%83 = xor i64 %82, -1
%84 = load i32, i32* %12
%85 = sext i32 %84 to i64
%86 = and i64 %85, %83
%87 = trunc i64 %86 to i32
store i32 %87, i32* %12
%88 = load i32, i32* %12
%89 = sub nsw i32 0, %88
store i32 %89, i32* %12
br label %90
90: ; preds = %78, %69
%91 = load i32*, i32** %11
store i32 0, i32* %91
%92 = load i32, i32* %12
store i32 %92, i32* %6
br label %93
93: ; preds = %90, %37, %35, %29
%94 = load i32, i32* %6
ret i32 %94
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@EIO = global i64 0
@.str.1 = [12 x i8] c"rdmsr:pread\00"
define i32 @get_msr_value(i32 %0, i32 %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = bitcast i32* %6 to i8*
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
store i32 0, i32* %4
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %43, label %14
14: ; preds = %5
%15 = call i32 @pread(i32 %12, i32* %6, i32 4, i32 %1)
%16 = icmp eq i32 %15, 4
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 127)
unreachable
20: ; preds = %14
%21 = tail call i32 @close(i32 %12)
%22 = sub i32 %2, %3
%23 = add i32 %22, 1
%24 = icmp slt i32 %23, 64
%25 = load i32, i32* %6
%26 = ashr i32 %25, %3
%27 = zext i32 %23 to i64
%28 = shl nsw i64 -1, %27
%29 = trunc i64 %28 to i32
%30 = xor i32 %29, -1
%31 = and i32 %26, %30
%32 = select i1 %24, i32 %31, i32 %25
%33 = sext i32 %32 to i64
%34 = zext i32 %22 to i64
%35 = shl nuw i64 1, %34
%36 = and i64 %35, %33
%37 = icmp eq i64 %36, 0
%38 = trunc i64 %35 to i32
%39 = xor i32 %38, -1
%40 = and i32 %32, %39
%41 = sub nsw i32 0, %40
%42 = select i1 %37, i32 %32, i32 %41
br label %43
43: ; preds = %5, %20
%44 = phi i32 [ 0, %20 ], [ 1, %5 ]
%45 = phi i32 [ %42, %20 ], [ 1, %5 ]
store i32 %44, i32* %4
ret i32 %45
}