#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 size_t uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int atoi(char const *);
char *getenv(char *);
int open_msr(size_t const, int /*<<< orphan*/);
int open_msrs(int *fds, const uint32_t *cpus_to_open, uint32_t n_fds) {
  uint32_t i;
  const char *env_ro = getenv("RAPLCAP_READ_ONLY");
  int ro = env_ro == NULL ? 0 : atoi(env_ro);
  for (i = 0; i < n_fds; i++) {
    if ((fds[i] = open_msr(cpus_to_open[i], ro == 0 ? O_RDWR : O_RDONLY)) < 0) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_msrs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xeb5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1169 <open_msrs+0x39>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1175 <open_msrs+0x45>
mov    -0x30(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    120a <open_msrs+0xda>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11bc <open_msrs+0x8c>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11c8 <open_msrs+0x98>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1040 <open_msr@plt>
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rdx
mov    %eax,(%rcx,%rdx,4)
cmp    $0x0,%eax
jge    11f4 <open_msrs+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <open_msrs+0xe1>
jmp    11f9 <open_msrs+0xc9>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1183 <open_msrs+0x53>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_msrs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xeb5(%rip),%rdi
call   1030 <getenv@plt>
xor    %r12d,%r12d
mov    $0x0,%ebp
test   %rax,%rax
je     1167 <open_msrs+0x37>
mov    %rax,%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
test   %r13,%r13
je     11b5 <open_msrs+0x85>
xor    %ebx,%ebx
mov    0x2e6b(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rax
test   %ebp,%ebp
jne    118e <open_msrs+0x5e>
mov    0x2e5a(%rip),%rax
mov    (%r14,%rbx,8),%rdi
mov    (%rax),%esi
call   1040 <open_msr@plt>
mov    %eax,(%r15,%rbx,4)
test   %eax,%eax
js     11af <open_msrs+0x7f>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1180 <open_msrs+0x50>
xor    %r12d,%r12d
jmp    11b5 <open_msrs+0x85>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"RAPLCAP_READ_ONLY\00"
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
define i32 @open_msrs(i32* %0, i64* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%11 = call i8* @getenv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
br label %18
15: ; preds = %3
%16 = load i8*, i8** %9
%17 = call i32 @atoi(i8* %16)
br label %18
18: ; preds = %15, %14
%19 = phi i32 [ 0, %14 ], [ %17, %15 ]
store i32 %19, i32* %10
store i64 0, i64* %8
br label %20
20: ; preds = %44, %18
%21 = load i64, i64* %8
%22 = load i64, i64* %7
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %47
24: ; preds = %20
%25 = load i64*, i64** %6
%26 = load i64, i64* %8
%27 = getelementptr inbounds i64, i64* %25, i64 %26
%28 = load i64, i64* %27
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = load i32, i32* @O_RDWR
br label %35
33: ; preds = %24
%34 = load i32, i32* @O_RDONLY
br label %35
35: ; preds = %33, %31
%36 = phi i32 [ %32, %31 ], [ %34, %33 ]
%37 = call i32 @open_msr(i64 %28, i32 %36)
%38 = load i32*, i32** %5
%39 = load i64, i64* %8
%40 = getelementptr inbounds i32, i32* %38, i64 %39
store i32 %37, i32* %40
%41 = icmp slt i32 %37, 0
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 -1, i32* %4
br label %48
43: ; preds = %35
br label %44
44: ; preds = %43
%45 = load i64, i64* %8
%46 = add i64 %45, 1
store i64 %46, i64* %8
br label %20
47: ; preds = %20
store i32 0, i32* %4
br label %48
48: ; preds = %47, %42
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [18 x i8] c"RAPLCAP_READ_ONLY\00"
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
define i32 @open_msrs(i32* nocapture writeonly %0, i64* nocapture %1, i64 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @atoi(i8* %4)
br label %8
8: ; preds = %3, %6
%9 = phi i32 [ %7, %6 ], [ 0, %3 ]
%10 = icmp eq i64 %2, 0
br i1 %10, label %26, label %11
11: ; preds = %8
%12 = icmp eq i32 %9, 0
br label %15
13: ; preds = %15
%14 = icmp eq i64 %25, %2
br i1 %14, label %26, label %15
15: ; preds = %11, %13
%16 = phi i64 [ 0, %11 ], [ %25, %13 ]
%17 = getelementptr inbounds i64, i64* %1, i64 %16
%18 = load i64, i64* %17
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_RDONLY
%21 = select i1 %12, i32 %19, i32 %20
%22 = tail call i32 @open_msr(i64 %18, i32 %21)
%23 = getelementptr inbounds i32, i32* %0, i64 %16
store i32 %22, i32* %23
%24 = icmp slt i32 %22, 0
%25 = add nuw i64 %16, 1
br i1 %24, label %26, label %13
26: ; preds = %15, %13, %8
%27 = phi i32 [ 0, %8 ], [ 0, %13 ], [ -1, %15 ]
ret i32 %27
}