#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
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;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ client_message;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ INADDR_ANY;
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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
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 {
      fd_set readset;
      struct timeval timeout;
      int ret;
      int nfds;
      timeout.tv_sec = 0;
      timeout.tv_usec = 100000;
      FD_ZERO(&readset);
      FD_SET(socket_desc, &readset);
      nfds = socket_desc + 1;
      ret = select(nfds, &readset, NULL, NULL, &timeout);
      if (ret < 0) {
        perror("select");
        close(socket_desc);
        return 1;
      } else if (ret > 0) {
        if (FD_ISSET(socket_desc, &readset)) {
          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) {
      fd_set readset;
      struct timeval timeout;
      int ret;
      int nfds;
      timeout.tv_sec = 0;
      timeout.tv_usec = 100000;
      FD_ZERO(&readset);
      FD_SET(client_sock, &readset);
      nfds = client_sock + 1;
      ret = select(nfds, &readset, NULL, NULL, &timeout);
      if (ret == -1) {
        perror("select");
        close(client_sock);
        close(socket_desc);
        return 1;
      } else if (FD_ISSET(client_sock, &readset)) {
        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 if (read_size == 0) {
          puts("Client disconnected");
          fflush(stdout);
          close(client_sock);
          break;
        } else {
          perror("recv");
          close(client_sock);
          close(socket_desc);
          return 1;
        }
      } else {
        spin();
      }
    }
  }
  close(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   10a0 <getchar@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    128c <loop+0x6c>
lea    0xda2(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
lea    0xda2(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x22b8,%edi
call   1070 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <bind@plt>
cmp    $0x0,%rax
je     12f8 <loop+0xd8>
lea    0xd65(%rip),%rdi
call   1100 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
lea    0xd52(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
call   10e0 <listen@plt>
cmp    $0x0,%rax
je     133f <loop+0x11f>
lea    0xd35(%rip),%rdi
call   1100 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
jmp    1344 <loop+0x124>
lea    0xd1f(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x186a0,-0x48(%rbp)
lea    -0x34(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   10c0 <select@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    13c8 <loop+0x1a8>
lea    0xcdf(%rip),%rdi
call   1100 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
cmpl   $0x0,-0x4c(%rbp)
jle    13f2 <loop+0x1d2>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13ed <loop+0x1cd>
jmp    1412 <loop+0x1f2>
jmp    13f9 <loop+0x1d9>
mov    $0x0,%al
call   1120 <spin@plt>
jmp    13fe <loop+0x1de>
jmp    1403 <loop+0x1e3>
mov    $0x1,%al
test   $0x1,%al
jne    1357 <loop+0x137>
jmp    1412 <loop+0x1f2>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1469 <loop+0x249>
mov    0x2b8a(%rip),%rax
mov    (%rax),%rax
mov    0x2b68(%rip),%rcx
cmp    (%rcx),%rax
je     1469 <loop+0x249>
lea    0xc45(%rip),%rdi
call   1100 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
lea    0xc34(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x60(%rbp)
movl   $0x186a0,-0x68(%rbp)
lea    -0x54(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x54(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%edi
lea    -0x54(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x68(%rbp),%r8
mov    %rcx,%rdx
call   10c0 <select@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0xffffffff,-0x6c(%rbp)
jne    14ee <loop+0x2ce>
lea    0xbc1(%rip),%rdi
call   1100 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
mov    -0x1c(%rbp),%edi
lea    -0x54(%rbp),%rsi
call   1060 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15d0 <loop+0x3b0>
mov    -0x1c(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x178(%rbp)
cmpq   $0x0,-0x178(%rbp)
jbe    156e <loop+0x34e>
mov    -0x178(%rbp),%rax
movb   $0x0,-0x170(%rbp,%rax,1)
lea    -0x170(%rbp),%rsi
lea    0xb6d(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    -0x178(%rbp),%rdx
call   1050 <write@plt>
jmp    15cb <loop+0x3ab>
cmpq   $0x0,-0x178(%rbp)
jne    15a3 <loop+0x383>
lea    0xb41(%rip),%rdi
call   1040 <puts@plt>
mov    0x2a29(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
jmp    15e1 <loop+0x3c1>
lea    0xb2e(%rip),%rdi
call   1100 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15e6 <loop+0x3c6>
jmp    15d7 <loop+0x3b7>
mov    $0x0,%al
call   1120 <spin@plt>
jmp    15dc <loop+0x3bc>
jmp    1475 <loop+0x255>
jmp    1344 <loop+0x124>
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
lea    0xdb8(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   10b0 <getchar@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2d81(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
cmp    $0xffffffff,%eax
jne    1290 <loop+0x60>
lea    0xd97(%rip),%rdi
call   1110 <perror@plt>
jmp    1513 <loop+0x2e3>
mov    %eax,%r12d
lea    0xd9b(%rip),%rdi
call   1050 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x22b8,%edi
call   1080 <htons@plt>
mov    %eax,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   1100 <bind@plt>
test   %rax,%rax
jne    14ed <loop+0x2bd>
lea    0xd6e(%rip),%rdi
call   1050 <puts@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   10f0 <listen@plt>
test   %rax,%rax
jne    14f6 <loop+0x2c6>
mov    %r12d,%r15d
add    $0x1,%r15d
lea    0x28(%rsp),%rbp
lea    0x50(%rsp),%rbx
lea    0x3c(%rsp),%r13
mov    %r15d,0x10(%rsp)
mov    %r12d,0xc(%rsp)
jmp    135c <loop+0x12c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xd8d(%rip),%rdi
call   1050 <puts@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   10e0 <fflush@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    0xc(%rsp),%r12d
mov    0x10(%rsp),%r15d
lea    0xd07(%rip),%rdi
call   1050 <puts@plt>
movl   $0xc,0x14(%rsp)
jmp    1387 <loop+0x157>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1130 <spin@plt>
movq   $0x0,0x58(%rsp)
movl   $0x186a0,0x50(%rsp)
mov    %rbp,%rdi
call   10c0 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1140 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   10d0 <select@plt>
test   %eax,%eax
js     14ff <loop+0x2cf>
test   %eax,%eax
je     1380 <loop+0x150>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     1387 <loop+0x157>
mov    %r12d,%edi
lea    0x40(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1120 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    140c <loop+0x1dc>
mov    0x2bc7(%rip),%rax
mov    (%rax),%rax
mov    0x2ba5(%rip),%rcx
cmp    (%rcx),%rax
jne    1544 <loop+0x314>
lea    0xc91(%rip),%rdi
call   1050 <puts@plt>
movq   $0x0,0x30(%rsp)
movl   $0x186a0,0x28(%rsp)
mov    %r13,%rdi
call   10c0 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   1140 <FD_SET@plt>
mov    %r14d,%r15d
add    $0x1,%r15d
jmp    147b <loop+0x24b>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1130 <spin@plt>
movq   $0x0,0x30(%rsp)
movl   $0x186a0,0x28(%rsp)
mov    %r13,%rdi
call   10c0 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   1140 <FD_SET@plt>
mov    %r15d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   10d0 <select@plt>
cmp    $0xffffffff,%eax
je     152a <loop+0x2fa>
mov    %r14d,%edi
mov    %r13,%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     1450 <loop+0x220>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1330 <loop+0x100>
mov    %rax,%r12
movb   $0x0,0x50(%rsp,%rax,1)
lea    0xbe9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <write@plt>
jmp    1457 <loop+0x227>
lea    0xb50(%rip),%rdi
jmp    1506 <loop+0x2d6>
lea    0xb5e(%rip),%rdi
jmp    1506 <loop+0x2d6>
lea    0xb88(%rip),%rdi
call   1110 <perror@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
mov    $0x1,%eax
add    $0x158,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb5d(%rip),%rdi
call   1110 <perror@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    0xc(%rsp),%edi
jmp    150e <loop+0x2de>
lea    0xb4a(%rip),%rdi
jmp    1506 <loop+0x2d6>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%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"
@.str.8 = [7 x i8] c"select\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.9 = [15 x i8] c"accept failed.\00"
@.str.10 = [21 x i8] c"\0DConnection accepted\00"
@.str.11 = [11 x i8] c"\0Drecv: %s\0A\00"
@.str.12 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
define i32 @loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca [256 x i8]
%9 = bitcast %struct.sockaddr_in* %1 to i8*
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @getchar()
%12 = tail call i32 @putchar(i32 10)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %121
19: ; preds = %0
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = tail call i32 @htons(i32 8888)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%28 = call i64 @bind(i32 %15, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @close(i32 %15)
br label %121
33: ; preds = %19
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* @SOMAXCONN
%36 = call i64 @listen(i32 %15, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %33
%39 = bitcast i32* %2 to i8*
%40 = bitcast %struct.sockaddr_in* %3 to i8*
%41 = bitcast i32* %4 to i8*
%42 = bitcast %struct.timeval* %5 to i8*
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%45 = add nuw nsw i32 %15, 1
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%47 = bitcast i32* %6 to i8*
%48 = bitcast %struct.timeval* %7 to i8*
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
br label %55
52: ; preds = %33
%53 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%54 = call i32 @close(i32 %15)
br label %121
55: ; preds = %38, %116
%56 = 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 i64 0, i64* %43
store i32 100000, i32* %44
%57 = call i32 @FD_ZERO(i32* %4)
%58 = call i32 @FD_SET(i32 %15, i32* %4)
%59 = call i32 @select(i32 %45, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %55, %72
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%63 = call i32 @close(i32 %15)
br label %115
64: ; preds = %55, %72
%65 = phi i32 [ %75, %72 ], [ %59, %55 ]
%66 = icmp eq i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %64
%68 = call i64 @FD_ISSET(i32 %15, i32* %4)
%69 = icmp eq i64 %68, 0
br i1 %69, label %72, label %77
70: ; preds = %64
%71 = call i32 (...) @spin()
br label %72
72: ; preds = %67, %70
store i64 0, i64* %43
store i32 100000, i32* %44
%73 = call i32 @FD_ZERO(i32* %4)
%74 = call i32 @FD_SET(i32 %15, i32* %4)
%75 = call i32 @select(i32 %45, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%76 = icmp slt i32 %75, 0
br i1 %76, label %61, label %64
77: ; preds = %67
%78 = call i32 @accept(i32 %15, %struct.sockaddr* %46, i32* %2)
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %87
80: ; preds = %77
%81 = load i64, i64* @errno
%82 = load i64, i64* @EAGAIN
%83 = icmp eq i64 %81, %82
br i1 %83, label %87, label %84
84: ; preds = %80
%85 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
%86 = call i32 @close(i32 %15)
br label %115
87: ; preds = %80, %77
%88 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
store i64 0, i64* %49
store i32 100000, i32* %50
%89 = call i32 @FD_ZERO(i32* %6)
%90 = call i32 @FD_SET(i32 %78, i32* %6)
%91 = add nsw i32 %78, 1
%92 = call i32 @select(i32 %91, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %98
94: ; preds = %87, %110
%95 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%96 = call i32 @close(i32 %78)
%97 = call i32 @close(i32 %15)
br label %115
98: ; preds = %87, %110
%99 = call i64 @FD_ISSET(i32 %78, i32* %6)
%100 = icmp eq i64 %99, 0
br i1 %100, label %108, label %101
101: ; preds = %98
%102 = call i64 @recv(i32 %78, i8* %51, i32 256, i32 0)
%103 = icmp eq i64 %102, 0
br i1 %103, label %116, label %104
104: ; preds = %101
%105 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 %102
store i8 0, i8* %105
%106 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %51)
%107 = call i32 @write(i32 %78, i8* %51, i64 %102)
br label %110
108: ; preds = %98
%109 = call i32 (...) @spin()
br label %110
110: ; preds = %108, %104
store i64 0, i64* %49
store i32 100000, i32* %50
%111 = call i32 @FD_ZERO(i32* %6)
%112 = call i32 @FD_SET(i32 %78, i32* %6)
%113 = call i32 @select(i32 %91, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%114 = icmp eq i32 %113, -1
br i1 %114, label %94, label %98
115: ; preds = %84, %61, %94
br label %121
116: ; preds = %101
%117 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
%118 = load i32, i32* @stdout
%119 = call i32 @fflush(i32 %118)
%120 = call i32 @close(i32 %78)
br label %55
121: ; preds = %115, %52, %30, %17
ret i32 1
}