#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 sample_freq;
  int freq;
  int inherit;
  int /*<<< orphan*/ config;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ PERF_COUNT_HW_CPU_CYCLES;
int /*<<< orphan*/ PERF_COUNT_SW_CPU_CLOCK;
int /*<<< orphan*/ PERF_TYPE_HARDWARE;
int /*<<< orphan*/ PERF_TYPE_SOFTWARE;
int /*<<< orphan*/ test_perf_event_all_cpu(struct perf_event_attr *);
int /*<<< orphan*/ test_perf_event_task(struct perf_event_attr *);
void test_bpf_perf_event(void) {
  struct perf_event_attr attr_type_hw = {
      .sample_freq = 50,
      .freq = 1,
      .type = PERF_TYPE_HARDWARE,
      .config = PERF_COUNT_HW_CPU_CYCLES,
      .inherit = 1,
  };
  struct perf_event_attr attr_type_sw = {
      .sample_freq = 50,
      .freq = 1,
      .type = PERF_TYPE_SOFTWARE,
      .config = PERF_COUNT_SW_CPU_CLOCK,
      .inherit = 1,
  };
  test_perf_event_all_cpu(&attr_type_hw);
  test_perf_event_task(&attr_type_hw);
  test_perf_event_all_cpu(&attr_type_sw);
  test_perf_event_task(&attr_type_sw);
}


x86 assembly (clang, -O0):
<test_bpf_perf_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x32,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
movl   $0x32,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <test_perf_event_all_cpu@plt>
lea    -0x18(%rbp),%rdi
call   1030 <test_perf_event_task@plt>
lea    -0x30(%rbp),%rdi
call   1040 <test_perf_event_all_cpu@plt>
lea    -0x30(%rbp),%rdi
call   1030 <test_perf_event_task@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_bpf_perf_event>:
push   %rbx
sub    $0x30,%rsp
movabs $0x100000032,%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x20(%rsp)
mov    0x2e7d(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x24(%rsp)
mov    0x2e88(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x28(%rsp)
mov    %rax,(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <test_perf_event_all_cpu@plt>
mov    %rbx,%rdi
call   1030 <test_perf_event_task@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <test_perf_event_all_cpu@plt>
mov    %rbx,%rdi
call   1030 <test_perf_event_task@plt>
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_COUNT_HW_CPU_CYCLES = global i32 0
@PERF_TYPE_HARDWARE = global i32 0
@PERF_COUNT_SW_CPU_CLOCK = global i32 0
@PERF_TYPE_SOFTWARE = global i32 0
define void @test_bpf_perf_event() {
%1 = alloca %struct.perf_event_attr
%2 = alloca %struct.perf_event_attr
%3 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 0
store i32 50, i32* %3
%4 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 1
store i32 1, i32* %4
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 2
store i32 1, i32* %5
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 3
%7 = load i32, i32* @PERF_COUNT_HW_CPU_CYCLES
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i32 0, i32 4
%9 = load i32, i32* @PERF_TYPE_HARDWARE
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i32 0, i32 0
store i32 50, i32* %10
%11 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i32 0, i32 1
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i32 0, i32 2
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i32 0, i32 3
%14 = load i32, i32* @PERF_COUNT_SW_CPU_CLOCK
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i32 0, i32 4
%16 = load i32, i32* @PERF_TYPE_SOFTWARE
store i32 %16, i32* %15
%17 = call i32 @test_perf_event_all_cpu(%struct.perf_event_attr* %1)
%18 = call i32 @test_perf_event_task(%struct.perf_event_attr* %1)
%19 = call i32 @test_perf_event_all_cpu(%struct.perf_event_attr* %2)
%20 = call i32 @test_perf_event_task(%struct.perf_event_attr* %2)
ret void
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32, i32 }
@PERF_COUNT_HW_CPU_CYCLES = global i32 0
@PERF_TYPE_HARDWARE = global i32 0
@PERF_COUNT_SW_CPU_CLOCK = global i32 0
@PERF_TYPE_SOFTWARE = global i32 0
define void @test_bpf_perf_event() {
%1 = alloca %struct.perf_event_attr
%2 = alloca %struct.perf_event_attr
%3 = bitcast %struct.perf_event_attr* %1 to i8*
%4 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 0
store i32 50, i32* %4
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 1
store i32 1, i32* %5
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 2
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 3
%8 = load i32, i32* @PERF_COUNT_HW_CPU_CYCLES
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %1, i64 0, i32 4
%10 = load i32, i32* @PERF_TYPE_HARDWARE
store i32 %10, i32* %9
%11 = bitcast %struct.perf_event_attr* %2 to i8*
%12 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 0
store i32 50, i32* %12
%13 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 1
store i32 1, i32* %13
%14 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 2
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 3
%16 = load i32, i32* @PERF_COUNT_SW_CPU_CLOCK
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %2, i64 0, i32 4
%18 = load i32, i32* @PERF_TYPE_SOFTWARE
store i32 %18, i32* %17
%19 = call i32 @test_perf_event_all_cpu(%struct.perf_event_attr* %1)
%20 = call i32 @test_perf_event_task(%struct.perf_event_attr* %1)
%21 = call i32 @test_perf_event_all_cpu(%struct.perf_event_attr* %2)
%22 = call i32 @test_perf_event_task(%struct.perf_event_attr* %2)
ret void
}