#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int lseek(int, unsigned int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned long long *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int read_msr(int cpu, unsigned int idx, unsigned long long *val) {
  int fd;
  char msr_file_name[64];
  sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_file_name, O_RDONLY);
  if (fd < 0)
    return -1;
  if (lseek(fd, idx, SEEK_CUR) == -1)
    goto err;
  if (read(fd, val, sizeof *val) != sizeof *val)
    goto err;
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<read_msr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a0 <read_msr+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <read_msr+0xb7>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11c2 <read_msr+0x72>
jmp    11f8 <read_msr+0xa8>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cltq
cmp    $0x8,%rax
je     11e4 <read_msr+0x94>
jmp    11f8 <read_msr+0xa8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1207 <read_msr+0xb7>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%edx
lea    0xe9a(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <sprintf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c3 <read_msr+0x73>
mov    %eax,%ebp
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     11b5 <read_msr+0x65>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <read@plt>
xor    %ebx,%ebx
cmp    $0x8,%eax
je     11ba <read_msr+0x6a>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11c8 <read_msr+0x78>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
define i32 @read_msr(i32 %0, i32 %1, i64* nocapture %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %3
%11 = load i32, i32* @SEEK_CUR
%12 = tail call i32 @lseek(i32 %8, i32 %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = tail call i32 @read(i32 %8, i64* %2, i32 8)
%16 = icmp eq i32 %15, 8
br i1 %16, label %18, label %17
17: ; preds = %14, %10
br label %18
18: ; preds = %14, %17
%19 = phi i32 [ -1, %17 ], [ 0, %14 ]
%20 = tail call i32 @close(i32 %8)
br label %21
21: ; preds = %18, %3
%22 = phi i32 [ -1, %3 ], [ %19, %18 ]
ret i32 %22
}