#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 uint64_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, int *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid) {
  int err = 1;
  uint64_t msr = 0;
  int fd;
  char file[20];
  if (cpu > 32) {
    goto out;
  }
  sprintf(file, "/dev/cpu/%d/msr", cpu);
  fd = open(file, O_RDONLY);
  if (fd < 0) {
    goto out;
  }
  lseek(fd, 0xc0010042, SEEK_CUR);
  if (read(fd, &msr, 8) != 8) {
    goto err1;
  }
  *fid = ((uint32_t)(msr & 0xffffffffull)) & 0x0000003f;
  *vid = ((uint32_t)(msr >> 32 & 0xffffffffull)) & 0x0000001f;
  err = 0;
err1:
  close(fd);
out:
  return err;
}


x86 assembly (clang, -O0):
<get_fidvid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x20,-0x4(%rbp)
jle    1180 <get_fidvid+0x30>
jmp    123b <get_fidvid+0xeb>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe72(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b7 <get_fidvid+0x67>
jmp    123b <get_fidvid+0xeb>
mov    -0x24(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    $0xc0010042,%esi
call   1030 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
je     11ec <get_fidvid+0x9c>
jmp    1233 <get_fidvid+0xe3>
movslq -0x20(%rbp),%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,%ecx
and    $0x3f,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
cltq
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,%ecx
and    $0x1f,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fidvid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0xc(%rsp)
mov    $0x1,%ebp
cmp    $0x20,%edi
jg     11dd <get_fidvid+0x8d>
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe8a(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11dd <get_fidvid+0x8d>
mov    %eax,%ebx
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0xc0010042,%esi
call   1030 <lseek@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <read@plt>
mov    $0x1,%ebp
cmp    $0x8,%eax
jne    11d6 <get_fidvid+0x86>
mov    0xc(%rsp),%eax
and    $0x3f,%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x30,%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 @get_fidvid(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [20 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32 1, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 32
br i1 %12, label %13, label %14
13: ; preds = %3
br label %49
14: ; preds = %3
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
br label %49
24: ; preds = %14
%25 = load i32, i32* %9
%26 = load i32, i32* @SEEK_CUR
%27 = call i32 @lseek(i32 %25, i32 -1073676222, i32 %26)
%28 = load i32, i32* %9
%29 = call i32 @read(i32 %28, i32* %8, i32 8)
%30 = icmp ne i32 %29, 8
br i1 %30, label %31, label %32
31: ; preds = %24
br label %46
32: ; preds = %24
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = and i64 %34, 4294967295
%36 = trunc i64 %35 to i32
%37 = and i32 %36, 63
%38 = load i32*, i32** %5
store i32 %37, i32* %38
%39 = load i32, i32* %8
%40 = ashr i32 %39, 32
%41 = sext i32 %40 to i64
%42 = and i64 %41, 4294967295
%43 = trunc i64 %42 to i32
%44 = and i32 %43, 31
%45 = load i32*, i32** %6
store i32 %44, i32* %45
store i32 0, i32* %7
br label %46
46: ; preds = %32, %31
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %23, %13
%50 = load i32, i32* %7
ret i32 %50
}

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 @get_fidvid(i32 %0, i32* nocapture writeonly %1, i32* nocapture readnone %2) {
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%8 = icmp sgt i32 %0, 32
br i1 %8, label %25, label %9
9: ; preds = %3
%10 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %7, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %9
%15 = load i32, i32* @SEEK_CUR
%16 = tail call i32 @lseek(i32 %12, i32 -1073676222, i32 %15)
%17 = call i32 @read(i32 %12, i32* %4, i32 8)
%18 = icmp eq i32 %17, 8
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %4
%21 = and i32 %20, 63
store i32 %21, i32* %1
br label %22
22: ; preds = %14, %19
%23 = phi i32 [ 1, %14 ], [ 0, %19 ]
%24 = tail call i32 @close(i32 %12)
br label %25
25: ; preds = %9, %3, %22
%26 = phi i32 [ 1, %3 ], [ 1, %9 ], [ %23, %22 ]
ret i32 %26
}