#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*/ uint32_t;
struct perf_event_attr {
  int /*<<< orphan*/ config;
  int /*<<< orphan*/ type;
  int /*<<< orphan*/ sample_period;
};

/* Variables and functions */
int /*<<< orphan*/ LONG_MAX;
int /*<<< orphan*/ PERF_EVENT_IOC_ENABLE;
int /*<<< orphan*/ __NR_perf_event_open;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int syscall(int /*<<< orphan*/, struct perf_event_attr *, int, int, int,
            unsigned long);
int bpf_open_perf_event(uint32_t type, uint64_t config, int pid, int cpu) {
  int fd;
  struct perf_event_attr attr = {};
  attr.sample_period = LONG_MAX;
  attr.type = type;
  attr.config = config;
  fd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, (1UL << 3));
  if (fd < 0) {
    fprintf(stderr, "perf_event_open: %s\n", strerror(errno));
    return -1;
  }
  if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
    perror("ioctl(PERF_EVENT_IOC_ENABLE)");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<bpf_open_perf_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    -0x28(%rbp),%rsi
mov    $0xffffffff,%r8d
mov    $0x8,%r9d
call   1070 <syscall@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1215 <bpf_open_perf_event+0xa5>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdfc(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <bpf_open_perf_event+0xe8>
mov    -0x18(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1252 <bpf_open_perf_event+0xe2>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <bpf_open_perf_event+0xe8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_open_perf_event>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %edi,0xc(%rsp)
mov    %esi,0x8(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
mov    $0xffffffff,%ebx
mov    $0x8,%r9d
mov    $0xffffffff,%r8d
call   1060 <syscall@plt>
test   %eax,%eax
js     11c0 <bpf_open_perf_event+0x60>
mov    %eax,%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f1 <bpf_open_perf_event+0x91>
mov    %ebp,%ebx
jmp    11e8 <bpf_open_perf_event+0x88>
mov    0x2e11(%rip),%rax
mov    (%rax),%ebp
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe22(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11e8 <bpf_open_perf_event+0x88>

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32 }
@LONG_MAX = global i32 0
@__NR_perf_event_open = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"perf_event_open: %s\0A\00"
@errno = global i32 0
@PERF_EVENT_IOC_ENABLE = global i32 0
@.str.1 = [29 x i8] c"ioctl(PERF_EVENT_IOC_ENABLE)\00"
define i32 @bpf_open_perf_event(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.perf_event_attr
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = bitcast %struct.perf_event_attr* %11 to i8*
%13 = load i32, i32* @LONG_MAX
%14 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %11, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %11, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %11, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @__NR_perf_event_open
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = call i32 @syscall(i32 %19, %struct.perf_event_attr* %11, i32 %20, i32 %21, i32 -1, i64 8)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 -1, i32* %5
br label %41
30: ; preds = %4
%31 = load i32, i32* %10
%32 = load i32, i32* @PERF_EVENT_IOC_ENABLE
%33 = call i64 @ioctl(i32 %31, i32 %32, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %10
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %5
br label %41
39: ; preds = %30
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %41
41: ; preds = %39, %35, %25
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32 }
@LONG_MAX = global i32 0
@__NR_perf_event_open = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"perf_event_open: %s\0A\00"
@errno = global i32 0
@PERF_EVENT_IOC_ENABLE = global i32 0
@.str.1 = [29 x i8] c"ioctl(PERF_EVENT_IOC_ENABLE)\00"
define i32 @bpf_open_perf_event(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.perf_event_attr
%6 = bitcast %struct.perf_event_attr* %5 to i8*
%7 = load i32, i32* @LONG_MAX
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i64 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i64 0, i32 1
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i64 0, i32 0
store i32 %1, i32* %10
%11 = load i32, i32* @__NR_perf_event_open
%12 = call i32 @syscall(i32 %11, %struct.perf_event_attr* %5, i32 %2, i32 %3, i32 -1, i64 8)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %26
19: ; preds = %4
%20 = load i32, i32* @PERF_EVENT_IOC_ENABLE
%21 = call i64 @ioctl(i32 %12, i32 %20, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @close(i32 %12)
br label %26
26: ; preds = %19, %23, %14
%27 = phi i32 [ -1, %14 ], [ -1, %23 ], [ %12, %19 ]
ret i32 %27
}