#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*/ do_read(int, int, char *);
int /*<<< orphan*/ do_write(int, int, char *);
int /*<<< orphan*/ handle_accpet(int, int);
void handle_events(int epollfd, struct epoll_event *events, int num,
                   int listenfd, char *buf) {
  int i;
  int fd;
  for (i = 0; i < num; i++) {
    fd = events[i].data.fd;
    if ((fd == listenfd) && (events[i].events & EPOLLIN))
      handle_accpet(epollfd, listenfd);
    else if (events[i].events & EPOLLIN)
      do_read(epollfd, fd, buf);
    else if (events[i].events & EPOLLOUT)
      do_write(epollfd, fd, buf);
  }
}


x86 assembly (clang, -O0):
<handle_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121e <handle_events+0xee>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    11a4 <handle_events+0x74>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2e5f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a4 <handle_events+0x74>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <handle_accpet@plt>
jmp    120b <handle_events+0xdb>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2e32(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d5 <handle_events+0xa5>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1030 <do_read@plt>
jmp    1206 <handle_events+0xd6>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2df1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1201 <handle_events+0xd1>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <do_write@plt>
jmp    1206 <handle_events+0xd6>
jmp    120b <handle_events+0xdb>
jmp    1210 <handle_events+0xe0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1150 <handle_events+0x20>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
test   %edx,%edx
jle    11b4 <handle_events+0x84>
mov    %ecx,%r13d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %edx,%r12d
xor    %ebp,%ebp
mov    0x2e90(%rip),%r14
jmp    1174 <handle_events+0x44>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r13d,%esi
call   1040 <handle_accpet@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
je     11b4 <handle_events+0x84>
mov    (%rbx,%rbp,8),%eax
mov    0x4(%rbx,%rbp,8),%esi
mov    (%r14),%ecx
cmp    %r13d,%esi
jne    1189 <handle_events+0x59>
mov    %ecx,%edx
and    %eax,%edx
jne    1160 <handle_events+0x30>
test   %eax,%ecx
je     119b <handle_events+0x6b>
mov    %r15d,%edi
mov    (%rsp),%rdx
call   1030 <do_read@plt>
jmp    116b <handle_events+0x3b>
mov    0x2e36(%rip),%rcx
test   %eax,(%rcx)
je     116b <handle_events+0x3b>
mov    %r15d,%edi
mov    (%rsp),%rdx
call   1050 <do_write@plt>
jmp    116b <handle_events+0x3b>
add    $0x8,%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 @handle_events(i32 %0, %struct.epoll_event* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca %struct.epoll_event*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store %struct.epoll_event* %1, %struct.epoll_event** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
store i32 0, i32* %11
br label %13
13: ; preds = %75, %5
%14 = load i32, i32* %11
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %78
17: ; preds = %13
%18 = load %struct.epoll_event*, %struct.epoll_event** %7
%19 = load i32, i32* %11
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %18, i64 %20
%22 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = load i32, i32* %9
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %17
%29 = load %struct.epoll_event*, %struct.epoll_event** %7
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %29, i64 %31
%33 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @EPOLLIN
%36 = and i32 %34, %35
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %28
%39 = load i32, i32* %6
%40 = load i32, i32* %9
%41 = call i32 @handle_accpet(i32 %39, i32 %40)
br label %74
42: ; preds = %28, %17
%43 = load %struct.epoll_event*, %struct.epoll_event** %7
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %43, i64 %45
%47 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @EPOLLIN
%50 = and i32 %48, %49
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %42
%53 = load i32, i32* %6
%54 = load i32, i32* %12
%55 = load i8*, i8** %10
%56 = call i32 @do_read(i32 %53, i32 %54, i8* %55)
br label %73
57: ; preds = %42
%58 = load %struct.epoll_event*, %struct.epoll_event** %7
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %58, i64 %60
%62 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i32, i32* @EPOLLOUT
%65 = and i32 %63, %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %57
%68 = load i32, i32* %6
%69 = load i32, i32* %12
%70 = load i8*, i8** %10
%71 = call i32 @do_write(i32 %68, i32 %69, i8* %70)
br label %72
72: ; preds = %67, %57
br label %73
73: ; preds = %72, %52
br label %74
74: ; preds = %73, %38
br label %75
75: ; preds = %74
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
store i32 %77, i32* %11
br label %13
78: ; preds = %13
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 @handle_events(i32 %0, %struct.epoll_event* nocapture %1, i32 %2, i32 %3, i8* %4) {
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %36
7: ; preds = %5
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %33
%10 = phi i64 [ 0, %7 ], [ %34, %33 ]
%11 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 %10, i32 1, i32 0
%12 = load i32, i32* %11
%13 = icmp ne i32 %12, %3
%14 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 %10, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @EPOLLIN
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
%19 = select i1 %13, i1 true, i1 %18
br i1 %19, label %22, label %20
20: ; preds = %9
%21 = tail call i32 @handle_accpet(i32 %0, i32 %3)
br label %33
22: ; preds = %9
%23 = and i32 %16, %15
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @do_read(i32 %0, i32 %12, i8* %4)
br label %33
27: ; preds = %22
%28 = load i32, i32* @EPOLLOUT
%29 = and i32 %28, %15
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = tail call i32 @do_write(i32 %0, i32 %12, i8* %4)
br label %33
33: ; preds = %20, %27, %31, %25
%34 = add nuw nsw i64 %10, 1
%35 = icmp eq i64 %34, %8
br i1 %35, label %36, label %9
36: ; preds = %33, %5
ret void
}