#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ clientname;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int INET_ADDRSTRLEN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
scalar_t__ listen(int, int);
int make_socket(int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read_from_client(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(void) {
  int sock;
  fd_set active_fd_set, read_fd_set;
  int i;
  struct sockaddr_in clientname;
  socklen_t size;
  sock = make_socket(5555);
  if (listen(sock, 1) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  FD_ZERO(&active_fd_set);
  FD_SET(sock, &active_fd_set);
  while (1) {
    read_fd_set = active_fd_set;
    if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
      perror("select");
      exit(EXIT_FAILURE);
    }
    for (i = 0; i < FD_SETSIZE; ++i) {
      if (!FD_ISSET(i, &read_fd_set))
        continue;
      if (i == sock) {
        int new;
        size = sizeof(clientname);
        new = accept(sock, (struct sockaddr *)&clientname, &size);
        if (new < 0) {
          perror("accept");
          exit(EXIT_FAILURE);
        }
        char str[INET_ADDRSTRLEN];
        inet_ntop(AF_INET, &clientname.sin_addr, str, INET_ADDRSTRLEN);
        fprintf(stderr, "Server: connect from host %s, port %u.\n", str,
                ntohs(clientname.sin_port));
        FD_SET(new, &active_fd_set);
      } else {
        if (read_from_client(i) < 0) {
          close(i);
          FD_CLR(i, &active_fd_set);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x15b3,%edi
call   10b0 <make_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    123d <main+0x4d>
lea    0xdd6(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    1299 <main+0xa9>
lea    0xd81(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d26(%rip),%rcx
cmp    (%rcx),%eax
jge    13e0 <main+0x1f0>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    12cc <main+0xdc>
jmp    13d2 <main+0x1e2>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    13a2 <main+0x1b2>
movl   $0x8,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1316 <main+0x126>
lea    0xd0b(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    0x2c6e(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xc90(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    13cd <main+0x1dd>
mov    -0x14(%rbp),%edi
call   10f0 <read_from_client@plt>
cmp    $0x0,%rax
jge    13c8 <main+0x1d8>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10e0 <FD_CLR@plt>
jmp    13cd <main+0x1dd>
jmp    13d2 <main+0x1e2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a0 <main+0xb0>
jmp    1252 <main+0x62>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x15b3,%edi
call   10b0 <make_socket@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    122b <main+0x3b>
lea    0xdda(%rip),%rdi
jmp    13a9 <main+0x1b9>
lea    -0x30(%rbp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1110 <FD_SET@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d81(%rip),%rbx
mov    (%rbx),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jns    1272 <main+0x82>
lea    0xd9a(%rip),%rdi
jmp    13a9 <main+0x1b9>
lea    -0x2c(%rbp),%r12
mov    %r15d,-0x34(%rbp)
jmp    129a <main+0xaa>
nopl   0x0(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
js     1266 <main+0x76>
mov    (%rbx),%edi
test   %edi,%edi
jle    1280 <main+0x90>
xor    %r13d,%r13d
jmp    12db <main+0xeb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
call   10f0 <read_from_client@plt>
test   %rax,%rax
js     1389 <main+0x199>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
mov    (%rbx),%edi
cmp    %edi,%r13d
jge    1280 <main+0x90>
mov    %r13d,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     12d0 <main+0xe0>
cmp    %r13d,%r15d
jne    12b0 <main+0xc0>
movl   $0x8,-0x38(%rbp)
mov    %r15d,%edi
lea    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   10d0 <accept@plt>
test   %eax,%eax
js     13a2 <main+0x1b2>
mov    %eax,%r14d
mov    %rsp,-0x48(%rbp)
mov    0x2cac(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
mov    (%rcx),%ecx
lea    -0x3c(%rbp),%rsi
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%r15d
mov    -0x40(%rbp),%edi
call   10a0 <ntohs@plt>
mov    %r15d,%edi
lea    0xcb3(%rip),%rsi
mov    %r12,%rdx
lea    -0x2c(%rbp),%r12
mov    %eax,%ecx
call   1060 <fprintf@plt>
mov    %r14d,%edi
lea    -0x30(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x48(%rbp),%rsp
mov    -0x34(%rbp),%r15d
jmp    12d0 <main+0xe0>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %r13d,%edi
lea    -0x30(%rbp),%rsi
call   10e0 <FD_CLR@plt>
jmp    12d0 <main+0xe0>
lea    0xc65(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [7 x i8] c"accept\00"
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str.3 = [40 x i8] c"Server: connect from host %s, port %u.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %1
%11 = call i32 @make_socket(i32 5555)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = call i64 @listen(i32 %12, i32 1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
%20 = call i32 @FD_ZERO(i32* %3)
%21 = load i32, i32* %2
%22 = call i32 @FD_SET(i32 %21, i32* %3)
br label %23
23: ; preds = %19, %87
%24 = load i32, i32* %3
store i32 %24, i32* %4
%25 = load i32, i32* @FD_SETSIZE
%26 = call i64 @select(i32 %25, i32* %4, i32* null, i32* null, i32* null)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
store i32 0, i32* %5
br label %33
33: ; preds = %84, %32
%34 = load i32, i32* %5
%35 = load i32, i32* @FD_SETSIZE
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %87
37: ; preds = %33
%38 = load i32, i32* %5
%39 = call i32 @FD_ISSET(i32 %38, i32* %4)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %37
br label %84
42: ; preds = %37
%43 = load i32, i32* %5
%44 = load i32, i32* %2
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %73
46: ; preds = %42
store i32 8, i32* %7
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %7)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %46
%57 = load i32, i32* @INET_ADDRSTRLEN
%58 = zext i32 %57 to i64
%59 = call i8* @llvm.stacksave()
store i8* %59, i8** %9
%60 = alloca i8, i64 %58
store i64 %58, i64* %10
%61 = load i32, i32* @AF_INET
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%63 = load i32, i32* @INET_ADDRSTRLEN
%64 = call i32 @inet_ntop(i32 %61, i32* %62, i8* %60, i32 %63)
%65 = load i32, i32* @stderr
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 @ntohs(i32 %67)
%69 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %60, i32 %68)
%70 = load i32, i32* %8
%71 = call i32 @FD_SET(i32 %70, i32* %3)
%72 = load i8*, i8** %9
br label %83
73: ; preds = %42
%74 = load i32, i32* %5
%75 = call i64 @read_from_client(i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %73
%78 = load i32, i32* %5
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %5
%81 = call i32 @FD_CLR(i32 %80, i32* %3)
br label %82
82: ; preds = %77, %73
br label %83
83: ; preds = %82, %56
br label %84
84: ; preds = %83, %41
%85 = load i32, i32* %5
%86 = add nsw i32 %85, 1
store i32 %86, i32* %5
br label %33
87: ; preds = %33
br label %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [7 x i8] c"accept\00"
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str.3 = [40 x i8] c"Server: connect from host %s, port %u.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @make_socket(i32 5555)
%10 = tail call i64 @listen(i32 %9, i32 1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = call i32 @FD_ZERO(i32* %1)
%18 = call i32 @FD_SET(i32 %9, i32* %1)
%19 = load i32, i32* %1
store i32 %19, i32* %2
%20 = load i32, i32* @FD_SETSIZE
%21 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %16
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %32
27: ; preds = %71, %32
%28 = phi i32 [ %33, %32 ], [ %73, %71 ]
%29 = load i32, i32* %1
store i32 %29, i32* %2
%30 = call i64 @select(i32 %28, i32* %2, i32* null, i32* null, i32* null)
%31 = icmp slt i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %23, %27
%33 = load i32, i32* @FD_SETSIZE
%34 = icmp sgt i32 %33, 0
br i1 %34, label %39, label %27
35: ; preds = %27, %16
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %32, %71
%40 = phi i32 [ %72, %71 ], [ 0, %32 ]
%41 = call i32 @FD_ISSET(i32 %40, i32* %2)
%42 = icmp eq i32 %41, 0
br i1 %42, label %71, label %43
43: ; preds = %39
%44 = icmp eq i32 %40, %9
br i1 %44, label %45, label %65
45: ; preds = %43
store i32 8, i32* %4
%46 = call i32 @accept(i32 %9, %struct.sockaddr* %24, i32* %4)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
%53 = load i32, i32* @INET_ADDRSTRLEN
%54 = zext i32 %53 to i64
%55 = call i8* @llvm.stacksave()
%56 = alloca i8, i64 %54
%57 = load i32, i32* @AF_INET
%58 = load i32, i32* @INET_ADDRSTRLEN
%59 = call i32 @inet_ntop(i32 %57, i32* %25, i8* %56, i32 %58)
%60 = load i32, i32* @stderr
%61 = load i32, i32* %26
%62 = call i32 @ntohs(i32 %61)
%63 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %56, i32 %62)
%64 = call i32 @FD_SET(i32 %46, i32* %1)
br label %71
65: ; preds = %43
%66 = call i64 @read_from_client(i32 %40)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = call i32 @close(i32 %40)
%70 = call i32 @FD_CLR(i32 %40, i32* %1)
br label %71
71: ; preds = %52, %68, %65, %39
%72 = add nuw nsw i32 %40, 1
%73 = load i32, i32* @FD_SETSIZE
%74 = icmp slt i32 %72, %73
br i1 %74, label %39, label %27
}