#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 PATH_MAX;
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ eprint(char *);
int snprintf(char *, int, char *, char const *, char const *);
int perf_counter_supported(const char *pmu, const char *counter) {
  char buf[PATH_MAX];
  if (snprintf(buf, PATH_MAX, "/sys/bus/event_source/devices/%s/events/%s", pmu,
               counter) >= PATH_MAX) {
    eprint("overflow in path name");
    return 0;
  }
  return !access(buf, R_OK);
}


x86 assembly (clang, -O0):
<perf_counter_supported>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
jl     11b7 <perf_counter_supported+0x87>
lea    0xe8c(%rip),%rdi
call   1050 <eprint@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11e0 <perf_counter_supported+0xb0>
mov    -0x38(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perf_counter_supported>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2eac(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe9e(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
cmp    (%r14),%eax
jge    118b <perf_counter_supported+0x5b>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
jmp    1199 <perf_counter_supported+0x69>
lea    0xe99(%rip),%rdi
call   1050 <eprint@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [43 x i8] c"/sys/bus/event_source/devices/%s/events/%s\00"
@.str.1 = [22 x i8] c"overflow in path name\00"
@R_OK = global i32 0
define i32 @perf_counter_supported(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i32, i32* @PATH_MAX
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
%17 = load i32, i32* @PATH_MAX
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %8
br label %27
21: ; preds = %2
%22 = load i32, i32* @R_OK
%23 = call i32 @access(i8* %12, i32 %22)
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
%26 = zext i1 %25 to i32
store i32 %26, i32* %3
store i32 1, i32* %8
br label %27
27: ; preds = %21, %19
%28 = load i8*, i8** %6
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [43 x i8] c"/sys/bus/event_source/devices/%s/events/%s\00"
@.str.1 = [22 x i8] c"overflow in path name\00"
@R_OK = global i32 0
define i32 @perf_counter_supported(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = load i32, i32* @PATH_MAX
%8 = icmp slt i32 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @eprint(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %16
11: ; preds = %2
%12 = load i32, i32* @R_OK
%13 = call i32 @access(i8* %5, i32 %12)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ 0, %9 ], [ %15, %11 ]
ret i32 %17
}