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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PERF_TYPE_RAW;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, int);
int perf_event_open(struct perf_event_attr *, int /*<<< orphan*/, int, int,
                    int /*<<< orphan*/);
int open_perf_counter(int event) {
  int fd;
  struct perf_event_attr pe = {0};
  pe.size = sizeof(struct perf_event_attr);
  pe.type = PERF_TYPE_RAW;
  pe.config = event;
  pe.pinned = 1;
  fd = perf_event_open(&pe, 0, -1, -1, 0);
  if (fd == -1)
    err(EXIT_FAILURE, "Error opening leader %llx", pe.config);
  return fd;
}


x86 assembly (clang, -O0):
<open_perf_counter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
movl   $0x14,-0x20(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %r8d,%r8d
mov    $0xffffffff,%ecx
mov    %r8d,%esi
mov    %ecx,%edx
call   1050 <perf_event_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a6 <open_perf_counter+0x76>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe5f(%rip),%rsi
call   1040 <err@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_perf_counter>:
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0x18(%rsp)
movl   $0x14,0x8(%rsp)
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %edi,0xc(%rsp)
movl   $0x1,0x10(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xffffffff,%edx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1040 <perf_event_open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1187 <open_perf_counter+0x67>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
lea    0xe7e(%rip),%rsi
call   1030 <err@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_TYPE_RAW = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [26 x i8] c"Error opening leader %llx\00"
define i32 @open_perf_counter(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.perf_event_attr
store i32 %0, i32* %2
%5 = bitcast %struct.perf_event_attr* %4 to i8*
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %4, i32 0, i32 0
store i32 20, i32* %6
%7 = load i32, i32* @PERF_TYPE_RAW
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %4, i32 0, i32 3
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %4, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %4, i32 0, i32 2
store i32 1, i32* %11
%12 = call i32 @perf_event_open(%struct.perf_event_attr* %4, i32 0, i32 -1, i32 -1, i32 0)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @EXIT_FAILURE
%17 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @err(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %15, %1
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_TYPE_RAW = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [26 x i8] c"Error opening leader %llx\00"
define i32 @open_perf_counter(i32 %0) {
%2 = alloca %struct.perf_event_attr
%3 = bitcast %struct.perf_event_attr* %2 to i8*
%4 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 4
store i32 0, i32* %4
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 0
store i32 20, i32* %5
%6 = load i32, i32* @PERF_TYPE_RAW
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 3
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 1
store i32 %0, i32* %8
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 2
store i32 1, i32* %9
%10 = call i32 @perf_event_open(%struct.perf_event_attr* %2, i32 0, i32 -1, i32 -1, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @EXIT_FAILURE
%14 = load i32, i32* %8
%15 = call i32 @err(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %1
ret i32 %10
}