#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;
};
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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
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 fcntl(int, int /*<<< orphan*/, ...);
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 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;
  int ret;
  int flags;
  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");
  flags = fcntl(socket_desc, F_GETFL);
  if (flags == -1) {
    perror("fcntl(*, F_GETFL)");
    close(socket_desc);
    return 1;
  }
  flags |= O_NONBLOCK;
  ret = fcntl(socket_desc, F_SETFL, flags);
  if (ret == -1) {
    perror("fcntl(*, F_SETFL, O_NONBLOCK)");
    close(socket_desc);
    return 1;
  }
  printf("status: %08x\n", flags);
  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 {
      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;
      } else {
        spin();
      }
    } while (client_sock <= 0);
    puts("\rConnection accepted");
    flags = fcntl(client_sock, F_GETFL);
    if (flags == -1) {
      perror("fcntl(*, F_GETFL)");
      close(socket_desc);
      close(client_sock);
      return 1;
    }
    flags |= O_NONBLOCK;
    ret = fcntl(client_sock, F_SETFL, O_NONBLOCK);
    if (ret == -1) {
      perror("fcntl(*, F_SETFL, O_NONBLOCK)");
      close(socket_desc);
      close(client_sock);
      return 1;
    }
    while (1) {
      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 if ((read_size == -1) && (errno != EAGAIN)) {
        perror("recv failed");
        close(socket_desc);
        close(client_sock);
        return 1;
      } else {
        spin();
      }
    }
  }
  close(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   10a0 <getchar@plt>
lea    0xee2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%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    15fa <loop+0x40a>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12a8 <loop+0xb8>
lea    0xdb5(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12fa <loop+0x10a>
lea    0xd75(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
mov    -0x20(%rbp),%esi
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2cba(%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   10d0 <bind@plt>
cmp    $0x0,%rax
je     136b <loop+0x17b>
lea    0xd30(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
lea    0xd1d(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
je     13b2 <loop+0x1c2>
lea    0xd00(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
jmp    13b7 <loop+0x1c7>
lea    0xcea(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1421 <loop+0x231>
mov    0x2bc2(%rip),%rax
mov    (%rax),%rax
mov    0x2b98(%rip),%rcx
cmp    (%rcx),%rax
je     1421 <loop+0x231>
lea    0xcc4(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
mov    $0x0,%al
call   1100 <spin@plt>
jmp    142d <loop+0x23d>
cmpl   $0x0,-0x24(%rbp)
jle    13ca <loop+0x1da>
lea    0xc9d(%rip),%rdi
call   1040 <puts@plt>
mov    -0x24(%rbp),%edi
mov    0x2b5b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    148b <loop+0x29b>
lea    0xbda(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
mov    0x2b4e(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2b1c(%rip),%rax
mov    (%rax),%esi
mov    0x2b33(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    14eb <loop+0x2fb>
lea    0xb8c(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
jmp    14f0 <loop+0x300>
mov    -0x24(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x148(%rbp)
cmpq   $0x0,-0x148(%rbp)
jbe    155a <loop+0x36a>
mov    -0x148(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rsi
lea    0xbb8(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    -0x148(%rbp),%rdx
call   1060 <write@plt>
jmp    15f0 <loop+0x400>
cmpq   $0x0,-0x148(%rbp)
jne    158f <loop+0x39f>
lea    0xb8c(%rip),%rdi
call   1040 <puts@plt>
mov    0x2a25(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
jmp    15f5 <loop+0x405>
cmpq   $0xffffffffffffffff,-0x148(%rbp)
ff
jne    15df <loop+0x3ef>
mov    0x2a0c(%rip),%rax
mov    (%rax),%rax
mov    0x29e2(%rip),%rcx
cmp    (%rcx),%rax
je     15df <loop+0x3ef>
lea    0xb51(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    15fa <loop+0x40a>
mov    $0x0,%al
call   1100 <spin@plt>
jmp    15eb <loop+0x3fb>
jmp    15f0 <loop+0x400>
jmp    14f0 <loop+0x300>
jmp    13b7 <loop+0x1c7>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   10b0 <getchar@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2db1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
jne    1260 <loop+0x60>
lea    0xdc7(%rip),%rdi
call   10f0 <perror@plt>
jmp    14c4 <loop+0x2c4>
mov    %eax,%r13d
lea    0xdcb(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1293 <loop+0x93>
lea    0xdb6(%rip),%rdi
jmp    14b7 <loop+0x2b7>
mov    %eax,%ebx
mov    0x2d44(%rip),%rax
or     (%rax),%ebx
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12c4 <loop+0xc4>
lea    0xd97(%rip),%rdi
jmp    14b7 <loop+0x2b7>
lea    0xda9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x22b8,%edi
call   1080 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jne    149e <loop+0x29e>
lea    0xd77(%rip),%rdi
call   1050 <puts@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   10d0 <listen@plt>
test   %rax,%rax
jne    14a7 <loop+0x2a7>
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%rbp
mov    0x2c68(%rip),%r14
lea    0x30(%rsp),%r12
jmp    1380 <loop+0x180>
nop
lea    0xda4(%rip),%rdi
call   1050 <puts@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    0x2c38(%rip),%r14
mov    %rbp,%r15
lea    0xc(%rsp),%rbp
lea    0xd21(%rip),%rdi
call   1050 <puts@plt>
movl   $0xc,0xc(%rsp)
jmp    13ab <loop+0x1ab>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1110 <spin@plt>
test   %ebx,%ebx
jg     13e0 <loop+0x1e0>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1100 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    13a0 <loop+0x1a0>
mov    (%r14),%rax
mov    0x2bc7(%rip),%rcx
cmp    (%rcx),%rax
je     13a0 <loop+0x1a0>
jmp    14b0 <loop+0x2b0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xcf4(%rip),%rdi
call   1050 <puts@plt>
mov    0x2bb5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
je     14db <loop+0x2db>
mov    0x2bd2(%rip),%rax
mov    (%rax),%edx
mov    0x2ba9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
je     14e4 <loop+0x2e4>
mov    %r15,%rbp
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%r14
test   %rax,%rax
lea    0xca4(%rip),%r15
je     1350 <loop+0x150>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,0x30(%rsp,%r14,1)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1070 <write@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1460 <loop+0x260>
jmp    1350 <loop+0x150>
lea    0xbdd(%rip),%rdi
jmp    14b7 <loop+0x2b7>
lea    0xbeb(%rip),%rdi
jmp    14b7 <loop+0x2b7>
lea    0xc15(%rip),%rdi
call   10f0 <perror@plt>
mov    %r13d,%edi
call   10a0 <close@plt>
mov    $0x1,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb62(%rip),%rdi
jmp    14eb <loop+0x2eb>
lea    0xb6b(%rip),%rdi
call   10f0 <perror@plt>
mov    %r13d,%edi
call   10a0 <close@plt>
mov    %ebx,%r13d
jmp    14bc <loop+0x2bc>

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

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