#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int EPOLLIN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ addfd(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
void lt(struct epoll_event *events, int number, int epollfd, int listenfd) {
  char buf[10];
  int i;
  for (i = 0; i < number; i++) {
    int sockfd = events[i].data.fd;
    if (sockfd == listenfd) {
      struct sockaddr_in client_addr;
      socklen_t client_addrlen = sizeof(client_addr);
      int connfd =
          accept(listenfd, (struct sockaddr *)&client_addr, &client_addrlen);
      if (connfd < 0) {
        perror("accept failed..");
        exit(-1);
      }
      addfd(epollfd, connfd, 0);
    } else if (events[i].events & EPOLLIN) {
      printf("sockefd %d read event trigger once\n", sockfd);
      int ret = recv(sockfd, buf, 10, 0);
      if (ret <= 0) {
        printf("some read error or client closed.\n");
        close(sockfd);
        continue;
      }
      printf("get %d bytes from client: %s\n", ret, buf);
    } else {
      printf("something else happened\n");
    }
  }
}


x86 assembly (clang, -O0):
<lt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12a8 <lt+0x138>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11ff <lt+0x8f>
movl   $0x4,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11ed <lt+0x7d>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0x38(%rbp),%esi
xor    %edx,%edx
call   1040 <addfd@plt>
jmp    1295 <lt+0x125>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2dd7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1282 <lt+0x112>
mov    -0x28(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     1268 <lt+0xf8>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
jmp    129a <lt+0x12a>
mov    -0x3c(%rbp),%esi
lea    -0x1e(%rbp),%rdx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1290 <lt+0x120>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1295 <lt+0x125>
jmp    129a <lt+0x12a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118c <lt+0x1c>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,(%rsp)
test   %esi,%esi
jle    1278 <lt+0xf8>
mov    %ecx,%ebx
mov    %rdi,%rbp
mov    %esi,%r12d
lea    0xe(%rsp),%r14
xor    %r13d,%r13d
jmp    11cd <lt+0x4d>
lea    0xea0(%rip),%rdi
call   1050 <puts@plt>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    %r13,%r12
je     1278 <lt+0xf8>
mov    0x4(%rbp,%r13,8),%r15d
cmp    %ebx,%r15d
jne    1210 <lt+0x90>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1090 <accept@plt>
test   %eax,%eax
js     1287 <lt+0x107>
mov    (%rsp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1040 <addfd@plt>
jmp    11c0 <lt+0x40>
cs nopw 0x0(%rax,%rax,1)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
test   %eax,0x0(%rbp,%r13,8)
je     11ab <lt+0x2b>
lea    0xde9(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    125f <lt+0xdf>
lea    0xde6(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11c0 <lt+0x40>
lea    0xe04(%rip),%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
call   1070 <close@plt>
jmp    11c0 <lt+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd72(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"accept failed..\00"
@EPOLLIN = global i32 0
@.str.1 = [36 x i8] c"sockefd %d read event trigger once\0A\00"
@.str.2 = [35 x i8] c"some read error or client closed.\0A\00"
@.str.3 = [30 x i8] c"get %d bytes from client: %s\0A\00"
@.str.4 = [25 x i8] c"something else happened\0A\00"
define void @lt(%struct.epoll_event* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.epoll_event*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store %struct.epoll_event* %0, %struct.epoll_event** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %10
br label %16
16: ; preds = %74, %4
%17 = load i32, i32* %10
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %77
20: ; preds = %16
%21 = load %struct.epoll_event*, %struct.epoll_event** %5
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i64 %23
%25 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %24, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = load i32, i32* %8
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %44
31: ; preds = %20
store i32 4, i32* %13
%32 = load i32, i32* %8
%33 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%34 = call i32 @accept(i32 %32, %struct.sockaddr* %33, i32* %13)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %31
%41 = load i32, i32* %7
%42 = load i32, i32* %14
%43 = call i32 @addfd(i32 %41, i32 %42, i32 0)
br label %73
44: ; preds = %20
%45 = load %struct.epoll_event*, %struct.epoll_event** %5
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %45, i64 %47
%49 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @EPOLLIN
%52 = and i32 %50, %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %70
54: ; preds = %44
%55 = load i32, i32* %11
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %11
%58 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%59 = call i32 @recv(i32 %57, i8* %58, i32 10, i32 0)
store i32 %59, i32* %15
%60 = load i32, i32* %15
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %54
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%64 = load i32, i32* %11
%65 = call i32 @close(i32 %64)
br label %74
66: ; preds = %54
%67 = load i32, i32* %15
%68 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %67, i8* %68)
br label %72
70: ; preds = %44
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
br label %73
73: ; preds = %72, %40
br label %74
74: ; preds = %73, %62
%75 = load i32, i32* %10
%76 = add nsw i32 %75, 1
store i32 %76, i32* %10
br label %16
77: ; preds = %16
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"accept failed..\00"
@EPOLLIN = global i32 0
@.str.1 = [36 x i8] c"sockefd %d read event trigger once\0A\00"
@.str.3 = [30 x i8] c"get %d bytes from client: %s\0A\00"
@str = [24 x i8] c"something else happened\00"
@str.5 = [34 x i8] c"some read error or client closed.\00"
define void @lt(%struct.epoll_event* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [10 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %48
10: ; preds = %4
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %10, %45
%16 = phi i64 [ 0, %10 ], [ %46, %45 ]
%17 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 %16, i32 1, i32 0
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, %3
br i1 %19, label %20, label %28
20: ; preds = %15
store i32 4, i32* %7
%21 = call i32 @accept(i32 %3, %struct.sockaddr* %13, i32* %7)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %20
%27 = call i32 @addfd(i32 %2, i32 %21, i32 0)
br label %45
28: ; preds = %15
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 %16, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @EPOLLIN
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%36 = call i32 @recv(i32 %18, i8* %8, i32 10, i32 0)
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
%40 = call i32 @close(i32 %18)
br label %45
41: ; preds = %34
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %36, i8* %8)
br label %45
43: ; preds = %28
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %38, %41, %26, %43
%46 = add nuw nsw i64 %16, 1
%47 = icmp eq i64 %46, %14
br i1 %47, label %48, label %15
48: ; preds = %45, %4
ret void
}