#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_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pwrite(int, int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
uint64_t set_msr_value(int cpu, uint32_t reg, uint64_t data) {
  int fd;
  char msr_file_name[64];
  sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_file_name, O_WRONLY);
  if (fd < 0) {
    if (errno == ENXIO) {
      fprintf(stderr, "wrmsr: No CPU %d\n", cpu);
      exit(2);
    } else if (errno == EIO) {
      fprintf(stderr, "wrmsr: CPU %d doesn't support MSRs\n", cpu);
      exit(3);
    } else {
      perror("wrmsr:open");
      exit(127);
    }
  }
  if (pwrite(fd, &data, sizeof data, reg) != sizeof data) {
    perror("wrmsr:pwrite");
    exit(127);
  }
  close(fd);
  return (1);
}


x86 assembly (clang, -O0):
<set_msr_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe71(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1241 <set_msr_value+0xd1>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <set_msr_value+0x7f>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    122b <set_msr_value+0xbb>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe06(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <pwrite@plt>
cltq
cmp    $0x4,%rax
je     1277 <set_msr_value+0x107>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_msr_value>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
mov    %edx,0xc(%rsp)
lea    0xe78(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11dc <set_msr_value+0x6c>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
mov    %ebp,%ecx
call   1050 <pwrite@plt>
cmp    $0x4,%eax
jne    1214 <set_msr_value+0xa4>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <set_msr_value+0xad>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xe36(%rip),%rdi
jmp    1252 <set_msr_value+0xe2>
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    124b <set_msr_value+0xdb>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"wrmsr: No CPU %d\0A\00"
@EIO = global i64 0
@.str.2 = [36 x i8] c"wrmsr: CPU %d doesn't support MSRs\0A\00"
@.str.3 = [11 x i8] c"wrmsr:open\00"
@.str.4 = [13 x i8] c"wrmsr:pwrite\00"
define i32 @set_msr_value(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENXIO
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* %4
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %17
%27 = load i64, i64* @errno
%28 = load i64, i64* @EIO
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* %4
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 127)
unreachable
38: ; preds = %3
%39 = load i32, i32* %7
%40 = load i32, i32* %5
%41 = call i32 @pwrite(i32 %39, i32* %6, i32 4, i32 %40)
%42 = sext i32 %41 to i64
%43 = icmp ne i64 %42, 4
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 127)
unreachable
47: ; preds = %38
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
ret i32 1
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"wrmsr: No CPU %d\0A\00"
@EIO = global i64 0
@.str.2 = [36 x i8] c"wrmsr: CPU %d doesn't support MSRs\0A\00"
@.str.3 = [11 x i8] c"wrmsr:open\00"
@.str.4 = [13 x i8] c"wrmsr:pwrite\00"
define i32 @set_msr_value(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [64 x i8]
store i32 %2, i32* %4
%6 = getelementptr inbounds [64 x i8], [64 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_WRONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %3
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENXIO
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %11
%20 = load i64, i64* @EIO
%21 = icmp eq i64 %12, %20
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%25 = tail call i32 @exit(i32 3)
unreachable
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 @exit(i32 127)
unreachable
29: ; preds = %3
%30 = call i32 @pwrite(i32 %9, i32* %4, i32 4, i32 %1)
%31 = icmp eq i32 %30, 4
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i32 @exit(i32 127)
unreachable
35: ; preds = %29
%36 = tail call i32 @close(i32 %9)
ret i32 1
}