#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int lseek(int, unsigned int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int write(int, unsigned long long *, int);
int write_msr(int cpu, unsigned int idx, unsigned long long val) {
  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)
    return -1;
  if (lseek(fd, idx, SEEK_CUR) == -1)
    goto err;
  if (write(fd, &val, sizeof val) != sizeof val)
    goto err;
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<write_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a0 <write_msr+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <write_msr+0xb7>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11c2 <write_msr+0x72>
jmp    11f8 <write_msr+0xa8>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cltq
cmp    $0x8,%rax
je     11e4 <write_msr+0x94>
jmp    11f8 <write_msr+0xa8>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1207 <write_msr+0xb7>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_msr>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %esi,%ebp
mov    %edi,%eax
mov    %rdx,0x8(%rsp)
lea    0xe9a(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c9 <write_msr+0x79>
mov    %eax,%ebx
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11bb <write_msr+0x6b>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    $0x8,%eax
je     11c0 <write_msr+0x70>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11ce <write_msr+0x7e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@SEEK_CUR = global i32 0
define i32 @write_msr(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca [64 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %38
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load i32, i32* %6
%22 = load i32, i32* @SEEK_CUR
%23 = call i32 @lseek(i32 %20, i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %19
br label %35
26: ; preds = %19
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %27, i64* %7, i32 8)
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %29, 8
br i1 %30, label %31, label %32
31: ; preds = %26
br label %35
32: ; preds = %26
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
store i32 0, i32* %4
br label %38
35: ; preds = %31, %25
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %4
br label %38
38: ; preds = %35, %32, %18
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_WRONLY = global i32 0
@SEEK_CUR = global i32 0
define i32 @write_msr(i32 %0, i32 %1, i64 %2) {
%4 = alloca i64
%5 = alloca [64 x i8]
store i64 %2, i64* %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 %22, label %11
11: ; preds = %3
%12 = load i32, i32* @SEEK_CUR
%13 = tail call i32 @lseek(i32 %9, i32 %1, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = call i32 @write(i32 %9, i64* %4, i32 8)
%17 = icmp eq i32 %16, 8
br i1 %17, label %19, label %18
18: ; preds = %15, %11
br label %19
19: ; preds = %15, %18
%20 = phi i32 [ -1, %18 ], [ 0, %15 ]
%21 = tail call i32 @close(i32 %9)
br label %22
22: ; preds = %19, %3
%23 = phi i32 [ -1, %3 ], [ %20, %19 ]
ret i32 %23
}