#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 /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client_message;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
int /*<<< orphan*/ INADDR_ANY;
int POLLERR;
int POLLHUP;
int POLLIN;
int POLLNVAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ spin();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, size_t);
int loop(void) {
  int socket_desc;
  struct sockaddr_in server;
  printf("Press any key to continue...");
  getchar();
  printf("\n");
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    perror("Could not create socket");
    return 1;
  }
  puts("Socket created");
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8888);
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) != 0) {
    perror("bind failed.");
    close(socket_desc);
    return 1;
  }
  puts("bind done");
  if (listen(socket_desc, SOMAXCONN) != 0) {
    perror("listen failed.");
    close(socket_desc);
    return 1;
  }
  while (1) {
    int client_sock, c;
    struct sockaddr_in client;
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    do {
      struct pollfd pfd;
      int ret;
      pfd.fd = socket_desc;
      pfd.events = POLLIN;
      ret = poll(&pfd, 1, 100);
      if (ret < 0) {
        perror("poll");
        close(socket_desc);
        return 1;
      } else if (ret > 0) {
        if (pfd.revents & POLLNVAL) {
          perror("POLLNVAL");
          close(socket_desc);
          return 1;
        }
        if (pfd.revents & POLLIN) {
          break;
        }
      } else {
        spin();
      }
    } while (1);
    client_sock =
        accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
    if (client_sock < 0 && (errno != EAGAIN)) {
      perror("accept failed.");
      close(socket_desc);
      return 1;
    }
    puts("\rConnection accepted");
    while (1) {
      struct pollfd pfd;
      int ret;
      pfd.fd = client_sock;
      pfd.events = POLLIN;
      ret = poll(&pfd, 1, 100);
      if ((pfd.revents & POLLNVAL) || (ret == -1)) {
        perror("recv failed");
        close(socket_desc);
        close(client_sock);
        return 1;
      } else if (pfd.revents & (POLLHUP | POLLERR)) {
        puts("Client disconnected");
        fflush(stdout);
        close(client_sock);
        break;
      } else if (pfd.revents & POLLIN) {
        char client_message[256];
        ssize_t read_size;
        read_size =
            recv(client_sock, client_message, sizeof(client_message), 0);
        if (read_size > 0) {
          client_message[read_size] = '\0';
          printf("\rrecv: %s\n", client_message);
          write(client_sock, client_message, read_size);
        }
      } else {
        spin();
      }
    }
  }
  close(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    125c <loop+0x6c>
lea    0xdd2(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     12c8 <loop+0xd8>
lea    0xd95(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
lea    0xd82(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    0x2cd2(%rip),%rax
mov    (%rax),%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     130f <loop+0x11f>
lea    0xd65(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
jmp    1314 <loop+0x124>
lea    0xd4f(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10c0 <poll@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1379 <loop+0x189>
lea    0xd2e(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
cmpl   $0x0,-0x44(%rbp)
jle    13d7 <loop+0x1e7>
mov    -0x38(%rbp),%eax
mov    0x2c43(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13b8 <loop+0x1c8>
lea    0xcf4(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
mov    -0x38(%rbp),%eax
mov    0x2bfe(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13d2 <loop+0x1e2>
jmp    13f7 <loop+0x207>
jmp    13de <loop+0x1ee>
mov    $0x0,%al
call   1100 <spin@plt>
jmp    13e3 <loop+0x1f3>
jmp    13e8 <loop+0x1f8>
mov    $0x1,%al
test   $0x1,%al
jne    1327 <loop+0x137>
jmp    13f7 <loop+0x207>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    144e <loop+0x25e>
mov    0x2b85(%rip),%rax
mov    (%rax),%rax
mov    0x2b63(%rip),%rcx
cmp    (%rcx),%rax
je     144e <loop+0x25e>
lea    0xc67(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
lea    0xc56(%rip),%rdi
call   1040 <puts@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2b59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10c0 <poll@plt>
mov    %eax,-0x54(%rbp)
mov    -0x48(%rbp),%eax
mov    0x2b44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    14a1 <loop+0x2b1>
cmpl   $0xffffffff,-0x54(%rbp)
jne    14c9 <loop+0x2d9>
lea    0xc18(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15aa <loop+0x3ba>
mov    -0x48(%rbp),%eax
mov    0x2b15(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2afc(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1510 <loop+0x320>
lea    0xbdc(%rip),%rdi
call   1040 <puts@plt>
mov    0x2a9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    15a5 <loop+0x3b5>
mov    -0x48(%rbp),%eax
mov    0x2aa6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     158f <loop+0x39f>
mov    -0x1c(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x168(%rbp)
cmpq   $0x0,-0x168(%rbp)
jbe    158a <loop+0x39a>
mov    -0x168(%rbp),%rax
movb   $0x0,-0x160(%rbp,%rax,1)
lea    -0x160(%rbp),%rsi
lea    0xb73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    -0x168(%rbp),%rdx
call   1050 <write@plt>
jmp    1596 <loop+0x3a6>
mov    $0x0,%al
call   1100 <spin@plt>
jmp    159b <loop+0x3ab>
jmp    15a0 <loop+0x3b0>
jmp    145a <loop+0x26a>
jmp    1314 <loop+0x124>
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2da9(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
jne    125f <loop+0x5f>
lea    0xdc8(%rip),%rdi
call   10f0 <perror@plt>
jmp    1503 <loop+0x303>
mov    %eax,%r12d
lea    0xdcc(%rip),%rdi
call   1050 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x28(%rsp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    $0x22b8,%edi
call   1070 <htons@plt>
mov    %eax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jne    14e6 <loop+0x2e6>
lea    0xda0(%rip),%rdi
call   1050 <puts@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   10c0 <listen@plt>
test   %rax,%rax
jne    14ef <loop+0x2ef>
lea    0xd93(%rip),%rdi
call   1050 <puts@plt>
movl   $0xc,0xc(%rsp)
mov    %r12d,0x40(%rsp)
mov    0x2cd0(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x44(%rsp)
lea    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10d0 <poll@plt>
lea    0xd7c(%rip),%rdi
test   %eax,%eax
js     14f6 <loop+0x2f6>
lea    0x40(%rsp),%rbx
mov    0x2caa(%rip),%r15
lea    0x10(%rsp),%r14
mov    %r12d,0x8(%rsp)
jmp    13a1 <loop+0x1a1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xd85(%rip),%rdi
call   1050 <puts@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    %r12d,%edi
call   1090 <close@plt>
lea    0xd01(%rip),%rdi
call   1050 <puts@plt>
movl   $0xc,0xc(%rsp)
mov    0x8(%rsp),%r12d
mov    %r12d,0x40(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x44(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10d0 <poll@plt>
test   %eax,%eax
js     14dd <loop+0x2dd>
test   %eax,%eax
je     14b0 <loop+0x2b0>
mov    0x48(%rsp),%ecx
test   %ecx,(%r15)
jne    1531 <loop+0x331>
mov    0x0(%rbp),%eax
test   %ecx,%eax
je     14ba <loop+0x2ba>
mov    %r12d,%edi
lea    0x30(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1100 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    13f4 <loop+0x1f4>
mov    0x2bbf(%rip),%rax
mov    (%rax),%rax
mov    0x2b9d(%rip),%rcx
cmp    (%rcx),%rax
jne    153a <loop+0x33a>
lea    0xcb0(%rip),%rdi
call   1050 <puts@plt>
jmp    1417 <loop+0x217>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1110 <spin@plt>
mov    %r12d,0x10(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10d0 <poll@plt>
mov    0x18(%rsp),%ecx
test   %ecx,(%r15)
jne    151a <loop+0x31a>
cmp    $0xffffffff,%eax
je     151a <loop+0x31a>
mov    0x2b86(%rip),%rax
mov    (%rax),%eax
mov    0x2b8d(%rip),%rdx
or     (%rdx),%eax
test   %ecx,%eax
jne    1340 <loop+0x140>
test   %ecx,0x0(%rbp)
je     1410 <loop+0x210>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1417 <loop+0x217>
mov    %rax,%r13
movb   $0x0,0x40(%rsp,%rax,1)
lea    0xc50(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <write@plt>
jmp    1417 <loop+0x217>
nopl   (%rax)
xor    %eax,%eax
call   1110 <spin@plt>
mov    0x0(%rbp),%eax
mov    %r12d,0x40(%rsp)
mov    %eax,0x44(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   10d0 <poll@plt>
test   %eax,%eax
jns    13a1 <loop+0x1a1>
lea    0xbaa(%rip),%rdi
jmp    14f6 <loop+0x2f6>
lea    0xb57(%rip),%rdi
jmp    14f6 <loop+0x2f6>
lea    0xb65(%rip),%rdi
call   10f0 <perror@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    $0x1,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb9f(%rip),%rdi
call   10f0 <perror@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
jmp    14fb <loop+0x2fb>
lea    0xb5b(%rip),%rdi
jmp    14f6 <loop+0x2f6>
lea    0xb5b(%rip),%rdi
call   10f0 <perror@plt>
mov    0x8(%rsp),%edi
jmp    14fe <loop+0x2fe>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"bind failed.\00"
@.str.5 = [10 x i8] c"bind done\00"
@SOMAXCONN = global i32 0
@.str.6 = [15 x i8] c"listen failed.\00"
@.str.7 = [36 x i8] c"Waiting for incoming connections...\00"
@POLLIN = global i32 0
@.str.8 = [5 x i8] c"poll\00"
@POLLNVAL = global i32 0
@.str.9 = [9 x i8] c"POLLNVAL\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.10 = [15 x i8] c"accept failed.\00"
@.str.11 = [21 x i8] c"\0DConnection accepted\00"
@.str.12 = [12 x i8] c"recv failed\00"
@POLLHUP = global i32 0
@POLLERR = global i32 0
@.str.13 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.14 = [11 x i8] c"\0Drecv: %s\0A\00"
define i32 @loop() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.pollfd
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i64
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (...) @getchar()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %169
23: ; preds = %0
%24 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = call i32 @htons(i32 8888)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %2
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %23
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
store i32 1, i32* %1
br label %169
40: ; preds = %23
%41 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* %2
%43 = load i32, i32* @SOMAXCONN
%44 = call i64 @listen(i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 1, i32* %1
br label %169
50: ; preds = %40
br label %51
51: ; preds = %50, %168
%52 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
store i32 12, i32* %5
br label %53
53: ; preds = %90, %51
%54 = load i32, i32* %2
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %54, i32* %55
%56 = load i32, i32* @POLLIN
%57 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
store i32 %56, i32* %57
%58 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 100)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
store i32 1, i32* %1
br label %169
65: ; preds = %53
%66 = load i32, i32* %8
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %86
68: ; preds = %65
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
%70 = load i32, i32* %69
%71 = load i32, i32* @POLLNVAL
%72 = and i32 %70, %71
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %68
%75 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%76 = load i32, i32* %2
%77 = call i32 @close(i32 %76)
store i32 1, i32* %1
br label %169
78: ; preds = %68
%79 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
%80 = load i32, i32* %79
%81 = load i32, i32* @POLLIN
%82 = and i32 %80, %81
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %78
br label %91
85: ; preds = %78
br label %88
86: ; preds = %65
%87 = call i32 (...) @spin()
br label %88
88: ; preds = %86, %85
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89
br i1 true, label %53, label %91
91: ; preds = %90, %84
%92 = load i32, i32* %2
%93 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%94 = call i32 @accept(i32 %92, %struct.sockaddr* %93, i32* %5)
store i32 %94, i32* %4
%95 = load i32, i32* %4
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %105
97: ; preds = %91
%98 = load i64, i64* @errno
%99 = load i64, i64* @EAGAIN
%100 = icmp ne i64 %98, %99
br i1 %100, label %101, label %105
101: ; preds = %97
%102 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%103 = load i32, i32* %2
%104 = call i32 @close(i32 %103)
store i32 1, i32* %1
br label %169
105: ; preds = %97, %91
%106 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0))
br label %107
107: ; preds = %105, %167
%108 = load i32, i32* %4
%109 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %108, i32* %109
%110 = load i32, i32* @POLLIN
%111 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %110, i32* %111
%112 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 100)
store i32 %112, i32* %10
%113 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%114 = load i32, i32* %113
%115 = load i32, i32* @POLLNVAL
%116 = and i32 %114, %115
%117 = icmp ne i32 %116, 0
br i1 %117, label %121, label %118
118: ; preds = %107
%119 = load i32, i32* %10
%120 = icmp eq i32 %119, -1
br i1 %120, label %121, label %127
121: ; preds = %118, %107
%122 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0))
%123 = load i32, i32* %2
%124 = call i32 @close(i32 %123)
%125 = load i32, i32* %4
%126 = call i32 @close(i32 %125)
store i32 1, i32* %1
br label %169
127: ; preds = %118
%128 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%129 = load i32, i32* %128
%130 = load i32, i32* @POLLHUP
%131 = load i32, i32* @POLLERR
%132 = or i32 %130, %131
%133 = and i32 %129, %132
%134 = icmp ne i32 %133, 0
br i1 %134, label %135, label %141
135: ; preds = %127
%136 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.13, i64 0, i64 0))
%137 = load i32, i32* @stdout
%138 = call i32 @fflush(i32 %137)
%139 = load i32, i32* %4
%140 = call i32 @close(i32 %139)
br label %168
141: ; preds = %127
%142 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%143 = load i32, i32* %142
%144 = load i32, i32* @POLLIN
%145 = and i32 %143, %144
%146 = icmp ne i32 %145, 0
br i1 %146, label %147, label %163
147: ; preds = %141
%148 = load i32, i32* %4
%149 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%150 = call i64 @recv(i32 %148, i8* %149, i32 256, i32 0)
store i64 %150, i64* %12
%151 = load i64, i64* %12
%152 = icmp ugt i64 %151, 0
br i1 %152, label %153, label %162
153: ; preds = %147
%154 = load i64, i64* %12
%155 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %154
store i8 0, i8* %155
%156 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%157 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i8* %156)
%158 = load i32, i32* %4
%159 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%160 = load i64, i64* %12
%161 = call i32 @write(i32 %158, i8* %159, i64 %160)
br label %162
162: ; preds = %153, %147
br label %165
163: ; preds = %141
%164 = call i32 (...) @spin()
br label %165
165: ; preds = %163, %162
br label %166
166: ; preds = %165
br label %167
167: ; preds = %166
br label %107
168: ; preds = %135
br label %51
169: ; preds = %121, %101, %74, %61, %46, %36, %21
%170 = load i32, i32* %1
ret i32 %170
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"bind failed.\00"
@.str.5 = [10 x i8] c"bind done\00"
@SOMAXCONN = global i32 0
@.str.6 = [15 x i8] c"listen failed.\00"
@.str.7 = [36 x i8] c"Waiting for incoming connections...\00"
@POLLIN = global i32 0
@.str.8 = [5 x i8] c"poll\00"
@POLLNVAL = global i32 0
@.str.9 = [9 x i8] c"POLLNVAL\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.10 = [15 x i8] c"accept failed.\00"
@.str.11 = [21 x i8] c"\0DConnection accepted\00"
@.str.12 = [12 x i8] c"recv failed\00"
@POLLHUP = global i32 0
@POLLERR = global i32 0
@.str.13 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.14 = [11 x i8] c"\0Drecv: %s\0A\00"
define i32 @loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.pollfd
%5 = alloca %struct.pollfd
%6 = alloca [256 x i8]
%7 = bitcast %struct.sockaddr_in* %1 to i8*
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 (...) @getchar()
%10 = tail call i32 @putchar(i32 10)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %146
17: ; preds = %0
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 8888)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i64 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @close(i32 %13)
br label %146
31: ; preds = %17
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @SOMAXCONN
%34 = call i64 @listen(i32 %13, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %54
36: ; preds = %31
%37 = bitcast i32* %2 to i8*
%38 = bitcast %struct.sockaddr_in* %3 to i8*
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
store i32 12, i32* %2
%40 = bitcast %struct.pollfd* %4 to i8*
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %13, i32* %41
%43 = load i32, i32* @POLLIN
store i32 %43, i32* %42
%44 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 100)
%45 = icmp slt i32 %44, 0
br i1 %45, label %72, label %46
46: ; preds = %36
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
%48 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%49 = bitcast %struct.pollfd* %5 to i8*
%50 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
br label %57
54: ; preds = %31
%55 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @close(i32 %13)
br label %146
57: ; preds = %80, %46
%58 = phi i32 [ %44, %46 ], [ %81, %80 ]
%59 = icmp eq i32 %58, 0
br i1 %59, label %69, label %60
60: ; preds = %57
%61 = load i32, i32* %47
%62 = load i32, i32* @POLLNVAL
%63 = and i32 %62, %61
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %60
%66 = load i32, i32* @POLLIN
%67 = and i32 %66, %61
%68 = icmp eq i32 %67, 0
br i1 %68, label %76, label %82
69: ; preds = %57
%70 = call i32 (...) @spin()
%71 = load i32, i32* @POLLIN
br label %76
72: ; preds = %137, %60, %76, %36
%73 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %36 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %137 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %76 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), %60 ]
%74 = call i32 @perror(i8* %73)
%75 = call i32 @close(i32 %13)
br label %136
76: ; preds = %65, %69
%77 = phi i32 [ %66, %65 ], [ %71, %69 ]
store i32 %13, i32* %41
store i32 %77, i32* %42
%78 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 100)
%79 = icmp slt i32 %78, 0
br i1 %79, label %72, label %80
80: ; preds = %76, %137
%81 = phi i32 [ %78, %76 ], [ %144, %137 ]
br label %57
82: ; preds = %65
%83 = call i32 @accept(i32 %13, %struct.sockaddr* %48, i32* %2)
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %92
85: ; preds = %82
%86 = load i64, i64* @errno
%87 = load i64, i64* @EAGAIN
%88 = icmp eq i64 %86, %87
br i1 %88, label %92, label %89
89: ; preds = %85
%90 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%91 = call i32 @close(i32 %13)
br label %136
92: ; preds = %85, %82
%93 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0))
store i32 %83, i32* %50
%94 = load i32, i32* @POLLIN
store i32 %94, i32* %51
%95 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 100)
%96 = load i32, i32* %52
%97 = load i32, i32* @POLLNVAL
%98 = and i32 %97, %96
%99 = icmp ne i32 %98, 0
%100 = icmp eq i32 %95, -1
%101 = select i1 %99, i1 true, i1 %100
br i1 %101, label %102, label %106
102: ; preds = %92, %127
%103 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0))
%104 = call i32 @close(i32 %13)
%105 = call i32 @close(i32 %83)
br label %136
106: ; preds = %92, %127
%107 = phi i32 [ %130, %127 ], [ %96, %92 ]
%108 = load i32, i32* @POLLHUP
%109 = load i32, i32* @POLLERR
%110 = or i32 %109, %108
%111 = and i32 %110, %107
%112 = icmp eq i32 %111, 0
br i1 %112, label %113, label %137
113: ; preds = %106
%114 = load i32, i32* @POLLIN
%115 = and i32 %114, %107
%116 = icmp eq i32 %115, 0
br i1 %116, label %125, label %117
117: ; preds = %113
%118 = call i64 @recv(i32 %83, i8* %53, i32 256, i32 0)
%119 = icmp eq i64 %118, 0
br i1 %119, label %124, label %120
120: ; preds = %117
%121 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %118
store i8 0, i8* %121
%122 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i8* %53)
%123 = call i32 @write(i32 %83, i8* %53, i64 %118)
br label %124
124: ; preds = %120, %117
br label %127
125: ; preds = %113
%126 = call i32 (...) @spin()
br label %127
127: ; preds = %124, %125
store i32 %83, i32* %50
%128 = load i32, i32* @POLLIN
store i32 %128, i32* %51
%129 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 100)
%130 = load i32, i32* %52
%131 = load i32, i32* @POLLNVAL
%132 = and i32 %131, %130
%133 = icmp ne i32 %132, 0
%134 = icmp eq i32 %129, -1
%135 = select i1 %133, i1 true, i1 %134
br i1 %135, label %102, label %106
136: ; preds = %89, %72, %102
br label %146
137: ; preds = %106
%138 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.13, i64 0, i64 0))
%139 = load i32, i32* @stdout
%140 = call i32 @fflush(i32 %139)
%141 = call i32 @close(i32 %83)
%142 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
store i32 12, i32* %2
store i32 %13, i32* %41
%143 = load i32, i32* @POLLIN
store i32 %143, i32* %42
%144 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 100)
%145 = icmp slt i32 %144, 0
br i1 %145, label %72, label %80
146: ; preds = %136, %54, %28, %15
ret i32 1
}