#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 write(int, int /*<<< orphan*/ *, int);
int prfcnt_wrmsr(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 = write(msr_fd, &val, 8);
  if (ret != 8) {
    perror("write");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<prfcnt_wrmsr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek64@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1188 <prfcnt_wrmsr+0x48>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
je     11bc <prfcnt_wrmsr+0x7c>
lea    0xe59(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prfcnt_wrmsr>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %edx,0xc(%rsp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek64@plt>
cmp    $0xffffffff,%eax
je     117c <prfcnt_wrmsr+0x3c>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0x8,%eax
jne    1185 <prfcnt_wrmsr+0x45>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe7d(%rip),%rdi
jmp    118c <prfcnt_wrmsr+0x4c>
lea    0xe7a(%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 = [6 x i8] c"write\00"
define i32 @prfcnt_wrmsr(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 = call i32 @write(i32 %18, i32* %6, i32 8)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 8
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
ret i32 0
}

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