#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, unsigned long long *, int, int);
int /*<<< orphan*/ printf(char *, int, int, int);
int pwrite(int, unsigned long long *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
unsigned long long put_msr(int cpu, unsigned long long new_msr, int offset) {
  unsigned long long old_msr;
  char msr_path[32];
  int retval;
  int fd;
  sprintf(msr_path, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_path, O_RDWR);
  if (fd < 0) {
    perror(msr_path);
    exit(1);
  }
  retval = pread(fd, &old_msr, sizeof old_msr, offset);
  if (retval != sizeof old_msr) {
    perror("pwrite");
    printf("pread cpu%d 0x%x = %d\n", cpu, offset, retval);
    exit(-2);
  }
  retval = pwrite(fd, &new_msr, sizeof new_msr, offset);
  if (retval != sizeof new_msr) {
    perror("pwrite");
    printf("pwrite cpu%d 0x%x = %d\n", cpu, offset, retval);
    exit(-2);
  }
  close(fd);
  return old_msr;
}


x86 assembly (clang, -O0):
<put_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    11d7 <put_msr+0x57>
lea    -0x40(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%edi
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <pread@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    $0x8,%rax
je     1227 <put_msr+0xa7>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x44(%rbp),%ecx
lea    0xdff(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%edi
mov    -0x14(%rbp),%ecx
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <pwrite@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    $0x8,%rax
je     1277 <put_msr+0xf7>
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x44(%rbp),%ecx
lea    0xdc6(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_msr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %edi,%r14d
mov    %rsi,0x8(%rsp)
lea    0xe64(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1090 <sprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1209 <put_msr+0x89>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
mov    %r15d,%ecx
call   1080 <pread@plt>
cmp    $0x8,%eax
jne    121d <put_msr+0x9d>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
mov    %r15d,%ecx
call   1050 <pwrite@plt>
cmp    $0x8,%eax
jne    1234 <put_msr+0xb4>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%ebx
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
lea    0xde5(%rip),%rdi
jmp    1249 <put_msr+0xc9>
mov    %eax,%ebx
lea    0xdd3(%rip),%rdi
call   1070 <perror@plt>
lea    0xde5(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
call   1030 <printf@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"pwrite\00"
@.str.2 = [23 x i8] c"pread cpu%d 0x%x = %d\0A\00"
@.str.3 = [24 x i8] c"pwrite cpu%d 0x%x = %d\0A\00"
define i64 @put_msr(i32 %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca [32 x i8]
store i64 %1, i64* %4
%7 = bitcast i64* %5 to i8*
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %8, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @perror(i8* %8)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = call i32 @pread(i32 %11, i64* %5, i32 8, i32 %2)
%18 = icmp eq i32 %17, 8
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %2, i32 %17)
%22 = tail call i32 @exit(i32 -2)
unreachable
23: ; preds = %16
%24 = call i32 @pwrite(i32 %11, i64* %4, i32 8, i32 %2)
%25 = icmp eq i32 %24, 8
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %2, i32 %24)
%29 = tail call i32 @exit(i32 -2)
unreachable
30: ; preds = %23
%31 = tail call i32 @close(i32 %11)
%32 = load i64, i64* %5
ret i64 %32
}