#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 exclude_kernel;
  int exclude_guest;
  int /*<<< orphan*/ type;
};
typedef int /*<<< orphan*/ attr;

/* Variables and functions */
int /*<<< orphan*/ PERF_EVENT_IOC_ENABLE;
int /*<<< orphan*/ PERF_TYPE_RAW;
int /*<<< orphan*/ __NR_perf_event_open;
int /*<<< orphan*/ assert(int);
int errno;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct perf_event_attr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int syscall(int /*<<< orphan*/, struct perf_event_attr *, int /*<<< orphan*/,
            int, int, int /*<<< orphan*/);
int start_counter() {
  struct perf_event_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.type = PERF_TYPE_RAW;
  attr.size = sizeof(attr);
  attr.config = 0x5101c4;
  attr.exclude_kernel = 1;
  attr.exclude_guest = 1;
  int fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
  if (fd == -1) {
    printf("errno: %d\n", errno);
  }
  assert(fd > 0);
  assert(!ioctl(fd, PERF_EVENT_IOC_ENABLE, 0));
  return fd;
}


x86 assembly (clang, -O0):
<start_counter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
movl   $0x14,-0x18(%rbp)
movl   $0x5101c4,-0x14(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edx
mov    %r8d,%ecx
call   1070 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <start_counter+0x83>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_counter>:
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movaps 0xe85(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
mov    $0xffffffff,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <syscall@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bc <start_counter+0x6c>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1060 <assert@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@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
@__NR_perf_event_open = global i32 0
@.str = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@PERF_EVENT_IOC_ENABLE = global i32 0
define i32 @start_counter() {
%1 = alloca %struct.perf_event_attr
%2 = alloca i32
%3 = call i32 @memset(%struct.perf_event_attr* %1, i32 0, i32 20)
%4 = load i32, i32* @PERF_TYPE_RAW
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 4
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 0
store i32 20, i32* %6
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 1
store i32 5308868, i32* %7
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 2
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 3
store i32 1, i32* %9
%10 = load i32, i32* @__NR_perf_event_open
%11 = call i32 @syscall(i32 %10, %struct.perf_event_attr* %1, i32 0, i32 -1, i32 -1, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %0
%18 = load i32, i32* %2
%19 = icmp sgt i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %2
%23 = load i32, i32* @PERF_EVENT_IOC_ENABLE
%24 = call i32 @ioctl(i32 %22, i32 %23, i32 0)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_TYPE_RAW = global i32 0
@__NR_perf_event_open = global i32 0
@.str = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@PERF_EVENT_IOC_ENABLE = global i32 0
define i32 @start_counter() {
%1 = alloca %struct.perf_event_attr
%2 = bitcast %struct.perf_event_attr* %1 to i8*
%3 = call i32 @memset(%struct.perf_event_attr* %1, i32 0, i32 20)
%4 = load i32, i32* @PERF_TYPE_RAW
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 4
store i32 %4, i32* %5
%6 = bitcast %struct.perf_event_attr* %1 to <4 x i32>*
store <4 x i32> <i32 20, i32 5308868, i32 1, i32 1>, <4 x i32>* %6
%7 = load i32, i32* @__NR_perf_event_open
%8 = call i32 @syscall(i32 %7, %struct.perf_event_attr* %1, i32 0, i32 -1, i32 -1, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %0
%14 = icmp sgt i32 %8, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32, i32* @PERF_EVENT_IOC_ENABLE
%18 = call i32 @ioctl(i32 %8, i32 %17, i32 0)
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
ret i32 %8
}