#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*/ uint64_t;
typedef int /*<<< orphan*/ data;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pread(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, char *);
int /*<<< orphan*/ strtoull(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
uint64_t rapl_read_max_energy(unsigned int zone) {
  uint64_t ret = 0;
  int err_save;
  char buf[96];
  char data[30];
  int fd;
  snprintf(buf, sizeof(buf),
           "/sys/class/powercap"
           "/intel-rapl:%u/%s",
           zone, "max_energy_range_uj");
  errno = 0;
  fd = open(buf, O_RDONLY);
  if (fd > 0) {
    if (pread(fd, data, sizeof(data), 0) > 0) {
      ret = strtoull(data, NULL, 0);
    }
    err_save = errno;
    if (close(fd)) {
      perror(buf);
    }
    errno = err_save;
  }
  if (errno) {
    perror(buf);
  }
  return ret;
}


x86 assembly (clang, -O0):
<rapl_read_max_energy>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x60,%esi
lea    0xe78(%rip),%rdx
lea    0xe96(%rip),%r8
call   1030 <snprintf@plt>
mov    0x2e3d(%rip),%rax
movl   $0x0,(%rax)
lea    -0x70(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jle    1234 <rapl_read_max_energy+0xd4>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x1e,%edx
xor    %ecx,%ecx
call   1080 <pread@plt>
cmp    $0x0,%rax
jle    11fe <rapl_read_max_energy+0x9e>
lea    -0x90(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x94(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1228 <rapl_read_max_energy+0xc8>
lea    -0x70(%rbp),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%ecx
mov    0x2da6(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d9d(%rip),%rax
cmpl   $0x0,(%rax)
je     124d <rapl_read_max_energy+0xed>
lea    -0x70(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_read_max_energy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ecx
lea    0xe8a(%rip),%rdx
lea    0xea8(%rip),%r8
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x60,%esi
call   1030 <snprintf@plt>
mov    0x2e42(%rip),%r14
movl   $0x0,(%r14)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    1203 <rapl_read_max_energy+0xa3>
mov    %eax,%ebp
mov    %rsp,%rsi
xor    %r15d,%r15d
mov    %eax,%edi
mov    $0x1e,%edx
xor    %ecx,%ecx
call   1080 <pread@plt>
test   %rax,%rax
jle    11dc <rapl_read_max_energy+0x7c>
mov    %rsp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %eax,%r15d
mov    (%r14),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1219 <rapl_read_max_energy+0xb9>
mov    %ebx,(%r14)
test   %ebx,%ebx
jne    120d <rapl_read_max_energy+0xad>
mov    %r15d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%ebx
xor    %r15d,%r15d
test   %ebx,%ebx
je     11f2 <rapl_read_max_energy+0x92>
lea    0x20(%rsp),%rdi
call   1070 <perror@plt>
jmp    11f2 <rapl_read_max_energy+0x92>
lea    0x20(%rsp),%rdi
call   1070 <perror@plt>
jmp    11eb <rapl_read_max_energy+0x8b>

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/class/powercap/intel-rapl:%u/%s\00"
@.str.1 = [20 x i8] c"max_energy_range_uj\00"
@errno = global i32 0
@O_RDONLY = global i32 0
define i32 @rapl_read_max_energy(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [96 x i8]
%6 = alloca [30 x i8]
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%8 = getelementptr inbounds [96 x i8], [96 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @snprintf(i8* %8, i32 96, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* @errno
%11 = getelementptr inbounds [96 x i8], [96 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %1
%17 = load i32, i32* %7
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%19 = call i64 @pread(i32 %17, i8* %18, i32 30, i32 0)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%23 = call i32 @strtoull(i8* %22, i32* null, i32 0)
store i32 %23, i32* %3
br label %24
24: ; preds = %21, %16
%25 = load i32, i32* @errno
store i32 %25, i32* %4
%26 = load i32, i32* %7
%27 = call i64 @close(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = getelementptr inbounds [96 x i8], [96 x i8]* %5, i64 0, i64 0
%31 = call i32 @perror(i8* %30)
br label %32
32: ; preds = %29, %24
%33 = load i32, i32* %4
store i32 %33, i32* @errno
br label %34
34: ; preds = %32, %1
%35 = load i32, i32* @errno
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = getelementptr inbounds [96 x i8], [96 x i8]* %5, i64 0, i64 0
%39 = call i32 @perror(i8* %38)
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/class/powercap/intel-rapl:%u/%s\00"
@.str.1 = [20 x i8] c"max_energy_range_uj\00"
@errno = global i32 0
@O_RDONLY = global i32 0
define i32 @rapl_read_max_energy(i32 %0) {
%2 = alloca [96 x i8]
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [96 x i8], [96 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 96, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* @errno
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = call i64 @pread(i32 %8, i8* %5, i32 30, i32 0)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @strtoull(i8* nocapture %5, i32* null, i32 0)
br label %15
15: ; preds = %13, %10
%16 = phi i32 [ %14, %13 ], [ 0, %10 ]
%17 = load i32, i32* @errno
%18 = call i64 @close(i32 %8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @perror(i8* %4)
br label %22
22: ; preds = %20, %15
store i32 %17, i32* @errno
br label %25
23: ; preds = %1
%24 = load i32, i32* @errno
br label %25
25: ; preds = %23, %22
%26 = phi i32 [ %24, %23 ], [ %17, %22 ]
%27 = phi i32 [ 0, %23 ], [ %16, %22 ]
%28 = icmp eq i32 %26, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @perror(i8* %4)
br label %31
31: ; preds = %29, %25
ret i32 %27
}