#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ handle_connection(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ new_connection(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int listen_fd, fd;
  int yes = 1;
  int addrlen;
  struct sockaddr_in addr;
  fd_set master_fds, read_fds;
  printf("Server: init\n");
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(10001);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(listen_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("Server: bind");
    exit(1);
  }
  if (listen(listen_fd, 10) == -1) {
    perror("Server: listen");
    exit(1);
  }
  FD_ZERO(&master_fds);
  FD_SET(listen_fd, &master_fds);
  while (1) {
    read_fds = master_fds;
    if (select(15 + 1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("Server: select");
      continue;
    }
    for (fd = 0; fd <= 15; fd++) {
      if (FD_ISSET(fd, &read_fds)) {
        if (fd == listen_fd) {
          new_connection(fd, &master_fds);
        } else {
          handle_connection(fd, &master_fds);
        }
      }
    }
  }
  close(listen_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    0xdec(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x2711,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0xcc>
lea    0xd61(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e8 <main+0xf8>
lea    0xd42(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x10,%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    1336 <main+0x146>
lea    0xcfe(%rip),%rdi
call   10c0 <perror@plt>
jmp    12fd <main+0x10d>
movl   $0x0,-0x18(%rbp)
cmpl   $0xf,-0x18(%rbp)
jg     139e <main+0x1ae>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     138b <main+0x19b>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    137a <main+0x18a>
mov    -0x18(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10e0 <new_connection@plt>
jmp    1386 <main+0x196>
mov    -0x18(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1070 <handle_connection@plt>
jmp    138b <main+0x19b>
jmp    1390 <main+0x1a0>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    133d <main+0x14d>
jmp    12fd <main+0x10d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movl   $0x1,0xc(%rsp)
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc9(%rip),%r14
mov    (%r14),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x2711,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     1341 <main+0x151>
mov    %ebx,%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     134a <main+0x15a>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1100 <FD_SET@plt>
lea    0x4(%rsp),%r15
lea    0xd48(%rip),%r12
jmp    12e0 <main+0xf0>
mov    %r12,%rdi
call   10c0 <perror@plt>
xchg   %ax,%ax
mov    0x8(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    $0x10,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0xe6>
xor    %ebp,%ebp
jmp    1322 <main+0x132>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <handle_connection@plt>
add    $0x1,%ebp
cmp    $0x10,%ebp
je     12e0 <main+0xf0>
mov    %ebp,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     131a <main+0x12a>
cmp    %ebp,%ebx
jne    1310 <main+0x120>
mov    %ebx,%edi
mov    %r14,%rsi
call   10e0 <new_connection@plt>
jmp    131a <main+0x12a>
lea    0xcb8(%rip),%rdi
jmp    1351 <main+0x161>
lea    0xcbc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"Server: init\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"Server: bind\00"
@.str.2 = [15 x i8] c"Server: listen\00"
@.str.3 = [15 x i8] c"Server: select\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%13 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, 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* %6
%17 = load i32, i32* %6
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %8, i32 4)
%21 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 10001)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %2
%37 = load i32, i32* %6
%38 = call i32 @listen(i32 %37, i32 10)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = call i32 @FD_ZERO(i32* %11)
%45 = load i32, i32* %6
%46 = call i32 @FD_SET(i32 %45, i32* %11)
br label %47
47: ; preds = %43, %51, %76
%48 = load i32, i32* %11
store i32 %48, i32* %12
%49 = call i32 @select(i32 16, i32* %12, i32* null, i32* null, i32* null)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %47
53: ; preds = %47
store i32 0, i32* %7
br label %54
54: ; preds = %73, %53
%55 = load i32, i32* %7
%56 = icmp sle i32 %55, 15
br i1 %56, label %57, label %76
57: ; preds = %54
%58 = load i32, i32* %7
%59 = call i64 @FD_ISSET(i32 %58, i32* %12)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %72
61: ; preds = %57
%62 = load i32, i32* %7
%63 = load i32, i32* %6
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32, i32* %7
%67 = call i32 @new_connection(i32 %66, i32* %11)
br label %71
68: ; preds = %61
%69 = load i32, i32* %7
%70 = call i32 @handle_connection(i32 %69, i32* %11)
br label %71
71: ; preds = %68, %65
br label %72
72: ; preds = %71, %57
br label %73
73: ; preds = %72
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
store i32 %75, i32* %7
br label %54
76: ; preds = %54
br label %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"Server: bind\00"
@.str.2 = [15 x i8] c"Server: listen\00"
@.str.3 = [15 x i8] c"Server: select\00"
@str = [13 x i8] c"Server: init\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %3, i32 4)
%18 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 10001)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %14, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %2
%32 = call i32 @listen(i32 %14, i32 10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = call i32 @FD_ZERO(i32* %5)
%39 = call i32 @FD_SET(i32 %14, i32* %5)
br label %40
40: ; preds = %46, %37
%41 = load i32, i32* %5
store i32 %41, i32* %6
%42 = call i32 @select(i32 16, i32* %6, i32* null, i32* null, i32* null)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %57, %44
br label %40
47: ; preds = %40, %57
%48 = phi i32 [ %58, %57 ], [ 0, %40 ]
%49 = call i64 @FD_ISSET(i32 %48, i32* %6)
%50 = icmp eq i64 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %47
%52 = icmp eq i32 %48, %14
br i1 %52, label %53, label %55
53: ; preds = %51
%54 = call i32 @new_connection(i32 %14, i32* %5)
br label %57
55: ; preds = %51
%56 = call i32 @handle_connection(i32 %48, i32* %5)
br label %57
57: ; preds = %47, %55, %53
%58 = add nuw nsw i32 %48, 1
%59 = icmp eq i32 %58, 16
br i1 %59, label %46, label %47
}