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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int pwrite(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
void wrmsr(int cpu, uint32_t reg, uint64_t data) {
  char fdchar[30];
  sprintf(fdchar, "/dev/cpu/%d/msr", cpu);
  int fd = open(fdchar, O_WRONLY);
  if (pwrite(fd, &data, sizeof data, reg) != sizeof data) {
    fprintf(stderr, "Failed to write data to %s\n", fdchar);
    close(fd);
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<wrmsr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <pwrite@plt>
cltq
cmp    $0x4,%rax
je     11e8 <wrmsr+0x88>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrmsr>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %edi,%eax
mov    %edx,0xc(%rsp)
lea    0xe8b(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
mov    %ebp,%ecx
call   1050 <pwrite@plt>
cmp    $0x4,%eax
jne    11bc <wrmsr+0x5c>
mov    %ebx,%edi
add    $0x38,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <close@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to write data to %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @wrmsr(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [30 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %7, 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 [30 x i8], [30 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = load i32, i32* %5
%17 = call i32 @pwrite(i32 %15, i32* %6, i32 4, i32 %16)
%18 = sext i32 %17 to i64
%19 = icmp ne i64 %18, 4
br i1 %19, label %20, label %28
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %8
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %3
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to write data to %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @wrmsr(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [30 x i8]
store i32 %2, i32* %4
%6 = getelementptr inbounds [30 x i8], [30 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 = call i32 @pwrite(i32 %9, i32* %4, i32 4, i32 %1)
%11 = icmp eq i32 %10, 4
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%15 = call i32 @close(i32 %9)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %3
%19 = tail call i32 @close(i32 %9)
ret void
}