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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PERF_TYPE_SOFTWARE;
int /*<<< orphan*/ __NR_perf_event_open;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int syscall(int /*<<< orphan*/, struct perf_event_attr *, int /*<<< orphan*/,
            int, int, int /*<<< orphan*/);
void break_perf_event_open(uint32_t off) {
  struct perf_event_attr pea = {
      .type = PERF_TYPE_SOFTWARE,
      .size = sizeof(struct perf_event_attr),
      .config = off,
      .mmap = 1,
      .freq = 1,
  };
  int fd = syscall(__NR_perf_event_open, &pea, 0, -1, -1, 0);
  if (fd < 0) {
    perror("perf_event_open");
    exit(EXIT_FAILURE);
  }
  if (close(fd) != 0) {
    perror("close");
    exit(EXIT_FAILURE);
  }
  return;
}


x86 assembly (clang, -O0):
<break_perf_event_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x14,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edx
mov    %r8d,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ba <break_perf_event_open+0x7a>
lea    0xe59(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     11e6 <break_perf_event_open+0xa6>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<break_perf_event_open>:
sub    $0x18,%rsp
movabs $0x100000014,%rax
mov    %rax,(%rsp)
movl   $0x1,0x8(%rsp)
mov    %edi,0xc(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
mov    $0xffffffff,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <syscall@plt>
test   %eax,%eax
js     11a1 <break_perf_event_open+0x61>
mov    %eax,%edi
call   1030 <close@plt>
test   %rax,%rax
jne    11aa <break_perf_event_open+0x6a>
add    $0x18,%rsp
ret
lea    0xe58(%rip),%rdi
jmp    11b1 <break_perf_event_open+0x71>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_TYPE_SOFTWARE = global i32 0
@__NR_perf_event_open = global i32 0
@.str = [16 x i8] c"perf_event_open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"close\00"
define void @break_perf_event_open(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.perf_event_attr
%4 = alloca i32
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i32 0, i32 0
store i32 20, i32* %5
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i32 0, i32 1
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i32 0, i32 2
store i32 1, i32* %7
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i32 0, i32 3
%9 = load i32, i32* %2
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i32 0, i32 4
%11 = load i32, i32* @PERF_TYPE_SOFTWARE
store i32 %11, i32* %10
%12 = load i32, i32* @__NR_perf_event_open
%13 = call i32 @syscall(i32 %12, %struct.perf_event_attr* %3, i32 0, i32 -1, i32 -1, i32 0)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = load i32, i32* %4
%22 = call i64 @close(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %20
ret void
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_TYPE_SOFTWARE = global i32 0
@__NR_perf_event_open = global i32 0
@.str = [16 x i8] c"perf_event_open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"close\00"
define void @break_perf_event_open(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 0
store i32 20, i32* %4
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 1
store i32 1, i32* %5
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 2
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 3
store i32 %0, i32* %7
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 4
%9 = load i32, i32* @PERF_TYPE_SOFTWARE
store i32 %9, i32* %8
%10 = load i32, i32* @__NR_perf_event_open
%11 = call i32 @syscall(i32 %10, %struct.perf_event_attr* %2, i32 0, i32 -1, i32 -1, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = call i64 @close(i32 %11)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
ret void
}