#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;
  long long sample_period;
  int inherit;
  int pinned;
  int exclude_kernel;
  int exclude_hv;
  int watermark;
  int wakeup_watermark;
  int /*<<< orphan*/ config;
  int /*<<< orphan*/ type;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int FASYNC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ F_SETOWN;
int /*<<< orphan*/ PERF_COUNT_HW_INSTRUCTIONS;
int /*<<< orphan*/ PERF_EVENT_IOC_RESET;
int /*<<< orphan*/ PERF_TYPE_HARDWARE;
int /*<<< orphan*/ __NR_perf_event_open;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct perf_event_attr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int syscall(int /*<<< orphan*/, struct perf_event_attr *, int, int, int,
            int /*<<< orphan*/);
int setup_count(long long count, pid_t pid) {
  struct perf_event_attr pe;
  int pfd = -1;
  memset(&pe, 0, sizeof(struct perf_event_attr));
  pe.type = PERF_TYPE_HARDWARE;
  pe.size = sizeof(struct perf_event_attr);
  pe.config = PERF_COUNT_HW_INSTRUCTIONS;
  pe.sample_period = count;
  pe.inherit = 1;
  pe.pinned = 1;
  pe.exclude_kernel = 1;
  pe.exclude_hv = 1;
  pe.watermark = 1;
  pe.wakeup_watermark = 1;
  printf("Waiting %lld instructions for PID %d\n", count, pid);
  pfd = syscall(__NR_perf_event_open, &pe, pid, -1, -1, 0);
  if (pfd < 0) {
    perror(NULL);
    printf("Error setting up instruction counting\n");
    exit(EXIT_FAILURE);
  }
  fcntl(pfd, F_SETOWN, pid);
  fcntl(pfd, F_SETFL, fcntl(pfd, F_GETFL) | FASYNC);
  ioctl(pfd, PERF_EVENT_IOC_RESET, 0);
  return pfd;
}


x86 assembly (clang, -O0):
<setup_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x30,-0x40(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    -0x40(%rbp),%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
mov    %r8d,%ecx
call   1070 <syscall@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    124f <setup_count+0xdf>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x44(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    %eax,%edx
mov    0x2d2f(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x44(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_count>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebx
mov    %rdi,%rbp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
movl   $0x30,(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rbp,0x8(%rsp)
movaps 0xe34(%rip),%xmm0
movups %xmm0,0x10(%rsp)
movabs $0x100000001,%rax
mov    %rax,0x20(%rsp)
lea    0xe29(%rip),%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %ebx,%edx
mov    $0xffffffff,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1080 <syscall@plt>
test   %eax,%eax
js     127c <setup_count+0xfc>
mov    %eax,%ebp
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%ebx
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <ioctl@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1090 <perror@plt>
lea    0xdac(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i64, i32, i32, i32, i32, i32, i32, i32, i32 }
@PERF_TYPE_HARDWARE = global i32 0
@PERF_COUNT_HW_INSTRUCTIONS = global i32 0
@.str = [38 x i8] c"Waiting %lld instructions for PID %d\0A\00"
@__NR_perf_event_open = global i32 0
@.str.1 = [39 x i8] c"Error setting up instruction counting\0A\00"
@EXIT_FAILURE = global i32 0
@F_SETOWN = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@FASYNC = global i32 0
@PERF_EVENT_IOC_RESET = global i32 0
define i32 @setup_count(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca %struct.perf_event_attr
%6 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
store i32 -1, i32* %6
%7 = call i32 @memset(%struct.perf_event_attr* %5, i32 0, i32 48)
%8 = load i32, i32* @PERF_TYPE_HARDWARE
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 9
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 0
store i32 48, i32* %10
%11 = load i32, i32* @PERF_COUNT_HW_INSTRUCTIONS
%12 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 8
store i32 %11, i32* %12
%13 = load i64, i64* %3
%14 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 1
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 2
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 3
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 4
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 5
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 6
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %5, i32 0, i32 7
store i32 1, i32* %20
%21 = load i64, i64* %3
%22 = load i32, i32* %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %21, i32 %22)
%24 = load i32, i32* @__NR_perf_event_open
%25 = load i32, i32* %4
%26 = call i32 @syscall(i32 %24, %struct.perf_event_attr* %5, i32 %25, i32 -1, i32 -1, i32 0)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %2
%30 = call i32 @perror(i32* null)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %2
%35 = load i32, i32* %6
%36 = load i32, i32* @F_SETOWN
%37 = load i32, i32* %4
%38 = call i32 (i32, i32, ...) @fcntl(i32 %35, i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @F_SETFL
%41 = load i32, i32* %6
%42 = load i32, i32* @F_GETFL
%43 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42)
%44 = load i32, i32* @FASYNC
%45 = or i32 %43, %44
%46 = call i32 (i32, i32, ...) @fcntl(i32 %39, i32 %40, i32 %45)
%47 = load i32, i32* %6
%48 = load i32, i32* @PERF_EVENT_IOC_RESET
%49 = call i32 @ioctl(i32 %47, i32 %48, i32 0)
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i64, i32, i32, i32, i32, i32, i32, i32, i32 }
@PERF_TYPE_HARDWARE = global i32 0
@PERF_COUNT_HW_INSTRUCTIONS = global i32 0
@.str = [38 x i8] c"Waiting %lld instructions for PID %d\0A\00"
@__NR_perf_event_open = global i32 0
@EXIT_FAILURE = global i32 0
@F_SETOWN = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@FASYNC = global i32 0
@PERF_EVENT_IOC_RESET = global i32 0
@str = [38 x i8] c"Error setting up instruction counting\00"
define i32 @setup_count(i64 %0, i32 %1) {
%3 = alloca %struct.perf_event_attr
%4 = bitcast %struct.perf_event_attr* %3 to i8*
%5 = call i32 @memset(%struct.perf_event_attr* %3, i32 0, i32 48)
%6 = load i32, i32* @PERF_TYPE_HARDWARE
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 9
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 0
store i32 48, i32* %8
%9 = load i32, i32* @PERF_COUNT_HW_INSTRUCTIONS
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 8
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 1
store i64 %0, i64* %11
%12 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 2
%13 = bitcast i32* %12 to <4 x i32>*
store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, <4 x i32>* %13
%14 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 6
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 7
store i32 1, i32* %15
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %0, i32 %1)
%17 = load i32, i32* @__NR_perf_event_open
%18 = call i32 @syscall(i32 %17, %struct.perf_event_attr* %3, i32 %1, i32 -1, i32 -1, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = call i32 @perror(i32* null)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32, i32* @F_SETOWN
%27 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %26, i32 %1)
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* @F_GETFL
%30 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %29)
%31 = load i32, i32* @FASYNC
%32 = or i32 %31, %30
%33 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %28, i32 %32)
%34 = load i32, i32* @PERF_EVENT_IOC_RESET
%35 = call i32 @ioctl(i32 %18, i32 %34, i32 0)
ret i32 %18
}