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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int lseek64(int, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, int /*<<< orphan*/ *, int);
int prfcnt_rdmsr(int msr_fd, unsigned int addr, __u64 *val) {
  off_t ret;
  ret = lseek64(msr_fd, addr, SEEK_SET);
  if (ret == (off_t)-1) {
    perror("lseek");
    exit(1);
  }
  ret = read(msr_fd, val, 8);
  if (ret != 8) {
    perror("read");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<prfcnt_rdmsr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek64@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1189 <prfcnt_rdmsr+0x49>
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
je     11bd <prfcnt_rdmsr+0x7d>
lea    0xe58(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prfcnt_rdmsr>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %edi,%ebp
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek64@plt>
cmp    $0xffffffff,%eax
je     1178 <prfcnt_rdmsr+0x38>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <read@plt>
cmp    $0x8,%eax
jne    1181 <prfcnt_rdmsr+0x41>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe81(%rip),%rdi
jmp    1188 <prfcnt_rdmsr+0x48>
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @prfcnt_rdmsr(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @lseek64(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32, i32* %4
%19 = load i32*, i32** %6
%20 = call i32 @read(i32 %18, i32* %19, i32 8)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 8
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
ret i32 0
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @prfcnt_rdmsr(i32 %0, i32 %1, i32* nocapture %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek64(i32 %0, i32 %1, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i32 @read(i32 %0, i32* %2, i32 8)
%12 = icmp eq i32 %11, 8
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
ret i32 0
}