#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_RDONLY;
scalar_t__ errno;
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_RDONLY);
  if (fd < 0) {
    if (errno == ENXIO) {
      fprintf(stderr, "rdmsr: No CPU %d\n", core);
      return -1;
    } else if (errno == EIO) {
      fprintf(stderr, "rdmsr: CPU %d doesn't support MSRs\n", core);
      return -1;
    } else {
      perror("rdmsr:open");
      fprintf(stderr, "Trying to open %s\n", msr_filename);
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%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    -0x8(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126d <open_msr+0x12d>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <open_msr+0xac>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    127a <open_msr+0x13a>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    1233 <open_msr+0xf3>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    127a <open_msr+0x13a>
lea    0xe0c(%rip),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rdx
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    127a <open_msr+0x13a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
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    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe95(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
jns    11de <open_msr+0x9e>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <open_msr+0x73>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
jmp    11cf <open_msr+0x8f>
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <open_msr+0xa7>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11d9 <open_msr+0x99>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = 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 i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i32, i32* %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %11, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %1
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENXIO
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i32, i32* %3
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %40
26: ; preds = %18
%27 = load i64, i64* @errno
%28 = load i64, i64* @EIO
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* %3
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %40
34: ; preds = %26
%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* %11)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %40
38: ; preds = %1
%39 = load i32, i32* %6
store i32 %39, i32* %2
store i32 1, i32* %7
br label %40
40: ; preds = %38, %34, %30, %22
%41 = load i8*, i8** %4
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = 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_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %26
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 %16
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)
br label %26
16: ; preds = %9
%17 = load i64, i64* @EIO
%18 = icmp eq i64 %10, %17
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %26
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %26
26: ; preds = %1, %22, %19, %13
%27 = phi i32 [ -1, %13 ], [ -1, %19 ], [ -1, %22 ], [ %7, %1 ]
ret i32 %27
}