#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 scalar_t__ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ perror(char *);
int pread(int, scalar_t__ *, int, unsigned int);
int /*<<< orphan*/ stderr;
long long read_msr(int fd, unsigned int which) {
  uint64_t data;
  if (pread(fd, &data, sizeof data, which) != sizeof data) {
    perror("rdmsr:pread");
    fprintf(stderr, "Error reading MSR %x\n", which);
    exit(127);
  }
  return (long long)data;
}


x86 assembly (clang, -O0):
<read_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <pread@plt>
cltq
cmp    $0x8,%rax
je     119c <read_msr+0x5c>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x7f,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msr>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
mov    %ebx,%ecx
call   1050 <pread@plt>
cmp    $0x8,%eax
jne    1168 <read_msr+0x28>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe91(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
mov    %ebx,%edx
call   1030 <fprintf@plt>
mov    $0x7f,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"rdmsr:pread\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Error reading MSR %x\0A\00"
define i64 @read_msr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* %4
%8 = call i32 @pread(i32 %6, i64* %5, i32 8, i32 %7)
%9 = sext i32 %8 to i64
%10 = icmp ne i64 %9, 8
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = load i32, i32* %4
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 127)
unreachable
17: ; preds = %2
%18 = load i64, i64* %5
ret i64 %18
}

LLVM IR (-O2):
@.str = [12 x i8] c"rdmsr:pread\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Error reading MSR %x\0A\00"
define i64 @read_msr(i32 %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = call i32 @pread(i32 %0, i64* %3, i32 8, i32 %1)
%6 = icmp eq i32 %5, 8
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%11 = tail call i32 @exit(i32 127)
unreachable
12: ; preds = %2
%13 = load i64, i64* %3
ret i64 %13
}