#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 epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ handle_connect(int, int);
int /*<<< orphan*/ handle_read(int, int);
int /*<<< orphan*/ handle_write(int, int);
void do_epoll(int epollfd, int listenfd) {
  struct epoll_event events[1000];
  int num = epoll_wait(epollfd, events, 1000, -1);
  int i;
  for (i = 0; i < num; i++) {
    int fd = events[i].data.fd;
    if ((fd == listenfd) && (events[i].events & EPOLLIN)) {
      handle_connect(epollfd, listenfd);
    } else if (events[i].events & EPOLLIN) {
      handle_read(epollfd, fd);
    } else if (events[i].events & EPOLLOUT) {
      handle_write(epollfd, fd);
    }
  }
}


x86 assembly (clang, -O0):
<do_epoll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1f50(%rbp),%rsi
mov    $0x3e8,%edx
mov    $0xffffffff,%ecx
call   1050 <epoll_wait@plt>
mov    %eax,-0x1f54(%rbp)
movl   $0x0,-0x1f58(%rbp)
mov    -0x1f58(%rbp),%eax
cmp    -0x1f54(%rbp),%eax
jge    1263 <do_epoll+0x123>
movslq -0x1f58(%rbp),%rax
mov    -0x1f4c(%rbp,%rax,8),%eax
mov    %eax,-0x1f5c(%rbp)
mov    -0x1f5c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11df <do_epoll+0x9f>
movslq -0x1f58(%rbp),%rax
mov    -0x1f50(%rbp,%rax,8),%eax
mov    0x2e24(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11df <do_epoll+0x9f>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <handle_connect@plt>
jmp    124a <do_epoll+0x10a>
movslq -0x1f58(%rbp),%rax
mov    -0x1f50(%rbp,%rax,8),%eax
mov    0x2df4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1212 <do_epoll+0xd2>
mov    -0x4(%rbp),%edi
mov    -0x1f5c(%rbp),%esi
call   1030 <handle_read@plt>
jmp    1245 <do_epoll+0x105>
movslq -0x1f58(%rbp),%rax
mov    -0x1f50(%rbp,%rax,8),%eax
mov    0x2db1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1240 <do_epoll+0x100>
mov    -0x4(%rbp),%edi
mov    -0x1f5c(%rbp),%esi
call   1060 <handle_write@plt>
jmp    1245 <do_epoll+0x105>
jmp    124a <do_epoll+0x10a>
jmp    124f <do_epoll+0x10f>
mov    -0x1f58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f58(%rbp)
jmp    117a <do_epoll+0x3a>
add    $0x1f60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_epoll>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f48,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0x3e8,%edx
mov    $0xffffffff,%ecx
call   1050 <epoll_wait@plt>
test   %eax,%eax
jle    11d6 <do_epoll+0x96>
mov    %eax,%r15d
xor    %ebp,%ebp
mov    0x2e70(%rip),%r12
mov    0x2e59(%rip),%r13
jmp    11a3 <do_epoll+0x63>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <handle_connect@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
je     11d6 <do_epoll+0x96>
mov    (%rsp,%rbp,8),%eax
mov    0x4(%rsp,%rbp,8),%esi
mov    (%r12),%ecx
cmp    %ebx,%esi
jne    11b8 <do_epoll+0x78>
mov    %ecx,%edx
and    %eax,%edx
jne    1190 <do_epoll+0x50>
test   %eax,%ecx
je     11c6 <do_epoll+0x86>
mov    %r14d,%edi
call   1030 <handle_read@plt>
jmp    119a <do_epoll+0x5a>
test   %eax,0x0(%r13)
je     119a <do_epoll+0x5a>
mov    %r14d,%edi
call   1060 <handle_write@plt>
jmp    119a <do_epoll+0x5a>
add    $0x1f48,%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 }
@EPOLLIN = global i32 0
@EPOLLOUT = global i32 0
define void @do_epoll(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1000 x %struct.epoll_event]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %5, i64 0, i64 0
%11 = call i32 @epoll_wait(i32 %9, %struct.epoll_event* %10, i32 1000, i32 -1)
store i32 %11, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %68, %2
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %71
16: ; preds = %12
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %5, i64 0, i64 %18
%20 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %19, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = load i32, i32* %4
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %16
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %5, i64 0, i64 %28
%30 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @EPOLLIN
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %26
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = call i32 @handle_connect(i32 %36, i32 %37)
br label %67
39: ; preds = %26, %16
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %5, i64 0, i64 %41
%43 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @EPOLLIN
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %39
%49 = load i32, i32* %3
%50 = load i32, i32* %8
%51 = call i32 @handle_read(i32 %49, i32 %50)
br label %66
52: ; preds = %39
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %5, i64 0, i64 %54
%56 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* @EPOLLOUT
%59 = and i32 %57, %58
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %52
%62 = load i32, i32* %3
%63 = load i32, i32* %8
%64 = call i32 @handle_write(i32 %62, i32 %63)
br label %65
65: ; preds = %61, %52
br label %66
66: ; preds = %65, %48
br label %67
67: ; preds = %66, %35
br label %68
68: ; preds = %67
%69 = load i32, i32* %7
%70 = add nsw i32 %69, 1
store i32 %70, i32* %7
br label %12
71: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLLOUT = global i32 0
define void @do_epoll(i32 %0, i32 %1) {
%3 = alloca [1000 x %struct.epoll_event]
%4 = bitcast [1000 x %struct.epoll_event]* %3 to i8*
%5 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %3, i64 0, i64 0
%6 = call i32 @epoll_wait(i32 %0, %struct.epoll_event* %5, i32 1000, i32 -1)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %37
8: ; preds = %2
%9 = zext i32 %6 to i64
br label %10
10: ; preds = %8, %34
%11 = phi i64 [ 0, %8 ], [ %35, %34 ]
%12 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %3, i64 0, i64 %11, i32 1, i32 0
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, %1
%15 = getelementptr inbounds [1000 x %struct.epoll_event], [1000 x %struct.epoll_event]* %3, i64 0, i64 %11, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @EPOLLIN
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
%20 = select i1 %14, i1 true, i1 %19
br i1 %20, label %23, label %21
21: ; preds = %10
%22 = call i32 @handle_connect(i32 %0, i32 %1)
br label %34
23: ; preds = %10
%24 = and i32 %17, %16
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @handle_read(i32 %0, i32 %13)
br label %34
28: ; preds = %23
%29 = load i32, i32* @EPOLLOUT
%30 = and i32 %29, %16
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = call i32 @handle_write(i32 %0, i32 %13)
br label %34
34: ; preds = %26, %32, %28, %21
%35 = add nuw nsw i64 %11, 1
%36 = icmp eq i64 %35, %9
br i1 %36, label %37, label %10
37: ; preds = %34, %2
ret void
}