#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__ EACCES;
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*/ snprintf(char *, int, char *, char);
int /*<<< orphan*/ stderr;
int rapl_open_msr(char core) {
  int fd_msr;
  char filename[BUFSIZ];
  snprintf(filename, BUFSIZ, "/dev/cpu/%d/msr", core);
  fd_msr = open(filename, O_RDONLY);
  if (fd_msr < 0) {
    if (errno == ENXIO) {
      fprintf(stderr, "no cpu core %d\n", core);
    } else if (errno == EIO) {
      fprintf(stderr, "core %d doesn't support MSRs\n", core);
    } else if (errno == EACCES) {
      fprintf(stderr, "Permission denied. On most systems you'll need root "
                      "privileges for this application.\n");
    }
    perror("open msr failed");
  }
  return fd_msr;
}


x86 assembly (clang, -O0):
<rapl_open_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    0x2e6b(%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,-0x20(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdx
movsbl -0x1(%rbp),%ecx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    126d <rapl_open_msr+0x12d>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <rapl_open_msr+0xac>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
movsbl -0x1(%rbp),%edx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1261 <rapl_open_msr+0x121>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <rapl_open_msr+0xe6>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
movsbl -0x1(%rbp),%edx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    125c <rapl_open_msr+0x11c>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
jne    1257 <rapl_open_msr+0x117>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    125c <rapl_open_msr+0x11c>
jmp    1261 <rapl_open_msr+0x121>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_open_msr>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe92(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     119b <rapl_open_msr+0x5b>
mov    %ebx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11c7 <rapl_open_msr+0x87>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
movsbl %r14b,%edx
lea    0xe4b(%rip),%rsi
jmp    11e7 <rapl_open_msr+0xa7>
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11fc <rapl_open_msr+0xbc>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
movsbl %r14b,%edx
lea    0xe39(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe9f(%rip),%rdi
call   1060 <perror@plt>
jmp    1190 <rapl_open_msr+0x50>
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <rapl_open_msr+0xae>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11ee <rapl_open_msr+0xae>

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 = [16 x i8] c"no cpu core %d\0A\00"
@EIO = global i64 0
@.str.2 = [30 x i8] c"core %d doesn't support MSRs\0A\00"
@EACCES = global i64 0
@.str.3 = [86 x i8] c"Permission denied. On most systems you'll need root privileges for this application.\0A\00"
@.str.4 = [16 x i8] c"open msr failed\00"
define i32 @rapl_open_msr(i8 signext %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i8 %0, i8* %2
%6 = load i32, i32* @BUFSIZ
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %4
%9 = alloca i8, i64 %7
store i64 %7, i64* %5
%10 = load i32, i32* @BUFSIZ
%11 = load i8, i8* %2
%12 = call i32 @snprintf(i8* %9, i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8 signext %11)
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %9, i32 %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %46
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENXIO
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i8, i8* %2
%24 = sext i8 %23 to i32
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %44
26: ; preds = %17
%27 = load i64, i64* @errno
%28 = load i64, i64* @EIO
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i8, i8* %2
%33 = sext i8 %32 to i32
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %43
35: ; preds = %26
%36 = load i64, i64* @errno
%37 = load i64, i64* @EACCES
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %39, %35
br label %43
43: ; preds = %42, %30
br label %44
44: ; preds = %43, %21
%45 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %1
%47 = load i32, i32* %3
%48 = load i8*, i8** %4
ret i32 %47
}

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 = [16 x i8] c"no cpu core %d\0A\00"
@EIO = global i64 0
@.str.2 = [30 x i8] c"core %d doesn't support MSRs\0A\00"
@EACCES = global i64 0
@.str.3 = [86 x i8] c"Permission denied. On most systems you'll need root privileges for this application.\0A\00"
@.str.4 = [16 x i8] c"open msr failed\00"
define i32 @rapl_open_msr(i8 signext %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8 signext %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 %32
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 = sext i8 %0 to i32
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %30
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 = sext i8 %0 to i32
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %30
24: ; preds = %17
%25 = load i64, i64* @EACCES
%26 = icmp eq i64 %10, %25
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %20, %27, %24, %13
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %1
ret i32 %7
}