#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 uint64_t;
struct perf_event_attr {
  int size;
  int type;
  unsigned int config;
  int config1;
};
typedef int /*<<< orphan*/ hw;
typedef int /*<<< orphan*/ fname;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int perf_event_open(struct perf_event_attr *, int, int, int,
                    int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
void evsetup(const char *ename, int *fd, unsigned int event, unsigned int umask,
             unsigned int filter0, unsigned int filter1) {
  char fname[1024];
  snprintf(fname, sizeof(fname), "%s/type", ename);
  FILE *fp = fopen(fname, "r");
  if (fp == 0) {
    err(1, "open %s", fname);
  }
  int type;
  int ret = fscanf(fp, "%d", &type);
  uint64_t filter0_64;
  uint64_t filter1_64;
  assert(ret == 1);
  fclose(fp);
  struct perf_event_attr hw = {};
  hw.size = sizeof(hw);
  hw.type = type;
  hw.config = event | (umask << 8);
  filter0_64 = (uint64_t)filter0;
  filter1_64 = (uint64_t)filter1;
  filter0_64 |= filter1_64 << 32;
  hw.config1 = filter0_64;
  int cpu = 0;
  int pid = -1;
  *fd = perf_event_open(&hw, pid, cpu, -1, 0);
  if (*fd == -1) {
    err(1, "CPU %d, box %s, event 0x%lx", cpu, ename, hw.config);
  }
}


x86 assembly (clang, -O0):
<evsetup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe48(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11ff <evsetup+0x7f>
lea    -0x420(%rbp),%rdx
mov    $0x1,%edi
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x428(%rbp),%rdi
lea    0xe05(%rip),%rsi
lea    -0x42c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x1,-0x430(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x448(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x10,-0x448(%rbp)
mov    -0x42c(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x440(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
or     -0x434(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,-0x43c(%rbp)
movl   $0x0,-0x44c(%rbp)
movl   $0xffffffff,-0x450(%rbp)
ff ff ff
mov    -0x450(%rbp),%esi
mov    -0x44c(%rbp),%edx
lea    -0x448(%rbp),%rdi
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   10a0 <perf_event_open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    131d <evsetup+0x19d>
mov    -0x44c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    -0x440(%rbp),%r8d
mov    $0x1,%edi
lea    0xcff(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evsetup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6f(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
call   1040 <snprintf@plt>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d5 <evsetup+0x65>
lea    0xe46(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <err@plt>
lea    0xe36(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <fscanf@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
movl   $0x0,0x1c(%rsp)
movl   $0x10,0x10(%rsp)
mov    0xc(%rsp),%eax
mov    %eax,0x14(%rsp)
shl    $0x8,%ebp
or     %r12d,%ebp
mov    %ebp,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0xffffffff,%esi
xor    %edx,%edx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1090 <perf_event_open@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
jne    125f <evsetup+0xef>
mov    0x18(%rsp),%r8d
lea    0xdc7(%rip),%rsi
mov    $0x1,%edi
xor    %edx,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <err@plt>
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32, i32 }
@.str = [8 x i8] c"%s/type\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"open %s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [28 x i8] c"CPU %d, box %s, event 0x%lx\00"
define void @evsetup(i8* %0, i32* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1024 x i8]
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca %struct.perf_event_attr
%20 = alloca i32
%21 = alloca i32
store i8* %0, i8** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%23 = load i8*, i8** %7
%24 = call i32 @snprintf(i8* %22, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %14
%27 = load i32*, i32** %14
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %6
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%31 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %6
%33 = load i32*, i32** %14
%34 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %15)
store i32 %34, i32* %16
%35 = load i32, i32* %16
%36 = icmp eq i32 %35, 1
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i32*, i32** %14
%40 = call i32 @fclose(i32* %39)
%41 = bitcast %struct.perf_event_attr* %19 to i8*
%42 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 0
store i32 16, i32* %42
%43 = load i32, i32* %15
%44 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* %9
%46 = load i32, i32* %10
%47 = shl i32 %46, 8
%48 = or i32 %45, %47
%49 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 2
store i32 %48, i32* %49
%50 = load i32, i32* %11
store i32 %50, i32* %17
%51 = load i32, i32* %12
store i32 %51, i32* %18
%52 = load i32, i32* %18
%53 = shl i32 %52, 32
%54 = load i32, i32* %17
%55 = or i32 %54, %53
store i32 %55, i32* %17
%56 = load i32, i32* %17
%57 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 3
store i32 %56, i32* %57
store i32 0, i32* %20
store i32 -1, i32* %21
%58 = load i32, i32* %21
%59 = load i32, i32* %20
%60 = call i32 @perf_event_open(%struct.perf_event_attr* %19, i32 %58, i32 %59, i32 -1, i32 0)
%61 = load i32*, i32** %8
store i32 %60, i32* %61
%62 = load i32*, i32** %8
%63 = load i32, i32* %62
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %71
65: ; preds = %32
%66 = load i32, i32* %20
%67 = load i8*, i8** %7
%68 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 2
%69 = load i32, i32* %68
%70 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %66, i8* %67, i32 %69)
br label %71
71: ; preds = %65, %32
ret void
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32 }
@.str = [8 x i8] c"%s/type\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"open %s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [28 x i8] c"CPU %d, box %s, event 0x%lx\00"
define void @evsetup(i8* %0, i32* nocapture writeonly %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca %struct.perf_event_attr
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %16
16: ; preds = %14, %6
%17 = bitcast i32* %8 to i8*
%18 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%19 = icmp eq i32 %18, 1
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = call i32 @fclose(i32* %12)
%23 = bitcast %struct.perf_event_attr* %9 to i8*
%24 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %9, i64 0, i32 3
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %9, i64 0, i32 0
store i32 16, i32* %25
%26 = load i32, i32* %8
%27 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %9, i64 0, i32 1
store i32 %26, i32* %27
%28 = shl i32 %3, 8
%29 = or i32 %28, %2
%30 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %9, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @perf_event_open(%struct.perf_event_attr* %9, i32 -1, i32 0, i32 -1, i32 0)
store i32 %31, i32* %1
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %16
%34 = load i32, i32* %30
%35 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 0, i8* %0, i32 %34)
br label %36
36: ; preds = %33, %16
ret void
}