#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 pollfd {
  int fd;
  scalar_t__ revents;
  int si_code;
  int si_status;
  scalar_t__ si_pid;
  void *events;
};
typedef int ssize_t;
typedef struct pollfd siginfo_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ pfd;

/* Variables and functions */
void *POLLIN;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ P_PID;
int /*<<< orphan*/ SIGABRT;
int WEXITED;
int WNOHANG;
int WSTOPPED;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, ...);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct pollfd *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int, int, scalar_t__);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, struct pollfd *, int);
int /*<<< orphan*/ sleeper();
int /*<<< orphan*/ stderr;
int waitfd(int /*<<< orphan*/, scalar_t__, int, int /*<<< orphan*/);
int /*<<< orphan*/ waitid(int /*<<< orphan*/, scalar_t__, struct pollfd *, int);
int main(void) {
  pid_t die_pid, ptrace_pid;
  int die_fd, ptrace_fd;
  siginfo_t info;
  struct pollfd pfd[2];
  int procs_left = 2;
  memset(pfd, 0, sizeof(pfd));
  die_pid = fork();
  if (die_pid == 0)
    sleeper();
  ptrace_pid = fork();
  if (ptrace_pid == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    sleeper();
  }
  die_fd = waitfd(P_PID, die_pid, WEXITED | WSTOPPED, 0);
  ptrace_fd = waitfd(P_PID, ptrace_pid, WEXITED | WSTOPPED, 0);
  if (die_fd < 0 || ptrace_fd < 0) {
    perror("Cannot waitfd()");
    exit(1);
  }
  pfd[0].fd = die_fd;
  pfd[0].events = POLLIN;
  pfd[1].fd = ptrace_fd;
  pfd[1].events = POLLIN;
  kill(ptrace_pid, SIGABRT);
  while (procs_left > 0) {
    ssize_t bytes;
    if (poll(pfd, 2, -1) < 0)
      perror("poll() failed");
    if (pfd[0].revents != 0) {
      if ((bytes = read(die_fd, &info, sizeof(siginfo_t))) <
          sizeof(siginfo_t)) {
        fprintf(stderr, "Only read %zi bytes\n", bytes);
        exit(1);
      }
      printf("die_fd returned code %i, status %i via waitfd read: revents are "
             "%x\n",
             info.si_code, info.si_status, pfd[0].revents);
      pfd[0].fd *= -1;
      procs_left--;
    }
    if (pfd[1].revents != 0) {
      memset(&info, 0, sizeof(siginfo_t));
      waitid(P_PID, ptrace_pid, &info, WEXITED | WSTOPPED | WNOHANG);
      if (info.si_pid != ptrace_pid) {
        fprintf(
            stderr,
            "waitfd said PID %i was ready, but waitid() says it isn't: %i\n",
            ptrace_pid, info.si_pid);
        exit(1);
      }
      printf(
          "ptrace_fd returned code %i, status %i via waitid; revents are %x\n",
          info.si_code, info.si_status, pfd[1].revents);
      pfd[1].fd *= -1;
      procs_left--;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121c <main+0x4c>
mov    $0x0,%al
call   1030 <sleeper@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124d <main+0x7d>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   10a0 <ptrace@plt>
mov    $0x0,%al
call   1030 <sleeper@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    0x2d86(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1090 <waitfd@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
mov    0x2d5d(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1090 <waitfd@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     12b3 <main+0xe3>
cmpl   $0x0,-0x20(%rbp)
jge    12c9 <main+0xf9>
lea    0xd46(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xa0(%rbp)
mov    0x2cef(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ca9(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
cmpl   $0x0,-0xa4(%rbp)
jle    1481 <main+0x2b1>
lea    -0xa0(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
cmp    $0x0,%rax
jge    133f <main+0x16f>
lea    0xcd6(%rip),%rdi
call   10c0 <perror@plt>
cmpq   $0x0,-0x98(%rbp)
je     13cd <main+0x1fd>
mov    -0x1c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x28,%edx
call   1060 <read@plt>
mov    %eax,-0xa8(%rbp)
cltq
cmp    $0x28,%rax
jae    1398 <main+0x1c8>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
movslq -0xa8(%rbp),%rdx
lea    0xc97(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x98(%rbp),%rcx
lea    0xc87(%rip),%rdi
call   1040 <printf@plt>
imul   $0xffffffff,-0xa0(%rbp),%eax
mov    %eax,-0xa0(%rbp)
mov    -0xa4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xa4(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     147c <main+0x2ac>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2bf1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    0x2bdc(%rip),%rax
mov    (%rax),%ecx
mov    0x2beb(%rip),%rax
or     (%rax),%ecx
mov    0x2bc2(%rip),%rax
or     (%rax),%ecx
lea    -0x48(%rbp),%rdx
call   10e0 <waitid@plt>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1450 <main+0x280>
mov    0x2b8a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xc38(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x70(%rbp),%rcx
lea    0xc54(%rip),%rdi
call   1040 <printf@plt>
imul   $0xffffffff,-0x78(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0xa4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xa4(%rbp)
jmp    1306 <main+0x136>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1203 <main+0x33>
xor    %eax,%eax
call   1030 <sleeper@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    122d <main+0x5d>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <ptrace@plt>
xor    %eax,%eax
call   1030 <sleeper@plt>
mov    0x2dac(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2d99(%rip),%r15
mov    0x2daa(%rip),%rbp
mov    0x0(%rbp),%edx
or     (%r15),%edx
mov    %r14,%rsi
xor    %ecx,%ecx
call   1090 <waitfd@plt>
mov    %eax,%r12d
mov    0x0(%r13),%edi
mov    0x0(%rbp),%edx
or     (%r15),%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1090 <waitfd@plt>
test   %r12d,%r12d
js     141c <main+0x24c>
test   %eax,%eax
js     141c <main+0x24c>
mov    %r12d,(%rsp)
mov    0x2d3f(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x20(%rsp)
mov    %eax,0x28(%rsp)
mov    %rcx,0x48(%rsp)
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <kill@plt>
mov    $0x2,%r13d
mov    %rsp,%r14
lea    0x50(%rsp),%rbp
lea    0xd73(%rip),%r15
jmp    12d9 <main+0x109>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %r13d,%r13d
jle    13c4 <main+0x1f4>
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
test   %rax,%rax
js     13a7 <main+0x1d7>
cmpq   $0x0,0x8(%rsp)
je     1331 <main+0x161>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x28,%edx
call   1060 <read@plt>
cmp    $0x27,%eax
jbe    13d5 <main+0x205>
mov    0x60(%rsp),%esi
mov    0x64(%rsp),%edx
mov    0x8(%rsp),%rcx
mov    %r15,%rdi
call   1040 <printf@plt>
negl   (%rsp)
add    $0xffffffff,%r13d
cmpq   $0x0,0x30(%rsp)
je     12d0 <main+0x100>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
mov    0x2c98(%rip),%rax
mov    (%rax),%ecx
mov    0x2c77(%rip),%rax
or     (%rax),%ecx
mov    0x2c66(%rip),%rax
or     (%rax),%ecx
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10e0 <waitid@plt>
mov    0x68(%rsp),%rcx
cmp    %rbx,%rcx
jne    13f8 <main+0x228>
mov    0x60(%rsp),%esi
mov    0x64(%rsp),%edx
mov    0x30(%rsp),%rcx
lea    0xd20(%rip),%rdi
call   1040 <printf@plt>
negl   0x28(%rsp)
add    $0xffffffff,%r13d
jmp    12d0 <main+0x100>
lea    0xc62(%rip),%rdi
call   10c0 <perror@plt>
cmpq   $0x0,0x8(%rsp)
jne    12fc <main+0x12c>
jmp    1331 <main+0x161>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edx
mov    0x2bda(%rip),%rax
mov    (%rax),%edi
lea    0xc37(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2bb9(%rip),%rax
mov    (%rax),%edi
lea    0xc6f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xbdd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32, i32, i64, i8* }
@PTRACE_TRACEME = global i32 0
@P_PID = global i32 0
@WEXITED = global i32 0
@WSTOPPED = global i32 0
@.str = [16 x i8] c"Cannot waitfd()\00"
@POLLIN = global i8* null
@SIGABRT = global i32 0
@.str.1 = [14 x i8] c"poll() failed\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"Only read %zi bytes\0A\00"
@.str.3 = [68 x i8] c"die_fd returned code %i, status %i via waitfd read: revents are %x\0A\00"
@WNOHANG = global i32 0
@.str.4 = [62 x i8] c"waitfd said PID %i was ready, but waitid() says it isn't: %i\0A\00"
@.str.5 = [66 x i8] c"ptrace_fd returned code %i, status %i via waitid; revents are %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.pollfd
%7 = alloca [2 x %struct.pollfd]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
store i32 2, i32* %8
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%11 = call i32 @memset(%struct.pollfd* %10, i32 0, i32 80)
%12 = call i64 (...) @fork()
store i64 %12, i64* %2
%13 = load i64, i64* %2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (...) @sleeper()
br label %17
17: ; preds = %15, %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @PTRACE_TRACEME
%23 = call i32 @ptrace(i32 %22, i32 0, i32 0, i32 0)
%24 = call i32 (...) @sleeper()
br label %25
25: ; preds = %21, %17
%26 = load i32, i32* @P_PID
%27 = load i64, i64* %2
%28 = load i32, i32* @WEXITED
%29 = load i32, i32* @WSTOPPED
%30 = or i32 %28, %29
%31 = call i32 @waitfd(i32 %26, i64 %27, i32 %30, i32 0)
store i32 %31, i32* %4
%32 = load i32, i32* @P_PID
%33 = load i64, i64* %3
%34 = load i32, i32* @WEXITED
%35 = load i32, i32* @WSTOPPED
%36 = or i32 %34, %35
%37 = call i32 @waitfd(i32 %32, i64 %33, i32 %36, i32 0)
store i32 %37, i32* %5
%38 = load i32, i32* %4
%39 = icmp slt i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %25
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40, %25
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %40
%47 = load i32, i32* %4
%48 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %48, i32 0, i32 0
store i32 %47, i32* %49
%50 = load i8*, i8** @POLLIN
%51 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %51, i32 0, i32 5
store i8* %50, i8** %52
%53 = load i32, i32* %5
%54 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %54, i32 0, i32 0
store i32 %53, i32* %55
%56 = load i8*, i8** @POLLIN
%57 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%58 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %57, i32 0, i32 5
store i8* %56, i8** %58
%59 = load i64, i64* %3
%60 = load i32, i32* @SIGABRT
%61 = call i32 @kill(i64 %59, i32 %60)
br label %62
62: ; preds = %143, %46
%63 = load i32, i32* %8
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %144
65: ; preds = %62
%66 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%67 = call i64 @poll(%struct.pollfd* %66, i32 2, i32 -1)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %71
71: ; preds = %69, %65
%72 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 1
%74 = load i64, i64* %73
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %102
76: ; preds = %71
%77 = load i32, i32* %4
%78 = call i32 @read(i32 %77, %struct.pollfd* %6, i32 40)
store i32 %78, i32* %9
%79 = sext i32 %78 to i64
%80 = icmp ult i64 %79, 40
br i1 %80, label %81, label %87
81: ; preds = %76
%82 = load i32, i32* @stderr
%83 = load i32, i32* %9
%84 = sext i32 %83 to i64
%85 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %84)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %76
%88 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%89 = load i32, i32* %88
%90 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 3
%91 = load i32, i32* %90
%92 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%93 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %92, i32 0, i32 1
%94 = load i64, i64* %93
%95 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %89, i32 %91, i64 %94)
%96 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%97 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %96, i32 0, i32 0
%98 = load i32, i32* %97
%99 = mul nsw i32 %98, -1
store i32 %99, i32* %97
%100 = load i32, i32* %8
%101 = add nsw i32 %100, -1
store i32 %101, i32* %8
br label %102
102: ; preds = %87, %71
%103 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%104 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %103, i32 0, i32 1
%105 = load i64, i64* %104
%106 = icmp ne i64 %105, 0
br i1 %106, label %107, label %143
107: ; preds = %102
%108 = call i32 @memset(%struct.pollfd* %6, i32 0, i32 40)
%109 = load i32, i32* @P_PID
%110 = load i64, i64* %3
%111 = load i32, i32* @WEXITED
%112 = load i32, i32* @WSTOPPED
%113 = or i32 %111, %112
%114 = load i32, i32* @WNOHANG
%115 = or i32 %113, %114
%116 = call i32 @waitid(i32 %109, i64 %110, %struct.pollfd* %6, i32 %115)
%117 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 4
%118 = load i64, i64* %117
%119 = load i64, i64* %3
%120 = icmp ne i64 %118, %119
br i1 %120, label %121, label %128
121: ; preds = %107
%122 = load i32, i32* @stderr
%123 = load i64, i64* %3
%124 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 4
%125 = load i64, i64* %124
%126 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %122, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i64 %123, i64 %125)
%127 = call i32 @exit(i32 1)
unreachable
128: ; preds = %107
%129 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%130 = load i32, i32* %129
%131 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 3
%132 = load i32, i32* %131
%133 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%134 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %133, i32 0, i32 1
%135 = load i64, i64* %134
%136 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %130, i32 %132, i64 %135)
%137 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%138 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %137, i32 0, i32 0
%139 = load i32, i32* %138
%140 = mul nsw i32 %139, -1
store i32 %140, i32* %138
%141 = load i32, i32* %8
%142 = add nsw i32 %141, -1
store i32 %142, i32* %8
br label %143
143: ; preds = %128, %102
br label %62
144: ; preds = %62
ret i32 0
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32, i32, i64, i8* }
@PTRACE_TRACEME = global i32 0
@P_PID = global i32 0
@WEXITED = global i32 0
@WSTOPPED = global i32 0
@.str = [16 x i8] c"Cannot waitfd()\00"
@POLLIN = global i8* null
@SIGABRT = global i32 0
@.str.1 = [14 x i8] c"poll() failed\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"Only read %zi bytes\0A\00"
@.str.3 = [68 x i8] c"die_fd returned code %i, status %i via waitfd read: revents are %x\0A\00"
@WNOHANG = global i32 0
@.str.4 = [62 x i8] c"waitfd said PID %i was ready, but waitid() says it isn't: %i\0A\00"
@.str.5 = [66 x i8] c"ptrace_fd returned code %i, status %i via waitid; revents are %x\0A\00"
define i32 @main() {
%1 = alloca %struct.pollfd
%2 = alloca [2 x %struct.pollfd]
%3 = bitcast %struct.pollfd* %1 to i8*
%4 = bitcast [2 x %struct.pollfd]* %2 to i8*
%5 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%6 = call i32 @memset(%struct.pollfd* %5, i32 0, i32 80)
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (...) @sleeper()
br label %11
11: ; preds = %9, %0
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @PTRACE_TRACEME
%16 = call i32 @ptrace(i32 %15, i32 0, i32 0, i32 0)
%17 = call i32 (...) @sleeper()
br label %18
18: ; preds = %14, %11
%19 = load i32, i32* @P_PID
%20 = load i32, i32* @WEXITED
%21 = load i32, i32* @WSTOPPED
%22 = or i32 %21, %20
%23 = call i32 @waitfd(i32 %19, i64 %7, i32 %22, i32 0)
%24 = load i32, i32* @P_PID
%25 = load i32, i32* @WEXITED
%26 = load i32, i32* @WSTOPPED
%27 = or i32 %26, %25
%28 = call i32 @waitfd(i32 %24, i64 %12, i32 %27, i32 0)
%29 = icmp slt i32 %23, 0
%30 = icmp slt i32 %28, 0
%31 = select i1 %29, i1 true, i1 %30
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %18
%36 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 %23, i32* %36
%37 = load i8*, i8** @POLLIN
%38 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 5
store i8* %37, i8** %38
%39 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 0
store i32 %28, i32* %39
%40 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 5
store i8* %37, i8** %40
%41 = load i32, i32* @SIGABRT
%42 = call i32 @kill(i64 %12, i32 %41)
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 2
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 3
%46 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 1
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 4
br label %48
48: ; preds = %35, %100
%49 = phi i32 [ 2, %35 ], [ %101, %100 ]
%50 = call i64 @poll(%struct.pollfd* %5, i32 2, i32 -1)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i64, i64* %43
%56 = icmp eq i64 %55, 0
br i1 %56, label %73, label %57
57: ; preds = %54
%58 = call i32 @read(i32 %23, %struct.pollfd* %1, i32 40)
%59 = icmp ult i32 %58, 40
br i1 %59, label %60, label %65
60: ; preds = %57
%61 = zext i32 %58 to i64
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %61)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = load i32, i32* %44
%67 = load i32, i32* %45
%68 = load i64, i64* %43
%69 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %66, i32 %67, i64 %68)
%70 = load i32, i32* %36
%71 = sub nsw i32 0, %70
store i32 %71, i32* %36
%72 = add nsw i32 %49, -1
br label %73
73: ; preds = %65, %54
%74 = phi i32 [ %72, %65 ], [ %49, %54 ]
%75 = load i64, i64* %46
%76 = icmp eq i64 %75, 0
br i1 %76, label %100, label %77
77: ; preds = %73
%78 = call i32 @memset(%struct.pollfd* %1, i32 0, i32 40)
%79 = load i32, i32* @P_PID
%80 = load i32, i32* @WEXITED
%81 = load i32, i32* @WSTOPPED
%82 = or i32 %81, %80
%83 = load i32, i32* @WNOHANG
%84 = or i32 %82, %83
%85 = call i32 @waitid(i32 %79, i64 %12, %struct.pollfd* %1, i32 %84)
%86 = load i64, i64* %47
%87 = icmp eq i64 %86, %12
br i1 %87, label %92, label %88
88: ; preds = %77
%89 = load i32, i32* @stderr
%90 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i64 %12, i64 %86)
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %77
%93 = load i32, i32* %44
%94 = load i32, i32* %45
%95 = load i64, i64* %46
%96 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %93, i32 %94, i64 %95)
%97 = load i32, i32* %39
%98 = sub nsw i32 0, %97
store i32 %98, i32* %39
%99 = add nsw i32 %74, -1
br label %100
100: ; preds = %92, %73
%101 = phi i32 [ %99, %92 ], [ %74, %73 ]
%102 = icmp sgt i32 %101, 0
br i1 %102, label %48, label %103
103: ; preds = %100
ret i32 0
}