#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 EPOLLOUT;
int /*<<< orphan*/ EPOLL_CTL_ADD;
void *STDIN_FILENO;
void *STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int epoll_create(int);
int epoll_ctl(int, int /*<<< orphan*/, void *, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ err_exit(char *);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int epfd = epoll_create(2);
  if (epfd == -1)
    err_exit("epoll_create");
  struct epoll_event event1;
  event1.events = EPOLLIN;
  event1.data.fd = STDIN_FILENO;
  if (epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event1) == -1)
    err_exit("epoll_ctl");
  struct epoll_event event2;
  event2.events = EPOLLOUT;
  event2.data.fd = STDOUT_FILENO;
  if (epoll_ctl(epfd, EPOLL_CTL_ADD, STDOUT_FILENO, &event2) == -1)
    err_exit("epoll_ctl");
  struct epoll_event evt_results[10];
  int n = epoll_wait(epfd, evt_results, 2, -1);
  int i;
  for (i = 0; i < n; ++i) {
    if (evt_results[i].events | EPOLLIN)
      printf("FD %d is ready to read\n", evt_results[i].data.fd);
    if (evt_results[i].events | EPOLLOUT)
      printf("FD %d is ready to write\n", evt_results[i].data.fd);
  }
  close(epfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x2,%edi
call   1080 <epoll_create@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1195 <main+0x35>
lea    0xe70(%rip),%rdi
call   1060 <err_exit@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%rdx
lea    -0x10(%rbp),%rcx
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    11e2 <main+0x82>
lea    0xe30(%rip),%rdi
call   1060 <err_exit@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%rdx
lea    -0x18(%rbp),%rcx
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    122f <main+0xcf>
lea    0xde3(%rip),%rdi
call   1060 <err_exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x2,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
mov    %eax,-0x74(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x74(%rbp),%eax
jge    12ca <main+0x16a>
movslq -0x78(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%eax
mov    0x2d76(%rip),%rcx
or     (%rcx),%eax
cmp    $0x0,%eax
je     1289 <main+0x129>
movslq -0x78(%rbp),%rax
mov    -0x6c(%rbp,%rax,8),%esi
lea    0xd93(%rip),%rdi
call   1030 <printf@plt>
movslq -0x78(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%eax
mov    0x2d30(%rip),%rcx
or     (%rcx),%eax
cmp    $0x0,%eax
je     12b7 <main+0x157>
movslq -0x78(%rbp),%rax
mov    -0x6c(%rbp,%rax,8),%esi
lea    0xd7d(%rip),%rdi
call   1030 <printf@plt>
jmp    12bc <main+0x15c>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    124f <main+0xef>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x80,%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    $0x2,%edi
call   1080 <epoll_create@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    118b <main+0x2b>
lea    0xe7a(%rip),%rdi
call   1060 <err_exit@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rdx
mov    %edx,0x1c(%rsp)
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%esi
lea    0x18(%rsp),%rcx
mov    %ebp,%edi
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    11cc <main+0x6c>
lea    0xe46(%rip),%rdi
call   1060 <err_exit@plt>
mov    0x2df5(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%rdx
mov    %edx,0x14(%rsp)
mov    (%rbx),%esi
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    1208 <main+0xa8>
lea    0xe0a(%rip),%rdi
call   1060 <err_exit@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
mov    $0x2,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
test   %eax,%eax
jle    1286 <main+0x126>
mov    %eax,%ebx
xor    %r14d,%r14d
lea    0xdfd(%rip),%r13
lea    0xdde(%rip),%rbp
jmp    1249 <main+0xe9>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%rbx
je     1286 <main+0x126>
mov    0x20(%rsp,%r14,8),%eax
mov    0x2d8b(%rip),%rcx
mov    (%rcx),%ecx
or     %eax,%ecx
je     1271 <main+0x111>
lea    (%r14,%r14,1),%r15
mov    0x24(%rsp,%r14,8),%esi
mov    %rbp,%rdi
call   1030 <printf@plt>
mov    0x20(%rsp,%r15,4),%eax
or     (%r12),%eax
je     1240 <main+0xe0>
mov    0x24(%rsp,%r14,8),%esi
mov    %r13,%rdi
call   1030 <printf@plt>
jmp    1240 <main+0xe0>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [13 x i8] c"epoll_create\00"
@EPOLLIN = global i32 0
@STDIN_FILENO = global i8* null
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [10 x i8] c"epoll_ctl\00"
@EPOLLOUT = global i32 0
@STDOUT_FILENO = global i8* null
@.str.2 = [24 x i8] c"FD %d is ready to read\0A\00"
@.str.3 = [25 x i8] c"FD %d is ready to write\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.epoll_event
%4 = alloca %struct.epoll_event
%5 = alloca [10 x %struct.epoll_event]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @epoll_create(i32 2)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i32, i32* @EPOLLIN
%15 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i8*, i8** @STDIN_FILENO
%17 = ptrtoint i8* %16 to i32
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %2
%21 = load i32, i32* @EPOLL_CTL_ADD
%22 = load i8*, i8** @STDIN_FILENO
%23 = call i32 @epoll_ctl(i32 %20, i32 %21, i8* %22, %struct.epoll_event* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %13
%28 = load i32, i32* @EPOLLOUT
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i8*, i8** @STDOUT_FILENO
%31 = ptrtoint i8* %30 to i32
%32 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %2
%35 = load i32, i32* @EPOLL_CTL_ADD
%36 = load i8*, i8** @STDOUT_FILENO
%37 = call i32 @epoll_ctl(i32 %34, i32 %35, i8* %36, %struct.epoll_event* %4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %27
%42 = load i32, i32* %2
%43 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %5, i64 0, i64 0
%44 = call i32 @epoll_wait(i32 %42, %struct.epoll_event* %43, i32 2, i32 -1)
store i32 %44, i32* %6
store i32 0, i32* %7
br label %45
45: ; preds = %84, %41
%46 = load i32, i32* %7
%47 = load i32, i32* %6
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %87
49: ; preds = %45
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %5, i64 0, i64 %51
%53 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = load i32, i32* @EPOLLIN
%56 = or i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %49
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %5, i64 0, i64 %60
%62 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %61, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %58, %49
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %5, i64 0, i64 %68
%70 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = load i32, i32* @EPOLLOUT
%73 = or i32 %71, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %83
75: ; preds = %66
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %5, i64 0, i64 %77
%79 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %78, i32 0, i32 1
%80 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %79, i32 0, i32 0
%81 = load i32, i32* %80
%82 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %81)
br label %83
83: ; preds = %75, %66
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %45
87: ; preds = %45
%88 = load i32, i32* %2
%89 = call i32 @close(i32 %88)
ret i32 0
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [13 x i8] c"epoll_create\00"
@EPOLLIN = global i32 0
@STDIN_FILENO = global i8* null
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [10 x i8] c"epoll_ctl\00"
@EPOLLOUT = global i32 0
@STDOUT_FILENO = global i8* null
@.str.2 = [24 x i8] c"FD %d is ready to read\0A\00"
@.str.3 = [25 x i8] c"FD %d is ready to write\0A\00"
define i32 @main() {
%1 = alloca %struct.epoll_event
%2 = alloca %struct.epoll_event
%3 = alloca [10 x %struct.epoll_event]
%4 = tail call i32 @epoll_create(i32 2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = bitcast %struct.epoll_event* %1 to i8*
%10 = load i32, i32* @EPOLLIN
%11 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i8*, i8** @STDIN_FILENO
%13 = ptrtoint i8* %12 to i64
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @EPOLL_CTL_ADD
%17 = call i32 @epoll_ctl(i32 %4, i32 %16, i8* %12, %struct.epoll_event* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %8
%22 = bitcast %struct.epoll_event* %2 to i8*
%23 = load i32, i32* @EPOLLOUT
%24 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i8*, i8** @STDOUT_FILENO
%26 = ptrtoint i8* %25 to i64
%27 = trunc i64 %26 to i32
%28 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %2, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @EPOLL_CTL_ADD
%30 = call i32 @epoll_ctl(i32 %4, i32 %29, i8* %25, %struct.epoll_event* %2)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = bitcast [10 x %struct.epoll_event]* %3 to i8*
%36 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %3, i64 0, i64 0
%37 = call i32 @epoll_wait(i32 %4, %struct.epoll_event* %36, i32 2, i32 -1)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %65
39: ; preds = %34
%40 = zext i32 %37 to i64
br label %41
41: ; preds = %39, %62
%42 = phi i64 [ 0, %39 ], [ %63, %62 ]
%43 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %3, i64 0, i64 %42, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @EPOLLIN
%46 = or i32 %45, %44
%47 = icmp eq i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %41
%49 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %3, i64 0, i64 %42, i32 1, i32 0
%50 = load i32, i32* %49
%51 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %43
br label %53
53: ; preds = %48, %41
%54 = phi i32 [ %52, %48 ], [ %44, %41 ]
%55 = load i32, i32* @EPOLLOUT
%56 = or i32 %55, %54
%57 = icmp eq i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %53
%59 = getelementptr inbounds [10 x %struct.epoll_event], [10 x %struct.epoll_event]* %3, i64 0, i64 %42, i32 1, i32 0
%60 = load i32, i32* %59
%61 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %53, %58
%63 = add nuw nsw i64 %42, 1
%64 = icmp eq i64 %63, %40
br i1 %64, label %65, label %41
65: ; preds = %62, %34
%66 = call i32 @close(i32 %4)
ret i32 0
}