#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__ {
  scalar_t__ fd;
};
struct epoll_event {
  TYPE_1__ data;
};

/* Variables and functions */
int EPOLLET;
int EPOLLIN;
int /*<<< orphan*/ O_RDWR;
scalar_t__ STDIN_FILENO;
int epoll_create1(int /*<<< orphan*/);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ free(struct epoll_event *);
struct epoll_event *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ register_fd(int, scalar_t__, int);
int main() {
  int epfd, ret, fd;
  struct epoll_event event;
  struct epoll_event *events;
  int nr_events, i;
  epfd = epoll_create1(0);
  if (epfd < 0)
    perror("epoll_create1");
  fd = open("doc", O_RDWR);
  register_fd(epfd, STDIN_FILENO, EPOLLIN | EPOLLET);
  events = malloc(sizeof(struct epoll_event) * 64);
  if (!events) {
    perror("malloc");
    return 1;
  }
  while (1) {
    nr_events = epoll_wait(epfd, events, 64, -1);
    if (nr_events < 0) {
      perror("epoll_wait");
      free(events);
      return 1;
    }
    char buf[100];
    for (i = 0; i < nr_events; i++) {
      if (events[i].data.fd == STDIN_FILENO) {
        printf("Hello World!\n");
      }
    }
  }
  free(events);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   10a0 <epoll_create1@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b2 <main+0x32>
lea    0xe53(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%rsi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
call   1050 <register_fd@plt>
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121f <main+0x9f>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <main+0x13b>
jmp    1224 <main+0xa4>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x40,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1268 <main+0xe8>
lea    0xdcb(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <main+0x13b>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12b6 <main+0x136>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%rax
jne    12a3 <main+0x123>
lea    0xd86(%rip),%rdi
call   1040 <printf@plt>
jmp    12a8 <main+0x128>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    126f <main+0xef>
jmp    1224 <main+0xa4>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   10a0 <epoll_create1@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11a5 <main+0x25>
lea    0xe60(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1080 <open@plt>
mov    0x2e07(%rip),%r15
mov    (%r15),%rsi
mov    0x2e15(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14d,%edi
call   1050 <register_fd@plt>
mov    $0x200,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    11fb <main+0x7b>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
jmp    122b <main+0xab>
mov    %rax,%r13
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x40,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
test   %eax,%eax
jns    123f <main+0xbf>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdde(%rip),%r12
jmp    1269 <main+0xe9>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x40,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
test   %eax,%eax
js     1217 <main+0x97>
test   %eax,%eax
je     1250 <main+0xd0>
mov    %eax,%ebp
mov    (%r15),%rax
xor    %ebx,%ebx
jmp    1289 <main+0x109>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1250 <main+0xd0>
cmp    %rax,0x0(%r13,%rbx,8)
jne    1280 <main+0x100>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    (%r15),%rax
jmp    1280 <main+0x100>

LLVM IR (-O0):
%struct.epoll_event = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@.str = [14 x i8] c"epoll_create1\00"
@.str.1 = [4 x i8] c"doc\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i64 0
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [11 x i8] c"epoll_wait\00"
@.str.4 = [14 x i8] c"Hello World!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.epoll_event
%6 = alloca %struct.epoll_event*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
store i32 0, i32* %1
%10 = call i32 @epoll_create1(i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = load i64, i64* @STDIN_FILENO
%20 = load i32, i32* @EPOLLIN
%21 = load i32, i32* @EPOLLET
%22 = or i32 %20, %21
%23 = call i32 @register_fd(i32 %18, i64 %19, i32 %22)
%24 = call %struct.epoll_event* @malloc(i32 512)
store %struct.epoll_event* %24, %struct.epoll_event** %6
%25 = load %struct.epoll_event*, %struct.epoll_event** %6
%26 = icmp ne %struct.epoll_event* %25, null
br i1 %26, label %29, label %27
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %62
29: ; preds = %15
br label %30
30: ; preds = %29, %61
%31 = load i32, i32* %2
%32 = load %struct.epoll_event*, %struct.epoll_event** %6
%33 = call i32 @epoll_wait(i32 %31, %struct.epoll_event* %32, i32 64, i32 -1)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%38 = load %struct.epoll_event*, %struct.epoll_event** %6
%39 = call i32 @free(%struct.epoll_event* %38)
store i32 1, i32* %1
br label %62
40: ; preds = %30
store i32 0, i32* %8
br label %41
41: ; preds = %58, %40
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %61
45: ; preds = %41
%46 = load %struct.epoll_event*, %struct.epoll_event** %6
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %46, i64 %48
%50 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %49, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = load i64, i64* @STDIN_FILENO
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %45
br label %58
58: ; preds = %57
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %41
61: ; preds = %41
br label %30
62: ; preds = %36, %27
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.epoll_event = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@.str = [14 x i8] c"epoll_create1\00"
@.str.1 = [4 x i8] c"doc\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i64 0
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [11 x i8] c"epoll_wait\00"
@str = [13 x i8] c"Hello World!\00"
define i32 @main() {
%1 = tail call i32 @epoll_create1(i32 0)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i64, i64* @STDIN_FILENO
%9 = load i32, i32* @EPOLLIN
%10 = load i32, i32* @EPOLLET
%11 = or i32 %10, %9
%12 = tail call i32 @register_fd(i32 %1, i64 %8, i32 %11)
%13 = tail call %struct.epoll_event* @malloc(i32 512)
%14 = icmp eq %struct.epoll_event* %13, null
br i1 %14, label %18, label %15
15: ; preds = %5
%16 = tail call i32 @epoll_wait(i32 %1, %struct.epoll_event* %13, i32 64, i32 -1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %29, label %23
18: ; preds = %5
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
20: ; preds = %41, %23
%21 = tail call i32 @epoll_wait(i32 %1, %struct.epoll_event* %13, i32 64, i32 -1)
%22 = icmp slt i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %15, %20
%24 = phi i32 [ %21, %20 ], [ %16, %15 ]
%25 = icmp eq i32 %24, 0
br i1 %25, label %20, label %26
26: ; preds = %23
%27 = zext i32 %24 to i64
%28 = load i64, i64* @STDIN_FILENO
br label %32
29: ; preds = %20, %15
%30 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @free(%struct.epoll_event* %13)
br label %45
32: ; preds = %26, %41
%33 = phi i64 [ %28, %26 ], [ %42, %41 ]
%34 = phi i64 [ 0, %26 ], [ %43, %41 ]
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %13, i64 %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, %33
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%40 = load i64, i64* @STDIN_FILENO
br label %41
41: ; preds = %32, %38
%42 = phi i64 [ %33, %32 ], [ %40, %38 ]
%43 = add nuw nsw i64 %34, 1
%44 = icmp eq i64 %43, %27
br i1 %44, label %20, label %32
45: ; preds = %29, %18
ret i32 1
}