#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ serveraddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNRESET;
int EPOLLET;
int EPOLLIN;
int EPOLLOUT;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ EPOLL_CTL_MOD;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int epoll_create(int);
int /*<<< orphan*/ epoll_ctl(int, int /*<<< orphan*/, int,
                             struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ setnonblocking(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int i, maxi, listenfd, connfd, sockfd, epfd, nfds, portnumber;
  ssize_t n;
  char readline[(1024 * 16)], writeline[(1024 * 16)];
  socklen_t clilen;
  if (2 == argc) {
    if ((portnumber = atoi(argv[1])) < 0) {
      fprintf(stderr, "Usage:%s portnumber\a\n", argv[0]);
      return 1;
    }
  } else {
    fprintf(stderr, "Usage:%s portnumber\a\n", argv[0]);
    return 1;
  }
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in clientaddr;
  struct sockaddr_in serveraddr;
  bzero(&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  char *local_addr = "127.0.0.1";
  inet_aton(local_addr, &(serveraddr.sin_addr));
  serveraddr.sin_port = htons(portnumber);
  bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
  listen(listenfd, 20);
  maxi = 0;
  struct epoll_event ev, events[20];
  epfd = epoll_create(256);
  setnonblocking(listenfd);
  ev.data.fd = listenfd;
  ev.events = EPOLLIN | EPOLLET;
  epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);
  int readsize = 0;
  for (;;) {
    nfds = epoll_wait(epfd, events, 20, 500);
    for (i = 0; i < nfds; ++i) {
      if (events[i].data.fd == listenfd) {
        connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clilen);
        if (connfd < 0) {
          perror("connfd<0");
          exit(EXIT_FAILURE);
        }
        setnonblocking(connfd);
        char *str = inet_ntoa(clientaddr.sin_addr);
        printf("accapt a connection from %s, socket %d\n", str, connfd);
        ev.data.fd = connfd;
        ev.events = EPOLLIN | EPOLLET;
        epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
      } else if (events[i].events & EPOLLIN) {
        printf("EPOLLIN\t");
        if ((sockfd = events[i].data.fd) < 0)
          continue;
        if ((n = read(sockfd, readline, (1024 * 16))) < 0) {
          if (errno == ECONNRESET) {
            close(sockfd);
            events[i].data.fd = -1;
          } else
            printf("socket %d readline error\n", sockfd);
        } else if (n == 0) {
          close(sockfd);
          events[i].data.fd = -1;
        }
        readsize = n;
        printf("read %d bytes from socket %d\n", n, sockfd);
        ev.data.fd = sockfd;
        ev.events = EPOLLOUT;
        epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
      } else if (events[i].events & EPOLLOUT) {
        if ((sockfd = events[i].data.fd) < 0)
          continue;
        n = write(sockfd, writeline, (1024 * 16));
        printf("write %d bytes to socket %d\n", n, sockfd);
        ev.data.fd = sockfd;
        ev.events = EPOLLIN | EPOLLET;
        epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
jne    12ad <main+0x6d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1130 <atoi@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    12a8 <main+0x68>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd69(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16a6 <main+0x466>
jmp    12d5 <main+0x95>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd3c(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16a6 <main+0x466>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    0x2ccb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1160 <socket@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x8060(%rbp),%rdi
mov    $0xc,%esi
call   1110 <bzero@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8058(%rbp)
lea    0xcfe(%rip),%rax
mov    %rax,-0x8068(%rbp)
mov    -0x8068(%rbp),%rdi
lea    -0x8060(%rbp),%rsi
call   10b0 <inet_aton@plt>
mov    -0x30(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x805c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x8060(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    -0x1c(%rbp),%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
movl   $0x0,-0x18(%rbp)
mov    $0x100,%edi
call   1140 <epoll_create@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   10f0 <setnonblocking@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x806c(%rbp)
mov    0x2c53(%rip),%rax
mov    (%rax),%eax
mov    0x2bfa(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8070(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2c30(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
lea    -0x8070(%rbp),%rcx
call   1080 <epoll_ctl@plt>
movl   $0x0,-0x8114(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x8110(%rbp),%rsi
mov    $0x14,%edx
mov    $0x1f4,%ecx
call   10d0 <epoll_wait@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    16a1 <main+0x461>
movslq -0x14(%rbp),%rax
mov    -0x810c(%rbp,%rax,8),%eax
cmp    -0x1c(%rbp),%eax
jne    14b6 <main+0x276>
mov    -0x1c(%rbp),%edi
lea    -0x8050(%rbp),%rsi
lea    -0x8044(%rbp),%rdx
call   1120 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1443 <main+0x203>
lea    0xbf0(%rip),%rdi
call   1100 <perror@plt>
mov    0x2b5c(%rip),%rax
mov    (%rax),%edi
call   1150 <exit@plt>
mov    -0x20(%rbp),%edi
call   10f0 <setnonblocking@plt>
mov    -0x8050(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x8120(%rbp)
mov    -0x8120(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xbbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x806c(%rbp)
mov    0x2b5b(%rip),%rax
mov    (%rax),%eax
mov    0x2b02(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8070(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2b38(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
lea    -0x8070(%rbp),%rcx
call   1080 <epoll_ctl@plt>
jmp    168e <main+0x44e>
movslq -0x14(%rbp),%rax
mov    -0x8110(%rbp,%rax,8),%eax
mov    0x2b18(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     15e4 <main+0x3a4>
lea    0xb77(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x14(%rbp),%rax
mov    -0x810c(%rbp,%rax,8),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    14fd <main+0x2bd>
jmp    1693 <main+0x453>
mov    -0x24(%rbp),%edi
lea    -0x4040(%rbp),%rsi
mov    $0x4000,%edx
call   1090 <read@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1569 <main+0x329>
mov    0x2a7c(%rip),%rax
mov    (%rax),%rax
mov    0x2a9a(%rip),%rcx
cmp    (%rcx),%rax
jne    1553 <main+0x313>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movslq -0x14(%rbp),%rax
movl   $0xffffffff,-0x810c(%rbp,%rax,8)
ff ff ff ff
jmp    1564 <main+0x324>
mov    -0x24(%rbp),%esi
lea    0xafd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    158f <main+0x34f>
cmpl   $0x0,-0x34(%rbp)
jne    158a <main+0x34a>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movslq -0x14(%rbp),%rax
movl   $0xffffffff,-0x810c(%rbp,%rax,8)
ff ff ff ff
jmp    158f <main+0x34f>
mov    -0x34(%rbp),%eax
mov    %eax,-0x8114(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xacf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x806c(%rbp)
mov    0x29ec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8070(%rbp)
mov    -0x28(%rbp),%edi
mov    0x29f2(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
lea    -0x8070(%rbp),%rcx
call   1080 <epoll_ctl@plt>
jmp    1689 <main+0x449>
movslq -0x14(%rbp),%rax
mov    -0x8110(%rbp,%rax,8),%eax
mov    0x29b2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1684 <main+0x444>
movslq -0x14(%rbp),%rax
mov    -0x810c(%rbp,%rax,8),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    161d <main+0x3dd>
jmp    1693 <main+0x453>
mov    -0x24(%rbp),%edi
lea    -0x8040(%rbp),%rsi
mov    $0x4000,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xa51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x806c(%rbp)
mov    0x2988(%rip),%rax
mov    (%rax),%eax
mov    0x292f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8070(%rbp)
mov    -0x28(%rbp),%edi
mov    0x294d(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
lea    -0x8070(%rbp),%rcx
call   1080 <epoll_ctl@plt>
jmp    1689 <main+0x449>
jmp    168e <main+0x44e>
jmp    1693 <main+0x453>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    13e6 <main+0x1a6>
jmp    13c3 <main+0x183>
mov    -0x4(%rbp),%eax
add    $0x8120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x80d8,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1266 <main+0x26>
mov    0x8(%rbx),%rdi
call   1130 <atoi@plt>
test   %eax,%eax
jns    1295 <main+0x55>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd87(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%eax
add    $0x80d8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    0x2d4a(%rip),%r15
mov    (%r15),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1160 <socket@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1110 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
lea    0xd42(%rip),%rdi
mov    %rbx,%rsi
call   10b0 <inet_aton@plt>
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    %r14d,%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
mov    $0x100,%edi
call   1140 <epoll_create@plt>
mov    %eax,%r13d
mov    %r14d,%edi
call   10f0 <setnonblocking@plt>
mov    0x2cc0(%rip),%rbx
mov    0x2c69(%rip),%rax
mov    (%rax),%eax
or     (%rbx),%eax
mov    %r14d,0x4(%rsp)
mov    %eax,(%rsp)
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
mov    %r13d,%edi
mov    %r14d,%edx
call   1080 <epoll_ctl@plt>
mov    %r13d,%edi
lea    0x30(%rsp),%rsi
mov    $0x14,%edx
mov    $0x1f4,%ecx
call   10d0 <epoll_wait@plt>
test   %eax,%eax
jle    134a <main+0x10a>
mov    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %r15d,%r15d
jmp    13d6 <main+0x196>
mov    %r12d,%edi
call   1070 <close@plt>
movl   $0xffffffff,0x34(%rsp,%rbp,4)
ff
lea    0xcec(%rip),%rdi
mov    %r13d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,0x4(%rsp)
mov    0x2c07(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2c13(%rip),%rax
mov    %ebx,%r13d
mov    0x2c29(%rip),%rbx
mov    (%rax),%esi
mov    %r13d,%edi
mov    %r12d,%edx
mov    %rsp,%rcx
call   1080 <epoll_ctl@plt>
add    $0x1,%r15
cmp    %r15,0x8(%rsp)
je     134a <main+0x10a>
mov    0x34(%rsp,%r15,8),%r12d
cmp    %r14d,%r12d
jne    1440 <main+0x200>
mov    %r14d,%edi
lea    0x20(%rsp),%rsi
lea    0x1c(%rsp),%rdx
call   1120 <accept@plt>
test   %eax,%eax
js     1522 <main+0x2e2>
mov    %eax,%r12d
mov    %eax,%edi
call   10f0 <setnonblocking@plt>
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xc15(%rip),%rdi
mov    %rax,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,0x4(%rsp)
mov    0x2b63(%rip),%rax
mov    (%rax),%eax
or     (%rbx),%eax
mov    %eax,(%rsp)
mov    0x2b9d(%rip),%rax
jmp    13b7 <main+0x177>
mov    0x30(%rsp,%r15,8),%eax
test   %eax,(%rbx)
je     1493 <main+0x253>
lea    (%r15,%r15,1),%rbp
lea    0xbfd(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x34(%rsp,%rbp,4),%r12d
test   %r12d,%r12d
js     13c7 <main+0x187>
mov    %r13d,%ebx
mov    %r12d,%edi
lea    0x40d0(%rsp),%rsi
mov    $0x4000,%edx
call   1090 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
js     14f2 <main+0x2b2>
jne    1381 <main+0x141>
jmp    1371 <main+0x131>
mov    0x2b0e(%rip),%rcx
test   %eax,(%rcx)
je     13c7 <main+0x187>
test   %r12d,%r12d
js     13c7 <main+0x187>
mov    %r12d,%edi
lea    0xd0(%rsp),%rsi
mov    $0x4000,%edx
call   1030 <write@plt>
lea    0xbcb(%rip),%rdi
mov    %eax,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,0x4(%rsp)
mov    0x2ab1(%rip),%rax
mov    (%rax),%eax
or     (%rbx),%eax
mov    %eax,(%rsp)
mov    0x2ad3(%rip),%rax
jmp    13b7 <main+0x177>
mov    0x2aa7(%rip),%rax
mov    (%rax),%rax
mov    0x2ac5(%rip),%rcx
cmp    (%rcx),%rax
je     1371 <main+0x131>
lea    0xb47(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1381 <main+0x141>
lea    0xaf7(%rip),%rdi
call   1100 <perror@plt>
mov    0x2a63(%rip),%rax
mov    (%rax),%edi
call   1150 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage:%s portnumber\07\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.2 = [9 x i8] c"connfd<0\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [40 x i8] c"accapt a connection from %s, socket %d\0A\00"
@.str.4 = [9 x i8] c"EPOLLIN\09\00"
@errno = global i64 0
@ECONNRESET = global i64 0
@.str.5 = [26 x i8] c"socket %d readline error\0A\00"
@.str.6 = [30 x i8] c"read %d bytes from socket %d\0A\00"
@EPOLLOUT = global i32 0
@EPOLL_CTL_MOD = global i32 0
@.str.7 = [29 x i8] c"write %d bytes to socket %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [16384 x i8]
%16 = alloca [16384 x i8]
%17 = alloca i32
%18 = alloca %struct.sockaddr_in
%19 = alloca %struct.sockaddr_in
%20 = alloca i8*
%21 = alloca %struct.epoll_event
%22 = alloca [20 x %struct.epoll_event]
%23 = alloca i32
%24 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%25 = load i32, i32* %4
%26 = icmp eq i32 2, %25
br i1 %26, label %27, label %40
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %13
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %37)
store i32 1, i32* %3
br label %234
39: ; preds = %27
br label %46
40: ; preds = %2
%41 = load i32, i32* @stderr
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %44)
store i32 1, i32* %3
br label %234
46: ; preds = %39
%47 = load i32, i32* @AF_INET
%48 = load i32, i32* @SOCK_STREAM
%49 = call i32 @socket(i32 %47, i32 %48, i32 0)
store i32 %49, i32* %8
%50 = call i32 @bzero(%struct.sockaddr_in* %19, i32 12)
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 2
store i32 %51, i32* %52
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %20
%53 = load i8*, i8** %20
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%55 = call i32 @inet_aton(i8* %53, i32* %54)
%56 = load i32, i32* %13
%57 = call i32 @htons(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
store i32 %57, i32* %58
%59 = load i32, i32* %8
%60 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%61 = call i32 @bind(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = load i32, i32* %8
%63 = call i32 @listen(i32 %62, i32 20)
store i32 0, i32* %7
%64 = call i32 @epoll_create(i32 256)
store i32 %64, i32* %11
%65 = load i32, i32* %8
%66 = call i32 @setnonblocking(i32 %65)
%67 = load i32, i32* %8
%68 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 1
%69 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %68, i32 0, i32 0
store i32 %67, i32* %69
%70 = load i32, i32* @EPOLLIN
%71 = load i32, i32* @EPOLLET
%72 = or i32 %70, %71
%73 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 0
store i32 %72, i32* %73
%74 = load i32, i32* %11
%75 = load i32, i32* @EPOLL_CTL_ADD
%76 = load i32, i32* %8
%77 = call i32 @epoll_ctl(i32 %74, i32 %75, i32 %76, %struct.epoll_event* %21)
store i32 0, i32* %23
br label %78
78: ; preds = %233, %46
%79 = load i32, i32* %11
%80 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 0
%81 = call i32 @epoll_wait(i32 %79, %struct.epoll_event* %80, i32 20, i32 500)
store i32 %81, i32* %12
store i32 0, i32* %6
br label %82
82: ; preds = %230, %78
%83 = load i32, i32* %6
%84 = load i32, i32* %12
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %233
86: ; preds = %82
%87 = load i32, i32* %6
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %88
%90 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %89, i32 0, i32 1
%91 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %90, i32 0, i32 0
%92 = load i32, i32* %91
%93 = load i32, i32* %8
%94 = icmp eq i32 %92, %93
br i1 %94, label %95, label %125
95: ; preds = %86
%96 = load i32, i32* %8
%97 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
%98 = call i32 @accept(i32 %96, %struct.sockaddr* %97, i32* %17)
store i32 %98, i32* %9
%99 = load i32, i32* %9
%100 = icmp slt i32 %99, 0
br i1 %100, label %101, label %105
101: ; preds = %95
%102 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%103 = load i32, i32* @EXIT_FAILURE
%104 = call i32 @exit(i32 %103)
unreachable
105: ; preds = %95
%106 = load i32, i32* %9
%107 = call i32 @setnonblocking(i32 %106)
%108 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%109 = load i32, i32* %108
%110 = call i8* @inet_ntoa(i32 %109)
store i8* %110, i8** %24
%111 = load i8*, i8** %24
%112 = load i32, i32* %9
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %111, i32 %112)
%114 = load i32, i32* %9
%115 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 1
%116 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %115, i32 0, i32 0
store i32 %114, i32* %116
%117 = load i32, i32* @EPOLLIN
%118 = load i32, i32* @EPOLLET
%119 = or i32 %117, %118
%120 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 0
store i32 %119, i32* %120
%121 = load i32, i32* %11
%122 = load i32, i32* @EPOLL_CTL_ADD
%123 = load i32, i32* %9
%124 = call i32 @epoll_ctl(i32 %121, i32 %122, i32 %123, %struct.epoll_event* %21)
br label %229
125: ; preds = %86
%126 = load i32, i32* %6
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %127
%129 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %128, i32 0, i32 0
%130 = load i32, i32* %129
%131 = load i32, i32* @EPOLLIN
%132 = and i32 %130, %131
%133 = icmp ne i32 %132, 0
br i1 %133, label %134, label %191
134: ; preds = %125
%135 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%136 = load i32, i32* %6
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %137
%139 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %138, i32 0, i32 1
%140 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %139, i32 0, i32 0
%141 = load i32, i32* %140
store i32 %141, i32* %10
%142 = icmp slt i32 %141, 0
br i1 %142, label %143, label %144
143: ; preds = %134
br label %230
144: ; preds = %134
%145 = load i32, i32* %10
%146 = getelementptr inbounds [16384 x i8], [16384 x i8]* %15, i64 0, i64 0
%147 = call i32 @read(i32 %145, i8* %146, i32 16384)
store i32 %147, i32* %14
%148 = icmp slt i32 %147, 0
br i1 %148, label %149, label %165
149: ; preds = %144
%150 = load i64, i64* @errno
%151 = load i64, i64* @ECONNRESET
%152 = icmp eq i64 %150, %151
br i1 %152, label %153, label %161
153: ; preds = %149
%154 = load i32, i32* %10
%155 = call i32 @close(i32 %154)
%156 = load i32, i32* %6
%157 = sext i32 %156 to i64
%158 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %157
%159 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %158, i32 0, i32 1
%160 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %159, i32 0, i32 0
store i32 -1, i32* %160
br label %164
161: ; preds = %149
%162 = load i32, i32* %10
%163 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %162)
br label %164
164: ; preds = %161, %153
br label %177
165: ; preds = %144
%166 = load i32, i32* %14
%167 = icmp eq i32 %166, 0
br i1 %167, label %168, label %176
168: ; preds = %165
%169 = load i32, i32* %10
%170 = call i32 @close(i32 %169)
%171 = load i32, i32* %6
%172 = sext i32 %171 to i64
%173 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %172
%174 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %173, i32 0, i32 1
%175 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %174, i32 0, i32 0
store i32 -1, i32* %175
br label %176
176: ; preds = %168, %165
br label %177
177: ; preds = %176, %164
%178 = load i32, i32* %14
store i32 %178, i32* %23
%179 = load i32, i32* %14
%180 = load i32, i32* %10
%181 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %179, i32 %180)
%182 = load i32, i32* %10
%183 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 1
%184 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %183, i32 0, i32 0
store i32 %182, i32* %184
%185 = load i32, i32* @EPOLLOUT
%186 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 0
store i32 %185, i32* %186
%187 = load i32, i32* %11
%188 = load i32, i32* @EPOLL_CTL_MOD
%189 = load i32, i32* %10
%190 = call i32 @epoll_ctl(i32 %187, i32 %188, i32 %189, %struct.epoll_event* %21)
br label %228
191: ; preds = %125
%192 = load i32, i32* %6
%193 = sext i32 %192 to i64
%194 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %193
%195 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %194, i32 0, i32 0
%196 = load i32, i32* %195
%197 = load i32, i32* @EPOLLOUT
%198 = and i32 %196, %197
%199 = icmp ne i32 %198, 0
br i1 %199, label %200, label %227
200: ; preds = %191
%201 = load i32, i32* %6
%202 = sext i32 %201 to i64
%203 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %22, i64 0, i64 %202
%204 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %203, i32 0, i32 1
%205 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %204, i32 0, i32 0
%206 = load i32, i32* %205
store i32 %206, i32* %10
%207 = icmp slt i32 %206, 0
br i1 %207, label %208, label %209
208: ; preds = %200
br label %230
209: ; preds = %200
%210 = load i32, i32* %10
%211 = getelementptr inbounds [16384 x i8], [16384 x i8]* %16, i64 0, i64 0
%212 = call i32 @write(i32 %210, i8* %211, i32 16384)
store i32 %212, i32* %14
%213 = load i32, i32* %14
%214 = load i32, i32* %10
%215 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %213, i32 %214)
%216 = load i32, i32* %10
%217 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 1
%218 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %217, i32 0, i32 0
store i32 %216, i32* %218
%219 = load i32, i32* @EPOLLIN
%220 = load i32, i32* @EPOLLET
%221 = or i32 %219, %220
%222 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %21, i32 0, i32 0
store i32 %221, i32* %222
%223 = load i32, i32* %11
%224 = load i32, i32* @EPOLL_CTL_MOD
%225 = load i32, i32* %10
%226 = call i32 @epoll_ctl(i32 %223, i32 %224, i32 %225, %struct.epoll_event* %21)
br label %227
227: ; preds = %209, %191
br label %228
228: ; preds = %227, %177
br label %229
229: ; preds = %228, %105
br label %230
230: ; preds = %229, %208, %143
%231 = load i32, i32* %6
%232 = add nsw i32 %231, 1
store i32 %232, i32* %6
br label %82
233: ; preds = %82
br label %78
234: ; preds = %40, %33
%235 = load i32, i32* %3
ret i32 %235
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage:%s portnumber\07\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.2 = [9 x i8] c"connfd<0\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [40 x i8] c"accapt a connection from %s, socket %d\0A\00"
@.str.4 = [9 x i8] c"EPOLLIN\09\00"
@errno = global i64 0
@ECONNRESET = global i64 0
@.str.5 = [26 x i8] c"socket %d readline error\0A\00"
@.str.6 = [30 x i8] c"read %d bytes from socket %d\0A\00"
@EPOLLOUT = global i32 0
@EPOLL_CTL_MOD = global i32 0
@.str.7 = [29 x i8] c"write %d bytes to socket %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16384 x i8]
%4 = alloca [16384 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.epoll_event
%9 = alloca [20 x %struct.epoll_event]
%10 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%12 = bitcast i32* %5 to i8*
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %123
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %123, label %19
19: ; preds = %14
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = bitcast %struct.sockaddr_in* %6 to i8*
%24 = bitcast %struct.sockaddr_in* %7 to i8*
%25 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
%29 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %28)
%30 = call i32 @htons(i32 %17)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i32 @bind(i32 %22, %struct.sockaddr* %32, i32 12)
%34 = call i32 @listen(i32 %22, i32 20)
%35 = bitcast %struct.epoll_event* %8 to i8*
%36 = bitcast [20 x %struct.epoll_event]* %9 to i8*
%37 = call i32 @epoll_create(i32 256)
%38 = call i32 @setnonblocking(i32 %22)
%39 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i64 0, i32 1, i32 0
store i32 %22, i32* %39
%40 = load i32, i32* @EPOLLIN
%41 = load i32, i32* @EPOLLET
%42 = or i32 %41, %40
%43 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i64 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* @EPOLL_CTL_ADD
%45 = call i32 @epoll_ctl(i32 %37, i32 %44, i32 %22, %struct.epoll_event* %8)
%46 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%47 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %9, i64 0, i64 0
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
br label %49
49: ; preds = %52, %19
%50 = call i32 @epoll_wait(i32 %37, %struct.epoll_event* %47, i32 20, i32 500)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %120, %49
br label %49
53: ; preds = %49
%54 = zext i32 %50 to i64
br label %55
55: ; preds = %53, %120
%56 = phi i64 [ 0, %53 ], [ %121, %120 ]
%57 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %9, i64 0, i64 %56, i32 1, i32 0
%58 = load i32, i32* %57
%59 = icmp eq i32 %58, %22
br i1 %59, label %60, label %75
60: ; preds = %55
%61 = call i32 @accept(i32 %22, %struct.sockaddr* %46, i32* %5)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %60
%68 = call i32 @setnonblocking(i32 %61)
%69 = load i32, i32* %48
%70 = call i8* @inet_ntoa(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %70, i32 %61)
store i32 %61, i32* %39
%72 = load i32, i32* @EPOLLIN
%73 = load i32, i32* @EPOLLET
%74 = or i32 %73, %72
store i32 %74, i32* %43
br label %115
75: ; preds = %55
%76 = getelementptr inbounds [20 x %struct.epoll_event], [20 x %struct.epoll_event]* %9, i64 0, i64 %56, i32 0
%77 = load i32, i32* %76
%78 = load i32, i32* @EPOLLIN
%79 = and i32 %78, %77
%80 = icmp eq i32 %79, 0
br i1 %80, label %103, label %81
81: ; preds = %75
%82 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%83 = load i32, i32* %57
%84 = icmp slt i32 %83, 0
br i1 %84, label %120, label %85
85: ; preds = %81
%86 = call i32 @read(i32 %83, i8* %10, i32 16384)
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %96
88: ; preds = %85
%89 = load i64, i64* @errno
%90 = load i64, i64* @ECONNRESET
%91 = icmp eq i64 %89, %90
br i1 %91, label %92, label %94
92: ; preds = %88
%93 = call i32 @close(i32 %83)
store i32 -1, i32* %57
br label %100
94: ; preds = %88
%95 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %83)
br label %100
96: ; preds = %85
%97 = icmp eq i32 %86, 0
br i1 %97, label %98, label %100
98: ; preds = %96
%99 = call i32 @close(i32 %83)
store i32 -1, i32* %57
br label %100
100: ; preds = %96, %98, %92, %94
%101 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %86, i32 %83)
store i32 %83, i32* %39
%102 = load i32, i32* @EPOLLOUT
store i32 %102, i32* %43
br label %115
103: ; preds = %75
%104 = load i32, i32* @EPOLLOUT
%105 = and i32 %104, %77
%106 = icmp eq i32 %105, 0
%107 = icmp slt i32 %58, 0
%108 = select i1 %106, i1 true, i1 %107
br i1 %108, label %120, label %109
109: ; preds = %103
%110 = call i32 @write(i32 %58, i8* %11, i32 16384)
%111 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %110, i32 %58)
store i32 %58, i32* %39
%112 = load i32, i32* @EPOLLIN
%113 = load i32, i32* @EPOLLET
%114 = or i32 %113, %112
store i32 %114, i32* %43
br label %115
115: ; preds = %100, %109, %67
%116 = phi i32* [ @EPOLL_CTL_ADD, %67 ], [ @EPOLL_CTL_MOD, %109 ], [ @EPOLL_CTL_MOD, %100 ]
%117 = phi i32 [ %61, %67 ], [ %58, %109 ], [ %83, %100 ]
%118 = load i32, i32* %116
%119 = call i32 @epoll_ctl(i32 %37, i32 %118, i32 %117, %struct.epoll_event* %8)
br label %120
120: ; preds = %115, %103, %81
%121 = add nuw nsw i64 %56, 1
%122 = icmp eq i64 %121, %54
br i1 %122, label %52, label %55
123: ; preds = %2, %14
%124 = load i32, i32* @stderr
%125 = load i8*, i8** %1
%126 = tail call i32 @fprintf(i32 %124, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %125)
ret i32 1
}