#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;
struct cmd_info {
  int dummy;
};
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ open_and_seek(int, unsigned long, int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strtol(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ strtoull(char const *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/);
int write(int, int /*<<< orphan*/ *, int);
int wr_msr(int argc, const char *argv[], const struct cmd_info *info) {
  unsigned long msr;
  int cpu;
  uint64_t data;
  int fd;
  int ret = 0;
  cpu = strtol(argv[1], NULL, 0);
  msr = strtoul(argv[2], NULL, 0);
  data = strtoull(argv[3], NULL, 0);
  if (open_and_seek(cpu, msr, O_WRONLY, &fd) < 0) {
    return -1;
  }
  if (write(fd, &data, sizeof(data)) != sizeof(data)) {
    fprintf(stderr, "write(): %s\n", strerror(errno));
    ret = -1;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<wr_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
call   1080 <open_and_seek@plt>
cmp    $0x0,%rax
jge    120c <wr_msr+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <wr_msr+0xea>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     125c <wr_msr+0xdc>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdb0(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wr_msr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %rax,%rbp
mov    0x18(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %eax,0x4(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1080 <open_and_seek@plt>
test   %rax,%rax
js     1229 <wr_msr+0xa9>
mov    (%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     121f <wr_msr+0x9f>
mov    0x2de8(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    (%rsp),%edi
call   1040 <close@plt>
jmp    122f <wr_msr+0xaf>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmd_info = type { i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"write(): %s\0A\00"
@errno = global i32 0
define i32 @wr_msr(i32 %0, i8** %1, %struct.cmd_info* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca %struct.cmd_info*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store %struct.cmd_info* %2, %struct.cmd_info** %7
store i32 0, i32* %12
%13 = load i8**, i8*** %6
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strtol(i8* %15, i32* null, i32 0)
store i32 %16, i32* %9
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i64 @strtoul(i8* %19, i32* null, i32 0)
store i64 %20, i64* %8
%21 = load i8**, i8*** %6
%22 = getelementptr inbounds i8*, i8** %21, i64 3
%23 = load i8*, i8** %22
%24 = call i32 @strtoull(i8* %23, i32* null, i32 0)
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = load i64, i64* %8
%27 = load i32, i32* @O_WRONLY
%28 = call i64 @open_and_seek(i32 %25, i64 %26, i32 %27, i32* %11)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %3
store i32 -1, i32* %4
br label %45
31: ; preds = %3
%32 = load i32, i32* %11
%33 = call i32 @write(i32 %32, i32* %10, i32 4)
%34 = sext i32 %33 to i64
%35 = icmp ne i64 %34, 4
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %39)
store i32 -1, i32* %12
br label %41
41: ; preds = %36, %31
%42 = load i32, i32* %11
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %12
store i32 %44, i32* %4
br label %45
45: ; preds = %41, %30
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.cmd_info = type { i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"write(): %s\0A\00"
@errno = global i32 0
define i32 @wr_msr(i32 %0, i8** nocapture %1, %struct.cmd_info* nocapture readnone %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strtol(i8* nocapture %9, i32* null, i32 0)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @strtoul(i8* nocapture %12, i32* null, i32 0)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 @strtoull(i8* nocapture %15, i32* null, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* @O_WRONLY
%18 = call i64 @open_and_seek(i32 %10, i64 %13, i32 %17, i32* %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %3
%21 = load i32, i32* %5
%22 = call i32 @write(i32 %21, i32* %4, i32 4)
%23 = icmp eq i32 %22, 4
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %24, %20
%30 = phi i32 [ -1, %24 ], [ 0, %20 ]
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %3, %29
%34 = phi i32 [ %30, %29 ], [ -1, %3 ]
ret i32 %34
}