#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};

/* Variables and functions */
int EPOLLIN;
int /*<<< orphan*/ EPOLL_CLOEXEC;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ close(int);
int epoll_create1(int /*<<< orphan*/);
scalar_t__ epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ ksft_exit_fail_msg(char *, char const *, char const *, ...);
void poll_pidfd(const char *test_name, int pidfd) {
  int c;
  int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
  struct epoll_event event, events[5];
  if (epoll_fd == -1)
    ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
                       "(errno %d)\n",
                       test_name, errno);
  event.events = EPOLLIN;
  event.data.fd = pidfd;
  if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
    ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
                       "(errno %d)\n",
                       test_name, errno);
  }
  c = epoll_wait(epoll_fd, events, 5, 5000);
  if (c != 1 || !(events[0].events & EPOLLIN))
    ksft_exit_fail_msg(
        "%s test: Unexpected epoll_wait result (c=%d, events=%x) ",
        "(errno %d)\n", test_name, c, events[0].events, errno);
  close(epoll_fd);
  return;
}


x86 assembly (clang, -O0):
<poll_pidfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1070 <epoll_create1@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1196 <poll_pidfd+0x46>
mov    -0x8(%rbp),%rsi
mov    0x2e43(%rip),%rax
movslq (%rax),%rdx
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rcx
call   1050 <epoll_ctl@plt>
cmp    $0x0,%rax
je     11e6 <poll_pidfd+0x96>
mov    -0x8(%rbp),%rsi
mov    0x2df3(%rip),%rax
movslq (%rax),%rdx
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x5,%edx
mov    $0x1388,%ecx
call   1060 <epoll_wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    121e <poll_pidfd+0xce>
mov    -0x50(%rbp),%eax
mov    0x2dd5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1248 <poll_pidfd+0xf8>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%ecx
mov    -0x50(%rbp),%r8d
mov    0x2d98(%rip),%rax
mov    (%rax),%r9d
lea    0xe3b(%rip),%rdi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_pidfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1070 <epoll_create1@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    118f <poll_pidfd+0x3f>
mov    0x2e4d(%rip),%rax
movslq (%rax),%rdx
lea    0xe7b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <ksft_exit_fail_msg@plt>
mov    0x2e52(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,0xc(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <epoll_ctl@plt>
test   %rax,%rax
je     11d8 <poll_pidfd+0x88>
mov    0x2e04(%rip),%rax
movslq (%rax),%rdx
lea    0xe6e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <ksft_exit_fail_msg@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
mov    $0x1388,%ecx
call   1060 <epoll_wait@plt>
mov    0x10(%rsp),%r8d
cmp    $0x1,%eax
jne    11fd <poll_pidfd+0xad>
test   %r8d,(%r15)
jne    1221 <poll_pidfd+0xd1>
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%r9d
lea    0xe67(%rip),%rdi
lea    0xe54(%rip),%rsi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <ksft_exit_fail_msg@plt>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLL_CLOEXEC = global i32 0
@.str = [60 x i8] c"%s test: Failed to create epoll file descriptor (errno %d)\0A\00"
@errno = global i32 0
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [57 x i8] c"%s test: Failed to add epoll file descriptor (errno %d)\0A\00"
@.str.2 = [57 x i8] c"%s test: Unexpected epoll_wait result (c=%d, events=%x) \00"
@.str.3 = [12 x i8] c"(errno %d)\0A\00"
define void @poll_pidfd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.epoll_event
%8 = alloca [5 x %struct.epoll_event]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @EPOLL_CLOEXEC
%10 = call i32 @epoll_create1(i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i8*
%18 = call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %17)
br label %19
19: ; preds = %13, %2
%20 = load i32, i32* @EPOLLIN
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %4
%23 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %6
%26 = load i32, i32* @EPOLL_CTL_ADD
%27 = load i32, i32* %4
%28 = call i64 @epoll_ctl(i32 %25, i32 %26, i32 %27, %struct.epoll_event* %7)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %19
%31 = load i8*, i8** %3
%32 = load i32, i32* @errno
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i8*
%35 = call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %34)
br label %36
36: ; preds = %30, %19
%37 = load i32, i32* %6
%38 = getelementptr inbounds [5 x %struct.epoll_event], [5 x %struct.epoll_event]* %8, i64 0, i64 0
%39 = call i32 @epoll_wait(i32 %37, %struct.epoll_event* %38, i32 5, i32 5000)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 1
br i1 %41, label %49, label %42
42: ; preds = %36
%43 = getelementptr inbounds [5 x %struct.epoll_event], [5 x %struct.epoll_event]* %8, i64 0, i64 0
%44 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load i32, i32* @EPOLLIN
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %57, label %49
49: ; preds = %42, %36
%50 = load i8*, i8** %3
%51 = load i32, i32* %5
%52 = getelementptr inbounds [5 x %struct.epoll_event], [5 x %struct.epoll_event]* %8, i64 0, i64 0
%53 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = load i32, i32* @errno
%56 = call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 %51, i32 %54, i32 %55)
br label %57
57: ; preds = %49, %42
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLL_CLOEXEC = global i32 0
@.str = [60 x i8] c"%s test: Failed to create epoll file descriptor (errno %d)\0A\00"
@errno = global i32 0
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [57 x i8] c"%s test: Failed to add epoll file descriptor (errno %d)\0A\00"
@.str.2 = [57 x i8] c"%s test: Unexpected epoll_wait result (c=%d, events=%x) \00"
@.str.3 = [12 x i8] c"(errno %d)\0A\00"
define void @poll_pidfd(i8* %0, i32 %1) {
%3 = alloca %struct.epoll_event
%4 = alloca [5 x %struct.epoll_event]
%5 = load i32, i32* @EPOLL_CLOEXEC
%6 = tail call i32 @epoll_create1(i32 %5)
%7 = bitcast %struct.epoll_event* %3 to i8*
%8 = bitcast [5 x %struct.epoll_event]* %4 to i8*
%9 = icmp eq i32 %6, -1
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13)
br label %15
15: ; preds = %10, %2
%16 = load i32, i32* @EPOLLIN
%17 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
store i32 %1, i32* %18
%19 = load i32, i32* @EPOLL_CTL_ADD
%20 = call i64 @epoll_ctl(i32 %6, i32 %19, i32 %1, %struct.epoll_event* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %25)
br label %27
27: ; preds = %22, %15
%28 = getelementptr inbounds [5 x %struct.epoll_event], [5 x %struct.epoll_event]* %4, i64 0, i64 0
%29 = call i32 @epoll_wait(i32 %6, %struct.epoll_event* %28, i32 5, i32 5000)
%30 = icmp eq i32 %29, 1
%31 = getelementptr inbounds [5 x %struct.epoll_event], [5 x %struct.epoll_event]* %4, i64 0, i64 0, i32 0
%32 = load i32, i32* %31
br i1 %30, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @EPOLLIN
%35 = and i32 %34, %32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27, %33
%38 = load i32, i32* @errno
%39 = call i32 (i8*, i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %29, i32 %32, i32 %38)
br label %40
40: ; preds = %37, %33
%41 = call i32 @close(i32 %6)
ret void
}