#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int rapl_open_file(unsigned int zone, int subzone, const char *file) {
  char filename[96];
  if (subzone >= 0) {
    sprintf(filename,
            "/sys/class/powercap"
            "/intel-rapl:%u/intel-rapl:%u:%d/%s",
            zone, zone, subzone, file);
  } else {
    sprintf(filename, "/sys/class/powercap/intel-rapl:%u/%s", zone, file);
  }
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "rapl_open_file:open: %s: %s\n", filename, strerror(errno));
  }
  return fd;
}


x86 assembly (clang, -O0):
<rapl_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1184 <rapl_open_file+0x44>
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
mov    -0x10(%rbp),%r9
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    119d <rapl_open_file+0x5d>
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xea0(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    11fa <rapl_open_file+0xba>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0xe66(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x74(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_open_file>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r9
mov    %edi,%edx
test   %esi,%esi
js     1167 <rapl_open_file+0x27>
mov    %esi,%r8d
lea    0xea7(%rip),%rsi
mov    %rsp,%rdi
mov    %edx,%ecx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    117b <rapl_open_file+0x3b>
lea    0xec8(%rip),%rsi
mov    %rsp,%rdi
mov    %r9,%rcx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bd <rapl_open_file+0x7d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xeab(%rip),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [54 x i8] c"/sys/class/powercap/intel-rapl:%u/intel-rapl:%u:%d/%s\00"
@.str.1 = [37 x i8] c"/sys/class/powercap/intel-rapl:%u/%s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [29 x i8] c"rapl_open_file:open: %s: %s\0A\00"
@errno = global i32 0
define i32 @rapl_open_file(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [96 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %3
%12 = getelementptr inbounds [96 x i8], [96 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %4
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %12, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14, i32 %15, i8* %16)
br label %23
18: ; preds = %3
%19 = getelementptr inbounds [96 x i8], [96 x i8]* %7, i64 0, i64 0
%20 = load i32, i32* %4
%21 = load i8*, i8** %6
%22 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %21)
br label %23
23: ; preds = %18, %11
%24 = getelementptr inbounds [96 x i8], [96 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [96 x i8], [96 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %31, i8* %33)
br label %35
35: ; preds = %29, %23
%36 = load i32, i32* %8
ret i32 %36
}

LLVM IR (-O2):
@.str = [54 x i8] c"/sys/class/powercap/intel-rapl:%u/intel-rapl:%u:%d/%s\00"
@.str.1 = [37 x i8] c"/sys/class/powercap/intel-rapl:%u/%s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [29 x i8] c"rapl_open_file:open: %s: %s\0A\00"
@errno = global i32 0
define i32 @rapl_open_file(i32 %0, i32 %1, i8* %2) {
%4 = alloca [96 x i8]
%5 = getelementptr inbounds [96 x i8], [96 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %1, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %0, i32 %1, i8* %2)
br label %11
9: ; preds = %3
%10 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %2)
br label %11
11: ; preds = %9, %7
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %18)
br label %20
20: ; preds = %15, %11
ret i32 %13
}