#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 scalar_t__ off_t;
typedef int /*<<< orphan*/ dev;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, unsigned long, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_and_seek(int cpu, unsigned long msr, int mode, int *fd) {
  char dev[512];
  snprintf(dev, sizeof(dev), "/dev/cpu/%d/msr", cpu);
  *fd = open(dev, mode);
  if (*fd < 0) {
    fprintf(stderr, "open(\"%s\"): %s\n", dev, strerror(errno));
    return -1;
  }
  if (lseek(*fd, msr, SEEK_SET) == (off_t)-1) {
    fprintf(stderr, "lseek(%lu): %s\n", msr, strerror(errno));
    close(*fd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_and_seek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x200,%esi
lea    0xe71(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x220(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    120d <open_and_seek+0xad>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rdx
mov    %rax,%rcx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <open_and_seek+0x133>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    128c <open_and_seek+0x12c>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x23c(%rbp),%edi
mov    -0x238(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <open_and_seek+0x133>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_and_seek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe82(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
mov    %eax,(%r15)
test   %eax,%eax
js     11f4 <open_and_seek+0x94>
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <lseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rcx
jne    1226 <open_and_seek+0xc6>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r15),%edi
call   1050 <close@plt>
jmp    1221 <open_and_seek+0xc1>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdfe(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [16 x i8] c"lseek(%lu): %s\0A\00"
define i32 @open_and_seek(i32 %0, i64 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [512 x i8]
store i32 %0, i32* %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %6
%13 = call i32 @snprintf(i8* %11, i32 512, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%15 = load i32, i32* %8
%16 = call i32 @open(i8* %14, i32 %15)
%17 = load i32*, i32** %9
store i32 %16, i32* %17
%18 = load i32*, i32** %9
%19 = load i32, i32* %18
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %4
%22 = load i32, i32* @stderr
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %5
br label %44
27: ; preds = %4
%28 = load i32*, i32** %9
%29 = load i32, i32* %28
%30 = load i64, i64* %7
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @lseek(i32 %29, i64 %30, i32 %31)
%33 = icmp eq i64 %32, -1
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = load i64, i64* %7
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %36, i8* %38)
%40 = load i32*, i32** %9
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %5
br label %44
43: ; preds = %27
store i32 0, i32* %5
br label %44
44: ; preds = %43, %34, %21
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [16 x i8] c"lseek(%lu): %s\0A\00"
define i32 @open_and_seek(i32 %0, i64 %1, i32 %2, i32* nocapture %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = call i32 @open(i8* %6, i32 %2)
store i32 %8, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %13)
br label %26
15: ; preds = %4
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @lseek(i32 %8, i64 %1, i32 %16)
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %1, i8* %22)
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %15, %19, %10
%27 = phi i32 [ -1, %10 ], [ -1, %19 ], [ 0, %15 ]
ret i32 %27
}