#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 */
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ active_fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int handle_readfd(char **, int);
int /*<<< orphan*/ handle_writefd(char *, int, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ shutdown_socket(int, int /*<<< orphan*/ *);
int run_server(int srv_socket) {
  int max_fd = -1;
  int act = -1;
  int i = 0;
  int dst_socket = -1;
  if (srv_socket < 0) {
    printf("Invalid listening socket!\n");
    return -1;
  }
  fd_set read_fd_set, active_fd_set;
  FD_ZERO(&read_fd_set);
  FD_ZERO(&active_fd_set);
  FD_SET(srv_socket, &active_fd_set);
  if (srv_socket > max_fd) {
    max_fd = srv_socket;
  }
  while (1) {
    memcpy(&read_fd_set, &active_fd_set, sizeof(active_fd_set));
    act = select(max_fd + 1, &read_fd_set, NULL, NULL, NULL);
    if (act < 0) {
      perror("select() failed");
      return -1;
    }
    for (i = 0; i <= max_fd; i++) {
      if (FD_ISSET(i, &read_fd_set)) {
        if (i == srv_socket) {
          dst_socket = accept(srv_socket, NULL, NULL);
          if (dst_socket == -1) {
            perror("accept() failed");
            continue;
          } else {
            FD_SET(dst_socket, &active_fd_set);
            if (dst_socket > max_fd) {
              max_fd = dst_socket;
            }
            printf("(Socket %d): Connection accepted.\n", dst_socket);
          }
        } else {
          dst_socket = i;
          char *message = NULL;
          int n = handle_readfd(&message, dst_socket);
          handle_writefd(message, dst_socket, srv_socket, max_fd,
                         &active_fd_set);
          if (n == 0) {
            printf("(Socket %d): Connection closed.\n", dst_socket);
            shutdown_socket(dst_socket, &active_fd_set);
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fb <run_server+0x4b>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <run_server+0x1e3>
lea    -0x1c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
lea    -0x20(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    122b <run_server+0x7b>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1230 <run_server+0x80>
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1281 <run_server+0xd1>
lea    0xdab(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <run_server+0x1e3>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     138e <run_server+0x1de>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     137b <run_server+0x1cb>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1317 <run_server+0x167>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12e3 <run_server+0x133>
lea    0xd52(%rip),%rdi
call   10b0 <perror@plt>
jmp    1380 <run_server+0x1d0>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1301 <run_server+0x151>
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1376 <run_server+0x1c6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1030 <handle_readfd@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    -0x20(%rbp),%r8
call   10a0 <handle_writefd@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1371 <run_server+0x1c1>
mov    -0x18(%rbp),%esi
lea    0xd00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <shutdown_socket@plt>
jmp    1376 <run_server+0x1c6>
jmp    137b <run_server+0x1cb>
jmp    1380 <run_server+0x1d0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1288 <run_server+0xd8>
jmp    1230 <run_server+0x80>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
jns    11f2 <run_server+0x32>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %edi,%r12d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
lea    0x1(%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
jns    1250 <run_server+0x90>
lea    0xdb7(%rip),%rdi
call   10c0 <perror@plt>
jmp    11de <run_server+0x1e>
lea    0xc(%rsp),%r14
lea    0x8(%rsp),%r15
mov    %r12d,%ebx
jmp    1286 <run_server+0xc6>
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
lea    0x1(%rbx),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
js     1242 <run_server+0x82>
test   %ebx,%ebx
js     1260 <run_server+0xa0>
xor    %r13d,%r13d
jmp    12dc <run_server+0x11c>
nop
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    %r13d,%esi
call   1030 <handle_readfd@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %r15,%r8
call   10b0 <handle_writefd@plt>
test   %ebp,%ebp
je     1325 <run_server+0x165>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%r13),%eax
cmp    %ebx,%r13d
mov    %eax,%r13d
jge    1260 <run_server+0xa0>
mov    %r13d,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12d0 <run_server+0x110>
cmp    %r13d,%r12d
jne    1290 <run_server+0xd0>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1343 <run_server+0x183>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
call   10e0 <FD_SET@plt>
cmp    %ebx,%ebp
cmovg  %ebp,%ebx
lea    0xd06(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12d0 <run_server+0x110>
lea    0xd17(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1070 <shutdown_socket@plt>
jmp    12d0 <run_server+0x110>
lea    0xcc6(%rip),%rdi
call   10c0 <perror@plt>
jmp    12d0 <run_server+0x110>

LLVM IR (-O0):
@.str = [27 x i8] c"Invalid listening socket!\0A\00"
@.str.1 = [16 x i8] c"select() failed\00"
@.str.2 = [16 x i8] c"accept() failed\00"
@.str.3 = [35 x i8] c"(Socket %d): Connection accepted.\0A\00"
@.str.4 = [33 x i8] c"(Socket %d): Connection closed.\0A\00"
define i32 @run_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %4
store i32 -1, i32* %5
store i32 0, i32* %6
store i32 -1, i32* %7
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %91
16: ; preds = %1
%17 = call i32 @FD_ZERO(i32* %8)
%18 = call i32 @FD_ZERO(i32* %9)
%19 = load i32, i32* %3
%20 = call i32 @FD_SET(i32 %19, i32* %9)
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %3
store i32 %25, i32* %4
br label %26
26: ; preds = %24, %16
br label %27
27: ; preds = %26, %90
%28 = call i32 @memcpy(i32* %8, i32* %9, i32 4)
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = call i32 @select(i32 %30, i32* %8, i32* null, i32* null, i32* null)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %91
36: ; preds = %27
store i32 0, i32* %6
br label %37
37: ; preds = %87, %36
%38 = load i32, i32* %6
%39 = load i32, i32* %4
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %90
41: ; preds = %37
%42 = load i32, i32* %6
%43 = call i64 @FD_ISSET(i32 %42, i32* %8)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %86
45: ; preds = %41
%46 = load i32, i32* %6
%47 = load i32, i32* %3
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %68
49: ; preds = %45
%50 = load i32, i32* %3
%51 = call i32 @accept(i32 %50, i32* null, i32* null)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %87
56: ; preds = %49
%57 = load i32, i32* %7
%58 = call i32 @FD_SET(i32 %57, i32* %9)
%59 = load i32, i32* %7
%60 = load i32, i32* %4
%61 = icmp sgt i32 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = load i32, i32* %7
store i32 %63, i32* %4
br label %64
64: ; preds = %62, %56
%65 = load i32, i32* %7
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %64
br label %85
68: ; preds = %45
%69 = load i32, i32* %6
store i32 %69, i32* %7
store i8* null, i8** %10
%70 = load i32, i32* %7
%71 = call i32 @handle_readfd(i8** %10, i32 %70)
store i32 %71, i32* %11
%72 = load i8*, i8** %10
%73 = load i32, i32* %7
%74 = load i32, i32* %3
%75 = load i32, i32* %4
%76 = call i32 @handle_writefd(i8* %72, i32 %73, i32 %74, i32 %75, i32* %9)
%77 = load i32, i32* %11
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %84
79: ; preds = %68
%80 = load i32, i32* %7
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %80)
%82 = load i32, i32* %7
%83 = call i32 @shutdown_socket(i32 %82, i32* %9)
br label %84
84: ; preds = %79, %68
br label %85
85: ; preds = %84, %67
br label %86
86: ; preds = %85, %41
br label %87
87: ; preds = %86, %54
%88 = load i32, i32* %6
%89 = add nsw i32 %88, 1
store i32 %89, i32* %6
br label %37
90: ; preds = %37
br label %27
91: ; preds = %34, %14
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
@.str.1 = [16 x i8] c"select() failed\00"
@.str.2 = [16 x i8] c"accept() failed\00"
@.str.3 = [35 x i8] c"(Socket %d): Connection accepted.\0A\00"
@.str.4 = [33 x i8] c"(Socket %d): Connection closed.\0A\00"
@str = [26 x i8] c"Invalid listening socket!\00"
define i32 @run_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = icmp slt i32 %0, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %61
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = call i32 @FD_ZERO(i32* %2)
%12 = call i32 @FD_ZERO(i32* %3)
%13 = call i32 @FD_SET(i32 %0, i32* %3)
%14 = call i32 @memcpy(i32* %2, i32* %3, i32 4)
%15 = add nuw nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %2, i32* null, i32* null, i32* null)
%17 = icmp slt i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %8
%19 = bitcast i8** %4 to i8*
br label %26
20: ; preds = %57, %26
%21 = phi i32 [ %27, %26 ], [ %58, %57 ]
%22 = call i32 @memcpy(i32* %2, i32* %3, i32 4)
%23 = add nsw i32 %21, 1
%24 = call i32 @select(i32 %23, i32* %2, i32* null, i32* null, i32* null)
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %18, %20
%27 = phi i32 [ %0, %18 ], [ %21, %20 ]
%28 = icmp slt i32 %27, 0
br i1 %28, label %20, label %31
29: ; preds = %20, %8
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %61
31: ; preds = %26, %57
%32 = phi i32 [ %59, %57 ], [ 0, %26 ]
%33 = phi i32 [ %58, %57 ], [ %27, %26 ]
%34 = call i64 @FD_ISSET(i32 %32, i32* %2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %57, label %36
36: ; preds = %31
%37 = icmp eq i32 %32, %0
br i1 %37, label %38, label %48
38: ; preds = %36
%39 = call i32 @accept(i32 %0, i32* null, i32* null)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %57
43: ; preds = %38
%44 = call i32 @FD_SET(i32 %39, i32* %3)
%45 = icmp sgt i32 %39, %33
%46 = select i1 %45, i32 %39, i32 %33
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %57
48: ; preds = %36
store i8* null, i8** %4
%49 = call i32 @handle_readfd(i8** %4, i32 %32)
%50 = load i8*, i8** %4
%51 = call i32 @handle_writefd(i8* %50, i32 %32, i32 %0, i32 %33, i32* %3)
%52 = icmp eq i32 %49, 0
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%55 = call i32 @shutdown_socket(i32 %32, i32* %3)
br label %56
56: ; preds = %53, %48
br label %57
57: ; preds = %31, %56, %43, %41
%58 = phi i32 [ %33, %41 ], [ %46, %43 ], [ %33, %56 ], [ %33, %31 ]
%59 = add nuw nsw i32 %32, 1
%60 = icmp slt i32 %32, %58
br i1 %60, label %31, label %20
61: ; preds = %29, %6
ret i32 -1
}