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


x86 assembly (clang, -O0):
<open_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%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    -0x4(%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    1248 <open_msr+0x108>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <open_msr+0x9e>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1243 <open_msr+0x103>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <open_msr+0xd7>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    123e <open_msr+0xfe>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rdx
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1243 <open_msr+0x103>
jmp    1248 <open_msr+0x108>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe92(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11df <open_msr+0x9f>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <open_msr+0x79>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
jmp    11d5 <open_msr+0x95>
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <open_msr+0xad>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r15d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11df <open_msr+0x9f>

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 = [21 x i8] c"open_msr: No CPU %d\0A\00"
@EIO = global i64 0
@.str.2 = [39 x i8] c"open_msr: CPU %d doesn't support MSRs\0A\00"
@.str.3 = [14 x i8] c"open_msr: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_RDONLY
%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 %38
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 %24
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i32, i32* %2
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %37
24: ; preds = %16
%25 = load i64, i64* @errno
%26 = load i64, i64* @EIO
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i32, i32* %2
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %36
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %9)
br label %36
36: ; preds = %32, %28
br label %37
37: ; preds = %36, %20
br label %38
38: ; preds = %37, %1
%39 = load i32, i32* %5
%40 = load i8*, i8** %3
ret i32 %39
}

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 = [21 x i8] c"open_msr: No CPU %d\0A\00"
@EIO = global i64 0
@.str.2 = [39 x i8] c"open_msr: CPU %d doesn't support MSRs\0A\00"
@.str.3 = [14 x i8] c"open_msr: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 ([21 x i8], [21 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 ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %26
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 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 = %13, %22, %19, %1
ret i32 %7
}