#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 BUFSIZ;
scalar_t__ EIO;
scalar_t__ ENXIO;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int open_msr(int core) {
  char msr_filename[BUFSIZ];
  int fd;
  sprintf(msr_filename, "/dev/cpu/%d/msr", core);
  fd = open(msr_filename, O_RDWR);
  if (fd < 0) {
    if (errno == ENXIO) {
      fprintf(stderr, "rdmsr: No CPU %d\n", core);
      exit(2);
    } else if (errno == EIO) {
      fprintf(stderr, "rdmsr: CPU %d doesn't support MSRs\n", core);
      exit(3);
    } else {
      perror("rdmsr:open");
      fprintf(stderr, "Trying to open %s\n", msr_filename);
      exit(127);
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edx
lea    0xe6f(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1262 <open_msr+0x112>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11f3 <open_msr+0xa3>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <open_msr+0xe1>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rdx
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_msr>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     11a4 <open_msr+0x54>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <open_msr+0x8e>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <open_msr+0xbe>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"rdmsr: No CPU %d\0A\00"
@EIO = global i64 0
@.str.2 = [36 x i8] c"rdmsr: CPU %d doesn't support MSRs\0A\00"
@.str.3 = [11 x i8] c"rdmsr:open\00"
@.str.4 = [19 x i8] c"Trying to open %s\0A\00"
define i32 @open_msr(i32 %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENXIO
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %9
%18 = load i64, i64* @EIO
%19 = icmp eq i64 %10, %18
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%23 = tail call i32 @exit(i32 3)
unreachable
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%28 = call i32 @exit(i32 127)
unreachable
29: ; preds = %1
ret i32 %7
}