#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 */
typedef int /*<<< orphan*/ msr;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pread(int, long long *, int, int);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strtol(char *, char **, int);
int main(int argc, char **argv) {
  int cpu, fd;
  long long msr;
  char msr_file_name[64];
  if (argc != 2) {
    return 1;
  }
  errno = 0;
  cpu = strtol(argv[1], (char **)NULL, 10);
  if (errno) {
    return 1;
  }
  sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_file_name, O_RDONLY);
  if (fd == -1) {
    perror("Failed to open");
    return 1;
  }
  pread(fd, &msr, sizeof(msr), 0x199);
  printf("msr 0x199: 0x%llx\n", msr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     118c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0xe7>
mov    0x2e45(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e1e(%rip),%rax
cmpq   $0x0,(%rax)
je     11d0 <main+0x70>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0xe7>
lea    -0x60(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe22(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121a <main+0xba>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0xe7>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
mov    $0x199,%ecx
call   1070 <pread@plt>
mov    -0x20(%rbp),%rsi
lea    0xde4(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    $0x1,%ebx
cmp    $0x2,%edi
jne    11ea <main+0x8a>
mov    0x2e60(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
cmpq   $0x0,(%r14)
jne    11ea <main+0x8a>
lea    0xe64(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11f4 <main+0x94>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
mov    $0x199,%ecx
call   1070 <pread@plt>
mov    0x8(%rsp),%rsi
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
jmp    11ea <main+0x8a>

LLVM IR (-O0):
@errno = global i64 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to open\00"
@.str.2 = [19 x i8] c"msr 0x199: 0x%llx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 1, i32* %3
br label %37
13: ; preds = %2
store i64 0, i64* @errno
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strtol(i8* %16, i8** null, i32 10)
store i32 %17, i32* %6
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 1, i32* %3
br label %37
21: ; preds = %13
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%23 = load i32, i32* %6
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %37
32: ; preds = %21
%33 = load i32, i32* %7
%34 = call i32 @pread(i32 %33, i64* %8, i32 8, i32 409)
%35 = load i64, i64* %8
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %32, %30, %20, %12
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to open\00"
@.str.2 = [19 x i8] c"msr 0x199: 0x%llx\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca [64 x i8]
%5 = bitcast i64* %3 to i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %25
8: ; preds = %2
store i64 0, i64* @errno
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strtol(i8* nocapture %10, i8** null, i32 10)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %8
%15 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %6, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %25
21: ; preds = %14
%22 = call i32 @pread(i32 %17, i64* %3, i32 8, i32 409)
%23 = load i64, i64* %3
%24 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %23)
br label %25
25: ; preds = %8, %2, %21, %19
%26 = phi i32 [ 1, %19 ], [ 0, %21 ], [ 1, %2 ], [ 1, %8 ]
ret i32 %26
}