#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, int, char *);
int /*<<< orphan*/ do_write(int, int, int, char *);
void handle_events(int epollfd, struct epoll_event *events, int num, int sockfd,
                   char *buf) {
  int fd;
  int i;
  for (i = 0; i < num; i++) {
    fd = events[i].data.fd;
    if (events[i].events & EPOLLIN)
      do_read(epollfd, fd, sockfd, buf);
    else if (events[i].events & EPOLLOUT)
      do_write(epollfd, fd, sockfd, 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,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d6 <handle_events+0xb6>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2e7b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     118f <handle_events+0x6f>
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1030 <do_read@plt>
jmp    11c3 <handle_events+0xa3>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2e37(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11be <handle_events+0x9e>
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1040 <do_write@plt>
jmp    11c3 <handle_events+0xa3>
jmp    11c8 <handle_events+0xa8>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1140 <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
test   %edx,%edx
jle    1194 <handle_events+0x74>
mov    %r8,%r14
mov    %ecx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %edx,%r13d
xor    %ebp,%ebp
jmp    1167 <handle_events+0x47>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r15d,%edx
mov    %r14,%rcx
call   1030 <do_read@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     1194 <handle_events+0x74>
mov    (%rbx,%rbp,8),%eax
mov    0x4(%rbx,%rbp,8),%esi
mov    0x2e73(%rip),%rcx
test   %eax,(%rcx)
jne    1150 <handle_events+0x30>
mov    0x2e58(%rip),%rcx
test   %eax,(%rcx)
je     115e <handle_events+0x3e>
mov    %r12d,%edi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <do_write@plt>
jmp    115e <handle_events+0x3e>
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* %12
br label %13
13: ; preds = %58, %5
%14 = load i32, i32* %12
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %61
17: ; preds = %13
%18 = load %struct.epoll_event*, %struct.epoll_event** %7
%19 = load i32, i32* %12
%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* %11
%25 = load %struct.epoll_event*, %struct.epoll_event** %7
%26 = load i32, i32* %12
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %25, i64 %27
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @EPOLLIN
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %17
%35 = load i32, i32* %6
%36 = load i32, i32* %11
%37 = load i32, i32* %9
%38 = load i8*, i8** %10
%39 = call i32 @do_read(i32 %35, i32 %36, i32 %37, i8* %38)
br label %57
40: ; preds = %17
%41 = load %struct.epoll_event*, %struct.epoll_event** %7
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %41, i64 %43
%45 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @EPOLLOUT
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %40
%51 = load i32, i32* %6
%52 = load i32, i32* %11
%53 = load i32, i32* %9
%54 = load i8*, i8** %10
%55 = call i32 @do_write(i32 %51, i32 %52, i32 %53, i8* %54)
br label %56
56: ; preds = %50, %40
br label %57
57: ; preds = %56, %34
br label %58
58: ; preds = %57
%59 = load i32, i32* %12
%60 = add nsw i32 %59, 1
store i32 %60, i32* %12
br label %13
61: ; 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 %29
7: ; preds = %5
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %26
%10 = phi i64 [ 0, %7 ], [ %27, %26 ]
%11 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 %10, i32 1, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 %10, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @EPOLLIN
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %9
%19 = tail call i32 @do_read(i32 %0, i32 %12, i32 %3, i8* %4)
br label %26
20: ; preds = %9
%21 = load i32, i32* @EPOLLOUT
%22 = and i32 %21, %14
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @do_write(i32 %0, i32 %12, i32 %3, i8* %4)
br label %26
26: ; preds = %18, %24, %20
%27 = add nuw nsw i64 %10, 1
%28 = icmp eq i64 %27, %8
br i1 %28, label %29, label %9
29: ; preds = %26, %5
ret void
}