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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PERF_FLAG_PID_CGROUP;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct perf_event_attr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perf_event_open(struct perf_event_attr *, int,
                                   int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int fd;
  struct perf_event_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.exclude_kernel = 1;
  attr.size = sizeof(attr);
  mkdir("/dev/cgroup/perf_event/blah", 0777);
  fd = open("/dev/cgroup/perf_event/blah", O_RDONLY);
  perror("open");
  rmdir("/dev/cgroup/perf_event/blah");
  sleep(2);
  perf_event_open(&attr, fd, 0, -1, PERF_FLAG_PID_CGROUP);
  perror("perf_event_open");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0x1,-0x20(%rbp)
movl   $0x8,-0x1c(%rbp)
lea    0xe45(%rip),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
lea    0xe10(%rip),%rdi
call   1040 <rmdir@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%r8d
lea    -0x20(%rbp),%rdi
xor    %edx,%edx
mov    $0xffffffff,%ecx
call   10a0 <perf_event_open@plt>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movabs $0x800000001,%rax
mov    %rax,0x8(%rsp)
lea    0xe4e(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
mov    %rbx,%rdi
call   1040 <rmdir@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%r8d
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
mov    $0xffffffff,%ecx
call   10a0 <perf_event_open@plt>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32 }
@.str = [28 x i8] c"/dev/cgroup/perf_event/blah\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PERF_FLAG_PID_CGROUP = global i32 0
@.str.2 = [16 x i8] c"perf_event_open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.perf_event_attr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @memset(%struct.perf_event_attr* %7, i32 0, i32 8)
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %7, i32 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %7, i32 0, i32 1
store i32 8, i32* %10
%11 = call i32 @mkdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 511)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @rmdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @sleep(i32 2)
%17 = load i32, i32* %6
%18 = load i32, i32* @PERF_FLAG_PID_CGROUP
%19 = call i32 @perf_event_open(%struct.perf_event_attr* %7, i32 %17, i32 0, i32 -1, i32 %18)
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
ret i32 0
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32 }
@.str = [28 x i8] c"/dev/cgroup/perf_event/blah\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PERF_FLAG_PID_CGROUP = global i32 0
@.str.2 = [16 x i8] c"perf_event_open\00"
define i32 @main(i32 %0, i8** nocapture readnone %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 8)
%6 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 0
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %3, i64 0, i32 1
store i32 8, i32* %7
%8 = call i32 @mkdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 511)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @rmdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @sleep(i32 2)
%14 = load i32, i32* @PERF_FLAG_PID_CGROUP
%15 = call i32 @perf_event_open(%struct.perf_event_attr* %3, i32 %10, i32 0, i32 -1, i32 %14)
%16 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @close(i32 %10)
ret i32 0
}